]> granicus.if.org Git - strace/blob - filter_qualify.c
print_array: enhance printing of unfetchable object addresses
[strace] / filter_qualify.c
1 /*
2  * Copyright (c) 2016 Dmitry V. Levin <ldv@altlinux.org>
3  * Copyright (c) 2016-2018 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 #include "defs.h"
30 #include "nsig.h"
31 #include "number_set.h"
32 #include "filter.h"
33 #include "delay.h"
34 #include "retval.h"
35
36 struct number_set *read_set;
37 struct number_set *write_set;
38 struct number_set *signal_set;
39
40 static struct number_set *abbrev_set;
41 static struct number_set *inject_set;
42 static struct number_set *raw_set;
43 static struct number_set *trace_set;
44 static struct number_set *verbose_set;
45
46 static int
47 sigstr_to_uint(const char *s)
48 {
49         if (*s >= '0' && *s <= '9')
50                 return string_to_uint_upto(s, 255);
51
52         if (strncasecmp(s, "SIG", 3) == 0)
53                 s += 3;
54
55         for (int i = 0; i <= 255; ++i) {
56                 const char *name = signame(i);
57
58                 if (strncasecmp(name, "SIG", 3) != 0)
59                         continue;
60
61                 name += 3;
62
63                 if (strcasecmp(name, s) != 0)
64                         continue;
65
66                 return i;
67         }
68
69         return -1;
70 }
71
72 static int
73 find_errno_by_name(const char *name)
74 {
75         for (unsigned int i = 1; i < nerrnos; ++i) {
76                 if (errnoent[i] && (strcasecmp(name, errnoent[i]) == 0))
77                         return i;
78         }
79
80         return -1;
81 }
82
83 static bool
84 parse_delay_token(const char *input, struct inject_opts *fopts, bool isenter)
85 {
86        unsigned flag = isenter ? INJECT_F_DELAY_ENTER : INJECT_F_DELAY_EXIT;
87
88        if (fopts->data.flags & flag) /* duplicate */
89                return false;
90        long long intval = string_to_ulonglong(input);
91        if (intval < 0) /* couldn't parse */
92                return false;
93
94        if (fopts->data.delay_idx == (uint16_t) -1)
95                fopts->data.delay_idx = alloc_delay_data();
96        /* populate .ts_enter or .ts_exit */
97        fill_delay_data(fopts->data.delay_idx, intval, isenter);
98        fopts->data.flags |= flag;
99
100        return true;
101 }
102
103 static bool
104 parse_inject_token(const char *const token, struct inject_opts *const fopts,
105                    const bool fault_tokens_only)
106 {
107         const char *val;
108         int intval;
109
110         if ((val = STR_STRIP_PREFIX(token, "when=")) != token) {
111                 /*
112                  *      == 1+1
113                  * F    == F+0
114                  * F+   == F+1
115                  * F+S
116                  */
117                 char *end;
118                 intval = string_to_uint_ex(val, &end, 0xffff, "+");
119                 if (intval < 1)
120                         return false;
121
122                 fopts->first = intval;
123
124                 if (*end) {
125                         val = end + 1;
126                         if (*val) {
127                                 /* F+S */
128                                 intval = string_to_uint_upto(val, 0xffff);
129                                 if (intval < 1)
130                                         return false;
131                                 fopts->step = intval;
132                         } else {
133                                 /* F+ == F+1 */
134                                 fopts->step = 1;
135                         }
136                 } else {
137                         /* F == F+0 */
138                         fopts->step = 0;
139                 }
140         } else if ((val = STR_STRIP_PREFIX(token, "error=")) != token) {
141                 if (fopts->data.flags & (INJECT_F_ERROR | INJECT_F_RETVAL))
142                         return false;
143                 intval = string_to_uint_upto(val, MAX_ERRNO_VALUE);
144                 if (intval < 0)
145                         intval = find_errno_by_name(val);
146                 if (intval < 1)
147                         return false;
148                 fopts->data.rval_idx = retval_new(intval);
149                 fopts->data.flags |= INJECT_F_ERROR;
150         } else if (!fault_tokens_only
151                    && (val = STR_STRIP_PREFIX(token, "retval=")) != token) {
152
153                 if (fopts->data.flags & (INJECT_F_ERROR | INJECT_F_RETVAL))
154                         return false;
155
156                 errno = 0;
157                 char *endp;
158                 unsigned long long ullval = strtoull(val, &endp, 0);
159                 if (endp == val || *endp || (kernel_ulong_t) ullval != ullval
160                     || ((ullval == 0 || ullval == ULLONG_MAX) && errno))
161                         return false;
162
163 #if ANY_WORDSIZE_LESS_THAN_KERNEL_LONG
164                 bool inadvertent_fault_injection = false;
165 #endif
166
167 #if !HAVE_ARCH_DEDICATED_ERR_REG
168                 if ((kernel_long_t) ullval < 0
169                     && (kernel_long_t) ullval >= -MAX_ERRNO_VALUE) {
170 # if ANY_WORDSIZE_LESS_THAN_KERNEL_LONG
171                         inadvertent_fault_injection = true;
172 # endif
173                         error_msg("Inadvertent injection of error %" PRI_kld
174                                   " is possible for retval=%llu",
175                                   -(kernel_long_t) ullval, ullval);
176                 }
177 # if ANY_WORDSIZE_LESS_THAN_KERNEL_LONG
178                 else if ((int) ullval < 0 && (int) ullval >= -MAX_ERRNO_VALUE) {
179                         inadvertent_fault_injection = true;
180                         error_msg("Inadvertent injection of error %d is"
181                                   " possible in compat personality for"
182                                   " retval=%llu",
183                                   -(int) ullval, ullval);
184                 }
185 # endif
186 #endif
187
188 #if ANY_WORDSIZE_LESS_THAN_KERNEL_LONG
189                 if (!inadvertent_fault_injection
190                     && (unsigned int) ullval != ullval) {
191                         error_msg("Injected return value %llu will be"
192                                   " clipped to %u in compat personality",
193                                   ullval, (unsigned int) ullval);
194                 }
195 #endif
196
197                 fopts->data.rval_idx = retval_new(ullval);
198                 fopts->data.flags |= INJECT_F_RETVAL;
199         } else if (!fault_tokens_only
200                    && (val = STR_STRIP_PREFIX(token, "signal=")) != token) {
201                 if (fopts->data.flags & INJECT_F_SIGNAL)
202                         return false;
203                 intval = sigstr_to_uint(val);
204                 if (intval < 1 || intval > NSIG_BYTES * 8)
205                         return false;
206                 fopts->data.signo = intval;
207                 fopts->data.flags |= INJECT_F_SIGNAL;
208         } else if (!fault_tokens_only
209                 && (val = STR_STRIP_PREFIX(token, "delay_enter=")) != token) {
210                 if (!parse_delay_token(val, fopts, true))
211                         return false;
212         } else if (!fault_tokens_only
213                 && (val = STR_STRIP_PREFIX(token, "delay_exit=")) != token) {
214                 if (!parse_delay_token(val, fopts, false))
215                         return false;
216         } else {
217                 return false;
218         }
219
220         return true;
221 }
222
223 static const char *
224 parse_inject_expression(char *const str,
225                         struct inject_opts *const fopts,
226                         const bool fault_tokens_only)
227 {
228         if (str[0] == '\0' || str[0] == ':')
229                 return "";
230
231         char *saveptr = NULL;
232         const char *name = strtok_r(str, ":", &saveptr);
233
234         char *token;
235         while ((token = strtok_r(NULL, ":", &saveptr))) {
236                 if (!parse_inject_token(token, fopts, fault_tokens_only))
237                         return NULL;
238         }
239
240         return name;
241 }
242
243 static void
244 qualify_read(const char *const str)
245 {
246         if (!read_set)
247                 read_set = alloc_number_set_array(1);
248         qualify_tokens(str, read_set, string_to_uint, "descriptor");
249 }
250
251 static void
252 qualify_write(const char *const str)
253 {
254         if (!write_set)
255                 write_set = alloc_number_set_array(1);
256         qualify_tokens(str, write_set, string_to_uint, "descriptor");
257 }
258
259 static void
260 qualify_signals(const char *const str)
261 {
262         if (!signal_set)
263                 signal_set = alloc_number_set_array(1);
264         qualify_tokens(str, signal_set, sigstr_to_uint, "signal");
265 }
266
267 static void
268 qualify_trace(const char *const str)
269 {
270         if (!trace_set)
271                 trace_set = alloc_number_set_array(SUPPORTED_PERSONALITIES);
272         qualify_syscall_tokens(str, trace_set);
273 }
274
275 static void
276 qualify_abbrev(const char *const str)
277 {
278         if (!abbrev_set)
279                 abbrev_set = alloc_number_set_array(SUPPORTED_PERSONALITIES);
280         qualify_syscall_tokens(str, abbrev_set);
281 }
282
283 static void
284 qualify_verbose(const char *const str)
285 {
286         if (!verbose_set)
287                 verbose_set = alloc_number_set_array(SUPPORTED_PERSONALITIES);
288         qualify_syscall_tokens(str, verbose_set);
289 }
290
291 static void
292 qualify_raw(const char *const str)
293 {
294         if (!raw_set)
295                 raw_set = alloc_number_set_array(SUPPORTED_PERSONALITIES);
296         qualify_syscall_tokens(str, raw_set);
297 }
298
299 static void
300 qualify_inject_common(const char *const str,
301                       const bool fault_tokens_only,
302                       const char *const description)
303 {
304         struct inject_opts opts = {
305                 .first = 1,
306                 .step = 1,
307                 .data = {
308                         .delay_idx = -1
309                 }
310         };
311         char *copy = xstrdup(str);
312         const char *name =
313                 parse_inject_expression(copy, &opts, fault_tokens_only);
314         if (!name)
315                 error_msg_and_die("invalid %s '%s'", description, str);
316
317         struct number_set *tmp_set =
318                 alloc_number_set_array(SUPPORTED_PERSONALITIES);
319         qualify_syscall_tokens(name, tmp_set);
320
321         free(copy);
322
323         /* If neither of retval, error, signal or delay is specified, then ... */
324         if (!opts.data.flags) {
325                 if (fault_tokens_only) {
326                         /* in fault= syntax the default error code is ENOSYS. */
327                         opts.data.rval_idx = retval_new(ENOSYS);
328                         opts.data.flags |= INJECT_F_ERROR;
329                 } else {
330                         /* in inject= syntax this is not allowed. */
331                         error_msg_and_die("invalid %s '%s'", description, str);
332                 }
333         }
334
335         /*
336          * Initialize inject_vec according to tmp_set.
337          * Merge tmp_set into inject_set.
338          */
339         for (unsigned int p = 0; p < SUPPORTED_PERSONALITIES; ++p) {
340                 if (number_set_array_is_empty(tmp_set, p))
341                         continue;
342
343                 if (!inject_set) {
344                         inject_set =
345                                 alloc_number_set_array(SUPPORTED_PERSONALITIES);
346                 }
347                 if (!inject_vec[p]) {
348                         inject_vec[p] = xcalloc(nsyscall_vec[p],
349                                                 sizeof(*inject_vec[p]));
350                 }
351
352                 for (unsigned int i = 0; i < nsyscall_vec[p]; ++i) {
353                         if (is_number_in_set_array(i, tmp_set, p)) {
354                                 add_number_to_set_array(i, inject_set, p);
355                                 inject_vec[p][i] = opts;
356                         }
357                 }
358         }
359
360         free_number_set_array(tmp_set, SUPPORTED_PERSONALITIES);
361 }
362
363 static void
364 qualify_fault(const char *const str)
365 {
366         qualify_inject_common(str, true, "fault argument");
367 }
368
369 static void
370 qualify_inject(const char *const str)
371 {
372         qualify_inject_common(str, false, "inject argument");
373 }
374
375 static const struct qual_options {
376         const char *name;
377         void (*qualify)(const char *);
378 } qual_options[] = {
379         { "trace",      qualify_trace   },
380         { "t",          qualify_trace   },
381         { "abbrev",     qualify_abbrev  },
382         { "a",          qualify_abbrev  },
383         { "verbose",    qualify_verbose },
384         { "v",          qualify_verbose },
385         { "raw",        qualify_raw     },
386         { "x",          qualify_raw     },
387         { "signal",     qualify_signals },
388         { "signals",    qualify_signals },
389         { "s",          qualify_signals },
390         { "read",       qualify_read    },
391         { "reads",      qualify_read    },
392         { "r",          qualify_read    },
393         { "write",      qualify_write   },
394         { "writes",     qualify_write   },
395         { "w",          qualify_write   },
396         { "fault",      qualify_fault   },
397         { "inject",     qualify_inject  },
398 };
399
400 void
401 qualify(const char *str)
402 {
403         const struct qual_options *opt = qual_options;
404
405         for (unsigned int i = 0; i < ARRAY_SIZE(qual_options); ++i) {
406                 const char *name = qual_options[i].name;
407                 const size_t len = strlen(name);
408                 const char *val = str_strip_prefix_len(str, name, len);
409
410                 if (val == str || *val != '=')
411                         continue;
412                 str = val + 1;
413                 opt = &qual_options[i];
414                 break;
415         }
416
417         opt->qualify(str);
418 }
419
420 unsigned int
421 qual_flags(const unsigned int scno)
422 {
423         return  (is_number_in_set_array(scno, trace_set, current_personality)
424                    ? QUAL_TRACE : 0)
425                 | (is_number_in_set_array(scno, abbrev_set, current_personality)
426                    ? QUAL_ABBREV : 0)
427                 | (is_number_in_set_array(scno, verbose_set, current_personality)
428                    ? QUAL_VERBOSE : 0)
429                 | (is_number_in_set_array(scno, raw_set, current_personality)
430                    ? QUAL_RAW : 0)
431                 | (is_number_in_set_array(scno, inject_set, current_personality)
432                    ? QUAL_INJECT : 0);
433 }