From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from simark.ca by simark.ca with LMTP id CVGzOXD69WrXfQYAWB0awg (envelope-from ) for ; Thu, 12 Nov 2026 12:42:08 -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=RfpT5pEu; dkim-atps=neutral Received: by simark.ca (Postfix, from userid 112) id E8B211E0BC; Thu, 12 Nov 2026 12:42:08 -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.1 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_SBL_CSS autolearn=ham autolearn_force=no version=4.0.1 Received: from vm01.sourceware.org (vm01.sourceware.org [IPv6:2620:52:6:3111::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 51C081E087 for ; Thu, 12 Nov 2026 12:42:08 -0500 (EST) Received: from vm01.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 84C544BA7991 for ; Thu, 12 Nov 2026 17:42:06 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 84C544BA7991 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=RfpT5pEu 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 B40194BA2E0B for ; Thu, 12 Nov 2026 17:41:25 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org B40194BA2E0B 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 B40194BA2E0B Authentication-Results: sourceware.org; arc=none smtp.remote-ip=170.10.129.124 ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1794505285; cv=none; b=Qb1eEBl+ydAo5UwwIFkY3wcF4GSuIWarOuEeP/cVfftBAN2glPNTA9dLzDZwVj45o8xz9DtmCBXx+FvvILfrCayqu8y70rt2gVdLL9U8euuA2EGFEloIt/ukXGVdUQcSoDwejj/xsRBzFcG98zBeEgYkAbpIBZ4QRkruBP1R3Qw= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1794505285; c=relaxed/simple; bh=124vWDoeuPFsEcUTGiYvHK69LfHGPyYWpAhL+tywgck=; h=DKIM-Signature:From:To:Subject:Date:Message-ID:MIME-Version; b=WsyRcBNYabbSjcpRatD6l6ST+KK/H3nzFA5d/GmhmmwPfx4IgCMqB9GloZ1DPXtrNYyC8wyXhN6KAKfr/mZxxEvtjNDT+pQyGYH7V2D3icT30LcL23hW1ZBE+bCRSyqrklT7rGEZsBGBPmjko8nRLLxmda/OwZsh4TSQU51vVIg= 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=RfpT5pEu DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org B40194BA2E0B DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1794505285; 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=8ghE6cwGAUwH9GwUWs8tB7ri7Y4VkpRVOMO7JHZaN20=; b=RfpT5pEupSXe22Xi+VHOrjFDWzQVDfBdh3V8LuUaeNB3sxLDqmWwz3KW9zalS6qRPY2SAx iQe4TWfv35lpSfoajz7sGoVtW5WAp56lCgbwWLJsUhibWUOOdjaQqqMLmjSWapufCforK7 pKvkNAapWP+MTvfs9NPYGvv3zUPCfJE= Received: from mx-prod-mc-05.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-221-NLWmW_6YPGGEAm6y_L4qMw-1; Thu, 12 Nov 2026 12:41:23 -0500 X-MC-Unique: NLWmW_6YPGGEAm6y_L4qMw-1 X-Mimecast-MFC-AGG-ID: NLWmW_6YPGGEAm6y_L4qMw_1794505282 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-05.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTPS id 5849719560AF for ; Thu, 12 Nov 2026 17:41:22 +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 A310C1956087 for ; Thu, 12 Nov 2026 17:41:20 +0000 (UTC) From: Keith Seitz To: gdb-patches@sourceware.org Subject: [PATCH 2/5] [gdb/remote] Fix stack overflow in parse_tracepoint_definition Date: Thu, 12 Nov 2026 09:41:13 -0800 Message-ID: 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: OcM8MR-fojj95aaT3hakk9RRtNjHsJa9yOcPiEFep50_1794505282 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/34971 demonstrates how a malicious remote stub may cause a stack overflow with a malformed tracepoint definition. Similar to parse_tsv_definition issues with missing colons in the packet reply and how that can cause hex2bin to copy more bytes than allocated, fix this in the same way: more stringent checking for proper packet layout. This fixes the missing-colon occurrences in parse_tracepoint_definition. While touching that parser, also require the enable flag to be 'E' or 'D' rather than treating any other character as disabled, and add unit tests for both. Bug: https://sourceware.org/bugzilla/show_bug.cgi?id=34971 --- gdb/tracepoint.c | 38 ++++++------ gdb/unittests/tracepoint-selftests.c | 88 ++++++++++++++++++++++++++++ 2 files changed, 108 insertions(+), 18 deletions(-) diff --git a/gdb/tracepoint.c b/gdb/tracepoint.c index 7a5af33b42e..7a3339617ef 100644 --- a/gdb/tracepoint.c +++ b/gdb/tracepoint.c @@ -3428,34 +3428,40 @@ parse_tracepoint_definition (const char *line, struct uploaded_tp **utpp) const char *p; char piece; ULONGEST num, addr, step, pass, orig_size, xlen, start; - int enabled, end; + int enabled; enum bptype type; const char *srctype; - char *buf; struct uploaded_tp *utp = NULL; + auto require = [&] (const char *q, char sep = ':') + { + return trace_definition_require_char (q, sep, "tracepoint", line); + }; + p = line; /* Both tracepoint and action definitions start with the same number and address sequence. */ piece = *p++; p = unpack_varlen_hex (p, &num); - p++; /* skip a colon */ + p = require (p); p = unpack_varlen_hex (p, &addr); - p++; /* skip a colon */ + p = require (p); if (piece == 'T') { gdb::unique_xmalloc_ptr cond; + if (*p != 'E' && *p != 'D') + bad_trace_definition ("tracepoint", line); enabled = (*p++ == 'E'); - p++; /* skip a colon */ + p = require (p); p = unpack_varlen_hex (p, &step); - p++; /* skip a colon */ + p = require (p); p = unpack_varlen_hex (p, &pass); type = bp_tracepoint; /* Thumb through optional fields. */ while (*p == ':') { - p++; /* skip a colon */ + p = require (p); if (*p == 'F') { type = bp_fast_tracepoint; @@ -3501,24 +3507,20 @@ parse_tracepoint_definition (const char *line, struct uploaded_tp **utpp) /* Parse a chunk of source form definition. */ utp = get_uploaded_tp (num, addr, utpp); srctype = p; - p = strchr (p, ':'); - p++; /* skip a colon */ + p = require (strchr (p, ':')); p = unpack_varlen_hex (p, &start); - p++; /* skip a colon */ + p = require (p); p = unpack_varlen_hex (p, &xlen); - p++; /* skip a colon */ - - buf = (char *) alloca (strlen (line)); + p = require (p); - end = hex2bin (p, (gdb_byte *) buf, strlen (p) / 2); - buf[end] = '\0'; + std::string decoded = hex2str (p); if (startswith (srctype, "at:")) - utp->at_string = make_unique_xstrdup (buf); + utp->at_string = make_unique_xstrdup (decoded.c_str ()); else if (startswith (srctype, "cond:")) - utp->cond_string = make_unique_xstrdup (buf); + utp->cond_string = make_unique_xstrdup (decoded.c_str ()); else if (startswith (srctype, "cmd:")) - utp->cmd_strings.emplace_back (xstrdup (buf)); + utp->cmd_strings.emplace_back (xstrdup (decoded.c_str ())); } else if (piece == 'V') { diff --git a/gdb/unittests/tracepoint-selftests.c b/gdb/unittests/tracepoint-selftests.c index 314390403b6..ded37f390e6 100644 --- a/gdb/unittests/tracepoint-selftests.c +++ b/gdb/unittests/tracepoint-selftests.c @@ -173,6 +173,91 @@ test_parse_tsv_definition () } } +/* Assert that LINE is rejected as a malformed tracepoint definition. */ + +static void +expect_bad_tracepoint_definition (const char *line) +{ + uploaded_tp *utps = nullptr; + + expect_bad_definition ([&] () { parse_tracepoint_definition (line, &utps); }, + [&] () { free_uploaded_tps (&utps); }); +} + +/* Test parse_tracepoint_definition, including missing-colon rejects and + enable-flag rejects. */ + +static void +test_parse_tracepoint_definition () +{ + uploaded_tp *utps = nullptr; + + /* Well-formed T definition: T::::. */ + { + const char *good = "T1:0:E:0:0"; + + parse_tracepoint_definition (good, &utps); + SELF_CHECK (utps != nullptr); + SELF_CHECK (utps->number == 1); + SELF_CHECK (utps->addr == 0); + SELF_CHECK (utps->enabled); + SELF_CHECK (utps->step == 0); + SELF_CHECK (utps->pass == 0); + free_uploaded_tps (&utps); + } + + /* Well-formed disabled T definition. */ + { + const char *good = "T1:0:D:0:0"; + + parse_tracepoint_definition (good, &utps); + SELF_CHECK (utps != nullptr); + SELF_CHECK (!utps->enabled); + free_uploaded_tps (&utps); + } + + expect_bad_tracepoint_definition ("T1"); + expect_bad_tracepoint_definition ("T1:0"); + expect_bad_tracepoint_definition ("T1:0::0:0"); + expect_bad_tracepoint_definition ("T1:0:X:0:0"); + expect_bad_tracepoint_definition ("T1:0:E"); + expect_bad_tracepoint_definition ("T1:0:E:0"); + + /* Well-formed Z definition: at-string hex-encodes "foo". + Encoded form is Z:::::. */ + { + const char *good = "Z1:0:at:0:3:666f6f"; + + parse_tracepoint_definition (good, &utps); + SELF_CHECK (utps != nullptr); + SELF_CHECK (utps->number == 1); + SELF_CHECK (utps->addr == 0); + SELF_CHECK (utps->at_string != nullptr + && streq (utps->at_string.get (), "foo")); + free_uploaded_tps (&utps); + } + + expect_bad_tracepoint_definition ("Z1"); + expect_bad_tracepoint_definition ("Z1:0"); + + /* 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[] = "Z1:0:at:0:0000"; + std::vector buf = make_stale_tail_buffer (prefix, "4444"); + expect_bad_tracepoint_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[] = "Z1:0:at:0:0000"; + std::vector buf + = make_stale_tail_buffer (prefix, std::string (64, '4')); + expect_bad_tracepoint_definition (buf.data ()); + } +} + } /* namespace tracepoint_tests */ } /* namespace selftests */ @@ -184,4 +269,7 @@ INIT_GDB_FILE (tracepoint_selftests) selftests::register_test ("parse_tsv_definition", selftests::tracepoint_tests::test_parse_tsv_definition); + selftests::register_test + ("parse_tracepoint_definition", + selftests::tracepoint_tests::test_parse_tracepoint_definition); } -- 2.56.0