about summary refs log tree commit diff
diff options
context:
space:
mode:
authorDominik Maier <domenukk@gmail.com>2021-01-08 11:36:40 +0100
committerDominik Maier <domenukk@gmail.com>2021-01-08 11:36:40 +0100
commit34732e3c5ede9020ff1802f0f0827e3731217dce (patch)
treed833692ced76c2c45a3cd26e9341487419dc812a
parent7eaca9c840e7e8599db3a7a09bcbf6d774ee7631 (diff)
downloadafl++-34732e3c5ede9020ff1802f0f0827e3731217dce.tar.gz
refactored kill signal env parsing
-rw-r--r--include/common.h5
-rw-r--r--src/afl-common.c34
-rw-r--r--src/afl-fuzz.c32
-rw-r--r--src/afl-showmap.c29
-rw-r--r--src/afl-tmin.c29
5 files changed, 47 insertions, 82 deletions
diff --git a/include/common.h b/include/common.h
index 125c3abf..9490ec5f 100644
--- a/include/common.h
+++ b/include/common.h
@@ -56,6 +56,11 @@ extern u8 *doc_path;                    /* path to documentation dir        */
 
 u8 *find_binary(u8 *fname);
 
+/* Parses the kill signal environment variable, FATALs on error.
+  If the env is not set, sets the env to default_signal for the signal handlers
+  and returns the default_signal. */
+int parse_afl_kill_signal_env(u8 *afl_kill_signal_env, int default_signal);
+
 /* Read a bitmap from file fname to memory
    This is for the -B option again. */
 
diff --git a/src/afl-common.c b/src/afl-common.c
index 21cb6ab4..3a7d0ce5 100644
--- a/src/afl-common.c
+++ b/src/afl-common.c
@@ -424,6 +424,40 @@ u8 *find_binary(u8 *fname) {
 
 }
 
+/* Parses the kill signal environment variable, FATALs on error.
+  If the env is not set, sets the env to default_signal for the signal handlers
+  and returns the default_signal. */
+int parse_afl_kill_signal_env(u8 *afl_kill_signal_env, int default_signal) {
+
+  if (afl_kill_signal_env && afl_kill_signal_env[0]) {
+
+    char *endptr;
+    u8    signal_code;
+    signal_code = (u8)strtoul(afl_kill_signal_env, &endptr, 10);
+    /* Did we manage to parse the full string? */
+    if (*endptr != '\0' || endptr == (char *)afl_kill_signal_env) {
+
+      FATAL("Invalid AFL_KILL_SIGNAL: %s (expected unsigned int)",
+            afl_kill_signal_env);
+
+    }
+
+    return signal_code;
+
+  } else {
+
+    char *sigstr = alloc_printf("%d", default_signal);
+    if (!sigstr) { FATAL("Failed to alloc mem for signal buf"); }
+
+    /* Set the env for signal handler */
+    setenv("AFL_KILL_SIGNAL", sigstr, 1);
+    free(sigstr);
+    return default_signal;
+
+  }
+
+}
+
 void check_environment_vars(char **envp) {
 
   if (be_quiet) { return; }
diff --git a/src/afl-fuzz.c b/src/afl-fuzz.c
index 5c363c63..37f8db8a 100644
--- a/src/afl-fuzz.c
+++ b/src/afl-fuzz.c
@@ -76,9 +76,9 @@ static void at_exit() {
 
   }
 
-  u8 kill_signal = SIGKILL;
+  int kill_signal = SIGKILL;
 
-  /* AFL_KILL_SIGNAL should already be initialized by afl_fsrv_init() */
+  /* AFL_KILL_SIGNAL should already be a valid int at this point */
   if (getenv("AFL_KILL_SIGNAL")) {
 
     kill_signal = atoi(getenv("AFL_KILL_SIGNAL"));
@@ -987,32 +987,8 @@ int main(int argc, char **argv_orig, char **envp) {
 
   #endif
 
-  afl->fsrv.kill_signal = SIGKILL;
-  if (afl->afl_env.afl_kill_signal) {
-
-    char *endptr;
-    u8    signal_code;
-    signal_code = (u8)strtoul(afl->afl_env.afl_kill_signal, &endptr, 10);
-    /* Did we manage to parse the full string? */
-    if (*endptr != '\0' || endptr == (char *)afl->afl_env.afl_kill_signal) {
-
-      FATAL("Invalid AFL_KILL_SIGNAL: %s (expected unsigned int)",
-            afl->afl_env.afl_kill_signal);
-
-    }
-
-    afl->fsrv.kill_signal = signal_code;
-
-  } else {
-
-    char *sigstr = alloc_printf("%d", (int)SIGKILL);
-    if (!sigstr) { FATAL("Failed to alloc mem for signal buf"); }
-
-    /* Set the env for signal handler */
-    setenv("AFL_KILL_SIGNAL", sigstr, 1);
-    free(sigstr);
-
-  }
+  afl->fsrv.kill_signal =
+      parse_afl_kill_signal_env(afl->afl_env.afl_kill_signal, SIGKILL);
 
   setup_signal_handlers();
   check_asan_opts(afl);
diff --git a/src/afl-showmap.c b/src/afl-showmap.c
index 2c9c38ed..5c9d38e0 100644
--- a/src/afl-showmap.c
+++ b/src/afl-showmap.c
@@ -1116,33 +1116,8 @@ int main(int argc, char **argv_orig, char **envp) {
 
     }
 
-    fsrv->kill_signal = SIGKILL;
-    char *afl_kill_signal_env = getenv("AFL_KILL_SIGNAL");
-    if (afl_kill_signal_env && afl_kill_signal_env[0]) {
-
-      char *endptr;
-      u8    signal_code;
-      signal_code = (u8)strtoul(afl_kill_signal_env, &endptr, 10);
-      /* Did we manage to parse the full string? */
-      if (*endptr != '\0' || endptr == afl_kill_signal_env) {
-
-        FATAL("Invalid AFL_KILL_SIGNAL: %s (expected unsigned int)",
-              afl_kill_signal_env);
-
-      }
-
-      fsrv->kill_signal = signal_code;
-
-    } else {
-
-      char *sigstr = alloc_printf("%d", (int)SIGKILL);
-      if (!sigstr) { FATAL("Failed to alloc mem for signal buf"); }
-
-      /* Set the env for signal handler */
-      setenv("AFL_KILL_SIGNAL", sigstr, 1);
-      free(sigstr);
-
-    }
+    fsrv->kill_signal =
+        parse_afl_kill_signal_env(getenv("AFL_KILL_SIGNAL"), SIGKILL);
 
     if (getenv("AFL_CRASH_EXITCODE")) {
 
diff --git a/src/afl-tmin.c b/src/afl-tmin.c
index 342de9c8..6e2d7708 100644
--- a/src/afl-tmin.c
+++ b/src/afl-tmin.c
@@ -1135,33 +1135,8 @@ int main(int argc, char **argv_orig, char **envp) {
 
   }
 
-  fsrv->kill_signal = SIGKILL;
-  char *afl_kill_signal_env = getenv("AFL_KILL_SIGNAL");
-  if (afl_kill_signal_env && afl_kill_signal_env[0]) {
-
-    char *endptr;
-    u8    signal_code;
-    signal_code = (u8)strtoul(afl_kill_signal_env, &endptr, 10);
-    /* Did we manage to parse the full string? */
-    if (*endptr != '\0' || endptr == afl_kill_signal_env) {
-
-      FATAL("Invalid AFL_KILL_SIGNAL: %s (expected unsigned int)",
-            afl_kill_signal_env);
-
-    }
-
-    fsrv->kill_signal = signal_code;
-
-  } else {
-
-    char *sigstr = alloc_printf("%d", (int)SIGKILL);
-    if (!sigstr) { FATAL("Failed to alloc mem for signal buf"); }
-
-    /* Set the env for signal handler */
-    setenv("AFL_KILL_SIGNAL", sigstr, 1);
-    free(sigstr);
-
-  }
+  fsrv->kill_signal =
+      parse_afl_kill_signal_env(getenv("AFL_KILL_SIGNAL"), SIGKILL);
 
   if (getenv("AFL_CRASH_EXITCODE")) {