xref: /openbmc/linux/kernel/ptrace.c (revision 8c5cf9e5)
11da177e4SLinus Torvalds /*
21da177e4SLinus Torvalds  * linux/kernel/ptrace.c
31da177e4SLinus Torvalds  *
41da177e4SLinus Torvalds  * (C) Copyright 1999 Linus Torvalds
51da177e4SLinus Torvalds  *
61da177e4SLinus Torvalds  * Common interfaces for "ptrace()" which we do not want
71da177e4SLinus Torvalds  * to continually duplicate across every architecture.
81da177e4SLinus Torvalds  */
91da177e4SLinus Torvalds 
10c59ede7bSRandy.Dunlap #include <linux/capability.h>
119984de1aSPaul Gortmaker #include <linux/export.h>
121da177e4SLinus Torvalds #include <linux/sched.h>
131da177e4SLinus Torvalds #include <linux/errno.h>
141da177e4SLinus Torvalds #include <linux/mm.h>
151da177e4SLinus Torvalds #include <linux/highmem.h>
161da177e4SLinus Torvalds #include <linux/pagemap.h>
171da177e4SLinus Torvalds #include <linux/ptrace.h>
181da177e4SLinus Torvalds #include <linux/security.h>
197ed20e1aSJesper Juhl #include <linux/signal.h>
20a5cb013dSAl Viro #include <linux/audit.h>
21b488893aSPavel Emelyanov #include <linux/pid_namespace.h>
22f17d30a8SAdrian Bunk #include <linux/syscalls.h>
233a709703SRoland McGrath #include <linux/uaccess.h>
242225a122SSuresh Siddha #include <linux/regset.h>
25bf26c018SFrederic Weisbecker #include <linux/hw_breakpoint.h>
26f701e5b7SVladimir Zapolskiy #include <linux/cn_proc.h>
271da177e4SLinus Torvalds 
28bf53de90SMarkus Metzger 
2962c124ffSTejun Heo static int ptrace_trapping_sleep_fn(void *flags)
3062c124ffSTejun Heo {
3162c124ffSTejun Heo 	schedule();
3262c124ffSTejun Heo 	return 0;
3362c124ffSTejun Heo }
3462c124ffSTejun Heo 
35bf53de90SMarkus Metzger /*
361da177e4SLinus Torvalds  * ptrace a task: make the debugger its new parent and
371da177e4SLinus Torvalds  * move it to the ptrace list.
381da177e4SLinus Torvalds  *
391da177e4SLinus Torvalds  * Must be called with the tasklist lock write-held.
401da177e4SLinus Torvalds  */
4136c8b586SIngo Molnar void __ptrace_link(struct task_struct *child, struct task_struct *new_parent)
421da177e4SLinus Torvalds {
43f470021aSRoland McGrath 	BUG_ON(!list_empty(&child->ptrace_entry));
44f470021aSRoland McGrath 	list_add(&child->ptrace_entry, &new_parent->ptraced);
451da177e4SLinus Torvalds 	child->parent = new_parent;
461da177e4SLinus Torvalds }
471da177e4SLinus Torvalds 
48e3bd058fSTejun Heo /**
49e3bd058fSTejun Heo  * __ptrace_unlink - unlink ptracee and restore its execution state
50e3bd058fSTejun Heo  * @child: ptracee to be unlinked
511da177e4SLinus Torvalds  *
520e9f0a4aSTejun Heo  * Remove @child from the ptrace list, move it back to the original parent,
530e9f0a4aSTejun Heo  * and restore the execution state so that it conforms to the group stop
540e9f0a4aSTejun Heo  * state.
550e9f0a4aSTejun Heo  *
560e9f0a4aSTejun Heo  * Unlinking can happen via two paths - explicit PTRACE_DETACH or ptracer
570e9f0a4aSTejun Heo  * exiting.  For PTRACE_DETACH, unless the ptracee has been killed between
580e9f0a4aSTejun Heo  * ptrace_check_attach() and here, it's guaranteed to be in TASK_TRACED.
590e9f0a4aSTejun Heo  * If the ptracer is exiting, the ptracee can be in any state.
600e9f0a4aSTejun Heo  *
610e9f0a4aSTejun Heo  * After detach, the ptracee should be in a state which conforms to the
620e9f0a4aSTejun Heo  * group stop.  If the group is stopped or in the process of stopping, the
630e9f0a4aSTejun Heo  * ptracee should be put into TASK_STOPPED; otherwise, it should be woken
640e9f0a4aSTejun Heo  * up from TASK_TRACED.
650e9f0a4aSTejun Heo  *
660e9f0a4aSTejun Heo  * If the ptracee is in TASK_TRACED and needs to be moved to TASK_STOPPED,
670e9f0a4aSTejun Heo  * it goes through TRACED -> RUNNING -> STOPPED transition which is similar
680e9f0a4aSTejun Heo  * to but in the opposite direction of what happens while attaching to a
690e9f0a4aSTejun Heo  * stopped task.  However, in this direction, the intermediate RUNNING
700e9f0a4aSTejun Heo  * state is not hidden even from the current ptracer and if it immediately
710e9f0a4aSTejun Heo  * re-attaches and performs a WNOHANG wait(2), it may fail.
72e3bd058fSTejun Heo  *
73e3bd058fSTejun Heo  * CONTEXT:
74e3bd058fSTejun Heo  * write_lock_irq(tasklist_lock)
751da177e4SLinus Torvalds  */
7636c8b586SIngo Molnar void __ptrace_unlink(struct task_struct *child)
771da177e4SLinus Torvalds {
785ecfbae0SOleg Nesterov 	BUG_ON(!child->ptrace);
795ecfbae0SOleg Nesterov 
801da177e4SLinus Torvalds 	child->ptrace = 0;
811da177e4SLinus Torvalds 	child->parent = child->real_parent;
82f470021aSRoland McGrath 	list_del_init(&child->ptrace_entry);
831da177e4SLinus Torvalds 
841da177e4SLinus Torvalds 	spin_lock(&child->sighand->siglock);
850e9f0a4aSTejun Heo 
861da177e4SLinus Torvalds 	/*
8773ddff2bSTejun Heo 	 * Clear all pending traps and TRAPPING.  TRAPPING should be
8873ddff2bSTejun Heo 	 * cleared regardless of JOBCTL_STOP_PENDING.  Do it explicitly.
8973ddff2bSTejun Heo 	 */
9073ddff2bSTejun Heo 	task_clear_jobctl_pending(child, JOBCTL_TRAP_MASK);
9173ddff2bSTejun Heo 	task_clear_jobctl_trapping(child);
9273ddff2bSTejun Heo 
9373ddff2bSTejun Heo 	/*
94a8f072c1STejun Heo 	 * Reinstate JOBCTL_STOP_PENDING if group stop is in effect and
950e9f0a4aSTejun Heo 	 * @child isn't dead.
961da177e4SLinus Torvalds 	 */
970e9f0a4aSTejun Heo 	if (!(child->flags & PF_EXITING) &&
980e9f0a4aSTejun Heo 	    (child->signal->flags & SIGNAL_STOP_STOPPED ||
998a88951bSOleg Nesterov 	     child->signal->group_stop_count)) {
100a8f072c1STejun Heo 		child->jobctl |= JOBCTL_STOP_PENDING;
1010e9f0a4aSTejun Heo 
1020e9f0a4aSTejun Heo 		/*
1038a88951bSOleg Nesterov 		 * This is only possible if this thread was cloned by the
1048a88951bSOleg Nesterov 		 * traced task running in the stopped group, set the signal
1058a88951bSOleg Nesterov 		 * for the future reports.
1068a88951bSOleg Nesterov 		 * FIXME: we should change ptrace_init_task() to handle this
1078a88951bSOleg Nesterov 		 * case.
1088a88951bSOleg Nesterov 		 */
1098a88951bSOleg Nesterov 		if (!(child->jobctl & JOBCTL_STOP_SIGMASK))
1108a88951bSOleg Nesterov 			child->jobctl |= SIGSTOP;
1118a88951bSOleg Nesterov 	}
1128a88951bSOleg Nesterov 
1138a88951bSOleg Nesterov 	/*
1140e9f0a4aSTejun Heo 	 * If transition to TASK_STOPPED is pending or in TASK_TRACED, kick
1150e9f0a4aSTejun Heo 	 * @child in the butt.  Note that @resume should be used iff @child
1160e9f0a4aSTejun Heo 	 * is in TASK_TRACED; otherwise, we might unduly disrupt
1170e9f0a4aSTejun Heo 	 * TASK_KILLABLE sleeps.
1180e9f0a4aSTejun Heo 	 */
119a8f072c1STejun Heo 	if (child->jobctl & JOBCTL_STOP_PENDING || task_is_traced(child))
1200e9f0a4aSTejun Heo 		signal_wake_up(child, task_is_traced(child));
1210e9f0a4aSTejun Heo 
1221da177e4SLinus Torvalds 	spin_unlock(&child->sighand->siglock);
1231da177e4SLinus Torvalds }
1241da177e4SLinus Torvalds 
125755e276bSTejun Heo /**
126755e276bSTejun Heo  * ptrace_check_attach - check whether ptracee is ready for ptrace operation
127755e276bSTejun Heo  * @child: ptracee to check for
128755e276bSTejun Heo  * @ignore_state: don't check whether @child is currently %TASK_TRACED
129755e276bSTejun Heo  *
130755e276bSTejun Heo  * Check whether @child is being ptraced by %current and ready for further
131755e276bSTejun Heo  * ptrace operations.  If @ignore_state is %false, @child also should be in
132755e276bSTejun Heo  * %TASK_TRACED state and on return the child is guaranteed to be traced
133755e276bSTejun Heo  * and not executing.  If @ignore_state is %true, @child can be in any
134755e276bSTejun Heo  * state.
135755e276bSTejun Heo  *
136755e276bSTejun Heo  * CONTEXT:
137755e276bSTejun Heo  * Grabs and releases tasklist_lock and @child->sighand->siglock.
138755e276bSTejun Heo  *
139755e276bSTejun Heo  * RETURNS:
140755e276bSTejun Heo  * 0 on success, -ESRCH if %child is not ready.
1411da177e4SLinus Torvalds  */
142755e276bSTejun Heo int ptrace_check_attach(struct task_struct *child, bool ignore_state)
1431da177e4SLinus Torvalds {
1441da177e4SLinus Torvalds 	int ret = -ESRCH;
1451da177e4SLinus Torvalds 
1461da177e4SLinus Torvalds 	/*
1471da177e4SLinus Torvalds 	 * We take the read lock around doing both checks to close a
1481da177e4SLinus Torvalds 	 * possible race where someone else was tracing our child and
1491da177e4SLinus Torvalds 	 * detached between these two checks.  After this locked check,
1501da177e4SLinus Torvalds 	 * we are sure that this is our traced child and that can only
1511da177e4SLinus Torvalds 	 * be changed by us so it's not changing right after this.
1521da177e4SLinus Torvalds 	 */
1531da177e4SLinus Torvalds 	read_lock(&tasklist_lock);
154c0c0b649SOleg Nesterov 	if ((child->ptrace & PT_PTRACED) && child->parent == current) {
155c0c0b649SOleg Nesterov 		/*
156c0c0b649SOleg Nesterov 		 * child->sighand can't be NULL, release_task()
157c0c0b649SOleg Nesterov 		 * does ptrace_unlink() before __exit_signal().
158c0c0b649SOleg Nesterov 		 */
1591da177e4SLinus Torvalds 		spin_lock_irq(&child->sighand->siglock);
160321fb561SOleg Nesterov 		WARN_ON_ONCE(task_is_stopped(child));
161544b2c91STejun Heo 		if (ignore_state || (task_is_traced(child) &&
162544b2c91STejun Heo 				     !(child->jobctl & JOBCTL_LISTENING)))
163321fb561SOleg Nesterov 			ret = 0;
1641da177e4SLinus Torvalds 		spin_unlock_irq(&child->sighand->siglock);
1651da177e4SLinus Torvalds 	}
1661da177e4SLinus Torvalds 	read_unlock(&tasklist_lock);
1671da177e4SLinus Torvalds 
168755e276bSTejun Heo 	if (!ret && !ignore_state)
16985ba2d86SRoland McGrath 		ret = wait_task_inactive(child, TASK_TRACED) ? 0 : -ESRCH;
1701da177e4SLinus Torvalds 
1711da177e4SLinus Torvalds 	/* All systems go.. */
1721da177e4SLinus Torvalds 	return ret;
1731da177e4SLinus Torvalds }
1741da177e4SLinus Torvalds 
17569f594a3SEric Paris static int ptrace_has_cap(struct user_namespace *ns, unsigned int mode)
17669f594a3SEric Paris {
17769f594a3SEric Paris 	if (mode & PTRACE_MODE_NOAUDIT)
17869f594a3SEric Paris 		return has_ns_capability_noaudit(current, ns, CAP_SYS_PTRACE);
17969f594a3SEric Paris 	else
18069f594a3SEric Paris 		return has_ns_capability(current, ns, CAP_SYS_PTRACE);
18169f594a3SEric Paris }
18269f594a3SEric Paris 
183006ebb40SStephen Smalley int __ptrace_may_access(struct task_struct *task, unsigned int mode)
184ab8d11beSMiklos Szeredi {
185c69e8d9cSDavid Howells 	const struct cred *cred = current_cred(), *tcred;
186b6dff3ecSDavid Howells 
187df26c40eSEric W. Biederman 	/* May we inspect the given task?
188df26c40eSEric W. Biederman 	 * This check is used both for attaching with ptrace
189df26c40eSEric W. Biederman 	 * and for allowing access to sensitive information in /proc.
190df26c40eSEric W. Biederman 	 *
191df26c40eSEric W. Biederman 	 * ptrace_attach denies several cases that /proc allows
192df26c40eSEric W. Biederman 	 * because setting up the necessary parent/child relationship
193df26c40eSEric W. Biederman 	 * or halting the specified task is impossible.
194df26c40eSEric W. Biederman 	 */
195df26c40eSEric W. Biederman 	int dumpable = 0;
196df26c40eSEric W. Biederman 	/* Don't let security modules deny introspection */
197df26c40eSEric W. Biederman 	if (task == current)
198df26c40eSEric W. Biederman 		return 0;
199c69e8d9cSDavid Howells 	rcu_read_lock();
200c69e8d9cSDavid Howells 	tcred = __task_cred(task);
2018409cca7SSerge E. Hallyn 	if (cred->user->user_ns == tcred->user->user_ns &&
2028409cca7SSerge E. Hallyn 	    (cred->uid == tcred->euid &&
2038409cca7SSerge E. Hallyn 	     cred->uid == tcred->suid &&
2048409cca7SSerge E. Hallyn 	     cred->uid == tcred->uid  &&
2058409cca7SSerge E. Hallyn 	     cred->gid == tcred->egid &&
2068409cca7SSerge E. Hallyn 	     cred->gid == tcred->sgid &&
2078409cca7SSerge E. Hallyn 	     cred->gid == tcred->gid))
2088409cca7SSerge E. Hallyn 		goto ok;
20969f594a3SEric Paris 	if (ptrace_has_cap(tcred->user->user_ns, mode))
2108409cca7SSerge E. Hallyn 		goto ok;
211c69e8d9cSDavid Howells 	rcu_read_unlock();
212ab8d11beSMiklos Szeredi 	return -EPERM;
2138409cca7SSerge E. Hallyn ok:
214c69e8d9cSDavid Howells 	rcu_read_unlock();
215ab8d11beSMiklos Szeredi 	smp_rmb();
216df26c40eSEric W. Biederman 	if (task->mm)
2176c5d5238SKawai, Hidehiro 		dumpable = get_dumpable(task->mm);
21869f594a3SEric Paris 	if (!dumpable  && !ptrace_has_cap(task_user_ns(task), mode))
219ab8d11beSMiklos Szeredi 		return -EPERM;
220ab8d11beSMiklos Szeredi 
2219e48858fSIngo Molnar 	return security_ptrace_access_check(task, mode);
222ab8d11beSMiklos Szeredi }
223ab8d11beSMiklos Szeredi 
224006ebb40SStephen Smalley bool ptrace_may_access(struct task_struct *task, unsigned int mode)
225ab8d11beSMiklos Szeredi {
226ab8d11beSMiklos Szeredi 	int err;
227ab8d11beSMiklos Szeredi 	task_lock(task);
228006ebb40SStephen Smalley 	err = __ptrace_may_access(task, mode);
229ab8d11beSMiklos Szeredi 	task_unlock(task);
2303a709703SRoland McGrath 	return !err;
231ab8d11beSMiklos Szeredi }
232ab8d11beSMiklos Szeredi 
2333544d72aSTejun Heo static int ptrace_attach(struct task_struct *task, long request,
2343544d72aSTejun Heo 			 unsigned long flags)
2351da177e4SLinus Torvalds {
2363544d72aSTejun Heo 	bool seize = (request == PTRACE_SEIZE);
2371da177e4SLinus Torvalds 	int retval;
238f5b40e36SLinus Torvalds 
2393544d72aSTejun Heo 	/*
2403544d72aSTejun Heo 	 * SEIZE will enable new ptrace behaviors which will be implemented
2413544d72aSTejun Heo 	 * gradually.  SEIZE_DEVEL is used to prevent applications
2423544d72aSTejun Heo 	 * expecting full SEIZE behaviors trapping on kernel commits which
2433544d72aSTejun Heo 	 * are still in the process of implementing them.
2443544d72aSTejun Heo 	 *
2453544d72aSTejun Heo 	 * Only test programs for new ptrace behaviors being implemented
2463544d72aSTejun Heo 	 * should set SEIZE_DEVEL.  If unset, SEIZE will fail with -EIO.
2473544d72aSTejun Heo 	 *
2483544d72aSTejun Heo 	 * Once SEIZE behaviors are completely implemented, this flag and
2493544d72aSTejun Heo 	 * the following test will be removed.
2503544d72aSTejun Heo 	 */
2513544d72aSTejun Heo 	retval = -EIO;
2523544d72aSTejun Heo 	if (seize && !(flags & PTRACE_SEIZE_DEVEL))
2533544d72aSTejun Heo 		goto out;
2543544d72aSTejun Heo 
255a5cb013dSAl Viro 	audit_ptrace(task);
256a5cb013dSAl Viro 
2571da177e4SLinus Torvalds 	retval = -EPERM;
258b79b7ba9SOleg Nesterov 	if (unlikely(task->flags & PF_KTHREAD))
259b79b7ba9SOleg Nesterov 		goto out;
260bac0abd6SPavel Emelyanov 	if (same_thread_group(task, current))
261f5b40e36SLinus Torvalds 		goto out;
262f5b40e36SLinus Torvalds 
263f2f0b00aSOleg Nesterov 	/*
264f2f0b00aSOleg Nesterov 	 * Protect exec's credential calculations against our interference;
2655e751e99SDavid Howells 	 * interference; SUID, SGID and LSM creds get determined differently
2665e751e99SDavid Howells 	 * under ptrace.
267d84f4f99SDavid Howells 	 */
268793285fcSOleg Nesterov 	retval = -ERESTARTNOINTR;
2699b1bf12dSKOSAKI Motohiro 	if (mutex_lock_interruptible(&task->signal->cred_guard_mutex))
270d84f4f99SDavid Howells 		goto out;
2714b105cbbSOleg Nesterov 
272f5b40e36SLinus Torvalds 	task_lock(task);
273006ebb40SStephen Smalley 	retval = __ptrace_may_access(task, PTRACE_MODE_ATTACH);
2744b105cbbSOleg Nesterov 	task_unlock(task);
2751da177e4SLinus Torvalds 	if (retval)
2764b105cbbSOleg Nesterov 		goto unlock_creds;
2771da177e4SLinus Torvalds 
2784b105cbbSOleg Nesterov 	write_lock_irq(&tasklist_lock);
279b79b7ba9SOleg Nesterov 	retval = -EPERM;
280b79b7ba9SOleg Nesterov 	if (unlikely(task->exit_state))
2814b105cbbSOleg Nesterov 		goto unlock_tasklist;
282f2f0b00aSOleg Nesterov 	if (task->ptrace)
2834b105cbbSOleg Nesterov 		goto unlock_tasklist;
284b79b7ba9SOleg Nesterov 
285f2f0b00aSOleg Nesterov 	task->ptrace = PT_PTRACED;
2863544d72aSTejun Heo 	if (seize)
2873544d72aSTejun Heo 		task->ptrace |= PT_SEIZED;
288f1c84daeSEric Paris 	if (ns_capable(task_user_ns(task), CAP_SYS_PTRACE))
2891da177e4SLinus Torvalds 		task->ptrace |= PT_PTRACE_CAP;
2901da177e4SLinus Torvalds 
2911da177e4SLinus Torvalds 	__ptrace_link(task, current);
2923544d72aSTejun Heo 
2933544d72aSTejun Heo 	/* SEIZE doesn't trap tracee on attach */
2943544d72aSTejun Heo 	if (!seize)
29533e9fc7dSOleg Nesterov 		send_sig_info(SIGSTOP, SEND_SIG_FORCED, task);
296b79b7ba9SOleg Nesterov 
297d79fdd6dSTejun Heo 	spin_lock(&task->sighand->siglock);
298d79fdd6dSTejun Heo 
299d79fdd6dSTejun Heo 	/*
30073ddff2bSTejun Heo 	 * If the task is already STOPPED, set JOBCTL_TRAP_STOP and
301d79fdd6dSTejun Heo 	 * TRAPPING, and kick it so that it transits to TRACED.  TRAPPING
302d79fdd6dSTejun Heo 	 * will be cleared if the child completes the transition or any
303d79fdd6dSTejun Heo 	 * event which clears the group stop states happens.  We'll wait
304d79fdd6dSTejun Heo 	 * for the transition to complete before returning from this
305d79fdd6dSTejun Heo 	 * function.
306d79fdd6dSTejun Heo 	 *
307d79fdd6dSTejun Heo 	 * This hides STOPPED -> RUNNING -> TRACED transition from the
308d79fdd6dSTejun Heo 	 * attaching thread but a different thread in the same group can
309d79fdd6dSTejun Heo 	 * still observe the transient RUNNING state.  IOW, if another
310d79fdd6dSTejun Heo 	 * thread's WNOHANG wait(2) on the stopped tracee races against
311d79fdd6dSTejun Heo 	 * ATTACH, the wait(2) may fail due to the transient RUNNING.
312d79fdd6dSTejun Heo 	 *
313d79fdd6dSTejun Heo 	 * The following task_is_stopped() test is safe as both transitions
314d79fdd6dSTejun Heo 	 * in and out of STOPPED are protected by siglock.
315d79fdd6dSTejun Heo 	 */
3167dd3db54STejun Heo 	if (task_is_stopped(task) &&
31773ddff2bSTejun Heo 	    task_set_jobctl_pending(task, JOBCTL_TRAP_STOP | JOBCTL_TRAPPING))
318d79fdd6dSTejun Heo 		signal_wake_up(task, 1);
319d79fdd6dSTejun Heo 
320d79fdd6dSTejun Heo 	spin_unlock(&task->sighand->siglock);
321d79fdd6dSTejun Heo 
322b79b7ba9SOleg Nesterov 	retval = 0;
3234b105cbbSOleg Nesterov unlock_tasklist:
3244b105cbbSOleg Nesterov 	write_unlock_irq(&tasklist_lock);
3254b105cbbSOleg Nesterov unlock_creds:
3269b1bf12dSKOSAKI Motohiro 	mutex_unlock(&task->signal->cred_guard_mutex);
327f5b40e36SLinus Torvalds out:
328f701e5b7SVladimir Zapolskiy 	if (!retval) {
32962c124ffSTejun Heo 		wait_on_bit(&task->jobctl, JOBCTL_TRAPPING_BIT,
33062c124ffSTejun Heo 			    ptrace_trapping_sleep_fn, TASK_UNINTERRUPTIBLE);
331f701e5b7SVladimir Zapolskiy 		proc_ptrace_connector(task, PTRACE_ATTACH);
332f701e5b7SVladimir Zapolskiy 	}
333f701e5b7SVladimir Zapolskiy 
3341da177e4SLinus Torvalds 	return retval;
3351da177e4SLinus Torvalds }
3361da177e4SLinus Torvalds 
337f2f0b00aSOleg Nesterov /**
338f2f0b00aSOleg Nesterov  * ptrace_traceme  --  helper for PTRACE_TRACEME
339f2f0b00aSOleg Nesterov  *
340f2f0b00aSOleg Nesterov  * Performs checks and sets PT_PTRACED.
341f2f0b00aSOleg Nesterov  * Should be used by all ptrace implementations for PTRACE_TRACEME.
342f2f0b00aSOleg Nesterov  */
343e3e89cc5SLinus Torvalds static int ptrace_traceme(void)
344f2f0b00aSOleg Nesterov {
345f2f0b00aSOleg Nesterov 	int ret = -EPERM;
346f2f0b00aSOleg Nesterov 
3474b105cbbSOleg Nesterov 	write_lock_irq(&tasklist_lock);
3484b105cbbSOleg Nesterov 	/* Are we already being traced? */
349f2f0b00aSOleg Nesterov 	if (!current->ptrace) {
350f2f0b00aSOleg Nesterov 		ret = security_ptrace_traceme(current->parent);
351f2f0b00aSOleg Nesterov 		/*
352f2f0b00aSOleg Nesterov 		 * Check PF_EXITING to ensure ->real_parent has not passed
353f2f0b00aSOleg Nesterov 		 * exit_ptrace(). Otherwise we don't report the error but
354f2f0b00aSOleg Nesterov 		 * pretend ->real_parent untraces us right after return.
355f2f0b00aSOleg Nesterov 		 */
356f2f0b00aSOleg Nesterov 		if (!ret && !(current->real_parent->flags & PF_EXITING)) {
357f2f0b00aSOleg Nesterov 			current->ptrace = PT_PTRACED;
358f2f0b00aSOleg Nesterov 			__ptrace_link(current, current->real_parent);
359f2f0b00aSOleg Nesterov 		}
360f2f0b00aSOleg Nesterov 	}
3614b105cbbSOleg Nesterov 	write_unlock_irq(&tasklist_lock);
3624b105cbbSOleg Nesterov 
363f2f0b00aSOleg Nesterov 	return ret;
364f2f0b00aSOleg Nesterov }
365f2f0b00aSOleg Nesterov 
36639c626aeSOleg Nesterov /*
36739c626aeSOleg Nesterov  * Called with irqs disabled, returns true if childs should reap themselves.
36839c626aeSOleg Nesterov  */
36939c626aeSOleg Nesterov static int ignoring_children(struct sighand_struct *sigh)
37039c626aeSOleg Nesterov {
37139c626aeSOleg Nesterov 	int ret;
37239c626aeSOleg Nesterov 	spin_lock(&sigh->siglock);
37339c626aeSOleg Nesterov 	ret = (sigh->action[SIGCHLD-1].sa.sa_handler == SIG_IGN) ||
37439c626aeSOleg Nesterov 	      (sigh->action[SIGCHLD-1].sa.sa_flags & SA_NOCLDWAIT);
37539c626aeSOleg Nesterov 	spin_unlock(&sigh->siglock);
37639c626aeSOleg Nesterov 	return ret;
37739c626aeSOleg Nesterov }
37839c626aeSOleg Nesterov 
37939c626aeSOleg Nesterov /*
38039c626aeSOleg Nesterov  * Called with tasklist_lock held for writing.
38139c626aeSOleg Nesterov  * Unlink a traced task, and clean it up if it was a traced zombie.
38239c626aeSOleg Nesterov  * Return true if it needs to be reaped with release_task().
38339c626aeSOleg Nesterov  * (We can't call release_task() here because we already hold tasklist_lock.)
38439c626aeSOleg Nesterov  *
38539c626aeSOleg Nesterov  * If it's a zombie, our attachedness prevented normal parent notification
38639c626aeSOleg Nesterov  * or self-reaping.  Do notification now if it would have happened earlier.
38739c626aeSOleg Nesterov  * If it should reap itself, return true.
38839c626aeSOleg Nesterov  *
389a7f0765eSOleg Nesterov  * If it's our own child, there is no notification to do. But if our normal
390a7f0765eSOleg Nesterov  * children self-reap, then this child was prevented by ptrace and we must
391a7f0765eSOleg Nesterov  * reap it now, in that case we must also wake up sub-threads sleeping in
392a7f0765eSOleg Nesterov  * do_wait().
39339c626aeSOleg Nesterov  */
39439c626aeSOleg Nesterov static bool __ptrace_detach(struct task_struct *tracer, struct task_struct *p)
39539c626aeSOleg Nesterov {
3969843a1e9SOleg Nesterov 	bool dead;
3979843a1e9SOleg Nesterov 
39839c626aeSOleg Nesterov 	__ptrace_unlink(p);
39939c626aeSOleg Nesterov 
4009843a1e9SOleg Nesterov 	if (p->exit_state != EXIT_ZOMBIE)
4019843a1e9SOleg Nesterov 		return false;
4029843a1e9SOleg Nesterov 
4039843a1e9SOleg Nesterov 	dead = !thread_group_leader(p);
4049843a1e9SOleg Nesterov 
4059843a1e9SOleg Nesterov 	if (!dead && thread_group_empty(p)) {
40639c626aeSOleg Nesterov 		if (!same_thread_group(p->real_parent, tracer))
4079843a1e9SOleg Nesterov 			dead = do_notify_parent(p, p->exit_signal);
408a7f0765eSOleg Nesterov 		else if (ignoring_children(tracer->sighand)) {
409a7f0765eSOleg Nesterov 			__wake_up_parent(p, tracer);
4109843a1e9SOleg Nesterov 			dead = true;
41139c626aeSOleg Nesterov 		}
412a7f0765eSOleg Nesterov 	}
41339c626aeSOleg Nesterov 	/* Mark it as in the process of being reaped. */
4149843a1e9SOleg Nesterov 	if (dead)
41539c626aeSOleg Nesterov 		p->exit_state = EXIT_DEAD;
4169843a1e9SOleg Nesterov 	return dead;
41739c626aeSOleg Nesterov }
41839c626aeSOleg Nesterov 
419e3e89cc5SLinus Torvalds static int ptrace_detach(struct task_struct *child, unsigned int data)
4201da177e4SLinus Torvalds {
42139c626aeSOleg Nesterov 	bool dead = false;
4224576145cSOleg Nesterov 
4237ed20e1aSJesper Juhl 	if (!valid_signal(data))
4241da177e4SLinus Torvalds 		return -EIO;
4251da177e4SLinus Torvalds 
4261da177e4SLinus Torvalds 	/* Architecture-specific hardware disable .. */
4271da177e4SLinus Torvalds 	ptrace_disable(child);
4287d941432SRoland McGrath 	clear_tsk_thread_flag(child, TIF_SYSCALL_TRACE);
4291da177e4SLinus Torvalds 
43095c3eb76SOleg Nesterov 	write_lock_irq(&tasklist_lock);
43139c626aeSOleg Nesterov 	/*
43239c626aeSOleg Nesterov 	 * This child can be already killed. Make sure de_thread() or
43339c626aeSOleg Nesterov 	 * our sub-thread doing do_wait() didn't do release_task() yet.
43439c626aeSOleg Nesterov 	 */
43595c3eb76SOleg Nesterov 	if (child->ptrace) {
43695c3eb76SOleg Nesterov 		child->exit_code = data;
4374576145cSOleg Nesterov 		dead = __ptrace_detach(current, child);
43895c3eb76SOleg Nesterov 	}
4391da177e4SLinus Torvalds 	write_unlock_irq(&tasklist_lock);
4401da177e4SLinus Torvalds 
441f701e5b7SVladimir Zapolskiy 	proc_ptrace_connector(child, PTRACE_DETACH);
4424576145cSOleg Nesterov 	if (unlikely(dead))
4434576145cSOleg Nesterov 		release_task(child);
4444576145cSOleg Nesterov 
4451da177e4SLinus Torvalds 	return 0;
4461da177e4SLinus Torvalds }
4471da177e4SLinus Torvalds 
44839c626aeSOleg Nesterov /*
449c7e49c14SOleg Nesterov  * Detach all tasks we were using ptrace on. Called with tasklist held
450c7e49c14SOleg Nesterov  * for writing, and returns with it held too. But note it can release
451c7e49c14SOleg Nesterov  * and reacquire the lock.
45239c626aeSOleg Nesterov  */
45339c626aeSOleg Nesterov void exit_ptrace(struct task_struct *tracer)
454c4b5ed25SNamhyung Kim 	__releases(&tasklist_lock)
455c4b5ed25SNamhyung Kim 	__acquires(&tasklist_lock)
45639c626aeSOleg Nesterov {
45739c626aeSOleg Nesterov 	struct task_struct *p, *n;
45839c626aeSOleg Nesterov 	LIST_HEAD(ptrace_dead);
45939c626aeSOleg Nesterov 
460c7e49c14SOleg Nesterov 	if (likely(list_empty(&tracer->ptraced)))
461c7e49c14SOleg Nesterov 		return;
462c7e49c14SOleg Nesterov 
46339c626aeSOleg Nesterov 	list_for_each_entry_safe(p, n, &tracer->ptraced, ptrace_entry) {
46439c626aeSOleg Nesterov 		if (__ptrace_detach(tracer, p))
46539c626aeSOleg Nesterov 			list_add(&p->ptrace_entry, &ptrace_dead);
46639c626aeSOleg Nesterov 	}
46739c626aeSOleg Nesterov 
468c7e49c14SOleg Nesterov 	write_unlock_irq(&tasklist_lock);
46939c626aeSOleg Nesterov 	BUG_ON(!list_empty(&tracer->ptraced));
47039c626aeSOleg Nesterov 
47139c626aeSOleg Nesterov 	list_for_each_entry_safe(p, n, &ptrace_dead, ptrace_entry) {
47239c626aeSOleg Nesterov 		list_del_init(&p->ptrace_entry);
47339c626aeSOleg Nesterov 		release_task(p);
47439c626aeSOleg Nesterov 	}
475c7e49c14SOleg Nesterov 
476c7e49c14SOleg Nesterov 	write_lock_irq(&tasklist_lock);
47739c626aeSOleg Nesterov }
47839c626aeSOleg Nesterov 
4791da177e4SLinus Torvalds int ptrace_readdata(struct task_struct *tsk, unsigned long src, char __user *dst, int len)
4801da177e4SLinus Torvalds {
4811da177e4SLinus Torvalds 	int copied = 0;
4821da177e4SLinus Torvalds 
4831da177e4SLinus Torvalds 	while (len > 0) {
4841da177e4SLinus Torvalds 		char buf[128];
4851da177e4SLinus Torvalds 		int this_len, retval;
4861da177e4SLinus Torvalds 
4871da177e4SLinus Torvalds 		this_len = (len > sizeof(buf)) ? sizeof(buf) : len;
4881da177e4SLinus Torvalds 		retval = access_process_vm(tsk, src, buf, this_len, 0);
4891da177e4SLinus Torvalds 		if (!retval) {
4901da177e4SLinus Torvalds 			if (copied)
4911da177e4SLinus Torvalds 				break;
4921da177e4SLinus Torvalds 			return -EIO;
4931da177e4SLinus Torvalds 		}
4941da177e4SLinus Torvalds 		if (copy_to_user(dst, buf, retval))
4951da177e4SLinus Torvalds 			return -EFAULT;
4961da177e4SLinus Torvalds 		copied += retval;
4971da177e4SLinus Torvalds 		src += retval;
4981da177e4SLinus Torvalds 		dst += retval;
4991da177e4SLinus Torvalds 		len -= retval;
5001da177e4SLinus Torvalds 	}
5011da177e4SLinus Torvalds 	return copied;
5021da177e4SLinus Torvalds }
5031da177e4SLinus Torvalds 
5041da177e4SLinus Torvalds int ptrace_writedata(struct task_struct *tsk, char __user *src, unsigned long dst, int len)
5051da177e4SLinus Torvalds {
5061da177e4SLinus Torvalds 	int copied = 0;
5071da177e4SLinus Torvalds 
5081da177e4SLinus Torvalds 	while (len > 0) {
5091da177e4SLinus Torvalds 		char buf[128];
5101da177e4SLinus Torvalds 		int this_len, retval;
5111da177e4SLinus Torvalds 
5121da177e4SLinus Torvalds 		this_len = (len > sizeof(buf)) ? sizeof(buf) : len;
5131da177e4SLinus Torvalds 		if (copy_from_user(buf, src, this_len))
5141da177e4SLinus Torvalds 			return -EFAULT;
5151da177e4SLinus Torvalds 		retval = access_process_vm(tsk, dst, buf, this_len, 1);
5161da177e4SLinus Torvalds 		if (!retval) {
5171da177e4SLinus Torvalds 			if (copied)
5181da177e4SLinus Torvalds 				break;
5191da177e4SLinus Torvalds 			return -EIO;
5201da177e4SLinus Torvalds 		}
5211da177e4SLinus Torvalds 		copied += retval;
5221da177e4SLinus Torvalds 		src += retval;
5231da177e4SLinus Torvalds 		dst += retval;
5241da177e4SLinus Torvalds 		len -= retval;
5251da177e4SLinus Torvalds 	}
5261da177e4SLinus Torvalds 	return copied;
5271da177e4SLinus Torvalds }
5281da177e4SLinus Torvalds 
5294abf9869SNamhyung Kim static int ptrace_setoptions(struct task_struct *child, unsigned long data)
5301da177e4SLinus Torvalds {
5318c5cf9e5SDenys Vlasenko 	if (data & ~(unsigned long)PTRACE_O_MASK)
5328c5cf9e5SDenys Vlasenko 		return -EINVAL;
5338c5cf9e5SDenys Vlasenko 
5341da177e4SLinus Torvalds 	child->ptrace &= ~PT_TRACE_MASK;
5351da177e4SLinus Torvalds 
5361da177e4SLinus Torvalds 	if (data & PTRACE_O_TRACESYSGOOD)
5371da177e4SLinus Torvalds 		child->ptrace |= PT_TRACESYSGOOD;
5381da177e4SLinus Torvalds 
5391da177e4SLinus Torvalds 	if (data & PTRACE_O_TRACEFORK)
5401da177e4SLinus Torvalds 		child->ptrace |= PT_TRACE_FORK;
5411da177e4SLinus Torvalds 
5421da177e4SLinus Torvalds 	if (data & PTRACE_O_TRACEVFORK)
5431da177e4SLinus Torvalds 		child->ptrace |= PT_TRACE_VFORK;
5441da177e4SLinus Torvalds 
5451da177e4SLinus Torvalds 	if (data & PTRACE_O_TRACECLONE)
5461da177e4SLinus Torvalds 		child->ptrace |= PT_TRACE_CLONE;
5471da177e4SLinus Torvalds 
5481da177e4SLinus Torvalds 	if (data & PTRACE_O_TRACEEXEC)
5491da177e4SLinus Torvalds 		child->ptrace |= PT_TRACE_EXEC;
5501da177e4SLinus Torvalds 
5511da177e4SLinus Torvalds 	if (data & PTRACE_O_TRACEVFORKDONE)
5521da177e4SLinus Torvalds 		child->ptrace |= PT_TRACE_VFORK_DONE;
5531da177e4SLinus Torvalds 
5541da177e4SLinus Torvalds 	if (data & PTRACE_O_TRACEEXIT)
5551da177e4SLinus Torvalds 		child->ptrace |= PT_TRACE_EXIT;
5561da177e4SLinus Torvalds 
5578c5cf9e5SDenys Vlasenko 	return 0;
5581da177e4SLinus Torvalds }
5591da177e4SLinus Torvalds 
560e16b2781SRoland McGrath static int ptrace_getsiginfo(struct task_struct *child, siginfo_t *info)
5611da177e4SLinus Torvalds {
562e4961254SOleg Nesterov 	unsigned long flags;
5631da177e4SLinus Torvalds 	int error = -ESRCH;
5641da177e4SLinus Torvalds 
565e4961254SOleg Nesterov 	if (lock_task_sighand(child, &flags)) {
5661da177e4SLinus Torvalds 		error = -EINVAL;
5671da177e4SLinus Torvalds 		if (likely(child->last_siginfo != NULL)) {
568e16b2781SRoland McGrath 			*info = *child->last_siginfo;
5691da177e4SLinus Torvalds 			error = 0;
5701da177e4SLinus Torvalds 		}
571e4961254SOleg Nesterov 		unlock_task_sighand(child, &flags);
5721da177e4SLinus Torvalds 	}
5731da177e4SLinus Torvalds 	return error;
5741da177e4SLinus Torvalds }
5751da177e4SLinus Torvalds 
576e16b2781SRoland McGrath static int ptrace_setsiginfo(struct task_struct *child, const siginfo_t *info)
5771da177e4SLinus Torvalds {
578e4961254SOleg Nesterov 	unsigned long flags;
5791da177e4SLinus Torvalds 	int error = -ESRCH;
5801da177e4SLinus Torvalds 
581e4961254SOleg Nesterov 	if (lock_task_sighand(child, &flags)) {
5821da177e4SLinus Torvalds 		error = -EINVAL;
5831da177e4SLinus Torvalds 		if (likely(child->last_siginfo != NULL)) {
584e16b2781SRoland McGrath 			*child->last_siginfo = *info;
5851da177e4SLinus Torvalds 			error = 0;
5861da177e4SLinus Torvalds 		}
587e4961254SOleg Nesterov 		unlock_task_sighand(child, &flags);
5881da177e4SLinus Torvalds 	}
5891da177e4SLinus Torvalds 	return error;
5901da177e4SLinus Torvalds }
5911da177e4SLinus Torvalds 
59236df29d7SRoland McGrath 
59336df29d7SRoland McGrath #ifdef PTRACE_SINGLESTEP
59436df29d7SRoland McGrath #define is_singlestep(request)		((request) == PTRACE_SINGLESTEP)
59536df29d7SRoland McGrath #else
59636df29d7SRoland McGrath #define is_singlestep(request)		0
59736df29d7SRoland McGrath #endif
59836df29d7SRoland McGrath 
5995b88abbfSRoland McGrath #ifdef PTRACE_SINGLEBLOCK
6005b88abbfSRoland McGrath #define is_singleblock(request)		((request) == PTRACE_SINGLEBLOCK)
6015b88abbfSRoland McGrath #else
6025b88abbfSRoland McGrath #define is_singleblock(request)		0
6035b88abbfSRoland McGrath #endif
6045b88abbfSRoland McGrath 
60536df29d7SRoland McGrath #ifdef PTRACE_SYSEMU
60636df29d7SRoland McGrath #define is_sysemu_singlestep(request)	((request) == PTRACE_SYSEMU_SINGLESTEP)
60736df29d7SRoland McGrath #else
60836df29d7SRoland McGrath #define is_sysemu_singlestep(request)	0
60936df29d7SRoland McGrath #endif
61036df29d7SRoland McGrath 
6114abf9869SNamhyung Kim static int ptrace_resume(struct task_struct *child, long request,
6124abf9869SNamhyung Kim 			 unsigned long data)
61336df29d7SRoland McGrath {
61436df29d7SRoland McGrath 	if (!valid_signal(data))
61536df29d7SRoland McGrath 		return -EIO;
61636df29d7SRoland McGrath 
61736df29d7SRoland McGrath 	if (request == PTRACE_SYSCALL)
61836df29d7SRoland McGrath 		set_tsk_thread_flag(child, TIF_SYSCALL_TRACE);
61936df29d7SRoland McGrath 	else
62036df29d7SRoland McGrath 		clear_tsk_thread_flag(child, TIF_SYSCALL_TRACE);
62136df29d7SRoland McGrath 
62236df29d7SRoland McGrath #ifdef TIF_SYSCALL_EMU
62336df29d7SRoland McGrath 	if (request == PTRACE_SYSEMU || request == PTRACE_SYSEMU_SINGLESTEP)
62436df29d7SRoland McGrath 		set_tsk_thread_flag(child, TIF_SYSCALL_EMU);
62536df29d7SRoland McGrath 	else
62636df29d7SRoland McGrath 		clear_tsk_thread_flag(child, TIF_SYSCALL_EMU);
62736df29d7SRoland McGrath #endif
62836df29d7SRoland McGrath 
6295b88abbfSRoland McGrath 	if (is_singleblock(request)) {
6305b88abbfSRoland McGrath 		if (unlikely(!arch_has_block_step()))
6315b88abbfSRoland McGrath 			return -EIO;
6325b88abbfSRoland McGrath 		user_enable_block_step(child);
6335b88abbfSRoland McGrath 	} else if (is_singlestep(request) || is_sysemu_singlestep(request)) {
63436df29d7SRoland McGrath 		if (unlikely(!arch_has_single_step()))
63536df29d7SRoland McGrath 			return -EIO;
63636df29d7SRoland McGrath 		user_enable_single_step(child);
6373a709703SRoland McGrath 	} else {
63836df29d7SRoland McGrath 		user_disable_single_step(child);
6393a709703SRoland McGrath 	}
64036df29d7SRoland McGrath 
64136df29d7SRoland McGrath 	child->exit_code = data;
6420666fb51SOleg Nesterov 	wake_up_state(child, __TASK_TRACED);
64336df29d7SRoland McGrath 
64436df29d7SRoland McGrath 	return 0;
64536df29d7SRoland McGrath }
64636df29d7SRoland McGrath 
6472225a122SSuresh Siddha #ifdef CONFIG_HAVE_ARCH_TRACEHOOK
6482225a122SSuresh Siddha 
6492225a122SSuresh Siddha static const struct user_regset *
6502225a122SSuresh Siddha find_regset(const struct user_regset_view *view, unsigned int type)
6512225a122SSuresh Siddha {
6522225a122SSuresh Siddha 	const struct user_regset *regset;
6532225a122SSuresh Siddha 	int n;
6542225a122SSuresh Siddha 
6552225a122SSuresh Siddha 	for (n = 0; n < view->n; ++n) {
6562225a122SSuresh Siddha 		regset = view->regsets + n;
6572225a122SSuresh Siddha 		if (regset->core_note_type == type)
6582225a122SSuresh Siddha 			return regset;
6592225a122SSuresh Siddha 	}
6602225a122SSuresh Siddha 
6612225a122SSuresh Siddha 	return NULL;
6622225a122SSuresh Siddha }
6632225a122SSuresh Siddha 
6642225a122SSuresh Siddha static int ptrace_regset(struct task_struct *task, int req, unsigned int type,
6652225a122SSuresh Siddha 			 struct iovec *kiov)
6662225a122SSuresh Siddha {
6672225a122SSuresh Siddha 	const struct user_regset_view *view = task_user_regset_view(task);
6682225a122SSuresh Siddha 	const struct user_regset *regset = find_regset(view, type);
6692225a122SSuresh Siddha 	int regset_no;
6702225a122SSuresh Siddha 
6712225a122SSuresh Siddha 	if (!regset || (kiov->iov_len % regset->size) != 0)
672c6a0dd7eSSuresh Siddha 		return -EINVAL;
6732225a122SSuresh Siddha 
6742225a122SSuresh Siddha 	regset_no = regset - view->regsets;
6752225a122SSuresh Siddha 	kiov->iov_len = min(kiov->iov_len,
6762225a122SSuresh Siddha 			    (__kernel_size_t) (regset->n * regset->size));
6772225a122SSuresh Siddha 
6782225a122SSuresh Siddha 	if (req == PTRACE_GETREGSET)
6792225a122SSuresh Siddha 		return copy_regset_to_user(task, view, regset_no, 0,
6802225a122SSuresh Siddha 					   kiov->iov_len, kiov->iov_base);
6812225a122SSuresh Siddha 	else
6822225a122SSuresh Siddha 		return copy_regset_from_user(task, view, regset_no, 0,
6832225a122SSuresh Siddha 					     kiov->iov_len, kiov->iov_base);
6842225a122SSuresh Siddha }
6852225a122SSuresh Siddha 
6862225a122SSuresh Siddha #endif
6872225a122SSuresh Siddha 
6881da177e4SLinus Torvalds int ptrace_request(struct task_struct *child, long request,
6894abf9869SNamhyung Kim 		   unsigned long addr, unsigned long data)
6901da177e4SLinus Torvalds {
691fca26f26STejun Heo 	bool seized = child->ptrace & PT_SEIZED;
6921da177e4SLinus Torvalds 	int ret = -EIO;
693544b2c91STejun Heo 	siginfo_t siginfo, *si;
6949fed81dcSNamhyung Kim 	void __user *datavp = (void __user *) data;
6959fed81dcSNamhyung Kim 	unsigned long __user *datalp = datavp;
696fca26f26STejun Heo 	unsigned long flags;
6971da177e4SLinus Torvalds 
6981da177e4SLinus Torvalds 	switch (request) {
69916c3e389SRoland McGrath 	case PTRACE_PEEKTEXT:
70016c3e389SRoland McGrath 	case PTRACE_PEEKDATA:
70116c3e389SRoland McGrath 		return generic_ptrace_peekdata(child, addr, data);
70216c3e389SRoland McGrath 	case PTRACE_POKETEXT:
70316c3e389SRoland McGrath 	case PTRACE_POKEDATA:
70416c3e389SRoland McGrath 		return generic_ptrace_pokedata(child, addr, data);
70516c3e389SRoland McGrath 
7061da177e4SLinus Torvalds #ifdef PTRACE_OLDSETOPTIONS
7071da177e4SLinus Torvalds 	case PTRACE_OLDSETOPTIONS:
7081da177e4SLinus Torvalds #endif
7091da177e4SLinus Torvalds 	case PTRACE_SETOPTIONS:
7101da177e4SLinus Torvalds 		ret = ptrace_setoptions(child, data);
7111da177e4SLinus Torvalds 		break;
7121da177e4SLinus Torvalds 	case PTRACE_GETEVENTMSG:
7139fed81dcSNamhyung Kim 		ret = put_user(child->ptrace_message, datalp);
7141da177e4SLinus Torvalds 		break;
715e16b2781SRoland McGrath 
7161da177e4SLinus Torvalds 	case PTRACE_GETSIGINFO:
717e16b2781SRoland McGrath 		ret = ptrace_getsiginfo(child, &siginfo);
718e16b2781SRoland McGrath 		if (!ret)
7199fed81dcSNamhyung Kim 			ret = copy_siginfo_to_user(datavp, &siginfo);
7201da177e4SLinus Torvalds 		break;
721e16b2781SRoland McGrath 
7221da177e4SLinus Torvalds 	case PTRACE_SETSIGINFO:
7239fed81dcSNamhyung Kim 		if (copy_from_user(&siginfo, datavp, sizeof siginfo))
724e16b2781SRoland McGrath 			ret = -EFAULT;
725e16b2781SRoland McGrath 		else
726e16b2781SRoland McGrath 			ret = ptrace_setsiginfo(child, &siginfo);
7271da177e4SLinus Torvalds 		break;
728e16b2781SRoland McGrath 
729fca26f26STejun Heo 	case PTRACE_INTERRUPT:
730fca26f26STejun Heo 		/*
731fca26f26STejun Heo 		 * Stop tracee without any side-effect on signal or job
732fca26f26STejun Heo 		 * control.  At least one trap is guaranteed to happen
733fca26f26STejun Heo 		 * after this request.  If @child is already trapped, the
734fca26f26STejun Heo 		 * current trap is not disturbed and another trap will
735fca26f26STejun Heo 		 * happen after the current trap is ended with PTRACE_CONT.
736fca26f26STejun Heo 		 *
737fca26f26STejun Heo 		 * The actual trap might not be PTRACE_EVENT_STOP trap but
738fca26f26STejun Heo 		 * the pending condition is cleared regardless.
739fca26f26STejun Heo 		 */
740fca26f26STejun Heo 		if (unlikely(!seized || !lock_task_sighand(child, &flags)))
741fca26f26STejun Heo 			break;
742fca26f26STejun Heo 
743544b2c91STejun Heo 		/*
744544b2c91STejun Heo 		 * INTERRUPT doesn't disturb existing trap sans one
745544b2c91STejun Heo 		 * exception.  If ptracer issued LISTEN for the current
746544b2c91STejun Heo 		 * STOP, this INTERRUPT should clear LISTEN and re-trap
747544b2c91STejun Heo 		 * tracee into STOP.
748544b2c91STejun Heo 		 */
749fca26f26STejun Heo 		if (likely(task_set_jobctl_pending(child, JOBCTL_TRAP_STOP)))
750544b2c91STejun Heo 			signal_wake_up(child, child->jobctl & JOBCTL_LISTENING);
751544b2c91STejun Heo 
752544b2c91STejun Heo 		unlock_task_sighand(child, &flags);
753544b2c91STejun Heo 		ret = 0;
754544b2c91STejun Heo 		break;
755544b2c91STejun Heo 
756544b2c91STejun Heo 	case PTRACE_LISTEN:
757544b2c91STejun Heo 		/*
758544b2c91STejun Heo 		 * Listen for events.  Tracee must be in STOP.  It's not
759544b2c91STejun Heo 		 * resumed per-se but is not considered to be in TRACED by
760544b2c91STejun Heo 		 * wait(2) or ptrace(2).  If an async event (e.g. group
761544b2c91STejun Heo 		 * stop state change) happens, tracee will enter STOP trap
762544b2c91STejun Heo 		 * again.  Alternatively, ptracer can issue INTERRUPT to
763544b2c91STejun Heo 		 * finish listening and re-trap tracee into STOP.
764544b2c91STejun Heo 		 */
765544b2c91STejun Heo 		if (unlikely(!seized || !lock_task_sighand(child, &flags)))
766544b2c91STejun Heo 			break;
767544b2c91STejun Heo 
768544b2c91STejun Heo 		si = child->last_siginfo;
769f9d81f61SOleg Nesterov 		if (likely(si && (si->si_code >> 8) == PTRACE_EVENT_STOP)) {
770544b2c91STejun Heo 			child->jobctl |= JOBCTL_LISTENING;
771544b2c91STejun Heo 			/*
772f9d81f61SOleg Nesterov 			 * If NOTIFY is set, it means event happened between
773f9d81f61SOleg Nesterov 			 * start of this trap and now.  Trigger re-trap.
774544b2c91STejun Heo 			 */
775544b2c91STejun Heo 			if (child->jobctl & JOBCTL_TRAP_NOTIFY)
776544b2c91STejun Heo 				signal_wake_up(child, true);
777fca26f26STejun Heo 			ret = 0;
778f9d81f61SOleg Nesterov 		}
779f9d81f61SOleg Nesterov 		unlock_task_sighand(child, &flags);
780fca26f26STejun Heo 		break;
781fca26f26STejun Heo 
7821bcf5482SAlexey Dobriyan 	case PTRACE_DETACH:	 /* detach a process that was attached. */
7831bcf5482SAlexey Dobriyan 		ret = ptrace_detach(child, data);
7841bcf5482SAlexey Dobriyan 		break;
78536df29d7SRoland McGrath 
7869c1a1259SMike Frysinger #ifdef CONFIG_BINFMT_ELF_FDPIC
7879c1a1259SMike Frysinger 	case PTRACE_GETFDPIC: {
788e0129ef9SOleg Nesterov 		struct mm_struct *mm = get_task_mm(child);
7899c1a1259SMike Frysinger 		unsigned long tmp = 0;
7909c1a1259SMike Frysinger 
791e0129ef9SOleg Nesterov 		ret = -ESRCH;
792e0129ef9SOleg Nesterov 		if (!mm)
793e0129ef9SOleg Nesterov 			break;
794e0129ef9SOleg Nesterov 
7959c1a1259SMike Frysinger 		switch (addr) {
7969c1a1259SMike Frysinger 		case PTRACE_GETFDPIC_EXEC:
797e0129ef9SOleg Nesterov 			tmp = mm->context.exec_fdpic_loadmap;
7989c1a1259SMike Frysinger 			break;
7999c1a1259SMike Frysinger 		case PTRACE_GETFDPIC_INTERP:
800e0129ef9SOleg Nesterov 			tmp = mm->context.interp_fdpic_loadmap;
8019c1a1259SMike Frysinger 			break;
8029c1a1259SMike Frysinger 		default:
8039c1a1259SMike Frysinger 			break;
8049c1a1259SMike Frysinger 		}
805e0129ef9SOleg Nesterov 		mmput(mm);
8069c1a1259SMike Frysinger 
8079fed81dcSNamhyung Kim 		ret = put_user(tmp, datalp);
8089c1a1259SMike Frysinger 		break;
8099c1a1259SMike Frysinger 	}
8109c1a1259SMike Frysinger #endif
8119c1a1259SMike Frysinger 
81236df29d7SRoland McGrath #ifdef PTRACE_SINGLESTEP
81336df29d7SRoland McGrath 	case PTRACE_SINGLESTEP:
81436df29d7SRoland McGrath #endif
8155b88abbfSRoland McGrath #ifdef PTRACE_SINGLEBLOCK
8165b88abbfSRoland McGrath 	case PTRACE_SINGLEBLOCK:
8175b88abbfSRoland McGrath #endif
81836df29d7SRoland McGrath #ifdef PTRACE_SYSEMU
81936df29d7SRoland McGrath 	case PTRACE_SYSEMU:
82036df29d7SRoland McGrath 	case PTRACE_SYSEMU_SINGLESTEP:
82136df29d7SRoland McGrath #endif
82236df29d7SRoland McGrath 	case PTRACE_SYSCALL:
82336df29d7SRoland McGrath 	case PTRACE_CONT:
82436df29d7SRoland McGrath 		return ptrace_resume(child, request, data);
82536df29d7SRoland McGrath 
82636df29d7SRoland McGrath 	case PTRACE_KILL:
82736df29d7SRoland McGrath 		if (child->exit_state)	/* already dead */
82836df29d7SRoland McGrath 			return 0;
82936df29d7SRoland McGrath 		return ptrace_resume(child, request, SIGKILL);
83036df29d7SRoland McGrath 
8312225a122SSuresh Siddha #ifdef CONFIG_HAVE_ARCH_TRACEHOOK
8322225a122SSuresh Siddha 	case PTRACE_GETREGSET:
8332225a122SSuresh Siddha 	case PTRACE_SETREGSET:
8342225a122SSuresh Siddha 	{
8352225a122SSuresh Siddha 		struct iovec kiov;
8369fed81dcSNamhyung Kim 		struct iovec __user *uiov = datavp;
8372225a122SSuresh Siddha 
8382225a122SSuresh Siddha 		if (!access_ok(VERIFY_WRITE, uiov, sizeof(*uiov)))
8392225a122SSuresh Siddha 			return -EFAULT;
8402225a122SSuresh Siddha 
8412225a122SSuresh Siddha 		if (__get_user(kiov.iov_base, &uiov->iov_base) ||
8422225a122SSuresh Siddha 		    __get_user(kiov.iov_len, &uiov->iov_len))
8432225a122SSuresh Siddha 			return -EFAULT;
8442225a122SSuresh Siddha 
8452225a122SSuresh Siddha 		ret = ptrace_regset(child, request, addr, &kiov);
8462225a122SSuresh Siddha 		if (!ret)
8472225a122SSuresh Siddha 			ret = __put_user(kiov.iov_len, &uiov->iov_len);
8482225a122SSuresh Siddha 		break;
8492225a122SSuresh Siddha 	}
8502225a122SSuresh Siddha #endif
8511da177e4SLinus Torvalds 	default:
8521da177e4SLinus Torvalds 		break;
8531da177e4SLinus Torvalds 	}
8541da177e4SLinus Torvalds 
8551da177e4SLinus Torvalds 	return ret;
8561da177e4SLinus Torvalds }
857481bed45SChristoph Hellwig 
8588053bdd5SOleg Nesterov static struct task_struct *ptrace_get_task_struct(pid_t pid)
8596b9c7ed8SChristoph Hellwig {
8606b9c7ed8SChristoph Hellwig 	struct task_struct *child;
8616b9c7ed8SChristoph Hellwig 
8628053bdd5SOleg Nesterov 	rcu_read_lock();
863228ebcbeSPavel Emelyanov 	child = find_task_by_vpid(pid);
864481bed45SChristoph Hellwig 	if (child)
865481bed45SChristoph Hellwig 		get_task_struct(child);
8668053bdd5SOleg Nesterov 	rcu_read_unlock();
867f400e198SSukadev Bhattiprolu 
868481bed45SChristoph Hellwig 	if (!child)
8696b9c7ed8SChristoph Hellwig 		return ERR_PTR(-ESRCH);
8706b9c7ed8SChristoph Hellwig 	return child;
871481bed45SChristoph Hellwig }
872481bed45SChristoph Hellwig 
8730ac15559SChristoph Hellwig #ifndef arch_ptrace_attach
8740ac15559SChristoph Hellwig #define arch_ptrace_attach(child)	do { } while (0)
8750ac15559SChristoph Hellwig #endif
8760ac15559SChristoph Hellwig 
8774abf9869SNamhyung Kim SYSCALL_DEFINE4(ptrace, long, request, long, pid, unsigned long, addr,
8784abf9869SNamhyung Kim 		unsigned long, data)
879481bed45SChristoph Hellwig {
880481bed45SChristoph Hellwig 	struct task_struct *child;
881481bed45SChristoph Hellwig 	long ret;
882481bed45SChristoph Hellwig 
8836b9c7ed8SChristoph Hellwig 	if (request == PTRACE_TRACEME) {
8846b9c7ed8SChristoph Hellwig 		ret = ptrace_traceme();
8856ea6dd93SHaavard Skinnemoen 		if (!ret)
8866ea6dd93SHaavard Skinnemoen 			arch_ptrace_attach(current);
887481bed45SChristoph Hellwig 		goto out;
8886b9c7ed8SChristoph Hellwig 	}
8896b9c7ed8SChristoph Hellwig 
8906b9c7ed8SChristoph Hellwig 	child = ptrace_get_task_struct(pid);
8916b9c7ed8SChristoph Hellwig 	if (IS_ERR(child)) {
8926b9c7ed8SChristoph Hellwig 		ret = PTR_ERR(child);
8936b9c7ed8SChristoph Hellwig 		goto out;
8946b9c7ed8SChristoph Hellwig 	}
895481bed45SChristoph Hellwig 
8963544d72aSTejun Heo 	if (request == PTRACE_ATTACH || request == PTRACE_SEIZE) {
8973544d72aSTejun Heo 		ret = ptrace_attach(child, request, data);
8980ac15559SChristoph Hellwig 		/*
8990ac15559SChristoph Hellwig 		 * Some architectures need to do book-keeping after
9000ac15559SChristoph Hellwig 		 * a ptrace attach.
9010ac15559SChristoph Hellwig 		 */
9020ac15559SChristoph Hellwig 		if (!ret)
9030ac15559SChristoph Hellwig 			arch_ptrace_attach(child);
904005f18dfSChristoph Hellwig 		goto out_put_task_struct;
905481bed45SChristoph Hellwig 	}
906481bed45SChristoph Hellwig 
907fca26f26STejun Heo 	ret = ptrace_check_attach(child, request == PTRACE_KILL ||
908fca26f26STejun Heo 				  request == PTRACE_INTERRUPT);
909481bed45SChristoph Hellwig 	if (ret < 0)
910481bed45SChristoph Hellwig 		goto out_put_task_struct;
911481bed45SChristoph Hellwig 
912481bed45SChristoph Hellwig 	ret = arch_ptrace(child, request, addr, data);
913481bed45SChristoph Hellwig 
914481bed45SChristoph Hellwig  out_put_task_struct:
915481bed45SChristoph Hellwig 	put_task_struct(child);
916481bed45SChristoph Hellwig  out:
917481bed45SChristoph Hellwig 	return ret;
918481bed45SChristoph Hellwig }
91976647323SAlexey Dobriyan 
9204abf9869SNamhyung Kim int generic_ptrace_peekdata(struct task_struct *tsk, unsigned long addr,
9214abf9869SNamhyung Kim 			    unsigned long data)
92276647323SAlexey Dobriyan {
92376647323SAlexey Dobriyan 	unsigned long tmp;
92476647323SAlexey Dobriyan 	int copied;
92576647323SAlexey Dobriyan 
92676647323SAlexey Dobriyan 	copied = access_process_vm(tsk, addr, &tmp, sizeof(tmp), 0);
92776647323SAlexey Dobriyan 	if (copied != sizeof(tmp))
92876647323SAlexey Dobriyan 		return -EIO;
92976647323SAlexey Dobriyan 	return put_user(tmp, (unsigned long __user *)data);
93076647323SAlexey Dobriyan }
931f284ce72SAlexey Dobriyan 
9324abf9869SNamhyung Kim int generic_ptrace_pokedata(struct task_struct *tsk, unsigned long addr,
9334abf9869SNamhyung Kim 			    unsigned long data)
934f284ce72SAlexey Dobriyan {
935f284ce72SAlexey Dobriyan 	int copied;
936f284ce72SAlexey Dobriyan 
937f284ce72SAlexey Dobriyan 	copied = access_process_vm(tsk, addr, &data, sizeof(data), 1);
938f284ce72SAlexey Dobriyan 	return (copied == sizeof(data)) ? 0 : -EIO;
939f284ce72SAlexey Dobriyan }
940032d82d9SRoland McGrath 
94196b8936aSChristoph Hellwig #if defined CONFIG_COMPAT
942032d82d9SRoland McGrath #include <linux/compat.h>
943032d82d9SRoland McGrath 
944032d82d9SRoland McGrath int compat_ptrace_request(struct task_struct *child, compat_long_t request,
945032d82d9SRoland McGrath 			  compat_ulong_t addr, compat_ulong_t data)
946032d82d9SRoland McGrath {
947032d82d9SRoland McGrath 	compat_ulong_t __user *datap = compat_ptr(data);
948032d82d9SRoland McGrath 	compat_ulong_t word;
949e16b2781SRoland McGrath 	siginfo_t siginfo;
950032d82d9SRoland McGrath 	int ret;
951032d82d9SRoland McGrath 
952032d82d9SRoland McGrath 	switch (request) {
953032d82d9SRoland McGrath 	case PTRACE_PEEKTEXT:
954032d82d9SRoland McGrath 	case PTRACE_PEEKDATA:
955032d82d9SRoland McGrath 		ret = access_process_vm(child, addr, &word, sizeof(word), 0);
956032d82d9SRoland McGrath 		if (ret != sizeof(word))
957032d82d9SRoland McGrath 			ret = -EIO;
958032d82d9SRoland McGrath 		else
959032d82d9SRoland McGrath 			ret = put_user(word, datap);
960032d82d9SRoland McGrath 		break;
961032d82d9SRoland McGrath 
962032d82d9SRoland McGrath 	case PTRACE_POKETEXT:
963032d82d9SRoland McGrath 	case PTRACE_POKEDATA:
964032d82d9SRoland McGrath 		ret = access_process_vm(child, addr, &data, sizeof(data), 1);
965032d82d9SRoland McGrath 		ret = (ret != sizeof(data) ? -EIO : 0);
966032d82d9SRoland McGrath 		break;
967032d82d9SRoland McGrath 
968032d82d9SRoland McGrath 	case PTRACE_GETEVENTMSG:
969032d82d9SRoland McGrath 		ret = put_user((compat_ulong_t) child->ptrace_message, datap);
970032d82d9SRoland McGrath 		break;
971032d82d9SRoland McGrath 
972e16b2781SRoland McGrath 	case PTRACE_GETSIGINFO:
973e16b2781SRoland McGrath 		ret = ptrace_getsiginfo(child, &siginfo);
974e16b2781SRoland McGrath 		if (!ret)
975e16b2781SRoland McGrath 			ret = copy_siginfo_to_user32(
976e16b2781SRoland McGrath 				(struct compat_siginfo __user *) datap,
977e16b2781SRoland McGrath 				&siginfo);
978e16b2781SRoland McGrath 		break;
979e16b2781SRoland McGrath 
980e16b2781SRoland McGrath 	case PTRACE_SETSIGINFO:
981e16b2781SRoland McGrath 		memset(&siginfo, 0, sizeof siginfo);
982e16b2781SRoland McGrath 		if (copy_siginfo_from_user32(
983e16b2781SRoland McGrath 			    &siginfo, (struct compat_siginfo __user *) datap))
984e16b2781SRoland McGrath 			ret = -EFAULT;
985e16b2781SRoland McGrath 		else
986e16b2781SRoland McGrath 			ret = ptrace_setsiginfo(child, &siginfo);
987e16b2781SRoland McGrath 		break;
9882225a122SSuresh Siddha #ifdef CONFIG_HAVE_ARCH_TRACEHOOK
9892225a122SSuresh Siddha 	case PTRACE_GETREGSET:
9902225a122SSuresh Siddha 	case PTRACE_SETREGSET:
9912225a122SSuresh Siddha 	{
9922225a122SSuresh Siddha 		struct iovec kiov;
9932225a122SSuresh Siddha 		struct compat_iovec __user *uiov =
9942225a122SSuresh Siddha 			(struct compat_iovec __user *) datap;
9952225a122SSuresh Siddha 		compat_uptr_t ptr;
9962225a122SSuresh Siddha 		compat_size_t len;
9972225a122SSuresh Siddha 
9982225a122SSuresh Siddha 		if (!access_ok(VERIFY_WRITE, uiov, sizeof(*uiov)))
9992225a122SSuresh Siddha 			return -EFAULT;
10002225a122SSuresh Siddha 
10012225a122SSuresh Siddha 		if (__get_user(ptr, &uiov->iov_base) ||
10022225a122SSuresh Siddha 		    __get_user(len, &uiov->iov_len))
10032225a122SSuresh Siddha 			return -EFAULT;
10042225a122SSuresh Siddha 
10052225a122SSuresh Siddha 		kiov.iov_base = compat_ptr(ptr);
10062225a122SSuresh Siddha 		kiov.iov_len = len;
10072225a122SSuresh Siddha 
10082225a122SSuresh Siddha 		ret = ptrace_regset(child, request, addr, &kiov);
10092225a122SSuresh Siddha 		if (!ret)
10102225a122SSuresh Siddha 			ret = __put_user(kiov.iov_len, &uiov->iov_len);
10112225a122SSuresh Siddha 		break;
10122225a122SSuresh Siddha 	}
10132225a122SSuresh Siddha #endif
1014e16b2781SRoland McGrath 
1015032d82d9SRoland McGrath 	default:
1016032d82d9SRoland McGrath 		ret = ptrace_request(child, request, addr, data);
1017032d82d9SRoland McGrath 	}
1018032d82d9SRoland McGrath 
1019032d82d9SRoland McGrath 	return ret;
1020032d82d9SRoland McGrath }
1021c269f196SRoland McGrath 
1022c269f196SRoland McGrath asmlinkage long compat_sys_ptrace(compat_long_t request, compat_long_t pid,
1023c269f196SRoland McGrath 				  compat_long_t addr, compat_long_t data)
1024c269f196SRoland McGrath {
1025c269f196SRoland McGrath 	struct task_struct *child;
1026c269f196SRoland McGrath 	long ret;
1027c269f196SRoland McGrath 
1028c269f196SRoland McGrath 	if (request == PTRACE_TRACEME) {
1029c269f196SRoland McGrath 		ret = ptrace_traceme();
1030c269f196SRoland McGrath 		goto out;
1031c269f196SRoland McGrath 	}
1032c269f196SRoland McGrath 
1033c269f196SRoland McGrath 	child = ptrace_get_task_struct(pid);
1034c269f196SRoland McGrath 	if (IS_ERR(child)) {
1035c269f196SRoland McGrath 		ret = PTR_ERR(child);
1036c269f196SRoland McGrath 		goto out;
1037c269f196SRoland McGrath 	}
1038c269f196SRoland McGrath 
10393544d72aSTejun Heo 	if (request == PTRACE_ATTACH || request == PTRACE_SEIZE) {
10403544d72aSTejun Heo 		ret = ptrace_attach(child, request, data);
1041c269f196SRoland McGrath 		/*
1042c269f196SRoland McGrath 		 * Some architectures need to do book-keeping after
1043c269f196SRoland McGrath 		 * a ptrace attach.
1044c269f196SRoland McGrath 		 */
1045c269f196SRoland McGrath 		if (!ret)
1046c269f196SRoland McGrath 			arch_ptrace_attach(child);
1047c269f196SRoland McGrath 		goto out_put_task_struct;
1048c269f196SRoland McGrath 	}
1049c269f196SRoland McGrath 
1050fca26f26STejun Heo 	ret = ptrace_check_attach(child, request == PTRACE_KILL ||
1051fca26f26STejun Heo 				  request == PTRACE_INTERRUPT);
1052c269f196SRoland McGrath 	if (!ret)
1053c269f196SRoland McGrath 		ret = compat_arch_ptrace(child, request, addr, data);
1054c269f196SRoland McGrath 
1055c269f196SRoland McGrath  out_put_task_struct:
1056c269f196SRoland McGrath 	put_task_struct(child);
1057c269f196SRoland McGrath  out:
1058c269f196SRoland McGrath 	return ret;
1059c269f196SRoland McGrath }
106096b8936aSChristoph Hellwig #endif	/* CONFIG_COMPAT */
1061bf26c018SFrederic Weisbecker 
1062bf26c018SFrederic Weisbecker #ifdef CONFIG_HAVE_HW_BREAKPOINT
1063bf26c018SFrederic Weisbecker int ptrace_get_breakpoints(struct task_struct *tsk)
1064bf26c018SFrederic Weisbecker {
1065bf26c018SFrederic Weisbecker 	if (atomic_inc_not_zero(&tsk->ptrace_bp_refcnt))
1066bf26c018SFrederic Weisbecker 		return 0;
1067bf26c018SFrederic Weisbecker 
1068bf26c018SFrederic Weisbecker 	return -1;
1069bf26c018SFrederic Weisbecker }
1070bf26c018SFrederic Weisbecker 
1071bf26c018SFrederic Weisbecker void ptrace_put_breakpoints(struct task_struct *tsk)
1072bf26c018SFrederic Weisbecker {
1073bf26c018SFrederic Weisbecker 	if (atomic_dec_and_test(&tsk->ptrace_bp_refcnt))
1074bf26c018SFrederic Weisbecker 		flush_ptrace_hw_breakpoint(tsk);
1075bf26c018SFrederic Weisbecker }
1076bf26c018SFrederic Weisbecker #endif /* CONFIG_HAVE_HW_BREAKPOINT */
1077