From: "Schimpe, Christina" <christina.schimpe@intel.com>
To: gdb-patches@sourceware.org
Subject: [PATCH 07/12] gdb, bfd: amd64 linux coredump support with shadow stack.
Date: Fri, 20 Dec 2024 20:04:56 +0000 [thread overview]
Message-ID: <20241220200501.324191-8-christina.schimpe@intel.com> (raw)
In-Reply-To: <20241220200501.324191-1-christina.schimpe@intel.com>
From: Felix Willgerodt <felix.willgerodt@intel.com>
Intel's Control-Flow Enforcement Technology (CET) provides the shadow
stack feature for the x86 architecture.
This commit adds support to write and read the shadow-stack node in
corefiles. This helps debugging return address violations post-mortem.
The format is synced with the linux kernel commit "x86: Add PTRACE
interface for shadow stack". As the linux kernel restricts shadow
stack support to 64-bit, apply the fix for amd64 only.
Co-Authored-By: Christina Schimpe <christina.schimpe@intel.com>
---
bfd/elf.c | 24 +++++++++
gdb/amd64-linux-tdep.c | 52 +++++++++++++++++--
.../gdb.arch/amd64-shadow-stack-corefile.exp | 50 ++++++++++++++++++
3 files changed, 122 insertions(+), 4 deletions(-)
create mode 100644 gdb/testsuite/gdb.arch/amd64-shadow-stack-corefile.exp
diff --git a/bfd/elf.c b/bfd/elf.c
index 78394319bf0..23f1ebcd83c 100644
--- a/bfd/elf.c
+++ b/bfd/elf.c
@@ -10365,6 +10365,12 @@ elfcore_grok_xstatereg (bfd *abfd, Elf_Internal_Note *note)
return elfcore_make_note_pseudosection (abfd, ".reg-xstate", note);
}
+static bool
+elfcore_grok_sspreg (bfd *abfd, Elf_Internal_Note *note)
+{
+ return elfcore_make_note_pseudosection (abfd, ".reg-ssp", note);
+}
+
static bool
elfcore_grok_ppc_vmx (bfd *abfd, Elf_Internal_Note *note)
{
@@ -11060,6 +11066,13 @@ elfcore_grok_note (bfd *abfd, Elf_Internal_Note *note)
else
return true;
+ case NT_X86_SHSTK: /* Linux CET extension. */
+ if (note->namesz == 6
+ && strcmp (note->namedata, "LINUX") == 0)
+ return elfcore_grok_sspreg (abfd, note);
+ else
+ return true;
+
case NT_PPC_VMX:
if (note->namesz == 6
&& strcmp (note->namedata, "LINUX") == 0)
@@ -12510,6 +12523,15 @@ elfcore_write_xstatereg (bfd *abfd, char *buf, int *bufsiz,
note_name, NT_X86_XSTATE, xfpregs, size);
}
+static char *
+elfcore_write_sspreg (bfd *abfd, char *buf, int *bufsiz,
+ const void *ssp, int size)
+{
+ const char *note_name = "LINUX";
+ return elfcore_write_note (abfd, buf, bufsiz,
+ note_name, NT_X86_SHSTK, ssp, size);
+}
+
char *
elfcore_write_x86_segbases (bfd *abfd, char *buf, int *bufsiz,
const void *regs, int size)
@@ -13105,6 +13127,8 @@ elfcore_write_register_note (bfd *abfd,
return elfcore_write_xstatereg (abfd, buf, bufsiz, data, size);
if (strcmp (section, ".reg-x86-segbases") == 0)
return elfcore_write_x86_segbases (abfd, buf, bufsiz, data, size);
+ if (strcmp (section, ".reg-ssp") == 0)
+ return elfcore_write_sspreg (abfd, buf, bufsiz, data, size);
if (strcmp (section, ".reg-ppc-vmx") == 0)
return elfcore_write_ppc_vmx (abfd, buf, bufsiz, data, size);
if (strcmp (section, ".reg-ppc-vsx") == 0)
diff --git a/gdb/amd64-linux-tdep.c b/gdb/amd64-linux-tdep.c
index 8ed381e1a2c..95f643b1217 100644
--- a/gdb/amd64-linux-tdep.c
+++ b/gdb/amd64-linux-tdep.c
@@ -44,6 +44,7 @@
#include "expop.h"
#include "arch/amd64-linux-tdesc.h"
#include "inferior.h"
+#include "x86-tdep.h"
/* The syscall's XML filename for i386. */
#define XML_SYSCALL_FILENAME_AMD64 "syscalls/amd64-linux.xml"
@@ -1586,6 +1587,14 @@ amd64_linux_record_signal (struct gdbarch *gdbarch,
return 0;
}
+/* Get shadow stack pointer state from core dump. */
+
+static bool
+amd64_linux_core_read_ssp_state_p (bfd *abfd)
+{
+ return bfd_get_section_by_name (abfd, ".reg-ssp") != NULL;
+}
+
/* Get Linux/x86 target description from core dump. */
static const struct target_desc *
@@ -1595,11 +1604,14 @@ amd64_linux_core_read_description (struct gdbarch *gdbarch,
{
/* Linux/x86-64. */
x86_xsave_layout layout;
- uint64_t xcr0 = i386_linux_core_read_xsave_info (abfd, layout);
- if (xcr0 == 0)
- xcr0 = X86_XSTATE_SSE_MASK;
+ uint64_t xstate_bv_mask = i386_linux_core_read_xsave_info (abfd, layout);
+ if (xstate_bv_mask == 0)
+ xstate_bv_mask = X86_XSTATE_SSE_MASK;
+
+ if (amd64_linux_core_read_ssp_state_p (abfd))
+ xstate_bv_mask |= X86_XSTATE_CET_U;
- return amd64_linux_read_description (xcr0 & X86_XSTATE_ALL_MASK,
+ return amd64_linux_read_description (xstate_bv_mask & X86_XSTATE_ALL_MASK,
gdbarch_ptr_bit (gdbarch) == 32);
}
@@ -1630,6 +1642,30 @@ static const struct regset amd64_linux_xstateregset =
amd64_linux_collect_xstateregset
};
+static void
+amd64_linux_supply_ssp (const struct regset *regset,
+ struct regcache *regcache, int regnum,
+ const void *ssp, size_t len)
+{
+ x86_supply_ssp (regcache, *static_cast<const uint64_t *> (ssp));
+}
+
+static void
+amd64_linux_collect_ssp (const struct regset *regset,
+ const struct regcache *regcache, int regnum,
+ void *ssp, size_t len)
+{
+ x86_collect_ssp (regcache, *static_cast<uint64_t *> (ssp));
+}
+
+/* Shadow stack pointer register. */
+
+static const struct regset amd64_linux_ssp_register
+ {
+ NULL, amd64_linux_supply_ssp, amd64_linux_collect_ssp
+ };
+
+
/* Iterate over core file register note sections. */
static void
@@ -1646,6 +1682,14 @@ amd64_linux_iterate_over_regset_sections (struct gdbarch *gdbarch,
cb (".reg-xstate", tdep->xsave_layout.sizeof_xsave,
tdep->xsave_layout.sizeof_xsave, &amd64_linux_xstateregset,
"XSAVE extended state", cb_data);
+
+ /* SSP can be unavailable. Thus, we need to check the register status
+ in case we write a core file (regcache != nullptr). */
+ if (tdep->ssp_regnum > 0
+ && (regcache == nullptr
+ || REG_VALID == regcache->get_register_status (tdep->ssp_regnum)))
+ cb (".reg-ssp", 8, 8, &amd64_linux_ssp_register,
+ "shadow stack pointer", cb_data);
}
/* The instruction sequences used in x86_64 machines for a
diff --git a/gdb/testsuite/gdb.arch/amd64-shadow-stack-corefile.exp b/gdb/testsuite/gdb.arch/amd64-shadow-stack-corefile.exp
new file mode 100644
index 00000000000..25cc1529f0d
--- /dev/null
+++ b/gdb/testsuite/gdb.arch/amd64-shadow-stack-corefile.exp
@@ -0,0 +1,50 @@
+# Copyright 2021-2024 Free Software Foundation, Inc.
+
+# This program is free software; you can redistribute it and/or modify
+# it under the terms of the GNU General Public License as published by
+# the Free Software Foundation; either version 3 of the License, or
+# (at your option) any later version.
+#
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with this program. If not, see <http://www.gnu.org/licenses/>.
+
+# Test the shadow stack pointer note in core dumps.
+
+require allow_ssp_tests
+
+standard_testfile amd64-shadow-stack.c
+set gcorefile ${binfile}.gcore
+
+save_vars { ::env(GLIBC_TUNABLES) } {
+
+ append_environment GLIBC_TUNABLES "glibc.cpu.hwcaps" "SHSTK"
+
+ if { [prepare_for_testing "failed to prepare" ${testfile} ${srcfile} \
+ additional_flags="-fcf-protection=return"] } {
+ return -1
+ }
+
+ if { ![runto_main] } {
+ return -1
+ }
+
+ # Save ssp for comparison in the corefile session.
+ set ssp [get_hexadecimal_valueof "\$pl3_ssp" ""]
+
+ if { ![gdb_gcore_cmd $gcorefile "save a corefile"] } {
+ return -1
+ }
+
+ # Now restart gdb and load the corefile.
+ clean_restart ${binfile}
+
+ gdb_test "core ${gcorefile}" \
+ "Core was generated by .*" "re-load generated corefile"
+
+ gdb_test "print /x \$pl3_ssp" "= $ssp"
+}
--
2.34.1
Intel Deutschland GmbH
Registered Address: Am Campeon 10, 85579 Neubiberg, Germany
Tel: +49 89 99 8853-0, www.intel.de
Managing Directors: Sean Fennelly, Jeffrey Schneiderman, Tiffany Doon Silva
Chairperson of the Supervisory Board: Nicole Lau
Registered Office: Munich
Commercial Register: Amtsgericht Muenchen HRB 186928
next prev parent reply other threads:[~2024-12-20 20:13 UTC|newest]
Thread overview: 72+ messages / expand[flat|nested] mbox.gz Atom feed top
2024-12-20 20:04 [PATCH 00/12] Add CET shadow stack support Schimpe, Christina
2024-12-20 20:04 ` [PATCH 01/12] gdb, testsuite: Rename set_sanitizer_default to append_environment Schimpe, Christina
2025-01-28 13:45 ` Guinevere Larsen
2025-01-30 13:07 ` Schimpe, Christina
2025-01-30 14:27 ` Tom de Vries
2025-01-30 16:39 ` Schimpe, Christina
2024-12-20 20:04 ` [PATCH 02/12] gdbserver: Add optional runtime register set type Schimpe, Christina
2025-01-28 13:35 ` Guinevere Larsen
2025-01-30 10:28 ` Schimpe, Christina
2025-01-30 13:53 ` Guinevere Larsen
2025-01-30 17:43 ` Schimpe, Christina
2025-02-06 2:59 ` Thiago Jung Bauermann
2025-02-06 12:15 ` Schimpe, Christina
2024-12-20 20:04 ` [PATCH 03/12] gdbserver: Add assert in x86_linux_read_description Schimpe, Christina
2025-02-06 3:00 ` Thiago Jung Bauermann
2024-12-20 20:04 ` [PATCH 04/12] gdb: Sync up x86-gcc-cpuid.h with cpuid.h from gcc 14 branch Schimpe, Christina
2025-02-06 3:03 ` Thiago Jung Bauermann
2025-02-06 12:23 ` Schimpe, Christina
2024-12-20 20:04 ` [PATCH 05/12] gdb, gdbserver: Use xstate_bv for target description creation on x86 Schimpe, Christina
2025-01-30 14:51 ` Guinevere Larsen
2025-01-30 16:45 ` Schimpe, Christina
2025-02-06 3:09 ` Thiago Jung Bauermann
2025-02-06 12:33 ` Schimpe, Christina
2024-12-20 20:04 ` [PATCH 06/12] gdb, gdbserver: Add support of Intel shadow stack pointer register Schimpe, Christina
2025-02-06 3:13 ` Thiago Jung Bauermann
2025-02-06 14:33 ` Schimpe, Christina
2025-02-08 3:44 ` Thiago Jung Bauermann
2024-12-20 20:04 ` Schimpe, Christina [this message]
2025-02-06 3:15 ` [PATCH 07/12] gdb, bfd: amd64 linux coredump support with shadow stack Thiago Jung Bauermann
2025-02-07 11:54 ` Schimpe, Christina
2024-12-20 20:04 ` [PATCH 08/12] gdb: Handle shadow stack pointer register unwinding for amd64 linux Schimpe, Christina
2025-01-30 14:29 ` Guinevere Larsen
2025-01-30 16:11 ` Schimpe, Christina
2025-01-30 16:13 ` Guinevere Larsen
2025-01-30 16:40 ` Schimpe, Christina
2025-02-06 3:30 ` Thiago Jung Bauermann
2025-02-06 14:40 ` Schimpe, Christina
2024-12-20 20:04 ` [PATCH 09/12] gdb, gdbarch: Enable inferior calls for shadow stack support Schimpe, Christina
2025-02-06 3:31 ` Thiago Jung Bauermann
2025-02-06 15:07 ` Schimpe, Christina
2025-02-08 3:57 ` Thiago Jung Bauermann
2025-02-10 8:37 ` Schimpe, Christina
2024-12-20 20:04 ` [PATCH 10/12] gdb: Implement amd64 linux shadow stack support for inferior calls Schimpe, Christina
2025-02-06 3:34 ` Thiago Jung Bauermann
2025-02-07 11:55 ` Schimpe, Christina
2024-12-20 20:05 ` [PATCH 11/12] gdb, gdbarch: Introduce gdbarch method to get the shadow stack pointer Schimpe, Christina
2025-01-28 20:27 ` Guinevere Larsen
2025-01-30 10:33 ` Luis Machado
2025-01-30 12:34 ` Schimpe, Christina
2025-01-30 13:42 ` Guinevere Larsen
2025-02-06 3:35 ` Thiago Jung Bauermann
2025-02-07 12:01 ` Schimpe, Christina
2025-02-08 4:03 ` Thiago Jung Bauermann
2025-02-10 8:58 ` Schimpe, Christina
2025-02-11 1:53 ` Thiago Jung Bauermann
2025-02-15 3:45 ` Thiago Jung Bauermann
2025-02-16 10:45 ` Schimpe, Christina
2025-02-20 8:48 ` Schimpe, Christina
2025-02-21 5:10 ` Thiago Jung Bauermann
2025-02-21 9:41 ` Schimpe, Christina
2024-12-20 20:05 ` [PATCH 12/12] gdb: Enable displaced stepping with shadow stack on amd64 linux Schimpe, Christina
2024-12-20 20:14 ` Eli Zaretskii
2025-01-02 9:04 ` Schimpe, Christina
2025-01-02 9:15 ` Eli Zaretskii
2025-02-06 3:37 ` Thiago Jung Bauermann
2025-01-16 14:01 ` [PING][PATCH 00/12] Add CET shadow stack support Schimpe, Christina
2025-01-27 9:44 ` [PING*2][PATCH " Schimpe, Christina
2025-01-30 15:01 ` [PATCH " Guinevere Larsen
2025-01-30 17:46 ` Schimpe, Christina
2025-02-04 3:57 ` Thiago Jung Bauermann
2025-02-04 9:40 ` Schimpe, Christina
2025-02-06 3:44 ` Thiago Jung Bauermann
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20241220200501.324191-8-christina.schimpe@intel.com \
--to=christina.schimpe@intel.com \
--cc=gdb-patches@sourceware.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox