Received: by 2002:ac0:da4c:0:0:0:0:0 with SMTP id a12csp456729imi; Fri, 22 Jul 2022 02:47:42 -0700 (PDT) X-Google-Smtp-Source: AGRyM1s5L4z8O2imccoGcgmCFJkG3lxrDBaOd/X+uAc7E+k0Mu7PMHCuN8ETDRWkDDXmZrXijkx/ X-Received: by 2002:a63:210:0:b0:419:c604:2c2b with SMTP id 16-20020a630210000000b00419c6042c2bmr2488826pgc.190.1658483261807; Fri, 22 Jul 2022 02:47:41 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1658483261; cv=none; d=google.com; s=arc-20160816; b=InHutacO8Qn1gL0zRkTPI8vQP0iQEpqs4WtUlIc+gCwdsTmiHramRNEyccoC1ve3iJ ICTCKTr5nbSL/AiTnLLIqZND7OhcdyLLoMmPyTD2t1gD6bilvws5um4OdxIlaoDPhFQE ZzBJbJ5MZDBXmyqKTcPFcS22HmI8DpmkA8r13+/synnu36duEKM0fQ3/4gIcUaK4CvJH GNOinEs3reaFVpMHaJ4OdVpLygmGnGwcdVZ+UAqYquSQ3hBDLLxf3QaynkJDSfUCbVgr UmRynxv/wr5MdxNSSdukkY6fFUeUE8rTuE2fi5JYJhD6Cuszkxr51gPFE6o7f8kv4BpP cyaw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :user-agent:references:in-reply-to:message-id:date:subject:cc:to :from:dkim-signature; bh=xVyWy1wy+xvzKcRZ5qDRKicUJu1WmcoxVbTZTVaj6mc=; b=b/mRgND8Id5Ou7ueTXyHBW6lyYeJ+meP0Kekr1bqiUQiXQol/pEZEst3uKFtuieVhO 8y1Hjzz5PnBT8ES5kEwYCVU9YC4nqrE/ETn1Zwk6RbyEv0OBhuMwTUSzI3eomNAUnpGE 6T1nxEaQJP997084OzEv3zUiK7Rey1vcth25+s48t9vVHTiJvTNfRR7QVx7TWmCYVDRB qEdk0SGErVgQtHV06LXIRvaryb2Na8Lb+ZXFlTMIpNZY8o/VEUHQn42J/D2zhO9DB4uO J0vBxkg2Dmgua+AqsYuUkF/GVOShzHn8lPuPFddhNOronUlFddeA/QX/mrpRLBPiEjag wSqQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=YmjFwnmL; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linuxfoundation.org Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id p14-20020a170902780e00b0016be96cf31bsi4878502pll.273.2022.07.22.02.47.26; Fri, 22 Jul 2022 02:47:41 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=YmjFwnmL; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linuxfoundation.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S235738AbiGVJTk (ORCPT + 99 others); Fri, 22 Jul 2022 05:19:40 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:50876 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S235815AbiGVJSj (ORCPT ); Fri, 22 Jul 2022 05:18:39 -0400 Received: from dfw.source.kernel.org (dfw.source.kernel.org [139.178.84.217]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 08685B9A2E; Fri, 22 Jul 2022 02:13:04 -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 dfw.source.kernel.org (Postfix) with ESMTPS id 8E71B61F4F; Fri, 22 Jul 2022 09:13:04 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 918CBC341C7; Fri, 22 Jul 2022 09:13:03 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=linuxfoundation.org; s=korg; t=1658481184; bh=+24eNmj7JBW2Luyx+OMFPP6OZKm+EOiinyIhDs6ctw8=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=YmjFwnmLxStpn/Ve4gBAP09B09qLjsAnV35KFQce9LA4lV5wDEpm6GjqgYEgPz+t1 9PKS8c4vcpKjn+pJMQjdvbAIrx0l/oia8hkRBOzyAafa6svDH6b+1FBIqFLh1dtLth PN33Vno/cU+fXJ6SkZxZQn89lWNNrE2XlnPiu0SE= From: Greg Kroah-Hartman To: linux-kernel@vger.kernel.org Cc: Greg Kroah-Hartman , stable@vger.kernel.org, "Peter Zijlstra (Intel)" , Borislav Petkov , Josh Poimboeuf , Alexei Starovoitov , Thadeu Lima de Souza Cascardo Subject: [PATCH 5.15 16/89] x86/alternative: Handle Jcc __x86_indirect_thunk_\reg Date: Fri, 22 Jul 2022 11:10:50 +0200 Message-Id: <20220722091134.281850401@linuxfoundation.org> X-Mailer: git-send-email 2.37.1 In-Reply-To: <20220722091133.320803732@linuxfoundation.org> References: <20220722091133.320803732@linuxfoundation.org> User-Agent: quilt/0.66 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit X-Spam-Status: No, score=-7.8 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_HI, SPF_HELO_NONE,SPF_PASS autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org From: Peter Zijlstra commit 2f0cbb2a8e5bbf101e9de118fc0eb168111a5e1e upstream. Handle the rare cases where the compiler (clang) does an indirect conditional tail-call using: Jcc __x86_indirect_thunk_\reg For the !RETPOLINE case this can be rewritten to fit the original (6 byte) instruction like: Jncc.d8 1f JMP *%\reg NOP 1: Signed-off-by: Peter Zijlstra (Intel) Reviewed-by: Borislav Petkov Acked-by: Josh Poimboeuf Tested-by: Alexei Starovoitov Link: https://lore.kernel.org/r/20211026120310.296470217@infradead.org Signed-off-by: Thadeu Lima de Souza Cascardo Signed-off-by: Greg Kroah-Hartman --- arch/x86/kernel/alternative.c | 40 ++++++++++++++++++++++++++++++++++++---- 1 file changed, 36 insertions(+), 4 deletions(-) --- a/arch/x86/kernel/alternative.c +++ b/arch/x86/kernel/alternative.c @@ -393,7 +393,8 @@ static int emit_indirect(int op, int reg static int patch_retpoline(void *addr, struct insn *insn, u8 *bytes) { retpoline_thunk_t *target; - int reg, i = 0; + int reg, ret, i = 0; + u8 op, cc; target = addr + insn->length + insn->immediate.value; reg = target - __x86_indirect_thunk_array; @@ -407,9 +408,36 @@ static int patch_retpoline(void *addr, s if (cpu_feature_enabled(X86_FEATURE_RETPOLINE)) return -1; - i = emit_indirect(insn->opcode.bytes[0], reg, bytes); - if (i < 0) - return i; + op = insn->opcode.bytes[0]; + + /* + * Convert: + * + * Jcc.d32 __x86_indirect_thunk_\reg + * + * into: + * + * Jncc.d8 1f + * JMP *%\reg + * NOP + * 1: + */ + /* Jcc.d32 second opcode byte is in the range: 0x80-0x8f */ + if (op == 0x0f && (insn->opcode.bytes[1] & 0xf0) == 0x80) { + cc = insn->opcode.bytes[1] & 0xf; + cc ^= 1; /* invert condition */ + + bytes[i++] = 0x70 + cc; /* Jcc.d8 */ + bytes[i++] = insn->length - 2; /* sizeof(Jcc.d8) == 2 */ + + /* Continue as if: JMP.d32 __x86_indirect_thunk_\reg */ + op = JMP32_INSN_OPCODE; + } + + ret = emit_indirect(op, reg, bytes + i); + if (ret < 0) + return ret; + i += ret; for (; i < insn->length;) bytes[i++] = BYTES_NOP1; @@ -443,6 +471,10 @@ void __init_or_module noinline apply_ret case JMP32_INSN_OPCODE: break; + case 0x0f: /* escape */ + if (op2 >= 0x80 && op2 <= 0x8f) + break; + fallthrough; default: WARN_ON_ONCE(1); continue;