rseq.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434
  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. static int rseq_get_rseq_cs(struct task_struct *t, struct rseq_cs *rseq_cs)
  142. {
  143. struct rseq_cs __user *urseq_cs;
  144. u64 ptr;
  145. u32 __user *usig;
  146. u32 sig;
  147. int ret;
  148. #ifdef CONFIG_64BIT
  149. if (get_user(ptr, &t->rseq->rseq_cs))
  150. return -EFAULT;
  151. #else
  152. if (copy_from_user(&ptr, &t->rseq->rseq_cs, sizeof(ptr)))
  153. return -EFAULT;
  154. #endif
  155. if (!ptr) {
  156. memset(rseq_cs, 0, sizeof(*rseq_cs));
  157. return 0;
  158. }
  159. if (ptr >= TASK_SIZE)
  160. return -EINVAL;
  161. urseq_cs = (struct rseq_cs __user *)(unsigned long)ptr;
  162. if (copy_from_user(rseq_cs, urseq_cs, sizeof(*rseq_cs)))
  163. return -EFAULT;
  164. if (rseq_cs->start_ip >= TASK_SIZE ||
  165. rseq_cs->start_ip + rseq_cs->post_commit_offset >= TASK_SIZE ||
  166. rseq_cs->abort_ip >= TASK_SIZE ||
  167. rseq_cs->version > 0)
  168. return -EINVAL;
  169. /* Check for overflow. */
  170. if (rseq_cs->start_ip + rseq_cs->post_commit_offset < rseq_cs->start_ip)
  171. return -EINVAL;
  172. /* Ensure that abort_ip is not in the critical section. */
  173. if (rseq_cs->abort_ip - rseq_cs->start_ip < rseq_cs->post_commit_offset)
  174. return -EINVAL;
  175. usig = (u32 __user *)(unsigned long)(rseq_cs->abort_ip - sizeof(u32));
  176. ret = get_user(sig, usig);
  177. if (ret)
  178. return ret;
  179. if (current->rseq_sig != sig) {
  180. printk_ratelimited(KERN_WARNING
  181. "Possible attack attempt. Unexpected rseq signature 0x%x, expecting 0x%x (pid=%d, addr=%p).\n",
  182. sig, current->rseq_sig, current->pid, usig);
  183. return -EINVAL;
  184. }
  185. return 0;
  186. }
  187. static bool rseq_warn_flags(const char *str, u32 flags)
  188. {
  189. u32 test_flags;
  190. if (!flags)
  191. return false;
  192. test_flags = flags & RSEQ_CS_NO_RESTART_FLAGS;
  193. if (test_flags)
  194. pr_warn_once("Deprecated flags (%u) in %s ABI structure", test_flags, str);
  195. test_flags = flags & ~RSEQ_CS_NO_RESTART_FLAGS;
  196. if (test_flags)
  197. pr_warn_once("Unknown flags (%u) in %s ABI structure", test_flags, str);
  198. return true;
  199. }
  200. static int rseq_need_restart(struct task_struct *t, u32 cs_flags)
  201. {
  202. u32 flags, event_mask;
  203. int ret;
  204. if (rseq_warn_flags("rseq_cs", cs_flags))
  205. return -EINVAL;
  206. /* Get thread flags. */
  207. ret = get_user(flags, &t->rseq->flags);
  208. if (ret)
  209. return ret;
  210. if (rseq_warn_flags("rseq", flags))
  211. return -EINVAL;
  212. /*
  213. * Load and clear event mask atomically with respect to
  214. * scheduler preemption.
  215. */
  216. preempt_disable();
  217. event_mask = t->rseq_event_mask;
  218. t->rseq_event_mask = 0;
  219. preempt_enable();
  220. return !!event_mask;
  221. }
  222. static int clear_rseq_cs(struct task_struct *t)
  223. {
  224. /*
  225. * The rseq_cs field is set to NULL on preemption or signal
  226. * delivery on top of rseq assembly block, as well as on top
  227. * of code outside of the rseq assembly block. This performs
  228. * a lazy clear of the rseq_cs field.
  229. *
  230. * Set rseq_cs to NULL.
  231. */
  232. #ifdef CONFIG_64BIT
  233. return put_user(0UL, &t->rseq->rseq_cs);
  234. #else
  235. if (clear_user(&t->rseq->rseq_cs, sizeof(t->rseq->rseq_cs)))
  236. return -EFAULT;
  237. return 0;
  238. #endif
  239. }
  240. /*
  241. * Unsigned comparison will be true when ip >= start_ip, and when
  242. * ip < start_ip + post_commit_offset.
  243. */
  244. static bool in_rseq_cs(unsigned long ip, struct rseq_cs *rseq_cs)
  245. {
  246. return ip - rseq_cs->start_ip < rseq_cs->post_commit_offset;
  247. }
  248. static int rseq_ip_fixup(struct pt_regs *regs)
  249. {
  250. unsigned long ip = instruction_pointer(regs);
  251. struct task_struct *t = current;
  252. struct rseq_cs rseq_cs;
  253. int ret;
  254. ret = rseq_get_rseq_cs(t, &rseq_cs);
  255. if (ret)
  256. return ret;
  257. /*
  258. * Handle potentially not being within a critical section.
  259. * If not nested over a rseq critical section, restart is useless.
  260. * Clear the rseq_cs pointer and return.
  261. */
  262. if (!in_rseq_cs(ip, &rseq_cs))
  263. return clear_rseq_cs(t);
  264. ret = rseq_need_restart(t, rseq_cs.flags);
  265. if (ret <= 0)
  266. return ret;
  267. ret = clear_rseq_cs(t);
  268. if (ret)
  269. return ret;
  270. trace_rseq_ip_fixup(ip, rseq_cs.start_ip, rseq_cs.post_commit_offset,
  271. rseq_cs.abort_ip);
  272. instruction_pointer_set(regs, (unsigned long)rseq_cs.abort_ip);
  273. return 0;
  274. }
  275. /*
  276. * This resume handler must always be executed between any of:
  277. * - preemption,
  278. * - signal delivery,
  279. * and return to user-space.
  280. *
  281. * This is how we can ensure that the entire rseq critical section
  282. * will issue the commit instruction only if executed atomically with
  283. * respect to other threads scheduled on the same CPU, and with respect
  284. * to signal handlers.
  285. */
  286. void __rseq_handle_notify_resume(struct ksignal *ksig, struct pt_regs *regs)
  287. {
  288. struct task_struct *t = current;
  289. int ret, sig;
  290. if (unlikely(t->flags & PF_EXITING))
  291. return;
  292. /*
  293. * regs is NULL if and only if the caller is in a syscall path. Skip
  294. * fixup and leave rseq_cs as is so that rseq_sycall() will detect and
  295. * kill a misbehaving userspace on debug kernels.
  296. */
  297. if (regs) {
  298. ret = rseq_ip_fixup(regs);
  299. if (unlikely(ret < 0))
  300. goto error;
  301. }
  302. if (unlikely(rseq_update_cpu_node_id(t)))
  303. goto error;
  304. return;
  305. error:
  306. sig = ksig ? ksig->sig : 0;
  307. force_sigsegv(sig);
  308. }
  309. #ifdef CONFIG_DEBUG_RSEQ
  310. /*
  311. * Terminate the process if a syscall is issued within a restartable
  312. * sequence.
  313. */
  314. void rseq_syscall(struct pt_regs *regs)
  315. {
  316. unsigned long ip = instruction_pointer(regs);
  317. struct task_struct *t = current;
  318. struct rseq_cs rseq_cs;
  319. if (!t->rseq)
  320. return;
  321. if (rseq_get_rseq_cs(t, &rseq_cs) || in_rseq_cs(ip, &rseq_cs))
  322. force_sig(SIGSEGV);
  323. }
  324. #endif
  325. /*
  326. * sys_rseq - setup restartable sequences for caller thread.
  327. */
  328. SYSCALL_DEFINE4(rseq, struct rseq __user *, rseq, u32, rseq_len,
  329. int, flags, u32, sig)
  330. {
  331. int ret;
  332. if (flags & RSEQ_FLAG_UNREGISTER) {
  333. if (flags & ~RSEQ_FLAG_UNREGISTER)
  334. return -EINVAL;
  335. /* Unregister rseq for current thread. */
  336. if (current->rseq != rseq || !current->rseq)
  337. return -EINVAL;
  338. if (rseq_len != current->rseq_len)
  339. return -EINVAL;
  340. if (current->rseq_sig != sig)
  341. return -EPERM;
  342. ret = rseq_reset_rseq_cpu_node_id(current);
  343. if (ret)
  344. return ret;
  345. current->rseq = NULL;
  346. current->rseq_sig = 0;
  347. current->rseq_len = 0;
  348. return 0;
  349. }
  350. if (unlikely(flags))
  351. return -EINVAL;
  352. if (current->rseq) {
  353. /*
  354. * If rseq is already registered, check whether
  355. * the provided address differs from the prior
  356. * one.
  357. */
  358. if (current->rseq != rseq || rseq_len != current->rseq_len)
  359. return -EINVAL;
  360. if (current->rseq_sig != sig)
  361. return -EPERM;
  362. /* Already registered. */
  363. return -EBUSY;
  364. }
  365. /*
  366. * If there was no rseq previously registered, ensure the provided rseq
  367. * is properly aligned, as communcated to user-space through the ELF
  368. * auxiliary vector AT_RSEQ_ALIGN. If rseq_len is the original rseq
  369. * size, the required alignment is the original struct rseq alignment.
  370. *
  371. * In order to be valid, rseq_len is either the original rseq size, or
  372. * large enough to contain all supported fields, as communicated to
  373. * user-space through the ELF auxiliary vector AT_RSEQ_FEATURE_SIZE.
  374. */
  375. if (rseq_len < ORIG_RSEQ_SIZE ||
  376. (rseq_len == ORIG_RSEQ_SIZE && !IS_ALIGNED((unsigned long)rseq, ORIG_RSEQ_SIZE)) ||
  377. (rseq_len != ORIG_RSEQ_SIZE && (!IS_ALIGNED((unsigned long)rseq, __alignof__(*rseq)) ||
  378. rseq_len < offsetof(struct rseq, end))))
  379. return -EINVAL;
  380. if (!access_ok(rseq, rseq_len))
  381. return -EFAULT;
  382. current->rseq = rseq;
  383. current->rseq_len = rseq_len;
  384. current->rseq_sig = sig;
  385. /*
  386. * If rseq was previously inactive, and has just been
  387. * registered, ensure the cpu_id_start and cpu_id fields
  388. * are updated before returning to user-space.
  389. */
  390. rseq_set_notify_resume(current);
  391. return 0;
  392. }