mprotect.c 14 KB

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