2 * Copyright (c) 2011 Comtrol Corp.
3 * Copyright (c) 2011-2017 The strace developers.
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 * 3. The name of the author may not be used to endorse or promote products
15 * derived from this software without specific prior written permission.
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31 #include <sys/param.h>
36 struct path_set global_path_set;
39 * Return true if specified path matches one that we're tracing.
42 pathmatch(const char *path, struct path_set *set)
46 for (i = 0; i < set->num_selected; ++i) {
47 if (strcmp(path, set->paths_selected[i]) == 0)
54 * Return true if specified path (in user-space) matches.
57 upathmatch(struct tcb *const tcp, const kernel_ulong_t upath,
60 char path[PATH_MAX + 1];
62 return umovestr(tcp, upath, sizeof(path), path) > 0 &&
67 * Return true if specified fd maps to a path we're tracing.
70 fdmatch(struct tcb *tcp, int fd, struct path_set *set)
72 char path[PATH_MAX + 1];
73 int n = getfdpath(tcp, fd, path, sizeof(path));
75 return n >= 0 && pathmatch(path, set);
79 * Add a path to the set we're tracing.
80 * Specifying NULL will delete all paths.
83 storepath(const char *path, struct path_set *set)
87 if (pathmatch(path, set))
88 return; /* already in table */
90 i = set->num_selected++;
91 set->paths_selected = xreallocarray(set->paths_selected,
93 sizeof(set->paths_selected[0]));
94 set->paths_selected[i] = path;
98 * Get path associated with fd.
101 getfdpath(struct tcb *tcp, int fd, char *buf, unsigned bufsize)
103 char linkpath[sizeof("/proc/%u/fd/%u") + 2 * sizeof(int)*3];
109 sprintf(linkpath, "/proc/%u/fd/%u", tcp->pid, fd);
110 n = readlink(linkpath, buf, bufsize - 1);
112 * NB: if buf is too small, readlink doesn't fail,
113 * it returns truncated result (IOW: n == bufsize - 1).
121 * Add a path to the set we're tracing. Also add the canonicalized
122 * version of the path. Specifying NULL will delete all paths.
125 pathtrace_select_set(const char *path, struct path_set *set)
129 storepath(path, set);
131 rpath = realpath(path, NULL);
136 /* if realpath and specified path are same, we're done */
137 if (strcmp(path, rpath) == 0) {
142 error_msg("Requested path '%s' resolved into '%s'", path, rpath);
143 storepath(rpath, set);
147 * Return true if syscall accesses a selected path
148 * (or if no paths have been specified for tracing).
151 pathtrace_match_set(struct tcb *tcp, struct path_set *set)
153 const struct_sysent *s;
157 if (!(s->sys_flags & (TRACE_FILE | TRACE_DESC | TRACE_NETWORK)))
161 * Check for special cases where we need to do something
162 * other than test arg[0].
168 case SEN_kexec_file_load:
173 return fdmatch(tcp, tcp->u_arg[0], set) ||
174 fdmatch(tcp, tcp->u_arg[1], set);
182 case SEN_inotify_add_watch:
185 case SEN_name_to_handle_at:
193 return fdmatch(tcp, tcp->u_arg[0], set) ||
194 upathmatch(tcp, tcp->u_arg[1], set);
200 return upathmatch(tcp, tcp->u_arg[0], set) ||
201 upathmatch(tcp, tcp->u_arg[1], set);
206 return upathmatch(tcp, tcp->u_arg[1], set);
211 /* fd, path, fd, path */
212 return fdmatch(tcp, tcp->u_arg[0], set) ||
213 fdmatch(tcp, tcp->u_arg[2], set) ||
214 upathmatch(tcp, tcp->u_arg[1], set) ||
215 upathmatch(tcp, tcp->u_arg[3], set);
219 case SEN_old_mmap_pgoff:
226 return fdmatch(tcp, tcp->u_arg[4], set);
230 return fdmatch(tcp, tcp->u_arg[1], set) ||
231 upathmatch(tcp, tcp->u_arg[2], set);
233 case SEN_copy_file_range:
235 /* fd, x, fd, x, x, x */
236 return fdmatch(tcp, tcp->u_arg[0], set) ||
237 fdmatch(tcp, tcp->u_arg[2], set);
241 return fdmatch(tcp, tcp->u_arg[2], set);
244 case SEN_fanotify_mark:
246 /* x, x, mask (64 bit), fd, path */
247 unsigned long long mask = 0;
248 int argn = getllval(tcp, &mask, 2);
249 return fdmatch(tcp, tcp->u_arg[argn], set) ||
250 upathmatch(tcp, tcp->u_arg[argn + 1], set);
258 kernel_ulong_t *args;
259 kernel_ulong_t select_args[5];
260 unsigned int oldselect_args[5];
264 if (SEN_oldselect == s->sen) {
265 if (sizeof(*select_args) == sizeof(*oldselect_args)) {
266 if (umove(tcp, tcp->u_arg[0], &select_args)) {
272 if (umove(tcp, tcp->u_arg[0], &oldselect_args)) {
276 for (n = 0; n < 5; ++n) {
277 select_args[n] = oldselect_args[n];
285 /* Kernel truncates arg[0] to int, we do the same. */
286 nfds = (int) args[0];
287 /* Kernel rejects negative nfds, so we don't parse it either. */
290 /* Beware of select(2^31-1, NULL, NULL, NULL) and similar... */
291 if (nfds > 1024*1024)
293 fdsize = (((nfds + 7) / 8) + current_wordsize-1) & -current_wordsize;
294 fds = xmalloc(fdsize);
296 for (i = 1; i <= 3; ++i) {
299 if (umoven(tcp, args[i], fdsize, fds) < 0) {
303 j = next_set_bit(fds, j, nfds);
306 if (fdmatch(tcp, j, set)) {
321 kernel_ulong_t start, cur, end;
323 start = tcp->u_arg[0];
324 nfds = tcp->u_arg[1];
326 if (nfds > 1024 * 1024)
328 end = start + sizeof(fds) * nfds;
330 if (nfds == 0 || end < start)
333 for (cur = start; cur < end; cur += sizeof(fds)) {
334 if (umove(tcp, cur, &fds))
336 if (fdmatch(tcp, fds.fd, set))
344 case SEN_epoll_create:
345 case SEN_epoll_create1:
348 case SEN_fanotify_init:
349 case SEN_inotify_init:
350 case SEN_inotify_init1:
351 case SEN_memfd_create:
352 case SEN_perf_event_open:
358 case SEN_timerfd_create:
359 case SEN_timerfd_gettime:
360 case SEN_timerfd_settime:
361 case SEN_userfaultfd:
363 * These have TRACE_FILE or TRACE_DESCRIPTOR or TRACE_NETWORK set,
364 * but they don't have any file descriptor or path args to test.
370 * Our fallback position for calls that haven't already
371 * been handled is to just check arg[0].
374 if (s->sys_flags & TRACE_FILE)
375 return upathmatch(tcp, tcp->u_arg[0], set);
377 if (s->sys_flags & (TRACE_DESC | TRACE_NETWORK))
378 return fdmatch(tcp, tcp->u_arg[0], set);