From mboxrd@z Thu Jan 1 00:00:00 1970 X-Msuck: nntp://news.gmane.org/gmane.linux.lib.musl.general/13720 Path: news.gmane.org!.POSTED.blaine.gmane.org!not-for-mail From: Alexey Izbyshev Newsgroups: gmane.linux.lib.musl.general Subject: Re: dlsym(handle) may search in unrelated libraries Date: Thu, 7 Feb 2019 16:42:15 +0300 Message-ID: <8031d662-bf4f-1321-155c-942a8c92e1d8@ispras.ru> References: <20190206160248.GB5469@voyager> <20190206202518.GC5469@voyager> <96c367533236e3e203f04a994ee65c47@ispras.ru> <20190207053327.GD5469@voyager> Reply-To: musl@lists.openwall.com Mime-Version: 1.0 Content-Type: text/plain; charset=utf-8; format=flowed Content-Transfer-Encoding: 7bit Injection-Info: blaine.gmane.org; posting-host="blaine.gmane.org:195.159.176.226"; logging-data="247166"; mail-complaints-to="usenet@blaine.gmane.org" User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:60.0) Gecko/20100101 Thunderbird/60.4.0 Cc: musl@lists.openwall.com To: Markus Wichmann Original-X-From: musl-return-13736-gllmg-musl=m.gmane.org@lists.openwall.com Thu Feb 07 14:42:01 2019 Return-path: Envelope-to: gllmg-musl@m.gmane.org Original-Received: from mother.openwall.net ([195.42.179.200]) by blaine.gmane.org with smtp (Exim 4.89) (envelope-from ) id 1grjwH-0012CB-3q for gllmg-musl@m.gmane.org; Thu, 07 Feb 2019 14:42:01 +0100 Original-Received: (qmail 30402 invoked by uid 550); 7 Feb 2019 13:41:59 -0000 Mailing-List: contact musl-help@lists.openwall.com; run by ezmlm Precedence: bulk List-Post: List-Help: List-Unsubscribe: List-Subscribe: List-ID: Original-Received: (qmail 30384 invoked from network); 7 Feb 2019 13:41:58 -0000 In-Reply-To: <20190207053327.GD5469@voyager> Content-Language: en-US Xref: news.gmane.org gmane.linux.lib.musl.general:13720 Archived-At: On 2/7/19 8:33 AM, Markus Wichmann wrote: > Let's consider the original code. liba depends on libb, which depends on > libc. dlopen("liba") returns a handle with libb and libc in the deps, > but libb->deps == 0. If we now call dlopen("libb"), that does the right > thing, but only because libb happens to be the last lib in the chain. If > we'd have loaded libx, liby, and libz before trying libb, it would add > all the symbols of libs x, y, and z to the libb handle. Your description almost captures the problem, but is imprecise in the last part: "it would add all the symbols of libs x, y, and z to the libb handle". load_deps() looks only at DT_NEEDED entries of libraries it iterates over, so, for example, if libx depends on both liby and libz, then liby and libz (but not libx) would be added to deps of libb. Moreover, consider the following dependency hierarchy (loaded on dlopen("liba")): liba libb libd libe In this case, even dlopen("libb") wouldn't do the right thing because load_deps() would find libe in DT_NEEDED of libd and add it to deps of libb. > > As you said, order is important. What is the correct order, depth-first > or breadth-first? I think it should be depth-first, but lack any > authoritative knowledge on this. dlsym(handle) uses so-called "dependency order"[1], which is breadth-first[2]. This is what musl current does in cases when load_deps() is called on a real first load of a library (so that everything that's further in the dso list are implicitly loaded dependencies of this library). So with the following dependency tree: > > liba->libb->libc > `>libx->liby > > the handle for liba would list libc before libx. > The correct order is what load_deps() does currently: liba libb libx libc liby > Easiest implementation is probably still going to be recursive. Let's hope the dependency trees don't get too wild. I think the easiest way is simply to modify load_deps() to always traverse DT_NEEDED in breadth-first order without relying on the dso list in the outer loop. load_deps() already effectively maintains a queue (deps) that can be used for BFS, so no recursion is needed. > > I'll look into it after work. > Thanks for following this up, Markus! [1] http://pubs.opengroup.org/onlinepubs/9699919799/functions/dlsym.html [2] http://pubs.opengroup.org/onlinepubs/9699919799/functions/dlopen.html Alexey