rseq.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470
  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. /* The original rseq structure size (including padding) is 32 bytes. */
  19. #define ORIG_RSEQ_SIZE 32
  20. #define RSEQ_CS_NO_RESTART_FLAGS (RSEQ_CS_FLAG_NO_RESTART_ON_PREEMPT | \
  21. RSEQ_CS_FLAG_NO_RESTART_ON_SIGNAL | \
  22. RSEQ_CS_FLAG_NO_RESTART_ON_MIGRATE)
  23. /*
  24. *
  25. * Restartable sequences are a lightweight interface that allows
  26. * user-level code to be executed atomically relative to scheduler
  27. * preemption and signal delivery. Typically used for implementing
  28. * per-cpu operations.
  29. *
  30. * It allows user-space to perform update operations on per-cpu data
  31. * without requiring heavy-weight atomic operations.
  32. *
  33. * Detailed algorithm of rseq user-space assembly sequences:
  34. *
  35. * init(rseq_cs)
  36. * cpu = TLS->rseq::cpu_id_start
  37. * [1] TLS->rseq::rseq_cs = rseq_cs
  38. * [start_ip] ----------------------------
  39. * [2] if (cpu != TLS->rseq::cpu_id)
  40. * goto abort_ip;
  41. * [3] <last_instruction_in_cs>
  42. * [post_commit_ip] ----------------------------
  43. *
  44. * The address of jump target abort_ip must be outside the critical
  45. * region, i.e.:
  46. *
  47. * [abort_ip] < [start_ip] || [abort_ip] >= [post_commit_ip]
  48. *
  49. * Steps [2]-[3] (inclusive) need to be a sequence of instructions in
  50. * userspace that can handle being interrupted between any of those
  51. * instructions, and then resumed to the abort_ip.
  52. *
  53. * 1. Userspace stores the address of the struct rseq_cs assembly
  54. * block descriptor into the rseq_cs field of the registered
  55. * struct rseq TLS area. This update is performed through a single
  56. * store within the inline assembly instruction sequence.
  57. * [start_ip]
  58. *
  59. * 2. Userspace tests to check whether the current cpu_id field match
  60. * the cpu number loaded before start_ip, branching to abort_ip
  61. * in case of a mismatch.
  62. *
  63. * If the sequence is preempted or interrupted by a signal
  64. * at or after start_ip and before post_commit_ip, then the kernel
  65. * clears TLS->__rseq_abi::rseq_cs, and sets the user-space return
  66. * ip to abort_ip before returning to user-space, so the preempted
  67. * execution resumes at abort_ip.
  68. *
  69. * 3. Userspace critical section final instruction before
  70. * post_commit_ip is the commit. The critical section is
  71. * self-terminating.
  72. * [post_commit_ip]
  73. *
  74. * 4. <success>
  75. *
  76. * On failure at [2], or if interrupted by preempt or signal delivery
  77. * between [1] and [3]:
  78. *
  79. * [abort_ip]
  80. * F1. <failure>
  81. */
  82. static int rseq_update_cpu_node_id(struct task_struct *t)
  83. {
  84. struct rseq __user *rseq = t->rseq;
  85. u32 cpu_id = raw_smp_processor_id();
  86. u32 node_id = cpu_to_node(cpu_id);
  87. u32 mm_cid = task_mm_cid(t);
  88. WARN_ON_ONCE((int) mm_cid < 0);
  89. if (!user_write_access_begin(rseq, t->rseq_len))
  90. goto efault;
  91. unsafe_put_user(cpu_id, &rseq->cpu_id_start, efault_end);
  92. unsafe_put_user(cpu_id, &rseq->cpu_id, efault_end);
  93. unsafe_put_user(node_id, &rseq->node_id, efault_end);
  94. unsafe_put_user(mm_cid, &rseq->mm_cid, efault_end);
  95. /*
  96. * Additional feature fields added after ORIG_RSEQ_SIZE
  97. * need to be conditionally updated only if
  98. * t->rseq_len != ORIG_RSEQ_SIZE.
  99. */
  100. user_write_access_end();
  101. trace_rseq_update(t);
  102. return 0;
  103. efault_end:
  104. user_write_access_end();
  105. efault:
  106. return -EFAULT;
  107. }
  108. static int rseq_reset_rseq_cpu_node_id(struct task_struct *t)
  109. {
  110. u32 cpu_id_start = 0, cpu_id = RSEQ_CPU_ID_UNINITIALIZED, node_id = 0,
  111. mm_cid = 0;
  112. /*
  113. * Reset cpu_id_start to its initial state (0).
  114. */
  115. if (put_user(cpu_id_start, &t->rseq->cpu_id_start))
  116. return -EFAULT;
  117. /*
  118. * Reset cpu_id to RSEQ_CPU_ID_UNINITIALIZED, so any user coming
  119. * in after unregistration can figure out that rseq needs to be
  120. * registered again.
  121. */
  122. if (put_user(cpu_id, &t->rseq->cpu_id))
  123. return -EFAULT;
  124. /*
  125. * Reset node_id to its initial state (0).
  126. */
  127. if (put_user(node_id, &t->rseq->node_id))
  128. return -EFAULT;
  129. /*
  130. * Reset mm_cid to its initial state (0).
  131. */
  132. if (put_user(mm_cid, &t->rseq->mm_cid))
  133. return -EFAULT;
  134. /*
  135. * Additional feature fields added after ORIG_RSEQ_SIZE
  136. * need to be conditionally reset only if
  137. * t->rseq_len != ORIG_RSEQ_SIZE.
  138. */
  139. return 0;
  140. }
  141. /*
  142. * Get the user-space pointer value stored in the 'rseq_cs' field.
  143. */
  144. static int rseq_get_rseq_cs_ptr_val(struct rseq __user *rseq, u64 *rseq_cs)
  145. {
  146. if (!rseq_cs)
  147. return -EFAULT;
  148. #ifdef CONFIG_64BIT
  149. if (get_user(*rseq_cs, &rseq->rseq_cs))
  150. return -EFAULT;
  151. #else
  152. if (copy_from_user(rseq_cs, &rseq->rseq_cs, sizeof(*rseq_cs)))
  153. return -EFAULT;
  154. #endif
  155. return 0;
  156. }
  157. /*
  158. * If the rseq_cs field of 'struct rseq' contains a valid pointer to
  159. * user-space, copy 'struct rseq_cs' from user-space and validate its fields.
  160. */
  161. static int rseq_get_rseq_cs(struct task_struct *t, struct rseq_cs *rseq_cs)
  162. {
  163. struct rseq_cs __user *urseq_cs;
  164. u64 ptr;
  165. u32 __user *usig;
  166. u32 sig;
  167. int ret;
  168. ret = rseq_get_rseq_cs_ptr_val(t->rseq, &ptr);
  169. if (ret)
  170. return ret;
  171. /* If the rseq_cs pointer is NULL, return a cleared struct rseq_cs. */
  172. if (!ptr) {
  173. memset(rseq_cs, 0, sizeof(*rseq_cs));
  174. return 0;
  175. }
  176. /* Check that the pointer value fits in the user-space process space. */
  177. if (ptr >= TASK_SIZE)
  178. return -EINVAL;
  179. urseq_cs = (struct rseq_cs __user *)(unsigned long)ptr;
  180. if (copy_from_user(rseq_cs, urseq_cs, sizeof(*rseq_cs)))
  181. return -EFAULT;
  182. if (rseq_cs->start_ip >= TASK_SIZE ||
  183. rseq_cs->start_ip + rseq_cs->post_commit_offset >= TASK_SIZE ||
  184. rseq_cs->abort_ip >= TASK_SIZE ||
  185. rseq_cs->version > 0)
  186. return -EINVAL;
  187. /* Check for overflow. */
  188. if (rseq_cs->start_ip + rseq_cs->post_commit_offset < rseq_cs->start_ip)
  189. return -EINVAL;
  190. /* Ensure that abort_ip is not in the critical section. */
  191. if (rseq_cs->abort_ip - rseq_cs->start_ip < rseq_cs->post_commit_offset)
  192. return -EINVAL;
  193. usig = (u32 __user *)(unsigned long)(rseq_cs->abort_ip - sizeof(u32));
  194. ret = get_user(sig, usig);
  195. if (ret)
  196. return ret;
  197. if (current->rseq_sig != sig) {
  198. printk_ratelimited(KERN_WARNING
  199. "Possible attack attempt. Unexpected rseq signature 0x%x, expecting 0x%x (pid=%d, addr=%p).\n",
  200. sig, current->rseq_sig, current->pid, usig);
  201. return -EINVAL;
  202. }
  203. return 0;
  204. }
  205. static bool rseq_warn_flags(const char *str, u32 flags)
  206. {
  207. u32 test_flags;
  208. if (!flags)
  209. return false;
  210. test_flags = flags & RSEQ_CS_NO_RESTART_FLAGS;
  211. if (test_flags)
  212. pr_warn_once("Deprecated flags (%u) in %s ABI structure", test_flags, str);
  213. test_flags = flags & ~RSEQ_CS_NO_RESTART_FLAGS;
  214. if (test_flags)
  215. pr_warn_once("Unknown flags (%u) in %s ABI structure", test_flags, str);
  216. return true;
  217. }
  218. static int rseq_need_restart(struct task_struct *t, u32 cs_flags)
  219. {
  220. u32 flags, event_mask;
  221. int ret;
  222. if (rseq_warn_flags("rseq_cs", cs_flags))
  223. return -EINVAL;
  224. /* Get thread flags. */
  225. ret = get_user(flags, &t->rseq->flags);
  226. if (ret)
  227. return ret;
  228. if (rseq_warn_flags("rseq", flags))
  229. return -EINVAL;
  230. /*
  231. * Load and clear event mask atomically with respect to
  232. * scheduler preemption and membarrier IPIs.
  233. */
  234. scoped_guard(RSEQ_EVENT_GUARD) {
  235. event_mask = t->rseq_event_mask;
  236. t->rseq_event_mask = 0;
  237. }
  238. return !!event_mask;
  239. }
  240. static int clear_rseq_cs(struct rseq __user *rseq)
  241. {
  242. /*
  243. * The rseq_cs field is set to NULL on preemption or signal
  244. * delivery on top of rseq assembly block, as well as on top
  245. * of code outside of the rseq assembly block. This performs
  246. * a lazy clear of the rseq_cs field.
  247. *
  248. * Set rseq_cs to NULL.
  249. */
  250. #ifdef CONFIG_64BIT
  251. return put_user(0UL, &rseq->rseq_cs);
  252. #else
  253. if (clear_user(&rseq->rseq_cs, sizeof(rseq->rseq_cs)))
  254. return -EFAULT;
  255. return 0;
  256. #endif
  257. }
  258. /*
  259. * Unsigned comparison will be true when ip >= start_ip, and when
  260. * ip < start_ip + post_commit_offset.
  261. */
  262. static bool in_rseq_cs(unsigned long ip, struct rseq_cs *rseq_cs)
  263. {
  264. return ip - rseq_cs->start_ip < rseq_cs->post_commit_offset;
  265. }
  266. static int rseq_ip_fixup(struct pt_regs *regs)
  267. {
  268. unsigned long ip = instruction_pointer(regs);
  269. struct task_struct *t = current;
  270. struct rseq_cs rseq_cs;
  271. int ret;
  272. ret = rseq_get_rseq_cs(t, &rseq_cs);
  273. if (ret)
  274. return ret;
  275. /*
  276. * Handle potentially not being within a critical section.
  277. * If not nested over a rseq critical section, restart is useless.
  278. * Clear the rseq_cs pointer and return.
  279. */
  280. if (!in_rseq_cs(ip, &rseq_cs))
  281. return clear_rseq_cs(t->rseq);
  282. ret = rseq_need_restart(t, rseq_cs.flags);
  283. if (ret <= 0)
  284. return ret;
  285. ret = clear_rseq_cs(t->rseq);
  286. if (ret)
  287. return ret;
  288. trace_rseq_ip_fixup(ip, rseq_cs.start_ip, rseq_cs.post_commit_offset,
  289. rseq_cs.abort_ip);
  290. instruction_pointer_set(regs, (unsigned long)rseq_cs.abort_ip);
  291. return 0;
  292. }
  293. /*
  294. * This resume handler must always be executed between any of:
  295. * - preemption,
  296. * - signal delivery,
  297. * and return to user-space.
  298. *
  299. * This is how we can ensure that the entire rseq critical section
  300. * will issue the commit instruction only if executed atomically with
  301. * respect to other threads scheduled on the same CPU, and with respect
  302. * to signal handlers.
  303. */
  304. void __rseq_handle_notify_resume(struct ksignal *ksig, struct pt_regs *regs)
  305. {
  306. struct task_struct *t = current;
  307. int ret, sig;
  308. if (unlikely(t->flags & PF_EXITING))
  309. return;
  310. /*
  311. * regs is NULL if and only if the caller is in a syscall path. Skip
  312. * fixup and leave rseq_cs as is so that rseq_sycall() will detect and
  313. * kill a misbehaving userspace on debug kernels.
  314. */
  315. if (regs) {
  316. ret = rseq_ip_fixup(regs);
  317. if (unlikely(ret < 0))
  318. goto error;
  319. }
  320. if (unlikely(rseq_update_cpu_node_id(t)))
  321. goto error;
  322. return;
  323. error:
  324. sig = ksig ? ksig->sig : 0;
  325. force_sigsegv(sig);
  326. }
  327. #ifdef CONFIG_DEBUG_RSEQ
  328. /*
  329. * Terminate the process if a syscall is issued within a restartable
  330. * sequence.
  331. */
  332. void rseq_syscall(struct pt_regs *regs)
  333. {
  334. unsigned long ip = instruction_pointer(regs);
  335. struct task_struct *t = current;
  336. struct rseq_cs rseq_cs;
  337. if (!t->rseq)
  338. return;
  339. if (rseq_get_rseq_cs(t, &rseq_cs) || in_rseq_cs(ip, &rseq_cs))
  340. force_sig(SIGSEGV);
  341. }
  342. #endif
  343. /*
  344. * sys_rseq - setup restartable sequences for caller thread.
  345. */
  346. SYSCALL_DEFINE4(rseq, struct rseq __user *, rseq, u32, rseq_len,
  347. int, flags, u32, sig)
  348. {
  349. int ret;
  350. u64 rseq_cs;
  351. if (flags & RSEQ_FLAG_UNREGISTER) {
  352. if (flags & ~RSEQ_FLAG_UNREGISTER)
  353. return -EINVAL;
  354. /* Unregister rseq for current thread. */
  355. if (current->rseq != rseq || !current->rseq)
  356. return -EINVAL;
  357. if (rseq_len != current->rseq_len)
  358. return -EINVAL;
  359. if (current->rseq_sig != sig)
  360. return -EPERM;
  361. ret = rseq_reset_rseq_cpu_node_id(current);
  362. if (ret)
  363. return ret;
  364. current->rseq = NULL;
  365. current->rseq_sig = 0;
  366. current->rseq_len = 0;
  367. return 0;
  368. }
  369. if (unlikely(flags))
  370. return -EINVAL;
  371. if (current->rseq) {
  372. /*
  373. * If rseq is already registered, check whether
  374. * the provided address differs from the prior
  375. * one.
  376. */
  377. if (current->rseq != rseq || rseq_len != current->rseq_len)
  378. return -EINVAL;
  379. if (current->rseq_sig != sig)
  380. return -EPERM;
  381. /* Already registered. */
  382. return -EBUSY;
  383. }
  384. /*
  385. * If there was no rseq previously registered, ensure the provided rseq
  386. * is properly aligned, as communcated to user-space through the ELF
  387. * auxiliary vector AT_RSEQ_ALIGN. If rseq_len is the original rseq
  388. * size, the required alignment is the original struct rseq alignment.
  389. *
  390. * In order to be valid, rseq_len is either the original rseq size, or
  391. * large enough to contain all supported fields, as communicated to
  392. * user-space through the ELF auxiliary vector AT_RSEQ_FEATURE_SIZE.
  393. */
  394. if (rseq_len < ORIG_RSEQ_SIZE ||
  395. (rseq_len == ORIG_RSEQ_SIZE && !IS_ALIGNED((unsigned long)rseq, ORIG_RSEQ_SIZE)) ||
  396. (rseq_len != ORIG_RSEQ_SIZE && (!IS_ALIGNED((unsigned long)rseq, __alignof__(*rseq)) ||
  397. rseq_len < offsetof(struct rseq, end))))
  398. return -EINVAL;
  399. if (!access_ok(rseq, rseq_len))
  400. return -EFAULT;
  401. /*
  402. * If the rseq_cs pointer is non-NULL on registration, clear it to
  403. * avoid a potential segfault on return to user-space. The proper thing
  404. * to do would have been to fail the registration but this would break
  405. * older libcs that reuse the rseq area for new threads without
  406. * clearing the fields.
  407. */
  408. if (rseq_get_rseq_cs_ptr_val(rseq, &rseq_cs))
  409. return -EFAULT;
  410. if (rseq_cs && clear_rseq_cs(rseq))
  411. return -EFAULT;
  412. current->rseq = rseq;
  413. current->rseq_len = rseq_len;
  414. current->rseq_sig = sig;
  415. /*
  416. * If rseq was previously inactive, and has just been
  417. * registered, ensure the cpu_id_start and cpu_id fields
  418. * are updated before returning to user-space.
  419. */
  420. rseq_set_notify_resume(current);
  421. return 0;
  422. }