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.93.201.37 ARC-Seal: i=2; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=pass; t=1795991548; b=p3tIyuJA2f2jF4auWJogjlJrb4LmfxdzzmU2VHfLMYX7jqZru7jOdcOgN51IFQkH3Xvk 8wamWJLAIMs6I4pJ14wxR3zuG2o8AXMGVX1r4tn+4nTx7adumoZQNcWxVe32Km2lGy2R/ Iqi2i+PPD4Mbtr5Fs/Ebip3qKxhqj/jCDCYPXRAT/2eCoGD4nVAl4k4hlR1lMcm05LiYg SqeBzyp0s8Ni6nne1NZCwz1U/mjwI25556EJdbktDLwqd4xaYp13WOIvNWuVKCenD4FST cmKglep/bxbJ6Og2EfkKXoLV871wBxnqjR3jxdrU62RPVYxbIE1JFmsRBHSWKfcY3iw== ARC-Message-Signature: i=2; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1795991548; 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=iyTgTV1EjMmYIQspIdvdkoKETqfyEgNnFbVCrj9zGCE=; b=q31YCWHqGS0kTjH2QqbJq3DFVoVyw+EevG55qpamcyYghbXq1HJ7LTK3BQcqr7ZP/je5 5ACNGhbdasYgEKpHygjbHM/Kylw7k+x4lwlKTOxmP3KMfRddMgggUbFcBCx8gPxQ8KxDE 4gr+BZydSh4xNegGWFqEhmpXyQmYB1jesLUXQi0lLYnKdVO3200E/vTayiWpIK+Rtejyp WeYK/k/2Z2iTyDDfgx0nYnhlsJZX7tF+AdaM1H7ZtZpy6g7YcnnGgRVQI9aqpN1xRu//j MJj4yyzFHs5YVrmSJLf3BV7Kqd3bN0i/4cygyILddxeKcs4wEzTyR+XVFdPCGP87eZA== 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=3JYNRo3S; arc=pass header.oldest-pass=0 smtp.remote-ip=40.93.201.37 Received: from CY3PR05CU001.outbound.protection.outlook.com (mail-westcentralusazon11013037.outbound.protection.outlook.com [40.93.201.37]) (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 E3F12606A31C for ; Sun, 29 Nov 2026 22:32:27 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org E3F12606A31C 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=3JYNRo3S ARC-Seal: i=1; a=rsa-sha256; s=arcselector10001; d=microsoft.com; cv=none; b=PfVqunf0vJv36Y/tow9ym5aFN2JuME5fU4gT/fmXwaudpFH7RzrtZYbiR5uNoeA+T/LsFF+wLfC5IyMIGkcWwQr9r50bY//2XBhJbkO5LfvzXZXMhnjpLDcS1OZT+LqklT0UpC57GnHLaa/10amZmjxiqQ2v1BDgCfkrVytbY1gb8GIOqhPo9RE14GrSSofIghNkRarJX06Y1GCiSp/MdbZGmfIRLp4oMR+FXCUsvlMUzTII7BPWmbJUe1WdhS2XO/2y1WJYDWHD3DwasV96RFq4Zxwg3nb2hHNdkjvQyCM5tOgJrv4Y8L9h2dqLxIX1ppqvSjP2nnkUz2amLffGBA== 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=iyTgTV1EjMmYIQspIdvdkoKETqfyEgNnFbVCrj9zGCE=; b=dFKrJbfNNxBBrGyB0d3lmP+Mdc6wpx8AHT2aoBMuNkGBK9jdalvqR0/ISXDUZG0vD5rf6SO9HMVm9/x1Y+j5IEItH7/+8Vy44wRhgANkvn/zjo7cugTeovrKijGpc1mZumWdaOEBhDyeRelCJwxc5PH/aW2F2sE6RviU/PUJYEE6qh2urpDsbaw8QS8Sh9g1wqtxI4cBkbW331SFEltFu/N0D07Un3qsNbwS0m5jfDiZMTgymyKFor2r0li/kgPuTkKW0mQF/o9Lz+fXftFdS+CoBR2NwooxjRg7oeMw0esflTJt73HsRhs79jt2Gitjla12IS7s7U0KAuaOYT23jw== 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=iyTgTV1EjMmYIQspIdvdkoKETqfyEgNnFbVCrj9zGCE=; b=3JYNRo3SjqBAHFyLPO/CNIIqXjixKhhfJzKvRRl34A8MMul9MfCqYRagLuufXyuh/RpS6+pFrW0xnKdO8obYvWofO1YMfKuqob7g6OwSmIxT9FcLo6Mo5bvH5kBFTcKYfvoCRgyLCZUgjP9vaO9Xq98BirtzMjpt2Vtv57XSEuA= Received: from PH8P223CA0009.NAMP223.PROD.OUTLOOK.COM (2603:10b6:510:2db::6) by SA3PR12MB9089.namprd12.prod.outlook.com (2603:10b6:806:39f::7) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.21.662.10; Sun, 29 Nov 2026 22:32:20 +0000 Received: from CY7PEPF000000EA.namprd04.prod.outlook.com (2603:10b6:510:2db:cafe::9a) by PH8P223CA0009.outlook.office365.com (2603:10b6:510:2db::6) 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:20 +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 CY7PEPF000000EA.mail.protection.outlook.com (10.167.247.166) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.21.688.5 via Frontend Transport; Sun, 29 Nov 2026 22:32:18 +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:12 -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 07/27] x86/entry: Handle int3 text patching with kernel shadow stack Date: Sun, 29 Nov 2026 22:29:32 +0000 Message-ID: <20261129222953.9124-8-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: CY7PEPF000000EA:EE_|SA3PR12MB9089:EE_ X-MS-Office365-Filtering-Correlation-Id: 79f56f57-bb66-445d-1da8-08df4e68a0ac X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0;ARA:13230040|36860700016|1800799024|376014|7416014|23010399003|82310400026|56012099006|10067099003|261026140227099003|261021135927099003|261026121527099003|261028144727099003|261022105027099003|261022075727099003|261021123027099003|261021121427099003|261102123227099003|261102130727099003|261009223027099003|18002099003|22082099003|11063799006|6133799003|3023799007; X-Microsoft-Antispam-Message-Info: RCJ7+kdmuoeWxCxnRiA1UECdfG2X23+4QOnnFXdAKz0zfrSupijECjYz8/3xOwL+TueFfAVLnbMNLjQG65pbAkIibaN+OHb31XYjdgi4+DE5Wb54B50rDUIQpU/4A3ZHFhxUk/q/IQngWLkt/Ogj/nQDQwAaiWhOkH2sU9F5FoI/5JM+6igiJkb4/ncRwE5JmhEn3PJG/wI+grilpwPLWmgDN8B8DgMGx+AG64ylj1YRPYmI2/tcCyPrrsM4D8kf/FXm7c9Ke8tU5Ii5g1BtSIpC+TMSTOMDo0Iq1orOBI/b0DY2IMuKiSGYfKfN2tQROnbOxtnRajNhY9Hz3JjDZ+misV1fsGJ2ubdJQcLefGK/abpSK/XWyglN/YWtKqgkpJm+cxlAxFQyKkyI2Gxsekxa1Dt/EYEGh2dN5Iqt0dJbBdNYVLtCOa9JUHTfkAHQsbXPuLqL4Gxl//KIqeWAHeSdbvS+wTtwPqVIKjDnbIZ3nTV+UEQQoYtkm103eWJ63bqJoHtBoUREWVB162VBgUz19uS2kPWIxdAOooW4gJan7yVpTWuCL4j8et4Ya+jjl/BQ0slY+476Nrz+dfDJRq1fv0MdfJ9gQpagBy8vgmq3424DDkygQ+ulqmxyAlSbZ62E6Ro5x6fhuLTWAw3QByY0ePSCLv6WCL6mAshQ/0abd9RZfntW5c9Q7Dbotl/pDr6mV2wjC1XZ2JG1zTjc9g== 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)(1800799024)(376014)(7416014)(23010399003)(82310400026)(56012099006)(10067099003)(261026140227099003)(261021135927099003)(261026121527099003)(261028144727099003)(261022105027099003)(261022075727099003)(261021123027099003)(261021121427099003)(261102123227099003)(261102130727099003)(261009223027099003)(18002099003)(22082099003)(11063799006)(6133799003)(3023799007);DIR:OUT;SFP:1101; X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: jzxW2tyilFcpJDvNajYZLyXL9E3+g4yyGohcfGAke076aV2ZwmDWnsXRPwaOGVLbnMo/n+hJmTNJINwWrQF+qn2nzhkxlFirvfHqBMPyZG6sXVrS4oim5eIxhYv1IT/vPGETzEsM5TDrkcCPyWFbX2pJR1AXAqlyQHBkkttfoIKAnFwtlx6nJ3EnxIxFFjCKnA+ldTKU1CWwgnBtl/kgh2wuGDe4nuQKDrfjLPR/lcmnPBke2WK/+ndKtyXb3Q3I3O06H3OUa/ovmh4kE76tYOhdjiDd/rL21IcQQKEMEtAeDbD5np5oJ+rEoi12x8IU9c+VcikmyVqfaNfZlU8zFQZ/Td61vgfLuFCaQB3dyWPeQg2ty+LUsLm4R9Xv/RoHRKebjD0AwyQYlpjebj/c71sRF3Kb8suHz2qZ95ukYnqidpAHlnWCB9ClDfec0IWN X-OriginatorOrg: amd.com X-MS-Exchange-CrossTenant-OriginalArrivalTime: 29 Nov 2026 22:32:18.8216 (UTC) X-MS-Exchange-CrossTenant-Network-Message-Id: 79f56f57-bb66-445d-1da8-08df4e68a0ac 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: CY7PEPF000000EA.namprd04.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Anonymous X-MS-Exchange-CrossTenant-FromEntityHeader: HybridOnPrem X-MS-Exchange-Transport-CrossTenantHeadersStamped: SA3PR12MB9089 Kernel running with shadow stacks enabled explode early into the boot during text-poke like: RIP: 0010:asm_fred_entrypoint_kernel+0x5f/0x70 <- ERETS, #CP RIP: 0010:native_send_call_func_ipi+0x11/0xf0 <- after emulated NOP5 smp_call_function_many_cond+0x2c0/0x5f0 smp_text_poke_batch_finish+0x12f/0x590 static_key_enable+0x1f/0x30 _cpu_up+0x11a/0x260 With kernel shadow stacks, a same-level FRED event pushes {CS, LIP, SSP} onto the shadow stack. ERETS raises #CP unless LIP and CS match the RIP and CS on the regular stack. The extable code already updates the frame through regs_set_return_ip() but this has not been extended to generic int3 based text patching. Emulating JMP and RET are simple enough - jmp sets the ret->ip like extables and ret too does this with an addition of an emulated stack pop to direct to the return IP which has to adjust the SSP accordingly by moving it one slot lower. Emulating a CALL is slightly more complicated - CALL pushes the return address onto the stack and jumps to the function as if it was called from LIP. FRED stacks add a redzone on the stack that can be used for the call emulation but there is no gap in shadow stack for the same. There are two ways to solve this: o Inspired by IDT handling, add a dummy shadow stack slot on shadow stack for X86_TRAP_BP. This slot can then be used for CALL emulation to push a ret frame below the FRED shadow stack frame and then steer the FRED shadow stack frame to return to the caller. In practice it looks like: .macro PUSH_KERN_SHSTK_FRAME xor %eax, %eax pushq %rax rdsspq %rax cmpb $X86_TRAP_BP, PTREGS_fred_vector(%rdi) jne .Lpush_shstk_done\@ ANNOTATE_INTRA_FUNCTION_CALL call 1f int3 1: /* Indicate one extra slot is available on frame */ add $8, %rsp incq (%rsp) .Lpush_shstk_done\@: push %rax .endm .macro POP_KERN_SHSTK_FRAME pop %rax pop %rax /* Spare slots are recalimed here. */ incsspq %rax .endm The CALL emulation moves the FRED frame up by one slot to occupy the spare slot, sets 8(%rsp) to 0 to indicate that spare slot has been consumed This has one key disadvantage that the INTRA_FUNCTION_CALL for every #BP will unbalance the RSB and CPU will mis-speculate every return after the handler has finished. Not ideal. o (Implemented here) Use the same spare frame flags at 8(%rsp) to insert a tail call at POP_KERN_SHSTK_FRAME during FRED_EXIT. This adds a matching RSB to call entry much like the orig_rax path in the ftrace_regs_call. The call emulation would have adjusted the SS frame to insert the ret frame, CS, and LIP. POP_KERN_SHSTK_FRAME adds the missing SSP to slot by pointing it to SPP + 32 after the INTRA_FUNCTION_CALL. The emulation functions are marked noisntr since subsequent patches enable Ftrace caller hooks which can recurse if the patching function can be probed. Signed-off-by: K Prateek Nayak --- arch/x86/entry/entry_64_fred.S | 92 ++++++++++++++--- arch/x86/include/asm/insn-eval.h | 22 ++++- arch/x86/include/asm/text-patching.h | 7 +- arch/x86/kernel/alternative.c | 4 +- arch/x86/kernel/asm-offsets.c | 3 + arch/x86/kernel/cet.c | 142 +++++++++++++++++++++++++-- arch/x86/kernel/traps.c | 4 +- 7 files changed, 247 insertions(+), 27 deletions(-) diff --git a/arch/x86/entry/entry_64_fred.S b/arch/x86/entry/entry_64_fred.S index 9ff32e2a9103..284976114c70 100644 --- a/arch/x86/entry/entry_64_fred.S +++ b/arch/x86/entry/entry_64_fred.S @@ -3,6 +3,7 @@ * The actual FRED entry points. */ +#include #include #include @@ -16,6 +17,79 @@ .code64 .section .noinstr.text, "ax" +#ifdef CONFIG_X86_KERN_SHADOW_STACK + +/* + * A INT3 breakpoint can emulate a CALL which requires pushing the + * return frame below this FRED event. Allow the handlers to communicate + * if it emulated a CALL by setting a flag on the stack. + * + * FRED event pushes { SSP, LIP, CS } onto the shadow stack and SSP + * points to record three slots for top here and it should stay that + * way. + * + * CALL emulation will consume the SSP, push a return frame below, and + * set a flag on stack. If the flag is set, FRED_EXIT will add an + * shadow stack frame on top by doing an INTRA_FUNCTION_CALL and + * populate the top with a pointer to three slots below, thus recreating + * the correct FRED frame. + */ +.macro PUSH_KERN_SHSTK_FRAME + xor %eax, %eax + rdsspq %rax /* NOP if SHSTK is off. */ + pushq %rax + /* + * Frame metadata: + * + * [15:8] Context (FRED_EVENT) + * [ 7:0] fred_exit_work_flags + * + * Context data is used by CET helpers to interpret + * SSP frame. fred_exit_work_flags is used to fixup + * shadow stack for CALL emulation. + */ + pushq $0x100 +.endm + +/* Clobbers RAX, RCX; Both will be restored later. */ +.macro POP_KERN_SHSTK_FRAME + popq %rax /* fred_exit_work_flags */ + + /* Only one action is supported currently. */ + test %al, %al + jz .Lpop_shstk_done\@ + + /* + * CALL Emulation has overwritten the shadow-stack frame and + * SSP is lost. Point it back to 3 slots down where the real + * return has to go. + * + * Doing a CALL here will add a RSB which the next RET will + * mispredict on but all RETs after that are balanced. + */ + ANNOTATE_INTRA_FUNCTION_CALL + call 1f + int3 +1: + /* Synthesize the corrct SSP to fix the FRED Frame. */ + addq $8, %rsp + rdsspq %rax + leaq 24(%rax), %rcx + wrssq %rcx, (%rax) + +.Lpop_shstk_done\@: + popq %rax +.endm + +#else /* !CONFIG_X86_KERN_SHADOW_STACK */ + +.macro PUSH_KERN_SHSTK_FRAME +.endm +.macro POP_KERN_SHSTK_FRAME +.endm + +#endif /* CONFIG_X86_KERN_SHADOW_STACK */ + .macro FRED_ENTER ssp=0 UNWIND_HINT_END_OF_STACK ANNOTATE_NOENDBR @@ -23,20 +97,16 @@ ENCODE_FRAME_POINTER movq %rsp, %rdi /* %rdi -> pt_regs */ -#ifdef CONFIG_X86_KERN_SHADOW_STACK - .if \ssp - rdsspq %rax /* NOP if SHSTK is off. Pre-zeroed. */ - push %rax - .endif -#endif +.if \ssp + PUSH_KERN_SHSTK_FRAME +.endif .endm .macro FRED_EXIT ssp=0 -#ifdef CONFIG_X86_KERN_SHADOW_STACK - .if \ssp - pop %rax /* Any dead register */ - .endif -#endif +.if \ssp + POP_KERN_SHSTK_FRAME +.endif + UNWIND_HINT_REGS POP_REGS .endm diff --git a/arch/x86/include/asm/insn-eval.h b/arch/x86/include/asm/insn-eval.h index 2f4c4d3d3f9b..75d40a399ace 100644 --- a/arch/x86/include/asm/insn-eval.h +++ b/arch/x86/include/asm/insn-eval.h @@ -83,8 +83,13 @@ static inline void insn_assign_reg(unsigned long *reg, u64 val, int bytes) } #ifdef CONFIG_X86_CET + void regs_set_return_ip(struct pt_regs *regs, unsigned long ip); -#else +void regs_emulate_call(struct pt_regs *regs, unsigned long ret, unsigned long func); +void regs_emulate_ret(struct pt_regs *regs, unsigned long ret_ip); + +#else /* !CONFIG_X86_CET */ + static inline void regs_set_return_ip(struct pt_regs *regs, unsigned long ip) { /* This is only for changing kernel return addresses: */ @@ -92,6 +97,19 @@ static inline void regs_set_return_ip(struct pt_regs *regs, unsigned long ip) regs->ip = ip; } -#endif + +static inline void regs_emulate_call(struct pt_regs *regs, + unsigned long ret, + unsigned long func) +{ + regs_set_return_ip(regs, func); +} + +static inline void regs_emulate_ret(struct pt_regs *regs, unsigned long ret_ip) +{ + regs_set_return_ip(regs, ret_ip); +} + +#endif /* CONFIG_X86_CET */ #endif /* _ASM_X86_INSN_EVAL_H */ diff --git a/arch/x86/include/asm/text-patching.h b/arch/x86/include/asm/text-patching.h index ea09381070e8..2c0c736fb62a 100644 --- a/arch/x86/include/asm/text-patching.h +++ b/arch/x86/include/asm/text-patching.h @@ -5,6 +5,7 @@ #include #include #include +#include /* * Currently, the max observed size in the kernel code is @@ -135,7 +136,7 @@ extern __ro_after_init unsigned long text_poke_mm_addr; static __always_inline void int3_emulate_jmp(struct pt_regs *regs, unsigned long ip) { - regs->ip = ip; + regs_set_return_ip(regs, ip); } static __always_inline @@ -167,14 +168,14 @@ static __always_inline void int3_emulate_call(struct pt_regs *regs, unsigned long ip, unsigned long func) { int3_emulate_push(regs, ip); - int3_emulate_jmp(regs, func); + regs_emulate_call(regs, ip, func); } static __always_inline void int3_emulate_ret(struct pt_regs *regs) { unsigned long ip = int3_emulate_pop(regs); - int3_emulate_jmp(regs, ip); + regs_emulate_ret(regs, ip); } static __always_inline diff --git a/arch/x86/kernel/alternative.c b/arch/x86/kernel/alternative.c index 582c6d8307d1..20880945a1fa 100644 --- a/arch/x86/kernel/alternative.c +++ b/arch/x86/kernel/alternative.c @@ -1981,7 +1981,7 @@ static bool decode_fineibt_preamble(struct pt_regs *regs, unsigned long *target, * Since regs->ip points to the middle of an instruction; it cannot * continue with the normal fixup. */ - regs->ip = *target; + regs_set_return_ip(regs, *target); return true; @@ -2085,7 +2085,7 @@ static bool decode_fineibt_paranoid(struct pt_regs *regs, unsigned long *target, *target = regs->r11 + fineibt_prefix_size; *type = regs->ax; - regs->ip = *target; + regs_set_return_ip(regs, *target); return true; } diff --git a/arch/x86/kernel/asm-offsets.c b/arch/x86/kernel/asm-offsets.c index acbd0c1fb2ad..fc1cbee2f9e9 100644 --- a/arch/x86/kernel/asm-offsets.c +++ b/arch/x86/kernel/asm-offsets.c @@ -112,6 +112,9 @@ static void __used common(void) BLANK(); DEFINE(PTREGS_SIZE, sizeof(struct pt_regs)); OFFSET(C_PTREGS_SIZE, pt_regs, orig_ax); +#ifdef CONFIG_X86_KERN_SHADOW_STACK + DEFINE(PTREGS_fred_vector, offsetof(struct pt_regs, fred_ss) + 4); +#endif /* TLB state for the entry code */ OFFSET(TLB_STATE_user_pcid_flush_mask, tlb_state, user_pcid_flush_mask); diff --git a/arch/x86/kernel/cet.c b/arch/x86/kernel/cet.c index 97375a8335c8..bab8ff1c64c5 100644 --- a/arch/x86/kernel/cet.c +++ b/arch/x86/kernel/cet.c @@ -114,6 +114,14 @@ static void ibt_clear_fred_wfe(struct pt_regs *regs) regs->fred_cs.wfe = 0; } +enum fred_exit_work { + FRED_EXIT_EMULATE_CALL = 1 +}; + +enum frame_context { + FRED_ENTRY = 1 +}; + /* * FRED hardware pushes this frame on to the shadow stack for * kernel=>kernel entry. First (lowest address) in this struct @@ -129,11 +137,23 @@ struct fred_shstk_entry_frame { * The kernel=>kernel entry code pushes SSP on to the entry stack. * This only occurs for kernel=>kernel entry. Not userspace. * - * SSP is pushed on to the frame below pt_regs. At the point it is - * pushed, it points to a special FRED entry frame. + * Number of spare frames available on shadow stack is pushed onto + * the frame below pt_regs followed by the SSP before the spare + * frames were populated (if any). At the point it is pushed, it + * points to a special FRED entry frame. */ -struct fred_kernel_frame { - struct fred_shstk_entry_frame *ssef; +struct kernel_event_frame { + union { + struct { + u8 fred_exit_work_flags; + u8 context; + }; + u64 metadata; + }; + union { + struct fred_shstk_entry_frame *ssef; + u64 *ssp; + }; struct pt_regs regs; }; @@ -158,7 +178,7 @@ static void do_kernel_shstk_cp(struct pt_regs *regs, unsigned long error_code) unsigned long ssp, ip, shstk_ip, sp_off = 0; u8 insn[sizeof(erets)] = { }; - ssef = container_of(regs, struct fred_kernel_frame, regs)->ssef; + ssef = container_of(regs, struct kernel_event_frame, regs)->ssef; ssp = cp_peek((unsigned long)&ssef->ssef_ssp); switch (error_code & CP_EC) { @@ -249,7 +269,7 @@ DEFINE_IDTENTRY_ERRORCODE(exc_control_protection) * * Update the FRED entry frame in addition to regs->ip. */ -void regs_set_return_ip(struct pt_regs *regs, unsigned long ip) +noinstr void regs_set_return_ip(struct pt_regs *regs, unsigned long ip) { struct fred_shstk_entry_frame *ssef; @@ -261,9 +281,117 @@ void regs_set_return_ip(struct pt_regs *regs, unsigned long ip) /* Only kernel-mode events save the 'ssef' pointer. */ WARN_ON_ONCE(user_mode(regs)); - ssef = container_of(regs, struct fred_kernel_frame, regs)->ssef; + ssef = container_of(regs, struct kernel_event_frame, regs)->ssef; + if (!ssef) + return; + + wrss(ip, &ssef->ssef_lip); +} + +/* + * Make the event return to @func as if it executed a CALL that returns to + * @ret. The caller has already pushed @ret on the regular stack. + * If int3 does not emulate a CALL, the dummy shadow stack frame is popped + * at FRED_EXIT \ssp=1 before ERETS. + */ +noinstr void regs_emulate_call(struct pt_regs *regs, unsigned long ret, unsigned long func) +{ + struct fred_shstk_entry_frame *ssef; + struct kernel_event_frame *frame; + int frame_size; + u64 *raw_frame; + + regs->ip = func; + + if (!cpu_feature_enabled(X86_FEATURE_KERN_SHSTK)) + return; + + WARN_ON_ONCE(user_mode(regs)); + + frame = container_of(regs, struct kernel_event_frame, regs); + + ssef = frame->ssef; + if (!ssef) + return; + + /* Someone else emulated a CALL for same frame? */ + if (WARN_ON_ONCE(frame->fred_exit_work_flags)) + return; + + /* + * Return frame must be right below the FRED frame + * else FRED_EXIT will not be able to recreate the + * SSP correctly. Leave breadcrumbs if we #CP. + */ + raw_frame = (u64 *)ssef; + frame_size = sizeof(*ssef) / sizeof(u64); + WARN_ON_ONCE((u64 *)ssef->ssef_ssp != raw_frame + frame_size); + + frame->fred_exit_work_flags |= FRED_EXIT_EMULATE_CALL; + wrss(func, &ssef->ssef_lip); + + /* + * Consume the SSP and move the rest of the FRED frame aka + * { LIP, CS } up by one slot. This is how the shadow stack + * looks at the end: + * + * +-----+ +-----+ <--- Top of Shadow Stack + * | SSP | | LIP | + * | LIP | -> | CS | + * | CS | +-----+ + * +-----+ <- SSP points | RET | + * | ... | here +-----+ + * +-----+ | ... | + * + * Since SSP should always point 3 slots below, a new frame is + * synthesized at FRED exit to reconstruct the SSP making the + * FRED frame whole again before ERETS. + * + * XXX: One wrss() call can be reduced by doing "ssef_lip" + * overwrite during the movement but keep it simple for now. + */ + for (int i = 0; i < frame_size - 1; ++i) + wrss(raw_frame[i + 1], &raw_frame[i]); + + /* + * Populate the ret frame and consme the FRED frame + * pointer so no other emulation is possible. + */ + wrss(ret, &raw_frame[frame_size - 1]); + frame->ssef = NULL; +} + +/* + * Make the event return to @ip as if it executed a RET. The caller + * has already popped @ip off the regular stack. + */ +noinstr void regs_emulate_ret(struct pt_regs *regs, unsigned long ip) +{ + struct fred_shstk_entry_frame *ssef; + + regs->ip = ip; + + if (!cpu_feature_enabled(X86_FEATURE_KERN_SHSTK)) + return; + + WARN_ON_ONCE(user_mode(regs)); + + ssef = container_of(regs, struct kernel_event_frame, regs)->ssef; if (!ssef) return; + /* + * Return target should point to the old SP on top of the shadow + * stack that was popped from the regular stack. + */ + if (*((u64 *)ssef->ssef_ssp) != ip) + return; + + /* Point the SP on the frame to the new SP one frame below. */ + wrss(ssef->ssef_ssp + sizeof(unsigned long), &ssef->ssef_ssp); + /* + * Set return ip on frame to the new return target such that + * return from FRED frame jumps to the target. + */ wrss(ip, &ssef->ssef_lip); } diff --git a/arch/x86/kernel/traps.c b/arch/x86/kernel/traps.c index 30aa8369957e..01b16dcd0d8c 100644 --- a/arch/x86/kernel/traps.c +++ b/arch/x86/kernel/traps.c @@ -466,9 +466,9 @@ noinstr bool handle_bug(struct pt_regs *regs) */ if (handled) { if (regs->ip == addr) - regs->ip += ud_len; + regs_set_return_ip(regs, addr + ud_len); } else { - regs->ip = addr; + regs_set_return_ip(regs, addr); } if (regs->flags & X86_EFLAGS_IF) -- 2.43.0