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