xref: /openbmc/linux/mm/mprotect.c (revision cdfd4325c0d878679bd6a3ba8285b71d9980e3c0)
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  *
71da177e4SLinus Torvalds  *  Address space accounting code	<alan@redhat.com>
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/slab.h>
141da177e4SLinus Torvalds #include <linux/shm.h>
151da177e4SLinus Torvalds #include <linux/mman.h>
161da177e4SLinus Torvalds #include <linux/fs.h>
171da177e4SLinus Torvalds #include <linux/highmem.h>
181da177e4SLinus Torvalds #include <linux/security.h>
191da177e4SLinus Torvalds #include <linux/mempolicy.h>
201da177e4SLinus Torvalds #include <linux/personality.h>
211da177e4SLinus Torvalds #include <linux/syscalls.h>
220697212aSChristoph Lameter #include <linux/swap.h>
230697212aSChristoph Lameter #include <linux/swapops.h>
241da177e4SLinus Torvalds #include <asm/uaccess.h>
251da177e4SLinus Torvalds #include <asm/pgtable.h>
261da177e4SLinus Torvalds #include <asm/cacheflush.h>
271da177e4SLinus Torvalds #include <asm/tlbflush.h>
281da177e4SLinus Torvalds 
291c12c4cfSVenki Pallipadi #ifndef pgprot_modify
301c12c4cfSVenki Pallipadi static inline pgprot_t pgprot_modify(pgprot_t oldprot, pgprot_t newprot)
311c12c4cfSVenki Pallipadi {
321c12c4cfSVenki Pallipadi 	return newprot;
331c12c4cfSVenki Pallipadi }
341c12c4cfSVenki Pallipadi #endif
351c12c4cfSVenki Pallipadi 
361da177e4SLinus Torvalds static void change_pte_range(struct mm_struct *mm, pmd_t *pmd,
37c1e6098bSPeter Zijlstra 		unsigned long addr, unsigned long end, pgprot_t newprot,
38c1e6098bSPeter Zijlstra 		int dirty_accountable)
391da177e4SLinus Torvalds {
400697212aSChristoph Lameter 	pte_t *pte, oldpte;
41705e87c0SHugh Dickins 	spinlock_t *ptl;
421da177e4SLinus Torvalds 
43705e87c0SHugh Dickins 	pte = pte_offset_map_lock(mm, pmd, addr, &ptl);
446606c3e0SZachary Amsden 	arch_enter_lazy_mmu_mode();
451da177e4SLinus Torvalds 	do {
460697212aSChristoph Lameter 		oldpte = *pte;
470697212aSChristoph Lameter 		if (pte_present(oldpte)) {
481da177e4SLinus Torvalds 			pte_t ptent;
491da177e4SLinus Torvalds 
501ea0704eSJeremy Fitzhardinge 			ptent = ptep_modify_prot_start(mm, addr, pte);
51c1e6098bSPeter Zijlstra 			ptent = pte_modify(ptent, newprot);
521ea0704eSJeremy Fitzhardinge 
53c1e6098bSPeter Zijlstra 			/*
54c1e6098bSPeter Zijlstra 			 * Avoid taking write faults for pages we know to be
55c1e6098bSPeter Zijlstra 			 * dirty.
56c1e6098bSPeter Zijlstra 			 */
57c1e6098bSPeter Zijlstra 			if (dirty_accountable && pte_dirty(ptent))
58c1e6098bSPeter Zijlstra 				ptent = pte_mkwrite(ptent);
591ea0704eSJeremy Fitzhardinge 
601ea0704eSJeremy Fitzhardinge 			ptep_modify_prot_commit(mm, addr, pte, ptent);
610697212aSChristoph Lameter #ifdef CONFIG_MIGRATION
620697212aSChristoph Lameter 		} else if (!pte_file(oldpte)) {
630697212aSChristoph Lameter 			swp_entry_t entry = pte_to_swp_entry(oldpte);
640697212aSChristoph Lameter 
650697212aSChristoph Lameter 			if (is_write_migration_entry(entry)) {
660697212aSChristoph Lameter 				/*
670697212aSChristoph Lameter 				 * A protection check is difficult so
680697212aSChristoph Lameter 				 * just be safe and disable write
690697212aSChristoph Lameter 				 */
700697212aSChristoph Lameter 				make_migration_entry_read(&entry);
710697212aSChristoph Lameter 				set_pte_at(mm, addr, pte,
720697212aSChristoph Lameter 					swp_entry_to_pte(entry));
731da177e4SLinus Torvalds 			}
740697212aSChristoph Lameter #endif
750697212aSChristoph Lameter 		}
760697212aSChristoph Lameter 
771da177e4SLinus Torvalds 	} while (pte++, addr += PAGE_SIZE, addr != end);
786606c3e0SZachary Amsden 	arch_leave_lazy_mmu_mode();
79705e87c0SHugh Dickins 	pte_unmap_unlock(pte - 1, ptl);
801da177e4SLinus Torvalds }
811da177e4SLinus Torvalds 
821da177e4SLinus Torvalds static inline void change_pmd_range(struct mm_struct *mm, pud_t *pud,
83c1e6098bSPeter Zijlstra 		unsigned long addr, unsigned long end, pgprot_t newprot,
84c1e6098bSPeter Zijlstra 		int dirty_accountable)
851da177e4SLinus Torvalds {
861da177e4SLinus Torvalds 	pmd_t *pmd;
871da177e4SLinus Torvalds 	unsigned long next;
881da177e4SLinus Torvalds 
891da177e4SLinus Torvalds 	pmd = pmd_offset(pud, addr);
901da177e4SLinus Torvalds 	do {
911da177e4SLinus Torvalds 		next = pmd_addr_end(addr, end);
921da177e4SLinus Torvalds 		if (pmd_none_or_clear_bad(pmd))
931da177e4SLinus Torvalds 			continue;
94c1e6098bSPeter Zijlstra 		change_pte_range(mm, pmd, addr, next, newprot, dirty_accountable);
951da177e4SLinus Torvalds 	} while (pmd++, addr = next, addr != end);
961da177e4SLinus Torvalds }
971da177e4SLinus Torvalds 
981da177e4SLinus Torvalds static inline void change_pud_range(struct mm_struct *mm, pgd_t *pgd,
99c1e6098bSPeter Zijlstra 		unsigned long addr, unsigned long end, pgprot_t newprot,
100c1e6098bSPeter Zijlstra 		int dirty_accountable)
1011da177e4SLinus Torvalds {
1021da177e4SLinus Torvalds 	pud_t *pud;
1031da177e4SLinus Torvalds 	unsigned long next;
1041da177e4SLinus Torvalds 
1051da177e4SLinus Torvalds 	pud = pud_offset(pgd, addr);
1061da177e4SLinus Torvalds 	do {
1071da177e4SLinus Torvalds 		next = pud_addr_end(addr, end);
1081da177e4SLinus Torvalds 		if (pud_none_or_clear_bad(pud))
1091da177e4SLinus Torvalds 			continue;
110c1e6098bSPeter Zijlstra 		change_pmd_range(mm, pud, addr, next, newprot, dirty_accountable);
1111da177e4SLinus Torvalds 	} while (pud++, addr = next, addr != end);
1121da177e4SLinus Torvalds }
1131da177e4SLinus Torvalds 
1141da177e4SLinus Torvalds static void change_protection(struct vm_area_struct *vma,
115c1e6098bSPeter Zijlstra 		unsigned long addr, unsigned long end, pgprot_t newprot,
116c1e6098bSPeter Zijlstra 		int dirty_accountable)
1171da177e4SLinus Torvalds {
1181da177e4SLinus Torvalds 	struct mm_struct *mm = vma->vm_mm;
1191da177e4SLinus Torvalds 	pgd_t *pgd;
1201da177e4SLinus Torvalds 	unsigned long next;
1211da177e4SLinus Torvalds 	unsigned long start = addr;
1221da177e4SLinus Torvalds 
1231da177e4SLinus Torvalds 	BUG_ON(addr >= end);
1241da177e4SLinus Torvalds 	pgd = pgd_offset(mm, addr);
1251da177e4SLinus Torvalds 	flush_cache_range(vma, addr, end);
1261da177e4SLinus Torvalds 	do {
1271da177e4SLinus Torvalds 		next = pgd_addr_end(addr, end);
1281da177e4SLinus Torvalds 		if (pgd_none_or_clear_bad(pgd))
1291da177e4SLinus Torvalds 			continue;
130c1e6098bSPeter Zijlstra 		change_pud_range(mm, pgd, addr, next, newprot, dirty_accountable);
1311da177e4SLinus Torvalds 	} while (pgd++, addr = next, addr != end);
1321da177e4SLinus Torvalds 	flush_tlb_range(vma, start, end);
1331da177e4SLinus Torvalds }
1341da177e4SLinus Torvalds 
135b6a2fea3SOllie Wild int
1361da177e4SLinus Torvalds mprotect_fixup(struct vm_area_struct *vma, struct vm_area_struct **pprev,
1371da177e4SLinus Torvalds 	unsigned long start, unsigned long end, unsigned long newflags)
1381da177e4SLinus Torvalds {
1391da177e4SLinus Torvalds 	struct mm_struct *mm = vma->vm_mm;
1401da177e4SLinus Torvalds 	unsigned long oldflags = vma->vm_flags;
1411da177e4SLinus Torvalds 	long nrpages = (end - start) >> PAGE_SHIFT;
1421da177e4SLinus Torvalds 	unsigned long charged = 0;
1431da177e4SLinus Torvalds 	pgoff_t pgoff;
1441da177e4SLinus Torvalds 	int error;
145c1e6098bSPeter Zijlstra 	int dirty_accountable = 0;
1461da177e4SLinus Torvalds 
1471da177e4SLinus Torvalds 	if (newflags == oldflags) {
1481da177e4SLinus Torvalds 		*pprev = vma;
1491da177e4SLinus Torvalds 		return 0;
1501da177e4SLinus Torvalds 	}
1511da177e4SLinus Torvalds 
1521da177e4SLinus Torvalds 	/*
1531da177e4SLinus Torvalds 	 * If we make a private mapping writable we increase our commit;
1541da177e4SLinus Torvalds 	 * but (without finer accounting) cannot reduce our commit if we
1551da177e4SLinus Torvalds 	 * make it unwritable again.
1561da177e4SLinus Torvalds 	 */
1571da177e4SLinus Torvalds 	if (newflags & VM_WRITE) {
158*cdfd4325SAndy Whitcroft 		if (!(oldflags & (VM_ACCOUNT|VM_WRITE|
159*cdfd4325SAndy Whitcroft 						VM_SHARED|VM_NORESERVE))) {
1601da177e4SLinus Torvalds 			charged = nrpages;
1611da177e4SLinus Torvalds 			if (security_vm_enough_memory(charged))
1621da177e4SLinus Torvalds 				return -ENOMEM;
1631da177e4SLinus Torvalds 			newflags |= VM_ACCOUNT;
1641da177e4SLinus Torvalds 		}
1651da177e4SLinus Torvalds 	}
1661da177e4SLinus Torvalds 
1671da177e4SLinus Torvalds 	/*
1681da177e4SLinus Torvalds 	 * First try to merge with previous and/or next vma.
1691da177e4SLinus Torvalds 	 */
1701da177e4SLinus Torvalds 	pgoff = vma->vm_pgoff + ((start - vma->vm_start) >> PAGE_SHIFT);
1711da177e4SLinus Torvalds 	*pprev = vma_merge(mm, *pprev, start, end, newflags,
1721da177e4SLinus Torvalds 			vma->anon_vma, vma->vm_file, pgoff, vma_policy(vma));
1731da177e4SLinus Torvalds 	if (*pprev) {
1741da177e4SLinus Torvalds 		vma = *pprev;
1751da177e4SLinus Torvalds 		goto success;
1761da177e4SLinus Torvalds 	}
1771da177e4SLinus Torvalds 
1781da177e4SLinus Torvalds 	*pprev = vma;
1791da177e4SLinus Torvalds 
1801da177e4SLinus Torvalds 	if (start != vma->vm_start) {
1811da177e4SLinus Torvalds 		error = split_vma(mm, vma, start, 1);
1821da177e4SLinus Torvalds 		if (error)
1831da177e4SLinus Torvalds 			goto fail;
1841da177e4SLinus Torvalds 	}
1851da177e4SLinus Torvalds 
1861da177e4SLinus Torvalds 	if (end != vma->vm_end) {
1871da177e4SLinus Torvalds 		error = split_vma(mm, vma, end, 0);
1881da177e4SLinus Torvalds 		if (error)
1891da177e4SLinus Torvalds 			goto fail;
1901da177e4SLinus Torvalds 	}
1911da177e4SLinus Torvalds 
1921da177e4SLinus Torvalds success:
1931da177e4SLinus Torvalds 	/*
1941da177e4SLinus Torvalds 	 * vm_flags and vm_page_prot are protected by the mmap_sem
1951da177e4SLinus Torvalds 	 * held in write mode.
1961da177e4SLinus Torvalds 	 */
1971da177e4SLinus Torvalds 	vma->vm_flags = newflags;
1981c12c4cfSVenki Pallipadi 	vma->vm_page_prot = pgprot_modify(vma->vm_page_prot,
1991c12c4cfSVenki Pallipadi 					  vm_get_page_prot(newflags));
2001c12c4cfSVenki Pallipadi 
201c1e6098bSPeter Zijlstra 	if (vma_wants_writenotify(vma)) {
2021ddd439eSHugh Dickins 		vma->vm_page_prot = vm_get_page_prot(newflags & ~VM_SHARED);
203c1e6098bSPeter Zijlstra 		dirty_accountable = 1;
204c1e6098bSPeter Zijlstra 	}
205d08b3851SPeter Zijlstra 
2068f860591SZhang, Yanmin 	if (is_vm_hugetlb_page(vma))
207d08b3851SPeter Zijlstra 		hugetlb_change_protection(vma, start, end, vma->vm_page_prot);
2088f860591SZhang, Yanmin 	else
209c1e6098bSPeter Zijlstra 		change_protection(vma, start, end, vma->vm_page_prot, dirty_accountable);
210ab50b8edSHugh Dickins 	vm_stat_account(mm, oldflags, vma->vm_file, -nrpages);
211ab50b8edSHugh Dickins 	vm_stat_account(mm, newflags, vma->vm_file, nrpages);
2121da177e4SLinus Torvalds 	return 0;
2131da177e4SLinus Torvalds 
2141da177e4SLinus Torvalds fail:
2151da177e4SLinus Torvalds 	vm_unacct_memory(charged);
2161da177e4SLinus Torvalds 	return error;
2171da177e4SLinus Torvalds }
2181da177e4SLinus Torvalds 
2191da177e4SLinus Torvalds asmlinkage long
2201da177e4SLinus Torvalds sys_mprotect(unsigned long start, size_t len, unsigned long prot)
2211da177e4SLinus Torvalds {
2221da177e4SLinus Torvalds 	unsigned long vm_flags, nstart, end, tmp, reqprot;
2231da177e4SLinus Torvalds 	struct vm_area_struct *vma, *prev;
2241da177e4SLinus Torvalds 	int error = -EINVAL;
2251da177e4SLinus Torvalds 	const int grows = prot & (PROT_GROWSDOWN|PROT_GROWSUP);
2261da177e4SLinus Torvalds 	prot &= ~(PROT_GROWSDOWN|PROT_GROWSUP);
2271da177e4SLinus Torvalds 	if (grows == (PROT_GROWSDOWN|PROT_GROWSUP)) /* can't be both */
2281da177e4SLinus Torvalds 		return -EINVAL;
2291da177e4SLinus Torvalds 
2301da177e4SLinus Torvalds 	if (start & ~PAGE_MASK)
2311da177e4SLinus Torvalds 		return -EINVAL;
2321da177e4SLinus Torvalds 	if (!len)
2331da177e4SLinus Torvalds 		return 0;
2341da177e4SLinus Torvalds 	len = PAGE_ALIGN(len);
2351da177e4SLinus Torvalds 	end = start + len;
2361da177e4SLinus Torvalds 	if (end <= start)
2371da177e4SLinus Torvalds 		return -ENOMEM;
238b845f313SDave Kleikamp 	if (!arch_validate_prot(prot))
2391da177e4SLinus Torvalds 		return -EINVAL;
2401da177e4SLinus Torvalds 
2411da177e4SLinus Torvalds 	reqprot = prot;
2421da177e4SLinus Torvalds 	/*
2431da177e4SLinus Torvalds 	 * Does the application expect PROT_READ to imply PROT_EXEC:
2441da177e4SLinus Torvalds 	 */
245b344e05cSHua Zhong 	if ((prot & PROT_READ) && (current->personality & READ_IMPLIES_EXEC))
2461da177e4SLinus Torvalds 		prot |= PROT_EXEC;
2471da177e4SLinus Torvalds 
2481da177e4SLinus Torvalds 	vm_flags = calc_vm_prot_bits(prot);
2491da177e4SLinus Torvalds 
2501da177e4SLinus Torvalds 	down_write(&current->mm->mmap_sem);
2511da177e4SLinus Torvalds 
2521da177e4SLinus Torvalds 	vma = find_vma_prev(current->mm, start, &prev);
2531da177e4SLinus Torvalds 	error = -ENOMEM;
2541da177e4SLinus Torvalds 	if (!vma)
2551da177e4SLinus Torvalds 		goto out;
2561da177e4SLinus Torvalds 	if (unlikely(grows & PROT_GROWSDOWN)) {
2571da177e4SLinus Torvalds 		if (vma->vm_start >= end)
2581da177e4SLinus Torvalds 			goto out;
2591da177e4SLinus Torvalds 		start = vma->vm_start;
2601da177e4SLinus Torvalds 		error = -EINVAL;
2611da177e4SLinus Torvalds 		if (!(vma->vm_flags & VM_GROWSDOWN))
2621da177e4SLinus Torvalds 			goto out;
2631da177e4SLinus Torvalds 	}
2641da177e4SLinus Torvalds 	else {
2651da177e4SLinus Torvalds 		if (vma->vm_start > start)
2661da177e4SLinus Torvalds 			goto out;
2671da177e4SLinus Torvalds 		if (unlikely(grows & PROT_GROWSUP)) {
2681da177e4SLinus Torvalds 			end = vma->vm_end;
2691da177e4SLinus Torvalds 			error = -EINVAL;
2701da177e4SLinus Torvalds 			if (!(vma->vm_flags & VM_GROWSUP))
2711da177e4SLinus Torvalds 				goto out;
2721da177e4SLinus Torvalds 		}
2731da177e4SLinus Torvalds 	}
2741da177e4SLinus Torvalds 	if (start > vma->vm_start)
2751da177e4SLinus Torvalds 		prev = vma;
2761da177e4SLinus Torvalds 
2771da177e4SLinus Torvalds 	for (nstart = start ; ; ) {
2781da177e4SLinus Torvalds 		unsigned long newflags;
2791da177e4SLinus Torvalds 
2801da177e4SLinus Torvalds 		/* Here we know that  vma->vm_start <= nstart < vma->vm_end. */
2811da177e4SLinus Torvalds 
2821da177e4SLinus Torvalds 		newflags = vm_flags | (vma->vm_flags & ~(VM_READ | VM_WRITE | VM_EXEC));
2831da177e4SLinus Torvalds 
2847e2cff42SPaolo 'Blaisorblade' Giarrusso 		/* newflags >> 4 shift VM_MAY% in place of VM_% */
2857e2cff42SPaolo 'Blaisorblade' Giarrusso 		if ((newflags & ~(newflags >> 4)) & (VM_READ | VM_WRITE | VM_EXEC)) {
2861da177e4SLinus Torvalds 			error = -EACCES;
2871da177e4SLinus Torvalds 			goto out;
2881da177e4SLinus Torvalds 		}
2891da177e4SLinus Torvalds 
2901da177e4SLinus Torvalds 		error = security_file_mprotect(vma, reqprot, prot);
2911da177e4SLinus Torvalds 		if (error)
2921da177e4SLinus Torvalds 			goto out;
2931da177e4SLinus Torvalds 
2941da177e4SLinus Torvalds 		tmp = vma->vm_end;
2951da177e4SLinus Torvalds 		if (tmp > end)
2961da177e4SLinus Torvalds 			tmp = end;
2971da177e4SLinus Torvalds 		error = mprotect_fixup(vma, &prev, nstart, tmp, newflags);
2981da177e4SLinus Torvalds 		if (error)
2991da177e4SLinus Torvalds 			goto out;
3001da177e4SLinus Torvalds 		nstart = tmp;
3011da177e4SLinus Torvalds 
3021da177e4SLinus Torvalds 		if (nstart < prev->vm_end)
3031da177e4SLinus Torvalds 			nstart = prev->vm_end;
3041da177e4SLinus Torvalds 		if (nstart >= end)
3051da177e4SLinus Torvalds 			goto out;
3061da177e4SLinus Torvalds 
3071da177e4SLinus Torvalds 		vma = prev->vm_next;
3081da177e4SLinus Torvalds 		if (!vma || vma->vm_start != nstart) {
3091da177e4SLinus Torvalds 			error = -ENOMEM;
3101da177e4SLinus Torvalds 			goto out;
3111da177e4SLinus Torvalds 		}
3121da177e4SLinus Torvalds 	}
3131da177e4SLinus Torvalds out:
3141da177e4SLinus Torvalds 	up_write(&current->mm->mmap_sem);
3151da177e4SLinus Torvalds 	return error;
3161da177e4SLinus Torvalds }
317