xref: /openbmc/linux/kernel/ptrace.c (revision e8440c14)
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))
120910ffdb1SOleg Nesterov 		ptrace_signal_wake_up(child, true);
1210e9f0a4aSTejun Heo 
1221da177e4SLinus Torvalds 	spin_unlock(&child->sighand->siglock);
1231da177e4SLinus Torvalds }
1241da177e4SLinus Torvalds 
1259899d11fSOleg Nesterov /* Ensure that nothing can wake it up, even SIGKILL */
1269899d11fSOleg Nesterov static bool ptrace_freeze_traced(struct task_struct *task)
1279899d11fSOleg Nesterov {
1289899d11fSOleg Nesterov 	bool ret = false;
1299899d11fSOleg Nesterov 
1309899d11fSOleg Nesterov 	/* Lockless, nobody but us can set this flag */
1319899d11fSOleg Nesterov 	if (task->jobctl & JOBCTL_LISTENING)
1329899d11fSOleg Nesterov 		return ret;
1339899d11fSOleg Nesterov 
1349899d11fSOleg Nesterov 	spin_lock_irq(&task->sighand->siglock);
1359899d11fSOleg Nesterov 	if (task_is_traced(task) && !__fatal_signal_pending(task)) {
1369899d11fSOleg Nesterov 		task->state = __TASK_TRACED;
1379899d11fSOleg Nesterov 		ret = true;
1389899d11fSOleg Nesterov 	}
1399899d11fSOleg Nesterov 	spin_unlock_irq(&task->sighand->siglock);
1409899d11fSOleg Nesterov 
1419899d11fSOleg Nesterov 	return ret;
1429899d11fSOleg Nesterov }
1439899d11fSOleg Nesterov 
1449899d11fSOleg Nesterov static void ptrace_unfreeze_traced(struct task_struct *task)
1459899d11fSOleg Nesterov {
1469899d11fSOleg Nesterov 	if (task->state != __TASK_TRACED)
1479899d11fSOleg Nesterov 		return;
1489899d11fSOleg Nesterov 
1499899d11fSOleg Nesterov 	WARN_ON(!task->ptrace || task->parent != current);
1509899d11fSOleg Nesterov 
1519899d11fSOleg Nesterov 	spin_lock_irq(&task->sighand->siglock);
1529899d11fSOleg Nesterov 	if (__fatal_signal_pending(task))
1539899d11fSOleg Nesterov 		wake_up_state(task, __TASK_TRACED);
1549899d11fSOleg Nesterov 	else
1559899d11fSOleg Nesterov 		task->state = TASK_TRACED;
1569899d11fSOleg Nesterov 	spin_unlock_irq(&task->sighand->siglock);
1579899d11fSOleg Nesterov }
1589899d11fSOleg Nesterov 
159755e276bSTejun Heo /**
160755e276bSTejun Heo  * ptrace_check_attach - check whether ptracee is ready for ptrace operation
161755e276bSTejun Heo  * @child: ptracee to check for
162755e276bSTejun Heo  * @ignore_state: don't check whether @child is currently %TASK_TRACED
163755e276bSTejun Heo  *
164755e276bSTejun Heo  * Check whether @child is being ptraced by %current and ready for further
165755e276bSTejun Heo  * ptrace operations.  If @ignore_state is %false, @child also should be in
166755e276bSTejun Heo  * %TASK_TRACED state and on return the child is guaranteed to be traced
167755e276bSTejun Heo  * and not executing.  If @ignore_state is %true, @child can be in any
168755e276bSTejun Heo  * state.
169755e276bSTejun Heo  *
170755e276bSTejun Heo  * CONTEXT:
171755e276bSTejun Heo  * Grabs and releases tasklist_lock and @child->sighand->siglock.
172755e276bSTejun Heo  *
173755e276bSTejun Heo  * RETURNS:
174755e276bSTejun Heo  * 0 on success, -ESRCH if %child is not ready.
1751da177e4SLinus Torvalds  */
176edea0d03SOleg Nesterov static int ptrace_check_attach(struct task_struct *child, bool ignore_state)
1771da177e4SLinus Torvalds {
1781da177e4SLinus Torvalds 	int ret = -ESRCH;
1791da177e4SLinus Torvalds 
1801da177e4SLinus Torvalds 	/*
1811da177e4SLinus Torvalds 	 * We take the read lock around doing both checks to close a
1821da177e4SLinus Torvalds 	 * possible race where someone else was tracing our child and
1831da177e4SLinus Torvalds 	 * detached between these two checks.  After this locked check,
1841da177e4SLinus Torvalds 	 * we are sure that this is our traced child and that can only
1851da177e4SLinus Torvalds 	 * be changed by us so it's not changing right after this.
1861da177e4SLinus Torvalds 	 */
1871da177e4SLinus Torvalds 	read_lock(&tasklist_lock);
1889899d11fSOleg Nesterov 	if (child->ptrace && child->parent == current) {
1899899d11fSOleg Nesterov 		WARN_ON(child->state == __TASK_TRACED);
190c0c0b649SOleg Nesterov 		/*
191c0c0b649SOleg Nesterov 		 * child->sighand can't be NULL, release_task()
192c0c0b649SOleg Nesterov 		 * does ptrace_unlink() before __exit_signal().
193c0c0b649SOleg Nesterov 		 */
1949899d11fSOleg Nesterov 		if (ignore_state || ptrace_freeze_traced(child))
195321fb561SOleg Nesterov 			ret = 0;
1961da177e4SLinus Torvalds 	}
1971da177e4SLinus Torvalds 	read_unlock(&tasklist_lock);
1981da177e4SLinus Torvalds 
1999899d11fSOleg Nesterov 	if (!ret && !ignore_state) {
2009899d11fSOleg Nesterov 		if (!wait_task_inactive(child, __TASK_TRACED)) {
2019899d11fSOleg Nesterov 			/*
2029899d11fSOleg Nesterov 			 * This can only happen if may_ptrace_stop() fails and
2039899d11fSOleg Nesterov 			 * ptrace_stop() changes ->state back to TASK_RUNNING,
2049899d11fSOleg Nesterov 			 * so we should not worry about leaking __TASK_TRACED.
2059899d11fSOleg Nesterov 			 */
2069899d11fSOleg Nesterov 			WARN_ON(child->state == __TASK_TRACED);
2079899d11fSOleg Nesterov 			ret = -ESRCH;
2089899d11fSOleg Nesterov 		}
2099899d11fSOleg Nesterov 	}
2101da177e4SLinus Torvalds 
2111da177e4SLinus Torvalds 	return ret;
2121da177e4SLinus Torvalds }
2131da177e4SLinus Torvalds 
21469f594a3SEric Paris static int ptrace_has_cap(struct user_namespace *ns, unsigned int mode)
21569f594a3SEric Paris {
21669f594a3SEric Paris 	if (mode & PTRACE_MODE_NOAUDIT)
21769f594a3SEric Paris 		return has_ns_capability_noaudit(current, ns, CAP_SYS_PTRACE);
21869f594a3SEric Paris 	else
21969f594a3SEric Paris 		return has_ns_capability(current, ns, CAP_SYS_PTRACE);
22069f594a3SEric Paris }
22169f594a3SEric Paris 
2229f99798fSTetsuo Handa /* Returns 0 on success, -errno on denial. */
2239f99798fSTetsuo Handa static int __ptrace_may_access(struct task_struct *task, unsigned int mode)
224ab8d11beSMiklos Szeredi {
225c69e8d9cSDavid Howells 	const struct cred *cred = current_cred(), *tcred;
226b6dff3ecSDavid Howells 
227df26c40eSEric W. Biederman 	/* May we inspect the given task?
228df26c40eSEric W. Biederman 	 * This check is used both for attaching with ptrace
229df26c40eSEric W. Biederman 	 * and for allowing access to sensitive information in /proc.
230df26c40eSEric W. Biederman 	 *
231df26c40eSEric W. Biederman 	 * ptrace_attach denies several cases that /proc allows
232df26c40eSEric W. Biederman 	 * because setting up the necessary parent/child relationship
233df26c40eSEric W. Biederman 	 * or halting the specified task is impossible.
234df26c40eSEric W. Biederman 	 */
235df26c40eSEric W. Biederman 	int dumpable = 0;
236df26c40eSEric W. Biederman 	/* Don't let security modules deny introspection */
237df26c40eSEric W. Biederman 	if (task == current)
238df26c40eSEric W. Biederman 		return 0;
239c69e8d9cSDavid Howells 	rcu_read_lock();
240c69e8d9cSDavid Howells 	tcred = __task_cred(task);
2415af66203SEric W. Biederman 	if (uid_eq(cred->uid, tcred->euid) &&
2425af66203SEric W. Biederman 	    uid_eq(cred->uid, tcred->suid) &&
2435af66203SEric W. Biederman 	    uid_eq(cred->uid, tcred->uid)  &&
2445af66203SEric W. Biederman 	    gid_eq(cred->gid, tcred->egid) &&
2455af66203SEric W. Biederman 	    gid_eq(cred->gid, tcred->sgid) &&
2465af66203SEric W. Biederman 	    gid_eq(cred->gid, tcred->gid))
2478409cca7SSerge E. Hallyn 		goto ok;
248c4a4d603SEric W. Biederman 	if (ptrace_has_cap(tcred->user_ns, mode))
2498409cca7SSerge E. Hallyn 		goto ok;
250c69e8d9cSDavid Howells 	rcu_read_unlock();
251ab8d11beSMiklos Szeredi 	return -EPERM;
2528409cca7SSerge E. Hallyn ok:
253c69e8d9cSDavid Howells 	rcu_read_unlock();
254ab8d11beSMiklos Szeredi 	smp_rmb();
255df26c40eSEric W. Biederman 	if (task->mm)
2566c5d5238SKawai, Hidehiro 		dumpable = get_dumpable(task->mm);
2574c44aaafSEric W. Biederman 	rcu_read_lock();
2584c44aaafSEric W. Biederman 	if (!dumpable && !ptrace_has_cap(__task_cred(task)->user_ns, mode)) {
2594c44aaafSEric W. Biederman 		rcu_read_unlock();
260ab8d11beSMiklos Szeredi 		return -EPERM;
2614c44aaafSEric W. Biederman 	}
2624c44aaafSEric W. Biederman 	rcu_read_unlock();
263ab8d11beSMiklos Szeredi 
2649e48858fSIngo Molnar 	return security_ptrace_access_check(task, mode);
265ab8d11beSMiklos Szeredi }
266ab8d11beSMiklos Szeredi 
267006ebb40SStephen Smalley bool ptrace_may_access(struct task_struct *task, unsigned int mode)
268ab8d11beSMiklos Szeredi {
269ab8d11beSMiklos Szeredi 	int err;
270ab8d11beSMiklos Szeredi 	task_lock(task);
271006ebb40SStephen Smalley 	err = __ptrace_may_access(task, mode);
272ab8d11beSMiklos Szeredi 	task_unlock(task);
2733a709703SRoland McGrath 	return !err;
274ab8d11beSMiklos Szeredi }
275ab8d11beSMiklos Szeredi 
2763544d72aSTejun Heo static int ptrace_attach(struct task_struct *task, long request,
277aa9147c9SDenys Vlasenko 			 unsigned long addr,
2783544d72aSTejun Heo 			 unsigned long flags)
2791da177e4SLinus Torvalds {
2803544d72aSTejun Heo 	bool seize = (request == PTRACE_SEIZE);
2811da177e4SLinus Torvalds 	int retval;
282f5b40e36SLinus Torvalds 
2833544d72aSTejun Heo 	retval = -EIO;
284aa9147c9SDenys Vlasenko 	if (seize) {
285aa9147c9SDenys Vlasenko 		if (addr != 0)
2863544d72aSTejun Heo 			goto out;
287aa9147c9SDenys Vlasenko 		if (flags & ~(unsigned long)PTRACE_O_MASK)
288aa9147c9SDenys Vlasenko 			goto out;
289aa9147c9SDenys Vlasenko 		flags = PT_PTRACED | PT_SEIZED | (flags << PT_OPT_FLAG_SHIFT);
290aa9147c9SDenys Vlasenko 	} else {
291aa9147c9SDenys Vlasenko 		flags = PT_PTRACED;
292aa9147c9SDenys Vlasenko 	}
2933544d72aSTejun Heo 
294a5cb013dSAl Viro 	audit_ptrace(task);
295a5cb013dSAl Viro 
2961da177e4SLinus Torvalds 	retval = -EPERM;
297b79b7ba9SOleg Nesterov 	if (unlikely(task->flags & PF_KTHREAD))
298b79b7ba9SOleg Nesterov 		goto out;
299bac0abd6SPavel Emelyanov 	if (same_thread_group(task, current))
300f5b40e36SLinus Torvalds 		goto out;
301f5b40e36SLinus Torvalds 
302f2f0b00aSOleg Nesterov 	/*
303f2f0b00aSOleg Nesterov 	 * Protect exec's credential calculations against our interference;
30486b6c1f3SDenys Vlasenko 	 * SUID, SGID and LSM creds get determined differently
3055e751e99SDavid Howells 	 * under ptrace.
306d84f4f99SDavid Howells 	 */
307793285fcSOleg Nesterov 	retval = -ERESTARTNOINTR;
3089b1bf12dSKOSAKI Motohiro 	if (mutex_lock_interruptible(&task->signal->cred_guard_mutex))
309d84f4f99SDavid Howells 		goto out;
3104b105cbbSOleg Nesterov 
311f5b40e36SLinus Torvalds 	task_lock(task);
312006ebb40SStephen Smalley 	retval = __ptrace_may_access(task, PTRACE_MODE_ATTACH);
3134b105cbbSOleg Nesterov 	task_unlock(task);
3141da177e4SLinus Torvalds 	if (retval)
3154b105cbbSOleg Nesterov 		goto unlock_creds;
3161da177e4SLinus Torvalds 
3174b105cbbSOleg Nesterov 	write_lock_irq(&tasklist_lock);
318b79b7ba9SOleg Nesterov 	retval = -EPERM;
319b79b7ba9SOleg Nesterov 	if (unlikely(task->exit_state))
3204b105cbbSOleg Nesterov 		goto unlock_tasklist;
321f2f0b00aSOleg Nesterov 	if (task->ptrace)
3224b105cbbSOleg Nesterov 		goto unlock_tasklist;
323b79b7ba9SOleg Nesterov 
3243544d72aSTejun Heo 	if (seize)
325aa9147c9SDenys Vlasenko 		flags |= PT_SEIZED;
3264c44aaafSEric W. Biederman 	rcu_read_lock();
3274c44aaafSEric W. Biederman 	if (ns_capable(__task_cred(task)->user_ns, CAP_SYS_PTRACE))
328aa9147c9SDenys Vlasenko 		flags |= PT_PTRACE_CAP;
3294c44aaafSEric W. Biederman 	rcu_read_unlock();
330aa9147c9SDenys Vlasenko 	task->ptrace = flags;
3311da177e4SLinus Torvalds 
3321da177e4SLinus Torvalds 	__ptrace_link(task, current);
3333544d72aSTejun Heo 
3343544d72aSTejun Heo 	/* SEIZE doesn't trap tracee on attach */
3353544d72aSTejun Heo 	if (!seize)
33633e9fc7dSOleg Nesterov 		send_sig_info(SIGSTOP, SEND_SIG_FORCED, task);
337b79b7ba9SOleg Nesterov 
338d79fdd6dSTejun Heo 	spin_lock(&task->sighand->siglock);
339d79fdd6dSTejun Heo 
340d79fdd6dSTejun Heo 	/*
34173ddff2bSTejun Heo 	 * If the task is already STOPPED, set JOBCTL_TRAP_STOP and
342d79fdd6dSTejun Heo 	 * TRAPPING, and kick it so that it transits to TRACED.  TRAPPING
343d79fdd6dSTejun Heo 	 * will be cleared if the child completes the transition or any
344d79fdd6dSTejun Heo 	 * event which clears the group stop states happens.  We'll wait
345d79fdd6dSTejun Heo 	 * for the transition to complete before returning from this
346d79fdd6dSTejun Heo 	 * function.
347d79fdd6dSTejun Heo 	 *
348d79fdd6dSTejun Heo 	 * This hides STOPPED -> RUNNING -> TRACED transition from the
349d79fdd6dSTejun Heo 	 * attaching thread but a different thread in the same group can
350d79fdd6dSTejun Heo 	 * still observe the transient RUNNING state.  IOW, if another
351d79fdd6dSTejun Heo 	 * thread's WNOHANG wait(2) on the stopped tracee races against
352d79fdd6dSTejun Heo 	 * ATTACH, the wait(2) may fail due to the transient RUNNING.
353d79fdd6dSTejun Heo 	 *
354d79fdd6dSTejun Heo 	 * The following task_is_stopped() test is safe as both transitions
355d79fdd6dSTejun Heo 	 * in and out of STOPPED are protected by siglock.
356d79fdd6dSTejun Heo 	 */
3577dd3db54STejun Heo 	if (task_is_stopped(task) &&
35873ddff2bSTejun Heo 	    task_set_jobctl_pending(task, JOBCTL_TRAP_STOP | JOBCTL_TRAPPING))
359910ffdb1SOleg Nesterov 		signal_wake_up_state(task, __TASK_STOPPED);
360d79fdd6dSTejun Heo 
361d79fdd6dSTejun Heo 	spin_unlock(&task->sighand->siglock);
362d79fdd6dSTejun Heo 
363b79b7ba9SOleg Nesterov 	retval = 0;
3644b105cbbSOleg Nesterov unlock_tasklist:
3654b105cbbSOleg Nesterov 	write_unlock_irq(&tasklist_lock);
3664b105cbbSOleg Nesterov unlock_creds:
3679b1bf12dSKOSAKI Motohiro 	mutex_unlock(&task->signal->cred_guard_mutex);
368f5b40e36SLinus Torvalds out:
369f701e5b7SVladimir Zapolskiy 	if (!retval) {
37062c124ffSTejun Heo 		wait_on_bit(&task->jobctl, JOBCTL_TRAPPING_BIT,
37162c124ffSTejun Heo 			    ptrace_trapping_sleep_fn, TASK_UNINTERRUPTIBLE);
372f701e5b7SVladimir Zapolskiy 		proc_ptrace_connector(task, PTRACE_ATTACH);
373f701e5b7SVladimir Zapolskiy 	}
374f701e5b7SVladimir Zapolskiy 
3751da177e4SLinus Torvalds 	return retval;
3761da177e4SLinus Torvalds }
3771da177e4SLinus Torvalds 
378f2f0b00aSOleg Nesterov /**
379f2f0b00aSOleg Nesterov  * ptrace_traceme  --  helper for PTRACE_TRACEME
380f2f0b00aSOleg Nesterov  *
381f2f0b00aSOleg Nesterov  * Performs checks and sets PT_PTRACED.
382f2f0b00aSOleg Nesterov  * Should be used by all ptrace implementations for PTRACE_TRACEME.
383f2f0b00aSOleg Nesterov  */
384e3e89cc5SLinus Torvalds static int ptrace_traceme(void)
385f2f0b00aSOleg Nesterov {
386f2f0b00aSOleg Nesterov 	int ret = -EPERM;
387f2f0b00aSOleg Nesterov 
3884b105cbbSOleg Nesterov 	write_lock_irq(&tasklist_lock);
3894b105cbbSOleg Nesterov 	/* Are we already being traced? */
390f2f0b00aSOleg Nesterov 	if (!current->ptrace) {
391f2f0b00aSOleg Nesterov 		ret = security_ptrace_traceme(current->parent);
392f2f0b00aSOleg Nesterov 		/*
393f2f0b00aSOleg Nesterov 		 * Check PF_EXITING to ensure ->real_parent has not passed
394f2f0b00aSOleg Nesterov 		 * exit_ptrace(). Otherwise we don't report the error but
395f2f0b00aSOleg Nesterov 		 * pretend ->real_parent untraces us right after return.
396f2f0b00aSOleg Nesterov 		 */
397f2f0b00aSOleg Nesterov 		if (!ret && !(current->real_parent->flags & PF_EXITING)) {
398f2f0b00aSOleg Nesterov 			current->ptrace = PT_PTRACED;
399f2f0b00aSOleg Nesterov 			__ptrace_link(current, current->real_parent);
400f2f0b00aSOleg Nesterov 		}
401f2f0b00aSOleg Nesterov 	}
4024b105cbbSOleg Nesterov 	write_unlock_irq(&tasklist_lock);
4034b105cbbSOleg Nesterov 
404f2f0b00aSOleg Nesterov 	return ret;
405f2f0b00aSOleg Nesterov }
406f2f0b00aSOleg Nesterov 
40739c626aeSOleg Nesterov /*
40839c626aeSOleg Nesterov  * Called with irqs disabled, returns true if childs should reap themselves.
40939c626aeSOleg Nesterov  */
41039c626aeSOleg Nesterov static int ignoring_children(struct sighand_struct *sigh)
41139c626aeSOleg Nesterov {
41239c626aeSOleg Nesterov 	int ret;
41339c626aeSOleg Nesterov 	spin_lock(&sigh->siglock);
41439c626aeSOleg Nesterov 	ret = (sigh->action[SIGCHLD-1].sa.sa_handler == SIG_IGN) ||
41539c626aeSOleg Nesterov 	      (sigh->action[SIGCHLD-1].sa.sa_flags & SA_NOCLDWAIT);
41639c626aeSOleg Nesterov 	spin_unlock(&sigh->siglock);
41739c626aeSOleg Nesterov 	return ret;
41839c626aeSOleg Nesterov }
41939c626aeSOleg Nesterov 
42039c626aeSOleg Nesterov /*
42139c626aeSOleg Nesterov  * Called with tasklist_lock held for writing.
42239c626aeSOleg Nesterov  * Unlink a traced task, and clean it up if it was a traced zombie.
42339c626aeSOleg Nesterov  * Return true if it needs to be reaped with release_task().
42439c626aeSOleg Nesterov  * (We can't call release_task() here because we already hold tasklist_lock.)
42539c626aeSOleg Nesterov  *
42639c626aeSOleg Nesterov  * If it's a zombie, our attachedness prevented normal parent notification
42739c626aeSOleg Nesterov  * or self-reaping.  Do notification now if it would have happened earlier.
42839c626aeSOleg Nesterov  * If it should reap itself, return true.
42939c626aeSOleg Nesterov  *
430a7f0765eSOleg Nesterov  * If it's our own child, there is no notification to do. But if our normal
431a7f0765eSOleg Nesterov  * children self-reap, then this child was prevented by ptrace and we must
432a7f0765eSOleg Nesterov  * reap it now, in that case we must also wake up sub-threads sleeping in
433a7f0765eSOleg Nesterov  * do_wait().
43439c626aeSOleg Nesterov  */
43539c626aeSOleg Nesterov static bool __ptrace_detach(struct task_struct *tracer, struct task_struct *p)
43639c626aeSOleg Nesterov {
4379843a1e9SOleg Nesterov 	bool dead;
4389843a1e9SOleg Nesterov 
43939c626aeSOleg Nesterov 	__ptrace_unlink(p);
44039c626aeSOleg Nesterov 
4419843a1e9SOleg Nesterov 	if (p->exit_state != EXIT_ZOMBIE)
4429843a1e9SOleg Nesterov 		return false;
4439843a1e9SOleg Nesterov 
4449843a1e9SOleg Nesterov 	dead = !thread_group_leader(p);
4459843a1e9SOleg Nesterov 
4469843a1e9SOleg Nesterov 	if (!dead && thread_group_empty(p)) {
44739c626aeSOleg Nesterov 		if (!same_thread_group(p->real_parent, tracer))
4489843a1e9SOleg Nesterov 			dead = do_notify_parent(p, p->exit_signal);
449a7f0765eSOleg Nesterov 		else if (ignoring_children(tracer->sighand)) {
450a7f0765eSOleg Nesterov 			__wake_up_parent(p, tracer);
4519843a1e9SOleg Nesterov 			dead = true;
45239c626aeSOleg Nesterov 		}
453a7f0765eSOleg Nesterov 	}
45439c626aeSOleg Nesterov 	/* Mark it as in the process of being reaped. */
4559843a1e9SOleg Nesterov 	if (dead)
45639c626aeSOleg Nesterov 		p->exit_state = EXIT_DEAD;
4579843a1e9SOleg Nesterov 	return dead;
45839c626aeSOleg Nesterov }
45939c626aeSOleg Nesterov 
460e3e89cc5SLinus Torvalds static int ptrace_detach(struct task_struct *child, unsigned int data)
4611da177e4SLinus Torvalds {
46239c626aeSOleg Nesterov 	bool dead = false;
4634576145cSOleg Nesterov 
4647ed20e1aSJesper Juhl 	if (!valid_signal(data))
4651da177e4SLinus Torvalds 		return -EIO;
4661da177e4SLinus Torvalds 
4671da177e4SLinus Torvalds 	/* Architecture-specific hardware disable .. */
4681da177e4SLinus Torvalds 	ptrace_disable(child);
4697d941432SRoland McGrath 	clear_tsk_thread_flag(child, TIF_SYSCALL_TRACE);
4701da177e4SLinus Torvalds 
47195c3eb76SOleg Nesterov 	write_lock_irq(&tasklist_lock);
47239c626aeSOleg Nesterov 	/*
47339c626aeSOleg Nesterov 	 * This child can be already killed. Make sure de_thread() or
47439c626aeSOleg Nesterov 	 * our sub-thread doing do_wait() didn't do release_task() yet.
47539c626aeSOleg Nesterov 	 */
47695c3eb76SOleg Nesterov 	if (child->ptrace) {
47795c3eb76SOleg Nesterov 		child->exit_code = data;
4784576145cSOleg Nesterov 		dead = __ptrace_detach(current, child);
47995c3eb76SOleg Nesterov 	}
4801da177e4SLinus Torvalds 	write_unlock_irq(&tasklist_lock);
4811da177e4SLinus Torvalds 
482f701e5b7SVladimir Zapolskiy 	proc_ptrace_connector(child, PTRACE_DETACH);
4834576145cSOleg Nesterov 	if (unlikely(dead))
4844576145cSOleg Nesterov 		release_task(child);
4854576145cSOleg Nesterov 
4861da177e4SLinus Torvalds 	return 0;
4871da177e4SLinus Torvalds }
4881da177e4SLinus Torvalds 
48939c626aeSOleg Nesterov /*
490c7e49c14SOleg Nesterov  * Detach all tasks we were using ptrace on. Called with tasklist held
491c7e49c14SOleg Nesterov  * for writing, and returns with it held too. But note it can release
492c7e49c14SOleg Nesterov  * and reacquire the lock.
49339c626aeSOleg Nesterov  */
49439c626aeSOleg Nesterov void exit_ptrace(struct task_struct *tracer)
495c4b5ed25SNamhyung Kim 	__releases(&tasklist_lock)
496c4b5ed25SNamhyung Kim 	__acquires(&tasklist_lock)
49739c626aeSOleg Nesterov {
49839c626aeSOleg Nesterov 	struct task_struct *p, *n;
49939c626aeSOleg Nesterov 	LIST_HEAD(ptrace_dead);
50039c626aeSOleg Nesterov 
501c7e49c14SOleg Nesterov 	if (likely(list_empty(&tracer->ptraced)))
502c7e49c14SOleg Nesterov 		return;
503c7e49c14SOleg Nesterov 
50439c626aeSOleg Nesterov 	list_for_each_entry_safe(p, n, &tracer->ptraced, ptrace_entry) {
505992fb6e1SOleg Nesterov 		if (unlikely(p->ptrace & PT_EXITKILL))
506992fb6e1SOleg Nesterov 			send_sig_info(SIGKILL, SEND_SIG_FORCED, p);
507992fb6e1SOleg Nesterov 
50839c626aeSOleg Nesterov 		if (__ptrace_detach(tracer, p))
50939c626aeSOleg Nesterov 			list_add(&p->ptrace_entry, &ptrace_dead);
51039c626aeSOleg Nesterov 	}
51139c626aeSOleg Nesterov 
512c7e49c14SOleg Nesterov 	write_unlock_irq(&tasklist_lock);
51339c626aeSOleg Nesterov 	BUG_ON(!list_empty(&tracer->ptraced));
51439c626aeSOleg Nesterov 
51539c626aeSOleg Nesterov 	list_for_each_entry_safe(p, n, &ptrace_dead, ptrace_entry) {
51639c626aeSOleg Nesterov 		list_del_init(&p->ptrace_entry);
51739c626aeSOleg Nesterov 		release_task(p);
51839c626aeSOleg Nesterov 	}
519c7e49c14SOleg Nesterov 
520c7e49c14SOleg Nesterov 	write_lock_irq(&tasklist_lock);
52139c626aeSOleg Nesterov }
52239c626aeSOleg Nesterov 
5231da177e4SLinus Torvalds int ptrace_readdata(struct task_struct *tsk, unsigned long src, char __user *dst, int len)
5241da177e4SLinus Torvalds {
5251da177e4SLinus Torvalds 	int copied = 0;
5261da177e4SLinus Torvalds 
5271da177e4SLinus Torvalds 	while (len > 0) {
5281da177e4SLinus Torvalds 		char buf[128];
5291da177e4SLinus Torvalds 		int this_len, retval;
5301da177e4SLinus Torvalds 
5311da177e4SLinus Torvalds 		this_len = (len > sizeof(buf)) ? sizeof(buf) : len;
5321da177e4SLinus Torvalds 		retval = access_process_vm(tsk, src, buf, this_len, 0);
5331da177e4SLinus Torvalds 		if (!retval) {
5341da177e4SLinus Torvalds 			if (copied)
5351da177e4SLinus Torvalds 				break;
5361da177e4SLinus Torvalds 			return -EIO;
5371da177e4SLinus Torvalds 		}
5381da177e4SLinus Torvalds 		if (copy_to_user(dst, buf, retval))
5391da177e4SLinus Torvalds 			return -EFAULT;
5401da177e4SLinus Torvalds 		copied += retval;
5411da177e4SLinus Torvalds 		src += retval;
5421da177e4SLinus Torvalds 		dst += retval;
5431da177e4SLinus Torvalds 		len -= retval;
5441da177e4SLinus Torvalds 	}
5451da177e4SLinus Torvalds 	return copied;
5461da177e4SLinus Torvalds }
5471da177e4SLinus Torvalds 
5481da177e4SLinus Torvalds int ptrace_writedata(struct task_struct *tsk, char __user *src, unsigned long dst, int len)
5491da177e4SLinus Torvalds {
5501da177e4SLinus Torvalds 	int copied = 0;
5511da177e4SLinus Torvalds 
5521da177e4SLinus Torvalds 	while (len > 0) {
5531da177e4SLinus Torvalds 		char buf[128];
5541da177e4SLinus Torvalds 		int this_len, retval;
5551da177e4SLinus Torvalds 
5561da177e4SLinus Torvalds 		this_len = (len > sizeof(buf)) ? sizeof(buf) : len;
5571da177e4SLinus Torvalds 		if (copy_from_user(buf, src, this_len))
5581da177e4SLinus Torvalds 			return -EFAULT;
5591da177e4SLinus Torvalds 		retval = access_process_vm(tsk, dst, buf, this_len, 1);
5601da177e4SLinus Torvalds 		if (!retval) {
5611da177e4SLinus Torvalds 			if (copied)
5621da177e4SLinus Torvalds 				break;
5631da177e4SLinus Torvalds 			return -EIO;
5641da177e4SLinus Torvalds 		}
5651da177e4SLinus Torvalds 		copied += retval;
5661da177e4SLinus Torvalds 		src += retval;
5671da177e4SLinus Torvalds 		dst += retval;
5681da177e4SLinus Torvalds 		len -= retval;
5691da177e4SLinus Torvalds 	}
5701da177e4SLinus Torvalds 	return copied;
5711da177e4SLinus Torvalds }
5721da177e4SLinus Torvalds 
5734abf9869SNamhyung Kim static int ptrace_setoptions(struct task_struct *child, unsigned long data)
5741da177e4SLinus Torvalds {
57586b6c1f3SDenys Vlasenko 	unsigned flags;
57686b6c1f3SDenys Vlasenko 
5778c5cf9e5SDenys Vlasenko 	if (data & ~(unsigned long)PTRACE_O_MASK)
5788c5cf9e5SDenys Vlasenko 		return -EINVAL;
5798c5cf9e5SDenys Vlasenko 
58086b6c1f3SDenys Vlasenko 	/* Avoid intermediate state when all opts are cleared */
58186b6c1f3SDenys Vlasenko 	flags = child->ptrace;
58286b6c1f3SDenys Vlasenko 	flags &= ~(PTRACE_O_MASK << PT_OPT_FLAG_SHIFT);
58386b6c1f3SDenys Vlasenko 	flags |= (data << PT_OPT_FLAG_SHIFT);
58486b6c1f3SDenys Vlasenko 	child->ptrace = flags;
5851da177e4SLinus Torvalds 
5868c5cf9e5SDenys Vlasenko 	return 0;
5871da177e4SLinus Torvalds }
5881da177e4SLinus Torvalds 
589e16b2781SRoland McGrath static int ptrace_getsiginfo(struct task_struct *child, siginfo_t *info)
5901da177e4SLinus Torvalds {
591e4961254SOleg Nesterov 	unsigned long flags;
5921da177e4SLinus Torvalds 	int error = -ESRCH;
5931da177e4SLinus Torvalds 
594e4961254SOleg Nesterov 	if (lock_task_sighand(child, &flags)) {
5951da177e4SLinus Torvalds 		error = -EINVAL;
5961da177e4SLinus Torvalds 		if (likely(child->last_siginfo != NULL)) {
597e16b2781SRoland McGrath 			*info = *child->last_siginfo;
5981da177e4SLinus Torvalds 			error = 0;
5991da177e4SLinus Torvalds 		}
600e4961254SOleg Nesterov 		unlock_task_sighand(child, &flags);
6011da177e4SLinus Torvalds 	}
6021da177e4SLinus Torvalds 	return error;
6031da177e4SLinus Torvalds }
6041da177e4SLinus Torvalds 
605e16b2781SRoland McGrath static int ptrace_setsiginfo(struct task_struct *child, const siginfo_t *info)
6061da177e4SLinus Torvalds {
607e4961254SOleg Nesterov 	unsigned long flags;
6081da177e4SLinus Torvalds 	int error = -ESRCH;
6091da177e4SLinus Torvalds 
610e4961254SOleg Nesterov 	if (lock_task_sighand(child, &flags)) {
6111da177e4SLinus Torvalds 		error = -EINVAL;
6121da177e4SLinus Torvalds 		if (likely(child->last_siginfo != NULL)) {
613e16b2781SRoland McGrath 			*child->last_siginfo = *info;
6141da177e4SLinus Torvalds 			error = 0;
6151da177e4SLinus Torvalds 		}
616e4961254SOleg Nesterov 		unlock_task_sighand(child, &flags);
6171da177e4SLinus Torvalds 	}
6181da177e4SLinus Torvalds 	return error;
6191da177e4SLinus Torvalds }
6201da177e4SLinus Torvalds 
62136df29d7SRoland McGrath 
62236df29d7SRoland McGrath #ifdef PTRACE_SINGLESTEP
62336df29d7SRoland McGrath #define is_singlestep(request)		((request) == PTRACE_SINGLESTEP)
62436df29d7SRoland McGrath #else
62536df29d7SRoland McGrath #define is_singlestep(request)		0
62636df29d7SRoland McGrath #endif
62736df29d7SRoland McGrath 
6285b88abbfSRoland McGrath #ifdef PTRACE_SINGLEBLOCK
6295b88abbfSRoland McGrath #define is_singleblock(request)		((request) == PTRACE_SINGLEBLOCK)
6305b88abbfSRoland McGrath #else
6315b88abbfSRoland McGrath #define is_singleblock(request)		0
6325b88abbfSRoland McGrath #endif
6335b88abbfSRoland McGrath 
63436df29d7SRoland McGrath #ifdef PTRACE_SYSEMU
63536df29d7SRoland McGrath #define is_sysemu_singlestep(request)	((request) == PTRACE_SYSEMU_SINGLESTEP)
63636df29d7SRoland McGrath #else
63736df29d7SRoland McGrath #define is_sysemu_singlestep(request)	0
63836df29d7SRoland McGrath #endif
63936df29d7SRoland McGrath 
6404abf9869SNamhyung Kim static int ptrace_resume(struct task_struct *child, long request,
6414abf9869SNamhyung Kim 			 unsigned long data)
64236df29d7SRoland McGrath {
64336df29d7SRoland McGrath 	if (!valid_signal(data))
64436df29d7SRoland McGrath 		return -EIO;
64536df29d7SRoland McGrath 
64636df29d7SRoland McGrath 	if (request == PTRACE_SYSCALL)
64736df29d7SRoland McGrath 		set_tsk_thread_flag(child, TIF_SYSCALL_TRACE);
64836df29d7SRoland McGrath 	else
64936df29d7SRoland McGrath 		clear_tsk_thread_flag(child, TIF_SYSCALL_TRACE);
65036df29d7SRoland McGrath 
65136df29d7SRoland McGrath #ifdef TIF_SYSCALL_EMU
65236df29d7SRoland McGrath 	if (request == PTRACE_SYSEMU || request == PTRACE_SYSEMU_SINGLESTEP)
65336df29d7SRoland McGrath 		set_tsk_thread_flag(child, TIF_SYSCALL_EMU);
65436df29d7SRoland McGrath 	else
65536df29d7SRoland McGrath 		clear_tsk_thread_flag(child, TIF_SYSCALL_EMU);
65636df29d7SRoland McGrath #endif
65736df29d7SRoland McGrath 
6585b88abbfSRoland McGrath 	if (is_singleblock(request)) {
6595b88abbfSRoland McGrath 		if (unlikely(!arch_has_block_step()))
6605b88abbfSRoland McGrath 			return -EIO;
6615b88abbfSRoland McGrath 		user_enable_block_step(child);
6625b88abbfSRoland McGrath 	} else if (is_singlestep(request) || is_sysemu_singlestep(request)) {
66336df29d7SRoland McGrath 		if (unlikely(!arch_has_single_step()))
66436df29d7SRoland McGrath 			return -EIO;
66536df29d7SRoland McGrath 		user_enable_single_step(child);
6663a709703SRoland McGrath 	} else {
66736df29d7SRoland McGrath 		user_disable_single_step(child);
6683a709703SRoland McGrath 	}
66936df29d7SRoland McGrath 
67036df29d7SRoland McGrath 	child->exit_code = data;
6710666fb51SOleg Nesterov 	wake_up_state(child, __TASK_TRACED);
67236df29d7SRoland McGrath 
67336df29d7SRoland McGrath 	return 0;
67436df29d7SRoland McGrath }
67536df29d7SRoland McGrath 
6762225a122SSuresh Siddha #ifdef CONFIG_HAVE_ARCH_TRACEHOOK
6772225a122SSuresh Siddha 
6782225a122SSuresh Siddha static const struct user_regset *
6792225a122SSuresh Siddha find_regset(const struct user_regset_view *view, unsigned int type)
6802225a122SSuresh Siddha {
6812225a122SSuresh Siddha 	const struct user_regset *regset;
6822225a122SSuresh Siddha 	int n;
6832225a122SSuresh Siddha 
6842225a122SSuresh Siddha 	for (n = 0; n < view->n; ++n) {
6852225a122SSuresh Siddha 		regset = view->regsets + n;
6862225a122SSuresh Siddha 		if (regset->core_note_type == type)
6872225a122SSuresh Siddha 			return regset;
6882225a122SSuresh Siddha 	}
6892225a122SSuresh Siddha 
6902225a122SSuresh Siddha 	return NULL;
6912225a122SSuresh Siddha }
6922225a122SSuresh Siddha 
6932225a122SSuresh Siddha static int ptrace_regset(struct task_struct *task, int req, unsigned int type,
6942225a122SSuresh Siddha 			 struct iovec *kiov)
6952225a122SSuresh Siddha {
6962225a122SSuresh Siddha 	const struct user_regset_view *view = task_user_regset_view(task);
6972225a122SSuresh Siddha 	const struct user_regset *regset = find_regset(view, type);
6982225a122SSuresh Siddha 	int regset_no;
6992225a122SSuresh Siddha 
7002225a122SSuresh Siddha 	if (!regset || (kiov->iov_len % regset->size) != 0)
701c6a0dd7eSSuresh Siddha 		return -EINVAL;
7022225a122SSuresh Siddha 
7032225a122SSuresh Siddha 	regset_no = regset - view->regsets;
7042225a122SSuresh Siddha 	kiov->iov_len = min(kiov->iov_len,
7052225a122SSuresh Siddha 			    (__kernel_size_t) (regset->n * regset->size));
7062225a122SSuresh Siddha 
7072225a122SSuresh Siddha 	if (req == PTRACE_GETREGSET)
7082225a122SSuresh Siddha 		return copy_regset_to_user(task, view, regset_no, 0,
7092225a122SSuresh Siddha 					   kiov->iov_len, kiov->iov_base);
7102225a122SSuresh Siddha 	else
7112225a122SSuresh Siddha 		return copy_regset_from_user(task, view, regset_no, 0,
7122225a122SSuresh Siddha 					     kiov->iov_len, kiov->iov_base);
7132225a122SSuresh Siddha }
7142225a122SSuresh Siddha 
715e8440c14SJosh Stone /*
716e8440c14SJosh Stone  * This is declared in linux/regset.h and defined in machine-dependent
717e8440c14SJosh Stone  * code.  We put the export here, near the primary machine-neutral use,
718e8440c14SJosh Stone  * to ensure no machine forgets it.
719e8440c14SJosh Stone  */
720e8440c14SJosh Stone EXPORT_SYMBOL_GPL(task_user_regset_view);
7212225a122SSuresh Siddha #endif
7222225a122SSuresh Siddha 
7231da177e4SLinus Torvalds int ptrace_request(struct task_struct *child, long request,
7244abf9869SNamhyung Kim 		   unsigned long addr, unsigned long data)
7251da177e4SLinus Torvalds {
726fca26f26STejun Heo 	bool seized = child->ptrace & PT_SEIZED;
7271da177e4SLinus Torvalds 	int ret = -EIO;
728544b2c91STejun Heo 	siginfo_t siginfo, *si;
7299fed81dcSNamhyung Kim 	void __user *datavp = (void __user *) data;
7309fed81dcSNamhyung Kim 	unsigned long __user *datalp = datavp;
731fca26f26STejun Heo 	unsigned long flags;
7321da177e4SLinus Torvalds 
7331da177e4SLinus Torvalds 	switch (request) {
73416c3e389SRoland McGrath 	case PTRACE_PEEKTEXT:
73516c3e389SRoland McGrath 	case PTRACE_PEEKDATA:
73616c3e389SRoland McGrath 		return generic_ptrace_peekdata(child, addr, data);
73716c3e389SRoland McGrath 	case PTRACE_POKETEXT:
73816c3e389SRoland McGrath 	case PTRACE_POKEDATA:
73916c3e389SRoland McGrath 		return generic_ptrace_pokedata(child, addr, data);
74016c3e389SRoland McGrath 
7411da177e4SLinus Torvalds #ifdef PTRACE_OLDSETOPTIONS
7421da177e4SLinus Torvalds 	case PTRACE_OLDSETOPTIONS:
7431da177e4SLinus Torvalds #endif
7441da177e4SLinus Torvalds 	case PTRACE_SETOPTIONS:
7451da177e4SLinus Torvalds 		ret = ptrace_setoptions(child, data);
7461da177e4SLinus Torvalds 		break;
7471da177e4SLinus Torvalds 	case PTRACE_GETEVENTMSG:
7489fed81dcSNamhyung Kim 		ret = put_user(child->ptrace_message, datalp);
7491da177e4SLinus Torvalds 		break;
750e16b2781SRoland McGrath 
7511da177e4SLinus Torvalds 	case PTRACE_GETSIGINFO:
752e16b2781SRoland McGrath 		ret = ptrace_getsiginfo(child, &siginfo);
753e16b2781SRoland McGrath 		if (!ret)
7549fed81dcSNamhyung Kim 			ret = copy_siginfo_to_user(datavp, &siginfo);
7551da177e4SLinus Torvalds 		break;
756e16b2781SRoland McGrath 
7571da177e4SLinus Torvalds 	case PTRACE_SETSIGINFO:
7589fed81dcSNamhyung Kim 		if (copy_from_user(&siginfo, datavp, sizeof siginfo))
759e16b2781SRoland McGrath 			ret = -EFAULT;
760e16b2781SRoland McGrath 		else
761e16b2781SRoland McGrath 			ret = ptrace_setsiginfo(child, &siginfo);
7621da177e4SLinus Torvalds 		break;
763e16b2781SRoland McGrath 
764fca26f26STejun Heo 	case PTRACE_INTERRUPT:
765fca26f26STejun Heo 		/*
766fca26f26STejun Heo 		 * Stop tracee without any side-effect on signal or job
767fca26f26STejun Heo 		 * control.  At least one trap is guaranteed to happen
768fca26f26STejun Heo 		 * after this request.  If @child is already trapped, the
769fca26f26STejun Heo 		 * current trap is not disturbed and another trap will
770fca26f26STejun Heo 		 * happen after the current trap is ended with PTRACE_CONT.
771fca26f26STejun Heo 		 *
772fca26f26STejun Heo 		 * The actual trap might not be PTRACE_EVENT_STOP trap but
773fca26f26STejun Heo 		 * the pending condition is cleared regardless.
774fca26f26STejun Heo 		 */
775fca26f26STejun Heo 		if (unlikely(!seized || !lock_task_sighand(child, &flags)))
776fca26f26STejun Heo 			break;
777fca26f26STejun Heo 
778544b2c91STejun Heo 		/*
779544b2c91STejun Heo 		 * INTERRUPT doesn't disturb existing trap sans one
780544b2c91STejun Heo 		 * exception.  If ptracer issued LISTEN for the current
781544b2c91STejun Heo 		 * STOP, this INTERRUPT should clear LISTEN and re-trap
782544b2c91STejun Heo 		 * tracee into STOP.
783544b2c91STejun Heo 		 */
784fca26f26STejun Heo 		if (likely(task_set_jobctl_pending(child, JOBCTL_TRAP_STOP)))
785910ffdb1SOleg Nesterov 			ptrace_signal_wake_up(child, child->jobctl & JOBCTL_LISTENING);
786544b2c91STejun Heo 
787544b2c91STejun Heo 		unlock_task_sighand(child, &flags);
788544b2c91STejun Heo 		ret = 0;
789544b2c91STejun Heo 		break;
790544b2c91STejun Heo 
791544b2c91STejun Heo 	case PTRACE_LISTEN:
792544b2c91STejun Heo 		/*
793544b2c91STejun Heo 		 * Listen for events.  Tracee must be in STOP.  It's not
794544b2c91STejun Heo 		 * resumed per-se but is not considered to be in TRACED by
795544b2c91STejun Heo 		 * wait(2) or ptrace(2).  If an async event (e.g. group
796544b2c91STejun Heo 		 * stop state change) happens, tracee will enter STOP trap
797544b2c91STejun Heo 		 * again.  Alternatively, ptracer can issue INTERRUPT to
798544b2c91STejun Heo 		 * finish listening and re-trap tracee into STOP.
799544b2c91STejun Heo 		 */
800544b2c91STejun Heo 		if (unlikely(!seized || !lock_task_sighand(child, &flags)))
801544b2c91STejun Heo 			break;
802544b2c91STejun Heo 
803544b2c91STejun Heo 		si = child->last_siginfo;
804f9d81f61SOleg Nesterov 		if (likely(si && (si->si_code >> 8) == PTRACE_EVENT_STOP)) {
805544b2c91STejun Heo 			child->jobctl |= JOBCTL_LISTENING;
806544b2c91STejun Heo 			/*
807f9d81f61SOleg Nesterov 			 * If NOTIFY is set, it means event happened between
808f9d81f61SOleg Nesterov 			 * start of this trap and now.  Trigger re-trap.
809544b2c91STejun Heo 			 */
810544b2c91STejun Heo 			if (child->jobctl & JOBCTL_TRAP_NOTIFY)
811910ffdb1SOleg Nesterov 				ptrace_signal_wake_up(child, true);
812fca26f26STejun Heo 			ret = 0;
813f9d81f61SOleg Nesterov 		}
814f9d81f61SOleg Nesterov 		unlock_task_sighand(child, &flags);
815fca26f26STejun Heo 		break;
816fca26f26STejun Heo 
8171bcf5482SAlexey Dobriyan 	case PTRACE_DETACH:	 /* detach a process that was attached. */
8181bcf5482SAlexey Dobriyan 		ret = ptrace_detach(child, data);
8191bcf5482SAlexey Dobriyan 		break;
82036df29d7SRoland McGrath 
8219c1a1259SMike Frysinger #ifdef CONFIG_BINFMT_ELF_FDPIC
8229c1a1259SMike Frysinger 	case PTRACE_GETFDPIC: {
823e0129ef9SOleg Nesterov 		struct mm_struct *mm = get_task_mm(child);
8249c1a1259SMike Frysinger 		unsigned long tmp = 0;
8259c1a1259SMike Frysinger 
826e0129ef9SOleg Nesterov 		ret = -ESRCH;
827e0129ef9SOleg Nesterov 		if (!mm)
828e0129ef9SOleg Nesterov 			break;
829e0129ef9SOleg Nesterov 
8309c1a1259SMike Frysinger 		switch (addr) {
8319c1a1259SMike Frysinger 		case PTRACE_GETFDPIC_EXEC:
832e0129ef9SOleg Nesterov 			tmp = mm->context.exec_fdpic_loadmap;
8339c1a1259SMike Frysinger 			break;
8349c1a1259SMike Frysinger 		case PTRACE_GETFDPIC_INTERP:
835e0129ef9SOleg Nesterov 			tmp = mm->context.interp_fdpic_loadmap;
8369c1a1259SMike Frysinger 			break;
8379c1a1259SMike Frysinger 		default:
8389c1a1259SMike Frysinger 			break;
8399c1a1259SMike Frysinger 		}
840e0129ef9SOleg Nesterov 		mmput(mm);
8419c1a1259SMike Frysinger 
8429fed81dcSNamhyung Kim 		ret = put_user(tmp, datalp);
8439c1a1259SMike Frysinger 		break;
8449c1a1259SMike Frysinger 	}
8459c1a1259SMike Frysinger #endif
8469c1a1259SMike Frysinger 
84736df29d7SRoland McGrath #ifdef PTRACE_SINGLESTEP
84836df29d7SRoland McGrath 	case PTRACE_SINGLESTEP:
84936df29d7SRoland McGrath #endif
8505b88abbfSRoland McGrath #ifdef PTRACE_SINGLEBLOCK
8515b88abbfSRoland McGrath 	case PTRACE_SINGLEBLOCK:
8525b88abbfSRoland McGrath #endif
85336df29d7SRoland McGrath #ifdef PTRACE_SYSEMU
85436df29d7SRoland McGrath 	case PTRACE_SYSEMU:
85536df29d7SRoland McGrath 	case PTRACE_SYSEMU_SINGLESTEP:
85636df29d7SRoland McGrath #endif
85736df29d7SRoland McGrath 	case PTRACE_SYSCALL:
85836df29d7SRoland McGrath 	case PTRACE_CONT:
85936df29d7SRoland McGrath 		return ptrace_resume(child, request, data);
86036df29d7SRoland McGrath 
86136df29d7SRoland McGrath 	case PTRACE_KILL:
86236df29d7SRoland McGrath 		if (child->exit_state)	/* already dead */
86336df29d7SRoland McGrath 			return 0;
86436df29d7SRoland McGrath 		return ptrace_resume(child, request, SIGKILL);
86536df29d7SRoland McGrath 
8662225a122SSuresh Siddha #ifdef CONFIG_HAVE_ARCH_TRACEHOOK
8672225a122SSuresh Siddha 	case PTRACE_GETREGSET:
8682225a122SSuresh Siddha 	case PTRACE_SETREGSET:
8692225a122SSuresh Siddha 	{
8702225a122SSuresh Siddha 		struct iovec kiov;
8719fed81dcSNamhyung Kim 		struct iovec __user *uiov = datavp;
8722225a122SSuresh Siddha 
8732225a122SSuresh Siddha 		if (!access_ok(VERIFY_WRITE, uiov, sizeof(*uiov)))
8742225a122SSuresh Siddha 			return -EFAULT;
8752225a122SSuresh Siddha 
8762225a122SSuresh Siddha 		if (__get_user(kiov.iov_base, &uiov->iov_base) ||
8772225a122SSuresh Siddha 		    __get_user(kiov.iov_len, &uiov->iov_len))
8782225a122SSuresh Siddha 			return -EFAULT;
8792225a122SSuresh Siddha 
8802225a122SSuresh Siddha 		ret = ptrace_regset(child, request, addr, &kiov);
8812225a122SSuresh Siddha 		if (!ret)
8822225a122SSuresh Siddha 			ret = __put_user(kiov.iov_len, &uiov->iov_len);
8832225a122SSuresh Siddha 		break;
8842225a122SSuresh Siddha 	}
8852225a122SSuresh Siddha #endif
8861da177e4SLinus Torvalds 	default:
8871da177e4SLinus Torvalds 		break;
8881da177e4SLinus Torvalds 	}
8891da177e4SLinus Torvalds 
8901da177e4SLinus Torvalds 	return ret;
8911da177e4SLinus Torvalds }
892481bed45SChristoph Hellwig 
8938053bdd5SOleg Nesterov static struct task_struct *ptrace_get_task_struct(pid_t pid)
8946b9c7ed8SChristoph Hellwig {
8956b9c7ed8SChristoph Hellwig 	struct task_struct *child;
8966b9c7ed8SChristoph Hellwig 
8978053bdd5SOleg Nesterov 	rcu_read_lock();
898228ebcbeSPavel Emelyanov 	child = find_task_by_vpid(pid);
899481bed45SChristoph Hellwig 	if (child)
900481bed45SChristoph Hellwig 		get_task_struct(child);
9018053bdd5SOleg Nesterov 	rcu_read_unlock();
902f400e198SSukadev Bhattiprolu 
903481bed45SChristoph Hellwig 	if (!child)
9046b9c7ed8SChristoph Hellwig 		return ERR_PTR(-ESRCH);
9056b9c7ed8SChristoph Hellwig 	return child;
906481bed45SChristoph Hellwig }
907481bed45SChristoph Hellwig 
9080ac15559SChristoph Hellwig #ifndef arch_ptrace_attach
9090ac15559SChristoph Hellwig #define arch_ptrace_attach(child)	do { } while (0)
9100ac15559SChristoph Hellwig #endif
9110ac15559SChristoph Hellwig 
9124abf9869SNamhyung Kim SYSCALL_DEFINE4(ptrace, long, request, long, pid, unsigned long, addr,
9134abf9869SNamhyung Kim 		unsigned long, data)
914481bed45SChristoph Hellwig {
915481bed45SChristoph Hellwig 	struct task_struct *child;
916481bed45SChristoph Hellwig 	long ret;
917481bed45SChristoph Hellwig 
9186b9c7ed8SChristoph Hellwig 	if (request == PTRACE_TRACEME) {
9196b9c7ed8SChristoph Hellwig 		ret = ptrace_traceme();
9206ea6dd93SHaavard Skinnemoen 		if (!ret)
9216ea6dd93SHaavard Skinnemoen 			arch_ptrace_attach(current);
922481bed45SChristoph Hellwig 		goto out;
9236b9c7ed8SChristoph Hellwig 	}
9246b9c7ed8SChristoph Hellwig 
9256b9c7ed8SChristoph Hellwig 	child = ptrace_get_task_struct(pid);
9266b9c7ed8SChristoph Hellwig 	if (IS_ERR(child)) {
9276b9c7ed8SChristoph Hellwig 		ret = PTR_ERR(child);
9286b9c7ed8SChristoph Hellwig 		goto out;
9296b9c7ed8SChristoph Hellwig 	}
930481bed45SChristoph Hellwig 
9313544d72aSTejun Heo 	if (request == PTRACE_ATTACH || request == PTRACE_SEIZE) {
932aa9147c9SDenys Vlasenko 		ret = ptrace_attach(child, request, addr, data);
9330ac15559SChristoph Hellwig 		/*
9340ac15559SChristoph Hellwig 		 * Some architectures need to do book-keeping after
9350ac15559SChristoph Hellwig 		 * a ptrace attach.
9360ac15559SChristoph Hellwig 		 */
9370ac15559SChristoph Hellwig 		if (!ret)
9380ac15559SChristoph Hellwig 			arch_ptrace_attach(child);
939005f18dfSChristoph Hellwig 		goto out_put_task_struct;
940481bed45SChristoph Hellwig 	}
941481bed45SChristoph Hellwig 
942fca26f26STejun Heo 	ret = ptrace_check_attach(child, request == PTRACE_KILL ||
943fca26f26STejun Heo 				  request == PTRACE_INTERRUPT);
944481bed45SChristoph Hellwig 	if (ret < 0)
945481bed45SChristoph Hellwig 		goto out_put_task_struct;
946481bed45SChristoph Hellwig 
947481bed45SChristoph Hellwig 	ret = arch_ptrace(child, request, addr, data);
9489899d11fSOleg Nesterov 	if (ret || request != PTRACE_DETACH)
9499899d11fSOleg Nesterov 		ptrace_unfreeze_traced(child);
950481bed45SChristoph Hellwig 
951481bed45SChristoph Hellwig  out_put_task_struct:
952481bed45SChristoph Hellwig 	put_task_struct(child);
953481bed45SChristoph Hellwig  out:
954481bed45SChristoph Hellwig 	return ret;
955481bed45SChristoph Hellwig }
95676647323SAlexey Dobriyan 
9574abf9869SNamhyung Kim int generic_ptrace_peekdata(struct task_struct *tsk, unsigned long addr,
9584abf9869SNamhyung Kim 			    unsigned long data)
95976647323SAlexey Dobriyan {
96076647323SAlexey Dobriyan 	unsigned long tmp;
96176647323SAlexey Dobriyan 	int copied;
96276647323SAlexey Dobriyan 
96376647323SAlexey Dobriyan 	copied = access_process_vm(tsk, addr, &tmp, sizeof(tmp), 0);
96476647323SAlexey Dobriyan 	if (copied != sizeof(tmp))
96576647323SAlexey Dobriyan 		return -EIO;
96676647323SAlexey Dobriyan 	return put_user(tmp, (unsigned long __user *)data);
96776647323SAlexey Dobriyan }
968f284ce72SAlexey Dobriyan 
9694abf9869SNamhyung Kim int generic_ptrace_pokedata(struct task_struct *tsk, unsigned long addr,
9704abf9869SNamhyung Kim 			    unsigned long data)
971f284ce72SAlexey Dobriyan {
972f284ce72SAlexey Dobriyan 	int copied;
973f284ce72SAlexey Dobriyan 
974f284ce72SAlexey Dobriyan 	copied = access_process_vm(tsk, addr, &data, sizeof(data), 1);
975f284ce72SAlexey Dobriyan 	return (copied == sizeof(data)) ? 0 : -EIO;
976f284ce72SAlexey Dobriyan }
977032d82d9SRoland McGrath 
97896b8936aSChristoph Hellwig #if defined CONFIG_COMPAT
979032d82d9SRoland McGrath #include <linux/compat.h>
980032d82d9SRoland McGrath 
981032d82d9SRoland McGrath int compat_ptrace_request(struct task_struct *child, compat_long_t request,
982032d82d9SRoland McGrath 			  compat_ulong_t addr, compat_ulong_t data)
983032d82d9SRoland McGrath {
984032d82d9SRoland McGrath 	compat_ulong_t __user *datap = compat_ptr(data);
985032d82d9SRoland McGrath 	compat_ulong_t word;
986e16b2781SRoland McGrath 	siginfo_t siginfo;
987032d82d9SRoland McGrath 	int ret;
988032d82d9SRoland McGrath 
989032d82d9SRoland McGrath 	switch (request) {
990032d82d9SRoland McGrath 	case PTRACE_PEEKTEXT:
991032d82d9SRoland McGrath 	case PTRACE_PEEKDATA:
992032d82d9SRoland McGrath 		ret = access_process_vm(child, addr, &word, sizeof(word), 0);
993032d82d9SRoland McGrath 		if (ret != sizeof(word))
994032d82d9SRoland McGrath 			ret = -EIO;
995032d82d9SRoland McGrath 		else
996032d82d9SRoland McGrath 			ret = put_user(word, datap);
997032d82d9SRoland McGrath 		break;
998032d82d9SRoland McGrath 
999032d82d9SRoland McGrath 	case PTRACE_POKETEXT:
1000032d82d9SRoland McGrath 	case PTRACE_POKEDATA:
1001032d82d9SRoland McGrath 		ret = access_process_vm(child, addr, &data, sizeof(data), 1);
1002032d82d9SRoland McGrath 		ret = (ret != sizeof(data) ? -EIO : 0);
1003032d82d9SRoland McGrath 		break;
1004032d82d9SRoland McGrath 
1005032d82d9SRoland McGrath 	case PTRACE_GETEVENTMSG:
1006032d82d9SRoland McGrath 		ret = put_user((compat_ulong_t) child->ptrace_message, datap);
1007032d82d9SRoland McGrath 		break;
1008032d82d9SRoland McGrath 
1009e16b2781SRoland McGrath 	case PTRACE_GETSIGINFO:
1010e16b2781SRoland McGrath 		ret = ptrace_getsiginfo(child, &siginfo);
1011e16b2781SRoland McGrath 		if (!ret)
1012e16b2781SRoland McGrath 			ret = copy_siginfo_to_user32(
1013e16b2781SRoland McGrath 				(struct compat_siginfo __user *) datap,
1014e16b2781SRoland McGrath 				&siginfo);
1015e16b2781SRoland McGrath 		break;
1016e16b2781SRoland McGrath 
1017e16b2781SRoland McGrath 	case PTRACE_SETSIGINFO:
1018e16b2781SRoland McGrath 		memset(&siginfo, 0, sizeof siginfo);
1019e16b2781SRoland McGrath 		if (copy_siginfo_from_user32(
1020e16b2781SRoland McGrath 			    &siginfo, (struct compat_siginfo __user *) datap))
1021e16b2781SRoland McGrath 			ret = -EFAULT;
1022e16b2781SRoland McGrath 		else
1023e16b2781SRoland McGrath 			ret = ptrace_setsiginfo(child, &siginfo);
1024e16b2781SRoland McGrath 		break;
10252225a122SSuresh Siddha #ifdef CONFIG_HAVE_ARCH_TRACEHOOK
10262225a122SSuresh Siddha 	case PTRACE_GETREGSET:
10272225a122SSuresh Siddha 	case PTRACE_SETREGSET:
10282225a122SSuresh Siddha 	{
10292225a122SSuresh Siddha 		struct iovec kiov;
10302225a122SSuresh Siddha 		struct compat_iovec __user *uiov =
10312225a122SSuresh Siddha 			(struct compat_iovec __user *) datap;
10322225a122SSuresh Siddha 		compat_uptr_t ptr;
10332225a122SSuresh Siddha 		compat_size_t len;
10342225a122SSuresh Siddha 
10352225a122SSuresh Siddha 		if (!access_ok(VERIFY_WRITE, uiov, sizeof(*uiov)))
10362225a122SSuresh Siddha 			return -EFAULT;
10372225a122SSuresh Siddha 
10382225a122SSuresh Siddha 		if (__get_user(ptr, &uiov->iov_base) ||
10392225a122SSuresh Siddha 		    __get_user(len, &uiov->iov_len))
10402225a122SSuresh Siddha 			return -EFAULT;
10412225a122SSuresh Siddha 
10422225a122SSuresh Siddha 		kiov.iov_base = compat_ptr(ptr);
10432225a122SSuresh Siddha 		kiov.iov_len = len;
10442225a122SSuresh Siddha 
10452225a122SSuresh Siddha 		ret = ptrace_regset(child, request, addr, &kiov);
10462225a122SSuresh Siddha 		if (!ret)
10472225a122SSuresh Siddha 			ret = __put_user(kiov.iov_len, &uiov->iov_len);
10482225a122SSuresh Siddha 		break;
10492225a122SSuresh Siddha 	}
10502225a122SSuresh Siddha #endif
1051e16b2781SRoland McGrath 
1052032d82d9SRoland McGrath 	default:
1053032d82d9SRoland McGrath 		ret = ptrace_request(child, request, addr, data);
1054032d82d9SRoland McGrath 	}
1055032d82d9SRoland McGrath 
1056032d82d9SRoland McGrath 	return ret;
1057032d82d9SRoland McGrath }
1058c269f196SRoland McGrath 
1059c269f196SRoland McGrath asmlinkage long compat_sys_ptrace(compat_long_t request, compat_long_t pid,
1060c269f196SRoland McGrath 				  compat_long_t addr, compat_long_t data)
1061c269f196SRoland McGrath {
1062c269f196SRoland McGrath 	struct task_struct *child;
1063c269f196SRoland McGrath 	long ret;
1064c269f196SRoland McGrath 
1065c269f196SRoland McGrath 	if (request == PTRACE_TRACEME) {
1066c269f196SRoland McGrath 		ret = ptrace_traceme();
1067c269f196SRoland McGrath 		goto out;
1068c269f196SRoland McGrath 	}
1069c269f196SRoland McGrath 
1070c269f196SRoland McGrath 	child = ptrace_get_task_struct(pid);
1071c269f196SRoland McGrath 	if (IS_ERR(child)) {
1072c269f196SRoland McGrath 		ret = PTR_ERR(child);
1073c269f196SRoland McGrath 		goto out;
1074c269f196SRoland McGrath 	}
1075c269f196SRoland McGrath 
10763544d72aSTejun Heo 	if (request == PTRACE_ATTACH || request == PTRACE_SEIZE) {
1077aa9147c9SDenys Vlasenko 		ret = ptrace_attach(child, request, addr, data);
1078c269f196SRoland McGrath 		/*
1079c269f196SRoland McGrath 		 * Some architectures need to do book-keeping after
1080c269f196SRoland McGrath 		 * a ptrace attach.
1081c269f196SRoland McGrath 		 */
1082c269f196SRoland McGrath 		if (!ret)
1083c269f196SRoland McGrath 			arch_ptrace_attach(child);
1084c269f196SRoland McGrath 		goto out_put_task_struct;
1085c269f196SRoland McGrath 	}
1086c269f196SRoland McGrath 
1087fca26f26STejun Heo 	ret = ptrace_check_attach(child, request == PTRACE_KILL ||
1088fca26f26STejun Heo 				  request == PTRACE_INTERRUPT);
10899899d11fSOleg Nesterov 	if (!ret) {
1090c269f196SRoland McGrath 		ret = compat_arch_ptrace(child, request, addr, data);
10919899d11fSOleg Nesterov 		if (ret || request != PTRACE_DETACH)
10929899d11fSOleg Nesterov 			ptrace_unfreeze_traced(child);
10939899d11fSOleg Nesterov 	}
1094c269f196SRoland McGrath 
1095c269f196SRoland McGrath  out_put_task_struct:
1096c269f196SRoland McGrath 	put_task_struct(child);
1097c269f196SRoland McGrath  out:
1098c269f196SRoland McGrath 	return ret;
1099c269f196SRoland McGrath }
110096b8936aSChristoph Hellwig #endif	/* CONFIG_COMPAT */
1101bf26c018SFrederic Weisbecker 
1102bf26c018SFrederic Weisbecker #ifdef CONFIG_HAVE_HW_BREAKPOINT
1103bf26c018SFrederic Weisbecker int ptrace_get_breakpoints(struct task_struct *tsk)
1104bf26c018SFrederic Weisbecker {
1105bf26c018SFrederic Weisbecker 	if (atomic_inc_not_zero(&tsk->ptrace_bp_refcnt))
1106bf26c018SFrederic Weisbecker 		return 0;
1107bf26c018SFrederic Weisbecker 
1108bf26c018SFrederic Weisbecker 	return -1;
1109bf26c018SFrederic Weisbecker }
1110bf26c018SFrederic Weisbecker 
1111bf26c018SFrederic Weisbecker void ptrace_put_breakpoints(struct task_struct *tsk)
1112bf26c018SFrederic Weisbecker {
1113bf26c018SFrederic Weisbecker 	if (atomic_dec_and_test(&tsk->ptrace_bp_refcnt))
1114bf26c018SFrederic Weisbecker 		flush_ptrace_hw_breakpoint(tsk);
1115bf26c018SFrederic Weisbecker }
1116bf26c018SFrederic Weisbecker #endif /* CONFIG_HAVE_HW_BREAKPOINT */
1117