Blame SOURCES/gdb-dont-overwrite-fsgsbase-m32.patch

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