From 242f2412bb14bede02ad0ebb1bab9328160fa0fc Mon Sep 17 00:00:00 2001 From: Scott Jones Date: Fri, 9 Oct 2026 21:38:13 -0400 Subject: [PATCH 1/2] Confirm a resumed drive password change's new slot before removing a key The rerun after an interrupted change treated any one slot added since as the change's new key. If the change stopped before luksChangeKey and a recovery key was enrolled before the rerun, entering the old password removed the recovery key, and entering the recovery key removed the old password's slot. luksChangeKey picks the new slot itself, so the journal can't name it, and LUKS2 leaves both keys between its two header updates. With both slots on the disk the rerun now asks for the new password, and the old one when it has only the new, and removes a slot only once the new password opens the added slot and the old password the old one. Otherwise it removes nothing and says how to settle the disk by hand. Found by Greptile on #14675. Co-Authored-By: Claude Opus 5.5 (1M context) --- bin/omarchy-drive-password | 80 ++++++++++++++++++++++++----- test/shell.d/drive-password-test.sh | 61 ++++++++++++++++++++-- 2 files changed, 124 insertions(+), 17 deletions(-) diff --git a/bin/omarchy-drive-password b/bin/omarchy-drive-password index e1d9dc74f72f..47648d405732 100755 --- a/bin/omarchy-drive-password +++ b/bin/omarchy-drive-password @@ -86,10 +86,18 @@ kill_slot() { printf '%s' "$password" | sudo flock "$disk_lock" cryptsetup luksKillSlot -q --key-file - "$drive" "$slot" } -# luksChangeKey writes the new key before it retires the old one (LUKS2 swaps -# them in one header update), so a failure that leaves the new key unable to -# open the drive changed nothing: that returns 2. One that stops in between -# leaves both keys, so the old key is retired here until it opens nothing. +# Whether the password opens that one key slot. Returns 2 when it doesn't, and +# another failure when the drive cannot be checked. +slot_opens() { + local drive=$1 password=$2 slot=$3 + printf '%s' "$password" | sudo flock "$disk_lock" cryptsetup open --test-passphrase --token-type passphrase-only --key-slot "$slot" --key-file - "$drive" >/dev/null 2>&1 +} + +# luksChangeKey writes the new key before it retires the old one, so a failure +# that leaves the new key unable to open the drive changed nothing: that +# returns 2. LUKS2 commits the new key to a free slot, then moves it over the +# old slot in a second header update; one that stops in between leaves both +# keys, so the old key is retired here until it opens nothing. # Sets new_slot. change_luks_key() { local drive=$1 current=$2 new=$3 old_slot=$4 remains attempt status @@ -238,12 +246,56 @@ change_pending() { return 1 } +# The disk holds the old password's slot and one slot added since the change +# began: luksChangeKey stopped between its two header updates, or another key +# (a recovery key) was enrolled in the slot it would have used. luksChangeKey +# picks that slot itself, so the journal can't name it, and nothing is removed +# until the new password opens the added slot and the old password the old one. +# Otherwise says how to settle the disk by hand. The user keeps a key they just +# typed either way; the journal has no check value for the new password, so a +# key typed in as the new one is taken as the new one. +confirm_added_slot() { + local password=$1 opened=$2 old_slot=$3 added=$4 new old=$1 status=0 + + echo "$root_drive has the old password's key slot $old_slot and key slot $added, added since the change began. Slot $added may hold the new password, or another key enrolled since, such as a recovery key." + new=$(gum input --password --header "New encryption password you chose for this change") || return 1 + if [[ $opened != "$old_slot" ]]; then + old=$(gum input --password --header "Old encryption password, from before this change") || return 1 + fi + slot_opens "$root_drive" "$new" "$added" || status=$? + if (( status == 0 )); then + slot_opens "$root_drive" "$old" "$old_slot" || status=$? + (( status != 0 )) || return 0 + # luksChangeKey stopped after rewriting the old slot's key and before its + # last header update: the slot is still listed, but no password opens it. + if (( status == 2 )); then + echo "The new password opens key slot $added, but the old password doesn't open slot $old_slot, so no key is removed." + echo "If you typed the old password correctly, slot $old_slot no longer holds it (the change stopped while rewriting that slot), and removing it is safe: sudo cryptsetup luksKillSlot $root_drive $old_slot" + echo "Then run omarchy-drive-password again to finish." + return 1 + fi + fi + if (( status != 2 )); then + echo "Could not check the passwords against $root_drive." + return 1 + fi + + echo "Those passwords don't show that slot $added holds this change's new password, so no key is removed." + echo "Settle it by hand: keep the slot of the password you want (sudo cryptsetup open --test-passphrase --token-type passphrase-only --verbose $root_drive names it), and remove only a slot you know holds a password you no longer use (sudo cryptsetup luksKillSlot $root_drive )." + if [[ -n $(slot_recorder 2>/dev/null) ]]; then + echo "Record the slots you kept for the boot checks: sudo omarchy-lifecycle-dispatch luks-slots owner= recovery=, with an empty recovery= when no recovery key is left." + fi + echo "Then remove $REKEY_STATE, and run omarchy-drive-password again if the disk should still take a new password." + return 1 +} + # Settle the disk on the password it opens with now, then record its slot. # Before the change was confirmed that is the old key (removing the slot the # change added, if any) or the new key in the one slot the change added -# (retiring the old slot); after, only the confirmed slot. Any other key on the -# disk, such as a recovery passphrase, is refused, and so is a disk that gained -# more than one slot since. +# (retiring the old slot), once confirm_added_slot shows that slot is the +# change's; after, only the confirmed slot. Any other key on the disk, such as +# a recovery passphrase, is refused, and so is a disk that gained more than one +# slot since. finish_interrupted_change() { local phase old_slot slots slot header password opened now added extra count=0 status=0 @@ -305,15 +357,15 @@ finish_interrupted_change() { echo "The key slots of $root_drive changed since the interrupted change. Finish it by hand." return 1 fi - - if [[ $opened == "$old_slot" ]]; then - [[ -z $added ]] || kill_slot "$root_drive" "$password" "$added" || return 1 - elif [[ -n $added && $opened == "$added" ]]; then - if [[ ,$now, == *",$old_slot,"* ]]; then + if [[ $opened != "$old_slot" && ( -z $added || $opened != "$added" ) ]]; then + opened="" + elif [[ -n $added && ,$now, == *",$old_slot,"* ]]; then + confirm_added_slot "$password" "$opened" "$old_slot" "$added" || return 1 + if [[ $opened == "$old_slot" ]]; then + kill_slot "$root_drive" "$password" "$added" || return 1 + else kill_slot "$root_drive" "$password" "$old_slot" || return 1 fi - else - opened="" fi [[ -z $opened ]] || rekey_state_put slot "$opened" phase record || return 1 elif [[ $phase == "record" && $opened != "$slot" ]]; then diff --git a/test/shell.d/drive-password-test.sh b/test/shell.d/drive-password-test.sh index 7479a598fcc3..a12667463112 100644 --- a/test/shell.d/drive-password-test.sh +++ b/test/shell.d/drive-password-test.sh @@ -467,9 +467,12 @@ for run_spec in "${matrix[@]}"; do [[ -e $journal ]] || fail "$backend: after '$point' a refused rerun keeps the journal" answer=$new_password fi + # A rerun that finds both keys (killed between luksChangeKey's two header + # updates) asks for the new password, and the old one when it was given + # the new, before it removes either slot. for (( again = 1; ; again++ )); do status=0 - attempt "$again" "$answer" || status=$? + attempt "$again" "$answer" "$new_password" "$old_password" || status=$? (( status == 0 )) && break (( status == 137 )) || fail "$backend: the rerun after '$point' finishes" "$(cat "$tmp/output")" recoverable "rerun after '$point' killed at step $again" @@ -521,7 +524,7 @@ unset TEST_CHANGE_PARTIAL TEST_KILL_FAIL if attempt 0 "$recovery_key"; then fail "an unconfirmed change refuses the recovery key"; fi said "That password does not finish the change" [[ -e $journal && -n $(opens "$system" "$recovery_key") ]] || fail "refusing the recovery key changes nothing" -attempt 0 "$new_password" || fail "the rerun retires the old key and finishes" "$(cat "$tmp/output")" +attempt 0 "$new_password" "$new_password" "$old_password" || fail "the rerun retires the old key and finishes" "$(cat "$tmp/output")" consistent "rerun after a failed retirement" "$new_password" pass "before the change is confirmed a rerun refuses the recovery key and retires the old key" @@ -529,10 +532,26 @@ fixture export TEST_CHANGE_PARTIAL=1 TEST_KILL_FAIL=1 attempt 0 "$old_password" "$new_password" "$new_password" || true unset TEST_CHANGE_PARTIAL TEST_KILL_FAIL -attempt 0 "$old_password" || fail "the rerun with the old password rolls back" "$(cat "$tmp/output")" +attempt 0 "$old_password" "$new_password" || fail "the rerun with the old password rolls back" "$(cat "$tmp/output")" consistent "rollback with the old password" "$old_password" pass "a rerun with the old password removes the half-added new key" +# With both keys on the disk, the added slot is only removed or kept once the +# new password opens it and the old password the old slot: a wrong new password +# removes nothing. +fixture +export TEST_CHANGE_PARTIAL=1 TEST_KILL_FAIL=1 +attempt 0 "$old_password" "$new_password" "$new_password" || true +unset TEST_CHANGE_PARTIAL TEST_KILL_FAIL +for answers in "$old_password not-the-new-one" "$new_password not-the-new-one $old_password" "$new_password $new_password not-the-old-one"; do + read -r -a answer_list <<<"$answers" + if attempt 0 "${answer_list[@]}"; then fail "a rerun with both keys refuses unconfirmed passwords: $answers"; fi + said "so no key is removed" + [[ -e $journal && -n $(opens "$system" "$old_password") && -n $(opens "$system" "$new_password") ]] || + fail "a rerun with unconfirmed passwords removes neither key: $answers" "$(cat "$tmp/output")" +done +pass "with both keys on the disk, a rerun removes neither until both passwords confirm the added slot" + # On Apple the slot record follows the confirmed change: a crash there leaves # the confirmed phase. use fake apple @@ -595,6 +614,42 @@ attempt 0 "$new_password" || fail "the rerun finishes after the orphaned change" consistent "orphaned luksChangeKey" "$new_password" pass "a luksChangeKey that outlives its run finishes before the rerun reads the disk" +# A change that stopped before luksChangeKey added nothing, so a key enrolled +# before the rerun (a recovery key, with cryptsetup luksAddKey) is not the +# change's, though it sits where the change's key would. Whichever password +# the rerun gets, the new password doesn't open that slot, so it removes +# neither that key nor the old password's slot. +for answer in "$old_password" later-key; do + fixture + attempt "$change_step" "$old_password" "$new_password" "$new_password" || true + [[ -e $journal && -z $(opens "$system" "$new_password") ]] || fail "the change stops before its key is added" "$(cat "$tmp/trace")" + printf '2\tlater-key\n' >>"$system.slots" + if attempt 0 "$answer" "$new_password" "$old_password"; then fail "a rerun with $answer leaves a key enrolled after the change stopped to the owner"; fi + said "Settle it by hand" + [[ -e $journal && $(opens "$system" later-key) == "2" && $(opens "$system" "$old_password") == "0" && -n $(opens "$system" "$recovery_key") ]] || + fail "a rerun with $answer removes no key" "$(cat "$tmp/sudo-calls" "$tmp/output")" + ! grep -q 'luksKillSlot' "$tmp/sudo-calls" || fail "a rerun with $answer kills no slot" "$(cat "$tmp/sudo-calls")" +done +pass "a rerun never removes a key enrolled after a change that stopped before adding its own" + +# luksChangeKey killed after rewriting the old slot's key and before its last +# header update leaves the old slot listed but opened by nothing, and the new +# key in the added slot. The rerun removes nothing and says the old slot is +# safe to remove; once the owner removes it, the rerun finishes on the new key. +fixture +attempt "$change_step" "$old_password" "$new_password" "$new_password" || true +awk -F'\t' -v OFS='\t' '$1 == 0 { $2 = "overwritten" } 1' "$system.slots" >"$system.slots.next" && mv -f "$system.slots.next" "$system.slots" +printf '2\t%s\n' "$new_password" >>"$system.slots" +if attempt 0 "$new_password" "$new_password" "$old_password"; then fail "a rerun with the old slot rewritten removes nothing"; fi +said "The new password opens key slot 2, but the old password doesn't open slot 0" +said "removing it is safe: sudo cryptsetup luksKillSlot $system 0" +! grep -q 'luksKillSlot' "$tmp/sudo-calls" && grep -q '^0'$'\t' "$system.slots" && [[ -e $journal ]] || + fail "a rerun with the old slot rewritten kills no slot and keeps the journal" "$(cat "$tmp/sudo-calls")" +awk -F'\t' '$1 != 0' "$system.slots" >"$system.slots.next" && mv -f "$system.slots.next" "$system.slots" +attempt 0 "$new_password" || fail "the rerun finishes once the rewritten slot is removed" "$(cat "$tmp/output")" +consistent "rewritten old slot removed by hand" "$new_password" +pass "a rerun with the old slot rewritten says it is safe to remove, then finishes once it is gone" + fixture printf '/dev/mapper/root crypt btrfs\n%s part \n/dev/fake-disk disk \n' "$system" >"$tmp/root-ancestry" if attempt 0 "$old_password" "$new_password" "$new_password"; then fail "a crypt layer without a named LUKS partition fails"; fi From fa3432151bb58689600b33578f11f7edf6e498d1 Mon Sep 17 00:00:00 2001 From: Scott Jones Date: Sat, 10 Oct 2026 23:04:20 -0400 Subject: [PATCH 2/2] Check each both-keys rerun case on its own, and soften the old-slot advice The test split its answers on spaces, and the new password has one, so two of its three cases stopped before the confirm step and still passed on the first case's output. Each case now keeps its answers whole, starts from empty output and prompts, checks it reached the confirm step and says its own message. When the new password opens the added slot but the old one doesn't open the old slot, that may be a typo or a slot rekeyed since, so the message now asks to check the old password before removing the slot. Co-Authored-By: Claude Opus 5.5 (1M context) --- bin/omarchy-drive-password | 11 +++++----- test/shell.d/drive-password-test.sh | 32 +++++++++++++++++++++-------- 2 files changed, 30 insertions(+), 13 deletions(-) diff --git a/bin/omarchy-drive-password b/bin/omarchy-drive-password index 47648d405732..185ce88aec59 100755 --- a/bin/omarchy-drive-password +++ b/bin/omarchy-drive-password @@ -266,12 +266,13 @@ confirm_added_slot() { if (( status == 0 )); then slot_opens "$root_drive" "$old" "$old_slot" || status=$? (( status != 0 )) || return 0 - # luksChangeKey stopped after rewriting the old slot's key and before its - # last header update: the slot is still listed, but no password opens it. + # A typo, or luksChangeKey stopped after rewriting the old slot's key and + # before its last header update (the slot is still listed, but no password + # opens it). Which one can't be told, so the owner checks before removing it. if (( status == 2 )); then - echo "The new password opens key slot $added, but the old password doesn't open slot $old_slot, so no key is removed." - echo "If you typed the old password correctly, slot $old_slot no longer holds it (the change stopped while rewriting that slot), and removing it is safe: sudo cryptsetup luksKillSlot $root_drive $old_slot" - echo "Then run omarchy-drive-password again to finish." + echo "The new password opens key slot $added, but the old password didn't open slot $old_slot, so no key is removed." + echo "Check that you typed the old password correctly, and run omarchy-drive-password again." + echo "If it still doesn't open slot $old_slot, the change may have stopped while rewriting that slot. Once you're sure slot $old_slot holds nothing you still need, remove it (sudo cryptsetup luksKillSlot $root_drive $old_slot) and run omarchy-drive-password again to finish." return 1 fi fi diff --git a/test/shell.d/drive-password-test.sh b/test/shell.d/drive-password-test.sh index a12667463112..3544d7c467b0 100644 --- a/test/shell.d/drive-password-test.sh +++ b/test/shell.d/drive-password-test.sh @@ -543,12 +543,28 @@ fixture export TEST_CHANGE_PARTIAL=1 TEST_KILL_FAIL=1 attempt 0 "$old_password" "$new_password" "$new_password" || true unset TEST_CHANGE_PARTIAL TEST_KILL_FAIL -for answers in "$old_password not-the-new-one" "$new_password not-the-new-one $old_password" "$new_password $new_password not-the-old-one"; do - read -r -a answer_list <<<"$answers" - if attempt 0 "${answer_list[@]}"; then fail "a rerun with both keys refuses unconfirmed passwords: $answers"; fi - said "so no key is removed" +# Each case's answers stay whole (the new password has a space in it), and +# each case is judged on its own output and prompts. +for c in 1 2 3; do + case $c in + 1) answers=("$old_password" not-the-new-one) ;; + 2) answers=("$new_password" not-the-new-one "$old_password") ;; + 3) answers=("$new_password" "$new_password" not-the-old-one) ;; + esac + : >"$tmp/output" + : >"$tmp/prompts" + if attempt 0 "${answers[@]}"; then fail "a rerun with both keys refuses unconfirmed passwords: case $c"; fi + grep -q 'New encryption password you chose for this change' "$tmp/prompts" || + fail "a rerun with both keys asks to confirm the added slot: case $c" "$(cat "$tmp/prompts" "$tmp/output")" + if (( c == 3 )); then + said "The new password opens key slot 2, but the old password didn't open slot 0, so no key is removed." + said "Check that you typed the old password correctly" + else + said "Those passwords don't show that slot 2 holds this change's new password, so no key is removed." + said "Settle it by hand" + fi [[ -e $journal && -n $(opens "$system" "$old_password") && -n $(opens "$system" "$new_password") ]] || - fail "a rerun with unconfirmed passwords removes neither key: $answers" "$(cat "$tmp/output")" + fail "a rerun with unconfirmed passwords removes neither key: case $c" "$(cat "$tmp/output")" done pass "with both keys on the disk, a rerun removes neither until both passwords confirm the added slot" @@ -641,14 +657,14 @@ attempt "$change_step" "$old_password" "$new_password" "$new_password" || true awk -F'\t' -v OFS='\t' '$1 == 0 { $2 = "overwritten" } 1' "$system.slots" >"$system.slots.next" && mv -f "$system.slots.next" "$system.slots" printf '2\t%s\n' "$new_password" >>"$system.slots" if attempt 0 "$new_password" "$new_password" "$old_password"; then fail "a rerun with the old slot rewritten removes nothing"; fi -said "The new password opens key slot 2, but the old password doesn't open slot 0" -said "removing it is safe: sudo cryptsetup luksKillSlot $system 0" +said "The new password opens key slot 2, but the old password didn't open slot 0" +said "remove it (sudo cryptsetup luksKillSlot $system 0)" ! grep -q 'luksKillSlot' "$tmp/sudo-calls" && grep -q '^0'$'\t' "$system.slots" && [[ -e $journal ]] || fail "a rerun with the old slot rewritten kills no slot and keeps the journal" "$(cat "$tmp/sudo-calls")" awk -F'\t' '$1 != 0' "$system.slots" >"$system.slots.next" && mv -f "$system.slots.next" "$system.slots" attempt 0 "$new_password" || fail "the rerun finishes once the rewritten slot is removed" "$(cat "$tmp/output")" consistent "rewritten old slot removed by hand" "$new_password" -pass "a rerun with the old slot rewritten says it is safe to remove, then finishes once it is gone" +pass "a rerun with the old slot rewritten says how to check it before removing it, then finishes once it is gone" fixture printf '/dev/mapper/root crypt btrfs\n%s part \n/dev/fake-disk disk \n' "$system" >"$tmp/root-ancestry"