[LTP] [PATCH v3 04/36] keyctl10: Test KEYCTL_DESCRIBE format parsing
Petr Vorel
pvorel@suse.cz
Wed Sep 16 16:03:30 CEST 2026
From: Andrea Cervesato <andrea.cervesato@suse.com>
Test the format parsing of KEYCTL_DESCRIBE: verify that describing
a valid key returns a string formatted as "type;uid;gid;perm;description"
and that all five fields match the key's attributes.
Also add keyctl_common.h helper header for shared test definitions.
Reviewed-by: Petr Vorel <pvorel@suse.cz>
Signed-off-by: Andrea Cervesato <andrea.cervesato@suse.com>
[ pvorel: Define as macros, use safe_add_key(), remove search_ring() ]
Signed-off-by: Petr Vorel <pvorel@suse.cz>
---
runtest/syscalls | 1 +
testcases/kernel/syscalls/keyctl/.gitignore | 1 +
testcases/kernel/syscalls/keyctl/keyctl10.c | 99 +++++++++++++++++++
.../kernel/syscalls/keyctl/keyctl_common.h | 33 +++++++
4 files changed, 134 insertions(+)
create mode 100644 testcases/kernel/syscalls/keyctl/keyctl10.c
create mode 100644 testcases/kernel/syscalls/keyctl/keyctl_common.h
diff --git a/runtest/syscalls b/runtest/syscalls
index d953f05588..9840cf678b 100644
--- a/runtest/syscalls
+++ b/runtest/syscalls
@@ -727,6 +727,7 @@ keyctl06 keyctl06
keyctl07 keyctl07
keyctl08 keyctl08
keyctl09 keyctl09
+keyctl10 keyctl10
kcmp01 kcmp01
kcmp02 kcmp02
diff --git a/testcases/kernel/syscalls/keyctl/.gitignore b/testcases/kernel/syscalls/keyctl/.gitignore
index f9948c1766..08b2d101db 100644
--- a/testcases/kernel/syscalls/keyctl/.gitignore
+++ b/testcases/kernel/syscalls/keyctl/.gitignore
@@ -7,3 +7,4 @@
/keyctl07
/keyctl08
/keyctl09
+/keyctl10
diff --git a/testcases/kernel/syscalls/keyctl/keyctl10.c b/testcases/kernel/syscalls/keyctl/keyctl10.c
new file mode 100644
index 0000000000..f069e91d37
--- /dev/null
+++ b/testcases/kernel/syscalls/keyctl/keyctl10.c
@@ -0,0 +1,99 @@
+// SPDX-License-Identifier: GPL-2.0-or-later
+/*
+ * Copyright (c) 2026 Andrea Cervesato <andrea.cervesato@suse.com>
+ */
+
+/*\
+ * Test the format parsing of ``KEYCTL_DESCRIBE`` of :manpage:`keyctl(2)`.
+ *
+ * ``KEYCTL_DESCRIBE`` formats a key description as
+ * "type;uid;gid;perm;description" and returns the full length of the
+ * string including the terminating NUL byte.
+ *
+ * [Algorithm]
+ *
+ * - describe a valid key and verify all five fields of the description
+ * match the key's attributes
+ */
+
+#include <stdio.h>
+#include <unistd.h>
+
+#include "keyctl_common.h"
+
+#define KEY_DESC "ltpkeyctl10"
+#define PAYLOAD "payload"
+#define BUF_SIZE 64
+
+static key_serial_t key;
+static long desc_len;
+static char buf[BUF_SIZE];
+
+static void setup(void)
+{
+ SAFE_KEYCTL(KEYCTL_JOIN_SESSION_KEYRING, 0, 0, 0, 0);
+
+ key = SAFE_NEW_USER_KEY(KEY_DESC, PAYLOAD, sizeof(PAYLOAD),
+ KEY_SPEC_PROCESS_KEYRING);
+ SAFE_KEYCTL(KEYCTL_SETPERM, key, KEY_PERM_SET, 0, 0);
+
+ TEST(keyctl(KEYCTL_DESCRIBE, key, (unsigned long)NULL, 0, 0));
+ if (TST_RET < 0)
+ tst_brk(TBROK | TTERRNO, "KEYCTL_DESCRIBE failed");
+
+ desc_len = TST_RET;
+}
+
+static void run(void)
+{
+ char type[32], desc[BUF_SIZE];
+ unsigned int uid, gid, perm;
+
+ memset(buf, 0, sizeof(buf));
+ TST_EXP_EQ_LI_SILENT(keyctl(KEYCTL_DESCRIBE, key, (unsigned long)buf,
+ sizeof(buf), 0), desc_len);
+ if (!TST_PASS)
+ return;
+
+ TST_EXP_EQ_SZ_SILENT((size_t)desc_len, strlen(buf) + 1);
+ if (!TST_PASS) {
+ tst_res(TINFO, "unexpected description '%s'", buf);
+ return;
+ }
+
+ if (sscanf(buf, "%31[^;];%u;%u;%x;%63[^\n]",
+ type, &uid, &gid, &perm, desc) != 5) {
+ tst_res(TFAIL, "malformed description '%s'", buf);
+ return;
+ }
+
+ if (strcmp(type, "user")) {
+ tst_res(TFAIL, "wrong key type '%s', expected 'user'", type);
+ return;
+ }
+
+ if (uid != getuid() || gid != getgid()) {
+ tst_res(TFAIL, "wrong owner uid %u gid %u, expected %u %u",
+ uid, gid, getuid(), getgid());
+ return;
+ }
+
+ if (perm != KEY_PERM_SET) {
+ tst_res(TFAIL, "wrong permissions %08x, expected %08x",
+ perm, KEY_PERM_SET);
+ return;
+ }
+
+ if (strcmp(desc, KEY_DESC)) {
+ tst_res(TFAIL, "wrong description '%s', expected '%s'",
+ desc, KEY_DESC);
+ return;
+ }
+
+ tst_res(TPASS, "described the key correctly");
+}
+
+static struct tst_test test = {
+ .setup = setup,
+ .test_all = run,
+};
diff --git a/testcases/kernel/syscalls/keyctl/keyctl_common.h b/testcases/kernel/syscalls/keyctl/keyctl_common.h
new file mode 100644
index 0000000000..1063163bc1
--- /dev/null
+++ b/testcases/kernel/syscalls/keyctl/keyctl_common.h
@@ -0,0 +1,33 @@
+// SPDX-License-Identifier: GPL-2.0-or-later
+/*
+ * Copyright (c) 2026 Andrea Cervesato <andrea.cervesato@suse.com>
+ */
+
+#ifndef KEYCTL_COMMON_H__
+#define KEYCTL_COMMON_H__
+
+#include <stdint.h>
+#include <string.h>
+
+#include "tst_test.h"
+#include "lapi/keyctl.h"
+
+#define KEY_PERM_ALL (KEY_POS_ALL | KEY_USR_ALL | KEY_GRP_ALL | KEY_OTH_ALL)
+#define KEY_PERM_SET (KEY_POS_ALL | KEY_USR_ALL)
+
+#define KEY_VIEW_BITS (KEY_POS_VIEW | KEY_USR_VIEW | KEY_GRP_VIEW | KEY_OTH_VIEW)
+#define KEY_PERM_NO_VIEW (KEY_PERM_ALL & ~KEY_VIEW_BITS)
+
+#define KEY_WRITE_BITS (KEY_POS_WRITE | KEY_USR_WRITE | KEY_GRP_WRITE | KEY_OTH_WRITE)
+#define KEY_PERM_NO_WRITE (KEY_PERM_ALL & ~KEY_WRITE_BITS)
+
+#define KEY_SETATTR_BITS (KEY_POS_SETATTR | KEY_USR_SETATTR | KEY_GRP_SETATTR | KEY_OTH_SETATTR)
+#define KEY_PERM_NO_SETATTR (KEY_PERM_ALL & ~KEY_SETATTR_BITS)
+
+#define SAFE_NEW_RING(desc) \
+ safe_add_key(__FILE__, __LINE__, "keyring", (desc), NULL, 0, KEY_SPEC_PROCESS_KEYRING)
+
+#define SAFE_NEW_USER_KEY(desc, payload, plen, ring) \
+ safe_add_key(__FILE__, __LINE__, "user", (desc), (payload), (plen), (ring))
+
+#endif /* KEYCTL_COMMON_H__ */
--
2.55.0
More information about the ltp
mailing list