Skip to content

Commit a6fadf7

Browse files
wildea01ctmarinas
authored andcommitted
arm64: mm: introduce present, faulting entries for PAGE_NONE
This is mostly a port of dbf62d5 ("ARM: mm: introduce L_PTE_VALID for page table entries") and 26ffd0d ("ARM: mm: introduce present, faulting entries for PAGE_NONE") from ARM, which makes use of present, faulting page table entries for page table entries mapped as PROT_NONE. The main difference with this implementation is that we can make use of the two pte type bits in order to avoid allocating a software bit for identifying PROT_NONE pages, instead reserving the 10b suffix for these types of mappings. This is required to prevent users from accessing such pages via syscalls such as read/write over a pipe. Signed-off-by: Will Deacon <will.deacon@arm.com> Signed-off-by: Catalin Marinas <catalin.marinas@arm.com>
1 parent 0252246 commit a6fadf7

File tree

1 file changed

+13
-11
lines changed

1 file changed

+13
-11
lines changed

arch/arm64/include/asm/pgtable.h

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,8 @@
2424
/*
2525
* Software defined PTE bits definition.
2626
*/
27-
#define PTE_VALID (_AT(pteval_t, 1) << 0) /* pte_present() check */
27+
#define PTE_VALID (_AT(pteval_t, 1) << 0)
28+
#define PTE_PROT_NONE (_AT(pteval_t, 1) << 1) /* only when !PTE_VALID */
2829
#define PTE_FILE (_AT(pteval_t, 1) << 2) /* only when !pte_present() */
2930
#define PTE_DIRTY (_AT(pteval_t, 1) << 55)
3031
#define PTE_SPECIAL (_AT(pteval_t, 1) << 56)
@@ -60,9 +61,12 @@ extern void __pgd_error(const char *file, int line, unsigned long val);
6061

6162
extern pgprot_t pgprot_default;
6263

63-
#define _MOD_PROT(p, b) __pgprot(pgprot_val(p) | (b))
64+
#define __pgprot_modify(prot,mask,bits) \
65+
__pgprot((pgprot_val(prot) & ~(mask)) | (bits))
66+
67+
#define _MOD_PROT(p, b) __pgprot_modify(p, 0, b)
6468

65-
#define PAGE_NONE _MOD_PROT(pgprot_default, PTE_NG | PTE_PXN | PTE_UXN | PTE_RDONLY)
69+
#define PAGE_NONE __pgprot_modify(pgprot_default, PTE_TYPE_MASK, PTE_PROT_NONE)
6670
#define PAGE_SHARED _MOD_PROT(pgprot_default, PTE_USER | PTE_NG | PTE_PXN | PTE_UXN)
6771
#define PAGE_SHARED_EXEC _MOD_PROT(pgprot_default, PTE_USER | PTE_NG | PTE_PXN)
6872
#define PAGE_COPY _MOD_PROT(pgprot_default, PTE_USER | PTE_NG | PTE_PXN | PTE_UXN | PTE_RDONLY)
@@ -72,7 +76,7 @@ extern pgprot_t pgprot_default;
7276
#define PAGE_KERNEL _MOD_PROT(pgprot_default, PTE_PXN | PTE_UXN | PTE_DIRTY)
7377
#define PAGE_KERNEL_EXEC _MOD_PROT(pgprot_default, PTE_UXN | PTE_DIRTY)
7478

75-
#define __PAGE_NONE __pgprot(_PAGE_DEFAULT | PTE_NG | PTE_PXN | PTE_UXN | PTE_RDONLY)
79+
#define __PAGE_NONE __pgprot(((_PAGE_DEFAULT) & ~PTE_TYPE_MASK) | PTE_PROT_NONE)
7680
#define __PAGE_SHARED __pgprot(_PAGE_DEFAULT | PTE_USER | PTE_NG | PTE_PXN | PTE_UXN)
7781
#define __PAGE_SHARED_EXEC __pgprot(_PAGE_DEFAULT | PTE_USER | PTE_NG | PTE_PXN)
7882
#define __PAGE_COPY __pgprot(_PAGE_DEFAULT | PTE_USER | PTE_NG | PTE_PXN | PTE_UXN | PTE_RDONLY)
@@ -125,14 +129,14 @@ extern struct page *empty_zero_page;
125129
/*
126130
* The following only work if pte_present(). Undefined behaviour otherwise.
127131
*/
128-
#define pte_present(pte) (pte_val(pte) & PTE_VALID)
132+
#define pte_present(pte) (pte_val(pte) & (PTE_VALID | PTE_PROT_NONE))
129133
#define pte_dirty(pte) (pte_val(pte) & PTE_DIRTY)
130134
#define pte_young(pte) (pte_val(pte) & PTE_AF)
131135
#define pte_special(pte) (pte_val(pte) & PTE_SPECIAL)
132136
#define pte_write(pte) (!(pte_val(pte) & PTE_RDONLY))
133137
#define pte_exec(pte) (!(pte_val(pte) & PTE_UXN))
134138

135-
#define pte_present_user(pte) \
139+
#define pte_valid_user(pte) \
136140
((pte_val(pte) & (PTE_VALID | PTE_USER)) == (PTE_VALID | PTE_USER))
137141

138142
#define PTE_BIT_FUNC(fn,op) \
@@ -156,7 +160,7 @@ extern void __sync_icache_dcache(pte_t pteval, unsigned long addr);
156160
static inline void set_pte_at(struct mm_struct *mm, unsigned long addr,
157161
pte_t *ptep, pte_t pte)
158162
{
159-
if (pte_present_user(pte)) {
163+
if (pte_valid_user(pte)) {
160164
if (pte_exec(pte))
161165
__sync_icache_dcache(pte, addr);
162166
if (!pte_dirty(pte))
@@ -172,9 +176,6 @@ static inline void set_pte_at(struct mm_struct *mm, unsigned long addr,
172176
#define pte_huge(pte) ((pte_val(pte) & PTE_TYPE_MASK) == PTE_TYPE_HUGEPAGE)
173177
#define pte_mkhuge(pte) (__pte((pte_val(pte) & ~PTE_TYPE_MASK) | PTE_TYPE_HUGEPAGE))
174178

175-
#define __pgprot_modify(prot,mask,bits) \
176-
__pgprot((pgprot_val(prot) & ~(mask)) | (bits))
177-
178179
#define __HAVE_ARCH_PTE_SPECIAL
179180

180181
/*
@@ -266,7 +267,8 @@ static inline pmd_t *pmd_offset(pud_t *pud, unsigned long addr)
266267

267268
static inline pte_t pte_modify(pte_t pte, pgprot_t newprot)
268269
{
269-
const pteval_t mask = PTE_USER | PTE_PXN | PTE_UXN | PTE_RDONLY;
270+
const pteval_t mask = PTE_USER | PTE_PXN | PTE_UXN | PTE_RDONLY |
271+
PTE_PROT_NONE | PTE_VALID;
270272
pte_val(pte) = (pte_val(pte) & ~mask) | (pgprot_val(newprot) & mask);
271273
return pte;
272274
}

0 commit comments

Comments
 (0)