tracehook.h 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582
  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. It should preserve enough information so that syscall_rollback()
  99. * can work (see asm-generic/syscall.h).
  100. *
  101. * Called without locks, just after entering kernel mode.
  102. */
  103. static inline __must_check int tracehook_report_syscall_entry(
  104. struct pt_regs *regs)
  105. {
  106. ptrace_report_syscall(regs);
  107. return 0;
  108. }
  109. /**
  110. * tracehook_report_syscall_exit - task has just finished a system call
  111. * @regs: user register state of current task
  112. * @step: nonzero if simulating single-step or block-step
  113. *
  114. * This will be called if %TIF_SYSCALL_TRACE has been set, when the
  115. * current task has just finished an attempted system call. Full
  116. * user register state is available here. It is safe to block here,
  117. * preventing signals from being processed.
  118. *
  119. * If @step is nonzero, this report is also in lieu of the normal
  120. * trap that would follow the system call instruction because
  121. * user_enable_block_step() or user_enable_single_step() was used.
  122. * In this case, %TIF_SYSCALL_TRACE might not be set.
  123. *
  124. * Called without locks, just before checking for pending signals.
  125. */
  126. static inline void tracehook_report_syscall_exit(struct pt_regs *regs, int step)
  127. {
  128. ptrace_report_syscall(regs);
  129. }
  130. /**
  131. * tracehook_unsafe_exec - check for exec declared unsafe due to tracing
  132. * @task: current task doing exec
  133. *
  134. * Return %LSM_UNSAFE_* bits applied to an exec because of tracing.
  135. *
  136. * Called with task_lock() held on @task.
  137. */
  138. static inline int tracehook_unsafe_exec(struct task_struct *task)
  139. {
  140. int unsafe = 0;
  141. int ptrace = task_ptrace(task);
  142. if (ptrace & PT_PTRACED) {
  143. if (ptrace & PT_PTRACE_CAP)
  144. unsafe |= LSM_UNSAFE_PTRACE_CAP;
  145. else
  146. unsafe |= LSM_UNSAFE_PTRACE;
  147. }
  148. return unsafe;
  149. }
  150. /**
  151. * tracehook_tracer_task - return the task that is tracing the given task
  152. * @tsk: task to consider
  153. *
  154. * Returns NULL if noone is tracing @task, or the &struct task_struct
  155. * pointer to its tracer.
  156. *
  157. * Must called under rcu_read_lock(). The pointer returned might be kept
  158. * live only by RCU. During exec, this may be called with task_lock()
  159. * held on @task, still held from when tracehook_unsafe_exec() was called.
  160. */
  161. static inline struct task_struct *tracehook_tracer_task(struct task_struct *tsk)
  162. {
  163. if (task_ptrace(tsk) & PT_PTRACED)
  164. return rcu_dereference(tsk->parent);
  165. return NULL;
  166. }
  167. /**
  168. * tracehook_report_exec - a successful exec was completed
  169. * @fmt: &struct linux_binfmt that performed the exec
  170. * @bprm: &struct linux_binprm containing exec details
  171. * @regs: user-mode register state
  172. *
  173. * An exec just completed, we are shortly going to return to user mode.
  174. * The freshly initialized register state can be seen and changed in @regs.
  175. * The name, file and other pointers in @bprm are still on hand to be
  176. * inspected, but will be freed as soon as this returns.
  177. *
  178. * Called with no locks, but with some kernel resources held live
  179. * and a reference on @fmt->module.
  180. */
  181. static inline void tracehook_report_exec(struct linux_binfmt *fmt,
  182. struct linux_binprm *bprm,
  183. struct pt_regs *regs)
  184. {
  185. if (!ptrace_event(PT_TRACE_EXEC, PTRACE_EVENT_EXEC, 0) &&
  186. unlikely(task_ptrace(current) & PT_PTRACED))
  187. send_sig(SIGTRAP, current, 0);
  188. }
  189. /**
  190. * tracehook_report_exit - task has begun to exit
  191. * @exit_code: pointer to value destined for @current->exit_code
  192. *
  193. * @exit_code points to the value passed to do_exit(), which tracing
  194. * might change here. This is almost the first thing in do_exit(),
  195. * before freeing any resources or setting the %PF_EXITING flag.
  196. *
  197. * Called with no locks held.
  198. */
  199. static inline void tracehook_report_exit(long *exit_code)
  200. {
  201. ptrace_event(PT_TRACE_EXIT, PTRACE_EVENT_EXIT, *exit_code);
  202. }
  203. /**
  204. * tracehook_prepare_clone - prepare for new child to be cloned
  205. * @clone_flags: %CLONE_* flags from clone/fork/vfork system call
  206. *
  207. * This is called before a new user task is to be cloned.
  208. * Its return value will be passed to tracehook_finish_clone().
  209. *
  210. * Called with no locks held.
  211. */
  212. static inline int tracehook_prepare_clone(unsigned clone_flags)
  213. {
  214. if (clone_flags & CLONE_UNTRACED)
  215. return 0;
  216. if (clone_flags & CLONE_VFORK) {
  217. if (current->ptrace & PT_TRACE_VFORK)
  218. return PTRACE_EVENT_VFORK;
  219. } else if ((clone_flags & CSIGNAL) != SIGCHLD) {
  220. if (current->ptrace & PT_TRACE_CLONE)
  221. return PTRACE_EVENT_CLONE;
  222. } else if (current->ptrace & PT_TRACE_FORK)
  223. return PTRACE_EVENT_FORK;
  224. return 0;
  225. }
  226. /**
  227. * tracehook_finish_clone - new child created and being attached
  228. * @child: new child task
  229. * @clone_flags: %CLONE_* flags from clone/fork/vfork system call
  230. * @trace: return value from tracehook_prepare_clone()
  231. *
  232. * This is called immediately after adding @child to its parent's children list.
  233. * The @trace value is that returned by tracehook_prepare_clone().
  234. *
  235. * Called with current's siglock and write_lock_irq(&tasklist_lock) held.
  236. */
  237. static inline void tracehook_finish_clone(struct task_struct *child,
  238. unsigned long clone_flags, int trace)
  239. {
  240. ptrace_init_task(child, (clone_flags & CLONE_PTRACE) || trace);
  241. }
  242. /**
  243. * tracehook_report_clone - in parent, new child is about to start running
  244. * @trace: return value from tracehook_prepare_clone()
  245. * @regs: parent's user register state
  246. * @clone_flags: flags from parent's system call
  247. * @pid: new child's PID in the parent's namespace
  248. * @child: new child task
  249. *
  250. * Called after a child is set up, but before it has been started
  251. * running. @trace is the value returned by tracehook_prepare_clone().
  252. * This is not a good place to block, because the child has not started
  253. * yet. Suspend the child here if desired, and then block in
  254. * tracehook_report_clone_complete(). This must prevent the child from
  255. * self-reaping if tracehook_report_clone_complete() uses the @child
  256. * pointer; otherwise it might have died and been released by the time
  257. * tracehook_report_clone_complete() is called.
  258. *
  259. * Called with no locks held, but the child cannot run until this returns.
  260. */
  261. static inline void tracehook_report_clone(int trace, struct pt_regs *regs,
  262. unsigned long clone_flags,
  263. pid_t pid, struct task_struct *child)
  264. {
  265. if (unlikely(trace) || unlikely(clone_flags & CLONE_PTRACE)) {
  266. /*
  267. * The child starts up with an immediate SIGSTOP.
  268. */
  269. sigaddset(&child->pending.signal, SIGSTOP);
  270. set_tsk_thread_flag(child, TIF_SIGPENDING);
  271. }
  272. }
  273. /**
  274. * tracehook_report_clone_complete - new child is running
  275. * @trace: return value from tracehook_prepare_clone()
  276. * @regs: parent's user register state
  277. * @clone_flags: flags from parent's system call
  278. * @pid: new child's PID in the parent's namespace
  279. * @child: child task, already running
  280. *
  281. * This is called just after the child has started running. This is
  282. * just before the clone/fork syscall returns, or blocks for vfork
  283. * child completion if @clone_flags has the %CLONE_VFORK bit set.
  284. * The @child pointer may be invalid if a self-reaping child died and
  285. * tracehook_report_clone() took no action to prevent it from self-reaping.
  286. *
  287. * Called with no locks held.
  288. */
  289. static inline void tracehook_report_clone_complete(int trace,
  290. struct pt_regs *regs,
  291. unsigned long clone_flags,
  292. pid_t pid,
  293. struct task_struct *child)
  294. {
  295. if (unlikely(trace))
  296. ptrace_event(0, trace, pid);
  297. }
  298. /**
  299. * tracehook_report_vfork_done - vfork parent's child has exited or exec'd
  300. * @child: child task, already running
  301. * @pid: new child's PID in the parent's namespace
  302. *
  303. * Called after a %CLONE_VFORK parent has waited for the child to complete.
  304. * The clone/vfork system call will return immediately after this.
  305. * The @child pointer may be invalid if a self-reaping child died and
  306. * tracehook_report_clone() took no action to prevent it from self-reaping.
  307. *
  308. * Called with no locks held.
  309. */
  310. static inline void tracehook_report_vfork_done(struct task_struct *child,
  311. pid_t pid)
  312. {
  313. ptrace_event(PT_TRACE_VFORK_DONE, PTRACE_EVENT_VFORK_DONE, pid);
  314. }
  315. /**
  316. * tracehook_prepare_release_task - task is being reaped, clean up tracing
  317. * @task: task in %EXIT_DEAD state
  318. *
  319. * This is called in release_task() just before @task gets finally reaped
  320. * and freed. This would be the ideal place to remove and clean up any
  321. * tracing-related state for @task.
  322. *
  323. * Called with no locks held.
  324. */
  325. static inline void tracehook_prepare_release_task(struct task_struct *task)
  326. {
  327. }
  328. /**
  329. * tracehook_finish_release_task - final tracing clean-up
  330. * @task: task in %EXIT_DEAD state
  331. *
  332. * This is called in release_task() when @task is being in the middle of
  333. * being reaped. After this, there must be no tracing entanglements.
  334. *
  335. * Called with write_lock_irq(&tasklist_lock) held.
  336. */
  337. static inline void tracehook_finish_release_task(struct task_struct *task)
  338. {
  339. ptrace_release_task(task);
  340. }
  341. /**
  342. * tracehook_signal_handler - signal handler setup is complete
  343. * @sig: number of signal being delivered
  344. * @info: siginfo_t of signal being delivered
  345. * @ka: sigaction setting that chose the handler
  346. * @regs: user register state
  347. * @stepping: nonzero if debugger single-step or block-step in use
  348. *
  349. * Called by the arch code after a signal handler has been set up.
  350. * Register and stack state reflects the user handler about to run.
  351. * Signal mask changes have already been made.
  352. *
  353. * Called without locks, shortly before returning to user mode
  354. * (or handling more signals).
  355. */
  356. static inline void tracehook_signal_handler(int sig, siginfo_t *info,
  357. const struct k_sigaction *ka,
  358. struct pt_regs *regs, int stepping)
  359. {
  360. if (stepping)
  361. ptrace_notify(SIGTRAP);
  362. }
  363. /**
  364. * tracehook_consider_ignored_signal - suppress short-circuit of ignored signal
  365. * @task: task receiving the signal
  366. * @sig: signal number being sent
  367. * @handler: %SIG_IGN or %SIG_DFL
  368. *
  369. * Return zero iff tracing doesn't care to examine this ignored signal,
  370. * so it can short-circuit normal delivery and never even get queued.
  371. * Either @handler is %SIG_DFL and @sig's default is ignore, or it's %SIG_IGN.
  372. *
  373. * Called with @task->sighand->siglock held.
  374. */
  375. static inline int tracehook_consider_ignored_signal(struct task_struct *task,
  376. int sig,
  377. void __user *handler)
  378. {
  379. return (task_ptrace(task) & PT_PTRACED) != 0;
  380. }
  381. /**
  382. * tracehook_consider_fatal_signal - suppress special handling of fatal signal
  383. * @task: task receiving the signal
  384. * @sig: signal number being sent
  385. * @handler: %SIG_DFL or %SIG_IGN
  386. *
  387. * Return nonzero to prevent special handling of this termination signal.
  388. * Normally @handler is %SIG_DFL. It can be %SIG_IGN if @sig is ignored,
  389. * in which case force_sig() is about to reset it to %SIG_DFL.
  390. * When this returns zero, this signal might cause a quick termination
  391. * that does not give the debugger a chance to intercept the signal.
  392. *
  393. * Called with or without @task->sighand->siglock held.
  394. */
  395. static inline int tracehook_consider_fatal_signal(struct task_struct *task,
  396. int sig,
  397. void __user *handler)
  398. {
  399. return (task_ptrace(task) & PT_PTRACED) != 0;
  400. }
  401. /**
  402. * tracehook_force_sigpending - let tracing force signal_pending(current) on
  403. *
  404. * Called when recomputing our signal_pending() flag. Return nonzero
  405. * to force the signal_pending() flag on, so that tracehook_get_signal()
  406. * will be called before the next return to user mode.
  407. *
  408. * Called with @current->sighand->siglock held.
  409. */
  410. static inline int tracehook_force_sigpending(void)
  411. {
  412. return 0;
  413. }
  414. /**
  415. * tracehook_get_signal - deliver synthetic signal to traced task
  416. * @task: @current
  417. * @regs: task_pt_regs(@current)
  418. * @info: details of synthetic signal
  419. * @return_ka: sigaction for synthetic signal
  420. *
  421. * Return zero to check for a real pending signal normally.
  422. * Return -1 after releasing the siglock to repeat the check.
  423. * Return a signal number to induce an artifical signal delivery,
  424. * setting *@info and *@return_ka to specify its details and behavior.
  425. *
  426. * The @return_ka->sa_handler value controls the disposition of the
  427. * signal, no matter the signal number. For %SIG_DFL, the return value
  428. * is a representative signal to indicate the behavior (e.g. %SIGTERM
  429. * for death, %SIGQUIT for core dump, %SIGSTOP for job control stop,
  430. * %SIGTSTP for stop unless in an orphaned pgrp), but the signal number
  431. * reported will be @info->si_signo instead.
  432. *
  433. * Called with @task->sighand->siglock held, before dequeuing pending signals.
  434. */
  435. static inline int tracehook_get_signal(struct task_struct *task,
  436. struct pt_regs *regs,
  437. siginfo_t *info,
  438. struct k_sigaction *return_ka)
  439. {
  440. return 0;
  441. }
  442. /**
  443. * tracehook_notify_jctl - report about job control stop/continue
  444. * @notify: nonzero if this is the last thread in the group to stop
  445. * @why: %CLD_STOPPED or %CLD_CONTINUED
  446. *
  447. * This is called when we might call do_notify_parent_cldstop().
  448. * It's called when about to stop for job control; we are already in
  449. * %TASK_STOPPED state, about to call schedule(). It's also called when
  450. * a delayed %CLD_STOPPED or %CLD_CONTINUED report is ready to be made.
  451. *
  452. * Return nonzero to generate a %SIGCHLD with @why, which is
  453. * normal if @notify is nonzero.
  454. *
  455. * Called with no locks held.
  456. */
  457. static inline int tracehook_notify_jctl(int notify, int why)
  458. {
  459. return notify || (current->ptrace & PT_PTRACED);
  460. }
  461. #define DEATH_REAP -1
  462. #define DEATH_DELAYED_GROUP_LEADER -2
  463. /**
  464. * tracehook_notify_death - task is dead, ready to notify parent
  465. * @task: @current task now exiting
  466. * @death_cookie: value to pass to tracehook_report_death()
  467. * @group_dead: nonzero if this was the last thread in the group to die
  468. *
  469. * A return value >= 0 means call do_notify_parent() with that signal
  470. * number. Negative return value can be %DEATH_REAP to self-reap right
  471. * now, or %DEATH_DELAYED_GROUP_LEADER to a zombie without notifying our
  472. * parent. Note that a return value of 0 means a do_notify_parent() call
  473. * that sends no signal, but still wakes up a parent blocked in wait*().
  474. *
  475. * Called with write_lock_irq(&tasklist_lock) held.
  476. */
  477. static inline int tracehook_notify_death(struct task_struct *task,
  478. void **death_cookie, int group_dead)
  479. {
  480. if (task->exit_signal == -1)
  481. return task->ptrace ? SIGCHLD : DEATH_REAP;
  482. /*
  483. * If something other than our normal parent is ptracing us, then
  484. * send it a SIGCHLD instead of honoring exit_signal. exit_signal
  485. * only has special meaning to our real parent.
  486. */
  487. if (thread_group_empty(task) && !ptrace_reparented(task))
  488. return task->exit_signal;
  489. return task->ptrace ? SIGCHLD : DEATH_DELAYED_GROUP_LEADER;
  490. }
  491. /**
  492. * tracehook_report_death - task is dead and ready to be reaped
  493. * @task: @current task now exiting
  494. * @signal: return value from tracheook_notify_death()
  495. * @death_cookie: value passed back from tracehook_notify_death()
  496. * @group_dead: nonzero if this was the last thread in the group to die
  497. *
  498. * Thread has just become a zombie or is about to self-reap. If positive,
  499. * @signal is the signal number just sent to the parent (usually %SIGCHLD).
  500. * If @signal is %DEATH_REAP, this thread will self-reap. If @signal is
  501. * %DEATH_DELAYED_GROUP_LEADER, this is a delayed_group_leader() zombie.
  502. * The @death_cookie was passed back by tracehook_notify_death().
  503. *
  504. * If normal reaping is not inhibited, @task->exit_state might be changing
  505. * in parallel.
  506. *
  507. * Called without locks.
  508. */
  509. static inline void tracehook_report_death(struct task_struct *task,
  510. int signal, void *death_cookie,
  511. int group_dead)
  512. {
  513. }
  514. #ifdef TIF_NOTIFY_RESUME
  515. /**
  516. * set_notify_resume - cause tracehook_notify_resume() to be called
  517. * @task: task that will call tracehook_notify_resume()
  518. *
  519. * Calling this arranges that @task will call tracehook_notify_resume()
  520. * before returning to user mode. If it's already running in user mode,
  521. * it will enter the kernel and call tracehook_notify_resume() soon.
  522. * If it's blocked, it will not be woken.
  523. */
  524. static inline void set_notify_resume(struct task_struct *task)
  525. {
  526. if (!test_and_set_tsk_thread_flag(task, TIF_NOTIFY_RESUME))
  527. kick_process(task);
  528. }
  529. /**
  530. * tracehook_notify_resume - report when about to return to user mode
  531. * @regs: user-mode registers of @current task
  532. *
  533. * This is called when %TIF_NOTIFY_RESUME has been set. Now we are
  534. * about to return to user mode, and the user state in @regs can be
  535. * inspected or adjusted. The caller in arch code has cleared
  536. * %TIF_NOTIFY_RESUME before the call. If the flag gets set again
  537. * asynchronously, this will be called again before we return to
  538. * user mode.
  539. *
  540. * Called without locks.
  541. */
  542. static inline void tracehook_notify_resume(struct pt_regs *regs)
  543. {
  544. }
  545. #endif /* TIF_NOTIFY_RESUME */
  546. #endif /* <linux/tracehook.h> */