1 /*
2 * Copyright (c) 2016 Dmitry V. Levin <ldv@altlinux.org>
3 * Copyright (c) 2016-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 #include "defs.h"
30 #include "nsig.h"
31 #include <regex.h>
32
33 typedef unsigned int number_slot_t;
34 #define BITS_PER_SLOT (sizeof(number_slot_t) * 8)
35
36 struct number_set {
37 number_slot_t *vec;
38 unsigned int nslots;
39 bool not;
40 };
41
42 struct number_set read_set;
43 struct number_set write_set;
44 struct number_set signal_set;
45
46 static struct number_set abbrev_set[SUPPORTED_PERSONALITIES];
47 static struct number_set inject_set[SUPPORTED_PERSONALITIES];
48 static struct number_set raw_set[SUPPORTED_PERSONALITIES];
49 static struct number_set trace_set[SUPPORTED_PERSONALITIES];
50 static struct number_set verbose_set[SUPPORTED_PERSONALITIES];
51
52 static void
number_setbit(const unsigned int i,number_slot_t * const vec)53 number_setbit(const unsigned int i, number_slot_t *const vec)
54 {
55 vec[i / BITS_PER_SLOT] |= (number_slot_t) 1 << (i % BITS_PER_SLOT);
56 }
57
58 static bool
number_isset(const unsigned int i,const number_slot_t * const vec)59 number_isset(const unsigned int i, const number_slot_t *const vec)
60 {
61 return vec[i / BITS_PER_SLOT] & ((number_slot_t) 1 << (i % BITS_PER_SLOT));
62 }
63
64 static void
reallocate_number_set(struct number_set * const set,const unsigned int new_nslots)65 reallocate_number_set(struct number_set *const set, const unsigned int new_nslots)
66 {
67 if (new_nslots <= set->nslots)
68 return;
69 set->vec = xreallocarray(set->vec, new_nslots, sizeof(*set->vec));
70 memset(set->vec + set->nslots, 0,
71 sizeof(*set->vec) * (new_nslots - set->nslots));
72 set->nslots = new_nslots;
73 }
74
75 static void
add_number_to_set(const unsigned int number,struct number_set * const set)76 add_number_to_set(const unsigned int number, struct number_set *const set)
77 {
78 reallocate_number_set(set, number / BITS_PER_SLOT + 1);
79 number_setbit(number, set->vec);
80 }
81
82 bool
is_number_in_set(const unsigned int number,const struct number_set * const set)83 is_number_in_set(const unsigned int number, const struct number_set *const set)
84 {
85 return ((number / BITS_PER_SLOT < set->nslots)
86 && number_isset(number, set->vec)) ^ set->not;
87 }
88
89 typedef int (*string_to_uint_func)(const char *);
90
91 /*
92 * Add numbers to SET according to STR specification.
93 */
94 static void
qualify_tokens(const char * const str,struct number_set * const set,string_to_uint_func func,const char * const name)95 qualify_tokens(const char *const str, struct number_set *const set,
96 string_to_uint_func func, const char *const name)
97 {
98 /* Clear the set. */
99 if (set->nslots)
100 memset(set->vec, 0, sizeof(*set->vec) * set->nslots);
101 set->not = false;
102
103 /*
104 * Each leading ! character means inversion
105 * of the remaining specification.
106 */
107 const char *s = str;
108 handle_inversion:
109 while (*s == '!') {
110 set->not = !set->not;
111 ++s;
112 }
113
114 if (strcmp(s, "none") == 0) {
115 /*
116 * No numbers are added to the set.
117 * Subsequent is_number_in_set invocations will return set->not.
118 */
119 return;
120 } else if (strcmp(s, "all") == 0) {
121 s = "!none";
122 goto handle_inversion;
123 }
124
125 /*
126 * Split the string into comma separated tokens.
127 * For each token, find out the corresponding number
128 * by calling FUNC, and add that number to the set.
129 * The absence of tokens or a negative answer
130 * from FUNC is a fatal error.
131 */
132 char *copy = xstrdup(s);
133 char *saveptr = NULL;
134 const char *token;
135 int number = -1;
136
137 for (token = strtok_r(copy, ",", &saveptr); token;
138 token = strtok_r(NULL, ",", &saveptr)) {
139 number = func(token);
140 if (number < 0) {
141 error_msg_and_die("invalid %s '%s'", name, token);
142 }
143
144 add_number_to_set(number, set);
145 }
146
147 free(copy);
148
149 if (number < 0) {
150 error_msg_and_die("invalid %s '%s'", name, str);
151 }
152 }
153
154 static int
sigstr_to_uint(const char * s)155 sigstr_to_uint(const char *s)
156 {
157 int i;
158
159 if (*s >= '0' && *s <= '9')
160 return string_to_uint_upto(s, 255);
161
162 if (strncasecmp(s, "SIG", 3) == 0)
163 s += 3;
164
165 for (i = 0; i <= 255; ++i) {
166 const char *name = signame(i);
167
168 if (strncasecmp(name, "SIG", 3) != 0)
169 continue;
170
171 name += 3;
172
173 if (strcasecmp(name, s) != 0)
174 continue;
175
176 return i;
177 }
178
179 return -1;
180 }
181
182 static bool
qualify_syscall_number(const char * s,struct number_set * set)183 qualify_syscall_number(const char *s, struct number_set *set)
184 {
185 int n = string_to_uint(s);
186 if (n < 0)
187 return false;
188
189 unsigned int p;
190 bool done = false;
191
192 for (p = 0; p < SUPPORTED_PERSONALITIES; ++p) {
193 if ((unsigned) n >= nsyscall_vec[p]) {
194 continue;
195 }
196 add_number_to_set(n, &set[p]);
197 done = true;
198 }
199
200 return done;
201 }
202
203 static void
regerror_msg_and_die(int errcode,const regex_t * preg,const char * str,const char * pattern)204 regerror_msg_and_die(int errcode, const regex_t *preg,
205 const char *str, const char *pattern)
206 {
207 char buf[512];
208
209 regerror(errcode, preg, buf, sizeof(buf));
210 error_msg_and_die("%s: %s: %s", str, pattern, buf);
211 }
212
213 static bool
qualify_syscall_regex(const char * s,struct number_set * set)214 qualify_syscall_regex(const char *s, struct number_set *set)
215 {
216 regex_t preg;
217 int rc;
218
219 if ((rc = regcomp(&preg, s, REG_EXTENDED | REG_NOSUB)) != 0)
220 regerror_msg_and_die(rc, &preg, "regcomp", s);
221
222 unsigned int p;
223 bool found = false;
224 for (p = 0; p < SUPPORTED_PERSONALITIES; ++p) {
225 unsigned int i;
226
227 for (i = 0; i < nsyscall_vec[p]; ++i) {
228 if (!sysent_vec[p][i].sys_name)
229 continue;
230 rc = regexec(&preg, sysent_vec[p][i].sys_name,
231 0, NULL, 0);
232 if (rc == REG_NOMATCH)
233 continue;
234 else if (rc)
235 regerror_msg_and_die(rc, &preg, "regexec", s);
236 add_number_to_set(i, &set[p]);
237 found = true;
238 }
239 }
240
241 regfree(&preg);
242 return found;
243 }
244
245 static unsigned int
lookup_class(const char * s)246 lookup_class(const char *s)
247 {
248 static const struct {
249 const char *name;
250 unsigned int value;
251 } syscall_class[] = {
252 { "desc", TRACE_DESC },
253 { "file", TRACE_FILE },
254 { "memory", TRACE_MEMORY },
255 { "process", TRACE_PROCESS },
256 { "signal", TRACE_SIGNAL },
257 { "ipc", TRACE_IPC },
258 { "network", TRACE_NETWORK },
259 { "%desc", TRACE_DESC },
260 { "%file", TRACE_FILE },
261 { "%memory", TRACE_MEMORY },
262 { "%process", TRACE_PROCESS },
263 { "%signal", TRACE_SIGNAL },
264 { "%ipc", TRACE_IPC },
265 { "%network", TRACE_NETWORK },
266 { "%stat", TRACE_STAT },
267 { "%lstat", TRACE_LSTAT },
268 { "%fstat", TRACE_FSTAT },
269 { "%%stat", TRACE_STAT_LIKE },
270 { "%statfs", TRACE_STATFS },
271 { "%fstatfs", TRACE_FSTATFS },
272 { "%%statfs", TRACE_STATFS_LIKE },
273 };
274
275 unsigned int i;
276 for (i = 0; i < ARRAY_SIZE(syscall_class); ++i) {
277 if (strcmp(s, syscall_class[i].name) == 0) {
278 return syscall_class[i].value;
279 }
280 }
281
282 return 0;
283 }
284
285 static bool
qualify_syscall_class(const char * s,struct number_set * set)286 qualify_syscall_class(const char *s, struct number_set *set)
287 {
288 const unsigned int n = lookup_class(s);
289 if (!n)
290 return false;
291
292 unsigned int p;
293 for (p = 0; p < SUPPORTED_PERSONALITIES; ++p) {
294 unsigned int i;
295
296 for (i = 0; i < nsyscall_vec[p]; ++i) {
297 if (!sysent_vec[p][i].sys_name
298 || (sysent_vec[p][i].sys_flags & n) != n) {
299 continue;
300 }
301 add_number_to_set(i, &set[p]);
302 }
303 }
304
305 return true;
306 }
307
308 static bool
qualify_syscall_name(const char * s,struct number_set * set)309 qualify_syscall_name(const char *s, struct number_set *set)
310 {
311 unsigned int p;
312 bool found = false;
313
314 for (p = 0; p < SUPPORTED_PERSONALITIES; ++p) {
315 unsigned int i;
316
317 for (i = 0; i < nsyscall_vec[p]; ++i) {
318 if (!sysent_vec[p][i].sys_name
319 || strcmp(s, sysent_vec[p][i].sys_name)) {
320 continue;
321 }
322 add_number_to_set(i, &set[p]);
323 found = true;
324 }
325 }
326
327 return found;
328 }
329
330 static bool
qualify_syscall(const char * token,struct number_set * set)331 qualify_syscall(const char *token, struct number_set *set)
332 {
333 bool ignore_fail = false;
334
335 while (*token == '?') {
336 token++;
337 ignore_fail = true;
338 }
339 if (*token >= '0' && *token <= '9')
340 return qualify_syscall_number(token, set) || ignore_fail;
341 if (*token == '/')
342 return qualify_syscall_regex(token + 1, set) || ignore_fail;
343 return qualify_syscall_class(token, set)
344 || qualify_syscall_name(token, set)
345 || ignore_fail;
346 }
347
348 /*
349 * Add syscall numbers to SETs for each supported personality
350 * according to STR specification.
351 */
352 static void
qualify_syscall_tokens(const char * const str,struct number_set * const set,const char * const name)353 qualify_syscall_tokens(const char *const str, struct number_set *const set,
354 const char *const name)
355 {
356 /* Clear all sets. */
357 unsigned int p;
358 for (p = 0; p < SUPPORTED_PERSONALITIES; ++p) {
359 if (set[p].nslots)
360 memset(set[p].vec, 0,
361 sizeof(*set[p].vec) * set[p].nslots);
362 set[p].not = false;
363 }
364
365 /*
366 * Each leading ! character means inversion
367 * of the remaining specification.
368 */
369 const char *s = str;
370 handle_inversion:
371 while (*s == '!') {
372 for (p = 0; p < SUPPORTED_PERSONALITIES; ++p) {
373 set[p].not = !set[p].not;
374 }
375 ++s;
376 }
377
378 if (strcmp(s, "none") == 0) {
379 /*
380 * No syscall numbers are added to sets.
381 * Subsequent is_number_in_set invocations
382 * will return set[p]->not.
383 */
384 return;
385 } else if (strcmp(s, "all") == 0) {
386 s = "!none";
387 goto handle_inversion;
388 }
389
390 /*
391 * Split the string into comma separated tokens.
392 * For each token, call qualify_syscall that will take care
393 * if adding appropriate syscall numbers to sets.
394 * The absence of tokens or a negative return code
395 * from qualify_syscall is a fatal error.
396 */
397 char *copy = xstrdup(s);
398 char *saveptr = NULL;
399 const char *token;
400 bool done = false;
401
402 for (token = strtok_r(copy, ",", &saveptr); token;
403 token = strtok_r(NULL, ",", &saveptr)) {
404 done = qualify_syscall(token, set);
405 if (!done) {
406 error_msg_and_die("invalid %s '%s'", name, token);
407 }
408 }
409
410 free(copy);
411
412 if (!done) {
413 error_msg_and_die("invalid %s '%s'", name, str);
414 }
415 }
416
417 static int
find_errno_by_name(const char * name)418 find_errno_by_name(const char *name)
419 {
420 unsigned int i;
421
422 for (i = 1; i < nerrnos; ++i) {
423 if (errnoent[i] && (strcasecmp(name, errnoent[i]) == 0))
424 return i;
425 }
426
427 return -1;
428 }
429
430 static bool
parse_inject_token(const char * const token,struct inject_opts * const fopts,const bool fault_tokens_only)431 parse_inject_token(const char *const token, struct inject_opts *const fopts,
432 const bool fault_tokens_only)
433 {
434 const char *val;
435 int intval;
436
437 if ((val = STR_STRIP_PREFIX(token, "when=")) != token) {
438 /*
439 * == 1+1
440 * F == F+0
441 * F+ == F+1
442 * F+S
443 */
444 char *end;
445 intval = string_to_uint_ex(val, &end, 0xffff, "+");
446 if (intval < 1)
447 return false;
448
449 fopts->first = intval;
450
451 if (*end) {
452 val = end + 1;
453 if (*val) {
454 /* F+S */
455 intval = string_to_uint_upto(val, 0xffff);
456 if (intval < 1)
457 return false;
458 fopts->step = intval;
459 } else {
460 /* F+ == F+1 */
461 fopts->step = 1;
462 }
463 } else {
464 /* F == F+0 */
465 fopts->step = 0;
466 }
467 } else if ((val = STR_STRIP_PREFIX(token, "error=")) != token) {
468 if (fopts->rval != INJECT_OPTS_RVAL_DEFAULT)
469 return false;
470 intval = string_to_uint_upto(val, MAX_ERRNO_VALUE);
471 if (intval < 0)
472 intval = find_errno_by_name(val);
473 if (intval < 1)
474 return false;
475 fopts->rval = -intval;
476 } else if (!fault_tokens_only
477 && (val = STR_STRIP_PREFIX(token, "retval=")) != token) {
478 if (fopts->rval != INJECT_OPTS_RVAL_DEFAULT)
479 return false;
480 intval = string_to_uint(val);
481 if (intval < 0)
482 return false;
483 fopts->rval = intval;
484 } else if (!fault_tokens_only
485 && (val = STR_STRIP_PREFIX(token, "signal=")) != token) {
486 intval = sigstr_to_uint(val);
487 if (intval < 1 || intval > NSIG_BYTES * 8)
488 return false;
489 fopts->signo = intval;
490 } else {
491 return false;
492 }
493
494 return true;
495 }
496
497 static char *
parse_inject_expression(const char * const s,char ** buf,struct inject_opts * const fopts,const bool fault_tokens_only)498 parse_inject_expression(const char *const s, char **buf,
499 struct inject_opts *const fopts,
500 const bool fault_tokens_only)
501 {
502 char *saveptr = NULL;
503 char *name = NULL;
504 char *token;
505
506 *buf = xstrdup(s);
507 for (token = strtok_r(*buf, ":", &saveptr); token;
508 token = strtok_r(NULL, ":", &saveptr)) {
509 if (!name)
510 name = token;
511 else if (!parse_inject_token(token, fopts, fault_tokens_only))
512 goto parse_error;
513 }
514
515 if (name)
516 return name;
517
518 parse_error:
519 free(*buf);
520 return *buf = NULL;
521 }
522
523 static void
qualify_read(const char * const str)524 qualify_read(const char *const str)
525 {
526 qualify_tokens(str, &read_set, string_to_uint, "descriptor");
527 }
528
529 static void
qualify_write(const char * const str)530 qualify_write(const char *const str)
531 {
532 qualify_tokens(str, &write_set, string_to_uint, "descriptor");
533 }
534
535 static void
qualify_signals(const char * const str)536 qualify_signals(const char *const str)
537 {
538 qualify_tokens(str, &signal_set, sigstr_to_uint, "signal");
539 }
540
541 static void
qualify_trace(const char * const str)542 qualify_trace(const char *const str)
543 {
544 qualify_syscall_tokens(str, trace_set, "system call");
545 }
546
547 static void
qualify_abbrev(const char * const str)548 qualify_abbrev(const char *const str)
549 {
550 qualify_syscall_tokens(str, abbrev_set, "system call");
551 }
552
553 static void
qualify_verbose(const char * const str)554 qualify_verbose(const char *const str)
555 {
556 qualify_syscall_tokens(str, verbose_set, "system call");
557 }
558
559 static void
qualify_raw(const char * const str)560 qualify_raw(const char *const str)
561 {
562 qualify_syscall_tokens(str, raw_set, "system call");
563 }
564
565 static void
qualify_inject_common(const char * const str,const bool fault_tokens_only,const char * const description)566 qualify_inject_common(const char *const str,
567 const bool fault_tokens_only,
568 const char *const description)
569 {
570 struct inject_opts opts = {
571 .first = 1,
572 .step = 1,
573 .rval = INJECT_OPTS_RVAL_DEFAULT,
574 .signo = 0
575 };
576 char *buf = NULL;
577 char *name = parse_inject_expression(str, &buf, &opts, fault_tokens_only);
578 if (!name) {
579 error_msg_and_die("invalid %s '%s'", description, str);
580 }
581
582 /* If neither of retval, error, or signal is specified, then ... */
583 if (opts.rval == INJECT_OPTS_RVAL_DEFAULT && !opts.signo) {
584 if (fault_tokens_only) {
585 /* in fault= syntax the default error code is ENOSYS. */
586 opts.rval = -ENOSYS;
587 } else {
588 /* in inject= syntax this is not allowed. */
589 error_msg_and_die("invalid %s '%s'", description, str);
590 }
591 }
592
593 struct number_set tmp_set[SUPPORTED_PERSONALITIES];
594 memset(tmp_set, 0, sizeof(tmp_set));
595 qualify_syscall_tokens(name, tmp_set, description);
596
597 free(buf);
598
599 /*
600 * Initialize inject_vec accourding to tmp_set.
601 * Merge tmp_set into inject_set.
602 */
603 unsigned int p;
604 for (p = 0; p < SUPPORTED_PERSONALITIES; ++p) {
605 if (!tmp_set[p].nslots && !tmp_set[p].not) {
606 continue;
607 }
608
609 if (!inject_vec[p]) {
610 inject_vec[p] = xcalloc(nsyscall_vec[p],
611 sizeof(*inject_vec[p]));
612 }
613
614 unsigned int i;
615 for (i = 0; i < nsyscall_vec[p]; ++i) {
616 if (is_number_in_set(i, &tmp_set[p])) {
617 add_number_to_set(i, &inject_set[p]);
618 inject_vec[p][i] = opts;
619 }
620 }
621
622 free(tmp_set[p].vec);
623 }
624 }
625
626 static void
qualify_fault(const char * const str)627 qualify_fault(const char *const str)
628 {
629 qualify_inject_common(str, true, "fault argument");
630 }
631
632 static void
qualify_inject(const char * const str)633 qualify_inject(const char *const str)
634 {
635 qualify_inject_common(str, false, "inject argument");
636 }
637
638 static const struct qual_options {
639 const char *name;
640 void (*qualify)(const char *);
641 } qual_options[] = {
642 { "trace", qualify_trace },
643 { "t", qualify_trace },
644 { "abbrev", qualify_abbrev },
645 { "a", qualify_abbrev },
646 { "verbose", qualify_verbose },
647 { "v", qualify_verbose },
648 { "raw", qualify_raw },
649 { "x", qualify_raw },
650 { "signal", qualify_signals },
651 { "signals", qualify_signals },
652 { "s", qualify_signals },
653 { "read", qualify_read },
654 { "reads", qualify_read },
655 { "r", qualify_read },
656 { "write", qualify_write },
657 { "writes", qualify_write },
658 { "w", qualify_write },
659 { "fault", qualify_fault },
660 { "inject", qualify_inject },
661 };
662
663 void
qualify(const char * str)664 qualify(const char *str)
665 {
666 const struct qual_options *opt = qual_options;
667 unsigned int i;
668
669 for (i = 0; i < ARRAY_SIZE(qual_options); ++i) {
670 const char *name = qual_options[i].name;
671 const size_t len = strlen(name);
672 const char *val = str_strip_prefix_len(str, name, len);
673
674 if (val == str || *val != '=')
675 continue;
676 str = val + 1;
677 opt = &qual_options[i];
678 break;
679 }
680
681 opt->qualify(str);
682 }
683
684 unsigned int
qual_flags(const unsigned int scno)685 qual_flags(const unsigned int scno)
686 {
687 return (is_number_in_set(scno, &trace_set[current_personality])
688 ? QUAL_TRACE : 0)
689 | (is_number_in_set(scno, &abbrev_set[current_personality])
690 ? QUAL_ABBREV : 0)
691 | (is_number_in_set(scno, &verbose_set[current_personality])
692 ? QUAL_VERBOSE : 0)
693 | (is_number_in_set(scno, &raw_set[current_personality])
694 ? QUAL_RAW : 0)
695 | (is_number_in_set(scno, &inject_set[current_personality])
696 ? QUAL_INJECT : 0);
697 }
698