Skip to content

Commit bedc451

Browse files
committed
Use cryptsetup --link-vk-to-keyring to save volume keys
cryptsetup open --link-vk-to-keyring (man cryptsetup-open) will link volume key to specified keyring after successfully unlocking the volume. Use this feature to save key to @U::%logon:cryptsetup:$UUID to support the following cases - volume is unlocked automatically say using TPM-sealed key - ask user to input passphrase to unlock the volume Signed-off-by: Coiby Xu <[email protected]>
1 parent d9677e1 commit bedc451

File tree

1 file changed

+44
-4
lines changed

1 file changed

+44
-4
lines changed

kdumpctl

Lines changed: 44 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1056,9 +1056,42 @@ check_final_action_config()
10561056
esac
10571057
}
10581058

1059+
_get_luks_key_by_unlock()
1060+
{
1061+
local _devuuid=$1 _key_des=$2
1062+
local _max_retries
1063+
local _attempt=1
1064+
1065+
local _luks_unlock_cmd=""
1066+
1067+
# Check if stdin is a terminal.
1068+
if [ -t 0 ]; then
1069+
_max_retries=5
1070+
ddebug "Attempting to unlock LUKS device. You have $_max_retries attempts."
1071+
else
1072+
# Not a terminal (e.g., running as system service), so only try once
1073+
# for cases where volume key is sealed to TPM which doesn't need user
1074+
# interaction.
1075+
_max_retries=1
1076+
ddebug "Attempting to unlock LUKS device (non-interactive mode)..."
1077+
fi
1078+
1079+
while [ "$_attempt" -le "$_max_retries" ]; do
1080+
if cryptsetup open "UUID=$_devuuid" DUMMY "--link-vk-to-keyring=@u::%logon:$_key_des" --test-passphrase; then
1081+
ddebug "Success: LUKS device unlocked."
1082+
dwarn "To avoid manually running kdumpctl, ensure the link-volume-key=@u::%logon:$_key_des option is correctly set up in /etc/crypttab (see man crypttab)."
1083+
return 0
1084+
fi
1085+
_attempt=$((_attempt + 1))
1086+
done
1087+
1088+
derror "Error: Could not unlock the LUKS device."
1089+
return 1
1090+
}
1091+
10591092
prepare_luks()
10601093
{
1061-
local _luks_dev _key_id _key_des
1094+
local _luks_dev _key_id _key_des _luks_unlock_cmd
10621095
declare -a _luks_devs
10631096

10641097
mapfile -t _luks_devs < <(get_all_kdump_crypt_dev)
@@ -1077,12 +1110,19 @@ prepare_luks()
10771110
_key_dir=$LUKS_CONFIGFS/$_devuuid
10781111
_key_des=$LUKS_KEY_PRFIX$_devuuid
10791112
if _key_id=$(keyctl request logon "$_key_des" 2> /dev/null); then
1080-
mkdir "$_key_dir"
1081-
printf "%s" "$_key_des" > "$_key_dir"/description
1113+
ddebug "Succesfully get @u::%logon:$_key_des"
1114+
elif _get_luks_key_by_unlock "$_devuuid" "$_key_des"; then
1115+
_key_id=$(keyctl request logon "$_key_des")
1116+
ddebug "Succesfully get @u::%logon:$_key_des after cryptsetup"
10821117
else
1083-
derror "Failed to get logon key $_key_des. Ensure the link-volume-key option is correctly set up in /etc/crypttab (see man crypttab) and that the key is available."
1118+
derror "Failed to get logon key $_key_des. Run 'kdumpctl restart' manually to start kdump."
10841119
return 1
10851120
fi
1121+
1122+
# Let the key expire after 300 seconds
1123+
keyctl timeout "$_key_id" 300
1124+
mkdir "$_key_dir"
1125+
printf "%s" "$_key_des" > "$_key_dir"/description
10861126
done
10871127
}
10881128

0 commit comments

Comments
 (0)