mprotect.c 13 KB

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