x86, pkeys: execute-only support
Subsystems (4)
RFC PATCH 1/1 x86, pkeys: execute-only support
This patch is on top of the base protection keys support which can be found here:
http://git.kernel.org/cgit/linux/kernel/git/daveh/x86-pkeys.git/log/?h=pkeys-v014
Protection keys provide new page-based protection in hardware. But, they have an interesting attribute: they only affect data accesses and never affect instruction fetches. That means that if we set up some memory which is set as "access-disabled" via protection keys, we can still execute from it.
This patch uses protection keys to set up mappings to do just that. If a user calls:
mmap(..., PROT_EXEC); or mprotect(ptr, sz, PROT_EXEC);
(note PROT_EXEC-only without PROT_READ/WRITE), the kernel will notice this, and set a special protection key on the memory. It also sets the appropriate bits in the Protection Keys User Rights (PKRU) register so that the memory becomes unreadable and unwritable.
I haven't found any userspace that does this today.
The security provided by this approach is not comprehensive. The PKRU register which controls access permissions is a normal user register writable from unprivileged userspace. An attacker who can execute the 'wrpkru' instruction can easily disable the protection provided by this feature.
The protection key that is used for execute-only support is permanently dedicated in a process. Even if all of the execute-only mappings go away, the key stays allocated. This could be fixed, but it will involve a reference count or a walk of all of the VMAs.
This code is still a bit rough, doesn't compile with a wide range of config options, etc... But it gives a rough idea about how complicated the feature is. It's surprisingly reasonable.
Semantically, we have a bit of a problem: What do we do when we mix execute-only pkey use with mprotect_pkey() use?
mprotect_pkey(ptr, PAGE_SIZE, PROT_WRITE, 6); // set pkey=6 mprotect(ptr, PAGE_SIZE, PROT_EXEC); // set pkey=X_ONLY_PKEY? mprotect(ptr, PAGE_SIZE, PROT_WRITE); // is pkey=6 again?
To solve that, we make the plain-mprotect()-initiated execute-only support only apply to VMAs that have the default protection key (0) set on them.
Proposed semantics:
1. protection key 0 is special and represents the default, unassigned protection key. It is always allocated.
2. mprotect() never affects a mapping's mprotect_pkey()-assigned protection key. A protection key of 0 (even if set explicitly) represents an unassigned protection key.
2a. mprotect(PROT_EXEC) on a mapping with an assigned protection
key may or may not result in a mapping with execute-only properties. mprotect_pkey() plus pkey_set() on all threads should be used to _guarantee_ execute-only semantics.
3. mprotect(PROT_EXEC) may result in an "execute-only" mapping. The kernel will internally attempt to allocate and dedicate a protection key for the purpose of execute-only mappings.
---
b/arch/x86/include/asm/mmu.h | 1 b/arch/x86/include/asm/mmu_context.h | 1 b/arch/x86/include/asm/pkeys.h | 41 +++++++++++++++- b/arch/x86/mm/fault.c | 87 +++++++++++++++++++++++++++++++++++ b/mm/mmap.c | 9 +++ b/mm/mprotect.c | 10 +--- 6 files changed, 139 insertions(+), 10 deletions(-)
mm/mprotect.c renamed from mm/mprotect.c~pkeys-xonly +3 -7
@@ -428,14 +428,10 @@ static int do_mprotect_pkey(unsigned lon/* Here we know that vma->vm_start <= nstart < vma->vm_end. */ -/* -* If this is a vanilla, non-pkey mprotect, inherit the -* pkey from the VMA we are working on. -*/ if (plain_mprotect) - newflags =calc_vm_prot_bits (prot,vma_pkey (vma)); -else - newflags =calc_vm_prot_bits (prot, pkey); + pkey =arch_override_mprotect_pkey (vma, prot, pkey); + + newflags =calc_vm_prot_bits (prot, pkey); newflags |= (vma->vm_flags & ~mask_off_old_flags);/* newflags >> 4 shift VM_MAY% in place of VM_% */
mm/mmap.c renamed from mm/mmap.c~pkeys-xonly +8 -1
@@ -1266,6 +1266,7 @@ unsigned long do_mmap(struct file *file, unsigned long pgoff, unsigned long *populate) { struct mm_struct *mm = current->mm; +int pkey =0 ; *populate =0 ;
@@ -1305,11 +1306,17 @@ unsigned long do_mmap(struct file *file,if (offset_in_page (addr))return addr; +if (prot ==PROT_EXEC ) { + pkey =execute_only_pkey (mm); +if (pkey <0 ) + pkey =0 ; + } +/* Do simple checking here so the lower-level routines won't have * to. we assume access permissions have been handled by the open * of the memory object, so we don't do any here. */ - vm_flags |=calc_vm_prot_bits (prot,0 ) |calc_vm_flag_bits (flags) | + vm_flags |=calc_vm_prot_bits (prot, pkey) |calc_vm_flag_bits (flags) | mm->def_flags |VM_MAYREAD |VM_MAYWRITE |VM_MAYEXEC ;if (flags &MAP_LOCKED )
arch/x86/include/asm/mmu_context.h renamed from arch/x86/include/asm/mmu_context.h~pkeys-xonly +1 -0
@@ -111,6 +111,7 @@ static inline int init_new_context(struc#ifdef CONFIG_X86_INTEL_MEMORY_PROTECTION_KEYS /* pkey 0 is the default and always allocated */ mm->context.pkey_allocation_map =0x1 ; + mm->context.execute_only_pkey =-1 ;#endif init_new_context_ldt (tsk, mm);
arch/x86/include/asm/mmu.h renamed from arch/x86/include/asm/mmu.h~pkeys-xonly +1 -0
@@ -28,6 +28,7 @@ typedef struct {* use it or not. protected by mmap_sem. */ u16 pkey_allocation_map; +s16 execute_only_pkey;#endif } mm_context_t;
arch/x86/include/asm/pkeys.h renamed from arch/x86/include/asm/pkeys.h~pkeys-xonly +39 -2
72 lines not on this page · raw message
arch/x86/mm/fault.c renamed from arch/x86/mm/fault.c~pkeys-xonly +87 -0
102 lines not on this page · raw message
_