xref: /openbmc/linux/kernel/ptrace.c (revision 62c124ff)
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>
111da177e4SLinus Torvalds #include <linux/module.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>
261da177e4SLinus Torvalds 
27bf53de90SMarkus Metzger 
2862c124ffSTejun Heo static int ptrace_trapping_sleep_fn(void *flags)
2962c124ffSTejun Heo {
3062c124ffSTejun Heo 	schedule();
3162c124ffSTejun Heo 	return 0;
3262c124ffSTejun Heo }
3362c124ffSTejun Heo 
34bf53de90SMarkus Metzger /*
351da177e4SLinus Torvalds  * ptrace a task: make the debugger its new parent and
361da177e4SLinus Torvalds  * move it to the ptrace list.
371da177e4SLinus Torvalds  *
381da177e4SLinus Torvalds  * Must be called with the tasklist lock write-held.
391da177e4SLinus Torvalds  */
4036c8b586SIngo Molnar void __ptrace_link(struct task_struct *child, struct task_struct *new_parent)
411da177e4SLinus Torvalds {
42f470021aSRoland McGrath 	BUG_ON(!list_empty(&child->ptrace_entry));
43f470021aSRoland McGrath 	list_add(&child->ptrace_entry, &new_parent->ptraced);
441da177e4SLinus Torvalds 	child->parent = new_parent;
451da177e4SLinus Torvalds }
461da177e4SLinus Torvalds 
47e3bd058fSTejun Heo /**
48e3bd058fSTejun Heo  * __ptrace_unlink - unlink ptracee and restore its execution state
49e3bd058fSTejun Heo  * @child: ptracee to be unlinked
501da177e4SLinus Torvalds  *
510e9f0a4aSTejun Heo  * Remove @child from the ptrace list, move it back to the original parent,
520e9f0a4aSTejun Heo  * and restore the execution state so that it conforms to the group stop
530e9f0a4aSTejun Heo  * state.
540e9f0a4aSTejun Heo  *
550e9f0a4aSTejun Heo  * Unlinking can happen via two paths - explicit PTRACE_DETACH or ptracer
560e9f0a4aSTejun Heo  * exiting.  For PTRACE_DETACH, unless the ptracee has been killed between
570e9f0a4aSTejun Heo  * ptrace_check_attach() and here, it's guaranteed to be in TASK_TRACED.
580e9f0a4aSTejun Heo  * If the ptracer is exiting, the ptracee can be in any state.
590e9f0a4aSTejun Heo  *
600e9f0a4aSTejun Heo  * After detach, the ptracee should be in a state which conforms to the
610e9f0a4aSTejun Heo  * group stop.  If the group is stopped or in the process of stopping, the
620e9f0a4aSTejun Heo  * ptracee should be put into TASK_STOPPED; otherwise, it should be woken
630e9f0a4aSTejun Heo  * up from TASK_TRACED.
640e9f0a4aSTejun Heo  *
650e9f0a4aSTejun Heo  * If the ptracee is in TASK_TRACED and needs to be moved to TASK_STOPPED,
660e9f0a4aSTejun Heo  * it goes through TRACED -> RUNNING -> STOPPED transition which is similar
670e9f0a4aSTejun Heo  * to but in the opposite direction of what happens while attaching to a
680e9f0a4aSTejun Heo  * stopped task.  However, in this direction, the intermediate RUNNING
690e9f0a4aSTejun Heo  * state is not hidden even from the current ptracer and if it immediately
700e9f0a4aSTejun Heo  * re-attaches and performs a WNOHANG wait(2), it may fail.
71e3bd058fSTejun Heo  *
72e3bd058fSTejun Heo  * CONTEXT:
73e3bd058fSTejun Heo  * write_lock_irq(tasklist_lock)
741da177e4SLinus Torvalds  */
7536c8b586SIngo Molnar void __ptrace_unlink(struct task_struct *child)
761da177e4SLinus Torvalds {
775ecfbae0SOleg Nesterov 	BUG_ON(!child->ptrace);
785ecfbae0SOleg Nesterov 
791da177e4SLinus Torvalds 	child->ptrace = 0;
801da177e4SLinus Torvalds 	child->parent = child->real_parent;
81f470021aSRoland McGrath 	list_del_init(&child->ptrace_entry);
821da177e4SLinus Torvalds 
831da177e4SLinus Torvalds 	spin_lock(&child->sighand->siglock);
840e9f0a4aSTejun Heo 
851da177e4SLinus Torvalds 	/*
86a8f072c1STejun Heo 	 * Reinstate JOBCTL_STOP_PENDING if group stop is in effect and
870e9f0a4aSTejun Heo 	 * @child isn't dead.
881da177e4SLinus Torvalds 	 */
890e9f0a4aSTejun Heo 	if (!(child->flags & PF_EXITING) &&
900e9f0a4aSTejun Heo 	    (child->signal->flags & SIGNAL_STOP_STOPPED ||
910e9f0a4aSTejun Heo 	     child->signal->group_stop_count))
92a8f072c1STejun Heo 		child->jobctl |= JOBCTL_STOP_PENDING;
930e9f0a4aSTejun Heo 
940e9f0a4aSTejun Heo 	/*
950e9f0a4aSTejun Heo 	 * If transition to TASK_STOPPED is pending or in TASK_TRACED, kick
960e9f0a4aSTejun Heo 	 * @child in the butt.  Note that @resume should be used iff @child
970e9f0a4aSTejun Heo 	 * is in TASK_TRACED; otherwise, we might unduly disrupt
980e9f0a4aSTejun Heo 	 * TASK_KILLABLE sleeps.
990e9f0a4aSTejun Heo 	 */
100a8f072c1STejun Heo 	if (child->jobctl & JOBCTL_STOP_PENDING || task_is_traced(child))
1010e9f0a4aSTejun Heo 		signal_wake_up(child, task_is_traced(child));
1020e9f0a4aSTejun Heo 
1031da177e4SLinus Torvalds 	spin_unlock(&child->sighand->siglock);
1041da177e4SLinus Torvalds }
1051da177e4SLinus Torvalds 
106755e276bSTejun Heo /**
107755e276bSTejun Heo  * ptrace_check_attach - check whether ptracee is ready for ptrace operation
108755e276bSTejun Heo  * @child: ptracee to check for
109755e276bSTejun Heo  * @ignore_state: don't check whether @child is currently %TASK_TRACED
110755e276bSTejun Heo  *
111755e276bSTejun Heo  * Check whether @child is being ptraced by %current and ready for further
112755e276bSTejun Heo  * ptrace operations.  If @ignore_state is %false, @child also should be in
113755e276bSTejun Heo  * %TASK_TRACED state and on return the child is guaranteed to be traced
114755e276bSTejun Heo  * and not executing.  If @ignore_state is %true, @child can be in any
115755e276bSTejun Heo  * state.
116755e276bSTejun Heo  *
117755e276bSTejun Heo  * CONTEXT:
118755e276bSTejun Heo  * Grabs and releases tasklist_lock and @child->sighand->siglock.
119755e276bSTejun Heo  *
120755e276bSTejun Heo  * RETURNS:
121755e276bSTejun Heo  * 0 on success, -ESRCH if %child is not ready.
1221da177e4SLinus Torvalds  */
123755e276bSTejun Heo int ptrace_check_attach(struct task_struct *child, bool ignore_state)
1241da177e4SLinus Torvalds {
1251da177e4SLinus Torvalds 	int ret = -ESRCH;
1261da177e4SLinus Torvalds 
1271da177e4SLinus Torvalds 	/*
1281da177e4SLinus Torvalds 	 * We take the read lock around doing both checks to close a
1291da177e4SLinus Torvalds 	 * possible race where someone else was tracing our child and
1301da177e4SLinus Torvalds 	 * detached between these two checks.  After this locked check,
1311da177e4SLinus Torvalds 	 * we are sure that this is our traced child and that can only
1321da177e4SLinus Torvalds 	 * be changed by us so it's not changing right after this.
1331da177e4SLinus Torvalds 	 */
1341da177e4SLinus Torvalds 	read_lock(&tasklist_lock);
135c0c0b649SOleg Nesterov 	if ((child->ptrace & PT_PTRACED) && child->parent == current) {
136c0c0b649SOleg Nesterov 		/*
137c0c0b649SOleg Nesterov 		 * child->sighand can't be NULL, release_task()
138c0c0b649SOleg Nesterov 		 * does ptrace_unlink() before __exit_signal().
139c0c0b649SOleg Nesterov 		 */
1401da177e4SLinus Torvalds 		spin_lock_irq(&child->sighand->siglock);
141321fb561SOleg Nesterov 		WARN_ON_ONCE(task_is_stopped(child));
142755e276bSTejun Heo 		if (task_is_traced(child) || ignore_state)
143321fb561SOleg Nesterov 			ret = 0;
1441da177e4SLinus Torvalds 		spin_unlock_irq(&child->sighand->siglock);
1451da177e4SLinus Torvalds 	}
1461da177e4SLinus Torvalds 	read_unlock(&tasklist_lock);
1471da177e4SLinus Torvalds 
148755e276bSTejun Heo 	if (!ret && !ignore_state)
14985ba2d86SRoland McGrath 		ret = wait_task_inactive(child, TASK_TRACED) ? 0 : -ESRCH;
1501da177e4SLinus Torvalds 
1511da177e4SLinus Torvalds 	/* All systems go.. */
1521da177e4SLinus Torvalds 	return ret;
1531da177e4SLinus Torvalds }
1541da177e4SLinus Torvalds 
155006ebb40SStephen Smalley int __ptrace_may_access(struct task_struct *task, unsigned int mode)
156ab8d11beSMiklos Szeredi {
157c69e8d9cSDavid Howells 	const struct cred *cred = current_cred(), *tcred;
158b6dff3ecSDavid Howells 
159df26c40eSEric W. Biederman 	/* May we inspect the given task?
160df26c40eSEric W. Biederman 	 * This check is used both for attaching with ptrace
161df26c40eSEric W. Biederman 	 * and for allowing access to sensitive information in /proc.
162df26c40eSEric W. Biederman 	 *
163df26c40eSEric W. Biederman 	 * ptrace_attach denies several cases that /proc allows
164df26c40eSEric W. Biederman 	 * because setting up the necessary parent/child relationship
165df26c40eSEric W. Biederman 	 * or halting the specified task is impossible.
166df26c40eSEric W. Biederman 	 */
167df26c40eSEric W. Biederman 	int dumpable = 0;
168df26c40eSEric W. Biederman 	/* Don't let security modules deny introspection */
169df26c40eSEric W. Biederman 	if (task == current)
170df26c40eSEric W. Biederman 		return 0;
171c69e8d9cSDavid Howells 	rcu_read_lock();
172c69e8d9cSDavid Howells 	tcred = __task_cred(task);
1738409cca7SSerge E. Hallyn 	if (cred->user->user_ns == tcred->user->user_ns &&
1748409cca7SSerge E. Hallyn 	    (cred->uid == tcred->euid &&
1758409cca7SSerge E. Hallyn 	     cred->uid == tcred->suid &&
1768409cca7SSerge E. Hallyn 	     cred->uid == tcred->uid  &&
1778409cca7SSerge E. Hallyn 	     cred->gid == tcred->egid &&
1788409cca7SSerge E. Hallyn 	     cred->gid == tcred->sgid &&
1798409cca7SSerge E. Hallyn 	     cred->gid == tcred->gid))
1808409cca7SSerge E. Hallyn 		goto ok;
1818409cca7SSerge E. Hallyn 	if (ns_capable(tcred->user->user_ns, CAP_SYS_PTRACE))
1828409cca7SSerge E. Hallyn 		goto ok;
183c69e8d9cSDavid Howells 	rcu_read_unlock();
184ab8d11beSMiklos Szeredi 	return -EPERM;
1858409cca7SSerge E. Hallyn ok:
186c69e8d9cSDavid Howells 	rcu_read_unlock();
187ab8d11beSMiklos Szeredi 	smp_rmb();
188df26c40eSEric W. Biederman 	if (task->mm)
1896c5d5238SKawai, Hidehiro 		dumpable = get_dumpable(task->mm);
1908409cca7SSerge E. Hallyn 	if (!dumpable && !task_ns_capable(task, CAP_SYS_PTRACE))
191ab8d11beSMiklos Szeredi 		return -EPERM;
192ab8d11beSMiklos Szeredi 
1939e48858fSIngo Molnar 	return security_ptrace_access_check(task, mode);
194ab8d11beSMiklos Szeredi }
195ab8d11beSMiklos Szeredi 
196006ebb40SStephen Smalley bool ptrace_may_access(struct task_struct *task, unsigned int mode)
197ab8d11beSMiklos Szeredi {
198ab8d11beSMiklos Szeredi 	int err;
199ab8d11beSMiklos Szeredi 	task_lock(task);
200006ebb40SStephen Smalley 	err = __ptrace_may_access(task, mode);
201ab8d11beSMiklos Szeredi 	task_unlock(task);
2023a709703SRoland McGrath 	return !err;
203ab8d11beSMiklos Szeredi }
204ab8d11beSMiklos Szeredi 
205e3e89cc5SLinus Torvalds static int ptrace_attach(struct task_struct *task)
2061da177e4SLinus Torvalds {
2071da177e4SLinus Torvalds 	int retval;
208f5b40e36SLinus Torvalds 
209a5cb013dSAl Viro 	audit_ptrace(task);
210a5cb013dSAl Viro 
2111da177e4SLinus Torvalds 	retval = -EPERM;
212b79b7ba9SOleg Nesterov 	if (unlikely(task->flags & PF_KTHREAD))
213b79b7ba9SOleg Nesterov 		goto out;
214bac0abd6SPavel Emelyanov 	if (same_thread_group(task, current))
215f5b40e36SLinus Torvalds 		goto out;
216f5b40e36SLinus Torvalds 
217f2f0b00aSOleg Nesterov 	/*
218f2f0b00aSOleg Nesterov 	 * Protect exec's credential calculations against our interference;
2195e751e99SDavid Howells 	 * interference; SUID, SGID and LSM creds get determined differently
2205e751e99SDavid Howells 	 * under ptrace.
221d84f4f99SDavid Howells 	 */
222793285fcSOleg Nesterov 	retval = -ERESTARTNOINTR;
2239b1bf12dSKOSAKI Motohiro 	if (mutex_lock_interruptible(&task->signal->cred_guard_mutex))
224d84f4f99SDavid Howells 		goto out;
2254b105cbbSOleg Nesterov 
226f5b40e36SLinus Torvalds 	task_lock(task);
227006ebb40SStephen Smalley 	retval = __ptrace_may_access(task, PTRACE_MODE_ATTACH);
2284b105cbbSOleg Nesterov 	task_unlock(task);
2291da177e4SLinus Torvalds 	if (retval)
2304b105cbbSOleg Nesterov 		goto unlock_creds;
2311da177e4SLinus Torvalds 
2324b105cbbSOleg Nesterov 	write_lock_irq(&tasklist_lock);
233b79b7ba9SOleg Nesterov 	retval = -EPERM;
234b79b7ba9SOleg Nesterov 	if (unlikely(task->exit_state))
2354b105cbbSOleg Nesterov 		goto unlock_tasklist;
236f2f0b00aSOleg Nesterov 	if (task->ptrace)
2374b105cbbSOleg Nesterov 		goto unlock_tasklist;
238b79b7ba9SOleg Nesterov 
239f2f0b00aSOleg Nesterov 	task->ptrace = PT_PTRACED;
2408409cca7SSerge E. Hallyn 	if (task_ns_capable(task, CAP_SYS_PTRACE))
2411da177e4SLinus Torvalds 		task->ptrace |= PT_PTRACE_CAP;
2421da177e4SLinus Torvalds 
2431da177e4SLinus Torvalds 	__ptrace_link(task, current);
24433e9fc7dSOleg Nesterov 	send_sig_info(SIGSTOP, SEND_SIG_FORCED, task);
245b79b7ba9SOleg Nesterov 
246d79fdd6dSTejun Heo 	spin_lock(&task->sighand->siglock);
247d79fdd6dSTejun Heo 
248d79fdd6dSTejun Heo 	/*
249a8f072c1STejun Heo 	 * If the task is already STOPPED, set JOBCTL_STOP_PENDING and
250d79fdd6dSTejun Heo 	 * TRAPPING, and kick it so that it transits to TRACED.  TRAPPING
251d79fdd6dSTejun Heo 	 * will be cleared if the child completes the transition or any
252d79fdd6dSTejun Heo 	 * event which clears the group stop states happens.  We'll wait
253d79fdd6dSTejun Heo 	 * for the transition to complete before returning from this
254d79fdd6dSTejun Heo 	 * function.
255d79fdd6dSTejun Heo 	 *
256d79fdd6dSTejun Heo 	 * This hides STOPPED -> RUNNING -> TRACED transition from the
257d79fdd6dSTejun Heo 	 * attaching thread but a different thread in the same group can
258d79fdd6dSTejun Heo 	 * still observe the transient RUNNING state.  IOW, if another
259d79fdd6dSTejun Heo 	 * thread's WNOHANG wait(2) on the stopped tracee races against
260d79fdd6dSTejun Heo 	 * ATTACH, the wait(2) may fail due to the transient RUNNING.
261d79fdd6dSTejun Heo 	 *
262d79fdd6dSTejun Heo 	 * The following task_is_stopped() test is safe as both transitions
263d79fdd6dSTejun Heo 	 * in and out of STOPPED are protected by siglock.
264d79fdd6dSTejun Heo 	 */
2657dd3db54STejun Heo 	if (task_is_stopped(task) &&
2667dd3db54STejun Heo 	    task_set_jobctl_pending(task,
2677dd3db54STejun Heo 				    JOBCTL_STOP_PENDING | JOBCTL_TRAPPING))
268d79fdd6dSTejun Heo 		signal_wake_up(task, 1);
269d79fdd6dSTejun Heo 
270d79fdd6dSTejun Heo 	spin_unlock(&task->sighand->siglock);
271d79fdd6dSTejun Heo 
272b79b7ba9SOleg Nesterov 	retval = 0;
2734b105cbbSOleg Nesterov unlock_tasklist:
2744b105cbbSOleg Nesterov 	write_unlock_irq(&tasklist_lock);
2754b105cbbSOleg Nesterov unlock_creds:
2769b1bf12dSKOSAKI Motohiro 	mutex_unlock(&task->signal->cred_guard_mutex);
277f5b40e36SLinus Torvalds out:
2780b1007c3STejun Heo 	if (!retval)
27962c124ffSTejun Heo 		wait_on_bit(&task->jobctl, JOBCTL_TRAPPING_BIT,
28062c124ffSTejun Heo 			    ptrace_trapping_sleep_fn, TASK_UNINTERRUPTIBLE);
2811da177e4SLinus Torvalds 	return retval;
2821da177e4SLinus Torvalds }
2831da177e4SLinus Torvalds 
284f2f0b00aSOleg Nesterov /**
285f2f0b00aSOleg Nesterov  * ptrace_traceme  --  helper for PTRACE_TRACEME
286f2f0b00aSOleg Nesterov  *
287f2f0b00aSOleg Nesterov  * Performs checks and sets PT_PTRACED.
288f2f0b00aSOleg Nesterov  * Should be used by all ptrace implementations for PTRACE_TRACEME.
289f2f0b00aSOleg Nesterov  */
290e3e89cc5SLinus Torvalds static int ptrace_traceme(void)
291f2f0b00aSOleg Nesterov {
292f2f0b00aSOleg Nesterov 	int ret = -EPERM;
293f2f0b00aSOleg Nesterov 
2944b105cbbSOleg Nesterov 	write_lock_irq(&tasklist_lock);
2954b105cbbSOleg Nesterov 	/* Are we already being traced? */
296f2f0b00aSOleg Nesterov 	if (!current->ptrace) {
297f2f0b00aSOleg Nesterov 		ret = security_ptrace_traceme(current->parent);
298f2f0b00aSOleg Nesterov 		/*
299f2f0b00aSOleg Nesterov 		 * Check PF_EXITING to ensure ->real_parent has not passed
300f2f0b00aSOleg Nesterov 		 * exit_ptrace(). Otherwise we don't report the error but
301f2f0b00aSOleg Nesterov 		 * pretend ->real_parent untraces us right after return.
302f2f0b00aSOleg Nesterov 		 */
303f2f0b00aSOleg Nesterov 		if (!ret && !(current->real_parent->flags & PF_EXITING)) {
304f2f0b00aSOleg Nesterov 			current->ptrace = PT_PTRACED;
305f2f0b00aSOleg Nesterov 			__ptrace_link(current, current->real_parent);
306f2f0b00aSOleg Nesterov 		}
307f2f0b00aSOleg Nesterov 	}
3084b105cbbSOleg Nesterov 	write_unlock_irq(&tasklist_lock);
3094b105cbbSOleg Nesterov 
310f2f0b00aSOleg Nesterov 	return ret;
311f2f0b00aSOleg Nesterov }
312f2f0b00aSOleg Nesterov 
31339c626aeSOleg Nesterov /*
31439c626aeSOleg Nesterov  * Called with irqs disabled, returns true if childs should reap themselves.
31539c626aeSOleg Nesterov  */
31639c626aeSOleg Nesterov static int ignoring_children(struct sighand_struct *sigh)
31739c626aeSOleg Nesterov {
31839c626aeSOleg Nesterov 	int ret;
31939c626aeSOleg Nesterov 	spin_lock(&sigh->siglock);
32039c626aeSOleg Nesterov 	ret = (sigh->action[SIGCHLD-1].sa.sa_handler == SIG_IGN) ||
32139c626aeSOleg Nesterov 	      (sigh->action[SIGCHLD-1].sa.sa_flags & SA_NOCLDWAIT);
32239c626aeSOleg Nesterov 	spin_unlock(&sigh->siglock);
32339c626aeSOleg Nesterov 	return ret;
32439c626aeSOleg Nesterov }
32539c626aeSOleg Nesterov 
32639c626aeSOleg Nesterov /*
32739c626aeSOleg Nesterov  * Called with tasklist_lock held for writing.
32839c626aeSOleg Nesterov  * Unlink a traced task, and clean it up if it was a traced zombie.
32939c626aeSOleg Nesterov  * Return true if it needs to be reaped with release_task().
33039c626aeSOleg Nesterov  * (We can't call release_task() here because we already hold tasklist_lock.)
33139c626aeSOleg Nesterov  *
33239c626aeSOleg Nesterov  * If it's a zombie, our attachedness prevented normal parent notification
33339c626aeSOleg Nesterov  * or self-reaping.  Do notification now if it would have happened earlier.
33439c626aeSOleg Nesterov  * If it should reap itself, return true.
33539c626aeSOleg Nesterov  *
336a7f0765eSOleg Nesterov  * If it's our own child, there is no notification to do. But if our normal
337a7f0765eSOleg Nesterov  * children self-reap, then this child was prevented by ptrace and we must
338a7f0765eSOleg Nesterov  * reap it now, in that case we must also wake up sub-threads sleeping in
339a7f0765eSOleg Nesterov  * do_wait().
34039c626aeSOleg Nesterov  */
34139c626aeSOleg Nesterov static bool __ptrace_detach(struct task_struct *tracer, struct task_struct *p)
34239c626aeSOleg Nesterov {
34339c626aeSOleg Nesterov 	__ptrace_unlink(p);
34439c626aeSOleg Nesterov 
34539c626aeSOleg Nesterov 	if (p->exit_state == EXIT_ZOMBIE) {
34639c626aeSOleg Nesterov 		if (!task_detached(p) && thread_group_empty(p)) {
34739c626aeSOleg Nesterov 			if (!same_thread_group(p->real_parent, tracer))
34839c626aeSOleg Nesterov 				do_notify_parent(p, p->exit_signal);
349a7f0765eSOleg Nesterov 			else if (ignoring_children(tracer->sighand)) {
350a7f0765eSOleg Nesterov 				__wake_up_parent(p, tracer);
35139c626aeSOleg Nesterov 				p->exit_signal = -1;
35239c626aeSOleg Nesterov 			}
353a7f0765eSOleg Nesterov 		}
35439c626aeSOleg Nesterov 		if (task_detached(p)) {
35539c626aeSOleg Nesterov 			/* Mark it as in the process of being reaped. */
35639c626aeSOleg Nesterov 			p->exit_state = EXIT_DEAD;
35739c626aeSOleg Nesterov 			return true;
35839c626aeSOleg Nesterov 		}
35939c626aeSOleg Nesterov 	}
36039c626aeSOleg Nesterov 
36139c626aeSOleg Nesterov 	return false;
36239c626aeSOleg Nesterov }
36339c626aeSOleg Nesterov 
364e3e89cc5SLinus Torvalds static int ptrace_detach(struct task_struct *child, unsigned int data)
3651da177e4SLinus Torvalds {
36639c626aeSOleg Nesterov 	bool dead = false;
3674576145cSOleg Nesterov 
3687ed20e1aSJesper Juhl 	if (!valid_signal(data))
3691da177e4SLinus Torvalds 		return -EIO;
3701da177e4SLinus Torvalds 
3711da177e4SLinus Torvalds 	/* Architecture-specific hardware disable .. */
3721da177e4SLinus Torvalds 	ptrace_disable(child);
3737d941432SRoland McGrath 	clear_tsk_thread_flag(child, TIF_SYSCALL_TRACE);
3741da177e4SLinus Torvalds 
37595c3eb76SOleg Nesterov 	write_lock_irq(&tasklist_lock);
37639c626aeSOleg Nesterov 	/*
37739c626aeSOleg Nesterov 	 * This child can be already killed. Make sure de_thread() or
37839c626aeSOleg Nesterov 	 * our sub-thread doing do_wait() didn't do release_task() yet.
37939c626aeSOleg Nesterov 	 */
38095c3eb76SOleg Nesterov 	if (child->ptrace) {
38195c3eb76SOleg Nesterov 		child->exit_code = data;
3824576145cSOleg Nesterov 		dead = __ptrace_detach(current, child);
38395c3eb76SOleg Nesterov 	}
3841da177e4SLinus Torvalds 	write_unlock_irq(&tasklist_lock);
3851da177e4SLinus Torvalds 
3864576145cSOleg Nesterov 	if (unlikely(dead))
3874576145cSOleg Nesterov 		release_task(child);
3884576145cSOleg Nesterov 
3891da177e4SLinus Torvalds 	return 0;
3901da177e4SLinus Torvalds }
3911da177e4SLinus Torvalds 
39239c626aeSOleg Nesterov /*
393c7e49c14SOleg Nesterov  * Detach all tasks we were using ptrace on. Called with tasklist held
394c7e49c14SOleg Nesterov  * for writing, and returns with it held too. But note it can release
395c7e49c14SOleg Nesterov  * and reacquire the lock.
39639c626aeSOleg Nesterov  */
39739c626aeSOleg Nesterov void exit_ptrace(struct task_struct *tracer)
398c4b5ed25SNamhyung Kim 	__releases(&tasklist_lock)
399c4b5ed25SNamhyung Kim 	__acquires(&tasklist_lock)
40039c626aeSOleg Nesterov {
40139c626aeSOleg Nesterov 	struct task_struct *p, *n;
40239c626aeSOleg Nesterov 	LIST_HEAD(ptrace_dead);
40339c626aeSOleg Nesterov 
404c7e49c14SOleg Nesterov 	if (likely(list_empty(&tracer->ptraced)))
405c7e49c14SOleg Nesterov 		return;
406c7e49c14SOleg Nesterov 
40739c626aeSOleg Nesterov 	list_for_each_entry_safe(p, n, &tracer->ptraced, ptrace_entry) {
40839c626aeSOleg Nesterov 		if (__ptrace_detach(tracer, p))
40939c626aeSOleg Nesterov 			list_add(&p->ptrace_entry, &ptrace_dead);
41039c626aeSOleg Nesterov 	}
41139c626aeSOleg Nesterov 
412c7e49c14SOleg Nesterov 	write_unlock_irq(&tasklist_lock);
41339c626aeSOleg Nesterov 	BUG_ON(!list_empty(&tracer->ptraced));
41439c626aeSOleg Nesterov 
41539c626aeSOleg Nesterov 	list_for_each_entry_safe(p, n, &ptrace_dead, ptrace_entry) {
41639c626aeSOleg Nesterov 		list_del_init(&p->ptrace_entry);
41739c626aeSOleg Nesterov 		release_task(p);
41839c626aeSOleg Nesterov 	}
419c7e49c14SOleg Nesterov 
420c7e49c14SOleg Nesterov 	write_lock_irq(&tasklist_lock);
42139c626aeSOleg Nesterov }
42239c626aeSOleg Nesterov 
4231da177e4SLinus Torvalds int ptrace_readdata(struct task_struct *tsk, unsigned long src, char __user *dst, int len)
4241da177e4SLinus Torvalds {
4251da177e4SLinus Torvalds 	int copied = 0;
4261da177e4SLinus Torvalds 
4271da177e4SLinus Torvalds 	while (len > 0) {
4281da177e4SLinus Torvalds 		char buf[128];
4291da177e4SLinus Torvalds 		int this_len, retval;
4301da177e4SLinus Torvalds 
4311da177e4SLinus Torvalds 		this_len = (len > sizeof(buf)) ? sizeof(buf) : len;
4321da177e4SLinus Torvalds 		retval = access_process_vm(tsk, src, buf, this_len, 0);
4331da177e4SLinus Torvalds 		if (!retval) {
4341da177e4SLinus Torvalds 			if (copied)
4351da177e4SLinus Torvalds 				break;
4361da177e4SLinus Torvalds 			return -EIO;
4371da177e4SLinus Torvalds 		}
4381da177e4SLinus Torvalds 		if (copy_to_user(dst, buf, retval))
4391da177e4SLinus Torvalds 			return -EFAULT;
4401da177e4SLinus Torvalds 		copied += retval;
4411da177e4SLinus Torvalds 		src += retval;
4421da177e4SLinus Torvalds 		dst += retval;
4431da177e4SLinus Torvalds 		len -= retval;
4441da177e4SLinus Torvalds 	}
4451da177e4SLinus Torvalds 	return copied;
4461da177e4SLinus Torvalds }
4471da177e4SLinus Torvalds 
4481da177e4SLinus Torvalds int ptrace_writedata(struct task_struct *tsk, char __user *src, unsigned long dst, int len)
4491da177e4SLinus Torvalds {
4501da177e4SLinus Torvalds 	int copied = 0;
4511da177e4SLinus Torvalds 
4521da177e4SLinus Torvalds 	while (len > 0) {
4531da177e4SLinus Torvalds 		char buf[128];
4541da177e4SLinus Torvalds 		int this_len, retval;
4551da177e4SLinus Torvalds 
4561da177e4SLinus Torvalds 		this_len = (len > sizeof(buf)) ? sizeof(buf) : len;
4571da177e4SLinus Torvalds 		if (copy_from_user(buf, src, this_len))
4581da177e4SLinus Torvalds 			return -EFAULT;
4591da177e4SLinus Torvalds 		retval = access_process_vm(tsk, dst, buf, this_len, 1);
4601da177e4SLinus Torvalds 		if (!retval) {
4611da177e4SLinus Torvalds 			if (copied)
4621da177e4SLinus Torvalds 				break;
4631da177e4SLinus Torvalds 			return -EIO;
4641da177e4SLinus Torvalds 		}
4651da177e4SLinus Torvalds 		copied += retval;
4661da177e4SLinus Torvalds 		src += retval;
4671da177e4SLinus Torvalds 		dst += retval;
4681da177e4SLinus Torvalds 		len -= retval;
4691da177e4SLinus Torvalds 	}
4701da177e4SLinus Torvalds 	return copied;
4711da177e4SLinus Torvalds }
4721da177e4SLinus Torvalds 
4734abf9869SNamhyung Kim static int ptrace_setoptions(struct task_struct *child, unsigned long data)
4741da177e4SLinus Torvalds {
4751da177e4SLinus Torvalds 	child->ptrace &= ~PT_TRACE_MASK;
4761da177e4SLinus Torvalds 
4771da177e4SLinus Torvalds 	if (data & PTRACE_O_TRACESYSGOOD)
4781da177e4SLinus Torvalds 		child->ptrace |= PT_TRACESYSGOOD;
4791da177e4SLinus Torvalds 
4801da177e4SLinus Torvalds 	if (data & PTRACE_O_TRACEFORK)
4811da177e4SLinus Torvalds 		child->ptrace |= PT_TRACE_FORK;
4821da177e4SLinus Torvalds 
4831da177e4SLinus Torvalds 	if (data & PTRACE_O_TRACEVFORK)
4841da177e4SLinus Torvalds 		child->ptrace |= PT_TRACE_VFORK;
4851da177e4SLinus Torvalds 
4861da177e4SLinus Torvalds 	if (data & PTRACE_O_TRACECLONE)
4871da177e4SLinus Torvalds 		child->ptrace |= PT_TRACE_CLONE;
4881da177e4SLinus Torvalds 
4891da177e4SLinus Torvalds 	if (data & PTRACE_O_TRACEEXEC)
4901da177e4SLinus Torvalds 		child->ptrace |= PT_TRACE_EXEC;
4911da177e4SLinus Torvalds 
4921da177e4SLinus Torvalds 	if (data & PTRACE_O_TRACEVFORKDONE)
4931da177e4SLinus Torvalds 		child->ptrace |= PT_TRACE_VFORK_DONE;
4941da177e4SLinus Torvalds 
4951da177e4SLinus Torvalds 	if (data & PTRACE_O_TRACEEXIT)
4961da177e4SLinus Torvalds 		child->ptrace |= PT_TRACE_EXIT;
4971da177e4SLinus Torvalds 
4981da177e4SLinus Torvalds 	return (data & ~PTRACE_O_MASK) ? -EINVAL : 0;
4991da177e4SLinus Torvalds }
5001da177e4SLinus Torvalds 
501e16b2781SRoland McGrath static int ptrace_getsiginfo(struct task_struct *child, siginfo_t *info)
5021da177e4SLinus Torvalds {
503e4961254SOleg Nesterov 	unsigned long flags;
5041da177e4SLinus Torvalds 	int error = -ESRCH;
5051da177e4SLinus Torvalds 
506e4961254SOleg Nesterov 	if (lock_task_sighand(child, &flags)) {
5071da177e4SLinus Torvalds 		error = -EINVAL;
5081da177e4SLinus Torvalds 		if (likely(child->last_siginfo != NULL)) {
509e16b2781SRoland McGrath 			*info = *child->last_siginfo;
5101da177e4SLinus Torvalds 			error = 0;
5111da177e4SLinus Torvalds 		}
512e4961254SOleg Nesterov 		unlock_task_sighand(child, &flags);
5131da177e4SLinus Torvalds 	}
5141da177e4SLinus Torvalds 	return error;
5151da177e4SLinus Torvalds }
5161da177e4SLinus Torvalds 
517e16b2781SRoland McGrath static int ptrace_setsiginfo(struct task_struct *child, const siginfo_t *info)
5181da177e4SLinus Torvalds {
519e4961254SOleg Nesterov 	unsigned long flags;
5201da177e4SLinus Torvalds 	int error = -ESRCH;
5211da177e4SLinus Torvalds 
522e4961254SOleg Nesterov 	if (lock_task_sighand(child, &flags)) {
5231da177e4SLinus Torvalds 		error = -EINVAL;
5241da177e4SLinus Torvalds 		if (likely(child->last_siginfo != NULL)) {
525e16b2781SRoland McGrath 			*child->last_siginfo = *info;
5261da177e4SLinus Torvalds 			error = 0;
5271da177e4SLinus Torvalds 		}
528e4961254SOleg Nesterov 		unlock_task_sighand(child, &flags);
5291da177e4SLinus Torvalds 	}
5301da177e4SLinus Torvalds 	return error;
5311da177e4SLinus Torvalds }
5321da177e4SLinus Torvalds 
53336df29d7SRoland McGrath 
53436df29d7SRoland McGrath #ifdef PTRACE_SINGLESTEP
53536df29d7SRoland McGrath #define is_singlestep(request)		((request) == PTRACE_SINGLESTEP)
53636df29d7SRoland McGrath #else
53736df29d7SRoland McGrath #define is_singlestep(request)		0
53836df29d7SRoland McGrath #endif
53936df29d7SRoland McGrath 
5405b88abbfSRoland McGrath #ifdef PTRACE_SINGLEBLOCK
5415b88abbfSRoland McGrath #define is_singleblock(request)		((request) == PTRACE_SINGLEBLOCK)
5425b88abbfSRoland McGrath #else
5435b88abbfSRoland McGrath #define is_singleblock(request)		0
5445b88abbfSRoland McGrath #endif
5455b88abbfSRoland McGrath 
54636df29d7SRoland McGrath #ifdef PTRACE_SYSEMU
54736df29d7SRoland McGrath #define is_sysemu_singlestep(request)	((request) == PTRACE_SYSEMU_SINGLESTEP)
54836df29d7SRoland McGrath #else
54936df29d7SRoland McGrath #define is_sysemu_singlestep(request)	0
55036df29d7SRoland McGrath #endif
55136df29d7SRoland McGrath 
5524abf9869SNamhyung Kim static int ptrace_resume(struct task_struct *child, long request,
5534abf9869SNamhyung Kim 			 unsigned long data)
55436df29d7SRoland McGrath {
55536df29d7SRoland McGrath 	if (!valid_signal(data))
55636df29d7SRoland McGrath 		return -EIO;
55736df29d7SRoland McGrath 
55836df29d7SRoland McGrath 	if (request == PTRACE_SYSCALL)
55936df29d7SRoland McGrath 		set_tsk_thread_flag(child, TIF_SYSCALL_TRACE);
56036df29d7SRoland McGrath 	else
56136df29d7SRoland McGrath 		clear_tsk_thread_flag(child, TIF_SYSCALL_TRACE);
56236df29d7SRoland McGrath 
56336df29d7SRoland McGrath #ifdef TIF_SYSCALL_EMU
56436df29d7SRoland McGrath 	if (request == PTRACE_SYSEMU || request == PTRACE_SYSEMU_SINGLESTEP)
56536df29d7SRoland McGrath 		set_tsk_thread_flag(child, TIF_SYSCALL_EMU);
56636df29d7SRoland McGrath 	else
56736df29d7SRoland McGrath 		clear_tsk_thread_flag(child, TIF_SYSCALL_EMU);
56836df29d7SRoland McGrath #endif
56936df29d7SRoland McGrath 
5705b88abbfSRoland McGrath 	if (is_singleblock(request)) {
5715b88abbfSRoland McGrath 		if (unlikely(!arch_has_block_step()))
5725b88abbfSRoland McGrath 			return -EIO;
5735b88abbfSRoland McGrath 		user_enable_block_step(child);
5745b88abbfSRoland McGrath 	} else if (is_singlestep(request) || is_sysemu_singlestep(request)) {
57536df29d7SRoland McGrath 		if (unlikely(!arch_has_single_step()))
57636df29d7SRoland McGrath 			return -EIO;
57736df29d7SRoland McGrath 		user_enable_single_step(child);
5783a709703SRoland McGrath 	} else {
57936df29d7SRoland McGrath 		user_disable_single_step(child);
5803a709703SRoland McGrath 	}
58136df29d7SRoland McGrath 
58236df29d7SRoland McGrath 	child->exit_code = data;
5830666fb51SOleg Nesterov 	wake_up_state(child, __TASK_TRACED);
58436df29d7SRoland McGrath 
58536df29d7SRoland McGrath 	return 0;
58636df29d7SRoland McGrath }
58736df29d7SRoland McGrath 
5882225a122SSuresh Siddha #ifdef CONFIG_HAVE_ARCH_TRACEHOOK
5892225a122SSuresh Siddha 
5902225a122SSuresh Siddha static const struct user_regset *
5912225a122SSuresh Siddha find_regset(const struct user_regset_view *view, unsigned int type)
5922225a122SSuresh Siddha {
5932225a122SSuresh Siddha 	const struct user_regset *regset;
5942225a122SSuresh Siddha 	int n;
5952225a122SSuresh Siddha 
5962225a122SSuresh Siddha 	for (n = 0; n < view->n; ++n) {
5972225a122SSuresh Siddha 		regset = view->regsets + n;
5982225a122SSuresh Siddha 		if (regset->core_note_type == type)
5992225a122SSuresh Siddha 			return regset;
6002225a122SSuresh Siddha 	}
6012225a122SSuresh Siddha 
6022225a122SSuresh Siddha 	return NULL;
6032225a122SSuresh Siddha }
6042225a122SSuresh Siddha 
6052225a122SSuresh Siddha static int ptrace_regset(struct task_struct *task, int req, unsigned int type,
6062225a122SSuresh Siddha 			 struct iovec *kiov)
6072225a122SSuresh Siddha {
6082225a122SSuresh Siddha 	const struct user_regset_view *view = task_user_regset_view(task);
6092225a122SSuresh Siddha 	const struct user_regset *regset = find_regset(view, type);
6102225a122SSuresh Siddha 	int regset_no;
6112225a122SSuresh Siddha 
6122225a122SSuresh Siddha 	if (!regset || (kiov->iov_len % regset->size) != 0)
613c6a0dd7eSSuresh Siddha 		return -EINVAL;
6142225a122SSuresh Siddha 
6152225a122SSuresh Siddha 	regset_no = regset - view->regsets;
6162225a122SSuresh Siddha 	kiov->iov_len = min(kiov->iov_len,
6172225a122SSuresh Siddha 			    (__kernel_size_t) (regset->n * regset->size));
6182225a122SSuresh Siddha 
6192225a122SSuresh Siddha 	if (req == PTRACE_GETREGSET)
6202225a122SSuresh Siddha 		return copy_regset_to_user(task, view, regset_no, 0,
6212225a122SSuresh Siddha 					   kiov->iov_len, kiov->iov_base);
6222225a122SSuresh Siddha 	else
6232225a122SSuresh Siddha 		return copy_regset_from_user(task, view, regset_no, 0,
6242225a122SSuresh Siddha 					     kiov->iov_len, kiov->iov_base);
6252225a122SSuresh Siddha }
6262225a122SSuresh Siddha 
6272225a122SSuresh Siddha #endif
6282225a122SSuresh Siddha 
6291da177e4SLinus Torvalds int ptrace_request(struct task_struct *child, long request,
6304abf9869SNamhyung Kim 		   unsigned long addr, unsigned long data)
6311da177e4SLinus Torvalds {
6321da177e4SLinus Torvalds 	int ret = -EIO;
633e16b2781SRoland McGrath 	siginfo_t siginfo;
6349fed81dcSNamhyung Kim 	void __user *datavp = (void __user *) data;
6359fed81dcSNamhyung Kim 	unsigned long __user *datalp = datavp;
6361da177e4SLinus Torvalds 
6371da177e4SLinus Torvalds 	switch (request) {
63816c3e389SRoland McGrath 	case PTRACE_PEEKTEXT:
63916c3e389SRoland McGrath 	case PTRACE_PEEKDATA:
64016c3e389SRoland McGrath 		return generic_ptrace_peekdata(child, addr, data);
64116c3e389SRoland McGrath 	case PTRACE_POKETEXT:
64216c3e389SRoland McGrath 	case PTRACE_POKEDATA:
64316c3e389SRoland McGrath 		return generic_ptrace_pokedata(child, addr, data);
64416c3e389SRoland McGrath 
6451da177e4SLinus Torvalds #ifdef PTRACE_OLDSETOPTIONS
6461da177e4SLinus Torvalds 	case PTRACE_OLDSETOPTIONS:
6471da177e4SLinus Torvalds #endif
6481da177e4SLinus Torvalds 	case PTRACE_SETOPTIONS:
6491da177e4SLinus Torvalds 		ret = ptrace_setoptions(child, data);
6501da177e4SLinus Torvalds 		break;
6511da177e4SLinus Torvalds 	case PTRACE_GETEVENTMSG:
6529fed81dcSNamhyung Kim 		ret = put_user(child->ptrace_message, datalp);
6531da177e4SLinus Torvalds 		break;
654e16b2781SRoland McGrath 
6551da177e4SLinus Torvalds 	case PTRACE_GETSIGINFO:
656e16b2781SRoland McGrath 		ret = ptrace_getsiginfo(child, &siginfo);
657e16b2781SRoland McGrath 		if (!ret)
6589fed81dcSNamhyung Kim 			ret = copy_siginfo_to_user(datavp, &siginfo);
6591da177e4SLinus Torvalds 		break;
660e16b2781SRoland McGrath 
6611da177e4SLinus Torvalds 	case PTRACE_SETSIGINFO:
6629fed81dcSNamhyung Kim 		if (copy_from_user(&siginfo, datavp, sizeof siginfo))
663e16b2781SRoland McGrath 			ret = -EFAULT;
664e16b2781SRoland McGrath 		else
665e16b2781SRoland McGrath 			ret = ptrace_setsiginfo(child, &siginfo);
6661da177e4SLinus Torvalds 		break;
667e16b2781SRoland McGrath 
6681bcf5482SAlexey Dobriyan 	case PTRACE_DETACH:	 /* detach a process that was attached. */
6691bcf5482SAlexey Dobriyan 		ret = ptrace_detach(child, data);
6701bcf5482SAlexey Dobriyan 		break;
67136df29d7SRoland McGrath 
6729c1a1259SMike Frysinger #ifdef CONFIG_BINFMT_ELF_FDPIC
6739c1a1259SMike Frysinger 	case PTRACE_GETFDPIC: {
674e0129ef9SOleg Nesterov 		struct mm_struct *mm = get_task_mm(child);
6759c1a1259SMike Frysinger 		unsigned long tmp = 0;
6769c1a1259SMike Frysinger 
677e0129ef9SOleg Nesterov 		ret = -ESRCH;
678e0129ef9SOleg Nesterov 		if (!mm)
679e0129ef9SOleg Nesterov 			break;
680e0129ef9SOleg Nesterov 
6819c1a1259SMike Frysinger 		switch (addr) {
6829c1a1259SMike Frysinger 		case PTRACE_GETFDPIC_EXEC:
683e0129ef9SOleg Nesterov 			tmp = mm->context.exec_fdpic_loadmap;
6849c1a1259SMike Frysinger 			break;
6859c1a1259SMike Frysinger 		case PTRACE_GETFDPIC_INTERP:
686e0129ef9SOleg Nesterov 			tmp = mm->context.interp_fdpic_loadmap;
6879c1a1259SMike Frysinger 			break;
6889c1a1259SMike Frysinger 		default:
6899c1a1259SMike Frysinger 			break;
6909c1a1259SMike Frysinger 		}
691e0129ef9SOleg Nesterov 		mmput(mm);
6929c1a1259SMike Frysinger 
6939fed81dcSNamhyung Kim 		ret = put_user(tmp, datalp);
6949c1a1259SMike Frysinger 		break;
6959c1a1259SMike Frysinger 	}
6969c1a1259SMike Frysinger #endif
6979c1a1259SMike Frysinger 
69836df29d7SRoland McGrath #ifdef PTRACE_SINGLESTEP
69936df29d7SRoland McGrath 	case PTRACE_SINGLESTEP:
70036df29d7SRoland McGrath #endif
7015b88abbfSRoland McGrath #ifdef PTRACE_SINGLEBLOCK
7025b88abbfSRoland McGrath 	case PTRACE_SINGLEBLOCK:
7035b88abbfSRoland McGrath #endif
70436df29d7SRoland McGrath #ifdef PTRACE_SYSEMU
70536df29d7SRoland McGrath 	case PTRACE_SYSEMU:
70636df29d7SRoland McGrath 	case PTRACE_SYSEMU_SINGLESTEP:
70736df29d7SRoland McGrath #endif
70836df29d7SRoland McGrath 	case PTRACE_SYSCALL:
70936df29d7SRoland McGrath 	case PTRACE_CONT:
71036df29d7SRoland McGrath 		return ptrace_resume(child, request, data);
71136df29d7SRoland McGrath 
71236df29d7SRoland McGrath 	case PTRACE_KILL:
71336df29d7SRoland McGrath 		if (child->exit_state)	/* already dead */
71436df29d7SRoland McGrath 			return 0;
71536df29d7SRoland McGrath 		return ptrace_resume(child, request, SIGKILL);
71636df29d7SRoland McGrath 
7172225a122SSuresh Siddha #ifdef CONFIG_HAVE_ARCH_TRACEHOOK
7182225a122SSuresh Siddha 	case PTRACE_GETREGSET:
7192225a122SSuresh Siddha 	case PTRACE_SETREGSET:
7202225a122SSuresh Siddha 	{
7212225a122SSuresh Siddha 		struct iovec kiov;
7229fed81dcSNamhyung Kim 		struct iovec __user *uiov = datavp;
7232225a122SSuresh Siddha 
7242225a122SSuresh Siddha 		if (!access_ok(VERIFY_WRITE, uiov, sizeof(*uiov)))
7252225a122SSuresh Siddha 			return -EFAULT;
7262225a122SSuresh Siddha 
7272225a122SSuresh Siddha 		if (__get_user(kiov.iov_base, &uiov->iov_base) ||
7282225a122SSuresh Siddha 		    __get_user(kiov.iov_len, &uiov->iov_len))
7292225a122SSuresh Siddha 			return -EFAULT;
7302225a122SSuresh Siddha 
7312225a122SSuresh Siddha 		ret = ptrace_regset(child, request, addr, &kiov);
7322225a122SSuresh Siddha 		if (!ret)
7332225a122SSuresh Siddha 			ret = __put_user(kiov.iov_len, &uiov->iov_len);
7342225a122SSuresh Siddha 		break;
7352225a122SSuresh Siddha 	}
7362225a122SSuresh Siddha #endif
7371da177e4SLinus Torvalds 	default:
7381da177e4SLinus Torvalds 		break;
7391da177e4SLinus Torvalds 	}
7401da177e4SLinus Torvalds 
7411da177e4SLinus Torvalds 	return ret;
7421da177e4SLinus Torvalds }
743481bed45SChristoph Hellwig 
7448053bdd5SOleg Nesterov static struct task_struct *ptrace_get_task_struct(pid_t pid)
7456b9c7ed8SChristoph Hellwig {
7466b9c7ed8SChristoph Hellwig 	struct task_struct *child;
7476b9c7ed8SChristoph Hellwig 
7488053bdd5SOleg Nesterov 	rcu_read_lock();
749228ebcbeSPavel Emelyanov 	child = find_task_by_vpid(pid);
750481bed45SChristoph Hellwig 	if (child)
751481bed45SChristoph Hellwig 		get_task_struct(child);
7528053bdd5SOleg Nesterov 	rcu_read_unlock();
753f400e198SSukadev Bhattiprolu 
754481bed45SChristoph Hellwig 	if (!child)
7556b9c7ed8SChristoph Hellwig 		return ERR_PTR(-ESRCH);
7566b9c7ed8SChristoph Hellwig 	return child;
757481bed45SChristoph Hellwig }
758481bed45SChristoph Hellwig 
7590ac15559SChristoph Hellwig #ifndef arch_ptrace_attach
7600ac15559SChristoph Hellwig #define arch_ptrace_attach(child)	do { } while (0)
7610ac15559SChristoph Hellwig #endif
7620ac15559SChristoph Hellwig 
7634abf9869SNamhyung Kim SYSCALL_DEFINE4(ptrace, long, request, long, pid, unsigned long, addr,
7644abf9869SNamhyung Kim 		unsigned long, data)
765481bed45SChristoph Hellwig {
766481bed45SChristoph Hellwig 	struct task_struct *child;
767481bed45SChristoph Hellwig 	long ret;
768481bed45SChristoph Hellwig 
7696b9c7ed8SChristoph Hellwig 	if (request == PTRACE_TRACEME) {
7706b9c7ed8SChristoph Hellwig 		ret = ptrace_traceme();
7716ea6dd93SHaavard Skinnemoen 		if (!ret)
7726ea6dd93SHaavard Skinnemoen 			arch_ptrace_attach(current);
773481bed45SChristoph Hellwig 		goto out;
7746b9c7ed8SChristoph Hellwig 	}
7756b9c7ed8SChristoph Hellwig 
7766b9c7ed8SChristoph Hellwig 	child = ptrace_get_task_struct(pid);
7776b9c7ed8SChristoph Hellwig 	if (IS_ERR(child)) {
7786b9c7ed8SChristoph Hellwig 		ret = PTR_ERR(child);
7796b9c7ed8SChristoph Hellwig 		goto out;
7806b9c7ed8SChristoph Hellwig 	}
781481bed45SChristoph Hellwig 
782481bed45SChristoph Hellwig 	if (request == PTRACE_ATTACH) {
783481bed45SChristoph Hellwig 		ret = ptrace_attach(child);
7840ac15559SChristoph Hellwig 		/*
7850ac15559SChristoph Hellwig 		 * Some architectures need to do book-keeping after
7860ac15559SChristoph Hellwig 		 * a ptrace attach.
7870ac15559SChristoph Hellwig 		 */
7880ac15559SChristoph Hellwig 		if (!ret)
7890ac15559SChristoph Hellwig 			arch_ptrace_attach(child);
790005f18dfSChristoph Hellwig 		goto out_put_task_struct;
791481bed45SChristoph Hellwig 	}
792481bed45SChristoph Hellwig 
793481bed45SChristoph Hellwig 	ret = ptrace_check_attach(child, request == PTRACE_KILL);
794481bed45SChristoph Hellwig 	if (ret < 0)
795481bed45SChristoph Hellwig 		goto out_put_task_struct;
796481bed45SChristoph Hellwig 
797481bed45SChristoph Hellwig 	ret = arch_ptrace(child, request, addr, data);
798481bed45SChristoph Hellwig 
799481bed45SChristoph Hellwig  out_put_task_struct:
800481bed45SChristoph Hellwig 	put_task_struct(child);
801481bed45SChristoph Hellwig  out:
802481bed45SChristoph Hellwig 	return ret;
803481bed45SChristoph Hellwig }
80476647323SAlexey Dobriyan 
8054abf9869SNamhyung Kim int generic_ptrace_peekdata(struct task_struct *tsk, unsigned long addr,
8064abf9869SNamhyung Kim 			    unsigned long data)
80776647323SAlexey Dobriyan {
80876647323SAlexey Dobriyan 	unsigned long tmp;
80976647323SAlexey Dobriyan 	int copied;
81076647323SAlexey Dobriyan 
81176647323SAlexey Dobriyan 	copied = access_process_vm(tsk, addr, &tmp, sizeof(tmp), 0);
81276647323SAlexey Dobriyan 	if (copied != sizeof(tmp))
81376647323SAlexey Dobriyan 		return -EIO;
81476647323SAlexey Dobriyan 	return put_user(tmp, (unsigned long __user *)data);
81576647323SAlexey Dobriyan }
816f284ce72SAlexey Dobriyan 
8174abf9869SNamhyung Kim int generic_ptrace_pokedata(struct task_struct *tsk, unsigned long addr,
8184abf9869SNamhyung Kim 			    unsigned long data)
819f284ce72SAlexey Dobriyan {
820f284ce72SAlexey Dobriyan 	int copied;
821f284ce72SAlexey Dobriyan 
822f284ce72SAlexey Dobriyan 	copied = access_process_vm(tsk, addr, &data, sizeof(data), 1);
823f284ce72SAlexey Dobriyan 	return (copied == sizeof(data)) ? 0 : -EIO;
824f284ce72SAlexey Dobriyan }
825032d82d9SRoland McGrath 
82696b8936aSChristoph Hellwig #if defined CONFIG_COMPAT
827032d82d9SRoland McGrath #include <linux/compat.h>
828032d82d9SRoland McGrath 
829032d82d9SRoland McGrath int compat_ptrace_request(struct task_struct *child, compat_long_t request,
830032d82d9SRoland McGrath 			  compat_ulong_t addr, compat_ulong_t data)
831032d82d9SRoland McGrath {
832032d82d9SRoland McGrath 	compat_ulong_t __user *datap = compat_ptr(data);
833032d82d9SRoland McGrath 	compat_ulong_t word;
834e16b2781SRoland McGrath 	siginfo_t siginfo;
835032d82d9SRoland McGrath 	int ret;
836032d82d9SRoland McGrath 
837032d82d9SRoland McGrath 	switch (request) {
838032d82d9SRoland McGrath 	case PTRACE_PEEKTEXT:
839032d82d9SRoland McGrath 	case PTRACE_PEEKDATA:
840032d82d9SRoland McGrath 		ret = access_process_vm(child, addr, &word, sizeof(word), 0);
841032d82d9SRoland McGrath 		if (ret != sizeof(word))
842032d82d9SRoland McGrath 			ret = -EIO;
843032d82d9SRoland McGrath 		else
844032d82d9SRoland McGrath 			ret = put_user(word, datap);
845032d82d9SRoland McGrath 		break;
846032d82d9SRoland McGrath 
847032d82d9SRoland McGrath 	case PTRACE_POKETEXT:
848032d82d9SRoland McGrath 	case PTRACE_POKEDATA:
849032d82d9SRoland McGrath 		ret = access_process_vm(child, addr, &data, sizeof(data), 1);
850032d82d9SRoland McGrath 		ret = (ret != sizeof(data) ? -EIO : 0);
851032d82d9SRoland McGrath 		break;
852032d82d9SRoland McGrath 
853032d82d9SRoland McGrath 	case PTRACE_GETEVENTMSG:
854032d82d9SRoland McGrath 		ret = put_user((compat_ulong_t) child->ptrace_message, datap);
855032d82d9SRoland McGrath 		break;
856032d82d9SRoland McGrath 
857e16b2781SRoland McGrath 	case PTRACE_GETSIGINFO:
858e16b2781SRoland McGrath 		ret = ptrace_getsiginfo(child, &siginfo);
859e16b2781SRoland McGrath 		if (!ret)
860e16b2781SRoland McGrath 			ret = copy_siginfo_to_user32(
861e16b2781SRoland McGrath 				(struct compat_siginfo __user *) datap,
862e16b2781SRoland McGrath 				&siginfo);
863e16b2781SRoland McGrath 		break;
864e16b2781SRoland McGrath 
865e16b2781SRoland McGrath 	case PTRACE_SETSIGINFO:
866e16b2781SRoland McGrath 		memset(&siginfo, 0, sizeof siginfo);
867e16b2781SRoland McGrath 		if (copy_siginfo_from_user32(
868e16b2781SRoland McGrath 			    &siginfo, (struct compat_siginfo __user *) datap))
869e16b2781SRoland McGrath 			ret = -EFAULT;
870e16b2781SRoland McGrath 		else
871e16b2781SRoland McGrath 			ret = ptrace_setsiginfo(child, &siginfo);
872e16b2781SRoland McGrath 		break;
8732225a122SSuresh Siddha #ifdef CONFIG_HAVE_ARCH_TRACEHOOK
8742225a122SSuresh Siddha 	case PTRACE_GETREGSET:
8752225a122SSuresh Siddha 	case PTRACE_SETREGSET:
8762225a122SSuresh Siddha 	{
8772225a122SSuresh Siddha 		struct iovec kiov;
8782225a122SSuresh Siddha 		struct compat_iovec __user *uiov =
8792225a122SSuresh Siddha 			(struct compat_iovec __user *) datap;
8802225a122SSuresh Siddha 		compat_uptr_t ptr;
8812225a122SSuresh Siddha 		compat_size_t len;
8822225a122SSuresh Siddha 
8832225a122SSuresh Siddha 		if (!access_ok(VERIFY_WRITE, uiov, sizeof(*uiov)))
8842225a122SSuresh Siddha 			return -EFAULT;
8852225a122SSuresh Siddha 
8862225a122SSuresh Siddha 		if (__get_user(ptr, &uiov->iov_base) ||
8872225a122SSuresh Siddha 		    __get_user(len, &uiov->iov_len))
8882225a122SSuresh Siddha 			return -EFAULT;
8892225a122SSuresh Siddha 
8902225a122SSuresh Siddha 		kiov.iov_base = compat_ptr(ptr);
8912225a122SSuresh Siddha 		kiov.iov_len = len;
8922225a122SSuresh Siddha 
8932225a122SSuresh Siddha 		ret = ptrace_regset(child, request, addr, &kiov);
8942225a122SSuresh Siddha 		if (!ret)
8952225a122SSuresh Siddha 			ret = __put_user(kiov.iov_len, &uiov->iov_len);
8962225a122SSuresh Siddha 		break;
8972225a122SSuresh Siddha 	}
8982225a122SSuresh Siddha #endif
899e16b2781SRoland McGrath 
900032d82d9SRoland McGrath 	default:
901032d82d9SRoland McGrath 		ret = ptrace_request(child, request, addr, data);
902032d82d9SRoland McGrath 	}
903032d82d9SRoland McGrath 
904032d82d9SRoland McGrath 	return ret;
905032d82d9SRoland McGrath }
906c269f196SRoland McGrath 
907c269f196SRoland McGrath asmlinkage long compat_sys_ptrace(compat_long_t request, compat_long_t pid,
908c269f196SRoland McGrath 				  compat_long_t addr, compat_long_t data)
909c269f196SRoland McGrath {
910c269f196SRoland McGrath 	struct task_struct *child;
911c269f196SRoland McGrath 	long ret;
912c269f196SRoland McGrath 
913c269f196SRoland McGrath 	if (request == PTRACE_TRACEME) {
914c269f196SRoland McGrath 		ret = ptrace_traceme();
915c269f196SRoland McGrath 		goto out;
916c269f196SRoland McGrath 	}
917c269f196SRoland McGrath 
918c269f196SRoland McGrath 	child = ptrace_get_task_struct(pid);
919c269f196SRoland McGrath 	if (IS_ERR(child)) {
920c269f196SRoland McGrath 		ret = PTR_ERR(child);
921c269f196SRoland McGrath 		goto out;
922c269f196SRoland McGrath 	}
923c269f196SRoland McGrath 
924c269f196SRoland McGrath 	if (request == PTRACE_ATTACH) {
925c269f196SRoland McGrath 		ret = ptrace_attach(child);
926c269f196SRoland McGrath 		/*
927c269f196SRoland McGrath 		 * Some architectures need to do book-keeping after
928c269f196SRoland McGrath 		 * a ptrace attach.
929c269f196SRoland McGrath 		 */
930c269f196SRoland McGrath 		if (!ret)
931c269f196SRoland McGrath 			arch_ptrace_attach(child);
932c269f196SRoland McGrath 		goto out_put_task_struct;
933c269f196SRoland McGrath 	}
934c269f196SRoland McGrath 
935c269f196SRoland McGrath 	ret = ptrace_check_attach(child, request == PTRACE_KILL);
936c269f196SRoland McGrath 	if (!ret)
937c269f196SRoland McGrath 		ret = compat_arch_ptrace(child, request, addr, data);
938c269f196SRoland McGrath 
939c269f196SRoland McGrath  out_put_task_struct:
940c269f196SRoland McGrath 	put_task_struct(child);
941c269f196SRoland McGrath  out:
942c269f196SRoland McGrath 	return ret;
943c269f196SRoland McGrath }
94496b8936aSChristoph Hellwig #endif	/* CONFIG_COMPAT */
945bf26c018SFrederic Weisbecker 
946bf26c018SFrederic Weisbecker #ifdef CONFIG_HAVE_HW_BREAKPOINT
947bf26c018SFrederic Weisbecker int ptrace_get_breakpoints(struct task_struct *tsk)
948bf26c018SFrederic Weisbecker {
949bf26c018SFrederic Weisbecker 	if (atomic_inc_not_zero(&tsk->ptrace_bp_refcnt))
950bf26c018SFrederic Weisbecker 		return 0;
951bf26c018SFrederic Weisbecker 
952bf26c018SFrederic Weisbecker 	return -1;
953bf26c018SFrederic Weisbecker }
954bf26c018SFrederic Weisbecker 
955bf26c018SFrederic Weisbecker void ptrace_put_breakpoints(struct task_struct *tsk)
956bf26c018SFrederic Weisbecker {
957bf26c018SFrederic Weisbecker 	if (atomic_dec_and_test(&tsk->ptrace_bp_refcnt))
958bf26c018SFrederic Weisbecker 		flush_ptrace_hw_breakpoint(tsk);
959bf26c018SFrederic Weisbecker }
960bf26c018SFrederic Weisbecker #endif /* CONFIG_HAVE_HW_BREAKPOINT */
961