• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  *  mm/mprotect.c
4  *
5  *  (C) Copyright 1994 Linus Torvalds
6  *  (C) Copyright 2002 Christoph Hellwig
7  *
8  *  Address space accounting code	<alan@lxorguk.ukuu.org.uk>
9  *  (C) Copyright 2002 Red Hat Inc, All Rights Reserved
10  */
11 
12 #include <linux/pagewalk.h>
13 #include <linux/hugetlb.h>
14 #include <linux/shm.h>
15 #include <linux/mman.h>
16 #include <linux/fs.h>
17 #include <linux/highmem.h>
18 #include <linux/security.h>
19 #include <linux/mempolicy.h>
20 #include <linux/personality.h>
21 #include <linux/syscalls.h>
22 #include <linux/swap.h>
23 #include <linux/swapops.h>
24 #include <linux/mmu_notifier.h>
25 #include <linux/migrate.h>
26 #include <linux/perf_event.h>
27 #include <linux/pkeys.h>
28 #include <linux/ksm.h>
29 #include <linux/uaccess.h>
30 #include <linux/mm_inline.h>
31 #include <linux/pgtable.h>
32 #include <asm/cacheflush.h>
33 #include <asm/mmu_context.h>
34 #include <asm/tlbflush.h>
35 #include <asm/tlb.h>
36 
37 #include "internal.h"
38 
change_pte_range(struct mmu_gather * tlb,struct vm_area_struct * vma,pmd_t * pmd,unsigned long addr,unsigned long end,pgprot_t newprot,unsigned long cp_flags)39 static unsigned long change_pte_range(struct mmu_gather *tlb,
40 		struct vm_area_struct *vma, pmd_t *pmd, unsigned long addr,
41 		unsigned long end, pgprot_t newprot, unsigned long cp_flags)
42 {
43 	pte_t *pte, oldpte;
44 	spinlock_t *ptl;
45 	unsigned long pages = 0;
46 	int target_node = NUMA_NO_NODE;
47 	bool dirty_accountable = cp_flags & MM_CP_DIRTY_ACCT;
48 	bool prot_numa = cp_flags & MM_CP_PROT_NUMA;
49 	bool uffd_wp = cp_flags & MM_CP_UFFD_WP;
50 	bool uffd_wp_resolve = cp_flags & MM_CP_UFFD_WP_RESOLVE;
51 
52 	tlb_change_page_size(tlb, PAGE_SIZE);
53 
54 	/*
55 	 * Can be called with only the mmap_lock for reading by
56 	 * prot_numa so we must check the pmd isn't constantly
57 	 * changing from under us from pmd_none to pmd_trans_huge
58 	 * and/or the other way around.
59 	 */
60 	if (pmd_trans_unstable(pmd))
61 		return 0;
62 
63 	/*
64 	 * The pmd points to a regular pte so the pmd can't change
65 	 * from under us even if the mmap_lock is only hold for
66 	 * reading.
67 	 */
68 	pte = pte_offset_map_lock(vma->vm_mm, pmd, addr, &ptl);
69 
70 	/* Get target node for single threaded private VMAs */
71 	if (prot_numa && !(vma->vm_flags & VM_SHARED) &&
72 	    atomic_read(&vma->vm_mm->mm_users) == 1)
73 		target_node = numa_node_id();
74 
75 	flush_tlb_batched_pending(vma->vm_mm);
76 	arch_enter_lazy_mmu_mode();
77 	do {
78 		oldpte = *pte;
79 		if (pte_present(oldpte)) {
80 			pte_t ptent;
81 			bool preserve_write = prot_numa && pte_write(oldpte);
82 
83 			/*
84 			 * Avoid trapping faults against the zero or KSM
85 			 * pages. See similar comment in change_huge_pmd.
86 			 */
87 			if (prot_numa) {
88 				struct page *page;
89 
90 				/* Avoid TLB flush if possible */
91 				if (pte_protnone(oldpte))
92 					continue;
93 
94 				page = vm_normal_page(vma, addr, oldpte);
95 				if (!page || PageKsm(page))
96 					continue;
97 
98 				/* Also skip shared copy-on-write pages */
99 				if (is_cow_mapping(vma->vm_flags) &&
100 				    page_count(page) != 1)
101 					continue;
102 
103 				/*
104 				 * While migration can move some dirty pages,
105 				 * it cannot move them all from MIGRATE_ASYNC
106 				 * context.
107 				 */
108 				if (page_is_file_lru(page) && PageDirty(page))
109 					continue;
110 
111 				/*
112 				 * Don't mess with PTEs if page is already on the node
113 				 * a single-threaded process is running on.
114 				 */
115 				if (target_node == page_to_nid(page))
116 					continue;
117 			}
118 
119 			oldpte = ptep_modify_prot_start(vma, addr, pte);
120 			ptent = pte_modify(oldpte, newprot);
121 			if (preserve_write)
122 				ptent = pte_mk_savedwrite(ptent);
123 
124 			if (uffd_wp) {
125 				ptent = pte_wrprotect(ptent);
126 				ptent = pte_mkuffd_wp(ptent);
127 			} else if (uffd_wp_resolve) {
128 				/*
129 				 * Leave the write bit to be handled
130 				 * by PF interrupt handler, then
131 				 * things like COW could be properly
132 				 * handled.
133 				 */
134 				ptent = pte_clear_uffd_wp(ptent);
135 			}
136 
137 			/* Avoid taking write faults for known dirty pages */
138 			if (dirty_accountable && pte_dirty(ptent) &&
139 					(pte_soft_dirty(ptent) ||
140 					 !(vma->vm_flags & VM_SOFTDIRTY))) {
141 				ptent = pte_mkwrite(ptent);
142 			}
143 			ptep_modify_prot_commit(vma, addr, pte, oldpte, ptent);
144 			if (pte_needs_flush(oldpte, ptent))
145 				tlb_flush_pte_range(tlb, addr, PAGE_SIZE);
146 			pages++;
147 		} else if (is_swap_pte(oldpte)) {
148 			swp_entry_t entry = pte_to_swp_entry(oldpte);
149 			pte_t newpte;
150 
151 			if (is_writable_migration_entry(entry)) {
152 				/*
153 				 * A protection check is difficult so
154 				 * just be safe and disable write
155 				 */
156 				entry = make_readable_migration_entry(
157 							swp_offset(entry));
158 				newpte = swp_entry_to_pte(entry);
159 				if (pte_swp_soft_dirty(oldpte))
160 					newpte = pte_swp_mksoft_dirty(newpte);
161 				if (pte_swp_uffd_wp(oldpte))
162 					newpte = pte_swp_mkuffd_wp(newpte);
163 			} else if (is_writable_device_private_entry(entry)) {
164 				/*
165 				 * We do not preserve soft-dirtiness. See
166 				 * copy_one_pte() for explanation.
167 				 */
168 				entry = make_readable_device_private_entry(
169 							swp_offset(entry));
170 				newpte = swp_entry_to_pte(entry);
171 				if (pte_swp_uffd_wp(oldpte))
172 					newpte = pte_swp_mkuffd_wp(newpte);
173 			} else if (is_writable_device_exclusive_entry(entry)) {
174 				entry = make_readable_device_exclusive_entry(
175 							swp_offset(entry));
176 				newpte = swp_entry_to_pte(entry);
177 				if (pte_swp_soft_dirty(oldpte))
178 					newpte = pte_swp_mksoft_dirty(newpte);
179 				if (pte_swp_uffd_wp(oldpte))
180 					newpte = pte_swp_mkuffd_wp(newpte);
181 			} else {
182 				newpte = oldpte;
183 			}
184 
185 			if (uffd_wp)
186 				newpte = pte_swp_mkuffd_wp(newpte);
187 			else if (uffd_wp_resolve)
188 				newpte = pte_swp_clear_uffd_wp(newpte);
189 
190 			if (!pte_same(oldpte, newpte)) {
191 				set_pte_at(vma->vm_mm, addr, pte, newpte);
192 				pages++;
193 			}
194 		}
195 	} while (pte++, addr += PAGE_SIZE, addr != end);
196 	arch_leave_lazy_mmu_mode();
197 	pte_unmap_unlock(pte - 1, ptl);
198 
199 	return pages;
200 }
201 
202 /*
203  * Used when setting automatic NUMA hinting protection where it is
204  * critical that a numa hinting PMD is not confused with a bad PMD.
205  */
pmd_none_or_clear_bad_unless_trans_huge(pmd_t * pmd)206 static inline int pmd_none_or_clear_bad_unless_trans_huge(pmd_t *pmd)
207 {
208 	pmd_t pmdval = pmd_read_atomic(pmd);
209 
210 	/* See pmd_none_or_trans_huge_or_clear_bad for info on barrier */
211 #ifdef CONFIG_TRANSPARENT_HUGEPAGE
212 	barrier();
213 #endif
214 
215 	if (pmd_none(pmdval))
216 		return 1;
217 	if (pmd_trans_huge(pmdval))
218 		return 0;
219 	if (unlikely(pmd_bad(pmdval))) {
220 		pmd_clear_bad(pmd);
221 		return 1;
222 	}
223 
224 	return 0;
225 }
226 
change_pmd_range(struct mmu_gather * tlb,struct vm_area_struct * vma,pud_t * pud,unsigned long addr,unsigned long end,pgprot_t newprot,unsigned long cp_flags)227 static inline unsigned long change_pmd_range(struct mmu_gather *tlb,
228 		struct vm_area_struct *vma, pud_t *pud, unsigned long addr,
229 		unsigned long end, pgprot_t newprot, unsigned long cp_flags)
230 {
231 	pmd_t *pmd;
232 	unsigned long next;
233 	unsigned long pages = 0;
234 	unsigned long nr_huge_updates = 0;
235 	struct mmu_notifier_range range;
236 
237 	range.start = 0;
238 
239 	pmd = pmd_offset(pud, addr);
240 	do {
241 		unsigned long this_pages;
242 
243 		next = pmd_addr_end(addr, end);
244 
245 		/*
246 		 * Automatic NUMA balancing walks the tables with mmap_lock
247 		 * held for read. It's possible a parallel update to occur
248 		 * between pmd_trans_huge() and a pmd_none_or_clear_bad()
249 		 * check leading to a false positive and clearing.
250 		 * Hence, it's necessary to atomically read the PMD value
251 		 * for all the checks.
252 		 */
253 		if (!is_swap_pmd(*pmd) && !pmd_devmap(*pmd) &&
254 		     pmd_none_or_clear_bad_unless_trans_huge(pmd))
255 			goto next;
256 
257 		/* invoke the mmu notifier if the pmd is populated */
258 		if (!range.start) {
259 			mmu_notifier_range_init(&range,
260 				MMU_NOTIFY_PROTECTION_VMA, 0,
261 				vma, vma->vm_mm, addr, end);
262 			mmu_notifier_invalidate_range_start(&range);
263 		}
264 
265 		if (is_swap_pmd(*pmd) || pmd_trans_huge(*pmd) || pmd_devmap(*pmd)) {
266 			if (next - addr != HPAGE_PMD_SIZE) {
267 				__split_huge_pmd(vma, pmd, addr, false, NULL);
268 			} else {
269 				/*
270 				 * change_huge_pmd() does not defer TLB flushes,
271 				 * so no need to propagate the tlb argument.
272 				 */
273 				int nr_ptes = change_huge_pmd(tlb, vma, pmd,
274 						addr, newprot, cp_flags);
275 
276 				if (nr_ptes) {
277 					if (nr_ptes == HPAGE_PMD_NR) {
278 						pages += HPAGE_PMD_NR;
279 						nr_huge_updates++;
280 					}
281 
282 					/* huge pmd was handled */
283 					goto next;
284 				}
285 			}
286 			/* fall through, the trans huge pmd just split */
287 		}
288 		this_pages = change_pte_range(tlb, vma, pmd, addr, next,
289 					      newprot, cp_flags);
290 		pages += this_pages;
291 next:
292 		cond_resched();
293 	} while (pmd++, addr = next, addr != end);
294 
295 	if (range.start)
296 		mmu_notifier_invalidate_range_end(&range);
297 
298 	if (nr_huge_updates)
299 		count_vm_numa_events(NUMA_HUGE_PTE_UPDATES, nr_huge_updates);
300 	return pages;
301 }
302 
change_pud_range(struct mmu_gather * tlb,struct vm_area_struct * vma,p4d_t * p4d,unsigned long addr,unsigned long end,pgprot_t newprot,unsigned long cp_flags)303 static inline unsigned long change_pud_range(struct mmu_gather *tlb,
304 		struct vm_area_struct *vma, p4d_t *p4d, unsigned long addr,
305 		unsigned long end, pgprot_t newprot, unsigned long cp_flags)
306 {
307 	pud_t *pud;
308 	unsigned long next;
309 	unsigned long pages = 0;
310 
311 	pud = pud_offset(p4d, addr);
312 	do {
313 		next = pud_addr_end(addr, end);
314 		if (pud_none_or_clear_bad(pud))
315 			continue;
316 		pages += change_pmd_range(tlb, vma, pud, addr, next, newprot,
317 					  cp_flags);
318 	} while (pud++, addr = next, addr != end);
319 
320 	return pages;
321 }
322 
change_p4d_range(struct mmu_gather * tlb,struct vm_area_struct * vma,pgd_t * pgd,unsigned long addr,unsigned long end,pgprot_t newprot,unsigned long cp_flags)323 static inline unsigned long change_p4d_range(struct mmu_gather *tlb,
324 		struct vm_area_struct *vma, pgd_t *pgd, unsigned long addr,
325 		unsigned long end, pgprot_t newprot, unsigned long cp_flags)
326 {
327 	p4d_t *p4d;
328 	unsigned long next;
329 	unsigned long pages = 0;
330 
331 	p4d = p4d_offset(pgd, addr);
332 	do {
333 		next = p4d_addr_end(addr, end);
334 		if (p4d_none_or_clear_bad(p4d))
335 			continue;
336 		pages += change_pud_range(tlb, vma, p4d, addr, next, newprot,
337 					  cp_flags);
338 	} while (p4d++, addr = next, addr != end);
339 
340 	return pages;
341 }
342 
change_protection_range(struct mmu_gather * tlb,struct vm_area_struct * vma,unsigned long addr,unsigned long end,pgprot_t newprot,unsigned long cp_flags)343 static unsigned long change_protection_range(struct mmu_gather *tlb,
344 		struct vm_area_struct *vma, unsigned long addr,
345 		unsigned long end, pgprot_t newprot, unsigned long cp_flags)
346 {
347 	struct mm_struct *mm = vma->vm_mm;
348 	pgd_t *pgd;
349 	unsigned long next;
350 	unsigned long pages = 0;
351 
352 	BUG_ON(addr >= end);
353 	pgd = pgd_offset(mm, addr);
354 	tlb_start_vma(tlb, vma);
355 	do {
356 		next = pgd_addr_end(addr, end);
357 		if (pgd_none_or_clear_bad(pgd))
358 			continue;
359 		pages += change_p4d_range(tlb, vma, pgd, addr, next, newprot,
360 					  cp_flags);
361 	} while (pgd++, addr = next, addr != end);
362 
363 	tlb_end_vma(tlb, vma);
364 
365 	return pages;
366 }
367 
change_protection(struct mmu_gather * tlb,struct vm_area_struct * vma,unsigned long start,unsigned long end,pgprot_t newprot,unsigned long cp_flags)368 unsigned long change_protection(struct mmu_gather *tlb,
369 		       struct vm_area_struct *vma, unsigned long start,
370 		       unsigned long end, pgprot_t newprot,
371 		       unsigned long cp_flags)
372 {
373 	unsigned long pages;
374 
375 	BUG_ON((cp_flags & MM_CP_UFFD_WP_ALL) == MM_CP_UFFD_WP_ALL);
376 
377 	if (is_vm_hugetlb_page(vma))
378 		pages = hugetlb_change_protection(vma, start, end, newprot);
379 	else
380 		pages = change_protection_range(tlb, vma, start, end, newprot,
381 						cp_flags);
382 
383 	return pages;
384 }
385 
prot_none_pte_entry(pte_t * pte,unsigned long addr,unsigned long next,struct mm_walk * walk)386 static int prot_none_pte_entry(pte_t *pte, unsigned long addr,
387 			       unsigned long next, struct mm_walk *walk)
388 {
389 	return pfn_modify_allowed(pte_pfn(*pte), *(pgprot_t *)(walk->private)) ?
390 		0 : -EACCES;
391 }
392 
prot_none_hugetlb_entry(pte_t * pte,unsigned long hmask,unsigned long addr,unsigned long next,struct mm_walk * walk)393 static int prot_none_hugetlb_entry(pte_t *pte, unsigned long hmask,
394 				   unsigned long addr, unsigned long next,
395 				   struct mm_walk *walk)
396 {
397 	return pfn_modify_allowed(pte_pfn(*pte), *(pgprot_t *)(walk->private)) ?
398 		0 : -EACCES;
399 }
400 
prot_none_test(unsigned long addr,unsigned long next,struct mm_walk * walk)401 static int prot_none_test(unsigned long addr, unsigned long next,
402 			  struct mm_walk *walk)
403 {
404 	return 0;
405 }
406 
407 static const struct mm_walk_ops prot_none_walk_ops = {
408 	.pte_entry		= prot_none_pte_entry,
409 	.hugetlb_entry		= prot_none_hugetlb_entry,
410 	.test_walk		= prot_none_test,
411 };
412 
413 int
mprotect_fixup(struct mmu_gather * tlb,struct vm_area_struct * vma,struct vm_area_struct ** pprev,unsigned long start,unsigned long end,unsigned long newflags)414 mprotect_fixup(struct mmu_gather *tlb, struct vm_area_struct *vma,
415 	       struct vm_area_struct **pprev, unsigned long start,
416 	       unsigned long end, unsigned long newflags)
417 {
418 	struct mm_struct *mm = vma->vm_mm;
419 	unsigned long oldflags = vma->vm_flags;
420 	long nrpages = (end - start) >> PAGE_SHIFT;
421 	unsigned long charged = 0;
422 	pgoff_t pgoff;
423 	int error;
424 	int dirty_accountable = 0;
425 
426 	if (newflags == oldflags) {
427 		*pprev = vma;
428 		return 0;
429 	}
430 
431 	/*
432 	 * Do PROT_NONE PFN permission checks here when we can still
433 	 * bail out without undoing a lot of state. This is a rather
434 	 * uncommon case, so doesn't need to be very optimized.
435 	 */
436 	if (arch_has_pfn_modify_check() &&
437 	    (vma->vm_flags & (VM_PFNMAP|VM_MIXEDMAP)) &&
438 	    (newflags & VM_ACCESS_FLAGS) == 0) {
439 		pgprot_t new_pgprot = vm_get_page_prot(newflags);
440 
441 		error = walk_page_range(current->mm, start, end,
442 				&prot_none_walk_ops, &new_pgprot);
443 		if (error)
444 			return error;
445 	}
446 
447 	/*
448 	 * If we make a private mapping writable we increase our commit;
449 	 * but (without finer accounting) cannot reduce our commit if we
450 	 * make it unwritable again. hugetlb mapping were accounted for
451 	 * even if read-only so there is no need to account for them here
452 	 */
453 	if (newflags & VM_WRITE) {
454 		/* Check space limits when area turns into data. */
455 		if (!may_expand_vm(mm, newflags, nrpages) &&
456 				may_expand_vm(mm, oldflags, nrpages))
457 			return -ENOMEM;
458 		if (!(oldflags & (VM_ACCOUNT|VM_WRITE|VM_HUGETLB|
459 						VM_SHARED|VM_NORESERVE))) {
460 			charged = nrpages;
461 			if (security_vm_enough_memory_mm(mm, charged))
462 				return -ENOMEM;
463 			newflags |= VM_ACCOUNT;
464 		}
465 	}
466 
467 	/*
468 	 * First try to merge with previous and/or next vma.
469 	 */
470 	pgoff = vma->vm_pgoff + ((start - vma->vm_start) >> PAGE_SHIFT);
471 	*pprev = vma_merge(mm, *pprev, start, end, newflags,
472 			   vma->anon_vma, vma->vm_file, pgoff, vma_policy(vma),
473 			   vma->vm_userfaultfd_ctx, anon_vma_name(vma));
474 	if (*pprev) {
475 		vma = *pprev;
476 		VM_WARN_ON((vma->vm_flags ^ newflags) & ~VM_SOFTDIRTY);
477 		goto success;
478 	}
479 
480 	*pprev = vma;
481 
482 	if (start != vma->vm_start) {
483 		error = split_vma(mm, vma, start, 1);
484 		if (error)
485 			goto fail;
486 	}
487 
488 	if (end != vma->vm_end) {
489 		error = split_vma(mm, vma, end, 0);
490 		if (error)
491 			goto fail;
492 	}
493 
494 success:
495 	/*
496 	 * vm_flags and vm_page_prot are protected by the mmap_lock
497 	 * held in write mode.
498 	 */
499 	vma->vm_flags = newflags;
500 	dirty_accountable = vma_wants_writenotify(vma, vma->vm_page_prot);
501 	vma_set_page_prot(vma);
502 
503 	change_protection(tlb, vma, start, end, vma->vm_page_prot,
504 			  dirty_accountable ? MM_CP_DIRTY_ACCT : 0);
505 
506 	/*
507 	 * Private VM_LOCKED VMA becoming writable: trigger COW to avoid major
508 	 * fault on access.
509 	 */
510 	if ((oldflags & (VM_WRITE | VM_SHARED | VM_LOCKED)) == VM_LOCKED &&
511 			(newflags & VM_WRITE)) {
512 		populate_vma_page_range(vma, start, end, NULL);
513 	}
514 
515 	vm_stat_account(mm, oldflags, -nrpages);
516 	vm_stat_account(mm, newflags, nrpages);
517 	perf_event_mmap(vma);
518 	return 0;
519 
520 fail:
521 	vm_unacct_memory(charged);
522 	return error;
523 }
524 
525 /*
526  * pkey==-1 when doing a legacy mprotect()
527  */
do_mprotect_pkey(unsigned long start,size_t len,unsigned long prot,int pkey)528 static int do_mprotect_pkey(unsigned long start, size_t len,
529 		unsigned long prot, int pkey)
530 {
531 	unsigned long nstart, end, tmp, reqprot;
532 	struct vm_area_struct *vma, *prev;
533 	int error = -EINVAL;
534 	const int grows = prot & (PROT_GROWSDOWN|PROT_GROWSUP);
535 	const bool rier = (current->personality & READ_IMPLIES_EXEC) &&
536 				(prot & PROT_READ);
537 	struct mmu_gather tlb;
538 
539 	start = untagged_addr(start);
540 
541 	prot &= ~(PROT_GROWSDOWN|PROT_GROWSUP);
542 	if (grows == (PROT_GROWSDOWN|PROT_GROWSUP)) /* can't be both */
543 		return -EINVAL;
544 
545 	if (start & ~PAGE_MASK)
546 		return -EINVAL;
547 	if (!len)
548 		return 0;
549 	len = PAGE_ALIGN(len);
550 	end = start + len;
551 	if (end <= start)
552 		return -ENOMEM;
553 	if (!arch_validate_prot(prot, start))
554 		return -EINVAL;
555 
556 	reqprot = prot;
557 
558 	if (mmap_write_lock_killable(current->mm))
559 		return -EINTR;
560 
561 	/*
562 	 * If userspace did not allocate the pkey, do not let
563 	 * them use it here.
564 	 */
565 	error = -EINVAL;
566 	if ((pkey != -1) && !mm_pkey_is_allocated(current->mm, pkey))
567 		goto out;
568 
569 	vma = find_vma(current->mm, start);
570 	error = -ENOMEM;
571 	if (!vma)
572 		goto out;
573 	prev = vma->vm_prev;
574 	if (unlikely(grows & PROT_GROWSDOWN)) {
575 		if (vma->vm_start >= end)
576 			goto out;
577 		start = vma->vm_start;
578 		error = -EINVAL;
579 		if (!(vma->vm_flags & VM_GROWSDOWN))
580 			goto out;
581 	} else {
582 		if (vma->vm_start > start)
583 			goto out;
584 		if (unlikely(grows & PROT_GROWSUP)) {
585 			end = vma->vm_end;
586 			error = -EINVAL;
587 			if (!(vma->vm_flags & VM_GROWSUP))
588 				goto out;
589 		}
590 	}
591 	if (start > vma->vm_start)
592 		prev = vma;
593 
594 	tlb_gather_mmu(&tlb, current->mm);
595 	for (nstart = start ; ; ) {
596 		unsigned long mask_off_old_flags;
597 		unsigned long newflags;
598 		int new_vma_pkey;
599 
600 		/* Here we know that vma->vm_start <= nstart < vma->vm_end. */
601 
602 		/* Does the application expect PROT_READ to imply PROT_EXEC */
603 		if (rier && (vma->vm_flags & VM_MAYEXEC))
604 			prot |= PROT_EXEC;
605 
606 		/*
607 		 * Each mprotect() call explicitly passes r/w/x permissions.
608 		 * If a permission is not passed to mprotect(), it must be
609 		 * cleared from the VMA.
610 		 */
611 		mask_off_old_flags = VM_READ | VM_WRITE | VM_EXEC |
612 					VM_FLAGS_CLEAR;
613 
614 		new_vma_pkey = arch_override_mprotect_pkey(vma, prot, pkey);
615 		newflags = calc_vm_prot_bits(prot, new_vma_pkey);
616 		newflags |= (vma->vm_flags & ~mask_off_old_flags);
617 
618 		/* newflags >> 4 shift VM_MAY% in place of VM_% */
619 		if ((newflags & ~(newflags >> 4)) & VM_ACCESS_FLAGS) {
620 			error = -EACCES;
621 			break;
622 		}
623 
624 		/* Allow architectures to sanity-check the new flags */
625 		if (!arch_validate_flags(newflags)) {
626 			error = -EINVAL;
627 			break;
628 		}
629 
630 		error = security_file_mprotect(vma, reqprot, prot);
631 		if (error)
632 			break;
633 
634 		tmp = vma->vm_end;
635 		if (tmp > end)
636 			tmp = end;
637 
638 		if (vma->vm_ops && vma->vm_ops->mprotect) {
639 			error = vma->vm_ops->mprotect(vma, nstart, tmp, newflags);
640 			if (error)
641 				break;
642 		}
643 
644 		error = mprotect_fixup(&tlb, vma, &prev, nstart, tmp, newflags);
645 		if (error)
646 			break;
647 
648 		nstart = tmp;
649 
650 		if (nstart < prev->vm_end)
651 			nstart = prev->vm_end;
652 		if (nstart >= end)
653 			break;
654 
655 		vma = prev->vm_next;
656 		if (!vma || vma->vm_start != nstart) {
657 			error = -ENOMEM;
658 			break;
659 		}
660 		prot = reqprot;
661 	}
662 	tlb_finish_mmu(&tlb);
663 out:
664 	mmap_write_unlock(current->mm);
665 	return error;
666 }
667 
SYSCALL_DEFINE3(mprotect,unsigned long,start,size_t,len,unsigned long,prot)668 SYSCALL_DEFINE3(mprotect, unsigned long, start, size_t, len,
669 		unsigned long, prot)
670 {
671 	return do_mprotect_pkey(start, len, prot, -1);
672 }
673 
674 #ifdef CONFIG_ARCH_HAS_PKEYS
675 
SYSCALL_DEFINE4(pkey_mprotect,unsigned long,start,size_t,len,unsigned long,prot,int,pkey)676 SYSCALL_DEFINE4(pkey_mprotect, unsigned long, start, size_t, len,
677 		unsigned long, prot, int, pkey)
678 {
679 	return do_mprotect_pkey(start, len, prot, pkey);
680 }
681 
SYSCALL_DEFINE2(pkey_alloc,unsigned long,flags,unsigned long,init_val)682 SYSCALL_DEFINE2(pkey_alloc, unsigned long, flags, unsigned long, init_val)
683 {
684 	int pkey;
685 	int ret;
686 
687 	/* No flags supported yet. */
688 	if (flags)
689 		return -EINVAL;
690 	/* check for unsupported init values */
691 	if (init_val & ~PKEY_ACCESS_MASK)
692 		return -EINVAL;
693 
694 	mmap_write_lock(current->mm);
695 	pkey = mm_pkey_alloc(current->mm);
696 
697 	ret = -ENOSPC;
698 	if (pkey == -1)
699 		goto out;
700 
701 	ret = arch_set_user_pkey_access(current, pkey, init_val);
702 	if (ret) {
703 		mm_pkey_free(current->mm, pkey);
704 		goto out;
705 	}
706 	ret = pkey;
707 out:
708 	mmap_write_unlock(current->mm);
709 	return ret;
710 }
711 
SYSCALL_DEFINE1(pkey_free,int,pkey)712 SYSCALL_DEFINE1(pkey_free, int, pkey)
713 {
714 	int ret;
715 
716 	mmap_write_lock(current->mm);
717 	ret = mm_pkey_free(current->mm, pkey);
718 	mmap_write_unlock(current->mm);
719 
720 	/*
721 	 * We could provide warnings or errors if any VMA still
722 	 * has the pkey set here.
723 	 */
724 	return ret;
725 }
726 
727 #endif /* CONFIG_ARCH_HAS_PKEYS */
728