rseq.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * Restartable sequences system call
  4. *
  5. * Copyright (C) 2015, Google, Inc.,
  6. * Paul Turner <[email protected]> and Andrew Hunter <[email protected]>
  7. * Copyright (C) 2015-2018, EfficiOS Inc.,
  8. * Mathieu Desnoyers <[email protected]>
  9. */
  10. #include <linux/sched.h>
  11. #include <linux/uaccess.h>
  12. #include <linux/syscalls.h>
  13. #include <linux/rseq.h>
  14. #include <linux/types.h>
  15. #include <asm/ptrace.h>
  16. #define CREATE_TRACE_POINTS
  17. #include <trace/events/rseq.h>
  18. #define RSEQ_CS_NO_RESTART_FLAGS (RSEQ_CS_FLAG_NO_RESTART_ON_PREEMPT | \
  19. RSEQ_CS_FLAG_NO_RESTART_ON_SIGNAL | \
  20. RSEQ_CS_FLAG_NO_RESTART_ON_MIGRATE)
  21. /*
  22. *
  23. * Restartable sequences are a lightweight interface that allows
  24. * user-level code to be executed atomically relative to scheduler
  25. * preemption and signal delivery. Typically used for implementing
  26. * per-cpu operations.
  27. *
  28. * It allows user-space to perform update operations on per-cpu data
  29. * without requiring heavy-weight atomic operations.
  30. *
  31. * Detailed algorithm of rseq user-space assembly sequences:
  32. *
  33. * init(rseq_cs)
  34. * cpu = TLS->rseq::cpu_id_start
  35. * [1] TLS->rseq::rseq_cs = rseq_cs
  36. * [start_ip] ----------------------------
  37. * [2] if (cpu != TLS->rseq::cpu_id)
  38. * goto abort_ip;
  39. * [3] <last_instruction_in_cs>
  40. * [post_commit_ip] ----------------------------
  41. *
  42. * The address of jump target abort_ip must be outside the critical
  43. * region, i.e.:
  44. *
  45. * [abort_ip] < [start_ip] || [abort_ip] >= [post_commit_ip]
  46. *
  47. * Steps [2]-[3] (inclusive) need to be a sequence of instructions in
  48. * userspace that can handle being interrupted between any of those
  49. * instructions, and then resumed to the abort_ip.
  50. *
  51. * 1. Userspace stores the address of the struct rseq_cs assembly
  52. * block descriptor into the rseq_cs field of the registered
  53. * struct rseq TLS area. This update is performed through a single
  54. * store within the inline assembly instruction sequence.
  55. * [start_ip]
  56. *
  57. * 2. Userspace tests to check whether the current cpu_id field match
  58. * the cpu number loaded before start_ip, branching to abort_ip
  59. * in case of a mismatch.
  60. *
  61. * If the sequence is preempted or interrupted by a signal
  62. * at or after start_ip and before post_commit_ip, then the kernel
  63. * clears TLS->__rseq_abi::rseq_cs, and sets the user-space return
  64. * ip to abort_ip before returning to user-space, so the preempted
  65. * execution resumes at abort_ip.
  66. *
  67. * 3. Userspace critical section final instruction before
  68. * post_commit_ip is the commit. The critical section is
  69. * self-terminating.
  70. * [post_commit_ip]
  71. *
  72. * 4. <success>
  73. *
  74. * On failure at [2], or if interrupted by preempt or signal delivery
  75. * between [1] and [3]:
  76. *
  77. * [abort_ip]
  78. * F1. <failure>
  79. */
  80. static int rseq_update_cpu_id(struct task_struct *t)
  81. {
  82. u32 cpu_id = raw_smp_processor_id();
  83. struct rseq __user *rseq = t->rseq;
  84. if (!user_write_access_begin(rseq, sizeof(*rseq)))
  85. goto efault;
  86. unsafe_put_user(cpu_id, &rseq->cpu_id_start, efault_end);
  87. unsafe_put_user(cpu_id, &rseq->cpu_id, efault_end);
  88. user_write_access_end();
  89. trace_rseq_update(t);
  90. return 0;
  91. efault_end:
  92. user_write_access_end();
  93. efault:
  94. return -EFAULT;
  95. }
  96. static int rseq_reset_rseq_cpu_id(struct task_struct *t)
  97. {
  98. u32 cpu_id_start = 0, cpu_id = RSEQ_CPU_ID_UNINITIALIZED;
  99. /*
  100. * Reset cpu_id_start to its initial state (0).
  101. */
  102. if (put_user(cpu_id_start, &t->rseq->cpu_id_start))
  103. return -EFAULT;
  104. /*
  105. * Reset cpu_id to RSEQ_CPU_ID_UNINITIALIZED, so any user coming
  106. * in after unregistration can figure out that rseq needs to be
  107. * registered again.
  108. */
  109. if (put_user(cpu_id, &t->rseq->cpu_id))
  110. return -EFAULT;
  111. return 0;
  112. }
  113. static int rseq_get_rseq_cs(struct task_struct *t, struct rseq_cs *rseq_cs)
  114. {
  115. struct rseq_cs __user *urseq_cs;
  116. u64 ptr;
  117. u32 __user *usig;
  118. u32 sig;
  119. int ret;
  120. #ifdef CONFIG_64BIT
  121. if (get_user(ptr, &t->rseq->rseq_cs))
  122. return -EFAULT;
  123. #else
  124. if (copy_from_user(&ptr, &t->rseq->rseq_cs, sizeof(ptr)))
  125. return -EFAULT;
  126. #endif
  127. if (!ptr) {
  128. memset(rseq_cs, 0, sizeof(*rseq_cs));
  129. return 0;
  130. }
  131. if (ptr >= TASK_SIZE)
  132. return -EINVAL;
  133. urseq_cs = (struct rseq_cs __user *)(unsigned long)ptr;
  134. if (copy_from_user(rseq_cs, urseq_cs, sizeof(*rseq_cs)))
  135. return -EFAULT;
  136. if (rseq_cs->start_ip >= TASK_SIZE ||
  137. rseq_cs->start_ip + rseq_cs->post_commit_offset >= TASK_SIZE ||
  138. rseq_cs->abort_ip >= TASK_SIZE ||
  139. rseq_cs->version > 0)
  140. return -EINVAL;
  141. /* Check for overflow. */
  142. if (rseq_cs->start_ip + rseq_cs->post_commit_offset < rseq_cs->start_ip)
  143. return -EINVAL;
  144. /* Ensure that abort_ip is not in the critical section. */
  145. if (rseq_cs->abort_ip - rseq_cs->start_ip < rseq_cs->post_commit_offset)
  146. return -EINVAL;
  147. usig = (u32 __user *)(unsigned long)(rseq_cs->abort_ip - sizeof(u32));
  148. ret = get_user(sig, usig);
  149. if (ret)
  150. return ret;
  151. if (current->rseq_sig != sig) {
  152. printk_ratelimited(KERN_WARNING
  153. "Possible attack attempt. Unexpected rseq signature 0x%x, expecting 0x%x (pid=%d, addr=%p).\n",
  154. sig, current->rseq_sig, current->pid, usig);
  155. return -EINVAL;
  156. }
  157. return 0;
  158. }
  159. static bool rseq_warn_flags(const char *str, u32 flags)
  160. {
  161. u32 test_flags;
  162. if (!flags)
  163. return false;
  164. test_flags = flags & RSEQ_CS_NO_RESTART_FLAGS;
  165. if (test_flags)
  166. pr_warn_once("Deprecated flags (%u) in %s ABI structure", test_flags, str);
  167. test_flags = flags & ~RSEQ_CS_NO_RESTART_FLAGS;
  168. if (test_flags)
  169. pr_warn_once("Unknown flags (%u) in %s ABI structure", test_flags, str);
  170. return true;
  171. }
  172. static int rseq_need_restart(struct task_struct *t, u32 cs_flags)
  173. {
  174. u32 flags, event_mask;
  175. int ret;
  176. if (rseq_warn_flags("rseq_cs", cs_flags))
  177. return -EINVAL;
  178. /* Get thread flags. */
  179. ret = get_user(flags, &t->rseq->flags);
  180. if (ret)
  181. return ret;
  182. if (rseq_warn_flags("rseq", flags))
  183. return -EINVAL;
  184. /*
  185. * Load and clear event mask atomically with respect to
  186. * scheduler preemption.
  187. */
  188. preempt_disable();
  189. event_mask = t->rseq_event_mask;
  190. t->rseq_event_mask = 0;
  191. preempt_enable();
  192. return !!event_mask;
  193. }
  194. static int clear_rseq_cs(struct task_struct *t)
  195. {
  196. /*
  197. * The rseq_cs field is set to NULL on preemption or signal
  198. * delivery on top of rseq assembly block, as well as on top
  199. * of code outside of the rseq assembly block. This performs
  200. * a lazy clear of the rseq_cs field.
  201. *
  202. * Set rseq_cs to NULL.
  203. */
  204. #ifdef CONFIG_64BIT
  205. return put_user(0UL, &t->rseq->rseq_cs);
  206. #else
  207. if (clear_user(&t->rseq->rseq_cs, sizeof(t->rseq->rseq_cs)))
  208. return -EFAULT;
  209. return 0;
  210. #endif
  211. }
  212. /*
  213. * Unsigned comparison will be true when ip >= start_ip, and when
  214. * ip < start_ip + post_commit_offset.
  215. */
  216. static bool in_rseq_cs(unsigned long ip, struct rseq_cs *rseq_cs)
  217. {
  218. return ip - rseq_cs->start_ip < rseq_cs->post_commit_offset;
  219. }
  220. static int rseq_ip_fixup(struct pt_regs *regs)
  221. {
  222. unsigned long ip = instruction_pointer(regs);
  223. struct task_struct *t = current;
  224. struct rseq_cs rseq_cs;
  225. int ret;
  226. ret = rseq_get_rseq_cs(t, &rseq_cs);
  227. if (ret)
  228. return ret;
  229. /*
  230. * Handle potentially not being within a critical section.
  231. * If not nested over a rseq critical section, restart is useless.
  232. * Clear the rseq_cs pointer and return.
  233. */
  234. if (!in_rseq_cs(ip, &rseq_cs))
  235. return clear_rseq_cs(t);
  236. ret = rseq_need_restart(t, rseq_cs.flags);
  237. if (ret <= 0)
  238. return ret;
  239. ret = clear_rseq_cs(t);
  240. if (ret)
  241. return ret;
  242. trace_rseq_ip_fixup(ip, rseq_cs.start_ip, rseq_cs.post_commit_offset,
  243. rseq_cs.abort_ip);
  244. instruction_pointer_set(regs, (unsigned long)rseq_cs.abort_ip);
  245. return 0;
  246. }
  247. /*
  248. * This resume handler must always be executed between any of:
  249. * - preemption,
  250. * - signal delivery,
  251. * and return to user-space.
  252. *
  253. * This is how we can ensure that the entire rseq critical section
  254. * will issue the commit instruction only if executed atomically with
  255. * respect to other threads scheduled on the same CPU, and with respect
  256. * to signal handlers.
  257. */
  258. void __rseq_handle_notify_resume(struct ksignal *ksig, struct pt_regs *regs)
  259. {
  260. struct task_struct *t = current;
  261. int ret, sig;
  262. if (unlikely(t->flags & PF_EXITING))
  263. return;
  264. /*
  265. * regs is NULL if and only if the caller is in a syscall path. Skip
  266. * fixup and leave rseq_cs as is so that rseq_sycall() will detect and
  267. * kill a misbehaving userspace on debug kernels.
  268. */
  269. if (regs) {
  270. ret = rseq_ip_fixup(regs);
  271. if (unlikely(ret < 0))
  272. goto error;
  273. }
  274. if (unlikely(rseq_update_cpu_id(t)))
  275. goto error;
  276. return;
  277. error:
  278. sig = ksig ? ksig->sig : 0;
  279. force_sigsegv(sig);
  280. }
  281. #ifdef CONFIG_DEBUG_RSEQ
  282. /*
  283. * Terminate the process if a syscall is issued within a restartable
  284. * sequence.
  285. */
  286. void rseq_syscall(struct pt_regs *regs)
  287. {
  288. unsigned long ip = instruction_pointer(regs);
  289. struct task_struct *t = current;
  290. struct rseq_cs rseq_cs;
  291. if (!t->rseq)
  292. return;
  293. if (rseq_get_rseq_cs(t, &rseq_cs) || in_rseq_cs(ip, &rseq_cs))
  294. force_sig(SIGSEGV);
  295. }
  296. #endif
  297. /*
  298. * sys_rseq - setup restartable sequences for caller thread.
  299. */
  300. SYSCALL_DEFINE4(rseq, struct rseq __user *, rseq, u32, rseq_len,
  301. int, flags, u32, sig)
  302. {
  303. int ret;
  304. if (flags & RSEQ_FLAG_UNREGISTER) {
  305. if (flags & ~RSEQ_FLAG_UNREGISTER)
  306. return -EINVAL;
  307. /* Unregister rseq for current thread. */
  308. if (current->rseq != rseq || !current->rseq)
  309. return -EINVAL;
  310. if (rseq_len != sizeof(*rseq))
  311. return -EINVAL;
  312. if (current->rseq_sig != sig)
  313. return -EPERM;
  314. ret = rseq_reset_rseq_cpu_id(current);
  315. if (ret)
  316. return ret;
  317. current->rseq = NULL;
  318. current->rseq_sig = 0;
  319. return 0;
  320. }
  321. if (unlikely(flags))
  322. return -EINVAL;
  323. if (current->rseq) {
  324. /*
  325. * If rseq is already registered, check whether
  326. * the provided address differs from the prior
  327. * one.
  328. */
  329. if (current->rseq != rseq || rseq_len != sizeof(*rseq))
  330. return -EINVAL;
  331. if (current->rseq_sig != sig)
  332. return -EPERM;
  333. /* Already registered. */
  334. return -EBUSY;
  335. }
  336. /*
  337. * If there was no rseq previously registered,
  338. * ensure the provided rseq is properly aligned and valid.
  339. */
  340. if (!IS_ALIGNED((unsigned long)rseq, __alignof__(*rseq)) ||
  341. rseq_len != sizeof(*rseq))
  342. return -EINVAL;
  343. if (!access_ok(rseq, rseq_len))
  344. return -EFAULT;
  345. current->rseq = rseq;
  346. current->rseq_sig = sig;
  347. /*
  348. * If rseq was previously inactive, and has just been
  349. * registered, ensure the cpu_id_start and cpu_id fields
  350. * are updated before returning to user-space.
  351. */
  352. rseq_set_notify_resume(current);
  353. return 0;
  354. }