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