rseq.c 10 KB

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