[LTP] [PATCH] syscalls/keyctl/keyctl09.c: fix test encrypted key

Nikolaus Voss nv@vosn.de
Thu Oct 6 08:15:55 CEST 2022


This commit fixes the test for adding encrypted keys with unencrypted data.
Unencryted data must be provided hex-ascii encoding. Due to a kernel
bug, the unencypted data was not decoded to binary thus the length of
the key was only half the specified key size. This patch doubles the key
size and adds a test with a wrong key size to trigger a corresponding
error.

This patch must be used with the kernel fix
https://lore.kernel.org/lkml/20220919072317.E41421357@mail.steuer-voss.de

test output:
~ # ./keyctl09
tst_kconfig.c:82: TINFO: Parsing kernel config '/proc/config.gz'
tst_test.c:1526: TINFO: Timeout per run is 0h 00m 30s
keyctl09.c:28: TPASS: add_key("user", "user:masterkey", "foo", 3, KEY_SPEC_PROCESS_KEYRING) returned 837616982
keyctl09.c:34: TPASS: add_key("encrypted", "ltptestkey1", ENCRYPTED_KEY_PREAMBLE HEXASCII_UNENCRYPTED_DATA, strlen(ENCRYPTED_KEY_PREAMBLE HEXASCII_UNENCRYPTED_DATA), KEY_SPEC_PROCESS_KEYRING) returned 449585633
keyctl09.c:44: TPASS: keyctl(KEYCTL_READ, TST_RET, buffer, sizeof(buffer)) returned 186
[ 3031.280283] trusted_key: encrypted key: decrypted data provided must contain only hexadecimal characters
keyctl09.c:50: TPASS: add_key("encrypted", "ltptestkey2", ENCRYPTED_KEY_PREAMBLE ASCII_UNENCRYPTED_DATA, strlen(ENCRYPTED_KEY_PREAMBLE ASCII_UNENCRYPTED_DATA), KEY_SPEC_PROCESS_KEYRING) : EINVAL (22)
[ 3031.284871] trusted_key: encrypted key: decrypted data provided does not match decrypted data length provided
keyctl09.c:58: TPASS: add_key("encrypted", "ltptestkey3", ENCRYPTED_KEY_PREAMBLE SHORT_HEXASC_UNENCRYPTED_DATA, strlen(ENCRYPTED_KEY_PREAMBLE SHORT_HEXASC_UNENCRYPTED_DATA), KEY_SPEC_PROCESS_KEYRING) : EINVAL (22)

Fixes: 342e7a0dd ("syscalls/keyctl09: test encrypted keys with provided decrypted data.")
Signed-off-by: Nikolaus Voss <nikolaus.voss@haag-streit.com>
---
 testcases/kernel/syscalls/keyctl/keyctl09.c | 30 +++++++++++++++++----
 1 file changed, 25 insertions(+), 5 deletions(-)

diff --git a/testcases/kernel/syscalls/keyctl/keyctl09.c b/testcases/kernel/syscalls/keyctl/keyctl09.c
index c88c481b9..c5165a280 100644
--- a/testcases/kernel/syscalls/keyctl/keyctl09.c
+++ b/testcases/kernel/syscalls/keyctl/keyctl09.c
@@ -13,8 +13,13 @@
 #include "tst_test.h"
 #include "lapi/keyctl.h"
 
-#define ENCRYPTED_KEY_VALID_PAYLOAD	"new enc32 user:masterkey 32 abcdefABCDEF1234567890aaaaaaaaaa"
-#define ENCRYPTED_KEY_INVALID_PAYLOAD	"new enc32 user:masterkey 32 plaintext123@123!123@123!123@123"
+#define ENCRYPTED_KEY_PREAMBLE		"new enc32 user:masterkey 32 "
+#define SHORT_HEXASC_UNENCRYPTED_DATA	"abcdefABCDEF1234567890aaaaaaaaaa"
+#define HEXASCII_UNENCRYPTED_DATA	SHORT_HEXASC_UNENCRYPTED_DATA \
+					SHORT_HEXASC_UNENCRYPTED_DATA
+#define SHORT_ASCII_UNENCRYPTED_DATA	"plaintext123@123!123@123!123@123"
+#define ASCII_UNENCRYPTED_DATA		SHORT_ASCII_UNENCRYPTED_DATA \
+					SHORT_ASCII_UNENCRYPTED_DATA
 
 static void do_test(void)
 {
@@ -27,8 +32,11 @@ static void do_test(void)
 		return;
 
 	TST_EXP_POSITIVE(add_key("encrypted", "ltptestkey1",
-			    ENCRYPTED_KEY_VALID_PAYLOAD,
-			    60, KEY_SPEC_PROCESS_KEYRING));
+			    ENCRYPTED_KEY_PREAMBLE
+			    HEXASCII_UNENCRYPTED_DATA,
+			    strlen(ENCRYPTED_KEY_PREAMBLE
+				   HEXASCII_UNENCRYPTED_DATA),
+			    KEY_SPEC_PROCESS_KEYRING));
 
 	if (!TST_PASS)
 		return;
@@ -38,8 +46,20 @@ static void do_test(void)
 	if (!TST_PASS)
 		return;
 
+	/* key not hex-ascii encoded */
 	TST_EXP_FAIL2(add_key("encrypted", "ltptestkey2",
-			    ENCRYPTED_KEY_INVALID_PAYLOAD, 60,
+			    ENCRYPTED_KEY_PREAMBLE
+			    ASCII_UNENCRYPTED_DATA,
+			    strlen(ENCRYPTED_KEY_PREAMBLE
+				   ASCII_UNENCRYPTED_DATA),
+			    KEY_SPEC_PROCESS_KEYRING), EINVAL);
+
+	/* key size mismatch */
+	TST_EXP_FAIL2(add_key("encrypted", "ltptestkey3",
+			    ENCRYPTED_KEY_PREAMBLE
+			    SHORT_HEXASC_UNENCRYPTED_DATA,
+			    strlen(ENCRYPTED_KEY_PREAMBLE
+				   SHORT_HEXASC_UNENCRYPTED_DATA),
 			    KEY_SPEC_PROCESS_KEYRING), EINVAL);
 
 	keyctl(KEYCTL_CLEAR, KEY_SPEC_PROCESS_KEYRING);
-- 
2.34.1



More information about the ltp mailing list