uaccess.c 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368
  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. * @op: operation to execute
  197. * @oparg: argument to operation
  198. * @oval: old value at uaddr
  199. * @uaddr: pointer to user space address
  200. *
  201. * Return:
  202. * 0 - On success
  203. * -EFAULT - User access resulted in a page fault
  204. * -EAGAIN - Atomic operation was unable to complete due to contention
  205. * -ENOSYS - Operation not supported
  206. */
  207. int arch_futex_atomic_op_inuser(int op, u32 oparg, int *oval, u32 __user *uaddr)
  208. {
  209. int oldval, ret;
  210. struct page *page;
  211. unsigned long addr = (unsigned long) uaddr;
  212. pte_t *pte;
  213. ret = -EFAULT;
  214. if (!access_ok(uaddr, sizeof(*uaddr)))
  215. return -EFAULT;
  216. preempt_disable();
  217. pte = maybe_map(addr, 1);
  218. if (pte == NULL)
  219. goto out_inuser;
  220. page = pte_page(*pte);
  221. #ifdef CONFIG_64BIT
  222. pagefault_disable();
  223. addr = (unsigned long) page_address(page) +
  224. (((unsigned long) addr) & ~PAGE_MASK);
  225. #else
  226. addr = (unsigned long) kmap_atomic(page) +
  227. ((unsigned long) addr & ~PAGE_MASK);
  228. #endif
  229. uaddr = (u32 *) addr;
  230. oldval = *uaddr;
  231. ret = 0;
  232. switch (op) {
  233. case FUTEX_OP_SET:
  234. *uaddr = oparg;
  235. break;
  236. case FUTEX_OP_ADD:
  237. *uaddr += oparg;
  238. break;
  239. case FUTEX_OP_OR:
  240. *uaddr |= oparg;
  241. break;
  242. case FUTEX_OP_ANDN:
  243. *uaddr &= ~oparg;
  244. break;
  245. case FUTEX_OP_XOR:
  246. *uaddr ^= oparg;
  247. break;
  248. default:
  249. ret = -ENOSYS;
  250. }
  251. #ifdef CONFIG_64BIT
  252. pagefault_enable();
  253. #else
  254. kunmap_atomic((void *)addr);
  255. #endif
  256. out_inuser:
  257. preempt_enable();
  258. if (ret == 0)
  259. *oval = oldval;
  260. return ret;
  261. }
  262. EXPORT_SYMBOL(arch_futex_atomic_op_inuser);
  263. /**
  264. * futex_atomic_cmpxchg_inatomic() - Compare and exchange the content of the
  265. * uaddr with newval if the current value is
  266. * oldval.
  267. * @uval: pointer to store content of @uaddr
  268. * @uaddr: pointer to user space address
  269. * @oldval: old value
  270. * @newval: new value to store to @uaddr
  271. *
  272. * Return:
  273. * 0 - On success
  274. * -EFAULT - User access resulted in a page fault
  275. * -EAGAIN - Atomic operation was unable to complete due to contention
  276. */
  277. int futex_atomic_cmpxchg_inatomic(u32 *uval, u32 __user *uaddr,
  278. u32 oldval, u32 newval)
  279. {
  280. struct page *page;
  281. pte_t *pte;
  282. int ret = -EFAULT;
  283. if (!access_ok(uaddr, sizeof(*uaddr)))
  284. return -EFAULT;
  285. preempt_disable();
  286. pte = maybe_map((unsigned long) uaddr, 1);
  287. if (pte == NULL)
  288. goto out_inatomic;
  289. page = pte_page(*pte);
  290. #ifdef CONFIG_64BIT
  291. pagefault_disable();
  292. uaddr = page_address(page) + (((unsigned long) uaddr) & ~PAGE_MASK);
  293. #else
  294. uaddr = kmap_atomic(page) + ((unsigned long) uaddr & ~PAGE_MASK);
  295. #endif
  296. *uval = *uaddr;
  297. ret = cmpxchg(uaddr, oldval, newval);
  298. #ifdef CONFIG_64BIT
  299. pagefault_enable();
  300. #else
  301. kunmap_atomic(uaddr);
  302. #endif
  303. ret = 0;
  304. out_inatomic:
  305. preempt_enable();
  306. return ret;
  307. }
  308. EXPORT_SYMBOL(futex_atomic_cmpxchg_inatomic);