aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorYour Name <you@example.com>2022-02-18 08:21:36 +0000
committerYour Name <you@example.com>2022-02-18 08:21:36 +0000
commitfc7bf33fb144162ede0c2d56b91ad31143113f16 (patch)
tree9ffc1ca018245aa6533a9194da7c21a03c4e4a7c
parentfcd06fa99ceeeb9769102357257be0c1e192641e (diff)
downloadafl++-fc7bf33fb144162ede0c2d56b91ad31143113f16.tar.gz
Reserved used address space on dlclose to prevent re-use
-rw-r--r--frida_mode/include/module.h11
-rw-r--r--frida_mode/src/main.c3
-rw-r--r--frida_mode/src/module.c106
3 files changed, 120 insertions, 0 deletions
diff --git a/frida_mode/include/module.h b/frida_mode/include/module.h
new file mode 100644
index 00000000..b66f7feb
--- /dev/null
+++ b/frida_mode/include/module.h
@@ -0,0 +1,11 @@
+#ifndef _MODULE_H
+#define _MODULE_H
+
+#include "frida-gumjs.h"
+
+void module_config(void);
+
+void module_init(void);
+
+#endif
+
diff --git a/frida_mode/src/main.c b/frida_mode/src/main.c
index d8521300..bb6e4109 100644
--- a/frida_mode/src/main.c
+++ b/frida_mode/src/main.c
@@ -21,6 +21,7 @@
#include "intercept.h"
#include "js.h"
#include "lib.h"
+#include "module.h"
#include "output.h"
#include "persistent.h"
#include "prefetch.h"
@@ -197,6 +198,7 @@ __attribute__((visibility("default"))) void afl_frida_start(void) {
instrument_config();
js_config();
lib_config();
+ module_config();
output_config();
persistent_config();
prefetch_config();
@@ -214,6 +216,7 @@ __attribute__((visibility("default"))) void afl_frida_start(void) {
entry_init();
instrument_init();
lib_init();
+ module_init();
persistent_init();
prefetch_init();
seccomp_init();
diff --git a/frida_mode/src/module.c b/frida_mode/src/module.c
new file mode 100644
index 00000000..65b394cd
--- /dev/null
+++ b/frida_mode/src/module.c
@@ -0,0 +1,106 @@
+#include "intercept.h"
+#include "module.h"
+#include "util.h"
+
+#if defined(__linux__)
+ #include <dlfcn.h>
+ #include <link.h>
+#endif
+
+static guint page_size = 0;
+static gboolean handle_dlclose = FALSE;
+
+void module_config(void) {
+
+#if defined(__linux__)
+ handle_dlclose = (getenv("AFL_FRIDA_NO_MODULE") == NULL);
+#else
+ FWARNF("AFL_FRIDA_MODULE not supported");
+#endif
+
+}
+
+typedef struct {
+
+ GumMemoryRange range;
+ GumPageProtection protection;
+ GumFileMapping file;
+
+} gum_range_t;
+
+gboolean found_range(const GumRangeDetails *details, gpointer user_data) {
+
+ gum_range_t range = {0};
+ GArray * ranges = (GArray *)user_data;
+
+ range.range = *details->range;
+ range.protection = details->protection;
+ if (details->file != NULL) { range.file = *details->file; }
+
+ g_array_append_val(ranges, range);
+ return FALSE;
+
+}
+
+#if defined(__linux__)
+static int on_dlclose(void *handle) {
+
+ GArray * ranges = NULL;
+ struct link_map *lm = NULL;
+ gum_range_t * range = NULL;
+ GumAddress base;
+ GumAddress limit;
+ gpointer mem;
+
+ if (dlinfo(handle, RTLD_DI_LINKMAP, &lm) < 0) {
+
+ FFATAL("Failed to dlinfo: %s", dlerror());
+
+ }
+
+ FVERBOSE("on_dlclose: %s", lm->l_name);
+
+ ranges = g_array_new(FALSE, TRUE, sizeof(gum_range_t));
+ gum_module_enumerate_ranges(lm->l_name, GUM_PAGE_EXECUTE, found_range,
+ ranges);
+
+ int ret = dlclose(handle);
+ if (ret != 0) {
+
+ FWARNF("dlclose returned: %d (%s)", ret, dlerror());
+ return ret;
+
+ }
+
+ for (guint i = 0; i < ranges->len; i++) {
+
+ range = &g_array_index(ranges, gum_range_t, i);
+ base = range->range.base_address;
+ limit = base + range->range.size;
+ FVERBOSE("Reserving range: 0x%016lx, 0x%016lX", base, limit);
+ mem = gum_memory_allocate(GSIZE_TO_POINTER(base), range->range.size,
+ page_size, GUM_PAGE_NO_ACCESS);
+ if (mem == NULL) { FATAL("Failed to allocate %p (%d)", mem, errno); }
+
+ }
+
+ g_array_free(ranges, TRUE);
+ return 0;
+
+}
+
+#endif
+
+void module_init(void) {
+
+ FOKF(cBLU "Module" cRST " - " cYEL " [%c]", handle_dlclose ? 'X' : ' ');
+
+#if defined(__linux__)
+ if (!handle_dlclose) { return; }
+
+ page_size = gum_query_page_size();
+ intercept_hook(dlclose, on_dlclose, NULL);
+#endif
+
+}
+