From patchwork Tue Nov 19 10:00:28 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Andrew Burgess X-Patchwork-Id: 101499 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 BA1AA385840F for ; Tue, 19 Nov 2024 10:01:23 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org BA1AA385840F 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=MB1AbkvW X-Original-To: gdb-patches@sourceware.org Delivered-To: gdb-patches@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 BB1283858D35 for ; Tue, 19 Nov 2024 10:00:34 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org BB1283858D35 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 BB1283858D35 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=1732010434; cv=none; b=iSDm734xQ10XXi7A/xvLD/oKvlowhRlwCK7BAKa8mkF8AgW3mVBm1cpn+D3KBJHHCHXcNV0awFEgT6m3EwIHgmWRlFSoG4bCwShRY6u6c5AMddx2M2N9O1UJE4fUSEyvnLg6htcOT6ulWzu/GoXxT1UE6BzDslyFHIjAAHF3isA= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1732010434; c=relaxed/simple; bh=p67ZKYhX4H/9fbE4b9KNaINCmrdGLWo6uLE2oTpOQkk=; h=DKIM-Signature:From:To:Subject:Date:Message-Id:MIME-Version; b=n4utjnsiCr+ECd0Ts2NLwmPycRR2zIkPNueQMqib1FVLfmlvHXrw0q8RhMm+Z4reknL8mAC32OSEljac3kosT8RFaTbaCLYn8Z1/sf+wbYjJkZHKT7ShcBlKQgaU3hEKXUQ45OieuHc4/v7iwCIgTY2kAs0ekCbOslReIexJs4Q= ARC-Authentication-Results: i=1; server2.sourceware.org DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org BB1283858D35 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1732010434; 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: content-transfer-encoding:content-transfer-encoding; bh=pJD+RslKI14XCszPR4v2qof0AsmH+YFp9uE8OmJdmfk=; b=MB1AbkvWf/5zHQ/372/tADRKoQXGa6I8MoQKDSGOT6B4OUuP4Rf7kQHbzNgjcA7bdxHjvd 3m+EnUMsePovz+DYUHv9FjTWUmO8Gz4JOBDqzTV1bCicAjIkaVyDJ4beAhwjqyXgLTut2r nNFBDgG9Q3mK80LbMsLQ+sQ7k80i1nM= Received: from mail-wm1-f71.google.com (mail-wm1-f71.google.com [209.85.128.71]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-413-O-LKh2gONNqXYLFnvRwgew-1; Tue, 19 Nov 2024 05:00:33 -0500 X-MC-Unique: O-LKh2gONNqXYLFnvRwgew-1 X-Mimecast-MFC-AGG-ID: O-LKh2gONNqXYLFnvRwgew Received: by mail-wm1-f71.google.com with SMTP id 5b1f17b1804b1-43163a40ee0so4982135e9.0 for ; Tue, 19 Nov 2024 02:00:32 -0800 (PST) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1732010431; x=1732615231; 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=pJD+RslKI14XCszPR4v2qof0AsmH+YFp9uE8OmJdmfk=; b=SjCExV/8U9dN25BJl/0MUqexuf0eiWfAiEucVNLvSdUX5rxscu/dowlPBvNZPCPF0v SCY0xC/qZuaLKQ+iuM0l3sn/F21iHiygeIddtOUt1Vbt6jN5npC/6EBbR2q0x605pV9V sPKG246Ha50h8FMDFw3WLOX2j1PtadXuQvocQd5Itcdtvm5GcO6+o2yKzjTcUjn0qjH4 ctSgydf0WgWyLP37kdR7vJDHpCv+2PVHI3YiZH2W2RrS30+HGOHl+d0yw41tH4PRK8WE tnOQ+XhcooqChyX3MPu3EtaXw5N335AtJKFU4qGG92olf+cB4KcS7GVgu1cVANmX1kqN ipaQ== X-Gm-Message-State: AOJu0Yz0k6p9W0IEXt0UQpYC/4mTBMzYRKrvQMCDfI6tkkRGSw57ftd7 ICv3/EDtQTZs2GydNQ71cbeKs2tMqSV3ndoiIF0aOM1f4AMGeucE33eo0WWxpuAt3DS3soMGEv8 wYoSg661CmuvJAtWAuG/bHFtg+CE7v6j7Aq/QULN7jQRPdLPCWuDwkaWWoDUL5c61hnIsLCzC6b 8SfjEk2KWy2muAN9ncrbKfKwUpZs6w/pexUxaxM4gte0s= X-Received: by 2002:a05:600c:c0d:b0:42c:e0da:f15c with SMTP id 5b1f17b1804b1-432df78c406mr122318865e9.20.1732010431267; Tue, 19 Nov 2024 02:00:31 -0800 (PST) X-Google-Smtp-Source: AGHT+IEA2tYvx8BtAPT9SRpEOXTXzCmjTWskKzSGhAJ6dEq5Y9m4kLEekwtb5mxGcJyeBzoGraVzZA== X-Received: by 2002:a05:600c:c0d:b0:42c:e0da:f15c with SMTP id 5b1f17b1804b1-432df78c406mr122318345e9.20.1732010430450; Tue, 19 Nov 2024 02:00:30 -0800 (PST) Received: from localhost (197.209.200.146.dyn.plus.net. [146.200.209.197]) by smtp.gmail.com with ESMTPSA id ffacd0b85a97d-3821adad619sm15219182f8f.27.2024.11.19.02.00.29 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 19 Nov 2024 02:00:30 -0800 (PST) From: Andrew Burgess To: gdb-patches@sourceware.org Cc: Andrew Burgess Subject: [PATCH] gdb/build-id: protect against weirdly short build-ids Date: Tue, 19 Nov 2024 10:00:28 +0000 Message-Id: X-Mailer: git-send-email 2.25.4 MIME-Version: 1.0 X-Mimecast-Spam-Score: 0 X-Mimecast-MFC-PROC-ID: hCQzNg7FZ63YSY8PcNp8p5rtZLNvwzISKkzp5spZoSo_1732010432 X-Mimecast-Originator: redhat.com X-Spam-Status: No, score=-11.7 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: gdb-patches@sourceware.org X-Mailman-Version: 2.1.30 Precedence: list List-Id: Gdb-patches mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: gdb-patches-bounces~patchwork=sourceware.org@sourceware.org While looking at build_id_to_bfd_suffix (in gdb/build-id.c) I realised that GDB would likely not do what we wanted if a build-id was ever a single byte. Right now, build-ids generated by the GNU linker are 32 bytes, but there's nothing that forces this to be the case, it's pretty easy to create a fake, single byte, build-id. Given that the build-id is an external input (read from the objfile), GDB should protect itself against these edge cases. The problem with build_id_to_bfd_suffix is that this function creates the path used to lookup either the debug information, or an executable, based on its build-id. So a 3-byte build-id 0xaabbcc will look in the path: `$DEBUG_FILE_DIRECTORY/.build-id/aa/bbcc.debug`. However, a single byte build-id 0xaa, will look in the file: `$DEBUG_FILE_DIRECTORY/.build-id/aa/.debug` which doesn't seem right. Worse, when looking for an objfile given a build-id GDB will look for a file called `$DEBUG_FILE_DIRECTORY/.build-id/aa/` with a trailing '/' character. I propose that, in build_id_to_bfd_suffix we just return early if the build-id is 1 byte (or less) with a return value that indicates no separate file was found. For testing I made use of the DWARF assembler. I needed to update the build-id creation proc, the existing code assumes that the build-id is a multiple of 4 bytes, so I added some additional padding to ensure that the generated note was a multiple of 4 bytes, even if the build-id was not. I added a test with a 1 byte build-id, and also for the case where the build-id has zero length. The zero length case already does what you'd expect (no debug is loaded) as the bfd library rejects the build-id when loading it from the objfile, but adding this additional test is pretty cheap. --- gdb/build-id.c | 19 +++- gdb/testsuite/gdb.dwarf2/short-build-id.exp | 119 ++++++++++++++++++++ gdb/testsuite/lib/dwarf.exp | 13 ++- 3 files changed, 142 insertions(+), 9 deletions(-) create mode 100644 gdb/testsuite/gdb.dwarf2/short-build-id.exp base-commit: 82eff6743b77908a502b4cf9030acc93caf69e74 diff --git a/gdb/build-id.c b/gdb/build-id.c index 9d4b005489d..8eaf3472e80 100644 --- a/gdb/build-id.c +++ b/gdb/build-id.c @@ -223,7 +223,13 @@ build_id_to_debug_bfd_1 (const std::string &original_link, /* Common code for finding BFDs of a given build-id. This function works with both debuginfo files (SUFFIX == ".debug") and executable - files (SUFFIX == ""). */ + files (SUFFIX == ""). + + The build-id will be split into a single byte sub-directory, followed by + the remaining build-id bytes as the filename, i.e. we use the lookup + format: `.build-id/xx/yy....zz`. As a consequence, if BUILD_ID_LEN is + less than 2 (bytes), no results will be found as there are not enough + bytes to form the `yy....zz` part of the lookup filename. */ static gdb_bfd_ref_ptr build_id_to_bfd_suffix (size_t build_id_len, const bfd_byte *build_id, @@ -231,6 +237,9 @@ build_id_to_bfd_suffix (size_t build_id_len, const bfd_byte *build_id, { SEPARATE_DEBUG_FILE_SCOPED_DEBUG_ENTER_EXIT; + if (build_id_len < 2) + return {}; + /* Keep backward compatibility so that DEBUG_FILE_DIRECTORY being "" will cause "/.build-id/..." lookups. */ @@ -249,11 +258,9 @@ build_id_to_bfd_suffix (size_t build_id_len, const bfd_byte *build_id, std::string link = debugdir.get (); link += "/.build-id/"; - if (size > 0) - { - size--; - string_appendf (link, "%02x/", (unsigned) *data++); - } + gdb_assert (size > 1); + size--; + string_appendf (link, "%02x/", (unsigned) *data++); while (size-- > 0) string_appendf (link, "%02x", (unsigned) *data++); diff --git a/gdb/testsuite/gdb.dwarf2/short-build-id.exp b/gdb/testsuite/gdb.dwarf2/short-build-id.exp new file mode 100644 index 00000000000..f40d5e4b005 --- /dev/null +++ b/gdb/testsuite/gdb.dwarf2/short-build-id.exp @@ -0,0 +1,119 @@ +# Copyright 2024 Free Software Foundation, Inc. +# +# This program is free software; you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation; either version 3 of the License, or +# (at your option) any later version. +# +# This program 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 General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with this program. If not, see . + +# Create a file with an artificially short (1-byte) build-id, and +# check that GDB doesn't try to load debug information. If we do try +# then we end up loading from: `debug-directory/.build-id/xx/.debug` +# which isn't right. + +load_lib dwarf.exp + +# This test can only be run on targets which support DWARF-2 and use gas. +require dwarf2_support + +# No remote host testing either. +require {!is_remote host} + +standard_testfile main.c + +# Create an assembler file which encodes BUILDID as the build-id. Compile +# this along with the global SRCFILE to create a test executable. +# +# Split the debug information out from the newly created executable and place +# it into the debug file directory. +# +# Load the executable into GDB and check to see if the debug information was +# loaded or not. For this test we are expecting that the debug information +# was not loaded. The reason is that, with short values for BUILDID, GDB ends +# up looking for the debug information in weird locations. +proc run_test { buildid } { + set len [string length $buildid] + + set asm_file [standard_output_file "$::testfile.$len.S"] + Dwarf::assemble $asm_file { + declare_labels int_label int_label2 + + upvar buildid buildid + + build_id $buildid + + cu { label cu_start } { + compile_unit {{language @DW_LANG_C}} { + int_label2: base_type { + {name int} + {byte_size 4 sdata} + {encoding @DW_ATE_signed} + } + + constant { + {name the_int} + {type :$int_label2} + {const_value 99 data1} + } + } + } + + aranges {} cu_start { + arange {} 0 0 + } + } + + set execfile [standard_output_file $::testfile.$len] + + if { [build_executable_from_specs "failed to build" \ + $execfile {debug no-build-id} \ + $::srcfile debug \ + $asm_file {}] } { + return + } + + # Create the debug directory. + set debugdir [standard_output_file "debugdir.$len"] + set build_id_dir $debugdir/.build-id/$buildid + remote_exec host "mkdir -p $build_id_dir" + + # Split out the debug information. + if {[gdb_gnu_strip_debug $execfile no-debuglink]} { + unresolved "failed to split out debug information" + return + } + + # Move the debug information into the debug directory. We place the debug + # information into a file called just '.debug'. GDB should not check this + # file, but at one point GDB would check this file, even though this + # doesn't make much sense. + set execfile_debug ${execfile}.debug + remote_exec host "mv $execfile_debug $build_id_dir/.debug" + + # Start GDB, set the debug-file-directory, and try loading the file. + clean_restart + + gdb_test_no_output "set debug-file-directory $debugdir" \ + "set debug-file-directory" + + gdb_file_cmd $execfile + + gdb_assert { $::gdb_file_cmd_debug_info eq "nodebug" } \ + "no debug should be loaded" + + # For sanity, read something that was encoded in the debug + # information, this should fail. + gdb_test "print the_int" \ + "(?:No symbol table is loaded|No symbol \"the_int\" in current context).*" +} + +foreach_with_prefix buildid { a4 "" } { + run_test $buildid +} diff --git a/gdb/testsuite/lib/dwarf.exp b/gdb/testsuite/lib/dwarf.exp index 1002c75dd09..f6348972968 100644 --- a/gdb/testsuite/lib/dwarf.exp +++ b/gdb/testsuite/lib/dwarf.exp @@ -3008,25 +3008,32 @@ namespace eval Dwarf { proc _note {type name hexdata} { set namelen [expr [string length $name] + 1] + set datalen [expr [string length $hexdata] / 2] # Name size. _op .4byte $namelen # Data size. - _op .4byte [expr [string length $hexdata] / 2] + _op .4byte $datalen # Type. _op .4byte $type # The name. _op .ascii [_quote $name] - # Alignment. + # Alignment (to 4-byte boundary). set align 2 set total [expr {($namelen + (1 << $align) - 1) & -(1 << $align)}] for {set i $namelen} {$i < $total} {incr i} { - _op .byte 0 + _op .byte 0 padding } # The data. foreach {a b} [split $hexdata {}] { _op .byte 0x$a$b } + # Alignment (to 4-byte boundary). + set align 2 + set total [expr {($datalen + (1 << $align) - 1) & -(1 << $align)}] + for {set i $datalen} {$i < $total} {incr i} { + _op .byte 0 padding + } } # Emit a note section holding the given build-id.