From patchwork Tue Dec 3 12:29:12 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Florian Weimer X-Patchwork-Id: 102314 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 545F53858D35 for ; Tue, 3 Dec 2024 12:31:25 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 545F53858D35 Authentication-Results: sourceware.org; dkim=pass (1024-bit key, unprotected) header.d=redhat.com header.i=@redhat.com header.a=rsa-sha256 header.s=mimecast20190719 header.b=NDIuEZLK X-Original-To: libc-alpha@sourceware.org Delivered-To: libc-alpha@sourceware.org Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.129.124]) by sourceware.org (Postfix) with ESMTP id 3B4993858D35 for ; Tue, 3 Dec 2024 12:29:20 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 3B4993858D35 Authentication-Results: sourceware.org; dmarc=pass (p=none dis=none) header.from=redhat.com Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=redhat.com ARC-Filter: OpenARC Filter v1.0.0 sourceware.org 3B4993858D35 Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=170.10.129.124 ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1733228960; cv=none; b=dsQS9cORPb9leE05PgM45Cki+OU2krkGQvuQllPvYJhjuRMNQBO6MurEgYXGinr25+nqYEQW50wxlK628ZMhKAVEydp2svA9LsHDkS8HCbIxXZwBSVUUDEahZHSN5vnzHqf25+Jfuujns7nHeIVFSl6znAPJrrM0nlzkU24fHPY= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1733228960; c=relaxed/simple; bh=iBri0NM7I4BYQNvlYZxcyBuonhPKi/kyrOQl+Rc7LIE=; h=DKIM-Signature:From:To:Subject:Date:Message-ID:MIME-Version; b=lSwEpiJUEK/S4TOGNmaYGfRLPEIWN/Fu4ii3CeTTKxDcUq4W2NmpCjFvbDFeA3GEs9IvHf//lFGt1T5tEO1hEAhJ9S1u+B0cHhHCA35NVctA70IOM6uorSse14pI5xPYnZj+4bCHtlFde6CetKoBi07KhVMwoOfk3lyzqdfSlt8= ARC-Authentication-Results: i=1; server2.sourceware.org DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 3B4993858D35 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1733228960; 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; bh=p/RXi4NvFMXMejcuF9N5gdzL3VnH2yPi2Z0Q4Vb+PH4=; b=NDIuEZLKgtq/O7Ax+070DskSBwAGl5UmSNiSOLnAjlDDK1GpIcOdBaPaxAmiUKVyNHU6Pf L11PYoqQOIiu60vk6pu0/S7gYxqhcfmlKlope+Qii1+mmbSRyvpJnEUOlLs+5JliEcTjwN U0GY8IcoWzsv2qD8hrT+zVoJ31RghJE= Received: from mx-prod-mc-01.mail-002.prod.us-west-2.aws.redhat.com (ec2-54-186-198-63.us-west-2.compute.amazonaws.com [54.186.198.63]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-97-jgW05EDbONKGmpy0b1lyZw-1; Tue, 03 Dec 2024 07:29:18 -0500 X-MC-Unique: jgW05EDbONKGmpy0b1lyZw-1 X-Mimecast-MFC-AGG-ID: jgW05EDbONKGmpy0b1lyZw Received: from mx-prod-int-05.mail-002.prod.us-west-2.aws.redhat.com (mx-prod-int-05.mail-002.prod.us-west-2.aws.redhat.com [10.30.177.17]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by mx-prod-mc-01.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTPS id 54EC0195609D; Tue, 3 Dec 2024 12:29:17 +0000 (UTC) Received: from oldenburg.str.redhat.com (unknown [10.45.224.6]) by mx-prod-int-05.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTPS id E81DC1956052; Tue, 3 Dec 2024 12:29:15 +0000 (UTC) From: Florian Weimer To: libc-alpha@sourceware.org Cc: Adhemerval Zanella Subject: [PATCH v2] dlfcn: dlinfo (RTLD_DI_PHDR) should work for proxy link maps (bug 32060) Date: Tue, 03 Dec 2024 13:29:12 +0100 Message-ID: <87wmghne5j.fsf@oldenburg.str.redhat.com> User-Agent: Gnus/5.13 (Gnus v5.13) MIME-Version: 1.0 X-Scanned-By: MIMEDefang 3.0 on 10.30.177.17 X-Mimecast-Spam-Score: 0 X-Mimecast-MFC-PROC-ID: 55rMen4V_o-H3ncWKtzduDb4fCLL-hPNCKoTxwrgscQ_1733228957 X-Mimecast-Originator: redhat.com X-Spam-Status: No, score=-12.2 required=5.0 tests=BAYES_00, DKIMWL_WL_HIGH, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, GIT_PATCH_0, KAM_SHORT, RCVD_IN_DNSWL_NONE, RCVD_IN_MSPIKE_H2, SPF_HELO_NONE, SPF_NONE, TXREP, URIBL_BLOCKED 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~patchwork=sourceware.org@sourceware.org Previously, 0 was returned as the program header address. To get a clean test run for the new dlfcn/tst-dlinfo-dlmopen test, it is necessary to handle RTLD_DI_ORIGIN failures in dlinfo, too. --- v2: Handle proxy link maps correctly for RTLD_DI_LINKMAP, RTLD_DI_LMID. Add new test dlfcn/tst-dlinfo-dlmopen. Fix RTLD_DI_ORIGIN for link maps with unknown origin. Use RTLD_DI_LINKMAP in dlfcn/tst-dlinfo-phdr per Adhemerval's suggestion. dlfcn/Makefile | 1 + dlfcn/dlinfo.c | 26 +++++++----- dlfcn/tst-dlinfo-dlmopen.c | 99 ++++++++++++++++++++++++++++++++++++++++++++++ dlfcn/tst-dlinfo-phdr.c | 73 +++++++++++++++++++++------------- 4 files changed, 163 insertions(+), 36 deletions(-) base-commit: f43eb2cf30fdff39bda1c2018246d4badabbc576 diff --git a/dlfcn/Makefile b/dlfcn/Makefile index d473773dd4..dcc0fd2b70 100644 --- a/dlfcn/Makefile +++ b/dlfcn/Makefile @@ -124,6 +124,7 @@ tststatic4-ENV = $(tststatic-ENV) tststatic5-ENV = $(tststatic-ENV) tests-internal += \ + tst-dlinfo-dlmopen \ tst-dlinfo-phdr \ # tests-internal diff --git a/dlfcn/dlinfo.c b/dlfcn/dlinfo.c index b0feb9362d..7d13b29822 100644 --- a/dlfcn/dlinfo.c +++ b/dlfcn/dlinfo.c @@ -49,41 +49,49 @@ dlinfo_doit (void *argsblock) break; case RTLD_DI_LMID: + /* Return the proxy namespace (if any). */ *(Lmid_t *) args->arg = l->l_ns; break; case RTLD_DI_LINKMAP: + /* Return the proxy link map (if any). */ *(struct link_map **) args->arg = l; break; case RTLD_DI_SERINFO: - _dl_rtld_di_serinfo (l, args->arg, false); + _dl_rtld_di_serinfo (l->l_real, args->arg, false); break; case RTLD_DI_SERINFOSIZE: - _dl_rtld_di_serinfo (l, args->arg, true); + _dl_rtld_di_serinfo (l->l_real, args->arg, true); break; case RTLD_DI_ORIGIN: - strcpy (args->arg, l->l_origin); + /* If the object was loaded by the kernel, origin information + could be unavailable. */ + if (l->l_real->l_origin == NULL || l->l_real->l_origin == (void *) -1) + { + args->result = -1; + _dl_signal_error (0, NULL, NULL, N_("RTLD_DI_ORIGIN unavailable")); + } + strcpy (args->arg, l->l_real->l_origin); break; case RTLD_DI_TLS_MODID: - *(size_t *) args->arg = 0; - *(size_t *) args->arg = l->l_tls_modid; + *(size_t *) args->arg = l->l_real->l_tls_modid; break; case RTLD_DI_TLS_DATA: { void *data = NULL; - if (l->l_tls_modid != 0) - data = GLRO(dl_tls_get_addr_soft) (l); + if (l->l_real->l_tls_modid != 0) + data = GLRO(dl_tls_get_addr_soft) (l->l_real); *(void **) args->arg = data; break; } case RTLD_DI_PHDR: - *(const ElfW(Phdr) **) args->arg = l->l_phdr; - args->result = l->l_phnum; + *(const ElfW(Phdr) **) args->arg = l->l_real->l_phdr; + args->result = l->l_real->l_phnum; break; } } diff --git a/dlfcn/tst-dlinfo-dlmopen.c b/dlfcn/tst-dlinfo-dlmopen.c new file mode 100644 index 0000000000..ef44005803 --- /dev/null +++ b/dlfcn/tst-dlinfo-dlmopen.c @@ -0,0 +1,99 @@ +/* Test for dlinfo with dlmopen. + Copyright (C) 2022-2024 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 + . */ + +/* This internal test assumes that dlopen handles are inplemented as + struct link_map pointers. */ + +#include +#include +#include +#include +#include +#include +#include +#include + +#include +#include + +/* Per-link-map checks. */ +static void +checks (struct link_map *l, Lmid_t expected_nsid) +{ + printf ("info: checking link map %p for \"%s\" [%ld]\n", + l, l->l_name, l->l_ns); + + /* Cause dlerror () to return an error message. */ + dlsym (RTLD_DEFAULT, "does-not-exist"); + + /* Use the extension that link maps are valid dlopen handles. */ + struct link_map *l2 = NULL; + TEST_COMPARE (dlinfo (l, RTLD_DI_LINKMAP, &l2), 0); + /* This equality should hold even if l is a proxy link map. */ + TEST_VERIFY (l2 == l); + /* Verify that the error message has been cleared. */ + TEST_COMPARE_STRING (dlerror (), NULL); + + /* Cause dlerror () to return an error message. */ + dlsym (RTLD_DEFAULT, "does-not-exist"); + + Lmid_t lmid = -1; + TEST_COMPARE (dlinfo (l, RTLD_DI_LMID, &lmid), 0); + /* Assume that namespaces are allocated sequentially. */ + TEST_COMPARE (lmid, expected_nsid); + /* Verify that the error message has been cleared. */ + TEST_COMPARE_STRING (dlerror (), NULL); + + char origin[4096]; /* Cannot size properly (bug 24298). */ + origin[0] = '\0'; + int result = dlinfo (l, RTLD_DI_ORIGIN, origin); + if (result != 0) + { + /* The RTLD_DI_ORIGIN request can fail, but should not crash. */ + TEST_COMPARE (result, -1); + TEST_VERIFY (dlerror () != NULL); + } + else + /* This should be an existing path. */ + TEST_COMPARE (access (origin, F_OK), 0); +} + +static int +do_test (void) +{ + /* Avoid a copy relocation. */ + struct r_debug *debug = xdlsym (RTLD_DEFAULT, "_r_debug"); + struct link_map *l = (struct link_map *) debug->r_map; + TEST_VERIFY_EXIT (l != NULL); + + for (; l != NULL; l = l->l_next) + checks (l, LM_ID_BASE); + + /* The secondary namespace does not contain the main executable, and + dl_iterate_phdr does not cover it. */ + struct link_map *secondary_libc = xdlmopen (LM_ID_NEWLM, LIBC_SO, RTLD_NOW); + for (l = secondary_libc; l != NULL; l = l->l_next) + /* Assume that namespaces are allocated sequentially. */ + checks (l, LM_ID_BASE + 1); + TEST_VERIFY (secondary_libc->l_real == secondary_libc); + xdlclose (secondary_libc); + + return 0; +} + +#include diff --git a/dlfcn/tst-dlinfo-phdr.c b/dlfcn/tst-dlinfo-phdr.c index fdffb17724..9e61134210 100644 --- a/dlfcn/tst-dlinfo-phdr.c +++ b/dlfcn/tst-dlinfo-phdr.c @@ -17,6 +17,7 @@ . */ #include +#include #include #include #include @@ -54,6 +55,42 @@ dlip_callback (struct dl_phdr_info *dlpi, size_t size, void *closure) return 0; } +/* Set by basic_checks. */ +static const ElfW(Phdr) *phdr; +static int phnum; + +/* Basic checks that can be used regardless of namespace. */ +static void +basic_checks (struct link_map *l) +{ + printf ("info: checking link map %p (%p) for \"%s\"\n", + l, l->l_phdr, l->l_name); + + /* Cause dlerror () to return an error message. */ + dlsym (RTLD_DEFAULT, "does-not-exist"); + + /* Use the extension that link maps are valid dlopen handles. */ + phnum = dlinfo (l, RTLD_DI_PHDR, &phdr); + TEST_VERIFY (phnum >= 0); + /* Verify that the error message has been cleared. */ + TEST_COMPARE_STRING (dlerror (), NULL); + + TEST_VERIFY (phdr == l->l_real->l_phdr); + TEST_COMPARE (phnum, l->l_real->l_phnum); + + /* Check that we can find PT_DYNAMIC among the array. */ + { + bool dynamic_found = false; + for (int i = 0; i < phnum; ++i) + if (phdr[i].p_type == PT_DYNAMIC) + { + dynamic_found = true; + TEST_COMPARE ((ElfW(Addr)) l->l_ld, l->l_addr + phdr[i].p_vaddr); + } + TEST_VERIFY (dynamic_found); + } +} + static int do_test (void) { @@ -64,33 +101,7 @@ do_test (void) do { - printf ("info: checking link map %p (%p) for \"%s\"\n", - l, l->l_phdr, l->l_name); - - /* Cause dlerror () to return an error message. */ - dlsym (RTLD_DEFAULT, "does-not-exist"); - - /* Use the extension that link maps are valid dlopen handles. */ - const ElfW(Phdr) *phdr; - int phnum = dlinfo (l, RTLD_DI_PHDR, &phdr); - TEST_VERIFY (phnum >= 0); - /* Verify that the error message has been cleared. */ - TEST_COMPARE_STRING (dlerror (), NULL); - - TEST_VERIFY (phdr == l->l_phdr); - TEST_COMPARE (phnum, l->l_phnum); - - /* Check that we can find PT_DYNAMIC among the array. */ - { - bool dynamic_found = false; - for (int i = 0; i < phnum; ++i) - if (phdr[i].p_type == PT_DYNAMIC) - { - dynamic_found = true; - TEST_COMPARE ((ElfW(Addr)) l->l_ld, l->l_addr + phdr[i].p_vaddr); - } - TEST_VERIFY (dynamic_found); - } + basic_checks (l); /* Check that dl_iterate_phdr finds the link map with the same program headers. */ @@ -119,6 +130,14 @@ do_test (void) } while (l != NULL); + /* The secondary namespace does not contain the main executable, and + dl_iterate_phdr does not cover it. */ + struct link_map *secondary_libc = xdlmopen (LM_ID_NEWLM, LIBC_SO, RTLD_NOW); + TEST_COMPARE (dlinfo (secondary_libc, RTLD_DI_LINKMAP, &l), 0); + for (; l != NULL; l = l->l_next) + basic_checks (l); + xdlclose (secondary_libc); + return 0; }