FDPIC: Respect PT_GNU_STACK exec protection markings when creating NOMMU stack
Subsystems (5)
PATCH 1/4 FDPIC: Respect PT_GNU_STACK exec protection markings when creating NOMMU stack
From: Mike Frysinger <[email protected]>
The current code will load the stack size and protection markings, but then only use the markings in the MMU code path. The NOMMU code path always passes PROT_EXEC to the mmap() call. While this doesn't matter to most people whilst the code is running, it will cause a pointless icache flush when starting every FDPIC application. Typically this icache flush will be of a region on the order of 128KB in size, or may be the entire icache, depending on the facilities available on the CPU.
In the case where the arch default behaviour seems to be desired (EXSTACK_DEFAULT), we probe VM_STACK_FLAGS for VM_EXEC to determine whether we should be setting PROT_EXEC or not.
For arches that support an MPU (Memory Protection Unit - an MMU without the virtual mapping capability), setting PROT_EXEC or not will make an important difference.
It should be noted that this change also affects the executability of the brk region, since ELF-FDPIC has that share with the stack. However, this is probably irrelevant as NOMMU programs aren't likely to use the brk region, preferring instead allocation via mmap().
---
arch/blackfin/include/asm/page.h | 5 +++++ arch/frv/include/asm/page.h | 2 -- fs/binfmt_elf_fdpic.c | 13 +++++++++++-- 3 files changed, 16 insertions(+), 4 deletions(-)
arch/blackfin/include/asm/page.h +5 -0
@@ -10,4 +10,9 @@#include <asm-generic/page.h> #define MAP_NR (addr) (((unsigned long)(addr)-PAGE_OFFSET) >> PAGE_SHIFT) +#define VM_DATA_DEFAULT_FLAGS \ + (VM_READ | VM_WRITE | \ + ((current->personality & READ_IMPLIES_EXEC) ? VM_EXEC : 0 ) | \ + VM_MAYREAD | VM_MAYWRITE | VM_MAYEXEC) +#endif
arch/frv/include/asm/page.h +0 -2
@@ -63,12 +63,10 @@ extern unsigned long max_pfn;#define virt_addr_valid (kaddr) pfn_valid(__pa(kaddr) >> PAGE_SHIFT) -#ifdef CONFIG_MMU #define VM_DATA_DEFAULT_FLAGS \ (VM_READ | VM_WRITE | \ ((current->personality & READ_IMPLIES_EXEC) ? VM_EXEC : 0 ) | \ VM_MAYREAD | VM_MAYWRITE | VM_MAYEXEC) -#endif #endif /* __ASSEMBLY__ */
fs/binfmt_elf_fdpic.c +11 -2
@@ -171,6 +171,9 @@ static int load_elf_fdpic_binary(struct linux_binprm *bprm,#ifdef ELF_FDPIC_PLAT_INIT unsigned long dynaddr;#endif +#ifndef CONFIG_MMU +unsigned long stack_prot; +#endif struct file *interpreter =NULL ;/* to shut gcc up */ char *interpreter_name =NULL ;int executable_stack;
@@ -316,6 +319,8 @@ static int load_elf_fdpic_binary(struct linux_binprm *bprm,* defunct, deceased, etc. after this point we have to exit via * error_kill */ set_personality (PER_LINUX_FDPIC ); +if (elf_read_implies_exec (&exec_params.hdr, executable_stack)) + current->personality |=READ_IMPLIES_EXEC ;set_binfmt (&elf_fdpic_format); current->mm->start_code =0 ;
@@ -377,9 +382,13 @@ static int load_elf_fdpic_binary(struct linux_binprm *bprm,if (stack_size <PAGE_SIZE *2 ) stack_size =PAGE_SIZE *2 ; + stack_prot =PROT_READ |PROT_WRITE ; +if (executable_stack ==EXSTACK_ENABLE_X || + (executable_stack ==EXSTACK_DEFAULT &&VM_STACK_FLAGS &VM_EXEC )) + stack_prot |=PROT_EXEC ; +down_write (¤t->mm->mmap_sem); - current->mm->start_brk =do_mmap (NULL ,0 , stack_size, -PROT_READ |PROT_WRITE |PROT_EXEC , + current->mm->start_brk =do_mmap (NULL ,0 , stack_size, stack_prot,MAP_PRIVATE |MAP_ANONYMOUS |MAP_UNINITIALIZED |MAP_GROWSDOWN ,0 );
PATCH 2/4 NOMMU: Avoiding duplicate icache flushes of shared maps
From: Mike Frysinger <[email protected]>
When working with FDPIC, there are many shared mappings of read-only code regions between applications (the C library, applet packages like busybox, etc.), but the current do_mmap_pgoff() function will issue an icache flush whenever a VMA is added to an MM instead of only doing it when the map is initially created.
The flush can instead be done when a region is first mmapped PROT_EXEC. Note that we may not rely on the first mapping of a region being executable - it's possible for it to be PROT_READ only, so we have to remember whether we've flushed the region or not, and then flush the entire region when a bit of it is made executable.
However, this also affects the brk area. That will no longer be executable. We can mprotect() it to PROT_EXEC on MPU-mode kernels, but for NOMMU mode kernels, when it increases the brk allocation, making sys_brk() flush the extra from the icache should suffice. The brk area probably isn't used by NOMMU programs since the brk area can only use up the leavings from the stack allocation, where the stack allocation is larger than requested.
---
include/linux/mm_types.h | 2 ++ mm/nommu.c | 11 ++++++++--- 2 files changed, 10 insertions(+), 3 deletions(-)
include/linux/mm_types.h +2 -0
@@ -123,6 +123,8 @@ struct vm_region {struct file *vm_file;/* the backing file or NULL */ atomic_t vm_usage;/* region usage count */ + bool vm_icache_flushed : 1; /* true if the icache has been flushed for +* this region */ };/*
mm/nommu.c +8 -3
@@ -432,6 +432,7 @@ SYSCALL_DEFINE1(brk, unsigned long, brk)/* * Ok, looks good - let it rip. */ +flush_icache_range (mm->brk, brk);return mm->brk = brk; }
@@ -1353,10 +1354,14 @@ unsigned long do_mmap_pgoff(struct file *file, share:add_vma_to_mm (current->mm, vma); -up_write (&nommu_region_sem); +/* we flush the region from the icache only when the first executable +* mapping of it is made */ +if (vma->vm_flags &VM_EXEC && !region->vm_icache_flushed) { +flush_icache_range (region->vm_start, region->vm_end); + region->vm_icache_flushed = true; + } -if (prot &PROT_EXEC ) -flush_icache_range (result, result + len); +up_write (&nommu_region_sem);kleave (" = %lx" , result);return result;
PATCH 3/4 NOMMU: Use copy_*_user_page() in access_process_vm()
In reply to David HowellsFrom: Jie Zhang <[email protected]>
The MMU code uses the copy_*_user_page() variants in access_process_vm() rather than copy_*_user() as the former includes an icache flush. This is important when doing things like setting software breakpoints with gdb. So switch the NOMMU code over to do the same.
This patch makes the reasonable assumption that copy_from_user_page() won't fail - which is probably fine, as we've checked the VMA from which we're copying is usable, and the copy is not allowed to cross VMAs. The one case where it might go wrong is if the VMA is a device rather than RAM, and that device returns an error which - in which case rubbish will be returned rather than EIO.
---
mm/nommu.c | 6 ++++-- 1 files changed, 4 insertions(+), 2 deletions(-)
mm/nommu.c +4 -2
@@ -1921,9 +1921,11 @@ int access_process_vm(struct task_struct *tsk, unsigned long addr, void *buf, in/* only read or write mappings where it is permitted */ if (write && vma->vm_flags &VM_MAYWRITE ) - len -=copy_to_user ((void *) addr, buf, len); +copy_to_user_page (vma,NULL , addr, + (void *) addr, buf, len);else if (!write && vma->vm_flags &VM_MAYREAD ) - len -=copy_from_user (buf, (void *) addr, len); +copy_from_user_page (vma,NULL , addr, + buf, (void *) addr, len);else len =0 ; }else {
PATCH 4/4 elf_fdpic_core_dump(): fix build break in 2.6.33-rc1
In reply to David HowellsFrom: Daisuke HATAYAMA <[email protected]>
Commit f6151dfea21496d43dbaba32cfcd9c9f404769bc causes ELF-FDPIC to break during building, so this patch fixes it together with a correction for the printk format.
---
fs/binfmt_elf_fdpic.c | 4 ++-- 1 files changed, 2 insertions(+), 2 deletions(-)
fs/binfmt_elf_fdpic.c +2 -2
@@ -1809,8 +1809,8 @@ static int elf_fdpic_core_dump(struct coredump_params *cprm)if (cprm->file->f_pos != offset) {/* Sanity check */ -printk (KERN_WARNING -"elf_core_dump: file->f_pos (%lld) != offset (%lld)\n" , +printk (KERN_WARNING "elf_fdpic_core_dump:" +" cprm->file->f_pos (%lld) != offset (%lld)\n" , cprm->file->f_pos, offset); }
Re: [PATCH 4/4] elf_fdpic_core_dump(): fix build break in 2.6.33-rc1
Quoted 10 lines · David Howells
From: Daisuke HATAYAMA <[email protected]>
Commit
f6151dfea21496d43dbaba32cfcd9c9f404769bccauses ELF-FDPIC to break during building, so this patch fixes it together with a correction for the printk format.Signed-off-by Daisuke HATAYAMA <[email protected]>Reviewed-by Masami Hiramatsu <[email protected]>Acked-by Paul Mundt <[email protected]>Signed-off-by David Howells <[email protected]>
I already pushed this through my tree, so you can just ignore this one.
Re: [PATCH 4/4] elf_fdpic_core_dump(): fix build break in 2.6.33-rc1
In reply to David HowellsQuoted 6 lines · David Howells
From: Daisuke HATAYAMA <[email protected]>
Commit
f6151dfea21496d43dbaba32cfcd9c9f404769bccauses ELF-FDPIC to break during building, so this patch fixes it together with a correction for the printk format.
The comment doesn't match the patch. There's no build breakage fix (the actual fix was already merged, and your patch reflects that), there's only a printk format "fix" that is actually a de-provement because it splits the string across multiple lines.
So a strong NAK to this patch.
Linus
Re: [PATCH 4/4] elf_fdpic_core_dump(): fix build break in 2.6.33-rc1
Quoted 1 line · Linus Torvalds
So a strong NAK to this patch.
Okay, forget that one then. Can you take the others?
David