Skip to content

Commit

Permalink
mm/core, x86/mm/pkeys: Differentiate instruction fetches
Browse files Browse the repository at this point in the history
As discussed earlier, we attempt to enforce protection keys in
software.

However, the code checks all faults to ensure that they are not
violating protection key permissions.  It was assumed that all
faults are either write faults where we check PKRU[key].WD (write
disable) or read faults where we check the AD (access disable)
bit.

But, there is a third category of faults for protection keys:
instruction faults.  Instruction faults never run afoul of
protection keys because they do not affect instruction fetches.

So, plumb the PF_INSTR bit down in to the
arch_vma_access_permitted() function where we do the protection
key checks.

We also add a new FAULT_FLAG_INSTRUCTION.  This is because
handle_mm_fault() is not passed the architecture-specific
error_code where we keep PF_INSTR, so we need to encode the
instruction fetch information in to the arch-generic fault
flags.

Signed-off-by: Dave Hansen <[email protected]>
Reviewed-by: Thomas Gleixner <[email protected]>
Cc: Andrew Morton <[email protected]>
Cc: Andy Lutomirski <[email protected]>
Cc: Borislav Petkov <[email protected]>
Cc: Brian Gerst <[email protected]>
Cc: Dave Hansen <[email protected]>
Cc: Denys Vlasenko <[email protected]>
Cc: H. Peter Anvin <[email protected]>
Cc: Linus Torvalds <[email protected]>
Cc: Peter Zijlstra <[email protected]>
Cc: Rik van Riel <[email protected]>
Cc: [email protected]
Link: http://lkml.kernel.org/r/[email protected]
Signed-off-by: Ingo Molnar <[email protected]>
  • Loading branch information
hansendc authored and Ingo Molnar committed Feb 18, 2016
1 parent 07f146f commit d61172b
Show file tree
Hide file tree
Showing 8 changed files with 24 additions and 8 deletions.
2 changes: 1 addition & 1 deletion arch/powerpc/include/asm/mmu_context.h
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,7 @@ static inline void arch_bprm_mm_init(struct mm_struct *mm,
}

static inline bool arch_vma_access_permitted(struct vm_area_struct *vma,
bool write, bool foreign)
bool write, bool execute, bool foreign)
{
/* by default, allow everything */
return true;
Expand Down
2 changes: 1 addition & 1 deletion arch/s390/include/asm/mmu_context.h
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ static inline void arch_bprm_mm_init(struct mm_struct *mm,
}

static inline bool arch_vma_access_permitted(struct vm_area_struct *vma,
bool write, bool foreign)
bool write, bool execute, bool foreign)
{
/* by default, allow everything */
return true;
Expand Down
5 changes: 4 additions & 1 deletion arch/x86/include/asm/mmu_context.h
Original file line number Diff line number Diff line change
Expand Up @@ -323,8 +323,11 @@ static inline bool vma_is_foreign(struct vm_area_struct *vma)
}

static inline bool arch_vma_access_permitted(struct vm_area_struct *vma,
bool write, bool foreign)
bool write, bool execute, bool foreign)
{
/* pkeys never affect instruction fetches */
if (execute)
return true;
/* allow access if the VMA is not one from this process */
if (foreign || vma_is_foreign(vma))
return true;
Expand Down
8 changes: 6 additions & 2 deletions arch/x86/mm/fault.c
Original file line number Diff line number Diff line change
Expand Up @@ -908,7 +908,8 @@ static inline bool bad_area_access_from_pkeys(unsigned long error_code,
if (error_code & PF_PK)
return true;
/* this checks permission keys on the VMA: */
if (!arch_vma_access_permitted(vma, (error_code & PF_WRITE), foreign))
if (!arch_vma_access_permitted(vma, (error_code & PF_WRITE),
(error_code & PF_INSTR), foreign))
return true;
return false;
}
Expand Down Expand Up @@ -1112,7 +1113,8 @@ access_error(unsigned long error_code, struct vm_area_struct *vma)
* faults just to hit a PF_PK as soon as we fill in a
* page.
*/
if (!arch_vma_access_permitted(vma, (error_code & PF_WRITE), foreign))
if (!arch_vma_access_permitted(vma, (error_code & PF_WRITE),
(error_code & PF_INSTR), foreign))
return 1;

if (error_code & PF_WRITE) {
Expand Down Expand Up @@ -1267,6 +1269,8 @@ __do_page_fault(struct pt_regs *regs, unsigned long error_code,

if (error_code & PF_WRITE)
flags |= FAULT_FLAG_WRITE;
if (error_code & PF_INSTR)
flags |= FAULT_FLAG_INSTRUCTION;

/*
* When running in the kernel we expect faults to occur only to
Expand Down
2 changes: 1 addition & 1 deletion include/asm-generic/mm_hooks.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ static inline void arch_bprm_mm_init(struct mm_struct *mm,
}

static inline bool arch_vma_access_permitted(struct vm_area_struct *vma,
bool write, bool foreign)
bool write, bool execute, bool foreign)
{
/* by default, allow everything */
return true;
Expand Down
1 change: 1 addition & 0 deletions include/linux/mm.h
Original file line number Diff line number Diff line change
Expand Up @@ -252,6 +252,7 @@ extern pgprot_t protection_map[16];
#define FAULT_FLAG_TRIED 0x20 /* Second try */
#define FAULT_FLAG_USER 0x40 /* The fault originated in userspace */
#define FAULT_FLAG_REMOTE 0x80 /* faulting for non current tsk/mm */
#define FAULT_FLAG_INSTRUCTION 0x100 /* The fault was during an instruction fetch */

/*
* vm_fault is filled by the the pagefault handler and passed to the vma's
Expand Down
11 changes: 9 additions & 2 deletions mm/gup.c
Original file line number Diff line number Diff line change
Expand Up @@ -449,7 +449,11 @@ static int check_vma_flags(struct vm_area_struct *vma, unsigned long gup_flags)
if (!(vm_flags & VM_MAYREAD))
return -EFAULT;
}
if (!arch_vma_access_permitted(vma, write, foreign))
/*
* gups are always data accesses, not instruction
* fetches, so execute=false here
*/
if (!arch_vma_access_permitted(vma, write, false, foreign))
return -EFAULT;
return 0;
}
Expand Down Expand Up @@ -629,8 +633,11 @@ bool vma_permits_fault(struct vm_area_struct *vma, unsigned int fault_flags)
/*
* The architecture might have a hardware protection
* mechanism other than read/write that can deny access.
*
* gup always represents data access, not instruction
* fetches, so execute=false here:
*/
if (!arch_vma_access_permitted(vma, write, foreign))
if (!arch_vma_access_permitted(vma, write, false, foreign))
return false;

return true;
Expand Down
1 change: 1 addition & 0 deletions mm/memory.c
Original file line number Diff line number Diff line change
Expand Up @@ -3380,6 +3380,7 @@ static int __handle_mm_fault(struct mm_struct *mm, struct vm_area_struct *vma,
pte_t *pte;

if (!arch_vma_access_permitted(vma, flags & FAULT_FLAG_WRITE,
flags & FAULT_FLAG_INSTRUCTION,
flags & FAULT_FLAG_REMOTE))
return VM_FAULT_SIGSEGV;

Expand Down

0 comments on commit d61172b

Please sign in to comment.