Skip to content

Commit

Permalink
initrdscripts: cryptsetup: have flasher remove pre-installed LUKS key
Browse files Browse the repository at this point in the history
If the flasher is being used on a pre-installed device, make sure the
encrypted data will be protected by removing the key.

This is a fail-safe to avoid flasher exploits.

Changelog-entry: have flasher remove pre-installed LUKS key
Signed-off-by: Alex Gonzalez <alexg@balena.io>
  • Loading branch information
alexgg committed Jun 6, 2024
1 parent 1b82704 commit 3199946
Showing 1 changed file with 19 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,27 @@ wait4file "/dev/disk/by-state" "50"
BALENA_NONENC_BOOT_DEV=""

cryptsetup_enabled() {
BALENA_NONENC_BOOT_DEV=$(get_state_path_from_label @@BALENA_NONENC_BOOT_LABEL@@)
# Only run if the boot partition is split
if [ ! -e "$BALENA_NONENC_BOOT_DEV" ]; then
return 1
fi

# Flasher should not try to unlock the partitions
if [ "$bootparam_flasher" = "true" ]; then
# Remove the encrypted key to guarantee flasher cannot mount pre-existing partitions
_tmpdir=$(mktemp -d)
if mount "$BALENA_NONENC_BOOT_DEV" "$_tmpdir"; then
if [ -f "${_tmpdir}/balena-luks.enc" ]; then
if ! rm "${_tmpdir}/balena-luks.enc"; then
fail "Failure to remove pre-installed LUKS key - aborting boot"
fi
sync
info "Removed pre-installed LUKS key."
fi
else
fail "Failure to mount $BALENA_NONENC_BOOT_DEV - aborting boot"
fi
return 1
fi

Expand All @@ -26,11 +45,6 @@ cryptsetup_enabled() {
fail "Device not locked - boot aborted"
fi

BALENA_NONENC_BOOT_DEV=$(get_state_path_from_label @@BALENA_NONENC_BOOT_LABEL@@)
# Only run if the boot partition is split
if [ ! -e "$BALENA_NONENC_BOOT_DEV" ]; then
return 1
fi

# Check whether there are any LUKS partitions
if ! lsblk -nlo fstype | grep -q crypto_LUKS; then
Expand Down

0 comments on commit 3199946

Please sign in to comment.