1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3 * linux/kernel/ptrace.c
4 *
5 * (C) Copyright 1999 Linus Torvalds
6 *
7 * Common interfaces for "ptrace()" which we do not want
8 * to continually duplicate across every architecture.
9 */
10
11 #include <linux/capability.h>
12 #include <linux/export.h>
13 #include <linux/sched.h>
14 #include <linux/sched/mm.h>
15 #include <linux/sched/coredump.h>
16 #include <linux/sched/task.h>
17 #include <linux/errno.h>
18 #include <linux/mm.h>
19 #include <linux/highmem.h>
20 #include <linux/pagemap.h>
21 #include <linux/ptrace.h>
22 #include <linux/security.h>
23 #include <linux/signal.h>
24 #include <linux/uio.h>
25 #include <linux/audit.h>
26 #include <linux/pid_namespace.h>
27 #include <linux/syscalls.h>
28 #include <linux/uaccess.h>
29 #include <linux/regset.h>
30 #include <linux/hw_breakpoint.h>
31 #include <linux/cn_proc.h>
32 #include <linux/compat.h>
33 #include <linux/sched/signal.h>
34 #include <linux/minmax.h>
35 #include <linux/syscall_user_dispatch.h>
36
37 #include <asm/syscall.h> /* for syscall_get_* */
38
39 /*
40 * Access another process' address space via ptrace.
41 * Source/target buffer must be kernel space,
42 * Do not walk the page table directly, use get_user_pages
43 */
ptrace_access_vm(struct task_struct * tsk,unsigned long addr,void * buf,int len,unsigned int gup_flags)44 int ptrace_access_vm(struct task_struct *tsk, unsigned long addr,
45 void *buf, int len, unsigned int gup_flags)
46 {
47 struct mm_struct *mm;
48 int ret;
49
50 mm = get_task_mm(tsk);
51 if (!mm)
52 return 0;
53
54 if (!tsk->ptrace ||
55 (current != tsk->parent) ||
56 ((get_dumpable(mm) != SUID_DUMP_USER) &&
57 !ptracer_capable(tsk, mm->user_ns))) {
58 mmput(mm);
59 return 0;
60 }
61
62 ret = access_remote_vm(mm, addr, buf, len, gup_flags);
63 mmput(mm);
64
65 return ret;
66 }
67
68
__ptrace_link(struct task_struct * child,struct task_struct * new_parent,const struct cred * ptracer_cred)69 void __ptrace_link(struct task_struct *child, struct task_struct *new_parent,
70 const struct cred *ptracer_cred)
71 {
72 BUG_ON(!list_empty(&child->ptrace_entry));
73 list_add(&child->ptrace_entry, &new_parent->ptraced);
74 child->parent = new_parent;
75 child->ptracer_cred = get_cred(ptracer_cred);
76 }
77
78 /*
79 * ptrace a task: make the debugger its new parent and
80 * move it to the ptrace list.
81 *
82 * Must be called with the tasklist lock write-held.
83 */
ptrace_link(struct task_struct * child,struct task_struct * new_parent)84 static void ptrace_link(struct task_struct *child, struct task_struct *new_parent)
85 {
86 __ptrace_link(child, new_parent, current_cred());
87 }
88
89 /**
90 * __ptrace_unlink - unlink ptracee and restore its execution state
91 * @child: ptracee to be unlinked
92 *
93 * Remove @child from the ptrace list, move it back to the original parent,
94 * and restore the execution state so that it conforms to the group stop
95 * state.
96 *
97 * Unlinking can happen via two paths - explicit PTRACE_DETACH or ptracer
98 * exiting. For PTRACE_DETACH, unless the ptracee has been killed between
99 * ptrace_check_attach() and here, it's guaranteed to be in TASK_TRACED.
100 * If the ptracer is exiting, the ptracee can be in any state.
101 *
102 * After detach, the ptracee should be in a state which conforms to the
103 * group stop. If the group is stopped or in the process of stopping, the
104 * ptracee should be put into TASK_STOPPED; otherwise, it should be woken
105 * up from TASK_TRACED.
106 *
107 * If the ptracee is in TASK_TRACED and needs to be moved to TASK_STOPPED,
108 * it goes through TRACED -> RUNNING -> STOPPED transition which is similar
109 * to but in the opposite direction of what happens while attaching to a
110 * stopped task. However, in this direction, the intermediate RUNNING
111 * state is not hidden even from the current ptracer and if it immediately
112 * re-attaches and performs a WNOHANG wait(2), it may fail.
113 *
114 * CONTEXT:
115 * write_lock_irq(tasklist_lock)
116 */
__ptrace_unlink(struct task_struct * child)117 void __ptrace_unlink(struct task_struct *child)
118 {
119 const struct cred *old_cred;
120 BUG_ON(!child->ptrace);
121
122 clear_task_syscall_work(child, SYSCALL_TRACE);
123 #if defined(CONFIG_GENERIC_ENTRY) || defined(TIF_SYSCALL_EMU)
124 clear_task_syscall_work(child, SYSCALL_EMU);
125 #endif
126
127 child->parent = child->real_parent;
128 list_del_init(&child->ptrace_entry);
129 old_cred = child->ptracer_cred;
130 child->ptracer_cred = NULL;
131 put_cred(old_cred);
132
133 spin_lock(&child->sighand->siglock);
134 child->ptrace = 0;
135 /*
136 * Clear all pending traps and TRAPPING. TRAPPING should be
137 * cleared regardless of JOBCTL_STOP_PENDING. Do it explicitly.
138 */
139 task_clear_jobctl_pending(child, JOBCTL_TRAP_MASK);
140 task_clear_jobctl_trapping(child);
141
142 /*
143 * Reinstate JOBCTL_STOP_PENDING if group stop is in effect and
144 * @child isn't dead.
145 */
146 if (!(child->flags & PF_EXITING) &&
147 (child->signal->flags & SIGNAL_STOP_STOPPED ||
148 child->signal->group_stop_count))
149 child->jobctl |= JOBCTL_STOP_PENDING;
150
151 /*
152 * If transition to TASK_STOPPED is pending or in TASK_TRACED, kick
153 * @child in the butt. Note that @resume should be used iff @child
154 * is in TASK_TRACED; otherwise, we might unduly disrupt
155 * TASK_KILLABLE sleeps.
156 */
157 if (child->jobctl & JOBCTL_STOP_PENDING || task_is_traced(child))
158 ptrace_signal_wake_up(child, true);
159
160 spin_unlock(&child->sighand->siglock);
161 }
162
looks_like_a_spurious_pid(struct task_struct * task)163 static bool looks_like_a_spurious_pid(struct task_struct *task)
164 {
165 if (task->exit_code != ((PTRACE_EVENT_EXEC << 8) | SIGTRAP))
166 return false;
167
168 if (task_pid_vnr(task) == task->ptrace_message)
169 return false;
170 /*
171 * The tracee changed its pid but the PTRACE_EVENT_EXEC event
172 * was not wait()'ed, most probably debugger targets the old
173 * leader which was destroyed in de_thread().
174 */
175 return true;
176 }
177
178 /*
179 * Ensure that nothing can wake it up, even SIGKILL
180 *
181 * A task is switched to this state while a ptrace operation is in progress;
182 * such that the ptrace operation is uninterruptible.
183 */
ptrace_freeze_traced(struct task_struct * task)184 static bool ptrace_freeze_traced(struct task_struct *task)
185 {
186 bool ret = false;
187
188 /* Lockless, nobody but us can set this flag */
189 if (task->jobctl & JOBCTL_LISTENING)
190 return ret;
191
192 spin_lock_irq(&task->sighand->siglock);
193 if (task_is_traced(task) && !looks_like_a_spurious_pid(task) &&
194 !__fatal_signal_pending(task)) {
195 task->jobctl |= JOBCTL_PTRACE_FROZEN;
196 ret = true;
197 }
198 spin_unlock_irq(&task->sighand->siglock);
199
200 return ret;
201 }
202
ptrace_unfreeze_traced(struct task_struct * task)203 static void ptrace_unfreeze_traced(struct task_struct *task)
204 {
205 unsigned long flags;
206
207 /*
208 * The child may be awake and may have cleared
209 * JOBCTL_PTRACE_FROZEN (see ptrace_resume). The child will
210 * not set JOBCTL_PTRACE_FROZEN or enter __TASK_TRACED anew.
211 */
212 if (lock_task_sighand(task, &flags)) {
213 task->jobctl &= ~JOBCTL_PTRACE_FROZEN;
214 if (__fatal_signal_pending(task)) {
215 task->jobctl &= ~JOBCTL_TRACED;
216 wake_up_state(task, __TASK_TRACED);
217 }
218 unlock_task_sighand(task, &flags);
219 }
220 }
221
222 /**
223 * ptrace_check_attach - check whether ptracee is ready for ptrace operation
224 * @child: ptracee to check for
225 * @ignore_state: don't check whether @child is currently %TASK_TRACED
226 *
227 * Check whether @child is being ptraced by %current and ready for further
228 * ptrace operations. If @ignore_state is %false, @child also should be in
229 * %TASK_TRACED state and on return the child is guaranteed to be traced
230 * and not executing. If @ignore_state is %true, @child can be in any
231 * state.
232 *
233 * CONTEXT:
234 * Grabs and releases tasklist_lock and @child->sighand->siglock.
235 *
236 * RETURNS:
237 * 0 on success, -ESRCH if %child is not ready.
238 */
ptrace_check_attach(struct task_struct * child,bool ignore_state)239 static int ptrace_check_attach(struct task_struct *child, bool ignore_state)
240 {
241 int ret = -ESRCH;
242
243 /*
244 * We take the read lock around doing both checks to close a
245 * possible race where someone else was tracing our child and
246 * detached between these two checks. After this locked check,
247 * we are sure that this is our traced child and that can only
248 * be changed by us so it's not changing right after this.
249 */
250 read_lock(&tasklist_lock);
251 if (child->ptrace && child->parent == current) {
252 /*
253 * child->sighand can't be NULL, release_task()
254 * does ptrace_unlink() before __exit_signal().
255 */
256 if (ignore_state || ptrace_freeze_traced(child))
257 ret = 0;
258 }
259 read_unlock(&tasklist_lock);
260
261 if (!ret && !ignore_state &&
262 WARN_ON_ONCE(!wait_task_inactive(child, __TASK_TRACED|TASK_FROZEN)))
263 ret = -ESRCH;
264
265 return ret;
266 }
267
ptrace_has_cap(struct user_namespace * ns,unsigned int mode)268 static bool ptrace_has_cap(struct user_namespace *ns, unsigned int mode)
269 {
270 if (mode & PTRACE_MODE_NOAUDIT)
271 return ns_capable_noaudit(ns, CAP_SYS_PTRACE);
272 return ns_capable(ns, CAP_SYS_PTRACE);
273 }
274
task_still_dumpable(struct task_struct * task,unsigned int mode)275 static bool task_still_dumpable(struct task_struct *task, unsigned int mode)
276 {
277 struct mm_struct *mm = task->mm;
278 if (mm) {
279 if (get_dumpable(mm) == SUID_DUMP_USER)
280 return true;
281 return ptrace_has_cap(mm->user_ns, mode);
282 }
283
284 if (task->user_dumpable)
285 return true;
286 return ptrace_has_cap(&init_user_ns, mode);
287 }
288
289 /* Returns 0 on success, -errno on denial. */
__ptrace_may_access(struct task_struct * task,unsigned int mode)290 static int __ptrace_may_access(struct task_struct *task, unsigned int mode)
291 {
292 const struct cred *cred = current_cred(), *tcred;
293 kuid_t caller_uid;
294 kgid_t caller_gid;
295
296 if (!(mode & PTRACE_MODE_FSCREDS) == !(mode & PTRACE_MODE_REALCREDS)) {
297 WARN(1, "denying ptrace access check without PTRACE_MODE_*CREDS\n");
298 return -EPERM;
299 }
300
301 /* May we inspect the given task?
302 * This check is used both for attaching with ptrace
303 * and for allowing access to sensitive information in /proc.
304 *
305 * ptrace_attach denies several cases that /proc allows
306 * because setting up the necessary parent/child relationship
307 * or halting the specified task is impossible.
308 */
309
310 /* Don't let security modules deny introspection */
311 if (same_thread_group(task, current))
312 return 0;
313 rcu_read_lock();
314 if (mode & PTRACE_MODE_FSCREDS) {
315 caller_uid = cred->fsuid;
316 caller_gid = cred->fsgid;
317 } else {
318 /*
319 * Using the euid would make more sense here, but something
320 * in userland might rely on the old behavior, and this
321 * shouldn't be a security problem since
322 * PTRACE_MODE_REALCREDS implies that the caller explicitly
323 * used a syscall that requests access to another process
324 * (and not a filesystem syscall to procfs).
325 */
326 caller_uid = cred->uid;
327 caller_gid = cred->gid;
328 }
329 tcred = __task_cred(task);
330 if (uid_eq(caller_uid, tcred->euid) &&
331 uid_eq(caller_uid, tcred->suid) &&
332 uid_eq(caller_uid, tcred->uid) &&
333 gid_eq(caller_gid, tcred->egid) &&
334 gid_eq(caller_gid, tcred->sgid) &&
335 gid_eq(caller_gid, tcred->gid))
336 goto ok;
337 if (ptrace_has_cap(tcred->user_ns, mode))
338 goto ok;
339 rcu_read_unlock();
340 return -EPERM;
341 ok:
342 rcu_read_unlock();
343 /*
344 * If a task drops privileges and becomes nondumpable (through a syscall
345 * like setresuid()) while we are trying to access it, we must ensure
346 * that the dumpability is read after the credentials; otherwise,
347 * we may be able to attach to a task that we shouldn't be able to
348 * attach to (as if the task had dropped privileges without becoming
349 * nondumpable).
350 * Pairs with a write barrier in commit_creds().
351 */
352 smp_rmb();
353 if (!task_still_dumpable(task, mode))
354 return -EPERM;
355
356 return security_ptrace_access_check(task, mode);
357 }
358
ptrace_may_access(struct task_struct * task,unsigned int mode)359 bool ptrace_may_access(struct task_struct *task, unsigned int mode)
360 {
361 int err;
362 task_lock(task);
363 err = __ptrace_may_access(task, mode);
364 task_unlock(task);
365 return !err;
366 }
367
check_ptrace_options(unsigned long data)368 static int check_ptrace_options(unsigned long data)
369 {
370 if (data & ~(unsigned long)PTRACE_O_MASK)
371 return -EINVAL;
372
373 if (unlikely(data & PTRACE_O_SUSPEND_SECCOMP)) {
374 if (!IS_ENABLED(CONFIG_CHECKPOINT_RESTORE) ||
375 !IS_ENABLED(CONFIG_SECCOMP))
376 return -EINVAL;
377
378 if (!capable(CAP_SYS_ADMIN))
379 return -EPERM;
380
381 if (seccomp_mode(¤t->seccomp) != SECCOMP_MODE_DISABLED ||
382 current->ptrace & PT_SUSPEND_SECCOMP)
383 return -EPERM;
384 }
385 return 0;
386 }
387
ptrace_set_stopped(struct task_struct * task,bool seize)388 static inline void ptrace_set_stopped(struct task_struct *task, bool seize)
389 {
390 guard(spinlock)(&task->sighand->siglock);
391
392 /* SEIZE doesn't trap tracee on attach */
393 if (!seize)
394 send_signal_locked(SIGSTOP, SEND_SIG_PRIV, task, PIDTYPE_PID);
395 /*
396 * If the task is already STOPPED, set JOBCTL_TRAP_STOP and
397 * TRAPPING, and kick it so that it transits to TRACED. TRAPPING
398 * will be cleared if the child completes the transition or any
399 * event which clears the group stop states happens. We'll wait
400 * for the transition to complete before returning from this
401 * function.
402 *
403 * This hides STOPPED -> RUNNING -> TRACED transition from the
404 * attaching thread but a different thread in the same group can
405 * still observe the transient RUNNING state. IOW, if another
406 * thread's WNOHANG wait(2) on the stopped tracee races against
407 * ATTACH, the wait(2) may fail due to the transient RUNNING.
408 *
409 * The following task_is_stopped() test is safe as both transitions
410 * in and out of STOPPED are protected by siglock.
411 */
412 if (task_is_stopped(task) &&
413 task_set_jobctl_pending(task, JOBCTL_TRAP_STOP | JOBCTL_TRAPPING)) {
414 task->jobctl &= ~JOBCTL_STOPPED;
415 signal_wake_up_state(task, __TASK_STOPPED);
416 }
417 }
418
ptrace_attach(struct task_struct * task,long request,unsigned long addr,unsigned long flags)419 static int ptrace_attach(struct task_struct *task, long request,
420 unsigned long addr,
421 unsigned long flags)
422 {
423 bool seize = (request == PTRACE_SEIZE);
424 int retval;
425
426 if (seize) {
427 if (addr != 0)
428 return -EIO;
429 /*
430 * This duplicates the check in check_ptrace_options() because
431 * ptrace_attach() and ptrace_setoptions() have historically
432 * used different error codes for unknown ptrace options.
433 */
434 if (flags & ~(unsigned long)PTRACE_O_MASK)
435 return -EIO;
436
437 retval = check_ptrace_options(flags);
438 if (retval)
439 return retval;
440 flags = PT_PTRACED | PT_SEIZED | (flags << PT_OPT_FLAG_SHIFT);
441 } else {
442 flags = PT_PTRACED;
443 }
444
445 audit_ptrace(task);
446
447 if (unlikely(task->flags & PF_KTHREAD))
448 return -EPERM;
449 if (same_thread_group(task, current))
450 return -EPERM;
451
452 /*
453 * Protect exec's credential calculations against our interference;
454 * SUID, SGID and LSM creds get determined differently
455 * under ptrace.
456 */
457 scoped_cond_guard (mutex_intr, return -ERESTARTNOINTR,
458 &task->signal->cred_guard_mutex) {
459
460 scoped_guard (task_lock, task) {
461 retval = __ptrace_may_access(task, PTRACE_MODE_ATTACH_REALCREDS);
462 if (retval)
463 return retval;
464 }
465
466 scoped_guard (write_lock_irq, &tasklist_lock) {
467 if (unlikely(task->exit_state))
468 return -EPERM;
469 if (task->ptrace)
470 return -EPERM;
471
472 task->ptrace = flags;
473 ptrace_link(task, current);
474 ptrace_set_stopped(task, seize);
475 }
476 }
477
478 /*
479 * We do not bother to change retval or clear JOBCTL_TRAPPING
480 * if wait_on_bit() was interrupted by SIGKILL. The tracer will
481 * not return to user-mode, it will exit and clear this bit in
482 * __ptrace_unlink() if it wasn't already cleared by the tracee;
483 * and until then nobody can ptrace this task.
484 */
485 wait_on_bit(&task->jobctl, JOBCTL_TRAPPING_BIT, TASK_KILLABLE);
486 proc_ptrace_connector(task, PTRACE_ATTACH);
487
488 return 0;
489 }
490
491 /**
492 * ptrace_traceme -- helper for PTRACE_TRACEME
493 *
494 * Performs checks and sets PT_PTRACED.
495 * Should be used by all ptrace implementations for PTRACE_TRACEME.
496 */
ptrace_traceme(void)497 static int ptrace_traceme(void)
498 {
499 int ret = -EPERM;
500
501 write_lock_irq(&tasklist_lock);
502 /* Are we already being traced? */
503 if (!current->ptrace) {
504 ret = security_ptrace_traceme(current->parent);
505 /*
506 * Check PF_EXITING to ensure ->real_parent has not passed
507 * exit_ptrace(). Otherwise we don't report the error but
508 * pretend ->real_parent untraces us right after return.
509 */
510 if (!ret && !(current->real_parent->flags & PF_EXITING)) {
511 current->ptrace = PT_PTRACED;
512 ptrace_link(current, current->real_parent);
513 }
514 }
515 write_unlock_irq(&tasklist_lock);
516
517 return ret;
518 }
519
520 /*
521 * Called with irqs disabled, returns true if childs should reap themselves.
522 */
ignoring_children(struct sighand_struct * sigh)523 static int ignoring_children(struct sighand_struct *sigh)
524 {
525 int ret;
526 spin_lock(&sigh->siglock);
527 ret = (sigh->action[SIGCHLD-1].sa.sa_handler == SIG_IGN) ||
528 (sigh->action[SIGCHLD-1].sa.sa_flags & SA_NOCLDWAIT);
529 spin_unlock(&sigh->siglock);
530 return ret;
531 }
532
533 /*
534 * Called with tasklist_lock held for writing.
535 * Unlink a traced task, and clean it up if it was a traced zombie.
536 * Return true if it needs to be reaped with release_task().
537 * (We can't call release_task() here because we already hold tasklist_lock.)
538 *
539 * If it's a zombie, our attachedness prevented normal parent notification
540 * or self-reaping. Do notification now if it would have happened earlier.
541 * If it should reap itself, return true.
542 *
543 * If it's our own child, there is no notification to do. But if our normal
544 * children self-reap, then this child was prevented by ptrace and we must
545 * reap it now, in that case we must also wake up sub-threads sleeping in
546 * do_wait().
547 */
__ptrace_detach(struct task_struct * tracer,struct task_struct * p)548 static bool __ptrace_detach(struct task_struct *tracer, struct task_struct *p)
549 {
550 bool dead;
551
552 __ptrace_unlink(p);
553
554 if (p->exit_state != EXIT_ZOMBIE)
555 return false;
556
557 dead = !thread_group_leader(p);
558
559 if (!dead && thread_group_empty(p)) {
560 if (!same_thread_group(p->real_parent, tracer))
561 dead = do_notify_parent(p, p->exit_signal);
562 else if (ignoring_children(tracer->sighand) ||
563 p->signal->autoreap) {
564 __wake_up_parent(p, tracer);
565 dead = true;
566 }
567 }
568 /* Mark it as in the process of being reaped. */
569 if (dead)
570 p->exit_state = EXIT_DEAD;
571 return dead;
572 }
573
ptrace_detach(struct task_struct * child,unsigned int data)574 static int ptrace_detach(struct task_struct *child, unsigned int data)
575 {
576 if (!valid_signal(data))
577 return -EIO;
578
579 /* Architecture-specific hardware disable .. */
580 ptrace_disable(child);
581
582 write_lock_irq(&tasklist_lock);
583 /*
584 * We rely on ptrace_freeze_traced(). It can't be killed and
585 * untraced by another thread, it can't be a zombie.
586 */
587 WARN_ON(!child->ptrace || child->exit_state);
588 /*
589 * tasklist_lock avoids the race with wait_task_stopped(), see
590 * the comment in ptrace_resume().
591 */
592 child->exit_code = data;
593 __ptrace_detach(current, child);
594 write_unlock_irq(&tasklist_lock);
595
596 proc_ptrace_connector(child, PTRACE_DETACH);
597
598 return 0;
599 }
600
601 /*
602 * Detach all tasks we were using ptrace on. Called with tasklist held
603 * for writing.
604 */
exit_ptrace(struct task_struct * tracer,struct list_head * dead)605 void exit_ptrace(struct task_struct *tracer, struct list_head *dead)
606 {
607 struct task_struct *p, *n;
608
609 list_for_each_entry_safe(p, n, &tracer->ptraced, ptrace_entry) {
610 if (unlikely(p->ptrace & PT_EXITKILL))
611 send_sig_info(SIGKILL, SEND_SIG_PRIV, p);
612
613 if (__ptrace_detach(tracer, p))
614 list_add(&p->ptrace_entry, dead);
615 }
616 }
617
ptrace_readdata(struct task_struct * tsk,unsigned long src,char __user * dst,int len)618 int ptrace_readdata(struct task_struct *tsk, unsigned long src, char __user *dst, int len)
619 {
620 int copied = 0;
621
622 while (len > 0) {
623 char buf[128];
624 int this_len, retval;
625
626 this_len = (len > sizeof(buf)) ? sizeof(buf) : len;
627 retval = ptrace_access_vm(tsk, src, buf, this_len, FOLL_FORCE);
628
629 if (!retval) {
630 if (copied)
631 break;
632 return -EIO;
633 }
634 if (copy_to_user(dst, buf, retval))
635 return -EFAULT;
636 copied += retval;
637 src += retval;
638 dst += retval;
639 len -= retval;
640 }
641 return copied;
642 }
643
ptrace_writedata(struct task_struct * tsk,char __user * src,unsigned long dst,int len)644 int ptrace_writedata(struct task_struct *tsk, char __user *src, unsigned long dst, int len)
645 {
646 int copied = 0;
647
648 while (len > 0) {
649 char buf[128];
650 int this_len, retval;
651
652 this_len = (len > sizeof(buf)) ? sizeof(buf) : len;
653 if (copy_from_user(buf, src, this_len))
654 return -EFAULT;
655 retval = ptrace_access_vm(tsk, dst, buf, this_len,
656 FOLL_FORCE | FOLL_WRITE);
657 if (!retval) {
658 if (copied)
659 break;
660 return -EIO;
661 }
662 copied += retval;
663 src += retval;
664 dst += retval;
665 len -= retval;
666 }
667 return copied;
668 }
669
ptrace_setoptions(struct task_struct * child,unsigned long data)670 static int ptrace_setoptions(struct task_struct *child, unsigned long data)
671 {
672 unsigned flags;
673 int ret;
674
675 ret = check_ptrace_options(data);
676 if (ret)
677 return ret;
678
679 /* Avoid intermediate state when all opts are cleared */
680 flags = child->ptrace;
681 flags &= ~(PTRACE_O_MASK << PT_OPT_FLAG_SHIFT);
682 flags |= (data << PT_OPT_FLAG_SHIFT);
683 child->ptrace = flags;
684
685 return 0;
686 }
687
ptrace_getsiginfo(struct task_struct * child,kernel_siginfo_t * info)688 static int ptrace_getsiginfo(struct task_struct *child, kernel_siginfo_t *info)
689 {
690 unsigned long flags;
691 int error = -ESRCH;
692
693 if (lock_task_sighand(child, &flags)) {
694 error = -EINVAL;
695 if (likely(child->last_siginfo != NULL)) {
696 copy_siginfo(info, child->last_siginfo);
697 error = 0;
698 }
699 unlock_task_sighand(child, &flags);
700 }
701 return error;
702 }
703
ptrace_setsiginfo(struct task_struct * child,const kernel_siginfo_t * info)704 static int ptrace_setsiginfo(struct task_struct *child, const kernel_siginfo_t *info)
705 {
706 unsigned long flags;
707 int error = -ESRCH;
708
709 if (lock_task_sighand(child, &flags)) {
710 error = -EINVAL;
711 if (likely(child->last_siginfo != NULL)) {
712 copy_siginfo(child->last_siginfo, info);
713 error = 0;
714 }
715 unlock_task_sighand(child, &flags);
716 }
717 return error;
718 }
719
ptrace_peek_siginfo(struct task_struct * child,unsigned long addr,unsigned long data)720 static int ptrace_peek_siginfo(struct task_struct *child,
721 unsigned long addr,
722 unsigned long data)
723 {
724 struct ptrace_peeksiginfo_args arg;
725 struct sigpending *pending;
726 struct sigqueue *q;
727 int ret, i;
728
729 ret = copy_from_user(&arg, (void __user *) addr,
730 sizeof(struct ptrace_peeksiginfo_args));
731 if (ret)
732 return -EFAULT;
733
734 if (arg.flags & ~PTRACE_PEEKSIGINFO_SHARED)
735 return -EINVAL; /* unknown flags */
736
737 if (arg.nr < 0)
738 return -EINVAL;
739
740 /* Ensure arg.off fits in an unsigned long */
741 if (arg.off > ULONG_MAX)
742 return 0;
743
744 if (arg.flags & PTRACE_PEEKSIGINFO_SHARED)
745 pending = &child->signal->shared_pending;
746 else
747 pending = &child->pending;
748
749 for (i = 0; i < arg.nr; ) {
750 kernel_siginfo_t info;
751 unsigned long off = arg.off + i;
752 bool found = false;
753
754 spin_lock_irq(&child->sighand->siglock);
755 list_for_each_entry(q, &pending->list, list) {
756 if (!off--) {
757 found = true;
758 copy_siginfo(&info, &q->info);
759 break;
760 }
761 }
762 spin_unlock_irq(&child->sighand->siglock);
763
764 if (!found) /* beyond the end of the list */
765 break;
766
767 #ifdef CONFIG_COMPAT
768 if (unlikely(in_compat_syscall())) {
769 compat_siginfo_t __user *uinfo = compat_ptr(data);
770
771 if (copy_siginfo_to_user32(uinfo, &info)) {
772 ret = -EFAULT;
773 break;
774 }
775
776 } else
777 #endif
778 {
779 siginfo_t __user *uinfo = (siginfo_t __user *) data;
780
781 if (copy_siginfo_to_user(uinfo, &info)) {
782 ret = -EFAULT;
783 break;
784 }
785 }
786
787 data += sizeof(siginfo_t);
788 i++;
789
790 if (signal_pending(current))
791 break;
792
793 cond_resched();
794 }
795
796 if (i > 0)
797 return i;
798
799 return ret;
800 }
801
802 #ifdef CONFIG_RSEQ
ptrace_get_rseq_configuration(struct task_struct * task,unsigned long size,void __user * data)803 static long ptrace_get_rseq_configuration(struct task_struct *task,
804 unsigned long size, void __user *data)
805 {
806 struct ptrace_rseq_configuration conf = {
807 .rseq_abi_pointer = (u64)(uintptr_t)task->rseq.usrptr,
808 .rseq_abi_size = task->rseq.len,
809 .signature = task->rseq.sig,
810 .flags = 0,
811 };
812
813 size = min_t(unsigned long, size, sizeof(conf));
814 if (copy_to_user(data, &conf, size))
815 return -EFAULT;
816 return sizeof(conf);
817 }
818 #endif
819
820 #define is_singlestep(request) ((request) == PTRACE_SINGLESTEP)
821
822 #ifdef PTRACE_SINGLEBLOCK
823 #define is_singleblock(request) ((request) == PTRACE_SINGLEBLOCK)
824 #else
825 #define is_singleblock(request) 0
826 #endif
827
828 #ifdef PTRACE_SYSEMU
829 #define is_sysemu_singlestep(request) ((request) == PTRACE_SYSEMU_SINGLESTEP)
830 #else
831 #define is_sysemu_singlestep(request) 0
832 #endif
833
ptrace_resume(struct task_struct * child,long request,unsigned long data)834 static int ptrace_resume(struct task_struct *child, long request,
835 unsigned long data)
836 {
837 if (!valid_signal(data))
838 return -EIO;
839
840 if (request == PTRACE_SYSCALL)
841 set_task_syscall_work(child, SYSCALL_TRACE);
842 else
843 clear_task_syscall_work(child, SYSCALL_TRACE);
844
845 #if defined(CONFIG_GENERIC_ENTRY) || defined(TIF_SYSCALL_EMU)
846 if (request == PTRACE_SYSEMU || request == PTRACE_SYSEMU_SINGLESTEP)
847 set_task_syscall_work(child, SYSCALL_EMU);
848 else
849 clear_task_syscall_work(child, SYSCALL_EMU);
850 #endif
851
852 if (is_singleblock(request)) {
853 if (unlikely(!arch_has_block_step()))
854 return -EIO;
855 user_enable_block_step(child);
856 } else if (is_singlestep(request) || is_sysemu_singlestep(request)) {
857 if (unlikely(!arch_has_single_step()))
858 return -EIO;
859 user_enable_single_step(child);
860 } else {
861 user_disable_single_step(child);
862 }
863
864 /*
865 * Change ->exit_code and ->state under siglock to avoid the race
866 * with wait_task_stopped() in between; a non-zero ->exit_code will
867 * wrongly look like another report from tracee.
868 *
869 * Note that we need siglock even if ->exit_code == data and/or this
870 * status was not reported yet, the new status must not be cleared by
871 * wait_task_stopped() after resume.
872 */
873 spin_lock_irq(&child->sighand->siglock);
874 child->exit_code = data;
875 child->jobctl &= ~JOBCTL_TRACED;
876 wake_up_state(child, __TASK_TRACED);
877 spin_unlock_irq(&child->sighand->siglock);
878
879 return 0;
880 }
881
882 #ifdef CONFIG_HAVE_ARCH_TRACEHOOK
883
884 static const struct user_regset *
find_regset(const struct user_regset_view * view,unsigned int type)885 find_regset(const struct user_regset_view *view, unsigned int type)
886 {
887 const struct user_regset *regset;
888 int n;
889
890 for (n = 0; n < view->n; ++n) {
891 regset = view->regsets + n;
892 if (regset->core_note_type == type)
893 return regset;
894 }
895
896 return NULL;
897 }
898
ptrace_regset(struct task_struct * task,int req,unsigned int type,struct iovec * kiov)899 static int ptrace_regset(struct task_struct *task, int req, unsigned int type,
900 struct iovec *kiov)
901 {
902 const struct user_regset_view *view = task_user_regset_view(task);
903 const struct user_regset *regset = find_regset(view, type);
904 int regset_no;
905
906 if (!regset || (kiov->iov_len % regset->size) != 0)
907 return -EINVAL;
908
909 regset_no = regset - view->regsets;
910 kiov->iov_len = min(kiov->iov_len,
911 (__kernel_size_t) (regset->n * regset->size));
912
913 if (req == PTRACE_GETREGSET)
914 return copy_regset_to_user(task, view, regset_no, 0,
915 kiov->iov_len, kiov->iov_base);
916 else
917 return copy_regset_from_user(task, view, regset_no, 0,
918 kiov->iov_len, kiov->iov_base);
919 }
920
921 /*
922 * This is declared in linux/regset.h and defined in machine-dependent
923 * code. We put the export here, near the primary machine-neutral use,
924 * to ensure no machine forgets it.
925 */
926 EXPORT_SYMBOL_GPL(task_user_regset_view);
927
928 static unsigned long
ptrace_get_syscall_info_entry(struct task_struct * child,struct pt_regs * regs,struct ptrace_syscall_info * info)929 ptrace_get_syscall_info_entry(struct task_struct *child, struct pt_regs *regs,
930 struct ptrace_syscall_info *info)
931 {
932 unsigned long args[ARRAY_SIZE(info->entry.args)];
933 int i;
934
935 info->entry.nr = syscall_get_nr(child, regs);
936 syscall_get_arguments(child, regs, args);
937 for (i = 0; i < ARRAY_SIZE(args); i++)
938 info->entry.args[i] = args[i];
939
940 /* args is the last field in struct ptrace_syscall_info.entry */
941 return offsetofend(struct ptrace_syscall_info, entry.args);
942 }
943
944 static unsigned long
ptrace_get_syscall_info_seccomp(struct task_struct * child,struct pt_regs * regs,struct ptrace_syscall_info * info)945 ptrace_get_syscall_info_seccomp(struct task_struct *child, struct pt_regs *regs,
946 struct ptrace_syscall_info *info)
947 {
948 /*
949 * As struct ptrace_syscall_info.entry is currently a subset
950 * of struct ptrace_syscall_info.seccomp, it makes sense to
951 * initialize that subset using ptrace_get_syscall_info_entry().
952 * This can be reconsidered in the future if these structures
953 * diverge significantly enough.
954 */
955 ptrace_get_syscall_info_entry(child, regs, info);
956 info->seccomp.ret_data = child->ptrace_message;
957
958 /*
959 * ret_data is the last non-reserved field
960 * in struct ptrace_syscall_info.seccomp
961 */
962 return offsetofend(struct ptrace_syscall_info, seccomp.ret_data);
963 }
964
965 static unsigned long
ptrace_get_syscall_info_exit(struct task_struct * child,struct pt_regs * regs,struct ptrace_syscall_info * info)966 ptrace_get_syscall_info_exit(struct task_struct *child, struct pt_regs *regs,
967 struct ptrace_syscall_info *info)
968 {
969 info->exit.rval = syscall_get_error(child, regs);
970 info->exit.is_error = !!info->exit.rval;
971 if (!info->exit.is_error)
972 info->exit.rval = syscall_get_return_value(child, regs);
973
974 /* is_error is the last field in struct ptrace_syscall_info.exit */
975 return offsetofend(struct ptrace_syscall_info, exit.is_error);
976 }
977
978 static int
ptrace_get_syscall_info_op(struct task_struct * child)979 ptrace_get_syscall_info_op(struct task_struct *child)
980 {
981 /*
982 * This does not need lock_task_sighand() to access
983 * child->last_siginfo because ptrace_freeze_traced()
984 * called earlier by ptrace_check_attach() ensures that
985 * the tracee cannot go away and clear its last_siginfo.
986 */
987 switch (child->last_siginfo ? child->last_siginfo->si_code : 0) {
988 case SIGTRAP | 0x80:
989 switch (child->ptrace_message) {
990 case PTRACE_EVENTMSG_SYSCALL_ENTRY:
991 return PTRACE_SYSCALL_INFO_ENTRY;
992 case PTRACE_EVENTMSG_SYSCALL_EXIT:
993 return PTRACE_SYSCALL_INFO_EXIT;
994 default:
995 return PTRACE_SYSCALL_INFO_NONE;
996 }
997 case SIGTRAP | (PTRACE_EVENT_SECCOMP << 8):
998 return PTRACE_SYSCALL_INFO_SECCOMP;
999 default:
1000 return PTRACE_SYSCALL_INFO_NONE;
1001 }
1002 }
1003
1004 static int
ptrace_get_syscall_info(struct task_struct * child,unsigned long user_size,void __user * datavp)1005 ptrace_get_syscall_info(struct task_struct *child, unsigned long user_size,
1006 void __user *datavp)
1007 {
1008 struct pt_regs *regs = task_pt_regs(child);
1009 struct ptrace_syscall_info info = {
1010 .op = ptrace_get_syscall_info_op(child),
1011 .arch = syscall_get_arch(child),
1012 .instruction_pointer = instruction_pointer(regs),
1013 .stack_pointer = user_stack_pointer(regs),
1014 };
1015 unsigned long actual_size = offsetof(struct ptrace_syscall_info, entry);
1016 unsigned long write_size;
1017
1018 switch (info.op) {
1019 case PTRACE_SYSCALL_INFO_ENTRY:
1020 actual_size = ptrace_get_syscall_info_entry(child, regs, &info);
1021 break;
1022 case PTRACE_SYSCALL_INFO_EXIT:
1023 actual_size = ptrace_get_syscall_info_exit(child, regs, &info);
1024 break;
1025 case PTRACE_SYSCALL_INFO_SECCOMP:
1026 actual_size = ptrace_get_syscall_info_seccomp(child, regs, &info);
1027 break;
1028 }
1029
1030 write_size = min(actual_size, user_size);
1031 return copy_to_user(datavp, &info, write_size) ? -EFAULT : actual_size;
1032 }
1033
1034 static int
ptrace_set_syscall_info_entry(struct task_struct * child,struct pt_regs * regs,struct ptrace_syscall_info * info)1035 ptrace_set_syscall_info_entry(struct task_struct *child, struct pt_regs *regs,
1036 struct ptrace_syscall_info *info)
1037 {
1038 unsigned long args[ARRAY_SIZE(info->entry.args)];
1039 int nr = info->entry.nr;
1040 int i;
1041
1042 /*
1043 * Check that the syscall number specified in info->entry.nr
1044 * is either a value of type "int" or a sign-extended value
1045 * of type "int".
1046 */
1047 if (nr != info->entry.nr)
1048 return -ERANGE;
1049
1050 for (i = 0; i < ARRAY_SIZE(args); i++) {
1051 args[i] = info->entry.args[i];
1052 /*
1053 * Check that the syscall argument specified in
1054 * info->entry.args[i] is either a value of type
1055 * "unsigned long" or a sign-extended value of type "long".
1056 */
1057 if (args[i] != info->entry.args[i])
1058 return -ERANGE;
1059 }
1060
1061 syscall_set_nr(child, regs, nr);
1062 /*
1063 * If the syscall number is set to -1, setting syscall arguments is not
1064 * just pointless, it would also clobber the syscall return value on
1065 * those architectures that share the same register both for the first
1066 * argument of syscall and its return value.
1067 */
1068 if (nr != -1)
1069 syscall_set_arguments(child, regs, args);
1070
1071 return 0;
1072 }
1073
1074 static int
ptrace_set_syscall_info_seccomp(struct task_struct * child,struct pt_regs * regs,struct ptrace_syscall_info * info)1075 ptrace_set_syscall_info_seccomp(struct task_struct *child, struct pt_regs *regs,
1076 struct ptrace_syscall_info *info)
1077 {
1078 /*
1079 * info->entry is currently a subset of info->seccomp,
1080 * info->seccomp.ret_data is currently ignored.
1081 */
1082 return ptrace_set_syscall_info_entry(child, regs, info);
1083 }
1084
1085 static int
ptrace_set_syscall_info_exit(struct task_struct * child,struct pt_regs * regs,struct ptrace_syscall_info * info)1086 ptrace_set_syscall_info_exit(struct task_struct *child, struct pt_regs *regs,
1087 struct ptrace_syscall_info *info)
1088 {
1089 long rval = info->exit.rval;
1090
1091 /*
1092 * Check that the return value specified in info->exit.rval
1093 * is either a value of type "long" or a sign-extended value
1094 * of type "long".
1095 */
1096 if (rval != info->exit.rval)
1097 return -ERANGE;
1098
1099 if (info->exit.is_error)
1100 syscall_set_return_value(child, regs, rval, 0);
1101 else
1102 syscall_set_return_value(child, regs, 0, rval);
1103
1104 return 0;
1105 }
1106
1107 static int
ptrace_set_syscall_info(struct task_struct * child,unsigned long user_size,const void __user * datavp)1108 ptrace_set_syscall_info(struct task_struct *child, unsigned long user_size,
1109 const void __user *datavp)
1110 {
1111 struct pt_regs *regs = task_pt_regs(child);
1112 struct ptrace_syscall_info info;
1113
1114 if (user_size < sizeof(info))
1115 return -EINVAL;
1116
1117 /*
1118 * The compatibility is tracked by info.op and info.flags: if user-space
1119 * does not instruct us to use unknown extra bits from future versions
1120 * of ptrace_syscall_info, we are not going to read them either.
1121 */
1122 if (copy_from_user(&info, datavp, sizeof(info)))
1123 return -EFAULT;
1124
1125 /* Reserved for future use. */
1126 if (info.flags || info.reserved)
1127 return -EINVAL;
1128
1129 /* Changing the type of the system call stop is not supported yet. */
1130 if (ptrace_get_syscall_info_op(child) != info.op)
1131 return -EINVAL;
1132
1133 switch (info.op) {
1134 case PTRACE_SYSCALL_INFO_ENTRY:
1135 return ptrace_set_syscall_info_entry(child, regs, &info);
1136 case PTRACE_SYSCALL_INFO_EXIT:
1137 return ptrace_set_syscall_info_exit(child, regs, &info);
1138 case PTRACE_SYSCALL_INFO_SECCOMP:
1139 return ptrace_set_syscall_info_seccomp(child, regs, &info);
1140 default:
1141 /* Other types of system call stops are not supported yet. */
1142 return -EINVAL;
1143 }
1144 }
1145 #endif /* CONFIG_HAVE_ARCH_TRACEHOOK */
1146
ptrace_request(struct task_struct * child,long request,unsigned long addr,unsigned long data)1147 int ptrace_request(struct task_struct *child, long request,
1148 unsigned long addr, unsigned long data)
1149 {
1150 bool seized = child->ptrace & PT_SEIZED;
1151 int ret = -EIO;
1152 kernel_siginfo_t siginfo, *si;
1153 void __user *datavp = (void __user *) data;
1154 unsigned long __user *datalp = datavp;
1155 unsigned long flags;
1156
1157 switch (request) {
1158 case PTRACE_PEEKTEXT:
1159 case PTRACE_PEEKDATA:
1160 return generic_ptrace_peekdata(child, addr, data);
1161 case PTRACE_POKETEXT:
1162 case PTRACE_POKEDATA:
1163 return generic_ptrace_pokedata(child, addr, data);
1164
1165 #ifdef PTRACE_OLDSETOPTIONS
1166 case PTRACE_OLDSETOPTIONS:
1167 #endif
1168 case PTRACE_SETOPTIONS:
1169 ret = ptrace_setoptions(child, data);
1170 break;
1171 case PTRACE_GETEVENTMSG:
1172 ret = put_user(child->ptrace_message, datalp);
1173 break;
1174
1175 case PTRACE_PEEKSIGINFO:
1176 ret = ptrace_peek_siginfo(child, addr, data);
1177 break;
1178
1179 case PTRACE_GETSIGINFO:
1180 ret = ptrace_getsiginfo(child, &siginfo);
1181 if (!ret)
1182 ret = copy_siginfo_to_user(datavp, &siginfo);
1183 break;
1184
1185 case PTRACE_SETSIGINFO:
1186 ret = copy_siginfo_from_user(&siginfo, datavp);
1187 if (!ret)
1188 ret = ptrace_setsiginfo(child, &siginfo);
1189 break;
1190
1191 case PTRACE_GETSIGMASK: {
1192 sigset_t *mask;
1193
1194 if (addr != sizeof(sigset_t)) {
1195 ret = -EINVAL;
1196 break;
1197 }
1198
1199 if (test_tsk_restore_sigmask(child))
1200 mask = &child->saved_sigmask;
1201 else
1202 mask = &child->blocked;
1203
1204 if (copy_to_user(datavp, mask, sizeof(sigset_t)))
1205 ret = -EFAULT;
1206 else
1207 ret = 0;
1208
1209 break;
1210 }
1211
1212 case PTRACE_SETSIGMASK: {
1213 sigset_t new_set;
1214
1215 if (addr != sizeof(sigset_t)) {
1216 ret = -EINVAL;
1217 break;
1218 }
1219
1220 if (copy_from_user(&new_set, datavp, sizeof(sigset_t))) {
1221 ret = -EFAULT;
1222 break;
1223 }
1224
1225 sigdelsetmask(&new_set, sigmask(SIGKILL)|sigmask(SIGSTOP));
1226
1227 /*
1228 * Every thread does recalc_sigpending() after resume, so
1229 * retarget_shared_pending() and recalc_sigpending() are not
1230 * called here.
1231 */
1232 spin_lock_irq(&child->sighand->siglock);
1233 child->blocked = new_set;
1234 spin_unlock_irq(&child->sighand->siglock);
1235
1236 clear_tsk_restore_sigmask(child);
1237
1238 ret = 0;
1239 break;
1240 }
1241
1242 case PTRACE_INTERRUPT:
1243 /*
1244 * Stop tracee without any side-effect on signal or job
1245 * control. At least one trap is guaranteed to happen
1246 * after this request. If @child is already trapped, the
1247 * current trap is not disturbed and another trap will
1248 * happen after the current trap is ended with PTRACE_CONT.
1249 *
1250 * The actual trap might not be PTRACE_EVENT_STOP trap but
1251 * the pending condition is cleared regardless.
1252 */
1253 if (unlikely(!seized || !lock_task_sighand(child, &flags)))
1254 break;
1255
1256 /*
1257 * INTERRUPT doesn't disturb existing trap sans one
1258 * exception. If ptracer issued LISTEN for the current
1259 * STOP, this INTERRUPT should clear LISTEN and re-trap
1260 * tracee into STOP.
1261 */
1262 if (likely(task_set_jobctl_pending(child, JOBCTL_TRAP_STOP)))
1263 ptrace_signal_wake_up(child, child->jobctl & JOBCTL_LISTENING);
1264
1265 unlock_task_sighand(child, &flags);
1266 ret = 0;
1267 break;
1268
1269 case PTRACE_LISTEN:
1270 /*
1271 * Listen for events. Tracee must be in STOP. It's not
1272 * resumed per-se but is not considered to be in TRACED by
1273 * wait(2) or ptrace(2). If an async event (e.g. group
1274 * stop state change) happens, tracee will enter STOP trap
1275 * again. Alternatively, ptracer can issue INTERRUPT to
1276 * finish listening and re-trap tracee into STOP.
1277 */
1278 if (unlikely(!seized || !lock_task_sighand(child, &flags)))
1279 break;
1280
1281 si = child->last_siginfo;
1282 if (likely(si && (si->si_code >> 8) == PTRACE_EVENT_STOP)) {
1283 child->jobctl |= JOBCTL_LISTENING;
1284 /*
1285 * If NOTIFY is set, it means event happened between
1286 * start of this trap and now. Trigger re-trap.
1287 */
1288 if (child->jobctl & JOBCTL_TRAP_NOTIFY)
1289 ptrace_signal_wake_up(child, true);
1290 ret = 0;
1291 }
1292 unlock_task_sighand(child, &flags);
1293 break;
1294
1295 case PTRACE_DETACH: /* detach a process that was attached. */
1296 ret = ptrace_detach(child, data);
1297 break;
1298
1299 #ifdef CONFIG_BINFMT_ELF_FDPIC
1300 case PTRACE_GETFDPIC: {
1301 struct mm_struct *mm = get_task_mm(child);
1302 unsigned long tmp = 0;
1303
1304 ret = -ESRCH;
1305 if (!mm)
1306 break;
1307
1308 switch (addr) {
1309 case PTRACE_GETFDPIC_EXEC:
1310 tmp = mm->context.exec_fdpic_loadmap;
1311 break;
1312 case PTRACE_GETFDPIC_INTERP:
1313 tmp = mm->context.interp_fdpic_loadmap;
1314 break;
1315 default:
1316 break;
1317 }
1318 mmput(mm);
1319
1320 ret = put_user(tmp, datalp);
1321 break;
1322 }
1323 #endif
1324
1325 case PTRACE_SINGLESTEP:
1326 #ifdef PTRACE_SINGLEBLOCK
1327 case PTRACE_SINGLEBLOCK:
1328 #endif
1329 #ifdef PTRACE_SYSEMU
1330 case PTRACE_SYSEMU:
1331 case PTRACE_SYSEMU_SINGLESTEP:
1332 #endif
1333 case PTRACE_SYSCALL:
1334 case PTRACE_CONT:
1335 return ptrace_resume(child, request, data);
1336
1337 case PTRACE_KILL:
1338 send_sig_info(SIGKILL, SEND_SIG_NOINFO, child);
1339 return 0;
1340
1341 #ifdef CONFIG_HAVE_ARCH_TRACEHOOK
1342 case PTRACE_GETREGSET:
1343 case PTRACE_SETREGSET: {
1344 struct iovec kiov;
1345 struct iovec __user *uiov = datavp;
1346
1347 if (!access_ok(uiov, sizeof(*uiov)))
1348 return -EFAULT;
1349
1350 if (__get_user(kiov.iov_base, &uiov->iov_base) ||
1351 __get_user(kiov.iov_len, &uiov->iov_len))
1352 return -EFAULT;
1353
1354 ret = ptrace_regset(child, request, addr, &kiov);
1355 if (!ret)
1356 ret = __put_user(kiov.iov_len, &uiov->iov_len);
1357 break;
1358 }
1359
1360 case PTRACE_GET_SYSCALL_INFO:
1361 ret = ptrace_get_syscall_info(child, addr, datavp);
1362 break;
1363
1364 case PTRACE_SET_SYSCALL_INFO:
1365 ret = ptrace_set_syscall_info(child, addr, datavp);
1366 break;
1367 #endif
1368
1369 case PTRACE_SECCOMP_GET_FILTER:
1370 ret = seccomp_get_filter(child, addr, datavp);
1371 break;
1372
1373 case PTRACE_SECCOMP_GET_METADATA:
1374 ret = seccomp_get_metadata(child, addr, datavp);
1375 break;
1376
1377 #ifdef CONFIG_RSEQ
1378 case PTRACE_GET_RSEQ_CONFIGURATION:
1379 ret = ptrace_get_rseq_configuration(child, addr, datavp);
1380 break;
1381 #endif
1382
1383 case PTRACE_SET_SYSCALL_USER_DISPATCH_CONFIG:
1384 ret = syscall_user_dispatch_set_config(child, addr, datavp);
1385 break;
1386
1387 case PTRACE_GET_SYSCALL_USER_DISPATCH_CONFIG:
1388 ret = syscall_user_dispatch_get_config(child, addr, datavp);
1389 break;
1390
1391 default:
1392 break;
1393 }
1394
1395 return ret;
1396 }
1397
SYSCALL_DEFINE4(ptrace,long,request,long,pid,unsigned long,addr,unsigned long,data)1398 SYSCALL_DEFINE4(ptrace, long, request, long, pid, unsigned long, addr,
1399 unsigned long, data)
1400 {
1401 struct task_struct *child;
1402 long ret;
1403
1404 if (request == PTRACE_TRACEME) {
1405 ret = ptrace_traceme();
1406 goto out;
1407 }
1408
1409 child = find_get_task_by_vpid(pid);
1410 if (!child) {
1411 ret = -ESRCH;
1412 goto out;
1413 }
1414
1415 if (request == PTRACE_ATTACH || request == PTRACE_SEIZE) {
1416 ret = ptrace_attach(child, request, addr, data);
1417 goto out_put_task_struct;
1418 }
1419
1420 ret = ptrace_check_attach(child, request == PTRACE_KILL ||
1421 request == PTRACE_INTERRUPT);
1422 if (ret < 0)
1423 goto out_put_task_struct;
1424
1425 ret = arch_ptrace(child, request, addr, data);
1426 if (ret || request != PTRACE_DETACH)
1427 ptrace_unfreeze_traced(child);
1428
1429 out_put_task_struct:
1430 put_task_struct(child);
1431 out:
1432 return ret;
1433 }
1434
generic_ptrace_peekdata(struct task_struct * tsk,unsigned long addr,unsigned long data)1435 int generic_ptrace_peekdata(struct task_struct *tsk, unsigned long addr,
1436 unsigned long data)
1437 {
1438 unsigned long tmp;
1439 int copied;
1440
1441 copied = ptrace_access_vm(tsk, addr, &tmp, sizeof(tmp), FOLL_FORCE);
1442 if (copied != sizeof(tmp))
1443 return -EIO;
1444 return put_user(tmp, (unsigned long __user *)data);
1445 }
1446
generic_ptrace_pokedata(struct task_struct * tsk,unsigned long addr,unsigned long data)1447 int generic_ptrace_pokedata(struct task_struct *tsk, unsigned long addr,
1448 unsigned long data)
1449 {
1450 int copied;
1451
1452 copied = ptrace_access_vm(tsk, addr, &data, sizeof(data),
1453 FOLL_FORCE | FOLL_WRITE);
1454 return (copied == sizeof(data)) ? 0 : -EIO;
1455 }
1456
1457 #if defined CONFIG_COMPAT
1458
compat_ptrace_request(struct task_struct * child,compat_long_t request,compat_ulong_t addr,compat_ulong_t data)1459 int compat_ptrace_request(struct task_struct *child, compat_long_t request,
1460 compat_ulong_t addr, compat_ulong_t data)
1461 {
1462 compat_ulong_t __user *datap = compat_ptr(data);
1463 compat_ulong_t word;
1464 kernel_siginfo_t siginfo;
1465 int ret;
1466
1467 switch (request) {
1468 case PTRACE_PEEKTEXT:
1469 case PTRACE_PEEKDATA:
1470 ret = ptrace_access_vm(child, addr, &word, sizeof(word),
1471 FOLL_FORCE);
1472 if (ret != sizeof(word))
1473 ret = -EIO;
1474 else
1475 ret = put_user(word, datap);
1476 break;
1477
1478 case PTRACE_POKETEXT:
1479 case PTRACE_POKEDATA:
1480 ret = ptrace_access_vm(child, addr, &data, sizeof(data),
1481 FOLL_FORCE | FOLL_WRITE);
1482 ret = (ret != sizeof(data) ? -EIO : 0);
1483 break;
1484
1485 case PTRACE_GETEVENTMSG:
1486 ret = put_user((compat_ulong_t) child->ptrace_message, datap);
1487 break;
1488
1489 case PTRACE_GETSIGINFO:
1490 ret = ptrace_getsiginfo(child, &siginfo);
1491 if (!ret)
1492 ret = copy_siginfo_to_user32(
1493 (struct compat_siginfo __user *) datap,
1494 &siginfo);
1495 break;
1496
1497 case PTRACE_SETSIGINFO:
1498 ret = copy_siginfo_from_user32(
1499 &siginfo, (struct compat_siginfo __user *) datap);
1500 if (!ret)
1501 ret = ptrace_setsiginfo(child, &siginfo);
1502 break;
1503 #ifdef CONFIG_HAVE_ARCH_TRACEHOOK
1504 case PTRACE_GETREGSET:
1505 case PTRACE_SETREGSET:
1506 {
1507 struct iovec kiov;
1508 struct compat_iovec __user *uiov =
1509 (struct compat_iovec __user *) datap;
1510 compat_uptr_t ptr;
1511 compat_size_t len;
1512
1513 if (!access_ok(uiov, sizeof(*uiov)))
1514 return -EFAULT;
1515
1516 if (__get_user(ptr, &uiov->iov_base) ||
1517 __get_user(len, &uiov->iov_len))
1518 return -EFAULT;
1519
1520 kiov.iov_base = compat_ptr(ptr);
1521 kiov.iov_len = len;
1522
1523 ret = ptrace_regset(child, request, addr, &kiov);
1524 if (!ret)
1525 ret = __put_user(kiov.iov_len, &uiov->iov_len);
1526 break;
1527 }
1528 #endif
1529
1530 default:
1531 ret = ptrace_request(child, request, addr, data);
1532 }
1533
1534 return ret;
1535 }
1536
COMPAT_SYSCALL_DEFINE4(ptrace,compat_long_t,request,compat_long_t,pid,compat_long_t,addr,compat_long_t,data)1537 COMPAT_SYSCALL_DEFINE4(ptrace, compat_long_t, request, compat_long_t, pid,
1538 compat_long_t, addr, compat_long_t, data)
1539 {
1540 struct task_struct *child;
1541 long ret;
1542
1543 if (request == PTRACE_TRACEME) {
1544 ret = ptrace_traceme();
1545 goto out;
1546 }
1547
1548 child = find_get_task_by_vpid(pid);
1549 if (!child) {
1550 ret = -ESRCH;
1551 goto out;
1552 }
1553
1554 if (request == PTRACE_ATTACH || request == PTRACE_SEIZE) {
1555 ret = ptrace_attach(child, request, addr, data);
1556 goto out_put_task_struct;
1557 }
1558
1559 ret = ptrace_check_attach(child, request == PTRACE_KILL ||
1560 request == PTRACE_INTERRUPT);
1561 if (!ret) {
1562 ret = compat_arch_ptrace(child, request, addr, data);
1563 if (ret || request != PTRACE_DETACH)
1564 ptrace_unfreeze_traced(child);
1565 }
1566
1567 out_put_task_struct:
1568 put_task_struct(child);
1569 out:
1570 return ret;
1571 }
1572 #endif /* CONFIG_COMPAT */
1573