mprotect.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445
  1. /*
  2. * mm/mprotect.c
  3. *
  4. * (C) Copyright 1994 Linus Torvalds
  5. * (C) Copyright 2002 Christoph Hellwig
  6. *
  7. * Address space accounting code <alan@lxorguk.ukuu.org.uk>
  8. * (C) Copyright 2002 Red Hat Inc, All Rights Reserved
  9. */
  10. #include <linux/mm.h>
  11. #include <linux/hugetlb.h>
  12. #include <linux/shm.h>
  13. #include <linux/mman.h>
  14. #include <linux/fs.h>
  15. #include <linux/highmem.h>
  16. #include <linux/security.h>
  17. #include <linux/mempolicy.h>
  18. #include <linux/personality.h>
  19. #include <linux/syscalls.h>
  20. #include <linux/swap.h>
  21. #include <linux/swapops.h>
  22. #include <linux/mmu_notifier.h>
  23. #include <linux/migrate.h>
  24. #include <linux/perf_event.h>
  25. #include <linux/ksm.h>
  26. #include <asm/uaccess.h>
  27. #include <asm/pgtable.h>
  28. #include <asm/cacheflush.h>
  29. #include <asm/tlbflush.h>
  30. #include "internal.h"
  31. /*
  32. * For a prot_numa update we only hold mmap_sem for read so there is a
  33. * potential race with faulting where a pmd was temporarily none. This
  34. * function checks for a transhuge pmd under the appropriate lock. It
  35. * returns a pte if it was successfully locked or NULL if it raced with
  36. * a transhuge insertion.
  37. */
  38. static pte_t *lock_pte_protection(struct vm_area_struct *vma, pmd_t *pmd,
  39. unsigned long addr, int prot_numa, spinlock_t **ptl)
  40. {
  41. pte_t *pte;
  42. spinlock_t *pmdl;
  43. /* !prot_numa is protected by mmap_sem held for write */
  44. if (!prot_numa)
  45. return pte_offset_map_lock(vma->vm_mm, pmd, addr, ptl);
  46. pmdl = pmd_lock(vma->vm_mm, pmd);
  47. if (unlikely(pmd_trans_huge(*pmd) || pmd_none(*pmd))) {
  48. spin_unlock(pmdl);
  49. return NULL;
  50. }
  51. pte = pte_offset_map_lock(vma->vm_mm, pmd, addr, ptl);
  52. spin_unlock(pmdl);
  53. return pte;
  54. }
  55. static unsigned long change_pte_range(struct vm_area_struct *vma, pmd_t *pmd,
  56. unsigned long addr, unsigned long end, pgprot_t newprot,
  57. int dirty_accountable, int prot_numa)
  58. {
  59. struct mm_struct *mm = vma->vm_mm;
  60. pte_t *pte, oldpte;
  61. spinlock_t *ptl;
  62. unsigned long pages = 0;
  63. pte = lock_pte_protection(vma, pmd, addr, prot_numa, &ptl);
  64. if (!pte)
  65. return 0;
  66. arch_enter_lazy_mmu_mode();
  67. do {
  68. oldpte = *pte;
  69. if (pte_present(oldpte)) {
  70. pte_t ptent;
  71. bool preserve_write = prot_numa && pte_write(oldpte);
  72. /*
  73. * Avoid trapping faults against the zero or KSM
  74. * pages. See similar comment in change_huge_pmd.
  75. */
  76. if (prot_numa) {
  77. struct page *page;
  78. page = vm_normal_page(vma, addr, oldpte);
  79. if (!page || PageKsm(page))
  80. continue;
  81. /* Avoid TLB flush if possible */
  82. if (pte_protnone(oldpte))
  83. continue;
  84. }
  85. ptent = ptep_modify_prot_start(mm, addr, pte);
  86. ptent = pte_modify(ptent, newprot);
  87. if (preserve_write)
  88. ptent = pte_mkwrite(ptent);
  89. /* Avoid taking write faults for known dirty pages */
  90. if (dirty_accountable && pte_dirty(ptent) &&
  91. (pte_soft_dirty(ptent) ||
  92. !(vma->vm_flags & VM_SOFTDIRTY))) {
  93. ptent = pte_mkwrite(ptent);
  94. }
  95. ptep_modify_prot_commit(mm, addr, pte, ptent);
  96. pages++;
  97. } else if (IS_ENABLED(CONFIG_MIGRATION)) {
  98. swp_entry_t entry = pte_to_swp_entry(oldpte);
  99. if (is_write_migration_entry(entry)) {
  100. pte_t newpte;
  101. /*
  102. * A protection check is difficult so
  103. * just be safe and disable write
  104. */
  105. make_migration_entry_read(&entry);
  106. newpte = swp_entry_to_pte(entry);
  107. if (pte_swp_soft_dirty(oldpte))
  108. newpte = pte_swp_mksoft_dirty(newpte);
  109. set_pte_at(mm, addr, pte, newpte);
  110. pages++;
  111. }
  112. }
  113. } while (pte++, addr += PAGE_SIZE, addr != end);
  114. arch_leave_lazy_mmu_mode();
  115. pte_unmap_unlock(pte - 1, ptl);
  116. return pages;
  117. }
  118. static inline unsigned long change_pmd_range(struct vm_area_struct *vma,
  119. pud_t *pud, unsigned long addr, unsigned long end,
  120. pgprot_t newprot, int dirty_accountable, int prot_numa)
  121. {
  122. pmd_t *pmd;
  123. struct mm_struct *mm = vma->vm_mm;
  124. unsigned long next;
  125. unsigned long pages = 0;
  126. unsigned long nr_huge_updates = 0;
  127. unsigned long mni_start = 0;
  128. pmd = pmd_offset(pud, addr);
  129. do {
  130. unsigned long this_pages;
  131. next = pmd_addr_end(addr, end);
  132. if (!pmd_trans_huge(*pmd) && pmd_none_or_clear_bad(pmd))
  133. continue;
  134. /* invoke the mmu notifier if the pmd is populated */
  135. if (!mni_start) {
  136. mni_start = addr;
  137. mmu_notifier_invalidate_range_start(mm, mni_start, end);
  138. }
  139. if (pmd_trans_huge(*pmd)) {
  140. if (next - addr != HPAGE_PMD_SIZE)
  141. split_huge_page_pmd(vma, addr, pmd);
  142. else {
  143. int nr_ptes = change_huge_pmd(vma, pmd, addr,
  144. newprot, prot_numa);
  145. if (nr_ptes) {
  146. if (nr_ptes == HPAGE_PMD_NR) {
  147. pages += HPAGE_PMD_NR;
  148. nr_huge_updates++;
  149. }
  150. /* huge pmd was handled */
  151. continue;
  152. }
  153. }
  154. /* fall through, the trans huge pmd just split */
  155. }
  156. this_pages = change_pte_range(vma, pmd, addr, next, newprot,
  157. dirty_accountable, prot_numa);
  158. pages += this_pages;
  159. } while (pmd++, addr = next, addr != end);
  160. if (mni_start)
  161. mmu_notifier_invalidate_range_end(mm, mni_start, end);
  162. if (nr_huge_updates)
  163. count_vm_numa_events(NUMA_HUGE_PTE_UPDATES, nr_huge_updates);
  164. return pages;
  165. }
  166. static inline unsigned long change_pud_range(struct vm_area_struct *vma,
  167. pgd_t *pgd, unsigned long addr, unsigned long end,
  168. pgprot_t newprot, int dirty_accountable, int prot_numa)
  169. {
  170. pud_t *pud;
  171. unsigned long next;
  172. unsigned long pages = 0;
  173. pud = pud_offset(pgd, addr);
  174. do {
  175. next = pud_addr_end(addr, end);
  176. if (pud_none_or_clear_bad(pud))
  177. continue;
  178. pages += change_pmd_range(vma, pud, addr, next, newprot,
  179. dirty_accountable, prot_numa);
  180. } while (pud++, addr = next, addr != end);
  181. return pages;
  182. }
  183. static unsigned long change_protection_range(struct vm_area_struct *vma,
  184. unsigned long addr, unsigned long end, pgprot_t newprot,
  185. int dirty_accountable, int prot_numa)
  186. {
  187. struct mm_struct *mm = vma->vm_mm;
  188. pgd_t *pgd;
  189. unsigned long next;
  190. unsigned long start = addr;
  191. unsigned long pages = 0;
  192. BUG_ON(addr >= end);
  193. pgd = pgd_offset(mm, addr);
  194. flush_cache_range(vma, addr, end);
  195. set_tlb_flush_pending(mm);
  196. do {
  197. next = pgd_addr_end(addr, end);
  198. if (pgd_none_or_clear_bad(pgd))
  199. continue;
  200. pages += change_pud_range(vma, pgd, addr, next, newprot,
  201. dirty_accountable, prot_numa);
  202. } while (pgd++, addr = next, addr != end);
  203. /* Only flush the TLB if we actually modified any entries: */
  204. if (pages)
  205. flush_tlb_range(vma, start, end);
  206. clear_tlb_flush_pending(mm);
  207. return pages;
  208. }
  209. unsigned long change_protection(struct vm_area_struct *vma, unsigned long start,
  210. unsigned long end, pgprot_t newprot,
  211. int dirty_accountable, int prot_numa)
  212. {
  213. unsigned long pages;
  214. if (is_vm_hugetlb_page(vma))
  215. pages = hugetlb_change_protection(vma, start, end, newprot);
  216. else
  217. pages = change_protection_range(vma, start, end, newprot, dirty_accountable, prot_numa);
  218. return pages;
  219. }
  220. int
  221. mprotect_fixup(struct vm_area_struct *vma, struct vm_area_struct **pprev,
  222. unsigned long start, unsigned long end, unsigned long newflags)
  223. {
  224. struct mm_struct *mm = vma->vm_mm;
  225. unsigned long oldflags = vma->vm_flags;
  226. long nrpages = (end - start) >> PAGE_SHIFT;
  227. unsigned long charged = 0;
  228. pgoff_t pgoff;
  229. int error;
  230. int dirty_accountable = 0;
  231. if (newflags == oldflags) {
  232. *pprev = vma;
  233. return 0;
  234. }
  235. /*
  236. * If we make a private mapping writable we increase our commit;
  237. * but (without finer accounting) cannot reduce our commit if we
  238. * make it unwritable again. hugetlb mapping were accounted for
  239. * even if read-only so there is no need to account for them here
  240. */
  241. if (newflags & VM_WRITE) {
  242. if (!(oldflags & (VM_ACCOUNT|VM_WRITE|VM_HUGETLB|
  243. VM_SHARED|VM_NORESERVE))) {
  244. charged = nrpages;
  245. if (security_vm_enough_memory_mm(mm, charged))
  246. return -ENOMEM;
  247. newflags |= VM_ACCOUNT;
  248. }
  249. }
  250. /*
  251. * First try to merge with previous and/or next vma.
  252. */
  253. pgoff = vma->vm_pgoff + ((start - vma->vm_start) >> PAGE_SHIFT);
  254. *pprev = vma_merge(mm, *pprev, start, end, newflags,
  255. vma->anon_vma, vma->vm_file, pgoff, vma_policy(vma));
  256. if (*pprev) {
  257. vma = *pprev;
  258. goto success;
  259. }
  260. *pprev = vma;
  261. if (start != vma->vm_start) {
  262. error = split_vma(mm, vma, start, 1);
  263. if (error)
  264. goto fail;
  265. }
  266. if (end != vma->vm_end) {
  267. error = split_vma(mm, vma, end, 0);
  268. if (error)
  269. goto fail;
  270. }
  271. success:
  272. /*
  273. * vm_flags and vm_page_prot are protected by the mmap_sem
  274. * held in write mode.
  275. */
  276. vma->vm_flags = newflags;
  277. dirty_accountable = vma_wants_writenotify(vma);
  278. vma_set_page_prot(vma);
  279. change_protection(vma, start, end, vma->vm_page_prot,
  280. dirty_accountable, 0);
  281. /*
  282. * Private VM_LOCKED VMA becoming writable: trigger COW to avoid major
  283. * fault on access.
  284. */
  285. if ((oldflags & (VM_WRITE | VM_SHARED | VM_LOCKED)) == VM_LOCKED &&
  286. (newflags & VM_WRITE)) {
  287. populate_vma_page_range(vma, start, end, NULL);
  288. }
  289. vm_stat_account(mm, oldflags, vma->vm_file, -nrpages);
  290. vm_stat_account(mm, newflags, vma->vm_file, nrpages);
  291. perf_event_mmap(vma);
  292. return 0;
  293. fail:
  294. vm_unacct_memory(charged);
  295. return error;
  296. }
  297. SYSCALL_DEFINE3(mprotect, unsigned long, start, size_t, len,
  298. unsigned long, prot)
  299. {
  300. unsigned long vm_flags, nstart, end, tmp, reqprot;
  301. struct vm_area_struct *vma, *prev;
  302. int error = -EINVAL;
  303. const int grows = prot & (PROT_GROWSDOWN|PROT_GROWSUP);
  304. prot &= ~(PROT_GROWSDOWN|PROT_GROWSUP);
  305. if (grows == (PROT_GROWSDOWN|PROT_GROWSUP)) /* can't be both */
  306. return -EINVAL;
  307. if (start & ~PAGE_MASK)
  308. return -EINVAL;
  309. if (!len)
  310. return 0;
  311. len = PAGE_ALIGN(len);
  312. end = start + len;
  313. if (end <= start)
  314. return -ENOMEM;
  315. if (!arch_validate_prot(prot))
  316. return -EINVAL;
  317. reqprot = prot;
  318. /*
  319. * Does the application expect PROT_READ to imply PROT_EXEC:
  320. */
  321. if ((prot & PROT_READ) && (current->personality & READ_IMPLIES_EXEC))
  322. prot |= PROT_EXEC;
  323. vm_flags = calc_vm_prot_bits(prot);
  324. down_write(&current->mm->mmap_sem);
  325. vma = find_vma(current->mm, start);
  326. error = -ENOMEM;
  327. if (!vma)
  328. goto out;
  329. prev = vma->vm_prev;
  330. if (unlikely(grows & PROT_GROWSDOWN)) {
  331. if (vma->vm_start >= end)
  332. goto out;
  333. start = vma->vm_start;
  334. error = -EINVAL;
  335. if (!(vma->vm_flags & VM_GROWSDOWN))
  336. goto out;
  337. } else {
  338. if (vma->vm_start > start)
  339. goto out;
  340. if (unlikely(grows & PROT_GROWSUP)) {
  341. end = vma->vm_end;
  342. error = -EINVAL;
  343. if (!(vma->vm_flags & VM_GROWSUP))
  344. goto out;
  345. }
  346. }
  347. if (start > vma->vm_start)
  348. prev = vma;
  349. for (nstart = start ; ; ) {
  350. unsigned long newflags;
  351. /* Here we know that vma->vm_start <= nstart < vma->vm_end. */
  352. newflags = vm_flags;
  353. newflags |= (vma->vm_flags & ~(VM_READ | VM_WRITE | VM_EXEC));
  354. /* newflags >> 4 shift VM_MAY% in place of VM_% */
  355. if ((newflags & ~(newflags >> 4)) & (VM_READ | VM_WRITE | VM_EXEC)) {
  356. error = -EACCES;
  357. goto out;
  358. }
  359. error = security_file_mprotect(vma, reqprot, prot);
  360. if (error)
  361. goto out;
  362. tmp = vma->vm_end;
  363. if (tmp > end)
  364. tmp = end;
  365. error = mprotect_fixup(vma, &prev, nstart, tmp, newflags);
  366. if (error)
  367. goto out;
  368. nstart = tmp;
  369. if (nstart < prev->vm_end)
  370. nstart = prev->vm_end;
  371. if (nstart >= end)
  372. goto out;
  373. vma = prev->vm_next;
  374. if (!vma || vma->vm_start != nstart) {
  375. error = -ENOMEM;
  376. goto out;
  377. }
  378. }
  379. out:
  380. up_write(&current->mm->mmap_sem);
  381. return error;
  382. }