Skip to contentSkip to conversation

binfmt_elf: Allow .bss in any interp PT_LOAD

PATCH Superseded Kees Cook 1 participant 1 message Nov 11, 2022

Subsystems (2)

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore

Kees Cook

author
#0 lore raw

PATCH 1/1 binfmt_elf: Allow .bss in any interp PT_LOAD

Traditionally, only the final PT_LOAD for load_elf_interp() supported having p_memsz > p_filesz. Recently, lld's construction of musl's libc.so on PowerPC64 started having two PT_LOAD program headers with p_memsz > p_filesz.

As the least invasive change possible, check for p_memsz > p_filesz for each PT_LOAD in load_elf_interp.

---
Rich, Pedro, and Fangrui, are you able to test this change? I haven't constructed a trivial reproducer yet (though it would be nice to have a self-contained test-case).

---
 fs/binfmt_elf.c | 19 ++++++++++---------
 1 file changed, 10 insertions(+), 9 deletions(-)
fs/binfmt_elf.c +10 -9
index 528e2ac8931f..3f07945ff085 100644
@@ -673,18 +673,19 @@ static unsigned long load_elf_interp(struct elfhdr *interp_elf_ex,
 				last_bss = k;
 				bss_prot = elf_prot;
 			}
+
+			/*
+			 * Now fill out any zeroed region (e.g. .bss): first pad the
+			 * last page from the file up to the page boundary, and zero
+			 * it from elf_bss up to the end of the page.
+			 */
+			if (last_bss > elf_bss && padzero(elf_bss)) {
+				error = -EFAULT;
+				goto out;
+			}
 		}
 	}
 
-	/*
-	 * Now fill out the bss section: first pad the last page from
-	 * the file up to the page boundary, and zero it from elf_bss
-	 * up to the end of the page.
-	 */
-	if (padzero(elf_bss)) {
-		error = -EFAULT;
-		goto out;
-	}
 	/*
 	 * Next, align both the file and mem bss up to the page size,
 	 * since this is where elf_bss was just zeroed up to, and where
Signature · 1 line
2.34.1