From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from simark.ca by simark.ca with LMTP id t1tGBTiNBmv92zsAWB0awg (envelope-from ) for ; Wed, 25 Nov 2026 02:24:40 -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=jTAT0+NS; dkim-atps=neutral Received: by simark.ca (Postfix, from userid 112) id EE8CA1E09B; Wed, 25 Nov 2026 02:24:39 -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 282501E09B for ; Wed, 25 Nov 2026 02:24:39 -0500 (EST) Received: from vm01.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 33D994C08F9A for ; Wed, 25 Nov 2026 07:24:37 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 33D994C08F9A 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=jTAT0+NS 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 6A62B4C08FA0 for ; Wed, 25 Nov 2026 07:24:11 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 6A62B4C08FA0 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 6A62B4C08FA0 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=1795591451; cv=none; b=DI8K8Be8fnf4ydjpHjAXVvmG5v/8G0Wjbt2wM92rY/GFFNzyfpN5/+x0zrn4TdScLnNsR+aiPa+sSLmfKTSlZJBnFYxUmvfTxnqmYeoBLMNJOt5uzeY1PeBtffA2h3qDJX4Zf4XsigFykoVZ3vyn+ZWZBh/UYyo/aPnxJxAalrw= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1795591451; c=relaxed/simple; bh=Sm7ZEKG2JSQdzBiiuCExnuFVqIZHiK+GEVahbM9zvU0=; h=DKIM-Signature:Message-ID:Date:MIME-Version:Subject:To:From; b=JoL3rgpiqxC/aMVRYxfxMTHluUpkF38RdVppn3jy63F5mz9hhCoz+JlfQ1eCd+6LV6XcVYfWjHd5iO0TnNFzl1wWqAMcfEWQXhYlcm83JaQASuvuW7zfAHD+BXgI9NjcDUI7M3mQDsF8MR3DDFKLbNtM7gPIu5pr3IAmjJiNg0U= 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=jTAT0+NS DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 6A62B4C08FA0 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1795591451; 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=qe1QeINTgH2qyGzWsn/w5SzwrEhh3CkLwrsBn7xewBo=; b=jTAT0+NScY5G1KXmuPfnWT9vmlw7VeAE+pPrPxo7K6GyZTUTLUAHb8KH91YSTfnvqLfyQs dGU+RZARQNxQoVkI8cO9pR8TcVtdz/yV8cT07iEsCWUtP+YAicvjRe5KTTMR012aLWgy2I Jy19SEWZCrdtL66qGGNrKAqb0eub7CM= Received: from mx-prod-mc-03.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-620-OHFPlhQ2MT6sjQmndTCJOw-1; Wed, 25 Nov 2026 07:24:09 +0000 X-MC-Unique: OHFPlhQ2MT6sjQmndTCJOw-1 X-Mimecast-MFC-AGG-ID: OHFPlhQ2MT6sjQmndTCJOw_1795591448 Received: from mx-prod-int-08.mail-002.prod.us-west-2.aws.redhat.com (mx-prod-int-08.mail-002.prod.us-west-2.aws.redhat.com [10.30.177.111]) (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-03.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTPS id 5E1BF1955F29 for ; Wed, 25 Nov 2026 07:24:08 +0000 (UTC) Received: from [100.90.172.125] (headnet04.pony-001.prod.iad2.dc.redhat.com [10.2.32.116]) by mx-prod-int-08.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTPS id 87BF01801971; Wed, 25 Nov 2026 07:24:07 +0000 (UTC) Message-ID: Date: Wed, 25 Nov 2026 00:24:05 -0700 MIME-Version: 1.0 User-Agent: Mozilla Thunderbird Subject: Re: [PATCH 3/5] gdbserver: fix buffer overflow in more m/x packet handling To: Andrew Burgess , gdb-patches@sourceware.org References: <7ec90ad7624f32c6edc9298eb067092c6b9631ee.1795186852.git.aburgess@redhat.com> From: Kevin Buettner In-Reply-To: <7ec90ad7624f32c6edc9298eb067092c6b9631ee.1795186852.git.aburgess@redhat.com> X-Scanned-By: MIMEDefang 3.4.1 on 10.30.177.111 X-Mimecast-Spam-Score: 0 X-Mimecast-MFC-PROC-ID: s22RH2vLxUeicVfwVP3xh01UhUUFsfc2T11OfEIZh_Y_1795591448 X-Mimecast-Originator: redhat.com Content-Language: en-US Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 7bit 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 On 11/20/26 8:12 AM, Andrew Burgess wrote: > Building on the work from two patches back, this commit addresses more > buffer overflow issues with 'm' and 'x' packet handling. The function > handle_limited_packets_while_waiting (in gdbserver/remote-utils.cc) > contains a loop which handles a small sub-set of packets, including 'm' > and 'x'. > > Both the 'm' and 'x' packets include a length field which declares how > much data should be read from the inferior. This length is used, > unchecked, to allocate a buffer in gdbserver. A malicious, or buggy > debug client could ask for a very large read which would cause gdbserver > to go out of memory trying to allocate the buffer. > > If the buffer allocation succeeds then the result is copied into the > client_state's own_buf buffer, which is 'PBUFSIZ + 1' bytes in size. > For the 'm' packet the amount written into own_buf is determined by the > size the client requested, which means we could overflow own_buf with > content read from the inferior's memory. > > This commit fixes these issues. For the 'm' packet we only support > lengths up to PBUFSIZ / 2. Each byte read from the inferior is encoded > as two hex characters, so trying to read any more from the inferior will > overflow own_buf. This limit is imposed before we try to allocate the > temporary buffer, so we know the requested buffer size will be > reasonable. > > For the 'x' packet we only allow reads of up to PBUFSIZ bytes. This > limit means that the allocated buffer size will be reasonable. The 'x' > packet code already protected writes into own_buf so overflow was never > a problem in this case. > > I've used gdb::unique_xmalloc_ptr to handle the buffer allocations for > 'm' and 'x' packets. This means that for the 'X' and 'M' packets, which > still manage their buffers with xmalloc and free, the MEM_BUF variable > is pushed down into the specific case block for each packet type. I > also used SCOPE_EXIT to ensure the buffers are always freed for these > two packets. > > There's an extension to the gdb.server/mx-packet-overflow.exp test which > manually sends a qSymbol packet to gdbserver to enter > handle_limited_packets_while_waiting, then sends various m and x packets > to test their handling. > > It is worth noting that the 'M' and 'X' packet handling performed by > decode_M_packet and decode_X_packet also have issues with trusting > client supplied lengths which could lead to excessive memory allocation > and buffer overflow. These will be addressed in a follow on commit. Claude's comments on this one: Patch 3/5 looks good to me. I have one optional suggestion, for the test. The new qSymbol part of mx-packet-overflow.exp calls run_mx_packet_tests (and, after patches 4 and 5, run_MX_packet_tests) while gdbserver is waiting for the qSymbol reply. Both procs start by calling get_hexadecimal_valueof to find the addresses of buffer and buffer2 and the value of buffer_length. That means GDB evaluates expressions, and sends its own packets, in the middle of the exchange. This works today, because GDB reads those variables with 'm' or 'x' packets, which handle_limited_packets_while_waiting services. But the test then depends on GDB never sending anything else while evaluating an expression. If that ever changed, gdbserver would leave the loop, treat GDB's packet as a malformed qSymbol reply, and every later test would fail without pointing at the real cause. A cheap way to avoid that is to look the values up once, before sending "maint packet qSymbol::", and pass them in, something like: set addr [get_hexadecimal_valueof buffer 0x0 \ "get buffer address"] set buffer_len [get_hexadecimal_valueof buffer_length 0x0 \ "get buffer length"] ... run_mx_packet_tests $addr $buffer_len Then the only packets sent during the exchange are the ones the test sends on purpose. It would also avoid running the same "get ... address" tests twice under two prefixes. This is only a robustness point, though, so I'm happy for it to go in as is. For my part, I checked how get_hexadecimal_valueof was being used. I discovered that you're correctly stripping the leading `0x` when it matters. Approved-By: Kevin Buettner