From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: smtp.subspace.kernel.org; arc=pass header.oldest-pass=0 smtp.remote-ip=40.107.208.42 ARC-Seal: i=2; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=pass; t=1795991615; b=4WDZqeo34OZVtr+O23gimNHZOS9RXub2SPOnu1GyGBQ0MWPibJTdHeujoROkoy/Z9QiL ViZqpH/cIUrC6OnBCxef4RQq6/oWQy/tubdQdQuexEDvE9OPXmh78ueu6CfF4DyGQUeEG VMxGNEpE6iM8vwHlgJB/e7h5SdZRBW1Im17ylhBzhRn2s1B19MDQ83JIyaHIU7FcLrRQ4 u/yPo2SOFBfY+Y6+gyVkKGmkd4INso9niOZhB4p78TmmdIfyN2pSovHac6CrDKAlnpCkQ edc3QJoyWroQuMvWjTpGgJhPMPDgmCGiutpV8nDrefDtlvKOd9QDmNjOYnBq/bmC1ZQ== ARC-Message-Signature: i=2; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1795991615; h=DMARC-Filter:DKIM-Signature:Received:Received: X-MS-Exchange-Authentication-Results:Received-SPF:Received:Received: From:To:CC:Subject:Date:Message-ID:X-Mailer:In-Reply-To:References: MIME-Version:Content-Transfer-Encoding:Content-Type:X-Originating-IP: X-ClientProxiedBy:X-EOPAttributedMessage:X-MS-PublicTrafficType: X-MS-TrafficTypeDiagnostic:X-MS-Office365-Filtering-Correlation-Id: X-MS-Exchange-SenderADCheck:X-MS-Exchange-AntiSpam-Relay: X-Microsoft-Antispam:X-Microsoft-Antispam-Message-Info: X-Forefront-Antispam-Report: X-MS-Exchange-AntiSpam-MessageData-ChunkCount: X-MS-Exchange-AntiSpam-MessageData-0:X-OriginatorOrg: X-MS-Exchange-CrossTenant-OriginalArrivalTime: X-MS-Exchange-CrossTenant-Network-Message-Id: X-MS-Exchange-CrossTenant-Id: X-MS-Exchange-CrossTenant-OriginalAttributedTenantConnectingIp: X-MS-Exchange-CrossTenant-AuthSource:X-MS-Exchange-CrossTenant-AuthAs: X-MS-Exchange-CrossTenant-FromEntityHeader: X-MS-Exchange-Transport-CrossTenantHeadersStamped; bh=8xbglRtWUz8ZEytXKnyeuQ0NH/w8vkjSU6JtDmXWAoY=; b=wCSx2IVQovIJBO8eo+APgKoNRyUUXujubYS+pfVIQTmevv5wc4XK5FV7dYQxhnuiqR7k 7MllJmEMTGcCB7uUnmfaeatfrbC0s7JoFP9bgGt92+3kb1GLSy1XP4g5DEcTFso7RpGLi coI3yydIs9bdaElkR77+n2zZULBe7/UXTzBv17y1rQKBztPdjBLnawW3QKpp7RTtgXlzx Psu7KUvEB4UfVmFhpD7X7ysJJEHcf0rUBQdm2stzJEmgPJWAHKJIByucvV7w7RlmPbfiO s1VrCeJFMLSNRACNa/dyxeyELdzWwNKYkAlq+3rFmicePdvxtSzQMA8rsbfOsEQeaHA== ARC-Authentication-Results: i=2; smtp.subspace.kernel.org; dmarc=pass header.from=amd.com; spf=fail smtp.mailfrom=amd.com; dkim=pass header.d=amd.com header.i=@amd.com header.a=rsa-sha256 header.s=selector1 header.b="KaS2/gQ0"; arc=pass header.oldest-pass=0 smtp.remote-ip=40.107.208.42 Received: from PH0PR06CU001.outbound.protection.outlook.com (mail-westus3azon11011042.outbound.protection.outlook.com [40.107.208.42]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange ECDHE (secp384r1) server-signature ECDSA (prime256v1) server-digest SHA256) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 58E69606A31C for ; Sun, 29 Nov 2026 22:33:35 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org 58E69606A31C Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=amd.com Authentication-Results: smtp.subspace.kernel.org; spf=fail smtp.mailfrom=amd.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key, unprotected) header.d=amd.com header.i=@amd.com header.a=rsa-sha256 header.s=selector1 header.b=KaS2/gQ0 ARC-Seal: i=1; a=rsa-sha256; s=arcselector10001; d=microsoft.com; cv=none; b=DPQhYs5oTpZdGBu8AwhVaxbCuU4VowcCeejtkwmklCZxhiLaYt0lbBdps6pZhAwV8xt3V6crMFnNf2xYKDUtVQsKhv8mELlDNAF8CI6iiC9gDv+8cV7d92VK1O0iNEgwmnbXUX0PsfynvxBsW4UBrsOjrh/6yEF/+c87EB1NuAomDUjIJySWp4BkBGv2BG5WVh4lpTkl6KRwrwDiiTJ9RjBxqb74otX9ZTyNXIs77yT/GhJkcozsgHzcL5ZKrCbqZklk6aWp3gzNZQ6ZTuyUVCO1AuRhfA/lPQ4rwosTyCFe0MTWTAPWB8A97N6PsHBy4qW/uMctr299K/tlt8cJsQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=microsoft.com; s=arcselector10001; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-AntiSpam-MessageData-ChunkCount:X-MS-Exchange-AntiSpam-MessageData-0:X-MS-Exchange-AntiSpam-MessageData-1; bh=8xbglRtWUz8ZEytXKnyeuQ0NH/w8vkjSU6JtDmXWAoY=; b=body4OLsoEQnd3iF3TxGXi199YCoLzpdR4gdBsoUL6nHb08RJVCNARd6aDldT6Apnln8oArTpNrhJMYJ4XX6VIzMvz+LCMJ7qn6PbHtF77ZzGVJXIRpyMixqzDjVeMVge4Lr9Z0PC1cfYm95K2jqsdbGLaOoSykN2xJJwkYF2tnfJGrTsWyhJDLmWefgJP00WgxWbCAzKMQleAgFD8TlyvekMdQHC7yxZj7CMp39TsfsKQ62DcNgduZvCzoztQmkP4sLBEcxIxCoAjpdJgi6K34PVJ5H0KZQe481xC7ctEjsgi5A2QG9/ukBBR1Y27cfVM6oYz9ljW5MTp/HrFJCFQ== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=pass (sender ip is 165.204.84.17) smtp.rcpttodomain=linux.intel.com smtp.mailfrom=amd.com; dmarc=pass (p=quarantine sp=quarantine pct=100) action=none header.from=amd.com; dkim=none (message not signed); arc=none (0) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=amd.com; s=selector1; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-SenderADCheck; bh=8xbglRtWUz8ZEytXKnyeuQ0NH/w8vkjSU6JtDmXWAoY=; b=KaS2/gQ0ZMFybh6MJI8hHeojQox5R7xJKVfN61WS38wYjMaFL7atViqleiWJ0rTpTkK04YJsDNrE7hG9ejuD+yNK9sBUHXYPFWDgeosvVBNppU1yQDf6RkKWLRyxW8CtC9l8WFyg7lm2Gt+FBvfRYMISdD/CJyjxZp9BsKslqOM= Received: from CH0PR03CA0195.namprd03.prod.outlook.com (2603:10b6:610:e4::20) by CH2PR12MB4261.namprd12.prod.outlook.com (2603:10b6:610:a9::8) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.21.662.12; Sun, 29 Nov 2026 22:33:26 +0000 Received: from CH7PEPF00000016.namprd21.prod.outlook.com (2603:10b6:610:e4:cafe::6c) by CH0PR03CA0195.outlook.office365.com (2603:10b6:610:e4::20) with Microsoft SMTP Server (version=TLS1_3, cipher=TLS_AES_256_GCM_SHA384) id 15.21.662.12 via Frontend Transport; Sun, 29 Nov 2026 22:33:26 +0000 X-MS-Exchange-Authentication-Results: mx.microsoft.com 1; spf=pass (sender IP is 165.204.84.17) smtp.mailfrom=amd.com; dkim=none (message not signed) header.d=none;dmarc=pass action=none header.from=amd.com; Received-SPF: Pass (protection.outlook.com: domain of amd.com designates 165.204.84.17 as permitted sender) receiver=protection.outlook.com; client-ip=165.204.84.17; helo=satlexmb07.amd.com; pr=C Received: from satlexmb07.amd.com (165.204.84.17) by CH7PEPF00000016.mail.protection.outlook.com (10.167.244.165) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.21.709.2 via Frontend Transport; Sun, 29 Nov 2026 22:33:26 +0000 Received: from BLRKPRNAYAK.amd.com (10.180.168.240) by satlexmb07.amd.com (10.181.42.216) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.2.2562.57; Sun, 29 Nov 2026 16:33:22 -0600 From: K Prateek Nayak To: Dave Hansen , Thomas Gleixner , Ingo Molnar , Borislav Petkov , the arch/x86 maintainers , CC: Andy Lutomirski , Peter Zijlstra , Rick Edgecombe , Andrew Cooper , Kees Cook , Pawan Gupta , K Prateek Nayak Subject: [RFC PATCH 11/27] x86/rethook: Prepare the rethook trampoline to handle shadow stack Date: Sun, 29 Nov 2026 22:29:36 +0000 Message-ID: <20261129222953.9124-12-kprateek.nayak@amd.com> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20261129222953.9124-1-kprateek.nayak@amd.com> References: <20261129222953.9124-1-kprateek.nayak@amd.com> X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Content-Type: text/plain X-ClientProxiedBy: satlexmb08.amd.com (10.181.42.217) To satlexmb07.amd.com (10.181.42.216) X-EOPAttributedMessage: 0 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: CH7PEPF00000016:EE_|CH2PR12MB4261:EE_ X-MS-Office365-Filtering-Correlation-Id: 660efa3d-370e-4830-ddda-08df4e68c931 X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0;ARA:13230040|82310400026|23010399003|36860700016|1800799024|7416014|376014|6133799003|32116099003|261009223027099003|22082099003|18002099003|10067099003|56012099006|11063799006|261102123227099003|261026121527099003|261026140227099003|261022075727099003|261022105027099003|261022113327099003|261021121427099003|261028144727099003; X-Microsoft-Antispam-Message-Info: 0mmoHdPPC4SxEddaLmOm8iNB9IM1l9tnjiWd+Lh5T+tkSYlks/B+i5vCkXXSBX4V2cejjF0u87/EsLVwJDmaBu6F3rfjLVFwztIzvS+w9y9qJCPmVpOymc39fhhvVx1nwjuBo0pHJ6EfqM70/RAEmJjruZb3K2M6DpoqkEMyUTE0JgQIBvRXYriKDE6FKHxhOWbO6EVFuTd8vUDVdYPu0LcuCMz1Fr04aQIxjRB21ThpNcip9WixmUdMDoQJzNN4eicJmQ8RdZWaI0F4a/26gKmpLbtCMonyOHmuON33ZRYKGyEmJSZ4RpfD6lyrDrWyRNi/hSOwCRavsSashYFcO5QvGO8okPgcrNPP5dsxL1ZcvEYWkutHejCdpUpnp3EXYTlltYhqiryy9/J5de8xt7cAzKJfuGVUBUH1XwgVr2v485s4SD1/MOp74caNFA9P2qiFMP2jhoDiyYMcA+9VH1+yC1Gm1ZQgxPhfohE/7mzUD2yjn7M58lZfVNjAnP1nXeyAaq4TCLBfqDqeRwtQ12H1RnuFftsDjNp5VtGj0vXFOoMT8V1pFNdA34p29KPz7mG+BeYxcBuwhd/0PJ/Vw0GZOeaK4nkjU7r8MCia1AWQZpV3eRJtyWVnQIF14QX8abcChlLJNzC0cFsIQWPSr+32/yV4BrvlxoHlFhn0kqEn5xwW6dmyBn8tRUGworILnUir32sktIlNaxj5qBsNSA== X-Forefront-Antispam-Report: CIP:165.204.84.17;CTRY:US;LANG:en;SCL:1;SRV:;IPV:NLI;SFV:NSPM;H:satlexmb07.amd.com;PTR:InfoDomainNonexistent;CAT:NONE;SFS:(13230040)(82310400026)(23010399003)(36860700016)(1800799024)(7416014)(376014)(6133799003)(32116099003)(261009223027099003)(22082099003)(18002099003)(10067099003)(56012099006)(11063799006)(261102123227099003)(261026121527099003)(261026140227099003)(261022075727099003)(261022105027099003)(261022113327099003)(261021121427099003)(261028144727099003);DIR:OUT;SFP:1101; X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: +xP1CNsB/IJ5sOfbm3/ZDKvg8b0qIaqgdiFsxGryfC0/IMnHooFLE/8i2m1+0PnzIX6mVVLMPFHyS74RxqZn1K0AHYiDTAFeP+84F0RybpjIxgnMauUZRwvBSEEW0ikv9pSWZV6YWvwXu+poTBa0bZUCXmshaZv3sSTuIUfcoWXXq+MQtCJKCsxEA6ETSk/rdECLQdzuwim47TQAIEI0QncXyb1whPhlAv+46hNQeyfFnpYqk4JjbmGkkDP+9Uqja0OiK/ez6mmpWKxd19Afu7Cf5yY4qtcnuIAD5nAqyj4CeJV4atFMRL2sGU3AyqNwfveORcm2w88K2KPT8diODjZGOZtQPNJwXjuv3fdhUbH9ZfzaYSZL3Sv3ZGSh6UrI/Z2qzVTlAUfRswHU2Ta5qzQcSLBMRtpBAloP5uj7FXpZGF+uvaItn0MBZ97bPpgt X-OriginatorOrg: amd.com X-MS-Exchange-CrossTenant-OriginalArrivalTime: 29 Nov 2026 22:33:26.8648 (UTC) X-MS-Exchange-CrossTenant-Network-Message-Id: 660efa3d-370e-4830-ddda-08df4e68c931 X-MS-Exchange-CrossTenant-Id: 3dd8961f-e488-4e60-8e11-a82d994e183d X-MS-Exchange-CrossTenant-OriginalAttributedTenantConnectingIp: TenantId=3dd8961f-e488-4e60-8e11-a82d994e183d;Ip=[165.204.84.17];Helo=[satlexmb07.amd.com] X-MS-Exchange-CrossTenant-AuthSource: CH7PEPF00000016.namprd21.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Anonymous X-MS-Exchange-CrossTenant-FromEntityHeader: HybridOnPrem X-MS-Exchange-Transport-CrossTenantHeadersStamped: CH2PR12MB4261 Rethook prepare caches the original return IP from the regs->sp state it was presented and insert the trampoline that later returns back to the cached IP. It is possible for multiple handlers to install multiple return trampolines such that the return from one jumps to another, and eventually landing to traced functions' caller's frame. Since return trampoline has hijacked a genuine return, it must balance the call, not only from the RSB perspective but also from shadow stack perspective. Similar to the return_to_handler, populate a dummy slot before triggering the rethook handlers. The rethook handler must populate the correct return IP in the slot using regs_set_return_ip() or expect a #CP for missing redirection. Since the return trampolines (although limited to two) can be chained in weird manner to jump between each other based on the order of pre-handler's running, the tail call technique to populate the return from the ftrace handler turn complicated. Instead, use a retpoline to return into the next trampoline until the execution finds its way back to the correct caller. Signed-off-by: K Prateek Nayak --- arch/x86/kernel/rethook.c | 34 ++++++++++++++++++++++++++++++++++ 1 file changed, 34 insertions(+) diff --git a/arch/x86/kernel/rethook.c b/arch/x86/kernel/rethook.c index 85e2f2d16a90..974ef400b1b3 100644 --- a/arch/x86/kernel/rethook.c +++ b/arch/x86/kernel/rethook.c @@ -6,6 +6,9 @@ #include #include #include +#include + +#include #include "kprobes/common.h" @@ -15,6 +18,35 @@ __visible void arch_rethook_trampoline_callback(struct pt_regs *regs); #define ANNOTATE_NOENDBR #endif +#ifdef CONFIG_X86_KERN_SHADOW_STACK + +/* + * The return to caller of traced function was hijacked by the trampoline and + * the trampoline now corrects the return address. Add a spare slot at SSP that + * the fixup function can use to syncing the return IP on stack from + * arch_rethook_trampoline_callback(). + * + * Clobbers RAX which is dead in both context. + */ +#define RETHOOK_PUSH_SHSTK_FRAME \ + ANNOTATE_INTRA_FUNCTION_CALL "\n" \ + " call 1f\n" \ + " int3\n" \ + "1:\n" \ + " xor %eax, %eax\n" \ + " rdsspq %rax\n" \ + " movq %rax, (%rsp)\n" \ + " pushq $0x300\n" + +#define RETHOOK_POP_SHSTK_FRAME \ + " addq $16, %rsp\n" + +#else /* !CONFIG_X86_KERN_SHADOW_STACK */ + +#define RETHOOK_PUSH_SHSTK_FRAME +#define RETHOOK_POP_SHSTK_FRAME + +#endif /* CONFIG_X86_KERN_SHADOW_STACK */ /* * When a target function returns, this code saves registers and calls * arch_rethook_trampoline_callback(), which calls the rethook handler. @@ -35,7 +67,9 @@ asm( " pushfq\n" SAVE_REGS_STRING " movq %rsp, %rdi\n" + RETHOOK_PUSH_SHSTK_FRAME " call arch_rethook_trampoline_callback\n" + RETHOOK_POP_SHSTK_FRAME RESTORE_REGS_STRING /* In the callback function, 'regs->flags' is copied to 'regs->ss'. */ " addq $16, %rsp\n" -- 2.43.0