mprotect.c 16 KB

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