Mirror of the gdb-patches mailing list
 help / color / mirror / Atom feed
From: Andrew Burgess <aburgess@redhat.com>
To: gdb-patches@sourceware.org
Cc: Andrew Burgess <aburgess@redhat.com>
Subject: [PATCH] gdbserver: fix memory leak in vRun handling
Date: Wed, 18 Nov 2026 11:24:59 +0000	[thread overview]
Message-ID: <1f52ddf6fec78fe15933d75609a09a1fcdc471a5.1795001077.git.aburgess@redhat.com> (raw)

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<char>.  The
decode_v_run_arg function already returned its results as a
gdb::unique_xmalloc_ptr<char>, 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 <http://www.gnu.org/licenses/>.
+
+# 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<char> 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<char> (arg);
+  return arg;
 }
 
 /* Run a new program.  */

base-commit: 5678a7bfceb3a6ea18bcde1ae68f7e96c43c998b
-- 
2.25.4


             reply	other threads:[~2026-11-18 11:25 UTC|newest]

Thread overview: 4+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-11-18 11:24 Andrew Burgess [this message]
2026-11-19 20:23 ` Keith Seitz
2026-11-23 14:45   ` Andrew Burgess
2026-11-19 20:38 ` Tom Tromey

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=1f52ddf6fec78fe15933d75609a09a1fcdc471a5.1795001077.git.aburgess@redhat.com \
    --to=aburgess@redhat.com \
    --cc=gdb-patches@sourceware.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox