mprotect.c 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377
  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@lxorguk.ukuu.org.uk>
  8. * (C) Copyright 2002 Red Hat Inc, All Rights Reserved
  9. */
  10. #include <linux/mm.h>
  11. #include <linux/hugetlb.h>
  12. #include <linux/shm.h>
  13. #include <linux/mman.h>
  14. #include <linux/fs.h>
  15. #include <linux/highmem.h>
  16. #include <linux/security.h>
  17. #include <linux/mempolicy.h>
  18. #include <linux/personality.h>
  19. #include <linux/syscalls.h>
  20. #include <linux/swap.h>
  21. #include <linux/swapops.h>
  22. #include <linux/mmu_notifier.h>
  23. #include <linux/migrate.h>
  24. #include <linux/perf_event.h>
  25. #include <asm/uaccess.h>
  26. #include <asm/pgtable.h>
  27. #include <asm/cacheflush.h>
  28. #include <asm/tlbflush.h>
  29. #ifndef pgprot_modify
  30. static inline pgprot_t pgprot_modify(pgprot_t oldprot, pgprot_t newprot)
  31. {
  32. return newprot;
  33. }
  34. #endif
  35. static void change_pte_range(struct mm_struct *mm, pmd_t *pmd,
  36. unsigned long addr, unsigned long end, pgprot_t newprot,
  37. int dirty_accountable)
  38. {
  39. pte_t *pte, oldpte;
  40. spinlock_t *ptl;
  41. #ifdef CONFIG_TIMA_RKP_L2_GROUP
  42. unsigned long tima_l2group_flag = 0;
  43. tima_l2group_entry_t *tima_l2group_buffer1 = NULL;
  44. tima_l2group_entry_t *tima_l2group_buffer2 = NULL;
  45. unsigned long tima_l2group_numb_entries = ((end-addr) >> PAGE_SHIFT);
  46. unsigned long tima_l2group_buffer_index = 0;
  47. #endif
  48. pte = pte_offset_map_lock(mm, pmd, addr, &ptl);
  49. arch_enter_lazy_mmu_mode();
  50. #ifdef CONFIG_TIMA_RKP_L2_GROUP
  51. /*
  52. * Lazy mmu mode for tima:
  53. */
  54. init_tima_rkp_group_buffers(tima_l2group_numb_entries, pte,
  55. &tima_l2group_flag, &tima_l2group_buffer_index,
  56. &tima_l2group_buffer1, &tima_l2group_buffer2);
  57. #endif /* CONFIG_TIMA_RKP_L2_GROUP */
  58. do {
  59. oldpte = *pte;
  60. if (pte_present(oldpte)) {
  61. pte_t ptent;
  62. #ifdef CONFIG_TIMA_RKP_L2_GROUP
  63. /*
  64. * skip the pte_clear here. Instead just
  65. * dereference the pte pointer and get the
  66. * pte value. tima_l2group_ptep_modify_prot_commit
  67. * will write the same pte anyway.
  68. */
  69. ptent = *pte;
  70. #else
  71. ptent = ptep_modify_prot_start(mm, addr, pte);
  72. #endif
  73. ptent = pte_modify(ptent, newprot);
  74. /*
  75. * Avoid taking write faults for pages we know to be
  76. * dirty.
  77. */
  78. if (dirty_accountable && pte_dirty(ptent))
  79. ptent = pte_mkwrite(ptent);
  80. #ifdef CONFIG_TIMA_RKP_L2_GROUP
  81. tima_l2group_ptep_modify_prot_commit(mm, addr,
  82. pte, ptent, tima_l2group_buffer1,
  83. tima_l2group_buffer2,
  84. &tima_l2group_buffer_index,
  85. tima_l2group_flag);
  86. #else
  87. ptep_modify_prot_commit(mm, addr, pte, ptent);
  88. #endif
  89. } else if (IS_ENABLED(CONFIG_MIGRATION) && !pte_file(oldpte)) {
  90. swp_entry_t entry = pte_to_swp_entry(oldpte);
  91. if (is_write_migration_entry(entry)) {
  92. /*
  93. * A protection check is difficult so
  94. * just be safe and disable write
  95. */
  96. make_migration_entry_read(&entry);
  97. set_pte_at(mm, addr, pte,
  98. swp_entry_to_pte(entry));
  99. }
  100. }
  101. } while (pte++, addr += PAGE_SIZE, addr != end);
  102. #ifdef CONFIG_TIMA_RKP_L2_GROUP
  103. if (tima_l2group_flag) {
  104. /* First: Flush the cache of the buffer to be read by the TZ side
  105. */
  106. if(tima_l2group_buffer1)
  107. flush_dcache_page(virt_to_page(tima_l2group_buffer1));
  108. if(tima_l2group_buffer2)
  109. flush_dcache_page(virt_to_page(tima_l2group_buffer2));
  110. /* Second: Pass the buffer pointers and length to TIMA to commit the changes
  111. */
  112. write_tima_rkp_group_buffers(tima_l2group_buffer_index,
  113. &tima_l2group_buffer1, &tima_l2group_buffer2);
  114. }
  115. #endif
  116. arch_leave_lazy_mmu_mode();
  117. pte_unmap_unlock(pte - 1, ptl);
  118. }
  119. static inline void change_pmd_range(struct vm_area_struct *vma, pud_t *pud,
  120. unsigned long addr, unsigned long end, pgprot_t newprot,
  121. int dirty_accountable)
  122. {
  123. pmd_t *pmd;
  124. unsigned long next;
  125. pmd = pmd_offset(pud, addr);
  126. do {
  127. next = pmd_addr_end(addr, end);
  128. if (pmd_trans_huge(*pmd)) {
  129. if (next - addr != HPAGE_PMD_SIZE)
  130. split_huge_page_pmd(vma->vm_mm, pmd);
  131. else if (change_huge_pmd(vma, pmd, addr, newprot))
  132. continue;
  133. /* fall through */
  134. }
  135. if (pmd_none_or_clear_bad(pmd))
  136. continue;
  137. change_pte_range(vma->vm_mm, pmd, addr, next, newprot,
  138. dirty_accountable);
  139. } while (pmd++, addr = next, addr != end);
  140. }
  141. static inline void change_pud_range(struct vm_area_struct *vma, pgd_t *pgd,
  142. unsigned long addr, unsigned long end, pgprot_t newprot,
  143. int dirty_accountable)
  144. {
  145. pud_t *pud;
  146. unsigned long next;
  147. pud = pud_offset(pgd, addr);
  148. do {
  149. next = pud_addr_end(addr, end);
  150. if (pud_none_or_clear_bad(pud))
  151. continue;
  152. change_pmd_range(vma, pud, addr, next, newprot,
  153. dirty_accountable);
  154. } while (pud++, addr = next, addr != end);
  155. }
  156. static void change_protection(struct vm_area_struct *vma,
  157. unsigned long addr, unsigned long end, pgprot_t newprot,
  158. int dirty_accountable)
  159. {
  160. struct mm_struct *mm = vma->vm_mm;
  161. pgd_t *pgd;
  162. unsigned long next;
  163. unsigned long start = addr;
  164. BUG_ON(addr >= end);
  165. pgd = pgd_offset(mm, addr);
  166. flush_cache_range(vma, addr, end);
  167. do {
  168. next = pgd_addr_end(addr, end);
  169. if (pgd_none_or_clear_bad(pgd))
  170. continue;
  171. change_pud_range(vma, pgd, addr, next, newprot,
  172. dirty_accountable);
  173. } while (pgd++, addr = next, addr != end);
  174. flush_tlb_range(vma, start, end);
  175. }
  176. int
  177. mprotect_fixup(struct vm_area_struct *vma, struct vm_area_struct **pprev,
  178. unsigned long start, unsigned long end, unsigned long newflags)
  179. {
  180. struct mm_struct *mm = vma->vm_mm;
  181. unsigned long oldflags = vma->vm_flags;
  182. long nrpages = (end - start) >> PAGE_SHIFT;
  183. unsigned long charged = 0;
  184. pgoff_t pgoff;
  185. int error;
  186. int dirty_accountable = 0;
  187. if (newflags == oldflags) {
  188. *pprev = vma;
  189. return 0;
  190. }
  191. /*
  192. * If we make a private mapping writable we increase our commit;
  193. * but (without finer accounting) cannot reduce our commit if we
  194. * make it unwritable again. hugetlb mapping were accounted for
  195. * even if read-only so there is no need to account for them here
  196. */
  197. if (newflags & VM_WRITE) {
  198. if (!(oldflags & (VM_ACCOUNT|VM_WRITE|VM_HUGETLB|
  199. VM_SHARED|VM_NORESERVE))) {
  200. charged = nrpages;
  201. if (security_vm_enough_memory_mm(mm, charged))
  202. return -ENOMEM;
  203. newflags |= VM_ACCOUNT;
  204. }
  205. }
  206. /*
  207. * First try to merge with previous and/or next vma.
  208. */
  209. pgoff = vma->vm_pgoff + ((start - vma->vm_start) >> PAGE_SHIFT);
  210. *pprev = vma_merge(mm, *pprev, start, end, newflags,
  211. vma->anon_vma, vma->vm_file, pgoff, vma_policy(vma),
  212. vma_get_anon_name(vma));
  213. if (*pprev) {
  214. vma = *pprev;
  215. goto success;
  216. }
  217. *pprev = vma;
  218. if (start != vma->vm_start) {
  219. error = split_vma(mm, vma, start, 1);
  220. if (error)
  221. goto fail;
  222. }
  223. if (end != vma->vm_end) {
  224. error = split_vma(mm, vma, end, 0);
  225. if (error)
  226. goto fail;
  227. }
  228. success:
  229. /*
  230. * vm_flags and vm_page_prot are protected by the mmap_sem
  231. * held in write mode.
  232. */
  233. vma->vm_flags = newflags;
  234. vma->vm_page_prot = pgprot_modify(vma->vm_page_prot,
  235. vm_get_page_prot(newflags));
  236. if (vma_wants_writenotify(vma)) {
  237. vma->vm_page_prot = vm_get_page_prot(newflags & ~VM_SHARED);
  238. dirty_accountable = 1;
  239. }
  240. mmu_notifier_invalidate_range_start(mm, start, end);
  241. if (is_vm_hugetlb_page(vma))
  242. hugetlb_change_protection(vma, start, end, vma->vm_page_prot);
  243. else
  244. change_protection(vma, start, end, vma->vm_page_prot, dirty_accountable);
  245. mmu_notifier_invalidate_range_end(mm, start, end);
  246. vm_stat_account(mm, oldflags, vma->vm_file, -nrpages);
  247. vm_stat_account(mm, newflags, vma->vm_file, nrpages);
  248. perf_event_mmap(vma);
  249. return 0;
  250. fail:
  251. vm_unacct_memory(charged);
  252. return error;
  253. }
  254. SYSCALL_DEFINE3(mprotect, unsigned long, start, size_t, len,
  255. unsigned long, prot)
  256. {
  257. unsigned long vm_flags, nstart, end, tmp, reqprot;
  258. struct vm_area_struct *vma, *prev;
  259. int error = -EINVAL;
  260. const int grows = prot & (PROT_GROWSDOWN|PROT_GROWSUP);
  261. prot &= ~(PROT_GROWSDOWN|PROT_GROWSUP);
  262. if (grows == (PROT_GROWSDOWN|PROT_GROWSUP)) /* can't be both */
  263. return -EINVAL;
  264. if (start & ~PAGE_MASK)
  265. return -EINVAL;
  266. if (!len)
  267. return 0;
  268. len = PAGE_ALIGN(len);
  269. end = start + len;
  270. if (end <= start)
  271. return -ENOMEM;
  272. if (!arch_validate_prot(prot))
  273. return -EINVAL;
  274. reqprot = prot;
  275. /*
  276. * Does the application expect PROT_READ to imply PROT_EXEC:
  277. */
  278. if ((prot & PROT_READ) && (current->personality & READ_IMPLIES_EXEC))
  279. prot |= PROT_EXEC;
  280. vm_flags = calc_vm_prot_bits(prot);
  281. down_write(&current->mm->mmap_sem);
  282. vma = find_vma(current->mm, start);
  283. error = -ENOMEM;
  284. if (!vma)
  285. goto out;
  286. prev = vma->vm_prev;
  287. if (unlikely(grows & PROT_GROWSDOWN)) {
  288. if (vma->vm_start >= end)
  289. goto out;
  290. start = vma->vm_start;
  291. error = -EINVAL;
  292. if (!(vma->vm_flags & VM_GROWSDOWN))
  293. goto out;
  294. }
  295. else {
  296. if (vma->vm_start > start)
  297. goto out;
  298. if (unlikely(grows & PROT_GROWSUP)) {
  299. end = vma->vm_end;
  300. error = -EINVAL;
  301. if (!(vma->vm_flags & VM_GROWSUP))
  302. goto out;
  303. }
  304. }
  305. if (start > vma->vm_start)
  306. prev = vma;
  307. for (nstart = start ; ; ) {
  308. unsigned long newflags;
  309. /* Here we know that vma->vm_start <= nstart < vma->vm_end. */
  310. newflags = vm_flags | (vma->vm_flags & ~(VM_READ | VM_WRITE | VM_EXEC));
  311. /* newflags >> 4 shift VM_MAY% in place of VM_% */
  312. if ((newflags & ~(newflags >> 4)) & (VM_READ | VM_WRITE | VM_EXEC)) {
  313. error = -EACCES;
  314. goto out;
  315. }
  316. error = security_file_mprotect(vma, reqprot, prot);
  317. if (error)
  318. goto out;
  319. tmp = vma->vm_end;
  320. if (tmp > end)
  321. tmp = end;
  322. error = mprotect_fixup(vma, &prev, nstart, tmp, newflags);
  323. if (error)
  324. goto out;
  325. nstart = tmp;
  326. if (nstart < prev->vm_end)
  327. nstart = prev->vm_end;
  328. if (nstart >= end)
  329. goto out;
  330. vma = prev->vm_next;
  331. if (!vma || vma->vm_start != nstart) {
  332. error = -ENOMEM;
  333. goto out;
  334. }
  335. }
  336. out:
  337. up_write(&current->mm->mmap_sem);
  338. return error;
  339. }