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