-
Notifications
You must be signed in to change notification settings - Fork 17
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
dlsym(RTLD_NEXT) doesn't always work #2
Comments
Interesting. Looking at your commit history, this is specifically for when the library you're hooking was opened with If I hack up your test case a bit: #define _GNU_SOURCE
#include <dlfcn.h>
#include <stdio.h>
__attribute__((__weak__))
void gnutls_handshake(void);
int main(void)
{
void *handle = dlopen("libgnutls.so",RTLD_LOCAL|RTLD_NOW);
printf("directly: gnutls_handshake = %p\n", gnutls_handshake);
void *fp=dlsym(handle,"gnutls_handshake");
printf("through handle: gnutls_handshake = %p\n", fp);
fp=dlsym(RTLD_DEFAULT,"gnutls_handshake");
printf("through RTLD_DEFAULT: gnutls_handshake = %p\n", fp);
return 0;
} I get
but with this preload: #include <stdio.h>
void gnutls_handshake(void) {}
__attribute__((__constructor__))
void ctor(void) {
fprintf(stderr, "address of preloaded gnutls_handshake is %p\n", gnutls_handshake);
} I get
In other words, without the What was your failing case? (Is there an actual GnuTLS-using program I can poke at?) |
Oh I had completely forgotten about that test case. I'm sorry, I don't remember the actual program that led me to write this code. I think it might have to do with program A loading a library B using RTLD_LOCAL, where that library B depends on library C (here C being GnuTLS). |
OK, I can reproduce this (glibc 2.19, Debian 8.1 x86_64): if I have a library Oddly enough, directly asking for the address of
Source code in this gist. This smells like a glibc bug. On my FreeBSD 10.2 VM (swapping
I think we should start by reporting this to the glibc maintainers and asking what the intended behavior is. I don't think there's a way to reliably determine in |
Thanks for investigating this further! I agree that there is some ambiguity in some cases. The ambiguity arises when multiple libraries that define the same symbol get loaded AND some of those symbols are being linked with the preloaded symbol instead. I think this can happen in the following cases:
I'll try to come up with test cases for both of these. Ideally a wrapper function would be able to identify the exact function that is being replaced, but I'm not sure if that's possible. |
Sorry, I got distracted by a bug in ld. This gist builds test cases for each of the above scenarios, as well as 7 test binaries. 6 binaries linked to two versions of each scenario and 1 binary that only uses An interesting test case is for example |
Sometimes you need to do some magic to find the entry point for the real function. Here's an example that uses
dl_iterate_phdr
whendlsym(RTLD_NEXT,...)
fails: https://github.com/jethrogb/ssltrace/blob/bf17c150a7/ssltrace.cpp#L74-L112The text was updated successfully, but these errors were encountered: