[v3,1/1] gdb: fix assert after register write failure
Checks
| Context |
Check |
Description |
| linaro-tcwg-bot/tcwg_gdb_build--master-arm |
success
|
Build passed
|
| linaro-tcwg-bot/tcwg_gdb_build--master-aarch64 |
success
|
Build passed
|
| linaro-tcwg-bot/tcwg_gdb_check--master-arm |
success
|
Test passed
|
| linaro-tcwg-bot/tcwg_gdb_check--master-aarch64 |
success
|
Test passed
|
Commit Message
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(-)
Comments
>>>>> 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
>>>>> 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
@@ -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
@@ -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