mprotect.c 11 KB

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