mprotect.c 7.2 KB

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