tracehook.h 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474
  1. /*
  2. * Tracing hooks
  3. *
  4. * Copyright (C) 2008 Red Hat, Inc. All rights reserved.
  5. *
  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. #ifndef _LINUX_TRACEHOOK_H
  46. #define _LINUX_TRACEHOOK_H 1
  47. #include <linux/sched.h>
  48. #include <linux/ptrace.h>
  49. #include <linux/security.h>
  50. struct linux_binprm;
  51. /**
  52. * tracehook_expect_breakpoints - guess if task memory might be touched
  53. * @task: current task, making a new mapping
  54. *
  55. * Return nonzero if @task is expected to want breakpoint insertion in
  56. * its memory at some point. A zero return is no guarantee it won't
  57. * be done, but this is a hint that it's known to be likely.
  58. *
  59. * May be called with @task->mm->mmap_sem held for writing.
  60. */
  61. static inline int tracehook_expect_breakpoints(struct task_struct *task)
  62. {
  63. return (task_ptrace(task) & PT_PTRACED) != 0;
  64. }
  65. /*
  66. * ptrace report for syscall entry and exit looks identical.
  67. */
  68. static inline void ptrace_report_syscall(struct pt_regs *regs)
  69. {
  70. int ptrace = task_ptrace(current);
  71. if (!(ptrace & PT_PTRACED))
  72. return;
  73. ptrace_notify(SIGTRAP | ((ptrace & PT_TRACESYSGOOD) ? 0x80 : 0));
  74. /*
  75. * this isn't the same as continuing with a signal, but it will do
  76. * for normal use. strace only continues with a signal if the
  77. * stopping signal is not SIGTRAP. -brl
  78. */
  79. if (current->exit_code) {
  80. send_sig(current->exit_code, current, 1);
  81. current->exit_code = 0;
  82. }
  83. }
  84. /**
  85. * tracehook_report_syscall_entry - task is about to attempt a system call
  86. * @regs: user register state of current task
  87. *
  88. * This will be called if %TIF_SYSCALL_TRACE has been set, when the
  89. * current task has just entered the kernel for a system call.
  90. * Full user register state is available here. Changing the values
  91. * in @regs can affect the system call number and arguments to be tried.
  92. * It is safe to block here, preventing the system call from beginning.
  93. *
  94. * Returns zero normally, or nonzero if the calling arch code should abort
  95. * the system call. That must prevent normal entry so no system call is
  96. * made. If @task ever returns to user mode after this, its register state
  97. * is unspecified, but should be something harmless like an %ENOSYS error
  98. * return.
  99. *
  100. * Called without locks, just after entering kernel mode.
  101. */
  102. static inline __must_check int tracehook_report_syscall_entry(
  103. struct pt_regs *regs)
  104. {
  105. ptrace_report_syscall(regs);
  106. return 0;
  107. }
  108. /**
  109. * tracehook_report_syscall_exit - task has just finished a system call
  110. * @regs: user register state of current task
  111. * @step: nonzero if simulating single-step or block-step
  112. *
  113. * This will be called if %TIF_SYSCALL_TRACE has been set, when the
  114. * current task has just finished an attempted system call. Full
  115. * user register state is available here. It is safe to block here,
  116. * preventing signals from being processed.
  117. *
  118. * If @step is nonzero, this report is also in lieu of the normal
  119. * trap that would follow the system call instruction because
  120. * user_enable_block_step() or user_enable_single_step() was used.
  121. * In this case, %TIF_SYSCALL_TRACE might not be set.
  122. *
  123. * Called without locks, just before checking for pending signals.
  124. */
  125. static inline void tracehook_report_syscall_exit(struct pt_regs *regs, int step)
  126. {
  127. ptrace_report_syscall(regs);
  128. }
  129. /**
  130. * tracehook_unsafe_exec - check for exec declared unsafe due to tracing
  131. * @task: current task doing exec
  132. *
  133. * Return %LSM_UNSAFE_* bits applied to an exec because of tracing.
  134. *
  135. * Called with task_lock() held on @task.
  136. */
  137. static inline int tracehook_unsafe_exec(struct task_struct *task)
  138. {
  139. int unsafe = 0;
  140. int ptrace = task_ptrace(task);
  141. if (ptrace & PT_PTRACED) {
  142. if (ptrace & PT_PTRACE_CAP)
  143. unsafe |= LSM_UNSAFE_PTRACE_CAP;
  144. else
  145. unsafe |= LSM_UNSAFE_PTRACE;
  146. }
  147. return unsafe;
  148. }
  149. /**
  150. * tracehook_tracer_task - return the task that is tracing the given task
  151. * @tsk: task to consider
  152. *
  153. * Returns NULL if noone is tracing @task, or the &struct task_struct
  154. * pointer to its tracer.
  155. *
  156. * Must called under rcu_read_lock(). The pointer returned might be kept
  157. * live only by RCU. During exec, this may be called with task_lock()
  158. * held on @task, still held from when tracehook_unsafe_exec() was called.
  159. */
  160. static inline struct task_struct *tracehook_tracer_task(struct task_struct *tsk)
  161. {
  162. if (task_ptrace(tsk) & PT_PTRACED)
  163. return rcu_dereference(tsk->parent);
  164. return NULL;
  165. }
  166. /**
  167. * tracehook_report_exec - a successful exec was completed
  168. * @fmt: &struct linux_binfmt that performed the exec
  169. * @bprm: &struct linux_binprm containing exec details
  170. * @regs: user-mode register state
  171. *
  172. * An exec just completed, we are shortly going to return to user mode.
  173. * The freshly initialized register state can be seen and changed in @regs.
  174. * The name, file and other pointers in @bprm are still on hand to be
  175. * inspected, but will be freed as soon as this returns.
  176. *
  177. * Called with no locks, but with some kernel resources held live
  178. * and a reference on @fmt->module.
  179. */
  180. static inline void tracehook_report_exec(struct linux_binfmt *fmt,
  181. struct linux_binprm *bprm,
  182. struct pt_regs *regs)
  183. {
  184. if (!ptrace_event(PT_TRACE_EXEC, PTRACE_EVENT_EXEC, 0) &&
  185. unlikely(task_ptrace(current) & PT_PTRACED))
  186. send_sig(SIGTRAP, current, 0);
  187. }
  188. /**
  189. * tracehook_report_exit - task has begun to exit
  190. * @exit_code: pointer to value destined for @current->exit_code
  191. *
  192. * @exit_code points to the value passed to do_exit(), which tracing
  193. * might change here. This is almost the first thing in do_exit(),
  194. * before freeing any resources or setting the %PF_EXITING flag.
  195. *
  196. * Called with no locks held.
  197. */
  198. static inline void tracehook_report_exit(long *exit_code)
  199. {
  200. ptrace_event(PT_TRACE_EXIT, PTRACE_EVENT_EXIT, *exit_code);
  201. }
  202. /**
  203. * tracehook_prepare_clone - prepare for new child to be cloned
  204. * @clone_flags: %CLONE_* flags from clone/fork/vfork system call
  205. *
  206. * This is called before a new user task is to be cloned.
  207. * Its return value will be passed to tracehook_finish_clone().
  208. *
  209. * Called with no locks held.
  210. */
  211. static inline int tracehook_prepare_clone(unsigned clone_flags)
  212. {
  213. if (clone_flags & CLONE_UNTRACED)
  214. return 0;
  215. if (clone_flags & CLONE_VFORK) {
  216. if (current->ptrace & PT_TRACE_VFORK)
  217. return PTRACE_EVENT_VFORK;
  218. } else if ((clone_flags & CSIGNAL) != SIGCHLD) {
  219. if (current->ptrace & PT_TRACE_CLONE)
  220. return PTRACE_EVENT_CLONE;
  221. } else if (current->ptrace & PT_TRACE_FORK)
  222. return PTRACE_EVENT_FORK;
  223. return 0;
  224. }
  225. /**
  226. * tracehook_finish_clone - new child created and being attached
  227. * @child: new child task
  228. * @clone_flags: %CLONE_* flags from clone/fork/vfork system call
  229. * @trace: return value from tracehook_clone_prepare()
  230. *
  231. * This is called immediately after adding @child to its parent's children list.
  232. * The @trace value is that returned by tracehook_prepare_clone().
  233. *
  234. * Called with current's siglock and write_lock_irq(&tasklist_lock) held.
  235. */
  236. static inline void tracehook_finish_clone(struct task_struct *child,
  237. unsigned long clone_flags, int trace)
  238. {
  239. ptrace_init_task(child, (clone_flags & CLONE_PTRACE) || trace);
  240. }
  241. /**
  242. * tracehook_report_clone - in parent, new child is about to start running
  243. * @trace: return value from tracehook_clone_prepare()
  244. * @regs: parent's user register state
  245. * @clone_flags: flags from parent's system call
  246. * @pid: new child's PID in the parent's namespace
  247. * @child: new child task
  248. *
  249. * Called after a child is set up, but before it has been started running.
  250. * The @trace value is that returned by tracehook_clone_prepare().
  251. * This is not a good place to block, because the child has not started yet.
  252. * Suspend the child here if desired, and block in tracehook_clone_complete().
  253. * This must prevent the child from self-reaping if tracehook_clone_complete()
  254. * uses the @child pointer; otherwise it might have died and been released by
  255. * the time tracehook_report_clone_complete() is called.
  256. *
  257. * Called with no locks held, but the child cannot run until this returns.
  258. */
  259. static inline void tracehook_report_clone(int trace, struct pt_regs *regs,
  260. unsigned long clone_flags,
  261. pid_t pid, struct task_struct *child)
  262. {
  263. if (unlikely(trace)) {
  264. /*
  265. * The child starts up with an immediate SIGSTOP.
  266. */
  267. sigaddset(&child->pending.signal, SIGSTOP);
  268. set_tsk_thread_flag(child, TIF_SIGPENDING);
  269. }
  270. }
  271. /**
  272. * tracehook_report_clone_complete - new child is running
  273. * @trace: return value from tracehook_clone_prepare()
  274. * @regs: parent's user register state
  275. * @clone_flags: flags from parent's system call
  276. * @pid: new child's PID in the parent's namespace
  277. * @child: child task, already running
  278. *
  279. * This is called just after the child has started running. This is
  280. * just before the clone/fork syscall returns, or blocks for vfork
  281. * child completion if @clone_flags has the %CLONE_VFORK bit set.
  282. * The @child pointer may be invalid if a self-reaping child died and
  283. * tracehook_report_clone() took no action to prevent it from self-reaping.
  284. *
  285. * Called with no locks held.
  286. */
  287. static inline void tracehook_report_clone_complete(int trace,
  288. struct pt_regs *regs,
  289. unsigned long clone_flags,
  290. pid_t pid,
  291. struct task_struct *child)
  292. {
  293. if (unlikely(trace))
  294. ptrace_event(0, trace, pid);
  295. }
  296. /**
  297. * tracehook_report_vfork_done - vfork parent's child has exited or exec'd
  298. * @child: child task, already running
  299. * @pid: new child's PID in the parent's namespace
  300. *
  301. * Called after a %CLONE_VFORK parent has waited for the child to complete.
  302. * The clone/vfork system call will return immediately after this.
  303. * The @child pointer may be invalid if a self-reaping child died and
  304. * tracehook_report_clone() took no action to prevent it from self-reaping.
  305. *
  306. * Called with no locks held.
  307. */
  308. static inline void tracehook_report_vfork_done(struct task_struct *child,
  309. pid_t pid)
  310. {
  311. ptrace_event(PT_TRACE_VFORK_DONE, PTRACE_EVENT_VFORK_DONE, pid);
  312. }
  313. /**
  314. * tracehook_prepare_release_task - task is being reaped, clean up tracing
  315. * @task: task in %EXIT_DEAD state
  316. *
  317. * This is called in release_task() just before @task gets finally reaped
  318. * and freed. This would be the ideal place to remove and clean up any
  319. * tracing-related state for @task.
  320. *
  321. * Called with no locks held.
  322. */
  323. static inline void tracehook_prepare_release_task(struct task_struct *task)
  324. {
  325. }
  326. /**
  327. * tracehook_finish_release_task - task is being reaped, clean up tracing
  328. * @task: task in %EXIT_DEAD state
  329. *
  330. * This is called in release_task() when @task is being in the middle of
  331. * being reaped. After this, there must be no tracing entanglements.
  332. *
  333. * Called with write_lock_irq(&tasklist_lock) held.
  334. */
  335. static inline void tracehook_finish_release_task(struct task_struct *task)
  336. {
  337. ptrace_release_task(task);
  338. }
  339. /**
  340. * tracehook_signal_handler - signal handler setup is complete
  341. * @sig: number of signal being delivered
  342. * @info: siginfo_t of signal being delivered
  343. * @ka: sigaction setting that chose the handler
  344. * @regs: user register state
  345. * @stepping: nonzero if debugger single-step or block-step in use
  346. *
  347. * Called by the arch code after a signal handler has been set up.
  348. * Register and stack state reflects the user handler about to run.
  349. * Signal mask changes have already been made.
  350. *
  351. * Called without locks, shortly before returning to user mode
  352. * (or handling more signals).
  353. */
  354. static inline void tracehook_signal_handler(int sig, siginfo_t *info,
  355. const struct k_sigaction *ka,
  356. struct pt_regs *regs, int stepping)
  357. {
  358. if (stepping)
  359. ptrace_notify(SIGTRAP);
  360. }
  361. /**
  362. * tracehook_consider_ignored_signal - suppress short-circuit of ignored signal
  363. * @task: task receiving the signal
  364. * @sig: signal number being sent
  365. * @handler: %SIG_IGN or %SIG_DFL
  366. *
  367. * Return zero iff tracing doesn't care to examine this ignored signal,
  368. * so it can short-circuit normal delivery and never even get queued.
  369. * Either @handler is %SIG_DFL and @sig's default is ignore, or it's %SIG_IGN.
  370. *
  371. * Called with @task->sighand->siglock held.
  372. */
  373. static inline int tracehook_consider_ignored_signal(struct task_struct *task,
  374. int sig,
  375. void __user *handler)
  376. {
  377. return (task_ptrace(task) & PT_PTRACED) != 0;
  378. }
  379. /**
  380. * tracehook_consider_fatal_signal - suppress special handling of fatal signal
  381. * @task: task receiving the signal
  382. * @sig: signal number being sent
  383. * @handler: %SIG_DFL or %SIG_IGN
  384. *
  385. * Return nonzero to prevent special handling of this termination signal.
  386. * Normally @handler is %SIG_DFL. It can be %SIG_IGN if @sig is ignored,
  387. * in which case force_sig() is about to reset it to %SIG_DFL.
  388. * When this returns zero, this signal might cause a quick termination
  389. * that does not give the debugger a chance to intercept the signal.
  390. *
  391. * Called with or without @task->sighand->siglock held.
  392. */
  393. static inline int tracehook_consider_fatal_signal(struct task_struct *task,
  394. int sig,
  395. void __user *handler)
  396. {
  397. return (task_ptrace(task) & PT_PTRACED) != 0;
  398. }
  399. /**
  400. * tracehook_get_signal - deliver synthetic signal to traced task
  401. * @task: @current
  402. * @regs: task_pt_regs(@current)
  403. * @info: details of synthetic signal
  404. * @return_ka: sigaction for synthetic signal
  405. *
  406. * Return zero to check for a real pending signal normally.
  407. * Return -1 after releasing the siglock to repeat the check.
  408. * Return a signal number to induce an artifical signal delivery,
  409. * setting *@info and *@return_ka to specify its details and behavior.
  410. *
  411. * The @return_ka->sa_handler value controls the disposition of the
  412. * signal, no matter the signal number. For %SIG_DFL, the return value
  413. * is a representative signal to indicate the behavior (e.g. %SIGTERM
  414. * for death, %SIGQUIT for core dump, %SIGSTOP for job control stop,
  415. * %SIGTSTP for stop unless in an orphaned pgrp), but the signal number
  416. * reported will be @info->si_signo instead.
  417. *
  418. * Called with @task->sighand->siglock held, before dequeuing pending signals.
  419. */
  420. static inline int tracehook_get_signal(struct task_struct *task,
  421. struct pt_regs *regs,
  422. siginfo_t *info,
  423. struct k_sigaction *return_ka)
  424. {
  425. return 0;
  426. }
  427. /**
  428. * tracehook_notify_jctl - report about job control stop/continue
  429. * @notify: nonzero if this is the last thread in the group to stop
  430. * @why: %CLD_STOPPED or %CLD_CONTINUED
  431. *
  432. * This is called when we might call do_notify_parent_cldstop().
  433. * It's called when about to stop for job control; we are already in
  434. * %TASK_STOPPED state, about to call schedule(). It's also called when
  435. * a delayed %CLD_STOPPED or %CLD_CONTINUED report is ready to be made.
  436. *
  437. * Return nonzero to generate a %SIGCHLD with @why, which is
  438. * normal if @notify is nonzero.
  439. *
  440. * Called with no locks held.
  441. */
  442. static inline int tracehook_notify_jctl(int notify, int why)
  443. {
  444. return notify || (current->ptrace & PT_PTRACED);
  445. }
  446. #endif /* <linux/tracehook.h> */