Skip to content

Commit a8502b6

Browse files
hansendcKAGA-KOKO
authored andcommitted
x86/pkeys: Make mprotect_key() mask off additional vm_flags
Today, mprotect() takes 4 bits of data: PROT_READ/WRITE/EXEC/NONE. Three of those bits: READ/WRITE/EXEC get translated directly in to vma->vm_flags by calc_vm_prot_bits(). If a bit is unset in mprotect()'s 'prot' argument then it must be cleared in vma->vm_flags during the mprotect() call. We do this clearing today by first calculating the VMA flags we want set, then clearing the ones we do not want to inherit from the original VMA: vm_flags = calc_vm_prot_bits(prot, key); ... newflags = vm_flags; newflags |= (vma->vm_flags & ~(VM_READ | VM_WRITE | VM_EXEC)); However, we *also* want to mask off the original VMA's vm_flags in which we store the protection key. To do that, this patch adds a new macro: ARCH_VM_PKEY_FLAGS which allows the architecture to specify additional bits that it would like cleared. We use that to ensure that the VM_PKEY_BIT* bits get cleared. Signed-off-by: Dave Hansen <[email protected]> Acked-by: Mel Gorman <[email protected]> Reviewed-by: Thomas Gleixner <[email protected]> Cc: [email protected] Cc: Dave Hansen <[email protected]> Cc: [email protected] Cc: [email protected] Cc: [email protected] Cc: [email protected] Cc: [email protected] Cc: [email protected] Link: http://lkml.kernel.org/r/[email protected] Signed-off-by: Thomas Gleixner <[email protected]>
1 parent 7d06d9c commit a8502b6

File tree

3 files changed

+13
-1
lines changed

3 files changed

+13
-1
lines changed

arch/x86/include/asm/pkeys.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,4 +38,6 @@ static inline int arch_override_mprotect_pkey(struct vm_area_struct *vma,
3838
extern int __arch_set_user_pkey_access(struct task_struct *tsk, int pkey,
3939
unsigned long init_val);
4040

41+
#define ARCH_VM_PKEY_FLAGS (VM_PKEY_BIT0 | VM_PKEY_BIT1 | VM_PKEY_BIT2 | VM_PKEY_BIT3)
42+
4143
#endif /*_ASM_X86_PKEYS_H */

include/linux/pkeys.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
#define execute_only_pkey(mm) (0)
1717
#define arch_override_mprotect_pkey(vma, prot, pkey) (0)
1818
#define PKEY_DEDICATED_EXECUTE_ONLY 0
19+
#define ARCH_VM_PKEY_FLAGS 0
1920
#endif /* ! CONFIG_ARCH_HAS_PKEYS */
2021

2122
#endif /* _LINUX_PKEYS_H */

mm/mprotect.c

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -417,6 +417,7 @@ static int do_mprotect_pkey(unsigned long start, size_t len,
417417
prev = vma;
418418

419419
for (nstart = start ; ; ) {
420+
unsigned long mask_off_old_flags;
420421
unsigned long newflags;
421422
int new_vma_pkey;
422423

@@ -426,9 +427,17 @@ static int do_mprotect_pkey(unsigned long start, size_t len,
426427
if (rier && (vma->vm_flags & VM_MAYEXEC))
427428
prot |= PROT_EXEC;
428429

430+
/*
431+
* Each mprotect() call explicitly passes r/w/x permissions.
432+
* If a permission is not passed to mprotect(), it must be
433+
* cleared from the VMA.
434+
*/
435+
mask_off_old_flags = VM_READ | VM_WRITE | VM_EXEC |
436+
ARCH_VM_PKEY_FLAGS;
437+
429438
new_vma_pkey = arch_override_mprotect_pkey(vma, prot, pkey);
430439
newflags = calc_vm_prot_bits(prot, new_vma_pkey);
431-
newflags |= (vma->vm_flags & ~(VM_READ | VM_WRITE | VM_EXEC));
440+
newflags |= (vma->vm_flags & ~mask_off_old_flags);
432441

433442
/* newflags >> 4 shift VM_MAY% in place of VM_% */
434443
if ((newflags & ~(newflags >> 4)) & (VM_READ | VM_WRITE | VM_EXEC)) {

0 commit comments

Comments
 (0)