Skip to contentSkip to conversation

keys: make keyring indexes independent of kernel addresses

PATCH In review Kyle Zeng 3 participants 4 messages Oct 13 – Oct 14

Subsystems (2)
lore
Share & tools
lore

Kyle Zeng

author
#0 lore raw

PATCH v2 1/1 keys: make keyring indexes independent of kernel addresses

KEYCTL_READ returns the serial numbers in a keyring in associative-array traversal order. The first word of the index is a public, deterministic hash of the type pointer, domain pointer and description. An unprivileged user can therefore insert keys with known descriptions, predict their order for each possible KASLR slide, and recover the kernel image address. On an x86-64 build with 16 MiB alignment, 45 user keys suffice to distinguish the candidate slides.

Remove the addresses from the whole index, not just its first hash word. Assign each key type and domain a nonzero 64-bit ID and hash those IDs and the description with a once-initialized SipHash key. Use the full IDs in the later index chunks as well. Truncating or hashing the IDs there would lose the guarantee that distinct type/domain/description tuples have a differing index bit.

Allocate IDs lazily since private key types need not be registered. Cache them in the index key so tree walks and copies of an existing index do not need to dereference a type descriptor that may have been unregistered. Keep the 32-bit chunk layout and diff_objects() bit offsets consistent, and retain the separate root slot for keyrings used by recursive search. The key comparison, permissions and userspace data format are unchanged; only the address-dependent in-memory ordering is replaced.

Fixes b2a4df200d57 ("KEYS: Expand the capacity of a keyring")
Assisted-by Codex:gpt-6-astra

---
Changes in v2:
- Use reverse Christmas tree order for local variable declarations, preserving initializer dependencies. No functional changes.

 include/linux/key-type.h |    1 
 include/linux/key.h      |    5 ++
 security/keys/keyring.c  |  150 +++++++++++++++++++++++++---------------------
 3 files changed, 86 insertions(+), 70 deletions(-)
include/linux/key-type.h +1 -0
index bb97bd3e5af4..c4f329b6ae3f 100644
@@ -161,6 +161,7 @@ struct key_type {
 				     const void *in, const void *in2);
 
 	/* internal fields */
+	atomic64_t		index_id;	/* Opaque keyring index ID */
 	struct list_head	link;		/* link in types list */
 	struct lock_class_key	lock_class;	/* key->sem lock class */
 } __randomize_layout;
include/linux/key.h +5 -0
index 81b8f05c6898..e40b5267658f 100644
@@ -107,6 +107,7 @@ struct keyring_name;
 
 struct key_tag {
 	struct rcu_head		rcu;
+	atomic64_t		index_id;	/* Opaque keyring index ID */
 	refcount_t		usage;
 	bool			removed;	/* T when subject removed */
 };
@@ -129,6 +130,8 @@ struct keyring_index_key {
 	struct key_type		*type;
 	struct key_tag		*domain_tag;	/* Domain of operation */
 	const char		*description;
+	u64			type_id;	/* Cached index IDs */
+	u64			domain_id;
 };
 
 union key_payload {
@@ -251,6 +254,8 @@ struct key {
 			struct key_type	*type;		/* type of key */
 			struct key_tag	*domain_tag;	/* Domain of operation */
 			char		*description;
+			u64		type_id;
+			u64		domain_id;
 		};
 	};
 
security/keys/keyring.c +80 -70

225 lines not on this page · raw message

Signature · 1 line
2.53.0
#1 lore raw
Sashiko has reviewed this patch and found no issues. It looks great!
Quoted 23 lines

KEYCTL_READ returns the serial numbers in a keyring in associative-array traversal order. The first word of the index is a public, deterministic hash of the type pointer, domain pointer and description. An unprivileged user can therefore insert keys with known descriptions, predict their order for each possible KASLR slide, and recover the kernel image address. On an x86-64 build with 16 MiB alignment, 45 user keys suffice to distinguish the candidate slides.

Remove the addresses from the whole index, not just its first hash word. Assign each key type and domain a nonzero 64-bit ID and hash those IDs and the description with a once-initialized SipHash key. Use the full IDs in the later index chunks as well. Truncating or hashing the IDs there would lose the guarantee that distinct type/domain/description tuples have a differing index bit.

Allocate IDs lazily since private key types need not be registered. Cache them in the index key so tree walks and copies of an existing index do not need to dereference a type descriptor that may have been unregistered. Keep the 32-bit chunk layout and diff_objects() bit offsets consistent, and retain the separate root slot for keyrings used by recursive search. The key comparison, permissions and userspace data format are unchanged; only the address-dependent in-memory ordering is replaced.

Quoted 3 lines
Fixes b2a4df200d57 ("KEYS: Expand the capacity of a keyring")
Assisted-by Codex:gpt-6-astra

Sashiko has reviewed this patch and found no issues. It looks great!

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1
maintainer Reviewed-by on v2 1/1
#2 lore raw
In reply to Kyle Zeng
Quoted 23 lines · Kyle Zeng

KEYCTL_READ returns the serial numbers in a keyring in associative-array traversal order. The first word of the index is a public, deterministic hash of the type pointer, domain pointer and description. An unprivileged user can therefore insert keys with known descriptions, predict their order for each possible KASLR slide, and recover the kernel image address. On an x86-64 build with 16 MiB alignment, 45 user keys suffice to distinguish the candidate slides.

Remove the addresses from the whole index, not just its first hash word. Assign each key type and domain a nonzero 64-bit ID and hash those IDs and the description with a once-initialized SipHash key. Use the full IDs in the later index chunks as well. Truncating or hashing the IDs there would lose the guarantee that distinct type/domain/description tuples have a differing index bit.

Allocate IDs lazily since private key types need not be registered. Cache them in the index key so tree walks and copies of an existing index do not need to dereference a type descriptor that may have been unregistered. Keep the 32-bit chunk layout and diff_objects() bit offsets consistent, and retain the separate root slot for keyrings used by recursive search. The key comparison, permissions and userspace data format are unchanged; only the address-dependent in-memory ordering is replaced.

Quoted 3 lines
Fixes b2a4df200d57 ("KEYS: Expand the capacity of a keyring")
Assisted-by Codex:gpt-6-astra

I'll apply this.

And thanks for addressing that and sorry for nitpicking. Looks good to me and thanks for addressing the bug. This is kind of AI assisted commit to works for me; and I believe that checks are balances are in place.

Not everyone gets this, and the problem is that they also in effect reduce the number of point of failures from two to one due ignorance-by-a-choice.

A person submitting an AI-assisted patch is also a reviewer, and ignoring this increases the probability of bad getting into mainline quite literally.

I think some people might not really understand this at all but is just that it is much less likely that two people get a patch wrong and at the same time, than just one. That's it ;-)

This is not an AI/LLM problem. It is a misconduct by people quite well scoped really...

Sorry for the rant just wanted to write this down and get this out of my chest. Absolutely nothing to do with this patch.

Br, Jarkko

maintainer
#3 lore raw
Quoted 24 lines · Jarkko Sakkinen
Quoted 23 lines · Kyle Zeng

KEYCTL_READ returns the serial numbers in a keyring in associative-array traversal order. The first word of the index is a public, deterministic hash of the type pointer, domain pointer and description. An unprivileged user can therefore insert keys with known descriptions, predict their order for each possible KASLR slide, and recover the kernel image address. On an x86-64 build with 16 MiB alignment, 45 user keys suffice to distinguish the candidate slides.

Remove the addresses from the whole index, not just its first hash word. Assign each key type and domain a nonzero 64-bit ID and hash those IDs and the description with a once-initialized SipHash key. Use the full IDs in the later index chunks as well. Truncating or hashing the IDs there would lose the guarantee that distinct type/domain/description tuples have a differing index bit.

Allocate IDs lazily since private key types need not be registered. Cache them in the index key so tree walks and copies of an existing index do not need to dereference a type descriptor that may have been unregistered. Keep the 32-bit chunk layout and diff_objects() bit offsets consistent, and retain the separate root slot for keyrings used by recursive search. The key comparison, permissions and userspace data format are unchanged; only the address-dependent in-memory ordering is replaced.

Quoted 8 lines
Quoted 3 lines
Fixes b2a4df200d57 ("KEYS: Expand the capacity of a keyring")
Assisted-by Codex:gpt-6-astra

I'll apply this.

Now I did it, and your patch is now in my master branch (not yet -next, will get there). Thank you.

It took a while because kunit test for assoc-array took almost the whole day [1] (mainly figuring out test data that is not too much pain to recap later on that replaced lottery I originally had).

[1] https://lore.kernel.org/keyrings/[email protected]/T/#u

Br, Jarkko