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=52.101.85.48 ARC-Seal: i=2; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=pass; t=1795991488; b=sXMqtfJHy198VppXHSNOCHRN7mPOSycd0Ad78CESqsZAj8qLuydqDwGRw5zgLzG141A1 vF4mzInGNzDk0Rf3X6NPT8v878+GulpQUWG4OyYOVLS6kCNnmKm0ftG8aD9r57g/5r0OF X2DOtyY/jq6c3+v552rB4J5mk4+tOHjgV57E32IbREW+uz51lU+UlspJ82No7SW9int6q raEJpzgdCY3Buos6fxgl7gZ4dYZFm4qaDksD1cj4ZUBzHQXXlGiUVA548ynJ19DOn4n21 m2CBFy25j+N8e+0+C9l3GNyYR8bhk+Q7VlSEOqumfmcd47GiD06rn9eVgzHDXEKXl3A== ARC-Message-Signature: i=2; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1795991488; 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=Xp5Yg727Ljzv7pmqhyHQrV6TFTa3aXzkKUzyhIwEIBY=; b=gFp8wDwv3VpZzKvDn7wKZ7KwNOiDYsfWErZQJ5Tofp0uMBokd/X+fjKA2fobBY53EG9h bpVVs5FpJBOp1xEINZkUsUyRcovML8iNGLlm6ACExN9JYVXR9Ztohq2VoXSOxF4Ir6DYg OmY3SwHYMtcx+TAsac0SwTjtCs3DWXF/leqrqHpMsfQvzh7lPT4N95riwmeOjyAxTfM7N H0kLEWhezD0AD54cz+Mmit8y7P4wlGM/xFL7z4CO50sDbaFQVA82AwqkeBhm20DlyPMv0 aiLXb1iBPD5+S5kcD68pkW47J1nFE5Fyotb/KuH1t+zAimMmBWJM15hx6+6hVMQeWvQ== 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=EoBaZAyq; arc=pass header.oldest-pass=0 smtp.remote-ip=52.101.85.48 Received: from BYAPR05CU005.outbound.protection.outlook.com (mail-westusazon11010048.outbound.protection.outlook.com [52.101.85.48]) (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 BEC46606A325 for ; Sun, 29 Nov 2026 22:31:27 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org BEC46606A325 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=EoBaZAyq ARC-Seal: i=1; a=rsa-sha256; s=arcselector10001; d=microsoft.com; cv=none; b=lS7Ig8+MS5QwnwLuriawC9s06JKfO13FQi9xZzfQojbEbJauBjwSIH/rwaOxlnGlsbKx6lA1N7bfNyXssPpXmpZqFaaScsckN1Wewfbn80zyFD6ebRwWwpabt3r+lrrn8JIv4NBquDBg9EC8QvMo73TNO6wlnNZ/6k9c5OpNK58HT+meeeSpy/r1IBygJY0qn87OKVUsjuMt5YBFms63MJkYOFb6p2/ugww0Lm5FcQwEFQjOUH9pE9pfC83QvUT7rpVrvMO1CwyYgZPS05RrKeKxvHkuX0mw+bTQEt+ONhX/3Eo5L4ywynJGEQ6rbZv2cOL8WiYzj9Tf254SRYjVbQ== 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=Xp5Yg727Ljzv7pmqhyHQrV6TFTa3aXzkKUzyhIwEIBY=; b=HJPZhpGhzM5xE/yEj5YDqwSQpXaeO1PB0G9RxM95c7M/NmgZ0904pSR3ReW7Tmbvu5VD54/B9slA4VNf0EYWPlZo5ZDcvy4xeQi0s7dYayVjWHOEK3qJpEL2o8X9Zt3pw5s1J7ngtVCmDfIa/4xsbntnC170ydoj+E+plrXDPIebjvxQlj6JTnVdOOWj8n3SLRi8LdSTYkosy5agY7rATa8bEg8EdiycdISpvJDaNVpGPkNzLsn9CHQZcwzdaFT+s0m1l+CcaDqYONJT9tl3JUT/Z4+6fLyRe03+Fmkh/NbD8ujMWM9wzt6eihp5iNJYKfNu5Z+c2xXTSXaNX/Irjg== 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=Xp5Yg727Ljzv7pmqhyHQrV6TFTa3aXzkKUzyhIwEIBY=; b=EoBaZAyqX8+RU1fG6+iCMiheldMCgj4aJT+Xokh85PTmXyw4VJ5kDwbGd0XO6KNeX32vnsLIRHVEYP0Kbyx1jI67Ayr+aAmJJD8CFCbp/hwsRbItKXvIAuRr6GQ38OE/M+fpSRi0pMz71bX59pODr47LCkUlAxrL9GWuKNo2Sq4= Received: from CH0PR03CA0259.namprd03.prod.outlook.com (2603:10b6:610:e5::24) by SJ1PR12MB6049.namprd12.prod.outlook.com (2603:10b6:a03:48c::22) 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:31:23 +0000 Received: from CH7PEPF0000001A.namprd21.prod.outlook.com (2603:10b6:610:e5:cafe::8a) by CH0PR03CA0259.outlook.office365.com (2603:10b6:610:e5::24) 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:31:23 +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 CH7PEPF0000001A.mail.protection.outlook.com (10.167.244.169) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.21.688.0 via Frontend Transport; Sun, 29 Nov 2026 22:31:23 +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:31:19 -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 04/27] x86/cet: Make kernel shadow stack #CP fatal instead of livelocking Date: Sun, 29 Nov 2026 22:29:29 +0000 Message-ID: <20261129222953.9124-5-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: CH7PEPF0000001A:EE_|SJ1PR12MB6049:EE_ X-MS-Office365-Filtering-Correlation-Id: 536d4631-6799-4d28-27e9-08df4e687fb6 X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0;ARA:13230040|36860700016|23010399003|1800799024|82310400026|7416014|376014|10067099003|56012099006|261028144727099003|261026121527099003|261102123227099003|261026140227099003|261021123027099003|261021121427099003|261022075727099003|261022105027099003|261009223027099003|18002099003|22082099003|5023799004|11063799006|6133799003|3023799007; X-Microsoft-Antispam-Message-Info: yr3NnTdQ9k61aRGscBRGmPlcqVy6mJTdIWRiu/AUyvd8vVtXs6TQdxhLFSYIBrecT8hLCpDtOhGXb3TmVioJOzHe/IfQ3oeHNBpLjlkJZ6ffivUR9pBK9TTvAzP2sYZltEURYtCp94VeRBxOnNgiojGf/5bScweWzCco1qVF2hmhWo5jqn1+8Rym1PLcSaPTkmVbeBLL5E0rqGlmeaD9yIHSjacsPQCD/51MCNc5nxLBxib3mZDqMkOE8IPP2tpSYfhMreonipMrsbxjQXuaWtoWxfCg2KNte829lnn8AIqqK8A1v2WK6TaG0b3sixz7EHuuxf20MC+ZQe4zVu9loz3JPUQCUMk905cZkKFe6Xw086OgNkHVKejDI3siT83dK8uUB/TD0TkbEzTx1iHu213sUQXpdWd++Vu2IPC5d6gXPA73U2r3yJEYZKo1w8EcwFdgRrEV1YE6huoVmAWI37vZmoYF0+7qrcO0POnPq32KlZuKwiEW0V2TIA87LME32AUARMXxVZ9dQznHnPEeqJYEL9SCZ7UXKtJB5tWsSgEzigc96/3KI+mNTGaC6HVbP4k0NzXZMHTpqSujOSZF3Ie8iqWBsh3bB6r+LlR7x6eTzcIN/84CQ16dAPAif5HGEvM3dQpLug/FIs9FZIl6aYJTKCqA0XccktRujs0A2fN6AhQ5VdJQct1EhPmePHdzdZeX+cWEMrtGC5f37PhJ2A== 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)(7416014)(376014)(10067099003)(56012099006)(261028144727099003)(261026121527099003)(261102123227099003)(261026140227099003)(261021123027099003)(261021121427099003)(261022075727099003)(261022105027099003)(261009223027099003)(18002099003)(22082099003)(5023799004)(11063799006)(6133799003)(3023799007);DIR:OUT;SFP:1101; X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: S1/I5er0jbs3YFuMLWf+60cz8oNnzXL2Q/ckyP069CbPzhimhajMy9O2cHNznOAk9cQUWfROAzXwGVJXsVsweqiCMgN9a3OT+nyKdlcpmajAd8jI6h6BitfLVyr84phuyysox0Y49Uzptq5mOiv+iPL12FNneKdOLr2y5lpJvCoANTSeLnQBoC2jA+wewaWSSAKYdyeqMMuzokxsVDMT4wgq1r1zTa13JhbLA0qtn+5hE1tJ94mJv//wnyHbB1RJ9GhV23pCg7xj0wh4BcKajipA52HcmyhH1WRolienjkPtYbe31cBoYpdwAqmEnDvW/c7hGGnYk/2DlgFg2uoxALYz0p9jq1y7EiWdGFot2Uz8TejcWIj+r2DxUsia6u6j/OVUwxFPb03ptdjxVuEAjI81wXkvhxtRZ/bXpNKS8ZLxUsOxwLtegvh1gAi8F4D0 X-OriginatorOrg: amd.com X-MS-Exchange-CrossTenant-OriginalArrivalTime: 29 Nov 2026 22:31:23.5777 (UTC) X-MS-Exchange-CrossTenant-Network-Message-Id: 536d4631-6799-4d28-27e9-08df4e687fb6 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: CH7PEPF0000001A.namprd21.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Anonymous X-MS-Exchange-CrossTenant-FromEntityHeader: HybridOnPrem X-MS-Exchange-Transport-CrossTenantHeadersStamped: SJ1PR12MB6049 A kernel-mode shadow stack #CP (NEAR-RET or FAR-RET/IRET) ends up in do_unexpected_cp(), which does WARN_ONCE() and returns to a shadow stack with mismatch. Returning executes that instruction again and faults again. After the first WARN this repeats silently forever. For example, IRET-to-self in sync_core() on a CPU without SERIALIZE just hangs the machine. Nothing can be fixed up at that point. Handle kernel-mode shadow stack regular stack top vs the shadow stack top for RET, and the RIP in the IRET/ERETS frame vs the shadow stack LIP for FAR-RET/IRET. Then die(). A code path that returns somewhere the shadow stack doesn't expect now shows up as a one-line oops naming both targets, instead of a hang or a WARN storm. Signed-off-by: K Prateek Nayak --- arch/x86/kernel/cet.c | 101 ++++++++++++++++++++++++++++++++---------- 1 file changed, 77 insertions(+), 24 deletions(-) diff --git a/arch/x86/kernel/cet.c b/arch/x86/kernel/cet.c index 4033775a922c..97375a8335c8 100644 --- a/arch/x86/kernel/cet.c +++ b/arch/x86/kernel/cet.c @@ -1,7 +1,9 @@ // SPDX-License-Identifier: GPL-2.0 #include +#include #include +#include #include #include #include @@ -112,6 +114,77 @@ static void ibt_clear_fred_wfe(struct pt_regs *regs) regs->fred_cs.wfe = 0; } +/* + * FRED hardware pushes this frame on to the shadow stack for + * kernel=>kernel entry. First (lowest address) in this struct + * is the last entry pushed by the hardware. + */ +struct fred_shstk_entry_frame { + u64 ssef_ssp; + u64 ssef_lip; + u64 ssef_cs; +}; + +/* + * 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. + */ +struct fred_kernel_frame { + struct fred_shstk_entry_frame *ssef; + struct pt_regs regs; +}; + +static unsigned long cp_peek(unsigned long addr) +{ + unsigned long val = 0; + + copy_from_kernel_nofault(&val, (void *)addr, sizeof(val)); + return val; +} + +/* + * Shadow stack #CPs are faults: regs->ip is the RET/IRET/ERETS that found + * the mismatch and returning from here would just execute it again. There + * is no way to recover, so say which two return addresses disagreed and + * die. + */ +static void do_kernel_shstk_cp(struct pt_regs *regs, unsigned long error_code) +{ + struct fred_shstk_entry_frame *ssef; + static const u8 erets[] = { 0xf2, 0x0f, 0x01, 0xca }; + unsigned long ssp, ip, shstk_ip, sp_off = 0; + u8 insn[sizeof(erets)] = { }; + + ssef = container_of(regs, struct fred_kernel_frame, regs)->ssef; + ssp = cp_peek((unsigned long)&ssef->ssef_ssp); + + switch (error_code & CP_EC) { + case CP_RET: + ip = cp_peek(regs->sp); + shstk_ip = cp_peek(ssp); + break; + case CP_IRET: + /* ERETS skips the error code slot, IRET does not. */ + copy_from_kernel_nofault(insn, (void *)regs->ip, sizeof(insn)); + if (!memcmp(insn, erets, sizeof(erets))) + sp_off = 8; + ip = cp_peek(regs->sp + sp_off); + shstk_ip = cp_peek(ssp + 8); + break; + default: + ip = shstk_ip = 0; + break; + } + + pr_emerg("Kernel shadow stack violation (%s) at %pS: stack: %pS shadow stack: %pS (ssp=%lx)\n", + cp_err_string(error_code), (void *)regs->ip, (void *)ip, + (void *)shstk_ip, ssp); + die("control protection fault", regs, error_code); +} + static void do_kernel_cp_fault(struct pt_regs *regs, unsigned long error_code) { if ((error_code & CP_EC) != CP_ENDBR) { @@ -156,36 +229,16 @@ DEFINE_IDTENTRY_ERRORCODE(exc_control_protection) else do_unexpected_cp(regs, error_code); } else { - if (cpu_feature_enabled(X86_FEATURE_IBT)) + if (cpu_feature_enabled(X86_FEATURE_KERN_SHSTK) && + (error_code & CP_EC) != CP_ENDBR) + do_kernel_shstk_cp(regs, error_code); + else if (cpu_feature_enabled(X86_FEATURE_IBT)) do_kernel_cp_fault(regs, error_code); else do_unexpected_cp(regs, error_code); } } -/* - * FRED hardware pushes this frame on to the shadow stack for - * kernel=>kernel entry. First (lowest address) in this struct - * is the last entry pushed by the hardware. - */ -struct fred_shstk_entry_frame { - u64 ssef_ssp; - u64 ssef_lip; - u64 ssef_cs; -}; - -/* - * 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. - */ -struct fred_kernel_frame { - struct fred_shstk_entry_frame *ssef; - struct pt_regs regs; -}; - /* * Change the return IP of an event. Use this instead of directly * manipulating regs->ip. -- 2.43.0