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