rseq.c 10.0 KB

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