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