mprotect.c 6.5 KB

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