From 5369407082db53b68a1100e7b5391a1e9c36e621 Mon Sep 17 00:00:00 2001 From: Orfeas <38209077+0xfea5@users.noreply.github.com> Date: Tue, 16 Apr 2024 00:19:57 +0300 Subject: Parse /proc/pid/maps --- src/main.c | 15 ++++-------- src/util.c | 15 ++++++++++++ src/util.h | 16 +++++++++++++ src/vm.c | 77 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ src/vm.h | 19 ++++++++++++++++ 5 files changed, 132 insertions(+), 10 deletions(-) create mode 100644 src/util.c create mode 100644 src/util.h create mode 100644 src/vm.c create mode 100644 src/vm.h diff --git a/src/main.c b/src/main.c index 1d23902..e255ec5 100644 --- a/src/main.c +++ b/src/main.c @@ -1,17 +1,12 @@ +#include +#include #include #include #include #include #include - -#define ERROR(...) \ - do { \ - fprintf(stderr, __VA_ARGS__); \ - exit(1); \ - } while (0) - -#define LOG(...) \ - fprintf(stderr, __VA_ARGS__) +#include "util.h" +#include "vm.h" int main(int argc, char *argv[]) { @@ -29,7 +24,7 @@ int main(int argc, char *argv[]) LOG("Attached to process %d\n", pid); /* Do stuff ... */ - sleep(5); + parse_vmmap(pid); ptrace(PTRACE_DETACH, pid, NULL, NULL); LOG("Detached from process %d\n", pid); diff --git a/src/util.c b/src/util.c new file mode 100644 index 0000000..11796ce --- /dev/null +++ b/src/util.c @@ -0,0 +1,15 @@ +#include +#include +#include +#include "util.h" + +void* xmalloc(size_t size) +{ + void *block = malloc(size); + if (!block) { + perror("malloc"); + exit(1); + } + memset(block, 0, size); + return block; +} diff --git a/src/util.h b/src/util.h new file mode 100644 index 0000000..5fd680e --- /dev/null +++ b/src/util.h @@ -0,0 +1,16 @@ +#ifndef _UTIL_H_ +#define _UTIL_H_ +#include + +#define ERROR(...) \ + do { \ + fprintf(stderr, __VA_ARGS__); \ + exit(1); \ + } while (0) + +#define LOG(...) \ + fprintf(stderr, __VA_ARGS__) + +void* xmalloc(size_t size); + +#endif // _UTIL_H_ diff --git a/src/vm.c b/src/vm.c new file mode 100644 index 0000000..8e62426 --- /dev/null +++ b/src/vm.c @@ -0,0 +1,77 @@ +#include +#include +#include +#include +#include +#include "vm.h" +#include "util.h" + +static void strtrim (char *str) +{ + char *begin = str; + while (isspace(*begin)) { + begin++; + } + size_t len = strlen(begin); + char *end = begin + len; + while (end > begin && isspace(*end)) { + end++; + } + + memmove(str, begin, end - begin + 1); +} + +VMMapping* parse_vmmap (int pid) +{ + char fmaps_path[128]; + sprintf(fmaps_path, "/proc/%d/maps", pid); + FILE *fmaps; + + if ((fmaps = fopen(fmaps_path, "r")) == NULL) { + perror("fopen"); + exit(1); + } + + uint64_t begin, end; + char perms[16]; + char filename[1024]; + VMMapping *head = NULL; + VMMapping *cur = NULL; + + while( + fscanf(fmaps, "%lx-%lx %15s %*x %*x:%*x %*u%1023[^\n]", + &begin, &end, perms, filename) != EOF) { + strtrim(filename); + + VMMapping *new_mapping = xmalloc(sizeof(VMMapping)); + *new_mapping = (VMMapping) { + .begin = begin, + .end = end, + .r = perms[0] == 'r', + .w = perms[1] == 'w', + .x = perms[2] == 'x', + .s = perms[3] == 's', + .p = perms[3] == 'p', + .name = strdup(filename), + .next = NULL, + }; + + if (head) { + cur->next = new_mapping; + cur = new_mapping; + } else { + head = cur = new_mapping; + } + + LOG("%lx-%lx %c%c%c%c %s\n", + cur->begin, + cur->end, + cur->r ? 'r' : '-', + cur->w ? 'w' : '-', + cur->x ? 'x' : '-', + cur->s ? 's' : 'p', + cur->name); + } + + return head; +} diff --git a/src/vm.h b/src/vm.h new file mode 100644 index 0000000..25849e7 --- /dev/null +++ b/src/vm.h @@ -0,0 +1,19 @@ +#ifndef _VM_H_ +#define _VM_H_ +#include + +typedef struct VMMapping { + uint64_t begin; + uint64_t end; + uint8_t r:1; + uint8_t w:1; + uint8_t x:1; + uint8_t s:1; + uint8_t p:1; + const char *name; + struct VMMapping *next; +} VMMapping; + +VMMapping* parse_vmmap (int pid); + +#endif // _VM_H_ -- cgit v1.2.3