xref: /openbmc/linux/mm/mprotect.c (revision 7d06d9c9bd813fc956b9c7bffc1b9724009983eb)
11da177e4SLinus Torvalds /*
21da177e4SLinus Torvalds  *  mm/mprotect.c
31da177e4SLinus Torvalds  *
41da177e4SLinus Torvalds  *  (C) Copyright 1994 Linus Torvalds
51da177e4SLinus Torvalds  *  (C) Copyright 2002 Christoph Hellwig
61da177e4SLinus Torvalds  *
7046c6884SAlan Cox  *  Address space accounting code	<alan@lxorguk.ukuu.org.uk>
81da177e4SLinus Torvalds  *  (C) Copyright 2002 Red Hat Inc, All Rights Reserved
91da177e4SLinus Torvalds  */
101da177e4SLinus Torvalds 
111da177e4SLinus Torvalds #include <linux/mm.h>
121da177e4SLinus Torvalds #include <linux/hugetlb.h>
131da177e4SLinus Torvalds #include <linux/shm.h>
141da177e4SLinus Torvalds #include <linux/mman.h>
151da177e4SLinus Torvalds #include <linux/fs.h>
161da177e4SLinus Torvalds #include <linux/highmem.h>
171da177e4SLinus Torvalds #include <linux/security.h>
181da177e4SLinus Torvalds #include <linux/mempolicy.h>
191da177e4SLinus Torvalds #include <linux/personality.h>
201da177e4SLinus Torvalds #include <linux/syscalls.h>
210697212aSChristoph Lameter #include <linux/swap.h>
220697212aSChristoph Lameter #include <linux/swapops.h>
23cddb8a5cSAndrea Arcangeli #include <linux/mmu_notifier.h>
2464cdd548SKOSAKI Motohiro #include <linux/migrate.h>
25cdd6c482SIngo Molnar #include <linux/perf_event.h>
2664a9a34eSMel Gorman #include <linux/ksm.h>
2762b5f7d0SDave Hansen #include <linux/pkeys.h>
281da177e4SLinus Torvalds #include <asm/uaccess.h>
291da177e4SLinus Torvalds #include <asm/pgtable.h>
301da177e4SLinus Torvalds #include <asm/cacheflush.h>
311da177e4SLinus Torvalds #include <asm/tlbflush.h>
321da177e4SLinus Torvalds 
3336f88188SKirill A. Shutemov #include "internal.h"
3436f88188SKirill A. Shutemov 
351ad9f620SMel Gorman /*
361ad9f620SMel Gorman  * For a prot_numa update we only hold mmap_sem for read so there is a
371ad9f620SMel Gorman  * potential race with faulting where a pmd was temporarily none. This
381ad9f620SMel Gorman  * function checks for a transhuge pmd under the appropriate lock. It
391ad9f620SMel Gorman  * returns a pte if it was successfully locked or NULL if it raced with
401ad9f620SMel Gorman  * a transhuge insertion.
411ad9f620SMel Gorman  */
421ad9f620SMel Gorman static pte_t *lock_pte_protection(struct vm_area_struct *vma, pmd_t *pmd,
431ad9f620SMel Gorman 			unsigned long addr, int prot_numa, spinlock_t **ptl)
441ad9f620SMel Gorman {
451ad9f620SMel Gorman 	pte_t *pte;
461ad9f620SMel Gorman 	spinlock_t *pmdl;
471ad9f620SMel Gorman 
481ad9f620SMel Gorman 	/* !prot_numa is protected by mmap_sem held for write */
491ad9f620SMel Gorman 	if (!prot_numa)
501ad9f620SMel Gorman 		return pte_offset_map_lock(vma->vm_mm, pmd, addr, ptl);
511ad9f620SMel Gorman 
521ad9f620SMel Gorman 	pmdl = pmd_lock(vma->vm_mm, pmd);
531ad9f620SMel Gorman 	if (unlikely(pmd_trans_huge(*pmd) || pmd_none(*pmd))) {
541ad9f620SMel Gorman 		spin_unlock(pmdl);
551ad9f620SMel Gorman 		return NULL;
561ad9f620SMel Gorman 	}
571ad9f620SMel Gorman 
581ad9f620SMel Gorman 	pte = pte_offset_map_lock(vma->vm_mm, pmd, addr, ptl);
591ad9f620SMel Gorman 	spin_unlock(pmdl);
601ad9f620SMel Gorman 	return pte;
611ad9f620SMel Gorman }
621ad9f620SMel Gorman 
634b10e7d5SMel Gorman static unsigned long change_pte_range(struct vm_area_struct *vma, pmd_t *pmd,
64c1e6098bSPeter Zijlstra 		unsigned long addr, unsigned long end, pgprot_t newprot,
650f19c179SMel Gorman 		int dirty_accountable, int prot_numa)
661da177e4SLinus Torvalds {
674b10e7d5SMel Gorman 	struct mm_struct *mm = vma->vm_mm;
680697212aSChristoph Lameter 	pte_t *pte, oldpte;
69705e87c0SHugh Dickins 	spinlock_t *ptl;
707da4d641SPeter Zijlstra 	unsigned long pages = 0;
711da177e4SLinus Torvalds 
721ad9f620SMel Gorman 	pte = lock_pte_protection(vma, pmd, addr, prot_numa, &ptl);
731ad9f620SMel Gorman 	if (!pte)
741ad9f620SMel Gorman 		return 0;
751ad9f620SMel Gorman 
766606c3e0SZachary Amsden 	arch_enter_lazy_mmu_mode();
771da177e4SLinus Torvalds 	do {
780697212aSChristoph Lameter 		oldpte = *pte;
790697212aSChristoph Lameter 		if (pte_present(oldpte)) {
801da177e4SLinus Torvalds 			pte_t ptent;
81b191f9b1SMel Gorman 			bool preserve_write = prot_numa && pte_write(oldpte);
821da177e4SLinus Torvalds 
83e944fd67SMel Gorman 			/*
84e944fd67SMel Gorman 			 * Avoid trapping faults against the zero or KSM
85e944fd67SMel Gorman 			 * pages. See similar comment in change_huge_pmd.
86e944fd67SMel Gorman 			 */
87e944fd67SMel Gorman 			if (prot_numa) {
88e944fd67SMel Gorman 				struct page *page;
89e944fd67SMel Gorman 
90e944fd67SMel Gorman 				page = vm_normal_page(vma, addr, oldpte);
91e944fd67SMel Gorman 				if (!page || PageKsm(page))
92e944fd67SMel Gorman 					continue;
9310c1045fSMel Gorman 
9410c1045fSMel Gorman 				/* Avoid TLB flush if possible */
9510c1045fSMel Gorman 				if (pte_protnone(oldpte))
9610c1045fSMel Gorman 					continue;
97e944fd67SMel Gorman 			}
98e944fd67SMel Gorman 
990c5f83c2SMel Gorman 			ptent = ptep_modify_prot_start(mm, addr, pte);
100c1e6098bSPeter Zijlstra 			ptent = pte_modify(ptent, newprot);
101b191f9b1SMel Gorman 			if (preserve_write)
102b191f9b1SMel Gorman 				ptent = pte_mkwrite(ptent);
1038a0516edSMel Gorman 
1048a0516edSMel Gorman 			/* Avoid taking write faults for known dirty pages */
10564e45507SPeter Feiner 			if (dirty_accountable && pte_dirty(ptent) &&
10664e45507SPeter Feiner 					(pte_soft_dirty(ptent) ||
1078a0516edSMel Gorman 					 !(vma->vm_flags & VM_SOFTDIRTY))) {
1089d85d586SAneesh Kumar K.V 				ptent = pte_mkwrite(ptent);
1098a0516edSMel Gorman 			}
1109d85d586SAneesh Kumar K.V 			ptep_modify_prot_commit(mm, addr, pte, ptent);
1114b10e7d5SMel Gorman 			pages++;
1120661a336SKirill A. Shutemov 		} else if (IS_ENABLED(CONFIG_MIGRATION)) {
1130697212aSChristoph Lameter 			swp_entry_t entry = pte_to_swp_entry(oldpte);
1140697212aSChristoph Lameter 
1150697212aSChristoph Lameter 			if (is_write_migration_entry(entry)) {
116c3d16e16SCyrill Gorcunov 				pte_t newpte;
1170697212aSChristoph Lameter 				/*
1180697212aSChristoph Lameter 				 * A protection check is difficult so
1190697212aSChristoph Lameter 				 * just be safe and disable write
1200697212aSChristoph Lameter 				 */
1210697212aSChristoph Lameter 				make_migration_entry_read(&entry);
122c3d16e16SCyrill Gorcunov 				newpte = swp_entry_to_pte(entry);
123c3d16e16SCyrill Gorcunov 				if (pte_swp_soft_dirty(oldpte))
124c3d16e16SCyrill Gorcunov 					newpte = pte_swp_mksoft_dirty(newpte);
125c3d16e16SCyrill Gorcunov 				set_pte_at(mm, addr, pte, newpte);
126e920e14cSMel Gorman 
1277da4d641SPeter Zijlstra 				pages++;
1280697212aSChristoph Lameter 			}
129e920e14cSMel Gorman 		}
1301da177e4SLinus Torvalds 	} while (pte++, addr += PAGE_SIZE, addr != end);
1316606c3e0SZachary Amsden 	arch_leave_lazy_mmu_mode();
132705e87c0SHugh Dickins 	pte_unmap_unlock(pte - 1, ptl);
1337da4d641SPeter Zijlstra 
1347da4d641SPeter Zijlstra 	return pages;
1351da177e4SLinus Torvalds }
1361da177e4SLinus Torvalds 
1377d12efaeSAndrew Morton static inline unsigned long change_pmd_range(struct vm_area_struct *vma,
1387d12efaeSAndrew Morton 		pud_t *pud, unsigned long addr, unsigned long end,
1397d12efaeSAndrew Morton 		pgprot_t newprot, int dirty_accountable, int prot_numa)
1401da177e4SLinus Torvalds {
1411da177e4SLinus Torvalds 	pmd_t *pmd;
142a5338093SRik van Riel 	struct mm_struct *mm = vma->vm_mm;
1431da177e4SLinus Torvalds 	unsigned long next;
1447da4d641SPeter Zijlstra 	unsigned long pages = 0;
14572403b4aSMel Gorman 	unsigned long nr_huge_updates = 0;
146a5338093SRik van Riel 	unsigned long mni_start = 0;
1471da177e4SLinus Torvalds 
1481da177e4SLinus Torvalds 	pmd = pmd_offset(pud, addr);
1491da177e4SLinus Torvalds 	do {
15025cbbef1SMel Gorman 		unsigned long this_pages;
15125cbbef1SMel Gorman 
1521da177e4SLinus Torvalds 		next = pmd_addr_end(addr, end);
1535c7fb56eSDan Williams 		if (!pmd_trans_huge(*pmd) && !pmd_devmap(*pmd)
1545c7fb56eSDan Williams 				&& pmd_none_or_clear_bad(pmd))
15588a9ab6eSRik van Riel 			continue;
156a5338093SRik van Riel 
157a5338093SRik van Riel 		/* invoke the mmu notifier if the pmd is populated */
158a5338093SRik van Riel 		if (!mni_start) {
159a5338093SRik van Riel 			mni_start = addr;
160a5338093SRik van Riel 			mmu_notifier_invalidate_range_start(mm, mni_start, end);
161a5338093SRik van Riel 		}
162a5338093SRik van Riel 
1635c7fb56eSDan Williams 		if (pmd_trans_huge(*pmd) || pmd_devmap(*pmd)) {
1646b9116a6SKirill A. Shutemov 			if (next - addr != HPAGE_PMD_SIZE) {
16578ddc534SKirill A. Shutemov 				split_huge_pmd(vma, pmd, addr);
166337d9abfSNaoya Horiguchi 				if (pmd_trans_unstable(pmd))
1676b9116a6SKirill A. Shutemov 					continue;
1686b9116a6SKirill A. Shutemov 			} else {
169f123d74aSMel Gorman 				int nr_ptes = change_huge_pmd(vma, pmd, addr,
170e944fd67SMel Gorman 						newprot, prot_numa);
171f123d74aSMel Gorman 
172f123d74aSMel Gorman 				if (nr_ptes) {
17372403b4aSMel Gorman 					if (nr_ptes == HPAGE_PMD_NR) {
17472403b4aSMel Gorman 						pages += HPAGE_PMD_NR;
17572403b4aSMel Gorman 						nr_huge_updates++;
17672403b4aSMel Gorman 					}
1771ad9f620SMel Gorman 
1781ad9f620SMel Gorman 					/* huge pmd was handled */
179cd7548abSJohannes Weiner 					continue;
1807da4d641SPeter Zijlstra 				}
181f123d74aSMel Gorman 			}
18288a9ab6eSRik van Riel 			/* fall through, the trans huge pmd just split */
183cd7548abSJohannes Weiner 		}
18425cbbef1SMel Gorman 		this_pages = change_pte_range(vma, pmd, addr, next, newprot,
1850f19c179SMel Gorman 				 dirty_accountable, prot_numa);
18625cbbef1SMel Gorman 		pages += this_pages;
1871da177e4SLinus Torvalds 	} while (pmd++, addr = next, addr != end);
1887da4d641SPeter Zijlstra 
189a5338093SRik van Riel 	if (mni_start)
190a5338093SRik van Riel 		mmu_notifier_invalidate_range_end(mm, mni_start, end);
191a5338093SRik van Riel 
19272403b4aSMel Gorman 	if (nr_huge_updates)
19372403b4aSMel Gorman 		count_vm_numa_events(NUMA_HUGE_PTE_UPDATES, nr_huge_updates);
1947da4d641SPeter Zijlstra 	return pages;
1951da177e4SLinus Torvalds }
1961da177e4SLinus Torvalds 
1977d12efaeSAndrew Morton static inline unsigned long change_pud_range(struct vm_area_struct *vma,
1987d12efaeSAndrew Morton 		pgd_t *pgd, unsigned long addr, unsigned long end,
1997d12efaeSAndrew Morton 		pgprot_t newprot, int dirty_accountable, int prot_numa)
2001da177e4SLinus Torvalds {
2011da177e4SLinus Torvalds 	pud_t *pud;
2021da177e4SLinus Torvalds 	unsigned long next;
2037da4d641SPeter Zijlstra 	unsigned long pages = 0;
2041da177e4SLinus Torvalds 
2051da177e4SLinus Torvalds 	pud = pud_offset(pgd, addr);
2061da177e4SLinus Torvalds 	do {
2071da177e4SLinus Torvalds 		next = pud_addr_end(addr, end);
2081da177e4SLinus Torvalds 		if (pud_none_or_clear_bad(pud))
2091da177e4SLinus Torvalds 			continue;
2107da4d641SPeter Zijlstra 		pages += change_pmd_range(vma, pud, addr, next, newprot,
2114b10e7d5SMel Gorman 				 dirty_accountable, prot_numa);
2121da177e4SLinus Torvalds 	} while (pud++, addr = next, addr != end);
2137da4d641SPeter Zijlstra 
2147da4d641SPeter Zijlstra 	return pages;
2151da177e4SLinus Torvalds }
2161da177e4SLinus Torvalds 
2177da4d641SPeter Zijlstra static unsigned long change_protection_range(struct vm_area_struct *vma,
218c1e6098bSPeter Zijlstra 		unsigned long addr, unsigned long end, pgprot_t newprot,
2194b10e7d5SMel Gorman 		int dirty_accountable, int prot_numa)
2201da177e4SLinus Torvalds {
2211da177e4SLinus Torvalds 	struct mm_struct *mm = vma->vm_mm;
2221da177e4SLinus Torvalds 	pgd_t *pgd;
2231da177e4SLinus Torvalds 	unsigned long next;
2241da177e4SLinus Torvalds 	unsigned long start = addr;
2257da4d641SPeter Zijlstra 	unsigned long pages = 0;
2261da177e4SLinus Torvalds 
2271da177e4SLinus Torvalds 	BUG_ON(addr >= end);
2281da177e4SLinus Torvalds 	pgd = pgd_offset(mm, addr);
2291da177e4SLinus Torvalds 	flush_cache_range(vma, addr, end);
23020841405SRik van Riel 	set_tlb_flush_pending(mm);
2311da177e4SLinus Torvalds 	do {
2321da177e4SLinus Torvalds 		next = pgd_addr_end(addr, end);
2331da177e4SLinus Torvalds 		if (pgd_none_or_clear_bad(pgd))
2341da177e4SLinus Torvalds 			continue;
2357da4d641SPeter Zijlstra 		pages += change_pud_range(vma, pgd, addr, next, newprot,
2364b10e7d5SMel Gorman 				 dirty_accountable, prot_numa);
2371da177e4SLinus Torvalds 	} while (pgd++, addr = next, addr != end);
2387da4d641SPeter Zijlstra 
2391233d588SIngo Molnar 	/* Only flush the TLB if we actually modified any entries: */
2401233d588SIngo Molnar 	if (pages)
2411da177e4SLinus Torvalds 		flush_tlb_range(vma, start, end);
24220841405SRik van Riel 	clear_tlb_flush_pending(mm);
2437da4d641SPeter Zijlstra 
2447da4d641SPeter Zijlstra 	return pages;
2457da4d641SPeter Zijlstra }
2467da4d641SPeter Zijlstra 
2477da4d641SPeter Zijlstra unsigned long change_protection(struct vm_area_struct *vma, unsigned long start,
2487da4d641SPeter Zijlstra 		       unsigned long end, pgprot_t newprot,
2494b10e7d5SMel Gorman 		       int dirty_accountable, int prot_numa)
2507da4d641SPeter Zijlstra {
2517da4d641SPeter Zijlstra 	unsigned long pages;
2527da4d641SPeter Zijlstra 
2537da4d641SPeter Zijlstra 	if (is_vm_hugetlb_page(vma))
2547da4d641SPeter Zijlstra 		pages = hugetlb_change_protection(vma, start, end, newprot);
2557da4d641SPeter Zijlstra 	else
2564b10e7d5SMel Gorman 		pages = change_protection_range(vma, start, end, newprot, dirty_accountable, prot_numa);
2577da4d641SPeter Zijlstra 
2587da4d641SPeter Zijlstra 	return pages;
2591da177e4SLinus Torvalds }
2601da177e4SLinus Torvalds 
261b6a2fea3SOllie Wild int
2621da177e4SLinus Torvalds mprotect_fixup(struct vm_area_struct *vma, struct vm_area_struct **pprev,
2631da177e4SLinus Torvalds 	unsigned long start, unsigned long end, unsigned long newflags)
2641da177e4SLinus Torvalds {
2651da177e4SLinus Torvalds 	struct mm_struct *mm = vma->vm_mm;
2661da177e4SLinus Torvalds 	unsigned long oldflags = vma->vm_flags;
2671da177e4SLinus Torvalds 	long nrpages = (end - start) >> PAGE_SHIFT;
2681da177e4SLinus Torvalds 	unsigned long charged = 0;
2691da177e4SLinus Torvalds 	pgoff_t pgoff;
2701da177e4SLinus Torvalds 	int error;
271c1e6098bSPeter Zijlstra 	int dirty_accountable = 0;
2721da177e4SLinus Torvalds 
2731da177e4SLinus Torvalds 	if (newflags == oldflags) {
2741da177e4SLinus Torvalds 		*pprev = vma;
2751da177e4SLinus Torvalds 		return 0;
2761da177e4SLinus Torvalds 	}
2771da177e4SLinus Torvalds 
2781da177e4SLinus Torvalds 	/*
2791da177e4SLinus Torvalds 	 * If we make a private mapping writable we increase our commit;
2801da177e4SLinus Torvalds 	 * but (without finer accounting) cannot reduce our commit if we
2815a6fe125SMel Gorman 	 * make it unwritable again. hugetlb mapping were accounted for
2825a6fe125SMel Gorman 	 * even if read-only so there is no need to account for them here
2831da177e4SLinus Torvalds 	 */
2841da177e4SLinus Torvalds 	if (newflags & VM_WRITE) {
28584638335SKonstantin Khlebnikov 		/* Check space limits when area turns into data. */
28684638335SKonstantin Khlebnikov 		if (!may_expand_vm(mm, newflags, nrpages) &&
28784638335SKonstantin Khlebnikov 				may_expand_vm(mm, oldflags, nrpages))
28884638335SKonstantin Khlebnikov 			return -ENOMEM;
2895a6fe125SMel Gorman 		if (!(oldflags & (VM_ACCOUNT|VM_WRITE|VM_HUGETLB|
290cdfd4325SAndy Whitcroft 						VM_SHARED|VM_NORESERVE))) {
2911da177e4SLinus Torvalds 			charged = nrpages;
292191c5424SAl Viro 			if (security_vm_enough_memory_mm(mm, charged))
2931da177e4SLinus Torvalds 				return -ENOMEM;
2941da177e4SLinus Torvalds 			newflags |= VM_ACCOUNT;
2951da177e4SLinus Torvalds 		}
2961da177e4SLinus Torvalds 	}
2971da177e4SLinus Torvalds 
2981da177e4SLinus Torvalds 	/*
2991da177e4SLinus Torvalds 	 * First try to merge with previous and/or next vma.
3001da177e4SLinus Torvalds 	 */
3011da177e4SLinus Torvalds 	pgoff = vma->vm_pgoff + ((start - vma->vm_start) >> PAGE_SHIFT);
3021da177e4SLinus Torvalds 	*pprev = vma_merge(mm, *pprev, start, end, newflags,
30319a809afSAndrea Arcangeli 			   vma->anon_vma, vma->vm_file, pgoff, vma_policy(vma),
30419a809afSAndrea Arcangeli 			   vma->vm_userfaultfd_ctx);
3051da177e4SLinus Torvalds 	if (*pprev) {
3061da177e4SLinus Torvalds 		vma = *pprev;
3071da177e4SLinus Torvalds 		goto success;
3081da177e4SLinus Torvalds 	}
3091da177e4SLinus Torvalds 
3101da177e4SLinus Torvalds 	*pprev = vma;
3111da177e4SLinus Torvalds 
3121da177e4SLinus Torvalds 	if (start != vma->vm_start) {
3131da177e4SLinus Torvalds 		error = split_vma(mm, vma, start, 1);
3141da177e4SLinus Torvalds 		if (error)
3151da177e4SLinus Torvalds 			goto fail;
3161da177e4SLinus Torvalds 	}
3171da177e4SLinus Torvalds 
3181da177e4SLinus Torvalds 	if (end != vma->vm_end) {
3191da177e4SLinus Torvalds 		error = split_vma(mm, vma, end, 0);
3201da177e4SLinus Torvalds 		if (error)
3211da177e4SLinus Torvalds 			goto fail;
3221da177e4SLinus Torvalds 	}
3231da177e4SLinus Torvalds 
3241da177e4SLinus Torvalds success:
3251da177e4SLinus Torvalds 	/*
3261da177e4SLinus Torvalds 	 * vm_flags and vm_page_prot are protected by the mmap_sem
3271da177e4SLinus Torvalds 	 * held in write mode.
3281da177e4SLinus Torvalds 	 */
3291da177e4SLinus Torvalds 	vma->vm_flags = newflags;
33064e45507SPeter Feiner 	dirty_accountable = vma_wants_writenotify(vma);
33164e45507SPeter Feiner 	vma_set_page_prot(vma);
332d08b3851SPeter Zijlstra 
3337d12efaeSAndrew Morton 	change_protection(vma, start, end, vma->vm_page_prot,
3347d12efaeSAndrew Morton 			  dirty_accountable, 0);
3357da4d641SPeter Zijlstra 
33636f88188SKirill A. Shutemov 	/*
33736f88188SKirill A. Shutemov 	 * Private VM_LOCKED VMA becoming writable: trigger COW to avoid major
33836f88188SKirill A. Shutemov 	 * fault on access.
33936f88188SKirill A. Shutemov 	 */
34036f88188SKirill A. Shutemov 	if ((oldflags & (VM_WRITE | VM_SHARED | VM_LOCKED)) == VM_LOCKED &&
34136f88188SKirill A. Shutemov 			(newflags & VM_WRITE)) {
34236f88188SKirill A. Shutemov 		populate_vma_page_range(vma, start, end, NULL);
34336f88188SKirill A. Shutemov 	}
34436f88188SKirill A. Shutemov 
34584638335SKonstantin Khlebnikov 	vm_stat_account(mm, oldflags, -nrpages);
34684638335SKonstantin Khlebnikov 	vm_stat_account(mm, newflags, nrpages);
34763bfd738SPekka Enberg 	perf_event_mmap(vma);
3481da177e4SLinus Torvalds 	return 0;
3491da177e4SLinus Torvalds 
3501da177e4SLinus Torvalds fail:
3511da177e4SLinus Torvalds 	vm_unacct_memory(charged);
3521da177e4SLinus Torvalds 	return error;
3531da177e4SLinus Torvalds }
3541da177e4SLinus Torvalds 
355*7d06d9c9SDave Hansen /*
356*7d06d9c9SDave Hansen  * pkey==-1 when doing a legacy mprotect()
357*7d06d9c9SDave Hansen  */
358*7d06d9c9SDave Hansen static int do_mprotect_pkey(unsigned long start, size_t len,
359*7d06d9c9SDave Hansen 		unsigned long prot, int pkey)
3601da177e4SLinus Torvalds {
36162b5f7d0SDave Hansen 	unsigned long nstart, end, tmp, reqprot;
3621da177e4SLinus Torvalds 	struct vm_area_struct *vma, *prev;
3631da177e4SLinus Torvalds 	int error = -EINVAL;
3641da177e4SLinus Torvalds 	const int grows = prot & (PROT_GROWSDOWN|PROT_GROWSUP);
365f138556dSPiotr Kwapulinski 	const bool rier = (current->personality & READ_IMPLIES_EXEC) &&
366f138556dSPiotr Kwapulinski 				(prot & PROT_READ);
367*7d06d9c9SDave Hansen 	/*
368*7d06d9c9SDave Hansen 	 * A temporary safety check since we are not validating
369*7d06d9c9SDave Hansen 	 * the pkey before we introduce the allocation code.
370*7d06d9c9SDave Hansen 	 */
371*7d06d9c9SDave Hansen 	if (pkey != -1)
372*7d06d9c9SDave Hansen 		return -EINVAL;
373f138556dSPiotr Kwapulinski 
3741da177e4SLinus Torvalds 	prot &= ~(PROT_GROWSDOWN|PROT_GROWSUP);
3751da177e4SLinus Torvalds 	if (grows == (PROT_GROWSDOWN|PROT_GROWSUP)) /* can't be both */
3761da177e4SLinus Torvalds 		return -EINVAL;
3771da177e4SLinus Torvalds 
3781da177e4SLinus Torvalds 	if (start & ~PAGE_MASK)
3791da177e4SLinus Torvalds 		return -EINVAL;
3801da177e4SLinus Torvalds 	if (!len)
3811da177e4SLinus Torvalds 		return 0;
3821da177e4SLinus Torvalds 	len = PAGE_ALIGN(len);
3831da177e4SLinus Torvalds 	end = start + len;
3841da177e4SLinus Torvalds 	if (end <= start)
3851da177e4SLinus Torvalds 		return -ENOMEM;
386b845f313SDave Kleikamp 	if (!arch_validate_prot(prot))
3871da177e4SLinus Torvalds 		return -EINVAL;
3881da177e4SLinus Torvalds 
3891da177e4SLinus Torvalds 	reqprot = prot;
3901da177e4SLinus Torvalds 
391dc0ef0dfSMichal Hocko 	if (down_write_killable(&current->mm->mmap_sem))
392dc0ef0dfSMichal Hocko 		return -EINTR;
3931da177e4SLinus Torvalds 
394097d5910SLinus Torvalds 	vma = find_vma(current->mm, start);
3951da177e4SLinus Torvalds 	error = -ENOMEM;
3961da177e4SLinus Torvalds 	if (!vma)
3971da177e4SLinus Torvalds 		goto out;
398097d5910SLinus Torvalds 	prev = vma->vm_prev;
3991da177e4SLinus Torvalds 	if (unlikely(grows & PROT_GROWSDOWN)) {
4001da177e4SLinus Torvalds 		if (vma->vm_start >= end)
4011da177e4SLinus Torvalds 			goto out;
4021da177e4SLinus Torvalds 		start = vma->vm_start;
4031da177e4SLinus Torvalds 		error = -EINVAL;
4041da177e4SLinus Torvalds 		if (!(vma->vm_flags & VM_GROWSDOWN))
4051da177e4SLinus Torvalds 			goto out;
4067d12efaeSAndrew Morton 	} else {
4071da177e4SLinus Torvalds 		if (vma->vm_start > start)
4081da177e4SLinus Torvalds 			goto out;
4091da177e4SLinus Torvalds 		if (unlikely(grows & PROT_GROWSUP)) {
4101da177e4SLinus Torvalds 			end = vma->vm_end;
4111da177e4SLinus Torvalds 			error = -EINVAL;
4121da177e4SLinus Torvalds 			if (!(vma->vm_flags & VM_GROWSUP))
4131da177e4SLinus Torvalds 				goto out;
4141da177e4SLinus Torvalds 		}
4151da177e4SLinus Torvalds 	}
4161da177e4SLinus Torvalds 	if (start > vma->vm_start)
4171da177e4SLinus Torvalds 		prev = vma;
4181da177e4SLinus Torvalds 
4191da177e4SLinus Torvalds 	for (nstart = start ; ; ) {
4201da177e4SLinus Torvalds 		unsigned long newflags;
421*7d06d9c9SDave Hansen 		int new_vma_pkey;
4221da177e4SLinus Torvalds 
4231da177e4SLinus Torvalds 		/* Here we know that vma->vm_start <= nstart < vma->vm_end. */
4241da177e4SLinus Torvalds 
425f138556dSPiotr Kwapulinski 		/* Does the application expect PROT_READ to imply PROT_EXEC */
426f138556dSPiotr Kwapulinski 		if (rier && (vma->vm_flags & VM_MAYEXEC))
427f138556dSPiotr Kwapulinski 			prot |= PROT_EXEC;
428f138556dSPiotr Kwapulinski 
429*7d06d9c9SDave Hansen 		new_vma_pkey = arch_override_mprotect_pkey(vma, prot, pkey);
430*7d06d9c9SDave Hansen 		newflags = calc_vm_prot_bits(prot, new_vma_pkey);
4317d12efaeSAndrew Morton 		newflags |= (vma->vm_flags & ~(VM_READ | VM_WRITE | VM_EXEC));
4321da177e4SLinus Torvalds 
4337e2cff42SPaolo 'Blaisorblade' Giarrusso 		/* newflags >> 4 shift VM_MAY% in place of VM_% */
4347e2cff42SPaolo 'Blaisorblade' Giarrusso 		if ((newflags & ~(newflags >> 4)) & (VM_READ | VM_WRITE | VM_EXEC)) {
4351da177e4SLinus Torvalds 			error = -EACCES;
4361da177e4SLinus Torvalds 			goto out;
4371da177e4SLinus Torvalds 		}
4381da177e4SLinus Torvalds 
4391da177e4SLinus Torvalds 		error = security_file_mprotect(vma, reqprot, prot);
4401da177e4SLinus Torvalds 		if (error)
4411da177e4SLinus Torvalds 			goto out;
4421da177e4SLinus Torvalds 
4431da177e4SLinus Torvalds 		tmp = vma->vm_end;
4441da177e4SLinus Torvalds 		if (tmp > end)
4451da177e4SLinus Torvalds 			tmp = end;
4461da177e4SLinus Torvalds 		error = mprotect_fixup(vma, &prev, nstart, tmp, newflags);
4471da177e4SLinus Torvalds 		if (error)
4481da177e4SLinus Torvalds 			goto out;
4491da177e4SLinus Torvalds 		nstart = tmp;
4501da177e4SLinus Torvalds 
4511da177e4SLinus Torvalds 		if (nstart < prev->vm_end)
4521da177e4SLinus Torvalds 			nstart = prev->vm_end;
4531da177e4SLinus Torvalds 		if (nstart >= end)
4541da177e4SLinus Torvalds 			goto out;
4551da177e4SLinus Torvalds 
4561da177e4SLinus Torvalds 		vma = prev->vm_next;
4571da177e4SLinus Torvalds 		if (!vma || vma->vm_start != nstart) {
4581da177e4SLinus Torvalds 			error = -ENOMEM;
4591da177e4SLinus Torvalds 			goto out;
4601da177e4SLinus Torvalds 		}
461f138556dSPiotr Kwapulinski 		prot = reqprot;
4621da177e4SLinus Torvalds 	}
4631da177e4SLinus Torvalds out:
4641da177e4SLinus Torvalds 	up_write(&current->mm->mmap_sem);
4651da177e4SLinus Torvalds 	return error;
4661da177e4SLinus Torvalds }
467*7d06d9c9SDave Hansen 
468*7d06d9c9SDave Hansen SYSCALL_DEFINE3(mprotect, unsigned long, start, size_t, len,
469*7d06d9c9SDave Hansen 		unsigned long, prot)
470*7d06d9c9SDave Hansen {
471*7d06d9c9SDave Hansen 	return do_mprotect_pkey(start, len, prot, -1);
472*7d06d9c9SDave Hansen }
473*7d06d9c9SDave Hansen 
474*7d06d9c9SDave Hansen SYSCALL_DEFINE4(pkey_mprotect, unsigned long, start, size_t, len,
475*7d06d9c9SDave Hansen 		unsigned long, prot, int, pkey)
476*7d06d9c9SDave Hansen {
477*7d06d9c9SDave Hansen 	return do_mprotect_pkey(start, len, prot, pkey);
478*7d06d9c9SDave Hansen }
479