Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
80 changes: 66 additions & 14 deletions bin/omarchy-drive-password
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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"

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit, not blocking: a failed old-password check can't tell us why the slot stopped opening. It may be a typo, or a slot rekeyed by hand since. The new password was just confirmed, so following this can't lock anyone out, but I'd soften it. Say the new password opens slot $added, ask them to check they typed the old password, and only then remove slot $old_slot if it holds nothing they still need.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Agreed, softened in fa34321. It now says the new password opens slot $added but the old one didn't open slot $old_slot, asks to check the old password was typed right and run it again, and only then to remove slot $old_slot once they're sure it holds nothing they still need.

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 <slot>)."
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=<slot> recovery=<slot>, 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

Expand Down Expand Up @@ -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
Expand Down
61 changes: 58 additions & 3 deletions test/shell.d/drive-password-test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -521,18 +524,34 @@ 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"

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"

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

new_password is 'new pass:word', so read -a splits it at the space. In the second and third cases the first answer becomes new, which opens nothing, and the run stops at "That password does not finish the change" before it reaches confirm_added_slot. said "so no key is removed" still passes because attempt appends to $tmp/output and the first case's text is still there.

Could you pass each case as an array and clear the output before each attempt? Something like:

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"
  if attempt 0 "${answers[@]}"; then fail "a rerun with both keys refuses unconfirmed passwords: case $c"; fi
  ...

I ran it that way: all three refuse and remove nothing (the first two say "Settle it by hand", the third gives the rewritten-slot message), so the script is fine. Only the test needs it.

@scottjones scottjones Oct 11, 2026 •

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good catch, thanks. Fixed in fa34321: each case now passes its answers as an array, clears the output and prompts before its attempt, checks it reached the "New encryption password you chose" prompt, and asserts its own message ("Settle it by hand" for 1 and 2, the old-slot message for 3). With confirm_added_slot bypassed, both the old block and the new one fail at case 1, since case 1's answers never had a space. Breaking only the case 2 check or only the case 3 check is where it shows: the old block still passed (only the later-key and old-slot-rewritten tests caught it), and the new one fails at case 2 or case 3.

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
Expand Down Expand Up @@ -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
Expand Down