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