livepatch: Introduce replace set support
Subsystems (3)
v5 cover livepatch: Introduce replace set support
We previously proposed a BPF+livepatch method to enable rapid experimentation with new kernel features without interrupting production workloads:
https://lore.kernel.org/live-patching/[email protected]/
In the resulting discussion, Song and Petr suggested adding a "replace set" to support scenarios where specific livepatches can be selectively replaced or skipped.
This patchset introduces a more flexible model using two new fields in struct klp_patch:
- provides: an unsigned int id identifying the patch replace set. By default (provides=0), any livepatch replaces any other livepatch.
- obsoletes: an optional array of unsigned int ids specifying additional provides ids to be replaced. This allows a new patch to explicitly obsolete patches from different replace sets.
A new livepatch atomically replaces any existing livepatch whose provides id matches either:
1. The new patch provides id (same replace set), or
2. Any id in the new patch obsoletes list
Additionally, this design deprecates the traditional non-atomic-replace model. Previously, setting 'replace' to 0 was the only way to keep certain livepatches persistent on the system, forcing developers to disable atomic replacement entirely. With the introduction of replace set, developers now have a selective option to keep specific livepatches persistent while maintaining atomic replacement capabilities elsewhere.
At present, KLP state, shadow variables, and callbacks are not integrated with the new replace_set mechanism in this patchset. Support for these features is deferred until Petr's klp-state-transfer infrastructure is completed and merged:
https://github.com/pmladek/linux/tree/klp-state-transfer-v1-iter12
Future Work
===========
- Allow provides and obsoletes to be configured dynamically at module load time, rather than being fixed at build time.
Changes
=======
v4(RFC)->v5:
- Add selftests and Remove the RFC
- Fmprove klp_has_function_conflict() (Song)
- Fix a pre-exisiting bug
- Fix bugs reported by sashiko
v4 (RFC): https://lore.kernel.org/live-patching/[email protected]/
v3->v4(RFC):
- Allow a livepatch to replace livepatches with different provides IDs. Replace the single replace_set field with two separate fields, provides and obsoletes, for more flexible replacement semantics. (Petr, Joe)
v3: https://lore.kernel.org/live-patching/[email protected]/
v2->v3:
- Address the feedback from Sachiko AI
- Fix the pre-existing NULL pointer dereference issue
- Move klp_find_func into core.h
- Don't deprecate stack_order completely
v2: https://lore.kernel.org/live-patching/[email protected]/
v1->v2:
- Incorporate feedback from Petr:
- Initialize replace_set to 0 by default
- Improve documentation
- Enforce that livepatches in different replace_sets cannot use the same
state->id.
- Enforce that livepatches in different replace_sets cannot modify the same function.
- Ensure consistent capitalization and naming usage of KLP_REPLACE_SET.
- Incorporate feedback from Sachiko AI:
- Skip the klp_transition patch during klp_force_transition().
v1 (RFC): https://lore.kernel.org/live-patching/[email protected]/
Yafang Shao (9):
livepatch: Fix wrong index in funcs cleanup error path
livepatch: Make klp_find_func() non static
livepatch: Call klp_init_patch_early() earlier
livepatch: Implement replace set for scoped atomic replace
livepatch: Deprecate stack_order
selftests: livepatch: Adapt atomic replace tests to provides/obsoletes
selftests: livepatch: Add provides/obsoletes test scenarios
selftests: livepatch: Add test for state ID conflict across provides
selftests: livepatch: Add test for function conflict across provides
.../ABI/removed/sysfs-kernel-livepatch | 16 + .../ABI/testing/sysfs-kernel-livepatch | 27 +- .../livepatch/cumulative-patches.rst | 93 ++-- Documentation/livepatch/livepatch.rst | 23 +- include/linux/livepatch.h | 7 +- kernel/livepatch/core.c | 114 +++-- kernel/livepatch/core.h | 2 + kernel/livepatch/state.c | 57 ++- kernel/livepatch/transition.c | 11 +- scripts/livepatch/init.c | 71 +++- scripts/livepatch/klp-build | 77 +++- tools/testing/selftests/livepatch/Makefile | 3 +- .../testing/selftests/livepatch/functions.sh | 15 + .../selftests/livepatch/test-callbacks.sh | 6 + .../selftests/livepatch/test-livepatch.sh | 6 + .../livepatch/test-provides-obsoletes.sh | 401 ++++++++++++++++++ .../selftests/livepatch/test_modules/Makefile | 11 + .../test_modules/test_klp_atomic_replace.c | 22 + .../test_modules/test_klp_callbacks_demo2.c | 12 + .../test_modules/test_klp_livepatch.c | 9 + .../test_modules/test_klp_provides.c | 72 ++++ .../livepatch/test_modules/test_klp_state.c | 13 + .../livepatch/test_modules/test_klp_state2.c | 23 + 23 files changed, 968 insertions(+), 123 deletions(-) create mode 100644 Documentation/ABI/removed/sysfs-kernel-livepatch create mode 100755 tools/testing/selftests/livepatch/test-provides-obsoletes.sh create mode 100644 tools/testing/selftests/livepatch/test_modules/test_klp_provides.c
Signature · 1 line
2.52.0
PATCH v5 1/9 livepatch: Fix wrong index in funcs cleanup error path
In the object allocation loop, when kzalloc() for funcs fails, the cleanup loop uses objs[i].funcs instead of objs[j].funcs. Since objs[i].funcs is still NULL at that point, it repeatedly calls kfree(NULL) and leaks all previously allocated funcs arrays. Fix by using the correct loop variable j.
--- scripts/livepatch/init.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
scripts/livepatch/init.c +1 -1
@@ -51,7 +51,7 @@ static int __init livepatch_mod_init(void)if (!funcs) { ret = -ENOMEM ;for (int j =0 ; j < i; j++) -kfree (objs[i].funcs); +kfree (objs[j].funcs); goto err_free_objs; }
Signature · 1 line
2.52.0
[email protected]
AI reviewRe: [PATCH v5 1/9] livepatch: Fix wrong index in funcs cleanup error path
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider: - [High] The patch introduces a double free vulnerability on the `funcs` allocation error path. --
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [High] The patch introduces a double free vulnerability on the funcs allocation error path. --
--- Patch [1]: [PATCH v5 1/9] livepatch: Fix wrong index in funcs cleanup error path --- commit c03811c85b7c79e0d681d9978c346e4424c48a99
Author: Yafang Shao <[email protected]>
livepatch: Fix wrong index in funcs cleanup error path
This commit modifies the cleanup loop in livepatch_mod_init() to use the index j instead of i when kzalloc() fails for a function array. This attempts to prevent leaking previously allocated function arrays.
scripts/livepatch/init.c static int __init livepatch_mod_init(void) · line 54
Quoted 10 lines
scripts/livepatch/init.c +1
-1@@ -51,7 +51,7 @@ static int __init livepatch_mod_init(void)if (!funcs) { ret = -ENOMEM ;for (int j =0 ; j < i; j++) -kfree (objs[i].funcs); +kfree (objs[j].funcs);
[Severity: High]
Does this introduce a double free in livepatch_mod_init()?
When the inner loop here frees objs[j].funcs, it leaves the dangling pointer in the objs array. The code then jumps to err_free_objs, which iterates over all objects and frees the arrays again:
err_free_objs:
for (int i = 0; i < nr_objs; i++) kfree(objs[i].funcs);
Since the pointers weren't set to NULL in the inner loop, won't the err_free_objs cleanup loop free the identical memory allocations a second time?
Quoted 3 lines
scripts/livepatch/init.c +0
-0goto err_free_objs; }
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1
Re: [PATCH v5 1/9] livepatch: Fix wrong index in funcs cleanup error path
Quoted 33 lines · On Sun, Aug 9, 2026 at 5:28 PM <[email protected]> wrote:
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [High] The patch introduces a double free vulnerability on thefuncsallocation error path. ----- Patch [1]: [PATCH v5 1/9] livepatch: Fix wrong index in funcs cleanup error path --- commit
c03811c85b7c79e0d681d9978c346e4424c48a99
Author: Yafang Shao <[email protected]>livepatch: Fix wrong index in funcs cleanup error path
This commit modifies the cleanup loop in
livepatch_mod_init()to use the index j instead of i whenkzalloc()fails for a function array. This attempts to prevent leaking previously allocated function arrays.scripts/livepatch/init.c static int __init livepatch_mod_init(void) · line 54
Quoted 10 lines
scripts/livepatch/init.c +1
-1@@ -51,7 +51,7 @@ static int __init livepatch_mod_init(void)if (!funcs) { ret = -ENOMEM ;for (int j =0 ; j < i; j++) -kfree (objs[i].funcs); +kfree (objs[j].funcs);[Severity: High]
Does this introduce a double free inlivepatch_mod_init()?When the inner loop here frees objs[j].funcs, it leaves the dangling pointer in the objs array. The code then jumps to
err_free_objs, which iterates over all objects and frees the arrays again:
Quoted 7 lines
err_free_objs:for (int i = 0; i < nr_objs; i++) kfree(objs[i].funcs);Since the pointers weren't set to NULL in the inner loop, won't the
err_free_objscleanup loop free the identical memory allocations a second time?
This is an invalid review issue. Since kfree(objs[i].funcs) is not added to err_free_objs in the current state, and it is moved from its current place to err_free_objs in patch #4.
Signature · 2 lines
Regards Yafang
PATCH v5 2/9 livepatch: Make klp_find_func() non static
In reply to Yafang ShaoMake klp_find_func() non static to allow its use in other source files by an upcoming patch.
While at it, rename the parameter @old_func to @func to better reflect its generic purpose.
--- kernel/livepatch/core.c | 18 +++++++++--------- kernel/livepatch/core.h | 1 + 2 files changed, 10 insertions(+), 9 deletions(-)
kernel/livepatch/core.c +9 -9
@@ -82,21 +82,21 @@ static bool klp_initialized(void)return !!klp_root_kobj; } -static struct klp_func *klp_find_func (struct klp_object *obj, -struct klp_func *old_func) +/* Check if @func is present in @obj. */ +struct klp_func *klp_find_func (struct klp_object *obj,struct klp_func *func) { -struct klp_func *func; +struct klp_func *obj_func; -klp_for_each_func (obj, func) { +klp_for_each_func (obj, obj_func) {/* * Besides identical old_sympos, also consider old_sympos * of 0 and 1 are identical. */ -if ((strcmp (old_func->old_name, func->old_name) ==0 ) && - ((old_func->old_sympos == func->old_sympos) || - (old_func->old_sympos ==0 && func->old_sympos ==1 ) || - (old_func->old_sympos ==1 && func->old_sympos ==0 ))) { -return func; +if ((strcmp (obj_func->old_name, func->old_name) ==0 ) && + ((obj_func->old_sympos == func->old_sympos) || + (obj_func->old_sympos ==0 && func->old_sympos ==1 ) || + (obj_func->old_sympos ==1 && func->old_sympos ==0 ))) { +return obj_func; } }
kernel/livepatch/core.h +1 -0
@@ -17,6 +17,7 @@ void klp_free_patch_async(struct klp_patch *patch);void klp_free_replaced_patches_async (struct klp_patch *new_patch);void klp_unpatch_replaced_patches (struct klp_patch *new_patch);void klp_discard_nops (struct klp_patch *new_patch); +struct klp_func *klp_find_func (struct klp_object *obj,struct klp_func *func);static inline bool klp_is_object_loaded (struct klp_object *obj) {
Signature · 1 line
2.52.0
[email protected]
AI reviewRe: [PATCH v5 2/9] livepatch: Make klp_find_func() non static
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
Pre-existing issues:
- [High] A malformed livepatch module with a missing old_name triggers a NULL pointer dereference in klp_find_func(). --
--- Patch [2]: [PATCH v5 2/9] livepatch: Make klp_find_func() non static --- commit c50e76ff358d6d9f27c1c156d6d6f492f2cdd3b1
Author: Yafang Shao <[email protected]>
livepatch: Make klp_find_func() non static
This commit makes klp_find_func() non-static to allow its use in other source files by an upcoming patch. It also renames the parameter @old_func to @func.
Quoted 4 lines
kernel/livepatch/core.c +0
-0
[ ... ]
Quoted 14 lines
kernel/livepatch/core.c +4
-4@@ -82,21 +82,21 @@ static bool klp_initialized(void)return !!klp_root_kobj; } -static struct klp_func *klp_find_func (struct klp_object *obj, -struct klp_func *old_func) +/* Check if @func is present in @obj. */ +struct klp_func *klp_find_func (struct klp_object *obj,struct klp_func *func) { -struct klp_func *func; +struct klp_func *obj_func; -klp_for_each_func (obj, func) { +klp_for_each_func (obj, obj_func) {
kernel/livepatch/core.c static bool klp_initialized(void) · line 95
Quoted 10 lines
kernel/livepatch/core.c +1
-5/* * Besides identical old_sympos, also consider old_sympos * of 0 and 1 are identical. */ -if ((strcmp (old_func->old_name, func->old_name) ==0 ) && - ((old_func->old_sympos == func->old_sympos) || - (old_func->old_sympos ==0 && func->old_sympos ==1 ) || - (old_func->old_sympos ==1 && func->old_sympos ==0 ))) { -return func; + if ((strcmp(obj_func->old_name, func->old_name) == 0) &&
[Severity: High]
This is a pre-existing issue, but could this strcmp() trigger a null pointer dereference if a malformed livepatch module has a missing old_name field?
When loading a livepatch module where patch->replace is true, if a struct klp_func element has .new_func defined but .old_name set to null, klp_init_patch_early() adds this unvalidated function to obj->func_list.
Subsequently, klp_add_object_nops() calls klp_find_func() on the unvalidated new patch object before klp_init_object() performs the expected null check:
kernel/livepatch/core.c:klp_add_object_nops() {
...
func = klp_find_func(obj, old_func);
...
}This unconditionally passes the unvalidated, null obj_func->old_name to strcmp().
Does this allow a malformed module to bypass validation and crash the system instead of being gracefully rejected?
Quoted 6 lines
kernel/livepatch/core.c +4
-0+ ((obj_func->old_sympos == func->old_sympos) || + (obj_func->old_sympos == 0 && func->old_sympos == 1) || + (obj_func->old_sympos == 1 && func->old_sympos == 0))) { +return obj_func; } }
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=2
Re: [PATCH v5 2/9] livepatch: Make klp_find_func() non static
Quoted 5 lines · On Sun, Aug 9, 2026 at 5:32 PM <[email protected]> wrote:
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
Pre-existing issues:
- [High] A malformed livepatch module with a missingold_nametriggers a NULL pointer dereference inklp_find_func().
Invalid review issue.
This issue was already resolved by commit:
livepatch: Fix NULL pointer dereference in klp_find_func()
Signature · 2 lines
Regards Yafang
PATCH v5 3/9 livepatch: Call klp_init_patch_early() earlier
In reply to Yafang ShaoInvoke klp_init_patch_early() during early initialization to avoid mixing for_each_() and for_each_*_static() variants later in the code.
No functional change. This prepares the code for an upcoming patch.
--- kernel/livepatch/core.c | 15 ++++++++------- 1 file changed, 8 insertions(+), 7 deletions(-)
kernel/livepatch/core.c +8 -7
@@ -1145,13 +1145,6 @@ int klp_enable_patch(struct klp_patch *patch)mutex_lock (&klp_mutex); -if (!klp_is_patch_compatible (patch)) { -pr_err ("Livepatch patch (%s) is not compatible with the already installed livepatches.\n" , - patch->mod->name); -mutex_unlock (&klp_mutex); -return -EINVAL ; - } -if (!try_module_get (patch->mod)) {mutex_unlock (&klp_mutex);return -ENODEV ;
@@ -1159,6 +1152,14 @@ int klp_enable_patch(struct klp_patch *patch)klp_init_patch_early (patch); +if (!klp_is_patch_compatible (patch)) { +pr_err ("Livepatch patch (%s) is not compatible with the already installed livepatches.\n" , + patch->mod->name); + ret = -EINVAL ; + goto err; + } + + ret =klp_init_patch (patch);if (ret) goto err;
Signature · 1 line
2.52.0
[email protected]
AI reviewRe: [PATCH v5 3/9] livepatch: Call klp_init_patch_early() earlier
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
Pre-existing issues:
- [High] When CONFIG_DEBUG_KOBJECT_RELEASE is enabled, an error during patch initialization causes a Use-After-Free during module unload due to delayed kobject release. --
--- Patch [3]: [PATCH v5 3/9] livepatch: Call klp_init_patch_early() earlier --- commit ebdb1e39e8d2124754d0f852abe91968ee2bb0d7
Author: Yafang Shao <[email protected]>
livepatch: Call klp_init_patch_early() earlier
This patch moves klp_init_patch_early() to an earlier stage during patch initialization. This is intended to avoid mixing for_each_() and for_each_*_static() variants later in the code.
Quoted 4 lines
kernel/livepatch/core.c +0
-0
[ ... ]
kernel/livepatch/core.c int klp_enable_patch(struct klp_patch *patch) · line 1150
Quoted 10 lines
kernel/livepatch/core.c +6
-0@@ -1149,6 +1142,14 @@ int klp_enable_patch(struct klp_patch *patch)klp_init_patch_early (patch); +if (!klp_is_patch_compatible (patch)) { +pr_err ("Livepatch patch (%s) is not compatible with the already installed livepatches.\n" , + patch->mod->name); + ret = -EINVAL ; + goto err; + }
[Severity: High]
This is a pre-existing issue, but does moving klp_init_patch_early() before klp_is_patch_compatible() make a delayed kobject release crash easier to hit if the patch is incompatible?
When klp_init_patch_early() completes, it has called kobject_init() on the patch->kobj, as well as all obj->kobj and func->kobj. If klp_is_patch_compatible() fails and jumps to the err label, klp_free_patch_start() and klp_free_patch_finish() are called.
klp_free_patch_start() drops the object and function kobject references:
kernel/livepatch/core.c:__klp_free_funcs() {
...
kobject_put(&func->kobj);
...
}kernel/livepatch/core.c:__klp_free_objects() {
...
kobject_put(&obj->kobj);
...
}However, klp_free_patch_finish() only waits for the patch kobject completion:
kernel/livepatch/core.c:klp_free_patch_finish() {
...
wait_for_completion(&patch->finish);
...
}When CONFIG_DEBUG_KOBJECT_RELEASE is enabled, the kobject_put() on the un-added objects queues a delayed work up to 4 seconds later to free the kobjects. Because klp_free_patch_finish() doesn't wait for the child kobjects, klp_enable_patch() can return and allow the module to unload, freeing the klp_object and klp_func arrays before the delayed work accesses them.
Can this result in a Use-After-Free during module unload?
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=3
Re: [PATCH v5 3/9] livepatch: Call klp_init_patch_early() earlier
Quoted 5 lines · On Sun, Aug 9, 2026 at 5:40 PM <[email protected]> wrote:
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
Pre-existing issues:
- [High] WhenCONFIG_DEBUG_KOBJECT_RELEASEis enabled, an error during patch initialization causes a Use-After-Free during module unload due to delayed kobject release.
Makes sense.
I will fix this pre-existing issue in the next version.
Signature · 2 lines
Regards Yafang
PATCH v5 4/9 livepatch: Implement replace set for scoped atomic replace
In reply to Yafang ShaoThe current bool replace flag is too coarse: it is either all or nothing. A livepatch with .replace=true replaces ALL existing livepatches, which is safe but inflexible. There is no way to have multiple independent livepatch sets coexist on the same system.
Replace it with a more flexible model using two new fields in struct klp_patch:
- provides: an unsigned int id identifying the patch replace set. By default (provides=0), any livepatch replaces any other livepatch.
- obsoletes: an optional array of unsigned int ids specifying additional provides ids to be replaced. This allows a new patch to explicitly obsolete patches from different replace sets.
A new livepatch atomically replaces any existing livepatch whose provides id matches either:
1. The new patch provides id (same replace set), or
2. Any id in the new patch obsoletes list
The klp-build script is updated with -p/--provides and -r/--obsoletes options. The obsoletes list automatically includes the provides id and deduplicates entries. Input validation rejects malformed values at build time.
Two helper functions are introduced:
- klp_patch_replaceable(): checks if an old patch should be replaced by a new patch.
- klp_has_function_conflict(): rejects loading a livepatch that would modify a function already patched by a livepatch with a different provides id.
--- .../ABI/testing/sysfs-kernel-livepatch | 22 ++++- .../livepatch/cumulative-patches.rst | 93 +++++++++++++------ Documentation/livepatch/livepatch.rst | 23 +++-- include/linux/livepatch.h | 7 +- kernel/livepatch/core.c | 69 ++++++++++++-- kernel/livepatch/core.h | 1 + kernel/livepatch/state.c | 57 ++++++++++-- kernel/livepatch/transition.c | 11 ++- scripts/livepatch/init.c | 71 +++++++++++++- scripts/livepatch/klp-build | 77 +++++++++++++-- 10 files changed, 350 insertions(+), 81 deletions(-)
Documentation/ABI/testing/sysfs-kernel-livepatch +17 -5
@@ -47,13 +47,25 @@ Description: disabled when the feature is used. See Documentation/livepatch/livepatch.rst for more information. -What: /sys/kernel/livepatch/<patch>/replace -Date: Jun 2024 -KernelVersion: 6.11.0 +What: /sys/kernel/livepatch/<patch>/provides +Date: Jun 2026 +KernelVersion: 7.4.0 Contact: [email protected] Description: - An attribute which indicates whether the patch supports - atomic-replace. + An attribute to show the provides id of this livepatch. + Only one active livepatch per provides id is allowed. + +What: /sys/kernel/livepatch/<patch>/obsoletes +Date: Jun 2026 +KernelVersion: 7.4.0 +Contact: [email protected] +Description: + An attribute to show the obsoletes ids of this livepatch. + The obsoletes ids are a comma-separated list of provides + ids that this patch obsoletes. When this livepatch is + loaded, any existing livepatch whose provides id matches + either this patch's provides id or any id in the obsoletes + list will be atomically replaced. What: /sys/kernel/livepatch/<patch>/stack_order Date: Jan 2025
Documentation/livepatch/cumulative-patches.rst +65 -28
105 lines not on this page · raw message
Documentation/livepatch/livepatch.rst +14 -9
@@ -347,15 +347,20 @@ to '0'. 5.3. Replacing -------------- -All enabled patches might get replaced by a cumulative patch that -has the .replace flag set. - -Once the new patch is enabled and the 'transition' finishes then -all the functions (struct klp_func) associated with the replaced -patches are removed from the corresponding struct klp_ops. Also -the ftrace handler is unregistered and the struct klp_ops is -freed when the related function is not modified by the new patch -and func_stack list becomes empty. +There can be only one active livepatch for a given ``provides`` id. +A new livepatch atomically replaces any existing livepatch whose +``provides`` id matches either the new patch's ``provides`` id or +any id in the new patch's ``obsoletes`` list. + +Once the transition is complete, all functions (``struct klp_func``) +associated with the matching replaced patches are removed from the +corresponding ``struct klp_ops``. If a function is no longer modified by +the new patch and its ``func_stack`` list becomes empty, the ftrace +handler is unregistered and the ``struct klp_ops`` is freed. + +Patches with a different ``provides`` id are not affected by this +process and remain active. This allows for the independent management +and stacking of multiple, non-conflicting livepatch sets. See Documentation/livepatch/cumulative-patches.rst for more details.
include/linux/livepatch.h +5 -2
@@ -123,7 +123,8 @@ struct klp_state {* @mod: reference to the live patch module * @objs: object entries for kernel objects to be patched * @states: system states that can get modified -* @replace: replace all actively used patches +* @provides: only one active livepatch per id +* @obsoletes: replace given livepatch id(s) * @list: list node for global list of actively used patches * @kobj: kobject for sysfs resources * @obj_list: dynamic list of the object entries
@@ -137,7 +138,9 @@ struct klp_patch {struct module *mod;struct klp_object *objs;struct klp_state *states; -bool replace; +unsigned int provides; +unsigned int *obsoletes; +unsigned int nr_obsoletes;/* internal */ struct list_head list;
kernel/livepatch/core.c +59 -10
127 lines not on this page · raw message
kernel/livepatch/core.h +1 -0
@@ -18,6 +18,7 @@ void klp_free_replaced_patches_async(struct klp_patch *new_patch);void klp_unpatch_replaced_patches (struct klp_patch *new_patch);void klp_discard_nops (struct klp_patch *new_patch);struct klp_func *klp_find_func (struct klp_object *obj,struct klp_func *func); +bool klp_patch_replaceable (struct klp_patch *old_patch,struct klp_patch *new_patch);static inline bool klp_is_object_loaded (struct klp_object *obj) {
kernel/livepatch/state.c +49 -8
82 lines not on this page · raw message
kernel/livepatch/transition.c +6 -5
@@ -89,7 +89,7 @@ static void klp_complete_transition(void) klp_transition_patch->mod->name, klp_target_state == KLP_TRANSITION_PATCHED ? "patching" : "unpatching"); -if (klp_transition_patch->replace && klp_target_state ==KLP_TRANSITION_PATCHED ) { +if (klp_target_state ==KLP_TRANSITION_PATCHED ) {klp_unpatch_replaced_patches (klp_transition_patch);klp_discard_nops (klp_transition_patch); }
@@ -498,7 +498,7 @@ void klp_try_complete_transition(void)*/ if (!patch->enabled)klp_free_patch_async (patch); -else if (patch->replace) +else klp_free_replaced_patches_async (patch); }
@@ -720,11 +720,12 @@ void klp_force_transition(void)klp_update_patch_state (idle_task (cpu));/* Set forced flag for patches being removed. */ -if (klp_target_state ==KLP_TRANSITION_UNPATCHED ) +if (klp_target_state ==KLP_TRANSITION_UNPATCHED ) { klp_transition_patch->forced = true; -else if (klp_transition_patch->replace) { + }else {klp_for_each_patch (patch) { -if (patch != klp_transition_patch) +if (patch != klp_transition_patch && +klp_patch_replaceable (patch, klp_transition_patch)) patch->forced = true; } }
scripts/livepatch/init.c +66 -5
101 lines not on this page · raw message
scripts/livepatch/klp-build +68 -9
119 lines not on this page · raw message
Signature · 1 line
2.52.0
[email protected]
AI reviewRe: [PATCH v5 4/9] livepatch: Implement replace set for scoped atomic replace
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider: - [Medium] The `--provides` argument in `klp-build` lacks input validation, allowing shell command…
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [Medium] The --provides argument in klp-build lacks input validation, allowing shell command injection during the kernel module build process. --
--- Patch [4]: [PATCH v5 4/9] livepatch: Implement replace set for scoped atomic replace --- commit 1fb78be5e32d18ecfd4bbeb7fa484d70bf6b9401
Author: Yafang Shao <[email protected]>
livepatch: Implement replace set for scoped atomic replace
This commit introduces a more flexible model for livepatch replacement by adding provides and obsoletes fields, allowing multiple independent livepatch sets to coexist on the same system.
Quoted 4 lines
scripts/livepatch/klp-build +0
-0
[ ... ]
scripts/livepatch/klp-build process_args() { · line 194
Quoted 9 lines
scripts/livepatch/klp-build +2
-3@@ -189,9 +190,26 @@ process_args() { NAME="$(module_name_string "$NAME")" shift 2 ;; - --no-replace) - REPLACE=0 - shift + -p | --provides) + PROVIDES="$2"
[Severity: Medium]
Is there a missing validation check for the provides argument here?
The obsoletes argument below checks for valid integers, but it appears this assignment accepts any unvalidated string.
Quoted 6 lines
scripts/livepatch/klp-build +6
-0+ shift 2 + ;; + -r | --obsoletes) + OBSOLETES="$2" + local obs_val="$2" + [[ "$obs_val" != \[*\] ]] && die "obsoletes must be enclosed in brackets, e.g., '[0,1,2]'"
[ ... ]
scripts/livepatch/klp-build build_patch_module() { · line 899
Quoted 6 lines
scripts/livepatch/klp-build +1
-1@@ -847,7 +896,17 @@ build_patch_module() { cflags=("-ffunction-sections") cflags+=("-fdata-sections") - [[ $REPLACE -eq 0 ]] && cflags+=("-DKLP_NO_REPLACE") + cflags+=("-DKLP_PROVIDES=$PROVIDES")
[Severity: Medium]
Because the provides argument lacks validation in process_args(), could this assignment allow shell command injection during the build process if un-escaped metacharacters are passed to the script?
Quoted 4 lines
scripts/livepatch/klp-build +4
-0+ + # Process OBSOLETES: remove brackets and spaces, convert to comma-separated string + # Input: "[0, 1, 2]" or "[]" or "" -> Output: "0,1,2" or empty + if [[ -n "$OBSOLETES" && "$OBSOLETES" != "[]" ]]; then
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=4
Re: [PATCH v5 4/9] livepatch: Implement replace set for scoped atomic replace
Quoted 35 lines · On Sun, Aug 9, 2026 at 5:33 PM <[email protected]> wrote:
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [Medium] The--providesargument inklp-buildlacks input validation, allowing shell command injection during the kernel module build process. ----- Patch [4]: [PATCH v5 4/9] livepatch: Implement replace set for scoped atomic replace --- commit
1fb78be5e32d18ecfd4bbeb7fa484d70bf6b9401
Author: Yafang Shao <[email protected]>livepatch: Implement replace set for scoped atomic replace
This commit introduces a more flexible model for livepatch replacement by adding provides and obsoletes fields, allowing multiple independent livepatch sets to coexist on the same system.
Quoted 4 lines
scripts/livepatch/klp-build +0
-0[ ... ]
scripts/livepatch/klp-build process_args() { · line 194
Quoted 9 lines
scripts/livepatch/klp-build +2
-3@@ -189,9 +190,26 @@ process_args() { NAME="$(module_name_string "$NAME")" shift 2 ;; - --no-replace) - REPLACE=0 - shift + -p | --provides) + PROVIDES="$2"[Severity: Medium]
Is there a missing validation check for the provides argument here?
Quoted 2 lines
The obsoletes argument below checks for valid integers, but it appears this assignment accepts any unvalidated string.
Makes sense
I will update it in the next version.
Signature · 2 lines
Regards Yafang
PATCH v5 5/9 livepatch: Deprecate stack_order
In reply to Yafang Shaostack_order is no longer needed for atomic-replace livepatches, as a single function can only be modified by a unique replace_set. Since this livepatch sysfs interface is mainly intended for kernel developers, backward compatibility does not need to be maintained.
--- .../ABI/removed/sysfs-kernel-livepatch | 16 +++++++++++++ .../ABI/testing/sysfs-kernel-livepatch | 9 ------- kernel/livepatch/core.c | 24 ------------------- 3 files changed, 16 insertions(+), 33 deletions(-) create mode 100644 Documentation/ABI/removed/sysfs-kernel-livepatch
Documentation/ABI/removed/sysfs-kernel-livepatch new file +16 -0
@@ -0,0 +1,16 @@ +What: /sys/kernel/livepatch/<patch>/replace +Date: Jun 2024 +KernelVersion: 6.11.0 +Contact: [email protected] +Description: + An attribute which indicates whether the patch supports + atomic-replace. + +What: /sys/kernel/livepatch/<patch>/stack_order +Date: Jan 2025 +KernelVersion: 6.14.0 +Description: + This attribute specifies the sequence in which live patch modules + are applied to the system. If multiple live patches modify the same + function, the implementation with the biggest 'stack_order' number + is used, unless a transition is currently in progress.
Documentation/ABI/testing/sysfs-kernel-livepatch +0 -9
@@ -67,15 +67,6 @@ Description: either this patch's provides id or any id in the obsoletes list will be atomically replaced. -What: /sys/kernel/livepatch/<patch>/stack_order -Date: Jan 2025 -KernelVersion: 6.14.0 -Description: - This attribute specifies the sequence in which live patch modules - are applied to the system. If multiple live patches modify the same - function, the implementation with the biggest 'stack_order' number - is used, unless a transition is currently in progress. - What: /sys/kernel/livepatch/<patch>/<object> Date: Nov 2014 KernelVersion: 3.19.0
kernel/livepatch/core.c +0 -24
@@ -352,7 +352,6 @@ int klp_apply_section_relocs(struct module *pmod, Elf_Shdr *sechdrs,* /sys/kernel/livepatch/<patch>/force * /sys/kernel/livepatch/<patch>/provides * /sys/kernel/livepatch/<patch>/obsoletes -* /sys/kernel/livepatch/<patch>/stack_order * /sys/kernel/livepatch/<patch>/<object> * /sys/kernel/livepatch/<patch>/<object>/patched * /sys/kernel/livepatch/<patch>/<object>/<function,sympos>
@@ -477,40 +476,17 @@ static ssize_t obsoletes_show(struct kobject *kobj,return len; } -static ssize_t stack_order_show (struct kobject *kobj, -struct kobj_attribute *attr,char *buf) -{ -struct klp_patch *patch, *this_patch; -int stack_order =0 ; - - this_patch = container_of(kobj, struct klp_patch, kobj); - -mutex_lock (&klp_mutex); - -klp_for_each_patch (patch) { - stack_order++; -if (patch == this_patch) -break ; - } - -mutex_unlock (&klp_mutex); - -return sysfs_emit (buf,"%d\n" , stack_order); -} -static struct kobj_attribute enabled_kobj_attr =__ATTR_RW (enabled);static struct kobj_attribute transition_kobj_attr =__ATTR_RO (transition);static struct kobj_attribute force_kobj_attr =__ATTR_WO (force);static struct kobj_attribute provides_kobj_attr =__ATTR_RO (provides);static struct kobj_attribute obsoletes_kobj_attr =__ATTR_RO (obsoletes); -static struct kobj_attribute stack_order_kobj_attr =__ATTR_RO (stack_order);static struct attribute *klp_patch_attrs[] = { &enabled_kobj_attr.attr, &transition_kobj_attr.attr, &force_kobj_attr.attr, &provides_kobj_attr.attr, &obsoletes_kobj_attr.attr, - &stack_order_kobj_attr.attr,NULL };ATTRIBUTE_GROUPS (klp_patch);
Signature · 1 line
2.52.0
PATCH v5 6/9 selftests: livepatch: Adapt atomic replace tests to provides/obsoletes
In reply to Yafang ShaoThe legacy "replace" field in struct klp_patch was replaced by the provides/obsoletes mechanism. As a result, the atomic replace selftests fail to build against kernels that only support provides/obsoletes.
Adapt the selftests so that they build and run on both old and new kernels:
- test_modules/Makefile detects whether the kernel header still declares the "replace" field. If so, it passes KLP_HAS_REPLACE to the modules via ccflags-y.
- test_klp_atomic_replace.c and other source files using .replace guard their replace module parameter and .replace assignment with #ifdef KLP_HAS_REPLACE. The #else branches are left as TODO placeholders for the upcoming provides/obsoletes based tests.
- functions.sh adds detect_replace_attr(), which loads a temporary livepatch and checks for the /sys/kernel/livepatch/<patch>/replace attribute, setting HAS_REPLACE_ATTR accordingly.
- test-livepatch.sh and test-callbacks.sh run the tests that depend on multiple livepatches coexisting or on the atomic replace behavior ("multiple livepatches" and "atomic replace") only when HAS_REPLACE_ATTR is set. These scenarios do not apply to the new kernel, where provides=0 replaces any other livepatch by default.
The provides/obsoletes based selftests will be added later.
--- tools/testing/selftests/livepatch/functions.sh | 15 +++++++++++++++ .../selftests/livepatch/test-callbacks.sh | 6 ++++++ .../selftests/livepatch/test-livepatch.sh | 6 ++++++ .../selftests/livepatch/test_modules/Makefile | 10 ++++++++++ .../test_modules/test_klp_atomic_replace.c | 16 ++++++++++++++++ .../test_modules/test_klp_callbacks_demo2.c | 16 ++++++++++++++++ .../livepatch/test_modules/test_klp_state.c | 7 +++++++ .../livepatch/test_modules/test_klp_state2.c | 7 +++++++ 8 files changed, 83 insertions(+)
tools/testing/selftests/livepatch/functions.sh +15 -0
@@ -351,6 +351,21 @@ function does_sysfs_exist() { [[ -f "$SYSFS_KLP_DIR/$mod/$attr" ]] } +# detect_replace_attr() - detect whether the running kernel still exposes +# the legacy "replace" sysfs attribute and set HAS_REPLACE_ATTR +# accordingly. The atomic replace tests are only run when the attribute +# is present. The provides/obsoletes based tests will be added later. +function detect_replace_attr() { + HAS_REPLACE_ATTR=0 + + load_lp test_klp_livepatch + if does_sysfs_exist test_klp_livepatch "replace"; then + HAS_REPLACE_ATTR=1 + fi + disable_lp test_klp_livepatch + unload_lp test_klp_livepatch +} + # check_sysfs_rights(modname, rel_path, expected_rights) - check sysfs # path permissions # modname - livepatch module creating the sysfs interface
tools/testing/selftests/livepatch/test-callbacks.sh +6 -0
@@ -11,6 +11,8 @@ MOD_TARGET_BUSY=test_klp_callbacks_busy setup_config +detect_replace_attr + # Test a combination of loading a kernel module and a livepatch that # patches a function in the first module. Load the target module
@@ -451,6 +453,7 @@ $MOD_TARGET_BUSY: busymod_work_func exit $MOD_TARGET_BUSY: ${MOD_TARGET_BUSY}_exit" +if [[ "$HAS_REPLACE_ATTR" == "1" ]]; then # Test loading multiple livepatches. This test-case is mainly for comparing # with the next test-case. #
@@ -499,8 +502,10 @@ $MOD_LIVEPATCH: post_unpatch_callback: vmlinux livepatch: '$MOD_LIVEPATCH': unpatching complete % rmmod $MOD_LIVEPATCH2 % rmmod $MOD_LIVEPATCH" +fi +if [[ "$HAS_REPLACE_ATTR" == "1" ]]; then # Load multiple livepatches, but the second as an 'atomic-replace' # patch. When the latter loads, the original livepatch should be # disabled and *none* of its pre/post-unpatch callbacks executed. On
@@ -548,6 +553,7 @@ $MOD_LIVEPATCH2: post_unpatch_callback: vmlinux livepatch: '$MOD_LIVEPATCH2': unpatching complete % rmmod $MOD_LIVEPATCH2 % rmmod $MOD_LIVEPATCH" +fi exit 0
tools/testing/selftests/livepatch/test-livepatch.sh +6 -0
@@ -13,6 +13,8 @@ MOD_TARGET_PATCH=test_klp_mod_patch setup_config +detect_replace_attr + # - load a livepatch that modifies the output from /proc/cmdline and # verify correct behavior
@@ -49,6 +51,7 @@ livepatch: '$MOD_LIVEPATCH1': unpatching complete % rmmod $MOD_LIVEPATCH1" +if [[ "$HAS_REPLACE_ATTR" == "1" ]]; then # - load a livepatch that modifies the output from /proc/cmdline and # verify correct behavior # - load another livepatch and verify that both livepatches are active
@@ -107,8 +110,10 @@ livepatch: '$MOD_LIVEPATCH1': starting unpatching transition livepatch: '$MOD_LIVEPATCH1': completing unpatching transition livepatch: '$MOD_LIVEPATCH1': unpatching complete % rmmod $MOD_LIVEPATCH1" +fi +if [[ "$HAS_REPLACE_ATTR" == "1" ]]; then # - load a livepatch that modifies the output from /proc/cmdline and # verify correct behavior # - load two additional livepatches and check the number of livepatch modules
@@ -196,6 +201,7 @@ livepatch: '$MOD_REPLACE': starting unpatching transition livepatch: '$MOD_REPLACE': completing unpatching transition livepatch: '$MOD_REPLACE': unpatching complete % rmmod $MOD_REPLACE" +fi # - load a target module that provides /proc/test_klp_mod_target with
tools/testing/selftests/livepatch/test_modules/Makefile +10 -0
@@ -16,6 +16,16 @@ obj-m += test_klp_atomic_replace.o \ test_klp_state3.o \ test_klp_syscall.o +# The legacy "replace" field was replaced by provides/obsoletes. When the +# kernel (KDIR) still supports "replace", pass KLP_HAS_REPLACE to the +# modules so that they can #ifdef the legacy "replace" code. Otherwise +# the modules are built with the provides/obsoletes based code (to be +# added later). +KLP_HAS_REPLACE := $(shell grep -q 'bool replace' $(KDIR)/include/linux/livepatch.h 2>/dev/null && echo y) +ifeq ($(KLP_HAS_REPLACE),y) +ccflags-y += -DKLP_HAS_REPLACE +endif + # Ensure that KDIR exists, otherwise skip the compilation modules: ifneq ("$(wildcard $(KDIR))", "")
tools/testing/selftests/livepatch/test_modules/test_klp_atomic_replace.c +16 -0
@@ -7,9 +7,16 @@#include <linux/kernel.h> #include <linux/livepatch.h> +#ifdef KLP_HAS_REPLACE static int replace;module_param (replace, int,0644 );MODULE_PARM_DESC (replace,"replace (default=0)" ); +#else +/* +* TODO: Add provides/obsoletes module parameters for the +* provides/obsoletes based tests (to be added later). +*/ +#endif #include <linux/seq_file.h> static int livepatch_meminfo_proc_show (struct seq_file *m,void *v)
@@ -36,12 +43,21 @@ static struct klp_object objs[] = {static struct klp_patch patch = { .mod =THIS_MODULE , .objs = objs, +#ifdef KLP_HAS_REPLACE/* set .replace in the init function below for demo purposes */ +#endif };static int test_klp_atomic_replace_init (void ) { +#ifdef KLP_HAS_REPLACE patch.replace = replace; +#else +/* +* TODO: Set provides/obsoletes from the module parameters +* for the provides/obsoletes based tests (to be added later). +*/ +#endif return klp_enable_patch (&patch); }
tools/testing/selftests/livepatch/test_modules/test_klp_callbacks_demo2.c +16 -0
@@ -7,9 +7,16 @@#include <linux/kernel.h> #include <linux/livepatch.h> +#ifdef KLP_HAS_REPLACE static int replace;module_param (replace, int,0644 );MODULE_PARM_DESC (replace,"replace (default=0)" ); +#else +/* +* TODO: Add provides/obsoletes module parameters for the +* provides/obsoletes based tests (to be added later). +*/ +#endif static const char *const module_state[] = { [MODULE_STATE_LIVE ] ="[MODULE_STATE_LIVE] Normal state" ,
@@ -72,12 +79,21 @@ static struct klp_object objs[] = {static struct klp_patch patch = { .mod =THIS_MODULE , .objs = objs, +#ifdef KLP_HAS_REPLACE/* set .replace in the init function below for demo purposes */ +#endif };static int test_klp_callbacks_demo2_init (void ) { +#ifdef KLP_HAS_REPLACE patch.replace = replace; +#else +/* +* TODO: Set provides/obsoletes from the module parameters +* for the provides/obsoletes based tests (to be added later). +*/ +#endif return klp_enable_patch (&patch); }
tools/testing/selftests/livepatch/test_modules/test_klp_state.c +7 -0
@@ -142,7 +142,14 @@ static struct klp_patch patch = { .mod = THIS_MODULE, .objs = objs, .states = states, +#ifdef KLP_HAS_REPLACE .replace = true, +#else +/* +* TODO: Add provides/obsoletes module parameters for the +* provides/obsoletes based tests (to be added later). +*/ +#endif };static int test_klp_callbacks_demo_init (void )
tools/testing/selftests/livepatch/test_modules/test_klp_state2.c +7 -0
@@ -171,7 +171,14 @@ static struct klp_patch patch = { .mod = THIS_MODULE, .objs = objs, .states = states, +#ifdef KLP_HAS_REPLACE .replace = true, +#else +/* +* TODO: Add provides/obsoletes module parameters for the +* provides/obsoletes based tests (to be added later). +*/ +#endif };static int test_klp_callbacks_demo_init (void )
Signature · 1 line
2.52.0
[email protected]
AI reviewRe: [PATCH v5 6/9] selftests: livepatch: Adapt atomic replace tests to provides/obsoletes
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider: - [Medium] The 'replace' feature detection in test_modules/Makefile fails for out-of-tree (O=) kernel…
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [Medium] The 'replace' feature detection in test_modules/Makefile fails for out-of-tree (O=) kernel builds, breaking tests on older kernels. --
--- Patch [6]: [PATCH v5 6/9] selftests: livepatch: Adapt atomic replace tests to provides/obsoletes --- commit e4d15e9f2792340ea543b405f9d074e729665c9b
Author: Yafang Shao <[email protected]>
selftests: livepatch: Adapt atomic replace tests to provides/obsoletes
The legacy "replace" field in struct klp_patch was replaced by the provides/obsoletes mechanism. This commit adapts selftests to build and run on both old and new kernels by detecting the presence of the "replace" field.
Quoted 5 lines
tools/testing/selftests/livepatch/test_modules/Makefile +0
-0
tools/testing/selftests/livepatch/test_modules/Makefile obj-m += test_klp_atomic_replace.o \ · line 24
Quoted 9 lines
tools/testing/selftests/livepatch/test_modules/Makefile +6
-0@@ -16,6 +16,16 @@ obj-m += test_klp_atomic_replace.o \ test_klp_state3.o \ test_klp_syscall.o +# The legacy "replace" field was replaced by provides/obsoletes. When the +# kernel (KDIR) still supports "replace", pass KLP_HAS_REPLACE to the +# modules so that they can #ifdef the legacy "replace" code. Otherwise +# the modules are built with the provides/obsoletes based code (to be +# added later). +KLP_HAS_REPLACE := $(shell grep -q 'bool replace' $(KDIR)/include/linux/livepatch.h 2>/dev/null && echo y)
[Severity: Medium]
Will this grep fail for out-of-tree (O=...) kernel builds?
If $(KDIR) points to the build directory rather than the source directory, $(KDIR)/include/linux/livepatch.h might not exist.
When tests are run, detect_replace_attr() will still correctly identify the replace sysfs attribute on the running kernel and set HAS_REPLACE_ATTR=1.
However, since the module would be compiled without KLP_HAS_REPLACE, it lacks the replace module parameter. When the test scripts attempt to load the module with replace=1, insmod will fail, causing the test suite to abort.
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=6
Re: [PATCH v5 6/9] selftests: livepatch: Adapt atomic replace tests to provides/obsoletes
Quoted 37 lines · On Sun, Aug 9, 2026 at 5:33 PM <[email protected]> wrote:
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [Medium] The 'replace' feature detection in test_modules/Makefile fails for out-of-tree (O=) kernel builds, breaking tests on older kernels. ----- Patch [6]: [PATCH v5 6/9] selftests: livepatch: Adapt atomic replace tests to provides/obsoletes --- commit
e4d15e9f2792340ea543b405f9d074e729665c9b
Author: Yafang Shao <[email protected]>selftests: livepatch: Adapt atomic replace tests to provides/obsoletes
The legacy "replace" field in struct
klp_patchwas replaced by the provides/obsoletes mechanism. This commit adapts selftests to build and run on both old and new kernels by detecting the presence of the "replace" field.Quoted 5 lines
tools/testing/selftests/livepatch/test_modules/Makefile +0
-0tools/testing/selftests/livepatch/test_modules/Makefile obj-m += test_klp_atomic_replace.o \ · line 24
Quoted 9 lines
tools/testing/selftests/livepatch/test_modules/Makefile +6
-0@@ -16,6 +16,16 @@ obj-m += test_klp_atomic_replace.o \ test_klp_state3.o \ test_klp_syscall.o +# The legacy "replace" field was replaced by provides/obsoletes. When the +# kernel (KDIR) still supports "replace", pass KLP_HAS_REPLACE to the +# modules so that they can #ifdef the legacy "replace" code. Otherwise +# the modules are built with the provides/obsoletes based code (to be +# added later). +KLP_HAS_REPLACE := $(shell grep -q 'bool replace' $(KDIR)/include/linux/livepatch.h 2>/dev/null && echo y)[Severity: Medium]
Will this grep fail for out-of-tree (O=...) kernel builds?If $(KDIR) points to the build directory rather than the source directory, $(KDIR)/include/linux/livepatch.h might not exist.
Quoted 6 lines
When tests are run,
detect_replace_attr()will still correctly identify the replace sysfs attribute on the running kernel and set HAS_REPLACE_ATTR=1.However, since the module would be compiled without
KLP_HAS_REPLACE, it lacks the replace module parameter. When the test scripts attempt to load the module with replace=1, insmod will fail, causing the test suite to abort.
I believe this case is invalid. We always run make run_tests to execute the full livepatch selftests, which ensures all kernel modules are rebuilt against the running kernel.
Signature · 2 lines
Regards Yafang
PATCH v5 7/9 selftests: livepatch: Add provides/obsoletes test scenarios
In reply to Yafang ShaoNow that the legacy "replace" field has been replaced by the provides/obsoletes mechanism, add test scenarios to verify the new behavior.
Fill in the #else branches of the test modules that were left as TODO placeholders in the previous patch:
- test_klp_atomic_replace.c gains provides and obsoletes module parameters for the provides/obsoletes based tests.
- test_klp_callbacks_demo2.c gains a provides module parameter.
- test_klp_livepatch.c gains a provides module parameter so that it can be loaded with a specific provides id for coexistence tests.
- test_klp_state.c and test_klp_state2.c add a comment in the #else branch noting that provides=0 (the default) is equivalent to the previous .replace=true behavior.
Add test-provides.sh with four test scenarios:
1. provides=0 replaces provides=0: two livepatches with provides=0 that modify different functions; the second replaces the first.
2. Same provides replaces: two livepatches with provides=1 that modify different functions; the second replaces the first.
3. Obsoletes replaces: a livepatch with provides=2 and obsoletes=[1] replaces a livepatch with provides=1. The provides/obsoletes sysfs values are verified.
4. provides=0 coexists with provides=1: two livepatches with different provides ids that modify different functions coexist, verifying that provides=0 does not replace a patch with a different provides id.
The provides/obsoletes based tests only run on kernels that no longer support the legacy "replace" attribute.
--- tools/testing/selftests/livepatch/Makefile | 3 +- .../livepatch/test-provides-obsoletes.sh | 195 ++++++++++++++++++ .../test_modules/test_klp_atomic_replace.c | 22 +- .../test_modules/test_klp_callbacks_demo2.c | 12 +- .../test_modules/test_klp_livepatch.c | 9 + .../livepatch/test_modules/test_klp_state.c | 5 +- .../livepatch/test_modules/test_klp_state2.c | 5 +- 7 files changed, 226 insertions(+), 25 deletions(-) create mode 100755 tools/testing/selftests/livepatch/test-provides-obsoletes.sh
tools/testing/selftests/livepatch/Makefile +2 -1
@@ -11,7 +11,8 @@ TEST_PROGS := \ test-ftrace.sh \ test-sysfs.sh \ test-syscall.sh \ - test-kprobe.sh + test-kprobe.sh \ + test-provides-obsoletes.sh TEST_FILES := settings
tools/testing/selftests/livepatch/test-provides-obsoletes.sh new file +195 -0
195 lines not on this page · raw message
tools/testing/selftests/livepatch/test_modules/test_klp_atomic_replace.c +14 -8
@@ -12,10 +12,15 @@ static int replace;module_param (replace, int,0644 );MODULE_PARM_DESC (replace,"replace (default=0)" );#else -/* -* TODO: Add provides/obsoletes module parameters for the -* provides/obsoletes based tests (to be added later). -*/ +static unsigned int provides; +module_param (provides, uint,0644 ); +MODULE_PARM_DESC (provides,"provides id (default=0)" ); + +#define KLP_MAX_OBSOLETES 16 +static unsigned int obsoletes[KLP_MAX_OBSOLETES ]; +static int nr_obsoletes; +module_param_array (obsoletes, uint, &nr_obsoletes,0644 ); +MODULE_PARM_DESC (obsoletes,"obsoletes provides ids" );#endif #include <linux/seq_file.h>
@@ -53,10 +58,11 @@ static int test_klp_atomic_replace_init(void)#ifdef KLP_HAS_REPLACE patch.replace = replace;#else -/* -* TODO: Set provides/obsoletes from the module parameters -* for the provides/obsoletes based tests (to be added later). -*/ + patch.provides = provides; +if (nr_obsoletes >0 ) { + patch.obsoletes = obsoletes; + patch.nr_obsoletes = nr_obsoletes; + }#endif return klp_enable_patch (&patch); }
tools/testing/selftests/livepatch/test_modules/test_klp_callbacks_demo2.c +4 -8
@@ -12,10 +12,9 @@ static int replace;module_param (replace, int,0644 );MODULE_PARM_DESC (replace,"replace (default=0)" );#else -/* -* TODO: Add provides/obsoletes module parameters for the -* provides/obsoletes based tests (to be added later). -*/ +static unsigned int provides; +module_param (provides, uint,0644 ); +MODULE_PARM_DESC (provides,"provides id (default=0)" );#endif static const char *const module_state[] = {
@@ -89,10 +88,7 @@ static int test_klp_callbacks_demo2_init(void)#ifdef KLP_HAS_REPLACE patch.replace = replace;#else -/* -* TODO: Set provides/obsoletes from the module parameters -* for the provides/obsoletes based tests (to be added later). -*/ + patch.provides = provides;#endif return klp_enable_patch (&patch); }
tools/testing/selftests/livepatch/test_modules/test_klp_livepatch.c +9 -0
@@ -7,6 +7,12 @@#include <linux/kernel.h> #include <linux/livepatch.h> +#ifndef KLP_HAS_REPLACE +static unsigned int provides; +module_param (provides, uint,0644 ); +MODULE_PARM_DESC (provides,"provides id (default=0)" ); +#endif +#include <linux/seq_file.h> static int livepatch_cmdline_proc_show (struct seq_file *m,void *v) {
@@ -36,6 +42,9 @@ static struct klp_patch patch = {static int test_klp_livepatch_init (void ) { +#ifndef KLP_HAS_REPLACE + patch.provides = provides; +#endif return klp_enable_patch (&patch); }
tools/testing/selftests/livepatch/test_modules/test_klp_state.c +1 -4
@@ -145,10 +145,7 @@ static struct klp_patch patch = {#ifdef KLP_HAS_REPLACE .replace = true,#else -/* -* TODO: Add provides/obsoletes module parameters for the -* provides/obsoletes based tests (to be added later). -*/ +/* provides=0 by default, replaces all provides=0 patches */ #endif };
tools/testing/selftests/livepatch/test_modules/test_klp_state2.c +1 -4
@@ -174,10 +174,7 @@ static struct klp_patch patch = {#ifdef KLP_HAS_REPLACE .replace = true,#else -/* -* TODO: Add provides/obsoletes module parameters for the -* provides/obsoletes based tests (to be added later). -*/ +/* provides=0 by default, replaces all provides=0 patches */ #endif };
Signature · 1 line
2.52.0
[email protected]
AI reviewRe: [PATCH v5 7/9] selftests: livepatch: Add provides/obsoletes test scenarios
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider: - [Medium] Race condition in test-provides-obsoletes.sh leading to flaky test failures. --
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [Medium] Race condition in test-provides-obsoletes.sh leading to flaky test failures. --
--- Patch [7]: [PATCH v5 7/9] selftests: livepatch: Add provides/obsoletes test scenarios --- commit b45263c641deb4b2b48bbe22a1f0bf862b20a22f
Author: Yafang Shao <[email protected]>
selftests: livepatch: Add provides/obsoletes test scenarios
This commit adds test scenarios for the new provides/obsoletes mechanism in livepatching. It introduces test-provides-obsoletes.sh to verify replacement, coexistence, and obsolete features.
Quoted 5 lines
tools/testing/selftests/livepatch/test-provides-obsoletes.sh new file +0
-0
[ ... ]
tools/testing/selftests/livepatch/test-provides-obsoletes.sh line 35
Quoted 10 lines
tools/testing/selftests/livepatch/test-provides-obsoletes.sh new file +10
-0+start_test "provides 0 replaces provides 0" + +load_lp $MOD_LIVEPATCH provides=0 +load_lp $MOD_ATOMIC provides=0 + +mods=($SYSFS_KLP_DIR/*) +nmods=${#mods[@]} +if [[ "$nmods" -ne 1 ]]; then + die "Expecting one module listed, found $nmods" +fi
[Severity: Medium]
Could this directory check race with the asynchronous sysfs cleanup of the replaced patch?
When load_lp returns, it polls until the new patch's sysfs transition file reads '0'. However, in the kernel, klp_try_complete_transition() clears klp_transition_patch before scheduling the asynchronous work to free replaced patches (klp_free_replaced_patches_async).
If the test checks the directory count immediately, couldn't it sporadically see the old patch before the async cleanup completes, causing the test to flake with "Expecting one module listed, found 2"?
This same pattern appears in the "same provides replaces" and "obsoletes replaces" test cases later in this script.
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=7
Re: [PATCH v5 7/9] selftests: livepatch: Add provides/obsoletes test scenarios
Quoted 41 lines · On Sun, Aug 9, 2026 at 5:31 PM <[email protected]> wrote:
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [Medium] Race condition intest-provides-obsoletes.shleading to flaky test failures. ----- Patch [7]: [PATCH v5 7/9] selftests: livepatch: Add provides/obsoletes test scenarios --- commit
b45263c641deb4b2b48bbe22a1f0bf862b20a22f
Author: Yafang Shao <[email protected]>selftests: livepatch: Add provides/obsoletes test scenarios
This commit adds test scenarios for the new provides/obsoletes mechanism in livepatching. It introduces
test-provides-obsoletes.shto verify replacement, coexistence, and obsolete features.Quoted 5 lines
tools/testing/selftests/livepatch/test-provides-obsoletes.sh new file +0
-0[ ... ]
tools/testing/selftests/livepatch/test-provides-obsoletes.sh line 35
Quoted 10 lines
tools/testing/selftests/livepatch/test-provides-obsoletes.sh new file +10
-0+start_test "provides 0 replaces provides 0" + +load_lp $MOD_LIVEPATCH provides=0 +load_lp $MOD_ATOMIC provides=0 + +mods=($SYSFS_KLP_DIR/*) +nmods=${#mods[@]} +if [[ "$nmods" -ne 1 ]]; then + die "Expecting one module listed, found $nmods" +fi[Severity: Medium]
Could this directory check race with the asynchronous sysfs cleanup of the replaced patch?When
load_lpreturns, it polls until the new patch's sysfs transition file reads '0'. However, in the kernel,klp_try_complete_transition()clearsklp_transition_patchbefore scheduling the asynchronous work to free replaced patches (klp_free_replaced_patches_async).
Quoted 6 lines
If the test checks the directory count immediately, couldn't it sporadically see the old patch before the async cleanup completes, causing the test to flake with "Expecting one module listed, found 2"?
This same pattern appears in the "same provides replaces" and "obsoletes replaces" test cases later in this script.
Makes sense.
I will update it in the next version.
Signature · 2 lines
Regards Yafang
PATCH v5 8/9 selftests: livepatch: Add test for state ID conflict across provides
In reply to Yafang ShaoLivepatches with different provides ids must not share the same state id. If a second livepatch attempts to reuse a state id already registered by a livepatch with a different provides id, the loading will fail. However, if the second livepatch's obsoletes list includes the first livepatch's provides id, the second livepatch replaces the first one and may reuse the same state id.
Add provides and obsoletes module parameters to test_klp_state.c and test_klp_state2.c (guarded by #ifndef KLP_HAS_REPLACE) so that they can be loaded with different provides ids and obsoletes ids.
Add a "state id conflict across provides" test scenario to test-provides.sh:
- Load test_klp_state with provides=1, which registers state ID 1.
- Attempt to load test_klp_state2 with provides=2, which reuses the same state ID 1. The second livepatch is rejected because livepatches with different provides ids must not share the same state id.
- Disable and unload the remaining livepatch.
Add a "taking over system state via obsoletes" test scenario to test-state.sh:
- Load test_klp_state with provides=10, which registers state ID 1.
- Load test_klp_state2 with provides=20 obsoletes=10, which reuses the same state ID 1. Although the provides ids differ, the second livepatch replaces the first one because its obsoletes list includes the first livepatch's provides id (10). The state is taken over successfully.
- Unload the replaced livepatch, then disable and unload the second livepatch.
--- .../livepatch/test-provides-obsoletes.sh | 86 +++++++++++++++++++ .../livepatch/test_modules/test_klp_state.c | 9 ++ .../livepatch/test_modules/test_klp_state2.c | 19 ++++ 3 files changed, 114 insertions(+)
tools/testing/selftests/livepatch/test-provides-obsoletes.sh +86 -0
@@ -6,6 +6,8 @@ MOD_ATOMIC=test_klp_atomic_replace MOD_LIVEPATCH=test_klp_livepatch +MOD_STATE=test_klp_state +MOD_STATE2=test_klp_state2 setup_config
@@ -192,4 +194,88 @@ livepatch: '$MOD_ATOMIC': completing unpatching transition livepatch: '$MOD_ATOMIC': unpatching complete % rmmod $MOD_ATOMIC" +# - load a livepatch with provides=1 that registers state ID 1 +# - try to load another livepatch with provides=2 that reuses the same +# state ID. The second one must be rejected because livepatches with +# different provides ids must not share the same state id. +# - disable and unload the remaining livepatch + +start_test "state id conflict across provides" + +load_lp $MOD_STATE provides=1 +load_failing_mod $MOD_STATE2 provides=2 + +disable_lp $MOD_STATE +unload_lp $MOD_STATE + +check_result "% insmod test_modules/$MOD_STATE.ko provides=1 +livepatch: enabling patch '$MOD_STATE' +livepatch: '$MOD_STATE': initializing patching transition +$MOD_STATE: pre_patch_callback: vmlinux +$MOD_STATE: allocate_loglevel_state: allocating space to store console_loglevel +livepatch: '$MOD_STATE': starting patching transition +livepatch: '$MOD_STATE': completing patching transition +$MOD_STATE: post_patch_callback: vmlinux +$MOD_STATE: fix_console_loglevel: fixing console_loglevel +livepatch: '$MOD_STATE': patching complete +% insmod test_modules/$MOD_STATE2.ko provides=2 +livepatch: Livepatch patch ($MOD_STATE2) is not compatible with the already installed livepatches. +insmod: ERROR: could not insert module test_modules/$MOD_STATE2.ko: Invalid parameters +% echo 0 > $SYSFS_KLP_DIR/$MOD_STATE/enabled +livepatch: '$MOD_STATE': initializing unpatching transition +$MOD_STATE: pre_unpatch_callback: vmlinux +$MOD_STATE: restore_console_loglevel: restoring console_loglevel +livepatch: '$MOD_STATE': starting unpatching transition +livepatch: '$MOD_STATE': completing unpatching transition +$MOD_STATE: post_unpatch_callback: vmlinux +$MOD_STATE: free_loglevel_state: freeing space for the stored console_loglevel +livepatch: '$MOD_STATE': unpatching complete +% rmmod $MOD_STATE" + + +# Take over system state change by a patch that obsoletes the old one. +# Although the provides IDs are different, the second patch's obsoletes +# list includes the first patch's provides ID, so it can replace the +# first patch and reuse the same state ID. + +start_test "taking over system state via obsoletes" + +load_lp $MOD_STATE provides=10 +load_lp $MOD_STATE2 provides=20 obsoletes=10 +unload_lp $MOD_STATE +disable_lp $MOD_STATE2 +unload_lp $MOD_STATE2 + +check_result "% insmod test_modules/$MOD_STATE.ko provides=10 +livepatch: enabling patch '$MOD_STATE' +livepatch: '$MOD_STATE': initializing patching transition +$MOD_STATE: pre_patch_callback: vmlinux +$MOD_STATE: allocate_loglevel_state: allocating space to store console_loglevel +livepatch: '$MOD_STATE': starting patching transition +livepatch: '$MOD_STATE': completing patching transition +$MOD_STATE: post_patch_callback: vmlinux +$MOD_STATE: fix_console_loglevel: fixing console_loglevel +livepatch: '$MOD_STATE': patching complete +% insmod test_modules/$MOD_STATE2.ko provides=20 obsoletes=10 +livepatch: enabling patch '$MOD_STATE2' +livepatch: '$MOD_STATE2': initializing patching transition +$MOD_STATE2: pre_patch_callback: vmlinux +$MOD_STATE2: allocate_loglevel_state: space to store console_loglevel already allocated +livepatch: '$MOD_STATE2': starting patching transition +livepatch: '$MOD_STATE2': completing patching transition +$MOD_STATE2: post_patch_callback: vmlinux +$MOD_STATE2: fix_console_loglevel: taking over the console_loglevel change +livepatch: '$MOD_STATE2': patching complete +% rmmod $MOD_STATE +% echo 0 > $SYSFS_KLP_DIR/$MOD_STATE2/enabled +livepatch: '$MOD_STATE2': initializing unpatching transition +$MOD_STATE2: pre_unpatch_callback: vmlinux +$MOD_STATE2: restore_console_loglevel: restoring console_loglevel +livepatch: '$MOD_STATE2': starting unpatching transition +livepatch: '$MOD_STATE2': completing unpatching transition +$MOD_STATE2: post_unpatch_callback: vmlinux +$MOD_STATE2: free_loglevel_state: freeing space for the stored console_loglevel +livepatch: '$MOD_STATE2': unpatching complete +% rmmod $MOD_STATE2" + exit 0
tools/testing/selftests/livepatch/test_modules/test_klp_state.c +9 -0
@@ -9,6 +9,12 @@#include <linux/printk.h> #include <linux/livepatch.h> +#ifndef KLP_HAS_REPLACE +static unsigned int provides; +module_param (provides, uint,0644 ); +MODULE_PARM_DESC (provides,"provides id (default=0)" ); +#endif +#define CONSOLE_LOGLEVEL_STATE 1/* Version 1 does not support migration. */ #define CONSOLE_LOGLEVEL_STATE_VERSION 1
@@ -151,6 +157,9 @@ static struct klp_patch patch = {static int test_klp_callbacks_demo_init (void ) { +#ifndef KLP_HAS_REPLACE + patch.provides = provides; +#endif return klp_enable_patch (&patch); }
tools/testing/selftests/livepatch/test_modules/test_klp_state2.c +19 -0
@@ -9,6 +9,18 @@#include <linux/printk.h> #include <linux/livepatch.h> +#ifndef KLP_HAS_REPLACE +static unsigned int provides; +module_param (provides, uint,0644 ); +MODULE_PARM_DESC (provides,"provides id (default=0)" ); + +#define KLP_MAX_OBSOLETES 16 +static unsigned int obsoletes[KLP_MAX_OBSOLETES ]; +static int nr_obsoletes; +module_param_array (obsoletes, uint, &nr_obsoletes,0644 ); +MODULE_PARM_DESC (obsoletes,"obsoletes provides ids" ); +#endif +#define CONSOLE_LOGLEVEL_STATE 1/* Version 2 supports migration. */ #define CONSOLE_LOGLEVEL_STATE_VERSION 2
@@ -180,6 +192,13 @@ static struct klp_patch patch = {static int test_klp_callbacks_demo_init (void ) { +#ifndef KLP_HAS_REPLACE + patch.provides = provides; +if (nr_obsoletes >0 ) { + patch.obsoletes = obsoletes; + patch.nr_obsoletes = nr_obsoletes; + } +#endif return klp_enable_patch (&patch); }
Signature · 1 line
2.52.0
PATCH v5 9/9 selftests: livepatch: Add test for function conflict across provides
In reply to Yafang ShaoLivepatches with different provides ids must not modify the same function. If a second livepatch attempts to modify a function that has already been modified by a loaded livepatch with a different provides id, the loading will fail. If the second livepatch shares the same provides id, or if its obsoletes list contains the first livepatch's provides id, it will load successfully and replace the first one.
Add a new test module test_klp_provides.c that patches meminfo_proc_show (the same function as test_klp_atomic_replace) so that the two modules can be loaded with different provides ids to test the function conflict detection.
Add three test scenarios to test-provides.sh:
1. Function conflict across provides: load test_klp_atomic_replace with provides=1, then attempt to load test_klp_provides with provides=2. The second livepatch is rejected because livepatches with different provides ids and no obsoletes must not modify the same function.
2. Function replace within same provides: load test_klp_atomic_replace with provides=1, then load test_klp_provides with provides=1. The second livepatch loads successfully and replaces the first one because they share the same provides id.
3. Function replace across provides with obsoletes: load test_klp_atomic_replace with provides=1, then load test_klp_provides with provides=2 and obsoletes=[1]. The second livepatch loads successfully and replaces the first one because the obsoletes list allows it to replace a livepatch with a different provides id.
--- .../livepatch/test-provides-obsoletes.sh | 120 ++++++++++++++++++ .../selftests/livepatch/test_modules/Makefile | 1 + .../test_modules/test_klp_provides.c | 72 +++++++++++ 3 files changed, 193 insertions(+) create mode 100644 tools/testing/selftests/livepatch/test_modules/test_klp_provides.c
tools/testing/selftests/livepatch/test-provides-obsoletes.sh +120 -0
@@ -8,6 +8,7 @@ MOD_ATOMIC=test_klp_atomic_replace MOD_LIVEPATCH=test_klp_livepatch MOD_STATE=test_klp_state MOD_STATE2=test_klp_state2 +MOD_PROVIDES=test_klp_provides setup_config
@@ -278,4 +279,123 @@ $MOD_STATE2: free_loglevel_state: freeing space for the stored console_loglevel livepatch: '$MOD_STATE2': unpatching complete % rmmod $MOD_STATE2" + +# - load a livepatch with provides=1 that modifies meminfo_proc_show +# - try to load another livepatch with provides=2 that modifies the +# same function. The second one must be rejected because livepatches +# with different provides ids and no obsoletes must not modify +# the same function. +# - disable and unload the remaining livepatch + +start_test "function conflict across provides" + +load_lp $MOD_ATOMIC provides=1 +load_failing_mod $MOD_PROVIDES provides=2 + +disable_lp $MOD_ATOMIC +unload_lp $MOD_ATOMIC + +check_result "% insmod test_modules/$MOD_ATOMIC.ko provides=1 +livepatch: enabling patch '$MOD_ATOMIC' +livepatch: '$MOD_ATOMIC': initializing patching transition +livepatch: '$MOD_ATOMIC': starting patching transition +livepatch: '$MOD_ATOMIC': completing patching transition +livepatch: '$MOD_ATOMIC': patching complete +% insmod test_modules/$MOD_PROVIDES.ko provides=2 +livepatch: Livepatch patch ($MOD_PROVIDES) is not compatible with the already installed livepatches. +insmod: ERROR: could not insert module test_modules/$MOD_PROVIDES.ko: Invalid parameters +% echo 0 > $SYSFS_KLP_DIR/$MOD_ATOMIC/enabled +livepatch: '$MOD_ATOMIC': initializing unpatching transition +livepatch: '$MOD_ATOMIC': starting unpatching transition +livepatch: '$MOD_ATOMIC': completing unpatching transition +livepatch: '$MOD_ATOMIC': unpatching complete +% rmmod $MOD_ATOMIC" + + +# - load a livepatch with provides=1 that modifies meminfo_proc_show +# - load another livepatch with provides=1 that modifies the same +# function. The second one loads successfully because livepatches +# with the same provides id replace each other. +# - disable and unload the remaining livepatch + +start_test "function replace within same provides" + +load_lp $MOD_ATOMIC provides=1 +load_lp $MOD_PROVIDES provides=1 + +mods=($SYSFS_KLP_DIR/*) +nmods=${#mods[@]} +if [[ "$nmods" -ne 1 ]]; then + die "Expecting one module listed, found $nmods" +fi +check_sysfs_value "$MOD_PROVIDES" "enabled" "1" + +disable_lp $MOD_PROVIDES +unload_lp $MOD_PROVIDES +unload_lp $MOD_ATOMIC + +check_result "% insmod test_modules/$MOD_ATOMIC.ko provides=1 +livepatch: enabling patch '$MOD_ATOMIC' +livepatch: '$MOD_ATOMIC': initializing patching transition +livepatch: '$MOD_ATOMIC': starting patching transition +livepatch: '$MOD_ATOMIC': completing patching transition +livepatch: '$MOD_ATOMIC': patching complete +% insmod test_modules/$MOD_PROVIDES.ko provides=1 +livepatch: enabling patch '$MOD_PROVIDES' +livepatch: '$MOD_PROVIDES': initializing patching transition +livepatch: '$MOD_PROVIDES': starting patching transition +livepatch: '$MOD_PROVIDES': completing patching transition +livepatch: '$MOD_PROVIDES': patching complete +% echo 0 > $SYSFS_KLP_DIR/$MOD_PROVIDES/enabled +livepatch: '$MOD_PROVIDES': initializing unpatching transition +livepatch: '$MOD_PROVIDES': starting unpatching transition +livepatch: '$MOD_PROVIDES': completing unpatching transition +livepatch: '$MOD_PROVIDES': unpatching complete +% rmmod $MOD_PROVIDES +% rmmod $MOD_ATOMIC" + + +# - load a livepatch with provides=1 that modifies meminfo_proc_show +# - load another livepatch with provides=2 and obsoletes=[1] that +# modifies the same function. The second one loads successfully +# because the obsoletes list allows it to replace the first one +# even though they have different provides ids. +# - disable and unload the remaining livepatch + +start_test "function replace across provides with obsoletes" + +load_lp $MOD_ATOMIC provides=1 +load_lp $MOD_PROVIDES provides=2 obsoletes=1 + +mods=($SYSFS_KLP_DIR/*) +nmods=${#mods[@]} +if [[ "$nmods" -ne 1 ]]; then + die "Expecting one module listed, found $nmods" +fi +check_sysfs_value "$MOD_PROVIDES" "enabled" "1" + +disable_lp $MOD_PROVIDES +unload_lp $MOD_PROVIDES +unload_lp $MOD_ATOMIC + +check_result "% insmod test_modules/$MOD_ATOMIC.ko provides=1 +livepatch: enabling patch '$MOD_ATOMIC' +livepatch: '$MOD_ATOMIC': initializing patching transition +livepatch: '$MOD_ATOMIC': starting patching transition +livepatch: '$MOD_ATOMIC': completing patching transition +livepatch: '$MOD_ATOMIC': patching complete +% insmod test_modules/$MOD_PROVIDES.ko provides=2 obsoletes=1 +livepatch: enabling patch '$MOD_PROVIDES' +livepatch: '$MOD_PROVIDES': initializing patching transition +livepatch: '$MOD_PROVIDES': starting patching transition +livepatch: '$MOD_PROVIDES': completing patching transition +livepatch: '$MOD_PROVIDES': patching complete +% echo 0 > $SYSFS_KLP_DIR/$MOD_PROVIDES/enabled +livepatch: '$MOD_PROVIDES': initializing unpatching transition +livepatch: '$MOD_PROVIDES': starting unpatching transition +livepatch: '$MOD_PROVIDES': completing unpatching transition +livepatch: '$MOD_PROVIDES': unpatching complete +% rmmod $MOD_PROVIDES +% rmmod $MOD_ATOMIC" + exit 0
tools/testing/selftests/livepatch/test_modules/Makefile +1 -0
@@ -10,6 +10,7 @@ obj-m += test_klp_atomic_replace.o \ test_klp_livepatch.o \ test_klp_mod_patch.o \ test_klp_mod_target.o \ + test_klp_provides.o \ test_klp_shadow_vars.o \ test_klp_state.o \ test_klp_state2.o \
tools/testing/selftests/livepatch/test_modules/test_klp_provides.c new file +72 -0
72 lines not on this page · raw message
Signature · 1 line
2.52.0
[email protected]
AI reviewRe: [PATCH v5 9/9] selftests: livepatch: Add test for function conflict across provides
Thank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider:
Thank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider:
New issues:
- [High] Malformed livepatch module missing old_name causes a kernel panic on load.
Pre-existing issues:
- [Medium] Race condition in sysfs polling causes flaky selftests. --
--- Patch [9]: [PATCH v5 9/9] selftests: livepatch: Add test for function conflict across provides --- commit eaca609db12a6842c4e94debcf9d9ef5049826db
Author: Yafang Shao <[email protected]>
selftests: livepatch: Add test for function conflict across provides
This commit adds a new test module test_klp_provides.c and three new test scenarios to test-provides-obsoletes.sh. These tests verify the behavior of livepatch function conflicts and replacement when using different provides IDs with and without obsoletes.
Quoted 4 lines
tools/testing/selftests/livepatch/test-provides-obsoletes.sh +0
-0
[ ... ]
Quoted 41 lines
tools/testing/selftests/livepatch/test-provides-obsoletes.sh +37
-0@@ -278,4 +279,123 @@ $MOD_STATE2: free_loglevel_state: freeing space for the stored console_loglevel livepatch: '$MOD_STATE2': unpatching complete % rmmod $MOD_STATE2" + +# - load a livepatch with provides=1 that modifies meminfo_proc_show +# - try to load another livepatch with provides=2 that modifies the +# same function. The second one must be rejected because livepatches +# with different provides ids and no obsoletes must not modify +# the same function. +# - disable and unload the remaining livepatch + +start_test "function conflict across provides" + +load_lp $MOD_ATOMIC provides=1 +load_failing_mod $MOD_PROVIDES provides=2 + +disable_lp $MOD_ATOMIC +unload_lp $MOD_ATOMIC + +check_result "% insmod test_modules/$MOD_ATOMIC.ko provides=1 +livepatch: enabling patch '$MOD_ATOMIC' +livepatch: '$MOD_ATOMIC': initializing patching transition +livepatch: '$MOD_ATOMIC': starting patching transition +livepatch: '$MOD_ATOMIC': completing patching transition +livepatch: '$MOD_ATOMIC': patching complete +% insmod test_modules/$MOD_PROVIDES.ko provides=2 +livepatch: Livepatch patch ($MOD_PROVIDES) is not compatible with the already installed livepatches. +insmod: ERROR: could not insert module test_modules/$MOD_PROVIDES.ko: Invalid parameters +% echo 0 > $SYSFS_KLP_DIR/$MOD_ATOMIC/enabled +livepatch: '$MOD_ATOMIC': initializing unpatching transition +livepatch: '$MOD_ATOMIC': starting unpatching transition +livepatch: '$MOD_ATOMIC': completing unpatching transition +livepatch: '$MOD_ATOMIC': unpatching complete +% rmmod $MOD_ATOMIC" + + +# - load a livepatch with provides=1 that modifies meminfo_proc_show +# - load another livepatch with provides=1 that modifies the same +# function. The second one loads successfully because livepatches +# with the same provides id replace each other.
tools/testing/selftests/livepatch/test-provides-obsoletes.sh $MOD_STATE2: free_loglevel_state: freeing space for the stored console_loglevel · line 328
Quoted 10 lines
tools/testing/selftests/livepatch/test-provides-obsoletes.sh +10
-0+# - disable and unload the remaining livepatch + +start_test "function replace within same provides" + +load_lp $MOD_ATOMIC provides=1 +load_lp $MOD_PROVIDES provides=1 + +mods=($SYSFS_KLP_DIR/*) +nmods=${#mods[@]} +if [[ "$nmods" -ne 1 ]]; then
[Severity: Medium]
This is a pre-existing issue, but does this pattern cause a regression in the form of flaky selftests?
The script synchronously counts the directories in the sysfs path immediately after a transition. However, the old patch's sysfs directory removal is deferred to an asynchronous workqueue:
kernel/livepatch/core.c:klp_free_replaced_patches_async()
Because of this, the script may momentarily see 2 directories instead of 1 before the asynchronous cleanup completes.
Quoted 2 lines
tools/testing/selftests/livepatch/test-provides-obsoletes.sh +2
-0+ die "Expecting one module listed, found $nmods" +fi
[ ... ]
Quoted 8 lines
tools/testing/selftests/livepatch/test_modules/test_klp_provides.c new file +2
-0@@ -0,0 +1,72 @@ +// SPDX-License-Identifier: GPL-2.0 +
[ ... ]
tools/testing/selftests/livepatch/test_modules/test_klp_provides.c line 61
Quoted 12 lines
tools/testing/selftests/livepatch/test_modules/test_klp_provides.c new file +12
-0+static int test_klp_provides_init (void ) +{ +#ifdef KLP_HAS_REPLACE + patch.replace = replace; +#else + patch.provides = provides; +if (nr_obsoletes >0 ) { + patch.obsoletes = obsoletes; + patch.nr_obsoletes = nr_obsoletes; + } +#endif +return klp_enable_patch (&patch);
[Severity: High]
Does klp_enable_patch() introduce a regression where a malformed livepatch module missing old_name causes a kernel panic on load?
If a livepatch module defines a klp_func with .old_name = NULL but provides .new_func, it bypasses the klp_for_each_func_static loop termination condition.
Since klp_is_patch_compatible() executes before klp_init_patch():
kernel/livepatch/core.c:klp_enable_patch() {
...
if (!klp_is_patch_compatible(patch)) {
...
ret = klp_init_patch(patch);
...
}klp_has_function_conflict() iterates over this malformed function, which then passes the malformed func to klp_find_func():
kernel/livepatch/state.c:klp_has_function_conflict() {
...
if (klp_find_func(old_obj, func))
...
}This then passes func->old_name to strcmp():
kernel/livepatch/core.c:klp_find_func() {
...
if ((strcmp(obj_func->old_name, func->old_name) == 0) &&
...
}resulting in an immediate null pointer dereference.
Quoted 1 line
+}
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=9