[PATCH v2 6/6] KEYS: Verify the key identity in __keyctl_unlink

From: Jarkko Sakkinen

Date: Mon Oct 19 2026 - 15:24:12 EST


Let's define the following preconditions:

- Two keys: A and B.
- R is a keyring.
- Both have the same type/description/domain.

Then:

1. KEYCTL_LINK(A, R) => {A}
1. KEYCTL_LINK(B, R) => {B}
2. KEYCTL_UNLINK(A, R) => {}

Instead, verify key tidentity before unlinking, and return -ENOENT on
mismatch.

Assisted-by: LLM
Cc: stable@xxxxxxxxxxxxxxx
Fixes: b2a4df200d57 ("KEYS: Expand the capacity of a keyring")
Reviewed-by: David Howells <dhowells@xxxxxxxxxx>
Signed-off-by: Jarkko Sakkinen <jarkko@xxxxxxxxxx>
---
security/keys/keyring.c | 6 ++++++
1 file changed, 6 insertions(+)

diff --git a/security/keys/keyring.c b/security/keys/keyring.c
index 7143f3079d20..b025331488c5 100644
--- a/security/keys/keyring.c
+++ b/security/keys/keyring.c
@@ -1501,9 +1501,15 @@ static int __key_unlink_begin(struct key *keyring, struct key *key,
struct assoc_array_edit **_edit)
{
struct assoc_array_edit *edit;
+ const void *object;

BUG_ON(*_edit != NULL);

+ object = assoc_array_find(&keyring->keys, &keyring_assoc_array_ops,
+ &key->index_key);
+ if (!object || keyring_ptr_to_key(object) != key)
+ return -ENOENT;
+
edit = assoc_array_delete(&keyring->keys, &keyring_assoc_array_ops,
&key->index_key);
if (IS_ERR(edit))
--
2.47.3