From 4084b6c7bd495bbe9be8fc0dc38939d68aaf829e Mon Sep 17 00:00:00 2001 From: Titouan Heyrendt Date: Thu, 17 Sep 2026 09:07:08 +0200 Subject: [PATCH] luks: retry the interactive password prompt on mismatch --- lib/types/luks.nix | 8 +++- tests/luks-password-mismatch.nix | 79 ++++++++++++++++++++++++++++++++ 2 files changed, 86 insertions(+), 1 deletion(-) create mode 100644 tests/luks-password-mismatch.nix diff --git a/lib/types/luks.nix b/lib/types/luks.nix index 9fc0b02..d433b9c 100644 --- a/lib/types/luks.nix +++ b/lib/types/luks.nix @@ -218,14 +218,20 @@ in export password # shellcheck disable=SC2154 if [ "$password" != "$password_check" ]; then - exit 1 + return 1 fi set -x else export password=disko fi } + attempts=0 until askPassword; do + attempts=$((attempts + 1)) + if [ "$attempts" -ge 3 ]; then + echo "Too many mismatched password attempts for ${config.device}, aborting." >&2 + exit 1 + fi echo "Passwords did not match, please try again." done ''} diff --git a/tests/luks-password-mismatch.nix b/tests/luks-password-mismatch.nix new file mode 100644 index 0000000..9b376c9 --- /dev/null +++ b/tests/luks-password-mismatch.nix @@ -0,0 +1,79 @@ +# A password mismatch during interactive LUKS formatting must let the +# user retry, not abort the whole format script. Retries are capped, and +# both the retry and the final failure report a clear message instead of +# failing silently. +{ + pkgs ? import { }, + ... +}: +let + lib = pkgs.lib; + disko = import ../. { + inherit lib; + checked = true; + }; + + mkDiskoConfig = device: { + disko.devices.disk.main = { + type = "disk"; + inherit device; + content = { + type = "gpt"; + partitions.luks = { + size = "100%"; + content = { + type = "luks"; + name = "cryptroot-${builtins.baseNameOf device}"; + content = { + type = "filesystem"; + format = "ext4"; + mountpoint = "/"; + }; + }; + }; + }; + }; + }; + + formatRetry = disko._cliFormat (mkDiskoConfig "/dev/vdb") pkgs; + formatGiveUp = disko._cliFormat (mkDiskoConfig "/dev/vdc") pkgs; +in +pkgs.testers.nixosTest { + name = "luks-password-mismatch"; + + nodes.machine = { + virtualisation.emptyDiskImages = [ + 4096 + 4096 + ]; + environment.systemPackages = [ pkgs.cryptsetup ]; + }; + + testScript = '' + machine.start() + machine.wait_for_unit("multi-user.target") + + # A mismatched password pair must not abort formatting: the retry loop + # should prompt again, say so, and succeed once a matching pair is + # entered. + machine.succeed( + "printf 'wrong\\nnotwrong\\nsecretsecret\\nsecretsecret\\n'" + " | ${lib.getExe formatRetry} > /tmp/retry.log 2>&1" + ) + machine.succeed( + "grep -qF 'Passwords did not match, please try again.' /tmp/retry.log" + ) + machine.succeed("cryptsetup isLuks /dev/vdb1") + + # Three mismatched pairs in a row must give up with a clear error + # instead of prompting forever or failing silently. + machine.fail( + "printf 'a\\nb\\nc\\nd\\ne\\nf\\n'" + " | ${lib.getExe formatGiveUp} > /tmp/giveup.log 2>&1" + ) + machine.succeed( + "grep -qF 'Too many mismatched password attempts' /tmp/giveup.log" + ) + machine.fail("cryptsetup isLuks /dev/vdc1") + ''; +}