mprotect.c 7.9 KB

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