Received: by 2002:ad5:4acb:0:0:0:0:0 with SMTP id n11csp4703715imw; Tue, 12 Jul 2022 12:43:17 -0700 (PDT) X-Google-Smtp-Source: AGRyM1t64NtywrPb29/yBYAl2QSbx/2NpNQaZ7bmFdPX4A/EaI0PE3nA/J6DmgHTgKSdIP7OZqvs X-Received: by 2002:a05:6402:3311:b0:43a:8714:d486 with SMTP id e17-20020a056402331100b0043a8714d486mr33937901eda.136.1657654997310; Tue, 12 Jul 2022 12:43:17 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1657654997; cv=none; d=google.com; s=arc-20160816; b=NJ4tLzr/6Pdv8qbkJe3OXHMvLDE5lGSqutXzsi0SmY5nt56PokxafhIjaEmN5lke50 NE86opZe+gaDXnMqk2JHBUc0B1pfMwBNQxp9XSMSGuahwAvui1Bdj4RMhPlNu68aP5OX Lw7PT4I4HHaOCegwnRAmrO6rkeOAP9VuJygE7Xw/Aoje1jYPk4ih733UWUkP0r0FbY9s AJs3TNHFo36WzMPG8u+ww8HVH4YCGpZaMfJhQCZIH5DD+QeWPkOlybf4ngVuYZBFjJ8n Bocl2Cz+DQRverXU4Yt+Zx3rtg7ss7PUD/FLTPwDcU9zsIxNRgsrPhR28IF6Nm2gE5XP Gccg== 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=9UyATRqju21NfxXojAWkxCZpqWee7g8lV3dfPOyX/Mo=; b=uEuR5Bc9+iqgXCmVnMDOd2Is5mN5jrzkEsqntYWpIzNgwLDIWK0ZoQ/LNq3AaeNEeK 14B90G08sNxAvNgdgAnL4MaG0IZkNlARsbQcBtJqJsM+rlWlW5qRCz18sfLIh47rT543 xB1UW88PYHB/0eggF6Vp0Rtju64lkl8EjIfcK9IEHEJ2xbtHA9L2wOPkxZ+GmUnuKW5t 6kEpAbvKo2js90WIWU0E0Fa+MF8ByT5xnNQAo7Aqzj26pEngJ9K1lc6fVmE6OHxwvtF1 Z3RmSvR8wwYbaflJ83foi1z+hUOE9eRDzmgxCBsRYRdc0kJvqtu2X/KrRLq8JrLXEn8g EDdA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=fivxlIih; 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 k6-20020aa7c386000000b0043a4f6a0127si12546787edq.105.2022.07.12.12.42.49; Tue, 12 Jul 2022 12:43:17 -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=fivxlIih; 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 S234418AbiGLSrJ (ORCPT + 99 others); Tue, 12 Jul 2022 14:47:09 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:46154 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S234151AbiGLSqd (ORCPT ); Tue, 12 Jul 2022 14:46:33 -0400 Received: from ams.source.kernel.org (ams.source.kernel.org [145.40.68.75]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id AEBF9DC89A; Tue, 12 Jul 2022 11:42:52 -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 BC49DB81BC1; Tue, 12 Jul 2022 18:42:50 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 04494C3411C; Tue, 12 Jul 2022 18:42:48 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=linuxfoundation.org; s=korg; t=1657651369; bh=7U7iZoqe4+dtDPPJp4LSzYXjPFIQ+BxwPO0m8HF420E=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=fivxlIihXDFP/t5ZDQIsoHlZgFnbIWcqBlfdHkttoyjauhiQ86IlmkK+03e+hl37m dz5Zvnb3je/USr+DjW4oBauMK9aRJAwl79U8ZXccy0pBP4IZdzYPbTOP/s5SkQgOGn cUD2Xwmq6+8glx5aw49iNNGahVehFynJeQP+zt24= 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 , Ben Hutchings Subject: [PATCH 5.10 056/130] x86/alternative: Handle Jcc __x86_indirect_thunk_\reg Date: Tue, 12 Jul 2022 20:38:22 +0200 Message-Id: <20220712183249.033570990@linuxfoundation.org> X-Mailer: git-send-email 2.37.0 In-Reply-To: <20220712183246.394947160@linuxfoundation.org> References: <20220712183246.394947160@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.7 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,T_SCC_BODY_TEXT_LINE 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: Ben Hutchings 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 @@ -548,7 +548,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; @@ -562,9 +563,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++] = 0x90; @@ -598,6 +626,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;