From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from simark.ca by simark.ca with LMTP id 2WGEDX8limhPFAAAWB0awg (envelope-from ) for ; Wed, 30 Jul 2025 10:00:31 -0400 Authentication-Results: simark.ca; dkim=pass (1024-bit key; unprotected) header.d=redhat.com header.i=@redhat.com header.a=rsa-sha256 header.s=mimecast20190719 header.b=XxW8ROtx; dkim-atps=neutral Received: by simark.ca (Postfix, from userid 112) id 1DCBA1E102; Wed, 30 Jul 2025 10:00:31 -0400 (EDT) X-Spam-Checker-Version: SpamAssassin 4.0.1 (2024-03-25) on simark.ca X-Spam-Level: X-Spam-Status: No, score=-10.1 required=5.0 tests=ARC_SIGNED,ARC_VALID, BAYES_00,DKIMWL_WL_HIGH,DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU, MAILING_LIST_MULTI,RCVD_IN_DNSWL_MED,RCVD_IN_VALIDITY_CERTIFIED, RCVD_IN_VALIDITY_RPBL,RCVD_IN_VALIDITY_SAFE autolearn=ham autolearn_force=no version=4.0.1 Received: from server2.sourceware.org (server2.sourceware.org [8.43.85.97]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (prime256v1) server-digest SHA256) (No client certificate requested) by simark.ca (Postfix) with ESMTPS id DDB341E091 for ; Wed, 30 Jul 2025 10:00:29 -0400 (EDT) Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 320A83858C66 for ; Wed, 30 Jul 2025 14:00:29 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 320A83858C66 Authentication-Results: sourceware.org; dkim=pass (1024-bit key, unprotected) header.d=redhat.com header.i=@redhat.com header.a=rsa-sha256 header.s=mimecast20190719 header.b=XxW8ROtx Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.129.124]) by sourceware.org (Postfix) with ESMTP id 055CF3858C41 for ; Wed, 30 Jul 2025 13:59:51 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 055CF3858C41 Authentication-Results: sourceware.org; dmarc=pass (p=quarantine dis=none) header.from=redhat.com Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=redhat.com ARC-Filter: OpenARC Filter v1.0.0 sourceware.org 055CF3858C41 Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=170.10.129.124 ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1753883992; cv=none; b=yDN2jqu1WC/9xYWnxPmgReZQlW0BtWl4X2/2Qct+kWjkrOkWrF47/VhzrQy4RxtGPv4DMTEkQcXIOCDNRGSwclCaAXvxjwNn+sV5BMTgQpD9LeyuadYKeR4Axiz5cHzSl+J4ko5sYk06sjg4TIAvWqn/ix/iJUkwm8bSzGXvhEI= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1753883992; c=relaxed/simple; bh=u2kVx37ZdHIy5lrhCn0HpUcWZ68tm2yL24XlL5ZNBOg=; h=DKIM-Signature:From:To:Subject:Date:Message-ID:MIME-Version; b=xms0GZVUxvnar3vbF2LkzHZAyp/DPOFNdCLQCuBZ1tAwHOPu41oMCydgsRryTQ0AY1h1guH6gR2+Xz8/5Yq0KmGNxFBLRhFAQ4tsI1ZjyB2uFs6JbXvWaJeUdauD2Z744TxQavxHsHA97b0OOn1iDwyp/s1fCrlFfXExP7Rnywk= ARC-Authentication-Results: i=1; server2.sourceware.org DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 055CF3858C41 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1753883991; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: in-reply-to:in-reply-to:references:references; bh=gG5bFTFIkqHhBYHktoTp6fbfGc1aLs4uDi5P9VvdCNQ=; b=XxW8ROtxcD5A/MSAIIw24KZG4f7ZzlxEegPJpZ4M5ZigYeutFsaXFzKUWm3GyCtlKvZeM/ Y/JybSiI+jcAX8jvx9HkwXn+oJi7ZmTKOttjbyzitWHN/eLnp/3p0an0zCqtpKQb5AcRdB lpMp4q02gPHJ5blTd4cit0G2HBVjuOg= Received: from mail-wr1-f72.google.com (mail-wr1-f72.google.com [209.85.221.72]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-15-7k8onk02N0i0tkuVizCRvg-1; Wed, 30 Jul 2025 09:59:50 -0400 X-MC-Unique: 7k8onk02N0i0tkuVizCRvg-1 X-Mimecast-MFC-AGG-ID: 7k8onk02N0i0tkuVizCRvg_1753883989 Received: by mail-wr1-f72.google.com with SMTP id ffacd0b85a97d-3a4e713e05bso2421220f8f.3 for ; Wed, 30 Jul 2025 06:59:50 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1753883989; x=1754488789; h=mime-version:message-id:date:references:in-reply-to:subject:cc:to :from:x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=gG5bFTFIkqHhBYHktoTp6fbfGc1aLs4uDi5P9VvdCNQ=; b=OQRQUOkL6o9mNxeInj0SCXYvrx9uiyDHHLqyXz9gSLP/v5sXDqZNo92w+J1k6/I74l ogTAz/pqSDRD5T87uk1ZOxFa2MxCZsP7VaguY3cuV6RBHdlAq2Ugzcw9tNzGVMYe3pI6 DIsAPv7sQ8wtroKQD6KKff4/5P5U0/YDEccRwTs9MhneFApK+Z+JvNcDKPPD9zPt4aeL C/i8iO4PpprniwkkvfJmqBe2M6rlZk1SRMYgYv8vMvgxVblXWYs9krLKrS7O8FziHeEc vIzvbL4D3iKDZcmbIU0ABtsrtHqXFTIs+lHBaT+mRSFPId8AxVz19X5iom1pprzfKbfa /1Xg== X-Forwarded-Encrypted: i=1; AJvYcCVq6GnqayNb/IWH/7YTrPQM1cb6UidhZJFQD5SMXa2/WveGvMHbpfj6oMqvILwTZtT8MBHgCZJWwWeCwA==@sourceware.org X-Gm-Message-State: AOJu0Yxr4icFnKQtsGs2TuaD5bhP2GEX/6RoJladsP075TERT1ICz1g2 Y3EJXH7CzeAQ00MrxzgaN4NIurhBvoiYKuoUQWGahShV4Hu0GcMTbTr4fkxghIEWJXLQiUFaJMo yXxtaLooiyvVA51e/ajxTs0XwcTJ+2ApC6qgcunL13Fb7RZ+sqZmHF5QUfAE5dAs= X-Gm-Gg: ASbGnctWs5EPcMat4PhhalkWJTizW8WvikDwyrEHDQo9sv2KmBBEMqHWFZnWSH9/mkE xV0hGGTvdS3azLuck3uoPnT6GTgzQckPuqTb0QVsHSI8Ip6l1J02fI7AU5QD9UtgD76YsDbZt86 1XlhHcvCLGWk6bZuYZ9KnNdm7J23geL0T4Qv+laJvo/WMg+hTqoPOySOmiiRDQQmzoggUTxAzkm rPsVplcDQjsAK07zfebEDsUfcTSVrS1V/Etgtu2ScKAY/iupJccPTPWw3C05vGoG22HmNgd3dC/ LOpu+rLQ1jBK6cvg5mrnCYjJl2dKIWj+QpqG+VWGlBX6hukwsky5Z7Kc4eVlHA== X-Received: by 2002:a5d:64e5:0:b0:3a4:eda1:6c39 with SMTP id ffacd0b85a97d-3b794fd57f8mr2907528f8f.13.1753883989011; Wed, 30 Jul 2025 06:59:49 -0700 (PDT) X-Google-Smtp-Source: AGHT+IFLBVPo4gufaB0mm92F0dCGqaxmc373w1MJDfENrCQ5Vub3n7kuP1+bKitnLRoHl15EY+tMyg== X-Received: by 2002:a5d:64e5:0:b0:3a4:eda1:6c39 with SMTP id ffacd0b85a97d-3b794fd57f8mr2907500f8f.13.1753883988449; Wed, 30 Jul 2025 06:59:48 -0700 (PDT) Received: from localhost (120.81.93.209.dyn.plus.net. [209.93.81.120]) by smtp.gmail.com with ESMTPSA id ffacd0b85a97d-3b793519f3asm5012448f8f.36.2025.07.30.06.59.47 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 30 Jul 2025 06:59:47 -0700 (PDT) From: Andrew Burgess To: Christina Schimpe , gdb-patches@sourceware.org Cc: thiago.bauermann@linaro.org, luis.machado@arm.com Subject: Re: [PATCH v5 12/12] gdb: Enable displaced stepping with shadow stack on amd64 linux. In-Reply-To: <20250628082810.332526-13-christina.schimpe@intel.com> References: <20250628082810.332526-1-christina.schimpe@intel.com> <20250628082810.332526-13-christina.schimpe@intel.com> Date: Wed, 30 Jul 2025 14:59:47 +0100 Message-ID: <874iutdbjg.fsf@redhat.com> MIME-Version: 1.0 X-Mimecast-Spam-Score: 0 X-Mimecast-MFC-PROC-ID: YPgnIFPGjgY8bMAy5dLGyH0Y-AKub9af7P-INiDN1vA_1753883989 X-Mimecast-Originator: redhat.com Content-Type: text/plain X-BeenThere: gdb-patches@sourceware.org X-Mailman-Version: 2.1.30 Precedence: list List-Id: Gdb-patches mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: gdb-patches-bounces~public-inbox=simark.ca@sourceware.org Christina Schimpe writes: > Currently, if displaced stepping is active and the single stepped instruction > is a call instruction, the return address atop the stack is the address > following the copied instruction. However, to allow normal program execution > it has to be the address following the original instruction. Due to that > reason, the return address is corrected in amd64_displaced_step_fixup and > i386_displaced_step_fixup. > > For programs that are shadow-stack enabled we see a control-protection > exception, as the address on the shadow stack does not match the address > atop the stack. > > Fix this by correcting the shadow stack top address as well. > > Reviewed-By: Luis Machado > Reviewed-By: Eli Zaretskii > --- > gdb/NEWS | 3 + > gdb/amd64-linux-tdep.c | 16 +++- > gdb/amd64-tdep.c | 15 +++ > gdb/doc/gdb.texinfo | 11 ++- > gdb/i386-tdep.c | 15 +++ > .../gdb.arch/amd64-shadow-stack-disp-step.exp | 92 +++++++++++++++++++ > 6 files changed, 149 insertions(+), 3 deletions(-) > create mode 100644 gdb/testsuite/gdb.arch/amd64-shadow-stack-disp-step.exp > > diff --git a/gdb/NEWS b/gdb/NEWS > index ba555f0dea1..60510fefea4 100644 > --- a/gdb/NEWS > +++ b/gdb/NEWS > @@ -3,6 +3,9 @@ > > *** Changes since GDB 16 > > +* Debugging Linux programs that use x86-64 or x86-64 with 32-bit pointer > + size (X32) Shadow Stacks are now supported. > + > * Support for the shadow stack pointer register on x86-64 or x86-64 with > 32-bit pointer size (X32) GNU/Linux. > > diff --git a/gdb/amd64-linux-tdep.c b/gdb/amd64-linux-tdep.c > index 899fe2df02c..782b66f1467 100644 > --- a/gdb/amd64-linux-tdep.c > +++ b/gdb/amd64-linux-tdep.c > @@ -1936,8 +1936,10 @@ amd64_linux_shadow_stack_element_size_aligned (gdbarch *gdbarch) > possible. */ > > static std::optional > -amd64_linux_get_shadow_stack_pointer (gdbarch *gdbarch, regcache *regcache) > +amd64_linux_get_shadow_stack_pointer (gdbarch *gdbarch, regcache *regcache, > + bool &shadow_stack_enabled) > { > + shadow_stack_enabled = false; > const i386_gdbarch_tdep *tdep = gdbarch_tdep (gdbarch); > > if (tdep == nullptr || tdep->ssp_regnum < 0) > @@ -1955,6 +1957,9 @@ amd64_linux_get_shadow_stack_pointer (gdbarch *gdbarch, regcache *regcache) > if (ssp == 0x0) > return {}; > > + /* In case there is a shadow stack pointer available which is non-null, > + the shadow stack feature is enabled. */ > + shadow_stack_enabled = true; > return ssp; > } > > @@ -1965,8 +1970,13 @@ static void > amd64_linux_shadow_stack_push (gdbarch *gdbarch, CORE_ADDR new_addr, > regcache *regcache) > { > + bool shadow_stack_enabled; > std::optional ssp > - = amd64_linux_get_shadow_stack_pointer (gdbarch, regcache); > + = amd64_linux_get_shadow_stack_pointer (gdbarch, regcache, > + shadow_stack_enabled); > + > + /* For amd64/Linux, if SSP has a value that means shadow stack is > + enabled. */ It feels like this statement should be associated with an assert. I'd suggest: if (!ssp.has_value ()) return; else gdb_assert (shadow_stack_enabled); > if (!ssp.has_value ()) > return; > > @@ -2122,6 +2132,8 @@ amd64_linux_init_abi_common(struct gdbarch_info info, struct gdbarch *gdbarch, > (gdbarch, amd64_linux_remove_non_address_bits_watchpoint); > > set_gdbarch_shadow_stack_push (gdbarch, amd64_linux_shadow_stack_push); > + set_gdbarch_get_shadow_stack_pointer (gdbarch, > + amd64_linux_get_shadow_stack_pointer); > dwarf2_frame_set_init_reg (gdbarch, amd64_init_reg); > } > > diff --git a/gdb/amd64-tdep.c b/gdb/amd64-tdep.c > index 450dbc38047..8afb3a7abba 100644 > --- a/gdb/amd64-tdep.c > +++ b/gdb/amd64-tdep.c > @@ -1917,6 +1917,21 @@ amd64_displaced_step_fixup (struct gdbarch *gdbarch, > displaced_debug_printf ("relocated return addr at %s to %s", > paddress (gdbarch, rsp), > paddress (gdbarch, retaddr)); > + > + /* If shadow stack is enabled, we need to correct the return address > + on the shadow stack too. */ > + bool shadow_stack_enabled; > + std::optional ssp > + = gdbarch_get_shadow_stack_pointer (gdbarch, regs, > + shadow_stack_enabled); > + if (ssp.has_value () && shadow_stack_enabled) Given the strengthening of the comment on gdbarch_get_shadow_stack_pointer that I suggest in the previous patch, I think this should become: if (shadow_stack_enabled) { gdb_assert (ssp.has_value ()); ... etc ... } > + { > + write_memory_unsigned_integer (*ssp, retaddr_len, byte_order, > + retaddr); > + displaced_debug_printf ("relocated shadow stack return addr at %s " > + "to %s", paddress (gdbarch, *ssp), > + paddress (gdbarch, retaddr)); > + } > } > } > > diff --git a/gdb/doc/gdb.texinfo b/gdb/doc/gdb.texinfo > index b5120b78426..488816d5ca2 100644 > --- a/gdb/doc/gdb.texinfo > +++ b/gdb/doc/gdb.texinfo > @@ -27059,12 +27059,20 @@ the program stream must be an @code{ENDBR} instruction, otherwise the > processor signals a control protection exception. > @end itemize > > -Impact on Call/Print: > +Impact on GDB commands: > +@itemize @bullet > +@item Call/Print: > Inferior calls in @value{GDBN} reset the current PC to the beginning of the > function that is called. No call instruction is executed, but the @code{RET} > instruction actually is. To avoid a control protection exception due to the > missing return address on the shadow stack, @value{GDBN} pushes the new return > address to the shadow stack and updates the shadow stack pointer. > +@item Step: > +With displaced stepping, @value{GDBN} may run an out of line copy of a call > +instruction. In this case, the wrong return address is pushed to the shadow > +stack. @value{GDBN} corrects this value to avoid a control protection > +exception. For more details on displaced stepping, see @ref{displaced-stepping}. > +@end itemize > > @node Alpha > @subsection Alpha > @@ -41741,6 +41749,7 @@ GLOBAL Disassembler_2 (Matches current architecture) > @cindex out-of-line single-stepping > @item set displaced-stepping > @itemx show displaced-stepping > +@anchor{displaced-stepping} > Control whether or not @value{GDBN} will do @dfn{displaced stepping} > if the target supports it. Displaced stepping is a way to single-step > over breakpoints without removing them from the inferior, by executing > diff --git a/gdb/i386-tdep.c b/gdb/i386-tdep.c > index 8eb5b4fac86..3b05ace2142 100644 > --- a/gdb/i386-tdep.c > +++ b/gdb/i386-tdep.c > @@ -899,6 +899,21 @@ i386_displaced_step_fixup (struct gdbarch *gdbarch, > displaced_debug_printf ("relocated return addr at %s to %s", > paddress (gdbarch, esp), > paddress (gdbarch, retaddr)); > + > + /* If shadow stack is enabled, we need to correct the return address > + on the shadow stack too. */ > + bool shadow_stack_enabled; > + std::optional ssp > + = gdbarch_get_shadow_stack_pointer (gdbarch, regs, > + shadow_stack_enabled); > + if (ssp.has_value () && shadow_stack_enabled) Same comment here as for amd64. > + { > + write_memory_unsigned_integer (*ssp, retaddr_len, byte_order, > + retaddr); > + displaced_debug_printf ("relocated shadow stack return addr at %s " > + "to %s", paddress (gdbarch, *ssp), > + paddress (gdbarch, retaddr)); > + } > } > } > > diff --git a/gdb/testsuite/gdb.arch/amd64-shadow-stack-disp-step.exp b/gdb/testsuite/gdb.arch/amd64-shadow-stack-disp-step.exp > new file mode 100644 > index 00000000000..47bb4df8cfe > --- /dev/null > +++ b/gdb/testsuite/gdb.arch/amd64-shadow-stack-disp-step.exp > @@ -0,0 +1,92 @@ > +# Copyright 2025 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 . > + > +# Test continue from call instructions with shadow stack and displaced > +# stepping being enabled. > + > +require allow_ssp_tests support_displaced_stepping > + > +standard_testfile amd64-shadow-stack.c > + > +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 All of the 'return -1' in the global scope should just be 'return'. The -1 is not checked or used. > + } > + > + # Enable displaced stepping. > + gdb_test_no_output "set displaced-stepping on" > + gdb_test "show displaced-stepping" ".* displaced stepping .* is on.*" > + > + if { ![runto_main] } { > + return -1 > + } > + > + # Get the address of the call1 instruction. I think you mean: # Get the address of the call to the call1 function. > + set call1_addr -1 > + gdb_test_multiple "disassemble main" "" { > + -re -wrap "($hex) <\\+($decimal)>:\\s*call\\s*0x.*.*" { > + set call1_addr $expect_out(1,string) > + pass $gdb_test_name > + } > + } > + > + if { $call1_addr == -1 } { > + return -1 > + } > + > + # Get the address of the call2 instruction. As above: # Get the address of the call to the call2 function. > + set call2_addr -1 > + gdb_test_multiple "disassemble call1" "" { > + -re -wrap "($hex) <\\+($decimal)>:\\s*call\\s*0x.*.*" { > + set call2_addr $expect_out(1,string) > + pass $gdb_test_name > + } > + } > + > + if { $call2_addr == -1 } { > + return -1 > + } > + > + gdb_test "break *$call1_addr" \ > + "Breakpoint $decimal at $hex.*" \ > + "break at the address of the call1 instruction" > + > + gdb_test "break *$call2_addr" \ > + "Breakpoint $decimal at $hex.*" \ > + "break at the address of the call2 instruction" > + > + # Depending on instruction generation we might end up in the call > + # instruction after "runto_main". Only resume until call1 instruction > + # in case the first instruction we're stopped at is not yet the call1 > + # instruction. Could you not just add some filler to the test program to avoid needing to do this? I don't really object (to this) but using filler might be simpler. > + set stop_addr [get_valueof "/x" "\$pc" "" "value of pc after runto_main"] > + if {[eval expr "$stop_addr < $call1_addr"]} { Is the 'eval expr' really needed here? There are plenty of the places in the testsuite where we just write something like: if { $stop_addr < $call1_addr } { ... } so I would have expected that to work. Thanks, Andrew > + gdb_test "continue" \ > + "Breakpoint $decimal, $call1_addr in main ().*" \ > + "continue until call1 instruction" > + } > + gdb_assert {$call1_addr == [get_valueof "/x" "\$pc" ""]} > + > + # Test continue from breakpoint at call1 and call2 instructions. > + gdb_test "continue" \ > + "Breakpoint $decimal, $call2_addr in call1 ().*" \ > + "continue from call1 instruction" > + > + gdb_continue_to_end "continue from call2 instruction" > +} > -- > 2.43.0