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