1 // SPDX-License-Identifier: GPL-2.0+
3 * Restartable sequences system call
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>
11 #include <linux/sched.h>
12 #include <linux/uaccess.h>
13 #include <linux/syscalls.h>
14 #include <linux/rseq.h>
15 #include <linux/types.h>
16 #include <asm/ptrace.h>
18 #define CREATE_TRACE_POINTS
19 #include <trace/events/rseq.h>
21 #define RSEQ_CS_NO_RESTART_FLAGS (RSEQ_CS_FLAG_NO_RESTART_ON_PREEMPT | \
22 RSEQ_CS_FLAG_NO_RESTART_ON_SIGNAL | \
23 RSEQ_CS_FLAG_NO_RESTART_ON_MIGRATE)
27 * Restartable sequences are a lightweight interface that allows
28 * user-level code to be executed atomically relative to scheduler
29 * preemption and signal delivery. Typically used for implementing
32 * It allows user-space to perform update operations on per-cpu data
33 * without requiring heavy-weight atomic operations.
35 * Detailed algorithm of rseq user-space assembly sequences:
38 * cpu = TLS->rseq::cpu_id_start
39 * [1] TLS->rseq::rseq_cs = rseq_cs
40 * [start_ip] ----------------------------
41 * [2] if (cpu != TLS->rseq::cpu_id)
43 * [3] <last_instruction_in_cs>
44 * [post_commit_ip] ----------------------------
46 * The address of jump target abort_ip must be outside the critical
49 * [abort_ip] < [start_ip] || [abort_ip] >= [post_commit_ip]
51 * Steps [2]-[3] (inclusive) need to be a sequence of instructions in
52 * userspace that can handle being interrupted between any of those
53 * instructions, and then resumed to the abort_ip.
55 * 1. Userspace stores the address of the struct rseq_cs assembly
56 * block descriptor into the rseq_cs field of the registered
57 * struct rseq TLS area. This update is performed through a single
58 * store within the inline assembly instruction sequence.
61 * 2. Userspace tests to check whether the current cpu_id field match
62 * the cpu number loaded before start_ip, branching to abort_ip
63 * in case of a mismatch.
65 * If the sequence is preempted or interrupted by a signal
66 * at or after start_ip and before post_commit_ip, then the kernel
67 * clears TLS->__rseq_abi::rseq_cs, and sets the user-space return
68 * ip to abort_ip before returning to user-space, so the preempted
69 * execution resumes at abort_ip.
71 * 3. Userspace critical section final instruction before
72 * post_commit_ip is the commit. The critical section is
78 * On failure at [2], or if interrupted by preempt or signal delivery
79 * between [1] and [3]:
85 static int rseq_update_cpu_id(struct task_struct *t)
87 u32 cpu_id = raw_smp_processor_id();
88 struct rseq __user *rseq = t->rseq;
90 if (!user_write_access_begin(rseq, sizeof(*rseq)))
92 unsafe_put_user(cpu_id, &rseq->cpu_id_start, efault_end);
93 unsafe_put_user(cpu_id, &rseq->cpu_id, efault_end);
94 user_write_access_end();
99 user_write_access_end();
104 static int rseq_reset_rseq_cpu_id(struct task_struct *t)
106 u32 cpu_id_start = 0, cpu_id = RSEQ_CPU_ID_UNINITIALIZED;
109 * Reset cpu_id_start to its initial state (0).
111 if (put_user(cpu_id_start, &t->rseq->cpu_id_start))
114 * Reset cpu_id to RSEQ_CPU_ID_UNINITIALIZED, so any user coming
115 * in after unregistration can figure out that rseq needs to be
118 if (put_user(cpu_id, &t->rseq->cpu_id))
123 static int rseq_get_rseq_cs(struct task_struct *t, struct rseq_cs *rseq_cs)
125 struct rseq_cs __user *urseq_cs;
132 if (get_user(ptr, &t->rseq->rseq_cs))
135 if (copy_from_user(&ptr, &t->rseq->rseq_cs, sizeof(ptr)))
139 memset(rseq_cs, 0, sizeof(*rseq_cs));
142 if (ptr >= TASK_SIZE)
144 urseq_cs = (struct rseq_cs __user *)(unsigned long)ptr;
145 if (copy_from_user(rseq_cs, urseq_cs, sizeof(*rseq_cs)))
148 if (rseq_cs->start_ip >= TASK_SIZE ||
149 rseq_cs->start_ip + rseq_cs->post_commit_offset >= TASK_SIZE ||
150 rseq_cs->abort_ip >= TASK_SIZE ||
151 rseq_cs->version > 0)
153 /* Check for overflow. */
154 if (rseq_cs->start_ip + rseq_cs->post_commit_offset < rseq_cs->start_ip)
156 /* Ensure that abort_ip is not in the critical section. */
157 if (rseq_cs->abort_ip - rseq_cs->start_ip < rseq_cs->post_commit_offset)
160 usig = (u32 __user *)(unsigned long)(rseq_cs->abort_ip - sizeof(u32));
161 ret = get_user(sig, usig);
165 if (current->rseq_sig != sig) {
166 printk_ratelimited(KERN_WARNING
167 "Possible attack attempt. Unexpected rseq signature 0x%x, expecting 0x%x (pid=%d, addr=%p).\n",
168 sig, current->rseq_sig, current->pid, usig);
174 static bool rseq_warn_flags(const char *str, u32 flags)
180 test_flags = flags & RSEQ_CS_NO_RESTART_FLAGS;
182 pr_warn_once("Deprecated flags (%u) in %s ABI structure", test_flags, str);
183 test_flags = flags & ~RSEQ_CS_NO_RESTART_FLAGS;
185 pr_warn_once("Unknown flags (%u) in %s ABI structure", test_flags, str);
189 static int rseq_need_restart(struct task_struct *t, u32 cs_flags)
191 u32 flags, event_mask;
194 if (rseq_warn_flags("rseq_cs", cs_flags))
197 /* Get thread flags. */
198 ret = get_user(flags, &t->rseq->flags);
202 if (rseq_warn_flags("rseq", flags))
206 * Load and clear event mask atomically with respect to
207 * scheduler preemption.
210 event_mask = t->rseq_event_mask;
211 t->rseq_event_mask = 0;
217 static int clear_rseq_cs(struct task_struct *t)
220 * The rseq_cs field is set to NULL on preemption or signal
221 * delivery on top of rseq assembly block, as well as on top
222 * of code outside of the rseq assembly block. This performs
223 * a lazy clear of the rseq_cs field.
225 * Set rseq_cs to NULL.
228 return put_user(0UL, &t->rseq->rseq_cs);
230 if (clear_user(&t->rseq->rseq_cs, sizeof(t->rseq->rseq_cs)))
237 * Unsigned comparison will be true when ip >= start_ip, and when
238 * ip < start_ip + post_commit_offset.
240 static bool in_rseq_cs(unsigned long ip, struct rseq_cs *rseq_cs)
242 return ip - rseq_cs->start_ip < rseq_cs->post_commit_offset;
245 static int rseq_ip_fixup(struct pt_regs *regs)
247 unsigned long ip = instruction_pointer(regs);
248 struct task_struct *t = current;
249 struct rseq_cs rseq_cs;
252 ret = rseq_get_rseq_cs(t, &rseq_cs);
257 * Handle potentially not being within a critical section.
258 * If not nested over a rseq critical section, restart is useless.
259 * Clear the rseq_cs pointer and return.
261 if (!in_rseq_cs(ip, &rseq_cs))
262 return clear_rseq_cs(t);
263 ret = rseq_need_restart(t, rseq_cs.flags);
266 ret = clear_rseq_cs(t);
269 trace_rseq_ip_fixup(ip, rseq_cs.start_ip, rseq_cs.post_commit_offset,
271 instruction_pointer_set(regs, (unsigned long)rseq_cs.abort_ip);
276 * This resume handler must always be executed between any of:
279 * and return to user-space.
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.
286 void __rseq_handle_notify_resume(struct ksignal *ksig, struct pt_regs *regs)
288 struct task_struct *t = current;
291 if (unlikely(t->flags & PF_EXITING))
295 * regs is NULL if and only if the caller is in a syscall path. Skip
296 * fixup and leave rseq_cs as is so that rseq_sycall() will detect and
297 * kill a misbehaving userspace on debug kernels.
300 ret = rseq_ip_fixup(regs);
301 if (unlikely(ret < 0))
304 if (unlikely(rseq_update_cpu_id(t)))
309 sig = ksig ? ksig->sig : 0;
313 #ifdef CONFIG_DEBUG_RSEQ
316 * Terminate the process if a syscall is issued within a restartable
319 void rseq_syscall(struct pt_regs *regs)
321 unsigned long ip = instruction_pointer(regs);
322 struct task_struct *t = current;
323 struct rseq_cs rseq_cs;
327 if (rseq_get_rseq_cs(t, &rseq_cs) || in_rseq_cs(ip, &rseq_cs))
334 * sys_rseq - setup restartable sequences for caller thread.
336 SYSCALL_DEFINE4(rseq, struct rseq __user *, rseq, u32, rseq_len,
337 int, flags, u32, sig)
341 if (flags & RSEQ_FLAG_UNREGISTER) {
342 if (flags & ~RSEQ_FLAG_UNREGISTER)
344 /* Unregister rseq for current thread. */
345 if (current->rseq != rseq || !current->rseq)
347 if (rseq_len != sizeof(*rseq))
349 if (current->rseq_sig != sig)
351 ret = rseq_reset_rseq_cpu_id(current);
354 current->rseq = NULL;
355 current->rseq_sig = 0;
364 * If rseq is already registered, check whether
365 * the provided address differs from the prior
368 if (current->rseq != rseq || rseq_len != sizeof(*rseq))
370 if (current->rseq_sig != sig)
372 /* Already registered. */
377 * If there was no rseq previously registered,
378 * ensure the provided rseq is properly aligned and valid.
380 if (!IS_ALIGNED((unsigned long)rseq, __alignof__(*rseq)) ||
381 rseq_len != sizeof(*rseq))
383 if (!access_ok(rseq, rseq_len))
385 current->rseq = rseq;
386 current->rseq_sig = sig;
388 * If rseq was previously inactive, and has just been
389 * registered, ensure the cpu_id_start and cpu_id fields
390 * are updated before returning to user-space.
392 rseq_set_notify_resume(current);