From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from green.apple.relay.mailchannels.net (green.apple.relay.mailchannels.net [23.83.208.77]) by sourceware.org (Postfix) with ESMTPS id CDDF9385842C for ; Thu, 25 May 2023 15:12:14 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org CDDF9385842C Authentication-Results: sourceware.org; dmarc=none (p=none dis=none) header.from=gotplt.org Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=gotplt.org X-Sender-Id: dreamhost|x-authsender|siddhesh@gotplt.org Received: from relay.mailchannels.net (localhost [127.0.0.1]) by relay.mailchannels.net (Postfix) with ESMTP id 813291422A9; Thu, 25 May 2023 15:12:13 +0000 (UTC) Received: from pdx1-sub0-mail-a306.dreamhost.com (unknown [127.0.0.6]) (Authenticated sender: dreamhost) by relay.mailchannels.net (Postfix) with ESMTPA id DACD2142486; Thu, 25 May 2023 15:12:11 +0000 (UTC) ARC-Seal: i=1; s=arc-2022; d=mailchannels.net; t=1685027533; a=rsa-sha256; cv=none; b=9SIXPOQHj/WzQqJEn2LgR3H0+CdwX6GjowtXZgLtuWk5wyV2I6S1hjoEycu3qbF0FmKYzm s/skoA9rWVaVU0GEFas+eINZB67CgXk6YWUmhxYnfmZQtAGRR2AStY8HSWrplmWg2bRRMn LW1RkK6wfR66BAOmy50Jmk7PpR0q9C3u9SBxTlBUgnKpgfQHlYSA+qQvI4ZJ1K54SXbpX0 LsGzJcQxD1M59BTeACBDSZL7txsx85qKBV8fbrp61dXKG7619KKUHUHkeKqzb1FjgCxQq8 WRYILzPezMXhlbx3W8IjpS47aGEilE41QfNGWpBJj900Sb7YRaKiZHydKft5Sw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=mailchannels.net; s=arc-2022; t=1685027533; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=+7J3DyjOtb4XeAqq+8T33+Qx2aqDguf9Z2rrdQEtglQ=; b=bCaMqe+ei+5/cTUVNshfyEs1REvptYqgTtRsKudPMQ40g8tZxHtj8HNrbuTC25bXNwR50E pgY5/8DBUHTRMeuWtjRmZFtL9GjwBsyekmVnig0M8pDcOhzDlm4SfKfL1AG9QZrvGlDIAg RoCJx+2dMV7NqaGRv9GqJUAAoVRnDd/a08Zrej2v6NoUlE7y1fXhrOmndV1YQt1kad9Ddh ImaACy/nQclI4jmbgZUs7PqY6qF8ppI4ysylgWjz2i/l2WErQu1G45qH+fxDk4W1zHtRFb KqOvl3MvbQrYLT+i4JjzYZxpUUZMerJZgVjdZQ3JKj51Bidhe1qTlLdrzp3uUA== ARC-Authentication-Results: i=1; rspamd-5cdf8fd7d9-wwc4v; auth=pass smtp.auth=dreamhost smtp.mailfrom=siddhesh@gotplt.org X-Sender-Id: dreamhost|x-authsender|siddhesh@gotplt.org X-MC-Relay: Good X-MailChannels-SenderId: dreamhost|x-authsender|siddhesh@gotplt.org X-MailChannels-Auth-Id: dreamhost X-Fumbling-Hysterical: 721b317450226f96_1685027533222_1465783825 X-MC-Loop-Signature: 1685027533222:1986397126 X-MC-Ingress-Time: 1685027533221 Received: from pdx1-sub0-mail-a306.dreamhost.com (pop.dreamhost.com [64.90.62.162]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384) by 100.127.59.19 (trex/6.8.1); Thu, 25 May 2023 15:12:13 +0000 Received: from [192.168.0.182] (bras-vprn-toroon4834w-lp130-02-142-113-138-85.dsl.bell.ca [142.113.138.85]) (using TLSv1.3 with cipher TLS_AES_128_GCM_SHA256 (128/128 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits)) (No client certificate requested) (Authenticated sender: siddhesh@gotplt.org) by pdx1-sub0-mail-a306.dreamhost.com (Postfix) with ESMTPSA id 4QRs3b2JK0z1k; Thu, 25 May 2023 08:12:11 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gotplt.org; s=dreamhost; t=1685027531; bh=+7J3DyjOtb4XeAqq+8T33+Qx2aqDguf9Z2rrdQEtglQ=; h=Date:Subject:To:Cc:From:Content-Type:Content-Transfer-Encoding; b=RBGzcToaw4SYcFGKAAwK+SiNiIzsgGPuJgSOsDc/KyjpDiRrqNlsA0xGInAodBjDG NJu2tPeNo23ORCmBqWNZ8peSn9/PdFiOV1TPt+R8+M7klO7f0JNbuhLt083Nu6OHrv XtEIBdWvdGTXf2apfhxe4NQjYHbAS/GHuU9qvD67vNGhcZt9b+CF2cMj/IgdBhXyJD R4M5bM6kph39E1eZ5JiMsDOCKgHyczEJBC9P2ecFOlEeh6vlQURe4Q5JqBbQOqX/oV 2o8Svh2Ih/wF54LRWuRSQH0R/wpoZPHN31TOI7JrTd83I5rkKCe7bqQf2+yJCg07xo UcGb2dBfDkYEA== Message-ID: Date: Thu, 25 May 2023 11:12:10 -0400 MIME-Version: 1.0 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:102.0) Gecko/20100101 Thunderbird/102.10.0 Subject: Re: [PATCH v3] elf: Add test for locating libraries in root dir (bug 30435) Content-Language: en-US To: Qixing ksyx Xue , libc-alpha@sourceware.org Cc: carlos@redhat.com, mooodyhunter@outlook.com References: <85084c05-c696-9e68-4c74-f0d6686e2de2@gotplt.org> From: Siddhesh Poyarekar In-Reply-To: Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 7bit X-Spam-Status: No, score=-3037.4 required=5.0 tests=BAYES_00,DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,GIT_PATCH_0,KAM_SHORT,NICE_REPLY_A,RCVD_IN_DNSWL_NONE,SPF_HELO_NONE,SPF_PASS,TXREP,T_SCC_BODY_TEXT_LINE autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on server2.sourceware.org List-Id: On 2023-05-25 10:56, Qixing ksyx Xue wrote: > When dlopen is being called, efforts have been made to improve > future lookup performance. This includes marking a search path > as non-existent using `stat`. However, if the root directory > is given as a search path, there exists a bug which erroneously > marks it as non-existing. > > The bug is reproduced under the following sequence: > > 1. dlopen is called to open a shared library, with at least: > 1) a dependency 'A.so' not directly under the '/' directory > (e.g. /lib/A.so), and > 2) another dependency 'B.so' resides in '/'. > 2. for this bug to reproduce, 'A.so' should be searched *before* 'B.so'. > 3. it first tries to find 'A.so' in /, (e.g. /A.so): > - this will (obviously) fail, > - since it's the first time we have seen the '/' directory, > its 'status' is 'unknown'. > 4. `buf[buflen - namelen - 1] = '\0'` is executed: > - it intends to remove the leaf and its final slash, > - because of the speciality of '/', its buflen == namelen + 1, > - it erroneously clears the entire buffer. > 6. it then calls 'stat' with the empty buffer: > - which will result in an error. > 7. so it marks '/' as 'nonexisting', future lookups will not consider > this path. > 8. while /B.so *does* exist, failure to look it up in the '/' > directory leads to a 'cannot open shared object file' error. > > This patch fixes the bug by preventing 'buflen', an index to put '\0', > from being set to 0, so that the root '/' is always kept. > Relative search paths are always considered as 'existing' so this > wont be affected. > > Writeup by Moody Liu > > Suggested-by: Carlos O'Donell > Signed-off-by: Qixing ksyx Xue Reviewed-by: Siddhesh Poyarekar I'll test and commit this shortly. Thanks, Sid > --- > elf/Makefile | 3 +++ > elf/tst-rootdir-lib.c | 23 +++++++++++++++++++ > elf/tst-rootdir.c | 37 +++++++++++++++++++++++++++++++ > elf/tst-rootdir.root/preclean.req | 0 > elf/tst-rootdir.script | 1 + > 5 files changed, 64 insertions(+) > create mode 100644 elf/tst-rootdir-lib.c > create mode 100644 elf/tst-rootdir.c > create mode 100644 elf/tst-rootdir.root/preclean.req > create mode 100644 elf/tst-rootdir.script > > diff --git a/elf/Makefile b/elf/Makefile > index e262f3e6b1..8640138a72 100644 > --- a/elf/Makefile > +++ b/elf/Makefile > @@ -325,6 +325,7 @@ static-dlopen-environment = \ > LD_LIBRARY_PATH=$(ld-library-path):$(common-objpfx)dlfcn > tst-tls9-static-ENV = $(static-dlopen-environment) > tst-single_threaded-static-dlopen-ENV = $(static-dlopen-environment) > +tst-rootdir-ENV = LD_LIBRARY_PATH=/ > > tests += \ > argv0test \ > @@ -506,6 +507,7 @@ tests-container += \ > tst-dlopen-tlsmodid-container \ > tst-pldd \ > tst-preload-pthread-libc \ > + tst-rootdir \ > # tests-container > > test-srcs = \ > @@ -855,6 +857,7 @@ modules-names += \ > tst-relsort1mod1 \ > tst-relsort1mod2 \ > tst-ro-dynamic-mod \ > + tst-rootdir-lib \ > tst-single_threaded-mod1 \ > tst-single_threaded-mod2 \ > tst-single_threaded-mod3 \ > diff --git a/elf/tst-rootdir-lib.c b/elf/tst-rootdir-lib.c > new file mode 100644 > index 0000000000..007c4b0095 > --- /dev/null > +++ b/elf/tst-rootdir-lib.c > @@ -0,0 +1,23 @@ > +/* Simple library for testing locating library in root directories. > + Copyright The GNU Toolchain Authors. > + This file is part of the GNU C Library. > + > + The GNU C Library is free software; you can redistribute it and/or > + modify it under the terms of the GNU Lesser General Public > + License as published by the Free Software Foundation; either > + version 2.1 of the License, or (at your option) any later version. > + > + The GNU C Library is distributed in the hope that it will be useful, > + but WITHOUT ANY WARRANTY; without even the implied warranty of > + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU > + Lesser General Public License for more details. > + > + You should have received a copy of the GNU Lesser General Public > + License along with the GNU C Library; if not, see > + . */ > + > +const char * > +test_func (void) > +{ > + return "Success"; > +} > diff --git a/elf/tst-rootdir.c b/elf/tst-rootdir.c > new file mode 100644 > index 0000000000..83284ea59e > --- /dev/null > +++ b/elf/tst-rootdir.c > @@ -0,0 +1,37 @@ > +/* Test code for locating libraries in root directories. > + Copyright The GNU Toolchain Authors. > + This file is part of the GNU C Library. > + > + The GNU C Library is free software; you can redistribute it and/or > + modify it under the terms of the GNU Lesser General Public > + License as published by the Free Software Foundation; either > + version 2.1 of the License, or (at your option) any later version. > + > + The GNU C Library is distributed in the hope that it will be useful, > + but WITHOUT ANY WARRANTY; without even the implied warranty of > + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU > + Lesser General Public License for more details. > + > + You should have received a copy of the GNU Lesser General Public > + License along with the GNU C Library; if not, see > + . */ > + > +#include > +#include > +#include > +#include > + > +static int > +do_test (void) > +{ > + void *handle = dlopen ("libtest.so", RTLD_LAZY); > + TEST_VERIFY_EXIT (handle != NULL); > + typedef const char *(test_func_t) (void); > + test_func_t *func = dlsym (handle, "test_func"); > + assert (func != NULL); > + TEST_COMPARE_STRING (func (), "Success"); > + dlclose (handle); > + return 0; > +} > + > +#include > diff --git a/elf/tst-rootdir.root/preclean.req b/elf/tst-rootdir.root/preclean.req > new file mode 100644 > index 0000000000..e69de29bb2 > diff --git a/elf/tst-rootdir.script b/elf/tst-rootdir.script > new file mode 100644 > index 0000000000..f852c0ec34 > --- /dev/null > +++ b/elf/tst-rootdir.script > @@ -0,0 +1 @@ > +cp $B/elf/tst-rootdir-lib.so /libtest.so