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