[LTP] [PATCH v2 08/33] keyctl16: Test KEYCTL_GET_SECURITY truncated copy
Andrea Cervesato
andrea.cervesato@suse.de
Fri Sep 4 14:08:57 CEST 2026
From: Andrea Cervesato <andrea.cervesato@suse.com>
Test KEYCTL_GET_SECURITY truncated copy behavior: verify that calling
with a 1-byte buffer receives exactly one byte while the return value
still reports the full label length.
Signed-off-by: Andrea Cervesato <andrea.cervesato@suse.com>
---
runtest/syscalls | 1 +
testcases/kernel/syscalls/keyctl/.gitignore | 1 +
testcases/kernel/syscalls/keyctl/keyctl16.c | 74 +++++++++++++++++++++++++++++
3 files changed, 76 insertions(+)
diff --git a/runtest/syscalls b/runtest/syscalls
index da901baae..89c8a95af 100644
--- a/runtest/syscalls
+++ b/runtest/syscalls
@@ -733,6 +733,7 @@ keyctl12 keyctl12
keyctl13 keyctl13
keyctl14 keyctl14
keyctl15 keyctl15
+keyctl16 keyctl16
kcmp01 kcmp01
kcmp02 kcmp02
diff --git a/testcases/kernel/syscalls/keyctl/.gitignore b/testcases/kernel/syscalls/keyctl/.gitignore
index 852fee4ea..c351716d0 100644
--- a/testcases/kernel/syscalls/keyctl/.gitignore
+++ b/testcases/kernel/syscalls/keyctl/.gitignore
@@ -13,3 +13,4 @@
/keyctl13
/keyctl14
/keyctl15
+/keyctl16
diff --git a/testcases/kernel/syscalls/keyctl/keyctl16.c b/testcases/kernel/syscalls/keyctl/keyctl16.c
new file mode 100644
index 000000000..f5662e326
--- /dev/null
+++ b/testcases/kernel/syscalls/keyctl/keyctl16.c
@@ -0,0 +1,74 @@
+// SPDX-License-Identifier: GPL-2.0-or-later
+/*
+ * Copyright (c) 2026 Andrea Cervesato <andrea.cervesato@suse.com>
+ */
+
+/*\
+ * Test ``KEYCTL_GET_SECURITY`` truncated copy of :manpage:`keyctl(2)`.
+ *
+ * ``KEYCTL_GET_SECURITY`` returns the full length of the label, but unlike
+ * ``KEYCTL_DESCRIBE`` the kernel performs a truncated copy when the user
+ * buffer is too small.
+ *
+ * [Algorithm]
+ *
+ * - verify a one byte buffer receives exactly one byte while the full
+ * length is still returned
+ */
+
+#include "keyctl_common.h"
+
+#define KEY_DESC "ltpkeyctl16"
+#define PAYLOAD "payload"
+#define BUF_SIZE 128
+
+static key_serial_t key;
+static long sec_len;
+static char buf[BUF_SIZE];
+
+static void setup(void)
+{
+ SAFE_KEYCTL(KEYCTL_JOIN_SESSION_KEYRING, 0, 0, 0, 0);
+
+ key = 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_GET_SECURITY, key, (unsigned long)NULL, 0, 0));
+ if (TST_RET < 0)
+ tst_brk(TBROK | TTERRNO, "KEYCTL_GET_SECURITY failed");
+
+ sec_len = TST_RET;
+}
+
+static void run(void)
+{
+ size_t i;
+
+ memset(buf, 0xAA, sizeof(buf));
+ TST_EXP_EQ_LI_SILENT(keyctl(KEYCTL_GET_SECURITY, key,
+ (unsigned long)buf, 1, 0), sec_len);
+ if (!TST_PASS)
+ return;
+
+ if (buf[0] == (char)0xAA) {
+ tst_res(TFAIL, "nothing was copied into the buffer");
+ return;
+ }
+
+ for (i = 1; i < sizeof(buf); i++) {
+ if (buf[i] != (char)0xAA) {
+ tst_res(TFAIL, "copy overran the buffer at offset %zu",
+ i);
+ return;
+ }
+ }
+
+ tst_res(TPASS, "exactly one byte copied, full length %ld returned",
+ sec_len);
+}
+
+static struct tst_test test = {
+ .setup = setup,
+ .test_all = run,
+};
--
2.51.0
More information about the ltp
mailing list