Mirror of strace – the linux syscall tracer
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

pathtrace.c 8.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394
  1. /*
  2. * Copyright (c) 2011 Comtrol Corp.
  3. * Copyright (c) 2011-2019 The strace developers.
  4. * All rights reserved.
  5. *
  6. * SPDX-License-Identifier: LGPL-2.1-or-later
  7. *
  8. */
  9. #include "defs.h"
  10. #include <limits.h>
  11. #include <poll.h>
  12. #include "syscall.h"
  13. #include "xstring.h"
  14. struct path_set global_path_set;
  15. /*
  16. * Return true if specified path matches one that we're tracing.
  17. */
  18. static bool
  19. pathmatch(const char *path, struct path_set *set)
  20. {
  21. unsigned i;
  22. for (i = 0; i < set->num_selected; ++i) {
  23. if (strcmp(path, set->paths_selected[i]) == 0)
  24. return true;
  25. }
  26. return false;
  27. }
  28. /*
  29. * Return true if specified path (in user-space) matches.
  30. */
  31. static bool
  32. upathmatch(struct tcb *const tcp, const kernel_ulong_t upath,
  33. struct path_set *set)
  34. {
  35. char path[PATH_MAX + 1];
  36. return umovestr(tcp, upath, sizeof(path), path) > 0 &&
  37. pathmatch(path, set);
  38. }
  39. /*
  40. * Return true if specified fd maps to a path we're tracing.
  41. */
  42. static bool
  43. fdmatch(struct tcb *tcp, int fd, struct path_set *set)
  44. {
  45. char path[PATH_MAX + 1];
  46. int n = getfdpath(tcp, fd, path, sizeof(path));
  47. return n >= 0 && pathmatch(path, set);
  48. }
  49. /*
  50. * Add a path to the set we're tracing.
  51. * Specifying NULL will delete all paths.
  52. */
  53. static void
  54. storepath(const char *path, struct path_set *set)
  55. {
  56. if (pathmatch(path, set))
  57. return; /* already in table */
  58. if (set->num_selected >= set->size)
  59. set->paths_selected =
  60. xgrowarray(set->paths_selected, &set->size,
  61. sizeof(set->paths_selected[0]));
  62. set->paths_selected[set->num_selected++] = path;
  63. }
  64. /*
  65. * Get path associated with fd.
  66. */
  67. int
  68. getfdpath(struct tcb *tcp, int fd, char *buf, unsigned bufsize)
  69. {
  70. char linkpath[sizeof("/proc/%u/fd/%u") + 2 * sizeof(int)*3];
  71. ssize_t n;
  72. if (fd < 0)
  73. return -1;
  74. xsprintf(linkpath, "/proc/%u/fd/%u", tcp->pid, fd);
  75. n = readlink(linkpath, buf, bufsize - 1);
  76. /*
  77. * NB: if buf is too small, readlink doesn't fail,
  78. * it returns truncated result (IOW: n == bufsize - 1).
  79. */
  80. if (n >= 0)
  81. buf[n] = '\0';
  82. return n;
  83. }
  84. /*
  85. * Add a path to the set we're tracing. Also add the canonicalized
  86. * version of the path. Specifying NULL will delete all paths.
  87. */
  88. void
  89. pathtrace_select_set(const char *path, struct path_set *set)
  90. {
  91. char *rpath;
  92. storepath(path, set);
  93. rpath = realpath(path, NULL);
  94. if (rpath == NULL)
  95. return;
  96. /* if realpath and specified path are same, we're done */
  97. if (strcmp(path, rpath) == 0) {
  98. free(rpath);
  99. return;
  100. }
  101. error_msg("Requested path '%s' resolved into '%s'", path, rpath);
  102. storepath(rpath, set);
  103. }
  104. static bool
  105. match_xselect_args(struct tcb *tcp, const kernel_ulong_t *args,
  106. struct path_set *set)
  107. {
  108. /* Kernel truncates arg[0] to int, we do the same. */
  109. int nfds = (int) args[0];
  110. /* Kernel rejects negative nfds, so we don't parse it either. */
  111. if (nfds <= 0)
  112. return false;
  113. /* Beware of select(2^31-1, NULL, NULL, NULL) and similar... */
  114. if (nfds > 1024*1024)
  115. nfds = 1024*1024;
  116. unsigned int fdsize = (((nfds + 7) / 8) + current_wordsize-1) & -current_wordsize;
  117. fd_set *fds = xmalloc(fdsize);
  118. for (unsigned int i = 1; i <= 3; ++i) {
  119. if (args[i] == 0)
  120. continue;
  121. if (umoven(tcp, args[i], fdsize, fds) < 0)
  122. continue;
  123. for (int j = 0;; ++j) {
  124. j = next_set_bit(fds, j, nfds);
  125. if (j < 0)
  126. break;
  127. if (fdmatch(tcp, j, set)) {
  128. free(fds);
  129. return true;
  130. }
  131. }
  132. }
  133. free(fds);
  134. return false;
  135. }
  136. /*
  137. * Return true if syscall accesses a selected path
  138. * (or if no paths have been specified for tracing).
  139. */
  140. bool
  141. pathtrace_match_set(struct tcb *tcp, struct path_set *set)
  142. {
  143. const struct_sysent *s;
  144. s = tcp_sysent(tcp);
  145. if (!(s->sys_flags & (TRACE_FILE | TRACE_DESC | TRACE_NETWORK)))
  146. return false;
  147. /*
  148. * Check for special cases where we need to do something
  149. * other than test arg[0].
  150. */
  151. switch (s->sen) {
  152. case SEN_dup2:
  153. case SEN_dup3:
  154. case SEN_kexec_file_load:
  155. case SEN_sendfile:
  156. case SEN_sendfile64:
  157. case SEN_tee:
  158. /* fd, fd */
  159. return fdmatch(tcp, tcp->u_arg[0], set) ||
  160. fdmatch(tcp, tcp->u_arg[1], set);
  161. case SEN_execveat:
  162. case SEN_faccessat:
  163. case SEN_fchmodat:
  164. case SEN_fchownat:
  165. case SEN_fspick:
  166. case SEN_fstatat64:
  167. case SEN_futimesat:
  168. case SEN_inotify_add_watch:
  169. case SEN_mkdirat:
  170. case SEN_mknodat:
  171. case SEN_name_to_handle_at:
  172. case SEN_newfstatat:
  173. case SEN_open_tree:
  174. case SEN_openat:
  175. case SEN_readlinkat:
  176. case SEN_statx:
  177. case SEN_unlinkat:
  178. case SEN_utimensat_time32:
  179. case SEN_utimensat_time64:
  180. /* fd, path */
  181. return fdmatch(tcp, tcp->u_arg[0], set) ||
  182. upathmatch(tcp, tcp->u_arg[1], set);
  183. case SEN_link:
  184. case SEN_mount:
  185. case SEN_pivotroot:
  186. /* path, path */
  187. return upathmatch(tcp, tcp->u_arg[0], set) ||
  188. upathmatch(tcp, tcp->u_arg[1], set);
  189. case SEN_quotactl:
  190. case SEN_symlink:
  191. /* x, path */
  192. return upathmatch(tcp, tcp->u_arg[1], set);
  193. case SEN_linkat:
  194. case SEN_move_mount:
  195. case SEN_renameat2:
  196. case SEN_renameat:
  197. /* fd, path, fd, path */
  198. return fdmatch(tcp, tcp->u_arg[0], set) ||
  199. fdmatch(tcp, tcp->u_arg[2], set) ||
  200. upathmatch(tcp, tcp->u_arg[1], set) ||
  201. upathmatch(tcp, tcp->u_arg[3], set);
  202. #if HAVE_ARCH_OLD_MMAP
  203. case SEN_old_mmap:
  204. # if HAVE_ARCH_OLD_MMAP_PGOFF
  205. case SEN_old_mmap_pgoff:
  206. # endif
  207. {
  208. kernel_ulong_t *args =
  209. fetch_indirect_syscall_args(tcp, tcp->u_arg[0], 6);
  210. return args && fdmatch(tcp, args[4], set);
  211. }
  212. #endif /* HAVE_ARCH_OLD_MMAP */
  213. case SEN_mmap:
  214. case SEN_mmap_4koff:
  215. case SEN_mmap_pgoff:
  216. case SEN_ARCH_mmap:
  217. /* x, x, x, x, fd */
  218. return fdmatch(tcp, tcp->u_arg[4], set);
  219. case SEN_symlinkat:
  220. /* x, fd, path */
  221. return fdmatch(tcp, tcp->u_arg[1], set) ||
  222. upathmatch(tcp, tcp->u_arg[2], set);
  223. case SEN_copy_file_range:
  224. case SEN_splice:
  225. /* fd, x, fd, x, x, x */
  226. return fdmatch(tcp, tcp->u_arg[0], set) ||
  227. fdmatch(tcp, tcp->u_arg[2], set);
  228. case SEN_epoll_ctl:
  229. /* x, x, fd, x */
  230. return fdmatch(tcp, tcp->u_arg[2], set);
  231. case SEN_fanotify_mark:
  232. {
  233. /* x, x, mask (64 bit), fd, path */
  234. unsigned long long mask = 0;
  235. int argn = getllval(tcp, &mask, 2);
  236. return fdmatch(tcp, tcp->u_arg[argn], set) ||
  237. upathmatch(tcp, tcp->u_arg[argn + 1], set);
  238. }
  239. #if HAVE_ARCH_OLD_SELECT
  240. case SEN_oldselect:
  241. {
  242. kernel_ulong_t *args =
  243. fetch_indirect_syscall_args(tcp, tcp->u_arg[0], 5);
  244. return args && match_xselect_args(tcp, args, set);
  245. }
  246. #endif
  247. case SEN_pselect6_time32:
  248. case SEN_pselect6_time64:
  249. case SEN_select:
  250. return match_xselect_args(tcp, tcp->u_arg, set);
  251. case SEN_poll_time32:
  252. case SEN_poll_time64:
  253. case SEN_ppoll_time32:
  254. case SEN_ppoll_time64:
  255. {
  256. struct pollfd fds;
  257. unsigned nfds;
  258. kernel_ulong_t start, cur, end;
  259. start = tcp->u_arg[0];
  260. nfds = tcp->u_arg[1];
  261. if (nfds > 1024 * 1024)
  262. nfds = 1024 * 1024;
  263. end = start + sizeof(fds) * nfds;
  264. if (nfds == 0 || end < start)
  265. return false;
  266. for (cur = start; cur < end; cur += sizeof(fds)) {
  267. if (umove(tcp, cur, &fds))
  268. break;
  269. if (fdmatch(tcp, fds.fd, set))
  270. return true;
  271. }
  272. return false;
  273. }
  274. case SEN_fsconfig: {
  275. /* x, x, x, maybe path, maybe fd */
  276. const unsigned int cmd = tcp->u_arg[1];
  277. switch (cmd) {
  278. case 3 /* FSCONFIG_SET_PATH */:
  279. case 4 /* FSCONFIG_SET_PATH_EMPTY */:
  280. return fdmatch(tcp, tcp->u_arg[4], set) ||
  281. upathmatch(tcp, tcp->u_arg[3], set);
  282. case 5 /* FSCONFIG_SET_FD */:
  283. return fdmatch(tcp, tcp->u_arg[4], set);
  284. }
  285. return false;
  286. }
  287. case SEN_accept4:
  288. case SEN_accept:
  289. case SEN_bpf:
  290. case SEN_epoll_create:
  291. case SEN_epoll_create1:
  292. case SEN_eventfd2:
  293. case SEN_eventfd:
  294. case SEN_fanotify_init:
  295. case SEN_fsmount:
  296. case SEN_fsopen:
  297. case SEN_inotify_init:
  298. case SEN_inotify_init1:
  299. case SEN_io_uring_enter:
  300. case SEN_io_uring_register:
  301. case SEN_io_uring_setup:
  302. case SEN_memfd_create:
  303. case SEN_mq_getsetattr:
  304. case SEN_mq_notify:
  305. case SEN_mq_open:
  306. case SEN_mq_timedreceive_time32:
  307. case SEN_mq_timedreceive_time64:
  308. case SEN_mq_timedsend_time32:
  309. case SEN_mq_timedsend_time64:
  310. case SEN_perf_event_open:
  311. case SEN_pidfd_open:
  312. case SEN_pipe:
  313. case SEN_pipe2:
  314. case SEN_printargs:
  315. case SEN_signalfd4:
  316. case SEN_signalfd:
  317. case SEN_socket:
  318. case SEN_socketpair:
  319. case SEN_timerfd_create:
  320. case SEN_timerfd_gettime32:
  321. case SEN_timerfd_gettime64:
  322. case SEN_timerfd_settime32:
  323. case SEN_timerfd_settime64:
  324. case SEN_userfaultfd:
  325. /*
  326. * These have TRACE_FILE or TRACE_DESCRIPTOR or TRACE_NETWORK set,
  327. * but they don't have any file descriptor or path args to test.
  328. */
  329. return false;
  330. }
  331. /*
  332. * Our fallback position for calls that haven't already
  333. * been handled is to just check arg[0].
  334. */
  335. if (s->sys_flags & TRACE_FILE)
  336. return upathmatch(tcp, tcp->u_arg[0], set);
  337. if (s->sys_flags & (TRACE_DESC | TRACE_NETWORK))
  338. return fdmatch(tcp, tcp->u_arg[0], set);
  339. return false;
  340. }