projects/linux·blob master · refs

1593 lines · 41.3 KiB

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