Mirror of the gdb-patches mailing list
 help / color / mirror / Atom feed
* [PATCH v3 0/1] gdb: fix assert after register write failure
@ 2026-11-04 13:03 Stephan Rohr
  2026-11-04 13:03 ` [PATCH v3 1/1] " Stephan Rohr
  2026-11-06 19:06 ` [PATCH v3 0/1] " Tom Tromey
  0 siblings, 2 replies; 5+ messages in thread
From: Stephan Rohr @ 2026-11-04 13:03 UTC (permalink / raw)
  To: gdb-patches; +Cc: tom, guinevere

From: "Rohr, Stephan" <stephan.rohr@intel.com>

Hi Tom, all,

thanks for the feedback.

I can confirm the use-after-free issue you found.  The proposed patch
now wraps the target_store_registers call in a try-catch block.
It invalidates the register only if the error is not a TARGET_CLOSE_ERROR.

I checked the "store_registers" implementations for other targets.  As
far as I can tell from reading the code, the ravenscar target has the
same use-after-free bug when the target beneath is a remote target,
see ravenscar_thread_target::store_registers ():

  temporarily_change_regcache_ptid changer (regcache, base);
  beneath ()->store_registers (regcache, i);

If the remote write fails in beneath ()->store_registers (), the
destructor of temporarily_change_regcache_ptid calls set_ptid on the
already destroyed regcache.  fetch_registers and prepare_to_store use
the same pattern.  I think this should be fixed separately.

IIUC, on the store_registers path, the regcache can only be deleted if
the remote target is unpushed, which is always followed by a
TARGET_CLOSE_ERROR.  If we want to be on the safe side, we could add a
lifetime token to the regcache:

  std::shared_ptr<int> m_lifetime = std::make_shared<int> (0);

and take a weak reference to it in raw_write before storing:

  std::weak_ptr<int> alive = m_lifetime;
  try
    {
      target_store_registers (this, regnum);
    }
  catch (const gdb_exception &)
    {
      if (!alive.expired ())
        invalidate (regnum);
      throw;
    }

We have no evidence that this is needed.  I think adding the try-catch
block and checking for a TARGET_CLOSE_ERROR suffices.  If you prefer, I
can also add the token.

You can find v2 of the patch at:

https://sourceware.org/pipermail/gdb-patches/2026-October/231112.html

and Tom's feedback:

https://sourceware.org/pipermail/gdb-patches/2026-October/231275.html

Changes since v2:

  - Removed the nullptr check and replaced with try-catch in
    regcache::raw_write ().
  - Added a testcase to gdb.server/server-kill.exp.
  - Reworded the commit message.

Thanks
Stephan

Eduard Sargsyan (1):
  gdb: fix assert after register write failure

 gdb/regcache.c                           | 22 ++++++++++++----------
 gdb/testsuite/gdb.server/server-kill.exp | 20 ++++++++++++++++++++
 2 files changed, 32 insertions(+), 10 deletions(-)

-- 
2.43.0

________________________________________
Intel Deutschland GmbH 

Registered Address: Dornacher Strasse 1, 85622 Feldkirchen, Germany 

Tel: +49 (89) 99143-0 

www.intel.de 

Managing Directors: Candice Moore, Jeffrey Schneiderman, Ramachandran Sitaraman

Chairperson of the Supervisory Board: Sonja Pierer

Registered Seat: Munich Commercial Register B: Amtsgericht Munich HRB 186928

This e-mail and any attachments may contain confidential material for
the sole use of the intended recipient(s). Any review or distribution
by others is strictly prohibited. If you are not the intended
recipient, please contact the sender and delete all copies.


^ permalink raw reply	[flat|nested] 5+ messages in thread

* [PATCH v3 1/1] gdb: fix assert after register write failure
  2026-11-04 13:03 [PATCH v3 0/1] gdb: fix assert after register write failure Stephan Rohr
@ 2026-11-04 13:03 ` Stephan Rohr
  2026-11-06 19:01   ` Tom Tromey
  2026-11-09 19:03   ` Tom Tromey
  2026-11-06 19:06 ` [PATCH v3 0/1] " Tom Tromey
  1 sibling, 2 replies; 5+ messages in thread
From: Stephan Rohr @ 2026-11-04 13:03 UTC (permalink / raw)
  To: gdb-patches; +Cc: tom, guinevere

From: Eduard Sargsyan <eduard.sargsyan@intel.com>

GDB tries to invalidate the register cache if a write to a register fails.
In case the connection to a remote inferior is closed during the write,
the remote target is unpushed, which deletes the regcache.  GDB receives
a TARGET_CLOSE_ERROR.  With this, the regcache is deleted and GDB runs
into a use-after-free bug; the previously set up make_scope_exit
invalidator writes to the already deleted regcache.

Fix this by catching the exception and only invalidating the regcache
if the error is not a TARGET_CLOSE_ERROR.

Co-Authored-By: Stephan Rohr <stephan.rohr@intel.com>
---
 gdb/regcache.c                           | 22 ++++++++++++----------
 gdb/testsuite/gdb.server/server-kill.exp | 20 ++++++++++++++++++++
 2 files changed, 32 insertions(+), 10 deletions(-)

diff --git a/gdb/regcache.c b/gdb/regcache.c
index 773e8ac37d0..abdd49190ee 100644
--- a/gdb/regcache.c
+++ b/gdb/regcache.c
@@ -881,16 +881,18 @@ regcache::raw_write (int regnum, gdb::array_view<const gdb_byte> src)
   target_prepare_to_store (this);
   raw_supply (regnum, src);
 
-  /* Invalidate the register after it is written, in case of a
-     failure.  */
-  auto invalidator
-    = make_scope_exit ([&] { this->invalidate (regnum); });
-
-  target_store_registers (this, regnum);
-
-  /* The target did not throw an error so we can discard invalidating
-     the register.  */
-  invalidator.release ();
+  try
+    {
+      target_store_registers (this, regnum);
+    }
+  catch (const gdb_exception_error &ex)
+    {
+      /* The write failed.  If the target is closed, the regcache is
+	 destroyed and is not accessible.  */
+      if (ex.error != TARGET_CLOSE_ERROR)
+	invalidate (regnum);
+      throw;
+    }
 }
 
 void
diff --git a/gdb/testsuite/gdb.server/server-kill.exp b/gdb/testsuite/gdb.server/server-kill.exp
index d4bbb466f04..a03e04ad63e 100644
--- a/gdb/testsuite/gdb.server/server-kill.exp
+++ b/gdb/testsuite/gdb.server/server-kill.exp
@@ -148,7 +148,27 @@ proc_with_prefix test_stepi {} {
     gdb_test "stepi" "(Target disconnected|Remote connection closed|Remote communication error).*"
 }
 
+# Regression test for a use-after-free bug in the regcache when the remote
+# connection is dropped during register write.  The regcache is destroyed in
+# this case.
+
+proc_with_prefix test_register_write {} {
+    if {![prepare]} {
+	return
+    }
+
+    # Ensure the frame-id is cached before killing gdbserver.  Otherwise,
+    # GDB notices the dead connection when computing the frame-id.
+    gdb_test "info frame" "Stack level 0, .*"
+    kill_server
+
+    # Write a register to the already killed remote connection.
+    gdb_test "set \$pc = \$pc + 1" \
+	"(Target disconnected|Remote (connection closed|communication error)).*"
+}
+
 test_tstatus
 test_unwind_nosyms
 test_unwind_syms
 test_stepi
+test_register_write
-- 
2.43.0

________________________________________
Intel Deutschland GmbH 

Registered Address: Dornacher Strasse 1, 85622 Feldkirchen, Germany 

Tel: +49 (89) 99143-0 

www.intel.de 

Managing Directors: Candice Moore, Jeffrey Schneiderman, Ramachandran Sitaraman

Chairperson of the Supervisory Board: Sonja Pierer

Registered Seat: Munich Commercial Register B: Amtsgericht Munich HRB 186928

This e-mail and any attachments may contain confidential material for
the sole use of the intended recipient(s). Any review or distribution
by others is strictly prohibited. If you are not the intended
recipient, please contact the sender and delete all copies.


^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: [PATCH v3 1/1] gdb: fix assert after register write failure
  2026-11-04 13:03 ` [PATCH v3 1/1] " Stephan Rohr
@ 2026-11-06 19:01   ` Tom Tromey
  2026-11-09 19:03   ` Tom Tromey
  1 sibling, 0 replies; 5+ messages in thread
From: Tom Tromey @ 2026-11-06 19:01 UTC (permalink / raw)
  To: Stephan Rohr; +Cc: gdb-patches, tom, guinevere

>>>>> Stephan Rohr <stephan.rohr@intel.com> writes:

> From: Eduard Sargsyan <eduard.sargsyan@intel.com>
> GDB tries to invalidate the register cache if a write to a register fails.
> In case the connection to a remote inferior is closed during the write,
> the remote target is unpushed, which deletes the regcache.  GDB receives
> a TARGET_CLOSE_ERROR.  With this, the regcache is deleted and GDB runs
> into a use-after-free bug; the previously set up make_scope_exit
> invalidator writes to the already deleted regcache.

> Fix this by catching the exception and only invalidating the regcache
> if the error is not a TARGET_CLOSE_ERROR.

Thanks for doing this.  I think this is ok.
Approved-By: Tom Tromey <tom@tromey.com>

Tom

^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: [PATCH v3 0/1] gdb: fix assert after register write failure
  2026-11-04 13:03 [PATCH v3 0/1] gdb: fix assert after register write failure Stephan Rohr
  2026-11-04 13:03 ` [PATCH v3 1/1] " Stephan Rohr
@ 2026-11-06 19:06 ` Tom Tromey
  1 sibling, 0 replies; 5+ messages in thread
From: Tom Tromey @ 2026-11-06 19:06 UTC (permalink / raw)
  To: Stephan Rohr; +Cc: gdb-patches, tom, guinevere

>>>>> Stephan Rohr <stephan.rohr@intel.com> writes:

> I checked the "store_registers" implementations for other targets.  As
> far as I can tell from reading the code, the ravenscar target has the
> same use-after-free bug when the target beneath is a remote target,
> see ravenscar_thread_target::store_registers ():

>   temporarily_change_regcache_ptid changer (regcache, base);
>   beneath ()->store_registers (regcache, i);

Thanks for pointing this out.

I filed a bug for this:

    https://sourceware.org/bugzilla/show_bug.cgi?id=34973

> IIUC, on the store_registers path, the regcache can only be deleted if
> the remote target is unpushed, which is always followed by a
> TARGET_CLOSE_ERROR.  If we want to be on the safe side, we could add a
> lifetime token to the regcache:

>   std::shared_ptr<int> m_lifetime = std::make_shared<int> (0);

> and take a weak reference to it in raw_write before storing:

Cool technique, thanks.

It does seem like overkill here.

OTOH the current code seems fragile, like maybe more UAFs are waiting to
be introduced in the future.

I didn't dig into the lifetime issues here to try to figure out a robust
solution though.

Tom

^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: [PATCH v3 1/1] gdb: fix assert after register write failure
  2026-11-04 13:03 ` [PATCH v3 1/1] " Stephan Rohr
  2026-11-06 19:01   ` Tom Tromey
@ 2026-11-09 19:03   ` Tom Tromey
  1 sibling, 0 replies; 5+ messages in thread
From: Tom Tromey @ 2026-11-09 19:03 UTC (permalink / raw)
  To: Stephan Rohr; +Cc: gdb-patches, tom, guinevere

>>>>> Stephan Rohr <stephan.rohr@intel.com> writes:

> Fix this by catching the exception and only invalidating the regcache
> if the error is not a TARGET_CLOSE_ERROR.

I looked at fixing up the ravenscar code and this made me realize:

> +  catch (const gdb_exception_error &ex)
> +    {
> +      /* The write failed.  If the target is closed, the regcache is
> +	 destroyed and is not accessible.  */
> +      if (ex.error != TARGET_CLOSE_ERROR)
> +	invalidate (regnum);
> +      throw;

... here, I think this code should catch gdb_exception.

Normally catching gdb_exception_error is what you want, with the idea
being that a QUIT ought to propagate upward.

However, in this case, a QUIT would violate the invariant here, leaving
the register apparently valid.

And, since this code is re-throwing anyway, I think simply catching all
exceptions here would be correct.

What do you think?

thanks,
Tom

^ permalink raw reply	[flat|nested] 5+ messages in thread

end of thread, other threads:[~2026-11-09 19:04 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2026-11-04 13:03 [PATCH v3 0/1] gdb: fix assert after register write failure Stephan Rohr
2026-11-04 13:03 ` [PATCH v3 1/1] " Stephan Rohr
2026-11-06 19:01   ` Tom Tromey
2026-11-09 19:03   ` Tom Tromey
2026-11-06 19:06 ` [PATCH v3 0/1] " Tom Tromey

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox