linux-kernel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: "tip-bot2 for Peter Zijlstra" <tip-bot2@linutronix.de>
To: linux-tip-commits@vger.kernel.org
Cc: "Peter Zijlstra (Intel)" <peterz@infradead.org>,
	Josh Poimboeuf <jpoimboe@kernel.org>,
	Nathan Chancellor <nathan@kernel.org>,
	linux@weissschuh.net, x86@kernel.org,
	linux-kernel@vger.kernel.org
Subject: [tip: objtool/core] objtool: Fix overlapping alternatives
Date: Mon, 13 Feb 2023 11:10:28 -0000	[thread overview]
Message-ID: <167628662854.4906.18315456608077226990.tip-bot2@tip-bot2> (raw)
In-Reply-To: <20230208172245.711471461@infradead.org>

The following commit has been merged into the objtool/core branch of tip:

Commit-ID:     339b90b37d80ff2f4064bf072584031f3b1b5838
Gitweb:        https://git.kernel.org/tip/339b90b37d80ff2f4064bf072584031f3b1b5838
Author:        Peter Zijlstra <peterz@infradead.org>
AuthorDate:    Wed, 08 Feb 2023 18:18:03 +01:00
Committer:     Peter Zijlstra <peterz@infradead.org>
CommitterDate: Mon, 13 Feb 2023 11:26:08 +01:00

objtool: Fix overlapping alternatives

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) <peterz@infradead.org>
Acked-by: Josh Poimboeuf <jpoimboe@kernel.org>
Tested-by: Nathan Chancellor <nathan@kernel.org> # build only
Tested-by: Thomas Weißschuh <linux@weissschuh.net> # compile and run
Link: https://lore.kernel.org/r/20230208172245.711471461@infradead.org
---
 tools/objtool/check.c | 69 ++++++++++++++++++++++++++----------------
 1 file changed, 43 insertions(+), 26 deletions(-)

diff --git a/tools/objtool/check.c b/tools/objtool/check.c
index 6f0adb2..7e9d3d3 100644
--- a/tools/objtool/check.c
+++ b/tools/objtool/check.c
@@ -1744,36 +1744,49 @@ static int handle_group_alt(struct objtool_file *file,
 			    struct instruction *orig_insn,
 			    struct instruction **new_insn)
 {
-	struct instruction *last_orig_insn, *last_new_insn = NULL, *insn, *nop = NULL;
+	struct instruction *last_new_insn = NULL, *insn, *nop = NULL;
 	struct alt_group *orig_alt_group, *new_alt_group;
 	unsigned long dest_off;
 
-
-	orig_alt_group = malloc(sizeof(*orig_alt_group));
+	orig_alt_group = orig_insn->alt_group;
 	if (!orig_alt_group) {
-		WARN("malloc failed");
-		return -1;
-	}
-	orig_alt_group->cfi = calloc(special_alt->orig_len,
-				     sizeof(struct cfi_state *));
-	if (!orig_alt_group->cfi) {
-		WARN("calloc failed");
-		return -1;
-	}
+		struct instruction *last_orig_insn = NULL;
 
-	last_orig_insn = NULL;
-	insn = orig_insn;
-	sec_for_each_insn_from(file, insn) {
-		if (insn->offset >= special_alt->orig_off + special_alt->orig_len)
-			break;
+		orig_alt_group = malloc(sizeof(*orig_alt_group));
+		if (!orig_alt_group) {
+			WARN("malloc failed");
+			return -1;
+		}
+		orig_alt_group->cfi = calloc(special_alt->orig_len,
+					     sizeof(struct cfi_state *));
+		if (!orig_alt_group->cfi) {
+			WARN("calloc failed");
+			return -1;
+		}
 
-		insn->alt_group = orig_alt_group;
-		last_orig_insn = insn;
-	}
-	orig_alt_group->orig_group = NULL;
-	orig_alt_group->first_insn = orig_insn;
-	orig_alt_group->last_insn = last_orig_insn;
+		insn = orig_insn;
+		sec_for_each_insn_from(file, insn) {
+			if (insn->offset >= special_alt->orig_off + special_alt->orig_len)
+				break;
 
+			insn->alt_group = orig_alt_group;
+			last_orig_insn = insn;
+		}
+		orig_alt_group->orig_group = NULL;
+		orig_alt_group->first_insn = orig_insn;
+		orig_alt_group->last_insn = last_orig_insn;
+	} else {
+		if (orig_alt_group->last_insn->offset + orig_alt_group->last_insn->len -
+		    orig_alt_group->first_insn->offset != special_alt->orig_len) {
+			WARN_FUNC("weirdly overlapping alternative! %ld != %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;
+		}
+	}
 
 	new_alt_group = malloc(sizeof(*new_alt_group));
 	if (!new_alt_group) {
@@ -1848,7 +1861,7 @@ static int handle_group_alt(struct objtool_file *file,
 
 		dest_off = arch_jump_destination(insn);
 		if (dest_off == special_alt->new_off + special_alt->new_len) {
-			insn->jump_dest = next_insn_same_sec(file, last_orig_insn);
+			insn->jump_dest = 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);
@@ -3226,8 +3239,12 @@ static int propagate_alt_cfi(struct objtool_file *file, struct instruction *insn
 		alt_cfi[group_off] = 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 = insn->alt_group->orig_group ?: insn->alt_group;
+			struct instruction *orig = orig_group->first_insn;
+			char *where = offstr(insn->sec, insn->offset);
+			WARN_FUNC("stack layout conflict in alternatives: %s",
+				  orig->sec, orig->offset, where);
+			free(where);
 			return -1;
 		}
 	}

  reply	other threads:[~2023-02-13 11:10 UTC|newest]

Thread overview: 37+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-02-08 17:17 [PATCH 00/10] objtool: Honey, I shrunk the instruction Peter Zijlstra
2023-02-08 17:17 ` [PATCH 01/10] objtool: Change arch_decode_instruction() signature Peter Zijlstra
2023-02-13 11:10   ` [tip: objtool/core] " tip-bot2 for Peter Zijlstra
2023-02-23  8:32   ` tip-bot2 for Peter Zijlstra
2023-02-08 17:17 ` [PATCH 02/10] objtool: Make instruction::stack_ops a single-linked list Peter Zijlstra
2023-02-13 11:10   ` [tip: objtool/core] " tip-bot2 for Peter Zijlstra
2023-02-23  8:32   ` tip-bot2 for Peter Zijlstra
2023-02-08 17:17 ` [PATCH 03/10] objtool: Make instruction::alts " Peter Zijlstra
2023-02-13 11:10   ` [tip: objtool/core] " tip-bot2 for Peter Zijlstra
2023-02-23  8:32   ` tip-bot2 for Peter Zijlstra
2023-02-08 17:18 ` [PATCH 04/10] objtool: Shrink instruction::{type,visited} Peter Zijlstra
2023-02-13 11:10   ` [tip: objtool/core] " tip-bot2 for Peter Zijlstra
2023-02-23  8:32   ` tip-bot2 for Peter Zijlstra
2023-02-08 17:18 ` [PATCH 05/10] objtool: Remove instruction::reloc Peter Zijlstra
2023-02-13 11:10   ` [tip: objtool/core] " tip-bot2 for Peter Zijlstra
2023-02-23  8:32   ` tip-bot2 for Peter Zijlstra
2023-02-08 17:18 ` [PATCH 06/10] objtool: Union instruction::{call_dest,jump_table} Peter Zijlstra
2023-02-13 11:10   ` [tip: objtool/core] " tip-bot2 for Peter Zijlstra
2023-02-23  8:32   ` tip-bot2 for Peter Zijlstra
2023-02-08 17:18 ` [PATCH 07/10] objtool: Fix overlapping alternatives Peter Zijlstra
2023-02-13 11:10   ` tip-bot2 for Peter Zijlstra [this message]
2023-02-23  8:32   ` [tip: objtool/core] " tip-bot2 for Peter Zijlstra
2023-02-08 17:18 ` [PATCH 08/10] x86: Fix FILL_RETURN_BUFFER Peter Zijlstra
2023-02-13 11:10   ` [tip: objtool/core] " tip-bot2 for Peter Zijlstra
2023-02-23  8:32   ` tip-bot2 for Peter Zijlstra
2023-02-08 17:18 ` [PATCH 09/10] objtool: Remove instruction::list Peter Zijlstra
2023-02-13 11:10   ` [tip: objtool/core] " tip-bot2 for Peter Zijlstra
2023-02-23  8:32   ` tip-bot2 for Peter Zijlstra
2023-02-08 17:18 ` [PATCH 10/10][HACK] objtool: Shrink reloc Peter Zijlstra
2023-02-08 18:35 ` [PATCH 00/10] objtool: Honey, I shrunk the instruction Nathan Chancellor
2023-02-08 20:22 ` Damian Tometzki
2023-02-09 10:22   ` Peter Zijlstra
2023-02-09 10:56     ` Damian Tometzki
2023-02-09 19:57 ` Josh Poimboeuf
2023-02-10  8:59   ` Peter Zijlstra
2023-02-10 13:47     ` [PATCH 00/10] objtool: Honey, I shrunk the instruction^Wreloc Peter Zijlstra
2023-02-10  1:50 ` [PATCH 00/10] objtool: Honey, I shrunk the instruction Thomas Weißschuh

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=167628662854.4906.18315456608077226990.tip-bot2@tip-bot2 \
    --to=tip-bot2@linutronix.de \
    --cc=jpoimboe@kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-tip-commits@vger.kernel.org \
    --cc=linux@weissschuh.net \
    --cc=nathan@kernel.org \
    --cc=peterz@infradead.org \
    --cc=x86@kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).