mprotect.c 9.3 KB

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