2 * Copyright (c) 2011 Comtrol Corp.
3 * Copyright (c) 2011-2019 The strace developers.
6 * SPDX-License-Identifier: LGPL-2.1-or-later
17 struct path_set global_path_set;
20 * Return true if specified path matches one that we're tracing.
23 pathmatch(const char *path, struct path_set *set)
27 for (i = 0; i < set->num_selected; ++i) {
28 if (strcmp(path, set->paths_selected[i]) == 0)
35 * Return true if specified path (in user-space) matches.
38 upathmatch(struct tcb *const tcp, const kernel_ulong_t upath,
41 char path[PATH_MAX + 1];
43 return umovestr(tcp, upath, sizeof(path), path) > 0 &&
48 * Return true if specified fd maps to a path we're tracing.
51 fdmatch(struct tcb *tcp, int fd, struct path_set *set)
53 char path[PATH_MAX + 1];
54 int n = getfdpath(tcp, fd, path, sizeof(path));
56 return n >= 0 && pathmatch(path, set);
60 * Add a path to the set we're tracing.
61 * Specifying NULL will delete all paths.
64 storepath(const char *path, struct path_set *set)
66 if (pathmatch(path, set))
67 return; /* already in table */
69 if (set->num_selected >= set->size)
71 xgrowarray(set->paths_selected, &set->size,
72 sizeof(set->paths_selected[0]));
74 set->paths_selected[set->num_selected++] = path;
78 * Get path associated with fd.
81 getfdpath(struct tcb *tcp, int fd, char *buf, unsigned bufsize)
83 char linkpath[sizeof("/proc/%u/fd/%u") + 2 * sizeof(int)*3];
89 xsprintf(linkpath, "/proc/%u/fd/%u", tcp->pid, fd);
90 n = readlink(linkpath, buf, bufsize - 1);
92 * NB: if buf is too small, readlink doesn't fail,
93 * it returns truncated result (IOW: n == bufsize - 1).
101 * Add a path to the set we're tracing. Also add the canonicalized
102 * version of the path. Specifying NULL will delete all paths.
105 pathtrace_select_set(const char *path, struct path_set *set)
109 storepath(path, set);
111 rpath = realpath(path, NULL);
116 /* if realpath and specified path are same, we're done */
117 if (strcmp(path, rpath) == 0) {
122 error_msg("Requested path '%s' resolved into '%s'", path, rpath);
123 storepath(rpath, set);
127 match_xselect_args(struct tcb *tcp, const kernel_ulong_t *args,
128 struct path_set *set)
130 /* Kernel truncates arg[0] to int, we do the same. */
131 int nfds = (int) args[0];
132 /* Kernel rejects negative nfds, so we don't parse it either. */
135 /* Beware of select(2^31-1, NULL, NULL, NULL) and similar... */
136 if (nfds > 1024*1024)
138 unsigned int fdsize = (((nfds + 7) / 8) + current_wordsize-1) & -current_wordsize;
139 fd_set *fds = xmalloc(fdsize);
141 for (unsigned int i = 1; i <= 3; ++i) {
144 if (umoven(tcp, args[i], fdsize, fds) < 0)
146 for (int j = 0;; ++j) {
147 j = next_set_bit(fds, j, nfds);
150 if (fdmatch(tcp, j, set)) {
162 * Return true if syscall accesses a selected path
163 * (or if no paths have been specified for tracing).
166 pathtrace_match_set(struct tcb *tcp, struct path_set *set)
168 const struct_sysent *s;
172 if (!(s->sys_flags & (TRACE_FILE | TRACE_DESC | TRACE_NETWORK)))
176 * Check for special cases where we need to do something
177 * other than test arg[0].
183 case SEN_kexec_file_load:
188 return fdmatch(tcp, tcp->u_arg[0], set) ||
189 fdmatch(tcp, tcp->u_arg[1], set);
198 case SEN_inotify_add_watch:
201 case SEN_name_to_handle_at:
208 case SEN_utimensat_time32:
209 case SEN_utimensat_time64:
211 return fdmatch(tcp, tcp->u_arg[0], set) ||
212 upathmatch(tcp, tcp->u_arg[1], set);
218 return upathmatch(tcp, tcp->u_arg[0], set) ||
219 upathmatch(tcp, tcp->u_arg[1], set);
224 return upathmatch(tcp, tcp->u_arg[1], set);
230 /* fd, path, fd, path */
231 return fdmatch(tcp, tcp->u_arg[0], set) ||
232 fdmatch(tcp, tcp->u_arg[2], set) ||
233 upathmatch(tcp, tcp->u_arg[1], set) ||
234 upathmatch(tcp, tcp->u_arg[3], set);
236 #if HAVE_ARCH_OLD_MMAP
238 # if HAVE_ARCH_OLD_MMAP_PGOFF
239 case SEN_old_mmap_pgoff:
242 kernel_ulong_t *args =
243 fetch_indirect_syscall_args(tcp, tcp->u_arg[0], 6);
245 return args && fdmatch(tcp, args[4], set);
247 #endif /* HAVE_ARCH_OLD_MMAP */
254 return fdmatch(tcp, tcp->u_arg[4], set);
258 return fdmatch(tcp, tcp->u_arg[1], set) ||
259 upathmatch(tcp, tcp->u_arg[2], set);
261 case SEN_copy_file_range:
263 /* fd, x, fd, x, x, x */
264 return fdmatch(tcp, tcp->u_arg[0], set) ||
265 fdmatch(tcp, tcp->u_arg[2], set);
269 return fdmatch(tcp, tcp->u_arg[2], set);
272 case SEN_fanotify_mark:
274 /* x, x, mask (64 bit), fd, path */
275 unsigned long long mask = 0;
276 int argn = getllval(tcp, &mask, 2);
277 return fdmatch(tcp, tcp->u_arg[argn], set) ||
278 upathmatch(tcp, tcp->u_arg[argn + 1], set);
280 #if HAVE_ARCH_OLD_SELECT
283 kernel_ulong_t *args =
284 fetch_indirect_syscall_args(tcp, tcp->u_arg[0], 5);
286 return args && match_xselect_args(tcp, args, set);
289 case SEN_pselect6_time32:
290 case SEN_pselect6_time64:
292 return match_xselect_args(tcp, tcp->u_arg, set);
293 case SEN_poll_time32:
294 case SEN_poll_time64:
295 case SEN_ppoll_time32:
296 case SEN_ppoll_time64:
300 kernel_ulong_t start, cur, end;
302 start = tcp->u_arg[0];
303 nfds = tcp->u_arg[1];
305 if (nfds > 1024 * 1024)
307 end = start + sizeof(fds) * nfds;
309 if (nfds == 0 || end < start)
312 for (cur = start; cur < end; cur += sizeof(fds)) {
313 if (umove(tcp, cur, &fds))
315 if (fdmatch(tcp, fds.fd, set))
323 /* x, x, x, maybe path, maybe fd */
324 const unsigned int cmd = tcp->u_arg[1];
326 case 3 /* FSCONFIG_SET_PATH */:
327 case 4 /* FSCONFIG_SET_PATH_EMPTY */:
328 return fdmatch(tcp, tcp->u_arg[4], set) ||
329 upathmatch(tcp, tcp->u_arg[3], set);
330 case 5 /* FSCONFIG_SET_FD */:
331 return fdmatch(tcp, tcp->u_arg[4], set);
340 case SEN_epoll_create:
341 case SEN_epoll_create1:
344 case SEN_fanotify_init:
347 case SEN_inotify_init:
348 case SEN_inotify_init1:
349 case SEN_io_uring_enter:
350 case SEN_io_uring_register:
351 case SEN_io_uring_setup:
352 case SEN_memfd_create:
353 case SEN_mq_getsetattr:
356 case SEN_mq_timedreceive_time32:
357 case SEN_mq_timedreceive_time64:
358 case SEN_mq_timedsend_time32:
359 case SEN_mq_timedsend_time64:
360 case SEN_perf_event_open:
369 case SEN_timerfd_create:
370 case SEN_timerfd_gettime32:
371 case SEN_timerfd_gettime64:
372 case SEN_timerfd_settime32:
373 case SEN_timerfd_settime64:
374 case SEN_userfaultfd:
376 * These have TRACE_FILE or TRACE_DESCRIPTOR or TRACE_NETWORK set,
377 * but they don't have any file descriptor or path args to test.
383 * Our fallback position for calls that haven't already
384 * been handled is to just check arg[0].
387 if (s->sys_flags & TRACE_FILE)
388 return upathmatch(tcp, tcp->u_arg[0], set);
390 if (s->sys_flags & (TRACE_DESC | TRACE_NETWORK))
391 return fdmatch(tcp, tcp->u_arg[0], set);