mprotect.c 7.5 KB

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