2 * Copyright (c) 2011, Comtrol Corp.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * 3. The name of the author may not be used to endorse or promote products
14 * derived from this software without specific prior written permission.
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
17 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
18 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
19 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
20 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
21 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
22 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
23 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
25 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30 #include <sys/param.h>
35 const char **paths_selected = NULL;
36 static unsigned num_selected = 0;
39 * Return true if specified path matches one that we're tracing.
42 pathmatch(const char *path)
46 for (i = 0; i < num_selected; ++i) {
47 if (strcmp(path, paths_selected[i]) == 0)
54 * Return true if specified path (in user-space) matches.
57 upathmatch(struct tcb *tcp, unsigned long upath)
59 char path[PATH_MAX + 1];
61 return umovestr(tcp, upath, sizeof path, path) > 0 &&
66 * Return true if specified fd maps to a path we're tracing.
69 fdmatch(struct tcb *tcp, int fd)
71 char path[PATH_MAX + 1];
72 int n = getfdpath(tcp, fd, path, sizeof(path));
74 return n >= 0 && pathmatch(path);
78 * Add a path to the set we're tracing.
79 * Specifying NULL will delete all paths.
82 storepath(const char *path)
87 return; /* already in table */
90 paths_selected = xreallocarray(paths_selected, num_selected,
91 sizeof(paths_selected[0]));
92 paths_selected[i] = path;
96 * Get path associated with fd.
99 getfdpath(struct tcb *tcp, int fd, char *buf, unsigned bufsize)
101 char linkpath[sizeof("/proc/%u/fd/%u") + 2 * sizeof(int)*3];
107 sprintf(linkpath, "/proc/%u/fd/%u", tcp->pid, fd);
108 n = readlink(linkpath, buf, bufsize - 1);
110 * NB: if buf is too small, readlink doesn't fail,
111 * it returns truncated result (IOW: n == bufsize - 1).
119 * Add a path to the set we're tracing. Also add the canonicalized
120 * version of the path. Secifying NULL will delete all paths.
123 pathtrace_select(const char *path)
129 rpath = realpath(path, NULL);
134 /* if realpath and specified path are same, we're done */
135 if (strcmp(path, rpath) == 0) {
140 error_msg("Requested path '%s' resolved into '%s'", path, rpath);
145 * Return true if syscall accesses a selected path
146 * (or if no paths have been specified for tracing).
149 pathtrace_match(struct tcb *tcp)
151 const struct_sysent *s;
155 if (!(s->sys_flags & (TRACE_FILE | TRACE_DESC | TRACE_NETWORK)))
159 * Check for special cases where we need to do something
160 * other than test arg[0].
166 case SEN_kexec_file_load:
171 return fdmatch(tcp, tcp->u_arg[0]) ||
172 fdmatch(tcp, tcp->u_arg[1]);
178 case SEN_inotify_add_watch:
181 case SEN_name_to_handle_at:
188 return fdmatch(tcp, tcp->u_arg[0]) ||
189 upathmatch(tcp, tcp->u_arg[1]);
195 return upathmatch(tcp, tcp->u_arg[0]) ||
196 upathmatch(tcp, tcp->u_arg[1]);
200 return upathmatch(tcp, tcp->u_arg[1]);
205 /* fd, path, fd, path */
206 return fdmatch(tcp, tcp->u_arg[0]) ||
207 fdmatch(tcp, tcp->u_arg[2]) ||
208 upathmatch(tcp, tcp->u_arg[1]) ||
209 upathmatch(tcp, tcp->u_arg[3]);
213 case SEN_old_mmap_pgoff:
220 return fdmatch(tcp, tcp->u_arg[4]);
224 return fdmatch(tcp, tcp->u_arg[1]) ||
225 upathmatch(tcp, tcp->u_arg[0]) ||
226 upathmatch(tcp, tcp->u_arg[2]);
228 case SEN_copy_file_range:
230 /* fd, x, fd, x, x, x */
231 return fdmatch(tcp, tcp->u_arg[0]) ||
232 fdmatch(tcp, tcp->u_arg[2]);
236 return fdmatch(tcp, tcp->u_arg[2]);
239 case SEN_fanotify_mark:
240 /* x, x, x, fd, path */
241 return fdmatch(tcp, tcp->u_arg[3]) ||
242 upathmatch(tcp, tcp->u_arg[4]);
250 long *args, oldargs[5];
255 if (SEN_oldselect == s->sen) {
256 if (umoven(tcp, tcp->u_arg[0], sizeof oldargs,
259 error_msg("umoven() failed");
265 /* Kernel truncates arg[0] to int, we do the same. */
266 nfds = (int) args[0];
267 /* Kernel rejects negative nfds, so we don't parse it either. */
270 /* Beware of select(2^31-1, NULL, NULL, NULL) and similar... */
271 if (nfds > 1024*1024)
273 fdsize = (((nfds + 7) / 8) + current_wordsize-1) & -current_wordsize;
274 fds = xmalloc(fdsize);
276 for (i = 1; i <= 3; ++i) {
279 if (umoven(tcp, args[i], fdsize, fds) < 0) {
280 error_msg("umoven() failed");
284 j = next_set_bit(fds, j, nfds);
287 if (fdmatch(tcp, j)) {
302 unsigned long start, cur, end;
304 start = tcp->u_arg[0];
305 nfds = tcp->u_arg[1];
307 end = start + sizeof(fds) * nfds;
309 if (nfds == 0 || end < start)
312 for (cur = start; cur < end; cur += sizeof(fds))
313 if ((umoven(tcp, cur, sizeof fds, &fds) == 0)
314 && fdmatch(tcp, fds.fd))
321 case SEN_epoll_create:
322 case SEN_epoll_create1:
325 case SEN_fanotify_init:
326 case SEN_inotify_init1:
327 case SEN_memfd_create:
328 case SEN_perf_event_open:
334 case SEN_timerfd_create:
335 case SEN_timerfd_gettime:
336 case SEN_timerfd_settime:
337 case SEN_userfaultfd:
339 * These have TRACE_FILE or TRACE_DESCRIPTOR or TRACE_NETWORK set,
340 * but they don't have any file descriptor or path args to test.
346 * Our fallback position for calls that haven't already
347 * been handled is to just check arg[0].
350 if (s->sys_flags & TRACE_FILE)
351 return upathmatch(tcp, tcp->u_arg[0]);
353 if (s->sys_flags & (TRACE_DESC | TRACE_NETWORK))
354 return fdmatch(tcp, tcp->u_arg[0]);