Subject: Question about dlsym() and lib searching
To: None <tech-userlevel@NetBSD.ORG>
From: Dave Huang <khym@bga.com>
List: tech-userlevel
Date: 04/29/1997 19:46:48
I was trying to figure out why runsocks from the SOCKS5 distribution
wasn't working right, and I think I've found the reason... but first,
let me ask if runsocks from socks5-beta-0.17.2-exportable works for
anybody under NetBSD... maybe I'm just doing something wrong :)

I don't think I am though... runsocks works by setting the LD_PRELOAD
environment variable (which doesn't seem to be documented in the ld.so
manpage, btw) in order to replace certain libc functions with socks
versions. That part seems to be working fine; the problem is that the
socks versions need to call the real libc versions, and they do that
by doing a dlopen() on the full path to libc, then using dlsym() to
get the address of the function. Now, the manpage for dlsym() says:

     dlsym() looks for a definition of symbol in the shared object designated
     by handle. The symbols address is returned. If the symbol cannot be re-
     solved, NULL is returned.

However, it seems to be returning the address of the SOCKS version of
the function instead of the one in the real libc. So, the function
ends up calling itself over and over until the stack limit is
exceeded, and the thing dumps core :)

I took a look at rtld.c, and it looks like dlopen() is supposed to set
some RTLD_DL flag, which dlsym() will then use to restrict the search
to just that one library. But apparently, since libc is already open,
dlopen() just returns a handle to the existing library, and returns
without setting that flag. The call to dlsym() then goes through the
standard search path, and finds the socks version of the function
first, since it was preloaded and got put at the beginning of the
chain.

So, my question is, is this correct behavior? And I guess another more
practical question is how do I get runsocks to work? :)

Name: Dave Huang     |   Mammal, mammal / their names are called /
INet: khym@bga.com   |   they raise a paw / the bat, the cat /
FurryMUCK: Dahan     |   dolphin and dog / koala bear and hog -- TMBG
Dahan: Hani G Y+C 21 Y++ L+++ W- C++ T++ A+ E+ S++ V++ F- Q+++ P+ B+ PA+ PL++