Skip to contentSkip to conversation

module: reject out-of-range relocation target indices

PATCH NAK Karl Mehltretter 5 participants 5 messages Sep 8 – Sep 10

Subsystems (3)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 module: reject out-of-range relocation target indices

apply_relocations() skips relocation sections whose sh_info target index is outside the section table. ARM, ARM64, LoongArch, PA-RISC and RISC-V use sh_info earlier in module_frob_arch_sections(), before this check.

ARM, ARM64, LoongArch and RISC-V use the unchecked index to read sh_flags outside the section header table. PA-RISC uses it to index an e_shnum-sized heap array for a read and an update. QEMU reproduced page-fault Oopses on ARM, ARM64, LoongArch and RISC-V, and a Data TLB miss on the PA-RISC array read.

Validate sh_info for SHT_REL and SHT_RELA sections in elf_validity_cache_sechdrs(). Reject the module with ENOEXEC before architecture code can use the index.

Fixes c298be74492b ("parisc: fix module loading failure of large kernel modules")
Fixes 7d485f647c1f ("ARM: 8220/1: allow modules outside of bl range")
Fixes fd045f6cd98e ("arm64: add support for module PLTs")
Fixes ab1ef68e5401 ("RISC-V: Add sections of PLT and GOT for kernel module")
Fixes fcdfe9d22bed ("LoongArch: Add ELF and module support")
Assisted-by LLM

---

A custom harness for upstream Frama-C 33.0 (Arsenic) Eva found the ARM32 instance in a source-identical ARM module_frob_arch_sections() slice. Eva reported the out-of-range section-table pointer and sh_flags access.

The analysis and ARM32 A/B test ran at Linux b9b3e33b70b7 ("Merge tag 'trace-v7.2-rc6' of
git://git.kernel.org/pub/scm/linux/kernel/git/trace/linux-trace"). The PA-RISC, ARM64, RISC-V, LoongArch and x86_64 A/B tests ran at the declared base commit, 28924df2a08f. arch/arm/kernel/module-plts.c and the touched loop in kernel/module/main.c are identical between the two commits.

Each A/B test changed only a relocation section's sh_info to 0x10000000. The same configurations and modules were used before and after the change. All controls loaded before and after the change. The fixed kernels rejected the malformed modules with ENOEXEC.

Original-kernel results with QEMU 10.2.1 TCG:

- ARM32, virt/Cortex-A15, GCC 15.2.0, multi_v7_defconfig plus
VMSPLIT_2G: page fault at module_frob_arch_sections()+0x160.
- ARM64, virt/Cortex-A57, GCC 15.2.0, defconfig: page fault at module_frob_arch_sections()+0x110.
- PA-RISC, B160L, hppa-linux-gcc 8.1.0, binutils 2.30,
generic-32bit_defconfig: Data TLB miss at module_frob_arch_sections()+0x11c on the stub_entries read for a counted R_PARISC_PCREL17F relocation.
- RISC-V, virt, GCC 15.2.0, defconfig plus RELOCATABLE with MODULE_SECTIONS enabled: page fault at module_frob_arch_sections()+0xe4.
- LoongArch, virt/LA464, LLVM 21.1.8, loongson64_defconfig: page fault at module_frob_arch_sections()+0x1b8.

On x86_64, which has no vulnerable early sh_info access, the original kernel loaded both modules. The fixed kernel loaded the control and rejected the malformed module with ENOEXEC. The test used pc/qemu64, x86_64_defconfig and GCC 15.2.0.

---
 kernel/module/main.c | 7 +++++++
 1 file changed, 7 insertions(+)
kernel/module/main.c +7 -0
index d0e1e0bd2ad0..30c7a05488bc 100644
@@ -1933,6 +1933,7 @@ static int elf_validity_ehdr(const struct load_info *info)
  * * Section array fits in the user provided data
  * * Section index 0 is NULL
  * * Section contents are inbounds
+ * * Relocation section target indices are inbounds
  *
  * Then updates @info with a &load_info->sechdrs pointer if valid.
  *
@@ -1983,6 +1984,12 @@ static int elf_validity_cache_sechdrs(struct load_info *info)
 	/* Validate contents are inbounds */
 	for (i = 1; i < info->hdr->e_shnum; i++) {
 		shdr = &sechdrs[i];
+		if ((shdr->sh_type == SHT_REL || shdr->sh_type == SHT_RELA) &&
+		    shdr->sh_info >= info->hdr->e_shnum) {
+			pr_err("Invalid ELF relocation section target index %u\n",
+			       shdr->sh_info);
+			return -ENOEXEC;
+		}
 		switch (shdr->sh_type) {
 		case SHT_NULL:
 		case SHT_NOBITS:

base-commit: 28924df2a08f440c73991b83028032c901de2ae4

Signature · 1 line
2.53.0
#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] A malformed ELF file with e_shnum == 0 can bypass the section header array bounds check and cause an out-of-bounds read when unconditionally accessing sechdrs[0]. --

commit eb153e5069bc4f227bac013d4c0cba2f178ed824
Author: Karl Mehltretter <[email protected]>

module: reject out-of-range relocation target indices

apply_relocations() skips relocation sections whose sh_info target index is outside the section table. ARM, ARM64, LoongArch, PA-RISC and RISC-V use sh_info earlier in module_frob_arch_sections(), before this check.

Quoted 4 lines
kernel/module/main.c +0 -0
index d0e1e0bd2ad06..30c7a05488bc3 100644

[ ... ]

kernel/module/main.c static int elf_validity_cache_sechdrs(struct load_info *info) · line 1986

Quoted 4 lines
kernel/module/main.c +0 -0
@@ -1983,6 +1984,12 @@ static int elf_validity_cache_sechdrs(struct load_info *info)
 	/* Validate contents are inbounds */
 	for (i = 1; i < info->hdr->e_shnum; i++) {
 		shdr = &sechdrs[i];

[Severity: High]
This is a pre-existing issue, but can a malformed ELF file with e_shnum == 0 bypass the section header array bounds check and cause an out-of-bounds read?

Earlier in elf_validity_cache_sechdrs() in kernel/module/main.c, the bounds check multiplies by e_shnum:

	if (info->hdr->e_shoff >= info->len
	    || (info->hdr->e_shnum * sizeof(Elf_Shdr) >
		info->len - info->hdr->e_shoff)) {

If e_shnum is 0, this evaluates to 0 and passes, failing to ensure space for even the mandatory sechdrs[0].

Then the code computes sechdrs and unconditionally accesses sechdrs[0]:

	sechdrs = (void *)info->hdr + info->hdr->e_shoff;

	if (sechdrs[0].sh_type != SHT_NULL
	    || sechdrs[0].sh_size != 0
	    || sechdrs[0].sh_addr != 0) {

If e_shoff is arbitrarily set near the end of info->len, does this unconditional access to sechdrs[0] cross the vmalloc allocation boundary?

This could hit an unmapped guard page and cause a fatal page fault and kernel Oops.

Could this sequence be triggered by a privileged user with CAP_SYS_MODULE calling init_module() with an ELF file where info->len is exactly PAGE_SIZE, e_shnum is 0, and e_shoff is PAGE_SIZE - 1?

Quoted 7 lines
kernel/module/main.c +6 -0
+		if ((shdr->sh_type == SHT_REL || shdr->sh_type == SHT_RELA) &&
+		    shdr->sh_info >= info->hdr->e_shnum) {
+			pr_err("Invalid ELF relocation section target index %u\n",
+			       shdr->sh_info);
+			return -ENOEXEC;
+		}
 		switch (shdr->sh_type) {
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1
#2 lore raw
In reply to Karl Mehltretter
Quoted 58 lines · Karl Mehltretter

apply_relocations() skips relocation sections whose sh_info target index is outside the section table. ARM, ARM64, LoongArch, PA-RISC and RISC-V use sh_info earlier in module_frob_arch_sections(), before this check.

ARM, ARM64, LoongArch and RISC-V use the unchecked index to read sh_flags outside the section header table. PA-RISC uses it to index an e_shnum-sized heap array for a read and an update. QEMU reproduced page-fault Oopses on ARM, ARM64, LoongArch and RISC-V, and a Data TLB miss on the PA-RISC array read.

Validate sh_info for SHT_REL and SHT_RELA sections in elf_validity_cache_sechdrs(). Reject the module with ENOEXEC before architecture code can use the index.

Fixes c298be74492b ("parisc: fix module loading failure of large kernel modules")
Fixes 7d485f647c1f ("ARM: 8220/1: allow modules outside of bl range")
Fixes fd045f6cd98e ("arm64: add support for module PLTs")
Fixes ab1ef68e5401 ("RISC-V: Add sections of PLT and GOT for kernel module")
Fixes fcdfe9d22bed ("LoongArch: Add ELF and module support")
Assisted-by LLM

---

A custom harness for upstream Frama-C 33.0 (Arsenic) Eva found the ARM32 instance in a source-identical ARM module_frob_arch_sections() slice. Eva reported the out-of-range section-table pointer and sh_flags access.

The analysis and ARM32 A/B test ran at Linux b9b3e33b70b7 ("Merge tag 'trace-v7.2-rc6' of
git://git.kernel.org/pub/scm/linux/kernel/git/trace/linux-trace"). The PA-RISC, ARM64, RISC-V, LoongArch and x86_64 A/B tests ran at the declared base commit, 28924df2a08f. arch/arm/kernel/module-plts.c and the touched loop in kernel/module/main.c are identical between the two commits.

Each A/B test changed only a relocation section's sh_info to 0x10000000. The same configurations and modules were used before and after the change. All controls loaded before and after the change. The fixed kernels rejected the malformed modules with ENOEXEC.

Original-kernel results with QEMU 10.2.1 TCG:

- ARM32, virt/Cortex-A15, GCC 15.2.0, multi_v7_defconfig plus
VMSPLIT_2G: page fault at module_frob_arch_sections()+0x160.
- ARM64, virt/Cortex-A57, GCC 15.2.0, defconfig: page fault at module_frob_arch_sections()+0x110.
- PA-RISC, B160L, hppa-linux-gcc 8.1.0, binutils 2.30,
generic-32bit_defconfig: Data TLB miss at module_frob_arch_sections()+0x11c on the stub_entries read for a counted R_PARISC_PCREL17F relocation.
- RISC-V, virt, GCC 15.2.0, defconfig plus RELOCATABLE with MODULE_SECTIONS enabled: page fault at module_frob_arch_sections()+0xe4.
- LoongArch, virt/LA464, LLVM 21.1.8, loongson64_defconfig: page fault at module_frob_arch_sections()+0x1b8.

Quoted 4 lines

On x86_64, which has no vulnerable early sh_info access, the original kernel loaded both modules. The fixed kernel loaded the control and rejected the malformed module with ENOEXEC. The test used pc/qemu64, x86_64_defconfig and GCC 15.2.0.

Interesting.
So, this patch helps to prevent loading modules with buggy entries, even if the module was e.g. loaded on x86-64 before.

For me the patch is OK, but it only adds one random sanitizing check, and where would we stop to check?

Quoted 17 lines
---
  kernel/module/main.c | 7 +++++++
  1 file changed, 7 insertions(+)
kernel/module/main.c +1 -0
index d0e1e0bd2ad0..30c7a05488bc 100644
@@ -1933,6 +1933,7 @@ static int elf_validity_ehdr(const struct load_info *info)
   * * Section array fits in the user provided data
   * * Section index 0 is NULL
   * * Section contents are inbounds
+ * * Relocation section target indices are inbounds
   *
   * Then updates @info with a &load_info->sechdrs pointer if valid.
   *

kernel/module/main.c static int elf_validity_cache_sechdrs(struct load_info *info) · line 1990

Quoted 7 lines
kernel/module/main.c +4 -0
@@ -1983,6 +1984,12 @@ static int elf_validity_cache_sechdrs(struct load_info *info)
  	/* Validate contents are inbounds */
  	for (i = 1; i < info->hdr->e_shnum; i++) {
  		shdr = &sechdrs[i];
+		if ((shdr->sh_type == SHT_REL || shdr->sh_type == SHT_RELA) &&
+		    shdr->sh_info >= info->hdr->e_shnum) {
+			pr_err("Invalid ELF relocation section target index %u\n",
+			       shdr->sh_info);

Suggestion:
Print the module name too, otherwise it's hard to find out later which module is broken.

Helge

Quoted 7 lines
kernel/module/main.c +2 -0
+			return -ENOEXEC;
+		}
  		switch (shdr->sh_type) {
  		case SHT_NULL:
  		case SHT_NOBITS:

base-commit: 28924df2a08f440c73991b83028032c901de2ae4

maintainer
#3 lore raw
Quoted 64 lines · Helge Deller
Quoted 58 lines · Karl Mehltretter

apply_relocations() skips relocation sections whose sh_info target index is outside the section table. ARM, ARM64, LoongArch, PA-RISC and RISC-V use sh_info earlier in module_frob_arch_sections(), before this check.

ARM, ARM64, LoongArch and RISC-V use the unchecked index to read sh_flags outside the section header table. PA-RISC uses it to index an e_shnum-sized heap array for a read and an update. QEMU reproduced page-fault Oopses on ARM, ARM64, LoongArch and RISC-V, and a Data TLB miss on the PA-RISC array read.

Validate sh_info for SHT_REL and SHT_RELA sections in elf_validity_cache_sechdrs(). Reject the module with ENOEXEC before architecture code can use the index.

Fixes c298be74492b ("parisc: fix module loading failure of large kernel modules")
Fixes 7d485f647c1f ("ARM: 8220/1: allow modules outside of bl range")
Fixes fd045f6cd98e ("arm64: add support for module PLTs")
Fixes ab1ef68e5401 ("RISC-V: Add sections of PLT and GOT for kernel module")
Fixes fcdfe9d22bed ("LoongArch: Add ELF and module support")
Assisted-by LLM

---

A custom harness for upstream Frama-C 33.0 (Arsenic) Eva found the ARM32 instance in a source-identical ARM module_frob_arch_sections() slice. Eva reported the out-of-range section-table pointer and sh_flags access.

The analysis and ARM32 A/B test ran at Linux b9b3e33b70b7 ("Merge tag 'trace-v7.2-rc6' of
git://git.kernel.org/pub/scm/linux/kernel/git/trace/linux-trace"). The PA-RISC, ARM64, RISC-V, LoongArch and x86_64 A/B tests ran at the declared base commit, 28924df2a08f. arch/arm/kernel/module-plts.c and the touched loop in kernel/module/main.c are identical between the two commits.

Each A/B test changed only a relocation section's sh_info to 0x10000000. The same configurations and modules were used before and after the change. All controls loaded before and after the change. The fixed kernels rejected the malformed modules with ENOEXEC.

Original-kernel results with QEMU 10.2.1 TCG:

- ARM32, virt/Cortex-A15, GCC 15.2.0, multi_v7_defconfig plus
VMSPLIT_2G: page fault at module_frob_arch_sections()+0x160.
- ARM64, virt/Cortex-A57, GCC 15.2.0, defconfig: page fault at module_frob_arch_sections()+0x110.
- PA-RISC, B160L, hppa-linux-gcc 8.1.0, binutils 2.30,
generic-32bit_defconfig: Data TLB miss at module_frob_arch_sections()+0x11c on the stub_entries read for a counted R_PARISC_PCREL17F relocation.
- RISC-V, virt, GCC 15.2.0, defconfig plus RELOCATABLE with MODULE_SECTIONS enabled: page fault at module_frob_arch_sections()+0xe4.
- LoongArch, virt/LA464, LLVM 21.1.8, loongson64_defconfig: page fault at module_frob_arch_sections()+0x1b8.

Quoted 4 lines

On x86_64, which has no vulnerable early sh_info access, the original kernel loaded both modules. The fixed kernel loaded the control and rejected the malformed module with ENOEXEC. The test used pc/qemu64, x86_64_defconfig and GCC 15.2.0.

Quoted 6 lines

Interesting.
So, this patch helps to prevent loading modules with buggy entries, even if the module was e.g. loaded on x86-64 before.

For me the patch is OK, but it only adds one random sanitizing check, and where would we stop to check?

Loading a module should normally at least get through the signature and blacklist checks without crashing due to a corrupted module ELF file. After that point, I believe the ELF data should be trusted, similar to how the actual module code is expected to be correct.

module_frob_arch_sections() is called later in the module-loading process, after the signature and blacklist checks, so I think this patch is not strictly necessary.

Signature · 2 lines
Thanks,
Petr
#4 lore raw
In reply to Karl Mehltretter
Quoted 14 lines · Karl Mehltretter

apply_relocations() skips relocation sections whose sh_info target index is outside the section table. ARM, ARM64, LoongArch, PA-RISC and RISC-V use sh_info earlier in module_frob_arch_sections(), before this check.

ARM, ARM64, LoongArch and RISC-V use the unchecked index to read sh_flags outside the section header table. PA-RISC uses it to index an e_shnum-sized heap array for a read and an update. QEMU reproduced page-fault Oopses on ARM, ARM64, LoongArch and RISC-V, and a Data TLB miss on the PA-RISC array read.

Validate sh_info for SHT_REL and SHT_RELA sections in elf_validity_cache_sechdrs(). Reject the module with ENOEXEC before architecture code can use the index.

Quoted 8 lines
Fixes c298be74492b ("parisc: fix module loading failure of large kernel modules")
Fixes 7d485f647c1f ("ARM: 8220/1: allow modules outside of bl range")
Fixes fd045f6cd98e ("arm64: add support for module PLTs")
Fixes ab1ef68e5401 ("RISC-V: Add sections of PLT and GOT for kernel module")
Fixes fcdfe9d22bed ("LoongArch: Add ELF and module support")
Assisted-by LLM

I don't think so, sorry, see below.

Quoted 59 lines

---

A custom harness for upstream Frama-C 33.0 (Arsenic) Eva found the ARM32 instance in a source-identical ARM module_frob_arch_sections() slice. Eva reported the out-of-range section-table pointer and sh_flags access.

The analysis and ARM32 A/B test ran at Linux b9b3e33b70b7 ("Merge tag 'trace-v7.2-rc6' of
git://git.kernel.org/pub/scm/linux/kernel/git/trace/linux-trace"). The PA-RISC, ARM64, RISC-V, LoongArch and x86_64 A/B tests ran at the declared base commit, 28924df2a08f. arch/arm/kernel/module-plts.c and the touched loop in kernel/module/main.c are identical between the two commits.

Each A/B test changed only a relocation section's sh_info to 0x10000000. The same configurations and modules were used before and after the change. All controls loaded before and after the change. The fixed kernels rejected the malformed modules with ENOEXEC.

Original-kernel results with QEMU 10.2.1 TCG:

- ARM32, virt/Cortex-A15, GCC 15.2.0, multi_v7_defconfig plus
VMSPLIT_2G: page fault at module_frob_arch_sections()+0x160.
- ARM64, virt/Cortex-A57, GCC 15.2.0, defconfig: page fault at module_frob_arch_sections()+0x110.
- PA-RISC, B160L, hppa-linux-gcc 8.1.0, binutils 2.30,
generic-32bit_defconfig: Data TLB miss at module_frob_arch_sections()+0x11c on the stub_entries read for a counted R_PARISC_PCREL17F relocation.
- RISC-V, virt, GCC 15.2.0, defconfig plus RELOCATABLE with MODULE_SECTIONS enabled: page fault at module_frob_arch_sections()+0xe4.
- LoongArch, virt/LA464, LLVM 21.1.8, loongson64_defconfig: page fault at module_frob_arch_sections()+0x1b8.

On x86_64, which has no vulnerable early sh_info access, the original kernel loaded both modules. The fixed kernel loaded the control and rejected the malformed module with ENOEXEC. The test used pc/qemu64, x86_64_defconfig and GCC 15.2.0.
---
kernel/module/main.c | 7 +++++++

1 file changed, 7 insertions(+)
kernel/module/main.c +1 -0
index d0e1e0bd2ad0..30c7a05488bc 100644
@@ -1933,6 +1933,7 @@ static int elf_validity_ehdr(const struct load_info *info)
 * * Section array fits in the user provided data
 * * Section index 0 is NULL
 * * Section contents are inbounds
+ * * Relocation section target indices are inbounds
 *
 * Then updates @info with a &load_info->sechdrs pointer if valid.
 *
@@ -1983,6 +1984,12 @@ static int elf_validity_cache_sechdrs(struct load_info *info)
 	/* Validate contents are inbounds */
 	for (i = 1; i < info->hdr->e_shnum; i++) {

kernel/module/main.c static int elf_validity_cache_sechdrs(struct load_info *info) · line 1995

Quoted 10 lines
kernel/module/main.c +6 -0
 		shdr = &sechdrs[i];
+		if ((shdr->sh_type == SHT_REL || shdr->sh_type == SHT_RELA) &&
+		    shdr->sh_info >= info->hdr->e_shnum) {
+			pr_err("Invalid ELF relocation section target index %u\n",
+			       shdr->sh_info);
+			return -ENOEXEC;
+		}
 		switch (shdr->sh_type) {
 		case SHT_NULL:
 		case SHT_NOBITS:

If the module passes the signature check and the blacklist check, the kernel would assume that the module is fine and ^W^W^W^W won't blow the whole
world up^W^W^W^W^W

Since module_frob_arch_sections() loads AFTER said checks pass, adding checks is just useless bloat imho.

NAK, sorry

Quoted 3 lines

base-commit: 28924df2a08f440c73991b83028032c901de2ae4

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