xref: /openbmc/linux/mm/mprotect.c (revision 72403b4a0fbdf433c1fe0127e49864658f6f6468)
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>
261da177e4SLinus Torvalds #include <asm/uaccess.h>
271da177e4SLinus Torvalds #include <asm/pgtable.h>
281da177e4SLinus Torvalds #include <asm/cacheflush.h>
291da177e4SLinus Torvalds #include <asm/tlbflush.h>
301da177e4SLinus Torvalds 
311c12c4cfSVenki Pallipadi #ifndef pgprot_modify
321c12c4cfSVenki Pallipadi static inline pgprot_t pgprot_modify(pgprot_t oldprot, pgprot_t newprot)
331c12c4cfSVenki Pallipadi {
341c12c4cfSVenki Pallipadi 	return newprot;
351c12c4cfSVenki Pallipadi }
361c12c4cfSVenki Pallipadi #endif
371c12c4cfSVenki Pallipadi 
384b10e7d5SMel Gorman static unsigned long change_pte_range(struct vm_area_struct *vma, pmd_t *pmd,
39c1e6098bSPeter Zijlstra 		unsigned long addr, unsigned long end, pgprot_t newprot,
400f19c179SMel Gorman 		int dirty_accountable, int prot_numa)
411da177e4SLinus Torvalds {
424b10e7d5SMel Gorman 	struct mm_struct *mm = vma->vm_mm;
430697212aSChristoph Lameter 	pte_t *pte, oldpte;
44705e87c0SHugh Dickins 	spinlock_t *ptl;
457da4d641SPeter Zijlstra 	unsigned long pages = 0;
461da177e4SLinus Torvalds 
47705e87c0SHugh Dickins 	pte = pte_offset_map_lock(mm, pmd, addr, &ptl);
486606c3e0SZachary Amsden 	arch_enter_lazy_mmu_mode();
491da177e4SLinus Torvalds 	do {
500697212aSChristoph Lameter 		oldpte = *pte;
510697212aSChristoph Lameter 		if (pte_present(oldpte)) {
521da177e4SLinus Torvalds 			pte_t ptent;
534b10e7d5SMel Gorman 			bool updated = false;
541da177e4SLinus Torvalds 
551ea0704eSJeremy Fitzhardinge 			ptent = ptep_modify_prot_start(mm, addr, pte);
564b10e7d5SMel Gorman 			if (!prot_numa) {
57c1e6098bSPeter Zijlstra 				ptent = pte_modify(ptent, newprot);
584b10e7d5SMel Gorman 				updated = true;
594b10e7d5SMel Gorman 			} else {
604b10e7d5SMel Gorman 				struct page *page;
614b10e7d5SMel Gorman 
624b10e7d5SMel Gorman 				page = vm_normal_page(vma, addr, oldpte);
634b10e7d5SMel Gorman 				if (page) {
641bc115d8SMel Gorman 					if (!pte_numa(oldpte)) {
654b10e7d5SMel Gorman 						ptent = pte_mknuma(ptent);
664b10e7d5SMel Gorman 						updated = true;
674b10e7d5SMel Gorman 					}
684b10e7d5SMel Gorman 				}
694b10e7d5SMel Gorman 			}
701ea0704eSJeremy Fitzhardinge 
71c1e6098bSPeter Zijlstra 			/*
72c1e6098bSPeter Zijlstra 			 * Avoid taking write faults for pages we know to be
73c1e6098bSPeter Zijlstra 			 * dirty.
74c1e6098bSPeter Zijlstra 			 */
754b10e7d5SMel Gorman 			if (dirty_accountable && pte_dirty(ptent)) {
76c1e6098bSPeter Zijlstra 				ptent = pte_mkwrite(ptent);
774b10e7d5SMel Gorman 				updated = true;
784b10e7d5SMel Gorman 			}
791ea0704eSJeremy Fitzhardinge 
804b10e7d5SMel Gorman 			if (updated)
814b10e7d5SMel Gorman 				pages++;
821ea0704eSJeremy Fitzhardinge 			ptep_modify_prot_commit(mm, addr, pte, ptent);
83ce1744f4SKonstantin Khlebnikov 		} else if (IS_ENABLED(CONFIG_MIGRATION) && !pte_file(oldpte)) {
840697212aSChristoph Lameter 			swp_entry_t entry = pte_to_swp_entry(oldpte);
850697212aSChristoph Lameter 
860697212aSChristoph Lameter 			if (is_write_migration_entry(entry)) {
87c3d16e16SCyrill Gorcunov 				pte_t newpte;
880697212aSChristoph Lameter 				/*
890697212aSChristoph Lameter 				 * A protection check is difficult so
900697212aSChristoph Lameter 				 * just be safe and disable write
910697212aSChristoph Lameter 				 */
920697212aSChristoph Lameter 				make_migration_entry_read(&entry);
93c3d16e16SCyrill Gorcunov 				newpte = swp_entry_to_pte(entry);
94c3d16e16SCyrill Gorcunov 				if (pte_swp_soft_dirty(oldpte))
95c3d16e16SCyrill Gorcunov 					newpte = pte_swp_mksoft_dirty(newpte);
96c3d16e16SCyrill Gorcunov 				set_pte_at(mm, addr, pte, newpte);
97e920e14cSMel Gorman 
987da4d641SPeter Zijlstra 				pages++;
990697212aSChristoph Lameter 			}
100e920e14cSMel Gorman 		}
1011da177e4SLinus Torvalds 	} while (pte++, addr += PAGE_SIZE, addr != end);
1026606c3e0SZachary Amsden 	arch_leave_lazy_mmu_mode();
103705e87c0SHugh Dickins 	pte_unmap_unlock(pte - 1, ptl);
1047da4d641SPeter Zijlstra 
1057da4d641SPeter Zijlstra 	return pages;
1061da177e4SLinus Torvalds }
1071da177e4SLinus Torvalds 
1087d12efaeSAndrew Morton static inline unsigned long change_pmd_range(struct vm_area_struct *vma,
1097d12efaeSAndrew Morton 		pud_t *pud, unsigned long addr, unsigned long end,
1107d12efaeSAndrew Morton 		pgprot_t newprot, int dirty_accountable, int prot_numa)
1111da177e4SLinus Torvalds {
1121da177e4SLinus Torvalds 	pmd_t *pmd;
1131da177e4SLinus Torvalds 	unsigned long next;
1147da4d641SPeter Zijlstra 	unsigned long pages = 0;
115*72403b4aSMel Gorman 	unsigned long nr_huge_updates = 0;
1161da177e4SLinus Torvalds 
1171da177e4SLinus Torvalds 	pmd = pmd_offset(pud, addr);
1181da177e4SLinus Torvalds 	do {
11925cbbef1SMel Gorman 		unsigned long this_pages;
12025cbbef1SMel Gorman 
1211da177e4SLinus Torvalds 		next = pmd_addr_end(addr, end);
122cd7548abSJohannes Weiner 		if (pmd_trans_huge(*pmd)) {
123cd7548abSJohannes Weiner 			if (next - addr != HPAGE_PMD_SIZE)
124e180377fSKirill A. Shutemov 				split_huge_page_pmd(vma, addr, pmd);
125f123d74aSMel Gorman 			else {
126f123d74aSMel Gorman 				int nr_ptes = change_huge_pmd(vma, pmd, addr,
127f123d74aSMel Gorman 						newprot, prot_numa);
128f123d74aSMel Gorman 
129f123d74aSMel Gorman 				if (nr_ptes) {
130*72403b4aSMel Gorman 					if (nr_ptes == HPAGE_PMD_NR) {
131*72403b4aSMel Gorman 						pages += HPAGE_PMD_NR;
132*72403b4aSMel Gorman 						nr_huge_updates++;
133*72403b4aSMel Gorman 					}
134cd7548abSJohannes Weiner 					continue;
1357da4d641SPeter Zijlstra 				}
136f123d74aSMel Gorman 			}
137cd7548abSJohannes Weiner 			/* fall through */
138cd7548abSJohannes Weiner 		}
1391da177e4SLinus Torvalds 		if (pmd_none_or_clear_bad(pmd))
1401da177e4SLinus Torvalds 			continue;
14125cbbef1SMel Gorman 		this_pages = change_pte_range(vma, pmd, addr, next, newprot,
1420f19c179SMel Gorman 				 dirty_accountable, prot_numa);
14325cbbef1SMel Gorman 		pages += this_pages;
1441da177e4SLinus Torvalds 	} while (pmd++, addr = next, addr != end);
1457da4d641SPeter Zijlstra 
146*72403b4aSMel Gorman 	if (nr_huge_updates)
147*72403b4aSMel Gorman 		count_vm_numa_events(NUMA_HUGE_PTE_UPDATES, nr_huge_updates);
1487da4d641SPeter Zijlstra 	return pages;
1491da177e4SLinus Torvalds }
1501da177e4SLinus Torvalds 
1517d12efaeSAndrew Morton static inline unsigned long change_pud_range(struct vm_area_struct *vma,
1527d12efaeSAndrew Morton 		pgd_t *pgd, unsigned long addr, unsigned long end,
1537d12efaeSAndrew Morton 		pgprot_t newprot, int dirty_accountable, int prot_numa)
1541da177e4SLinus Torvalds {
1551da177e4SLinus Torvalds 	pud_t *pud;
1561da177e4SLinus Torvalds 	unsigned long next;
1577da4d641SPeter Zijlstra 	unsigned long pages = 0;
1581da177e4SLinus Torvalds 
1591da177e4SLinus Torvalds 	pud = pud_offset(pgd, addr);
1601da177e4SLinus Torvalds 	do {
1611da177e4SLinus Torvalds 		next = pud_addr_end(addr, end);
1621da177e4SLinus Torvalds 		if (pud_none_or_clear_bad(pud))
1631da177e4SLinus Torvalds 			continue;
1647da4d641SPeter Zijlstra 		pages += change_pmd_range(vma, pud, addr, next, newprot,
1654b10e7d5SMel Gorman 				 dirty_accountable, prot_numa);
1661da177e4SLinus Torvalds 	} while (pud++, addr = next, addr != end);
1677da4d641SPeter Zijlstra 
1687da4d641SPeter Zijlstra 	return pages;
1691da177e4SLinus Torvalds }
1701da177e4SLinus Torvalds 
1717da4d641SPeter Zijlstra static unsigned long change_protection_range(struct vm_area_struct *vma,
172c1e6098bSPeter Zijlstra 		unsigned long addr, unsigned long end, pgprot_t newprot,
1734b10e7d5SMel Gorman 		int dirty_accountable, int prot_numa)
1741da177e4SLinus Torvalds {
1751da177e4SLinus Torvalds 	struct mm_struct *mm = vma->vm_mm;
1761da177e4SLinus Torvalds 	pgd_t *pgd;
1771da177e4SLinus Torvalds 	unsigned long next;
1781da177e4SLinus Torvalds 	unsigned long start = addr;
1797da4d641SPeter Zijlstra 	unsigned long pages = 0;
1801da177e4SLinus Torvalds 
1811da177e4SLinus Torvalds 	BUG_ON(addr >= end);
1821da177e4SLinus Torvalds 	pgd = pgd_offset(mm, addr);
1831da177e4SLinus Torvalds 	flush_cache_range(vma, addr, end);
1841da177e4SLinus Torvalds 	do {
1851da177e4SLinus Torvalds 		next = pgd_addr_end(addr, end);
1861da177e4SLinus Torvalds 		if (pgd_none_or_clear_bad(pgd))
1871da177e4SLinus Torvalds 			continue;
1887da4d641SPeter Zijlstra 		pages += change_pud_range(vma, pgd, addr, next, newprot,
1894b10e7d5SMel Gorman 				 dirty_accountable, prot_numa);
1901da177e4SLinus Torvalds 	} while (pgd++, addr = next, addr != end);
1917da4d641SPeter Zijlstra 
1921233d588SIngo Molnar 	/* Only flush the TLB if we actually modified any entries: */
1931233d588SIngo Molnar 	if (pages)
1941da177e4SLinus Torvalds 		flush_tlb_range(vma, start, end);
1957da4d641SPeter Zijlstra 
1967da4d641SPeter Zijlstra 	return pages;
1977da4d641SPeter Zijlstra }
1987da4d641SPeter Zijlstra 
1997da4d641SPeter Zijlstra unsigned long change_protection(struct vm_area_struct *vma, unsigned long start,
2007da4d641SPeter Zijlstra 		       unsigned long end, pgprot_t newprot,
2014b10e7d5SMel Gorman 		       int dirty_accountable, int prot_numa)
2027da4d641SPeter Zijlstra {
2037da4d641SPeter Zijlstra 	struct mm_struct *mm = vma->vm_mm;
2047da4d641SPeter Zijlstra 	unsigned long pages;
2057da4d641SPeter Zijlstra 
2067da4d641SPeter Zijlstra 	mmu_notifier_invalidate_range_start(mm, start, end);
2077da4d641SPeter Zijlstra 	if (is_vm_hugetlb_page(vma))
2087da4d641SPeter Zijlstra 		pages = hugetlb_change_protection(vma, start, end, newprot);
2097da4d641SPeter Zijlstra 	else
2104b10e7d5SMel Gorman 		pages = change_protection_range(vma, start, end, newprot, dirty_accountable, prot_numa);
2117da4d641SPeter Zijlstra 	mmu_notifier_invalidate_range_end(mm, start, end);
2127da4d641SPeter Zijlstra 
2137da4d641SPeter Zijlstra 	return pages;
2141da177e4SLinus Torvalds }
2151da177e4SLinus Torvalds 
216b6a2fea3SOllie Wild int
2171da177e4SLinus Torvalds mprotect_fixup(struct vm_area_struct *vma, struct vm_area_struct **pprev,
2181da177e4SLinus Torvalds 	unsigned long start, unsigned long end, unsigned long newflags)
2191da177e4SLinus Torvalds {
2201da177e4SLinus Torvalds 	struct mm_struct *mm = vma->vm_mm;
2211da177e4SLinus Torvalds 	unsigned long oldflags = vma->vm_flags;
2221da177e4SLinus Torvalds 	long nrpages = (end - start) >> PAGE_SHIFT;
2231da177e4SLinus Torvalds 	unsigned long charged = 0;
2241da177e4SLinus Torvalds 	pgoff_t pgoff;
2251da177e4SLinus Torvalds 	int error;
226c1e6098bSPeter Zijlstra 	int dirty_accountable = 0;
2271da177e4SLinus Torvalds 
2281da177e4SLinus Torvalds 	if (newflags == oldflags) {
2291da177e4SLinus Torvalds 		*pprev = vma;
2301da177e4SLinus Torvalds 		return 0;
2311da177e4SLinus Torvalds 	}
2321da177e4SLinus Torvalds 
2331da177e4SLinus Torvalds 	/*
2341da177e4SLinus Torvalds 	 * If we make a private mapping writable we increase our commit;
2351da177e4SLinus Torvalds 	 * but (without finer accounting) cannot reduce our commit if we
2365a6fe125SMel Gorman 	 * make it unwritable again. hugetlb mapping were accounted for
2375a6fe125SMel Gorman 	 * even if read-only so there is no need to account for them here
2381da177e4SLinus Torvalds 	 */
2391da177e4SLinus Torvalds 	if (newflags & VM_WRITE) {
2405a6fe125SMel Gorman 		if (!(oldflags & (VM_ACCOUNT|VM_WRITE|VM_HUGETLB|
241cdfd4325SAndy Whitcroft 						VM_SHARED|VM_NORESERVE))) {
2421da177e4SLinus Torvalds 			charged = nrpages;
243191c5424SAl Viro 			if (security_vm_enough_memory_mm(mm, charged))
2441da177e4SLinus Torvalds 				return -ENOMEM;
2451da177e4SLinus Torvalds 			newflags |= VM_ACCOUNT;
2461da177e4SLinus Torvalds 		}
2471da177e4SLinus Torvalds 	}
2481da177e4SLinus Torvalds 
2491da177e4SLinus Torvalds 	/*
2501da177e4SLinus Torvalds 	 * First try to merge with previous and/or next vma.
2511da177e4SLinus Torvalds 	 */
2521da177e4SLinus Torvalds 	pgoff = vma->vm_pgoff + ((start - vma->vm_start) >> PAGE_SHIFT);
2531da177e4SLinus Torvalds 	*pprev = vma_merge(mm, *pprev, start, end, newflags,
2541da177e4SLinus Torvalds 			vma->anon_vma, vma->vm_file, pgoff, vma_policy(vma));
2551da177e4SLinus Torvalds 	if (*pprev) {
2561da177e4SLinus Torvalds 		vma = *pprev;
2571da177e4SLinus Torvalds 		goto success;
2581da177e4SLinus Torvalds 	}
2591da177e4SLinus Torvalds 
2601da177e4SLinus Torvalds 	*pprev = vma;
2611da177e4SLinus Torvalds 
2621da177e4SLinus Torvalds 	if (start != vma->vm_start) {
2631da177e4SLinus Torvalds 		error = split_vma(mm, vma, start, 1);
2641da177e4SLinus Torvalds 		if (error)
2651da177e4SLinus Torvalds 			goto fail;
2661da177e4SLinus Torvalds 	}
2671da177e4SLinus Torvalds 
2681da177e4SLinus Torvalds 	if (end != vma->vm_end) {
2691da177e4SLinus Torvalds 		error = split_vma(mm, vma, end, 0);
2701da177e4SLinus Torvalds 		if (error)
2711da177e4SLinus Torvalds 			goto fail;
2721da177e4SLinus Torvalds 	}
2731da177e4SLinus Torvalds 
2741da177e4SLinus Torvalds success:
2751da177e4SLinus Torvalds 	/*
2761da177e4SLinus Torvalds 	 * vm_flags and vm_page_prot are protected by the mmap_sem
2771da177e4SLinus Torvalds 	 * held in write mode.
2781da177e4SLinus Torvalds 	 */
2791da177e4SLinus Torvalds 	vma->vm_flags = newflags;
2801c12c4cfSVenki Pallipadi 	vma->vm_page_prot = pgprot_modify(vma->vm_page_prot,
2811c12c4cfSVenki Pallipadi 					  vm_get_page_prot(newflags));
2821c12c4cfSVenki Pallipadi 
283c1e6098bSPeter Zijlstra 	if (vma_wants_writenotify(vma)) {
2841ddd439eSHugh Dickins 		vma->vm_page_prot = vm_get_page_prot(newflags & ~VM_SHARED);
285c1e6098bSPeter Zijlstra 		dirty_accountable = 1;
286c1e6098bSPeter Zijlstra 	}
287d08b3851SPeter Zijlstra 
2887d12efaeSAndrew Morton 	change_protection(vma, start, end, vma->vm_page_prot,
2897d12efaeSAndrew Morton 			  dirty_accountable, 0);
2907da4d641SPeter Zijlstra 
291ab50b8edSHugh Dickins 	vm_stat_account(mm, oldflags, vma->vm_file, -nrpages);
292ab50b8edSHugh Dickins 	vm_stat_account(mm, newflags, vma->vm_file, nrpages);
29363bfd738SPekka Enberg 	perf_event_mmap(vma);
2941da177e4SLinus Torvalds 	return 0;
2951da177e4SLinus Torvalds 
2961da177e4SLinus Torvalds fail:
2971da177e4SLinus Torvalds 	vm_unacct_memory(charged);
2981da177e4SLinus Torvalds 	return error;
2991da177e4SLinus Torvalds }
3001da177e4SLinus Torvalds 
3016a6160a7SHeiko Carstens SYSCALL_DEFINE3(mprotect, unsigned long, start, size_t, len,
3026a6160a7SHeiko Carstens 		unsigned long, prot)
3031da177e4SLinus Torvalds {
3041da177e4SLinus Torvalds 	unsigned long vm_flags, nstart, end, tmp, reqprot;
3051da177e4SLinus Torvalds 	struct vm_area_struct *vma, *prev;
3061da177e4SLinus Torvalds 	int error = -EINVAL;
3071da177e4SLinus Torvalds 	const int grows = prot & (PROT_GROWSDOWN|PROT_GROWSUP);
3081da177e4SLinus Torvalds 	prot &= ~(PROT_GROWSDOWN|PROT_GROWSUP);
3091da177e4SLinus Torvalds 	if (grows == (PROT_GROWSDOWN|PROT_GROWSUP)) /* can't be both */
3101da177e4SLinus Torvalds 		return -EINVAL;
3111da177e4SLinus Torvalds 
3121da177e4SLinus Torvalds 	if (start & ~PAGE_MASK)
3131da177e4SLinus Torvalds 		return -EINVAL;
3141da177e4SLinus Torvalds 	if (!len)
3151da177e4SLinus Torvalds 		return 0;
3161da177e4SLinus Torvalds 	len = PAGE_ALIGN(len);
3171da177e4SLinus Torvalds 	end = start + len;
3181da177e4SLinus Torvalds 	if (end <= start)
3191da177e4SLinus Torvalds 		return -ENOMEM;
320b845f313SDave Kleikamp 	if (!arch_validate_prot(prot))
3211da177e4SLinus Torvalds 		return -EINVAL;
3221da177e4SLinus Torvalds 
3231da177e4SLinus Torvalds 	reqprot = prot;
3241da177e4SLinus Torvalds 	/*
3251da177e4SLinus Torvalds 	 * Does the application expect PROT_READ to imply PROT_EXEC:
3261da177e4SLinus Torvalds 	 */
327b344e05cSHua Zhong 	if ((prot & PROT_READ) && (current->personality & READ_IMPLIES_EXEC))
3281da177e4SLinus Torvalds 		prot |= PROT_EXEC;
3291da177e4SLinus Torvalds 
3301da177e4SLinus Torvalds 	vm_flags = calc_vm_prot_bits(prot);
3311da177e4SLinus Torvalds 
3321da177e4SLinus Torvalds 	down_write(&current->mm->mmap_sem);
3331da177e4SLinus Torvalds 
334097d5910SLinus Torvalds 	vma = find_vma(current->mm, start);
3351da177e4SLinus Torvalds 	error = -ENOMEM;
3361da177e4SLinus Torvalds 	if (!vma)
3371da177e4SLinus Torvalds 		goto out;
338097d5910SLinus Torvalds 	prev = vma->vm_prev;
3391da177e4SLinus Torvalds 	if (unlikely(grows & PROT_GROWSDOWN)) {
3401da177e4SLinus Torvalds 		if (vma->vm_start >= end)
3411da177e4SLinus Torvalds 			goto out;
3421da177e4SLinus Torvalds 		start = vma->vm_start;
3431da177e4SLinus Torvalds 		error = -EINVAL;
3441da177e4SLinus Torvalds 		if (!(vma->vm_flags & VM_GROWSDOWN))
3451da177e4SLinus Torvalds 			goto out;
3467d12efaeSAndrew Morton 	} else {
3471da177e4SLinus Torvalds 		if (vma->vm_start > start)
3481da177e4SLinus Torvalds 			goto out;
3491da177e4SLinus Torvalds 		if (unlikely(grows & PROT_GROWSUP)) {
3501da177e4SLinus Torvalds 			end = vma->vm_end;
3511da177e4SLinus Torvalds 			error = -EINVAL;
3521da177e4SLinus Torvalds 			if (!(vma->vm_flags & VM_GROWSUP))
3531da177e4SLinus Torvalds 				goto out;
3541da177e4SLinus Torvalds 		}
3551da177e4SLinus Torvalds 	}
3561da177e4SLinus Torvalds 	if (start > vma->vm_start)
3571da177e4SLinus Torvalds 		prev = vma;
3581da177e4SLinus Torvalds 
3591da177e4SLinus Torvalds 	for (nstart = start ; ; ) {
3601da177e4SLinus Torvalds 		unsigned long newflags;
3611da177e4SLinus Torvalds 
3621da177e4SLinus Torvalds 		/* Here we know that vma->vm_start <= nstart < vma->vm_end. */
3631da177e4SLinus Torvalds 
3647d12efaeSAndrew Morton 		newflags = vm_flags;
3657d12efaeSAndrew Morton 		newflags |= (vma->vm_flags & ~(VM_READ | VM_WRITE | VM_EXEC));
3661da177e4SLinus Torvalds 
3677e2cff42SPaolo 'Blaisorblade' Giarrusso 		/* newflags >> 4 shift VM_MAY% in place of VM_% */
3687e2cff42SPaolo 'Blaisorblade' Giarrusso 		if ((newflags & ~(newflags >> 4)) & (VM_READ | VM_WRITE | VM_EXEC)) {
3691da177e4SLinus Torvalds 			error = -EACCES;
3701da177e4SLinus Torvalds 			goto out;
3711da177e4SLinus Torvalds 		}
3721da177e4SLinus Torvalds 
3731da177e4SLinus Torvalds 		error = security_file_mprotect(vma, reqprot, prot);
3741da177e4SLinus Torvalds 		if (error)
3751da177e4SLinus Torvalds 			goto out;
3761da177e4SLinus Torvalds 
3771da177e4SLinus Torvalds 		tmp = vma->vm_end;
3781da177e4SLinus Torvalds 		if (tmp > end)
3791da177e4SLinus Torvalds 			tmp = end;
3801da177e4SLinus Torvalds 		error = mprotect_fixup(vma, &prev, nstart, tmp, newflags);
3811da177e4SLinus Torvalds 		if (error)
3821da177e4SLinus Torvalds 			goto out;
3831da177e4SLinus Torvalds 		nstart = tmp;
3841da177e4SLinus Torvalds 
3851da177e4SLinus Torvalds 		if (nstart < prev->vm_end)
3861da177e4SLinus Torvalds 			nstart = prev->vm_end;
3871da177e4SLinus Torvalds 		if (nstart >= end)
3881da177e4SLinus Torvalds 			goto out;
3891da177e4SLinus Torvalds 
3901da177e4SLinus Torvalds 		vma = prev->vm_next;
3911da177e4SLinus Torvalds 		if (!vma || vma->vm_start != nstart) {
3921da177e4SLinus Torvalds 			error = -ENOMEM;
3931da177e4SLinus Torvalds 			goto out;
3941da177e4SLinus Torvalds 		}
3951da177e4SLinus Torvalds 	}
3961da177e4SLinus Torvalds out:
3971da177e4SLinus Torvalds 	up_write(&current->mm->mmap_sem);
3981da177e4SLinus Torvalds 	return error;
3991da177e4SLinus Torvalds }
400