mprotect.c 11 KB

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