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]);
229 /* fd, x, fd, x, x */
230 return fdmatch(tcp, tcp->u_arg[0]) ||
231 fdmatch(tcp, tcp->u_arg[2]);
235 return fdmatch(tcp, tcp->u_arg[2]);
238 case SEN_fanotify_mark:
239 /* x, x, x, fd, path */
240 return fdmatch(tcp, tcp->u_arg[3]) ||
241 upathmatch(tcp, tcp->u_arg[4]);
249 long *args, oldargs[5];
254 if (SEN_oldselect == s->sen) {
255 if (umoven(tcp, tcp->u_arg[0], sizeof oldargs,
258 error_msg("umoven() failed");
264 /* Kernel truncates arg[0] to int, we do the same. */
265 nfds = (int) args[0];
266 /* Kernel rejects negative nfds, so we don't parse it either. */
269 /* Beware of select(2^31-1, NULL, NULL, NULL) and similar... */
270 if (nfds > 1024*1024)
272 fdsize = (((nfds + 7) / 8) + current_wordsize-1) & -current_wordsize;
273 fds = xmalloc(fdsize);
275 for (i = 1; i <= 3; ++i) {
278 if (umoven(tcp, args[i], fdsize, fds) < 0) {
279 error_msg("umoven() failed");
283 j = next_set_bit(fds, j, nfds);
286 if (fdmatch(tcp, j)) {
301 unsigned long start, cur, end;
303 start = tcp->u_arg[0];
304 nfds = tcp->u_arg[1];
306 end = start + sizeof(fds) * nfds;
308 if (nfds == 0 || end < start)
311 for (cur = start; cur < end; cur += sizeof(fds))
312 if ((umoven(tcp, cur, sizeof fds, &fds) == 0)
313 && fdmatch(tcp, fds.fd))
320 case SEN_epoll_create:
321 case SEN_epoll_create1:
324 case SEN_fanotify_init:
325 case SEN_inotify_init1:
326 case SEN_memfd_create:
327 case SEN_perf_event_open:
333 case SEN_timerfd_create:
334 case SEN_timerfd_gettime:
335 case SEN_timerfd_settime:
336 case SEN_userfaultfd:
338 * These have TRACE_FILE or TRACE_DESCRIPTOR or TRACE_NETWORK set,
339 * but they don't have any file descriptor or path args to test.
345 * Our fallback position for calls that haven't already
346 * been handled is to just check arg[0].
349 if (s->sys_flags & TRACE_FILE)
350 return upathmatch(tcp, tcp->u_arg[0]);
352 if (s->sys_flags & (TRACE_DESC | TRACE_NETWORK))
353 return fdmatch(tcp, tcp->u_arg[0]);