mprotect.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556
  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/pkeys.h>
  26. #include <linux/ksm.h>
  27. #include <linux/uaccess.h>
  28. #include <asm/pgtable.h>
  29. #include <asm/cacheflush.h>
  30. #include <asm/mmu_context.h>
  31. #include <asm/tlbflush.h>
  32. #include "internal.h"
  33. static unsigned long change_pte_range(struct vm_area_struct *vma, pmd_t *pmd,
  34. unsigned long addr, unsigned long end, pgprot_t newprot,
  35. int dirty_accountable, int prot_numa)
  36. {
  37. struct mm_struct *mm = vma->vm_mm;
  38. pte_t *pte, oldpte;
  39. spinlock_t *ptl;
  40. unsigned long pages = 0;
  41. int target_node = NUMA_NO_NODE;
  42. /*
  43. * Can be called with only the mmap_sem for reading by
  44. * prot_numa so we must check the pmd isn't constantly
  45. * changing from under us from pmd_none to pmd_trans_huge
  46. * and/or the other way around.
  47. */
  48. if (pmd_trans_unstable(pmd))
  49. return 0;
  50. /*
  51. * The pmd points to a regular pte so the pmd can't change
  52. * from under us even if the mmap_sem is only hold for
  53. * reading.
  54. */
  55. pte = pte_offset_map_lock(vma->vm_mm, pmd, addr, &ptl);
  56. /* Get target node for single threaded private VMAs */
  57. if (prot_numa && !(vma->vm_flags & VM_SHARED) &&
  58. atomic_read(&vma->vm_mm->mm_users) == 1)
  59. target_node = numa_node_id();
  60. flush_tlb_batched_pending(vma->vm_mm);
  61. arch_enter_lazy_mmu_mode();
  62. do {
  63. oldpte = *pte;
  64. if (pte_present(oldpte)) {
  65. pte_t ptent;
  66. bool preserve_write = prot_numa && pte_write(oldpte);
  67. /*
  68. * Avoid trapping faults against the zero or KSM
  69. * pages. See similar comment in change_huge_pmd.
  70. */
  71. if (prot_numa) {
  72. struct page *page;
  73. page = vm_normal_page(vma, addr, oldpte);
  74. if (!page || PageKsm(page))
  75. continue;
  76. /* Avoid TLB flush if possible */
  77. if (pte_protnone(oldpte))
  78. continue;
  79. /*
  80. * Don't mess with PTEs if page is already on the node
  81. * a single-threaded process is running on.
  82. */
  83. if (target_node == page_to_nid(page))
  84. continue;
  85. }
  86. ptent = ptep_modify_prot_start(mm, addr, pte);
  87. ptent = pte_modify(ptent, newprot);
  88. if (preserve_write)
  89. ptent = pte_mk_savedwrite(ptent);
  90. /* Avoid taking write faults for known dirty pages */
  91. if (dirty_accountable && pte_dirty(ptent) &&
  92. (pte_soft_dirty(ptent) ||
  93. !(vma->vm_flags & VM_SOFTDIRTY))) {
  94. ptent = pte_mkwrite(ptent);
  95. }
  96. ptep_modify_prot_commit(mm, addr, pte, ptent);
  97. pages++;
  98. } else if (IS_ENABLED(CONFIG_MIGRATION)) {
  99. swp_entry_t entry = pte_to_swp_entry(oldpte);
  100. if (is_write_migration_entry(entry)) {
  101. pte_t newpte;
  102. /*
  103. * A protection check is difficult so
  104. * just be safe and disable write
  105. */
  106. make_migration_entry_read(&entry);
  107. newpte = swp_entry_to_pte(entry);
  108. if (pte_swp_soft_dirty(oldpte))
  109. newpte = pte_swp_mksoft_dirty(newpte);
  110. set_pte_at(mm, addr, pte, newpte);
  111. pages++;
  112. }
  113. }
  114. } while (pte++, addr += PAGE_SIZE, addr != end);
  115. arch_leave_lazy_mmu_mode();
  116. pte_unmap_unlock(pte - 1, ptl);
  117. return pages;
  118. }
  119. static inline unsigned long change_pmd_range(struct vm_area_struct *vma,
  120. pud_t *pud, unsigned long addr, unsigned long end,
  121. pgprot_t newprot, int dirty_accountable, int prot_numa)
  122. {
  123. pmd_t *pmd;
  124. struct mm_struct *mm = vma->vm_mm;
  125. unsigned long next;
  126. unsigned long pages = 0;
  127. unsigned long nr_huge_updates = 0;
  128. unsigned long mni_start = 0;
  129. pmd = pmd_offset(pud, addr);
  130. do {
  131. unsigned long this_pages;
  132. next = pmd_addr_end(addr, end);
  133. if (!pmd_trans_huge(*pmd) && !pmd_devmap(*pmd)
  134. && pmd_none_or_clear_bad(pmd))
  135. continue;
  136. /* invoke the mmu notifier if the pmd is populated */
  137. if (!mni_start) {
  138. mni_start = addr;
  139. mmu_notifier_invalidate_range_start(mm, mni_start, end);
  140. }
  141. if (pmd_trans_huge(*pmd) || pmd_devmap(*pmd)) {
  142. if (next - addr != HPAGE_PMD_SIZE) {
  143. __split_huge_pmd(vma, pmd, addr, false, NULL);
  144. } else {
  145. int nr_ptes = change_huge_pmd(vma, pmd, addr,
  146. newprot, prot_numa);
  147. if (nr_ptes) {
  148. if (nr_ptes == HPAGE_PMD_NR) {
  149. pages += HPAGE_PMD_NR;
  150. nr_huge_updates++;
  151. }
  152. /* huge pmd was handled */
  153. continue;
  154. }
  155. }
  156. /* fall through, the trans huge pmd just split */
  157. }
  158. this_pages = change_pte_range(vma, pmd, addr, next, newprot,
  159. dirty_accountable, prot_numa);
  160. pages += this_pages;
  161. } while (pmd++, addr = next, addr != end);
  162. if (mni_start)
  163. mmu_notifier_invalidate_range_end(mm, mni_start, end);
  164. if (nr_huge_updates)
  165. count_vm_numa_events(NUMA_HUGE_PTE_UPDATES, nr_huge_updates);
  166. return pages;
  167. }
  168. static inline unsigned long change_pud_range(struct vm_area_struct *vma,
  169. p4d_t *p4d, unsigned long addr, unsigned long end,
  170. pgprot_t newprot, int dirty_accountable, int prot_numa)
  171. {
  172. pud_t *pud;
  173. unsigned long next;
  174. unsigned long pages = 0;
  175. pud = pud_offset(p4d, addr);
  176. do {
  177. next = pud_addr_end(addr, end);
  178. if (pud_none_or_clear_bad(pud))
  179. continue;
  180. pages += change_pmd_range(vma, pud, addr, next, newprot,
  181. dirty_accountable, prot_numa);
  182. } while (pud++, addr = next, addr != end);
  183. return pages;
  184. }
  185. static inline unsigned long change_p4d_range(struct vm_area_struct *vma,
  186. pgd_t *pgd, unsigned long addr, unsigned long end,
  187. pgprot_t newprot, int dirty_accountable, int prot_numa)
  188. {
  189. p4d_t *p4d;
  190. unsigned long next;
  191. unsigned long pages = 0;
  192. p4d = p4d_offset(pgd, addr);
  193. do {
  194. next = p4d_addr_end(addr, end);
  195. if (p4d_none_or_clear_bad(p4d))
  196. continue;
  197. pages += change_pud_range(vma, p4d, addr, next, newprot,
  198. dirty_accountable, prot_numa);
  199. } while (p4d++, addr = next, addr != end);
  200. return pages;
  201. }
  202. static unsigned long change_protection_range(struct vm_area_struct *vma,
  203. unsigned long addr, unsigned long end, pgprot_t newprot,
  204. int dirty_accountable, int prot_numa)
  205. {
  206. struct mm_struct *mm = vma->vm_mm;
  207. pgd_t *pgd;
  208. unsigned long next;
  209. unsigned long start = addr;
  210. unsigned long pages = 0;
  211. BUG_ON(addr >= end);
  212. pgd = pgd_offset(mm, addr);
  213. flush_cache_range(vma, addr, end);
  214. set_tlb_flush_pending(mm);
  215. do {
  216. next = pgd_addr_end(addr, end);
  217. if (pgd_none_or_clear_bad(pgd))
  218. continue;
  219. pages += change_p4d_range(vma, pgd, addr, next, newprot,
  220. dirty_accountable, prot_numa);
  221. } while (pgd++, addr = next, addr != end);
  222. /* Only flush the TLB if we actually modified any entries: */
  223. if (pages)
  224. flush_tlb_range(vma, start, end);
  225. clear_tlb_flush_pending(mm);
  226. return pages;
  227. }
  228. unsigned long change_protection(struct vm_area_struct *vma, unsigned long start,
  229. unsigned long end, pgprot_t newprot,
  230. int dirty_accountable, int prot_numa)
  231. {
  232. unsigned long pages;
  233. if (is_vm_hugetlb_page(vma))
  234. pages = hugetlb_change_protection(vma, start, end, newprot);
  235. else
  236. pages = change_protection_range(vma, start, end, newprot, dirty_accountable, prot_numa);
  237. return pages;
  238. }
  239. int
  240. mprotect_fixup(struct vm_area_struct *vma, struct vm_area_struct **pprev,
  241. unsigned long start, unsigned long end, unsigned long newflags)
  242. {
  243. struct mm_struct *mm = vma->vm_mm;
  244. unsigned long oldflags = vma->vm_flags;
  245. long nrpages = (end - start) >> PAGE_SHIFT;
  246. unsigned long charged = 0;
  247. pgoff_t pgoff;
  248. int error;
  249. int dirty_accountable = 0;
  250. if (newflags == oldflags) {
  251. *pprev = vma;
  252. return 0;
  253. }
  254. /*
  255. * If we make a private mapping writable we increase our commit;
  256. * but (without finer accounting) cannot reduce our commit if we
  257. * make it unwritable again. hugetlb mapping were accounted for
  258. * even if read-only so there is no need to account for them here
  259. */
  260. if (newflags & VM_WRITE) {
  261. /* Check space limits when area turns into data. */
  262. if (!may_expand_vm(mm, newflags, nrpages) &&
  263. may_expand_vm(mm, oldflags, nrpages))
  264. return -ENOMEM;
  265. if (!(oldflags & (VM_ACCOUNT|VM_WRITE|VM_HUGETLB|
  266. VM_SHARED|VM_NORESERVE))) {
  267. charged = nrpages;
  268. if (security_vm_enough_memory_mm(mm, charged))
  269. return -ENOMEM;
  270. newflags |= VM_ACCOUNT;
  271. }
  272. }
  273. /*
  274. * First try to merge with previous and/or next vma.
  275. */
  276. pgoff = vma->vm_pgoff + ((start - vma->vm_start) >> PAGE_SHIFT);
  277. *pprev = vma_merge(mm, *pprev, start, end, newflags,
  278. vma->anon_vma, vma->vm_file, pgoff, vma_policy(vma),
  279. vma->vm_userfaultfd_ctx);
  280. if (*pprev) {
  281. vma = *pprev;
  282. VM_WARN_ON((vma->vm_flags ^ newflags) & ~VM_SOFTDIRTY);
  283. goto success;
  284. }
  285. *pprev = vma;
  286. if (start != vma->vm_start) {
  287. error = split_vma(mm, vma, start, 1);
  288. if (error)
  289. goto fail;
  290. }
  291. if (end != vma->vm_end) {
  292. error = split_vma(mm, vma, end, 0);
  293. if (error)
  294. goto fail;
  295. }
  296. success:
  297. /*
  298. * vm_flags and vm_page_prot are protected by the mmap_sem
  299. * held in write mode.
  300. */
  301. vma->vm_flags = newflags;
  302. dirty_accountable = vma_wants_writenotify(vma, vma->vm_page_prot);
  303. vma_set_page_prot(vma);
  304. change_protection(vma, start, end, vma->vm_page_prot,
  305. dirty_accountable, 0);
  306. /*
  307. * Private VM_LOCKED VMA becoming writable: trigger COW to avoid major
  308. * fault on access.
  309. */
  310. if ((oldflags & (VM_WRITE | VM_SHARED | VM_LOCKED)) == VM_LOCKED &&
  311. (newflags & VM_WRITE)) {
  312. populate_vma_page_range(vma, start, end, NULL);
  313. }
  314. vm_stat_account(mm, oldflags, -nrpages);
  315. vm_stat_account(mm, newflags, nrpages);
  316. perf_event_mmap(vma);
  317. return 0;
  318. fail:
  319. vm_unacct_memory(charged);
  320. return error;
  321. }
  322. /*
  323. * pkey==-1 when doing a legacy mprotect()
  324. */
  325. static int do_mprotect_pkey(unsigned long start, size_t len,
  326. unsigned long prot, int pkey)
  327. {
  328. unsigned long nstart, end, tmp, reqprot;
  329. struct vm_area_struct *vma, *prev;
  330. int error = -EINVAL;
  331. const int grows = prot & (PROT_GROWSDOWN|PROT_GROWSUP);
  332. const bool rier = (current->personality & READ_IMPLIES_EXEC) &&
  333. (prot & PROT_READ);
  334. prot &= ~(PROT_GROWSDOWN|PROT_GROWSUP);
  335. if (grows == (PROT_GROWSDOWN|PROT_GROWSUP)) /* can't be both */
  336. return -EINVAL;
  337. if (start & ~PAGE_MASK)
  338. return -EINVAL;
  339. if (!len)
  340. return 0;
  341. len = PAGE_ALIGN(len);
  342. end = start + len;
  343. if (end <= start)
  344. return -ENOMEM;
  345. if (!arch_validate_prot(prot))
  346. return -EINVAL;
  347. reqprot = prot;
  348. if (down_write_killable(&current->mm->mmap_sem))
  349. return -EINTR;
  350. /*
  351. * If userspace did not allocate the pkey, do not let
  352. * them use it here.
  353. */
  354. error = -EINVAL;
  355. if ((pkey != -1) && !mm_pkey_is_allocated(current->mm, pkey))
  356. goto out;
  357. vma = find_vma(current->mm, start);
  358. error = -ENOMEM;
  359. if (!vma)
  360. goto out;
  361. prev = vma->vm_prev;
  362. if (unlikely(grows & PROT_GROWSDOWN)) {
  363. if (vma->vm_start >= end)
  364. goto out;
  365. start = vma->vm_start;
  366. error = -EINVAL;
  367. if (!(vma->vm_flags & VM_GROWSDOWN))
  368. goto out;
  369. } else {
  370. if (vma->vm_start > start)
  371. goto out;
  372. if (unlikely(grows & PROT_GROWSUP)) {
  373. end = vma->vm_end;
  374. error = -EINVAL;
  375. if (!(vma->vm_flags & VM_GROWSUP))
  376. goto out;
  377. }
  378. }
  379. if (start > vma->vm_start)
  380. prev = vma;
  381. for (nstart = start ; ; ) {
  382. unsigned long mask_off_old_flags;
  383. unsigned long newflags;
  384. int new_vma_pkey;
  385. /* Here we know that vma->vm_start <= nstart < vma->vm_end. */
  386. /* Does the application expect PROT_READ to imply PROT_EXEC */
  387. if (rier && (vma->vm_flags & VM_MAYEXEC))
  388. prot |= PROT_EXEC;
  389. /*
  390. * Each mprotect() call explicitly passes r/w/x permissions.
  391. * If a permission is not passed to mprotect(), it must be
  392. * cleared from the VMA.
  393. */
  394. mask_off_old_flags = VM_READ | VM_WRITE | VM_EXEC |
  395. ARCH_VM_PKEY_FLAGS;
  396. new_vma_pkey = arch_override_mprotect_pkey(vma, prot, pkey);
  397. newflags = calc_vm_prot_bits(prot, new_vma_pkey);
  398. newflags |= (vma->vm_flags & ~mask_off_old_flags);
  399. /* newflags >> 4 shift VM_MAY% in place of VM_% */
  400. if ((newflags & ~(newflags >> 4)) & (VM_READ | VM_WRITE | VM_EXEC)) {
  401. error = -EACCES;
  402. goto out;
  403. }
  404. error = security_file_mprotect(vma, reqprot, prot);
  405. if (error)
  406. goto out;
  407. tmp = vma->vm_end;
  408. if (tmp > end)
  409. tmp = end;
  410. error = mprotect_fixup(vma, &prev, nstart, tmp, newflags);
  411. if (error)
  412. goto out;
  413. nstart = tmp;
  414. if (nstart < prev->vm_end)
  415. nstart = prev->vm_end;
  416. if (nstart >= end)
  417. goto out;
  418. vma = prev->vm_next;
  419. if (!vma || vma->vm_start != nstart) {
  420. error = -ENOMEM;
  421. goto out;
  422. }
  423. prot = reqprot;
  424. }
  425. out:
  426. up_write(&current->mm->mmap_sem);
  427. return error;
  428. }
  429. SYSCALL_DEFINE3(mprotect, unsigned long, start, size_t, len,
  430. unsigned long, prot)
  431. {
  432. return do_mprotect_pkey(start, len, prot, -1);
  433. }
  434. #ifdef CONFIG_ARCH_HAS_PKEYS
  435. SYSCALL_DEFINE4(pkey_mprotect, unsigned long, start, size_t, len,
  436. unsigned long, prot, int, pkey)
  437. {
  438. return do_mprotect_pkey(start, len, prot, pkey);
  439. }
  440. SYSCALL_DEFINE2(pkey_alloc, unsigned long, flags, unsigned long, init_val)
  441. {
  442. int pkey;
  443. int ret;
  444. /* No flags supported yet. */
  445. if (flags)
  446. return -EINVAL;
  447. /* check for unsupported init values */
  448. if (init_val & ~PKEY_ACCESS_MASK)
  449. return -EINVAL;
  450. down_write(&current->mm->mmap_sem);
  451. pkey = mm_pkey_alloc(current->mm);
  452. ret = -ENOSPC;
  453. if (pkey == -1)
  454. goto out;
  455. ret = arch_set_user_pkey_access(current, pkey, init_val);
  456. if (ret) {
  457. mm_pkey_free(current->mm, pkey);
  458. goto out;
  459. }
  460. ret = pkey;
  461. out:
  462. up_write(&current->mm->mmap_sem);
  463. return ret;
  464. }
  465. SYSCALL_DEFINE1(pkey_free, int, pkey)
  466. {
  467. int ret;
  468. down_write(&current->mm->mmap_sem);
  469. ret = mm_pkey_free(current->mm, pkey);
  470. up_write(&current->mm->mmap_sem);
  471. /*
  472. * We could provie warnings or errors if any VMA still
  473. * has the pkey set here.
  474. */
  475. return ret;
  476. }
  477. #endif /* CONFIG_ARCH_HAS_PKEYS */