From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from simark.ca by simark.ca with LMTP id YZPsJGz69WrXfQYAWB0awg (envelope-from ) for ; Thu, 12 Nov 2026 12:42:04 -0500 Authentication-Results: simark.ca; dkim=pass (1024-bit key; unprotected) header.d=redhat.com header.i=@redhat.com header.a=rsa-sha256 header.s=mimecast20190719 header.b=fSraA96p; dkim-atps=neutral Received: by simark.ca (Postfix, from userid 112) id 92E001E091; Thu, 12 Nov 2026 12:42:04 -0500 (EST) X-Spam-Checker-Version: SpamAssassin 4.0.1 (2024-03-25) on simark.ca X-Spam-Level: X-Spam-Status: No, score=-3.4 required=5.0 tests=ARC_SIGNED,ARC_VALID,BAYES_00, DKIMWL_WL_HIGH,DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,MAILING_LIST_MULTI, RCVD_IN_DNSWL_MED,RCVD_IN_VALIDITY_CERTIFIED_BLOCKED, RCVD_IN_VALIDITY_RPBL_BLOCKED,RCVD_IN_VALIDITY_SAFE_BLOCKED autolearn=ham autolearn_force=no version=4.0.1 Received: from vm01.sourceware.org (vm01.sourceware.org [38.145.34.32]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange x25519 server-signature ECDSA (prime256v1) server-digest SHA256) (No client certificate requested) by simark.ca (Postfix) with ESMTPS id 694651E091 for ; Thu, 12 Nov 2026 12:42:02 -0500 (EST) Received: from vm01.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id EE1364BA7984 for ; Thu, 12 Nov 2026 17:42:01 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org EE1364BA7984 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=fSraA96p 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 6B8904BA2E28 for ; Thu, 12 Nov 2026 17:41:23 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 6B8904BA2E28 Authentication-Results: sourceware.org; dmarc=pass (p=quarantine 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 6B8904BA2E28 Authentication-Results: sourceware.org; arc=none smtp.remote-ip=170.10.133.124 ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1794505283; cv=none; b=mTTV/ubzidi9HJwL5oqp6XH7yOsQuK1u8bLYzZvK0HRMDmLoAf9A5T8ZBGk25dWuDJ7xNqrcpx6K1OHCiCyJwkoAkhi8q4FPrca1NjJaMuqdZqH7IE2CRq7O9CgOSO6wwETkLvczP6/8QtR0K/p8ky3Fv3XC6cCZo0GGrwvR6O4= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1794505283; c=relaxed/simple; bh=uDfMijLZaXGxD6ztlvJ0i3xXz2IUUUwp9gIv4WHdGAo=; h=DKIM-Signature:From:To:Subject:Date:Message-ID:MIME-Version; b=Q0dYmEtIKBKmnLynxiW7W5wtUyVO3YAMu7UHuX+zJar86kfvHdGdfHoisG8RmjAZvAnNe0/UMCiJta1ePWYBa3VhCqzf+UBGbCIz/tCXsgyZBWvjd3L1VO6hbGQlkF65I2cqiB4O9yyAdihmP8uQWtuwqUt/Cwr6NUELxBv2fhU= ARC-Authentication-Results: i=1; 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=fSraA96p DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 6B8904BA2E28 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1794505283; 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: in-reply-to:in-reply-to:references:references; bh=yq3Vw+NJ2F+DVvRrTcd1WTfeazdL2sRF8V1qKVRskP4=; b=fSraA96pRqKjzsjulYRPz15Zkz0g4NF5Ywe4dWVN1q1/2dVcE+LlbtiQbIdCxJF5tP6ZIK YI7yLuwC3+Y/wyBMdCjC0Mm7WsFxcUK1k9SbTKtqqrfPD5Oa+zkU8QO0/JZY4qSBhcdTAN l6b32Q990QRa4QhFSzDr0/67+W17SA8= Received: from mx-prod-mc-06.mail-002.prod.us-west-2.aws.redhat.com (ec2-35-165-154-97.us-west-2.compute.amazonaws.com [35.165.154.97]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-532-0XQr06RhMYODGDUES16-FA-1; Thu, 12 Nov 2026 12:41:21 -0500 X-MC-Unique: 0XQr06RhMYODGDUES16-FA-1 X-Mimecast-MFC-AGG-ID: 0XQr06RhMYODGDUES16-FA_1794505280 Received: from mx-prod-int-03.mail-002.prod.us-west-2.aws.redhat.com (mx-prod-int-03.mail-002.prod.us-west-2.aws.redhat.com [10.30.177.12]) (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-06.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTPS id ACDCC180034E for ; Thu, 12 Nov 2026 17:41:20 +0000 (UTC) Received: from keiths-thinkpadp1gen7.redhat.corp (headnet04.pony-001.prod.iad2.dc.redhat.com [10.2.32.116]) by mx-prod-int-03.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTP id D94151956087 for ; Thu, 12 Nov 2026 17:41:19 +0000 (UTC) From: Keith Seitz To: gdb-patches@sourceware.org Subject: [PATCH 1/5] [gdb/remote] Fix stack overflow in parse_tsv_definition Date: Thu, 12 Nov 2026 09:41:12 -0800 Message-ID: <0075bc3fc2908558e86385531ee62479683149a7.1794430701.git.keiths@redhat.com> In-Reply-To: References: MIME-Version: 1.0 X-Scanned-By: MIMEDefang 3.0 on 10.30.177.12 X-Mimecast-Spam-Score: 0 X-Mimecast-MFC-PROC-ID: IOCRtur3sUyW-LB2Gp_tfS9rKs9CuGH_tvPPV9xQ6yM_1794505280 X-Mimecast-Originator: redhat.com Content-Transfer-Encoding: 8bit content-type: text/plain; charset="US-ASCII"; x-default=true 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~public-inbox=simark.ca@sourceware.org gdb/34967 demonstrates how a malicious remote stub may cause a stack overflow with a malformed tsv definition. parse_tsv_definition skips field separators without checking whether the skipped character is really the field separator. Worse, a short definition such as "1:0:0000" (which is missing a colon) would point a buffer past the terminating NUL into leftover bytes of a reused RSP packet buffer. hex2bin then copied more bytes than allocated by alloca. Fix this by introducing shared helpers that require a field separator and report a malformed definition. Only parse_tsv_definition is converted here; follow-up patches will use the same helpers for the remaining trace definition parsers. Bug: https://sourceware.org/bugzilla/show_bug.cgi?id=34967 --- gdb/tracepoint.c | 40 +++++++-- gdb/unittests/tracepoint-selftests.c | 119 +++++++++++++++++++++++++++ 2 files changed, 150 insertions(+), 9 deletions(-) diff --git a/gdb/tracepoint.c b/gdb/tracepoint.c index d903887e3ac..7a5af33b42e 100644 --- a/gdb/tracepoint.c +++ b/gdb/tracepoint.c @@ -3377,6 +3377,29 @@ Status line: '%s'\n"), p, line); } } +/* Report a malformed uploaded trace-related definition. KIND is a short + label such as "tsv" or "tracepoint"; LINE is the full text being + parsed. */ + +[[noreturn]] static void +bad_trace_definition (const char *kind, const char *line) +{ + error (_("bad %s definition: %s"), kind, line); +} + +/* If P points at SEP, return the character after it. Otherwise throw + via bad_trace_definition. P may be NULL (e.g. a failed strchr), + which is treated as a missing separator. */ + +static const char * +trace_definition_require_char (const char *p, char sep, const char *kind, + const char *line) +{ + if (p == nullptr || *p != sep) + bad_trace_definition (kind, line); + return p + 1; +} + void parse_tracepoint_status (const char *p, tracepoint *tp, struct uploaded_tp *utp) @@ -3518,27 +3541,26 @@ void parse_tsv_definition (const char *line, struct uploaded_tsv **utsvp) { const char *p; - char *buf; ULONGEST num, initval, builtin; - int end; struct uploaded_tsv *utsv = NULL; - buf = (char *) alloca (strlen (line)); + auto require = [&] (const char *q, char sep = ':') + { + return trace_definition_require_char (q, sep, "tsv", line); + }; p = line; p = unpack_varlen_hex (p, &num); - p++; /* skip a colon */ + p = require (p); p = unpack_varlen_hex (p, &initval); - p++; /* skip a colon */ + p = require (p); p = unpack_varlen_hex (p, &builtin); - p++; /* skip a colon */ - end = hex2bin (p, (gdb_byte *) buf, strlen (p) / 2); - buf[end] = '\0'; + p = require (p); utsv = get_uploaded_tsv (num, utsvp); utsv->initial_value = initval; utsv->builtin = builtin; - utsv->name = xstrdup (buf); + utsv->name = xstrdup (hex2str (p).c_str ()); } /* Given a line of text defining a static tracepoint marker, parse it diff --git a/gdb/unittests/tracepoint-selftests.c b/gdb/unittests/tracepoint-selftests.c index 6349f5075f4..314390403b6 100644 --- a/gdb/unittests/tracepoint-selftests.c +++ b/gdb/unittests/tracepoint-selftests.c @@ -20,9 +20,44 @@ #include "gdbsupport/selftest.h" #include "tracepoint.h" +#include +#include + namespace selftests { namespace tracepoint_tests { +/* Run PARSE and assert that it throws a gdb_exception_error. CLEANUP + always runs afterward. */ + +template +static void +expect_bad_definition (Parse parse, Cleanup cleanup) +{ + bool threw = false; + + try + { + parse (); + } + catch (const gdb_exception_error &ex) + { + SELF_CHECK (ex.reason == RETURN_ERROR); + threw = true; + } + + SELF_CHECK (threw); + cleanup (); +} + +/* Like the two-argument overload, with a no-op cleanup. */ + +template +static void +expect_bad_definition (Parse parse) +{ + expect_bad_definition (parse, [] () {}); +} + static void test_parse_static_tracepoint_marker_definition () { @@ -57,6 +92,87 @@ test_parse_static_tracepoint_marker_definition () SELF_CHECK (end == def + strlen (def)); } +/* Free UTSVP, including each node's name. */ + +static void +cleanup_uploaded_tsvs (uploaded_tsv **utsvp) +{ + for (uploaded_tsv *utsv = *utsvp; utsv != nullptr; utsv = utsv->next) + xfree (const_cast (utsv->name)); + free_uploaded_tsvs (utsvp); +} + +/* Build a buffer that mimics a reused RSP packet buffer (rs->buf): + PREFIX is a NUL-terminated definition, and STALE_HEX is appended + immediately after that NUL -- as leftover bytes from a previous, longer + reply (for example qTStatus) that a subsequent short reply overwrote. + + Returns a vector with the following layout: + + [ prefix bytes | NUL | stale_hex bytes | NUL ] + + The final NUL keeps strlen on the stale region well-defined. */ + +static std::vector +make_stale_tail_buffer (const char *prefix, const std::string &stale_hex) +{ + std::vector buf (prefix, prefix + strlen (prefix) + 1); + buf.insert (buf.end (), stale_hex.begin (), stale_hex.end ()); + buf.push_back ('\0'); + return buf; +} + +/* Assert that LINE is rejected as a malformed TSV definition. */ + +static void +expect_bad_tsv_definition (const char *line) +{ + uploaded_tsv *utsvs = nullptr; + + expect_bad_definition ([&] () { parse_tsv_definition (line, &utsvs); }, + [&] () { cleanup_uploaded_tsvs (&utsvs); }); +} + +/* Test parse_tsv_definition, including buffers with stale bytes after a + short definition's terminating NUL, as when reusing an RSP packet + buffer. */ + +static void +test_parse_tsv_definition () +{ + uploaded_tsv *utsvs = nullptr; + + /* Well-formed definition: name hex-encodes "foo". */ + { + const char *good = "1:0:0:666f6f"; + + parse_tsv_definition (good, &utsvs); + SELF_CHECK (utsvs != nullptr); + SELF_CHECK (utsvs->number == 1); + SELF_CHECK (utsvs->initial_value == 0); + SELF_CHECK (utsvs->builtin == 0); + SELF_CHECK (utsvs->name != nullptr && streq (utsvs->name, "foo")); + cleanup_uploaded_tsvs (&utsvs); + } + + /* Short stale tail. Too small to overflow the old alloca buffer, but + still missing a trailing colon, so the parser must throw. */ + { + static const char prefix[] = "1:0:0000"; + std::vector buf = make_stale_tail_buffer (prefix, "4444"); + expect_bad_tsv_definition (buf.data ()); + } + + /* Long stale tail. Large enough to overflow the old alloca buffer; with + the fix, the missing trailing colon must cause a throw instead. */ + { + static const char prefix[] = "1:0:0000"; + std::vector buf + = make_stale_tail_buffer (prefix, std::string (64, '4')); + expect_bad_tsv_definition (buf.data ()); + } +} + } /* namespace tracepoint_tests */ } /* namespace selftests */ @@ -65,4 +181,7 @@ INIT_GDB_FILE (tracepoint_selftests) selftests::register_test ("parse_static_tracepoint_marker_definition", selftests::tracepoint_tests::test_parse_static_tracepoint_marker_definition); + selftests::register_test + ("parse_tsv_definition", + selftests::tracepoint_tests::test_parse_tsv_definition); } -- 2.56.0