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