Last active
September 18, 2015 19:05
-
-
Save geofft/01daf337b671762d0c3a to your computer and use it in GitHub Desktop.
LD_PRELOAD + RTLD_LOCAL
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdio.h> | |
void gnutls_handshake(void); | |
__attribute__((__constructor__)) | |
static void ctor(void) { | |
printf("%p from intermediate constructor\n", gnutls_handshake); | |
} | |
void intermediate_fn(void) { | |
printf("%p from intermediate function\n", gnutls_handshake); | |
gnutls_handshake(); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#define _GNU_SOURCE | |
#include <dlfcn.h> | |
#include <stdio.h> | |
int main(int argc, char *argv[]) | |
{ | |
void *handle; | |
void (*fp)(void); | |
int rtld_flags = RTLD_NOW; | |
if (argc > 1 && argv[1][0] == 'g') | |
rtld_flags |= RTLD_GLOBAL; | |
else | |
rtld_flags |= RTLD_LOCAL; | |
handle = dlopen("./intermediate.so", rtld_flags); | |
fp = dlsym(handle, "gnutls_handshake"); | |
printf("%p from main through dlsym intermediate.so\n", fp); | |
fp = dlsym(handle, "intermediate_fn"); | |
fp(); | |
handle = dlopen("libgnutls.so", RTLD_LOCAL | RTLD_NOW); | |
fp = dlsym(handle, "gnutls_handshake"); | |
printf("%p from main through dlsym libgnutls.so\n", fp); | |
return 0; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
all: intermediate.so preload.so main | |
intermediate.so: intermediate.c | |
cc -o $@ -fPIC -shared $< -lgnutls | |
preload.so: preload.c | |
cc -o $@ -fPIC -shared $< -ldl | |
main: main.c | |
cc -o $@ $< -ldl |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#define _GNU_SOURCE | |
#include <dlfcn.h> | |
#include <stdio.h> | |
void gnutls_handshake(void) { | |
printf("%p from RTLD_NEXT inside preload\n", dlsym(RTLD_NEXT, "gnutls_handshake")); | |
} | |
__attribute__((__constructor__)) | |
static void ctor(void) { | |
printf("%p from preload constructor\n", gnutls_handshake); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment