[LTP] [PATCH v3 14/36] keyctl20: Test KEYCTL_MOVE displacement
Petr Vorel
pvorel@suse.cz
Wed Sep 16 16:03:40 CEST 2026
From: Andrea Cervesato <andrea.cervesato@suse.com>
Test KEYCTL_MOVE displacement behavior: verify moving a key without
KEYCTL_MOVE_EXCL into a destination holding a matching key displaces
the existing key.
Reviewed-by: Petr Vorel <pvorel@suse.cz>
Signed-off-by: Andrea Cervesato <andrea.cervesato@suse.com>
[ pvorel:
* Use SAFE_NEW_RING(), SAFE_NEW_USER_KEY(), keyctl() instead of
search_ring()
* Remove keyctl(KEYCTL_UNLINK, ...) check (Cyril) ]
Signed-off-by: Petr Vorel <pvorel@suse.cz>
---
runtest/syscalls | 1 +
testcases/kernel/syscalls/keyctl/.gitignore | 1 +
testcases/kernel/syscalls/keyctl/keyctl20.c | 51 +++++++++++++++++++++
3 files changed, 53 insertions(+)
create mode 100644 testcases/kernel/syscalls/keyctl/keyctl20.c
diff --git a/runtest/syscalls b/runtest/syscalls
index affa47e957..63a16dd5a3 100644
--- a/runtest/syscalls
+++ b/runtest/syscalls
@@ -737,6 +737,7 @@ keyctl16 keyctl16
keyctl17 keyctl17
keyctl18 keyctl18
keyctl19 keyctl19
+keyctl20 keyctl20
kcmp01 kcmp01
kcmp02 kcmp02
diff --git a/testcases/kernel/syscalls/keyctl/.gitignore b/testcases/kernel/syscalls/keyctl/.gitignore
index f321079233..acddfa79ec 100644
--- a/testcases/kernel/syscalls/keyctl/.gitignore
+++ b/testcases/kernel/syscalls/keyctl/.gitignore
@@ -17,3 +17,4 @@
/keyctl17
/keyctl18
/keyctl19
+/keyctl20
diff --git a/testcases/kernel/syscalls/keyctl/keyctl20.c b/testcases/kernel/syscalls/keyctl/keyctl20.c
new file mode 100644
index 0000000000..31f1c25ae0
--- /dev/null
+++ b/testcases/kernel/syscalls/keyctl/keyctl20.c
@@ -0,0 +1,51 @@
+// SPDX-License-Identifier: GPL-2.0-or-later
+/*
+ * Copyright (c) 2026 Andrea Cervesato <andrea.cervesato@suse.com>
+ */
+
+/*\
+ * Test ``KEYCTL_MOVE`` displacement of :manpage:`keyctl(2)`.
+ *
+ * [Algorithm]
+ *
+ * - move a key without ``KEYCTL_MOVE_EXCL`` into a destination keyring that
+ * already contains a matching key, verify it displaces the existing key
+ */
+
+#include "keyctl_common.h"
+
+#define RING_A_DESC "ltpkeyctl20_a"
+#define RING_B_DESC "ltpkeyctl20_b"
+#define KEY_DESC "k"
+#define PAYLOAD "payload"
+
+static key_serial_t ring_a, ring_b;
+static key_serial_t key_a, key_excl;
+
+static void setup(void)
+{
+ SAFE_KEYCTL(KEYCTL_JOIN_SESSION_KEYRING, 0, 0, 0, 0);
+
+ ring_a = SAFE_NEW_RING(RING_A_DESC);
+ ring_b = SAFE_NEW_RING(RING_B_DESC);
+
+ key_a = SAFE_NEW_USER_KEY(KEY_DESC, PAYLOAD, sizeof(PAYLOAD), ring_a);
+ key_excl = SAFE_NEW_USER_KEY(KEY_DESC, PAYLOAD, sizeof(PAYLOAD), ring_b);
+
+ /* Keep key_excl in session keyring so displacement does not destroy it */
+ SAFE_KEYCTL(KEYCTL_LINK, key_excl, KEY_SPEC_SESSION_KEYRING, 0, 0);
+}
+
+static void run(void)
+{
+ SAFE_KEYCTL(KEYCTL_LINK, key_a, ring_a, 0, 0);
+ TST_EXP_PASS(keyctl(KEYCTL_MOVE, key_a, ring_a, ring_b, 0));
+
+ TST_EXP_EQ_LI(keyctl(KEYCTL_SEARCH, ring_b, "user", KEY_DESC), key_a);
+}
+
+static struct tst_test test = {
+ .setup = setup,
+ .test_all = run,
+ .min_kver = "5.3",
+};
--
2.55.0
More information about the ltp
mailing list