Skip to contentSkip to conversation

common/encrypt: allow the use of 'fscrypt:' as key prefix

PATCH Superseded Luís Henriques 3 participants 5 messages Apr 1, 2022 – Apr 4, 2022

v1 of 3 Superseded Latest is v3 →

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 common/encrypt: allow the use of 'fscrypt:' as key prefix

fscrypt keys have used the $FSTYP as prefix. However this format is being deprecated -- newer kernels already allow the usage of the generic 'fscrypt:' prefix for ext4 and f2fs. This patch allows the usage of this new prefix for testing filesystems that have never supported the old format, but keeping the $FSTYP prefix for filesystems that support it, so that old kernels can be tested.

---
 common/encrypt | 38 +++++++++++++++++++++++++++-----------
 1 file changed, 27 insertions(+), 11 deletions(-)
common/encrypt +27 -11
index f90c4ef05a3f..897c97e0f6fa 100644
@@ -250,6 +250,27 @@ _num_to_hex()
 	fi
 }
 
+# Keys are named $FSTYP:KEYDESC where KEYDESC is the 16-character key descriptor
+# hex string.  Newer kernels (ext4 4.8 and later, f2fs 4.6 and later) also allow
+# the common key prefix "fscrypt:" in addition to their filesystem-specific key
+# prefix ("ext4:", "f2fs:").  It would be nice to use the common key prefix, but
+# for now use the filesystem- specific prefix for these 2 filesystems to make it
+# possible to test older kernels, and the "fscrypt" prefix for anything else.
+_get_fs_keyprefix()
+{
+	local prefix=""
+
+	case $FSTYP in
+	ext4|f2fs|ubifs)
+		prefix="$FSTYP"
+		;;
+	*)
+		prefix="fscrypt"
+		;;
+	esac
+	echo $prefix
+}
+
 # Add the specified raw encryption key to the session keyring, using the
 # specified key descriptor.
 _add_session_encryption_key()
@@ -268,18 +289,11 @@ _add_session_encryption_key()
 	#	};
 	#
 	# The kernel ignores 'mode' but requires that 'size' be 64.
-	#
-	# Keys are named $FSTYP:KEYDESC where KEYDESC is the 16-character key
-	# descriptor hex string.  Newer kernels (ext4 4.8 and later, f2fs 4.6
-	# and later) also allow the common key prefix "fscrypt:" in addition to
-	# their filesystem-specific key prefix ("ext4:", "f2fs:").  It would be
-	# nice to use the common key prefix, but for now use the filesystem-
-	# specific prefix to make it possible to test older kernels...
-	#
 	local mode=$(_num_to_hex 0 4)
 	local size=$(_num_to_hex 64 4)
+	local prefix=$(_get_fs_keyprefix)
 	echo -n -e "${mode}${raw}${size}" |
-		$KEYCTL_PROG padd logon $FSTYP:$keydesc @s >>$seqres.full
+		$KEYCTL_PROG padd logon $prefix:$keydesc @s >>$seqres.full
 }
 
 #
@@ -302,7 +316,8 @@ _generate_session_encryption_key()
 _unlink_session_encryption_key()
 {
 	local keydesc=$1
-	local keyid=$($KEYCTL_PROG search @s logon $FSTYP:$keydesc)
+	local prefix=$(_get_fs_keyprefix)
+	local keyid=$($KEYCTL_PROG search @s logon $prefix:$keydesc)
 	$KEYCTL_PROG unlink $keyid >>$seqres.full
 }
 
@@ -310,7 +325,8 @@ _unlink_session_encryption_key()
 _revoke_session_encryption_key()
 {
 	local keydesc=$1
-	local keyid=$($KEYCTL_PROG search @s logon $FSTYP:$keydesc)
+	local prefix=$(_get_fs_keyprefix)
+	local keyid=$($KEYCTL_PROG search @s logon $prefix:$keydesc)
 	$KEYCTL_PROG revoke $keyid >>$seqres.full
 }
 
Reviewed-by on v1 1/1
#1 lore raw
Quoted 76 lines · Luís Henriques

fscrypt keys have used the $FSTYP as prefix. However this format is being deprecated -- newer kernels already allow the usage of the generic 'fscrypt:' prefix for ext4 and f2fs. This patch allows the usage of this new prefix for testing filesystems that have never supported the old format, but keeping the $FSTYP prefix for filesystems that support it, so that old kernels can be tested.

---
 common/encrypt | 38 +++++++++++++++++++++++++++-----------
 1 file changed, 27 insertions(+), 11 deletions(-)
common/encrypt +25 -10
index f90c4ef05a3f..897c97e0f6fa 100644
@@ -250,6 +250,27 @@ _num_to_hex()
 	fi
 }
 
+# Keys are named $FSTYP:KEYDESC where KEYDESC is the 16-character key descriptor
+# hex string.  Newer kernels (ext4 4.8 and later, f2fs 4.6 and later) also allow
+# the common key prefix "fscrypt:" in addition to their filesystem-specific key
+# prefix ("ext4:", "f2fs:").  It would be nice to use the common key prefix, but
+# for now use the filesystem- specific prefix for these 2 filesystems to make it
+# possible to test older kernels, and the "fscrypt" prefix for anything else.
+_get_fs_keyprefix()
+{
+	local prefix=""
+
+	case $FSTYP in
+	ext4|f2fs|ubifs)
+		prefix="$FSTYP"
+		;;
+	*)
+		prefix="fscrypt"
+		;;
+	esac
+	echo $prefix
+}
+
 # Add the specified raw encryption key to the session keyring, using the
 # specified key descriptor.
 _add_session_encryption_key()
@@ -268,18 +289,11 @@ _add_session_encryption_key()
 	#	};
 	#
 	# The kernel ignores 'mode' but requires that 'size' be 64.
-	#
-	# Keys are named $FSTYP:KEYDESC where KEYDESC is the 16-character key
-	# descriptor hex string.  Newer kernels (ext4 4.8 and later, f2fs 4.6
-	# and later) also allow the common key prefix "fscrypt:" in addition to
-	# their filesystem-specific key prefix ("ext4:", "f2fs:").  It would be
-	# nice to use the common key prefix, but for now use the filesystem-
-	# specific prefix to make it possible to test older kernels...
-	#
 	local mode=$(_num_to_hex 0 4)
 	local size=$(_num_to_hex 64 4)
+	local prefix=$(_get_fs_keyprefix)
 	echo -n -e "${mode}${raw}${size}" |
-		$KEYCTL_PROG padd logon $FSTYP:$keydesc @s >>$seqres.full
+		$KEYCTL_PROG padd logon $prefix:$keydesc @s >>$seqres.full
 }
 
 #
@@ -302,7 +316,8 @@ _generate_session_encryption_key()
 _unlink_session_encryption_key()
 {
 	local keydesc=$1
-	local keyid=$($KEYCTL_PROG search @s logon $FSTYP:$keydesc)
+	local prefix=$(_get_fs_keyprefix)
+	local keyid=$($KEYCTL_PROG search @s logon $prefix:$keydesc)
 	$KEYCTL_PROG unlink $keyid >>$seqres.full
 }
 

common/encrypt _unlink_session_encryption_key() · line 332

Quoted 9 lines
common/encrypt +2 -1
@@ -310,7 +325,8 @@ _unlink_session_encryption_key()
 _revoke_session_encryption_key()
 {
 	local keydesc=$1
-	local keyid=$($KEYCTL_PROG search @s logon $FSTYP:$keydesc)
+	local prefix=$(_get_fs_keyprefix)
+	local keyid=$($KEYCTL_PROG search @s logon $prefix:$keydesc)
 	$KEYCTL_PROG revoke $keyid >>$seqres.full
 }
 
#2 lore raw
In reply to Luís Henriques
Quoted 30 lines · Luís Henriques

fscrypt keys have used the $FSTYP as prefix. However this format is being deprecated -- newer kernels already allow the usage of the generic 'fscrypt:' prefix for ext4 and f2fs. This patch allows the usage of this new prefix for testing filesystems that have never supported the old format, but keeping the $FSTYP prefix for filesystems that support it, so that old kernels can be tested.

---
 common/encrypt | 38 +++++++++++++++++++++++++++-----------
 1 file changed, 27 insertions(+), 11 deletions(-)
common/encrypt +10 -0
index f90c4ef05a3f..897c97e0f6fa 100644
@@ -250,6 +250,27 @@ _num_to_hex()
 	fi
 }
 
+# Keys are named $FSTYP:KEYDESC where KEYDESC is the 16-character key descriptor
+# hex string.  Newer kernels (ext4 4.8 and later, f2fs 4.6 and later) also allow
+# the common key prefix "fscrypt:" in addition to their filesystem-specific key
+# prefix ("ext4:", "f2fs:").  It would be nice to use the common key prefix, but
+# for now use the filesystem- specific prefix for these 2 filesystems to make it
+# possible to test older kernels, and the "fscrypt" prefix for anything else.
+_get_fs_keyprefix()
+{
+	local prefix=""
+

common/encrypt _num_to_hex() · line 272

Quoted 10 lines
common/encrypt +10 -0
+	case $FSTYP in
+	ext4|f2fs|ubifs)
+		prefix="$FSTYP"
+		;;
+	*)
+		prefix="fscrypt"
+		;;
+	esac
+	echo $prefix
+}

ubifs can use the "fscrypt" prefix, since there was never a kernel that supported ubifs encryption but not the "fscrypt" prefix. Also, the "prefix" local variable is unnecessary. So:

	case $FSTYP in
	ext4|f2fs)
		echo $FSTYP
		;;
	*)
		echo fscrypt
		;;
	esac

Otherwise, this patch looks fine if we want to keep supporting testing kernels older than 4.8. However, since 4.4 is no longer a supported LTS kernel, perhaps this is no longer needed and we could just always use "fscrypt"? I'm not sure what xfstests's policy on old kernels is.

- Eric

#3 lore raw
Quoted 55 lines · Eric Biggers
Quoted 30 lines · Luís Henriques

fscrypt keys have used the $FSTYP as prefix. However this format is being deprecated -- newer kernels already allow the usage of the generic 'fscrypt:' prefix for ext4 and f2fs. This patch allows the usage of this new prefix for testing filesystems that have never supported the old format, but keeping the $FSTYP prefix for filesystems that support it, so that old kernels can be tested.

---
 common/encrypt | 38 +++++++++++++++++++++++++++-----------
 1 file changed, 27 insertions(+), 11 deletions(-)
common/encrypt +10 -0
index f90c4ef05a3f..897c97e0f6fa 100644
@@ -250,6 +250,27 @@ _num_to_hex()
 	fi
 }
 
+# Keys are named $FSTYP:KEYDESC where KEYDESC is the 16-character key descriptor
+# hex string.  Newer kernels (ext4 4.8 and later, f2fs 4.6 and later) also allow
+# the common key prefix "fscrypt:" in addition to their filesystem-specific key
+# prefix ("ext4:", "f2fs:").  It would be nice to use the common key prefix, but
+# for now use the filesystem- specific prefix for these 2 filesystems to make it
+# possible to test older kernels, and the "fscrypt" prefix for anything else.
+_get_fs_keyprefix()
+{
+	local prefix=""
+

common/encrypt _num_to_hex() · line 272

Quoted 10 lines
common/encrypt +10 -0
+	case $FSTYP in
+	ext4|f2fs|ubifs)
+		prefix="$FSTYP"
+		;;
+	*)
+		prefix="fscrypt"
+		;;
+	esac
+	echo $prefix
+}

ubifs can use the "fscrypt" prefix, since there was never a kernel that supported ubifs encryption but not the "fscrypt" prefix. Also, the "prefix" local variable is unnecessary. So:

	case $FSTYP in
	ext4|f2fs)
		echo $FSTYP
		;;
	*)
		echo fscrypt
		;;
	esac
Quoted 4 lines

Otherwise, this patch looks fine if we want to keep supporting testing kernels older than 4.8. However, since 4.4 is no longer a supported LTS kernel, perhaps this is no longer needed and we could just always use "fscrypt"? I'm not sure what xfstests's policy on old kernels is.

Thank you for your feedback. I'll resend the patch with your changes. I am, of course, OK dropping support for older kernels on fstests, but I'll leave that decision for the maintainers; if anyone thinks that support should be dropped, I can send another version of the patch doing that.

Cheers,

Signature · 1 line
Luís
#4 lore raw
In reply to Luís Henriques
Quoted 76 lines · Luís Henriques

fscrypt keys have used the $FSTYP as prefix. However this format is being deprecated -- newer kernels already allow the usage of the generic 'fscrypt:' prefix for ext4 and f2fs. This patch allows the usage of this new prefix for testing filesystems that have never supported the old format, but keeping the $FSTYP prefix for filesystems that support it, so that old kernels can be tested.

---
 common/encrypt | 38 +++++++++++++++++++++++++++-----------
 1 file changed, 27 insertions(+), 11 deletions(-)
common/encrypt +25 -10
index f90c4ef05a3f..897c97e0f6fa 100644
@@ -250,6 +250,27 @@ _num_to_hex()
 	fi
 }
 
+# Keys are named $FSTYP:KEYDESC where KEYDESC is the 16-character key descriptor
+# hex string.  Newer kernels (ext4 4.8 and later, f2fs 4.6 and later) also allow
+# the common key prefix "fscrypt:" in addition to their filesystem-specific key
+# prefix ("ext4:", "f2fs:").  It would be nice to use the common key prefix, but
+# for now use the filesystem- specific prefix for these 2 filesystems to make it
+# possible to test older kernels, and the "fscrypt" prefix for anything else.
+_get_fs_keyprefix()
+{
+	local prefix=""
+
+	case $FSTYP in
+	ext4|f2fs|ubifs)
+		prefix="$FSTYP"
+		;;
+	*)
+		prefix="fscrypt"
+		;;
+	esac
+	echo $prefix
+}
+
 # Add the specified raw encryption key to the session keyring, using the
 # specified key descriptor.
 _add_session_encryption_key()
@@ -268,18 +289,11 @@ _add_session_encryption_key()
 	#	};
 	#
 	# The kernel ignores 'mode' but requires that 'size' be 64.
-	#
-	# Keys are named $FSTYP:KEYDESC where KEYDESC is the 16-character key
-	# descriptor hex string.  Newer kernels (ext4 4.8 and later, f2fs 4.6
-	# and later) also allow the common key prefix "fscrypt:" in addition to
-	# their filesystem-specific key prefix ("ext4:", "f2fs:").  It would be
-	# nice to use the common key prefix, but for now use the filesystem-
-	# specific prefix to make it possible to test older kernels...
-	#
 	local mode=$(_num_to_hex 0 4)
 	local size=$(_num_to_hex 64 4)
+	local prefix=$(_get_fs_keyprefix)
 	echo -n -e "${mode}${raw}${size}" |
-		$KEYCTL_PROG padd logon $FSTYP:$keydesc @s >>$seqres.full
+		$KEYCTL_PROG padd logon $prefix:$keydesc @s >>$seqres.full
 }
 
 #
@@ -302,7 +316,8 @@ _generate_session_encryption_key()
 _unlink_session_encryption_key()
 {
 	local keydesc=$1
-	local keyid=$($KEYCTL_PROG search @s logon $FSTYP:$keydesc)
+	local prefix=$(_get_fs_keyprefix)
+	local keyid=$($KEYCTL_PROG search @s logon $prefix:$keydesc)
 	$KEYCTL_PROG unlink $keyid >>$seqres.full
 }
 

common/encrypt _unlink_session_encryption_key() · line 332

Quoted 9 lines
common/encrypt +2 -1
@@ -310,7 +325,8 @@ _unlink_session_encryption_key()
 _revoke_session_encryption_key()
 {
 	local keydesc=$1
-	local keyid=$($KEYCTL_PROG search @s logon $FSTYP:$keydesc)
+	local prefix=$(_get_fs_keyprefix)
+	local keyid=$($KEYCTL_PROG search @s logon $prefix:$keydesc)
 	$KEYCTL_PROG revoke $keyid >>$seqres.full
 }
 

For the record, without this patch in place, generic/397 hangs when tested against the current ceph+fscrypt pile. With this, the test passes.

Cheers,

Signature · 1 line
Jeff Layton <[email protected]>