How to do Binary instrumentation of syscall brk ? (x86-64 Linux) (maybe valgrind?)

后端 未结 2 1345
一整个雨季
一整个雨季 2021-01-16 23:44

I\'d like to instrument syscall brk (and other calls but this in first order, it\'s most important to me) in given binary (preferably on actual syscall/sysenter lev

相关标签:
2条回答
  • 2021-01-17 00:10

    LD_PRELOAD will trap C calls to brk(), but it won't trap the actual system call (int/syscall instruction). There's no portable way to trap those, but on Linux, ptrace will do it. Memory can also be allocated to a program by mmap(), so you'll need to intercept that call too.

    Of course, what it seems you're really looking for is rlimit().

    0 讨论(0)
  • 2021-01-17 00:24

    Yeah, I don't think you want valgrind for this.

    You can use LD_PRELOAD or linker tricks to capture brk(2): see these other discussions:

    Function interposition in Linux without dlsym

    Overriding 'malloc' using the LD_PRELOAD mechanism

    Code might look like this:

    #include <unistd.h>
    #include <dlfcn.h>
    
    /* prototype int brk(void *addr); */
    
    static int (*real_brk)(void *addr) = NULL;
    
    int brk(void * addr) {
    
        real_brk = dlsym(RTLD_NEXT, "brk");
        if (real_brk == NULL) {
                fprintf(stderr, "error mapping brk: %s\n", dlerror());
                return -1;
        }
        printf("calling brk(2) for %p\n", addr);
        return (real_brk (addr));
    }`   
    

    and then LD_PRELOAD that to intercept brk(2)

    0 讨论(0)
提交回复
热议问题