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_PREEMPT_MIGRATE_FLAGS (RSEQ_CS_FLAG_NO_RESTART_ON_MIGRATE | \
22 RSEQ_CS_FLAG_NO_RESTART_ON_PREEMPT)
26 * Restartable sequences are a lightweight interface that allows
27 * user-level code to be executed atomically relative to scheduler
28 * preemption and signal delivery. Typically used for implementing
31 * It allows user-space to perform update operations on per-cpu data
32 * without requiring heavy-weight atomic operations.
34 * Detailed algorithm of rseq user-space assembly sequences:
37 * cpu = TLS->rseq::cpu_id_start
38 * [1] TLS->rseq::rseq_cs = rseq_cs
39 * [start_ip] ----------------------------
40 * [2] if (cpu != TLS->rseq::cpu_id)
42 * [3] <last_instruction_in_cs>
43 * [post_commit_ip] ----------------------------
45 * The address of jump target abort_ip must be outside the critical
48 * [abort_ip] < [start_ip] || [abort_ip] >= [post_commit_ip]
50 * Steps [2]-[3] (inclusive) need to be a sequence of instructions in
51 * userspace that can handle being interrupted between any of those
52 * instructions, and then resumed to the abort_ip.
54 * 1. Userspace stores the address of the struct rseq_cs assembly
55 * block descriptor into the rseq_cs field of the registered
56 * struct rseq TLS area. This update is performed through a single
57 * store within the inline assembly instruction sequence.
60 * 2. Userspace tests to check whether the current cpu_id field match
61 * the cpu number loaded before start_ip, branching to abort_ip
62 * in case of a mismatch.
64 * If the sequence is preempted or interrupted by a signal
65 * at or after start_ip and before post_commit_ip, then the kernel
66 * clears TLS->__rseq_abi::rseq_cs, and sets the user-space return
67 * ip to abort_ip before returning to user-space, so the preempted
68 * execution resumes at abort_ip.
70 * 3. Userspace critical section final instruction before
71 * post_commit_ip is the commit. The critical section is
77 * On failure at [2], or if interrupted by preempt or signal delivery
78 * between [1] and [3]:
84 static int rseq_update_cpu_id(struct task_struct *t)
86 u32 cpu_id = raw_smp_processor_id();
87 struct rseq __user *rseq = t->rseq;
89 if (!user_write_access_begin(rseq, sizeof(*rseq)))
91 unsafe_put_user(cpu_id, &rseq->cpu_id_start, efault_end);
92 unsafe_put_user(cpu_id, &rseq->cpu_id, efault_end);
93 user_write_access_end();
98 user_write_access_end();
103 static int rseq_reset_rseq_cpu_id(struct task_struct *t)
105 u32 cpu_id_start = 0, cpu_id = RSEQ_CPU_ID_UNINITIALIZED;
108 * Reset cpu_id_start to its initial state (0).
110 if (put_user(cpu_id_start, &t->rseq->cpu_id_start))
113 * Reset cpu_id to RSEQ_CPU_ID_UNINITIALIZED, so any user coming
114 * in after unregistration can figure out that rseq needs to be
117 if (put_user(cpu_id, &t->rseq->cpu_id))
122 static int rseq_get_rseq_cs(struct task_struct *t, struct rseq_cs *rseq_cs)
124 struct rseq_cs __user *urseq_cs;
131 if (get_user(ptr, &t->rseq->rseq_cs.ptr64))
134 if (copy_from_user(&ptr, &t->rseq->rseq_cs.ptr64, sizeof(ptr)))
138 memset(rseq_cs, 0, sizeof(*rseq_cs));
141 if (ptr >= TASK_SIZE)
143 urseq_cs = (struct rseq_cs __user *)(unsigned long)ptr;
144 if (copy_from_user(rseq_cs, urseq_cs, sizeof(*rseq_cs)))
147 if (rseq_cs->start_ip >= TASK_SIZE ||
148 rseq_cs->start_ip + rseq_cs->post_commit_offset >= TASK_SIZE ||
149 rseq_cs->abort_ip >= TASK_SIZE ||
150 rseq_cs->version > 0)
152 /* Check for overflow. */
153 if (rseq_cs->start_ip + rseq_cs->post_commit_offset < rseq_cs->start_ip)
155 /* Ensure that abort_ip is not in the critical section. */
156 if (rseq_cs->abort_ip - rseq_cs->start_ip < rseq_cs->post_commit_offset)
159 usig = (u32 __user *)(unsigned long)(rseq_cs->abort_ip - sizeof(u32));
160 ret = get_user(sig, usig);
164 if (current->rseq_sig != sig) {
165 printk_ratelimited(KERN_WARNING
166 "Possible attack attempt. Unexpected rseq signature 0x%x, expecting 0x%x (pid=%d, addr=%p).\n",
167 sig, current->rseq_sig, current->pid, usig);
173 static int rseq_need_restart(struct task_struct *t, u32 cs_flags)
175 u32 flags, event_mask;
178 /* Get thread flags. */
179 ret = get_user(flags, &t->rseq->flags);
183 /* Take critical section flags into account. */
187 * Restart on signal can only be inhibited when restart on
188 * preempt and restart on migrate are inhibited too. Otherwise,
189 * a preempted signal handler could fail to restart the prior
190 * execution context on sigreturn.
192 if (unlikely((flags & RSEQ_CS_FLAG_NO_RESTART_ON_SIGNAL) &&
193 (flags & RSEQ_CS_PREEMPT_MIGRATE_FLAGS) !=
194 RSEQ_CS_PREEMPT_MIGRATE_FLAGS))
198 * Load and clear event mask atomically with respect to
199 * scheduler preemption.
202 event_mask = t->rseq_event_mask;
203 t->rseq_event_mask = 0;
206 return !!(event_mask & ~flags);
209 static int clear_rseq_cs(struct task_struct *t)
212 * The rseq_cs field is set to NULL on preemption or signal
213 * delivery on top of rseq assembly block, as well as on top
214 * of code outside of the rseq assembly block. This performs
215 * a lazy clear of the rseq_cs field.
217 * Set rseq_cs to NULL.
220 return put_user(0UL, &t->rseq->rseq_cs.ptr64);
222 if (clear_user(&t->rseq->rseq_cs.ptr64, sizeof(t->rseq->rseq_cs.ptr64)))
229 * Unsigned comparison will be true when ip >= start_ip, and when
230 * ip < start_ip + post_commit_offset.
232 static bool in_rseq_cs(unsigned long ip, struct rseq_cs *rseq_cs)
234 return ip - rseq_cs->start_ip < rseq_cs->post_commit_offset;
237 static int rseq_ip_fixup(struct pt_regs *regs)
239 unsigned long ip = instruction_pointer(regs);
240 struct task_struct *t = current;
241 struct rseq_cs rseq_cs;
244 ret = rseq_get_rseq_cs(t, &rseq_cs);
249 * Handle potentially not being within a critical section.
250 * If not nested over a rseq critical section, restart is useless.
251 * Clear the rseq_cs pointer and return.
253 if (!in_rseq_cs(ip, &rseq_cs))
254 return clear_rseq_cs(t);
255 ret = rseq_need_restart(t, rseq_cs.flags);
258 ret = clear_rseq_cs(t);
261 trace_rseq_ip_fixup(ip, rseq_cs.start_ip, rseq_cs.post_commit_offset,
263 instruction_pointer_set(regs, (unsigned long)rseq_cs.abort_ip);
268 * This resume handler must always be executed between any of:
271 * and return to user-space.
273 * This is how we can ensure that the entire rseq critical section
274 * will issue the commit instruction only if executed atomically with
275 * respect to other threads scheduled on the same CPU, and with respect
276 * to signal handlers.
278 void __rseq_handle_notify_resume(struct ksignal *ksig, struct pt_regs *regs)
280 struct task_struct *t = current;
283 if (unlikely(t->flags & PF_EXITING))
285 ret = rseq_ip_fixup(regs);
286 if (unlikely(ret < 0))
288 if (unlikely(rseq_update_cpu_id(t)))
293 sig = ksig ? ksig->sig : 0;
297 #ifdef CONFIG_DEBUG_RSEQ
300 * Terminate the process if a syscall is issued within a restartable
303 void rseq_syscall(struct pt_regs *regs)
305 unsigned long ip = instruction_pointer(regs);
306 struct task_struct *t = current;
307 struct rseq_cs rseq_cs;
311 if (rseq_get_rseq_cs(t, &rseq_cs) || in_rseq_cs(ip, &rseq_cs))
318 * sys_rseq - setup restartable sequences for caller thread.
320 SYSCALL_DEFINE4(rseq, struct rseq __user *, rseq, u32, rseq_len,
321 int, flags, u32, sig)
325 if (flags & RSEQ_FLAG_UNREGISTER) {
326 if (flags & ~RSEQ_FLAG_UNREGISTER)
328 /* Unregister rseq for current thread. */
329 if (current->rseq != rseq || !current->rseq)
331 if (rseq_len != sizeof(*rseq))
333 if (current->rseq_sig != sig)
335 ret = rseq_reset_rseq_cpu_id(current);
338 current->rseq = NULL;
339 current->rseq_sig = 0;
348 * If rseq is already registered, check whether
349 * the provided address differs from the prior
352 if (current->rseq != rseq || rseq_len != sizeof(*rseq))
354 if (current->rseq_sig != sig)
356 /* Already registered. */
361 * If there was no rseq previously registered,
362 * ensure the provided rseq is properly aligned and valid.
364 if (!IS_ALIGNED((unsigned long)rseq, __alignof__(*rseq)) ||
365 rseq_len != sizeof(*rseq))
367 if (!access_ok(rseq, rseq_len))
369 current->rseq = rseq;
370 current->rseq_sig = sig;
372 * If rseq was previously inactive, and has just been
373 * registered, ensure the cpu_id_start and cpu_id fields
374 * are updated before returning to user-space.
376 rseq_set_notify_resume(current);