blob: 8b06d4f2b814bd98a39ed407d0819739b830c31e [file] [log] [blame]
Roland McGrath88ac2922008-07-25 19:45:43 -07001/*
2 * Tracing hooks
3 *
Roland McGrathae6d2ed2009-09-23 15:56:53 -07004 * Copyright (C) 2008-2009 Red Hat, Inc. All rights reserved.
Roland McGrath88ac2922008-07-25 19:45:43 -07005 *
6 * This copyrighted material is made available to anyone wishing to use,
7 * modify, copy, or redistribute it subject to the terms and conditions
8 * of the GNU General Public License v.2.
9 *
10 * This file defines hook entry points called by core code where
11 * user tracing/debugging support might need to do something. These
12 * entry points are called tracehook_*(). Each hook declared below
13 * has a detailed kerneldoc comment giving the context (locking et
14 * al) from which it is called, and the meaning of its return value.
15 *
16 * Each function here typically has only one call site, so it is ok
17 * to have some nontrivial tracehook_*() inlines. In all cases, the
18 * fast path when no tracing is enabled should be very short.
19 *
20 * The purpose of this file and the tracehook_* layer is to consolidate
21 * the interface that the kernel core and arch code uses to enable any
22 * user debugging or tracing facility (such as ptrace). The interfaces
23 * here are carefully documented so that maintainers of core and arch
24 * code do not need to think about the implementation details of the
25 * tracing facilities. Likewise, maintainers of the tracing code do not
26 * need to understand all the calling core or arch code in detail, just
27 * documented circumstances of each call, such as locking conditions.
28 *
29 * If the calling core code changes so that locking is different, then
30 * it is ok to change the interface documented here. The maintainer of
31 * core code changing should notify the maintainers of the tracing code
32 * that they need to work out the change.
33 *
34 * Some tracehook_*() inlines take arguments that the current tracing
35 * implementations might not necessarily use. These function signatures
36 * are chosen to pass in all the information that is on hand in the
37 * caller and might conceivably be relevant to a tracer, so that the
38 * core code won't have to be updated when tracing adds more features.
39 * If a call site changes so that some of those parameters are no longer
40 * already on hand without extra work, then the tracehook_* interface
41 * can change so there is no make-work burden on the core code. The
42 * maintainer of core code changing should notify the maintainers of the
43 * tracing code that they need to work out the change.
44 */
45
46#ifndef _LINUX_TRACEHOOK_H
47#define _LINUX_TRACEHOOK_H 1
48
49#include <linux/sched.h>
50#include <linux/ptrace.h>
Roland McGrath6341c392008-07-25 19:45:44 -070051#include <linux/security.h>
52struct linux_binprm;
53
Roland McGrath283d7552008-07-25 19:45:52 -070054/*
55 * ptrace report for syscall entry and exit looks identical.
56 */
57static inline void ptrace_report_syscall(struct pt_regs *regs)
58{
Tejun Heod21142e2011-06-17 16:50:34 +020059 int ptrace = current->ptrace;
Roland McGrath283d7552008-07-25 19:45:52 -070060
61 if (!(ptrace & PT_PTRACED))
62 return;
63
64 ptrace_notify(SIGTRAP | ((ptrace & PT_TRACESYSGOOD) ? 0x80 : 0));
65
66 /*
67 * this isn't the same as continuing with a signal, but it will do
68 * for normal use. strace only continues with a signal if the
69 * stopping signal is not SIGTRAP. -brl
70 */
71 if (current->exit_code) {
72 send_sig(current->exit_code, current, 1);
73 current->exit_code = 0;
74 }
75}
76
77/**
78 * tracehook_report_syscall_entry - task is about to attempt a system call
79 * @regs: user register state of current task
80 *
81 * This will be called if %TIF_SYSCALL_TRACE has been set, when the
82 * current task has just entered the kernel for a system call.
83 * Full user register state is available here. Changing the values
84 * in @regs can affect the system call number and arguments to be tried.
85 * It is safe to block here, preventing the system call from beginning.
86 *
87 * Returns zero normally, or nonzero if the calling arch code should abort
88 * the system call. That must prevent normal entry so no system call is
89 * made. If @task ever returns to user mode after this, its register state
90 * is unspecified, but should be something harmless like an %ENOSYS error
Roland McGrath828c3652008-07-25 19:45:57 -070091 * return. It should preserve enough information so that syscall_rollback()
92 * can work (see asm-generic/syscall.h).
Roland McGrath283d7552008-07-25 19:45:52 -070093 *
94 * Called without locks, just after entering kernel mode.
95 */
96static inline __must_check int tracehook_report_syscall_entry(
97 struct pt_regs *regs)
98{
99 ptrace_report_syscall(regs);
100 return 0;
101}
102
103/**
104 * tracehook_report_syscall_exit - task has just finished a system call
105 * @regs: user register state of current task
106 * @step: nonzero if simulating single-step or block-step
107 *
108 * This will be called if %TIF_SYSCALL_TRACE has been set, when the
109 * current task has just finished an attempted system call. Full
110 * user register state is available here. It is safe to block here,
111 * preventing signals from being processed.
112 *
113 * If @step is nonzero, this report is also in lieu of the normal
114 * trap that would follow the system call instruction because
115 * user_enable_block_step() or user_enable_single_step() was used.
116 * In this case, %TIF_SYSCALL_TRACE might not be set.
117 *
118 * Called without locks, just before checking for pending signals.
119 */
120static inline void tracehook_report_syscall_exit(struct pt_regs *regs, int step)
121{
Oleg Nesterov2f0edac2009-12-15 16:47:19 -0800122 if (step) {
123 siginfo_t info;
124 user_single_step_siginfo(current, regs, &info);
125 force_sig_info(SIGTRAP, &info, current);
126 return;
127 }
128
Roland McGrath283d7552008-07-25 19:45:52 -0700129 ptrace_report_syscall(regs);
130}
131
Roland McGrathfa8e26c2008-07-25 19:45:50 -0700132/**
Roland McGrath6341c392008-07-25 19:45:44 -0700133 * tracehook_unsafe_exec - check for exec declared unsafe due to tracing
134 * @task: current task doing exec
135 *
136 * Return %LSM_UNSAFE_* bits applied to an exec because of tracing.
137 *
KOSAKI Motohiro9b1bf122010-10-27 15:34:08 -0700138 * @task->signal->cred_guard_mutex is held by the caller through the do_execve().
Roland McGrath6341c392008-07-25 19:45:44 -0700139 */
140static inline int tracehook_unsafe_exec(struct task_struct *task)
141{
142 int unsafe = 0;
Tejun Heod21142e2011-06-17 16:50:34 +0200143 int ptrace = task->ptrace;
Roland McGrath6341c392008-07-25 19:45:44 -0700144 if (ptrace & PT_PTRACED) {
145 if (ptrace & PT_PTRACE_CAP)
146 unsafe |= LSM_UNSAFE_PTRACE_CAP;
147 else
148 unsafe |= LSM_UNSAFE_PTRACE;
149 }
150 return unsafe;
151}
152
153/**
Roland McGrath0d094ef2008-07-25 19:45:49 -0700154 * tracehook_tracer_task - return the task that is tracing the given task
155 * @tsk: task to consider
156 *
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300157 * Returns NULL if no one is tracing @task, or the &struct task_struct
Roland McGrath0d094ef2008-07-25 19:45:49 -0700158 * pointer to its tracer.
159 *
160 * Must called under rcu_read_lock(). The pointer returned might be kept
161 * live only by RCU. During exec, this may be called with task_lock()
162 * held on @task, still held from when tracehook_unsafe_exec() was called.
163 */
164static inline struct task_struct *tracehook_tracer_task(struct task_struct *tsk)
165{
Tejun Heod21142e2011-06-17 16:50:34 +0200166 if (tsk->ptrace & PT_PTRACED)
Roland McGrath0d094ef2008-07-25 19:45:49 -0700167 return rcu_dereference(tsk->parent);
168 return NULL;
169}
170
171/**
Roland McGrath09a05392008-07-25 19:45:47 -0700172 * tracehook_prepare_clone - prepare for new child to be cloned
173 * @clone_flags: %CLONE_* flags from clone/fork/vfork system call
174 *
175 * This is called before a new user task is to be cloned.
176 * Its return value will be passed to tracehook_finish_clone().
177 *
178 * Called with no locks held.
179 */
180static inline int tracehook_prepare_clone(unsigned clone_flags)
181{
Tejun Heo643ad832011-06-17 16:50:35 +0200182 int event = 0;
183
Roland McGrath09a05392008-07-25 19:45:47 -0700184 if (clone_flags & CLONE_UNTRACED)
185 return 0;
186
Tejun Heo643ad832011-06-17 16:50:35 +0200187 if (clone_flags & CLONE_VFORK)
188 event = PTRACE_EVENT_VFORK;
189 else if ((clone_flags & CSIGNAL) != SIGCHLD)
190 event = PTRACE_EVENT_CLONE;
191 else
192 event = PTRACE_EVENT_FORK;
Roland McGrath09a05392008-07-25 19:45:47 -0700193
Tejun Heo643ad832011-06-17 16:50:35 +0200194 return ptrace_event_enabled(current, event) ? event : 0;
Roland McGrath09a05392008-07-25 19:45:47 -0700195}
196
197/**
198 * tracehook_finish_clone - new child created and being attached
199 * @child: new child task
200 * @clone_flags: %CLONE_* flags from clone/fork/vfork system call
Roland McGratha9906a12008-07-26 14:41:26 -0700201 * @trace: return value from tracehook_prepare_clone()
Roland McGrath09a05392008-07-25 19:45:47 -0700202 *
203 * This is called immediately after adding @child to its parent's children list.
204 * The @trace value is that returned by tracehook_prepare_clone().
205 *
206 * Called with current's siglock and write_lock_irq(&tasklist_lock) held.
207 */
208static inline void tracehook_finish_clone(struct task_struct *child,
209 unsigned long clone_flags, int trace)
210{
211 ptrace_init_task(child, (clone_flags & CLONE_PTRACE) || trace);
212}
213
214/**
215 * tracehook_report_clone - in parent, new child is about to start running
Roland McGrath09a05392008-07-25 19:45:47 -0700216 * @regs: parent's user register state
217 * @clone_flags: flags from parent's system call
218 * @pid: new child's PID in the parent's namespace
219 * @child: new child task
220 *
Oleg Nesterov087eb432009-06-04 16:29:07 -0700221 * Called after a child is set up, but before it has been started running.
Roland McGratha9906a12008-07-26 14:41:26 -0700222 * This is not a good place to block, because the child has not started
223 * yet. Suspend the child here if desired, and then block in
224 * tracehook_report_clone_complete(). This must prevent the child from
225 * self-reaping if tracehook_report_clone_complete() uses the @child
226 * pointer; otherwise it might have died and been released by the time
Roland McGrath22f30162008-09-05 14:00:23 -0700227 * tracehook_report_clone_complete() is called.
Roland McGrath09a05392008-07-25 19:45:47 -0700228 *
229 * Called with no locks held, but the child cannot run until this returns.
230 */
Oleg Nesterov087eb432009-06-04 16:29:07 -0700231static inline void tracehook_report_clone(struct pt_regs *regs,
Roland McGrath09a05392008-07-25 19:45:47 -0700232 unsigned long clone_flags,
233 pid_t pid, struct task_struct *child)
234{
Tejun Heod21142e2011-06-17 16:50:34 +0200235 if (unlikely(child->ptrace)) {
Roland McGrath09a05392008-07-25 19:45:47 -0700236 /*
Oleg Nesterov087eb432009-06-04 16:29:07 -0700237 * It doesn't matter who attached/attaching to this
238 * task, the pending SIGSTOP is right in any case.
Roland McGrath09a05392008-07-25 19:45:47 -0700239 */
240 sigaddset(&child->pending.signal, SIGSTOP);
241 set_tsk_thread_flag(child, TIF_SIGPENDING);
242 }
243}
244
245/**
246 * tracehook_report_clone_complete - new child is running
Roland McGratha9906a12008-07-26 14:41:26 -0700247 * @trace: return value from tracehook_prepare_clone()
Roland McGrath09a05392008-07-25 19:45:47 -0700248 * @regs: parent's user register state
249 * @clone_flags: flags from parent's system call
250 * @pid: new child's PID in the parent's namespace
251 * @child: child task, already running
252 *
253 * This is called just after the child has started running. This is
254 * just before the clone/fork syscall returns, or blocks for vfork
255 * child completion if @clone_flags has the %CLONE_VFORK bit set.
256 * The @child pointer may be invalid if a self-reaping child died and
257 * tracehook_report_clone() took no action to prevent it from self-reaping.
258 *
259 * Called with no locks held.
260 */
261static inline void tracehook_report_clone_complete(int trace,
262 struct pt_regs *regs,
263 unsigned long clone_flags,
264 pid_t pid,
265 struct task_struct *child)
266{
267 if (unlikely(trace))
Tejun Heo643ad832011-06-17 16:50:35 +0200268 ptrace_event(trace, pid);
Roland McGrath09a05392008-07-25 19:45:47 -0700269}
270
Roland McGrathdaded342008-07-25 19:45:47 -0700271/**
Roland McGrathc45aea22008-07-25 19:45:50 -0700272 * tracehook_signal_handler - signal handler setup is complete
273 * @sig: number of signal being delivered
274 * @info: siginfo_t of signal being delivered
275 * @ka: sigaction setting that chose the handler
276 * @regs: user register state
277 * @stepping: nonzero if debugger single-step or block-step in use
278 *
279 * Called by the arch code after a signal handler has been set up.
280 * Register and stack state reflects the user handler about to run.
281 * Signal mask changes have already been made.
282 *
283 * Called without locks, shortly before returning to user mode
284 * (or handling more signals).
285 */
286static inline void tracehook_signal_handler(int sig, siginfo_t *info,
287 const struct k_sigaction *ka,
288 struct pt_regs *regs, int stepping)
289{
290 if (stepping)
291 ptrace_notify(SIGTRAP);
292}
293
Roland McGrath115a3262008-08-04 13:56:01 -0700294#define DEATH_REAP -1
295#define DEATH_DELAYED_GROUP_LEADER -2
296
Roland McGrath2b2a1ff2008-07-25 19:45:54 -0700297/**
298 * tracehook_notify_death - task is dead, ready to notify parent
299 * @task: @current task now exiting
300 * @death_cookie: value to pass to tracehook_report_death()
301 * @group_dead: nonzero if this was the last thread in the group to die
302 *
Roland McGrath5c7edcd2008-07-31 02:04:09 -0700303 * A return value >= 0 means call do_notify_parent() with that signal
304 * number. Negative return value can be %DEATH_REAP to self-reap right
305 * now, or %DEATH_DELAYED_GROUP_LEADER to a zombie without notifying our
306 * parent. Note that a return value of 0 means a do_notify_parent() call
307 * that sends no signal, but still wakes up a parent blocked in wait*().
Roland McGrath2b2a1ff2008-07-25 19:45:54 -0700308 *
309 * Called with write_lock_irq(&tasklist_lock) held.
310 */
311static inline int tracehook_notify_death(struct task_struct *task,
312 void **death_cookie, int group_dead)
313{
Oleg Nesterovbb24c672009-04-02 16:58:20 -0700314 if (task_detached(task))
Roland McGrath5c7edcd2008-07-31 02:04:09 -0700315 return task->ptrace ? SIGCHLD : DEATH_REAP;
Roland McGrath2b2a1ff2008-07-25 19:45:54 -0700316
317 /*
318 * If something other than our normal parent is ptracing us, then
319 * send it a SIGCHLD instead of honoring exit_signal. exit_signal
320 * only has special meaning to our real parent.
321 */
322 if (thread_group_empty(task) && !ptrace_reparented(task))
323 return task->exit_signal;
324
Roland McGrath5c7edcd2008-07-31 02:04:09 -0700325 return task->ptrace ? SIGCHLD : DEATH_DELAYED_GROUP_LEADER;
Roland McGrath2b2a1ff2008-07-25 19:45:54 -0700326}
327
Roland McGrath64b12082008-07-25 19:45:56 -0700328#ifdef TIF_NOTIFY_RESUME
329/**
330 * set_notify_resume - cause tracehook_notify_resume() to be called
331 * @task: task that will call tracehook_notify_resume()
332 *
333 * Calling this arranges that @task will call tracehook_notify_resume()
334 * before returning to user mode. If it's already running in user mode,
335 * it will enter the kernel and call tracehook_notify_resume() soon.
336 * If it's blocked, it will not be woken.
337 */
338static inline void set_notify_resume(struct task_struct *task)
339{
340 if (!test_and_set_tsk_thread_flag(task, TIF_NOTIFY_RESUME))
341 kick_process(task);
342}
343
344/**
345 * tracehook_notify_resume - report when about to return to user mode
346 * @regs: user-mode registers of @current task
347 *
348 * This is called when %TIF_NOTIFY_RESUME has been set. Now we are
349 * about to return to user mode, and the user state in @regs can be
350 * inspected or adjusted. The caller in arch code has cleared
351 * %TIF_NOTIFY_RESUME before the call. If the flag gets set again
352 * asynchronously, this will be called again before we return to
353 * user mode.
354 *
355 * Called without locks.
356 */
357static inline void tracehook_notify_resume(struct pt_regs *regs)
358{
359}
360#endif /* TIF_NOTIFY_RESUME */
361
Roland McGrath88ac2922008-07-25 19:45:43 -0700362#endif /* <linux/tracehook.h> */