From nobody Thu Apr 27 18:20:12 2023 X-Original-To: dev-commits-src-branches@mlmmj.nyi.freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2610:1c1:1:606c::19:1]) by mlmmj.nyi.freebsd.org (Postfix) with ESMTP id 4Q6kYT1jbTz47lvX; Thu, 27 Apr 2023 18:20:13 +0000 (UTC) (envelope-from git@FreeBSD.org) Received: from mxrelay.nyi.freebsd.org (mxrelay.nyi.freebsd.org [IPv6:2610:1c1:1:606c::19:3]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256 client-signature RSA-PSS (4096 bits) client-digest SHA256) (Client CN "mxrelay.nyi.freebsd.org", Issuer "R3" (verified OK)) by mx1.freebsd.org (Postfix) with ESMTPS id 4Q6kYT19sRz42kY; Thu, 27 Apr 2023 18:20:13 +0000 (UTC) (envelope-from git@FreeBSD.org) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1682619613; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding; bh=EaeLzfmhpnznKz+0NZC4MWtMTLd1cRXX0L3CTXmMLNc=; b=lkGuZ0RmDTefBCcNSw0MOB96pL1i33ZHBNegTPPD073s4et0isMS0nhcRkhvfRGhxCdyYY 32MpN2m0sVWslmiu71YXdnmVSwX2UBZ/xMTDRyC8zOzvDFkjcvNM8NrCEPOSO4EeINBSca oIEBzgR/fuYr5Hu8anWi/IPkb9paobKPywFchZv1+I5olES8DbguOrBCqroZQX+uvmU2sb waEpG2MGE2vYOrfzlnKSvL3sHWdI0VQCX65y9T1naB8faGlsU4UyfH1+qXaRzG7ATEVoJk FHmWZBHraAHOPcdCr1XD3OlY25tUmGrFiK4N3+jVC8uJtD87zair4i7B33f3Yg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1682619613; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding; bh=EaeLzfmhpnznKz+0NZC4MWtMTLd1cRXX0L3CTXmMLNc=; b=wE4FU+DI8hX4Bv6ih7Hv3VZPOJNGD8yZq9NcUd3tqmrVqLsBkVW0jNcn1So8JpSg7/EBOZ Tst6XgcidU13d+hPfSEHOPAPYPKoN2qS/tsrH4az5goirtDnn/3XlxAUJ27mk0WZGB1P6r humw4QEbyow4f1O/HZnH78cNvA0t6Kr/ZwsTd8Y6OxpFDKMSAZm4l7It4v5JPWS+/qmN6T iyry+nfDLQfLwQXXl3jvcJKrZWlTtTNAdspM4PJ2vwrEBiS1JqZDzJ1kaOaSJphyXg70SW Wdlkgy/70yIkxt5ToTlD0e2kA3RFrWiOHWND53fS80ZygJv71ogZgi9nBt/zew== ARC-Authentication-Results: i=1; mx1.freebsd.org; none ARC-Seal: i=1; s=dkim; d=freebsd.org; t=1682619613; a=rsa-sha256; cv=none; b=db61DT+c0bvkhbJaG+eV/3XFkj1A+eC06+iV53Rm+zfOOky5aVJXmHoYBPNSd69VVrnuu7 6iqX1LssT6UzAM9AJKbl61KkMnrtcNhlOwykt3dc05cqmtAWDjclm9BhG58DKv+vXOlshD rsBJ40T29RMIGu1NF6Veud85YnOeWL59cdr1BFv/NNG0sjqs1kWdl8j/2jAUFKbtxcgEiw Y6SpIg5n7+bYT/2TKDaKD7x9nG+AfItLyqzsNRh3O/+8Ll26VFjLPhTeP32LFTmHh+wsk1 rEp/xrMMvlRi621U2gLgB13Dq+Ovb+SD2P/l8EckocAAA6WFEVCwIhXUxSsvLw== Received: from gitrepo.freebsd.org (gitrepo.freebsd.org [IPv6:2610:1c1:1:6068::e6a:5]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256) (Client did not present a certificate) by mxrelay.nyi.freebsd.org (Postfix) with ESMTPS id 4Q6kYT03SYzSqc; Thu, 27 Apr 2023 18:20:13 +0000 (UTC) (envelope-from git@FreeBSD.org) Received: from gitrepo.freebsd.org ([127.0.1.44]) by gitrepo.freebsd.org (8.16.1/8.16.1) with ESMTP id 33RIKC6L069939; Thu, 27 Apr 2023 18:20:12 GMT (envelope-from git@gitrepo.freebsd.org) Received: (from git@localhost) by gitrepo.freebsd.org (8.16.1/8.16.1/Submit) id 33RIKCqv069930; Thu, 27 Apr 2023 18:20:12 GMT (envelope-from git) Date: Thu, 27 Apr 2023 18:20:12 GMT Message-Id: <202304271820.33RIKCqv069930@gitrepo.freebsd.org> To: src-committers@FreeBSD.org, dev-commits-src-all@FreeBSD.org, dev-commits-src-branches@FreeBSD.org From: Ed Maste Subject: git: 9e4ae81971f7 - stable/13 - link_elf: fix SysV hash function overflow List-Id: Commits to the stable branches of the FreeBSD src repository List-Archive: https://lists.freebsd.org/archives/dev-commits-src-branches List-Help: List-Post: List-Subscribe: List-Unsubscribe: Sender: owner-dev-commits-src-branches@freebsd.org X-BeenThere: dev-commits-src-branches@freebsd.org MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: 8bit X-Git-Committer: emaste X-Git-Repository: src X-Git-Refname: refs/heads/stable/13 X-Git-Reftype: branch X-Git-Commit: 9e4ae81971f7b85982c73c1b29424149b56f38dd Auto-Submitted: auto-generated X-ThisMailContainsUnwantedMimeParts: N The branch stable/13 has been updated by emaste: URL: https://cgit.FreeBSD.org/src/commit/?id=9e4ae81971f7b85982c73c1b29424149b56f38dd commit 9e4ae81971f7b85982c73c1b29424149b56f38dd Author: Ed Maste AuthorDate: 2023-04-12 14:04:27 +0000 Commit: Ed Maste CommitDate: 2023-04-27 17:05:28 +0000 link_elf: fix SysV hash function overflow Quoting from https://maskray.me/blog/2023-04-12-elf-hash-function: The System V Application Binary Interface (generic ABI) specifies the ELF object file format. When producing an output executable or shared object needing a dynamic symbol table (.dynsym), a linker generates a .hash section with type SHT_HASH to hold a symbol hash table. A DT_HASH tag is produced to hold the address of .hash. The function is supposed to return a value no larger than 0x0fffffff. Unfortunately, there is a bug. When unsigned long consists of more than 32 bits, the return value may be larger than UINT32_MAX. For instance, elf_hash((const unsigned char *)"\xff\x0f\x0f\x0f\x0f\x0f\x12") returns 0x100000002, which is clearly unintended, as the function should behave the same way regardless of whether long represents a 32-bit integer or a 64-bit integer. Reviewed by: kib, Fangrui Song Sponsored by: The FreeBSD Foundation Differential Revision: https://reviews.freebsd.org/D39517 (cherry picked from commit 2ef2c26f3f132af33f6f12cd7b27d4dbbd7fa435) --- sys/kern/link_elf.c | 19 ++++++++----------- 1 file changed, 8 insertions(+), 11 deletions(-) diff --git a/sys/kern/link_elf.c b/sys/kern/link_elf.c index 245c8697cf79..3c06c8c79108 100644 --- a/sys/kern/link_elf.c +++ b/sys/kern/link_elf.c @@ -1481,23 +1481,20 @@ relocate_file(elf_file_t ef) } /* - * Hash function for symbol table lookup. Don't even think about changing - * this. It is specified by the System V ABI. + * SysV hash function for symbol table lookup. It is specified by the + * System V ABI. */ -static unsigned long +static Elf32_Word elf_hash(const char *name) { - const unsigned char *p = (const unsigned char *) name; - unsigned long h = 0; - unsigned long g; + const unsigned char *p = (const unsigned char *)name; + Elf32_Word h = 0; while (*p != '\0') { h = (h << 4) + *p++; - if ((g = h & 0xf0000000) != 0) - h ^= g >> 24; - h &= ~g; + h ^= (h >> 24) & 0xf0; } - return (h); + return (h & 0x0fffffff); } static int @@ -1508,7 +1505,7 @@ link_elf_lookup_symbol1(linker_file_t lf, const char *name, c_linker_sym_t *sym, unsigned long symnum; const Elf_Sym* symp; const char *strp; - unsigned long hash; + Elf32_Word hash; /* If we don't have a hash, bail. */ if (ef->buckets == NULL || ef->nbuckets == 0) {