mprotect.c 13 KB

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