xref: /openbmc/linux/kernel/ptrace.c (revision 174cd4b1)
1 /*
2  * linux/kernel/ptrace.c
3  *
4  * (C) Copyright 1999 Linus Torvalds
5  *
6  * Common interfaces for "ptrace()" which we do not want
7  * to continually duplicate across every architecture.
8  */
9 
10 #include <linux/capability.h>
11 #include <linux/export.h>
12 #include <linux/sched.h>
13 #include <linux/sched/mm.h>
14 #include <linux/sched/coredump.h>
15 #include <linux/errno.h>
16 #include <linux/mm.h>
17 #include <linux/highmem.h>
18 #include <linux/pagemap.h>
19 #include <linux/ptrace.h>
20 #include <linux/security.h>
21 #include <linux/signal.h>
22 #include <linux/uio.h>
23 #include <linux/audit.h>
24 #include <linux/pid_namespace.h>
25 #include <linux/syscalls.h>
26 #include <linux/uaccess.h>
27 #include <linux/regset.h>
28 #include <linux/hw_breakpoint.h>
29 #include <linux/cn_proc.h>
30 #include <linux/compat.h>
31 
32 /*
33  * Access another process' address space via ptrace.
34  * Source/target buffer must be kernel space,
35  * Do not walk the page table directly, use get_user_pages
36  */
37 int ptrace_access_vm(struct task_struct *tsk, unsigned long addr,
38 		     void *buf, int len, unsigned int gup_flags)
39 {
40 	struct mm_struct *mm;
41 	int ret;
42 
43 	mm = get_task_mm(tsk);
44 	if (!mm)
45 		return 0;
46 
47 	if (!tsk->ptrace ||
48 	    (current != tsk->parent) ||
49 	    ((get_dumpable(mm) != SUID_DUMP_USER) &&
50 	     !ptracer_capable(tsk, mm->user_ns))) {
51 		mmput(mm);
52 		return 0;
53 	}
54 
55 	ret = __access_remote_vm(tsk, mm, addr, buf, len, gup_flags);
56 	mmput(mm);
57 
58 	return ret;
59 }
60 
61 
62 /*
63  * ptrace a task: make the debugger its new parent and
64  * move it to the ptrace list.
65  *
66  * Must be called with the tasklist lock write-held.
67  */
68 void __ptrace_link(struct task_struct *child, struct task_struct *new_parent)
69 {
70 	BUG_ON(!list_empty(&child->ptrace_entry));
71 	list_add(&child->ptrace_entry, &new_parent->ptraced);
72 	child->parent = new_parent;
73 	rcu_read_lock();
74 	child->ptracer_cred = get_cred(__task_cred(new_parent));
75 	rcu_read_unlock();
76 }
77 
78 /**
79  * __ptrace_unlink - unlink ptracee and restore its execution state
80  * @child: ptracee to be unlinked
81  *
82  * Remove @child from the ptrace list, move it back to the original parent,
83  * and restore the execution state so that it conforms to the group stop
84  * state.
85  *
86  * Unlinking can happen via two paths - explicit PTRACE_DETACH or ptracer
87  * exiting.  For PTRACE_DETACH, unless the ptracee has been killed between
88  * ptrace_check_attach() and here, it's guaranteed to be in TASK_TRACED.
89  * If the ptracer is exiting, the ptracee can be in any state.
90  *
91  * After detach, the ptracee should be in a state which conforms to the
92  * group stop.  If the group is stopped or in the process of stopping, the
93  * ptracee should be put into TASK_STOPPED; otherwise, it should be woken
94  * up from TASK_TRACED.
95  *
96  * If the ptracee is in TASK_TRACED and needs to be moved to TASK_STOPPED,
97  * it goes through TRACED -> RUNNING -> STOPPED transition which is similar
98  * to but in the opposite direction of what happens while attaching to a
99  * stopped task.  However, in this direction, the intermediate RUNNING
100  * state is not hidden even from the current ptracer and if it immediately
101  * re-attaches and performs a WNOHANG wait(2), it may fail.
102  *
103  * CONTEXT:
104  * write_lock_irq(tasklist_lock)
105  */
106 void __ptrace_unlink(struct task_struct *child)
107 {
108 	const struct cred *old_cred;
109 	BUG_ON(!child->ptrace);
110 
111 	clear_tsk_thread_flag(child, TIF_SYSCALL_TRACE);
112 
113 	child->parent = child->real_parent;
114 	list_del_init(&child->ptrace_entry);
115 	old_cred = child->ptracer_cred;
116 	child->ptracer_cred = NULL;
117 	put_cred(old_cred);
118 
119 	spin_lock(&child->sighand->siglock);
120 	child->ptrace = 0;
121 	/*
122 	 * Clear all pending traps and TRAPPING.  TRAPPING should be
123 	 * cleared regardless of JOBCTL_STOP_PENDING.  Do it explicitly.
124 	 */
125 	task_clear_jobctl_pending(child, JOBCTL_TRAP_MASK);
126 	task_clear_jobctl_trapping(child);
127 
128 	/*
129 	 * Reinstate JOBCTL_STOP_PENDING if group stop is in effect and
130 	 * @child isn't dead.
131 	 */
132 	if (!(child->flags & PF_EXITING) &&
133 	    (child->signal->flags & SIGNAL_STOP_STOPPED ||
134 	     child->signal->group_stop_count)) {
135 		child->jobctl |= JOBCTL_STOP_PENDING;
136 
137 		/*
138 		 * This is only possible if this thread was cloned by the
139 		 * traced task running in the stopped group, set the signal
140 		 * for the future reports.
141 		 * FIXME: we should change ptrace_init_task() to handle this
142 		 * case.
143 		 */
144 		if (!(child->jobctl & JOBCTL_STOP_SIGMASK))
145 			child->jobctl |= SIGSTOP;
146 	}
147 
148 	/*
149 	 * If transition to TASK_STOPPED is pending or in TASK_TRACED, kick
150 	 * @child in the butt.  Note that @resume should be used iff @child
151 	 * is in TASK_TRACED; otherwise, we might unduly disrupt
152 	 * TASK_KILLABLE sleeps.
153 	 */
154 	if (child->jobctl & JOBCTL_STOP_PENDING || task_is_traced(child))
155 		ptrace_signal_wake_up(child, true);
156 
157 	spin_unlock(&child->sighand->siglock);
158 }
159 
160 /* Ensure that nothing can wake it up, even SIGKILL */
161 static bool ptrace_freeze_traced(struct task_struct *task)
162 {
163 	bool ret = false;
164 
165 	/* Lockless, nobody but us can set this flag */
166 	if (task->jobctl & JOBCTL_LISTENING)
167 		return ret;
168 
169 	spin_lock_irq(&task->sighand->siglock);
170 	if (task_is_traced(task) && !__fatal_signal_pending(task)) {
171 		task->state = __TASK_TRACED;
172 		ret = true;
173 	}
174 	spin_unlock_irq(&task->sighand->siglock);
175 
176 	return ret;
177 }
178 
179 static void ptrace_unfreeze_traced(struct task_struct *task)
180 {
181 	if (task->state != __TASK_TRACED)
182 		return;
183 
184 	WARN_ON(!task->ptrace || task->parent != current);
185 
186 	spin_lock_irq(&task->sighand->siglock);
187 	if (__fatal_signal_pending(task))
188 		wake_up_state(task, __TASK_TRACED);
189 	else
190 		task->state = TASK_TRACED;
191 	spin_unlock_irq(&task->sighand->siglock);
192 }
193 
194 /**
195  * ptrace_check_attach - check whether ptracee is ready for ptrace operation
196  * @child: ptracee to check for
197  * @ignore_state: don't check whether @child is currently %TASK_TRACED
198  *
199  * Check whether @child is being ptraced by %current and ready for further
200  * ptrace operations.  If @ignore_state is %false, @child also should be in
201  * %TASK_TRACED state and on return the child is guaranteed to be traced
202  * and not executing.  If @ignore_state is %true, @child can be in any
203  * state.
204  *
205  * CONTEXT:
206  * Grabs and releases tasklist_lock and @child->sighand->siglock.
207  *
208  * RETURNS:
209  * 0 on success, -ESRCH if %child is not ready.
210  */
211 static int ptrace_check_attach(struct task_struct *child, bool ignore_state)
212 {
213 	int ret = -ESRCH;
214 
215 	/*
216 	 * We take the read lock around doing both checks to close a
217 	 * possible race where someone else was tracing our child and
218 	 * detached between these two checks.  After this locked check,
219 	 * we are sure that this is our traced child and that can only
220 	 * be changed by us so it's not changing right after this.
221 	 */
222 	read_lock(&tasklist_lock);
223 	if (child->ptrace && child->parent == current) {
224 		WARN_ON(child->state == __TASK_TRACED);
225 		/*
226 		 * child->sighand can't be NULL, release_task()
227 		 * does ptrace_unlink() before __exit_signal().
228 		 */
229 		if (ignore_state || ptrace_freeze_traced(child))
230 			ret = 0;
231 	}
232 	read_unlock(&tasklist_lock);
233 
234 	if (!ret && !ignore_state) {
235 		if (!wait_task_inactive(child, __TASK_TRACED)) {
236 			/*
237 			 * This can only happen if may_ptrace_stop() fails and
238 			 * ptrace_stop() changes ->state back to TASK_RUNNING,
239 			 * so we should not worry about leaking __TASK_TRACED.
240 			 */
241 			WARN_ON(child->state == __TASK_TRACED);
242 			ret = -ESRCH;
243 		}
244 	}
245 
246 	return ret;
247 }
248 
249 static int ptrace_has_cap(struct user_namespace *ns, unsigned int mode)
250 {
251 	if (mode & PTRACE_MODE_NOAUDIT)
252 		return has_ns_capability_noaudit(current, ns, CAP_SYS_PTRACE);
253 	else
254 		return has_ns_capability(current, ns, CAP_SYS_PTRACE);
255 }
256 
257 /* Returns 0 on success, -errno on denial. */
258 static int __ptrace_may_access(struct task_struct *task, unsigned int mode)
259 {
260 	const struct cred *cred = current_cred(), *tcred;
261 	struct mm_struct *mm;
262 	kuid_t caller_uid;
263 	kgid_t caller_gid;
264 
265 	if (!(mode & PTRACE_MODE_FSCREDS) == !(mode & PTRACE_MODE_REALCREDS)) {
266 		WARN(1, "denying ptrace access check without PTRACE_MODE_*CREDS\n");
267 		return -EPERM;
268 	}
269 
270 	/* May we inspect the given task?
271 	 * This check is used both for attaching with ptrace
272 	 * and for allowing access to sensitive information in /proc.
273 	 *
274 	 * ptrace_attach denies several cases that /proc allows
275 	 * because setting up the necessary parent/child relationship
276 	 * or halting the specified task is impossible.
277 	 */
278 
279 	/* Don't let security modules deny introspection */
280 	if (same_thread_group(task, current))
281 		return 0;
282 	rcu_read_lock();
283 	if (mode & PTRACE_MODE_FSCREDS) {
284 		caller_uid = cred->fsuid;
285 		caller_gid = cred->fsgid;
286 	} else {
287 		/*
288 		 * Using the euid would make more sense here, but something
289 		 * in userland might rely on the old behavior, and this
290 		 * shouldn't be a security problem since
291 		 * PTRACE_MODE_REALCREDS implies that the caller explicitly
292 		 * used a syscall that requests access to another process
293 		 * (and not a filesystem syscall to procfs).
294 		 */
295 		caller_uid = cred->uid;
296 		caller_gid = cred->gid;
297 	}
298 	tcred = __task_cred(task);
299 	if (uid_eq(caller_uid, tcred->euid) &&
300 	    uid_eq(caller_uid, tcred->suid) &&
301 	    uid_eq(caller_uid, tcred->uid)  &&
302 	    gid_eq(caller_gid, tcred->egid) &&
303 	    gid_eq(caller_gid, tcred->sgid) &&
304 	    gid_eq(caller_gid, tcred->gid))
305 		goto ok;
306 	if (ptrace_has_cap(tcred->user_ns, mode))
307 		goto ok;
308 	rcu_read_unlock();
309 	return -EPERM;
310 ok:
311 	rcu_read_unlock();
312 	mm = task->mm;
313 	if (mm &&
314 	    ((get_dumpable(mm) != SUID_DUMP_USER) &&
315 	     !ptrace_has_cap(mm->user_ns, mode)))
316 	    return -EPERM;
317 
318 	return security_ptrace_access_check(task, mode);
319 }
320 
321 bool ptrace_may_access(struct task_struct *task, unsigned int mode)
322 {
323 	int err;
324 	task_lock(task);
325 	err = __ptrace_may_access(task, mode);
326 	task_unlock(task);
327 	return !err;
328 }
329 
330 static int ptrace_attach(struct task_struct *task, long request,
331 			 unsigned long addr,
332 			 unsigned long flags)
333 {
334 	bool seize = (request == PTRACE_SEIZE);
335 	int retval;
336 
337 	retval = -EIO;
338 	if (seize) {
339 		if (addr != 0)
340 			goto out;
341 		if (flags & ~(unsigned long)PTRACE_O_MASK)
342 			goto out;
343 		flags = PT_PTRACED | PT_SEIZED | (flags << PT_OPT_FLAG_SHIFT);
344 	} else {
345 		flags = PT_PTRACED;
346 	}
347 
348 	audit_ptrace(task);
349 
350 	retval = -EPERM;
351 	if (unlikely(task->flags & PF_KTHREAD))
352 		goto out;
353 	if (same_thread_group(task, current))
354 		goto out;
355 
356 	/*
357 	 * Protect exec's credential calculations against our interference;
358 	 * SUID, SGID and LSM creds get determined differently
359 	 * under ptrace.
360 	 */
361 	retval = -ERESTARTNOINTR;
362 	if (mutex_lock_interruptible(&task->signal->cred_guard_mutex))
363 		goto out;
364 
365 	task_lock(task);
366 	retval = __ptrace_may_access(task, PTRACE_MODE_ATTACH_REALCREDS);
367 	task_unlock(task);
368 	if (retval)
369 		goto unlock_creds;
370 
371 	write_lock_irq(&tasklist_lock);
372 	retval = -EPERM;
373 	if (unlikely(task->exit_state))
374 		goto unlock_tasklist;
375 	if (task->ptrace)
376 		goto unlock_tasklist;
377 
378 	if (seize)
379 		flags |= PT_SEIZED;
380 	task->ptrace = flags;
381 
382 	__ptrace_link(task, current);
383 
384 	/* SEIZE doesn't trap tracee on attach */
385 	if (!seize)
386 		send_sig_info(SIGSTOP, SEND_SIG_FORCED, task);
387 
388 	spin_lock(&task->sighand->siglock);
389 
390 	/*
391 	 * If the task is already STOPPED, set JOBCTL_TRAP_STOP and
392 	 * TRAPPING, and kick it so that it transits to TRACED.  TRAPPING
393 	 * will be cleared if the child completes the transition or any
394 	 * event which clears the group stop states happens.  We'll wait
395 	 * for the transition to complete before returning from this
396 	 * function.
397 	 *
398 	 * This hides STOPPED -> RUNNING -> TRACED transition from the
399 	 * attaching thread but a different thread in the same group can
400 	 * still observe the transient RUNNING state.  IOW, if another
401 	 * thread's WNOHANG wait(2) on the stopped tracee races against
402 	 * ATTACH, the wait(2) may fail due to the transient RUNNING.
403 	 *
404 	 * The following task_is_stopped() test is safe as both transitions
405 	 * in and out of STOPPED are protected by siglock.
406 	 */
407 	if (task_is_stopped(task) &&
408 	    task_set_jobctl_pending(task, JOBCTL_TRAP_STOP | JOBCTL_TRAPPING))
409 		signal_wake_up_state(task, __TASK_STOPPED);
410 
411 	spin_unlock(&task->sighand->siglock);
412 
413 	retval = 0;
414 unlock_tasklist:
415 	write_unlock_irq(&tasklist_lock);
416 unlock_creds:
417 	mutex_unlock(&task->signal->cred_guard_mutex);
418 out:
419 	if (!retval) {
420 		/*
421 		 * We do not bother to change retval or clear JOBCTL_TRAPPING
422 		 * if wait_on_bit() was interrupted by SIGKILL. The tracer will
423 		 * not return to user-mode, it will exit and clear this bit in
424 		 * __ptrace_unlink() if it wasn't already cleared by the tracee;
425 		 * and until then nobody can ptrace this task.
426 		 */
427 		wait_on_bit(&task->jobctl, JOBCTL_TRAPPING_BIT, TASK_KILLABLE);
428 		proc_ptrace_connector(task, PTRACE_ATTACH);
429 	}
430 
431 	return retval;
432 }
433 
434 /**
435  * ptrace_traceme  --  helper for PTRACE_TRACEME
436  *
437  * Performs checks and sets PT_PTRACED.
438  * Should be used by all ptrace implementations for PTRACE_TRACEME.
439  */
440 static int ptrace_traceme(void)
441 {
442 	int ret = -EPERM;
443 
444 	write_lock_irq(&tasklist_lock);
445 	/* Are we already being traced? */
446 	if (!current->ptrace) {
447 		ret = security_ptrace_traceme(current->parent);
448 		/*
449 		 * Check PF_EXITING to ensure ->real_parent has not passed
450 		 * exit_ptrace(). Otherwise we don't report the error but
451 		 * pretend ->real_parent untraces us right after return.
452 		 */
453 		if (!ret && !(current->real_parent->flags & PF_EXITING)) {
454 			current->ptrace = PT_PTRACED;
455 			__ptrace_link(current, current->real_parent);
456 		}
457 	}
458 	write_unlock_irq(&tasklist_lock);
459 
460 	return ret;
461 }
462 
463 /*
464  * Called with irqs disabled, returns true if childs should reap themselves.
465  */
466 static int ignoring_children(struct sighand_struct *sigh)
467 {
468 	int ret;
469 	spin_lock(&sigh->siglock);
470 	ret = (sigh->action[SIGCHLD-1].sa.sa_handler == SIG_IGN) ||
471 	      (sigh->action[SIGCHLD-1].sa.sa_flags & SA_NOCLDWAIT);
472 	spin_unlock(&sigh->siglock);
473 	return ret;
474 }
475 
476 /*
477  * Called with tasklist_lock held for writing.
478  * Unlink a traced task, and clean it up if it was a traced zombie.
479  * Return true if it needs to be reaped with release_task().
480  * (We can't call release_task() here because we already hold tasklist_lock.)
481  *
482  * If it's a zombie, our attachedness prevented normal parent notification
483  * or self-reaping.  Do notification now if it would have happened earlier.
484  * If it should reap itself, return true.
485  *
486  * If it's our own child, there is no notification to do. But if our normal
487  * children self-reap, then this child was prevented by ptrace and we must
488  * reap it now, in that case we must also wake up sub-threads sleeping in
489  * do_wait().
490  */
491 static bool __ptrace_detach(struct task_struct *tracer, struct task_struct *p)
492 {
493 	bool dead;
494 
495 	__ptrace_unlink(p);
496 
497 	if (p->exit_state != EXIT_ZOMBIE)
498 		return false;
499 
500 	dead = !thread_group_leader(p);
501 
502 	if (!dead && thread_group_empty(p)) {
503 		if (!same_thread_group(p->real_parent, tracer))
504 			dead = do_notify_parent(p, p->exit_signal);
505 		else if (ignoring_children(tracer->sighand)) {
506 			__wake_up_parent(p, tracer);
507 			dead = true;
508 		}
509 	}
510 	/* Mark it as in the process of being reaped. */
511 	if (dead)
512 		p->exit_state = EXIT_DEAD;
513 	return dead;
514 }
515 
516 static int ptrace_detach(struct task_struct *child, unsigned int data)
517 {
518 	if (!valid_signal(data))
519 		return -EIO;
520 
521 	/* Architecture-specific hardware disable .. */
522 	ptrace_disable(child);
523 
524 	write_lock_irq(&tasklist_lock);
525 	/*
526 	 * We rely on ptrace_freeze_traced(). It can't be killed and
527 	 * untraced by another thread, it can't be a zombie.
528 	 */
529 	WARN_ON(!child->ptrace || child->exit_state);
530 	/*
531 	 * tasklist_lock avoids the race with wait_task_stopped(), see
532 	 * the comment in ptrace_resume().
533 	 */
534 	child->exit_code = data;
535 	__ptrace_detach(current, child);
536 	write_unlock_irq(&tasklist_lock);
537 
538 	proc_ptrace_connector(child, PTRACE_DETACH);
539 
540 	return 0;
541 }
542 
543 /*
544  * Detach all tasks we were using ptrace on. Called with tasklist held
545  * for writing.
546  */
547 void exit_ptrace(struct task_struct *tracer, struct list_head *dead)
548 {
549 	struct task_struct *p, *n;
550 
551 	list_for_each_entry_safe(p, n, &tracer->ptraced, ptrace_entry) {
552 		if (unlikely(p->ptrace & PT_EXITKILL))
553 			send_sig_info(SIGKILL, SEND_SIG_FORCED, p);
554 
555 		if (__ptrace_detach(tracer, p))
556 			list_add(&p->ptrace_entry, dead);
557 	}
558 }
559 
560 int ptrace_readdata(struct task_struct *tsk, unsigned long src, char __user *dst, int len)
561 {
562 	int copied = 0;
563 
564 	while (len > 0) {
565 		char buf[128];
566 		int this_len, retval;
567 
568 		this_len = (len > sizeof(buf)) ? sizeof(buf) : len;
569 		retval = ptrace_access_vm(tsk, src, buf, this_len, FOLL_FORCE);
570 
571 		if (!retval) {
572 			if (copied)
573 				break;
574 			return -EIO;
575 		}
576 		if (copy_to_user(dst, buf, retval))
577 			return -EFAULT;
578 		copied += retval;
579 		src += retval;
580 		dst += retval;
581 		len -= retval;
582 	}
583 	return copied;
584 }
585 
586 int ptrace_writedata(struct task_struct *tsk, char __user *src, unsigned long dst, int len)
587 {
588 	int copied = 0;
589 
590 	while (len > 0) {
591 		char buf[128];
592 		int this_len, retval;
593 
594 		this_len = (len > sizeof(buf)) ? sizeof(buf) : len;
595 		if (copy_from_user(buf, src, this_len))
596 			return -EFAULT;
597 		retval = ptrace_access_vm(tsk, dst, buf, this_len,
598 				FOLL_FORCE | FOLL_WRITE);
599 		if (!retval) {
600 			if (copied)
601 				break;
602 			return -EIO;
603 		}
604 		copied += retval;
605 		src += retval;
606 		dst += retval;
607 		len -= retval;
608 	}
609 	return copied;
610 }
611 
612 static int ptrace_setoptions(struct task_struct *child, unsigned long data)
613 {
614 	unsigned flags;
615 
616 	if (data & ~(unsigned long)PTRACE_O_MASK)
617 		return -EINVAL;
618 
619 	if (unlikely(data & PTRACE_O_SUSPEND_SECCOMP)) {
620 		if (!IS_ENABLED(CONFIG_CHECKPOINT_RESTORE) ||
621 		    !IS_ENABLED(CONFIG_SECCOMP))
622 			return -EINVAL;
623 
624 		if (!capable(CAP_SYS_ADMIN))
625 			return -EPERM;
626 
627 		if (seccomp_mode(&current->seccomp) != SECCOMP_MODE_DISABLED ||
628 		    current->ptrace & PT_SUSPEND_SECCOMP)
629 			return -EPERM;
630 	}
631 
632 	/* Avoid intermediate state when all opts are cleared */
633 	flags = child->ptrace;
634 	flags &= ~(PTRACE_O_MASK << PT_OPT_FLAG_SHIFT);
635 	flags |= (data << PT_OPT_FLAG_SHIFT);
636 	child->ptrace = flags;
637 
638 	return 0;
639 }
640 
641 static int ptrace_getsiginfo(struct task_struct *child, siginfo_t *info)
642 {
643 	unsigned long flags;
644 	int error = -ESRCH;
645 
646 	if (lock_task_sighand(child, &flags)) {
647 		error = -EINVAL;
648 		if (likely(child->last_siginfo != NULL)) {
649 			*info = *child->last_siginfo;
650 			error = 0;
651 		}
652 		unlock_task_sighand(child, &flags);
653 	}
654 	return error;
655 }
656 
657 static int ptrace_setsiginfo(struct task_struct *child, const siginfo_t *info)
658 {
659 	unsigned long flags;
660 	int error = -ESRCH;
661 
662 	if (lock_task_sighand(child, &flags)) {
663 		error = -EINVAL;
664 		if (likely(child->last_siginfo != NULL)) {
665 			*child->last_siginfo = *info;
666 			error = 0;
667 		}
668 		unlock_task_sighand(child, &flags);
669 	}
670 	return error;
671 }
672 
673 static int ptrace_peek_siginfo(struct task_struct *child,
674 				unsigned long addr,
675 				unsigned long data)
676 {
677 	struct ptrace_peeksiginfo_args arg;
678 	struct sigpending *pending;
679 	struct sigqueue *q;
680 	int ret, i;
681 
682 	ret = copy_from_user(&arg, (void __user *) addr,
683 				sizeof(struct ptrace_peeksiginfo_args));
684 	if (ret)
685 		return -EFAULT;
686 
687 	if (arg.flags & ~PTRACE_PEEKSIGINFO_SHARED)
688 		return -EINVAL; /* unknown flags */
689 
690 	if (arg.nr < 0)
691 		return -EINVAL;
692 
693 	if (arg.flags & PTRACE_PEEKSIGINFO_SHARED)
694 		pending = &child->signal->shared_pending;
695 	else
696 		pending = &child->pending;
697 
698 	for (i = 0; i < arg.nr; ) {
699 		siginfo_t info;
700 		s32 off = arg.off + i;
701 
702 		spin_lock_irq(&child->sighand->siglock);
703 		list_for_each_entry(q, &pending->list, list) {
704 			if (!off--) {
705 				copy_siginfo(&info, &q->info);
706 				break;
707 			}
708 		}
709 		spin_unlock_irq(&child->sighand->siglock);
710 
711 		if (off >= 0) /* beyond the end of the list */
712 			break;
713 
714 #ifdef CONFIG_COMPAT
715 		if (unlikely(in_compat_syscall())) {
716 			compat_siginfo_t __user *uinfo = compat_ptr(data);
717 
718 			if (copy_siginfo_to_user32(uinfo, &info) ||
719 			    __put_user(info.si_code, &uinfo->si_code)) {
720 				ret = -EFAULT;
721 				break;
722 			}
723 
724 		} else
725 #endif
726 		{
727 			siginfo_t __user *uinfo = (siginfo_t __user *) data;
728 
729 			if (copy_siginfo_to_user(uinfo, &info) ||
730 			    __put_user(info.si_code, &uinfo->si_code)) {
731 				ret = -EFAULT;
732 				break;
733 			}
734 		}
735 
736 		data += sizeof(siginfo_t);
737 		i++;
738 
739 		if (signal_pending(current))
740 			break;
741 
742 		cond_resched();
743 	}
744 
745 	if (i > 0)
746 		return i;
747 
748 	return ret;
749 }
750 
751 #ifdef PTRACE_SINGLESTEP
752 #define is_singlestep(request)		((request) == PTRACE_SINGLESTEP)
753 #else
754 #define is_singlestep(request)		0
755 #endif
756 
757 #ifdef PTRACE_SINGLEBLOCK
758 #define is_singleblock(request)		((request) == PTRACE_SINGLEBLOCK)
759 #else
760 #define is_singleblock(request)		0
761 #endif
762 
763 #ifdef PTRACE_SYSEMU
764 #define is_sysemu_singlestep(request)	((request) == PTRACE_SYSEMU_SINGLESTEP)
765 #else
766 #define is_sysemu_singlestep(request)	0
767 #endif
768 
769 static int ptrace_resume(struct task_struct *child, long request,
770 			 unsigned long data)
771 {
772 	bool need_siglock;
773 
774 	if (!valid_signal(data))
775 		return -EIO;
776 
777 	if (request == PTRACE_SYSCALL)
778 		set_tsk_thread_flag(child, TIF_SYSCALL_TRACE);
779 	else
780 		clear_tsk_thread_flag(child, TIF_SYSCALL_TRACE);
781 
782 #ifdef TIF_SYSCALL_EMU
783 	if (request == PTRACE_SYSEMU || request == PTRACE_SYSEMU_SINGLESTEP)
784 		set_tsk_thread_flag(child, TIF_SYSCALL_EMU);
785 	else
786 		clear_tsk_thread_flag(child, TIF_SYSCALL_EMU);
787 #endif
788 
789 	if (is_singleblock(request)) {
790 		if (unlikely(!arch_has_block_step()))
791 			return -EIO;
792 		user_enable_block_step(child);
793 	} else if (is_singlestep(request) || is_sysemu_singlestep(request)) {
794 		if (unlikely(!arch_has_single_step()))
795 			return -EIO;
796 		user_enable_single_step(child);
797 	} else {
798 		user_disable_single_step(child);
799 	}
800 
801 	/*
802 	 * Change ->exit_code and ->state under siglock to avoid the race
803 	 * with wait_task_stopped() in between; a non-zero ->exit_code will
804 	 * wrongly look like another report from tracee.
805 	 *
806 	 * Note that we need siglock even if ->exit_code == data and/or this
807 	 * status was not reported yet, the new status must not be cleared by
808 	 * wait_task_stopped() after resume.
809 	 *
810 	 * If data == 0 we do not care if wait_task_stopped() reports the old
811 	 * status and clears the code too; this can't race with the tracee, it
812 	 * takes siglock after resume.
813 	 */
814 	need_siglock = data && !thread_group_empty(current);
815 	if (need_siglock)
816 		spin_lock_irq(&child->sighand->siglock);
817 	child->exit_code = data;
818 	wake_up_state(child, __TASK_TRACED);
819 	if (need_siglock)
820 		spin_unlock_irq(&child->sighand->siglock);
821 
822 	return 0;
823 }
824 
825 #ifdef CONFIG_HAVE_ARCH_TRACEHOOK
826 
827 static const struct user_regset *
828 find_regset(const struct user_regset_view *view, unsigned int type)
829 {
830 	const struct user_regset *regset;
831 	int n;
832 
833 	for (n = 0; n < view->n; ++n) {
834 		regset = view->regsets + n;
835 		if (regset->core_note_type == type)
836 			return regset;
837 	}
838 
839 	return NULL;
840 }
841 
842 static int ptrace_regset(struct task_struct *task, int req, unsigned int type,
843 			 struct iovec *kiov)
844 {
845 	const struct user_regset_view *view = task_user_regset_view(task);
846 	const struct user_regset *regset = find_regset(view, type);
847 	int regset_no;
848 
849 	if (!regset || (kiov->iov_len % regset->size) != 0)
850 		return -EINVAL;
851 
852 	regset_no = regset - view->regsets;
853 	kiov->iov_len = min(kiov->iov_len,
854 			    (__kernel_size_t) (regset->n * regset->size));
855 
856 	if (req == PTRACE_GETREGSET)
857 		return copy_regset_to_user(task, view, regset_no, 0,
858 					   kiov->iov_len, kiov->iov_base);
859 	else
860 		return copy_regset_from_user(task, view, regset_no, 0,
861 					     kiov->iov_len, kiov->iov_base);
862 }
863 
864 /*
865  * This is declared in linux/regset.h and defined in machine-dependent
866  * code.  We put the export here, near the primary machine-neutral use,
867  * to ensure no machine forgets it.
868  */
869 EXPORT_SYMBOL_GPL(task_user_regset_view);
870 #endif
871 
872 int ptrace_request(struct task_struct *child, long request,
873 		   unsigned long addr, unsigned long data)
874 {
875 	bool seized = child->ptrace & PT_SEIZED;
876 	int ret = -EIO;
877 	siginfo_t siginfo, *si;
878 	void __user *datavp = (void __user *) data;
879 	unsigned long __user *datalp = datavp;
880 	unsigned long flags;
881 
882 	switch (request) {
883 	case PTRACE_PEEKTEXT:
884 	case PTRACE_PEEKDATA:
885 		return generic_ptrace_peekdata(child, addr, data);
886 	case PTRACE_POKETEXT:
887 	case PTRACE_POKEDATA:
888 		return generic_ptrace_pokedata(child, addr, data);
889 
890 #ifdef PTRACE_OLDSETOPTIONS
891 	case PTRACE_OLDSETOPTIONS:
892 #endif
893 	case PTRACE_SETOPTIONS:
894 		ret = ptrace_setoptions(child, data);
895 		break;
896 	case PTRACE_GETEVENTMSG:
897 		ret = put_user(child->ptrace_message, datalp);
898 		break;
899 
900 	case PTRACE_PEEKSIGINFO:
901 		ret = ptrace_peek_siginfo(child, addr, data);
902 		break;
903 
904 	case PTRACE_GETSIGINFO:
905 		ret = ptrace_getsiginfo(child, &siginfo);
906 		if (!ret)
907 			ret = copy_siginfo_to_user(datavp, &siginfo);
908 		break;
909 
910 	case PTRACE_SETSIGINFO:
911 		if (copy_from_user(&siginfo, datavp, sizeof siginfo))
912 			ret = -EFAULT;
913 		else
914 			ret = ptrace_setsiginfo(child, &siginfo);
915 		break;
916 
917 	case PTRACE_GETSIGMASK:
918 		if (addr != sizeof(sigset_t)) {
919 			ret = -EINVAL;
920 			break;
921 		}
922 
923 		if (copy_to_user(datavp, &child->blocked, sizeof(sigset_t)))
924 			ret = -EFAULT;
925 		else
926 			ret = 0;
927 
928 		break;
929 
930 	case PTRACE_SETSIGMASK: {
931 		sigset_t new_set;
932 
933 		if (addr != sizeof(sigset_t)) {
934 			ret = -EINVAL;
935 			break;
936 		}
937 
938 		if (copy_from_user(&new_set, datavp, sizeof(sigset_t))) {
939 			ret = -EFAULT;
940 			break;
941 		}
942 
943 		sigdelsetmask(&new_set, sigmask(SIGKILL)|sigmask(SIGSTOP));
944 
945 		/*
946 		 * Every thread does recalc_sigpending() after resume, so
947 		 * retarget_shared_pending() and recalc_sigpending() are not
948 		 * called here.
949 		 */
950 		spin_lock_irq(&child->sighand->siglock);
951 		child->blocked = new_set;
952 		spin_unlock_irq(&child->sighand->siglock);
953 
954 		ret = 0;
955 		break;
956 	}
957 
958 	case PTRACE_INTERRUPT:
959 		/*
960 		 * Stop tracee without any side-effect on signal or job
961 		 * control.  At least one trap is guaranteed to happen
962 		 * after this request.  If @child is already trapped, the
963 		 * current trap is not disturbed and another trap will
964 		 * happen after the current trap is ended with PTRACE_CONT.
965 		 *
966 		 * The actual trap might not be PTRACE_EVENT_STOP trap but
967 		 * the pending condition is cleared regardless.
968 		 */
969 		if (unlikely(!seized || !lock_task_sighand(child, &flags)))
970 			break;
971 
972 		/*
973 		 * INTERRUPT doesn't disturb existing trap sans one
974 		 * exception.  If ptracer issued LISTEN for the current
975 		 * STOP, this INTERRUPT should clear LISTEN and re-trap
976 		 * tracee into STOP.
977 		 */
978 		if (likely(task_set_jobctl_pending(child, JOBCTL_TRAP_STOP)))
979 			ptrace_signal_wake_up(child, child->jobctl & JOBCTL_LISTENING);
980 
981 		unlock_task_sighand(child, &flags);
982 		ret = 0;
983 		break;
984 
985 	case PTRACE_LISTEN:
986 		/*
987 		 * Listen for events.  Tracee must be in STOP.  It's not
988 		 * resumed per-se but is not considered to be in TRACED by
989 		 * wait(2) or ptrace(2).  If an async event (e.g. group
990 		 * stop state change) happens, tracee will enter STOP trap
991 		 * again.  Alternatively, ptracer can issue INTERRUPT to
992 		 * finish listening and re-trap tracee into STOP.
993 		 */
994 		if (unlikely(!seized || !lock_task_sighand(child, &flags)))
995 			break;
996 
997 		si = child->last_siginfo;
998 		if (likely(si && (si->si_code >> 8) == PTRACE_EVENT_STOP)) {
999 			child->jobctl |= JOBCTL_LISTENING;
1000 			/*
1001 			 * If NOTIFY is set, it means event happened between
1002 			 * start of this trap and now.  Trigger re-trap.
1003 			 */
1004 			if (child->jobctl & JOBCTL_TRAP_NOTIFY)
1005 				ptrace_signal_wake_up(child, true);
1006 			ret = 0;
1007 		}
1008 		unlock_task_sighand(child, &flags);
1009 		break;
1010 
1011 	case PTRACE_DETACH:	 /* detach a process that was attached. */
1012 		ret = ptrace_detach(child, data);
1013 		break;
1014 
1015 #ifdef CONFIG_BINFMT_ELF_FDPIC
1016 	case PTRACE_GETFDPIC: {
1017 		struct mm_struct *mm = get_task_mm(child);
1018 		unsigned long tmp = 0;
1019 
1020 		ret = -ESRCH;
1021 		if (!mm)
1022 			break;
1023 
1024 		switch (addr) {
1025 		case PTRACE_GETFDPIC_EXEC:
1026 			tmp = mm->context.exec_fdpic_loadmap;
1027 			break;
1028 		case PTRACE_GETFDPIC_INTERP:
1029 			tmp = mm->context.interp_fdpic_loadmap;
1030 			break;
1031 		default:
1032 			break;
1033 		}
1034 		mmput(mm);
1035 
1036 		ret = put_user(tmp, datalp);
1037 		break;
1038 	}
1039 #endif
1040 
1041 #ifdef PTRACE_SINGLESTEP
1042 	case PTRACE_SINGLESTEP:
1043 #endif
1044 #ifdef PTRACE_SINGLEBLOCK
1045 	case PTRACE_SINGLEBLOCK:
1046 #endif
1047 #ifdef PTRACE_SYSEMU
1048 	case PTRACE_SYSEMU:
1049 	case PTRACE_SYSEMU_SINGLESTEP:
1050 #endif
1051 	case PTRACE_SYSCALL:
1052 	case PTRACE_CONT:
1053 		return ptrace_resume(child, request, data);
1054 
1055 	case PTRACE_KILL:
1056 		if (child->exit_state)	/* already dead */
1057 			return 0;
1058 		return ptrace_resume(child, request, SIGKILL);
1059 
1060 #ifdef CONFIG_HAVE_ARCH_TRACEHOOK
1061 	case PTRACE_GETREGSET:
1062 	case PTRACE_SETREGSET: {
1063 		struct iovec kiov;
1064 		struct iovec __user *uiov = datavp;
1065 
1066 		if (!access_ok(VERIFY_WRITE, uiov, sizeof(*uiov)))
1067 			return -EFAULT;
1068 
1069 		if (__get_user(kiov.iov_base, &uiov->iov_base) ||
1070 		    __get_user(kiov.iov_len, &uiov->iov_len))
1071 			return -EFAULT;
1072 
1073 		ret = ptrace_regset(child, request, addr, &kiov);
1074 		if (!ret)
1075 			ret = __put_user(kiov.iov_len, &uiov->iov_len);
1076 		break;
1077 	}
1078 #endif
1079 
1080 	case PTRACE_SECCOMP_GET_FILTER:
1081 		ret = seccomp_get_filter(child, addr, datavp);
1082 		break;
1083 
1084 	default:
1085 		break;
1086 	}
1087 
1088 	return ret;
1089 }
1090 
1091 static struct task_struct *ptrace_get_task_struct(pid_t pid)
1092 {
1093 	struct task_struct *child;
1094 
1095 	rcu_read_lock();
1096 	child = find_task_by_vpid(pid);
1097 	if (child)
1098 		get_task_struct(child);
1099 	rcu_read_unlock();
1100 
1101 	if (!child)
1102 		return ERR_PTR(-ESRCH);
1103 	return child;
1104 }
1105 
1106 #ifndef arch_ptrace_attach
1107 #define arch_ptrace_attach(child)	do { } while (0)
1108 #endif
1109 
1110 SYSCALL_DEFINE4(ptrace, long, request, long, pid, unsigned long, addr,
1111 		unsigned long, data)
1112 {
1113 	struct task_struct *child;
1114 	long ret;
1115 
1116 	if (request == PTRACE_TRACEME) {
1117 		ret = ptrace_traceme();
1118 		if (!ret)
1119 			arch_ptrace_attach(current);
1120 		goto out;
1121 	}
1122 
1123 	child = ptrace_get_task_struct(pid);
1124 	if (IS_ERR(child)) {
1125 		ret = PTR_ERR(child);
1126 		goto out;
1127 	}
1128 
1129 	if (request == PTRACE_ATTACH || request == PTRACE_SEIZE) {
1130 		ret = ptrace_attach(child, request, addr, data);
1131 		/*
1132 		 * Some architectures need to do book-keeping after
1133 		 * a ptrace attach.
1134 		 */
1135 		if (!ret)
1136 			arch_ptrace_attach(child);
1137 		goto out_put_task_struct;
1138 	}
1139 
1140 	ret = ptrace_check_attach(child, request == PTRACE_KILL ||
1141 				  request == PTRACE_INTERRUPT);
1142 	if (ret < 0)
1143 		goto out_put_task_struct;
1144 
1145 	ret = arch_ptrace(child, request, addr, data);
1146 	if (ret || request != PTRACE_DETACH)
1147 		ptrace_unfreeze_traced(child);
1148 
1149  out_put_task_struct:
1150 	put_task_struct(child);
1151  out:
1152 	return ret;
1153 }
1154 
1155 int generic_ptrace_peekdata(struct task_struct *tsk, unsigned long addr,
1156 			    unsigned long data)
1157 {
1158 	unsigned long tmp;
1159 	int copied;
1160 
1161 	copied = ptrace_access_vm(tsk, addr, &tmp, sizeof(tmp), FOLL_FORCE);
1162 	if (copied != sizeof(tmp))
1163 		return -EIO;
1164 	return put_user(tmp, (unsigned long __user *)data);
1165 }
1166 
1167 int generic_ptrace_pokedata(struct task_struct *tsk, unsigned long addr,
1168 			    unsigned long data)
1169 {
1170 	int copied;
1171 
1172 	copied = ptrace_access_vm(tsk, addr, &data, sizeof(data),
1173 			FOLL_FORCE | FOLL_WRITE);
1174 	return (copied == sizeof(data)) ? 0 : -EIO;
1175 }
1176 
1177 #if defined CONFIG_COMPAT
1178 
1179 int compat_ptrace_request(struct task_struct *child, compat_long_t request,
1180 			  compat_ulong_t addr, compat_ulong_t data)
1181 {
1182 	compat_ulong_t __user *datap = compat_ptr(data);
1183 	compat_ulong_t word;
1184 	siginfo_t siginfo;
1185 	int ret;
1186 
1187 	switch (request) {
1188 	case PTRACE_PEEKTEXT:
1189 	case PTRACE_PEEKDATA:
1190 		ret = ptrace_access_vm(child, addr, &word, sizeof(word),
1191 				FOLL_FORCE);
1192 		if (ret != sizeof(word))
1193 			ret = -EIO;
1194 		else
1195 			ret = put_user(word, datap);
1196 		break;
1197 
1198 	case PTRACE_POKETEXT:
1199 	case PTRACE_POKEDATA:
1200 		ret = ptrace_access_vm(child, addr, &data, sizeof(data),
1201 				FOLL_FORCE | FOLL_WRITE);
1202 		ret = (ret != sizeof(data) ? -EIO : 0);
1203 		break;
1204 
1205 	case PTRACE_GETEVENTMSG:
1206 		ret = put_user((compat_ulong_t) child->ptrace_message, datap);
1207 		break;
1208 
1209 	case PTRACE_GETSIGINFO:
1210 		ret = ptrace_getsiginfo(child, &siginfo);
1211 		if (!ret)
1212 			ret = copy_siginfo_to_user32(
1213 				(struct compat_siginfo __user *) datap,
1214 				&siginfo);
1215 		break;
1216 
1217 	case PTRACE_SETSIGINFO:
1218 		memset(&siginfo, 0, sizeof siginfo);
1219 		if (copy_siginfo_from_user32(
1220 			    &siginfo, (struct compat_siginfo __user *) datap))
1221 			ret = -EFAULT;
1222 		else
1223 			ret = ptrace_setsiginfo(child, &siginfo);
1224 		break;
1225 #ifdef CONFIG_HAVE_ARCH_TRACEHOOK
1226 	case PTRACE_GETREGSET:
1227 	case PTRACE_SETREGSET:
1228 	{
1229 		struct iovec kiov;
1230 		struct compat_iovec __user *uiov =
1231 			(struct compat_iovec __user *) datap;
1232 		compat_uptr_t ptr;
1233 		compat_size_t len;
1234 
1235 		if (!access_ok(VERIFY_WRITE, uiov, sizeof(*uiov)))
1236 			return -EFAULT;
1237 
1238 		if (__get_user(ptr, &uiov->iov_base) ||
1239 		    __get_user(len, &uiov->iov_len))
1240 			return -EFAULT;
1241 
1242 		kiov.iov_base = compat_ptr(ptr);
1243 		kiov.iov_len = len;
1244 
1245 		ret = ptrace_regset(child, request, addr, &kiov);
1246 		if (!ret)
1247 			ret = __put_user(kiov.iov_len, &uiov->iov_len);
1248 		break;
1249 	}
1250 #endif
1251 
1252 	default:
1253 		ret = ptrace_request(child, request, addr, data);
1254 	}
1255 
1256 	return ret;
1257 }
1258 
1259 COMPAT_SYSCALL_DEFINE4(ptrace, compat_long_t, request, compat_long_t, pid,
1260 		       compat_long_t, addr, compat_long_t, data)
1261 {
1262 	struct task_struct *child;
1263 	long ret;
1264 
1265 	if (request == PTRACE_TRACEME) {
1266 		ret = ptrace_traceme();
1267 		goto out;
1268 	}
1269 
1270 	child = ptrace_get_task_struct(pid);
1271 	if (IS_ERR(child)) {
1272 		ret = PTR_ERR(child);
1273 		goto out;
1274 	}
1275 
1276 	if (request == PTRACE_ATTACH || request == PTRACE_SEIZE) {
1277 		ret = ptrace_attach(child, request, addr, data);
1278 		/*
1279 		 * Some architectures need to do book-keeping after
1280 		 * a ptrace attach.
1281 		 */
1282 		if (!ret)
1283 			arch_ptrace_attach(child);
1284 		goto out_put_task_struct;
1285 	}
1286 
1287 	ret = ptrace_check_attach(child, request == PTRACE_KILL ||
1288 				  request == PTRACE_INTERRUPT);
1289 	if (!ret) {
1290 		ret = compat_arch_ptrace(child, request, addr, data);
1291 		if (ret || request != PTRACE_DETACH)
1292 			ptrace_unfreeze_traced(child);
1293 	}
1294 
1295  out_put_task_struct:
1296 	put_task_struct(child);
1297  out:
1298 	return ret;
1299 }
1300 #endif	/* CONFIG_COMPAT */
1301