From 814242225725f338e35f9af372ee55daba5b4f38 Mon Sep 17 00:00:00 2001 From: David Carlier Date: Wed, 23 Oct 2019 20:07:16 +0100 Subject: Porting libtokencap to Darwin. Reading only main addresses and read only's. --- libtokencap/libtokencap.so.c | 37 +++++++++++++++++++++++++++++++++++-- 1 file changed, 35 insertions(+), 2 deletions(-) (limited to 'libtokencap/libtokencap.so.c') diff --git a/libtokencap/libtokencap.so.c b/libtokencap/libtokencap.so.c index 39095beb..212fa31d 100644 --- a/libtokencap/libtokencap.so.c +++ b/libtokencap/libtokencap.so.c @@ -26,10 +26,15 @@ #include "../types.h" #include "../config.h" -#ifndef __linux__ -#error "Sorry, this library is Linux-specific for now!" +#if !defined(__linux__) && !defined(__APPLE__) +#error "Sorry, this library is unsupported in this platform for now!" #endif /* !__linux__ */ +#if defined(__APPLE__) +#include +#include +#endif + /* Mapping data and such */ #define MAX_MAPPINGS 1024 @@ -46,6 +51,7 @@ static FILE* __tokencap_out_file; static void __tokencap_load_mappings(void) { +#if defined(__linux__) u8 buf[MAX_LINE]; FILE* f = fopen("/proc/self/maps", "r"); @@ -69,7 +75,34 @@ static void __tokencap_load_mappings(void) { } fclose(f); +#elif defined(__APPLE__) + struct vm_region_submap_info_64 region; + mach_msg_type_number_t cnt = VM_REGION_SUBMAP_INFO_COUNT_64; + vm_address_t base = 0; + vm_size_t size = 0; + natural_t depth = 0; + + __tokencap_ro_loaded = 1; + + while (1) { + + if (vm_region_recurse_64(mach_task_self(), &base, &size, &depth, + (vm_region_info_64_t)®ion, &cnt) != KERN_SUCCESS) break; + + if (region.is_submap) { + depth++; + } else { + /* We only care of main map addresses and the read only kinds */ + if ((region.protection & VM_PROT_READ) && !(region.protection & VM_PROT_WRITE)) { + __tokencap_ro[__tokencap_ro_cnt].st = (void *)base; + __tokencap_ro[__tokencap_ro_cnt].en = (void *)(base + size); + + if (++__tokencap_ro_cnt == MAX_MAPPINGS) break; + } + } + } +#endif } /* Check an address against the list of read-only mappings. */ -- cgit 1.4.1 From b4b26d420771ca19a26828d9fdd53cdd66dab9ee Mon Sep 17 00:00:00 2001 From: David Carlier Date: Thu, 24 Oct 2019 18:48:08 +0100 Subject: FreeBSD implementation --- libtokencap/Makefile | 3 +++ libtokencap/libtokencap.so.c | 50 +++++++++++++++++++++++++++++++++++++++++++- 2 files changed, 52 insertions(+), 1 deletion(-) (limited to 'libtokencap/libtokencap.so.c') diff --git a/libtokencap/Makefile b/libtokencap/Makefile index 702ce696..07c13144 100644 --- a/libtokencap/Makefile +++ b/libtokencap/Makefile @@ -27,6 +27,9 @@ endif ifeq "$(shell uname)" "Darwin" TARGETS = libtokencap.so endif +ifeq "$(shell uname)" "FreeBSD" + TARGETS = libtokencap.so +endif all: $(TARGETS) libtokencap.so: libtokencap.so.c ../config.h 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 #include #include +#include #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 #include +#elif defined(__FreeBSD__) +#include +#include +#include +#include #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 } -- cgit 1.4.1 From e0ff20dd372579f4e4a540df00c8d6f4e218672b Mon Sep 17 00:00:00 2001 From: hexcoder- Date: Fri, 25 Oct 2019 01:51:53 +0200 Subject: cosmetics --- libtokencap/libtokencap.so.c | 34 +++++++++++++++++++--------------- 1 file changed, 19 insertions(+), 15 deletions(-) (limited to 'libtokencap/libtokencap.so.c') diff --git a/libtokencap/libtokencap.so.c b/libtokencap/libtokencap.so.c index 1050378c..ddeae8b8 100644 --- a/libtokencap/libtokencap.so.c +++ b/libtokencap/libtokencap.so.c @@ -27,18 +27,18 @@ #include "../types.h" #include "../config.h" -#if !defined(__linux__) && !defined(__APPLE__) && !defined(__FreeBSD__) -#error "Sorry, this library is unsupported in this platform for now!" -#endif /* !__linux__ */ - -#if defined(__APPLE__) -#include -#include -#elif defined(__FreeBSD__) -#include -#include -#include -#include +#if !defined __linux__ && !defined __APPLE__ && !defined __FreeBSD__ +# error "Sorry, this library is unsupported in this platform for now!" +#endif /* !__linux__ && !__APPLE__ && ! __FreeBSD__ */ + +#if defined __APPLE__ +# include +# include +#elif defined __FreeBSD__ +# include +# include +# include +# include #endif /* Mapping data and such */ @@ -57,7 +57,8 @@ static FILE* __tokencap_out_file; static void __tokencap_load_mappings(void) { -#if defined(__linux__) +#if defined __linux__ + u8 buf[MAX_LINE]; FILE* f = fopen("/proc/self/maps", "r"); @@ -81,7 +82,9 @@ static void __tokencap_load_mappings(void) { } fclose(f); -#elif defined(__APPLE__) + +#elif defined __APPLE__ + struct vm_region_submap_info_64 region; mach_msg_type_number_t cnt = VM_REGION_SUBMAP_INFO_COUNT_64; vm_address_t base = 0; @@ -108,7 +111,8 @@ static void __tokencap_load_mappings(void) { } } -#elif defined(__FreeBSD__) +#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]); -- cgit 1.4.1