Received: by 2002:a05:7412:d8a:b0:e2:908c:2ebd with SMTP id b10csp32584rdg; Wed, 11 Oct 2023 19:47:58 -0700 (PDT) X-Google-Smtp-Source: AGHT+IGF8DyIG4l1JwAmZZ/5l0aSLaKn8EtQ/MAOoL7dH5ZNiXQbCPULARC1S57C/SKRzPqLz7h3 X-Received: by 2002:a05:6a20:938b:b0:156:851e:b167 with SMTP id x11-20020a056a20938b00b00156851eb167mr23443491pzh.44.1697078877989; Wed, 11 Oct 2023 19:47:57 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1697078877; cv=none; d=google.com; s=arc-20160816; b=OK5GVTCUfPiSrYWpDjzbZxPyUPpHBwybTwNjhumUFKHt1wE5w7sIhJcTGNqE/2Frwp CmeQwy2or3QDkSHEckgDiq7Mgy6gjemwX/lr8sUtGSaiFLKA9L34ePdZwgJv4Jm2Txhv mIQK2CNDpFsOn1LB0JsXvmtkNCOc8CfrfGd1YOlauNkUp5bZfPhb8JQof4r01yF0qdJb lnzlG+XMgSyl4/X7Wg33CuRnbvBMIl/Dw4A+rT0Gq84gVvbqxu+AxAglmtEvBEJ7pZV2 wt/y+gkPGIb0xwCpXCI86mfLmVj+FpLvYvZpxlXyHx3zTDbWecdAZunTt5Vq61YrJYxs dGcw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:in-reply-to:content-disposition:mime-version :references:message-id:subject:cc:to:from:date:dkim-signature; bh=AQuzfbD0Khk5x5KuLO16sQu2sqt/lovedljOZqndKJg=; fh=GfP2fXubZ1Wn/83gcagvnbPSZkRsv/t7CztUMqA9EH8=; b=mbXo3Wbiy1GtBEIEpywWmMKIxLufwdgX89qvzplE92gHv26R27dqdqPqGBX/JRpo8B aHagRSO60poFMb9raJOZpzmkvXYiR2Mei6FV0FUWMaNsGw3pJoBKYwy3nU9/venAESPu xGdhNAVitPluMqcZqXNyJGjjWKFFyeGfKOb5q7/4WTkwOKqREtXbCV6Wj46qHpkxBbbt uyc5F1xPNe5zlBtUXYvSHUxHiBNGGvMMPDsKGvC0adMdV9Xq8i0mLctAmx2D1SWjvr7v SJrBKiwXvWfaC92YZKpjRZNMlQQ32irlGoJxubTwWFsJzWscaUweF+I6w7sXLCYpb0zm Qdhw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=eL+erSy7; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.32 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Return-Path: Received: from agentk.vger.email (agentk.vger.email. [23.128.96.32]) by mx.google.com with ESMTPS id nu1-20020a17090b1b0100b0027d113631f0si1290646pjb.24.2023.10.11.19.47.57 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 11 Oct 2023 19:47:57 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.32 as permitted sender) client-ip=23.128.96.32; Authentication-Results: mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=eL+erSy7; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.32 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Received: from out1.vger.email (depot.vger.email [IPv6:2620:137:e000::3:0]) by agentk.vger.email (Postfix) with ESMTP id B64C08042389; Wed, 11 Oct 2023 19:47:55 -0700 (PDT) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.10 at agentk.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1376748AbjJLCrm (ORCPT + 99 others); Wed, 11 Oct 2023 22:47:42 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:35220 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S233040AbjJLCrl (ORCPT ); Wed, 11 Oct 2023 22:47:41 -0400 Received: from smtp.kernel.org (relay.kernel.org [52.25.139.140]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 58B50A4 for ; Wed, 11 Oct 2023 19:47:40 -0700 (PDT) Received: by smtp.kernel.org (Postfix) with ESMTPSA id A9EC2C433C8; Thu, 12 Oct 2023 02:47:39 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1697078860; bh=vhaHa8oKbBL75azKtpMU5ef0xkRutZtJTgdLyMCYYkg=; h=Date:From:To:Cc:Subject:References:In-Reply-To:From; b=eL+erSy7d10Jqji3GPYgAwTz6KfGrwSZ38v1Yhjdgos3aZ4jVE2EFku1x6pGc1NVK 3kwMcM9THnC6qTF9iCJBOdIK50UH8tciwpeNXAc44rGz/m5PDFplWN8E9Msoxt9y34 a5+Z+jVf087YhX+BNBInV01ed+EZGAsrvopZOr0aNGaw5S9Dyu8TLnKC0Y1n2Gfma+ fSBqNp5Mqte4siSpugopi9qfkLvhjWt/uskk0IKX/3NMc+PG0RiKs7Jpow89q6E3Gt /BmFvA6mCfCwCnoIaiDXceOYBY5C006Es0IL1yNHk5i+xA574KO0hANbo/HOFJE+MD QKNJurnGQ+6bA== Date: Wed, 11 Oct 2023 19:47:37 -0700 From: Josh Poimboeuf To: Peter Zijlstra Cc: Borislav Petkov , David Kaplan , x86@kernel.org, luto@kernel.org, linux-kernel@vger.kernel.org Subject: [PATCH v2] objtool: Fix return thunk patching in retpolines Message-ID: <20231012024737.eg5phclogp67ik6x@treble> References: <20231010171020.462211-2-david.kaplan@amd.com> <20231010174833.GG14330@noisy.programming.kicks-ass.net> <20231010195721.p5pb273kevg7ydxz@treble> <20231010200429.GIZSWuTWSUM9aId7a6@fat_crate.local> <20231010201912.7pjksbparssqu34k@treble> <20231010212254.ypk2wdogno55shit@treble> <20231011074142.GK14330@noisy.programming.kicks-ass.net> <20231011162843.grv6kixw4ides6uw@treble> <20231011223513.GH6307@noisy.programming.kicks-ass.net> <20231012022758.lf62lgf5jx5xrno7@treble> MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Disposition: inline In-Reply-To: <20231012022758.lf62lgf5jx5xrno7@treble> X-Spam-Status: No, score=-1.2 required=5.0 tests=DKIMWL_WL_HIGH,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,MAILING_LIST_MULTI, SPF_HELO_NONE,SPF_PASS autolearn=unavailable autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on agentk.vger.email Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-Greylist: Sender passed SPF test, not delayed by milter-greylist-4.6.4 (agentk.vger.email [0.0.0.0]); Wed, 11 Oct 2023 19:47:55 -0700 (PDT) With CONFIG_RETHUNK enabled, the compiler replaces every RET with a tail call to a return thunk ('JMP __x86_return_thunk'). Objtool annotates all such return sites so they can be patched during boot by apply_returns(). The implementation of __x86_return_thunk() is just a bare RET. It's only meant to be used temporarily until apply_returns() patches all return sites with either a JMP to another return thunk or an actual RET. The following commit e92626af3234 ("x86/retpoline: Remove .text..__x86.return_thunk section") retpolines broke objtool's detection of return sites in retpolines. Since retpolines and return thunks are now in the same section, the compiler no longer uses relocations for the intra-section jumps between the retpolines and the return thunk, causing objtool to overlook them. As a result, none of the retpolines' return sites get patched. Each one stays at 'JMP __x86_return_thunk', effectively a bare RET. Fix it by teaching objtool to detect when a non-relocated jump target is a return thunk (or retpoline). Fixes: e92626af3234 ("x86/retpoline: Remove .text..__x86.return_thunk section") Reported-by: David Kaplan Signed-off-by: Josh Poimboeuf --- tools/objtool/check.c | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/tools/objtool/check.c b/tools/objtool/check.c index e308d1ba664e..e94756e09ca9 100644 --- a/tools/objtool/check.c +++ b/tools/objtool/check.c @@ -1610,6 +1610,22 @@ static int add_jump_destinations(struct objtool_file *file) return -1; } + /* + * An intra-TU jump in retpoline.o might not have a relocation + * for its jump dest, in which case the above + * add_{retpoline,return}_call() didn't happen. + */ + if (jump_dest->sym && jump_dest->offset == jump_dest->sym->offset) { + if (jump_dest->sym->retpoline_thunk) { + add_retpoline_call(file, insn); + continue; + } + if (jump_dest->sym->return_thunk) { + add_return_call(file, insn, true); + continue; + } + } + /* * Cross-function jump. */ -- 2.41.0