Skip to content

Commit d61172b

Browse files
hansendcIngo Molnar
authored andcommitted
mm/core, x86/mm/pkeys: Differentiate instruction fetches
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 <dave.hansen@linux.intel.com> Reviewed-by: Thomas Gleixner <tglx@linutronix.de> Cc: Andrew Morton <akpm@linux-foundation.org> Cc: Andy Lutomirski <luto@amacapital.net> Cc: Borislav Petkov <bp@alien8.de> Cc: Brian Gerst <brgerst@gmail.com> Cc: Dave Hansen <dave@sr71.net> Cc: Denys Vlasenko <dvlasenk@redhat.com> Cc: H. Peter Anvin <hpa@zytor.com> Cc: Linus Torvalds <torvalds@linux-foundation.org> Cc: Peter Zijlstra <peterz@infradead.org> Cc: Rik van Riel <riel@redhat.com> Cc: linux-mm@kvack.org Link: http://lkml.kernel.org/r/20160212210224.96928009@viggo.jf.intel.com Signed-off-by: Ingo Molnar <mingo@kernel.org>
1 parent 07f146f commit d61172b

File tree

8 files changed

+24
-8
lines changed

8 files changed

+24
-8
lines changed

arch/powerpc/include/asm/mmu_context.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -149,7 +149,7 @@ static inline void arch_bprm_mm_init(struct mm_struct *mm,
149149
}
150150

151151
static inline bool arch_vma_access_permitted(struct vm_area_struct *vma,
152-
bool write, bool foreign)
152+
bool write, bool execute, bool foreign)
153153
{
154154
/* by default, allow everything */
155155
return true;

arch/s390/include/asm/mmu_context.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,7 @@ static inline void arch_bprm_mm_init(struct mm_struct *mm,
131131
}
132132

133133
static inline bool arch_vma_access_permitted(struct vm_area_struct *vma,
134-
bool write, bool foreign)
134+
bool write, bool execute, bool foreign)
135135
{
136136
/* by default, allow everything */
137137
return true;

arch/x86/include/asm/mmu_context.h

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -323,8 +323,11 @@ static inline bool vma_is_foreign(struct vm_area_struct *vma)
323323
}
324324

325325
static inline bool arch_vma_access_permitted(struct vm_area_struct *vma,
326-
bool write, bool foreign)
326+
bool write, bool execute, bool foreign)
327327
{
328+
/* pkeys never affect instruction fetches */
329+
if (execute)
330+
return true;
328331
/* allow access if the VMA is not one from this process */
329332
if (foreign || vma_is_foreign(vma))
330333
return true;

arch/x86/mm/fault.c

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -908,7 +908,8 @@ static inline bool bad_area_access_from_pkeys(unsigned long error_code,
908908
if (error_code & PF_PK)
909909
return true;
910910
/* this checks permission keys on the VMA: */
911-
if (!arch_vma_access_permitted(vma, (error_code & PF_WRITE), foreign))
911+
if (!arch_vma_access_permitted(vma, (error_code & PF_WRITE),
912+
(error_code & PF_INSTR), foreign))
912913
return true;
913914
return false;
914915
}
@@ -1112,7 +1113,8 @@ access_error(unsigned long error_code, struct vm_area_struct *vma)
11121113
* faults just to hit a PF_PK as soon as we fill in a
11131114
* page.
11141115
*/
1115-
if (!arch_vma_access_permitted(vma, (error_code & PF_WRITE), foreign))
1116+
if (!arch_vma_access_permitted(vma, (error_code & PF_WRITE),
1117+
(error_code & PF_INSTR), foreign))
11161118
return 1;
11171119

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

12681270
if (error_code & PF_WRITE)
12691271
flags |= FAULT_FLAG_WRITE;
1272+
if (error_code & PF_INSTR)
1273+
flags |= FAULT_FLAG_INSTRUCTION;
12701274

12711275
/*
12721276
* When running in the kernel we expect faults to occur only to

include/asm-generic/mm_hooks.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ static inline void arch_bprm_mm_init(struct mm_struct *mm,
2727
}
2828

2929
static inline bool arch_vma_access_permitted(struct vm_area_struct *vma,
30-
bool write, bool foreign)
30+
bool write, bool execute, bool foreign)
3131
{
3232
/* by default, allow everything */
3333
return true;

include/linux/mm.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -252,6 +252,7 @@ extern pgprot_t protection_map[16];
252252
#define FAULT_FLAG_TRIED 0x20 /* Second try */
253253
#define FAULT_FLAG_USER 0x40 /* The fault originated in userspace */
254254
#define FAULT_FLAG_REMOTE 0x80 /* faulting for non current tsk/mm */
255+
#define FAULT_FLAG_INSTRUCTION 0x100 /* The fault was during an instruction fetch */
255256

256257
/*
257258
* vm_fault is filled by the the pagefault handler and passed to the vma's

mm/gup.c

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -449,7 +449,11 @@ static int check_vma_flags(struct vm_area_struct *vma, unsigned long gup_flags)
449449
if (!(vm_flags & VM_MAYREAD))
450450
return -EFAULT;
451451
}
452-
if (!arch_vma_access_permitted(vma, write, foreign))
452+
/*
453+
* gups are always data accesses, not instruction
454+
* fetches, so execute=false here
455+
*/
456+
if (!arch_vma_access_permitted(vma, write, false, foreign))
453457
return -EFAULT;
454458
return 0;
455459
}
@@ -629,8 +633,11 @@ bool vma_permits_fault(struct vm_area_struct *vma, unsigned int fault_flags)
629633
/*
630634
* The architecture might have a hardware protection
631635
* mechanism other than read/write that can deny access.
636+
*
637+
* gup always represents data access, not instruction
638+
* fetches, so execute=false here:
632639
*/
633-
if (!arch_vma_access_permitted(vma, write, foreign))
640+
if (!arch_vma_access_permitted(vma, write, false, foreign))
634641
return false;
635642

636643
return true;

mm/memory.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3380,6 +3380,7 @@ static int __handle_mm_fault(struct mm_struct *mm, struct vm_area_struct *vma,
33803380
pte_t *pte;
33813381

33823382
if (!arch_vma_access_permitted(vma, flags & FAULT_FLAG_WRITE,
3383+
flags & FAULT_FLAG_INSTRUCTION,
33833384
flags & FAULT_FLAG_REMOTE))
33843385
return VM_FAULT_SIGSEGV;
33853386

0 commit comments

Comments
 (0)