summaryrefslogtreecommitdiff
path: root/include/linux/livepatch.h
diff options
context:
space:
mode:
authorYafang Shao <laoar.shao@gmail.com>2026-08-30 13:48:56 +0800
committerPetr Mladek <pmladek@suse.com>2026-09-04 10:28:52 +0200
commit7f4ca0d0b40ac1ccd82ebaf1514c2d26c3db71c4 (patch)
treec06b77664cce847faa15458731d8fc4ca7db32c9 /include/linux/livepatch.h
parent26260251022fbc2f248a3d747a9b2b961b18d2d8 (diff)
downloadlinux-next-7f4ca0d0b40ac1ccd82ebaf1514c2d26c3db71c4.tar.gz
linux-next-7f4ca0d0b40ac1ccd82ebaf1514c2d26c3db71c4.zip
livepatch: Fix UAF of unregistered patch kobjects
The kobjects of a livepatch are released via kobject_put(). When CONFIG_DEBUG_KOBJECT_RELEASE is enabled, kobject_put() does not release the kobject synchronously but schedules a delayed release with a random delay of up to 4 seconds (see kobject_release() in lib/kobject.c). klp_free_patch_finish() only waits for the release of the patch kobject: klp_free_patch_finish(): kobject_put(&patch->kobj); wait_for_completion(&patch->finish); patch->finish is completed by the patch kobject's release callback. If the patch kobject was never added to sysfs, or if some child kobjects were initialized but never added to sysfs (e.g. when klp_enable_patch() fails after klp_init_patch_early()), those un-added children do not hold a reference on the patch kobject. kobject_add() is what takes the parent reference, so the patch kobject can be released first, completing patch->finish while the child releases are still pending. The caller then unloads the livepatch module, which destroys the static klp_object and klp_func structures. The delayed child release callbacks later access this freed memory, causing a use-after-free. Fix it by making every child kobject hold an explicit reference on its parent from the moment the object is initialized: klp_init_object_early() takes a reference on the patch kobject and klp_init_func_early() takes a reference on the object kobject. Unlike the reference taken by kobject_add(), these references also exist for objects that are never added to sysfs, and the release callbacks drop them unconditionally. This guarantees the patch kobject is released only after all child kobjects have been released, so patch->finish cannot be completed before the static structures are safe to free. Because kobj->parent is set only by kobject_add(), add explicit back-pointers, obj->patch and func->obj, so the release callbacks can find the parent. Dynamic objects and nop functions are freed by their release callbacks; save the parent pointer before freeing and drop the parent reference afterwards. Reported-by: sashiko-bot <sashiko-bot@kernel.org> Closes: https://lore.kernel.org/all/20260809094046.50ED31F000E9@smtp.kernel.org/ Suggested-by: Petr Mladek <pmladek@suse.com> Signed-off-by: Yafang Shao <laoar.shao@gmail.com> Acked-by: Song Liu <song@kernel.org> Reviewed-by: Petr Mladek <pmladek@suse.com> Tested-by: Petr Mladek <pmladek@suse.com> Link: https://patch.msgid.link/20260830054857.64758-2-laoar.shao@gmail.com Signed-off-by: Petr Mladek <pmladek@suse.com>
Diffstat (limited to 'include/linux/livepatch.h')
-rw-r--r--include/linux/livepatch.h4
1 files changed, 4 insertions, 0 deletions
diff --git a/include/linux/livepatch.h b/include/linux/livepatch.h
index ba9e3988c07c..5f74f79c22b4 100644
--- a/include/linux/livepatch.h
+++ b/include/linux/livepatch.h
@@ -33,6 +33,7 @@
* @kobj: kobject for sysfs resources
* @node: list node for klp_object func_list
* @stack_node: list node for klp_ops func_stack list
+ * @obj: back pointer to the owning object
* @old_size: size of the old function
* @new_size: size of the new function
* @nop: temporary patch to use the original code again; dyn. allocated
@@ -72,6 +73,7 @@ struct klp_func {
struct kobject kobj;
struct list_head node;
struct list_head stack_node;
+ struct klp_object *obj;
unsigned long old_size, new_size;
bool nop;
bool patched;
@@ -86,6 +88,7 @@ struct klp_func {
* @kobj: kobject for sysfs resources
* @func_list: dynamic list of the function entries
* @node: list node for klp_patch obj_list
+ * @patch: back pointer to the owning patch
* @mod: kernel module associated with the patched object
* (NULL for vmlinux)
* @dynamic: temporary object for nop functions; dynamically allocated
@@ -101,6 +104,7 @@ struct klp_object {
struct kobject kobj;
struct list_head func_list;
struct list_head node;
+ struct klp_patch *patch;
struct module *mod;
bool dynamic;
bool patched;