From 33421374d303764a91093ba54a8aeb75f454b92c Mon Sep 17 00:00:00 2001 From: Tau Date: Sat, 13 Apr 2024 14:41:01 +0200 Subject: [PATCH] fix: Properly escape LUKS password --- core/disk/luks/luks.go | 8 ++++---- core/util/util.go | 5 ++++- 2 files changed, 8 insertions(+), 5 deletions(-) diff --git a/core/disk/luks/luks.go b/core/disk/luks/luks.go index 56cc713d..f474b0cc 100644 --- a/core/disk/luks/luks.go +++ b/core/disk/luks/luks.go @@ -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) } @@ -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) } diff --git a/core/util/util.go b/core/util/util.go index 40ccd4ec..a416db39 100644 --- a/core/util/util.go +++ b/core/util/util.go @@ -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