/*
- * 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 "defs.h"
#include <sys/param.h>
-#ifdef HAVE_POLL_H
-# include <poll.h>
-#endif
-#ifdef HAVE_SYS_POLL_H
-# include <sys/poll.h>
-#endif
+#include <poll.h>
#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 *tcp, unsigned long 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);
}
/*
* Add a path to the set we're tracing.
- * Secifying NULL will delete all paths.
+ * 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 = realloc(paths_selected, num_selected * sizeof(paths_selected[0]));
- if (!paths_selected)
- die_out_of_memory();
- 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;
}
/*
* 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);
return;
}
- fprintf(stderr, "Requested path '%s' resolved into '%s'\n",
- path, rpath);
- storepath(rpath);
+ error_msg("Requested path '%s' resolved into '%s'", path, 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)))
- return 0;
+ if (!(s->sys_flags & (TRACE_FILE | TRACE_DESC | TRACE_NETWORK)))
+ return false;
/*
* Check for special cases where we need to do something
* other than test arg[0].
*/
- if (s->sys_func == sys_dup2 ||
- s->sys_func == sys_dup3 ||
- s->sys_func == sys_sendfile ||
- s->sys_func == sys_sendfile64 ||
- s->sys_func == sys_tee)
- {
+ switch (s->sen) {
+ case SEN_dup2:
+ case SEN_dup3:
+ case SEN_kexec_file_load:
+ case SEN_sendfile:
+ case SEN_sendfile64:
+ case SEN_tee:
/* fd, fd */
- return fdmatch(tcp, tcp->u_arg[0]) ||
- fdmatch(tcp, tcp->u_arg[1]);
- }
-
- if (s->sys_func == sys_inotify_add_watch ||
- s->sys_func == sys_faccessat ||
- s->sys_func == sys_fchmodat ||
- s->sys_func == sys_futimesat ||
- s->sys_func == sys_mkdirat ||
- s->sys_func == sys_unlinkat ||
- s->sys_func == sys_newfstatat ||
- s->sys_func == sys_mknodat ||
- s->sys_func == sys_openat ||
- s->sys_func == sys_readlinkat ||
- s->sys_func == sys_utimensat ||
- s->sys_func == sys_fchownat ||
- s->sys_func == sys_pipe2)
- {
+ 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:
+ case SEN_fstatat64:
+ case SEN_futimesat:
+ case SEN_inotify_add_watch:
+ case SEN_mkdirat:
+ case SEN_mknodat:
+ case SEN_name_to_handle_at:
+ 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);
- if (s->sys_func == sys_link ||
- s->sys_func == sys_mount)
- {
+ 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);
- if (s->sys_func == sys_quotactl)
- {
+ case SEN_quotactl:
+ case SEN_symlink:
/* x, path */
- return upathmatch(tcp, tcp->u_arg[1]);
- }
+ return upathmatch(tcp, tcp->u_arg[1], set);
- if (s->sys_func == sys_renameat ||
- s->sys_func == sys_linkat)
- {
+ 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);
- if (
- s->sys_func == sys_old_mmap ||
+ case SEN_old_mmap:
#if defined(S390)
- s->sys_func == sys_old_mmap_pgoff ||
+ case SEN_old_mmap_pgoff:
#endif
- s->sys_func == sys_mmap ||
- s->sys_func == sys_mmap_pgoff ||
- s->sys_func == sys_mmap_4koff
- ) {
+ case SEN_mmap:
+ case SEN_mmap_4koff:
+ 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);
- if (s->sys_func == sys_symlinkat) {
- /* path, fd, path */
- return fdmatch(tcp, tcp->u_arg[1]) ||
- upathmatch(tcp, tcp->u_arg[0]) ||
- upathmatch(tcp, tcp->u_arg[2]);
- }
+ case SEN_symlinkat:
+ /* x, fd, path */
+ return fdmatch(tcp, tcp->u_arg[1], set) ||
+ upathmatch(tcp, tcp->u_arg[2], set);
- if (s->sys_func == sys_splice) {
- /* fd, x, fd, x, x */
- return fdmatch(tcp, tcp->u_arg[0]) ||
- fdmatch(tcp, tcp->u_arg[2]);
- }
+ case SEN_copy_file_range:
+ case SEN_splice:
+ /* fd, x, fd, x, x, x */
+ return fdmatch(tcp, tcp->u_arg[0], set) ||
+ fdmatch(tcp, tcp->u_arg[2], set);
- if (s->sys_func == sys_epoll_ctl) {
+ case SEN_epoll_ctl:
/* x, x, fd, x */
- return fdmatch(tcp, tcp->u_arg[2]);
- }
+ return fdmatch(tcp, tcp->u_arg[2], set);
- if (s->sys_func == sys_select ||
- s->sys_func == sys_oldselect ||
- s->sys_func == sys_pselect6)
+
+ case SEN_fanotify_mark:
+ {
+ /* 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:
{
int i, j;
- unsigned nfds;
- long *args, oldargs[5];
- unsigned fdsize;
+ int nfds;
+ kernel_ulong_t *args;
+ kernel_ulong_t select_args[5];
+ unsigned int oldselect_args[5];
+ unsigned int fdsize;
fd_set *fds;
- if (s->sys_func == sys_oldselect) {
- if (umoven(tcp, tcp->u_arg[0], sizeof oldargs,
- (char*) oldargs) < 0)
- {
- fprintf(stderr, "umoven() failed\n");
- return 0;
+ if (SEN_oldselect == s->sen) {
+ if (sizeof(*select_args) == sizeof(*oldselect_args)) {
+ if (umove(tcp, tcp->u_arg[0], &select_args)) {
+ return false;
+ }
+ } else {
+ unsigned int n;
+
+ if (umove(tcp, tcp->u_arg[0], &oldselect_args)) {
+ return false;
+ }
+
+ for (n = 0; n < 5; ++n) {
+ select_args[n] = oldselect_args[n];
+ }
}
- args = oldargs;
- } else
+ args = select_args;
+ } else {
args = tcp->u_arg;
+ }
- nfds = args[0];
+ /* Kernel truncates arg[0] to int, we do the same. */
+ nfds = (int) args[0];
+ /* Kernel rejects negative nfds, so we don't parse it either. */
+ if (nfds <= 0)
+ return false;
/* Beware of select(2^31-1, NULL, NULL, NULL) and similar... */
- if (args[0] > 1024*1024)
+ if (nfds > 1024*1024)
nfds = 1024*1024;
- if (args[0] < 0)
- nfds = 0;
- fdsize = ((((nfds + 7) / 8) + sizeof(long) - 1)
- & -sizeof(long));
- fds = malloc(fdsize);
- if (!fds)
- die_out_of_memory();
+ fdsize = (((nfds + 7) / 8) + current_wordsize-1) & -current_wordsize;
+ fds = xmalloc(fdsize);
for (i = 1; i <= 3; ++i) {
if (args[i] == 0)
continue;
-
- if (umoven(tcp, args[i], fdsize, (char *) fds) < 0) {
- fprintf(stderr, "umoven() failed\n");
+ if (umoven(tcp, args[i], fdsize, fds) < 0) {
continue;
}
-
- for (j = 0; j < nfds; ++j)
- if (FD_ISSET(j, fds) && fdmatch(tcp, j)) {
+ for (j = 0;; j++) {
+ j = next_set_bit(fds, j, nfds);
+ if (j < 0)
+ break;
+ if (fdmatch(tcp, j, set)) {
free(fds);
- return 1;
+ return true;
}
+ }
}
free(fds);
- return 0;
+ return false;
}
- if (s->sys_func == sys_poll ||
- s->sys_func == sys_ppoll)
+ case SEN_poll:
+ case SEN_ppoll:
{
struct pollfd fds;
unsigned nfds;
- unsigned long start, cur, end;
+ kernel_ulong_t start, cur, end;
start = tcp->u_arg[0];
nfds = tcp->u_arg[1];
end = start + sizeof(fds) * nfds;
if (nfds == 0 || end < start)
- return 0;
+ return false;
for (cur = start; cur < end; cur += sizeof(fds))
- if ((umoven(tcp, cur, sizeof fds, (char *) &fds) == 0)
- && fdmatch(tcp, fds.fd))
- return 1;
+ if ((umove(tcp, cur, &fds) == 0)
+ && fdmatch(tcp, fds.fd, set))
+ return true;
- return 0;
+ return false;
}
- if (s->sys_func == printargs ||
- s->sys_func == sys_pipe ||
- s->sys_func == sys_pipe2 ||
- s->sys_func == sys_eventfd2 ||
- s->sys_func == sys_eventfd ||
- s->sys_func == sys_inotify_init1 ||
- s->sys_func == sys_timerfd_create ||
- s->sys_func == sys_timerfd_settime ||
- s->sys_func == sys_timerfd_gettime ||
- s->sys_func == sys_epoll_create ||
- strcmp(s->sys_name, "fanotify_init") == 0)
- {
+ case SEN_bpf:
+ case SEN_epoll_create:
+ case SEN_epoll_create1:
+ 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:
+ case SEN_pipe:
+ case SEN_pipe2:
+ case SEN_printargs:
+ case SEN_socket:
+ case SEN_socketpair:
+ case SEN_timerfd_create:
+ case SEN_timerfd_gettime:
+ case SEN_timerfd_settime:
+ case SEN_userfaultfd:
/*
- * These have TRACE_FILE or TRACE_DESCRIPTOR set, but they
- * don't have any file descriptor or path args to test.
+ * 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;
}
/*
*/
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)
- return fdmatch(tcp, tcp->u_arg[0]);
+ if (s->sys_flags & (TRACE_DESC | TRACE_NETWORK))
+ return fdmatch(tcp, tcp->u_arg[0], set);
- return 0;
+ return false;
}