Skip to content

Commit d208103

Browse files
committed
Make cache_key aware of mountpoints on key sources
When attempting to unlock a BE using keys from another filesystem named as a key source via org.zfsbootmenu:keysource, attempt to discover and strip the expected mountpoint from the key source when looking for keys. This allows, for example, zfs create -o mountpoint=/etc/zfs/keys zpool/keys zfs set org.zfsbootmenu:keysource=zpool/keys \ keylocation=file:///etc/zfs/keys/zpool.key zpool/ROOT/bootenv to work with ZFSBootMenu. Previously, the cache_key function would only properly identify keys when the key source had a mountpoint of /. When mountpoint=none or mountpoint=legacy, or in the event a key does not exist at the new relative path, the prior behavior of looking for a path verbatim with respect to the key source is preserved.
1 parent 2d03ff5 commit d208103

File tree

1 file changed

+48
-6
lines changed

1 file changed

+48
-6
lines changed

90zfsbootmenu/zfsbootmenu-lib.sh

Lines changed: 48 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1925,7 +1925,8 @@ be_keysource() {
19251925
# returns: 0 iff a key was cached
19261926

19271927
cache_key() {
1928-
local fs keylocation keyfile keydir keysrc keycache mutex mnt ret
1928+
local fs mnt ret mutex keycache
1929+
local ksmount relkeyloc keypath keylocation keyfile keydir keysrc
19291930

19301931
fs="${1}"
19311932
if [ -z "${fs}" ]; then
@@ -1995,19 +1996,60 @@ cache_key() {
19951996
return 1
19961997
fi
19971998

1999+
relkeyloc=""
2000+
if ksmount="$(zfs get -o value -H mountpoint "${keysrc}" )"; then
2001+
case "${ksmount}" in
2002+
none|legacy)
2003+
zdebug "no discernable mountpoint for ${keysrc}, using only absolute key path"
2004+
;;
2005+
/*)
2006+
# For absolute mountpoints, strip the root
2007+
ksmount="${ksmount#/}"
2008+
2009+
# Key location relative to expected mountpoint of keysource
2010+
relkeyloc="${keyfile#${ksmount}}"
2011+
relkeyloc="${relkeyloc#/}"
2012+
2013+
zdebug "${keysrc} mounts at ${ksmount}, trying relative path ${relkeyloc}"
2014+
2015+
if [ "${relkeyloc}" = "${keyfile}" ]; then
2016+
# If location isn't different, there is no relative location
2017+
relkeyloc=""
2018+
zdebug "relative path ${relkeyloc} matches absolute, ignoring"
2019+
fi
2020+
;;
2021+
*)
2022+
zwarn "ignoring nonsense mountpoint ${ksmount} on filesystem ${keysrc}"
2023+
;;
2024+
esac
2025+
fi
2026+
2027+
if [ -n "${relkeyloc}" ] && [ -e "${mnt}/${relkeyloc}" ]; then
2028+
# Prefer a path relative to the expected mountpoint of the keysource
2029+
keypath="${mnt}/${relkeyloc}"
2030+
zdebug "caching key from mount-relative path ${keysrc}:${relkeyloc}"
2031+
elif [ -e "${mnt}/${keyfile}" ]; then
2032+
keypath="${mnt}/${keyfile}"
2033+
zdebug "caching key from absolute path ${keysrc}:${keyfile}"
2034+
else
2035+
keypath=""
2036+
zdebug "no key found at ${keysrc}:${keyfile}"
2037+
fi
2038+
19982039
ret=1
1999-
if [ -e "${mnt}/${keyfile}" ]; then
2040+
if [ -n "${keypath}" ]; then
2041+
# Cache target is always full path below fs cache root
20002042
keydir="${keyfile%/*}"
20012043
if [ "${keydir}" != "${keyfile}" ] && [ -n "${keydir}" ]; then
20022044
mkdir -p "${keycache}/${keydir}"
20032045
fi
20042046

2005-
if cp "${mnt}/${keyfile}" "${keycache}/${keyfile}"; then
2006-
zdebug "copied key ${mnt}/${keyfile} to ${keycache}/${keyfile}"
2047+
if cp "${keypath}" "${keycache}/${keyfile}"; then
2048+
zdebug "copied key ${keypath} to ${keycache}/${keyfile}"
20072049
ret=0
2050+
else
2051+
zerror "failed to copy ${keypath} to ${keycache}/${keyfile}"
20082052
fi
2009-
else
2010-
zdebug "key file ${keysrc}:${keyfile} not found at ${mnt}/${keyfile}"
20112053
fi
20122054

20132055
# Clean up mount and mutex

0 commit comments

Comments
 (0)