From nobody Fri Apr 10 21:50:56 2026 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id A3471C32772 for ; Thu, 18 Aug 2022 16:14:17 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1344178AbiHRQOQ (ORCPT ); Thu, 18 Aug 2022 12:14:16 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:33816 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1343822AbiHRQON (ORCPT ); Thu, 18 Aug 2022 12:14:13 -0400 Received: from sin.source.kernel.org (sin.source.kernel.org [IPv6:2604:1380:40e1:4800::1]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id A2E0747BAD for ; Thu, 18 Aug 2022 09:14:12 -0700 (PDT) Received: from smtp.kernel.org (relay.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by sin.source.kernel.org (Postfix) with ESMTPS id 1BA24CE20F3 for ; Thu, 18 Aug 2022 16:14:11 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id E7A0DC433B5; Thu, 18 Aug 2022 16:14:08 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1660839249; bh=0FaXdO6sfrfgK53welgzflSejRlsGfs8OGtOLtKDMz0=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=rhNc45uJMrV6S7gd1FopprJA05XudQLG2G5s/XB6w3LkY3IxBODFOrYsu9UqDgoxt atm4CEV2fSvMZJrIId7PZRCe/ytuALSHLoQ1SNJcK5IWIRkcXwqNFre0/9VXVyG0i6 AVHJuyCuYCTIl9crabfilmE2/f19tymrYYFgaekJnRmVNHQko90YIkGTVtX5sYdCCE S0CJGg560JUpCLSeb9+ll2iHdAzb03vJybKOQND2PmsM2wEC8nzwW0bpH2g3vhYRCO K2KtIW1BRICwPyai9/qNMxb9CFSAmng3dtGXHwXc73o9GPOpnWur4MMijSXQmR643a BRIvCP7Fal5Zw== From: Josh Poimboeuf To: x86@kernel.org Cc: linux-kernel@vger.kernel.org, Pengfei Xu , "Yang, Weijiang" , "Su, Heng" , Peter Zijlstra , Paolo Bonzini Subject: [PATCH 1/3] x86/ibt, objtool: Add IBT_NOSEAL() Date: Thu, 18 Aug 2022 08:53:41 -0700 Message-Id: <5b86c2e3fff1f1e1f3fda126c315819205eb9d20.1660837839.git.jpoimboe@kernel.org> X-Mailer: git-send-email 2.37.2 In-Reply-To: References: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Type: text/plain; charset="utf-8" Add a macro which prevents a function from getting sealed if there are no compile-time references to it. Signed-off-by: Josh Poimboeuf --- arch/x86/include/asm/ibt.h | 10 ++++++++++ tools/objtool/check.c | 3 ++- 2 files changed, 12 insertions(+), 1 deletion(-) diff --git a/arch/x86/include/asm/ibt.h b/arch/x86/include/asm/ibt.h index 689880eca9ba..372e8eee6e02 100644 --- a/arch/x86/include/asm/ibt.h +++ b/arch/x86/include/asm/ibt.h @@ -31,6 +31,16 @@ =20 #define __noendbr __attribute__((nocf_check)) =20 +/* + * Create a dummy function pointer reference to prevent objtool from marki= ng + * the function as needing to be "sealed" (i.e. ENDBR converted to NOP by + * apply_ibt_endbr()). + */ +#define IBT_NOSEAL(fname) \ + ".pushsection .discard.ibt_endbr_noseal\n\t" \ + _ASM_PTR fname "\n\t" \ + ".popsection\n\t" + static inline __attribute_const__ u32 gen_endbr(void) { u32 endbr; diff --git a/tools/objtool/check.c b/tools/objtool/check.c index 0cec74da7ffe..91678252a9b6 100644 --- a/tools/objtool/check.c +++ b/tools/objtool/check.c @@ -4096,7 +4096,8 @@ static int validate_ibt(struct objtool_file *file) * These sections can reference text addresses, but not with * the intent to indirect branch to them. */ - if (!strncmp(sec->name, ".discard", 8) || + if ((!strncmp(sec->name, ".discard", 8) && + strcmp(sec->name, ".discard.ibt_endbr_noseal")) || !strncmp(sec->name, ".debug", 6) || !strcmp(sec->name, ".altinstructions") || !strcmp(sec->name, ".ibt_endbr_seal") || --=20 2.37.2 From nobody Fri Apr 10 21:50:56 2026 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 9640BC32772 for ; Thu, 18 Aug 2022 16:14:22 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1344240AbiHRQOT (ORCPT ); Thu, 18 Aug 2022 12:14:19 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:33818 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1344076AbiHRQON (ORCPT ); Thu, 18 Aug 2022 12:14:13 -0400 Received: from ams.source.kernel.org (ams.source.kernel.org [IPv6:2604:1380:4601:e00::1]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id E5B8147BBA for ; Thu, 18 Aug 2022 09:14:12 -0700 (PDT) Received: from smtp.kernel.org (relay.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by ams.source.kernel.org (Postfix) with ESMTPS id 8CCADB8221C for ; Thu, 18 Aug 2022 16:14:11 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 9C553C433C1; Thu, 18 Aug 2022 16:14:09 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1660839250; bh=7q/WN5I1Z0IaU6xaTUCpYoDjYa2773Zrh8H7eHNY+7A=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=nllylCRK/qNurG8PvM007baAU/nZfmxuCCjdMwcoghZLIjUbQFYF+U2JG+GLa8JyF lxmRv7rTZ8VZgJN16/p9dbf4XbFxABj9XwY0Y6G9jVj1lgaiPT7H8GbE0GJh1uOKbx oUNzdr86tkthx5bVhNstAOhtRddn6ssL3PeqIhRarn7/o6SOF9V5W40UNee9AuG+ZE hrBWDRhWs1W+oCeafEZkIjhge3mG32ZBUp+z6fUiuWLGp+1+c6G3r5ClLZ7fBwtszH JDEObp73q21pKLfLhnLuvNkR288ayjPTosSWUJPOYKVV7YkWOS0ktADnzebS8KR8Mo +UuwCDWqFFShg== From: Josh Poimboeuf To: x86@kernel.org Cc: linux-kernel@vger.kernel.org, Pengfei Xu , "Yang, Weijiang" , "Su, Heng" , Peter Zijlstra , Paolo Bonzini Subject: [PATCH 2/3] x86/kvm: Simplify FOP_SETCC() Date: Thu, 18 Aug 2022 08:53:42 -0700 Message-Id: <7c13d94d1a775156f7e36eed30509b274a229140.1660837839.git.jpoimboe@kernel.org> X-Mailer: git-send-email 2.37.2 In-Reply-To: References: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Type: text/plain; charset="utf-8" SETCC_ALIGN and FOP_ALIGN are both 16. Remove the special casing for FOP_SETCC() and just make it a normal fastop. Signed-off-by: Josh Poimboeuf --- arch/x86/kvm/emulate.c | 23 ++++------------------- 1 file changed, 4 insertions(+), 19 deletions(-) diff --git a/arch/x86/kvm/emulate.c b/arch/x86/kvm/emulate.c index b4eeb7c75dfa..205d566ebd72 100644 --- a/arch/x86/kvm/emulate.c +++ b/arch/x86/kvm/emulate.c @@ -446,27 +446,12 @@ static int fastop(struct x86_emulate_ctxt *ctxt, fast= op_t fop); FOP_END =20 /* Special case for SETcc - 1 instruction per cc */ - -/* - * Depending on .config the SETcc functions look like: - * - * ENDBR [4 bytes; CONFIG_X86_KERNEL_IBT] - * SETcc %al [3 bytes] - * RET | JMP __x86_return_thunk [1,5 bytes; CONFIG_RETHUNK] - * INT3 [1 byte; CONFIG_SLS] - */ -#define SETCC_ALIGN 16 - #define FOP_SETCC(op) \ - ".align " __stringify(SETCC_ALIGN) " \n\t" \ - ".type " #op ", @function \n\t" \ - #op ": \n\t" \ - ASM_ENDBR \ + FOP_FUNC(op) \ #op " %al \n\t" \ - __FOP_RET(#op) \ - ".skip " __stringify(SETCC_ALIGN) " - (.-" #op "), 0xcc \n\t" + FOP_RET(op) =20 -__FOP_START(setcc, SETCC_ALIGN) +FOP_START(setcc) FOP_SETCC(seto) FOP_SETCC(setno) FOP_SETCC(setc) @@ -1079,7 +1064,7 @@ static int em_bsr_c(struct x86_emulate_ctxt *ctxt) static __always_inline u8 test_cc(unsigned int condition, unsigned long fl= ags) { u8 rc; - void (*fop)(void) =3D (void *)em_setcc + SETCC_ALIGN * (condition & 0xf); + void (*fop)(void) =3D (void *)em_setcc + FASTOP_SIZE * (condition & 0xf); =20 flags =3D (flags & EFLAGS_MASK) | X86_EFLAGS_IF; asm("push %[flags]; popf; " CALL_NOSPEC --=20 2.37.2 From nobody Fri Apr 10 21:50:56 2026 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 9946BC32774 for ; Thu, 18 Aug 2022 16:14:25 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1344336AbiHRQOW (ORCPT ); Thu, 18 Aug 2022 12:14:22 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:33856 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1344144AbiHRQOO (ORCPT ); Thu, 18 Aug 2022 12:14:14 -0400 Received: from ams.source.kernel.org (ams.source.kernel.org [145.40.68.75]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 93D3F474DC for ; Thu, 18 Aug 2022 09:14:13 -0700 (PDT) Received: from smtp.kernel.org (relay.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by ams.source.kernel.org (Postfix) with ESMTPS id 2E393B8221D for ; Thu, 18 Aug 2022 16:14:12 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 5C2F1C43140; Thu, 18 Aug 2022 16:14:10 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1660839250; bh=LklsORKu6eLoWSUy8Uv/91SX4T5e2aOeczaluthpBWw=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=QsLszNJ3f2L4nbyCqD+5T07lURvGYi6AVA/5VggLdyEV1znUd7o2KRiNvSKgubRPF nUOYJT7odSMOSfU7PwwLh8FLzhTn1ZiaGtsfyfbjc3ziYqk1Henxd0ayZ4MRhKwZXr LbyJH7Z2KbA8ImR+YhkLDQ2NLZk8v53CQRUtEGty3e98Nac8Cp6OblMaOj4Gdv+BYB Y6cO4/kxUgInyOR8eEmYSaijciZUak64MSmrapVdjQ0U5xW2RE9KJIqyIQ8d8RR/47 pECRO4NzhNWnVIs3GxzYpkZKc2JmexHOwJ5TAL/7ynmPSXbtjCEsCqMcbs1Iaiwczw 1yi9o5fJwAbhQ== From: Josh Poimboeuf To: x86@kernel.org Cc: linux-kernel@vger.kernel.org, Pengfei Xu , "Yang, Weijiang" , "Su, Heng" , Peter Zijlstra , Paolo Bonzini Subject: [PATCH 3/3] x86/kvm: Fix "missing ENDBR" BUG for fastop functions Date: Thu, 18 Aug 2022 08:53:43 -0700 Message-Id: <0d4116f90e9d0c1b754bb90c585e6f0415a1c508.1660837839.git.jpoimboe@kernel.org> X-Mailer: git-send-email 2.37.2 In-Reply-To: References: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Type: text/plain; charset="utf-8" The following BUG was reported: traps: Missing ENDBR: andw_ax_dx+0x0/0x10 [kvm] ------------[ cut here ]------------ kernel BUG at arch/x86/kernel/traps.c:253! invalid opcode: 0000 [#1] PREEMPT SMP NOPTI asm_exc_control_protection+0x2b/0x30 RIP: 0010:andw_ax_dx+0x0/0x10 [kvm] Code: c3 cc cc cc cc 0f 1f 44 00 00 66 0f 1f 00 48 19 d0 c3 cc cc cc cc 0f 1f 40 00 f3 0f 1e fa 20 d0 c3 cc cc cc cc 0f 1f 44 00 00 <66> 0f 1f 00 66 21 d0 c3 cc cc cc cc 0f 1f 40 00 66 0f 1f 00 21 d0 ? andb_al_dl+0x10/0x10 [kvm] ? fastop+0x5d/0xa0 [kvm] x86_emulate_insn+0x822/0x1060 [kvm] x86_emulate_instruction+0x46f/0x750 [kvm] complete_emulated_mmio+0x216/0x2c0 [kvm] kvm_arch_vcpu_ioctl_run+0x604/0x650 [kvm] kvm_vcpu_ioctl+0x2f4/0x6b0 [kvm] ? wake_up_q+0xa0/0xa0 The BUG occurred because the ENDBR in the andw_ax_dx() fastop function had been incorrectly "sealed" (converted to a NOP) by apply_ibt_endbr(). Objtool marked it to be sealed because KVM has no compile-time references to the function. Instead KVM calculates its address at runtime. Prevent objtool from annotating fastop functions as sealable by creating throwaway dummy compile-time references to the functions. Fixes: 6649fa876da4 ("x86/ibt,kvm: Add ENDBR to fastops") Reported-by: Pengfei Xu Debugged-by: Peter Zijlstra Signed-off-by: Josh Poimboeuf --- arch/x86/kvm/emulate.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/arch/x86/kvm/emulate.c b/arch/x86/kvm/emulate.c index 205d566ebd72..f092c54d1a2f 100644 --- a/arch/x86/kvm/emulate.c +++ b/arch/x86/kvm/emulate.c @@ -326,7 +326,8 @@ static int fastop(struct x86_emulate_ctxt *ctxt, fastop= _t fop); ".align " __stringify(FASTOP_SIZE) " \n\t" \ ".type " name ", @function \n\t" \ name ":\n\t" \ - ASM_ENDBR + ASM_ENDBR \ + IBT_NOSEAL(name) =20 #define FOP_FUNC(name) \ __FOP_FUNC(#name) --=20 2.37.2