mprotect.c 7.4 KB

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