xref: /openbmc/linux/mm/mprotect.c (revision 10c1045f28e86ac90589a188f0be2d7a4347efdf)
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>
271da177e4SLinus Torvalds #include <asm/uaccess.h>
281da177e4SLinus Torvalds #include <asm/pgtable.h>
291da177e4SLinus Torvalds #include <asm/cacheflush.h>
301da177e4SLinus Torvalds #include <asm/tlbflush.h>
311da177e4SLinus Torvalds 
321ad9f620SMel Gorman /*
331ad9f620SMel Gorman  * For a prot_numa update we only hold mmap_sem for read so there is a
341ad9f620SMel Gorman  * potential race with faulting where a pmd was temporarily none. This
351ad9f620SMel Gorman  * function checks for a transhuge pmd under the appropriate lock. It
361ad9f620SMel Gorman  * returns a pte if it was successfully locked or NULL if it raced with
371ad9f620SMel Gorman  * a transhuge insertion.
381ad9f620SMel Gorman  */
391ad9f620SMel Gorman static pte_t *lock_pte_protection(struct vm_area_struct *vma, pmd_t *pmd,
401ad9f620SMel Gorman 			unsigned long addr, int prot_numa, spinlock_t **ptl)
411ad9f620SMel Gorman {
421ad9f620SMel Gorman 	pte_t *pte;
431ad9f620SMel Gorman 	spinlock_t *pmdl;
441ad9f620SMel Gorman 
451ad9f620SMel Gorman 	/* !prot_numa is protected by mmap_sem held for write */
461ad9f620SMel Gorman 	if (!prot_numa)
471ad9f620SMel Gorman 		return pte_offset_map_lock(vma->vm_mm, pmd, addr, ptl);
481ad9f620SMel Gorman 
491ad9f620SMel Gorman 	pmdl = pmd_lock(vma->vm_mm, pmd);
501ad9f620SMel Gorman 	if (unlikely(pmd_trans_huge(*pmd) || pmd_none(*pmd))) {
511ad9f620SMel Gorman 		spin_unlock(pmdl);
521ad9f620SMel Gorman 		return NULL;
531ad9f620SMel Gorman 	}
541ad9f620SMel Gorman 
551ad9f620SMel Gorman 	pte = pte_offset_map_lock(vma->vm_mm, pmd, addr, ptl);
561ad9f620SMel Gorman 	spin_unlock(pmdl);
571ad9f620SMel Gorman 	return pte;
581ad9f620SMel Gorman }
591ad9f620SMel Gorman 
604b10e7d5SMel Gorman static unsigned long change_pte_range(struct vm_area_struct *vma, pmd_t *pmd,
61c1e6098bSPeter Zijlstra 		unsigned long addr, unsigned long end, pgprot_t newprot,
620f19c179SMel Gorman 		int dirty_accountable, int prot_numa)
631da177e4SLinus Torvalds {
644b10e7d5SMel Gorman 	struct mm_struct *mm = vma->vm_mm;
650697212aSChristoph Lameter 	pte_t *pte, oldpte;
66705e87c0SHugh Dickins 	spinlock_t *ptl;
677da4d641SPeter Zijlstra 	unsigned long pages = 0;
681da177e4SLinus Torvalds 
691ad9f620SMel Gorman 	pte = lock_pte_protection(vma, pmd, addr, prot_numa, &ptl);
701ad9f620SMel Gorman 	if (!pte)
711ad9f620SMel Gorman 		return 0;
721ad9f620SMel Gorman 
736606c3e0SZachary Amsden 	arch_enter_lazy_mmu_mode();
741da177e4SLinus Torvalds 	do {
750697212aSChristoph Lameter 		oldpte = *pte;
760697212aSChristoph Lameter 		if (pte_present(oldpte)) {
771da177e4SLinus Torvalds 			pte_t ptent;
781da177e4SLinus Torvalds 
79e944fd67SMel Gorman 			/*
80e944fd67SMel Gorman 			 * Avoid trapping faults against the zero or KSM
81e944fd67SMel Gorman 			 * pages. See similar comment in change_huge_pmd.
82e944fd67SMel Gorman 			 */
83e944fd67SMel Gorman 			if (prot_numa) {
84e944fd67SMel Gorman 				struct page *page;
85e944fd67SMel Gorman 
86e944fd67SMel Gorman 				page = vm_normal_page(vma, addr, oldpte);
87e944fd67SMel Gorman 				if (!page || PageKsm(page))
88e944fd67SMel Gorman 					continue;
89*10c1045fSMel Gorman 
90*10c1045fSMel Gorman 				/* Avoid TLB flush if possible */
91*10c1045fSMel Gorman 				if (pte_protnone(oldpte))
92*10c1045fSMel Gorman 					continue;
93e944fd67SMel Gorman 			}
94e944fd67SMel Gorman 
950c5f83c2SMel Gorman 			ptent = ptep_modify_prot_start(mm, addr, pte);
96c1e6098bSPeter Zijlstra 			ptent = pte_modify(ptent, newprot);
978a0516edSMel Gorman 
988a0516edSMel Gorman 			/* Avoid taking write faults for known dirty pages */
9964e45507SPeter Feiner 			if (dirty_accountable && pte_dirty(ptent) &&
10064e45507SPeter Feiner 					(pte_soft_dirty(ptent) ||
1018a0516edSMel Gorman 					 !(vma->vm_flags & VM_SOFTDIRTY))) {
1029d85d586SAneesh Kumar K.V 				ptent = pte_mkwrite(ptent);
1038a0516edSMel Gorman 			}
1049d85d586SAneesh Kumar K.V 			ptep_modify_prot_commit(mm, addr, pte, ptent);
1054b10e7d5SMel Gorman 			pages++;
1060661a336SKirill A. Shutemov 		} else if (IS_ENABLED(CONFIG_MIGRATION)) {
1070697212aSChristoph Lameter 			swp_entry_t entry = pte_to_swp_entry(oldpte);
1080697212aSChristoph Lameter 
1090697212aSChristoph Lameter 			if (is_write_migration_entry(entry)) {
110c3d16e16SCyrill Gorcunov 				pte_t newpte;
1110697212aSChristoph Lameter 				/*
1120697212aSChristoph Lameter 				 * A protection check is difficult so
1130697212aSChristoph Lameter 				 * just be safe and disable write
1140697212aSChristoph Lameter 				 */
1150697212aSChristoph Lameter 				make_migration_entry_read(&entry);
116c3d16e16SCyrill Gorcunov 				newpte = swp_entry_to_pte(entry);
117c3d16e16SCyrill Gorcunov 				if (pte_swp_soft_dirty(oldpte))
118c3d16e16SCyrill Gorcunov 					newpte = pte_swp_mksoft_dirty(newpte);
119c3d16e16SCyrill Gorcunov 				set_pte_at(mm, addr, pte, newpte);
120e920e14cSMel Gorman 
1217da4d641SPeter Zijlstra 				pages++;
1220697212aSChristoph Lameter 			}
123e920e14cSMel Gorman 		}
1241da177e4SLinus Torvalds 	} while (pte++, addr += PAGE_SIZE, addr != end);
1256606c3e0SZachary Amsden 	arch_leave_lazy_mmu_mode();
126705e87c0SHugh Dickins 	pte_unmap_unlock(pte - 1, ptl);
1277da4d641SPeter Zijlstra 
1287da4d641SPeter Zijlstra 	return pages;
1291da177e4SLinus Torvalds }
1301da177e4SLinus Torvalds 
1317d12efaeSAndrew Morton static inline unsigned long change_pmd_range(struct vm_area_struct *vma,
1327d12efaeSAndrew Morton 		pud_t *pud, unsigned long addr, unsigned long end,
1337d12efaeSAndrew Morton 		pgprot_t newprot, int dirty_accountable, int prot_numa)
1341da177e4SLinus Torvalds {
1351da177e4SLinus Torvalds 	pmd_t *pmd;
136a5338093SRik van Riel 	struct mm_struct *mm = vma->vm_mm;
1371da177e4SLinus Torvalds 	unsigned long next;
1387da4d641SPeter Zijlstra 	unsigned long pages = 0;
13972403b4aSMel Gorman 	unsigned long nr_huge_updates = 0;
140a5338093SRik van Riel 	unsigned long mni_start = 0;
1411da177e4SLinus Torvalds 
1421da177e4SLinus Torvalds 	pmd = pmd_offset(pud, addr);
1431da177e4SLinus Torvalds 	do {
14425cbbef1SMel Gorman 		unsigned long this_pages;
14525cbbef1SMel Gorman 
1461da177e4SLinus Torvalds 		next = pmd_addr_end(addr, end);
14788a9ab6eSRik van Riel 		if (!pmd_trans_huge(*pmd) && pmd_none_or_clear_bad(pmd))
14888a9ab6eSRik van Riel 			continue;
149a5338093SRik van Riel 
150a5338093SRik van Riel 		/* invoke the mmu notifier if the pmd is populated */
151a5338093SRik van Riel 		if (!mni_start) {
152a5338093SRik van Riel 			mni_start = addr;
153a5338093SRik van Riel 			mmu_notifier_invalidate_range_start(mm, mni_start, end);
154a5338093SRik van Riel 		}
155a5338093SRik van Riel 
156cd7548abSJohannes Weiner 		if (pmd_trans_huge(*pmd)) {
157cd7548abSJohannes Weiner 			if (next - addr != HPAGE_PMD_SIZE)
158e180377fSKirill A. Shutemov 				split_huge_page_pmd(vma, addr, pmd);
159f123d74aSMel Gorman 			else {
160f123d74aSMel Gorman 				int nr_ptes = change_huge_pmd(vma, pmd, addr,
161e944fd67SMel Gorman 						newprot, prot_numa);
162f123d74aSMel Gorman 
163f123d74aSMel Gorman 				if (nr_ptes) {
16472403b4aSMel Gorman 					if (nr_ptes == HPAGE_PMD_NR) {
16572403b4aSMel Gorman 						pages += HPAGE_PMD_NR;
16672403b4aSMel Gorman 						nr_huge_updates++;
16772403b4aSMel Gorman 					}
1681ad9f620SMel Gorman 
1691ad9f620SMel Gorman 					/* huge pmd was handled */
170cd7548abSJohannes Weiner 					continue;
1717da4d641SPeter Zijlstra 				}
172f123d74aSMel Gorman 			}
17388a9ab6eSRik van Riel 			/* fall through, the trans huge pmd just split */
174cd7548abSJohannes Weiner 		}
17525cbbef1SMel Gorman 		this_pages = change_pte_range(vma, pmd, addr, next, newprot,
1760f19c179SMel Gorman 				 dirty_accountable, prot_numa);
17725cbbef1SMel Gorman 		pages += this_pages;
1781da177e4SLinus Torvalds 	} while (pmd++, addr = next, addr != end);
1797da4d641SPeter Zijlstra 
180a5338093SRik van Riel 	if (mni_start)
181a5338093SRik van Riel 		mmu_notifier_invalidate_range_end(mm, mni_start, end);
182a5338093SRik van Riel 
18372403b4aSMel Gorman 	if (nr_huge_updates)
18472403b4aSMel Gorman 		count_vm_numa_events(NUMA_HUGE_PTE_UPDATES, nr_huge_updates);
1857da4d641SPeter Zijlstra 	return pages;
1861da177e4SLinus Torvalds }
1871da177e4SLinus Torvalds 
1887d12efaeSAndrew Morton static inline unsigned long change_pud_range(struct vm_area_struct *vma,
1897d12efaeSAndrew Morton 		pgd_t *pgd, unsigned long addr, unsigned long end,
1907d12efaeSAndrew Morton 		pgprot_t newprot, int dirty_accountable, int prot_numa)
1911da177e4SLinus Torvalds {
1921da177e4SLinus Torvalds 	pud_t *pud;
1931da177e4SLinus Torvalds 	unsigned long next;
1947da4d641SPeter Zijlstra 	unsigned long pages = 0;
1951da177e4SLinus Torvalds 
1961da177e4SLinus Torvalds 	pud = pud_offset(pgd, addr);
1971da177e4SLinus Torvalds 	do {
1981da177e4SLinus Torvalds 		next = pud_addr_end(addr, end);
1991da177e4SLinus Torvalds 		if (pud_none_or_clear_bad(pud))
2001da177e4SLinus Torvalds 			continue;
2017da4d641SPeter Zijlstra 		pages += change_pmd_range(vma, pud, addr, next, newprot,
2024b10e7d5SMel Gorman 				 dirty_accountable, prot_numa);
2031da177e4SLinus Torvalds 	} while (pud++, addr = next, addr != end);
2047da4d641SPeter Zijlstra 
2057da4d641SPeter Zijlstra 	return pages;
2061da177e4SLinus Torvalds }
2071da177e4SLinus Torvalds 
2087da4d641SPeter Zijlstra static unsigned long change_protection_range(struct vm_area_struct *vma,
209c1e6098bSPeter Zijlstra 		unsigned long addr, unsigned long end, pgprot_t newprot,
2104b10e7d5SMel Gorman 		int dirty_accountable, int prot_numa)
2111da177e4SLinus Torvalds {
2121da177e4SLinus Torvalds 	struct mm_struct *mm = vma->vm_mm;
2131da177e4SLinus Torvalds 	pgd_t *pgd;
2141da177e4SLinus Torvalds 	unsigned long next;
2151da177e4SLinus Torvalds 	unsigned long start = addr;
2167da4d641SPeter Zijlstra 	unsigned long pages = 0;
2171da177e4SLinus Torvalds 
2181da177e4SLinus Torvalds 	BUG_ON(addr >= end);
2191da177e4SLinus Torvalds 	pgd = pgd_offset(mm, addr);
2201da177e4SLinus Torvalds 	flush_cache_range(vma, addr, end);
22120841405SRik van Riel 	set_tlb_flush_pending(mm);
2221da177e4SLinus Torvalds 	do {
2231da177e4SLinus Torvalds 		next = pgd_addr_end(addr, end);
2241da177e4SLinus Torvalds 		if (pgd_none_or_clear_bad(pgd))
2251da177e4SLinus Torvalds 			continue;
2267da4d641SPeter Zijlstra 		pages += change_pud_range(vma, pgd, addr, next, newprot,
2274b10e7d5SMel Gorman 				 dirty_accountable, prot_numa);
2281da177e4SLinus Torvalds 	} while (pgd++, addr = next, addr != end);
2297da4d641SPeter Zijlstra 
2301233d588SIngo Molnar 	/* Only flush the TLB if we actually modified any entries: */
2311233d588SIngo Molnar 	if (pages)
2321da177e4SLinus Torvalds 		flush_tlb_range(vma, start, end);
23320841405SRik van Riel 	clear_tlb_flush_pending(mm);
2347da4d641SPeter Zijlstra 
2357da4d641SPeter Zijlstra 	return pages;
2367da4d641SPeter Zijlstra }
2377da4d641SPeter Zijlstra 
2387da4d641SPeter Zijlstra unsigned long change_protection(struct vm_area_struct *vma, unsigned long start,
2397da4d641SPeter Zijlstra 		       unsigned long end, pgprot_t newprot,
2404b10e7d5SMel Gorman 		       int dirty_accountable, int prot_numa)
2417da4d641SPeter Zijlstra {
2427da4d641SPeter Zijlstra 	unsigned long pages;
2437da4d641SPeter Zijlstra 
2447da4d641SPeter Zijlstra 	if (is_vm_hugetlb_page(vma))
2457da4d641SPeter Zijlstra 		pages = hugetlb_change_protection(vma, start, end, newprot);
2467da4d641SPeter Zijlstra 	else
2474b10e7d5SMel Gorman 		pages = change_protection_range(vma, start, end, newprot, dirty_accountable, prot_numa);
2487da4d641SPeter Zijlstra 
2497da4d641SPeter Zijlstra 	return pages;
2501da177e4SLinus Torvalds }
2511da177e4SLinus Torvalds 
252b6a2fea3SOllie Wild int
2531da177e4SLinus Torvalds mprotect_fixup(struct vm_area_struct *vma, struct vm_area_struct **pprev,
2541da177e4SLinus Torvalds 	unsigned long start, unsigned long end, unsigned long newflags)
2551da177e4SLinus Torvalds {
2561da177e4SLinus Torvalds 	struct mm_struct *mm = vma->vm_mm;
2571da177e4SLinus Torvalds 	unsigned long oldflags = vma->vm_flags;
2581da177e4SLinus Torvalds 	long nrpages = (end - start) >> PAGE_SHIFT;
2591da177e4SLinus Torvalds 	unsigned long charged = 0;
2601da177e4SLinus Torvalds 	pgoff_t pgoff;
2611da177e4SLinus Torvalds 	int error;
262c1e6098bSPeter Zijlstra 	int dirty_accountable = 0;
2631da177e4SLinus Torvalds 
2641da177e4SLinus Torvalds 	if (newflags == oldflags) {
2651da177e4SLinus Torvalds 		*pprev = vma;
2661da177e4SLinus Torvalds 		return 0;
2671da177e4SLinus Torvalds 	}
2681da177e4SLinus Torvalds 
2691da177e4SLinus Torvalds 	/*
2701da177e4SLinus Torvalds 	 * If we make a private mapping writable we increase our commit;
2711da177e4SLinus Torvalds 	 * but (without finer accounting) cannot reduce our commit if we
2725a6fe125SMel Gorman 	 * make it unwritable again. hugetlb mapping were accounted for
2735a6fe125SMel Gorman 	 * even if read-only so there is no need to account for them here
2741da177e4SLinus Torvalds 	 */
2751da177e4SLinus Torvalds 	if (newflags & VM_WRITE) {
2765a6fe125SMel Gorman 		if (!(oldflags & (VM_ACCOUNT|VM_WRITE|VM_HUGETLB|
277cdfd4325SAndy Whitcroft 						VM_SHARED|VM_NORESERVE))) {
2781da177e4SLinus Torvalds 			charged = nrpages;
279191c5424SAl Viro 			if (security_vm_enough_memory_mm(mm, charged))
2801da177e4SLinus Torvalds 				return -ENOMEM;
2811da177e4SLinus Torvalds 			newflags |= VM_ACCOUNT;
2821da177e4SLinus Torvalds 		}
2831da177e4SLinus Torvalds 	}
2841da177e4SLinus Torvalds 
2851da177e4SLinus Torvalds 	/*
2861da177e4SLinus Torvalds 	 * First try to merge with previous and/or next vma.
2871da177e4SLinus Torvalds 	 */
2881da177e4SLinus Torvalds 	pgoff = vma->vm_pgoff + ((start - vma->vm_start) >> PAGE_SHIFT);
2891da177e4SLinus Torvalds 	*pprev = vma_merge(mm, *pprev, start, end, newflags,
2901da177e4SLinus Torvalds 			vma->anon_vma, vma->vm_file, pgoff, vma_policy(vma));
2911da177e4SLinus Torvalds 	if (*pprev) {
2921da177e4SLinus Torvalds 		vma = *pprev;
2931da177e4SLinus Torvalds 		goto success;
2941da177e4SLinus Torvalds 	}
2951da177e4SLinus Torvalds 
2961da177e4SLinus Torvalds 	*pprev = vma;
2971da177e4SLinus Torvalds 
2981da177e4SLinus Torvalds 	if (start != vma->vm_start) {
2991da177e4SLinus Torvalds 		error = split_vma(mm, vma, start, 1);
3001da177e4SLinus Torvalds 		if (error)
3011da177e4SLinus Torvalds 			goto fail;
3021da177e4SLinus Torvalds 	}
3031da177e4SLinus Torvalds 
3041da177e4SLinus Torvalds 	if (end != vma->vm_end) {
3051da177e4SLinus Torvalds 		error = split_vma(mm, vma, end, 0);
3061da177e4SLinus Torvalds 		if (error)
3071da177e4SLinus Torvalds 			goto fail;
3081da177e4SLinus Torvalds 	}
3091da177e4SLinus Torvalds 
3101da177e4SLinus Torvalds success:
3111da177e4SLinus Torvalds 	/*
3121da177e4SLinus Torvalds 	 * vm_flags and vm_page_prot are protected by the mmap_sem
3131da177e4SLinus Torvalds 	 * held in write mode.
3141da177e4SLinus Torvalds 	 */
3151da177e4SLinus Torvalds 	vma->vm_flags = newflags;
31664e45507SPeter Feiner 	dirty_accountable = vma_wants_writenotify(vma);
31764e45507SPeter Feiner 	vma_set_page_prot(vma);
318d08b3851SPeter Zijlstra 
3197d12efaeSAndrew Morton 	change_protection(vma, start, end, vma->vm_page_prot,
3207d12efaeSAndrew Morton 			  dirty_accountable, 0);
3217da4d641SPeter Zijlstra 
322ab50b8edSHugh Dickins 	vm_stat_account(mm, oldflags, vma->vm_file, -nrpages);
323ab50b8edSHugh Dickins 	vm_stat_account(mm, newflags, vma->vm_file, nrpages);
32463bfd738SPekka Enberg 	perf_event_mmap(vma);
3251da177e4SLinus Torvalds 	return 0;
3261da177e4SLinus Torvalds 
3271da177e4SLinus Torvalds fail:
3281da177e4SLinus Torvalds 	vm_unacct_memory(charged);
3291da177e4SLinus Torvalds 	return error;
3301da177e4SLinus Torvalds }
3311da177e4SLinus Torvalds 
3326a6160a7SHeiko Carstens SYSCALL_DEFINE3(mprotect, unsigned long, start, size_t, len,
3336a6160a7SHeiko Carstens 		unsigned long, prot)
3341da177e4SLinus Torvalds {
3351da177e4SLinus Torvalds 	unsigned long vm_flags, nstart, end, tmp, reqprot;
3361da177e4SLinus Torvalds 	struct vm_area_struct *vma, *prev;
3371da177e4SLinus Torvalds 	int error = -EINVAL;
3381da177e4SLinus Torvalds 	const int grows = prot & (PROT_GROWSDOWN|PROT_GROWSUP);
3391da177e4SLinus Torvalds 	prot &= ~(PROT_GROWSDOWN|PROT_GROWSUP);
3401da177e4SLinus Torvalds 	if (grows == (PROT_GROWSDOWN|PROT_GROWSUP)) /* can't be both */
3411da177e4SLinus Torvalds 		return -EINVAL;
3421da177e4SLinus Torvalds 
3431da177e4SLinus Torvalds 	if (start & ~PAGE_MASK)
3441da177e4SLinus Torvalds 		return -EINVAL;
3451da177e4SLinus Torvalds 	if (!len)
3461da177e4SLinus Torvalds 		return 0;
3471da177e4SLinus Torvalds 	len = PAGE_ALIGN(len);
3481da177e4SLinus Torvalds 	end = start + len;
3491da177e4SLinus Torvalds 	if (end <= start)
3501da177e4SLinus Torvalds 		return -ENOMEM;
351b845f313SDave Kleikamp 	if (!arch_validate_prot(prot))
3521da177e4SLinus Torvalds 		return -EINVAL;
3531da177e4SLinus Torvalds 
3541da177e4SLinus Torvalds 	reqprot = prot;
3551da177e4SLinus Torvalds 	/*
3561da177e4SLinus Torvalds 	 * Does the application expect PROT_READ to imply PROT_EXEC:
3571da177e4SLinus Torvalds 	 */
358b344e05cSHua Zhong 	if ((prot & PROT_READ) && (current->personality & READ_IMPLIES_EXEC))
3591da177e4SLinus Torvalds 		prot |= PROT_EXEC;
3601da177e4SLinus Torvalds 
3611da177e4SLinus Torvalds 	vm_flags = calc_vm_prot_bits(prot);
3621da177e4SLinus Torvalds 
3631da177e4SLinus Torvalds 	down_write(&current->mm->mmap_sem);
3641da177e4SLinus Torvalds 
365097d5910SLinus Torvalds 	vma = find_vma(current->mm, start);
3661da177e4SLinus Torvalds 	error = -ENOMEM;
3671da177e4SLinus Torvalds 	if (!vma)
3681da177e4SLinus Torvalds 		goto out;
369097d5910SLinus Torvalds 	prev = vma->vm_prev;
3701da177e4SLinus Torvalds 	if (unlikely(grows & PROT_GROWSDOWN)) {
3711da177e4SLinus Torvalds 		if (vma->vm_start >= end)
3721da177e4SLinus Torvalds 			goto out;
3731da177e4SLinus Torvalds 		start = vma->vm_start;
3741da177e4SLinus Torvalds 		error = -EINVAL;
3751da177e4SLinus Torvalds 		if (!(vma->vm_flags & VM_GROWSDOWN))
3761da177e4SLinus Torvalds 			goto out;
3777d12efaeSAndrew Morton 	} else {
3781da177e4SLinus Torvalds 		if (vma->vm_start > start)
3791da177e4SLinus Torvalds 			goto out;
3801da177e4SLinus Torvalds 		if (unlikely(grows & PROT_GROWSUP)) {
3811da177e4SLinus Torvalds 			end = vma->vm_end;
3821da177e4SLinus Torvalds 			error = -EINVAL;
3831da177e4SLinus Torvalds 			if (!(vma->vm_flags & VM_GROWSUP))
3841da177e4SLinus Torvalds 				goto out;
3851da177e4SLinus Torvalds 		}
3861da177e4SLinus Torvalds 	}
3871da177e4SLinus Torvalds 	if (start > vma->vm_start)
3881da177e4SLinus Torvalds 		prev = vma;
3891da177e4SLinus Torvalds 
3901da177e4SLinus Torvalds 	for (nstart = start ; ; ) {
3911da177e4SLinus Torvalds 		unsigned long newflags;
3921da177e4SLinus Torvalds 
3931da177e4SLinus Torvalds 		/* Here we know that vma->vm_start <= nstart < vma->vm_end. */
3941da177e4SLinus Torvalds 
3957d12efaeSAndrew Morton 		newflags = vm_flags;
3967d12efaeSAndrew Morton 		newflags |= (vma->vm_flags & ~(VM_READ | VM_WRITE | VM_EXEC));
3971da177e4SLinus Torvalds 
3987e2cff42SPaolo 'Blaisorblade' Giarrusso 		/* newflags >> 4 shift VM_MAY% in place of VM_% */
3997e2cff42SPaolo 'Blaisorblade' Giarrusso 		if ((newflags & ~(newflags >> 4)) & (VM_READ | VM_WRITE | VM_EXEC)) {
4001da177e4SLinus Torvalds 			error = -EACCES;
4011da177e4SLinus Torvalds 			goto out;
4021da177e4SLinus Torvalds 		}
4031da177e4SLinus Torvalds 
4041da177e4SLinus Torvalds 		error = security_file_mprotect(vma, reqprot, prot);
4051da177e4SLinus Torvalds 		if (error)
4061da177e4SLinus Torvalds 			goto out;
4071da177e4SLinus Torvalds 
4081da177e4SLinus Torvalds 		tmp = vma->vm_end;
4091da177e4SLinus Torvalds 		if (tmp > end)
4101da177e4SLinus Torvalds 			tmp = end;
4111da177e4SLinus Torvalds 		error = mprotect_fixup(vma, &prev, nstart, tmp, newflags);
4121da177e4SLinus Torvalds 		if (error)
4131da177e4SLinus Torvalds 			goto out;
4141da177e4SLinus Torvalds 		nstart = tmp;
4151da177e4SLinus Torvalds 
4161da177e4SLinus Torvalds 		if (nstart < prev->vm_end)
4171da177e4SLinus Torvalds 			nstart = prev->vm_end;
4181da177e4SLinus Torvalds 		if (nstart >= end)
4191da177e4SLinus Torvalds 			goto out;
4201da177e4SLinus Torvalds 
4211da177e4SLinus Torvalds 		vma = prev->vm_next;
4221da177e4SLinus Torvalds 		if (!vma || vma->vm_start != nstart) {
4231da177e4SLinus Torvalds 			error = -ENOMEM;
4241da177e4SLinus Torvalds 			goto out;
4251da177e4SLinus Torvalds 		}
4261da177e4SLinus Torvalds 	}
4271da177e4SLinus Torvalds out:
4281da177e4SLinus Torvalds 	up_write(&current->mm->mmap_sem);
4291da177e4SLinus Torvalds 	return error;
4301da177e4SLinus Torvalds }
431