Skip to content

Commit f9c0a39

Browse files
committed
binfmt_elf: Only report padzero() errors when PROT_WRITE
Errors with padzero() should be caught unless we're expecting a pathological (non-writable) segment. Report -EFAULT only when PROT_WRITE is present. Additionally add some more documentation to padzero(), elf_map(), and elf_load(). Cc: Eric Biederman <[email protected]> Cc: Alexander Viro <[email protected]> Cc: Christian Brauner <[email protected]> Cc: [email protected] Cc: [email protected] Suggested-by: Eric Biederman <[email protected]> Tested-by: Pedro Falcato <[email protected]> Signed-off-by: Sebastian Ott <[email protected]> Link: https://lore.kernel.org/r/[email protected] Signed-off-by: Kees Cook <[email protected]>
1 parent d5ca24f commit f9c0a39

File tree

1 file changed

+23
-9
lines changed

1 file changed

+23
-9
lines changed

fs/binfmt_elf.c

Lines changed: 23 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -110,19 +110,19 @@ static struct linux_binfmt elf_format = {
110110

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

113-
/* We need to explicitly zero any fractional pages
114-
after the data section (i.e. bss). This would
115-
contain the junk from the file that should not
116-
be in memory
113+
/*
114+
* We need to explicitly zero any trailing portion of the page that follows
115+
* p_filesz when it ends before the page ends (e.g. bss), otherwise this
116+
* memory will contain the junk from the file that should not be present.
117117
*/
118-
static int padzero(unsigned long elf_bss)
118+
static int padzero(unsigned long address)
119119
{
120120
unsigned long nbyte;
121121

122-
nbyte = ELF_PAGEOFFSET(elf_bss);
122+
nbyte = ELF_PAGEOFFSET(address);
123123
if (nbyte) {
124124
nbyte = ELF_MIN_ALIGN - nbyte;
125-
if (clear_user((void __user *) elf_bss, nbyte))
125+
if (clear_user((void __user *)address, nbyte))
126126
return -EFAULT;
127127
}
128128
return 0;
@@ -348,6 +348,11 @@ create_elf_tables(struct linux_binprm *bprm, const struct elfhdr *exec,
348348
return 0;
349349
}
350350

351+
/*
352+
* Map "eppnt->p_filesz" bytes from "filep" offset "eppnt->p_offset"
353+
* into memory at "addr". (Note that p_filesz is rounded up to the
354+
* next page, so any extra bytes from the file must be wiped.)
355+
*/
351356
static unsigned long elf_map(struct file *filep, unsigned long addr,
352357
const struct elf_phdr *eppnt, int prot, int type,
353358
unsigned long total_size)
@@ -387,6 +392,11 @@ static unsigned long elf_map(struct file *filep, unsigned long addr,
387392
return(map_addr);
388393
}
389394

395+
/*
396+
* Map "eppnt->p_filesz" bytes from "filep" offset "eppnt->p_offset"
397+
* into memory at "addr". Memory from "p_filesz" through "p_memsz"
398+
* rounded up to the next page is zeroed.
399+
*/
390400
static unsigned long elf_load(struct file *filep, unsigned long addr,
391401
const struct elf_phdr *eppnt, int prot, int type,
392402
unsigned long total_size)
@@ -404,8 +414,12 @@ static unsigned long elf_load(struct file *filep, unsigned long addr,
404414
zero_end = map_addr + ELF_PAGEOFFSET(eppnt->p_vaddr) +
405415
eppnt->p_memsz;
406416

407-
/* Zero the end of the last mapped page */
408-
padzero(zero_start);
417+
/*
418+
* Zero the end of the last mapped page but ignore
419+
* any errors if the segment isn't writable.
420+
*/
421+
if (padzero(zero_start) && (prot & PROT_WRITE))
422+
return -EFAULT;
409423
}
410424
} else {
411425
map_addr = zero_start = ELF_PAGESTART(addr);

0 commit comments

Comments
 (0)