]> granicus.if.org Git - strace/blobdiff - filter_qualify.c
tests: check decoding of vcpu auxstr
[strace] / filter_qualify.c
index ddba3d5e8ac6250b5ea1562f9a7d6c157adfa564..e28c67298b27888a446ec2208f2c98ddef868580 100644 (file)
@@ -1,6 +1,6 @@
 /*
  * Copyright (c) 2016 Dmitry V. Levin <ldv@altlinux.org>
- * Copyright (c) 2016-2017 The strace developers.
+ * Copyright (c) 2016-2018 The strace developers.
  * All rights reserved.
  *
  * Redistribution and use in source and binary forms, with or without
@@ -30,6 +30,8 @@
 #include "nsig.h"
 #include "number_set.h"
 #include "filter.h"
+#include "delay.h"
+#include "retval.h"
 
 struct number_set *read_set;
 struct number_set *write_set;
@@ -41,18 +43,21 @@ static struct number_set *raw_set;
 static struct number_set *trace_set;
 static struct number_set *verbose_set;
 
+/* Only syscall numbers are personality-specific so far.  */
+struct inject_personality_data {
+       uint16_t scno;
+};
+
 static int
 sigstr_to_uint(const char *s)
 {
-       int i;
-
        if (*s >= '0' && *s <= '9')
                return string_to_uint_upto(s, 255);
 
        if (strncasecmp(s, "SIG", 3) == 0)
                s += 3;
 
-       for (i = 0; i <= 255; ++i) {
+       for (int i = 0; i <= 255; ++i) {
                const char *name = signame(i);
 
                if (strncasecmp(name, "SIG", 3) != 0)
@@ -72,9 +77,7 @@ sigstr_to_uint(const char *s)
 static int
 find_errno_by_name(const char *name)
 {
-       unsigned int i;
-
-       for (i = 1; i < nerrnos; ++i) {
+       for (unsigned int i = 1; i < nerrnos; ++i) {
                if (errnoent[i] && (strcasecmp(name, errnoent[i]) == 0))
                        return i;
        }
@@ -82,8 +85,29 @@ find_errno_by_name(const char *name)
        return -1;
 }
 
+static bool
+parse_delay_token(const char *input, struct inject_opts *fopts, bool isenter)
+{
+       unsigned flag = isenter ? INJECT_F_DELAY_ENTER : INJECT_F_DELAY_EXIT;
+
+       if (fopts->data.flags & flag) /* duplicate */
+               return false;
+       long long intval = string_to_ulonglong(input);
+       if (intval < 0) /* couldn't parse */
+               return false;
+
+       if (fopts->data.delay_idx == (uint16_t) -1)
+               fopts->data.delay_idx = alloc_delay_data();
+       /* populate .ts_enter or .ts_exit */
+       fill_delay_data(fopts->data.delay_idx, intval, isenter);
+       fopts->data.flags |= flag;
+
+       return true;
+}
+
 static bool
 parse_inject_token(const char *const token, struct inject_opts *const fopts,
+                  struct inject_personality_data *const pdata,
                   const bool fault_tokens_only)
 {
        const char *val;
@@ -119,24 +143,85 @@ parse_inject_token(const char *const token, struct inject_opts *const fopts,
                        /* F == F+0 */
                        fopts->step = 0;
                }
+       } else if ((val = STR_STRIP_PREFIX(token, "syscall=")) != token) {
+               if (fopts->data.flags & INJECT_F_SYSCALL)
+                       return false;
+
+               for (unsigned int p = 0; p < SUPPORTED_PERSONALITIES; ++p) {
+                       kernel_long_t scno = scno_by_name(val, p, 0);
+
+                       if (scno < 0)
+                               return false;
+
+                       /*
+                        * We want to inject only pure system calls with no side
+                        * effects.
+                        */
+                       if (!(sysent_vec[p][scno].sys_flags & TRACE_PURE))
+                               return false;
+
+                       pdata[p].scno = scno;
+               }
+
+               fopts->data.flags |= INJECT_F_SYSCALL;
        } else if ((val = STR_STRIP_PREFIX(token, "error=")) != token) {
-               if (fopts->data.flags & INJECT_F_RETVAL)
+               if (fopts->data.flags & (INJECT_F_ERROR | INJECT_F_RETVAL))
                        return false;
                intval = string_to_uint_upto(val, MAX_ERRNO_VALUE);
                if (intval < 0)
                        intval = find_errno_by_name(val);
                if (intval < 1)
                        return false;
-               fopts->data.rval = -intval;
-               fopts->data.flags |= INJECT_F_RETVAL;
+               fopts->data.rval_idx = retval_new(intval);
+               fopts->data.flags |= INJECT_F_ERROR;
        } else if (!fault_tokens_only
                   && (val = STR_STRIP_PREFIX(token, "retval=")) != token) {
-               if (fopts->data.flags & INJECT_F_RETVAL)
+
+               if (fopts->data.flags & (INJECT_F_ERROR | INJECT_F_RETVAL))
                        return false;
-               intval = string_to_uint(val);
-               if (intval < 0)
+
+               errno = 0;
+               char *endp;
+               unsigned long long ullval = strtoull(val, &endp, 0);
+               if (endp == val || *endp || (kernel_ulong_t) ullval != ullval
+                   || ((ullval == 0 || ullval == ULLONG_MAX) && errno))
                        return false;
-               fopts->data.rval = intval;
+
+#if ANY_WORDSIZE_LESS_THAN_KERNEL_LONG
+               bool inadvertent_fault_injection = false;
+#endif
+
+#if !HAVE_ARCH_DEDICATED_ERR_REG
+               if ((kernel_long_t) ullval < 0
+                   && (kernel_long_t) ullval >= -MAX_ERRNO_VALUE) {
+# if ANY_WORDSIZE_LESS_THAN_KERNEL_LONG
+                       inadvertent_fault_injection = true;
+# endif
+                       error_msg("Inadvertent injection of error %" PRI_kld
+                                 " is possible for retval=%llu",
+                                 -(kernel_long_t) ullval, ullval);
+               }
+# if ANY_WORDSIZE_LESS_THAN_KERNEL_LONG
+               else if ((int) ullval < 0 && (int) ullval >= -MAX_ERRNO_VALUE) {
+                       inadvertent_fault_injection = true;
+                       error_msg("Inadvertent injection of error %d is"
+                                 " possible in compat personality for"
+                                 " retval=%llu",
+                                 -(int) ullval, ullval);
+               }
+# endif
+#endif
+
+#if ANY_WORDSIZE_LESS_THAN_KERNEL_LONG
+               if (!inadvertent_fault_injection
+                   && (unsigned int) ullval != ullval) {
+                       error_msg("Injected return value %llu will be"
+                                 " clipped to %u in compat personality",
+                                 ullval, (unsigned int) ullval);
+               }
+#endif
+
+               fopts->data.rval_idx = retval_new(ullval);
                fopts->data.flags |= INJECT_F_RETVAL;
        } else if (!fault_tokens_only
                   && (val = STR_STRIP_PREFIX(token, "signal=")) != token) {
@@ -147,6 +232,14 @@ parse_inject_token(const char *const token, struct inject_opts *const fopts,
                        return false;
                fopts->data.signo = intval;
                fopts->data.flags |= INJECT_F_SIGNAL;
+       } else if (!fault_tokens_only
+               && (val = STR_STRIP_PREFIX(token, "delay_enter=")) != token) {
+               if (!parse_delay_token(val, fopts, true))
+                       return false;
+       } else if (!fault_tokens_only
+               && (val = STR_STRIP_PREFIX(token, "delay_exit=")) != token) {
+               if (!parse_delay_token(val, fopts, false))
+                       return false;
        } else {
                return false;
        }
@@ -154,20 +247,21 @@ parse_inject_token(const char *const token, struct inject_opts *const fopts,
        return true;
 }
 
-static char *
+static const char *
 parse_inject_expression(char *const str,
                        struct inject_opts *const fopts,
+                       struct inject_personality_data *const pdata,
                        const bool fault_tokens_only)
 {
+       if (str[0] == '\0' || str[0] == ':')
+               return "";
+
        char *saveptr = NULL;
-       char *name = NULL;
-       char *token;
+       const char *name = strtok_r(str, ":", &saveptr);
 
-       for (token = strtok_r(str, ":", &saveptr); token;
-            token = strtok_r(NULL, ":", &saveptr)) {
-               if (!name)
-                       name = token;
-               else if (!parse_inject_token(token, fopts, fault_tokens_only))
+       char *token;
+       while ((token = strtok_r(NULL, ":", &saveptr))) {
+               if (!parse_inject_token(token, fopts, pdata, fault_tokens_only))
                        return NULL;
        }
 
@@ -237,37 +331,41 @@ qualify_inject_common(const char *const str,
 {
        struct inject_opts opts = {
                .first = 1,
-               .step = 1
+               .step = 1,
+               .data = {
+                       .delay_idx = -1
+               }
        };
+       struct inject_personality_data pdata[SUPPORTED_PERSONALITIES] = { { 0 } };
        char *copy = xstrdup(str);
-       char *name = parse_inject_expression(copy, &opts, fault_tokens_only);
+       const char *name =
+               parse_inject_expression(copy, &opts, pdata, fault_tokens_only);
        if (!name)
                error_msg_and_die("invalid %s '%s'", description, str);
 
-       /* If neither of retval, error, or signal is specified, then ... */
-       if (!opts.data.flags) {
+       struct number_set *tmp_set =
+               alloc_number_set_array(SUPPORTED_PERSONALITIES);
+       qualify_syscall_tokens(name, tmp_set);
+
+       free(copy);
+
+       /* If neither of retval, error, signal or delay is specified, then ... */
+       if (!(opts.data.flags & INJECT_ACTION_FLAGS)) {
                if (fault_tokens_only) {
                        /* in fault= syntax the default error code is ENOSYS. */
-                       opts.data.rval = -ENOSYS;
-                       opts.data.flags |= INJECT_F_RETVAL;
+                       opts.data.rval_idx = retval_new(ENOSYS);
+                       opts.data.flags |= INJECT_F_ERROR;
                } else {
                        /* in inject= syntax this is not allowed. */
                        error_msg_and_die("invalid %s '%s'", description, str);
                }
        }
 
-       struct number_set *tmp_set =
-               alloc_number_set_array(SUPPORTED_PERSONALITIES);
-       qualify_syscall_tokens(name, tmp_set);
-
-       free(copy);
-
        /*
         * Initialize inject_vec according to tmp_set.
         * Merge tmp_set into inject_set.
         */
-       unsigned int p;
-       for (p = 0; p < SUPPORTED_PERSONALITIES; ++p) {
+       for (unsigned int p = 0; p < SUPPORTED_PERSONALITIES; ++p) {
                if (number_set_array_is_empty(tmp_set, p))
                        continue;
 
@@ -280,11 +378,14 @@ qualify_inject_common(const char *const str,
                                                sizeof(*inject_vec[p]));
                }
 
-               unsigned int i;
-               for (i = 0; i < nsyscall_vec[p]; ++i) {
+               for (unsigned int i = 0; i < nsyscall_vec[p]; ++i) {
                        if (is_number_in_set_array(i, tmp_set, p)) {
                                add_number_to_set_array(i, inject_set, p);
                                inject_vec[p][i] = opts;
+
+                               /* Copy per-personality data.  */
+                               inject_vec[p][i].data.scno =
+                                       pdata[p].scno;
                        }
                }
        }
@@ -304,6 +405,21 @@ qualify_inject(const char *const str)
        qualify_inject_common(str, false, "inject argument");
 }
 
+#ifdef HAVE_LINUX_KVM_H
+static void
+qualify_kvm(const char *const str)
+{
+       if (strcmp(str, "vcpu") == 0) {
+               if (os_release >= KERNEL_VERSION(4, 16, 0))
+                       kvm_run_structure_decoder_init();
+               else
+                       error_msg("-e kvm=vcpu option needs Linux 4.16.0 or higher");
+       } else {
+               error_msg("unknown value for -e kvm= option: %s", str);
+       }
+}
+#endif
+
 static const struct qual_options {
        const char *name;
        void (*qualify)(const char *);
@@ -327,15 +443,17 @@ static const struct qual_options {
        { "w",          qualify_write   },
        { "fault",      qualify_fault   },
        { "inject",     qualify_inject  },
+#ifdef HAVE_LINUX_KVM_H
+       { "kvm",        qualify_kvm     },
+#endif
 };
 
 void
 qualify(const char *str)
 {
        const struct qual_options *opt = qual_options;
-       unsigned int i;
 
-       for (i = 0; i < ARRAY_SIZE(qual_options); ++i) {
+       for (unsigned int i = 0; i < ARRAY_SIZE(qual_options); ++i) {
                const char *name = qual_options[i].name;
                const size_t len = strlen(name);
                const char *val = str_strip_prefix_len(str, name, len);