Skip to content

Commit 1c12c4c

Browse files
Venki Pallipaditorvalds
authored andcommitted
mprotect: prevent alteration of the PAT bits
There is a defect in mprotect, which lets the user change the page cache type bits by-passing the kernel reserve_memtype and free_memtype wrappers. Fix the problem by not letting mprotect change the PAT bits. Signed-off-by: Venkatesh Pallipadi <venkatesh.pallipadi@intel.com> Signed-off-by: Suresh Siddha <suresh.b.siddha@intel.com> Signed-off-by: Ingo Molnar <mingo@elte.hu> Signed-off-by: Hugh Dickins <hugh@veritas.com> Signed-off-by: Andrew Morton <akpm@linux-foundation.org> Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
1 parent 44c8143 commit 1c12c4c

2 files changed

Lines changed: 23 additions & 4 deletions

File tree

‎include/asm-x86/pgtable.h‎

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,8 @@
5757
#define _KERNPG_TABLE (_PAGE_PRESENT | _PAGE_RW | _PAGE_ACCESSED | \
5858
_PAGE_DIRTY)
5959

60-
#define _PAGE_CHG_MASK (PTE_MASK | _PAGE_ACCESSED | _PAGE_DIRTY)
60+
#define _PAGE_CHG_MASK (PTE_MASK | _PAGE_PCD | _PAGE_PWT | \
61+
_PAGE_ACCESSED | _PAGE_DIRTY)
6162

6263
#define _PAGE_CACHE_MASK (_PAGE_PCD | _PAGE_PWT)
6364
#define _PAGE_CACHE_WB (0)
@@ -288,12 +289,21 @@ static inline pte_t pte_modify(pte_t pte, pgprot_t newprot)
288289
* Chop off the NX bit (if present), and add the NX portion of
289290
* the newprot (if present):
290291
*/
291-
val &= _PAGE_CHG_MASK & ~_PAGE_NX;
292-
val |= pgprot_val(newprot) & __supported_pte_mask;
292+
val &= _PAGE_CHG_MASK;
293+
val |= pgprot_val(newprot) & (~_PAGE_CHG_MASK) & __supported_pte_mask;
293294

294295
return __pte(val);
295296
}
296297

298+
/* mprotect needs to preserve PAT bits when updating vm_page_prot */
299+
#define pgprot_modify pgprot_modify
300+
static inline pgprot_t pgprot_modify(pgprot_t oldprot, pgprot_t newprot)
301+
{
302+
pgprotval_t preservebits = pgprot_val(oldprot) & _PAGE_CHG_MASK;
303+
pgprotval_t addbits = pgprot_val(newprot);
304+
return __pgprot(preservebits | addbits);
305+
}
306+
297307
#define pte_pgprot(x) __pgprot(pte_val(x) & (0xfff | _PAGE_NX))
298308

299309
#define canon_pgprot(p) __pgprot(pgprot_val(p) & __supported_pte_mask)

‎mm/mprotect.c‎

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,13 @@
2626
#include <asm/cacheflush.h>
2727
#include <asm/tlbflush.h>
2828

29+
#ifndef pgprot_modify
30+
static inline pgprot_t pgprot_modify(pgprot_t oldprot, pgprot_t newprot)
31+
{
32+
return newprot;
33+
}
34+
#endif
35+
2936
static void change_pte_range(struct mm_struct *mm, pmd_t *pmd,
3037
unsigned long addr, unsigned long end, pgprot_t newprot,
3138
int dirty_accountable)
@@ -192,7 +199,9 @@ mprotect_fixup(struct vm_area_struct *vma, struct vm_area_struct **pprev,
192199
* held in write mode.
193200
*/
194201
vma->vm_flags = newflags;
195-
vma->vm_page_prot = vm_get_page_prot(newflags);
202+
vma->vm_page_prot = pgprot_modify(vma->vm_page_prot,
203+
vm_get_page_prot(newflags));
204+
196205
if (vma_wants_writenotify(vma)) {
197206
vma->vm_page_prot = vm_get_page_prot(newflags & ~VM_SHARED);
198207
dirty_accountable = 1;

0 commit comments

Comments
 (0)