Linux Modules
 help / color / mirror / Atom feed
* [PATCH] lib/bug: ignore non-allocated module bug tables
@ 2026-07-29 17:42 Laxman Acharya Padhya
  2026-07-29 17:58 ` sashiko-bot
  0 siblings, 1 reply; 2+ messages in thread
From: Laxman Acharya Padhya @ 2026-07-29 17:42 UTC (permalink / raw)
  To: Andrew Morton
  Cc: Luis Chamberlain, Petr Pavlu, Daniel Gomez, Sami Tolvanen,
	Aaron Tomlin, linux-modules, linux-kernel

rewrite_section_headers() initially sets sh_addr for every section to its
address in the temporary module image. Only SHF_ALLOC sections are later
copied to module memory and have sh_addr updated.

module_bug_finalize() accepts any section named __bug_table. A malformed
module can therefore leave mod->bug_table pointing into the temporary
image, which is freed after loading. A later BUG/WARN lookup would then
scan freed memory.

Require the bug table section to be allocated before retaining its
address. Valid modules are unchanged because __bug_table is allocated.

Fixes: 7664c5a1da47 ("[PATCH] Generic BUG implementation")
Signed-off-by: Laxman Acharya Padhya <acharyalaxman8848@gmail.com>
---
 lib/bug.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)

diff --git a/lib/bug.c b/lib/bug.c
index 7c1c2c27f..44966ead1 100644
--- a/lib/bug.c
+++ b/lib/bug.c
@@ -94,7 +94,8 @@ void module_bug_finalize(const Elf_Ehdr *hdr, const Elf_Shdr *sechdrs,
 	/* Find the __bug_table section, if present */
 	secstrings = (char *)hdr + sechdrs[hdr->e_shstrndx].sh_offset;
 	for (i = 1; i < hdr->e_shnum; i++) {
-		if (strcmp(secstrings+sechdrs[i].sh_name, "__bug_table"))
+		if (strcmp(secstrings + sechdrs[i].sh_name, "__bug_table") ||
+		    !(sechdrs[i].sh_flags & SHF_ALLOC))
 			continue;
 		mod->bug_table = (void *) sechdrs[i].sh_addr;
 		mod->num_bugs = sechdrs[i].sh_size / sizeof(struct bug_entry);
-- 
2.51.2


^ permalink raw reply related	[flat|nested] 2+ messages in thread

* Re: [PATCH] lib/bug: ignore non-allocated module bug tables
  2026-07-29 17:42 [PATCH] lib/bug: ignore non-allocated module bug tables Laxman Acharya Padhya
@ 2026-07-29 17:58 ` sashiko-bot
  0 siblings, 0 replies; 2+ messages in thread
From: sashiko-bot @ 2026-07-29 17:58 UTC (permalink / raw)
  To: Laxman Acharya Padhya; +Cc: linux-modules

Thank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider:

Pre-existing issues:
- [High] Systemic Use-After-Free (UAF) due to missing SHF_ALLOC validation across multiple subsystems.
- [High] Integer overflow in module layout calculation leading to out-of-bounds reads.
--

--- Patch [1]: [PATCH] lib/bug: ignore non-allocated module bug tables ---
commit 2f65def59be64b4d6ec8eb77329b4e7bc9acac8c
Author: Laxman Acharya Padhya <acharyalaxman8848@gmail.com>

lib/bug: ignore non-allocated module bug tables

This commit updates module_bug_finalize() to require the __bug_table section
to have the SHF_ALLOC flag set before retaining its address. This prevents a
malformed module from leaving the bug table pointing into the temporary
module image, which is freed after loading.

> diff --git a/lib/bug.c b/lib/bug.c
> index 7c1c2c27f58e7..44966ead1e5fa 100644
> --- a/lib/bug.c
> +++ b/lib/bug.c
> @@ -94,7 +94,8 @@ void module_bug_finalize(const Elf_Ehdr *hdr, const Elf_Shdr *sechdrs,
>  	/* Find the __bug_table section, if present */
>  	secstrings = (char *)hdr + sechdrs[hdr->e_shstrndx].sh_offset;
>  	for (i = 1; i < hdr->e_shnum; i++) {
> -		if (strcmp(secstrings+sechdrs[i].sh_name, "__bug_table"))
> +		if (strcmp(secstrings + sechdrs[i].sh_name, "__bug_table") ||
> +		    !(sechdrs[i].sh_flags & SHF_ALLOC))
>  			continue;

[Severity: High]
This is a pre-existing issue, but does this same vulnerability exist in other
subsystems that look up sections by name without checking SHF_ALLOC?

For example, in module_cfi_finalize() it assigns the section address to
mod->kcfi_traps without verifying the SHF_ALLOC flag:

kernel/cfi.c:module_cfi_finalize() {
    ...
    if (strcmp(secstrings + sechdrs[i].sh_name, "__kcfi_traps"))
        continue;
    mod->kcfi_traps = (s32 *)sechdrs[i].sh_addr;
    ...
}

Similarly, in unwind_module_init(), unvalidated ORC section addresses are
saved into the module structure:

arch/x86/kernel/unwind_orc.c:unwind_module_init() {
    ...
    mod->arch.orc_unwind_ip = orc_ip;
    mod->arch.orc_unwind = orc;
    ...
}

There is also module_init_ftrace_plt() which saves the unvalidated ftrace
trampolines address retrieved via find_section(), which ignores flags:

arch/arm64/include/asm/module.h:find_section() {
    ...
    if (strcmp(name, secstrs + s->sh_name) == 0)
        return s;
    ...
}

If a malformed module provides these sections without the SHF_ALLOC flag,
would their sh_addr similarly point to the temporary module image buffer
and cause a use-after-free when the buffer is freed?

>  		mod->bug_table = (void *) sechdrs[i].sh_addr;
>  		mod->num_bugs = sechdrs[i].sh_size / sizeof(struct bug_entry);

[Severity: High]
This isn't a bug introduced by this patch, but can an extremely large
section size cause an integer overflow leading to an out-of-bounds read
here?

If a malformed module provides a section marked with SHT_NOBITS and
SHF_ALLOC, elf_validity_cache_sechdrs() explicitly skips size validation:

kernel/module/main.c:elf_validity_cache_sechdrs() {
    ...
    case SHT_NOBITS:
        /* No contents, offset/size don't mean anything */
        continue;
    ...
}

Then, in module_get_offset_and_type(), adding a massive 64-bit sh_size to a
32-bit offset truncates the result:

kernel/module/main.c:module_get_offset_and_type() {
    ...
    mod->mem[type].size = offset + sechdr->sh_size;
    ...
}

This wrap-around results in a small allocated memory layout size. However,
since module_bug_finalize() uses the original un-truncated 64-bit sh_size
to calculate mod->num_bugs, would a subsequent kernel warning or bug
trigger iteration far beyond the actually allocated memory?

-- 
Sashiko AI review · https://sashiko.dev/#/patchset/20260729174234.86242-1-acharyalaxman8848@gmail.com?part=1

^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2026-07-29 17:58 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-07-29 17:42 [PATCH] lib/bug: ignore non-allocated module bug tables Laxman Acharya Padhya
2026-07-29 17:58 ` sashiko-bot

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox