Skip to contentSkip to conversation

module: fix lost error code from codetag_load_module()

PATCH Superseded Hao Ge 6 participants 9 messages Aug 24 – Aug 27

Subsystems (2)

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore

Hao Ge

author
#0 lore raw

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.

Fixes 044d2aee6c57 ("alloc_tag: handle module codetag load errors as module load failures")
---
 kernel/module/main.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
kernel/module/main.c +2 -1
index 46dd8d25a605..afb810f0154c 100644
@@ -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
#1 lore raw
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] 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
Acked-by on v1 1/1
#2 lore raw
In reply to Hao Ge
Quoted 8 lines · Hao Ge

If codetag_load_module() fails, err is never set and load_module() returns 0 after the module has been torn down.

Fixes 044d2aee6c57 ("alloc_tag: handle module codetag load errors as module load failures")
Quoted 21 lines
---
 kernel/module/main.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
kernel/module/main.c +2 -2
index 46dd8d25a605..afb810f0154c 100644
@@ -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

reviewer Reviewed-by on v1 1/1
#3 lore raw
In reply to Hao Ge
Quoted 28 lines · Hao Ge

If codetag_load_module() fails, err is never set and load_module() returns 0 after the module has been torn down.

Fixes 044d2aee6c57 ("alloc_tag: handle module codetag load errors as module load failures")
---
 kernel/module/main.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
kernel/module/main.c +2 -1
index 46dd8d25a605..afb810f0154c 100644
@@ -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
Reviewed-by on v1 1/1
#4 lore raw
In reply to Hao Ge
Quoted 3 lines · Hao Ge

If codetag_load_module() fails, err is never set and load_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")
Quoted 10 lines

---
kernel/module/main.c | 3 ++-

1 file changed, 2 insertions(+), 1 deletion(-)
kernel/module/main.c +0 -0
index 46dd8d25a605..afb810f0154c 100644

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 -0
 		goto sysfs_cleanup;
 
 	/* Get rid of temporary copy. */

--- Thanks!
https://lore.kernel.org/all/[email protected]/

maintainer
#5 lore raw
In reply to Hao Ge
Quoted 16 lines · Hao Ge

If codetag_load_module() fails, err is never set and load_module() returns 0 after the module has been torn down.

Fixes 044d2aee6c57 ("alloc_tag: handle module codetag load errors as module load failures")
---
 kernel/module/main.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
kernel/module/main.c +0 -0
index 46dd8d25a605..afb810f0154c 100644

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
index d0e1e0bd2ad0..c1b34dc1e89a 100644
@@ -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
#6 lore raw
Quoted 26 lines · Petr Pavlu
Quoted 24 lines · Hao Ge

If codetag_load_module() fails, err is never set and load_module() returns 0 after the module has been torn down.

Fixes 044d2aee6c57 ("alloc_tag: handle module codetag load errors as module load failures")
---
 kernel/module/main.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
kernel/module/main.c +2 -1
index 46dd8d25a605..afb810f0154c 100644
@@ -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() allocates mod->klp_info. However, if codetag_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
index d0e1e0bd2ad0..c1b34dc1e89a 100644
@@ -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

Reviewed-by
#7 lore raw
In reply to Petr Pavlu
Quoted 56 lines · Petr Pavlu
Quoted 4 lines · Hao Ge

If codetag_load_module() fails, err is never set and load_module() returns 0 after the module has been torn down.

Fixes 044d2aee6c57 ("alloc_tag: handle module codetag load errors as

module 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
index 46dd8d25a605..afb810f0154c 100644
@@ -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 -1
 			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 +3 -2
index d0e1e0bd2ad0..c1b34dc1e89a 100644
@@ -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!

--- Thanks!
https://lore.kernel.org/all/[email protected]/

author
#8 lore raw
In reply to Petr Pavlu

Hi Petr

Quoted 25 lines · Petr Pavlu
Quoted 24 lines · Hao Ge

If codetag_load_module() fails, err is never set and load_module() returns 0 after the module has been torn down.

Fixes 044d2aee6c57 ("alloc_tag: handle module codetag load errors as module load failures")
---
 kernel/module/main.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
kernel/module/main.c +2 -1
index 46dd8d25a605..afb810f0154c 100644
@@ -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() allocates mod->klp_info. However, if codetag_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
index d0e1e0bd2ad0..c1b34dc1e89a 100644
@@ -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: