mprotect.c 10 KB

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