From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from simark.ca by simark.ca with LMTP id PfFgAy6L/WrsERwAWB0awg (envelope-from ) for ; Wed, 18 Nov 2026 06:25:34 -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=NapBOe1X; dkim-atps=neutral Received: by simark.ca (Postfix, from userid 112) id F0FC81E087; Wed, 18 Nov 2026 06:25:33 -0500 (EST) X-Spam-Checker-Version: SpamAssassin 4.0.1 (2024-03-25) on simark.ca X-Spam-Level: X-Spam-Status: No, score=-6.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 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 AB7461E087 for ; Wed, 18 Nov 2026 06:25:32 -0500 (EST) Received: from vm01.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 3F5EA4BA23CB for ; Wed, 18 Nov 2026 11:25:32 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 3F5EA4BA23CB 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=NapBOe1X 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 3B46E4BA2E1A for ; Wed, 18 Nov 2026 11:25:06 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 3B46E4BA2E1A 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 3B46E4BA2E1A 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=1795001106; cv=none; b=x87h5zFo/MEM9F6QzH7udRUh+MqErwrU6qA8KWqEGS4oQFwBRrWPBGnnK/K8SlWMjRd/nvqpAr6b26sktXjQ+q8C/jJi8990YU/3Y/Ajt1PVpG2Rg+si/lmlVu67b36aevB0iY3nqGl8yV/KazzrzAF02WZG4cxhnUior1m96mw= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1795001106; c=relaxed/simple; bh=i9wyFd17BEAddvkIwQWKDcSNZ9AHKtWbfhDj9Cc20xo=; h=DKIM-Signature:From:To:Subject:Date:Message-Id:MIME-Version; b=tmaiSeU1aTvy64KY6N2bq/eh/uM8OngF0S3gD5M9OU/0vZjRZAR0d5V3vLNwY5PjRHqM68nu72L9LAScTgv17HxFMuojk5qLLrPp0b+j9cCs/92KQ8CqiFpaEBuQyAXIbNz1vP5HHbjYkvdj5e5m8VPxXfBW8w4LMU8N4ZGz5Pg= 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=NapBOe1X DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 3B46E4BA2E1A DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1795001105; 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=DsnurMnrTHhKyeITxV4isXy/rGBvRiMhXmBP/nA1OfQ=; b=NapBOe1XvUZ5ab9uJOWV+Z2oXSRv9Sz5+yiA5ZhmoOXnGOJJMSFemqiE2JwLtm8T2kd29z tInl/mRap/FvmVX9kGyU5FiblskHiwi/S8QQIVFhTrYK40aauS9AOjfOqfiRRwObIXY7Jp XjqvfhVmlSZqyESSUVjpoqrtz11uwYE= Received: from mail-wr1-f69.google.com (mail-wr1-f69.google.com [209.85.221.69]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-479-7x6tDRgKO_GhhnCxf4LwLw-1; Wed, 18 Nov 2026 06:25:04 -0500 X-MC-Unique: 7x6tDRgKO_GhhnCxf4LwLw-1 X-Mimecast-MFC-AGG-ID: 7x6tDRgKO_GhhnCxf4LwLw_1795001103 Received: by mail-wr1-f69.google.com with SMTP id ffacd0b85a97d-490c1867460so503282f8f.2 for ; Wed, 18 Nov 2026 03:25:04 -0800 (PST) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20260707; t=1795001103; x=1795605903; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:x-gm-gg:x-gm-message-state:from:to:cc:subject:date :message-id:reply-to:content-type; bh=DsnurMnrTHhKyeITxV4isXy/rGBvRiMhXmBP/nA1OfQ=; b=aZ9r5KrsnlI8oE+PTTHKaqRpXD4ATbuDnEX9oHXQgQn/qt7bpYjsBLQBFrCl+QKq7F /RGeJFd6aGxy9ObokHluHTqicTzaDhsO9pn6H4zjIB7GVw9gkgGRf6v+IGnMvn03avtk JnRCUk+PrWLetu3wvWTd14+3NKdZ2GB+KYPDtqNXYNSY03PHBCjp15cF7Vu+MJI4sE4c ObHCUduIB7bicSw6xEBc+d9WuMqk/bqJPnPp5Paes0lEfcvgXOKVK2K3/Ab4XMk4CqOL 2MZbKhSjSCv+54gvCmAJwDwC0mSjnQ7oWcfpQLzLgGffK8puQ7wwP/mkC3wRl2dGaObU /lSQ== X-Gm-Message-State: APuU4Z8bAcN14cMvahmM4t2XQFqBlXTfXhkB7ccj7Fl1wvdYdP/e63V9 2PqWLzs220gvkzldaHr7ta4PWdJ0U7eKMjJKFCzxIE7UaSwI4yIjQNT9FAhsQ6Y+Rn8REa8NVMK gQobvfek2Q0IPUCAKOKcBH+6lte2rpXj8YmpnftbDY0p0CREVdQrvTWn+pR6TOyoAi9Ip3NcV1/ pRJ2xZeajuF19w9PD9ksji8u13M1tWVx6Icquy+vlT/EZcIqY= X-Gm-Gg: AZFdTZcA7bML+dYmvFEkXnkZCl9fhjrdVMBnD99WcRXNK5wMYaEaZ3sm5uUjkj9LBKp a6AMzZDWUuwi04vVBz303r2rVAoJazosZ8rsQEbBFvdDDlolTFkqmr0foHn+mpd07zx2JvXupv/ iWA82t/MBVOlmWz13nav290mbdvNb0ChfqE/B869AiSl2QPCb6XjoHC4E8wXNIdGrxgQjA7KIiA LzREgO9fzHz9u81hQZOJAlC/S219sE/tmbqtOC73HG7rjKBgFhFR+DIQGfsOLj68gS5MTU0sgCT AXmxnEd/+joN406XdhU+gLeIy4J/4q0+n9/VT6vT1VUAua52QCMF8Dn7xAx0pWg82yeI4kt2vR9 LsjtyFULImDCBcEUHMI5E4IZ+OwRxGFFXVQ97qt+rZg4N X-Received: by 2002:a05:6000:24c7:b0:4a4:ee9f:a3d2 with SMTP id ffacd0b85a97d-4a63eac5b4bmr4776512f8f.55.1795001103026; Wed, 18 Nov 2026 03:25:03 -0800 (PST) X-Received: by 2002:a05:6000:24c7:b0:4a4:ee9f:a3d2 with SMTP id ffacd0b85a97d-4a63eac5b4bmr4776439f8f.55.1795001102428; Wed, 18 Nov 2026 03:25:02 -0800 (PST) Received: from localhost ([83.216.138.189]) by smtp.gmail.com with ESMTPSA id ffacd0b85a97d-4a6575a5c28sm4337597f8f.59.2026.11.18.03.25.01 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 18 Nov 2026 03:25:02 -0800 (PST) From: Andrew Burgess To: gdb-patches@sourceware.org Cc: Andrew Burgess Subject: [PATCH] gdbserver: fix memory leak in vRun handling Date: Wed, 18 Nov 2026 11:24:59 +0000 Message-Id: <1f52ddf6fec78fe15933d75609a09a1fcdc471a5.1795001077.git.aburgess@redhat.com> X-Mailer: git-send-email 2.25.4 MIME-Version: 1.0 X-Mimecast-Spam-Score: 0 X-Mimecast-MFC-PROC-ID: jKp9R125SDi1TGTroB2aKNsknZR6IbiSteVltrPx0to_1795001103 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 In decode_v_run_arg (gdbserver/server.cc) a buffer is allocated in which the inferior argument will be decoded. hex2bin is then used to decode the inferior argument. If hex2bin throws an exception (on seeing a non-hex character), we return from decode_v_run_arg without freeing the previously allocated buffer. Fix this by creating the buffer as gdb::unique_xmalloc_ptr. The decode_v_run_arg function already returned its results as a gdb::unique_xmalloc_ptr, but this was previously created at the return point, now we just create it earlier. Currently the decode_v_run_arg function injects a null character after the decoded argument string. I wondered if hex2bin could ever decode less than LEN bytes, however I don't believe this is possible due to the checks performed early in decode_v_run_arg, and based on the value of LEN passed in. To reflect this I have added an assert within decode_v_run_arg. Other than the memory leak being fixed, there should be no user visible changes after this commit. I have added a test that exercises the memory leak code path, however, the memory leak isn't detected by default. Run the test by hand with gdbserver running under valgrind or using the address sanitizer and the leak will show up. --- gdb/testsuite/gdb.server/invalid-vrun.exp | 60 +++++++++++++++++++++++ gdbserver/server.cc | 17 +++++-- 2 files changed, 73 insertions(+), 4 deletions(-) create mode 100644 gdb/testsuite/gdb.server/invalid-vrun.exp diff --git a/gdb/testsuite/gdb.server/invalid-vrun.exp b/gdb/testsuite/gdb.server/invalid-vrun.exp new file mode 100644 index 00000000000..5f189b7feb9 --- /dev/null +++ b/gdb/testsuite/gdb.server/invalid-vrun.exp @@ -0,0 +1,60 @@ +# This testcase is part of GDB, the GNU debugger. +# +# Copyright 2026 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 . + +# Test handling of vRun packet where argument fields contains invalid +# non-hex characters. + +load_lib gdbserver-support.exp + +require allow_gdbserver_tests + +standard_testfile normal.c + +save_vars { GDBFLAGS } { + # If GDB and GDBserver are both running locally, set the sysroot to avoid + # reading files via the remote protocol (the `is_remote target` check is + # already done above). + if { ![is_remote host] } { + set GDBFLAGS "$GDBFLAGS -ex \"set sysroot\"" + } + if {[prepare_for_testing "failed to prepare" $testfile $srcfile]} { + return + } +} + +# Make sure we're disconnected, in case we're testing with an +# extended-remote board, therefore already connected. +gdb_test "disconnect" ".*" + +# Start gdbserver and connect. Must start in extended mode so that we +# can use vRun. +set target_exec [gdbserver_download_current_prog] + +set res [gdbserver_start "--multi" $target_exec] +set gdbserver_protocol "extended-remote" +set gdbserver_gdbport [lindex $res 1] +gdb_target_cmd $gdbserver_protocol $gdbserver_gdbport + +# Send a valid vRun packet to prove that gdbserver isn't rejecting all +# packets. +gdb_test "maint packet vRun;;6162" "\r\nreceived: \"T.*" \ + "valid vRun packet accepted" + +# Send an invalid packet. Once upon a time this used to leak memory +# inside gdbserver. +gdb_test "maint packet vRun;;xxxx" "\r\nreceived: \"E01\"" \ + "non-hex inferior argument rejected" diff --git a/gdbserver/server.cc b/gdbserver/server.cc index 74774fcde7c..f01497bdd06 100644 --- a/gdbserver/server.cc +++ b/gdbserver/server.cc @@ -3412,7 +3412,7 @@ decode_v_run_arg (const char *ptr, size_t len) /* Buffer to decode the argument into. The '+ 1' is for the null terminator we will add. */ - char *arg = (char *) xmalloc (len + 1); + gdb::unique_xmalloc_ptr arg ((char *) xmalloc (len + 1)); /* Decode the argument from the packet and add a null terminator. We do this within a try block as invalid characters within the PTR buffer @@ -3420,15 +3420,24 @@ decode_v_run_arg (const char *ptr, size_t len) returning nullptr in order to clean up some memory allocations. */ try { - hex2bin (ptr, (gdb_byte *) arg, len); - arg[len] = '\0'; + size_t res = hex2bin (ptr, (gdb_byte *) arg.get (), len); + + /* hex2bin can return early if it encounters a null character, + but this will not happen in this case as LEN is based on a + scan of the PTR buffer and will be capped at any null + character within. As such, as we already check for an even + number of bytes above, we expect hex2bin to parse exactly LEN + bytes. */ + gdb_assert (res == len); + + arg.get ()[len] = '\0'; } catch (const gdb_exception_error &exception) { return nullptr; } - return gdb::unique_xmalloc_ptr (arg); + return arg; } /* Run a new program. */ base-commit: 5678a7bfceb3a6ea18bcde1ae68f7e96c43c998b -- 2.25.4