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