|
|
a8223e |
From FEDORA_PATCHES Mon Sep 17 00:00:00 2001
|
|
|
a8223e |
From: Tom de Vries <tdevries@suse.de>
|
|
|
a8223e |
Date: Tue, 1 Jun 2021 10:14:31 -0700
|
|
|
a8223e |
Subject: gdb-dont-overwrite-fsgsbase-m32.patch
|
|
|
a8223e |
|
|
|
a8223e |
;; Backport "[gdb/server] Don't overwrite fs/gs_base with -m32"
|
|
|
a8223e |
;; (Tom de Vries)
|
|
|
a8223e |
|
|
|
a8223e |
Consider a minimal test-case test.c:
|
|
|
a8223e |
...
|
|
|
a8223e |
int main (void) { return 0; }
|
|
|
a8223e |
...
|
|
|
a8223e |
compiled with -m32:
|
|
|
a8223e |
...
|
|
|
a8223e |
$ gcc test.c -m32
|
|
|
a8223e |
...
|
|
|
a8223e |
|
|
|
a8223e |
When running the exec using gdbserver on openSUSE Factory (currently running a
|
|
|
a8223e |
linux kernel version 5.10.5):
|
|
|
a8223e |
...
|
|
|
a8223e |
$ gdbserver localhost:12345 a.out
|
|
|
a8223e |
...
|
|
|
a8223e |
to which we connect in a gdb session, we run into a segfault in the inferior:
|
|
|
a8223e |
...
|
|
|
a8223e |
$ gdb -batch -q -ex "target remote localhost:12345" -ex continue
|
|
|
a8223e |
Program received signal SIGSEGV, Segmentation fault.
|
|
|
a8223e |
0xf7dd8bd2 in init_cacheinfo () at ../sysdeps/x86/cacheinfo.c:761
|
|
|
a8223e |
...
|
|
|
a8223e |
|
|
|
a8223e |
The segfault is caused by gdbserver overwriting $gs_base with 0 using
|
|
|
a8223e |
PTRACE_SETREGS. After it is overwritten, the next use of $gs in the inferior
|
|
|
a8223e |
will trigger the segfault.
|
|
|
a8223e |
|
|
|
a8223e |
Before linux kernel version 5.9, the value used by PTRACE_SETREGS for $gs_base
|
|
|
a8223e |
was ignored, but starting version 5.9, the linux kernel has support for
|
|
|
a8223e |
intel architecture extension FSGSBASE, which allows users to modify $gs_base,
|
|
|
a8223e |
and consequently PTRACE_SETREGS can no longer ignore the $gs_base value.
|
|
|
a8223e |
|
|
|
a8223e |
The overwrite of $gs_base with 0 is done by a memset in x86_fill_gregset,
|
|
|
a8223e |
which was added in commit 9e0aa64f551 "Fix gdbserver qGetTLSAddr for
|
|
|
a8223e |
x86_64 -m32". The memset intends to zero-extend 32-bit registers that are
|
|
|
a8223e |
tracked in the regcache to 64-bit when writing them into the PTRACE_SETREGS
|
|
|
a8223e |
data argument. But in addition, it overwrites other registers that are
|
|
|
a8223e |
not tracked in the regcache, such as $gs_base.
|
|
|
a8223e |
|
|
|
a8223e |
Fix the segfault by redoing the fix from commit 9e0aa64f551 in minimal form.
|
|
|
a8223e |
|
|
|
a8223e |
Tested on x86_64-linux:
|
|
|
a8223e |
- openSUSE Leap 15.2 (using kernel version 5.3.18):
|
|
|
a8223e |
- native
|
|
|
a8223e |
- gdbserver -m32
|
|
|
a8223e |
- -m32
|
|
|
a8223e |
- openSUSE Factory (using kernel version 5.10.5):
|
|
|
a8223e |
- native
|
|
|
a8223e |
- m32
|
|
|
a8223e |
|
|
|
a8223e |
gdbserver/ChangeLog:
|
|
|
a8223e |
|
|
|
a8223e |
2021-01-20 Tom de Vries <tdevries@suse.de>
|
|
|
a8223e |
|
|
|
a8223e |
* linux-x86-low.cc (collect_register_i386): New function.
|
|
|
a8223e |
(x86_fill_gregset): Remove memset. Use collect_register_i386.
|
|
|
a8223e |
|
|
|
a8223e |
diff --git a/gdbserver/linux-x86-low.cc b/gdbserver/linux-x86-low.cc
|
|
|
a8223e |
--- a/gdbserver/linux-x86-low.cc
|
|
|
a8223e |
+++ b/gdbserver/linux-x86-low.cc
|
|
|
a8223e |
@@ -398,6 +398,35 @@ x86_target::low_cannot_fetch_register (int regno)
|
|
|
a8223e |
}
|
|
|
a8223e |
|
|
|
a8223e |
static void
|
|
|
a8223e |
+collect_register_i386 (struct regcache *regcache, int regno, void *buf)
|
|
|
a8223e |
+{
|
|
|
a8223e |
+ collect_register (regcache, regno, buf);
|
|
|
a8223e |
+
|
|
|
a8223e |
+#ifdef __x86_64__
|
|
|
a8223e |
+ /* In case of x86_64 -m32, collect_register only writes 4 bytes, but the
|
|
|
a8223e |
+ space reserved in buf for the register is 8 bytes. Make sure the entire
|
|
|
a8223e |
+ reserved space is initialized. */
|
|
|
a8223e |
+
|
|
|
a8223e |
+ gdb_assert (register_size (regcache->tdesc, regno) == 4);
|
|
|
a8223e |
+
|
|
|
a8223e |
+ if (regno == RAX)
|
|
|
a8223e |
+ {
|
|
|
a8223e |
+ /* Sign extend EAX value to avoid potential syscall restart
|
|
|
a8223e |
+ problems.
|
|
|
a8223e |
+
|
|
|
a8223e |
+ See amd64_linux_collect_native_gregset() in
|
|
|
a8223e |
+ gdb/amd64-linux-nat.c for a detailed explanation. */
|
|
|
a8223e |
+ *(int64_t *) buf = *(int32_t *) buf;
|
|
|
a8223e |
+ }
|
|
|
a8223e |
+ else
|
|
|
a8223e |
+ {
|
|
|
a8223e |
+ /* Zero-extend. */
|
|
|
a8223e |
+ *(uint64_t *) buf = *(uint32_t *) buf;
|
|
|
a8223e |
+ }
|
|
|
a8223e |
+#endif
|
|
|
a8223e |
+}
|
|
|
a8223e |
+
|
|
|
a8223e |
+static void
|
|
|
a8223e |
x86_fill_gregset (struct regcache *regcache, void *buf)
|
|
|
a8223e |
{
|
|
|
a8223e |
int i;
|
|
|
a8223e |
@@ -411,32 +440,14 @@ x86_fill_gregset (struct regcache *regcache, void *buf)
|
|
|
a8223e |
|
|
|
a8223e |
return;
|
|
|
a8223e |
}
|
|
|
a8223e |
-
|
|
|
a8223e |
- /* 32-bit inferior registers need to be zero-extended.
|
|
|
a8223e |
- Callers would read uninitialized memory otherwise. */
|
|
|
a8223e |
- memset (buf, 0x00, X86_64_USER_REGS * 8);
|
|
|
a8223e |
#endif
|
|
|
a8223e |
|
|
|
a8223e |
for (i = 0; i < I386_NUM_REGS; i++)
|
|
|
a8223e |
- collect_register (regcache, i, ((char *) buf) + i386_regmap[i]);
|
|
|
a8223e |
-
|
|
|
a8223e |
- collect_register_by_name (regcache, "orig_eax",
|
|
|
a8223e |
- ((char *) buf) + ORIG_EAX * REGSIZE);
|
|
|
a8223e |
+ collect_register_i386 (regcache, i, ((char *) buf) + i386_regmap[i]);
|
|
|
a8223e |
|
|
|
a8223e |
-#ifdef __x86_64__
|
|
|
a8223e |
- /* Sign extend EAX value to avoid potential syscall restart
|
|
|
a8223e |
- problems.
|
|
|
a8223e |
-
|
|
|
a8223e |
- See amd64_linux_collect_native_gregset() in gdb/amd64-linux-nat.c
|
|
|
a8223e |
- for a detailed explanation. */
|
|
|
a8223e |
- if (register_size (regcache->tdesc, 0) == 4)
|
|
|
a8223e |
- {
|
|
|
a8223e |
- void *ptr = ((gdb_byte *) buf
|
|
|
a8223e |
- + i386_regmap[find_regno (regcache->tdesc, "eax")]);
|
|
|
a8223e |
-
|
|
|
a8223e |
- *(int64_t *) ptr = *(int32_t *) ptr;
|
|
|
a8223e |
- }
|
|
|
a8223e |
-#endif
|
|
|
a8223e |
+ /* Handle ORIG_EAX, which is not in i386_regmap. */
|
|
|
a8223e |
+ collect_register_i386 (regcache, find_regno (regcache->tdesc, "orig_eax"),
|
|
|
a8223e |
+ ((char *) buf) + ORIG_EAX * REGSIZE);
|
|
|
a8223e |
}
|
|
|
a8223e |
|
|
|
a8223e |
static void
|