]> granicus.if.org Git - strace/blob - pathtrace.c
tests: improve setugid error diagnostics a bit
[strace] / pathtrace.c
1 /*
2  * Copyright (c) 2011 Comtrol Corp.
3  * Copyright (c) 2011-2017 The strace developers.
4  * All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
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.
16  *
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.
27  *
28  */
29
30 #include "defs.h"
31 #include <sys/param.h>
32 #include <poll.h>
33
34 #include "syscall.h"
35 #include "xstring.h"
36
37 struct path_set global_path_set;
38
39 /*
40  * Return true if specified path matches one that we're tracing.
41  */
42 static bool
43 pathmatch(const char *path, struct path_set *set)
44 {
45         unsigned i;
46
47         for (i = 0; i < set->num_selected; ++i) {
48                 if (strcmp(path, set->paths_selected[i]) == 0)
49                         return true;
50         }
51         return false;
52 }
53
54 /*
55  * Return true if specified path (in user-space) matches.
56  */
57 static bool
58 upathmatch(struct tcb *const tcp, const kernel_ulong_t upath,
59            struct path_set *set)
60 {
61         char path[PATH_MAX + 1];
62
63         return umovestr(tcp, upath, sizeof(path), path) > 0 &&
64                 pathmatch(path, set);
65 }
66
67 /*
68  * Return true if specified fd maps to a path we're tracing.
69  */
70 static bool
71 fdmatch(struct tcb *tcp, int fd, struct path_set *set)
72 {
73         char path[PATH_MAX + 1];
74         int n = getfdpath(tcp, fd, path, sizeof(path));
75
76         return n >= 0 && pathmatch(path, set);
77 }
78
79 /*
80  * Add a path to the set we're tracing.
81  * Specifying NULL will delete all paths.
82  */
83 static void
84 storepath(const char *path, struct path_set *set)
85 {
86         if (pathmatch(path, set))
87                 return; /* already in table */
88
89         if (set->num_selected >= set->size)
90                 set->paths_selected =
91                         xgrowarray(set->paths_selected, &set->size,
92                                    sizeof(set->paths_selected[0]));
93
94         set->paths_selected[set->num_selected++] = path;
95 }
96
97 /*
98  * Get path associated with fd.
99  */
100 int
101 getfdpath(struct tcb *tcp, int fd, char *buf, unsigned bufsize)
102 {
103         char linkpath[sizeof("/proc/%u/fd/%u") + 2 * sizeof(int)*3];
104         ssize_t n;
105
106         if (fd < 0)
107                 return -1;
108
109         xsprintf(linkpath, "/proc/%u/fd/%u", tcp->pid, fd);
110         n = readlink(linkpath, buf, bufsize - 1);
111         /*
112          * NB: if buf is too small, readlink doesn't fail,
113          * it returns truncated result (IOW: n == bufsize - 1).
114          */
115         if (n >= 0)
116                 buf[n] = '\0';
117         return n;
118 }
119
120 /*
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.
123  */
124 void
125 pathtrace_select_set(const char *path, struct path_set *set)
126 {
127         char *rpath;
128
129         storepath(path, set);
130
131         rpath = realpath(path, NULL);
132
133         if (rpath == NULL)
134                 return;
135
136         /* if realpath and specified path are same, we're done */
137         if (strcmp(path, rpath) == 0) {
138                 free(rpath);
139                 return;
140         }
141
142         error_msg("Requested path '%s' resolved into '%s'", path, rpath);
143         storepath(rpath, set);
144 }
145
146 /*
147  * Return true if syscall accesses a selected path
148  * (or if no paths have been specified for tracing).
149  */
150 bool
151 pathtrace_match_set(struct tcb *tcp, struct path_set *set)
152 {
153         const struct_sysent *s;
154
155         s = tcp->s_ent;
156
157         if (!(s->sys_flags & (TRACE_FILE | TRACE_DESC | TRACE_NETWORK)))
158                 return false;
159
160         /*
161          * Check for special cases where we need to do something
162          * other than test arg[0].
163          */
164
165         switch (s->sen) {
166         case SEN_dup2:
167         case SEN_dup3:
168         case SEN_kexec_file_load:
169         case SEN_sendfile:
170         case SEN_sendfile64:
171         case SEN_tee:
172                 /* fd, fd */
173                 return fdmatch(tcp, tcp->u_arg[0], set) ||
174                         fdmatch(tcp, tcp->u_arg[1], set);
175
176         case SEN_execveat:
177         case SEN_faccessat:
178         case SEN_fchmodat:
179         case SEN_fchownat:
180         case SEN_fstatat64:
181         case SEN_futimesat:
182         case SEN_inotify_add_watch:
183         case SEN_mkdirat:
184         case SEN_mknodat:
185         case SEN_name_to_handle_at:
186         case SEN_newfstatat:
187         case SEN_openat:
188         case SEN_readlinkat:
189         case SEN_statx:
190         case SEN_unlinkat:
191         case SEN_utimensat:
192                 /* fd, path */
193                 return fdmatch(tcp, tcp->u_arg[0], set) ||
194                         upathmatch(tcp, tcp->u_arg[1], set);
195
196         case SEN_link:
197         case SEN_mount:
198         case SEN_pivotroot:
199                 /* path, path */
200                 return upathmatch(tcp, tcp->u_arg[0], set) ||
201                         upathmatch(tcp, tcp->u_arg[1], set);
202
203         case SEN_quotactl:
204         case SEN_symlink:
205                 /* x, path */
206                 return upathmatch(tcp, tcp->u_arg[1], set);
207
208         case SEN_linkat:
209         case SEN_renameat2:
210         case SEN_renameat:
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);
216
217 #ifdef HAVE_ARCH_OLD_MMAP
218         case SEN_old_mmap:
219 # ifdef HAVE_ARCH_OLD_MMAP_PGOFF
220         case SEN_old_mmap_pgoff:
221 # endif
222         {
223                 kernel_ulong_t *args = fetch_old_mmap_args(tcp);
224
225                 return args && fdmatch(tcp, args[4], set);
226         }
227 #endif /* HAVE_ARCH_OLD_MMAP */
228
229         case SEN_mmap:
230         case SEN_mmap_4koff:
231         case SEN_mmap_pgoff:
232         case SEN_ARCH_mmap:
233                 /* x, x, x, x, fd */
234                 return fdmatch(tcp, tcp->u_arg[4], set);
235
236         case SEN_symlinkat:
237                 /* x, fd, path */
238                 return fdmatch(tcp, tcp->u_arg[1], set) ||
239                         upathmatch(tcp, tcp->u_arg[2], set);
240
241         case SEN_copy_file_range:
242         case SEN_splice:
243                 /* fd, x, fd, x, x, x */
244                 return fdmatch(tcp, tcp->u_arg[0], set) ||
245                         fdmatch(tcp, tcp->u_arg[2], set);
246
247         case SEN_epoll_ctl:
248                 /* x, x, fd, x */
249                 return fdmatch(tcp, tcp->u_arg[2], set);
250
251
252         case SEN_fanotify_mark:
253         {
254                 /* x, x, mask (64 bit), fd, path */
255                 unsigned long long mask = 0;
256                 int argn = getllval(tcp, &mask, 2);
257                 return fdmatch(tcp, tcp->u_arg[argn], set) ||
258                         upathmatch(tcp, tcp->u_arg[argn + 1], set);
259         }
260         case SEN_oldselect:
261         case SEN_pselect6:
262         case SEN_select:
263         {
264                 int     i, j;
265                 int     nfds;
266                 kernel_ulong_t *args;
267                 kernel_ulong_t select_args[5];
268                 unsigned int oldselect_args[5];
269                 unsigned int fdsize;
270                 fd_set *fds;
271
272                 if (SEN_oldselect == s->sen) {
273                         if (sizeof(*select_args) == sizeof(*oldselect_args)) {
274                                 if (umove(tcp, tcp->u_arg[0], &select_args)) {
275                                         return false;
276                                 }
277                         } else {
278                                 unsigned int n;
279
280                                 if (umove(tcp, tcp->u_arg[0], &oldselect_args)) {
281                                         return false;
282                                 }
283
284                                 for (n = 0; n < 5; ++n) {
285                                         select_args[n] = oldselect_args[n];
286                                 }
287                         }
288                         args = select_args;
289                 } else {
290                         args = tcp->u_arg;
291                 }
292
293                 /* Kernel truncates arg[0] to int, we do the same. */
294                 nfds = (int) args[0];
295                 /* Kernel rejects negative nfds, so we don't parse it either. */
296                 if (nfds <= 0)
297                         return false;
298                 /* Beware of select(2^31-1, NULL, NULL, NULL) and similar... */
299                 if (nfds > 1024*1024)
300                         nfds = 1024*1024;
301                 fdsize = (((nfds + 7) / 8) + current_wordsize-1) & -current_wordsize;
302                 fds = xmalloc(fdsize);
303
304                 for (i = 1; i <= 3; ++i) {
305                         if (args[i] == 0)
306                                 continue;
307                         if (umoven(tcp, args[i], fdsize, fds) < 0) {
308                                 continue;
309                         }
310                         for (j = 0;; j++) {
311                                 j = next_set_bit(fds, j, nfds);
312                                 if (j < 0)
313                                         break;
314                                 if (fdmatch(tcp, j, set)) {
315                                         free(fds);
316                                         return true;
317                                 }
318                         }
319                 }
320                 free(fds);
321                 return false;
322         }
323
324         case SEN_poll:
325         case SEN_ppoll:
326         {
327                 struct pollfd fds;
328                 unsigned nfds;
329                 kernel_ulong_t start, cur, end;
330
331                 start = tcp->u_arg[0];
332                 nfds = tcp->u_arg[1];
333
334                 if (nfds > 1024 * 1024)
335                         nfds = 1024 * 1024;
336                 end = start + sizeof(fds) * nfds;
337
338                 if (nfds == 0 || end < start)
339                         return false;
340
341                 for (cur = start; cur < end; cur += sizeof(fds)) {
342                         if (umove(tcp, cur, &fds))
343                                 break;
344                         if (fdmatch(tcp, fds.fd, set))
345                                 return true;
346                 }
347
348                 return false;
349         }
350
351         case SEN_accept4:
352         case SEN_accept:
353         case SEN_bpf:
354         case SEN_epoll_create:
355         case SEN_epoll_create1:
356         case SEN_eventfd2:
357         case SEN_eventfd:
358         case SEN_fanotify_init:
359         case SEN_inotify_init:
360         case SEN_inotify_init1:
361         case SEN_memfd_create:
362         case SEN_mq_getsetattr:
363         case SEN_mq_notify:
364         case SEN_mq_open:
365         case SEN_mq_timedreceive:
366         case SEN_mq_timedsend:
367         case SEN_perf_event_open:
368         case SEN_pipe:
369         case SEN_pipe2:
370         case SEN_printargs:
371         case SEN_signalfd4:
372         case SEN_signalfd:
373         case SEN_socket:
374         case SEN_socketpair:
375         case SEN_timerfd_create:
376         case SEN_timerfd_gettime:
377         case SEN_timerfd_settime:
378         case SEN_userfaultfd:
379                 /*
380                  * These have TRACE_FILE or TRACE_DESCRIPTOR or TRACE_NETWORK set,
381                  * but they don't have any file descriptor or path args to test.
382                  */
383                 return false;
384         }
385
386         /*
387          * Our fallback position for calls that haven't already
388          * been handled is to just check arg[0].
389          */
390
391         if (s->sys_flags & TRACE_FILE)
392                 return upathmatch(tcp, tcp->u_arg[0], set);
393
394         if (s->sys_flags & (TRACE_DESC | TRACE_NETWORK))
395                 return fdmatch(tcp, tcp->u_arg[0], set);
396
397         return false;
398 }