mprotect.c 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318
  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/slab.h>
  13. #include <linux/shm.h>
  14. #include <linux/mman.h>
  15. #include <linux/fs.h>
  16. #include <linux/highmem.h>
  17. #include <linux/security.h>
  18. #include <linux/mempolicy.h>
  19. #include <linux/personality.h>
  20. #include <linux/syscalls.h>
  21. #include <linux/swap.h>
  22. #include <linux/swapops.h>
  23. #include <linux/mmu_notifier.h>
  24. #include <linux/migrate.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 mm_struct *mm, 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_none_or_clear_bad(pmd))
  82. continue;
  83. change_pte_range(mm, pmd, addr, next, newprot, dirty_accountable);
  84. } while (pmd++, addr = next, addr != end);
  85. }
  86. static inline void change_pud_range(struct mm_struct *mm, pgd_t *pgd,
  87. unsigned long addr, unsigned long end, pgprot_t newprot,
  88. int dirty_accountable)
  89. {
  90. pud_t *pud;
  91. unsigned long next;
  92. pud = pud_offset(pgd, addr);
  93. do {
  94. next = pud_addr_end(addr, end);
  95. if (pud_none_or_clear_bad(pud))
  96. continue;
  97. change_pmd_range(mm, pud, addr, next, newprot, dirty_accountable);
  98. } while (pud++, addr = next, addr != end);
  99. }
  100. static void change_protection(struct vm_area_struct *vma,
  101. unsigned long addr, unsigned long end, pgprot_t newprot,
  102. int dirty_accountable)
  103. {
  104. struct mm_struct *mm = vma->vm_mm;
  105. pgd_t *pgd;
  106. unsigned long next;
  107. unsigned long start = addr;
  108. BUG_ON(addr >= end);
  109. pgd = pgd_offset(mm, addr);
  110. flush_cache_range(vma, addr, end);
  111. do {
  112. next = pgd_addr_end(addr, end);
  113. if (pgd_none_or_clear_bad(pgd))
  114. continue;
  115. change_pud_range(mm, pgd, addr, next, newprot, dirty_accountable);
  116. } while (pgd++, addr = next, addr != end);
  117. flush_tlb_range(vma, start, end);
  118. }
  119. int
  120. mprotect_fixup(struct vm_area_struct *vma, struct vm_area_struct **pprev,
  121. unsigned long start, unsigned long end, unsigned long newflags)
  122. {
  123. struct mm_struct *mm = vma->vm_mm;
  124. unsigned long oldflags = vma->vm_flags;
  125. long nrpages = (end - start) >> PAGE_SHIFT;
  126. unsigned long charged = 0;
  127. pgoff_t pgoff;
  128. int error;
  129. int dirty_accountable = 0;
  130. if (newflags == oldflags) {
  131. *pprev = vma;
  132. return 0;
  133. }
  134. /*
  135. * If we make a private mapping writable we increase our commit;
  136. * but (without finer accounting) cannot reduce our commit if we
  137. * make it unwritable again. hugetlb mapping were accounted for
  138. * even if read-only so there is no need to account for them here
  139. */
  140. if (newflags & VM_WRITE) {
  141. if (!(oldflags & (VM_ACCOUNT|VM_WRITE|VM_HUGETLB|
  142. VM_SHARED|VM_NORESERVE))) {
  143. charged = nrpages;
  144. if (security_vm_enough_memory(charged))
  145. return -ENOMEM;
  146. newflags |= VM_ACCOUNT;
  147. }
  148. }
  149. /*
  150. * First try to merge with previous and/or next vma.
  151. */
  152. pgoff = vma->vm_pgoff + ((start - vma->vm_start) >> PAGE_SHIFT);
  153. *pprev = vma_merge(mm, *pprev, start, end, newflags,
  154. vma->anon_vma, vma->vm_file, pgoff, vma_policy(vma));
  155. if (*pprev) {
  156. vma = *pprev;
  157. goto success;
  158. }
  159. *pprev = vma;
  160. if (start != vma->vm_start) {
  161. error = split_vma(mm, vma, start, 1);
  162. if (error)
  163. goto fail;
  164. }
  165. if (end != vma->vm_end) {
  166. error = split_vma(mm, vma, end, 0);
  167. if (error)
  168. goto fail;
  169. }
  170. success:
  171. /*
  172. * vm_flags and vm_page_prot are protected by the mmap_sem
  173. * held in write mode.
  174. */
  175. vma->vm_flags = newflags;
  176. vma->vm_page_prot = pgprot_modify(vma->vm_page_prot,
  177. vm_get_page_prot(newflags));
  178. if (vma_wants_writenotify(vma)) {
  179. vma->vm_page_prot = vm_get_page_prot(newflags & ~VM_SHARED);
  180. dirty_accountable = 1;
  181. }
  182. mmu_notifier_invalidate_range_start(mm, start, end);
  183. if (is_vm_hugetlb_page(vma))
  184. hugetlb_change_protection(vma, start, end, vma->vm_page_prot);
  185. else
  186. change_protection(vma, start, end, vma->vm_page_prot, dirty_accountable);
  187. mmu_notifier_invalidate_range_end(mm, start, end);
  188. vm_stat_account(mm, oldflags, vma->vm_file, -nrpages);
  189. vm_stat_account(mm, newflags, vma->vm_file, nrpages);
  190. return 0;
  191. fail:
  192. vm_unacct_memory(charged);
  193. return error;
  194. }
  195. SYSCALL_DEFINE3(mprotect, unsigned long, start, size_t, len,
  196. unsigned long, prot)
  197. {
  198. unsigned long vm_flags, nstart, end, tmp, reqprot;
  199. struct vm_area_struct *vma, *prev;
  200. int error = -EINVAL;
  201. const int grows = prot & (PROT_GROWSDOWN|PROT_GROWSUP);
  202. prot &= ~(PROT_GROWSDOWN|PROT_GROWSUP);
  203. if (grows == (PROT_GROWSDOWN|PROT_GROWSUP)) /* can't be both */
  204. return -EINVAL;
  205. if (start & ~PAGE_MASK)
  206. return -EINVAL;
  207. if (!len)
  208. return 0;
  209. len = PAGE_ALIGN(len);
  210. end = start + len;
  211. if (end <= start)
  212. return -ENOMEM;
  213. if (!arch_validate_prot(prot))
  214. return -EINVAL;
  215. reqprot = prot;
  216. /*
  217. * Does the application expect PROT_READ to imply PROT_EXEC:
  218. */
  219. if ((prot & PROT_READ) && (current->personality & READ_IMPLIES_EXEC))
  220. prot |= PROT_EXEC;
  221. vm_flags = calc_vm_prot_bits(prot);
  222. down_write(&current->mm->mmap_sem);
  223. vma = find_vma_prev(current->mm, start, &prev);
  224. error = -ENOMEM;
  225. if (!vma)
  226. goto out;
  227. if (unlikely(grows & PROT_GROWSDOWN)) {
  228. if (vma->vm_start >= end)
  229. goto out;
  230. start = vma->vm_start;
  231. error = -EINVAL;
  232. if (!(vma->vm_flags & VM_GROWSDOWN))
  233. goto out;
  234. }
  235. else {
  236. if (vma->vm_start > start)
  237. goto out;
  238. if (unlikely(grows & PROT_GROWSUP)) {
  239. end = vma->vm_end;
  240. error = -EINVAL;
  241. if (!(vma->vm_flags & VM_GROWSUP))
  242. goto out;
  243. }
  244. }
  245. if (start > vma->vm_start)
  246. prev = vma;
  247. for (nstart = start ; ; ) {
  248. unsigned long newflags;
  249. /* Here we know that vma->vm_start <= nstart < vma->vm_end. */
  250. newflags = vm_flags | (vma->vm_flags & ~(VM_READ | VM_WRITE | VM_EXEC));
  251. /* newflags >> 4 shift VM_MAY% in place of VM_% */
  252. if ((newflags & ~(newflags >> 4)) & (VM_READ | VM_WRITE | VM_EXEC)) {
  253. error = -EACCES;
  254. goto out;
  255. }
  256. error = security_file_mprotect(vma, reqprot, prot);
  257. if (error)
  258. goto out;
  259. tmp = vma->vm_end;
  260. if (tmp > end)
  261. tmp = end;
  262. error = mprotect_fixup(vma, &prev, nstart, tmp, newflags);
  263. if (error)
  264. goto out;
  265. nstart = tmp;
  266. if (nstart < prev->vm_end)
  267. nstart = prev->vm_end;
  268. if (nstart >= end)
  269. goto out;
  270. vma = prev->vm_next;
  271. if (!vma || vma->vm_start != nstart) {
  272. error = -ENOMEM;
  273. goto out;
  274. }
  275. }
  276. out:
  277. up_write(&current->mm->mmap_sem);
  278. return error;
  279. }