uaccess.c 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Copyright (C) 2002 - 2007 Jeff Dike (jdike@{addtoit,linux.intel}.com)
  4. */
  5. #include <linux/err.h>
  6. #include <linux/highmem.h>
  7. #include <linux/mm.h>
  8. #include <linux/module.h>
  9. #include <linux/sched.h>
  10. #include <asm/current.h>
  11. #include <asm/page.h>
  12. #include <kern_util.h>
  13. #include <asm/futex.h>
  14. #include <os.h>
  15. pte_t *virt_to_pte(struct mm_struct *mm, unsigned long addr)
  16. {
  17. pgd_t *pgd;
  18. p4d_t *p4d;
  19. pud_t *pud;
  20. pmd_t *pmd;
  21. if (mm == NULL)
  22. return NULL;
  23. pgd = pgd_offset(mm, addr);
  24. if (!pgd_present(*pgd))
  25. return NULL;
  26. p4d = p4d_offset(pgd, addr);
  27. if (!p4d_present(*p4d))
  28. return NULL;
  29. pud = pud_offset(p4d, addr);
  30. if (!pud_present(*pud))
  31. return NULL;
  32. pmd = pmd_offset(pud, addr);
  33. if (!pmd_present(*pmd))
  34. return NULL;
  35. return pte_offset_kernel(pmd, addr);
  36. }
  37. static pte_t *maybe_map(unsigned long virt, int is_write)
  38. {
  39. pte_t *pte = virt_to_pte(current->mm, virt);
  40. int err, dummy_code;
  41. if ((pte == NULL) || !pte_present(*pte) ||
  42. (is_write && !pte_write(*pte))) {
  43. err = handle_page_fault(virt, 0, is_write, 1, &dummy_code);
  44. if (err)
  45. return NULL;
  46. pte = virt_to_pte(current->mm, virt);
  47. }
  48. if (!pte_present(*pte))
  49. pte = NULL;
  50. return pte;
  51. }
  52. static int do_op_one_page(unsigned long addr, int len, int is_write,
  53. int (*op)(unsigned long addr, int len, void *arg), void *arg)
  54. {
  55. struct page *page;
  56. pte_t *pte;
  57. int n;
  58. pte = maybe_map(addr, is_write);
  59. if (pte == NULL)
  60. return -1;
  61. page = pte_page(*pte);
  62. #ifdef CONFIG_64BIT
  63. pagefault_disable();
  64. addr = (unsigned long) page_address(page) +
  65. (addr & ~PAGE_MASK);
  66. #else
  67. addr = (unsigned long) kmap_atomic(page) +
  68. (addr & ~PAGE_MASK);
  69. #endif
  70. n = (*op)(addr, len, arg);
  71. #ifdef CONFIG_64BIT
  72. pagefault_enable();
  73. #else
  74. kunmap_atomic((void *)addr);
  75. #endif
  76. return n;
  77. }
  78. static long buffer_op(unsigned long addr, int len, int is_write,
  79. int (*op)(unsigned long, int, void *), void *arg)
  80. {
  81. long size, remain, n;
  82. size = min(PAGE_ALIGN(addr) - addr, (unsigned long) len);
  83. remain = len;
  84. n = do_op_one_page(addr, size, is_write, op, arg);
  85. if (n != 0) {
  86. remain = (n < 0 ? remain : 0);
  87. goto out;
  88. }
  89. addr += size;
  90. remain -= size;
  91. if (remain == 0)
  92. goto out;
  93. while (addr < ((addr + remain) & PAGE_MASK)) {
  94. n = do_op_one_page(addr, PAGE_SIZE, is_write, op, arg);
  95. if (n != 0) {
  96. remain = (n < 0 ? remain : 0);
  97. goto out;
  98. }
  99. addr += PAGE_SIZE;
  100. remain -= PAGE_SIZE;
  101. }
  102. if (remain == 0)
  103. goto out;
  104. n = do_op_one_page(addr, remain, is_write, op, arg);
  105. if (n != 0) {
  106. remain = (n < 0 ? remain : 0);
  107. goto out;
  108. }
  109. return 0;
  110. out:
  111. return remain;
  112. }
  113. static int copy_chunk_from_user(unsigned long from, int len, void *arg)
  114. {
  115. unsigned long *to_ptr = arg, to = *to_ptr;
  116. memcpy((void *) to, (void *) from, len);
  117. *to_ptr += len;
  118. return 0;
  119. }
  120. unsigned long raw_copy_from_user(void *to, const void __user *from, unsigned long n)
  121. {
  122. return buffer_op((unsigned long) from, n, 0, copy_chunk_from_user, &to);
  123. }
  124. EXPORT_SYMBOL(raw_copy_from_user);
  125. static int copy_chunk_to_user(unsigned long to, int len, void *arg)
  126. {
  127. unsigned long *from_ptr = arg, from = *from_ptr;
  128. memcpy((void *) to, (void *) from, len);
  129. *from_ptr += len;
  130. return 0;
  131. }
  132. unsigned long raw_copy_to_user(void __user *to, const void *from, unsigned long n)
  133. {
  134. return buffer_op((unsigned long) to, n, 1, copy_chunk_to_user, &from);
  135. }
  136. EXPORT_SYMBOL(raw_copy_to_user);
  137. static int strncpy_chunk_from_user(unsigned long from, int len, void *arg)
  138. {
  139. char **to_ptr = arg, *to = *to_ptr;
  140. int n;
  141. strncpy(to, (void *) from, len);
  142. n = strnlen(to, len);
  143. *to_ptr += n;
  144. if (n < len)
  145. return 1;
  146. return 0;
  147. }
  148. long strncpy_from_user(char *dst, const char __user *src, long count)
  149. {
  150. long n;
  151. char *ptr = dst;
  152. if (!access_ok(src, 1))
  153. return -EFAULT;
  154. n = buffer_op((unsigned long) src, count, 0, strncpy_chunk_from_user,
  155. &ptr);
  156. if (n != 0)
  157. return -EFAULT;
  158. return strnlen(dst, count);
  159. }
  160. EXPORT_SYMBOL(strncpy_from_user);
  161. static int clear_chunk(unsigned long addr, int len, void *unused)
  162. {
  163. memset((void *) addr, 0, len);
  164. return 0;
  165. }
  166. unsigned long __clear_user(void __user *mem, unsigned long len)
  167. {
  168. return buffer_op((unsigned long) mem, len, 1, clear_chunk, NULL);
  169. }
  170. EXPORT_SYMBOL(__clear_user);
  171. static int strnlen_chunk(unsigned long str, int len, void *arg)
  172. {
  173. int *len_ptr = arg, n;
  174. n = strnlen((void *) str, len);
  175. *len_ptr += n;
  176. if (n < len)
  177. return 1;
  178. return 0;
  179. }
  180. long strnlen_user(const char __user *str, long len)
  181. {
  182. int count = 0, n;
  183. if (!access_ok(str, 1))
  184. return -EFAULT;
  185. n = buffer_op((unsigned long) str, len, 0, strnlen_chunk, &count);
  186. if (n == 0)
  187. return count + 1;
  188. return 0;
  189. }
  190. EXPORT_SYMBOL(strnlen_user);
  191. /**
  192. * arch_futex_atomic_op_inuser() - Atomic arithmetic operation with constant
  193. * argument and comparison of the previous
  194. * futex value with another constant.
  195. *
  196. * @encoded_op: encoded operation to execute
  197. * @uaddr: pointer to user space address
  198. *
  199. * Return:
  200. * 0 - On success
  201. * -EFAULT - User access resulted in a page fault
  202. * -EAGAIN - Atomic operation was unable to complete due to contention
  203. * -ENOSYS - Operation not supported
  204. */
  205. int arch_futex_atomic_op_inuser(int op, u32 oparg, int *oval, u32 __user *uaddr)
  206. {
  207. int oldval, ret;
  208. struct page *page;
  209. unsigned long addr = (unsigned long) uaddr;
  210. pte_t *pte;
  211. ret = -EFAULT;
  212. if (!access_ok(uaddr, sizeof(*uaddr)))
  213. return -EFAULT;
  214. preempt_disable();
  215. pte = maybe_map(addr, 1);
  216. if (pte == NULL)
  217. goto out_inuser;
  218. page = pte_page(*pte);
  219. #ifdef CONFIG_64BIT
  220. pagefault_disable();
  221. addr = (unsigned long) page_address(page) +
  222. (((unsigned long) addr) & ~PAGE_MASK);
  223. #else
  224. addr = (unsigned long) kmap_atomic(page) +
  225. ((unsigned long) addr & ~PAGE_MASK);
  226. #endif
  227. uaddr = (u32 *) addr;
  228. oldval = *uaddr;
  229. ret = 0;
  230. switch (op) {
  231. case FUTEX_OP_SET:
  232. *uaddr = oparg;
  233. break;
  234. case FUTEX_OP_ADD:
  235. *uaddr += oparg;
  236. break;
  237. case FUTEX_OP_OR:
  238. *uaddr |= oparg;
  239. break;
  240. case FUTEX_OP_ANDN:
  241. *uaddr &= ~oparg;
  242. break;
  243. case FUTEX_OP_XOR:
  244. *uaddr ^= oparg;
  245. break;
  246. default:
  247. ret = -ENOSYS;
  248. }
  249. #ifdef CONFIG_64BIT
  250. pagefault_enable();
  251. #else
  252. kunmap_atomic((void *)addr);
  253. #endif
  254. out_inuser:
  255. preempt_enable();
  256. if (ret == 0)
  257. *oval = oldval;
  258. return ret;
  259. }
  260. EXPORT_SYMBOL(arch_futex_atomic_op_inuser);
  261. /**
  262. * futex_atomic_cmpxchg_inatomic() - Compare and exchange the content of the
  263. * uaddr with newval if the current value is
  264. * oldval.
  265. * @uval: pointer to store content of @uaddr
  266. * @uaddr: pointer to user space address
  267. * @oldval: old value
  268. * @newval: new value to store to @uaddr
  269. *
  270. * Return:
  271. * 0 - On success
  272. * -EFAULT - User access resulted in a page fault
  273. * -EAGAIN - Atomic operation was unable to complete due to contention
  274. */
  275. int futex_atomic_cmpxchg_inatomic(u32 *uval, u32 __user *uaddr,
  276. u32 oldval, u32 newval)
  277. {
  278. struct page *page;
  279. pte_t *pte;
  280. int ret = -EFAULT;
  281. if (!access_ok(uaddr, sizeof(*uaddr)))
  282. return -EFAULT;
  283. preempt_disable();
  284. pte = maybe_map((unsigned long) uaddr, 1);
  285. if (pte == NULL)
  286. goto out_inatomic;
  287. page = pte_page(*pte);
  288. #ifdef CONFIG_64BIT
  289. pagefault_disable();
  290. uaddr = page_address(page) + (((unsigned long) uaddr) & ~PAGE_MASK);
  291. #else
  292. uaddr = kmap_atomic(page) + ((unsigned long) uaddr & ~PAGE_MASK);
  293. #endif
  294. *uval = *uaddr;
  295. ret = cmpxchg(uaddr, oldval, newval);
  296. #ifdef CONFIG_64BIT
  297. pagefault_enable();
  298. #else
  299. kunmap_atomic(uaddr);
  300. #endif
  301. ret = 0;
  302. out_inatomic:
  303. preempt_enable();
  304. return ret;
  305. }
  306. EXPORT_SYMBOL(futex_atomic_cmpxchg_inatomic);