mprotect.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587
  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. int
  266. mprotect_fixup(struct vm_area_struct *vma, struct vm_area_struct **pprev,
  267. unsigned long start, unsigned long end, unsigned long newflags)
  268. {
  269. struct mm_struct *mm = vma->vm_mm;
  270. unsigned long oldflags = vma->vm_flags;
  271. long nrpages = (end - start) >> PAGE_SHIFT;
  272. unsigned long charged = 0;
  273. pgoff_t pgoff;
  274. int error;
  275. int dirty_accountable = 0;
  276. if (newflags == oldflags) {
  277. *pprev = vma;
  278. return 0;
  279. }
  280. /*
  281. * If we make a private mapping writable we increase our commit;
  282. * but (without finer accounting) cannot reduce our commit if we
  283. * make it unwritable again. hugetlb mapping were accounted for
  284. * even if read-only so there is no need to account for them here
  285. */
  286. if (newflags & VM_WRITE) {
  287. /* Check space limits when area turns into data. */
  288. if (!may_expand_vm(mm, newflags, nrpages) &&
  289. may_expand_vm(mm, oldflags, nrpages))
  290. return -ENOMEM;
  291. if (!(oldflags & (VM_ACCOUNT|VM_WRITE|VM_HUGETLB|
  292. VM_SHARED|VM_NORESERVE))) {
  293. charged = nrpages;
  294. if (security_vm_enough_memory_mm(mm, charged))
  295. return -ENOMEM;
  296. newflags |= VM_ACCOUNT;
  297. }
  298. }
  299. /*
  300. * First try to merge with previous and/or next vma.
  301. */
  302. pgoff = vma->vm_pgoff + ((start - vma->vm_start) >> PAGE_SHIFT);
  303. *pprev = vma_merge(mm, *pprev, start, end, newflags,
  304. vma->anon_vma, vma->vm_file, pgoff, vma_policy(vma),
  305. vma->vm_userfaultfd_ctx);
  306. if (*pprev) {
  307. vma = *pprev;
  308. VM_WARN_ON((vma->vm_flags ^ newflags) & ~VM_SOFTDIRTY);
  309. goto success;
  310. }
  311. *pprev = vma;
  312. if (start != vma->vm_start) {
  313. error = split_vma(mm, vma, start, 1);
  314. if (error)
  315. goto fail;
  316. }
  317. if (end != vma->vm_end) {
  318. error = split_vma(mm, vma, end, 0);
  319. if (error)
  320. goto fail;
  321. }
  322. success:
  323. /*
  324. * vm_flags and vm_page_prot are protected by the mmap_sem
  325. * held in write mode.
  326. */
  327. vma->vm_flags = newflags;
  328. dirty_accountable = vma_wants_writenotify(vma, vma->vm_page_prot);
  329. vma_set_page_prot(vma);
  330. change_protection(vma, start, end, vma->vm_page_prot,
  331. dirty_accountable, 0);
  332. /*
  333. * Private VM_LOCKED VMA becoming writable: trigger COW to avoid major
  334. * fault on access.
  335. */
  336. if ((oldflags & (VM_WRITE | VM_SHARED | VM_LOCKED)) == VM_LOCKED &&
  337. (newflags & VM_WRITE)) {
  338. populate_vma_page_range(vma, start, end, NULL);
  339. }
  340. vm_stat_account(mm, oldflags, -nrpages);
  341. vm_stat_account(mm, newflags, nrpages);
  342. perf_event_mmap(vma);
  343. return 0;
  344. fail:
  345. vm_unacct_memory(charged);
  346. return error;
  347. }
  348. /*
  349. * pkey==-1 when doing a legacy mprotect()
  350. */
  351. static int do_mprotect_pkey(unsigned long start, size_t len,
  352. unsigned long prot, int pkey)
  353. {
  354. unsigned long nstart, end, tmp, reqprot;
  355. struct vm_area_struct *vma, *prev;
  356. int error = -EINVAL;
  357. const int grows = prot & (PROT_GROWSDOWN|PROT_GROWSUP);
  358. const bool rier = (current->personality & READ_IMPLIES_EXEC) &&
  359. (prot & PROT_READ);
  360. prot &= ~(PROT_GROWSDOWN|PROT_GROWSUP);
  361. if (grows == (PROT_GROWSDOWN|PROT_GROWSUP)) /* can't be both */
  362. return -EINVAL;
  363. if (start & ~PAGE_MASK)
  364. return -EINVAL;
  365. if (!len)
  366. return 0;
  367. len = PAGE_ALIGN(len);
  368. end = start + len;
  369. if (end <= start)
  370. return -ENOMEM;
  371. if (!arch_validate_prot(prot, start))
  372. return -EINVAL;
  373. reqprot = prot;
  374. if (down_write_killable(&current->mm->mmap_sem))
  375. return -EINTR;
  376. /*
  377. * If userspace did not allocate the pkey, do not let
  378. * them use it here.
  379. */
  380. error = -EINVAL;
  381. if ((pkey != -1) && !mm_pkey_is_allocated(current->mm, pkey))
  382. goto out;
  383. vma = find_vma(current->mm, start);
  384. error = -ENOMEM;
  385. if (!vma)
  386. goto out;
  387. prev = vma->vm_prev;
  388. if (unlikely(grows & PROT_GROWSDOWN)) {
  389. if (vma->vm_start >= end)
  390. goto out;
  391. start = vma->vm_start;
  392. error = -EINVAL;
  393. if (!(vma->vm_flags & VM_GROWSDOWN))
  394. goto out;
  395. } else {
  396. if (vma->vm_start > start)
  397. goto out;
  398. if (unlikely(grows & PROT_GROWSUP)) {
  399. end = vma->vm_end;
  400. error = -EINVAL;
  401. if (!(vma->vm_flags & VM_GROWSUP))
  402. goto out;
  403. }
  404. }
  405. if (start > vma->vm_start)
  406. prev = vma;
  407. for (nstart = start ; ; ) {
  408. unsigned long mask_off_old_flags;
  409. unsigned long newflags;
  410. int new_vma_pkey;
  411. /* Here we know that vma->vm_start <= nstart < vma->vm_end. */
  412. /* Does the application expect PROT_READ to imply PROT_EXEC */
  413. if (rier && (vma->vm_flags & VM_MAYEXEC))
  414. prot |= PROT_EXEC;
  415. /*
  416. * Each mprotect() call explicitly passes r/w/x permissions.
  417. * If a permission is not passed to mprotect(), it must be
  418. * cleared from the VMA.
  419. */
  420. mask_off_old_flags = VM_READ | VM_WRITE | VM_EXEC |
  421. VM_FLAGS_CLEAR;
  422. new_vma_pkey = arch_override_mprotect_pkey(vma, prot, pkey);
  423. newflags = calc_vm_prot_bits(prot, new_vma_pkey);
  424. newflags |= (vma->vm_flags & ~mask_off_old_flags);
  425. /* newflags >> 4 shift VM_MAY% in place of VM_% */
  426. if ((newflags & ~(newflags >> 4)) & (VM_READ | VM_WRITE | VM_EXEC)) {
  427. error = -EACCES;
  428. goto out;
  429. }
  430. error = security_file_mprotect(vma, reqprot, prot);
  431. if (error)
  432. goto out;
  433. tmp = vma->vm_end;
  434. if (tmp > end)
  435. tmp = end;
  436. error = mprotect_fixup(vma, &prev, nstart, tmp, newflags);
  437. if (error)
  438. goto out;
  439. nstart = tmp;
  440. if (nstart < prev->vm_end)
  441. nstart = prev->vm_end;
  442. if (nstart >= end)
  443. goto out;
  444. vma = prev->vm_next;
  445. if (!vma || vma->vm_start != nstart) {
  446. error = -ENOMEM;
  447. goto out;
  448. }
  449. prot = reqprot;
  450. }
  451. out:
  452. up_write(&current->mm->mmap_sem);
  453. return error;
  454. }
  455. SYSCALL_DEFINE3(mprotect, unsigned long, start, size_t, len,
  456. unsigned long, prot)
  457. {
  458. return do_mprotect_pkey(start, len, prot, -1);
  459. }
  460. #ifdef CONFIG_ARCH_HAS_PKEYS
  461. SYSCALL_DEFINE4(pkey_mprotect, unsigned long, start, size_t, len,
  462. unsigned long, prot, int, pkey)
  463. {
  464. return do_mprotect_pkey(start, len, prot, pkey);
  465. }
  466. SYSCALL_DEFINE2(pkey_alloc, unsigned long, flags, unsigned long, init_val)
  467. {
  468. int pkey;
  469. int ret;
  470. /* No flags supported yet. */
  471. if (flags)
  472. return -EINVAL;
  473. /* check for unsupported init values */
  474. if (init_val & ~PKEY_ACCESS_MASK)
  475. return -EINVAL;
  476. down_write(&current->mm->mmap_sem);
  477. pkey = mm_pkey_alloc(current->mm);
  478. ret = -ENOSPC;
  479. if (pkey == -1)
  480. goto out;
  481. ret = arch_set_user_pkey_access(current, pkey, init_val);
  482. if (ret) {
  483. mm_pkey_free(current->mm, pkey);
  484. goto out;
  485. }
  486. ret = pkey;
  487. out:
  488. up_write(&current->mm->mmap_sem);
  489. return ret;
  490. }
  491. SYSCALL_DEFINE1(pkey_free, int, pkey)
  492. {
  493. int ret;
  494. down_write(&current->mm->mmap_sem);
  495. ret = mm_pkey_free(current->mm, pkey);
  496. up_write(&current->mm->mmap_sem);
  497. /*
  498. * We could provie warnings or errors if any VMA still
  499. * has the pkey set here.
  500. */
  501. return ret;
  502. }
  503. #endif /* CONFIG_ARCH_HAS_PKEYS */