about summary refs log tree commit diff
path: root/libtokencap/libtokencap.so.c
diff options
context:
space:
mode:
authorDavid Carlier <devnexen@gmail.com>2019-10-24 18:48:08 +0100
committerDavid Carlier <devnexen@gmail.com>2019-10-24 22:26:18 +0100
commitb4b26d420771ca19a26828d9fdd53cdd66dab9ee (patch)
tree57dfc94c2eded019770279483b6bf1074829d1fd /libtokencap/libtokencap.so.c
parent814242225725f338e35f9af372ee55daba5b4f38 (diff)
downloadafl++-b4b26d420771ca19a26828d9fdd53cdd66dab9ee.tar.gz
FreeBSD implementation
Diffstat (limited to 'libtokencap/libtokencap.so.c')
-rw-r--r--libtokencap/libtokencap.so.c50
1 files changed, 49 insertions, 1 deletions
diff --git a/libtokencap/libtokencap.so.c b/libtokencap/libtokencap.so.c
index 212fa31d..1050378c 100644
--- a/libtokencap/libtokencap.so.c
+++ b/libtokencap/libtokencap.so.c
@@ -22,17 +22,23 @@
 #include <stdio.h>
 #include <string.h>
 #include <ctype.h>
+#include <unistd.h>
 
 #include "../types.h"
 #include "../config.h"
 
-#if !defined(__linux__) && !defined(__APPLE__)
+#if !defined(__linux__) && !defined(__APPLE__) && !defined(__FreeBSD__)
 #error "Sorry, this library is unsupported in this platform for now!"
 #endif                                                        /* !__linux__ */
 
 #if defined(__APPLE__)
 #include <mach/vm_map.h>
 #include <mach/mach_init.h>
+#elif defined(__FreeBSD__)
+#include <sys/types.h>
+#include <sys/sysctl.h>
+#include <sys/user.h>
+#include <sys/mman.h>
 #endif
 
 /* Mapping data and such */
@@ -102,6 +108,48 @@ static void __tokencap_load_mappings(void) {
     }
   }
 
+#elif defined(__FreeBSD__)
+  int mib[] = {CTL_KERN, KERN_PROC, KERN_PROC_VMMAP, getpid()};
+  char *buf, *low, *high;
+  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 (sysctl(mib, miblen, buf, &len, NULL, 0) == -1) {
+
+     munmap(buf, len);
+     return;
+
+  }
+
+  low = buf;
+  high = low + len;
+
+  __tokencap_ro_loaded = 1;
+
+  while (low < high) {
+     struct kinfo_vmentry *region = (struct kinfo_vmentry *)low;
+     size_t size = region->kve_structsize;
+
+     if (size == 0) break;
+
+     /* We go through the whole mapping of the process and track read-only addresses */
+     if ((region->kve_protection & KVME_PROT_READ) &&
+	 !(region->kve_protection & KVME_PROT_WRITE)) {
+          __tokencap_ro[__tokencap_ro_cnt].st = (void *)region->kve_start;
+          __tokencap_ro[__tokencap_ro_cnt].en = (void *)region->kve_end;
+
+	  if (++__tokencap_ro_cnt == MAX_MAPPINGS) break;
+     }
+
+     low += size;
+  }
+
+  munmap(buf, len);
 #endif
 }