From patchwork Thu May 25 13:29:11 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: 70077 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 60D3E3858414 for ; Thu, 25 May 2023 13:30:18 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 60D3E3858414 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=sourceware.org; s=default; t=1685021418; bh=qJRmZell6KpEsrHRDqe1vq/gDZ9Ufa3FFoD+99OvW+Q=; 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=JLaoG1GOZ0CTd6o7MFFG5fqzjUi2NKKTCoSDZoc3atRDSoUPLt47GYK+DpczQIEFJ yVWcL3jQY/XmmOpJgAy/nynd4WuHYcAgs6ekVDAEwAXVU6GXBCH8kyX8Xf4k6KmPco bOF0JcqXY6e8oiUhd2WP3G87YHn9Wi7LdmaOfe3I= X-Original-To: libc-alpha@sourceware.org Delivered-To: libc-alpha@sourceware.org Received: from AUS01-ME3-obe.outbound.protection.outlook.com (mail-me3aus01olkn20812.outbound.protection.outlook.com [IPv6:2a01:111:f403:7004::812]) by sourceware.org (Postfix) with ESMTPS id 7D6023858D32 for ; Thu, 25 May 2023 13:29:54 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 7D6023858D32 ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=f7HzPJUHbzLU/ASNtgLnu4+mxML5dsIjcGsGZ7TyM8OAFgJyUmpd1RXpwcRDD8EqY5JQdLxAaoAnhrLpHi0gOqT7JvdMSaAonZxaOrnyiQP3lHlZQTgpXT2oRbM9vSOEp+9L7qS5YQBQW3gjpn++zkeW+4CcztBdKLuFSJOqBM++h3qyL1H8M9VDUResx2wcwGI0byT5Yrjj9UrqKNZ/T1+bQEhrb/agnhxLbZiLFErjislkeLHGqmbF9PqrcB6fR1R3rco30UdHwv9f9uKsRi+7ERb2hqNTk++DtoSdfkIKudco3lRgE2qbXkES4E4HDhfxs8PC+H6Iy09zJIgpCw== 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=qJRmZell6KpEsrHRDqe1vq/gDZ9Ufa3FFoD+99OvW+Q=; b=EzxZUXZEm8+FqFkfB7dXXwWWCPmRvBtmYX/XgmcypIRhB66jEzeA8p7HYMP+BGJhByuRCbcljOGfOJz9cbSY5uFrwtjUReOqC2p11SOBhkuMiGrJc38gh6hzAGfO0XKwt41Il38BeeOWM8JecNzk2f7Vm+1V1rdLHcpyUtV62xtYG30kwu6qD7OKV2M9/6E6r0ufyb8Oxnzc4cfUCqxqDOv1vMJHUIceC9jVXaQSQDfR6WBhhju4uTuW+47Mo86pW3sLEibOyGdh8xi1akHSt1jjP8E3EhGaiYQ0aukVFdnodiGriuJriM83eW/sypvm4qfFPKrr87pXiHxVStHNZg== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=none; dmarc=none; dkim=none; arc=none Received: from SYBP282MB2253.AUSP282.PROD.OUTLOOK.COM (2603:10c6:10:99::13) by ME3P282MB3051.AUSP282.PROD.OUTLOOK.COM (2603:10c6:220:150::5) 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 13:29:48 +0000 Received: from SYBP282MB2253.AUSP282.PROD.OUTLOOK.COM ([fe80::5803:b455:bc94:142a]) by SYBP282MB2253.AUSP282.PROD.OUTLOOK.COM ([fe80::5803:b455:bc94:142a%7]) with mapi id 15.20.6433.016; Thu, 25 May 2023 13:29:48 +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 09:29:11 -0400 Message-ID: X-Mailer: git-send-email 2.22.0.windows.1 In-Reply-To: <918b5baa-1e56-b9e2-99e7-645ece52ff88@gotplt.org> References: <918b5baa-1e56-b9e2-99e7-645ece52ff88@gotplt.org> X-TMN: [4smMsxtHpmk4MlFURpmm9HxtVFQli4Yja2js2hmkJ/ZLKrqcOTidmOhZ8Ppp4K+X] X-ClientProxiedBy: MN2PR20CA0056.namprd20.prod.outlook.com (2603:10b6:208:235::25) To SYBP282MB2253.AUSP282.PROD.OUTLOOK.COM (2603:10c6:10:99::13) X-Microsoft-Original-Message-ID: <20230525132911.421-1-qixingxue@outlook.com> MIME-Version: 1.0 X-MS-Exchange-MessageSentRepresentingType: 1 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: SYBP282MB2253:EE_|ME3P282MB3051:EE_ X-MS-Office365-Filtering-Correlation-Id: d54b687b-f39f-4ee8-0cca-08db5d241c36 X-MS-Exchange-SLBlob-MailProps: V85gaVfRD49+AmP8fiBIm83+Tu0fe3D8vnNZHNnilMFq0Dz3kaFqjpkLw30mTtgwSN3Yh5BWzqG7WTtJ7k/bw742EIdhcKRtowvZfy7zQEvtH0FC5DGn59a/RsHiovN4mDozGt4T8AmgTptl3gbuYciVgYAkIlBduaK3JDhzVWqGa1i+mXC9CPRTFtjZE8jhxRxsaC0v0mz4+mxu3Mrw2YW4wojoT+8DkpQ24VVdeVgk3ruq6VoIr4zRYxlZd3TlJMFQFhIhxyb+TaioAx+Z8Ns2OieXtiK40Ggx/1qBQzYHRFVNSEmIHwHDz3twdtsHkCvf7v8bDupXvFoUwF6RLO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x8+fx7Q3m3nEVd3GTi+UFBqfE7rIxLSci5bajczIpbIfDlUazXhvttpW4MkDUEufo40Y0B0fw7AXY932k3YBIZ55DssC+5c2UBTtjNsyqlqDiDFPXG4OUz3CtDmpfDbTZvVRoW3vEnhyBBwLxlnMoRGGlwi2qZ60Vvrb482y7+TFjnk9npZKPZ9pvWEbvI5HKA+EhpUol+7vFwJGHWJkDOOWRpyPvK3aweVD4ad8ixVKtrfR/xyfspKf4jtwPcaChTwG9utGzwRBU+F6+l7g9vxNW9ca9/GMzi8pjZZz7YSUa946uvFmMFiEiefZpP3Z2bZgBj2tx++gUjYW/naXS5QLyluqIzBl4aLYvlc220mE4wVKP0u7PTl4Cb5Itu2ox2rQVfjgASdLaVI= X-OriginatorOrg: outlook.com X-MS-Exchange-CrossTenant-Network-Message-Id: d54b687b-f39f-4ee8-0cca-08db5d241c36 X-MS-Exchange-CrossTenant-AuthSource: SYBP282MB2253.AUSP282.PROD.OUTLOOK.COM X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 25 May 2023 13:29:48.0644 (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: ME3P282MB3051 X-Spam-Status: No, score=-11.0 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 --- 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..441960864e --- /dev/null +++ b/elf/tst-rootdir-lib.c @@ -0,0 +1,23 @@ +/* Simple library for testing locating library in root directories. + Copyright (C) 2023 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..cb61a1db3a --- /dev/null +++ b/elf/tst-rootdir.c @@ -0,0 +1,37 @@ +/* Test code for locating libraries in root directories. + Copyright (C) 2023 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