]> granicus.if.org Git - strace/blobdiff - pathtrace.c
netlink: add a basic rtnetlink parser of nsid messages
[strace] / pathtrace.c
index d991aeda4072466ecff0155615879ca330198d5e..4376b6c93451ea62a056416611128a345cf0a100 100644 (file)
@@ -1,5 +1,6 @@
 /*
- * Copyright (c) 2011, Comtrol Corp.
+ * Copyright (c) 2011 Comtrol Corp.
+ * Copyright (c) 2011-2017 The strace developers.
  * All rights reserved.
  *
  * Redistribution and use in source and binary forms, with or without
 
 #include "syscall.h"
 
-const char **paths_selected = NULL;
-static unsigned num_selected = 0;
+struct path_set global_path_set;
 
 /*
  * Return true if specified path matches one that we're tracing.
  */
-static int
-pathmatch(const char *path)
+static bool
+pathmatch(const char *path, struct path_set *set)
 {
        unsigned i;
 
-       for (i = 0; i < num_selected; ++i) {
-               if (strcmp(path, paths_selected[i]) == 0)
-                       return 1;
+       for (i = 0; i < set->num_selected; ++i) {
+               if (strcmp(path, set->paths_selected[i]) == 0)
+                       return true;
        }
-       return 0;
+       return false;
 }
 
 /*
  * Return true if specified path (in user-space) matches.
  */
-static int
-upathmatch(struct tcb *const tcp, const kernel_ulong_t upath)
+static bool
+upathmatch(struct tcb *const tcp, const kernel_ulong_t upath,
+          struct path_set *set)
 {
        char path[PATH_MAX + 1];
 
-       return umovestr(tcp, upath, sizeof path, path) > 0 &&
-               pathmatch(path);
+       return umovestr(tcp, upath, sizeof(path), path) > 0 &&
+               pathmatch(path, set);
 }
 
 /*
  * Return true if specified fd maps to a path we're tracing.
  */
-static int
-fdmatch(struct tcb *tcp, int fd)
+static bool
+fdmatch(struct tcb *tcp, int fd, struct path_set *set)
 {
        char path[PATH_MAX + 1];
        int n = getfdpath(tcp, fd, path, sizeof(path));
 
-       return n >= 0 && pathmatch(path);
+       return n >= 0 && pathmatch(path, set);
 }
 
 /*
@@ -79,17 +80,18 @@ fdmatch(struct tcb *tcp, int fd)
  * Specifying NULL will delete all paths.
  */
 static void
-storepath(const char *path)
+storepath(const char *path, struct path_set *set)
 {
        unsigned i;
 
-       if (pathmatch(path))
+       if (pathmatch(path, set))
                return; /* already in table */
 
-       i = num_selected++;
-       paths_selected = xreallocarray(paths_selected, num_selected,
-                                      sizeof(paths_selected[0]));
-       paths_selected[i] = path;
+       i = set->num_selected++;
+       set->paths_selected = xreallocarray(set->paths_selected,
+                                           set->num_selected,
+                                           sizeof(set->paths_selected[0]));
+       set->paths_selected[i] = path;
 }
 
 /*
@@ -120,11 +122,11 @@ getfdpath(struct tcb *tcp, int fd, char *buf, unsigned bufsize)
  * version of the path.  Secifying NULL will delete all paths.
  */
 void
-pathtrace_select(const char *path)
+pathtrace_select_set(const char *path, struct path_set *set)
 {
        char *rpath;
 
-       storepath(path);
+       storepath(path, set);
 
        rpath = realpath(path, NULL);
 
@@ -138,22 +140,22 @@ pathtrace_select(const char *path)
        }
 
        error_msg("Requested path '%s' resolved into '%s'", path, rpath);
-       storepath(rpath);
+       storepath(rpath, set);
 }
 
 /*
  * Return true if syscall accesses a selected path
  * (or if no paths have been specified for tracing).
  */
-int
-pathtrace_match(struct tcb *tcp)
+bool
+pathtrace_match_set(struct tcb *tcp, struct path_set *set)
 {
        const struct_sysent *s;
 
        s = tcp->s_ent;
 
        if (!(s->sys_flags & (TRACE_FILE | TRACE_DESC | TRACE_NETWORK)))
-               return 0;
+               return false;
 
        /*
         * Check for special cases where we need to do something
@@ -168,9 +170,10 @@ pathtrace_match(struct tcb *tcp)
        case SEN_sendfile64:
        case SEN_tee:
                /* fd, fd */
-               return fdmatch(tcp, tcp->u_arg[0]) ||
-                       fdmatch(tcp, tcp->u_arg[1]);
+               return fdmatch(tcp, tcp->u_arg[0], set) ||
+                       fdmatch(tcp, tcp->u_arg[1], set);
 
+       case SEN_execveat:
        case SEN_faccessat:
        case SEN_fchmodat:
        case SEN_fchownat:
@@ -183,31 +186,33 @@ pathtrace_match(struct tcb *tcp)
        case SEN_newfstatat:
        case SEN_openat:
        case SEN_readlinkat:
+       case SEN_statx:
        case SEN_unlinkat:
        case SEN_utimensat:
                /* fd, path */
-               return fdmatch(tcp, tcp->u_arg[0]) ||
-                       upathmatch(tcp, tcp->u_arg[1]);
+               return fdmatch(tcp, tcp->u_arg[0], set) ||
+                       upathmatch(tcp, tcp->u_arg[1], set);
 
        case SEN_link:
        case SEN_mount:
        case SEN_pivotroot:
                /* path, path */
-               return upathmatch(tcp, tcp->u_arg[0]) ||
-                       upathmatch(tcp, tcp->u_arg[1]);
+               return upathmatch(tcp, tcp->u_arg[0], set) ||
+                       upathmatch(tcp, tcp->u_arg[1], set);
 
        case SEN_quotactl:
+       case SEN_symlink:
                /* x, path */
-               return upathmatch(tcp, tcp->u_arg[1]);
+               return upathmatch(tcp, tcp->u_arg[1], set);
 
        case SEN_linkat:
        case SEN_renameat2:
        case SEN_renameat:
                /* fd, path, fd, path */
-               return fdmatch(tcp, tcp->u_arg[0]) ||
-                       fdmatch(tcp, tcp->u_arg[2]) ||
-                       upathmatch(tcp, tcp->u_arg[1]) ||
-                       upathmatch(tcp, tcp->u_arg[3]);
+               return fdmatch(tcp, tcp->u_arg[0], set) ||
+                       fdmatch(tcp, tcp->u_arg[2], set) ||
+                       upathmatch(tcp, tcp->u_arg[1], set) ||
+                       upathmatch(tcp, tcp->u_arg[3], set);
 
        case SEN_old_mmap:
 #if defined(S390)
@@ -218,30 +223,32 @@ pathtrace_match(struct tcb *tcp)
        case SEN_mmap_pgoff:
        case SEN_ARCH_mmap:
                /* x, x, x, x, fd */
-               return fdmatch(tcp, tcp->u_arg[4]);
+               return fdmatch(tcp, tcp->u_arg[4], set);
 
        case SEN_symlinkat:
-               /* path, fd, path */
-               return fdmatch(tcp, tcp->u_arg[1]) ||
-                       upathmatch(tcp, tcp->u_arg[0]) ||
-                       upathmatch(tcp, tcp->u_arg[2]);
+               /* x, fd, path */
+               return fdmatch(tcp, tcp->u_arg[1], set) ||
+                       upathmatch(tcp, tcp->u_arg[2], set);
 
        case SEN_copy_file_range:
        case SEN_splice:
                /* fd, x, fd, x, x, x */
-               return fdmatch(tcp, tcp->u_arg[0]) ||
-                       fdmatch(tcp, tcp->u_arg[2]);
+               return fdmatch(tcp, tcp->u_arg[0], set) ||
+                       fdmatch(tcp, tcp->u_arg[2], set);
 
        case SEN_epoll_ctl:
                /* x, x, fd, x */
-               return fdmatch(tcp, tcp->u_arg[2]);
+               return fdmatch(tcp, tcp->u_arg[2], set);
 
 
        case SEN_fanotify_mark:
-               /* x, x, x, fd, path */
-               return fdmatch(tcp, tcp->u_arg[3]) ||
-                       upathmatch(tcp, tcp->u_arg[4]);
-
+       {
+               /* x, x, mask (64 bit), fd, path */
+               unsigned long long mask = 0;
+               int argn = getllval(tcp, &mask, 2);
+               return fdmatch(tcp, tcp->u_arg[argn], set) ||
+                       upathmatch(tcp, tcp->u_arg[argn + 1], set);
+       }
        case SEN_oldselect:
        case SEN_pselect6:
        case SEN_select:
@@ -257,13 +264,13 @@ pathtrace_match(struct tcb *tcp)
                if (SEN_oldselect == s->sen) {
                        if (sizeof(*select_args) == sizeof(*oldselect_args)) {
                                if (umove(tcp, tcp->u_arg[0], &select_args)) {
-                                       return 0;
+                                       return false;
                                }
                        } else {
                                unsigned int n;
 
                                if (umove(tcp, tcp->u_arg[0], &oldselect_args)) {
-                                       return 0;
+                                       return false;
                                }
 
                                for (n = 0; n < 5; ++n) {
@@ -279,7 +286,7 @@ pathtrace_match(struct tcb *tcp)
                nfds = (int) args[0];
                /* Kernel rejects negative nfds, so we don't parse it either. */
                if (nfds <= 0)
-                       return 0;
+                       return false;
                /* Beware of select(2^31-1, NULL, NULL, NULL) and similar... */
                if (nfds > 1024*1024)
                        nfds = 1024*1024;
@@ -296,14 +303,14 @@ pathtrace_match(struct tcb *tcp)
                                j = next_set_bit(fds, j, nfds);
                                if (j < 0)
                                        break;
-                               if (fdmatch(tcp, j)) {
+                               if (fdmatch(tcp, j, set)) {
                                        free(fds);
-                                       return 1;
+                                       return true;
                                }
                        }
                }
                free(fds);
-               return 0;
+               return false;
        }
 
        case SEN_poll:
@@ -319,14 +326,14 @@ pathtrace_match(struct tcb *tcp)
                end = start + sizeof(fds) * nfds;
 
                if (nfds == 0 || end < start)
-                       return 0;
+                       return false;
 
                for (cur = start; cur < end; cur += sizeof(fds))
                        if ((umove(tcp, cur, &fds) == 0)
-                           && fdmatch(tcp, fds.fd))
-                               return 1;
+                           && fdmatch(tcp, fds.fd, set))
+                               return true;
 
-               return 0;
+               return false;
        }
 
        case SEN_bpf:
@@ -335,6 +342,7 @@ pathtrace_match(struct tcb *tcp)
        case SEN_eventfd2:
        case SEN_eventfd:
        case SEN_fanotify_init:
+       case SEN_inotify_init:
        case SEN_inotify_init1:
        case SEN_memfd_create:
        case SEN_perf_event_open:
@@ -351,7 +359,7 @@ pathtrace_match(struct tcb *tcp)
                 * These have TRACE_FILE or TRACE_DESCRIPTOR or TRACE_NETWORK set,
                 * but they don't have any file descriptor or path args to test.
                 */
-               return 0;
+               return false;
        }
 
        /*
@@ -360,10 +368,10 @@ pathtrace_match(struct tcb *tcp)
         */
 
        if (s->sys_flags & TRACE_FILE)
-               return upathmatch(tcp, tcp->u_arg[0]);
+               return upathmatch(tcp, tcp->u_arg[0], set);
 
        if (s->sys_flags & (TRACE_DESC | TRACE_NETWORK))
-               return fdmatch(tcp, tcp->u_arg[0]);
+               return fdmatch(tcp, tcp->u_arg[0], set);
 
-       return 0;
+       return false;
 }