From nobody Fri Sep 12 18:08:59 2025 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 A9B05C636CC for ; Wed, 8 Feb 2023 17:24:35 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230471AbjBHRYe (ORCPT ); Wed, 8 Feb 2023 12:24:34 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:55502 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231820AbjBHRYK (ORCPT ); Wed, 8 Feb 2023 12:24:10 -0500 Received: from desiato.infradead.org (desiato.infradead.org [IPv6:2001:8b0:10b:1:d65d:64ff:fe57:4e05]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 634424F84E for ; Wed, 8 Feb 2023 09:23:59 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=infradead.org; s=desiato.20200630; h=Content-Type:MIME-Version:References: Subject:Cc:To:From:Date:Message-ID:Sender:Reply-To:Content-Transfer-Encoding: Content-ID:Content-Description:In-Reply-To; bh=rcZCmoIUcgLAbnZrH9bmcKFtR9+VWwtjQJpHM8y0dwU=; b=ZwzycoRhBe720t52V6JFKc/80J TLYRBAd2Q1EQRx2k7IQPbUI/JnFkzI6XV7zjMBdByLB6VTtVH1c7PODLaP1ce3IX/YqJiNPzAb90N HP5WsGY45Q5ADfBGiFbanS/iqb+F7jr0/suucQu2rl4Wcrpi2dlUn39Uw7CJ+dDaCrVpM3Hxeh5zv fVFcc3PqO0Bc4QquCSX2jFBrnb87rVzkLoYomWyJoKG8nt9J20aBl3PPbL+QsUswItCzcpQPJR4XE ymKSNI0RgVkyFlOxWVWLPBwwHNIQxO7xW8AK23aFuWTa0P+I1WfWl0YKj8GpQgVzq+vvik5hySYu5 qOLrn9ug==; Received: from j130084.upc-j.chello.nl ([24.132.130.84] helo=noisy.programming.kicks-ass.net) by desiato.infradead.org with esmtpsa (Exim 4.96 #2 (Red Hat Linux)) id 1pPoA5-007Vvl-14; Wed, 08 Feb 2023 17:23:13 +0000 Received: from hirez.programming.kicks-ass.net (hirez.programming.kicks-ass.net [192.168.1.225]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits)) (Client did not present a certificate) by noisy.programming.kicks-ass.net (Postfix) with ESMTPS id 49753300BE3; Wed, 8 Feb 2023 18:23:50 +0100 (CET) Received: by hirez.programming.kicks-ass.net (Postfix, from userid 0) id 1ABAD23D8CFB3; Wed, 8 Feb 2023 18:23:50 +0100 (CET) Message-ID: <20230208172245.711471461@infradead.org> User-Agent: quilt/0.66 Date: Wed, 08 Feb 2023 18:18:03 +0100 From: Peter Zijlstra To: x86@kernel.org, jpoimboe@redhat.com, linux@weissschuh.net Cc: linux-kernel@vger.kernel.org, peterz@infradead.org Subject: [PATCH 07/10] objtool: Fix overlapping alternatives References: <20230208171756.898991570@infradead.org> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Things like ALTERNATIVE_{2,3}() generate multiple alternatives on the same place, objtool would override the first orig_alt_group with the second (or third), failing to check the CFI among all the different variants. Signed-off-by: Peter Zijlstra (Intel) --- tools/objtool/check.c | 69 +++++++++++++++++++++++++++++++--------------= ----- 1 file changed, 43 insertions(+), 26 deletions(-) --- a/tools/objtool/check.c +++ b/tools/objtool/check.c @@ -1732,36 +1732,49 @@ static int handle_group_alt(struct objto struct instruction *orig_insn, struct instruction **new_insn) { - struct instruction *last_orig_insn, *last_new_insn =3D NULL, *insn, *nop = =3D NULL; + struct instruction *last_new_insn =3D NULL, *insn, *nop =3D NULL; struct alt_group *orig_alt_group, *new_alt_group; unsigned long dest_off; =20 - - orig_alt_group =3D malloc(sizeof(*orig_alt_group)); + orig_alt_group =3D orig_insn->alt_group; if (!orig_alt_group) { - WARN("malloc failed"); - return -1; - } - orig_alt_group->cfi =3D calloc(special_alt->orig_len, - sizeof(struct cfi_state *)); - if (!orig_alt_group->cfi) { - WARN("calloc failed"); - return -1; - } + struct instruction *last_orig_insn =3D NULL; =20 - last_orig_insn =3D NULL; - insn =3D orig_insn; - sec_for_each_insn_from(file, insn) { - if (insn->offset >=3D special_alt->orig_off + special_alt->orig_len) - break; + orig_alt_group =3D malloc(sizeof(*orig_alt_group)); + if (!orig_alt_group) { + WARN("malloc failed"); + return -1; + } + orig_alt_group->cfi =3D calloc(special_alt->orig_len, + sizeof(struct cfi_state *)); + if (!orig_alt_group->cfi) { + WARN("calloc failed"); + return -1; + } =20 - insn->alt_group =3D orig_alt_group; - last_orig_insn =3D insn; - } - orig_alt_group->orig_group =3D NULL; - orig_alt_group->first_insn =3D orig_insn; - orig_alt_group->last_insn =3D last_orig_insn; + insn =3D orig_insn; + sec_for_each_insn_from(file, insn) { + if (insn->offset >=3D special_alt->orig_off + special_alt->orig_len) + break; =20 + insn->alt_group =3D orig_alt_group; + last_orig_insn =3D insn; + } + orig_alt_group->orig_group =3D NULL; + orig_alt_group->first_insn =3D orig_insn; + orig_alt_group->last_insn =3D last_orig_insn; + } else { + if (orig_alt_group->last_insn->offset + orig_alt_group->last_insn->len - + orig_alt_group->first_insn->offset !=3D special_alt->orig_len) { + WARN_FUNC("weirdly overlapping alternative! %ld !=3D %d", + orig_insn->sec, orig_insn->offset, + orig_alt_group->last_insn->offset + + orig_alt_group->last_insn->len - + orig_alt_group->first_insn->offset, + special_alt->orig_len); + return -1; + } + } =20 new_alt_group =3D malloc(sizeof(*new_alt_group)); if (!new_alt_group) { @@ -1836,7 +1849,7 @@ static int handle_group_alt(struct objto =20 dest_off =3D arch_jump_destination(insn); if (dest_off =3D=3D special_alt->new_off + special_alt->new_len) { - insn->jump_dest =3D next_insn_same_sec(file, last_orig_insn); + insn->jump_dest =3D next_insn_same_sec(file, orig_alt_group->last_insn); if (!insn->jump_dest) { WARN_FUNC("can't find alternative jump destination", insn->sec, insn->offset); @@ -3214,8 +3227,12 @@ static int propagate_alt_cfi(struct objt alt_cfi[group_off] =3D insn->cfi; } else { if (cficmp(alt_cfi[group_off], insn->cfi)) { - WARN_FUNC("stack layout conflict in alternatives", - insn->sec, insn->offset); + struct alt_group *orig_group =3D insn->alt_group->orig_group ?: insn->a= lt_group; + struct instruction *orig =3D orig_group->first_insn; + char *where =3D offstr(insn->sec, insn->offset); + WARN_FUNC("stack layout conflict in alternatives: %s", + orig->sec, orig->offset, where); + free(where); return -1; } }