mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: Kyle Zeng <kylebot@openai.com>
To: linux-kernel@vger.kernel.org
Cc: akpm@linux-foundation.org, outbounddisclosures@openai.com,
	Kyle Zeng <kylebot@openai.com>
Subject: [PATCH] assoc_array: preserve shortcut prefixes when splitting
Date: Tue,  6 Oct 2026 15:14:09 -0700	[thread overview]
Message-ID: <20261006221409.35477-1-kylebot@openai.com> (raw)

assoc_array_insert_mid_shortcut() advances past a parent node's key
segment when deciding whether a pre-shortcut is needed.  A root shortcut
has no such parent.  If the first differing nibble is at bit 4, the code
therefore installs a node indexed at level 4 directly at the root, where
assoc_array_walk() starts at level 0.  A successfully inserted object can
then be unreachable by lookup.

For keyrings, a user can turn these lookup failures into an oracle for
the pointer-derived index hash and recover the KASLR slide of a known
kernel image.

Only advance past the parent segment when the old shortcut actually has
a parent.  This retains the prefix before a root split at bit 4 while
leaving the non-root and bit-0 cases unchanged.

Also preserve the last complete prefix word when the split is
word-aligned.  The unconditional zero-bit shift currently clears that
word and can make both sides of the split unsearchable.  Use the same
partial-word guard as the terminal-node shortcut constructor.

Fixes: 3cb989501c26 ("Add a generic associative array implementation.")
Assisted-by: Codex:gpt-6-astra
Signed-off-by: Kyle Zeng <kylebot@openai.com>
---
 lib/assoc_array.c | 13 ++++++++-----
 1 file changed, 8 insertions(+), 5 deletions(-)

diff --git a/lib/assoc_array.c b/lib/assoc_array.c
index b6c9723e12ce..ee4f3994f494 100644
--- a/lib/assoc_array.c
+++ b/lib/assoc_array.c
@@ -841,9 +841,10 @@ static bool assoc_array_insert_mid_shortcut(struct assoc_array_edit *edit,
 
 	/* Insert a new shortcut before the new node if this segment isn't of
 	 * zero length - otherwise we just connect the new node directly to the
-	 * parent.
+	 * parent.  A root shortcut has no parent consuming the first level.
 	 */
-	level += ASSOC_ARRAY_LEVEL_STEP;
+	if (shortcut->back_pointer)
+		level += ASSOC_ARRAY_LEVEL_STEP;
 	if (diff > level) {
 		pr_devel("pre-shortcut %d...%d\n", level, diff);
 		keylen = round_up(diff, ASSOC_ARRAY_KEY_CHUNK_SIZE);
@@ -865,9 +866,11 @@ static bool assoc_array_insert_mid_shortcut(struct assoc_array_edit *edit,
 		memcpy(new_s0->index_key, shortcut->index_key,
 		       flex_array_size(new_s0, index_key, keylen));
 
-		blank = ULONG_MAX << (diff & ASSOC_ARRAY_KEY_CHUNK_MASK);
-		pr_devel("blank off [%zu] %d: %lx\n", keylen - 1, diff, blank);
-		new_s0->index_key[keylen - 1] &= ~blank;
+		if (diff & ASSOC_ARRAY_KEY_CHUNK_MASK) {
+			blank = ULONG_MAX << (diff & ASSOC_ARRAY_KEY_CHUNK_MASK);
+			pr_devel("blank off [%zu] %d: %lx\n", keylen - 1, diff, blank);
+			new_s0->index_key[keylen - 1] &= ~blank;
+		}
 	} else {
 		pr_devel("no pre-shortcut\n");
 		edit->set[0].to = assoc_array_node_to_ptr(new_n0);

                 reply	other threads:[~2026-10-06 22:14 UTC|newest]

Thread overview: [no followups] expand[flat|nested]  mbox.gz  Atom feed

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20261006221409.35477-1-kylebot@openai.com \
    --to=kylebot@openai.com \
    --cc=akpm@linux-foundation.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=outbounddisclosures@openai.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox

all inboxes | Powered by JetHome®