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.209.52 ARC-Seal: i=2; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=pass; t=1795991561; b=dwCslv8fO4xmTxEeFLCnGkBWZvBzvCCJf6OcP3fDtW9/2rPwMZiqbw0JK66Ghy6Jz4Ok 3iC75nGx5QuzhXeJV0C2gQiIPzrbAN0qepjKXqqsPq5zJUnMAJb1UCmLtW8VB6frqnlNU ONjmr5yRDNUbC2ABrlVtOjQ7d2Cmnam366k0ZGeY1akl88849oSPnVVZ48za5EwxnzFFy TC2D3DVDSdv4Bnze/3r6TRUHc7XfVa2lq8NIozu5qkh3i/i7qKHlAIJqhIyd0DaDcEXdf HgP9DJL3hYeeeYyrMVt2xPO9anrNiQnswo7phEp4w9ZH74xNJZbpXQCV408rsfQrB3Q== ARC-Message-Signature: i=2; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1795991561; 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=86niB7K0CGDOhSOdWBFWbjCWrNlA7cwQDQ2jICnbhy0=; b=y3bJFBnpFEr9ZCMn6KgeiBjK7KXnc6vtUdYfnPIccb46wA6McNdXp/IiBr7VTnL7X78L Bet5dY4Ew+/CUS3Rw2Sa7aaJSxeeyUaDaADP+qSXGNQvHm/2ILZ6UPBsFD2MmqgCtrh2+ N8JPz3PyueLs0ODeTkbF0sOrbbUL8eZSy2gUxxAG8K2vkjVlvYdccDG73uVEZj+8SE9gN wQpn8cz+2KL1k111+3ohmxXDzaVQjexUcFq3kXNlmCvrlI289GplxDHxrLA/+Zs45QeVp rijAyWS/oUDJW7c8Iuj3MowUWMU98S8YBZOJubXUYTr+6qpghAS6UdpArid54To2BVA== 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=fBNUOrwX; arc=pass header.oldest-pass=0 smtp.remote-ip=40.107.209.52 Received: from PH8PR06CU001.outbound.protection.outlook.com (mail-westus3azon11012052.outbound.protection.outlook.com [40.107.209.52]) (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 00264606A31C for ; Sun, 29 Nov 2026 22:32:40 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org 00264606A31C 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=fBNUOrwX ARC-Seal: i=1; a=rsa-sha256; s=arcselector10001; d=microsoft.com; cv=none; b=mkTmka5ha7PgMqlHtuCroK7fe2gWM0cIAyj04L74MeADcR7lZJnmPN+CYyrhkFe8cDWM/scvnx+4JX9gzRKqhnOwqRuQDSl51WABeLUpUyS3T6JMoxCZ+hHINMz4OUcgeXskNx0Fqp51BBEhWli9W/M004qR/xjAFIEZLvQ0t06NzEwF4bRwFo68WGBwrZNAxwrqYISuhkM9Dw+WjlVkq69pbmftK3LsVn+jVrqz/35dXUwJzIuu1RNthfQB65z+f7yI//bCk3Gf9Ja33/kf29mcMuVa199YNrJ48p0TuQ43tRj44ylNkCNyszADob8HZcJJIY6Xv2j3gQvVr7VXZQ== 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=86niB7K0CGDOhSOdWBFWbjCWrNlA7cwQDQ2jICnbhy0=; b=sOhMgWiWnUN4vlOMAsCf/tGUG3v2m4M13xfUBMG4vTB12JCKsc7cBJ6FuoWxfr7jPNXSgZrrjES7YhtvYpUY035tDO5XQC+1Mr4JFTGbGExtd9LwslMa91F5S6E3llAoEL4t+c/niWP9neiHegBocZu2qBVghPbx8LEQf1AR21NM0E3Y25l3Xzle0Ro7EYToe1d5b/EjpmipoSLwOyJoo5lPGB3zk+WWSknifo8gFTU5eqNYhxrEu76kMiUgLSk9IJ7gmD3qMMl4c6zBxQ4jZcUaKd0gMbd0rwfGTUog8o6daEPo8pgav13hTZyfLAAd4+vGiPmsSJWrwdVzgi0wcQ== 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=86niB7K0CGDOhSOdWBFWbjCWrNlA7cwQDQ2jICnbhy0=; b=fBNUOrwXyos420vRC5dR2ptISO+CsQpwTdRrqI8YtIE5V9/pqTP5ohmCauXbK0oNWrZAV1HSmXR8PAs7lG6hE/DtZ3jI+LwTzY3J2n+5EYC5nTt2E6kx6L6LPmq+0/SWBT+FKbPkdZoYnq/Cg4kd6al5VShSUqUuA80+/N0UT0E= Received: from DS7PR03CA0356.namprd03.prod.outlook.com (2603:10b6:8:55::22) by LV0PR12MB764842.namprd12.prod.outlook.com (2603:10b6:408:3b6::17) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.21.662.11; Sun, 29 Nov 2026 22:32:35 +0000 Received: from CH7PEPF00000019.namprd21.prod.outlook.com (2603:10b6:8:55:cafe::83) by DS7PR03CA0356.outlook.office365.com (2603:10b6:8:55::22) 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:32:35 +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 CH7PEPF00000019.mail.protection.outlook.com (10.167.244.168) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.21.639.0 via Frontend Transport; Sun, 29 Nov 2026 22:32:34 +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:32:29 -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 08/27] x86/retpoline: Handle retpolies and thunks with kernel shadow stack Date: Sun, 29 Nov 2026 22:29:33 +0000 Message-ID: <20261129222953.9124-9-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: CH7PEPF00000019:EE_|LV0PR12MB764842:EE_ X-MS-Office365-Filtering-Correlation-Id: 29f66a44-ffd1-419f-db46-08df4e68aa10 X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0;ARA:13230040|36860700016|23010399003|1800799024|82310400026|376014|7416014|10067099003|56012099006|261028144727099003|261026121527099003|261026140227099003|261021123027099003|261021121427099003|261102123227099003|261022105027099003|261102130727099003|261009223027099003|18002099003|22082099003|11063799006|6133799003|3023799007; X-Microsoft-Antispam-Message-Info: VA3yoUAdD6aGAY6zr4QFhhwDp/AWBqjNkOUDW9KZ2kExRHDd9eP30r4RQqAOJbonCKaRQePKkT4TiybIJQP3l2XVjPxGEo7ChPnUGlPsqHxt6GxQqSgdO7HSG8F0OLYe6ESuP+WXgWAyINSOBDangfk/gVTd7ccdmbY2JbbOLC9URxacEh1RtA1qUmJ0HsmfEx2xZ0UxpJ+AtJLD2TatK5YdlY0N6LJ8fcbQjwWJppWoGvcXQFIFpwbkbPB9gcMlC9CV/Ic39uVI86OVKSrIG9vhnhn2mRA2UU3TgVLCXe7aNjUCU8VCVbZ64n0kBRcWVJGIfxTJxaTZVkFCqvXvCu890HLGq8NM1c++uV5QXqZXVY1vxgHoUfGv9YXwY8O3NzzwGyqI+BgmZPTHKBqLevlz92epzORGbca2rxxWvXA3gANpRS05UeBarjtUbOKoBGNBFyKSyz2D4PYahwT4BZiDagIJ5nbxrakgRLMqh3vRfz3DZgM00MrOgBluCNhMvZiaKf/rwISJTuvDHnfzwd5+aFC+d4Gkmm4y+NQnxTS73fQNjwwihMRdEIuvJDsdkaw2VejnzTfMiCBSSwuSVBx8G/qkjrVQFGROFkbLwmBgQ+LLZI386bxlGQk/eblbVf6+JJ7OrwN9hiPqSmIBZY2+rE/x2Yvnm7pPdCe54oss+vEJ+NIpBZenQI6Nx47/6oDtKJzJpJiwWO/4KJic3Q== 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)(36860700016)(23010399003)(1800799024)(82310400026)(376014)(7416014)(10067099003)(56012099006)(261028144727099003)(261026121527099003)(261026140227099003)(261021123027099003)(261021121427099003)(261102123227099003)(261022105027099003)(261102130727099003)(261009223027099003)(18002099003)(22082099003)(11063799006)(6133799003)(3023799007);DIR:OUT;SFP:1101; X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: CCsqMpXJ8owBO1XRSurMFX6I9+Jq7rdOi1M2oHfT2/Do41oPO8Lp6Fj/LCo8l3r1SgSMxdDPPxyHm/yo+QscrsevuBlkpJx6yMejZDN53wSdJZoajnXDQf/oU3+8o9VXgPhJFT8UyYMgyPQ6v+Dl8wf886AUpI4hd1uXOS4qw3hpxtK/dYlLIPmLYhE8I7XolYvTVTTy/xFSwz7oDafecEdZfJulJOLlHa0c5zXkR1BukTQW+2WEfJfbBTqu8QgSdVMFJeToBxgWHA3UnHwAVaI3m/zPMTEqZs5j4V6aTMJVzK/ywU5VsWnn30ARIU11AhTYtg4/cNfdEFlCovNe6i7yB5ITgOzh44YGV66kZBmhSYh9gFzLzUMgoZvr/AgdIKCQ776WTiIzppt+3LSwSTeynin5JVin5BeegF9DtwTmuyzhKgrMv0FGiKzzxD/3 X-OriginatorOrg: amd.com X-MS-Exchange-CrossTenant-OriginalArrivalTime: 29 Nov 2026 22:32:34.6134 (UTC) X-MS-Exchange-CrossTenant-Network-Message-Id: 29f66a44-ffd1-419f-db46-08df4e68aa10 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: CH7PEPF00000019.namprd21.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Anonymous X-MS-Exchange-CrossTenant-FromEntityHeader: HybridOnPrem X-MS-Exchange-Transport-CrossTenantHeadersStamped: LV0PR12MB764842 Retpolines need to correct the return address on the shadow stack to match that of the indirect call target such that the return doesn't lead to a #CP. Direct jump to an out of line trampoline that handles shadow stack and does the final RET which allows keeping the retpoline thunk size below 32-bytes. SRSO adds a CALL, LEA, RET pattern which needs to pop an intermediate shadow stack slot since the correct return address exists right below. The handle_interrupted_saferet() handling with shadow stack is hard and there are no processors that need Safe-RET sequence to mitigate SRSO and support FRED (touch wood!). Make any CPUs that toggle Safe-RET with X86_FEATURE_KERN_SHSTK call an alternalte Safe-RET thunk that handle_interrupted_saferet() is unaware of. If FRED enabled systems ever need Safe-RET (hopefully never), handle_interrupted_saferet() has to spot if the control is past incsspq or not (perhaps using another global symbol): o If control is past incsspq, emulate a POP + RET like normal. o If incsspq has not yet executed, use the FRED shadow stack frame's SSP to redirect the SSP at return to one slot above (similar to how int3_emulate_ret() does) in addition to handling the rest of the LEA + RET sequence. Call depth thunk only needs to pop 16 entries when the RSB is stuffed which is also copied for the RSB_FILL_LOOP. Retpolines cannot clobber registers and need to handle it on stack :-( Saferet sequences are longer :-( Survives a spectre_v2=retpolines,generic :-) Signed-off-by: K Prateek Nayak --- arch/x86/Kconfig | 12 ---- arch/x86/include/asm/alternative.h | 3 +- arch/x86/include/asm/nospec-branch.h | 27 +++++++- arch/x86/lib/retpoline.S | 92 +++++++++++++++++++++++++++- 4 files changed, 116 insertions(+), 18 deletions(-) diff --git a/arch/x86/Kconfig b/arch/x86/Kconfig index a5bad1fd3010..559b9bc95c38 100644 --- a/arch/x86/Kconfig +++ b/arch/x86/Kconfig @@ -1907,18 +1907,6 @@ config X86_KERN_SHADOW_STACK depends on VMAP_STACK depends on AS_WRUSS select X86_CET - # The retpoline sequence is "call; mov %reg,(%rsp); ret": the - # CALL pushes a shadow stack entry and the RET then uses a - # rewritten return address. Mismatch every time. - depends on !MITIGATION_RETPOLINE - # The indirect call thunks are retpoline-based (see above) and - # call_depth_return_thunk() refills the RSB with 16 CALLs, then - # does "add $128,%rsp; ret", leaving 16 stale shadow stack entries. - depends on !MITIGATION_CALL_DEPTH_TRACKING - # Safe-RET is "call srso_safe_ret" followed by - # "lea 8(%rsp),%rsp; ret": the RET consumes the caller's return - # address while the shadow stack top holds the CALL's. - depends on !MITIGATION_SRSO # Replaces return addresses on the stack with return_to_handler, # and return_to_handler itself returns with a retpoline-style # "call; mov %rdi,(%rsp); ret". diff --git a/arch/x86/include/asm/alternative.h b/arch/x86/include/asm/alternative.h index 08af86ef090a..83ee5527ed4a 100644 --- a/arch/x86/include/asm/alternative.h +++ b/arch/x86/include/asm/alternative.h @@ -329,7 +329,8 @@ void nop_func(void); .endm .macro ALTERNATIVE_3 oldinstr, newinstr1, ft_flags1, newinstr2, ft_flags2, newinstr3, ft_flags3 - __ALTERNATIVE(ALTERNATIVE_2(\oldinstr, \newinstr1, \ft_flags1, \newinstr2, \ft_flags2), + __ALTERNATIVE(__ALTERNATIVE(__ALTERNATIVE(\oldinstr, \newinstr1, \ft_flags1), + \newinstr2, \ft_flags2), \newinstr3, \ft_flags3) .endm diff --git a/arch/x86/include/asm/nospec-branch.h b/arch/x86/include/asm/nospec-branch.h index 1abeb9af000b..0872dceb3819 100644 --- a/arch/x86/include/asm/nospec-branch.h +++ b/arch/x86/include/asm/nospec-branch.h @@ -121,6 +121,7 @@ */ #define RETPOLINE_THUNK_SIZE 32 + #define RSB_CLEAR_LOOPS 32 /* To forcibly overwrite all entries */ /* @@ -132,6 +133,24 @@ int3; \ 772: +#ifdef CONFIG_X86_KERN_SHADOW_STACK + +/* INCSSP #UDs when the supervisor shadow stack is not enabled. */ +#define __FIXUP_SHSTK_SLOTS(reg, nr) \ + xor reg, reg; \ + rdsspq reg; \ + test reg, reg; \ + jz 773f; \ + mov $nr, reg; \ + incsspq reg; \ +773: + +#else /* !CONFIG_X86_KERN_SHADOW_STACK */ + +#define __FIXUP_SHSTK_SLOTS(reg, nr) + +#endif /* CONFIG_X86_KERN_SHADOW_STACK */ + /* * Stuff the entire RSB. * @@ -149,6 +168,7 @@ dec reg; \ jnz 771b; \ /* barrier for jnz misprediction */ \ + __FIXUP_SHSTK_SLOTS(reg, nr); \ lfence; \ CREDIT_CALL_DEPTH \ CALL_THUNKS_DEBUG_INC_CTXSW @@ -173,10 +193,11 @@ * On PBRSB-vulnerable CPUs, it is not safe for a RET to be executed * before this point. */ -#define __FILL_ONE_RETURN \ +#define __FILL_ONE_RETURN(reg) \ __FILL_RETURN_SLOT \ add $(BITS_PER_LONG/8), %_ASM_SP; \ - lfence; + lfence; \ + __FIXUP_SHSTK_SLOTS(reg, 1); /* * Helper for detecting if an interrupt occurred at an unsafe location within @@ -289,7 +310,7 @@ .macro FILL_RETURN_BUFFER reg:req nr:req ftr:req ftr2=ALT_NOT(X86_FEATURE_ALWAYS) ALTERNATIVE_2 "jmp .Lskip_rsb_\@", \ __stringify(__FILL_RETURN_BUFFER(\reg,\nr)), \ftr, \ - __stringify(nop;nop;__FILL_ONE_RETURN), \ftr2 + __stringify(nop;nop;__FILL_ONE_RETURN(\reg)), \ftr2 .Lskip_rsb_\@: .endm diff --git a/arch/x86/lib/retpoline.S b/arch/x86/lib/retpoline.S index f9ca1d8eabca..d4a8c2b434a1 100644 --- a/arch/x86/lib/retpoline.S +++ b/arch/x86/lib/retpoline.S @@ -29,6 +29,37 @@ RET .endm +#ifdef CONFIG_X86_KERN_SHADOW_STACK + +SYM_CODE_START(__x86_shstk_poline_ret) + UNWIND_HINT_FUNC + ANNOTATE_NOENDBR + push %rax + xorl %eax, %eax + rdsspq %rax + testq %rax, %rax + jz 1f + push %rcx + movq 16(%rsp), %rcx + wrssq %rcx, (%rax) + pop %rcx +1: pop %rax + RET +SYM_CODE_END(__x86_shstk_poline_ret) + +.macro RETPOLINE_SHSTK reg + POLINE \reg + jmp __x86_shstk_poline_ret +.endm + +#else /* CONFIG_X86_KERN_SHADOW_STACK */ + +.macro RETPOLINE_SHSTK reg + RETPOLINE \reg +.endm + +#endif + .macro THUNK reg .align RETPOLINE_THUNK_SIZE @@ -36,9 +67,11 @@ SYM_INNER_LABEL(__x86_indirect_thunk_\reg, SYM_L_GLOBAL) UNWIND_HINT_UNDEFINED ANNOTATE_NOENDBR - ALTERNATIVE_2 __stringify(RETPOLINE \reg), \ + ALTERNATIVE_3 __stringify(RETPOLINE \reg), \ __stringify(lfence; ANNOTATE_RETPOLINE_SAFE; jmp *%\reg; int3), X86_FEATURE_RETPOLINE_LFENCE, \ + __stringify(RETPOLINE_SHSTK \reg), X86_FEATURE_KERN_SHSTK, \ __stringify(ANNOTATE_RETPOLINE_SAFE; jmp *%\reg), ALT_NOT(X86_FEATURE_RETPOLINE) + SYM_PIC_ALIAS(__x86_indirect_thunk_\reg) .endm @@ -228,10 +261,50 @@ SYM_CODE_START_NOALIGN(srso_alias_safe_ret) int3 SYM_FUNC_END(srso_alias_safe_ret) +/* + * Safe-RET sequence system with X86_FEATURE_KERN_SHSTK. + * + * This does not need to adhere to constraints of + * handle_interrupted_saferet() since none of the FRED enabled systems + * need Safe-RET mitigation. It only exists as a precaution for systems + * that enable kernel shadow stack and somehow lands here. + * + * If this ever needs a handle_interrupted_saferet() like handling, + * check if incsspq has been handled or not, and if it hasn't been + * handled, use the FRED event frame to pop the shadow stack by + * redirecting the SSP on top of the frame and continue as usual. + */ +SYM_CODE_START_NOALIGN(srso_alias_safe_ret_shstk) +#ifdef CONFIG_X86_KERN_SHADOW_STACK + movq %rcx, (%_ASM_SP) + movq $0, %rcx + rdsspq %rcx + jrcxz 1f + movl $1, %ecx + incsspq %rcx +1: + movq (%_ASM_SP), %rcx +#endif + lea 8(%_ASM_SP), %_ASM_SP + UNWIND_HINT_FUNC + ANNOTATE_UNRET_SAFE + ret + /* End of Safe-RET sequence */ + int3 +SYM_FUNC_END(srso_alias_safe_ret_shstk) + SYM_CODE_START_NOALIGN(srso_alias_return_thunk) UNWIND_HINT_FUNC ANNOTATE_NOENDBR - call srso_alias_safe_ret + + /* + * Switch to kernel shadow stack handling before + * it is toggled on. + */ + ALTERNATIVE "call srso_alias_safe_ret_shstk", \ + "call srso_alias_safe_ret", \ + ALT_NOT(X86_FEATURE_KERN_SHSTK) + ud2 SYM_CODE_END(srso_alias_return_thunk) .popsection @@ -394,6 +467,20 @@ __EXPORT_THUNK(entry_untrain_ret) #ifdef CONFIG_MITIGATION_CALL_DEPTH_TRACKING +.macro FIXUP_CALL_DEPTH_SHSTK +#ifdef CONFIG_X86_KERN_SHADOW_STACK + push %rax + xor %eax, %eax + rdsspq %rax + test %rax, %rax + jz .Lshstk_skip\@ + movl $16, %eax + incsspq %rax +.Lshstk_skip\@: + pop %rax +#endif +.endm + .align 64 SYM_FUNC_START(call_depth_return_thunk) ANNOTATE_NOENDBR @@ -417,6 +504,7 @@ SYM_FUNC_START(call_depth_return_thunk) .endr add $(8*16), %rsp + FIXUP_CALL_DEPTH_SHSTK CREDIT_CALL_DEPTH ANNOTATE_UNRET_SAFE -- 2.43.0