From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from simark.ca by simark.ca with LMTP id qFTmHO/OZWcXaSUAWB0awg (envelope-from ) for ; Fri, 20 Dec 2024 15:09:19 -0500 Authentication-Results: simark.ca; dkim=pass (2048-bit key; unprotected) header.d=intel.com header.i=@intel.com header.a=rsa-sha256 header.s=Intel header.b=DMqanGkg; dkim-atps=neutral Received: by simark.ca (Postfix, from userid 112) id 711D31E097; Fri, 20 Dec 2024 15:09:19 -0500 (EST) X-Spam-Checker-Version: SpamAssassin 4.0.0 (2022-12-13) on simark.ca X-Spam-Level: X-Spam-Status: No, score=-6.4 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 autolearn=unavailable autolearn_force=no version=4.0.0 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 B66931E091 for ; Fri, 20 Dec 2024 15:09:18 -0500 (EST) Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 4DE3B3858C39 for ; Fri, 20 Dec 2024 20:09:18 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 4DE3B3858C39 Authentication-Results: sourceware.org; dkim=pass (2048-bit key, unprotected) header.d=intel.com header.i=@intel.com header.a=rsa-sha256 header.s=Intel header.b=DMqanGkg Received: from mgamail.intel.com (mgamail.intel.com [198.175.65.10]) by sourceware.org (Postfix) with ESMTPS id BED613858C3A for ; Fri, 20 Dec 2024 20:06:27 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org BED613858C3A Authentication-Results: sourceware.org; dmarc=pass (p=none dis=none) header.from=intel.com Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=intel.com ARC-Filter: OpenARC Filter v1.0.0 sourceware.org BED613858C3A Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=198.175.65.10 ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1734725188; cv=none; b=YINuiVcnZsswDpIWD+HCL5VGMflIRTQPleDrPH4+MIkb3glA3t9DrG9+k23tgQ8b7SlRUxKMavfxjrH33/M9dlWNsiNQbB9NSMM1O/LXi7Gcw5X/iw0FsuparlQykNOBuh3N/zBSBiVHiC0yzwVILi+E9empBVOIc1yRiwFda48= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1734725188; c=relaxed/simple; bh=zhUsPmvC8tq3P+b+e326FO+bP/m6DCbNrV7XFbUC5fg=; h=DKIM-Signature:From:To:Subject:Date:Message-Id:MIME-Version; b=YNhbNLgXJuOi8IvEwLY29qkqnijwvbGrU7Xrk62yOPw3yfTnmK1or0rQHSZ0xuG61WI0+53V6zcwE8Q9GVDfvWldVls2Xhk/+bchNCWKcU+TKNZ+vwnzCFyOUIlqfNWNm0qe/f5DOq7jbhwDA7/gAks4SYGipUXVrV+rcth4BJE= ARC-Authentication-Results: i=1; server2.sourceware.org DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org BED613858C3A DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1734725188; x=1766261188; h=from:to:subject:date:message-id:in-reply-to:references: mime-version:content-transfer-encoding; bh=zhUsPmvC8tq3P+b+e326FO+bP/m6DCbNrV7XFbUC5fg=; b=DMqanGkga8G5IxGS9Y4qVhQ9D9vXuo4fDK4PSpwVeWNVuOg+mr6HFDyS 3Eef7WOdpVdHloM2tRl04bpc3tk4Gf2Fv8mQaZr+aPcZvaRpBaor9fP3+ ZWZsevAcIVdGUglFH0gnTFsK7oEIHKVND569uMH4NB+4A/GUDTI086fEK WVmcgvLqsmWWL6YnBgLYMwo3TLHvX9WK/EhPxmuEmLDg4qfwtYwAUj58y 9BNaFV2ayLGCAZlQfMRw5RAn/ENraDYizj9p90USRgi23orjdooRfjbb1 +/wDhBmnWd7Z4ovyVtUEZYgcg2UPhL6MbzN/2VObAZjuMLhdMEAGMz4We A==; X-CSE-ConnectionGUID: Yt7kdoq2R16ENZ2HJ+uMxA== X-CSE-MsgGUID: vrDbgZXERdCD+R4cFe2v8g== X-IronPort-AV: E=McAfee;i="6700,10204,11292"; a="52689153" X-IronPort-AV: E=Sophos;i="6.12,251,1728975600"; d="scan'208";a="52689153" Received: from fmviesa008.fm.intel.com ([10.60.135.148]) by orvoesa102.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 20 Dec 2024 12:06:27 -0800 X-CSE-ConnectionGUID: j2Qq+njKQUi4MuoPlPEr0Q== X-CSE-MsgGUID: TS3CNO48Th67vJDywFyS5A== X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="6.12,251,1728975600"; d="scan'208";a="98809961" Received: from gkldtt-dev-004.igk.intel.com (HELO localhost) ([10.123.221.202]) by fmviesa008-auth.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 20 Dec 2024 12:06:26 -0800 From: "Schimpe, Christina" To: gdb-patches@sourceware.org Subject: [PATCH 09/12] gdb, gdbarch: Enable inferior calls for shadow stack support. Date: Fri, 20 Dec 2024 20:04:58 +0000 Message-Id: <20241220200501.324191-10-christina.schimpe@intel.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20241220200501.324191-1-christina.schimpe@intel.com> References: <20241220200501.324191-1-christina.schimpe@intel.com> MIME-Version: 1.0 Content-Type: text/plain; charset="us-ascii" Content-Transfer-Encoding: 7bit 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 Inferior calls in GDB reset the current PC to the beginning of the function that is called. As no call instruction is executed the new return address needs to be pushed to the shadow stack and the shadow stack pointer needs to be updated. This commit adds a new gdbarch method to push an address on the shadow stack. The method is used to adapt the function 'call_function_by_hand_dummy' for inferior call shadow stack support. --- gdb/gdbarch-gen.c | 32 ++++++++++++++++++++++++++++++++ gdb/gdbarch-gen.h | 14 ++++++++++++++ gdb/gdbarch_components.py | 16 ++++++++++++++++ gdb/infcall.c | 6 ++++++ 4 files changed, 68 insertions(+) diff --git a/gdb/gdbarch-gen.c b/gdb/gdbarch-gen.c index d05c7a3cbdf..c33c476dfdb 100644 --- a/gdb/gdbarch-gen.c +++ b/gdb/gdbarch-gen.c @@ -260,6 +260,7 @@ struct gdbarch gdbarch_get_pc_address_flags_ftype *get_pc_address_flags = default_get_pc_address_flags; gdbarch_read_core_file_mappings_ftype *read_core_file_mappings = default_read_core_file_mappings; gdbarch_use_target_description_from_corefile_notes_ftype *use_target_description_from_corefile_notes = default_use_target_description_from_corefile_notes; + gdbarch_shadow_stack_push_ftype *shadow_stack_push = nullptr; }; /* Create a new ``struct gdbarch'' based on information provided by @@ -531,6 +532,7 @@ verify_gdbarch (struct gdbarch *gdbarch) /* Skip verify of get_pc_address_flags, invalid_p == 0. */ /* Skip verify of read_core_file_mappings, invalid_p == 0. */ /* Skip verify of use_target_description_from_corefile_notes, invalid_p == 0. */ + /* Skip verify of shadow_stack_push, has predicate. */ if (!log.empty ()) internal_error (_("verify_gdbarch: the following are invalid ...%s"), log.c_str ()); @@ -1396,6 +1398,12 @@ gdbarch_dump (struct gdbarch *gdbarch, struct ui_file *file) gdb_printf (file, "gdbarch_dump: use_target_description_from_corefile_notes = <%s>\n", host_address_to_string (gdbarch->use_target_description_from_corefile_notes)); + gdb_printf (file, + "gdbarch_dump: gdbarch_shadow_stack_push_p() = %d\n", + gdbarch_shadow_stack_push_p (gdbarch)); + gdb_printf (file, + "gdbarch_dump: shadow_stack_push = <%s>\n", + host_address_to_string (gdbarch->shadow_stack_push)); if (gdbarch->dump_tdep != NULL) gdbarch->dump_tdep (gdbarch, file); } @@ -5507,3 +5515,27 @@ set_gdbarch_use_target_description_from_corefile_notes (struct gdbarch *gdbarch, { gdbarch->use_target_description_from_corefile_notes = use_target_description_from_corefile_notes; } + +bool +gdbarch_shadow_stack_push_p (struct gdbarch *gdbarch) +{ + gdb_assert (gdbarch != NULL); + return gdbarch->shadow_stack_push != NULL; +} + +void +gdbarch_shadow_stack_push (struct gdbarch *gdbarch, CORE_ADDR new_addr) +{ + gdb_assert (gdbarch != NULL); + gdb_assert (gdbarch->shadow_stack_push != NULL); + if (gdbarch_debug >= 2) + gdb_printf (gdb_stdlog, "gdbarch_shadow_stack_push called\n"); + gdbarch->shadow_stack_push (gdbarch, new_addr); +} + +void +set_gdbarch_shadow_stack_push (struct gdbarch *gdbarch, + gdbarch_shadow_stack_push_ftype shadow_stack_push) +{ + gdbarch->shadow_stack_push = shadow_stack_push; +} diff --git a/gdb/gdbarch-gen.h b/gdb/gdbarch-gen.h index 9fda85f860f..ff14a3666b9 100644 --- a/gdb/gdbarch-gen.h +++ b/gdb/gdbarch-gen.h @@ -1778,3 +1778,17 @@ extern void set_gdbarch_read_core_file_mappings (struct gdbarch *gdbarch, gdbarc typedef bool (gdbarch_use_target_description_from_corefile_notes_ftype) (struct gdbarch *gdbarch, struct bfd *corefile_bfd); extern bool gdbarch_use_target_description_from_corefile_notes (struct gdbarch *gdbarch, struct bfd *corefile_bfd); extern void set_gdbarch_use_target_description_from_corefile_notes (struct gdbarch *gdbarch, gdbarch_use_target_description_from_corefile_notes_ftype *use_target_description_from_corefile_notes); + +/* Some targets support special hardware-assisted control-flow protection + technologies. For example, Intel's Control-flow Enforcement Technology (CET) + provides a shadow stack and indirect branch tracking. + To enable inferior calls the function shadow_stack_push has to be provided. + + Push the address NEW_ADDR on the shadow stack and update the shadow stack + pointer. */ + +extern bool gdbarch_shadow_stack_push_p (struct gdbarch *gdbarch); + +typedef void (gdbarch_shadow_stack_push_ftype) (struct gdbarch *gdbarch, CORE_ADDR new_addr); +extern void gdbarch_shadow_stack_push (struct gdbarch *gdbarch, CORE_ADDR new_addr); +extern void set_gdbarch_shadow_stack_push (struct gdbarch *gdbarch, gdbarch_shadow_stack_push_ftype *shadow_stack_push); diff --git a/gdb/gdbarch_components.py b/gdb/gdbarch_components.py index cc7c6d8677b..52f265e8e0e 100644 --- a/gdb/gdbarch_components.py +++ b/gdb/gdbarch_components.py @@ -2815,3 +2815,19 @@ The corefile's bfd is passed through COREFILE_BFD. predefault="default_use_target_description_from_corefile_notes", invalid=False, ) + +Method( + comment=""" +Some targets support special hardware-assisted control-flow protection +technologies. For example, Intel's Control-flow Enforcement Technology (CET) +provides a shadow stack and indirect branch tracking. +To enable inferior calls the function shadow_stack_push has to be provided. + +Push the address NEW_ADDR on the shadow stack and update the shadow stack +pointer. +""", + type="void", + name="shadow_stack_push", + params=[("CORE_ADDR", "new_addr")], + predicate=True, +) diff --git a/gdb/infcall.c b/gdb/infcall.c index 6399278c6ae..3a4f1e35a2f 100644 --- a/gdb/infcall.c +++ b/gdb/infcall.c @@ -1453,6 +1453,12 @@ call_function_by_hand_dummy (struct value *function, bp_addr, args.size (), args.data (), sp, return_method, struct_addr); + /* Push the return address of the inferior (bp_addr) on the shadow stack + and update the shadow stack pointer. As we don't execute a call + instruction to start the inferior we need to handle this manually. */ + if (gdbarch_shadow_stack_push_p (gdbarch)) + gdbarch_shadow_stack_push (gdbarch, bp_addr); + /* Set up a frame ID for the dummy frame so we can pass it to set_momentary_breakpoint. We need to give the breakpoint a frame ID so that the breakpoint code can correctly re-identify the -- 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