Skip to content

Commit

Permalink
fix: Properly escape LUKS password
Browse files Browse the repository at this point in the history
  • Loading branch information
taukakao authored Apr 13, 2024
1 parent f3cfe62 commit 3342137
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 5 deletions.
8 changes: 4 additions & 4 deletions core/disk/luks/luks.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,14 +45,14 @@ func IsLuks(part Partition) (bool, error) {
func LuksOpen(part Partition, mapping, password string) error {
var luksOpenCmd string
if password != "" {
luksOpenCmd = fmt.Sprintf("echo '%s' | ", password)
luksOpenCmd = "printf \"%%s\" \"$LUKSPASS\" | "
} else {
luksOpenCmd = ""
}

luksOpenCmd += "cryptsetup open %s %s"

err := util.RunCommand(fmt.Sprintf(luksOpenCmd, part.GetPath(), mapping))
err := util.RunCommand(fmt.Sprintf(luksOpenCmd, part.GetPath(), mapping), "LUKSPASS="+password)
if err != nil {
return fmt.Errorf("failed to open LUKS-encrypted partition: %s", err)
}
Expand Down Expand Up @@ -91,9 +91,9 @@ func LuksClose(mapping string) error {
}

func LuksFormat(part Partition, password string) error {
luksFormatCmd := "echo '%s' | cryptsetup -q luksFormat %s"
luksFormatCmd := "printf \"%%s\" \"$LUKSPASS\" | cryptsetup -q luksFormat %s"

err := util.RunCommand(fmt.Sprintf(luksFormatCmd, password, part.GetPath()))
err := util.RunCommand(fmt.Sprintf(luksFormatCmd, part.GetPath()), "LUKSPASS="+password)
if err != nil {
return fmt.Errorf("failed to create LUKS-encrypted partition: %s", err)
}
Expand Down
5 changes: 4 additions & 1 deletion core/util/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,13 @@ import (
)

// RunCommand executes a command in a subshell
func RunCommand(command string) error {
//
// envVars are environement variables in the form MYVAR=myvalue that will be passed to the command
func RunCommand(command string, envVars ...string) error {
stderr := new(bytes.Buffer)

cmd := exec.Command("sh", "-c", command)
cmd.Env = append(os.Environ(), envVars...)
cmd.Stdout = os.Stdout
cmd.Stderr = stderr

Expand Down

0 comments on commit 3342137

Please sign in to comment.