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