From patchwork Fri Mar 7 09:36:17 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Florian Weimer X-Patchwork-Id: 107490 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 3CD67385840C for ; Fri, 7 Mar 2025 09:37:07 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 3CD67385840C 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=O9RbTPHz 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.133.124]) by sourceware.org (Postfix) with ESMTP id 812A13858D1E for ; Fri, 7 Mar 2025 09:36:25 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 812A13858D1E 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 812A13858D1E Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=170.10.133.124 ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1741340185; cv=none; b=uU26RiY794mg8Vi6brimAHoVSMg6GuGCCohcXeoCAaxFYvc9qEbTXcbb2VrzRrLMmdpEjuKkdL6RkehyV1B3lDwGq/Mf75QdhbvznISmbg4CDCxYnE+gMq/3MkEa6Jx8l9mfixVmYENryQGgz/oSb4S38v6rE1pr8cPyj6yzWMY= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1741340185; c=relaxed/simple; bh=jEALHWFTg3H83PZbb6e+4fhodptWV+/ZNCxmCZYcrg8=; h=DKIM-Signature:From:To:Subject:Date:Message-ID:MIME-Version; b=aNN7LSCE/jjvwRJWmlQJCW3T10UgGbZsWtcfSVCmlo6gQmaUrRzzGybwDoPnINcvxpmNvLms716kzyCQerwKior7NpzjnpGr43U9jP+7Wt6F4MKWJlmxiZlqAEd5aubNTok/DlrBG8eLV9Y7aPPhXgtvIWDojOnhnkuGP3gRGVo= ARC-Authentication-Results: i=1; server2.sourceware.org DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 812A13858D1E DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1741340185; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:mime-version:mime-version:content-type:content-type; bh=sk8tyXUM07JkgWqDGKmO7105YkcUX0eHnBlY+HEXmVA=; b=O9RbTPHz+/3xU2P5l0mTbHrm1wPSqSNriqrXK41OAXVosRE/WJtPq1BdXx9uUBLsgXF7/D PGI58rsg7TiP/koTuEcggZ656p52GpQ0ND7OisSj6ngRzNVvulP7kRv/ZqrRO14x0nSZDG YSbMf7rNVphupZeojB6nouH04JlxxDA= Received: from mx-prod-mc-04.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-258-9j2WFjDLMY2n5O7DjQiKpw-1; Fri, 07 Mar 2025 04:36:23 -0500 X-MC-Unique: 9j2WFjDLMY2n5O7DjQiKpw-1 X-Mimecast-MFC-AGG-ID: 9j2WFjDLMY2n5O7DjQiKpw_1741340181 Received: from mx-prod-int-02.mail-002.prod.us-west-2.aws.redhat.com (mx-prod-int-02.mail-002.prod.us-west-2.aws.redhat.com [10.30.177.15]) (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-04.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTPS id 8510919560BB for ; Fri, 7 Mar 2025 09:36:21 +0000 (UTC) Received: from oldenburg.str.redhat.com (unknown [10.2.16.37]) by mx-prod-int-02.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTPS id C86BB1956095 for ; Fri, 7 Mar 2025 09:36:20 +0000 (UTC) From: Florian Weimer To: libc-alpha@sourceware.org Subject: [PATCH v2] elf: Fix handling of symbol versions which hash to zero (bug 29190) Date: Fri, 07 Mar 2025 10:36:17 +0100 Message-ID: <87y0xh6v3i.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.15 X-Mimecast-Spam-Score: 0 X-Mimecast-MFC-PROC-ID: iyCmWpgzSbRbEwY5WNzZ3IphvNt5LtX5Kn4ZRIEJVLI_1741340181 X-Mimecast-Originator: redhat.com X-Spam-Status: No, score=-10.8 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 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 This was found through code inspection. No application impact is known. --- elf/Makefile | 21 +++++++++++++ elf/dl-lookup.c | 22 ++++++++++---- elf/dl-version.c | 7 +++++ elf/tst-version-hash-zero-linkmod.c | 22 ++++++++++++++ elf/tst-version-hash-zero-linkmod.map | 7 +++++ elf/tst-version-hash-zero-mod.c | 20 +++++++++++++ elf/tst-version-hash-zero-mod.map | 13 ++++++++ elf/tst-version-hash-zero-refmod.c | 23 ++++++++++++++ elf/tst-version-hash-zero.c | 56 +++++++++++++++++++++++++++++++++++ 9 files changed, 185 insertions(+), 6 deletions(-) base-commit: 59dc232df277c21239c357e3519682c26e182cd7 diff --git a/elf/Makefile b/elf/Makefile index 18b59a8632..67052b5694 100644 --- a/elf/Makefile +++ b/elf/Makefile @@ -498,6 +498,7 @@ tests += \ tst-unique2 \ tst-unwind-ctor \ tst-unwind-main \ + tst-version-hash-zero \ unload3 \ unload4 \ unload5 \ @@ -1035,6 +1036,9 @@ modules-names += \ tst-unique2mod1 \ tst-unique2mod2 \ tst-unwind-ctor-lib \ + tst-version-hash-zero-linkmod \ + tst-version-hash-zero-mod \ + tst-version-hash-zero-refmod \ unload2dep \ unload2mod \ unload3mod1 \ @@ -3412,3 +3416,20 @@ $(objpfx)tst-nolink-libc-2: $(objpfx)tst-nolink-libc.o -Wl,--dynamic-linker=$(objpfx)ld.so $(objpfx)tst-nolink-libc-2.out: $(objpfx)tst-nolink-libc-2 $(objpfx)ld.so $< > $@ 2>&1; $(evaluate-test) + +$(objpfx)tst-version-hash-zero.out: \ + $(objpfx)tst-version-hash-zero-mod.so \ + $(objpfx)tst-version-hash-zero-refmod.so +LDFLAGS-tst-version-hash-zero-mod.so = \ + -Wl,--version-script=tst-version-hash-zero-mod.map +# The run-time test module tst-version-hash-zero-refmod.so is linked +# to a stub module, tst-version-hash-zero-linkmod.so, to produce an +# expected relocation error. +$(objpfx)tst-version-hash-zero-refmod.so: \ + $(objpfx)tst-version-hash-zero-linkmod.so +LDFLAGS-tst-version-hash-zero-linkmod.so = \ + -Wl,--version-script=tst-version-hash-zero-linkmod.map \ + -Wl,--soname=tst-version-hash-zero-mod.so +$(objpfx)tst-version-hash-zero-refmod.so: \ + $(objpfx)tst-version-hash-zero-linkmod.so +tst-version-hash-zero-refmod.so-no-z-defs = yes diff --git a/elf/dl-lookup.c b/elf/dl-lookup.c index ece647f009..2f5cd674f5 100644 --- a/elf/dl-lookup.c +++ b/elf/dl-lookup.c @@ -100,12 +100,22 @@ check_match (const char *const undef_name, /* We can match the version information or use the default one if it is not hidden. */ ElfW(Half) ndx = verstab[symidx] & 0x7fff; - if ((map->l_versions[ndx].hash != version->hash - || strcmp (map->l_versions[ndx].name, version->name)) - && (version->hidden || map->l_versions[ndx].hash - || (verstab[symidx] & 0x8000))) - /* It's not the version we want. */ - return NULL; + if (map->l_versions[ndx].hash == version->hash + && strcmp (map->l_versions[ndx].name, version->name) == 0) + /* This is an exact version match. Return the symbol below. */ + ; + else + { + if (!version->hidden + && map->l_versions[ndx].name[0] == '\0' + && (verstab[symidx] & 0x8000) == 0 + && (*num_versions)++ == 0) + /* This is the global default version. Store it as a + fallback match. */ + *versioned_sym = sym; + + return NULL; + } } } else diff --git a/elf/dl-version.c b/elf/dl-version.c index d414bd1e18..2fbf4942b9 100644 --- a/elf/dl-version.c +++ b/elf/dl-version.c @@ -357,6 +357,13 @@ _dl_check_map_versions (struct link_map *map, int verbose, int trace_mode) ent = (ElfW(Verdef) *) ((char *) ent + ent->vd_next); } } + + /* The empty string has ELF hash zero. This avoids a NULL check + before the version string comparison in check_match in + dl-lookup.c. */ + for (unsigned int i = 0; i < map->l_nversions; ++i) + if (map->l_versions[i].name == NULL) + map->l_versions[i].name = ""; } /* When there is a DT_VERNEED entry with libc.so on DT_NEEDED, issue diff --git a/elf/tst-version-hash-zero-linkmod.c b/elf/tst-version-hash-zero-linkmod.c new file mode 100644 index 0000000000..15e2506d01 --- /dev/null +++ b/elf/tst-version-hash-zero-linkmod.c @@ -0,0 +1,22 @@ +/* Stub module for linking tst-version-hash-zero-refmod.so. + Copyright (C) 2025 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; see the file COPYING.LIB. If + not, see . */ + +/* The version script assigns a different symbol version for the stub + module. Loading the module with the incorrect version is expected + to fail. */ +#include "tst-version-hash-zero-mod.c" diff --git a/elf/tst-version-hash-zero-linkmod.map b/elf/tst-version-hash-zero-linkmod.map new file mode 100644 index 0000000000..2dba7c22d7 --- /dev/null +++ b/elf/tst-version-hash-zero-linkmod.map @@ -0,0 +1,7 @@ +Base { + local: *; +}; + +OTHER_VERSION { + global: global_variable; +} Base; diff --git a/elf/tst-version-hash-zero-mod.c b/elf/tst-version-hash-zero-mod.c new file mode 100644 index 0000000000..ac6b0dc4a5 --- /dev/null +++ b/elf/tst-version-hash-zero-mod.c @@ -0,0 +1,20 @@ +/* Test module with a zero version symbol hash. + Copyright (C) 2025 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; see the file COPYING.LIB. If + not, see . */ + +/* The symbol version is assigned by version script. */ +int global_variable; diff --git a/elf/tst-version-hash-zero-mod.map b/elf/tst-version-hash-zero-mod.map new file mode 100644 index 0000000000..41eaff7914 --- /dev/null +++ b/elf/tst-version-hash-zero-mod.map @@ -0,0 +1,13 @@ +Base { + local: *; +}; + +/* Define the version so that tst-version-hash-zero-refmod.so passes + the initial symbol version check. */ +OTHER_VERSION { +} Base; + +/* This version string hashes to zero. */ +PPPPPPPPPPPP { + global: global_variable; +} Base; diff --git a/elf/tst-version-hash-zero-refmod.c b/elf/tst-version-hash-zero-refmod.c new file mode 100644 index 0000000000..cd8b3dcef5 --- /dev/null +++ b/elf/tst-version-hash-zero-refmod.c @@ -0,0 +1,23 @@ +/* Test module that triggers a relocation failure in tst-version-hash-zero. + Copyright (C) 2025 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; see the file COPYING.LIB. If + not, see . */ + +/* This is bound to global_variable@@OTHER_VERSION via + tst-version-hash-zero-linkmod.so, but at run time, only + global_variable@PPPPPPPPPPPP exists. */ +extern int global_variable; +int *pointer_variable = &global_variable; diff --git a/elf/tst-version-hash-zero.c b/elf/tst-version-hash-zero.c new file mode 100644 index 0000000000..66a0db4f51 --- /dev/null +++ b/elf/tst-version-hash-zero.c @@ -0,0 +1,56 @@ +/* Symbols with version hash zero should not match any version (bug 29190). + Copyright (C) 2025 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; see the file COPYING.LIB. If + not, see . */ + +#include +#include +#include +#include + +static int +do_test (void) +{ + void *handle = xdlopen ("tst-version-hash-zero-mod.so", RTLD_NOW); + + /* This used to crash because some struct r_found_version entries + with hash zero did not have valid version strings. */ + TEST_VERIFY (xdlvsym (handle, "global_variable", "PPPPPPPPPPPP") != NULL); + + /* Consistency check. */ + TEST_VERIFY (xdlsym (handle, "global_variable") + == xdlvsym (handle, "global_variable", "PPPPPPPPPPPP")); + + /* This symbol version is supposed to be missing. */ + TEST_VERIFY (dlvsym (handle, "global_variable", "OTHER_VERSION") == NULL); + + /* tst-version-hash-zero-refmod.so references + global_variable@@OTHER_VERSION and is expected to fail to load. + dlvsym sets the hidden flag during lookup. Relocation does not, + so this exercises a different failure case. */ + TEST_VERIFY_EXIT (dlopen ("tst-version-hash-zero-refmod.so", RTLD_NOW) + == NULL); + const char *message = dlerror (); + if (strstr (message, + ": undefined symbol: global_variable, version OTHER_VERSION") + == NULL) + FAIL_EXIT1 ("unexpected dlopen failure: %s", message); + + xdlclose (handle); + return 0; +} + +#include