rseq.c 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358
  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. unsigned long ptr;
  110. u32 __user *usig;
  111. u32 sig;
  112. int ret;
  113. ret = __get_user(ptr, &t->rseq->rseq_cs);
  114. if (ret)
  115. return ret;
  116. if (!ptr) {
  117. memset(rseq_cs, 0, sizeof(*rseq_cs));
  118. return 0;
  119. }
  120. urseq_cs = (struct rseq_cs __user *)ptr;
  121. if (copy_from_user(rseq_cs, urseq_cs, sizeof(*rseq_cs)))
  122. return -EFAULT;
  123. if (rseq_cs->version > 0)
  124. return -EINVAL;
  125. /* Ensure that abort_ip is not in the critical section. */
  126. if (rseq_cs->abort_ip - rseq_cs->start_ip < rseq_cs->post_commit_offset)
  127. return -EINVAL;
  128. usig = (u32 __user *)(rseq_cs->abort_ip - sizeof(u32));
  129. ret = get_user(sig, usig);
  130. if (ret)
  131. return ret;
  132. if (current->rseq_sig != sig) {
  133. printk_ratelimited(KERN_WARNING
  134. "Possible attack attempt. Unexpected rseq signature 0x%x, expecting 0x%x (pid=%d, addr=%p).\n",
  135. sig, current->rseq_sig, current->pid, usig);
  136. return -EPERM;
  137. }
  138. return 0;
  139. }
  140. static int rseq_need_restart(struct task_struct *t, u32 cs_flags)
  141. {
  142. u32 flags, event_mask;
  143. int ret;
  144. /* Get thread flags. */
  145. ret = __get_user(flags, &t->rseq->flags);
  146. if (ret)
  147. return ret;
  148. /* Take critical section flags into account. */
  149. flags |= cs_flags;
  150. /*
  151. * Restart on signal can only be inhibited when restart on
  152. * preempt and restart on migrate are inhibited too. Otherwise,
  153. * a preempted signal handler could fail to restart the prior
  154. * execution context on sigreturn.
  155. */
  156. if (unlikely((flags & RSEQ_CS_FLAG_NO_RESTART_ON_SIGNAL) &&
  157. (flags & RSEQ_CS_PREEMPT_MIGRATE_FLAGS) !=
  158. RSEQ_CS_PREEMPT_MIGRATE_FLAGS))
  159. return -EINVAL;
  160. /*
  161. * Load and clear event mask atomically with respect to
  162. * scheduler preemption.
  163. */
  164. preempt_disable();
  165. event_mask = t->rseq_event_mask;
  166. t->rseq_event_mask = 0;
  167. preempt_enable();
  168. return !!(event_mask & ~flags);
  169. }
  170. static int clear_rseq_cs(struct task_struct *t)
  171. {
  172. /*
  173. * The rseq_cs field is set to NULL on preemption or signal
  174. * delivery on top of rseq assembly block, as well as on top
  175. * of code outside of the rseq assembly block. This performs
  176. * a lazy clear of the rseq_cs field.
  177. *
  178. * Set rseq_cs to NULL with single-copy atomicity.
  179. */
  180. return __put_user(0UL, &t->rseq->rseq_cs);
  181. }
  182. /*
  183. * Unsigned comparison will be true when ip >= start_ip, and when
  184. * ip < start_ip + post_commit_offset.
  185. */
  186. static bool in_rseq_cs(unsigned long ip, struct rseq_cs *rseq_cs)
  187. {
  188. return ip - rseq_cs->start_ip < rseq_cs->post_commit_offset;
  189. }
  190. static int rseq_ip_fixup(struct pt_regs *regs)
  191. {
  192. unsigned long ip = instruction_pointer(regs);
  193. struct task_struct *t = current;
  194. struct rseq_cs rseq_cs;
  195. int ret;
  196. ret = rseq_get_rseq_cs(t, &rseq_cs);
  197. if (ret)
  198. return ret;
  199. /*
  200. * Handle potentially not being within a critical section.
  201. * If not nested over a rseq critical section, restart is useless.
  202. * Clear the rseq_cs pointer and return.
  203. */
  204. if (!in_rseq_cs(ip, &rseq_cs))
  205. return clear_rseq_cs(t);
  206. ret = rseq_need_restart(t, rseq_cs.flags);
  207. if (ret <= 0)
  208. return ret;
  209. ret = clear_rseq_cs(t);
  210. if (ret)
  211. return ret;
  212. trace_rseq_ip_fixup(ip, rseq_cs.start_ip, rseq_cs.post_commit_offset,
  213. rseq_cs.abort_ip);
  214. instruction_pointer_set(regs, (unsigned long)rseq_cs.abort_ip);
  215. return 0;
  216. }
  217. /*
  218. * This resume handler must always be executed between any of:
  219. * - preemption,
  220. * - signal delivery,
  221. * and return to user-space.
  222. *
  223. * This is how we can ensure that the entire rseq critical section,
  224. * consisting of both the C part and the assembly instruction sequence,
  225. * will issue the commit instruction only if executed atomically with
  226. * respect to other threads scheduled on the same CPU, and with respect
  227. * to signal handlers.
  228. */
  229. void __rseq_handle_notify_resume(struct ksignal *ksig, struct pt_regs *regs)
  230. {
  231. struct task_struct *t = current;
  232. int ret, sig;
  233. if (unlikely(t->flags & PF_EXITING))
  234. return;
  235. if (unlikely(!access_ok(VERIFY_WRITE, t->rseq, sizeof(*t->rseq))))
  236. goto error;
  237. ret = rseq_ip_fixup(regs);
  238. if (unlikely(ret < 0))
  239. goto error;
  240. if (unlikely(rseq_update_cpu_id(t)))
  241. goto error;
  242. return;
  243. error:
  244. sig = ksig ? ksig->sig : 0;
  245. force_sigsegv(sig, t);
  246. }
  247. #ifdef CONFIG_DEBUG_RSEQ
  248. /*
  249. * Terminate the process if a syscall is issued within a restartable
  250. * sequence.
  251. */
  252. void rseq_syscall(struct pt_regs *regs)
  253. {
  254. unsigned long ip = instruction_pointer(regs);
  255. struct task_struct *t = current;
  256. struct rseq_cs rseq_cs;
  257. if (!t->rseq)
  258. return;
  259. if (!access_ok(VERIFY_READ, t->rseq, sizeof(*t->rseq)) ||
  260. rseq_get_rseq_cs(t, &rseq_cs) || in_rseq_cs(ip, &rseq_cs))
  261. force_sig(SIGSEGV, t);
  262. }
  263. #endif
  264. /*
  265. * sys_rseq - setup restartable sequences for caller thread.
  266. */
  267. SYSCALL_DEFINE4(rseq, struct rseq __user *, rseq, u32, rseq_len,
  268. int, flags, u32, sig)
  269. {
  270. int ret;
  271. if (flags & RSEQ_FLAG_UNREGISTER) {
  272. /* Unregister rseq for current thread. */
  273. if (current->rseq != rseq || !current->rseq)
  274. return -EINVAL;
  275. if (current->rseq_len != rseq_len)
  276. return -EINVAL;
  277. if (current->rseq_sig != sig)
  278. return -EPERM;
  279. ret = rseq_reset_rseq_cpu_id(current);
  280. if (ret)
  281. return ret;
  282. current->rseq = NULL;
  283. current->rseq_len = 0;
  284. current->rseq_sig = 0;
  285. return 0;
  286. }
  287. if (unlikely(flags))
  288. return -EINVAL;
  289. if (current->rseq) {
  290. /*
  291. * If rseq is already registered, check whether
  292. * the provided address differs from the prior
  293. * one.
  294. */
  295. if (current->rseq != rseq || current->rseq_len != rseq_len)
  296. return -EINVAL;
  297. if (current->rseq_sig != sig)
  298. return -EPERM;
  299. /* Already registered. */
  300. return -EBUSY;
  301. }
  302. /*
  303. * If there was no rseq previously registered,
  304. * ensure the provided rseq is properly aligned and valid.
  305. */
  306. if (!IS_ALIGNED((unsigned long)rseq, __alignof__(*rseq)) ||
  307. rseq_len != sizeof(*rseq))
  308. return -EINVAL;
  309. if (!access_ok(VERIFY_WRITE, rseq, rseq_len))
  310. return -EFAULT;
  311. current->rseq = rseq;
  312. current->rseq_len = rseq_len;
  313. current->rseq_sig = sig;
  314. /*
  315. * If rseq was previously inactive, and has just been
  316. * registered, ensure the cpu_id_start and cpu_id fields
  317. * are updated before returning to user-space.
  318. */
  319. rseq_set_notify_resume(current);
  320. return 0;
  321. }