mprotect.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456
  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/ksm.h>
  26. #include <linux/pkeys.h>
  27. #include <asm/uaccess.h>
  28. #include <asm/pgtable.h>
  29. #include <asm/cacheflush.h>
  30. #include <asm/tlbflush.h>
  31. #include "internal.h"
  32. /*
  33. * For a prot_numa update we only hold mmap_sem for read so there is a
  34. * potential race with faulting where a pmd was temporarily none. This
  35. * function checks for a transhuge pmd under the appropriate lock. It
  36. * returns a pte if it was successfully locked or NULL if it raced with
  37. * a transhuge insertion.
  38. */
  39. static pte_t *lock_pte_protection(struct vm_area_struct *vma, pmd_t *pmd,
  40. unsigned long addr, int prot_numa, spinlock_t **ptl)
  41. {
  42. pte_t *pte;
  43. spinlock_t *pmdl;
  44. /* !prot_numa is protected by mmap_sem held for write */
  45. if (!prot_numa)
  46. return pte_offset_map_lock(vma->vm_mm, pmd, addr, ptl);
  47. pmdl = pmd_lock(vma->vm_mm, pmd);
  48. if (unlikely(pmd_trans_huge(*pmd) || pmd_none(*pmd))) {
  49. spin_unlock(pmdl);
  50. return NULL;
  51. }
  52. pte = pte_offset_map_lock(vma->vm_mm, pmd, addr, ptl);
  53. spin_unlock(pmdl);
  54. return pte;
  55. }
  56. static unsigned long change_pte_range(struct vm_area_struct *vma, pmd_t *pmd,
  57. unsigned long addr, unsigned long end, pgprot_t newprot,
  58. int dirty_accountable, int prot_numa)
  59. {
  60. struct mm_struct *mm = vma->vm_mm;
  61. pte_t *pte, oldpte;
  62. spinlock_t *ptl;
  63. unsigned long pages = 0;
  64. pte = lock_pte_protection(vma, pmd, addr, prot_numa, &ptl);
  65. if (!pte)
  66. return 0;
  67. arch_enter_lazy_mmu_mode();
  68. do {
  69. oldpte = *pte;
  70. if (pte_present(oldpte)) {
  71. pte_t ptent;
  72. bool preserve_write = prot_numa && pte_write(oldpte);
  73. /*
  74. * Avoid trapping faults against the zero or KSM
  75. * pages. See similar comment in change_huge_pmd.
  76. */
  77. if (prot_numa) {
  78. struct page *page;
  79. page = vm_normal_page(vma, addr, oldpte);
  80. if (!page || PageKsm(page))
  81. continue;
  82. /* Avoid TLB flush if possible */
  83. if (pte_protnone(oldpte))
  84. continue;
  85. }
  86. ptent = ptep_modify_prot_start(mm, addr, pte);
  87. ptent = pte_modify(ptent, newprot);
  88. if (preserve_write)
  89. ptent = pte_mkwrite(ptent);
  90. /* Avoid taking write faults for known dirty pages */
  91. if (dirty_accountable && pte_dirty(ptent) &&
  92. (pte_soft_dirty(ptent) ||
  93. !(vma->vm_flags & VM_SOFTDIRTY))) {
  94. ptent = pte_mkwrite(ptent);
  95. }
  96. ptep_modify_prot_commit(mm, addr, pte, ptent);
  97. pages++;
  98. } else if (IS_ENABLED(CONFIG_MIGRATION)) {
  99. swp_entry_t entry = pte_to_swp_entry(oldpte);
  100. if (is_write_migration_entry(entry)) {
  101. pte_t newpte;
  102. /*
  103. * A protection check is difficult so
  104. * just be safe and disable write
  105. */
  106. make_migration_entry_read(&entry);
  107. newpte = swp_entry_to_pte(entry);
  108. if (pte_swp_soft_dirty(oldpte))
  109. newpte = pte_swp_mksoft_dirty(newpte);
  110. set_pte_at(mm, addr, pte, newpte);
  111. pages++;
  112. }
  113. }
  114. } while (pte++, addr += PAGE_SIZE, addr != end);
  115. arch_leave_lazy_mmu_mode();
  116. pte_unmap_unlock(pte - 1, ptl);
  117. return pages;
  118. }
  119. static inline unsigned long change_pmd_range(struct vm_area_struct *vma,
  120. pud_t *pud, unsigned long addr, unsigned long end,
  121. pgprot_t newprot, int dirty_accountable, int prot_numa)
  122. {
  123. pmd_t *pmd;
  124. struct mm_struct *mm = vma->vm_mm;
  125. unsigned long next;
  126. unsigned long pages = 0;
  127. unsigned long nr_huge_updates = 0;
  128. unsigned long mni_start = 0;
  129. pmd = pmd_offset(pud, addr);
  130. do {
  131. unsigned long this_pages;
  132. next = pmd_addr_end(addr, end);
  133. if (!pmd_trans_huge(*pmd) && !pmd_devmap(*pmd)
  134. && pmd_none_or_clear_bad(pmd))
  135. continue;
  136. /* invoke the mmu notifier if the pmd is populated */
  137. if (!mni_start) {
  138. mni_start = addr;
  139. mmu_notifier_invalidate_range_start(mm, mni_start, end);
  140. }
  141. if (pmd_trans_huge(*pmd) || pmd_devmap(*pmd)) {
  142. if (next - addr != HPAGE_PMD_SIZE) {
  143. split_huge_pmd(vma, pmd, addr);
  144. if (pmd_trans_unstable(pmd))
  145. continue;
  146. } else {
  147. int nr_ptes = change_huge_pmd(vma, pmd, addr,
  148. newprot, prot_numa);
  149. if (nr_ptes) {
  150. if (nr_ptes == HPAGE_PMD_NR) {
  151. pages += HPAGE_PMD_NR;
  152. nr_huge_updates++;
  153. }
  154. /* huge pmd was handled */
  155. continue;
  156. }
  157. }
  158. /* fall through, the trans huge pmd just split */
  159. }
  160. this_pages = change_pte_range(vma, pmd, addr, next, newprot,
  161. dirty_accountable, prot_numa);
  162. pages += this_pages;
  163. } while (pmd++, addr = next, addr != end);
  164. if (mni_start)
  165. mmu_notifier_invalidate_range_end(mm, mni_start, end);
  166. if (nr_huge_updates)
  167. count_vm_numa_events(NUMA_HUGE_PTE_UPDATES, nr_huge_updates);
  168. return pages;
  169. }
  170. static inline unsigned long change_pud_range(struct vm_area_struct *vma,
  171. pgd_t *pgd, unsigned long addr, unsigned long end,
  172. pgprot_t newprot, int dirty_accountable, int prot_numa)
  173. {
  174. pud_t *pud;
  175. unsigned long next;
  176. unsigned long pages = 0;
  177. pud = pud_offset(pgd, addr);
  178. do {
  179. next = pud_addr_end(addr, end);
  180. if (pud_none_or_clear_bad(pud))
  181. continue;
  182. pages += change_pmd_range(vma, pud, addr, next, newprot,
  183. dirty_accountable, prot_numa);
  184. } while (pud++, addr = next, addr != end);
  185. return pages;
  186. }
  187. static unsigned long change_protection_range(struct vm_area_struct *vma,
  188. unsigned long addr, unsigned long end, pgprot_t newprot,
  189. int dirty_accountable, int prot_numa)
  190. {
  191. struct mm_struct *mm = vma->vm_mm;
  192. pgd_t *pgd;
  193. unsigned long next;
  194. unsigned long start = addr;
  195. unsigned long pages = 0;
  196. BUG_ON(addr >= end);
  197. pgd = pgd_offset(mm, addr);
  198. flush_cache_range(vma, addr, end);
  199. set_tlb_flush_pending(mm);
  200. do {
  201. next = pgd_addr_end(addr, end);
  202. if (pgd_none_or_clear_bad(pgd))
  203. continue;
  204. pages += change_pud_range(vma, pgd, addr, next, newprot,
  205. dirty_accountable, prot_numa);
  206. } while (pgd++, addr = next, addr != end);
  207. /* Only flush the TLB if we actually modified any entries: */
  208. if (pages)
  209. flush_tlb_range(vma, start, end);
  210. clear_tlb_flush_pending(mm);
  211. return pages;
  212. }
  213. unsigned long change_protection(struct vm_area_struct *vma, unsigned long start,
  214. unsigned long end, pgprot_t newprot,
  215. int dirty_accountable, int prot_numa)
  216. {
  217. unsigned long pages;
  218. if (is_vm_hugetlb_page(vma))
  219. pages = hugetlb_change_protection(vma, start, end, newprot);
  220. else
  221. pages = change_protection_range(vma, start, end, newprot, dirty_accountable, prot_numa);
  222. return pages;
  223. }
  224. int
  225. mprotect_fixup(struct vm_area_struct *vma, struct vm_area_struct **pprev,
  226. unsigned long start, unsigned long end, unsigned long newflags)
  227. {
  228. struct mm_struct *mm = vma->vm_mm;
  229. unsigned long oldflags = vma->vm_flags;
  230. long nrpages = (end - start) >> PAGE_SHIFT;
  231. unsigned long charged = 0;
  232. pgoff_t pgoff;
  233. int error;
  234. int dirty_accountable = 0;
  235. if (newflags == oldflags) {
  236. *pprev = vma;
  237. return 0;
  238. }
  239. /*
  240. * If we make a private mapping writable we increase our commit;
  241. * but (without finer accounting) cannot reduce our commit if we
  242. * make it unwritable again. hugetlb mapping were accounted for
  243. * even if read-only so there is no need to account for them here
  244. */
  245. if (newflags & VM_WRITE) {
  246. /* Check space limits when area turns into data. */
  247. if (!may_expand_vm(mm, newflags, nrpages) &&
  248. may_expand_vm(mm, oldflags, nrpages))
  249. return -ENOMEM;
  250. if (!(oldflags & (VM_ACCOUNT|VM_WRITE|VM_HUGETLB|
  251. VM_SHARED|VM_NORESERVE))) {
  252. charged = nrpages;
  253. if (security_vm_enough_memory_mm(mm, charged))
  254. return -ENOMEM;
  255. newflags |= VM_ACCOUNT;
  256. }
  257. }
  258. /*
  259. * First try to merge with previous and/or next vma.
  260. */
  261. pgoff = vma->vm_pgoff + ((start - vma->vm_start) >> PAGE_SHIFT);
  262. *pprev = vma_merge(mm, *pprev, start, end, newflags,
  263. vma->anon_vma, vma->vm_file, pgoff, vma_policy(vma),
  264. vma->vm_userfaultfd_ctx);
  265. if (*pprev) {
  266. vma = *pprev;
  267. goto success;
  268. }
  269. *pprev = vma;
  270. if (start != vma->vm_start) {
  271. error = split_vma(mm, vma, start, 1);
  272. if (error)
  273. goto fail;
  274. }
  275. if (end != vma->vm_end) {
  276. error = split_vma(mm, vma, end, 0);
  277. if (error)
  278. goto fail;
  279. }
  280. success:
  281. /*
  282. * vm_flags and vm_page_prot are protected by the mmap_sem
  283. * held in write mode.
  284. */
  285. vma->vm_flags = newflags;
  286. dirty_accountable = vma_wants_writenotify(vma);
  287. vma_set_page_prot(vma);
  288. change_protection(vma, start, end, vma->vm_page_prot,
  289. dirty_accountable, 0);
  290. /*
  291. * Private VM_LOCKED VMA becoming writable: trigger COW to avoid major
  292. * fault on access.
  293. */
  294. if ((oldflags & (VM_WRITE | VM_SHARED | VM_LOCKED)) == VM_LOCKED &&
  295. (newflags & VM_WRITE)) {
  296. populate_vma_page_range(vma, start, end, NULL);
  297. }
  298. vm_stat_account(mm, oldflags, -nrpages);
  299. vm_stat_account(mm, newflags, nrpages);
  300. perf_event_mmap(vma);
  301. return 0;
  302. fail:
  303. vm_unacct_memory(charged);
  304. return error;
  305. }
  306. SYSCALL_DEFINE3(mprotect, unsigned long, start, size_t, len,
  307. unsigned long, prot)
  308. {
  309. unsigned long nstart, end, tmp, reqprot;
  310. struct vm_area_struct *vma, *prev;
  311. int error = -EINVAL;
  312. const int grows = prot & (PROT_GROWSDOWN|PROT_GROWSUP);
  313. const bool rier = (current->personality & READ_IMPLIES_EXEC) &&
  314. (prot & PROT_READ);
  315. prot &= ~(PROT_GROWSDOWN|PROT_GROWSUP);
  316. if (grows == (PROT_GROWSDOWN|PROT_GROWSUP)) /* can't be both */
  317. return -EINVAL;
  318. if (start & ~PAGE_MASK)
  319. return -EINVAL;
  320. if (!len)
  321. return 0;
  322. len = PAGE_ALIGN(len);
  323. end = start + len;
  324. if (end <= start)
  325. return -ENOMEM;
  326. if (!arch_validate_prot(prot))
  327. return -EINVAL;
  328. reqprot = prot;
  329. if (down_write_killable(&current->mm->mmap_sem))
  330. return -EINTR;
  331. vma = find_vma(current->mm, start);
  332. error = -ENOMEM;
  333. if (!vma)
  334. goto out;
  335. prev = vma->vm_prev;
  336. if (unlikely(grows & PROT_GROWSDOWN)) {
  337. if (vma->vm_start >= end)
  338. goto out;
  339. start = vma->vm_start;
  340. error = -EINVAL;
  341. if (!(vma->vm_flags & VM_GROWSDOWN))
  342. goto out;
  343. } else {
  344. if (vma->vm_start > start)
  345. goto out;
  346. if (unlikely(grows & PROT_GROWSUP)) {
  347. end = vma->vm_end;
  348. error = -EINVAL;
  349. if (!(vma->vm_flags & VM_GROWSUP))
  350. goto out;
  351. }
  352. }
  353. if (start > vma->vm_start)
  354. prev = vma;
  355. for (nstart = start ; ; ) {
  356. unsigned long newflags;
  357. int pkey = arch_override_mprotect_pkey(vma, prot, -1);
  358. /* Here we know that vma->vm_start <= nstart < vma->vm_end. */
  359. /* Does the application expect PROT_READ to imply PROT_EXEC */
  360. if (rier && (vma->vm_flags & VM_MAYEXEC))
  361. prot |= PROT_EXEC;
  362. newflags = calc_vm_prot_bits(prot, pkey);
  363. newflags |= (vma->vm_flags & ~(VM_READ | VM_WRITE | VM_EXEC));
  364. /* newflags >> 4 shift VM_MAY% in place of VM_% */
  365. if ((newflags & ~(newflags >> 4)) & (VM_READ | VM_WRITE | VM_EXEC)) {
  366. error = -EACCES;
  367. goto out;
  368. }
  369. error = security_file_mprotect(vma, reqprot, prot);
  370. if (error)
  371. goto out;
  372. tmp = vma->vm_end;
  373. if (tmp > end)
  374. tmp = end;
  375. error = mprotect_fixup(vma, &prev, nstart, tmp, newflags);
  376. if (error)
  377. goto out;
  378. nstart = tmp;
  379. if (nstart < prev->vm_end)
  380. nstart = prev->vm_end;
  381. if (nstart >= end)
  382. goto out;
  383. vma = prev->vm_next;
  384. if (!vma || vma->vm_start != nstart) {
  385. error = -ENOMEM;
  386. goto out;
  387. }
  388. prot = reqprot;
  389. }
  390. out:
  391. up_write(&current->mm->mmap_sem);
  392. return error;
  393. }