From patchwork Fri Nov 10 14:03:48 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Adhemerval Zanella X-Patchwork-Id: 742847 Delivered-To: patch@linaro.org Received: by 2002:a5d:67cf:0:b0:32d:baff:b0ca with SMTP id n15csp212964wrw; Fri, 10 Nov 2023 06:04:07 -0800 (PST) X-Google-Smtp-Source: AGHT+IGK1XJ+BUYys+0u2QlhfW3MahEhCQ8ixRZaKnppuLRA8S8MalE3fkicSgrWfLjO9827UhYJ X-Received: by 2002:a05:620a:1909:b0:778:8cce:b25c with SMTP id bj9-20020a05620a190900b007788cceb25cmr7642422qkb.22.1699625047293; Fri, 10 Nov 2023 06:04:07 -0800 (PST) ARC-Seal: i=2; a=rsa-sha256; t=1699625047; cv=pass; d=google.com; s=arc-20160816; b=I/XPib8ZD5GToz+2vSyTz5dN6+ucmnEskmp5cMDfAFjcQs4MITnaL+SCUkZH8JvW3v nUqEjLy84hWcleFIPrVT7Taiq9yQd0bWQPLI86akt3RxUNdhOWJmpQNgXM1hy3cmpj1R CqhdwS+Q6IaGureJexnlKzOLM20d7FCna29+PUKFntA4+IMEgr7mg5VGSM4Qb5jFmfVx NkYYkIzKXRODr2pJOqT9dLNrHxNvQ12ZzcrEJPs2UlMCvRuqbsfR+PdnFHTwArSNxYxU 68f8OSGOLLIsUDSN18Vutj4XmNYYbdWi/txfXlJjPtyzZpS8GWYbm9lI5C6Zkvl6pnYv cZsw== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=errors-to:list-subscribe:list-help:list-post:list-archive :list-unsubscribe:list-id:precedence:content-transfer-encoding :mime-version:message-id:date:subject:cc:to:from:dkim-signature :arc-filter:dmarc-filter:delivered-to; bh=hDS5CJswVn97xjTqjePKOfpI5k0rV2vkwDPtND8TSlU=; fh=8/w64HoW9XdjJSHbU45fU9we0u7dYsYFVsA5tX2ozH0=; b=JzQe1oOgwemRDJ/z03yDey789fQ2Du1p6gsGF6+dJGCyh8YV4o5Ph9m+Lt0NW/O/MO dxydQ0xP+G2xPuaktO6nminbjFLhtHdmDN7OkengzD5FMbt3AwHh4rRVl3JRAauITpQK Z3smjYDWYnbLAryIwQz1HHw4KRB1bW0HqXBoYo1MHpFkXD+tX3b2xy6sC5gmorEFpspQ qKiUQKI9Kt6BjczJkUnUzNZlavAzGizv89cpUJfFLbXzWNXt8dtSkxOGFEeEIDVBWk1x crd0OECfKYKOjhcr5LvIc5eg4rxlboaWgH56BSxYoJmt2GVKchdtzRy6K4rAYrqUd5M4 HXig== ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b=Di+wOMdR; arc=pass (i=1); spf=pass (google.com: domain of libc-alpha-bounces+patch=linaro.org@sourceware.org designates 8.43.85.97 as permitted sender) smtp.mailfrom="libc-alpha-bounces+patch=linaro.org@sourceware.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linaro.org Return-Path: Received: from server2.sourceware.org (server2.sourceware.org. [8.43.85.97]) by mx.google.com with ESMTPS id ee1-20020a05620a800100b0077419b27790si4892360qkb.97.2023.11.10.06.04.07 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 10 Nov 2023 06:04:07 -0800 (PST) Received-SPF: pass (google.com: domain of libc-alpha-bounces+patch=linaro.org@sourceware.org designates 8.43.85.97 as permitted sender) client-ip=8.43.85.97; Authentication-Results: mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b=Di+wOMdR; arc=pass (i=1); spf=pass (google.com: domain of libc-alpha-bounces+patch=linaro.org@sourceware.org designates 8.43.85.97 as permitted sender) smtp.mailfrom="libc-alpha-bounces+patch=linaro.org@sourceware.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linaro.org Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id EBAEB3858C78 for ; Fri, 10 Nov 2023 14:04:06 +0000 (GMT) X-Original-To: libc-alpha@sourceware.org Delivered-To: libc-alpha@sourceware.org Received: from mail-pf1-x430.google.com (mail-pf1-x430.google.com [IPv6:2607:f8b0:4864:20::430]) by sourceware.org (Postfix) with ESMTPS id 318E63858D33 for ; Fri, 10 Nov 2023 14:04:00 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 318E63858D33 Authentication-Results: sourceware.org; dmarc=pass (p=none dis=none) header.from=linaro.org Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=linaro.org ARC-Filter: OpenARC Filter v1.0.0 sourceware.org 318E63858D33 Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=2607:f8b0:4864:20::430 ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1699625042; cv=none; b=Ujjbcjo4QhzezN9EIT2BIqOTvHMd7bm8i0jv9eJo0is2QRyvbxv/PzWO1CRPcMxB7Iyq2r5Du0xtVx+HoKo3FD3rupiyD1JcbcaoLlSVOQqt66mVLgolTROOLGlRpZWqIuzNcFzLi6o8VVMR1TrdLRKubhRDFYnFk7H+dAMDazk= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1699625042; c=relaxed/simple; bh=RYp3WsC1zcI7MpPZ+svfOd1OtLR+fpub55qVbhNdero=; h=DKIM-Signature:From:To:Subject:Date:Message-Id:MIME-Version; b=XG86OXYuYe9QEeGahHtzNyaeEb9nDMKlPhUnbBwXzxEDzAwDO6ZrnH0GUWFURcwoks4uua/5rYVJxfHOrMpI7FTFg8gLUe7TELiAbmdo2lB3SZdo9/YmEfCdr3ix7/RqjCRjUsPrtv31lrdcjBJpKCvM/v6BPo1BKWaQtH1bMlM= ARC-Authentication-Results: i=1; server2.sourceware.org Received: by mail-pf1-x430.google.com with SMTP id d2e1a72fcca58-6c4ed8eef16so293817b3a.0 for ; Fri, 10 Nov 2023 06:04:00 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; t=1699625038; x=1700229838; darn=sourceware.org; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:from:to:cc:subject:date:message-id:reply-to; bh=hDS5CJswVn97xjTqjePKOfpI5k0rV2vkwDPtND8TSlU=; b=Di+wOMdRLj29XypClxjBKMXMFVGl0WMAKEsDETltwxEumhOk+nYnNvkyf+UIvgRKDK 8o20e0nVpWco+Jp6odOpDMpxuZFQGFPHFWPea9NvRNHwgalHBRjbtqAXAHRU+oFh4y6f EMHY4IIWtYGbtFvmWZYtgxWMrJLlSlZA/a+6MGK5D2cwXzVvJ+yQlpeRktMYmbVa1FF4 QGYn5rBo5NHGvin7rGdBMot8mdMMhFEHqhqyRdJiXIOML2y2YMATMVm0Zv0Gs7QTB37S SuWH9HsgwqQOudMYMDnbklJxmWK8TS7UcLOa+Mtbk1FjfpDanTMGTHIQH30haseGkGdj b/5A== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1699625038; x=1700229838; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=hDS5CJswVn97xjTqjePKOfpI5k0rV2vkwDPtND8TSlU=; b=iWAco+Cl/sRGaPkV5yEiymSkqKDI++vENOe6Eu35AvIcrhMik6bJ+9X8EsDB9Z/qNl eCIsYxc1jPSu+TvGHDFCJeyecAPGwfH5jEQdH+u/BD+OQrva/klQ4IHEwUPVPakE8LBS GIDF2KrLv5jJiEfails8YZ0X0GgC1ppVkKbOCIiraiBFFss+xF9Ww57xQw5UHkcS0q7X 3jMzJ+y03fLDluekoKXghF3k/tM3vMuU4tEUIyABmr21fxsMaAhfQMgOOWGAd3PWIDsX tBJainSeX2iyxF0Y218qI5QaqYnO1UwzfpC5//vpXi3NYfq/T4AgCRTpLTbV3Nxo0g0M 96HA== X-Gm-Message-State: AOJu0YzwUliAhYMVrXdE9tm4BhImMMbZXcEmWePSPABsuVmsgcZYTBYC zcl1Cpykbt/nC4ITn6aD+VUm5pz5cOjkh2MopsU2rw== X-Received: by 2002:a05:6a00:3998:b0:6bc:f819:fcf0 with SMTP id fi24-20020a056a00399800b006bcf819fcf0mr7955236pfb.1.1699625033397; Fri, 10 Nov 2023 06:03:53 -0800 (PST) Received: from mandiga.. ([2804:1b3:a7c1:4617:390f:c3e9:d24e:571c]) by smtp.gmail.com with ESMTPSA id j6-20020a056a00234600b0068ffd56f705sm12519610pfj.118.2023.11.10.06.03.51 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 10 Nov 2023 06:03:52 -0800 (PST) From: Adhemerval Zanella To: libc-alpha@sourceware.org Cc: Matthew Malcomson Subject: [PATCH] elf: Handle non-directory name in search path (BZ 31035) Date: Fri, 10 Nov 2023 11:03:48 -0300 Message-Id: <20231110140348.1206879-1-adhemerval.zanella@linaro.org> X-Mailer: git-send-email 2.34.1 MIME-Version: 1.0 X-Spam-Status: No, score=-12.8 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, GIT_PATCH_0, KAM_SHORT, 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 X-BeenThere: libc-alpha@sourceware.org X-Mailman-Version: 2.1.30 Precedence: list List-Id: Libc-alpha mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: libc-alpha-bounces+patch=linaro.org@sourceware.org The open_path stops if a relative path in search patch contains a component that is a non directory (for instance, if the component is an existing file). For instance: $ cat > lib.c < void foo (void) {} > EOF $ gcc -shared -fPIC -o lib.so lib.c $ cat > main.c < $@; \ + $(evaluate-test) diff --git a/elf/dl-load.c b/elf/dl-load.c index 2923b1141d..8d351153c8 100644 --- a/elf/dl-load.c +++ b/elf/dl-load.c @@ -1810,7 +1810,6 @@ open_path (const char *name, size_t namelen, int mode, size_t cnt; char *edp; int here_any = 0; - int err; /* If we are debugging the search for libraries print the path now if it hasn't happened now. */ @@ -1911,8 +1910,12 @@ open_path (const char *name, size_t namelen, int mode, return -1; } } - if (here_any && (err = errno) != ENOENT && err != EACCES) - /* The file exists and is readable, but something went wrong. */ + + /* Continue the search if the file does not exist (ENOENT), if it can + not be accessed (EACCES), or if the a component in the path is not a + directory (for instance, if the component is a existing file meaning + essentially that the pathname is invalid - ENOTDIR). */ + if (here_any && errno != ENOENT && errno != EACCES && errno != ENOTDIR) return -1; /* Remember whether we found anything. */ diff --git a/elf/tst-non-directory-mod.c b/elf/tst-non-directory-mod.c new file mode 100644 index 0000000000..aa6d4c27df --- /dev/null +++ b/elf/tst-non-directory-mod.c @@ -0,0 +1 @@ +void foo (void) {} diff --git a/elf/tst-non-directory-path.c b/elf/tst-non-directory-path.c new file mode 100644 index 0000000000..cb54289a07 --- /dev/null +++ b/elf/tst-non-directory-path.c @@ -0,0 +1,4 @@ +int main (int argc, char *argv[]) +{ + return 0; +} diff --git a/elf/tst-non-directory-path.sh b/elf/tst-non-directory-path.sh new file mode 100755 index 0000000000..b21cae30c6 --- /dev/null +++ b/elf/tst-non-directory-path.sh @@ -0,0 +1,77 @@ +#!/bin/sh +# Test if library search path does not terminates with non-directory +# components. +# Copyright (C) 2023 Free Software Foundation, Inc. +# 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 +# . + +set -e + +rtld=$1 +test_program=$2 +test_wrapper_env=$3 +run_program_env=$4 +# Remove the last space to allow concatenate extra paths. +library_path=$(echo $5 | tr -d '[:space:]') +objpfx=$6 + +test_binary=$(basename ${test_program}) +test_libpath=${test_binary}-x + +mkdir -p ${objpfx}/${test_libpath} +mv ${objpfx}/tst-non-directory-mod.so ${objpfx}/${test_libpath} + +# Check with absolute paths. +${test_wrapper_env} \ +${run_program_env} \ +$rtld --inhibit-cache \ + --library-path \ + "$library_path":"${objpfx}"tst-non-directory-invalid:"${objpfx}"${test_libpath} \ + $test_program 2>&1 && rc=0 || rc=$? + +${test_wrapper_env} \ +${run_program_env} \ +$rtld --inhibit-cache \ + --library-path \ + "$library_path":"${objpfx}"${test_binary}:"${objpfx}"${test_libpath} \ + $test_program 2>&1 && rc=0 || rc=$? + +# Rlative paths along with non-existent path in search list. +cd "$objpfx" +${test_wrapper_env} \ +${run_program_env} \ +$rtld --inhibit-cache \ + --library-path \ + "$library_path":../elf/tst-non-directory-invalid::../elf/${test_libpath} \ + $test_program 2>&1 && rc=0 || rc=$? + +# Relative paths along with component in the path is not a directory in search list. +${test_wrapper_env} \ +${run_program_env} \ +$rtld --inhibit-cache \ + --library-path \ + "$library_path":../elf/${test_binary}:../elf/${test_libpath} \ + $test_program 2>&1 && rc=0 || rc=$? + +# Relative paths along with non-existent path and a component in the path that is not a directory. +${test_wrapper_env} \ +${run_program_env} \ +$rtld --inhibit-cache \ + --library-path \ + "$library_path":../elf/tst-non-directory-invalid:../elf/${test_binary}:../elf/${test_libpath} \ + $test_program 2>&1 && rc=0 || rc=$? + +exit $rc