mprotect.c 13 KB

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