Follow @Openwall on Twitter for new release announcements and other news
[<prev] [next>] [thread-next>] [day] [month] [year] [list]
Date: Wed, 06 Feb 2019 00:02:39 +0300
From: Alexey Izbyshev <>
Subject: dlsym(handle) may search in unrelated libraries


I've discovered a bug in musl dynamic loader (tested on 1.1.21) which is 
demonstrated by the following simple example:

$ cat bar.c
int bar = 42;
$ musl-gcc -fPIC -shared bar.c -o
$ cat foo.c
extern int bar;
int *foo = &bar;
$ musl-gcc -fPIC -shared foo.c -L. -lbar -Wl,-rpath='$ORIGIN' -o
$ cat main.c
#include <dlfcn.h>
#include <stdio.h>

int main(void) {
   if (!dlopen("", RTLD_NOW))
     return 1;
   void *h = dlopen("", RTLD_NOW);
   printf("%p\n", dlsym(h, "bar"));
$ musl-gcc main.c -Wl,-rpath='$ORIGIN' -ldl
$ ./a.out

dlsym(handle) is supposed to search only in the library referred to by 
the handle and in its dependencies. "" doesn't have 
dependencies and doesn't have a definition for "bar", so dlsym(h, "bar") 
should return NULL, but it finds "bar" in instead.

The problem occurs because of the following:
1) Initially, "deps" in dso structure for is NULL.
2) When dlopen("") is called, "first_load" is true, despite 
that it's not actually the first load (ldso/dynlink.c:1835):

     /* First load handling */
     int first_load = !p->deps;
     if (first_load) {

3) load_deps() then iterates over the dso list starting from 
"", treating all libraries found in DT_NEEDED of each processed 
dso as dependencies of "". However, the dso list already 
contains "" loaded earlier, so "" (which is needed by 
"") is treated as a dependency of "". As a result, 
dlsym(h, "bar") succeeds.

It's also notable that "" and "" were loaded with 
RTLD_LOCAL, but this bug effectively makes their symbols available in 
such searches regardless of the scope of a library used with dlsym().

ISTM that load_deps(p) was written to work only in real "first load" 
situations, where "p" is initially the last dso in the list, and new 
dsos are only added to the list in the course of recursive loading of 
the dependencies of "p".

Could this be fixed? Thanks!

(Please CC me on replying, I'm not subscribed to the list.)


Powered by blists - more mailing lists

Confused about mailing lists and their use? Read about mailing lists on Wikipedia and check out these guidelines on proper formatting of your messages.