From: "Zhu, Yi" <chuyee@google.com>
To: 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, linux-kernel@vger.kernel.org
Subject: [PATCH v2 1/3] livepatch: fix racy klp_ops lookup in klp_check_stack_func()
Date: Fri, 4 Dec 2026 04:51:39 +0000 [thread overview]
Message-ID: <2c9187f276d603835bf98ab8c60374e68deb1c03.1796357900.git.chuyee@google.com> (raw)
In-Reply-To: <cover.1796357900.git.chuyee@google.com>
Since commit e3ff7c609f39 ("livepatch,sched: Add livepatch task switching
to cond_resched()"), klp_check_stack() can run from
__klp_sched_try_switch() with only preemption disabled and without
klp_mutex. klp_module_coming() and klp_module_going() can patch and
unpatch an object of the transition patch at the same time, under
klp_mutex, and the stack check's klp_find_ops() lookup isn't safe
against that:
- klp_find_ops() walks the global klp_ops list with plain list
primitives while klp_patch_func() adds to it and klp_unpatch_func()
removes from it and frees the klp_ops with kfree() right away.
- klp_unpatch_object() only clears obj->patched after all funcs are
unpatched, and unregister_ftrace_function() sleeps for each of them.
Meanwhile the stack check still visits the object, and
klp_find_ops() returns NULL for funcs that are already unpatched,
which klp_check_stack_func() dereferences.
- Removing the last func from a func_stack leaves it empty, so both
klp_find_ops() and the list_is_singular()/list_next_entry() logic in
klp_check_stack_func() can treat the list head as a klp_func.
- klp_patch_object() sets obj->patched without ordering it after the
func_stack and klp_ops updates.
Store old_func in struct klp_ops and match on it, so a lookup doesn't
depend on the func_stack contents. Add and remove klp_ops with the RCU
list primitives, publish one only once its ftrace ops is registered,
unpublish it before emptying its func_stack and free it with
kfree_rcu(). Readers already run with preemption disabled.
In klp_check_stack_func(), skip a func whose klp_ops is gone, and find
the previous function through func->stack_node.next, which
list_del_rcu() leaves intact. Such a func belongs to a module that is
coming or going, so none of its code can be on a stack, and the core
doesn't check stacks when it patches or unpatches such an object anyway.
Set obj->patched with smp_store_release() and read it with
smp_load_acquire() in klp_check_stack(). klp_free_object_loaded() also
clears func->old_func and, for NOPs, func->new_func right after
unpatching, so mark those accesses with WRITE_ONCE()/READ_ONCE() and use
the immutable ops->old_func once the lookup succeeds.
Fixes: e3ff7c609f39 ("livepatch,sched: Add livepatch task switching to cond_resched()")
Signed-off-by: "Zhu, Yi" <chuyee@google.com>
---
kernel/livepatch/core.c | 5 ++--
kernel/livepatch/patch.c | 43 ++++++++++++++++++++++++-----------
kernel/livepatch/patch.h | 4 ++++
kernel/livepatch/transition.c | 37 ++++++++++++++++++++++++------
4 files changed, 67 insertions(+), 22 deletions(-)
diff --git a/kernel/livepatch/core.c b/kernel/livepatch/core.c
index b83715272e8c..92fbb524165a 100644
--- a/kernel/livepatch/core.c
+++ b/kernel/livepatch/core.c
@@ -724,11 +724,12 @@ static void klp_free_object_loaded(struct klp_object *obj)
obj->mod = NULL;
+ /* klp_check_stack() may still read these locklessly. */
klp_for_each_func(obj, func) {
- func->old_func = NULL;
+ WRITE_ONCE(func->old_func, NULL);
if (func->nop)
- func->new_func = NULL;
+ WRITE_ONCE(func->new_func, NULL);
}
}
diff --git a/kernel/livepatch/patch.c b/kernel/livepatch/patch.c
index 3f54a017bbf6..7330a189d6d0 100644
--- a/kernel/livepatch/patch.c
+++ b/kernel/livepatch/patch.c
@@ -20,17 +20,21 @@
#include "patch.h"
#include "transition.h"
+/*
+ * All klp_ops. Updates are serialized by klp_mutex. Lookups normally hold
+ * klp_mutex too, but the stack check can also run from __klp_sched_try_switch()
+ * with only preemption disabled, so entries are added and removed with the RCU
+ * list primitives.
+ */
static LIST_HEAD(klp_ops);
struct klp_ops *klp_find_ops(void *old_func)
{
struct klp_ops *ops;
- struct klp_func *func;
- list_for_each_entry(ops, &klp_ops, node) {
- func = list_first_entry(&ops->func_stack, struct klp_func,
- stack_node);
- if (func->old_func == old_func)
+ list_for_each_entry_rcu(ops, &klp_ops, node,
+ lockdep_is_held(&klp_mutex)) {
+ if (ops->old_func == old_func)
return ops;
}
@@ -147,9 +151,14 @@ static void klp_unpatch_func(struct klp_func *func)
WARN_ON(unregister_ftrace_function(&ops->fops));
WARN_ON(ftrace_set_filter_ip(&ops->fops, ftrace_loc, 1, 0));
+ /*
+ * Unpublish ops before emptying its func_stack. Lockless
+ * klp_find_ops() readers may still see it until a grace
+ * period has elapsed, hence kfree_rcu().
+ */
+ list_del_rcu(&ops->node);
list_del_rcu(&func->stack_node);
- list_del(&ops->node);
- kfree(ops);
+ kfree_rcu(ops, rcu);
} else {
list_del_rcu(&func->stack_node);
}
@@ -191,8 +200,7 @@ static int klp_patch_func(struct klp_func *func)
FTRACE_OPS_FL_IPMODIFY |
FTRACE_OPS_FL_PERMANENT;
- list_add(&ops->node, &klp_ops);
-
+ ops->old_func = func->old_func;
INIT_LIST_HEAD(&ops->func_stack);
list_add_rcu(&func->stack_node, &ops->func_stack);
@@ -211,7 +219,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.
+ */
+ list_add_rcu(&ops->node, &klp_ops);
} else {
list_add_rcu(&func->stack_node, &ops->func_stack);
}
@@ -222,7 +235,6 @@ static int klp_patch_func(struct klp_func *func)
err:
list_del_rcu(&func->stack_node);
- list_del(&ops->node);
kfree(ops);
return ret;
}
@@ -240,7 +252,7 @@ static void __klp_unpatch_object(struct klp_object *obj, bool nops_only)
}
if (obj->dynamic || !nops_only)
- obj->patched = false;
+ WRITE_ONCE(obj->patched, false);
}
@@ -264,7 +276,12 @@ int klp_patch_object(struct klp_object *obj)
return ret;
}
}
- obj->patched = true;
+
+ /*
+ * Publish the object to klp_check_stack() only after all of its funcs
+ * are on their func_stacks. Pairs with smp_load_acquire() there.
+ */
+ smp_store_release(&obj->patched, true);
return 0;
}
diff --git a/kernel/livepatch/patch.h b/kernel/livepatch/patch.h
index d5f2fbe373e0..bf3b9017910a 100644
--- a/kernel/livepatch/patch.h
+++ b/kernel/livepatch/patch.h
@@ -16,13 +16,17 @@
* func_stack (front of the list).
*
* @node: node for the global klp_ops list
+ * @old_func: the patched function shared by all klp_func's on @func_stack
* @func_stack: list head for the stack of klp_func's (active func is on top)
* @fops: registered ftrace ops struct
+ * @rcu: for freeing after lockless klp_find_ops() readers are done
*/
struct klp_ops {
struct list_head node;
+ void *old_func;
struct list_head func_stack;
struct ftrace_ops fops;
+ struct rcu_head rcu;
};
struct klp_ops *klp_find_ops(void *old_func);
diff --git a/kernel/livepatch/transition.c b/kernel/livepatch/transition.c
index ef70ff582da1..07e786c2d812 100644
--- a/kernel/livepatch/transition.c
+++ b/kernel/livepatch/transition.c
@@ -214,25 +214,47 @@ static int klp_check_stack_func(struct klp_func *func, unsigned long *entries,
* Check for the to-be-unpatched function
* (the func itself).
*/
- func_addr = (unsigned long)func->new_func;
+ func_addr = (unsigned long)READ_ONCE(func->new_func);
func_size = func->new_size;
} else {
+ struct list_head *next;
+
/*
* Check for the to-be-patched function
* (the previous func).
*/
- ops = klp_find_ops(func->old_func);
+ ops = klp_find_ops(READ_ONCE(func->old_func));
+
+ /*
+ * klp_module_coming() and klp_module_going() can patch and
+ * unpatch an object of the transition patch while this runs
+ * from __klp_sched_try_switch() without klp_mutex. Then ops
+ * may already be gone, or func may already be off its
+ * func_stack. The module's code can't be on any stack at that
+ * point, and the core doesn't check stacks when it patches or
+ * unpatches such an object anyway, so there is nothing to check.
+ * For the same reason, func->old_func and func->new_func may be
+ * cleared by klp_free_object_loaded() meanwhile; use the
+ * immutable ops->old_func once ops is found.
+ *
+ * list_del_rcu() leaves func->stack_node.next pointing to the
+ * previous func or to the func_stack head, so use it rather than
+ * list_is_singular(), which an emptied func_stack would defeat.
+ */
+ if (!ops)
+ return 0;
- if (list_is_singular(&ops->func_stack)) {
+ next = READ_ONCE(func->stack_node.next);
+ if (next == &ops->func_stack) {
/* original function */
- func_addr = (unsigned long)func->old_func;
+ func_addr = (unsigned long)ops->old_func;
func_size = func->old_size;
} else {
/* previously patched function */
struct klp_func *prev;
- prev = list_next_entry(func, stack_node);
- func_addr = (unsigned long)prev->new_func;
+ prev = list_entry(next, struct klp_func, stack_node);
+ func_addr = (unsigned long)READ_ONCE(prev->new_func);
func_size = prev->new_size;
}
}
@@ -267,7 +289,8 @@ static int klp_check_stack(struct task_struct *task, const char **oldname)
nr_entries = ret;
klp_for_each_object(klp_transition_patch, obj) {
- if (!obj->patched)
+ /* Pairs with smp_store_release() in klp_patch_object(). */
+ if (!smp_load_acquire(&obj->patched))
continue;
klp_for_each_func(obj, func) {
ret = klp_check_stack_func(func, entries, nr_entries);
--
2.56.1.987.g2ec0ab4260-goog
next prev parent reply other threads:[~2026-12-04 4:51 UTC|newest]
Thread overview: 8+ 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:35 ` [PATCH 2/2] livepatch: check task stacks against a sorted range table Zhu, Yi
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 ` Zhu, Yi [this message]
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
[not found] ` <sashiko-outbox-249141@kernel.org>
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=2c9187f276d603835bf98ab8c60374e68deb1c03.1796357900.git.chuyee@google.com \
--to=chuyee@google.com \
--cc=dylanbhatch@google.com \
--cc=joe.lawrence@redhat.com \
--cc=jpoimboe@kernel.org \
--cc=linux-kernel@vger.kernel.org \
--cc=live-patching@vger.kernel.org \
--cc=mbenes@suse.cz \
--cc=pmladek@suse.com \
--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 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®