Skip to content

Commit 585a018

Browse files
ebiedermkees
authored andcommitted
binfmt_elf: Support segments with 0 filesz and misaligned starts
Implement a helper elf_load() that wraps elf_map() and performs all of the necessary work to ensure that when "memsz > filesz" the bytes described by "memsz > filesz" are zeroed. An outstanding issue is if the first segment has filesz 0, and has a randomized location. But that is the same as today. In this change I replaced an open coded padzero() that did not clear all of the way to the end of the page, with padzero() that does. I also stopped checking the return of padzero() as there is at least one known case where testing for failure is the wrong thing to do. It looks like binfmt_elf_fdpic may have the proper set of tests for when error handling can be safely completed. I found a couple of commits in the old history https://git.kernel.org/pub/scm/linux/kernel/git/tglx/history.git, that look very interesting in understanding this code. commit 39b56d9 ("[PATCH] binfmt_elf: clearing bss may fail") commit c6e2227 ("[SPARC64]: Missing user access return value checks in fs/binfmt_elf.c and fs/compat.c") commit 5bf3be0 ("v2.4.10.1 -> v2.4.10.2") Looking at commit 39b56d9 ("[PATCH] binfmt_elf: clearing bss may fail"): > commit 39b56d9 > Author: Pavel Machek <pavel@ucw.cz> > Date: Wed Feb 9 22:40:30 2005 -0800 > > [PATCH] binfmt_elf: clearing bss may fail > > So we discover that Borland's Kylix application builder emits weird elf > files which describe a non-writeable bss segment. > > So remove the clear_user() check at the place where we zero out the bss. I > don't _think_ there are any security implications here (plus we've never > checked that clear_user() return value, so whoops if it is a problem). > > Signed-off-by: Pavel Machek <pavel@suse.cz> > Signed-off-by: Andrew Morton <akpm@osdl.org> > Signed-off-by: Linus Torvalds <torvalds@osdl.org> It seems pretty clear that binfmt_elf_fdpic with skipping clear_user() for non-writable segments and otherwise calling clear_user(), aka padzero(), and checking it's return code is the right thing to do. I just skipped the error checking as that avoids breaking things. And notably, it looks like Borland's Kylix died in 2005 so it might be safe to just consider read-only segments with memsz > filesz an error. Reported-by: Sebastian Ott <sebott@redhat.com> Reported-by: Thomas Weißschuh <linux@weissschuh.net> Closes: https://lkml.kernel.org/r/20230914-bss-alloc-v1-1-78de67d2c6dd@weissschuh.net Signed-off-by: "Eric W. Biederman" <ebiederm@xmission.com> Link: https://lore.kernel.org/r/87sf71f123.fsf@email.froward.int.ebiederm.org Tested-by: Pedro Falcato <pedro.falcato@gmail.com> Signed-off-by: Sebastian Ott <sebott@redhat.com> Link: https://lore.kernel.org/r/20230929032435.2391507-1-keescook@chromium.org Signed-off-by: Kees Cook <keescook@chromium.org>
1 parent ff7a654 commit 585a018

File tree

1 file changed

+48
-63
lines changed

1 file changed

+48
-63
lines changed

fs/binfmt_elf.c

Lines changed: 48 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -110,25 +110,6 @@ static struct linux_binfmt elf_format = {
110110

111111
#define BAD_ADDR(x) (unlikely((unsigned long)(x) >= TASK_SIZE))
112112

113-
static int set_brk(unsigned long start, unsigned long end, int prot)
114-
{
115-
start = ELF_PAGEALIGN(start);
116-
end = ELF_PAGEALIGN(end);
117-
if (end > start) {
118-
/*
119-
* Map the last of the bss segment.
120-
* If the header is requesting these pages to be
121-
* executable, honour that (ppc32 needs this).
122-
*/
123-
int error = vm_brk_flags(start, end - start,
124-
prot & PROT_EXEC ? VM_EXEC : 0);
125-
if (error)
126-
return error;
127-
}
128-
current->mm->start_brk = current->mm->brk = end;
129-
return 0;
130-
}
131-
132113
/* We need to explicitly zero any fractional pages
133114
after the data section (i.e. bss). This would
134115
contain the junk from the file that should not
@@ -406,6 +387,51 @@ static unsigned long elf_map(struct file *filep, unsigned long addr,
406387
return(map_addr);
407388
}
408389

390+
static unsigned long elf_load(struct file *filep, unsigned long addr,
391+
const struct elf_phdr *eppnt, int prot, int type,
392+
unsigned long total_size)
393+
{
394+
unsigned long zero_start, zero_end;
395+
unsigned long map_addr;
396+
397+
if (eppnt->p_filesz) {
398+
map_addr = elf_map(filep, addr, eppnt, prot, type, total_size);
399+
if (BAD_ADDR(map_addr))
400+
return map_addr;
401+
if (eppnt->p_memsz > eppnt->p_filesz) {
402+
zero_start = map_addr + ELF_PAGEOFFSET(eppnt->p_vaddr) +
403+
eppnt->p_filesz;
404+
zero_end = map_addr + ELF_PAGEOFFSET(eppnt->p_vaddr) +
405+
eppnt->p_memsz;
406+
407+
/* Zero the end of the last mapped page */
408+
padzero(zero_start);
409+
}
410+
} else {
411+
map_addr = zero_start = ELF_PAGESTART(addr);
412+
zero_end = zero_start + ELF_PAGEOFFSET(eppnt->p_vaddr) +
413+
eppnt->p_memsz;
414+
}
415+
if (eppnt->p_memsz > eppnt->p_filesz) {
416+
/*
417+
* Map the last of the segment.
418+
* If the header is requesting these pages to be
419+
* executable, honour that (ppc32 needs this).
420+
*/
421+
int error;
422+
423+
zero_start = ELF_PAGEALIGN(zero_start);
424+
zero_end = ELF_PAGEALIGN(zero_end);
425+
426+
error = vm_brk_flags(zero_start, zero_end - zero_start,
427+
prot & PROT_EXEC ? VM_EXEC : 0);
428+
if (error)
429+
map_addr = error;
430+
}
431+
return map_addr;
432+
}
433+
434+
409435
static unsigned long total_mapping_size(const struct elf_phdr *phdr, int nr)
410436
{
411437
elf_addr_t min_addr = -1;
@@ -829,7 +855,6 @@ static int load_elf_binary(struct linux_binprm *bprm)
829855
struct elf_phdr *elf_ppnt, *elf_phdata, *interp_elf_phdata = NULL;
830856
struct elf_phdr *elf_property_phdata = NULL;
831857
unsigned long elf_bss, elf_brk;
832-
int bss_prot = 0;
833858
int retval, i;
834859
unsigned long elf_entry;
835860
unsigned long e_entry;
@@ -1040,33 +1065,6 @@ static int load_elf_binary(struct linux_binprm *bprm)
10401065
if (elf_ppnt->p_type != PT_LOAD)
10411066
continue;
10421067

1043-
if (unlikely (elf_brk > elf_bss)) {
1044-
unsigned long nbyte;
1045-
1046-
/* There was a PT_LOAD segment with p_memsz > p_filesz
1047-
before this one. Map anonymous pages, if needed,
1048-
and clear the area. */
1049-
retval = set_brk(elf_bss + load_bias,
1050-
elf_brk + load_bias,
1051-
bss_prot);
1052-
if (retval)
1053-
goto out_free_dentry;
1054-
nbyte = ELF_PAGEOFFSET(elf_bss);
1055-
if (nbyte) {
1056-
nbyte = ELF_MIN_ALIGN - nbyte;
1057-
if (nbyte > elf_brk - elf_bss)
1058-
nbyte = elf_brk - elf_bss;
1059-
if (clear_user((void __user *)elf_bss +
1060-
load_bias, nbyte)) {
1061-
/*
1062-
* This bss-zeroing can fail if the ELF
1063-
* file specifies odd protections. So
1064-
* we don't check the return value
1065-
*/
1066-
}
1067-
}
1068-
}
1069-
10701068
elf_prot = make_prot(elf_ppnt->p_flags, &arch_state,
10711069
!!interpreter, false);
10721070

@@ -1162,7 +1160,7 @@ static int load_elf_binary(struct linux_binprm *bprm)
11621160
}
11631161
}
11641162

1165-
error = elf_map(bprm->file, load_bias + vaddr, elf_ppnt,
1163+
error = elf_load(bprm->file, load_bias + vaddr, elf_ppnt,
11661164
elf_prot, elf_flags, total_size);
11671165
if (BAD_ADDR(error)) {
11681166
retval = IS_ERR_VALUE(error) ?
@@ -1217,10 +1215,8 @@ static int load_elf_binary(struct linux_binprm *bprm)
12171215
if (end_data < k)
12181216
end_data = k;
12191217
k = elf_ppnt->p_vaddr + elf_ppnt->p_memsz;
1220-
if (k > elf_brk) {
1221-
bss_prot = elf_prot;
1218+
if (k > elf_brk)
12221219
elf_brk = k;
1223-
}
12241220
}
12251221

12261222
e_entry = elf_ex->e_entry + load_bias;
@@ -1232,18 +1228,7 @@ static int load_elf_binary(struct linux_binprm *bprm)
12321228
start_data += load_bias;
12331229
end_data += load_bias;
12341230

1235-
/* Calling set_brk effectively mmaps the pages that we need
1236-
* for the bss and break sections. We must do this before
1237-
* mapping in the interpreter, to make sure it doesn't wind
1238-
* up getting placed where the bss needs to go.
1239-
*/
1240-
retval = set_brk(elf_bss, elf_brk, bss_prot);
1241-
if (retval)
1242-
goto out_free_dentry;
1243-
if (likely(elf_bss != elf_brk) && unlikely(padzero(elf_bss))) {
1244-
retval = -EFAULT; /* Nobody gets to see this, but.. */
1245-
goto out_free_dentry;
1246-
}
1231+
current->mm->start_brk = current->mm->brk = ELF_PAGEALIGN(elf_brk);
12471232

12481233
if (interpreter) {
12491234
elf_entry = load_elf_interp(interp_elf_ex,

0 commit comments

Comments
 (0)