1
2
3
4
5
6
7
8
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#define RSEQ_CS_PREEMPT_MIGRATE_FLAGS (RSEQ_CS_FLAG_NO_RESTART_ON_MIGRATE | \
22 RSEQ_CS_FLAG_NO_RESTART_ON_PREEMPT)
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84static int rseq_update_cpu_id(struct task_struct *t)
85{
86 u32 cpu_id = raw_smp_processor_id();
87 struct rseq __user *rseq = t->rseq;
88
89 if (!user_write_access_begin(rseq, sizeof(*rseq)))
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 user_write_access_end();
94 trace_rseq_update(t);
95 return 0;
96
97efault_end:
98 user_write_access_end();
99efault:
100 return -EFAULT;
101}
102
103static int rseq_reset_rseq_cpu_id(struct task_struct *t)
104{
105 u32 cpu_id_start = 0, cpu_id = RSEQ_CPU_ID_UNINITIALIZED;
106
107
108
109
110 if (put_user(cpu_id_start, &t->rseq->cpu_id_start))
111 return -EFAULT;
112
113
114
115
116
117 if (put_user(cpu_id, &t->rseq->cpu_id))
118 return -EFAULT;
119 return 0;
120}
121
122static int rseq_get_rseq_cs(struct task_struct *t, struct rseq_cs *rseq_cs)
123{
124 struct rseq_cs __user *urseq_cs;
125 u64 ptr;
126 u32 __user *usig;
127 u32 sig;
128 int ret;
129
130#ifdef CONFIG_64BIT
131 if (get_user(ptr, &t->rseq->rseq_cs.ptr64))
132 return -EFAULT;
133#else
134 if (copy_from_user(&ptr, &t->rseq->rseq_cs.ptr64, sizeof(ptr)))
135 return -EFAULT;
136#endif
137 if (!ptr) {
138 memset(rseq_cs, 0, sizeof(*rseq_cs));
139 return 0;
140 }
141 if (ptr >= TASK_SIZE)
142 return -EINVAL;
143 urseq_cs = (struct rseq_cs __user *)(unsigned long)ptr;
144 if (copy_from_user(rseq_cs, urseq_cs, sizeof(*rseq_cs)))
145 return -EFAULT;
146
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)
151 return -EINVAL;
152
153 if (rseq_cs->start_ip + rseq_cs->post_commit_offset < rseq_cs->start_ip)
154 return -EINVAL;
155
156 if (rseq_cs->abort_ip - rseq_cs->start_ip < rseq_cs->post_commit_offset)
157 return -EINVAL;
158
159 usig = (u32 __user *)(unsigned long)(rseq_cs->abort_ip - sizeof(u32));
160 ret = get_user(sig, usig);
161 if (ret)
162 return ret;
163
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);
168 return -EINVAL;
169 }
170 return 0;
171}
172
173static int rseq_need_restart(struct task_struct *t, u32 cs_flags)
174{
175 u32 flags, event_mask;
176 int ret;
177
178
179 ret = get_user(flags, &t->rseq->flags);
180 if (ret)
181 return ret;
182
183
184 flags |= cs_flags;
185
186
187
188
189
190
191
192 if (unlikely((flags & RSEQ_CS_FLAG_NO_RESTART_ON_SIGNAL) &&
193 (flags & RSEQ_CS_PREEMPT_MIGRATE_FLAGS) !=
194 RSEQ_CS_PREEMPT_MIGRATE_FLAGS))
195 return -EINVAL;
196
197
198
199
200
201 preempt_disable();
202 event_mask = t->rseq_event_mask;
203 t->rseq_event_mask = 0;
204 preempt_enable();
205
206 return !!(event_mask & ~flags);
207}
208
209static int clear_rseq_cs(struct task_struct *t)
210{
211
212
213
214
215
216
217
218
219#ifdef CONFIG_64BIT
220 return put_user(0UL, &t->rseq->rseq_cs.ptr64);
221#else
222 if (clear_user(&t->rseq->rseq_cs.ptr64, sizeof(t->rseq->rseq_cs.ptr64)))
223 return -EFAULT;
224 return 0;
225#endif
226}
227
228
229
230
231
232static bool in_rseq_cs(unsigned long ip, struct rseq_cs *rseq_cs)
233{
234 return ip - rseq_cs->start_ip < rseq_cs->post_commit_offset;
235}
236
237static int rseq_ip_fixup(struct pt_regs *regs)
238{
239 unsigned long ip = instruction_pointer(regs);
240 struct task_struct *t = current;
241 struct rseq_cs rseq_cs;
242 int ret;
243
244 ret = rseq_get_rseq_cs(t, &rseq_cs);
245 if (ret)
246 return ret;
247
248
249
250
251
252
253 if (!in_rseq_cs(ip, &rseq_cs))
254 return clear_rseq_cs(t);
255 ret = rseq_need_restart(t, rseq_cs.flags);
256 if (ret <= 0)
257 return ret;
258 ret = clear_rseq_cs(t);
259 if (ret)
260 return ret;
261 trace_rseq_ip_fixup(ip, rseq_cs.start_ip, rseq_cs.post_commit_offset,
262 rseq_cs.abort_ip);
263 instruction_pointer_set(regs, (unsigned long)rseq_cs.abort_ip);
264 return 0;
265}
266
267
268
269
270
271
272
273
274
275
276
277
278void __rseq_handle_notify_resume(struct ksignal *ksig, struct pt_regs *regs)
279{
280 struct task_struct *t = current;
281 int ret, sig;
282
283 if (unlikely(t->flags & PF_EXITING))
284 return;
285
286
287
288
289
290
291 if (regs) {
292 ret = rseq_ip_fixup(regs);
293 if (unlikely(ret < 0))
294 goto error;
295 }
296 if (unlikely(rseq_update_cpu_id(t)))
297 goto error;
298 return;
299
300error:
301 sig = ksig ? ksig->sig : 0;
302 force_sigsegv(sig);
303}
304
305#ifdef CONFIG_DEBUG_RSEQ
306
307
308
309
310
311void rseq_syscall(struct pt_regs *regs)
312{
313 unsigned long ip = instruction_pointer(regs);
314 struct task_struct *t = current;
315 struct rseq_cs rseq_cs;
316
317 if (!t->rseq)
318 return;
319 if (rseq_get_rseq_cs(t, &rseq_cs) || in_rseq_cs(ip, &rseq_cs))
320 force_sig(SIGSEGV);
321}
322
323#endif
324
325
326
327
328SYSCALL_DEFINE4(rseq, struct rseq __user *, rseq, u32, rseq_len,
329 int, flags, u32, sig)
330{
331 int ret;
332
333 if (flags & RSEQ_FLAG_UNREGISTER) {
334 if (flags & ~RSEQ_FLAG_UNREGISTER)
335 return -EINVAL;
336
337 if (current->rseq != rseq || !current->rseq)
338 return -EINVAL;
339 if (rseq_len != sizeof(*rseq))
340 return -EINVAL;
341 if (current->rseq_sig != sig)
342 return -EPERM;
343 ret = rseq_reset_rseq_cpu_id(current);
344 if (ret)
345 return ret;
346 current->rseq = NULL;
347 current->rseq_sig = 0;
348 return 0;
349 }
350
351 if (unlikely(flags))
352 return -EINVAL;
353
354 if (current->rseq) {
355
356
357
358
359
360 if (current->rseq != rseq || rseq_len != sizeof(*rseq))
361 return -EINVAL;
362 if (current->rseq_sig != sig)
363 return -EPERM;
364
365 return -EBUSY;
366 }
367
368
369
370
371
372 if (!IS_ALIGNED((unsigned long)rseq, __alignof__(*rseq)) ||
373 rseq_len != sizeof(*rseq))
374 return -EINVAL;
375 if (!access_ok(rseq, rseq_len))
376 return -EFAULT;
377 current->rseq = rseq;
378 current->rseq_sig = sig;
379
380
381
382
383
384 rseq_set_notify_resume(current);
385
386 return 0;
387}
388