mprotect.c 11 KB

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