1457c8996SThomas Gleixner // SPDX-License-Identifier: GPL-2.0-only 21da177e4SLinus Torvalds /* 31da177e4SLinus Torvalds * linux/kernel/ptrace.c 41da177e4SLinus Torvalds * 51da177e4SLinus Torvalds * (C) Copyright 1999 Linus Torvalds 61da177e4SLinus Torvalds * 71da177e4SLinus Torvalds * Common interfaces for "ptrace()" which we do not want 81da177e4SLinus Torvalds * to continually duplicate across every architecture. 91da177e4SLinus Torvalds */ 101da177e4SLinus Torvalds 11c59ede7bSRandy.Dunlap #include <linux/capability.h> 129984de1aSPaul Gortmaker #include <linux/export.h> 131da177e4SLinus Torvalds #include <linux/sched.h> 146e84f315SIngo Molnar #include <linux/sched/mm.h> 15f7ccbae4SIngo Molnar #include <linux/sched/coredump.h> 1629930025SIngo Molnar #include <linux/sched/task.h> 171da177e4SLinus Torvalds #include <linux/errno.h> 181da177e4SLinus Torvalds #include <linux/mm.h> 191da177e4SLinus Torvalds #include <linux/highmem.h> 201da177e4SLinus Torvalds #include <linux/pagemap.h> 211da177e4SLinus Torvalds #include <linux/ptrace.h> 221da177e4SLinus Torvalds #include <linux/security.h> 237ed20e1aSJesper Juhl #include <linux/signal.h> 24a27bb332SKent Overstreet #include <linux/uio.h> 25a5cb013dSAl Viro #include <linux/audit.h> 26b488893aSPavel Emelyanov #include <linux/pid_namespace.h> 27f17d30a8SAdrian Bunk #include <linux/syscalls.h> 283a709703SRoland McGrath #include <linux/uaccess.h> 292225a122SSuresh Siddha #include <linux/regset.h> 30bf26c018SFrederic Weisbecker #include <linux/hw_breakpoint.h> 31f701e5b7SVladimir Zapolskiy #include <linux/cn_proc.h> 3284c751bdSAndrey Vagin #include <linux/compat.h> 33fcfc2aa0SAndrei Vagin #include <linux/sched/signal.h> 3490f093faSPiotr Figiel #include <linux/minmax.h> 351da177e4SLinus Torvalds 36201766a2SElvira Khabirova #include <asm/syscall.h> /* for syscall_get_* */ 37201766a2SElvira Khabirova 3884d77d3fSEric W. Biederman /* 3984d77d3fSEric W. Biederman * Access another process' address space via ptrace. 4084d77d3fSEric W. Biederman * Source/target buffer must be kernel space, 4184d77d3fSEric W. Biederman * Do not walk the page table directly, use get_user_pages 4284d77d3fSEric W. Biederman */ 4384d77d3fSEric W. Biederman int ptrace_access_vm(struct task_struct *tsk, unsigned long addr, 4484d77d3fSEric W. Biederman void *buf, int len, unsigned int gup_flags) 4584d77d3fSEric W. Biederman { 4684d77d3fSEric W. Biederman struct mm_struct *mm; 4784d77d3fSEric W. Biederman int ret; 4884d77d3fSEric W. Biederman 4984d77d3fSEric W. Biederman mm = get_task_mm(tsk); 5084d77d3fSEric W. Biederman if (!mm) 5184d77d3fSEric W. Biederman return 0; 5284d77d3fSEric W. Biederman 5384d77d3fSEric W. Biederman if (!tsk->ptrace || 5484d77d3fSEric W. Biederman (current != tsk->parent) || 5584d77d3fSEric W. Biederman ((get_dumpable(mm) != SUID_DUMP_USER) && 5684d77d3fSEric W. Biederman !ptracer_capable(tsk, mm->user_ns))) { 5784d77d3fSEric W. Biederman mmput(mm); 5884d77d3fSEric W. Biederman return 0; 5984d77d3fSEric W. Biederman } 6084d77d3fSEric W. Biederman 61d3f5ffcaSJohn Hubbard ret = __access_remote_vm(mm, addr, buf, len, gup_flags); 6284d77d3fSEric W. Biederman mmput(mm); 6384d77d3fSEric W. Biederman 6484d77d3fSEric W. Biederman return ret; 6584d77d3fSEric W. Biederman } 6684d77d3fSEric W. Biederman 67bf53de90SMarkus Metzger 68c70d9d80SEric W. Biederman void __ptrace_link(struct task_struct *child, struct task_struct *new_parent, 69c70d9d80SEric W. Biederman const struct cred *ptracer_cred) 70c70d9d80SEric W. Biederman { 71c70d9d80SEric W. Biederman BUG_ON(!list_empty(&child->ptrace_entry)); 72c70d9d80SEric W. Biederman list_add(&child->ptrace_entry, &new_parent->ptraced); 73c70d9d80SEric W. Biederman child->parent = new_parent; 74c70d9d80SEric W. Biederman child->ptracer_cred = get_cred(ptracer_cred); 75c70d9d80SEric W. Biederman } 76c70d9d80SEric W. Biederman 77bf53de90SMarkus Metzger /* 781da177e4SLinus Torvalds * ptrace a task: make the debugger its new parent and 791da177e4SLinus Torvalds * move it to the ptrace list. 801da177e4SLinus Torvalds * 811da177e4SLinus Torvalds * Must be called with the tasklist lock write-held. 821da177e4SLinus Torvalds */ 83c70d9d80SEric W. Biederman static void ptrace_link(struct task_struct *child, struct task_struct *new_parent) 841da177e4SLinus Torvalds { 856994eefbSJann Horn __ptrace_link(child, new_parent, current_cred()); 861da177e4SLinus Torvalds } 871da177e4SLinus Torvalds 88e3bd058fSTejun Heo /** 89e3bd058fSTejun Heo * __ptrace_unlink - unlink ptracee and restore its execution state 90e3bd058fSTejun Heo * @child: ptracee to be unlinked 911da177e4SLinus Torvalds * 920e9f0a4aSTejun Heo * Remove @child from the ptrace list, move it back to the original parent, 930e9f0a4aSTejun Heo * and restore the execution state so that it conforms to the group stop 940e9f0a4aSTejun Heo * state. 950e9f0a4aSTejun Heo * 960e9f0a4aSTejun Heo * Unlinking can happen via two paths - explicit PTRACE_DETACH or ptracer 970e9f0a4aSTejun Heo * exiting. For PTRACE_DETACH, unless the ptracee has been killed between 980e9f0a4aSTejun Heo * ptrace_check_attach() and here, it's guaranteed to be in TASK_TRACED. 990e9f0a4aSTejun Heo * If the ptracer is exiting, the ptracee can be in any state. 1000e9f0a4aSTejun Heo * 1010e9f0a4aSTejun Heo * After detach, the ptracee should be in a state which conforms to the 1020e9f0a4aSTejun Heo * group stop. If the group is stopped or in the process of stopping, the 1030e9f0a4aSTejun Heo * ptracee should be put into TASK_STOPPED; otherwise, it should be woken 1040e9f0a4aSTejun Heo * up from TASK_TRACED. 1050e9f0a4aSTejun Heo * 1060e9f0a4aSTejun Heo * If the ptracee is in TASK_TRACED and needs to be moved to TASK_STOPPED, 1070e9f0a4aSTejun Heo * it goes through TRACED -> RUNNING -> STOPPED transition which is similar 1080e9f0a4aSTejun Heo * to but in the opposite direction of what happens while attaching to a 1090e9f0a4aSTejun Heo * stopped task. However, in this direction, the intermediate RUNNING 1100e9f0a4aSTejun Heo * state is not hidden even from the current ptracer and if it immediately 1110e9f0a4aSTejun Heo * re-attaches and performs a WNOHANG wait(2), it may fail. 112e3bd058fSTejun Heo * 113e3bd058fSTejun Heo * CONTEXT: 114e3bd058fSTejun Heo * write_lock_irq(tasklist_lock) 1151da177e4SLinus Torvalds */ 11636c8b586SIngo Molnar void __ptrace_unlink(struct task_struct *child) 1171da177e4SLinus Torvalds { 11864b875f7SEric W. Biederman const struct cred *old_cred; 1195ecfbae0SOleg Nesterov BUG_ON(!child->ptrace); 1205ecfbae0SOleg Nesterov 12164c19ba2SGabriel Krisman Bertazi clear_task_syscall_work(child, SYSCALL_TRACE); 12264eb35f7SGabriel Krisman Bertazi #if defined(CONFIG_GENERIC_ENTRY) || defined(TIF_SYSCALL_EMU) 12364eb35f7SGabriel Krisman Bertazi clear_task_syscall_work(child, SYSCALL_EMU); 12415532fd6SSudeep Holla #endif 1250a5bf409SAles Novak 1261da177e4SLinus Torvalds child->parent = child->real_parent; 127f470021aSRoland McGrath list_del_init(&child->ptrace_entry); 12864b875f7SEric W. Biederman old_cred = child->ptracer_cred; 12964b875f7SEric W. Biederman child->ptracer_cred = NULL; 13064b875f7SEric W. Biederman put_cred(old_cred); 1311da177e4SLinus Torvalds 1321da177e4SLinus Torvalds spin_lock(&child->sighand->siglock); 1331333ab03SOleg Nesterov child->ptrace = 0; 1341da177e4SLinus Torvalds /* 13573ddff2bSTejun Heo * Clear all pending traps and TRAPPING. TRAPPING should be 13673ddff2bSTejun Heo * cleared regardless of JOBCTL_STOP_PENDING. Do it explicitly. 13773ddff2bSTejun Heo */ 13873ddff2bSTejun Heo task_clear_jobctl_pending(child, JOBCTL_TRAP_MASK); 13973ddff2bSTejun Heo task_clear_jobctl_trapping(child); 14073ddff2bSTejun Heo 14173ddff2bSTejun Heo /* 142a8f072c1STejun Heo * Reinstate JOBCTL_STOP_PENDING if group stop is in effect and 1430e9f0a4aSTejun Heo * @child isn't dead. 1441da177e4SLinus Torvalds */ 1450e9f0a4aSTejun Heo if (!(child->flags & PF_EXITING) && 1460e9f0a4aSTejun Heo (child->signal->flags & SIGNAL_STOP_STOPPED || 1478a88951bSOleg Nesterov child->signal->group_stop_count)) { 148a8f072c1STejun Heo child->jobctl |= JOBCTL_STOP_PENDING; 1490e9f0a4aSTejun Heo 1500e9f0a4aSTejun Heo /* 1518a88951bSOleg Nesterov * This is only possible if this thread was cloned by the 1528a88951bSOleg Nesterov * traced task running in the stopped group, set the signal 1538a88951bSOleg Nesterov * for the future reports. 1548a88951bSOleg Nesterov * FIXME: we should change ptrace_init_task() to handle this 1558a88951bSOleg Nesterov * case. 1568a88951bSOleg Nesterov */ 1578a88951bSOleg Nesterov if (!(child->jobctl & JOBCTL_STOP_SIGMASK)) 1588a88951bSOleg Nesterov child->jobctl |= SIGSTOP; 1598a88951bSOleg Nesterov } 1608a88951bSOleg Nesterov 1618a88951bSOleg Nesterov /* 1620e9f0a4aSTejun Heo * If transition to TASK_STOPPED is pending or in TASK_TRACED, kick 1630e9f0a4aSTejun Heo * @child in the butt. Note that @resume should be used iff @child 1640e9f0a4aSTejun Heo * is in TASK_TRACED; otherwise, we might unduly disrupt 1650e9f0a4aSTejun Heo * TASK_KILLABLE sleeps. 1660e9f0a4aSTejun Heo */ 167a8f072c1STejun Heo if (child->jobctl & JOBCTL_STOP_PENDING || task_is_traced(child)) 168910ffdb1SOleg Nesterov ptrace_signal_wake_up(child, true); 1690e9f0a4aSTejun Heo 1701da177e4SLinus Torvalds spin_unlock(&child->sighand->siglock); 1711da177e4SLinus Torvalds } 1721da177e4SLinus Torvalds 173dbb5afadSOleg Nesterov static bool looks_like_a_spurious_pid(struct task_struct *task) 174dbb5afadSOleg Nesterov { 175dbb5afadSOleg Nesterov if (task->exit_code != ((PTRACE_EVENT_EXEC << 8) | SIGTRAP)) 176dbb5afadSOleg Nesterov return false; 177dbb5afadSOleg Nesterov 178dbb5afadSOleg Nesterov if (task_pid_vnr(task) == task->ptrace_message) 179dbb5afadSOleg Nesterov return false; 180dbb5afadSOleg Nesterov /* 181dbb5afadSOleg Nesterov * The tracee changed its pid but the PTRACE_EVENT_EXEC event 182dbb5afadSOleg Nesterov * was not wait()'ed, most probably debugger targets the old 183dbb5afadSOleg Nesterov * leader which was destroyed in de_thread(). 184dbb5afadSOleg Nesterov */ 185dbb5afadSOleg Nesterov return true; 186dbb5afadSOleg Nesterov } 187dbb5afadSOleg Nesterov 188*31cae1eaSPeter Zijlstra /* 189*31cae1eaSPeter Zijlstra * Ensure that nothing can wake it up, even SIGKILL 190*31cae1eaSPeter Zijlstra * 191*31cae1eaSPeter Zijlstra * A task is switched to this state while a ptrace operation is in progress; 192*31cae1eaSPeter Zijlstra * such that the ptrace operation is uninterruptible. 193*31cae1eaSPeter Zijlstra */ 1949899d11fSOleg Nesterov static bool ptrace_freeze_traced(struct task_struct *task) 1959899d11fSOleg Nesterov { 1969899d11fSOleg Nesterov bool ret = false; 1979899d11fSOleg Nesterov 1989899d11fSOleg Nesterov /* Lockless, nobody but us can set this flag */ 1999899d11fSOleg Nesterov if (task->jobctl & JOBCTL_LISTENING) 2009899d11fSOleg Nesterov return ret; 2019899d11fSOleg Nesterov 2029899d11fSOleg Nesterov spin_lock_irq(&task->sighand->siglock); 203dbb5afadSOleg Nesterov if (task_is_traced(task) && !looks_like_a_spurious_pid(task) && 204dbb5afadSOleg Nesterov !__fatal_signal_pending(task)) { 2052500ad1cSEric W. Biederman task->jobctl |= JOBCTL_PTRACE_FROZEN; 2069899d11fSOleg Nesterov ret = true; 2079899d11fSOleg Nesterov } 2089899d11fSOleg Nesterov spin_unlock_irq(&task->sighand->siglock); 2099899d11fSOleg Nesterov 2109899d11fSOleg Nesterov return ret; 2119899d11fSOleg Nesterov } 2129899d11fSOleg Nesterov 2139899d11fSOleg Nesterov static void ptrace_unfreeze_traced(struct task_struct *task) 2149899d11fSOleg Nesterov { 2152500ad1cSEric W. Biederman unsigned long flags; 2169899d11fSOleg Nesterov 2175402e97aSbsegall@google.com /* 2182500ad1cSEric W. Biederman * The child may be awake and may have cleared 2192500ad1cSEric W. Biederman * JOBCTL_PTRACE_FROZEN (see ptrace_resume). The child will 2202500ad1cSEric W. Biederman * not set JOBCTL_PTRACE_FROZEN or enter __TASK_TRACED anew. 2215402e97aSbsegall@google.com */ 2222500ad1cSEric W. Biederman if (lock_task_sighand(task, &flags)) { 2232500ad1cSEric W. Biederman task->jobctl &= ~JOBCTL_PTRACE_FROZEN; 224*31cae1eaSPeter Zijlstra if (__fatal_signal_pending(task)) { 225*31cae1eaSPeter Zijlstra task->jobctl &= ~TASK_TRACED; 2269899d11fSOleg Nesterov wake_up_state(task, __TASK_TRACED); 227*31cae1eaSPeter Zijlstra } 2282500ad1cSEric W. Biederman unlock_task_sighand(task, &flags); 2295402e97aSbsegall@google.com } 2309899d11fSOleg Nesterov } 2319899d11fSOleg Nesterov 232755e276bSTejun Heo /** 233755e276bSTejun Heo * ptrace_check_attach - check whether ptracee is ready for ptrace operation 234755e276bSTejun Heo * @child: ptracee to check for 235755e276bSTejun Heo * @ignore_state: don't check whether @child is currently %TASK_TRACED 236755e276bSTejun Heo * 237755e276bSTejun Heo * Check whether @child is being ptraced by %current and ready for further 238755e276bSTejun Heo * ptrace operations. If @ignore_state is %false, @child also should be in 239755e276bSTejun Heo * %TASK_TRACED state and on return the child is guaranteed to be traced 240755e276bSTejun Heo * and not executing. If @ignore_state is %true, @child can be in any 241755e276bSTejun Heo * state. 242755e276bSTejun Heo * 243755e276bSTejun Heo * CONTEXT: 244755e276bSTejun Heo * Grabs and releases tasklist_lock and @child->sighand->siglock. 245755e276bSTejun Heo * 246755e276bSTejun Heo * RETURNS: 247755e276bSTejun Heo * 0 on success, -ESRCH if %child is not ready. 2481da177e4SLinus Torvalds */ 249edea0d03SOleg Nesterov static int ptrace_check_attach(struct task_struct *child, bool ignore_state) 2501da177e4SLinus Torvalds { 2511da177e4SLinus Torvalds int ret = -ESRCH; 2521da177e4SLinus Torvalds 2531da177e4SLinus Torvalds /* 2541da177e4SLinus Torvalds * We take the read lock around doing both checks to close a 2551da177e4SLinus Torvalds * possible race where someone else was tracing our child and 2561da177e4SLinus Torvalds * detached between these two checks. After this locked check, 2571da177e4SLinus Torvalds * we are sure that this is our traced child and that can only 2581da177e4SLinus Torvalds * be changed by us so it's not changing right after this. 2591da177e4SLinus Torvalds */ 2601da177e4SLinus Torvalds read_lock(&tasklist_lock); 2619899d11fSOleg Nesterov if (child->ptrace && child->parent == current) { 262c0c0b649SOleg Nesterov /* 263c0c0b649SOleg Nesterov * child->sighand can't be NULL, release_task() 264c0c0b649SOleg Nesterov * does ptrace_unlink() before __exit_signal(). 265c0c0b649SOleg Nesterov */ 2669899d11fSOleg Nesterov if (ignore_state || ptrace_freeze_traced(child)) 267321fb561SOleg Nesterov ret = 0; 2681da177e4SLinus Torvalds } 2691da177e4SLinus Torvalds read_unlock(&tasklist_lock); 2701da177e4SLinus Torvalds 2717b0fe136SEric W. Biederman if (!ret && !ignore_state && 2727b0fe136SEric W. Biederman WARN_ON_ONCE(!wait_task_inactive(child, __TASK_TRACED))) 2739899d11fSOleg Nesterov ret = -ESRCH; 2741da177e4SLinus Torvalds 2751da177e4SLinus Torvalds return ret; 2761da177e4SLinus Torvalds } 2771da177e4SLinus Torvalds 278cf237052SMickaël Salaün static bool ptrace_has_cap(struct user_namespace *ns, unsigned int mode) 27969f594a3SEric Paris { 28069f594a3SEric Paris if (mode & PTRACE_MODE_NOAUDIT) 281cf237052SMickaël Salaün return ns_capable_noaudit(ns, CAP_SYS_PTRACE); 282cf237052SMickaël Salaün return ns_capable(ns, CAP_SYS_PTRACE); 28369f594a3SEric Paris } 28469f594a3SEric Paris 2859f99798fSTetsuo Handa /* Returns 0 on success, -errno on denial. */ 2869f99798fSTetsuo Handa static int __ptrace_may_access(struct task_struct *task, unsigned int mode) 287ab8d11beSMiklos Szeredi { 288c69e8d9cSDavid Howells const struct cred *cred = current_cred(), *tcred; 289bfedb589SEric W. Biederman struct mm_struct *mm; 290caaee623SJann Horn kuid_t caller_uid; 291caaee623SJann Horn kgid_t caller_gid; 292caaee623SJann Horn 293caaee623SJann Horn if (!(mode & PTRACE_MODE_FSCREDS) == !(mode & PTRACE_MODE_REALCREDS)) { 294caaee623SJann Horn WARN(1, "denying ptrace access check without PTRACE_MODE_*CREDS\n"); 295caaee623SJann Horn return -EPERM; 296caaee623SJann Horn } 297b6dff3ecSDavid Howells 298df26c40eSEric W. Biederman /* May we inspect the given task? 299df26c40eSEric W. Biederman * This check is used both for attaching with ptrace 300df26c40eSEric W. Biederman * and for allowing access to sensitive information in /proc. 301df26c40eSEric W. Biederman * 302df26c40eSEric W. Biederman * ptrace_attach denies several cases that /proc allows 303df26c40eSEric W. Biederman * because setting up the necessary parent/child relationship 304df26c40eSEric W. Biederman * or halting the specified task is impossible. 305df26c40eSEric W. Biederman */ 306caaee623SJann Horn 307df26c40eSEric W. Biederman /* Don't let security modules deny introspection */ 30873af963fSMark Grondona if (same_thread_group(task, current)) 309df26c40eSEric W. Biederman return 0; 310c69e8d9cSDavid Howells rcu_read_lock(); 311caaee623SJann Horn if (mode & PTRACE_MODE_FSCREDS) { 312caaee623SJann Horn caller_uid = cred->fsuid; 313caaee623SJann Horn caller_gid = cred->fsgid; 314caaee623SJann Horn } else { 315caaee623SJann Horn /* 316caaee623SJann Horn * Using the euid would make more sense here, but something 317caaee623SJann Horn * in userland might rely on the old behavior, and this 318caaee623SJann Horn * shouldn't be a security problem since 319caaee623SJann Horn * PTRACE_MODE_REALCREDS implies that the caller explicitly 320caaee623SJann Horn * used a syscall that requests access to another process 321caaee623SJann Horn * (and not a filesystem syscall to procfs). 322caaee623SJann Horn */ 323caaee623SJann Horn caller_uid = cred->uid; 324caaee623SJann Horn caller_gid = cred->gid; 325caaee623SJann Horn } 326c69e8d9cSDavid Howells tcred = __task_cred(task); 327caaee623SJann Horn if (uid_eq(caller_uid, tcred->euid) && 328caaee623SJann Horn uid_eq(caller_uid, tcred->suid) && 329caaee623SJann Horn uid_eq(caller_uid, tcred->uid) && 330caaee623SJann Horn gid_eq(caller_gid, tcred->egid) && 331caaee623SJann Horn gid_eq(caller_gid, tcred->sgid) && 332caaee623SJann Horn gid_eq(caller_gid, tcred->gid)) 3338409cca7SSerge E. Hallyn goto ok; 334cf237052SMickaël Salaün if (ptrace_has_cap(tcred->user_ns, mode)) 3358409cca7SSerge E. Hallyn goto ok; 336c69e8d9cSDavid Howells rcu_read_unlock(); 337ab8d11beSMiklos Szeredi return -EPERM; 3388409cca7SSerge E. Hallyn ok: 339c69e8d9cSDavid Howells rcu_read_unlock(); 340f6581f5bSJann Horn /* 341f6581f5bSJann Horn * If a task drops privileges and becomes nondumpable (through a syscall 342f6581f5bSJann Horn * like setresuid()) while we are trying to access it, we must ensure 343f6581f5bSJann Horn * that the dumpability is read after the credentials; otherwise, 344f6581f5bSJann Horn * we may be able to attach to a task that we shouldn't be able to 345f6581f5bSJann Horn * attach to (as if the task had dropped privileges without becoming 346f6581f5bSJann Horn * nondumpable). 347f6581f5bSJann Horn * Pairs with a write barrier in commit_creds(). 348f6581f5bSJann Horn */ 349f6581f5bSJann Horn smp_rmb(); 350bfedb589SEric W. Biederman mm = task->mm; 351bfedb589SEric W. Biederman if (mm && 352bfedb589SEric W. Biederman ((get_dumpable(mm) != SUID_DUMP_USER) && 353cf237052SMickaël Salaün !ptrace_has_cap(mm->user_ns, mode))) 354ab8d11beSMiklos Szeredi return -EPERM; 355ab8d11beSMiklos Szeredi 3569e48858fSIngo Molnar return security_ptrace_access_check(task, mode); 357ab8d11beSMiklos Szeredi } 358ab8d11beSMiklos Szeredi 359006ebb40SStephen Smalley bool ptrace_may_access(struct task_struct *task, unsigned int mode) 360ab8d11beSMiklos Szeredi { 361ab8d11beSMiklos Szeredi int err; 362ab8d11beSMiklos Szeredi task_lock(task); 363006ebb40SStephen Smalley err = __ptrace_may_access(task, mode); 364ab8d11beSMiklos Szeredi task_unlock(task); 3653a709703SRoland McGrath return !err; 366ab8d11beSMiklos Szeredi } 367ab8d11beSMiklos Szeredi 368ee1fee90SJann Horn static int check_ptrace_options(unsigned long data) 369ee1fee90SJann Horn { 370ee1fee90SJann Horn if (data & ~(unsigned long)PTRACE_O_MASK) 371ee1fee90SJann Horn return -EINVAL; 372ee1fee90SJann Horn 373ee1fee90SJann Horn if (unlikely(data & PTRACE_O_SUSPEND_SECCOMP)) { 374ee1fee90SJann Horn if (!IS_ENABLED(CONFIG_CHECKPOINT_RESTORE) || 375ee1fee90SJann Horn !IS_ENABLED(CONFIG_SECCOMP)) 376ee1fee90SJann Horn return -EINVAL; 377ee1fee90SJann Horn 378ee1fee90SJann Horn if (!capable(CAP_SYS_ADMIN)) 379ee1fee90SJann Horn return -EPERM; 380ee1fee90SJann Horn 381ee1fee90SJann Horn if (seccomp_mode(¤t->seccomp) != SECCOMP_MODE_DISABLED || 382ee1fee90SJann Horn current->ptrace & PT_SUSPEND_SECCOMP) 383ee1fee90SJann Horn return -EPERM; 384ee1fee90SJann Horn } 385ee1fee90SJann Horn return 0; 386ee1fee90SJann Horn } 387ee1fee90SJann Horn 3883544d72aSTejun Heo static int ptrace_attach(struct task_struct *task, long request, 389aa9147c9SDenys Vlasenko unsigned long addr, 3903544d72aSTejun Heo unsigned long flags) 3911da177e4SLinus Torvalds { 3923544d72aSTejun Heo bool seize = (request == PTRACE_SEIZE); 3931da177e4SLinus Torvalds int retval; 394f5b40e36SLinus Torvalds 3953544d72aSTejun Heo retval = -EIO; 396aa9147c9SDenys Vlasenko if (seize) { 397aa9147c9SDenys Vlasenko if (addr != 0) 3983544d72aSTejun Heo goto out; 399ee1fee90SJann Horn /* 400ee1fee90SJann Horn * This duplicates the check in check_ptrace_options() because 401ee1fee90SJann Horn * ptrace_attach() and ptrace_setoptions() have historically 402ee1fee90SJann Horn * used different error codes for unknown ptrace options. 403ee1fee90SJann Horn */ 404aa9147c9SDenys Vlasenko if (flags & ~(unsigned long)PTRACE_O_MASK) 405aa9147c9SDenys Vlasenko goto out; 406ee1fee90SJann Horn retval = check_ptrace_options(flags); 407ee1fee90SJann Horn if (retval) 408ee1fee90SJann Horn return retval; 409aa9147c9SDenys Vlasenko flags = PT_PTRACED | PT_SEIZED | (flags << PT_OPT_FLAG_SHIFT); 410aa9147c9SDenys Vlasenko } else { 411aa9147c9SDenys Vlasenko flags = PT_PTRACED; 412aa9147c9SDenys Vlasenko } 4133544d72aSTejun Heo 414a5cb013dSAl Viro audit_ptrace(task); 415a5cb013dSAl Viro 4161da177e4SLinus Torvalds retval = -EPERM; 417e8b33b8cSJens Axboe if (unlikely(task->flags & PF_KTHREAD)) 418b79b7ba9SOleg Nesterov goto out; 419bac0abd6SPavel Emelyanov if (same_thread_group(task, current)) 420f5b40e36SLinus Torvalds goto out; 421f5b40e36SLinus Torvalds 422f2f0b00aSOleg Nesterov /* 423f2f0b00aSOleg Nesterov * Protect exec's credential calculations against our interference; 42486b6c1f3SDenys Vlasenko * SUID, SGID and LSM creds get determined differently 4255e751e99SDavid Howells * under ptrace. 426d84f4f99SDavid Howells */ 427793285fcSOleg Nesterov retval = -ERESTARTNOINTR; 4289b1bf12dSKOSAKI Motohiro if (mutex_lock_interruptible(&task->signal->cred_guard_mutex)) 429d84f4f99SDavid Howells goto out; 4304b105cbbSOleg Nesterov 431f5b40e36SLinus Torvalds task_lock(task); 432caaee623SJann Horn retval = __ptrace_may_access(task, PTRACE_MODE_ATTACH_REALCREDS); 4334b105cbbSOleg Nesterov task_unlock(task); 4341da177e4SLinus Torvalds if (retval) 4354b105cbbSOleg Nesterov goto unlock_creds; 4361da177e4SLinus Torvalds 4374b105cbbSOleg Nesterov write_lock_irq(&tasklist_lock); 438b79b7ba9SOleg Nesterov retval = -EPERM; 439b79b7ba9SOleg Nesterov if (unlikely(task->exit_state)) 4404b105cbbSOleg Nesterov goto unlock_tasklist; 441f2f0b00aSOleg Nesterov if (task->ptrace) 4424b105cbbSOleg Nesterov goto unlock_tasklist; 443b79b7ba9SOleg Nesterov 444aa9147c9SDenys Vlasenko task->ptrace = flags; 4451da177e4SLinus Torvalds 446c70d9d80SEric W. Biederman ptrace_link(task, current); 4473544d72aSTejun Heo 4483544d72aSTejun Heo /* SEIZE doesn't trap tracee on attach */ 4493544d72aSTejun Heo if (!seize) 450079b22dcSEric W. Biederman send_sig_info(SIGSTOP, SEND_SIG_PRIV, task); 451b79b7ba9SOleg Nesterov 452d79fdd6dSTejun Heo spin_lock(&task->sighand->siglock); 453d79fdd6dSTejun Heo 454d79fdd6dSTejun Heo /* 45573ddff2bSTejun Heo * If the task is already STOPPED, set JOBCTL_TRAP_STOP and 456d79fdd6dSTejun Heo * TRAPPING, and kick it so that it transits to TRACED. TRAPPING 457d79fdd6dSTejun Heo * will be cleared if the child completes the transition or any 458d79fdd6dSTejun Heo * event which clears the group stop states happens. We'll wait 459d79fdd6dSTejun Heo * for the transition to complete before returning from this 460d79fdd6dSTejun Heo * function. 461d79fdd6dSTejun Heo * 462d79fdd6dSTejun Heo * This hides STOPPED -> RUNNING -> TRACED transition from the 463d79fdd6dSTejun Heo * attaching thread but a different thread in the same group can 464d79fdd6dSTejun Heo * still observe the transient RUNNING state. IOW, if another 465d79fdd6dSTejun Heo * thread's WNOHANG wait(2) on the stopped tracee races against 466d79fdd6dSTejun Heo * ATTACH, the wait(2) may fail due to the transient RUNNING. 467d79fdd6dSTejun Heo * 468d79fdd6dSTejun Heo * The following task_is_stopped() test is safe as both transitions 469d79fdd6dSTejun Heo * in and out of STOPPED are protected by siglock. 470d79fdd6dSTejun Heo */ 4717dd3db54STejun Heo if (task_is_stopped(task) && 472*31cae1eaSPeter Zijlstra task_set_jobctl_pending(task, JOBCTL_TRAP_STOP | JOBCTL_TRAPPING)) { 473*31cae1eaSPeter Zijlstra task->jobctl &= ~JOBCTL_STOPPED; 474910ffdb1SOleg Nesterov signal_wake_up_state(task, __TASK_STOPPED); 475*31cae1eaSPeter Zijlstra } 476d79fdd6dSTejun Heo 477d79fdd6dSTejun Heo spin_unlock(&task->sighand->siglock); 478d79fdd6dSTejun Heo 479b79b7ba9SOleg Nesterov retval = 0; 4804b105cbbSOleg Nesterov unlock_tasklist: 4814b105cbbSOleg Nesterov write_unlock_irq(&tasklist_lock); 4824b105cbbSOleg Nesterov unlock_creds: 4839b1bf12dSKOSAKI Motohiro mutex_unlock(&task->signal->cred_guard_mutex); 484f5b40e36SLinus Torvalds out: 485f701e5b7SVladimir Zapolskiy if (!retval) { 4867c3b00e0SOleg Nesterov /* 4877c3b00e0SOleg Nesterov * We do not bother to change retval or clear JOBCTL_TRAPPING 4887c3b00e0SOleg Nesterov * if wait_on_bit() was interrupted by SIGKILL. The tracer will 4897c3b00e0SOleg Nesterov * not return to user-mode, it will exit and clear this bit in 4907c3b00e0SOleg Nesterov * __ptrace_unlink() if it wasn't already cleared by the tracee; 4917c3b00e0SOleg Nesterov * and until then nobody can ptrace this task. 4927c3b00e0SOleg Nesterov */ 4937c3b00e0SOleg Nesterov wait_on_bit(&task->jobctl, JOBCTL_TRAPPING_BIT, TASK_KILLABLE); 494f701e5b7SVladimir Zapolskiy proc_ptrace_connector(task, PTRACE_ATTACH); 495f701e5b7SVladimir Zapolskiy } 496f701e5b7SVladimir Zapolskiy 4971da177e4SLinus Torvalds return retval; 4981da177e4SLinus Torvalds } 4991da177e4SLinus Torvalds 500f2f0b00aSOleg Nesterov /** 501f2f0b00aSOleg Nesterov * ptrace_traceme -- helper for PTRACE_TRACEME 502f2f0b00aSOleg Nesterov * 503f2f0b00aSOleg Nesterov * Performs checks and sets PT_PTRACED. 504f2f0b00aSOleg Nesterov * Should be used by all ptrace implementations for PTRACE_TRACEME. 505f2f0b00aSOleg Nesterov */ 506e3e89cc5SLinus Torvalds static int ptrace_traceme(void) 507f2f0b00aSOleg Nesterov { 508f2f0b00aSOleg Nesterov int ret = -EPERM; 509f2f0b00aSOleg Nesterov 5104b105cbbSOleg Nesterov write_lock_irq(&tasklist_lock); 5114b105cbbSOleg Nesterov /* Are we already being traced? */ 512f2f0b00aSOleg Nesterov if (!current->ptrace) { 513f2f0b00aSOleg Nesterov ret = security_ptrace_traceme(current->parent); 514f2f0b00aSOleg Nesterov /* 515f2f0b00aSOleg Nesterov * Check PF_EXITING to ensure ->real_parent has not passed 516f2f0b00aSOleg Nesterov * exit_ptrace(). Otherwise we don't report the error but 517f2f0b00aSOleg Nesterov * pretend ->real_parent untraces us right after return. 518f2f0b00aSOleg Nesterov */ 519f2f0b00aSOleg Nesterov if (!ret && !(current->real_parent->flags & PF_EXITING)) { 520f2f0b00aSOleg Nesterov current->ptrace = PT_PTRACED; 521c70d9d80SEric W. Biederman ptrace_link(current, current->real_parent); 522f2f0b00aSOleg Nesterov } 523f2f0b00aSOleg Nesterov } 5244b105cbbSOleg Nesterov write_unlock_irq(&tasklist_lock); 5254b105cbbSOleg Nesterov 526f2f0b00aSOleg Nesterov return ret; 527f2f0b00aSOleg Nesterov } 528f2f0b00aSOleg Nesterov 52939c626aeSOleg Nesterov /* 53039c626aeSOleg Nesterov * Called with irqs disabled, returns true if childs should reap themselves. 53139c626aeSOleg Nesterov */ 53239c626aeSOleg Nesterov static int ignoring_children(struct sighand_struct *sigh) 53339c626aeSOleg Nesterov { 53439c626aeSOleg Nesterov int ret; 53539c626aeSOleg Nesterov spin_lock(&sigh->siglock); 53639c626aeSOleg Nesterov ret = (sigh->action[SIGCHLD-1].sa.sa_handler == SIG_IGN) || 53739c626aeSOleg Nesterov (sigh->action[SIGCHLD-1].sa.sa_flags & SA_NOCLDWAIT); 53839c626aeSOleg Nesterov spin_unlock(&sigh->siglock); 53939c626aeSOleg Nesterov return ret; 54039c626aeSOleg Nesterov } 54139c626aeSOleg Nesterov 54239c626aeSOleg Nesterov /* 54339c626aeSOleg Nesterov * Called with tasklist_lock held for writing. 54439c626aeSOleg Nesterov * Unlink a traced task, and clean it up if it was a traced zombie. 54539c626aeSOleg Nesterov * Return true if it needs to be reaped with release_task(). 54639c626aeSOleg Nesterov * (We can't call release_task() here because we already hold tasklist_lock.) 54739c626aeSOleg Nesterov * 54839c626aeSOleg Nesterov * If it's a zombie, our attachedness prevented normal parent notification 54939c626aeSOleg Nesterov * or self-reaping. Do notification now if it would have happened earlier. 55039c626aeSOleg Nesterov * If it should reap itself, return true. 55139c626aeSOleg Nesterov * 552a7f0765eSOleg Nesterov * If it's our own child, there is no notification to do. But if our normal 553a7f0765eSOleg Nesterov * children self-reap, then this child was prevented by ptrace and we must 554a7f0765eSOleg Nesterov * reap it now, in that case we must also wake up sub-threads sleeping in 555a7f0765eSOleg Nesterov * do_wait(). 55639c626aeSOleg Nesterov */ 55739c626aeSOleg Nesterov static bool __ptrace_detach(struct task_struct *tracer, struct task_struct *p) 55839c626aeSOleg Nesterov { 5599843a1e9SOleg Nesterov bool dead; 5609843a1e9SOleg Nesterov 56139c626aeSOleg Nesterov __ptrace_unlink(p); 56239c626aeSOleg Nesterov 5639843a1e9SOleg Nesterov if (p->exit_state != EXIT_ZOMBIE) 5649843a1e9SOleg Nesterov return false; 5659843a1e9SOleg Nesterov 5669843a1e9SOleg Nesterov dead = !thread_group_leader(p); 5679843a1e9SOleg Nesterov 5689843a1e9SOleg Nesterov if (!dead && thread_group_empty(p)) { 56939c626aeSOleg Nesterov if (!same_thread_group(p->real_parent, tracer)) 5709843a1e9SOleg Nesterov dead = do_notify_parent(p, p->exit_signal); 571a7f0765eSOleg Nesterov else if (ignoring_children(tracer->sighand)) { 572a7f0765eSOleg Nesterov __wake_up_parent(p, tracer); 5739843a1e9SOleg Nesterov dead = true; 57439c626aeSOleg Nesterov } 575a7f0765eSOleg Nesterov } 57639c626aeSOleg Nesterov /* Mark it as in the process of being reaped. */ 5779843a1e9SOleg Nesterov if (dead) 57839c626aeSOleg Nesterov p->exit_state = EXIT_DEAD; 5799843a1e9SOleg Nesterov return dead; 58039c626aeSOleg Nesterov } 58139c626aeSOleg Nesterov 582e3e89cc5SLinus Torvalds static int ptrace_detach(struct task_struct *child, unsigned int data) 5831da177e4SLinus Torvalds { 5847ed20e1aSJesper Juhl if (!valid_signal(data)) 5851da177e4SLinus Torvalds return -EIO; 5861da177e4SLinus Torvalds 5871da177e4SLinus Torvalds /* Architecture-specific hardware disable .. */ 5881da177e4SLinus Torvalds ptrace_disable(child); 5891da177e4SLinus Torvalds 59095c3eb76SOleg Nesterov write_lock_irq(&tasklist_lock); 59139c626aeSOleg Nesterov /* 59264a4096cSOleg Nesterov * We rely on ptrace_freeze_traced(). It can't be killed and 59364a4096cSOleg Nesterov * untraced by another thread, it can't be a zombie. 59439c626aeSOleg Nesterov */ 59564a4096cSOleg Nesterov WARN_ON(!child->ptrace || child->exit_state); 59664a4096cSOleg Nesterov /* 59764a4096cSOleg Nesterov * tasklist_lock avoids the race with wait_task_stopped(), see 59864a4096cSOleg Nesterov * the comment in ptrace_resume(). 59964a4096cSOleg Nesterov */ 60095c3eb76SOleg Nesterov child->exit_code = data; 60164a4096cSOleg Nesterov __ptrace_detach(current, child); 6021da177e4SLinus Torvalds write_unlock_irq(&tasklist_lock); 6031da177e4SLinus Torvalds 604f701e5b7SVladimir Zapolskiy proc_ptrace_connector(child, PTRACE_DETACH); 6054576145cSOleg Nesterov 6061da177e4SLinus Torvalds return 0; 6071da177e4SLinus Torvalds } 6081da177e4SLinus Torvalds 60939c626aeSOleg Nesterov /* 610c7e49c14SOleg Nesterov * Detach all tasks we were using ptrace on. Called with tasklist held 6117c8bd232SOleg Nesterov * for writing. 61239c626aeSOleg Nesterov */ 6137c8bd232SOleg Nesterov void exit_ptrace(struct task_struct *tracer, struct list_head *dead) 61439c626aeSOleg Nesterov { 61539c626aeSOleg Nesterov struct task_struct *p, *n; 616c7e49c14SOleg Nesterov 61739c626aeSOleg Nesterov list_for_each_entry_safe(p, n, &tracer->ptraced, ptrace_entry) { 618992fb6e1SOleg Nesterov if (unlikely(p->ptrace & PT_EXITKILL)) 619079b22dcSEric W. Biederman send_sig_info(SIGKILL, SEND_SIG_PRIV, p); 620992fb6e1SOleg Nesterov 62139c626aeSOleg Nesterov if (__ptrace_detach(tracer, p)) 6227c8bd232SOleg Nesterov list_add(&p->ptrace_entry, dead); 62339c626aeSOleg Nesterov } 62439c626aeSOleg Nesterov } 62539c626aeSOleg Nesterov 6261da177e4SLinus Torvalds int ptrace_readdata(struct task_struct *tsk, unsigned long src, char __user *dst, int len) 6271da177e4SLinus Torvalds { 6281da177e4SLinus Torvalds int copied = 0; 6291da177e4SLinus Torvalds 6301da177e4SLinus Torvalds while (len > 0) { 6311da177e4SLinus Torvalds char buf[128]; 6321da177e4SLinus Torvalds int this_len, retval; 6331da177e4SLinus Torvalds 6341da177e4SLinus Torvalds this_len = (len > sizeof(buf)) ? sizeof(buf) : len; 63584d77d3fSEric W. Biederman retval = ptrace_access_vm(tsk, src, buf, this_len, FOLL_FORCE); 63684d77d3fSEric W. Biederman 6371da177e4SLinus Torvalds if (!retval) { 6381da177e4SLinus Torvalds if (copied) 6391da177e4SLinus Torvalds break; 6401da177e4SLinus Torvalds return -EIO; 6411da177e4SLinus Torvalds } 6421da177e4SLinus Torvalds if (copy_to_user(dst, buf, retval)) 6431da177e4SLinus Torvalds return -EFAULT; 6441da177e4SLinus Torvalds copied += retval; 6451da177e4SLinus Torvalds src += retval; 6461da177e4SLinus Torvalds dst += retval; 6471da177e4SLinus Torvalds len -= retval; 6481da177e4SLinus Torvalds } 6491da177e4SLinus Torvalds return copied; 6501da177e4SLinus Torvalds } 6511da177e4SLinus Torvalds 6521da177e4SLinus Torvalds int ptrace_writedata(struct task_struct *tsk, char __user *src, unsigned long dst, int len) 6531da177e4SLinus Torvalds { 6541da177e4SLinus Torvalds int copied = 0; 6551da177e4SLinus Torvalds 6561da177e4SLinus Torvalds while (len > 0) { 6571da177e4SLinus Torvalds char buf[128]; 6581da177e4SLinus Torvalds int this_len, retval; 6591da177e4SLinus Torvalds 6601da177e4SLinus Torvalds this_len = (len > sizeof(buf)) ? sizeof(buf) : len; 6611da177e4SLinus Torvalds if (copy_from_user(buf, src, this_len)) 6621da177e4SLinus Torvalds return -EFAULT; 66384d77d3fSEric W. Biederman retval = ptrace_access_vm(tsk, dst, buf, this_len, 664f307ab6dSLorenzo Stoakes FOLL_FORCE | FOLL_WRITE); 6651da177e4SLinus Torvalds if (!retval) { 6661da177e4SLinus Torvalds if (copied) 6671da177e4SLinus Torvalds break; 6681da177e4SLinus Torvalds return -EIO; 6691da177e4SLinus Torvalds } 6701da177e4SLinus Torvalds copied += retval; 6711da177e4SLinus Torvalds src += retval; 6721da177e4SLinus Torvalds dst += retval; 6731da177e4SLinus Torvalds len -= retval; 6741da177e4SLinus Torvalds } 6751da177e4SLinus Torvalds return copied; 6761da177e4SLinus Torvalds } 6771da177e4SLinus Torvalds 6784abf9869SNamhyung Kim static int ptrace_setoptions(struct task_struct *child, unsigned long data) 6791da177e4SLinus Torvalds { 68086b6c1f3SDenys Vlasenko unsigned flags; 681ee1fee90SJann Horn int ret; 68286b6c1f3SDenys Vlasenko 683ee1fee90SJann Horn ret = check_ptrace_options(data); 684ee1fee90SJann Horn if (ret) 685ee1fee90SJann Horn return ret; 68613c4a901STycho Andersen 68786b6c1f3SDenys Vlasenko /* Avoid intermediate state when all opts are cleared */ 68886b6c1f3SDenys Vlasenko flags = child->ptrace; 68986b6c1f3SDenys Vlasenko flags &= ~(PTRACE_O_MASK << PT_OPT_FLAG_SHIFT); 69086b6c1f3SDenys Vlasenko flags |= (data << PT_OPT_FLAG_SHIFT); 69186b6c1f3SDenys Vlasenko child->ptrace = flags; 6921da177e4SLinus Torvalds 6938c5cf9e5SDenys Vlasenko return 0; 6941da177e4SLinus Torvalds } 6951da177e4SLinus Torvalds 696ae7795bcSEric W. Biederman static int ptrace_getsiginfo(struct task_struct *child, kernel_siginfo_t *info) 6971da177e4SLinus Torvalds { 698e4961254SOleg Nesterov unsigned long flags; 6991da177e4SLinus Torvalds int error = -ESRCH; 7001da177e4SLinus Torvalds 701e4961254SOleg Nesterov if (lock_task_sighand(child, &flags)) { 7021da177e4SLinus Torvalds error = -EINVAL; 7031da177e4SLinus Torvalds if (likely(child->last_siginfo != NULL)) { 7040752d7bfSEric W. Biederman copy_siginfo(info, child->last_siginfo); 7051da177e4SLinus Torvalds error = 0; 7061da177e4SLinus Torvalds } 707e4961254SOleg Nesterov unlock_task_sighand(child, &flags); 7081da177e4SLinus Torvalds } 7091da177e4SLinus Torvalds return error; 7101da177e4SLinus Torvalds } 7111da177e4SLinus Torvalds 712ae7795bcSEric W. Biederman static int ptrace_setsiginfo(struct task_struct *child, const kernel_siginfo_t *info) 7131da177e4SLinus Torvalds { 714e4961254SOleg Nesterov unsigned long flags; 7151da177e4SLinus Torvalds int error = -ESRCH; 7161da177e4SLinus Torvalds 717e4961254SOleg Nesterov if (lock_task_sighand(child, &flags)) { 7181da177e4SLinus Torvalds error = -EINVAL; 7191da177e4SLinus Torvalds if (likely(child->last_siginfo != NULL)) { 7200752d7bfSEric W. Biederman copy_siginfo(child->last_siginfo, info); 7211da177e4SLinus Torvalds error = 0; 7221da177e4SLinus Torvalds } 723e4961254SOleg Nesterov unlock_task_sighand(child, &flags); 7241da177e4SLinus Torvalds } 7251da177e4SLinus Torvalds return error; 7261da177e4SLinus Torvalds } 7271da177e4SLinus Torvalds 72884c751bdSAndrey Vagin static int ptrace_peek_siginfo(struct task_struct *child, 72984c751bdSAndrey Vagin unsigned long addr, 73084c751bdSAndrey Vagin unsigned long data) 73184c751bdSAndrey Vagin { 73284c751bdSAndrey Vagin struct ptrace_peeksiginfo_args arg; 73384c751bdSAndrey Vagin struct sigpending *pending; 73484c751bdSAndrey Vagin struct sigqueue *q; 73584c751bdSAndrey Vagin int ret, i; 73684c751bdSAndrey Vagin 73784c751bdSAndrey Vagin ret = copy_from_user(&arg, (void __user *) addr, 73884c751bdSAndrey Vagin sizeof(struct ptrace_peeksiginfo_args)); 73984c751bdSAndrey Vagin if (ret) 74084c751bdSAndrey Vagin return -EFAULT; 74184c751bdSAndrey Vagin 74284c751bdSAndrey Vagin if (arg.flags & ~PTRACE_PEEKSIGINFO_SHARED) 74384c751bdSAndrey Vagin return -EINVAL; /* unknown flags */ 74484c751bdSAndrey Vagin 74584c751bdSAndrey Vagin if (arg.nr < 0) 74684c751bdSAndrey Vagin return -EINVAL; 74784c751bdSAndrey Vagin 748f6e2aa91SEric W. Biederman /* Ensure arg.off fits in an unsigned long */ 749f6e2aa91SEric W. Biederman if (arg.off > ULONG_MAX) 750f6e2aa91SEric W. Biederman return 0; 751f6e2aa91SEric W. Biederman 75284c751bdSAndrey Vagin if (arg.flags & PTRACE_PEEKSIGINFO_SHARED) 75384c751bdSAndrey Vagin pending = &child->signal->shared_pending; 75484c751bdSAndrey Vagin else 75584c751bdSAndrey Vagin pending = &child->pending; 75684c751bdSAndrey Vagin 75784c751bdSAndrey Vagin for (i = 0; i < arg.nr; ) { 758ae7795bcSEric W. Biederman kernel_siginfo_t info; 759f6e2aa91SEric W. Biederman unsigned long off = arg.off + i; 760f6e2aa91SEric W. Biederman bool found = false; 76184c751bdSAndrey Vagin 76284c751bdSAndrey Vagin spin_lock_irq(&child->sighand->siglock); 76384c751bdSAndrey Vagin list_for_each_entry(q, &pending->list, list) { 76484c751bdSAndrey Vagin if (!off--) { 765f6e2aa91SEric W. Biederman found = true; 76684c751bdSAndrey Vagin copy_siginfo(&info, &q->info); 76784c751bdSAndrey Vagin break; 76884c751bdSAndrey Vagin } 76984c751bdSAndrey Vagin } 77084c751bdSAndrey Vagin spin_unlock_irq(&child->sighand->siglock); 77184c751bdSAndrey Vagin 772f6e2aa91SEric W. Biederman if (!found) /* beyond the end of the list */ 77384c751bdSAndrey Vagin break; 77484c751bdSAndrey Vagin 77584c751bdSAndrey Vagin #ifdef CONFIG_COMPAT 7765c465217SAndy Lutomirski if (unlikely(in_compat_syscall())) { 77784c751bdSAndrey Vagin compat_siginfo_t __user *uinfo = compat_ptr(data); 77884c751bdSAndrey Vagin 779cc731525SEric W. Biederman if (copy_siginfo_to_user32(uinfo, &info)) { 780706b23bdSMathieu Desnoyers ret = -EFAULT; 781706b23bdSMathieu Desnoyers break; 782706b23bdSMathieu Desnoyers } 783706b23bdSMathieu Desnoyers 78484c751bdSAndrey Vagin } else 78584c751bdSAndrey Vagin #endif 78684c751bdSAndrey Vagin { 78784c751bdSAndrey Vagin siginfo_t __user *uinfo = (siginfo_t __user *) data; 78884c751bdSAndrey Vagin 789cc731525SEric W. Biederman if (copy_siginfo_to_user(uinfo, &info)) { 79084c751bdSAndrey Vagin ret = -EFAULT; 79184c751bdSAndrey Vagin break; 79284c751bdSAndrey Vagin } 793706b23bdSMathieu Desnoyers } 79484c751bdSAndrey Vagin 79584c751bdSAndrey Vagin data += sizeof(siginfo_t); 79684c751bdSAndrey Vagin i++; 79784c751bdSAndrey Vagin 79884c751bdSAndrey Vagin if (signal_pending(current)) 79984c751bdSAndrey Vagin break; 80084c751bdSAndrey Vagin 80184c751bdSAndrey Vagin cond_resched(); 80284c751bdSAndrey Vagin } 80384c751bdSAndrey Vagin 80484c751bdSAndrey Vagin if (i > 0) 80584c751bdSAndrey Vagin return i; 80684c751bdSAndrey Vagin 80784c751bdSAndrey Vagin return ret; 80884c751bdSAndrey Vagin } 80936df29d7SRoland McGrath 81090f093faSPiotr Figiel #ifdef CONFIG_RSEQ 81190f093faSPiotr Figiel static long ptrace_get_rseq_configuration(struct task_struct *task, 81290f093faSPiotr Figiel unsigned long size, void __user *data) 81390f093faSPiotr Figiel { 81490f093faSPiotr Figiel struct ptrace_rseq_configuration conf = { 81590f093faSPiotr Figiel .rseq_abi_pointer = (u64)(uintptr_t)task->rseq, 81690f093faSPiotr Figiel .rseq_abi_size = sizeof(*task->rseq), 81790f093faSPiotr Figiel .signature = task->rseq_sig, 81890f093faSPiotr Figiel .flags = 0, 81990f093faSPiotr Figiel }; 82090f093faSPiotr Figiel 82190f093faSPiotr Figiel size = min_t(unsigned long, size, sizeof(conf)); 82290f093faSPiotr Figiel if (copy_to_user(data, &conf, size)) 82390f093faSPiotr Figiel return -EFAULT; 82490f093faSPiotr Figiel return sizeof(conf); 82590f093faSPiotr Figiel } 82690f093faSPiotr Figiel #endif 82790f093faSPiotr Figiel 82836df29d7SRoland McGrath #ifdef PTRACE_SINGLESTEP 82936df29d7SRoland McGrath #define is_singlestep(request) ((request) == PTRACE_SINGLESTEP) 83036df29d7SRoland McGrath #else 83136df29d7SRoland McGrath #define is_singlestep(request) 0 83236df29d7SRoland McGrath #endif 83336df29d7SRoland McGrath 8345b88abbfSRoland McGrath #ifdef PTRACE_SINGLEBLOCK 8355b88abbfSRoland McGrath #define is_singleblock(request) ((request) == PTRACE_SINGLEBLOCK) 8365b88abbfSRoland McGrath #else 8375b88abbfSRoland McGrath #define is_singleblock(request) 0 8385b88abbfSRoland McGrath #endif 8395b88abbfSRoland McGrath 84036df29d7SRoland McGrath #ifdef PTRACE_SYSEMU 84136df29d7SRoland McGrath #define is_sysemu_singlestep(request) ((request) == PTRACE_SYSEMU_SINGLESTEP) 84236df29d7SRoland McGrath #else 84336df29d7SRoland McGrath #define is_sysemu_singlestep(request) 0 84436df29d7SRoland McGrath #endif 84536df29d7SRoland McGrath 8464abf9869SNamhyung Kim static int ptrace_resume(struct task_struct *child, long request, 8474abf9869SNamhyung Kim unsigned long data) 84836df29d7SRoland McGrath { 84936df29d7SRoland McGrath if (!valid_signal(data)) 85036df29d7SRoland McGrath return -EIO; 85136df29d7SRoland McGrath 85236df29d7SRoland McGrath if (request == PTRACE_SYSCALL) 85364c19ba2SGabriel Krisman Bertazi set_task_syscall_work(child, SYSCALL_TRACE); 85436df29d7SRoland McGrath else 85564c19ba2SGabriel Krisman Bertazi clear_task_syscall_work(child, SYSCALL_TRACE); 85636df29d7SRoland McGrath 85764eb35f7SGabriel Krisman Bertazi #if defined(CONFIG_GENERIC_ENTRY) || defined(TIF_SYSCALL_EMU) 85836df29d7SRoland McGrath if (request == PTRACE_SYSEMU || request == PTRACE_SYSEMU_SINGLESTEP) 85964eb35f7SGabriel Krisman Bertazi set_task_syscall_work(child, SYSCALL_EMU); 86036df29d7SRoland McGrath else 86164eb35f7SGabriel Krisman Bertazi clear_task_syscall_work(child, SYSCALL_EMU); 86236df29d7SRoland McGrath #endif 86336df29d7SRoland McGrath 8645b88abbfSRoland McGrath if (is_singleblock(request)) { 8655b88abbfSRoland McGrath if (unlikely(!arch_has_block_step())) 8665b88abbfSRoland McGrath return -EIO; 8675b88abbfSRoland McGrath user_enable_block_step(child); 8685b88abbfSRoland McGrath } else if (is_singlestep(request) || is_sysemu_singlestep(request)) { 86936df29d7SRoland McGrath if (unlikely(!arch_has_single_step())) 87036df29d7SRoland McGrath return -EIO; 87136df29d7SRoland McGrath user_enable_single_step(child); 8723a709703SRoland McGrath } else { 87336df29d7SRoland McGrath user_disable_single_step(child); 8743a709703SRoland McGrath } 87536df29d7SRoland McGrath 876b72c1869SOleg Nesterov /* 877b72c1869SOleg Nesterov * Change ->exit_code and ->state under siglock to avoid the race 878b72c1869SOleg Nesterov * with wait_task_stopped() in between; a non-zero ->exit_code will 879b72c1869SOleg Nesterov * wrongly look like another report from tracee. 880b72c1869SOleg Nesterov * 881b72c1869SOleg Nesterov * Note that we need siglock even if ->exit_code == data and/or this 882b72c1869SOleg Nesterov * status was not reported yet, the new status must not be cleared by 883b72c1869SOleg Nesterov * wait_task_stopped() after resume. 884b72c1869SOleg Nesterov */ 885b72c1869SOleg Nesterov spin_lock_irq(&child->sighand->siglock); 88636df29d7SRoland McGrath child->exit_code = data; 887*31cae1eaSPeter Zijlstra child->jobctl &= ~JOBCTL_TRACED; 8880666fb51SOleg Nesterov wake_up_state(child, __TASK_TRACED); 889b72c1869SOleg Nesterov spin_unlock_irq(&child->sighand->siglock); 89036df29d7SRoland McGrath 89136df29d7SRoland McGrath return 0; 89236df29d7SRoland McGrath } 89336df29d7SRoland McGrath 8942225a122SSuresh Siddha #ifdef CONFIG_HAVE_ARCH_TRACEHOOK 8952225a122SSuresh Siddha 8962225a122SSuresh Siddha static const struct user_regset * 8972225a122SSuresh Siddha find_regset(const struct user_regset_view *view, unsigned int type) 8982225a122SSuresh Siddha { 8992225a122SSuresh Siddha const struct user_regset *regset; 9002225a122SSuresh Siddha int n; 9012225a122SSuresh Siddha 9022225a122SSuresh Siddha for (n = 0; n < view->n; ++n) { 9032225a122SSuresh Siddha regset = view->regsets + n; 9042225a122SSuresh Siddha if (regset->core_note_type == type) 9052225a122SSuresh Siddha return regset; 9062225a122SSuresh Siddha } 9072225a122SSuresh Siddha 9082225a122SSuresh Siddha return NULL; 9092225a122SSuresh Siddha } 9102225a122SSuresh Siddha 9112225a122SSuresh Siddha static int ptrace_regset(struct task_struct *task, int req, unsigned int type, 9122225a122SSuresh Siddha struct iovec *kiov) 9132225a122SSuresh Siddha { 9142225a122SSuresh Siddha const struct user_regset_view *view = task_user_regset_view(task); 9152225a122SSuresh Siddha const struct user_regset *regset = find_regset(view, type); 9162225a122SSuresh Siddha int regset_no; 9172225a122SSuresh Siddha 9182225a122SSuresh Siddha if (!regset || (kiov->iov_len % regset->size) != 0) 919c6a0dd7eSSuresh Siddha return -EINVAL; 9202225a122SSuresh Siddha 9212225a122SSuresh Siddha regset_no = regset - view->regsets; 9222225a122SSuresh Siddha kiov->iov_len = min(kiov->iov_len, 9232225a122SSuresh Siddha (__kernel_size_t) (regset->n * regset->size)); 9242225a122SSuresh Siddha 9252225a122SSuresh Siddha if (req == PTRACE_GETREGSET) 9262225a122SSuresh Siddha return copy_regset_to_user(task, view, regset_no, 0, 9272225a122SSuresh Siddha kiov->iov_len, kiov->iov_base); 9282225a122SSuresh Siddha else 9292225a122SSuresh Siddha return copy_regset_from_user(task, view, regset_no, 0, 9302225a122SSuresh Siddha kiov->iov_len, kiov->iov_base); 9312225a122SSuresh Siddha } 9322225a122SSuresh Siddha 933e8440c14SJosh Stone /* 934e8440c14SJosh Stone * This is declared in linux/regset.h and defined in machine-dependent 935e8440c14SJosh Stone * code. We put the export here, near the primary machine-neutral use, 936e8440c14SJosh Stone * to ensure no machine forgets it. 937e8440c14SJosh Stone */ 938e8440c14SJosh Stone EXPORT_SYMBOL_GPL(task_user_regset_view); 939201766a2SElvira Khabirova 940201766a2SElvira Khabirova static unsigned long 941201766a2SElvira Khabirova ptrace_get_syscall_info_entry(struct task_struct *child, struct pt_regs *regs, 942201766a2SElvira Khabirova struct ptrace_syscall_info *info) 943201766a2SElvira Khabirova { 944201766a2SElvira Khabirova unsigned long args[ARRAY_SIZE(info->entry.args)]; 945201766a2SElvira Khabirova int i; 946201766a2SElvira Khabirova 947201766a2SElvira Khabirova info->op = PTRACE_SYSCALL_INFO_ENTRY; 948201766a2SElvira Khabirova info->entry.nr = syscall_get_nr(child, regs); 949201766a2SElvira Khabirova syscall_get_arguments(child, regs, args); 950201766a2SElvira Khabirova for (i = 0; i < ARRAY_SIZE(args); i++) 951201766a2SElvira Khabirova info->entry.args[i] = args[i]; 952201766a2SElvira Khabirova 953201766a2SElvira Khabirova /* args is the last field in struct ptrace_syscall_info.entry */ 954201766a2SElvira Khabirova return offsetofend(struct ptrace_syscall_info, entry.args); 955201766a2SElvira Khabirova } 956201766a2SElvira Khabirova 957201766a2SElvira Khabirova static unsigned long 958201766a2SElvira Khabirova ptrace_get_syscall_info_seccomp(struct task_struct *child, struct pt_regs *regs, 959201766a2SElvira Khabirova struct ptrace_syscall_info *info) 960201766a2SElvira Khabirova { 961201766a2SElvira Khabirova /* 962201766a2SElvira Khabirova * As struct ptrace_syscall_info.entry is currently a subset 963201766a2SElvira Khabirova * of struct ptrace_syscall_info.seccomp, it makes sense to 964201766a2SElvira Khabirova * initialize that subset using ptrace_get_syscall_info_entry(). 965201766a2SElvira Khabirova * This can be reconsidered in the future if these structures 966201766a2SElvira Khabirova * diverge significantly enough. 967201766a2SElvira Khabirova */ 968201766a2SElvira Khabirova ptrace_get_syscall_info_entry(child, regs, info); 969201766a2SElvira Khabirova info->op = PTRACE_SYSCALL_INFO_SECCOMP; 970201766a2SElvira Khabirova info->seccomp.ret_data = child->ptrace_message; 971201766a2SElvira Khabirova 972201766a2SElvira Khabirova /* ret_data is the last field in struct ptrace_syscall_info.seccomp */ 973201766a2SElvira Khabirova return offsetofend(struct ptrace_syscall_info, seccomp.ret_data); 974201766a2SElvira Khabirova } 975201766a2SElvira Khabirova 976201766a2SElvira Khabirova static unsigned long 977201766a2SElvira Khabirova ptrace_get_syscall_info_exit(struct task_struct *child, struct pt_regs *regs, 978201766a2SElvira Khabirova struct ptrace_syscall_info *info) 979201766a2SElvira Khabirova { 980201766a2SElvira Khabirova info->op = PTRACE_SYSCALL_INFO_EXIT; 981201766a2SElvira Khabirova info->exit.rval = syscall_get_error(child, regs); 982201766a2SElvira Khabirova info->exit.is_error = !!info->exit.rval; 983201766a2SElvira Khabirova if (!info->exit.is_error) 984201766a2SElvira Khabirova info->exit.rval = syscall_get_return_value(child, regs); 985201766a2SElvira Khabirova 986201766a2SElvira Khabirova /* is_error is the last field in struct ptrace_syscall_info.exit */ 987201766a2SElvira Khabirova return offsetofend(struct ptrace_syscall_info, exit.is_error); 988201766a2SElvira Khabirova } 989201766a2SElvira Khabirova 990201766a2SElvira Khabirova static int 991201766a2SElvira Khabirova ptrace_get_syscall_info(struct task_struct *child, unsigned long user_size, 992201766a2SElvira Khabirova void __user *datavp) 993201766a2SElvira Khabirova { 994201766a2SElvira Khabirova struct pt_regs *regs = task_pt_regs(child); 995201766a2SElvira Khabirova struct ptrace_syscall_info info = { 996201766a2SElvira Khabirova .op = PTRACE_SYSCALL_INFO_NONE, 997201766a2SElvira Khabirova .arch = syscall_get_arch(child), 998201766a2SElvira Khabirova .instruction_pointer = instruction_pointer(regs), 999201766a2SElvira Khabirova .stack_pointer = user_stack_pointer(regs), 1000201766a2SElvira Khabirova }; 1001201766a2SElvira Khabirova unsigned long actual_size = offsetof(struct ptrace_syscall_info, entry); 1002201766a2SElvira Khabirova unsigned long write_size; 1003201766a2SElvira Khabirova 1004201766a2SElvira Khabirova /* 1005201766a2SElvira Khabirova * This does not need lock_task_sighand() to access 1006201766a2SElvira Khabirova * child->last_siginfo because ptrace_freeze_traced() 1007201766a2SElvira Khabirova * called earlier by ptrace_check_attach() ensures that 1008201766a2SElvira Khabirova * the tracee cannot go away and clear its last_siginfo. 1009201766a2SElvira Khabirova */ 1010201766a2SElvira Khabirova switch (child->last_siginfo ? child->last_siginfo->si_code : 0) { 1011201766a2SElvira Khabirova case SIGTRAP | 0x80: 1012201766a2SElvira Khabirova switch (child->ptrace_message) { 1013201766a2SElvira Khabirova case PTRACE_EVENTMSG_SYSCALL_ENTRY: 1014201766a2SElvira Khabirova actual_size = ptrace_get_syscall_info_entry(child, regs, 1015201766a2SElvira Khabirova &info); 1016201766a2SElvira Khabirova break; 1017201766a2SElvira Khabirova case PTRACE_EVENTMSG_SYSCALL_EXIT: 1018201766a2SElvira Khabirova actual_size = ptrace_get_syscall_info_exit(child, regs, 1019201766a2SElvira Khabirova &info); 1020201766a2SElvira Khabirova break; 1021201766a2SElvira Khabirova } 1022201766a2SElvira Khabirova break; 1023201766a2SElvira Khabirova case SIGTRAP | (PTRACE_EVENT_SECCOMP << 8): 1024201766a2SElvira Khabirova actual_size = ptrace_get_syscall_info_seccomp(child, regs, 1025201766a2SElvira Khabirova &info); 1026201766a2SElvira Khabirova break; 1027201766a2SElvira Khabirova } 1028201766a2SElvira Khabirova 1029201766a2SElvira Khabirova write_size = min(actual_size, user_size); 1030201766a2SElvira Khabirova return copy_to_user(datavp, &info, write_size) ? -EFAULT : actual_size; 1031201766a2SElvira Khabirova } 1032201766a2SElvira Khabirova #endif /* CONFIG_HAVE_ARCH_TRACEHOOK */ 10332225a122SSuresh Siddha 10341da177e4SLinus Torvalds int ptrace_request(struct task_struct *child, long request, 10354abf9869SNamhyung Kim unsigned long addr, unsigned long data) 10361da177e4SLinus Torvalds { 1037fca26f26STejun Heo bool seized = child->ptrace & PT_SEIZED; 10381da177e4SLinus Torvalds int ret = -EIO; 1039ae7795bcSEric W. Biederman kernel_siginfo_t siginfo, *si; 10409fed81dcSNamhyung Kim void __user *datavp = (void __user *) data; 10419fed81dcSNamhyung Kim unsigned long __user *datalp = datavp; 1042fca26f26STejun Heo unsigned long flags; 10431da177e4SLinus Torvalds 10441da177e4SLinus Torvalds switch (request) { 104516c3e389SRoland McGrath case PTRACE_PEEKTEXT: 104616c3e389SRoland McGrath case PTRACE_PEEKDATA: 104716c3e389SRoland McGrath return generic_ptrace_peekdata(child, addr, data); 104816c3e389SRoland McGrath case PTRACE_POKETEXT: 104916c3e389SRoland McGrath case PTRACE_POKEDATA: 105016c3e389SRoland McGrath return generic_ptrace_pokedata(child, addr, data); 105116c3e389SRoland McGrath 10521da177e4SLinus Torvalds #ifdef PTRACE_OLDSETOPTIONS 10531da177e4SLinus Torvalds case PTRACE_OLDSETOPTIONS: 10541da177e4SLinus Torvalds #endif 10551da177e4SLinus Torvalds case PTRACE_SETOPTIONS: 10561da177e4SLinus Torvalds ret = ptrace_setoptions(child, data); 10571da177e4SLinus Torvalds break; 10581da177e4SLinus Torvalds case PTRACE_GETEVENTMSG: 10599fed81dcSNamhyung Kim ret = put_user(child->ptrace_message, datalp); 10601da177e4SLinus Torvalds break; 1061e16b2781SRoland McGrath 106284c751bdSAndrey Vagin case PTRACE_PEEKSIGINFO: 106384c751bdSAndrey Vagin ret = ptrace_peek_siginfo(child, addr, data); 106484c751bdSAndrey Vagin break; 106584c751bdSAndrey Vagin 10661da177e4SLinus Torvalds case PTRACE_GETSIGINFO: 1067e16b2781SRoland McGrath ret = ptrace_getsiginfo(child, &siginfo); 1068e16b2781SRoland McGrath if (!ret) 10699fed81dcSNamhyung Kim ret = copy_siginfo_to_user(datavp, &siginfo); 10701da177e4SLinus Torvalds break; 1071e16b2781SRoland McGrath 10721da177e4SLinus Torvalds case PTRACE_SETSIGINFO: 10734cd2e0e7SEric W. Biederman ret = copy_siginfo_from_user(&siginfo, datavp); 10744cd2e0e7SEric W. Biederman if (!ret) 1075e16b2781SRoland McGrath ret = ptrace_setsiginfo(child, &siginfo); 10761da177e4SLinus Torvalds break; 1077e16b2781SRoland McGrath 1078fcfc2aa0SAndrei Vagin case PTRACE_GETSIGMASK: { 1079fcfc2aa0SAndrei Vagin sigset_t *mask; 1080fcfc2aa0SAndrei Vagin 108129000caeSAndrey Vagin if (addr != sizeof(sigset_t)) { 108229000caeSAndrey Vagin ret = -EINVAL; 108329000caeSAndrey Vagin break; 108429000caeSAndrey Vagin } 108529000caeSAndrey Vagin 1086fcfc2aa0SAndrei Vagin if (test_tsk_restore_sigmask(child)) 1087fcfc2aa0SAndrei Vagin mask = &child->saved_sigmask; 1088fcfc2aa0SAndrei Vagin else 1089fcfc2aa0SAndrei Vagin mask = &child->blocked; 1090fcfc2aa0SAndrei Vagin 1091fcfc2aa0SAndrei Vagin if (copy_to_user(datavp, mask, sizeof(sigset_t))) 109229000caeSAndrey Vagin ret = -EFAULT; 109329000caeSAndrey Vagin else 109429000caeSAndrey Vagin ret = 0; 109529000caeSAndrey Vagin 109629000caeSAndrey Vagin break; 1097fcfc2aa0SAndrei Vagin } 109829000caeSAndrey Vagin 109929000caeSAndrey Vagin case PTRACE_SETSIGMASK: { 110029000caeSAndrey Vagin sigset_t new_set; 110129000caeSAndrey Vagin 110229000caeSAndrey Vagin if (addr != sizeof(sigset_t)) { 110329000caeSAndrey Vagin ret = -EINVAL; 110429000caeSAndrey Vagin break; 110529000caeSAndrey Vagin } 110629000caeSAndrey Vagin 110729000caeSAndrey Vagin if (copy_from_user(&new_set, datavp, sizeof(sigset_t))) { 110829000caeSAndrey Vagin ret = -EFAULT; 110929000caeSAndrey Vagin break; 111029000caeSAndrey Vagin } 111129000caeSAndrey Vagin 111229000caeSAndrey Vagin sigdelsetmask(&new_set, sigmask(SIGKILL)|sigmask(SIGSTOP)); 111329000caeSAndrey Vagin 111429000caeSAndrey Vagin /* 111529000caeSAndrey Vagin * Every thread does recalc_sigpending() after resume, so 111629000caeSAndrey Vagin * retarget_shared_pending() and recalc_sigpending() are not 111729000caeSAndrey Vagin * called here. 111829000caeSAndrey Vagin */ 111929000caeSAndrey Vagin spin_lock_irq(&child->sighand->siglock); 112029000caeSAndrey Vagin child->blocked = new_set; 112129000caeSAndrey Vagin spin_unlock_irq(&child->sighand->siglock); 112229000caeSAndrey Vagin 1123fcfc2aa0SAndrei Vagin clear_tsk_restore_sigmask(child); 1124fcfc2aa0SAndrei Vagin 112529000caeSAndrey Vagin ret = 0; 112629000caeSAndrey Vagin break; 112729000caeSAndrey Vagin } 112829000caeSAndrey Vagin 1129fca26f26STejun Heo case PTRACE_INTERRUPT: 1130fca26f26STejun Heo /* 1131fca26f26STejun Heo * Stop tracee without any side-effect on signal or job 1132fca26f26STejun Heo * control. At least one trap is guaranteed to happen 1133fca26f26STejun Heo * after this request. If @child is already trapped, the 1134fca26f26STejun Heo * current trap is not disturbed and another trap will 1135fca26f26STejun Heo * happen after the current trap is ended with PTRACE_CONT. 1136fca26f26STejun Heo * 1137fca26f26STejun Heo * The actual trap might not be PTRACE_EVENT_STOP trap but 1138fca26f26STejun Heo * the pending condition is cleared regardless. 1139fca26f26STejun Heo */ 1140fca26f26STejun Heo if (unlikely(!seized || !lock_task_sighand(child, &flags))) 1141fca26f26STejun Heo break; 1142fca26f26STejun Heo 1143544b2c91STejun Heo /* 1144544b2c91STejun Heo * INTERRUPT doesn't disturb existing trap sans one 1145544b2c91STejun Heo * exception. If ptracer issued LISTEN for the current 1146544b2c91STejun Heo * STOP, this INTERRUPT should clear LISTEN and re-trap 1147544b2c91STejun Heo * tracee into STOP. 1148544b2c91STejun Heo */ 1149fca26f26STejun Heo if (likely(task_set_jobctl_pending(child, JOBCTL_TRAP_STOP))) 1150910ffdb1SOleg Nesterov ptrace_signal_wake_up(child, child->jobctl & JOBCTL_LISTENING); 1151544b2c91STejun Heo 1152544b2c91STejun Heo unlock_task_sighand(child, &flags); 1153544b2c91STejun Heo ret = 0; 1154544b2c91STejun Heo break; 1155544b2c91STejun Heo 1156544b2c91STejun Heo case PTRACE_LISTEN: 1157544b2c91STejun Heo /* 1158544b2c91STejun Heo * Listen for events. Tracee must be in STOP. It's not 1159544b2c91STejun Heo * resumed per-se but is not considered to be in TRACED by 1160544b2c91STejun Heo * wait(2) or ptrace(2). If an async event (e.g. group 1161544b2c91STejun Heo * stop state change) happens, tracee will enter STOP trap 1162544b2c91STejun Heo * again. Alternatively, ptracer can issue INTERRUPT to 1163544b2c91STejun Heo * finish listening and re-trap tracee into STOP. 1164544b2c91STejun Heo */ 1165544b2c91STejun Heo if (unlikely(!seized || !lock_task_sighand(child, &flags))) 1166544b2c91STejun Heo break; 1167544b2c91STejun Heo 1168544b2c91STejun Heo si = child->last_siginfo; 1169f9d81f61SOleg Nesterov if (likely(si && (si->si_code >> 8) == PTRACE_EVENT_STOP)) { 1170544b2c91STejun Heo child->jobctl |= JOBCTL_LISTENING; 1171544b2c91STejun Heo /* 1172f9d81f61SOleg Nesterov * If NOTIFY is set, it means event happened between 1173f9d81f61SOleg Nesterov * start of this trap and now. Trigger re-trap. 1174544b2c91STejun Heo */ 1175544b2c91STejun Heo if (child->jobctl & JOBCTL_TRAP_NOTIFY) 1176910ffdb1SOleg Nesterov ptrace_signal_wake_up(child, true); 1177fca26f26STejun Heo ret = 0; 1178f9d81f61SOleg Nesterov } 1179f9d81f61SOleg Nesterov unlock_task_sighand(child, &flags); 1180fca26f26STejun Heo break; 1181fca26f26STejun Heo 11821bcf5482SAlexey Dobriyan case PTRACE_DETACH: /* detach a process that was attached. */ 11831bcf5482SAlexey Dobriyan ret = ptrace_detach(child, data); 11841bcf5482SAlexey Dobriyan break; 118536df29d7SRoland McGrath 11869c1a1259SMike Frysinger #ifdef CONFIG_BINFMT_ELF_FDPIC 11879c1a1259SMike Frysinger case PTRACE_GETFDPIC: { 1188e0129ef9SOleg Nesterov struct mm_struct *mm = get_task_mm(child); 11899c1a1259SMike Frysinger unsigned long tmp = 0; 11909c1a1259SMike Frysinger 1191e0129ef9SOleg Nesterov ret = -ESRCH; 1192e0129ef9SOleg Nesterov if (!mm) 1193e0129ef9SOleg Nesterov break; 1194e0129ef9SOleg Nesterov 11959c1a1259SMike Frysinger switch (addr) { 11969c1a1259SMike Frysinger case PTRACE_GETFDPIC_EXEC: 1197e0129ef9SOleg Nesterov tmp = mm->context.exec_fdpic_loadmap; 11989c1a1259SMike Frysinger break; 11999c1a1259SMike Frysinger case PTRACE_GETFDPIC_INTERP: 1200e0129ef9SOleg Nesterov tmp = mm->context.interp_fdpic_loadmap; 12019c1a1259SMike Frysinger break; 12029c1a1259SMike Frysinger default: 12039c1a1259SMike Frysinger break; 12049c1a1259SMike Frysinger } 1205e0129ef9SOleg Nesterov mmput(mm); 12069c1a1259SMike Frysinger 12079fed81dcSNamhyung Kim ret = put_user(tmp, datalp); 12089c1a1259SMike Frysinger break; 12099c1a1259SMike Frysinger } 12109c1a1259SMike Frysinger #endif 12119c1a1259SMike Frysinger 121236df29d7SRoland McGrath #ifdef PTRACE_SINGLESTEP 121336df29d7SRoland McGrath case PTRACE_SINGLESTEP: 121436df29d7SRoland McGrath #endif 12155b88abbfSRoland McGrath #ifdef PTRACE_SINGLEBLOCK 12165b88abbfSRoland McGrath case PTRACE_SINGLEBLOCK: 12175b88abbfSRoland McGrath #endif 121836df29d7SRoland McGrath #ifdef PTRACE_SYSEMU 121936df29d7SRoland McGrath case PTRACE_SYSEMU: 122036df29d7SRoland McGrath case PTRACE_SYSEMU_SINGLESTEP: 122136df29d7SRoland McGrath #endif 122236df29d7SRoland McGrath case PTRACE_SYSCALL: 122336df29d7SRoland McGrath case PTRACE_CONT: 122436df29d7SRoland McGrath return ptrace_resume(child, request, data); 122536df29d7SRoland McGrath 122636df29d7SRoland McGrath case PTRACE_KILL: 12276a2d90baSEric W. Biederman send_sig_info(SIGKILL, SEND_SIG_NOINFO, child); 122836df29d7SRoland McGrath return 0; 122936df29d7SRoland McGrath 12302225a122SSuresh Siddha #ifdef CONFIG_HAVE_ARCH_TRACEHOOK 12312225a122SSuresh Siddha case PTRACE_GETREGSET: 123229000caeSAndrey Vagin case PTRACE_SETREGSET: { 12332225a122SSuresh Siddha struct iovec kiov; 12349fed81dcSNamhyung Kim struct iovec __user *uiov = datavp; 12352225a122SSuresh Siddha 123696d4f267SLinus Torvalds if (!access_ok(uiov, sizeof(*uiov))) 12372225a122SSuresh Siddha return -EFAULT; 12382225a122SSuresh Siddha 12392225a122SSuresh Siddha if (__get_user(kiov.iov_base, &uiov->iov_base) || 12402225a122SSuresh Siddha __get_user(kiov.iov_len, &uiov->iov_len)) 12412225a122SSuresh Siddha return -EFAULT; 12422225a122SSuresh Siddha 12432225a122SSuresh Siddha ret = ptrace_regset(child, request, addr, &kiov); 12442225a122SSuresh Siddha if (!ret) 12452225a122SSuresh Siddha ret = __put_user(kiov.iov_len, &uiov->iov_len); 12462225a122SSuresh Siddha break; 12472225a122SSuresh Siddha } 1248201766a2SElvira Khabirova 1249201766a2SElvira Khabirova case PTRACE_GET_SYSCALL_INFO: 1250201766a2SElvira Khabirova ret = ptrace_get_syscall_info(child, addr, datavp); 1251201766a2SElvira Khabirova break; 12522225a122SSuresh Siddha #endif 1253f8e529edSTycho Andersen 1254f8e529edSTycho Andersen case PTRACE_SECCOMP_GET_FILTER: 1255f8e529edSTycho Andersen ret = seccomp_get_filter(child, addr, datavp); 1256f8e529edSTycho Andersen break; 1257f8e529edSTycho Andersen 125826500475STycho Andersen case PTRACE_SECCOMP_GET_METADATA: 125926500475STycho Andersen ret = seccomp_get_metadata(child, addr, datavp); 126026500475STycho Andersen break; 126126500475STycho Andersen 126290f093faSPiotr Figiel #ifdef CONFIG_RSEQ 126390f093faSPiotr Figiel case PTRACE_GET_RSEQ_CONFIGURATION: 126490f093faSPiotr Figiel ret = ptrace_get_rseq_configuration(child, addr, datavp); 126590f093faSPiotr Figiel break; 126690f093faSPiotr Figiel #endif 126790f093faSPiotr Figiel 12681da177e4SLinus Torvalds default: 12691da177e4SLinus Torvalds break; 12701da177e4SLinus Torvalds } 12711da177e4SLinus Torvalds 12721da177e4SLinus Torvalds return ret; 12731da177e4SLinus Torvalds } 1274481bed45SChristoph Hellwig 12754abf9869SNamhyung Kim SYSCALL_DEFINE4(ptrace, long, request, long, pid, unsigned long, addr, 12764abf9869SNamhyung Kim unsigned long, data) 1277481bed45SChristoph Hellwig { 1278481bed45SChristoph Hellwig struct task_struct *child; 1279481bed45SChristoph Hellwig long ret; 1280481bed45SChristoph Hellwig 12816b9c7ed8SChristoph Hellwig if (request == PTRACE_TRACEME) { 12826b9c7ed8SChristoph Hellwig ret = ptrace_traceme(); 1283481bed45SChristoph Hellwig goto out; 12846b9c7ed8SChristoph Hellwig } 12856b9c7ed8SChristoph Hellwig 12862ee08260SMike Rapoport child = find_get_task_by_vpid(pid); 12872ee08260SMike Rapoport if (!child) { 12882ee08260SMike Rapoport ret = -ESRCH; 12896b9c7ed8SChristoph Hellwig goto out; 12906b9c7ed8SChristoph Hellwig } 1291481bed45SChristoph Hellwig 12923544d72aSTejun Heo if (request == PTRACE_ATTACH || request == PTRACE_SEIZE) { 1293aa9147c9SDenys Vlasenko ret = ptrace_attach(child, request, addr, data); 1294005f18dfSChristoph Hellwig goto out_put_task_struct; 1295481bed45SChristoph Hellwig } 1296481bed45SChristoph Hellwig 1297fca26f26STejun Heo ret = ptrace_check_attach(child, request == PTRACE_KILL || 1298fca26f26STejun Heo request == PTRACE_INTERRUPT); 1299481bed45SChristoph Hellwig if (ret < 0) 1300481bed45SChristoph Hellwig goto out_put_task_struct; 1301481bed45SChristoph Hellwig 1302481bed45SChristoph Hellwig ret = arch_ptrace(child, request, addr, data); 13039899d11fSOleg Nesterov if (ret || request != PTRACE_DETACH) 13049899d11fSOleg Nesterov ptrace_unfreeze_traced(child); 1305481bed45SChristoph Hellwig 1306481bed45SChristoph Hellwig out_put_task_struct: 1307481bed45SChristoph Hellwig put_task_struct(child); 1308481bed45SChristoph Hellwig out: 1309481bed45SChristoph Hellwig return ret; 1310481bed45SChristoph Hellwig } 131176647323SAlexey Dobriyan 13124abf9869SNamhyung Kim int generic_ptrace_peekdata(struct task_struct *tsk, unsigned long addr, 13134abf9869SNamhyung Kim unsigned long data) 131476647323SAlexey Dobriyan { 131576647323SAlexey Dobriyan unsigned long tmp; 131676647323SAlexey Dobriyan int copied; 131776647323SAlexey Dobriyan 131884d77d3fSEric W. Biederman copied = ptrace_access_vm(tsk, addr, &tmp, sizeof(tmp), FOLL_FORCE); 131976647323SAlexey Dobriyan if (copied != sizeof(tmp)) 132076647323SAlexey Dobriyan return -EIO; 132176647323SAlexey Dobriyan return put_user(tmp, (unsigned long __user *)data); 132276647323SAlexey Dobriyan } 1323f284ce72SAlexey Dobriyan 13244abf9869SNamhyung Kim int generic_ptrace_pokedata(struct task_struct *tsk, unsigned long addr, 13254abf9869SNamhyung Kim unsigned long data) 1326f284ce72SAlexey Dobriyan { 1327f284ce72SAlexey Dobriyan int copied; 1328f284ce72SAlexey Dobriyan 132984d77d3fSEric W. Biederman copied = ptrace_access_vm(tsk, addr, &data, sizeof(data), 1330f307ab6dSLorenzo Stoakes FOLL_FORCE | FOLL_WRITE); 1331f284ce72SAlexey Dobriyan return (copied == sizeof(data)) ? 0 : -EIO; 1332f284ce72SAlexey Dobriyan } 1333032d82d9SRoland McGrath 133496b8936aSChristoph Hellwig #if defined CONFIG_COMPAT 1335032d82d9SRoland McGrath 1336032d82d9SRoland McGrath int compat_ptrace_request(struct task_struct *child, compat_long_t request, 1337032d82d9SRoland McGrath compat_ulong_t addr, compat_ulong_t data) 1338032d82d9SRoland McGrath { 1339032d82d9SRoland McGrath compat_ulong_t __user *datap = compat_ptr(data); 1340032d82d9SRoland McGrath compat_ulong_t word; 1341ae7795bcSEric W. Biederman kernel_siginfo_t siginfo; 1342032d82d9SRoland McGrath int ret; 1343032d82d9SRoland McGrath 1344032d82d9SRoland McGrath switch (request) { 1345032d82d9SRoland McGrath case PTRACE_PEEKTEXT: 1346032d82d9SRoland McGrath case PTRACE_PEEKDATA: 134784d77d3fSEric W. Biederman ret = ptrace_access_vm(child, addr, &word, sizeof(word), 1348f307ab6dSLorenzo Stoakes FOLL_FORCE); 1349032d82d9SRoland McGrath if (ret != sizeof(word)) 1350032d82d9SRoland McGrath ret = -EIO; 1351032d82d9SRoland McGrath else 1352032d82d9SRoland McGrath ret = put_user(word, datap); 1353032d82d9SRoland McGrath break; 1354032d82d9SRoland McGrath 1355032d82d9SRoland McGrath case PTRACE_POKETEXT: 1356032d82d9SRoland McGrath case PTRACE_POKEDATA: 135784d77d3fSEric W. Biederman ret = ptrace_access_vm(child, addr, &data, sizeof(data), 1358f307ab6dSLorenzo Stoakes FOLL_FORCE | FOLL_WRITE); 1359032d82d9SRoland McGrath ret = (ret != sizeof(data) ? -EIO : 0); 1360032d82d9SRoland McGrath break; 1361032d82d9SRoland McGrath 1362032d82d9SRoland McGrath case PTRACE_GETEVENTMSG: 1363032d82d9SRoland McGrath ret = put_user((compat_ulong_t) child->ptrace_message, datap); 1364032d82d9SRoland McGrath break; 1365032d82d9SRoland McGrath 1366e16b2781SRoland McGrath case PTRACE_GETSIGINFO: 1367e16b2781SRoland McGrath ret = ptrace_getsiginfo(child, &siginfo); 1368e16b2781SRoland McGrath if (!ret) 1369e16b2781SRoland McGrath ret = copy_siginfo_to_user32( 1370e16b2781SRoland McGrath (struct compat_siginfo __user *) datap, 1371e16b2781SRoland McGrath &siginfo); 1372e16b2781SRoland McGrath break; 1373e16b2781SRoland McGrath 1374e16b2781SRoland McGrath case PTRACE_SETSIGINFO: 13754cd2e0e7SEric W. Biederman ret = copy_siginfo_from_user32( 13764cd2e0e7SEric W. Biederman &siginfo, (struct compat_siginfo __user *) datap); 13774cd2e0e7SEric W. Biederman if (!ret) 1378e16b2781SRoland McGrath ret = ptrace_setsiginfo(child, &siginfo); 1379e16b2781SRoland McGrath break; 13802225a122SSuresh Siddha #ifdef CONFIG_HAVE_ARCH_TRACEHOOK 13812225a122SSuresh Siddha case PTRACE_GETREGSET: 13822225a122SSuresh Siddha case PTRACE_SETREGSET: 13832225a122SSuresh Siddha { 13842225a122SSuresh Siddha struct iovec kiov; 13852225a122SSuresh Siddha struct compat_iovec __user *uiov = 13862225a122SSuresh Siddha (struct compat_iovec __user *) datap; 13872225a122SSuresh Siddha compat_uptr_t ptr; 13882225a122SSuresh Siddha compat_size_t len; 13892225a122SSuresh Siddha 139096d4f267SLinus Torvalds if (!access_ok(uiov, sizeof(*uiov))) 13912225a122SSuresh Siddha return -EFAULT; 13922225a122SSuresh Siddha 13932225a122SSuresh Siddha if (__get_user(ptr, &uiov->iov_base) || 13942225a122SSuresh Siddha __get_user(len, &uiov->iov_len)) 13952225a122SSuresh Siddha return -EFAULT; 13962225a122SSuresh Siddha 13972225a122SSuresh Siddha kiov.iov_base = compat_ptr(ptr); 13982225a122SSuresh Siddha kiov.iov_len = len; 13992225a122SSuresh Siddha 14002225a122SSuresh Siddha ret = ptrace_regset(child, request, addr, &kiov); 14012225a122SSuresh Siddha if (!ret) 14022225a122SSuresh Siddha ret = __put_user(kiov.iov_len, &uiov->iov_len); 14032225a122SSuresh Siddha break; 14042225a122SSuresh Siddha } 14052225a122SSuresh Siddha #endif 1406e16b2781SRoland McGrath 1407032d82d9SRoland McGrath default: 1408032d82d9SRoland McGrath ret = ptrace_request(child, request, addr, data); 1409032d82d9SRoland McGrath } 1410032d82d9SRoland McGrath 1411032d82d9SRoland McGrath return ret; 1412032d82d9SRoland McGrath } 1413c269f196SRoland McGrath 141462a6fa97SHeiko Carstens COMPAT_SYSCALL_DEFINE4(ptrace, compat_long_t, request, compat_long_t, pid, 141562a6fa97SHeiko Carstens compat_long_t, addr, compat_long_t, data) 1416c269f196SRoland McGrath { 1417c269f196SRoland McGrath struct task_struct *child; 1418c269f196SRoland McGrath long ret; 1419c269f196SRoland McGrath 1420c269f196SRoland McGrath if (request == PTRACE_TRACEME) { 1421c269f196SRoland McGrath ret = ptrace_traceme(); 1422c269f196SRoland McGrath goto out; 1423c269f196SRoland McGrath } 1424c269f196SRoland McGrath 14252ee08260SMike Rapoport child = find_get_task_by_vpid(pid); 14262ee08260SMike Rapoport if (!child) { 14272ee08260SMike Rapoport ret = -ESRCH; 1428c269f196SRoland McGrath goto out; 1429c269f196SRoland McGrath } 1430c269f196SRoland McGrath 14313544d72aSTejun Heo if (request == PTRACE_ATTACH || request == PTRACE_SEIZE) { 1432aa9147c9SDenys Vlasenko ret = ptrace_attach(child, request, addr, data); 1433c269f196SRoland McGrath goto out_put_task_struct; 1434c269f196SRoland McGrath } 1435c269f196SRoland McGrath 1436fca26f26STejun Heo ret = ptrace_check_attach(child, request == PTRACE_KILL || 1437fca26f26STejun Heo request == PTRACE_INTERRUPT); 14389899d11fSOleg Nesterov if (!ret) { 1439c269f196SRoland McGrath ret = compat_arch_ptrace(child, request, addr, data); 14409899d11fSOleg Nesterov if (ret || request != PTRACE_DETACH) 14419899d11fSOleg Nesterov ptrace_unfreeze_traced(child); 14429899d11fSOleg Nesterov } 1443c269f196SRoland McGrath 1444c269f196SRoland McGrath out_put_task_struct: 1445c269f196SRoland McGrath put_task_struct(child); 1446c269f196SRoland McGrath out: 1447c269f196SRoland McGrath return ret; 1448c269f196SRoland McGrath } 144996b8936aSChristoph Hellwig #endif /* CONFIG_COMPAT */ 1450