mprotect.c 6.8 KB

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