Skip to contentSkip to conversation

syscalls/keyctl09: test encrypted keys with provided decrypted data.

PATCH Superseded Yael Tzur 2 participants 2 messages Feb 22, 2022 – Feb 23, 2022 Keys-trusted

v3 of 4 Superseded Latest is v4 →

lore
Share & tools
lore

Yael Tzur

author
#0 lore raw

PATCH v3 1/1 syscalls/keyctl09: test encrypted keys with provided decrypted data.

Test that encrypted keys can be instantiated using hex-ascii encoded user-provided decrypted data.
(https://lore.kernel.org/lkml/[email protected]/).

---

Notes:

    v -> v2: added key revocation and made styling changes.
    
    v2 -> v3: updated per latest kernel patch version.
 runtest/syscalls                            |  1 +
 testcases/kernel/syscalls/keyctl/.gitignore |  1 +
 testcases/kernel/syscalls/keyctl/keyctl09.c | 55 +++++++++++++++++++++
 3 files changed, 57 insertions(+)
 create mode 100644 testcases/kernel/syscalls/keyctl/keyctl09.c
runtest/syscalls +1 -0
index bcf3d56c9..ccea1ddbd 100644
@@ -643,6 +643,7 @@ keyctl05 keyctl05
 keyctl06 keyctl06
 keyctl07 keyctl07
 keyctl08 keyctl08
+keyctl09 keyctl09
 
 kcmp01 kcmp01
 kcmp02 kcmp02
testcases/kernel/syscalls/keyctl/.gitignore +1 -0
index 3544ac79c..f9948c176 100644
@@ -6,3 +6,4 @@
 /keyctl06
 /keyctl07
 /keyctl08
+/keyctl09
testcases/kernel/syscalls/keyctl/keyctl09.c new file +55 -0

55 lines not on this page · raw message

Signature · 1 line
2.35.1.473.g83b2b277ed-goog
#1 lore raw

Hi!

Quoted 6 lines
testcases/kernel/syscalls/keyctl/keyctl09.c new file +6 -0
+static void do_test(void)
+{
+	key_serial_t masterkey;
+	key_serial_t encryptedkey1;
+	key_serial_t encryptedkey2;
+	char buffer[128];

testcases/kernel/syscalls/keyctl/keyctl09.c line 33

Quoted 10 lines
testcases/kernel/syscalls/keyctl/keyctl09.c new file +10 -0
+
+	masterkey = add_key("user", "user:masterkey", "foo", 3,
+			    KEY_SPEC_PROCESS_KEYRING);
+	if (masterkey == -1)
+		tst_brk(TBROK | TERRNO, "Failed to add user key");
+
+	encryptedkey1 = add_key("encrypted", "ltptestkey1", ENCRYPTED_KEY_1_PAYLOAD,
+				60, KEY_SPEC_PROCESS_KEYRING);
+	if (encryptedkey1 == -1)
+		tst_brk(TFAIL, "Failed to instantiate encrypted key using payload decrypted data");

I guess that we should print errno (by adding the | TERRNO to the TFAIL) here as well.

Also we can make the message shorter since the FAIL part is printed by the library because of the TFAIL flag. So maybe something as:

	tst_brk(TFAIL | TERRNO, "instatiation of encrypted key with decrypted payload");

Which would print message as:

foo.c:XX: TFAIL: instatiation of encrypted key with decrypted payload: ENOMEM (12)

Or even better use the LTP TST_EXP_*() macros which will generate most of the code for you.

Assuming the return value from add_key() on success is >= 0 we can do:

	TST_EXP_POSITIVE(add_key("encrypted", "ltptestkey1",
	                         ENCRYPTED_KEY_1_PAYLOAD,
			         60, KEY_SPEC_PROCESS_KEYRING));

	if (!TST_PASS)
		return;

The TST_EXP_POSITIVE() has optional printf-like parameters if you want to customize the message, so if you want to keep the original message you can do:

	TST_EXP_POSITIVE(add_key(...),
	                 "instatiation of encrypted key with decrypted payload");

And the return value from add_key is stored in TST_RET.

testcases/kernel/syscalls/keyctl/keyctl09.c line 37

Quoted 3 lines
testcases/kernel/syscalls/keyctl/keyctl09.c new file +3 -0
+	TEST(keyctl(KEYCTL_READ, encryptedkey1, buffer, sizeof(buffer)));
+	if (TST_RET < 0)
+		tst_brk(TFAIL, "KEYCTL_READ failed for encryptedkey1");

And here as well.

testcases/kernel/syscalls/keyctl/keyctl09.c line 42

Quoted 4 lines
testcases/kernel/syscalls/keyctl/keyctl09.c new file +4 -0
+	encryptedkey2 = add_key("encrypted", "ltptestkey2", ENCRYPTED_KEY_2_PAYLOAD,
+				60, KEY_SPEC_PROCESS_KEYRING);
+	if (encryptedkey2 != -1)
+		tst_brk(TFAIL, "Instantiation of encrypted key using non hex-encoded decrypted data unexpectedly succeeded");

We should check that the errno was set correctly here as well. We do have a TST_EXP_FAIL() macro for this. If this is supposed to end with EINVAL it can be simply done as:

	TST_EXP_FAIL2(add_key("encrypted", "ltptestkey2",
	              ENCRYPTED_KEY_2_PAYLOAD, 60,
		      KEY_SPEC_PROCESS_KEYRING), EINVAL);

And you can pass a printf-like parameters to this macro as well to customize the message.

Quoted 12 lines
testcases/kernel/syscalls/keyctl/keyctl09.c new file +12 -0
+	tst_res(TPASS, "Encrypted keys were instantiated with decrypted data as expected");
+
+	keyctl(KEYCTL_CLEAR, KEY_SPEC_PROCESS_KEYRING);
+}
+
+static struct tst_test test = {
+	.test_all = do_test,
+	.needs_kconfigs = (const char *[]) {
+		"CONFIG_USER_DECRYPTED_DATA=y",
+		NULL
+	}
+};
Signature · 2 lines
Cyril Hrubis
[email protected]