mprotect.c 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290
  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_RESERVED) {
  114. BUG_ON(oldflags & VM_WRITE);
  115. printk(KERN_WARNING "program %s is using MAP_PRIVATE, "
  116. "PROT_WRITE mprotect of VM_RESERVED memory, "
  117. "which is deprecated. Please report this to "
  118. "linux-kernel@vger.kernel.org\n",current->comm);
  119. return -EACCES;
  120. }
  121. if (!(oldflags & (VM_ACCOUNT|VM_WRITE|VM_SHARED|VM_HUGETLB))) {
  122. charged = nrpages;
  123. if (security_vm_enough_memory(charged))
  124. return -ENOMEM;
  125. newflags |= VM_ACCOUNT;
  126. }
  127. }
  128. newprot = protection_map[newflags & 0xf];
  129. /*
  130. * First try to merge with previous and/or next vma.
  131. */
  132. pgoff = vma->vm_pgoff + ((start - vma->vm_start) >> PAGE_SHIFT);
  133. *pprev = vma_merge(mm, *pprev, start, end, newflags,
  134. vma->anon_vma, vma->vm_file, pgoff, vma_policy(vma));
  135. if (*pprev) {
  136. vma = *pprev;
  137. goto success;
  138. }
  139. *pprev = vma;
  140. if (start != vma->vm_start) {
  141. error = split_vma(mm, vma, start, 1);
  142. if (error)
  143. goto fail;
  144. }
  145. if (end != vma->vm_end) {
  146. error = split_vma(mm, vma, end, 0);
  147. if (error)
  148. goto fail;
  149. }
  150. success:
  151. /*
  152. * vm_flags and vm_page_prot are protected by the mmap_sem
  153. * held in write mode.
  154. */
  155. vma->vm_flags = newflags;
  156. vma->vm_page_prot = newprot;
  157. change_protection(vma, start, end, newprot);
  158. vm_stat_account(mm, oldflags, vma->vm_file, -nrpages);
  159. vm_stat_account(mm, newflags, vma->vm_file, nrpages);
  160. return 0;
  161. fail:
  162. vm_unacct_memory(charged);
  163. return error;
  164. }
  165. asmlinkage long
  166. sys_mprotect(unsigned long start, size_t len, unsigned long prot)
  167. {
  168. unsigned long vm_flags, nstart, end, tmp, reqprot;
  169. struct vm_area_struct *vma, *prev;
  170. int error = -EINVAL;
  171. const int grows = prot & (PROT_GROWSDOWN|PROT_GROWSUP);
  172. prot &= ~(PROT_GROWSDOWN|PROT_GROWSUP);
  173. if (grows == (PROT_GROWSDOWN|PROT_GROWSUP)) /* can't be both */
  174. return -EINVAL;
  175. if (start & ~PAGE_MASK)
  176. return -EINVAL;
  177. if (!len)
  178. return 0;
  179. len = PAGE_ALIGN(len);
  180. end = start + len;
  181. if (end <= start)
  182. return -ENOMEM;
  183. if (prot & ~(PROT_READ | PROT_WRITE | PROT_EXEC | PROT_SEM))
  184. return -EINVAL;
  185. reqprot = prot;
  186. /*
  187. * Does the application expect PROT_READ to imply PROT_EXEC:
  188. */
  189. if (unlikely((prot & PROT_READ) &&
  190. (current->personality & READ_IMPLIES_EXEC)))
  191. prot |= PROT_EXEC;
  192. vm_flags = calc_vm_prot_bits(prot);
  193. down_write(&current->mm->mmap_sem);
  194. vma = find_vma_prev(current->mm, start, &prev);
  195. error = -ENOMEM;
  196. if (!vma)
  197. goto out;
  198. if (unlikely(grows & PROT_GROWSDOWN)) {
  199. if (vma->vm_start >= end)
  200. goto out;
  201. start = vma->vm_start;
  202. error = -EINVAL;
  203. if (!(vma->vm_flags & VM_GROWSDOWN))
  204. goto out;
  205. }
  206. else {
  207. if (vma->vm_start > start)
  208. goto out;
  209. if (unlikely(grows & PROT_GROWSUP)) {
  210. end = vma->vm_end;
  211. error = -EINVAL;
  212. if (!(vma->vm_flags & VM_GROWSUP))
  213. goto out;
  214. }
  215. }
  216. if (start > vma->vm_start)
  217. prev = vma;
  218. for (nstart = start ; ; ) {
  219. unsigned long newflags;
  220. /* Here we know that vma->vm_start <= nstart < vma->vm_end. */
  221. if (is_vm_hugetlb_page(vma)) {
  222. error = -EACCES;
  223. goto out;
  224. }
  225. newflags = vm_flags | (vma->vm_flags & ~(VM_READ | VM_WRITE | VM_EXEC));
  226. /* newflags >> 4 shift VM_MAY% in place of VM_% */
  227. if ((newflags & ~(newflags >> 4)) & (VM_READ | VM_WRITE | VM_EXEC)) {
  228. error = -EACCES;
  229. goto out;
  230. }
  231. error = security_file_mprotect(vma, reqprot, prot);
  232. if (error)
  233. goto out;
  234. tmp = vma->vm_end;
  235. if (tmp > end)
  236. tmp = end;
  237. error = mprotect_fixup(vma, &prev, nstart, tmp, newflags);
  238. if (error)
  239. goto out;
  240. nstart = tmp;
  241. if (nstart < prev->vm_end)
  242. nstart = prev->vm_end;
  243. if (nstart >= end)
  244. goto out;
  245. vma = prev->vm_next;
  246. if (!vma || vma->vm_start != nstart) {
  247. error = -ENOMEM;
  248. goto out;
  249. }
  250. }
  251. out:
  252. up_write(&current->mm->mmap_sem);
  253. return error;
  254. }