mprotect.c 9.7 KB

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