From: sashiko-bot@kernel.org
To: "Zhu, Yi" <chuyee@google.com>,
"Josh Poimboeuf" <jpoimboe@kernel.org>,
"Miroslav Benes" <mbenes@suse.cz>,
"Petr Mladek" <pmladek@suse.com>,
"Joe Lawrence" <joe.lawrence@redhat.com>,
"Song Liu" <song@kernel.org>,
"Wilson Felipe Pereira" <wfelipe@google.com>,
"Dylan Hatch" <dylanbhatch@google.com>
Cc: live-patching@vger.kernel.org
Subject: Re: [PATCH 1/2] livepatch: look up klp_ops in a hash table
Date: Fri, 04 Dec 2026 00:51:19 +0000 [thread overview]
Message-ID: <sashiko-outbox-248859@kernel.org> (raw)
In-Reply-To: <3f1027446e0fb57f76fca545840f99f19fa0237c.1796344045.git.chuyee@google.com>
Thank you for your contribution! Sashiko AI review found 2 potential
issues to consider:
- [High] livepatch: NULL deref in klp_check_stack_func() during
klp_unpatch_func()
- [Medium] livepatch: missing obj->patched ordering before
klp_find_ops() in stack check
--
commit cf2b103b9d30e9b6b5eff152bfd055a1002ecd44
Author: Zhu, Yi <chuyee@google.com>
livepatch: look up klp_ops in a hash table
This commit replaces the global klp_ops linked list with an RCU-protected
hash table keyed on old_func to speed up klp_find_ops() lookups, moving
entry publication after ftrace registration and unpublishing before ftrace
unregistration with kfree_rcu().
> diff --git a/kernel/livepatch/patch.c b/kernel/livepatch/patch.c
> index 3f54a017bbf69..e93105187bc43 100644
> --- a/kernel/livepatch/patch.c
> +++ b/kernel/livepatch/patch.c
[ ... ]
> @@ -144,12 +150,17 @@ static void klp_unpatch_func(struct klp_func *func)
> if (WARN_ON(!ftrace_loc))
> return;
>
> + /*
> + * Unpublish ops before tearing it down. Lockless
> + * klp_find_ops() readers may still see it until a grace
> + * period has elapsed, hence kfree_rcu().
> + */
> + hash_del_rcu(&ops->node);
> WARN_ON(unregister_ftrace_function(&ops->fops));
> WARN_ON(ftrace_set_filter_ip(&ops->fops, ftrace_loc, 1, 0));
>
> list_del_rcu(&func->stack_node);
> - list_del(&ops->node);
> - kfree(ops);
> + kfree_rcu(ops, rcu);
> } else {
> list_del_rcu(&func->stack_node);
> }
[Severity: High]
Can unpublishing ops before unregister_ftrace_function() cause a NULL
pointer dereference in klp_check_stack_func()?
When a target module is unloaded during an active transition,
__klp_unpatch_object() leaves obj->patched true until every
klp_unpatch_func() call finishes. For FTRACE_OPS_FL_DYNAMIC livepatch ops,
unregister_ftrace_function() blocks across synchronize_rcu_tasks_rude() and
synchronize_rcu_tasks(), keeping ops unpublished for milliseconds while
tasks schedule on other CPUs:
CPU0 CPU1
klp_module_going()
...
klp_unpatch_object(obj)
klp_unpatch_func(func)
hash_del_rcu(&ops->node)
unregister_ftrace_function(...)
__schedule()
__klp_sched_try_switch()
klp_check_stack()
/* obj->patched is still true */
klp_check_stack_func()
ops = klp_find_ops(...) /* NULL */
list_is_singular(&ops->func_stack)
<-- NULL pointer dereference
Even if klp_find_ops() returns ops just before hash_del_rcu(),
list_del_rcu(&func->stack_node) can empty ops->func_stack before the reader
checks list_is_singular(), sending klp_check_stack_func() into the else
branch with a type-confused list_next_entry(func, stack_node).
[ ... ]
> @@ -211,7 +221,12 @@ static int klp_patch_func(struct klp_func *func)
> goto err;
> }
>
> -
> + /*
> + * Publish ops for lockless klp_find_ops() readers only once it
> + * is fully set up, so that the error path above can free it
> + * right away.
> + */
> + hash_add_rcu(klp_ops_table, &ops->node, (unsigned long)ops->old_func);
> } else {
> list_add_rcu(&func->stack_node, &ops->func_stack);
> }
[Severity: Medium]
On weakly-ordered architectures, can obj->patched become visible to
klp_check_stack() before the hash_add_rcu() bucket update?
Moving hash_add_rcu() after register_ftrace_function() leaves no barrier
ordering it before klp_patch_object() sets obj->patched = true, because
hash_add_rcu()'s store-release only orders prior stores before the bucket
write. In klp_check_stack(), the plain load of obj->patched has only a
control dependency before klp_find_ops() reads klp_ops_table:
CPU0 CPU1
klp_module_coming()
klp_patch_object(obj)
klp_patch_func(func)
hash_add_rcu(...)
obj->patched = true
__klp_sched_try_switch()
klp_check_stack()
if (!obj->patched) /* sees true */
klp_check_stack_func()
klp_find_ops(...) /* sees NULL */
list_is_singular(&ops->func_stack)
<-- NULL pointer dereference
Publishing obj->patched with smp_store_release() paired with
smp_load_acquire() in klp_check_stack() would order the hash table update
before the reader's lookup.
--
Sashiko AI review · https://sashiko.dev/#/patchset/cover.1796344045.git.chuyee@google.com?part=1
next prev parent reply other threads:[~2026-12-04 0:51 UTC|newest]
Thread overview: 11+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-12-04 0:35 [PATCH 0/2] livepatch: make the transition stack check scale with patch size Zhu, Yi
2026-12-04 0:35 ` [PATCH 1/2] livepatch: look up klp_ops in a hash table Zhu, Yi
2026-12-04 0:51 ` sashiko-bot [this message]
2026-12-04 0:35 ` [PATCH 2/2] livepatch: check task stacks against a sorted range table Zhu, Yi
2026-12-04 0:59 ` sashiko-bot
2026-12-04 4:51 ` [PATCH v2 0/3] livepatch: make the transition stack check scale with patch size Zhu, Yi
2026-12-04 4:51 ` [PATCH v2 1/3] livepatch: fix racy klp_ops lookup in klp_check_stack_func() Zhu, Yi
2026-12-04 4:51 ` [PATCH v2 2/3] livepatch: look up klp_ops in a hash table Zhu, Yi
2026-12-04 4:51 ` [PATCH v2 3/3] livepatch: check task stacks against a sorted range table Zhu, Yi
2026-12-04 5:25 ` sashiko-bot
2026-12-04 18:02 ` Zhu, Yi
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=sashiko-outbox-248859@kernel.org \
--to=sashiko-bot@kernel.org \
--cc=chuyee@google.com \
--cc=dylanbhatch@google.com \
--cc=joe.lawrence@redhat.com \
--cc=jpoimboe@kernel.org \
--cc=live-patching@vger.kernel.org \
--cc=mbenes@suse.cz \
--cc=pmladek@suse.com \
--cc=sashiko-reviews@lists.linux.dev \
--cc=song@kernel.org \
--cc=wfelipe@google.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 an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.