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/pgsize_migration.h>
21 #include <linux/personality.h>
22 #include <linux/syscalls.h>
23 #include <linux/swap.h>
24 #include <linux/swapops.h>
25 #include <linux/mmu_notifier.h>
26 #include <linux/migrate.h>
27 #include <linux/perf_event.h>
28 #include <linux/pkeys.h>
29 #include <linux/ksm.h>
30 #include <linux/uaccess.h>
31 #include <linux/mm_inline.h>
32 #include <linux/pgtable.h>
33 #include <asm/cacheflush.h>
34 #include <asm/mmu_context.h>
35 #include <asm/tlbflush.h>
36
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_count(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_writable_migration_entry(entry)) {
148 /*
149 * A protection check is difficult so
150 * just be safe and disable write
151 */
152 entry = make_readable_migration_entry(
153 swp_offset(entry));
154 newpte = swp_entry_to_pte(entry);
155 if (pte_swp_soft_dirty(oldpte))
156 newpte = pte_swp_mksoft_dirty(newpte);
157 if (pte_swp_uffd_wp(oldpte))
158 newpte = pte_swp_mkuffd_wp(newpte);
159 } else if (is_writable_device_private_entry(entry)) {
160 /*
161 * We do not preserve soft-dirtiness. See
162 * copy_one_pte() for explanation.
163 */
164 entry = make_readable_device_private_entry(
165 swp_offset(entry));
166 newpte = swp_entry_to_pte(entry);
167 if (pte_swp_uffd_wp(oldpte))
168 newpte = pte_swp_mkuffd_wp(newpte);
169 } else if (is_writable_device_exclusive_entry(entry)) {
170 entry = make_readable_device_exclusive_entry(
171 swp_offset(entry));
172 newpte = swp_entry_to_pte(entry);
173 if (pte_swp_soft_dirty(oldpte))
174 newpte = pte_swp_mksoft_dirty(newpte);
175 if (pte_swp_uffd_wp(oldpte))
176 newpte = pte_swp_mkuffd_wp(newpte);
177 } else {
178 newpte = oldpte;
179 }
180
181 if (uffd_wp)
182 newpte = pte_swp_mkuffd_wp(newpte);
183 else if (uffd_wp_resolve)
184 newpte = pte_swp_clear_uffd_wp(newpte);
185
186 if (!pte_same(oldpte, newpte)) {
187 set_pte_at(vma->vm_mm, addr, pte, newpte);
188 pages++;
189 }
190 }
191 } while (pte++, addr += PAGE_SIZE, addr != end);
192 arch_leave_lazy_mmu_mode();
193 pte_unmap_unlock(pte - 1, ptl);
194
195 return pages;
196 }
197
198 /*
199 * Used when setting automatic NUMA hinting protection where it is
200 * critical that a numa hinting PMD is not confused with a bad PMD.
201 */
pmd_none_or_clear_bad_unless_trans_huge(pmd_t * pmd)202 static inline int pmd_none_or_clear_bad_unless_trans_huge(pmd_t *pmd)
203 {
204 pmd_t pmdval = pmd_read_atomic(pmd);
205
206 /* See pmd_none_or_trans_huge_or_clear_bad for info on barrier */
207 #ifdef CONFIG_TRANSPARENT_HUGEPAGE
208 barrier();
209 #endif
210
211 if (pmd_none(pmdval))
212 return 1;
213 if (pmd_trans_huge(pmdval))
214 return 0;
215 if (unlikely(pmd_bad(pmdval))) {
216 pmd_clear_bad(pmd);
217 return 1;
218 }
219
220 return 0;
221 }
222
change_pmd_range(struct vm_area_struct * vma,pud_t * pud,unsigned long addr,unsigned long end,pgprot_t newprot,unsigned long cp_flags)223 static inline unsigned long change_pmd_range(struct vm_area_struct *vma,
224 pud_t *pud, unsigned long addr, unsigned long end,
225 pgprot_t newprot, unsigned long cp_flags)
226 {
227 pmd_t *pmd;
228 unsigned long next;
229 unsigned long pages = 0;
230 unsigned long nr_huge_updates = 0;
231 struct mmu_notifier_range range;
232
233 range.start = 0;
234
235 pmd = pmd_offset(pud, addr);
236 do {
237 unsigned long this_pages;
238
239 next = pmd_addr_end(addr, end);
240
241 /*
242 * Automatic NUMA balancing walks the tables with mmap_lock
243 * held for read. It's possible a parallel update to occur
244 * between pmd_trans_huge() and a pmd_none_or_clear_bad()
245 * check leading to a false positive and clearing.
246 * Hence, it's necessary to atomically read the PMD value
247 * for all the checks.
248 */
249 if (!is_swap_pmd(*pmd) && !pmd_devmap(*pmd) &&
250 pmd_none_or_clear_bad_unless_trans_huge(pmd))
251 goto next;
252
253 /* invoke the mmu notifier if the pmd is populated */
254 if (!range.start) {
255 mmu_notifier_range_init(&range,
256 MMU_NOTIFY_PROTECTION_VMA, 0,
257 vma, vma->vm_mm, addr, end);
258 mmu_notifier_invalidate_range_start(&range);
259 }
260
261 if (is_swap_pmd(*pmd) || pmd_trans_huge(*pmd) || pmd_devmap(*pmd)) {
262 if (next - addr != HPAGE_PMD_SIZE) {
263 __split_huge_pmd(vma, pmd, addr, false, NULL);
264 } else {
265 int nr_ptes = change_huge_pmd(vma, pmd, addr,
266 newprot, cp_flags);
267
268 if (nr_ptes) {
269 if (nr_ptes == HPAGE_PMD_NR) {
270 pages += HPAGE_PMD_NR;
271 nr_huge_updates++;
272 }
273
274 /* huge pmd was handled */
275 goto next;
276 }
277 }
278 /* fall through, the trans huge pmd just split */
279 }
280 this_pages = change_pte_range(vma, pmd, addr, next, newprot,
281 cp_flags);
282 pages += this_pages;
283 next:
284 cond_resched();
285 } while (pmd++, addr = next, addr != end);
286
287 if (range.start)
288 mmu_notifier_invalidate_range_end(&range);
289
290 if (nr_huge_updates)
291 count_vm_numa_events(NUMA_HUGE_PTE_UPDATES, nr_huge_updates);
292 return pages;
293 }
294
change_pud_range(struct vm_area_struct * vma,p4d_t * p4d,unsigned long addr,unsigned long end,pgprot_t newprot,unsigned long cp_flags)295 static inline unsigned long change_pud_range(struct vm_area_struct *vma,
296 p4d_t *p4d, unsigned long addr, unsigned long end,
297 pgprot_t newprot, unsigned long cp_flags)
298 {
299 pud_t *pud;
300 unsigned long next;
301 unsigned long pages = 0;
302
303 pud = pud_offset(p4d, addr);
304 do {
305 next = pud_addr_end(addr, end);
306 if (pud_none_or_clear_bad(pud))
307 continue;
308 pages += change_pmd_range(vma, pud, addr, next, newprot,
309 cp_flags);
310 } while (pud++, addr = next, addr != end);
311
312 return pages;
313 }
314
change_p4d_range(struct vm_area_struct * vma,pgd_t * pgd,unsigned long addr,unsigned long end,pgprot_t newprot,unsigned long cp_flags)315 static inline unsigned long change_p4d_range(struct vm_area_struct *vma,
316 pgd_t *pgd, unsigned long addr, unsigned long end,
317 pgprot_t newprot, unsigned long cp_flags)
318 {
319 p4d_t *p4d;
320 unsigned long next;
321 unsigned long pages = 0;
322
323 p4d = p4d_offset(pgd, addr);
324 do {
325 next = p4d_addr_end(addr, end);
326 if (p4d_none_or_clear_bad(p4d))
327 continue;
328 pages += change_pud_range(vma, p4d, addr, next, newprot,
329 cp_flags);
330 } while (p4d++, addr = next, addr != end);
331
332 return pages;
333 }
334
change_protection_range(struct vm_area_struct * vma,unsigned long addr,unsigned long end,pgprot_t newprot,unsigned long cp_flags)335 static unsigned long change_protection_range(struct vm_area_struct *vma,
336 unsigned long addr, unsigned long end, pgprot_t newprot,
337 unsigned long cp_flags)
338 {
339 struct mm_struct *mm = vma->vm_mm;
340 pgd_t *pgd;
341 unsigned long next;
342 unsigned long start = addr;
343 unsigned long pages = 0;
344
345 BUG_ON(addr >= end);
346 pgd = pgd_offset(mm, addr);
347 flush_cache_range(vma, addr, end);
348 inc_tlb_flush_pending(mm);
349 do {
350 next = pgd_addr_end(addr, end);
351 if (pgd_none_or_clear_bad(pgd))
352 continue;
353 pages += change_p4d_range(vma, pgd, addr, next, newprot,
354 cp_flags);
355 } while (pgd++, addr = next, addr != end);
356
357 /* Only flush the TLB if we actually modified any entries: */
358 if (pages)
359 flush_tlb_range(vma, start, end);
360 dec_tlb_flush_pending(mm);
361
362 return pages;
363 }
364
change_protection(struct vm_area_struct * vma,unsigned long start,unsigned long end,pgprot_t newprot,unsigned long cp_flags)365 unsigned long change_protection(struct vm_area_struct *vma, unsigned long start,
366 unsigned long end, pgprot_t newprot,
367 unsigned long cp_flags)
368 {
369 unsigned long pages;
370
371 BUG_ON((cp_flags & MM_CP_UFFD_WP_ALL) == MM_CP_UFFD_WP_ALL);
372
373 if (is_vm_hugetlb_page(vma))
374 pages = hugetlb_change_protection(vma, start, end, newprot);
375 else
376 pages = change_protection_range(vma, start, end, newprot,
377 cp_flags);
378
379 return pages;
380 }
381
prot_none_pte_entry(pte_t * pte,unsigned long addr,unsigned long next,struct mm_walk * walk)382 static int prot_none_pte_entry(pte_t *pte, unsigned long addr,
383 unsigned long next, struct mm_walk *walk)
384 {
385 return pfn_modify_allowed(pte_pfn(*pte), *(pgprot_t *)(walk->private)) ?
386 0 : -EACCES;
387 }
388
prot_none_hugetlb_entry(pte_t * pte,unsigned long hmask,unsigned long addr,unsigned long next,struct mm_walk * walk)389 static int prot_none_hugetlb_entry(pte_t *pte, unsigned long hmask,
390 unsigned long addr, unsigned long next,
391 struct mm_walk *walk)
392 {
393 return pfn_modify_allowed(pte_pfn(*pte), *(pgprot_t *)(walk->private)) ?
394 0 : -EACCES;
395 }
396
prot_none_test(unsigned long addr,unsigned long next,struct mm_walk * walk)397 static int prot_none_test(unsigned long addr, unsigned long next,
398 struct mm_walk *walk)
399 {
400 return 0;
401 }
402
403 static const struct mm_walk_ops prot_none_walk_ops = {
404 .pte_entry = prot_none_pte_entry,
405 .hugetlb_entry = prot_none_hugetlb_entry,
406 .test_walk = prot_none_test,
407 };
408
409 int
mprotect_fixup(struct vm_area_struct * vma,struct vm_area_struct ** pprev,unsigned long start,unsigned long end,unsigned long newflags)410 mprotect_fixup(struct vm_area_struct *vma, struct vm_area_struct **pprev,
411 unsigned long start, unsigned long end, unsigned long newflags)
412 {
413 struct mm_struct *mm = vma->vm_mm;
414 unsigned long oldflags = vma->vm_flags;
415 long nrpages = (end - start) >> PAGE_SHIFT;
416 unsigned long charged = 0;
417 pgoff_t pgoff;
418 int error;
419 int dirty_accountable = 0;
420
421 if (newflags == oldflags) {
422 *pprev = vma;
423 return 0;
424 }
425
426 /*
427 * Do PROT_NONE PFN permission checks here when we can still
428 * bail out without undoing a lot of state. This is a rather
429 * uncommon case, so doesn't need to be very optimized.
430 */
431 if (arch_has_pfn_modify_check() &&
432 (vma->vm_flags & (VM_PFNMAP|VM_MIXEDMAP)) &&
433 (newflags & VM_ACCESS_FLAGS) == 0) {
434 pgprot_t new_pgprot = vm_get_page_prot(newflags);
435
436 error = walk_page_range(current->mm, start, end,
437 &prot_none_walk_ops, &new_pgprot);
438 if (error)
439 return error;
440 }
441
442 /*
443 * If we make a private mapping writable we increase our commit;
444 * but (without finer accounting) cannot reduce our commit if we
445 * make it unwritable again. hugetlb mapping were accounted for
446 * even if read-only so there is no need to account for them here
447 */
448 if (newflags & VM_WRITE) {
449 /* Check space limits when area turns into data. */
450 if (!may_expand_vm(mm, newflags, nrpages) &&
451 may_expand_vm(mm, oldflags, nrpages))
452 return -ENOMEM;
453 if (!(oldflags & (VM_ACCOUNT|VM_WRITE|VM_HUGETLB|
454 VM_SHARED|VM_NORESERVE))) {
455 charged = nrpages;
456 if (security_vm_enough_memory_mm(mm, charged))
457 return -ENOMEM;
458 newflags |= VM_ACCOUNT;
459 }
460 }
461
462 /*
463 * First try to merge with previous and/or next vma.
464 */
465 pgoff = vma->vm_pgoff + ((start - vma->vm_start) >> PAGE_SHIFT);
466 *pprev = vma_merge(mm, *pprev, start, end, newflags,
467 vma->anon_vma, vma->vm_file, pgoff, vma_policy(vma),
468 vma->vm_userfaultfd_ctx, anon_vma_name(vma));
469 if (*pprev) {
470 vma = *pprev;
471 VM_WARN_ON((vma->vm_flags ^ newflags) & ~VM_SOFTDIRTY);
472 goto success;
473 }
474
475 *pprev = vma;
476
477 if (start != vma->vm_start) {
478 error = split_vma(mm, vma, start, 1);
479 if (error)
480 goto fail;
481 }
482
483 if (end != vma->vm_end) {
484 error = split_vma(mm, vma, end, 0);
485 if (error)
486 goto fail;
487 }
488
489 success:
490 /*
491 * vm_flags and vm_page_prot are protected by the mmap_lock
492 * held in write mode.
493 */
494 vma->vm_flags = vma_pad_fixup_flags(vma, newflags);
495
496 dirty_accountable = vma_wants_writenotify(vma, vma->vm_page_prot);
497 vma_set_page_prot(vma);
498
499 change_protection(vma, start, end, vma->vm_page_prot,
500 dirty_accountable ? MM_CP_DIRTY_ACCT : 0);
501
502 /*
503 * Private VM_LOCKED VMA becoming writable: trigger COW to avoid major
504 * fault on access.
505 */
506 if ((oldflags & (VM_WRITE | VM_SHARED | VM_LOCKED)) == VM_LOCKED &&
507 (newflags & VM_WRITE)) {
508 populate_vma_page_range(vma, start, end, NULL);
509 }
510
511 vm_stat_account(mm, oldflags, -nrpages);
512 vm_stat_account(mm, newflags, nrpages);
513 perf_event_mmap(vma);
514 return 0;
515
516 fail:
517 vm_unacct_memory(charged);
518 return error;
519 }
520
521 /*
522 * pkey==-1 when doing a legacy mprotect()
523 */
do_mprotect_pkey(unsigned long start,size_t len,unsigned long prot,int pkey)524 static int do_mprotect_pkey(unsigned long start, size_t len,
525 unsigned long prot, int pkey)
526 {
527 unsigned long nstart, end, tmp, reqprot;
528 struct vm_area_struct *vma, *prev;
529 int error = -EINVAL;
530 const int grows = prot & (PROT_GROWSDOWN|PROT_GROWSUP);
531 const bool rier = (current->personality & READ_IMPLIES_EXEC) &&
532 (prot & PROT_READ);
533
534 start = untagged_addr(start);
535
536 prot &= ~(PROT_GROWSDOWN|PROT_GROWSUP);
537 if (grows == (PROT_GROWSDOWN|PROT_GROWSUP)) /* can't be both */
538 return -EINVAL;
539
540 if (start & ~PAGE_MASK)
541 return -EINVAL;
542 if (!len)
543 return 0;
544 len = PAGE_ALIGN(len);
545 end = start + len;
546 if (end <= start)
547 return -ENOMEM;
548 if (!arch_validate_prot(prot, start))
549 return -EINVAL;
550
551 reqprot = prot;
552
553 if (mmap_write_lock_killable(current->mm))
554 return -EINTR;
555
556 /*
557 * If userspace did not allocate the pkey, do not let
558 * them use it here.
559 */
560 error = -EINVAL;
561 if ((pkey != -1) && !mm_pkey_is_allocated(current->mm, pkey))
562 goto out;
563
564 vma = find_vma(current->mm, start);
565 error = -ENOMEM;
566 if (!vma)
567 goto out;
568 prev = vma->vm_prev;
569 if (unlikely(grows & PROT_GROWSDOWN)) {
570 if (vma->vm_start >= end)
571 goto out;
572 start = vma->vm_start;
573 error = -EINVAL;
574 if (!(vma->vm_flags & VM_GROWSDOWN))
575 goto out;
576 } else {
577 if (vma->vm_start > start)
578 goto out;
579 if (unlikely(grows & PROT_GROWSUP)) {
580 end = vma->vm_end;
581 error = -EINVAL;
582 if (!(vma->vm_flags & VM_GROWSUP))
583 goto out;
584 }
585 }
586 if (start > vma->vm_start)
587 prev = vma;
588
589 for (nstart = start ; ; ) {
590 unsigned long mask_off_old_flags;
591 unsigned long newflags;
592 int new_vma_pkey;
593
594 /* Here we know that vma->vm_start <= nstart < vma->vm_end. */
595
596 /* Does the application expect PROT_READ to imply PROT_EXEC */
597 if (rier && (vma->vm_flags & VM_MAYEXEC))
598 prot |= PROT_EXEC;
599
600 /*
601 * Each mprotect() call explicitly passes r/w/x permissions.
602 * If a permission is not passed to mprotect(), it must be
603 * cleared from the VMA.
604 */
605 mask_off_old_flags = VM_READ | VM_WRITE | VM_EXEC |
606 VM_FLAGS_CLEAR;
607
608 new_vma_pkey = arch_override_mprotect_pkey(vma, prot, pkey);
609 newflags = calc_vm_prot_bits(prot, new_vma_pkey);
610 newflags |= (vma->vm_flags & ~mask_off_old_flags);
611
612 /* newflags >> 4 shift VM_MAY% in place of VM_% */
613 if ((newflags & ~(newflags >> 4)) & VM_ACCESS_FLAGS) {
614 error = -EACCES;
615 goto out;
616 }
617
618 /* Allow architectures to sanity-check the new flags */
619 if (!arch_validate_flags(newflags)) {
620 error = -EINVAL;
621 goto out;
622 }
623
624 error = security_file_mprotect(vma, reqprot, prot);
625 if (error)
626 goto out;
627
628 tmp = vma->vm_end;
629 if (tmp > end)
630 tmp = end;
631
632 if (vma->vm_ops && vma->vm_ops->mprotect) {
633 error = vma->vm_ops->mprotect(vma, nstart, tmp, newflags);
634 if (error)
635 goto out;
636 }
637
638 error = mprotect_fixup(vma, &prev, nstart, tmp, newflags);
639 if (error)
640 goto out;
641
642 nstart = tmp;
643
644 if (nstart < prev->vm_end)
645 nstart = prev->vm_end;
646 if (nstart >= end)
647 goto out;
648
649 vma = prev->vm_next;
650 if (!vma || vma->vm_start != nstart) {
651 error = -ENOMEM;
652 goto out;
653 }
654 prot = reqprot;
655 }
656 out:
657 mmap_write_unlock(current->mm);
658 return error;
659 }
660
SYSCALL_DEFINE3(mprotect,unsigned long,start,size_t,len,unsigned long,prot)661 SYSCALL_DEFINE3(mprotect, unsigned long, start, size_t, len,
662 unsigned long, prot)
663 {
664 return do_mprotect_pkey(start, len, prot, -1);
665 }
666
667 #ifdef CONFIG_ARCH_HAS_PKEYS
668
SYSCALL_DEFINE4(pkey_mprotect,unsigned long,start,size_t,len,unsigned long,prot,int,pkey)669 SYSCALL_DEFINE4(pkey_mprotect, unsigned long, start, size_t, len,
670 unsigned long, prot, int, pkey)
671 {
672 return do_mprotect_pkey(start, len, prot, pkey);
673 }
674
SYSCALL_DEFINE2(pkey_alloc,unsigned long,flags,unsigned long,init_val)675 SYSCALL_DEFINE2(pkey_alloc, unsigned long, flags, unsigned long, init_val)
676 {
677 int pkey;
678 int ret;
679
680 /* No flags supported yet. */
681 if (flags)
682 return -EINVAL;
683 /* check for unsupported init values */
684 if (init_val & ~PKEY_ACCESS_MASK)
685 return -EINVAL;
686
687 mmap_write_lock(current->mm);
688 pkey = mm_pkey_alloc(current->mm);
689
690 ret = -ENOSPC;
691 if (pkey == -1)
692 goto out;
693
694 ret = arch_set_user_pkey_access(current, pkey, init_val);
695 if (ret) {
696 mm_pkey_free(current->mm, pkey);
697 goto out;
698 }
699 ret = pkey;
700 out:
701 mmap_write_unlock(current->mm);
702 return ret;
703 }
704
SYSCALL_DEFINE1(pkey_free,int,pkey)705 SYSCALL_DEFINE1(pkey_free, int, pkey)
706 {
707 int ret;
708
709 mmap_write_lock(current->mm);
710 ret = mm_pkey_free(current->mm, pkey);
711 mmap_write_unlock(current->mm);
712
713 /*
714 * We could provide warnings or errors if any VMA still
715 * has the pkey set here.
716 */
717 return ret;
718 }
719
720 #endif /* CONFIG_ARCH_HAS_PKEYS */
721