From patchwork Thu May 25 14:56:03 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Qixing ksyx Xue X-Patchwork-Id: 70082 Return-Path: X-Original-To: patchwork@sourceware.org Delivered-To: patchwork@sourceware.org Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 2E2CE3858428 for ; Thu, 25 May 2023 14:57:17 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 2E2CE3858428 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=sourceware.org; s=default; t=1685026637; bh=ESNVmVY8+/kRsW/YO6HEHJK0Fbt0nNImLnU4esocNgc=; h=To:Cc:Subject:Date:In-Reply-To:References:List-Id: List-Unsubscribe:List-Archive:List-Post:List-Help:List-Subscribe: From:Reply-To:From; b=yUQneEfwNizG27og46rGLcKm29G+BvCA+acGULsqfAusxu2JsKTY42Eo3tzlXqOvk pj0Yfhrc/Vkdmeyseqo76joD0DKOJzP5dHo+V4Od+Yrfu+SWP6PvEtwNtlAWOy8bjJ iIHm1uNFjEkFyVBiP+oFyJ6tNcwWjBqfEE7uKtZA= X-Original-To: libc-alpha@sourceware.org Delivered-To: libc-alpha@sourceware.org Received: from AUS01-SY4-obe.outbound.protection.outlook.com (mail-sy4aus01olkn2081f.outbound.protection.outlook.com [IPv6:2a01:111:f403:7005::81f]) by sourceware.org (Postfix) with ESMTPS id 27D7A3858422 for ; Thu, 25 May 2023 14:56:51 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 27D7A3858422 ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=TsfTqzVXncqFCQOpXOw3yk8Yt7cl4hqX1NE8xemB4S19/mchQTkI2K/0BgAIhEerU3tuLkXg5MD948UvuCmJcxTqWx0ZJKNmjqkIySaTjfAuiXVyn7goiZiVk/Xh9RaP1rLTlIW518PwY1NJiLk9qBGaVOSoNcOhp+9XeRAUX/Fnt1s5PV4q4d+CAIT2eaTgBnAaeWqgAP+pQ6KIkilhKkBfIefO7fYelKLR2b/CF/6IX5WkxadGRof4/dUYeJxaOtk/hJHklveDWrvts22+kqAa5TJ7EiUFeKLhsMtJydu7rT+c0IJ0kviOLh1KCgxBZkS+S/QuqiwgYXAyzYQd8g== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=microsoft.com; s=arcselector9901; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-AntiSpam-MessageData-ChunkCount:X-MS-Exchange-AntiSpam-MessageData-0:X-MS-Exchange-AntiSpam-MessageData-1; bh=ESNVmVY8+/kRsW/YO6HEHJK0Fbt0nNImLnU4esocNgc=; b=CPLgpY/LY7G0m7RL7oC80v0n74TO9y+u3ajNcRuE1T0YNauYqUcotQKI2yltFc1S/4ON250OhaweSnrj8QcMopXOwaPpM2w88Uk8jKHfWrRrxNDtlyLLy+0O1xIBb1jYlUqN3qzQYtTE4pGo6IKMRoDJUbrs462ngO1gyTjyR9M2UidgS/EsXpB8chETxoRE0cy6ylPckuNxrwtFNi34mUwwWxvojNA9GO8d8Cxt9AXqedq0KeRhFQY5q6hy2b2ezQhyYtWuaCQP9yJG/pnsaHyMo+gqzZxau+NRHAvex3BC9E4hg3FOVnZeN2PQ/svdhUk4vWqjSzyDKaXDe9tBUg== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=none; dmarc=none; dkim=none; arc=none Received: from ME2P282MB2242.AUSP282.PROD.OUTLOOK.COM (2603:10c6:220:eb::14) by SYBP282MB4200.AUSP282.PROD.OUTLOOK.COM (2603:10c6:10:1ab::14) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.6433.16; Thu, 25 May 2023 14:56:44 +0000 Received: from ME2P282MB2242.AUSP282.PROD.OUTLOOK.COM ([fe80::e7b2:ce6a:695e:ef76]) by ME2P282MB2242.AUSP282.PROD.OUTLOOK.COM ([fe80::e7b2:ce6a:695e:ef76%6]) with mapi id 15.20.6433.018; Thu, 25 May 2023 14:56:44 +0000 To: libc-alpha@sourceware.org, siddhesh@gotplt.org Cc: carlos@redhat.com, mooodyhunter@outlook.com, Qixing ksyx Xue Subject: [PATCH v3] elf: Add test for locating libraries in root dir (bug 30435) Date: Thu, 25 May 2023 10:56:03 -0400 Message-ID: X-Mailer: git-send-email 2.22.0.windows.1 In-Reply-To: <85084c05-c696-9e68-4c74-f0d6686e2de2@gotplt.org> References: <85084c05-c696-9e68-4c74-f0d6686e2de2@gotplt.org> X-TMN: [Tuo3n9m+vOfNqB6tap56YU4eN8qTj24CmZR9AtLJM9PsIiUZtudD7u/drWEGXazs] X-ClientProxiedBy: MN2PR01CA0062.prod.exchangelabs.com (2603:10b6:208:23f::31) To ME2P282MB2242.AUSP282.PROD.OUTLOOK.COM (2603:10c6:220:eb::14) X-Microsoft-Original-Message-ID: <20230525145602.471-1-qixingxue@outlook.com> MIME-Version: 1.0 X-MS-Exchange-MessageSentRepresentingType: 1 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: ME2P282MB2242:EE_|SYBP282MB4200:EE_ X-MS-Office365-Filtering-Correlation-Id: 877c7f74-2aca-416e-3994-08db5d3040fc X-MS-Exchange-SLBlob-MailProps: V85gaVfRD49+AmP8fiBIm83+Tu0fe3D8vnNZHNnilMFq0Dz3kaFqjpkLw30mTtgwSN3Yh5BWzqG7WTtJ7k/bw742EIdhcKRtowvZfy7zQEvtH0FC5DGn59a/RsHiovN4mDozGt4T8AmgTptl3gbuYciVgYAkIlBduaK3JDhzVWqGa1i+mXC9CPRTFtjZE8jhxRxsaC0v0mz4+mxu3Mrw2YW4wojoT+8DkpQ24VVdeVgk3ruq6VoIr4zRYxlZd3TlJMFQFhIhxyb+TaioAx+Z8Ns2OieXtiK40Ggx/1qBQzYHRFVNSEmIHwHDz3twdtsHkCvf7v8bDupXvFoUwF6R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VXx+pcL108xmVrG8YSjn1cI1ZwuvfvTiHadWGnaJG0diiSWw1zXOEyttV8bW3Bkx3P4a7yiiz72M35zC6FN4GfhcORwUoBhr/K7LT3puwk/wL4V/cw2nDrdCPclqtbZ3jCOBzUsHFzIqorcgUtuHJnK7ywi/pV1hoWPOufrallWQkcNKOQ6YL2tcva8uo6KFIV99GnetK9g26iEQosA3xlS5k93dXyKjhif8h9ku7aTySCw68UZsGmJkKD2VVvWPzdO+GESN/BbSG2zWqBTtU1H6JZOxcdBHroiIPbdCFcbz8lLHQbr1/0V3MfgdNULRFwzdjH06PB0MX+OTPbEu3Viq2Qn6IZxXUWGZuSkebC4jpoW9UeERZNicAcr3Of1hbZH4jNWex3HM= X-OriginatorOrg: outlook.com X-MS-Exchange-CrossTenant-Network-Message-Id: 877c7f74-2aca-416e-3994-08db5d3040fc X-MS-Exchange-CrossTenant-AuthSource: ME2P282MB2242.AUSP282.PROD.OUTLOOK.COM X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 25 May 2023 14:56:44.7377 (UTC) X-MS-Exchange-CrossTenant-FromEntityHeader: Hosted X-MS-Exchange-CrossTenant-Id: 84df9e7f-e9f6-40af-b435-aaaaaaaaaaaa X-MS-Exchange-CrossTenant-RMS-PersistedConsumerOrg: 00000000-0000-0000-0000-000000000000 X-MS-Exchange-Transport-CrossTenantHeadersStamped: SYBP282MB4200 X-Spam-Status: No, score=-10.4 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, FREEMAIL_FROM, GIT_PATCH_0, KAM_SHORT, SPF_HELO_PASS, 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 X-BeenThere: libc-alpha@sourceware.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Libc-alpha mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-Patchwork-Original-From: Qixing ksyx Xue via Libc-alpha From: Qixing ksyx Xue Reply-To: Qixing ksyx Xue Errors-To: libc-alpha-bounces+patchwork=sourceware.org@sourceware.org Sender: "Libc-alpha" 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 --- 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