mprotect.c 8.6 KB

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