mprotect.c 7.7 KB

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