mprotect.c 7.6 KB

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