about summary refs log tree commit diff
path: root/examples/afl_untracer
diff options
context:
space:
mode:
authorDavid Carlier <devnexen@gmail.com>2020-04-30 21:29:30 +0100
committerDavid Carlier <devnexen@gmail.com>2020-04-30 21:31:37 +0100
commit59043b24ccdb15c2af3281570551990bc58e162e (patch)
treeef9e5d9748dfddf20e1ccf770dab1d2738bf6f37 /examples/afl_untracer
parent15547eb654d8edc9ecb2cd880322a82953cfa492 (diff)
downloadafl++-59043b24ccdb15c2af3281570551990bc58e162e.tar.gz
afl-untracer raw freebsd support.
Diffstat (limited to 'examples/afl_untracer')
-rw-r--r--examples/afl_untracer/afl-untracer.c67
1 files changed, 67 insertions, 0 deletions
diff --git a/examples/afl_untracer/afl-untracer.c b/examples/afl_untracer/afl-untracer.c
index 4d512356..275df320 100644
--- a/examples/afl_untracer/afl-untracer.c
+++ b/examples/afl_untracer/afl-untracer.c
@@ -61,6 +61,9 @@
 #include <sys/ucontext.h>
 #elif defined(__APPLE__) && defined(__LP64__)
 #include <mach-o/dyld_images.h>
+#elif defined(__FreeBSD__)
+#include <sys/sysctl.h>
+#include <sys/user.h>
 #else
 #error "Unsupproted platform"
 #endif
@@ -165,6 +168,67 @@ void read_library_information() {
 
   if (debug) fprintf(stderr, "\n");
 
+#elif defined(__FreeBSD__)
+  int    mib[] = {CTL_KERN, KERN_PROC, KERN_PROC_VMMAP, -1};
+  char *buf, *start, *end;
+  size_t miblen = sizeof(mib) / sizeof(mib[0]);
+  size_t len;
+
+  if (sysctl(mib, miblen, NULL, &len, NULL, 0) == -1) {
+
+    return;
+
+  }
+
+  len = len * 4 / 3;
+
+  buf = mmap(NULL, len, PROT_READ | PROT_WRITE, MAP_SHARED | MAP_ANON, -1, 0);
+  if (buf == MAP_FAILED) {
+
+    return;
+
+  }
+
+  if (sysctl(mib, miblen, buf, &len, NULL, 0) == -1) {
+
+    munmap(buf, len);
+    return;
+
+  }
+
+  start = buf;
+  end = buf + len;
+
+  while (start < end) {
+
+    struct kinfo_vmentry *region = (struct kinfo_vmentry *)start;
+    size_t                size = region->kve_structsize;
+
+    if (size == 0) {
+
+      break;
+
+    }
+
+    if ((region->kve_protection & KVME_PROT_READ) &&
+        !(region->kve_protection & KVME_PROT_EXEC)) {
+
+        liblist[liblist_cnt].name = region->kve_path[0] != '\0' ? strdup(region->kve_path) : 0;
+        liblist[liblist_cnt].addr_start = region->kve_start;
+        liblist[liblist_cnt].addr_end = region->kve_end;
+
+        if (debug) {
+          fprintf(stderr, "%s:%x (%lx-%lx)\n", liblist[liblist_cnt].name,
+                  liblist[liblist_cnt].addr_end - liblist[liblist_cnt].addr_start,
+                  liblist[liblist_cnt].addr_start, liblist[liblist_cnt].addr_end - 1);
+        }
+
+        liblist_cnt++;
+    }
+
+    start += size;
+
+  }
 #endif
 
 }
@@ -524,6 +588,9 @@ static void sigtrap_handler(int signum, siginfo_t *si, void *context) {
 #elif defined(__linux__)
   ctx->uc_mcontext.gregs[REG_RIP] -= 1;
   addr = ctx->uc_mcontext.gregs[REG_RIP];
+#elif defined(__FreeBSD__) && defined(__LP64__)
+  ctx->uc_mcontext.mc_rip -= 1;
+  addr = ctx->uc_mcontext.mc_rip;
 #else
 #error "Unsupported platform"
 #endif