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.195.20 ARC-Seal: i=2; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=pass; t=1795991651; b=3a+0JfQ+2aKXHsxcLpQ1JRY+OPrtZWz5qE1rg/d95AMZaV/qaSbikIdLDKWYDzKldrjs O2oA67b1/cOtxOc8FuXzYMSMC9RQfjucY9S44w9skVeBAYuzLv6WvrrHhe8na+wLZypa4 Gx1kMCHv8ipJRTUQd/yNpPK/OnHhxp46SBMuu+MmWJOReUpLROXTeFW9dEVQz7J6JiC6h lnS0LIA5NyX4/v6gPv7my7IGG6XTZjD8WYqyGzH5aahYEUd+oa9PoPJ2W0rHwvzXA73Jo u8IPlSWudSEtleev4CO5A+fI9uF+whvkdNDyRbNob0OtjC10rzG02mBlRGrscwPawyA== ARC-Message-Signature: i=2; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1795991651; 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=AmfMO7vtxBJJjbMy9NttZ2CbbkqIqWLem8AdjYC4BAs=; b=SasGgSVEOf6ccKQCrLyjwLTsgGkFFsju+Ts4QWJ2/G6kFZeD4H+KH1DPYG/vFB5PUwsT h6z8MjDj5x5q42opM/lM3UXyaf+HdR4oCFWimq3MT2/IuBEX3soYjUAVxCLLBbaa7N50r PDlCbXKz0dsAwarQnG1aM4DB0/RzaWcuaW255CSU4PSV577dSjBWwfHtdymor/3HVjFXv 5wouCQ8cfwO/1H8qSTAiECBIiu6ACe9YogkfsnZS52nkRtr+VgrSgQ69B+ihoSIYy93HK gDitYaS9lVqkux0WjvdYKD+XeJl7t25NhMwWNSiS3MvhHSHa31xHiLqO4D3ol7fsTkw== 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=oCWmG2qu; arc=pass header.oldest-pass=0 smtp.remote-ip=40.93.195.20 Received: from SN4PR2101CU001.outbound.protection.outlook.com (mail-southcentralusazon11012020.outbound.protection.outlook.com [40.93.195.20]) (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 0AD2F606A31C for ; Sun, 29 Nov 2026 22:34:10 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org 0AD2F606A31C 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=oCWmG2qu ARC-Seal: i=1; a=rsa-sha256; s=arcselector10001; d=microsoft.com; cv=none; b=Kv+OLW8FhVwUtpbil2WhtIlMpAjTcGziGBrZ6XVrlEG2pw/aqOI/T3eXYSxqSTqJLYSJ2f/7wXbRWFAWez9TdbNJUAVj8Lb8PoLiN6IbOxCEGEtoHElKoaDQ6bciII5D7mAHidT3WKlpmMr9Xg2ODLiiGLYVW+TGGFQ8TJi4kuJsjmpIqO+idjGyLJ9/SgQ9mINp/J1ho3LWusVdwj6Sr0DyYcABx+a9kCgKwS4CPunxTx0s6lYeDF7N6+WX97n+ldLttn+pNWfVN8tWZYdqvPg6U+JrxQ9jLZqlw9lS35rgk/Rf0CIgdrOBNyCuLiog5WDdiGI3/u0U6b6i36r22w== 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=AmfMO7vtxBJJjbMy9NttZ2CbbkqIqWLem8AdjYC4BAs=; b=YAwTLFnf1SqzLsHQkGAo1KfJeK1AtY8tj5t29zBdcZcn1w4hDQjc5ED7VDK0HWqPABuOdinskQ57ZKCawQT7VSLbODNTBGyGcAxuFn1KBdKtRRv3YXiSFbP7zLJet4xCrgos9/u1fsNs5D5/Xw4UlO3fLgBa3J0seph4hgp1wuCdgGzQM3n2DwYh7MbRZMQ7tCwejiXpEzzIFUYexMtLsDlnfhRYhPRiTU0yrbjuY8ao9kOahyy3NVXHPYmShLWLOHJDSfqqMfOmOxrd/PxZaEIv0N8nH96DTZqPtV+6U/l//F2/z1BC5wom3NSVplfehCzjjW5REx2w02AHyWrx9A== 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=AmfMO7vtxBJJjbMy9NttZ2CbbkqIqWLem8AdjYC4BAs=; b=oCWmG2qu2iYjbwNpkUnLTjMyLy2dscwkshCV8QNx7C0q997LxnvHdMlCkloEhk2ECxbOADzhTSOfijqiH7wf57EQ6Vc0x4uZDF3T+bbBrJlYN7wM99KSdQrinZcCr2bKxylyJewo4TTVN/G+iecgtZCiM87Q/l3bntOsYll0pFI= Received: from CYXPR03CA0029.namprd03.prod.outlook.com (2603:10b6:930:d2::13) by DM4PR12MB5842.namprd12.prod.outlook.com (2603:10b6:8:65::13) 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:34:02 +0000 Received: from CY7PEPF000000F6.namprd05.prod.outlook.com (2603:10b6:930:d2:cafe::30) by CYXPR03CA0029.outlook.office365.com (2603:10b6:930:d2::13) 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:34:02 +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 CY7PEPF000000F6.mail.protection.outlook.com (10.167.247.154) 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:34:02 +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:57 -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 13/27] x86/cet: Introduce regs_set_return_trampoline() for rethook trampoline Date: Sun, 29 Nov 2026 22:29:38 +0000 Message-ID: <20261129222953.9124-14-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: CY7PEPF000000F6:EE_|DM4PR12MB5842:EE_ X-MS-Office365-Filtering-Correlation-Id: 6123cdb5-32d7-418d-0aac-08df4e68de6c 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|261021121427099003|261022105027099003|261102123227099003|261009223027099003|18002099003|22082099003|11063799006; X-Microsoft-Antispam-Message-Info: 8ULYdBJuuXGgG4mm8CWrebp0TwyG9wh+CtbrYm/f2mahQsExAHDVlOrQEG1KD6rN7r0/S6OvVcRurtLOIHzAa0dJlKXxU2TXtRkZWOD0Dq5+n/jSO1snOlEQpSCrgM8WL6YK+Wcyrno46XOKxGRY4bMibdkZbfGBQOViXu4Lw3aDrMYVhtuZaCwy1AoYLLAZBjtFqqfCk1TnX7FpftWLu5Bt3H2c0nMs21w5NhAKbujDed7RJnL+kgyybnrqmsxsOD54V4lFSJWaJwWIe7ayUvbtiyasSR8qzZ0w9GYBjoRS+juCZxN5B7exDSXi2qBzGWT3FsAIuDWTKYCPcebc4/5k9ytXV05iISs5k3DVroQQ5TdjOfZy1K+fT87ypi5Ftglp0BPvH10eFCxJdsYlOqyQfm9fF1GYxE+xIhvK0kSlMJZZ3GF4MvqNtl4qGcQS6TjIfJ3zmius+Zya2HyNPLDF1MFKfnJCejqbFyJG6Epb0UA89pftbgFeP3UfqqvkfKKgJstnsw43mVTe7GYKfemsDK08RUny1W4Tn1zHdTL7VFQZpDIsrf4id9xYgMZhq5dKSON+hUlqUzZJtD7XSZlhoVQRoC6Ba3q2d5dBT/inuauFleivYltcZ+xZWJnenc0vy5GQBDo4P9zOxqQMy/IxdUNzOvzk7JVkDKn/QiawMcraZkoVCcIyjhiwQIySaJJnA6Lrl0Yp5MX7PUFs6w== 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)(261021121427099003)(261022105027099003)(261102123227099003)(261009223027099003)(18002099003)(22082099003)(11063799006);DIR:OUT;SFP:1101; X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: LTewp6oO6a4PfAe7FcMoyjVVfAwUjmAF/8nIDLKm1Yc88AtFGfbu47vv+JVGIf2BTp1jiiy9kRmrGkiEEP9DZtb3xSuR3m56CuRuhHy3NDlXWuMNK7u7pD3z4P5bEIGb2gfMuVOijr/YbKE2nbJJKKsWyai9OSvD/ub5InSm4a0xL/L75gR76ZGxmzJ8EpYaiqrp5CauTWTr4QlIV8x03VevvRvRIZYi/30sv0C7HHuFE9ocDNGYjsUISYvTTENI46hx14SLHNRvf+QEipvk9T7H8BR6RmF2uRKwftlhhO+VMlOLrHbh+N5eyJUkHKWLKwbL2zj0K8HLP8INqxXbsMy2cVC3MrnqcmAp5jXYmiq3vaMyYUI0gGE8L0chmoCsCbUov8+p6JoJ9GeMQ8XUH/9PLd0OjhaHZ1xe6T1vb0KWUvgSmJYYizH4wK1HzEPL X-OriginatorOrg: amd.com X-MS-Exchange-CrossTenant-OriginalArrivalTime: 29 Nov 2026 22:34:02.4200 (UTC) X-MS-Exchange-CrossTenant-Network-Message-Id: 6123cdb5-32d7-418d-0aac-08df4e68de6c 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: CY7PEPF000000F6.namprd05.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Anonymous X-MS-Exchange-CrossTenant-FromEntityHeader: HybridOnPrem X-MS-Exchange-Transport-CrossTenantHeadersStamped: DM4PR12MB5842 Rethooks hijack the function return to jump to a return trampoline and then to the actual caller. Rethooks can be added from three contexts: caller traced function int3 asm_fred_entrypoint_kernel exec_int3() rethook_hook() caller call traced_function call ftrace_caller call ftrace_handlers rethook_hook() caller call traced_function jmp optprobe_template_entry call probe_handlers rethook_hook() In each case, the return trampoline frame needs to be handled distinctly relative to the frame where pt_regs are created and SSP is stashed. Provide a regs_set_return_trampoline() that will be used in subsequent commits to hijacks the shadow stack entry of return from traced function correctly using the context saved in the entry metadata. The trampolines will use a retpoline to insert a shadow stack slot corresponding to correct return address and return to the caller correctly. Signed-off-by: K Prateek Nayak --- arch/x86/include/asm/insn-eval.h | 5 +++ arch/x86/kernel/cet.c | 77 ++++++++++++++++++++++++++++++++ 2 files changed, 82 insertions(+) diff --git a/arch/x86/include/asm/insn-eval.h b/arch/x86/include/asm/insn-eval.h index 75d40a399ace..2cc5562c7d29 100644 --- a/arch/x86/include/asm/insn-eval.h +++ b/arch/x86/include/asm/insn-eval.h @@ -87,6 +87,7 @@ static inline void insn_assign_reg(unsigned long *reg, u64 val, int bytes) void regs_set_return_ip(struct pt_regs *regs, unsigned long ip); 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); +void regs_set_ret_trampoline(struct pt_regs *regs, unsigned long ret); #else /* !CONFIG_X86_CET */ @@ -110,6 +111,10 @@ static inline void regs_emulate_ret(struct pt_regs *regs, unsigned long ret_ip) regs_set_return_ip(regs, ret_ip); } +static inline void regs_set_ret_trampoline(struct pt_regs *regs, unsigned long ret) +{ +} + #endif /* CONFIG_X86_CET */ #endif /* _ASM_X86_INSN_EVAL_H */ diff --git a/arch/x86/kernel/cet.c b/arch/x86/kernel/cet.c index fb80c841bbfb..da80c6822242 100644 --- a/arch/x86/kernel/cet.c +++ b/arch/x86/kernel/cet.c @@ -433,3 +433,80 @@ noinstr void regs_emulate_ret(struct pt_regs *regs, unsigned long ip) */ wrss(ip, &ssef->ssef_lip); } + +/* + * Hijack the return to jump to @ret. This is called from the int3 + * handler and a spare frame is available. Use it to stuff the return + * trampoline between the actual return and fred_kernel_frame such that + * return from caller is gouted to @ret. + * + * Caller has already pointed the top of regular stack to @ret. + */ +noinstr void regs_set_ret_trampoline(struct pt_regs *regs, unsigned long ret) +{ + struct fred_shstk_entry_frame *ssef; + struct kernel_event_frame *frame; + unsigned int offset = 0; + + 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; + + switch (frame->context) { + case FRED_ENTRY: + /* + * Re-direct the ret below the FRED frame to the new return + * address such that the traced function's RET will jump to + * the trampoline. + */ + wrss(ret, (u64 *)ssef->ssef_ssp); + break; + case FTRACE_CALLER: + /* + * The stack at ftrace_.*caller is: + * + * caller_function + * call traced_function + * call ftrace_caller + * frame->ssp = rdsspq(); + * + * To redirect return of traced traced_function, the + * shadow stack entry below the frame->ssp should be + * modified. + */ + offset = 1; + fallthrough; + case OPTPORBE_TRAMPOLINE: + /* + * Optprobes are eecuted from a jmp to trampoline: + * + * caller_function + * call traced_function + * jmp optprobe_template_entry + * frame->ssp = rdsspq(); + * + * In this case, the return from traced_function is at + * the top of shadow stack at frame->ssp. + */ + wrss(ret, frame->ssp + offset); + break; + case RETURN_TRAMPOLINE: + /* + * Trampolines should only be set at fentry. For + * chaining tampoline calls, use regs_set_return_ip(). + */ + WARN_ONCE(1, "Setting trampoline from return trampoline!\n"); + break; + default: + WARN_ONCE(1, "Unknown calling context %d! Expect #CP\n", + frame->context); + break; + } +} -- 2.43.0