module: fix lost error code from codetag_load_module()
Subsystems (2)
PATCH 1/1 module: fix lost error code from codetag_load_module()
If codetag_load_module() fails, err is never set and load_module() returns 0 after the module has been torn down.
--- kernel/module/main.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-)
kernel/module/main.c +2 -1
@@ -3568,7 +3568,8 @@ static int load_module(struct load_info *info, const char __user *uargs, goto sysfs_cleanup; } -if (codetag_load_module (mod)) + err =codetag_load_module (mod); +if (err) goto sysfs_cleanup;/* Get rid of temporary copy. */
Signature · 1 line
2.25.1
[email protected]
AI reviewThank 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] Missing call to codetag_unload_module() in the error path leaves dangling module pointers in codetag_types IDR. --
--- Patch [1]: [PATCH] module: fix lost error code from codetag_load_module() --- commit 5006baafc1e00091b0abb9c8d393876e887ccb31
Author: Hao Ge <[email protected]>
module: fix lost error code from codetag_load_module()
This commit fixes a bug where the error code from codetag_load_module() was previously lost. It ensures load_module() correctly captures the error and aborts module loading upon failure.
kernel/module/main.c static int load_module(struct load_info *info, const char __user *uargs, · line 3586
Quoted 10 lines
kernel/module/main.c +2
-1@@ -3581,7 +3581,8 @@ static int load_module(struct load_info *info, const char __user *uargs, goto sysfs_cleanup; } -if (codetag_load_module (mod)) + err =codetag_load_module (mod); +if (err) goto sysfs_cleanup;
[Severity: High]
This is a pre-existing issue, but by fixing the error path here, we might expose a use-after-free bug during module load failures.
If codetag_load_module() fails partway through the list of codetag_types:
lib/codetag.c:codetag_load_module() {
...
list_for_each_entry(cttype, &codetag_types, link) {
ret = codetag_module_init(cttype, mod);
if (ret)
break;
}
...
}It returns an error without rolling back the types that were already successfully initialized. We then jump to sysfs_cleanup, which eventually calls module_deallocate() in the free_module block to free the module memory.
However, there is no call to codetag_unload_module() anywhere in this cleanup path to clean up the dangling references from the codetag_types IDR.
Will this leave dangling pointers that result in a use-after-free dereference of the freed module memory when codetags are later iterated (for example, when reading from /proc/allocinfo)?
lib/codetag.c:codetag_next_ct() {
...
ct = get_first_module_ct(cmod);
...
}Quoted 2 lines
kernel/module/main.c +0
-0/* Get rid of temporary copy. */
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1
Quoted 8 lines · Hao Ge
If
codetag_load_module()fails, err is never set andload_module()returns 0 after the module has been torn down.Fixes 044d2aee6c57 ("alloc_tag: handle module codetag load errors as module load failures")Reported-by Sashiko <[email protected]>Signed-off-by Hao Ge <[email protected]>
Quoted 21 lines
--- kernel/module/main.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-)kernel/module/main.c +2
-2@@ -3568,7 +3568,8 @@ static int load_module(struct load_info *info, const char __user *uargs, goto sysfs_cleanup; } -if (codetag_load_module (mod)) + err =codetag_load_module (mod); +if (err) goto sysfs_cleanup;/* Get rid of temporary copy. */ --2.25.1
Quoted 28 lines · Hao Ge
If
codetag_load_module()fails, err is never set andload_module()returns 0 after the module has been torn down.Fixes 044d2aee6c57 ("alloc_tag: handle module codetag load errors as module load failures")Reported-by Sashiko <[email protected]>Signed-off-by Hao Ge <[email protected]>--- kernel/module/main.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-)kernel/module/main.c +2
-1@@ -3568,7 +3568,8 @@ static int load_module(struct load_info *info, const char __user *uargs, goto sysfs_cleanup; } -if (codetag_load_module (mod)) + err =codetag_load_module (mod); +if (err) goto sysfs_cleanup;/* Get rid of temporary copy. */ Signature · 1 line
2.25.1
Signature · 1 line
Aaron Tomlin
Quoted 3 lines · Hao Ge
If
codetag_load_module()fails, err is never set andload_module()returns 0 after the module has been torn down.
I see!
Quoted 3 lines
Fixes 044d2aee6c57 ("alloc_tag: handle module codetag load errors as module load failures")Reported-by Sashiko <[email protected]>
Quoted 10 lines
Signed-off-by Hao Ge <[email protected]>---
kernel/module/main.c| 3 ++-1 file changed, 2 insertions(+), 1 deletion(-)kernel/module/main.c +0
-0
kernel/module/main.c static int load_module(struct load_info *info, const char __user *uargs, · line 3572
Quoted 6 lines
kernel/module/main.c +2
-1@@ -3568,7 +3568,8 @@ static int load_module(struct load_info *info, const char __user *uargs, goto sysfs_cleanup; } -if (codetag_load_module (mod)) + err =codetag_load_module (mod); +if (err)
Nice!
kernel/module/main.c static int load_module(struct load_info *info, const char __user *uargs, · line 3575
Quoted 4 lines
kernel/module/main.c +0
-0goto sysfs_cleanup;/* Get rid of temporary copy. */
Petr Pavlu
maintainerQuoted 16 lines · Hao Ge
If
codetag_load_module()fails, err is never set andload_module()returns 0 after the module has been torn down.Fixes 044d2aee6c57 ("alloc_tag: handle module codetag load errors as module load failures")Reported-by Sashiko <[email protected]>Signed-off-by Hao Ge <[email protected]>--- kernel/module/main.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-)kernel/module/main.c +0
-0
kernel/module/main.c static int load_module(struct load_info *info, const char __user *uargs, · line 3575
Quoted 9 lines
kernel/module/main.c +2
-1@@ -3568,7 +3568,8 @@ static int load_module(struct load_info *info, const char __user *uargs, goto sysfs_cleanup; } -if (codetag_load_module (mod)) + err =codetag_load_module (mod); +if (err) goto sysfs_cleanup;/* Get rid of temporary copy. */
This looks ok to me but there appears another bug related to this code that would be good to fix at the same time. If the module is a livepatch, the preceding call to copy_module_elf() allocates mod->klp_info. However, if codetag_load_module() fails, the code doesn't free it.
I think we want something like this (not tested):
kernel/module/main.c +6 -2
@@ -3581,8 +3581,9 @@ static int load_module(struct load_info *info, const char __user *uargs, goto sysfs_cleanup; } -if (codetag_load_module (mod)) - goto sysfs_cleanup; + err =codetag_load_module (mod); +if (err) + goto livepatch_cleanup;/* Get rid of temporary copy. */ free_copy (info, flags);
@@ -3592,6 +3593,9 @@ static int load_module(struct load_info *info, const char __user *uargs,return do_init_module (mod); + livepatch_cleanup: +if (is_livepatch_module (mod)) +free_module_elf (mod); sysfs_cleanup:mod_sysfs_teardown (mod); coming_cleanup:
Signature · 2 lines
Thanks, Petr
Quoted 26 lines · Petr Pavlu
Quoted 24 lines · Hao Ge
If
codetag_load_module()fails, err is never set andload_module()returns 0 after the module has been torn down.Fixes 044d2aee6c57 ("alloc_tag: handle module codetag load errors as module load failures")Reported-by Sashiko <[email protected]>Signed-off-by Hao Ge <[email protected]>--- kernel/module/main.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-)kernel/module/main.c +2
-1@@ -3568,7 +3568,8 @@ static int load_module(struct load_info *info, const char __user *uargs, goto sysfs_cleanup; } -if (codetag_load_module (mod)) + err =codetag_load_module (mod); +if (err) goto sysfs_cleanup;
Quoted 7 lines
kernel/module/main.c static int load_module(struct load_info *info, const char __user *uargs, · line 3588
Quoted 1 line
kernel/module/main.c +0
-0/* Get rid of temporary copy. */ This looks ok to me but there appears another bug related to this code that would be good to fix at the same time. If the module is a livepatch, the preceding call to
copy_module_elf()allocatesmod->klp_info. However, ifcodetag_load_module()fails, the code doesn't free it.
Ah, I see. Nice catch!
Quoted 2 lines
I think we want something like this (not tested):
Yep, that looks like the right fix to me. Thanks!
Quoted 31 lines
kernel/module/main.c +6
-3@@ -3581,8 +3581,9 @@ static int load_module(struct load_info *info, const char __user *uargs, goto sysfs_cleanup; } -if (codetag_load_module (mod)) - goto sysfs_cleanup; + err =codetag_load_module (mod); +if (err) + goto livepatch_cleanup;/* Get rid of temporary copy. */ free_copy (info, flags);@@ -3592,6 +3593,9 @@ static int load_module(struct load_info *info, const char __user *uargs,return do_init_module (mod); + livepatch_cleanup: +if (is_livepatch_module (mod)) +free_module_elf (mod); sysfs_cleanup:mod_sysfs_teardown (mod); coming_cleanup: --Thanks,
Petr
Bradley Morgan
Reviewed-byQuoted 56 lines · Petr Pavlu
Quoted 4 lines · Hao Ge
If
codetag_load_module()fails, err is never set andload_module()returns 0 after the module has been torn down.Fixes 044d2aee6c57 ("alloc_tag: handle module codetag load errors asmodule load failures")
kernel/module/main.c static int load_module(struct load_info *info,
Quoted 12 lines
--- kernel/module/main.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-)kernel/module/main.c +0
-0@@ -3568,7 +3568,8 @@ static int load_module(struct load_info *info,const char
__user*uargs,kernel/module/main.c static int load_module(struct load_info *info, const char __user *uargs, · line 3588
Quoted 9 lines
kernel/module/main.c +2
-1goto sysfs_cleanup; } -if (codetag_load_module (mod)) + err =codetag_load_module (mod); +if (err) goto sysfs_cleanup;/* Get rid of temporary copy. */ This looks ok to me but there appears another bug related to this code that would be good to fix at the same time. If the module is a livepatch, the preceding call to
copy_module_elf()allocatesmod->klp_info. However, ifcodetag_load_module()fails, the code doesn't free it.I think we want something like this (not tested):
kernel/module/main.c +3
-2@@ -3581,8 +3581,9 @@ static int load_module(struct load_info *info, const char __user *uargs, goto sysfs_cleanup; } -if (codetag_load_module (mod)) - goto sysfs_cleanup; + err =codetag_load_module (mod); +if (err) + goto livepatch_cleanup;/* Get rid of temporary copy. */ free_copy (info, flags);
kernel/module/main.c static int load_module(struct load_info *info, const char __user *uargs, · line 3601
Quoted 9 lines
kernel/module/main.c +3
-0@@ -3592,6 +3593,9 @@ static int load_module(struct load_info *info, const char __user *uargs,return do_init_module (mod); + livepatch_cleanup: +if (is_livepatch_module (mod)) +free_module_elf (mod); sysfs_cleanup:mod_sysfs_teardown (mod); coming_cleanup:
If patch is submitted!
Hi Petr
Quoted 25 lines · Petr Pavlu
Quoted 24 lines · Hao Ge
If
codetag_load_module()fails, err is never set andload_module()returns 0 after the module has been torn down.Fixes 044d2aee6c57 ("alloc_tag: handle module codetag load errors as module load failures")Reported-by Sashiko <[email protected]>Signed-off-by Hao Ge <[email protected]>--- kernel/module/main.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-)kernel/module/main.c +2
-1@@ -3568,7 +3568,8 @@ static int load_module(struct load_info *info, const char __user *uargs, goto sysfs_cleanup; } -if (codetag_load_module (mod)) + err =codetag_load_module (mod); +if (err) goto sysfs_cleanup;
Quoted 8 lines
kernel/module/main.c static int load_module(struct load_info *info, const char __user *uargs, · line 3588
Quoted 1 line
kernel/module/main.c +0
-0/* Get rid of temporary copy. */ This looks ok to me but there appears another bug related to this code that would be good to fix at the same time. If the module is a livepatch, the preceding call to
copy_module_elf()allocatesmod->klp_info. However, ifcodetag_load_module()fails, the code doesn't free it.
Yes, I missed that. Thanks for catching it!
I've folded your suggested fix into v2 (already sent) and tested it.
Thanks
Best Regards
Hao
Quoted 29 lines
I think we want something like this (not tested):
kernel/module/main.c +6
-2@@ -3581,8 +3581,9 @@ static int load_module(struct load_info *info, const char __user *uargs, goto sysfs_cleanup; } -if (codetag_load_module (mod)) - goto sysfs_cleanup; + err =codetag_load_module (mod); +if (err) + goto livepatch_cleanup;/* Get rid of temporary copy. */ free_copy (info, flags);@@ -3592,6 +3593,9 @@ static int load_module(struct load_info *info, const char __user *uargs,return do_init_module (mod); + livepatch_cleanup: +if (is_livepatch_module (mod)) +free_module_elf (mod); sysfs_cleanup:mod_sysfs_teardown (mod); coming_cleanup: