rseq.c 9.9 KB

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