Skip to contentSkip to conversation

objtool: Relocation sanity check for alternatives

PATCH Applied Josh Poimboeuf 8 participants 18 messages Feb 10, 2020 – Feb 20, 2020 objtool

lore
Share & tools
lore
#0 lore raw

cover objtool: Relocation sanity check for alternatives

Patches 1-2 are in preparation for patch 3.

Patch 3 adds sanity checking to prevent unsupported relocations in alternatives.

Josh Poimboeuf (3):
objtool: Fail the kernel build on fatal errors
objtool: Add is_static_jump() helper
objtool: Add relocation check for alternative sections

 tools/objtool/check.c | 48 +++++++++++++++++++++++++++++++++++--------
 1 file changed, 39 insertions(+), 9 deletions(-)

Josh Poimboeuf (3):
objtool: Fail the kernel build on fatal errors
objtool: Add is_static_jump() helper
objtool: Add relocation check for alternative sections

 tools/objtool/check.c | 48 +++++++++++++++++++++++++++++++++++--------
 1 file changed, 39 insertions(+), 9 deletions(-)
Signature · 1 line
2.21.1
#1 lore raw

PATCH 1/3 objtool: Fail the kernel build on fatal errors

When objtool encounters a fatal error, it usually means the binary is corrupt or otherwise broken in some way. Up until now, such errors were just treated as warnings which didn't fail the kernel build.

However, objtool is now stable enough that if a fatal error is discovered, it most likely means something is seriously wrong and it should fail the kernel build.

Note that this doesn't apply to "normal" objtool warnings; only fatal ones.

---
 tools/objtool/check.c | 12 +++++++++---
 1 file changed, 9 insertions(+), 3 deletions(-)
tools/objtool/check.c +9 -3
index b6da413bcbd6..61d2d1877fd2 100644
@@ -2509,8 +2509,14 @@ int check(const char *_objname, bool orc)
 out:
 	cleanup(&file);
 
-	/* ignore warnings for now until we get all the code cleaned up */
-	if (ret || warnings)
-		return 0;
+	if (ret < 0) {
+		/*
+		 *  Fatal error.  The binary is corrupt or otherwise broken in
+		 *  some way, or objtool itself is broken.  Fail the kernel
+		 *  build.
+		 */
+		return ret;
+	}
+
 	return 0;
 }
Signature · 1 line
2.21.1
Reviewed-by on v1 1/3
#2 lore raw

Re: [PATCH 1/3] objtool: Fail the kernel build on fatal errors

Quoted 8 lines · Josh Poimboeuf

When objtool encounters a fatal error, it usually means the binary is corrupt or otherwise broken in some way. Up until now, such errors were just treated as warnings which didn't fail the kernel build.

However, objtool is now stable enough that if a fatal error is discovered, it most likely means something is seriously wrong and it should fail the kernel build.

Quoted 5 lines

Note that this doesn't apply to "normal" objtool warnings; only fatal ones.

Quoted 27 lines
---
  tools/objtool/check.c | 12 +++++++++---
  1 file changed, 9 insertions(+), 3 deletions(-)
tools/objtool/check.c +9 -3
index b6da413bcbd6..61d2d1877fd2 100644
@@ -2509,8 +2509,14 @@ int check(const char *_objname, bool orc)
  out:
  	cleanup(&file);
 
-	/* ignore warnings for now until we get all the code cleaned up */
-	if (ret || warnings)
-		return 0;
+	if (ret < 0) {
+		/*
+		 *  Fatal error.  The binary is corrupt or otherwise broken in
+		 *  some way, or objtool itself is broken.  Fail the kernel
+		 *  build.
+		 */
+		return ret;
+	}
+
  	return 0;
  }
Signature · 1 line
Julien Thierry
merge bot Applied
#3 lore raw

[tip: core/objtool] objtool: Fail the kernel build on fatal errors

In reply to Josh Poimboeuf
The following commit has been merged into the core/objtool branch of tip:

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

Commit-ID:     644592d328370af4b3e027b7b1ae9f81613782d8
Gitweb:        https://git.kernel.org/tip/644592d328370af4b3e027b7b1ae9f81613782d8
Author:        Josh Poimboeuf <[email protected]>
AuthorDate:    Mon, 10 Feb 2020 12:32:38 -06:00
Committer:     Borislav Petkov <[email protected]>
CommitterDate: Tue, 11 Feb 2020 13:27:03 +01:00

objtool: Fail the kernel build on fatal errors

When objtool encounters a fatal error, it usually means the binary is corrupt or otherwise broken in some way. Up until now, such errors were just treated as warnings which didn't fail the kernel build.

However, objtool is now stable enough that if a fatal error is discovered, it most likely means something is seriously wrong and it should fail the kernel build.

Note that this doesn't apply to "normal" objtool warnings; only fatal ones.

---
 tools/objtool/check.c | 12 +++++++++---
 1 file changed, 9 insertions(+), 3 deletions(-)
tools/objtool/check.c +9 -3
index 4768d91..796f6a1 100644
@@ -2491,8 +2491,14 @@ int check(const char *_objname, bool orc)
 out:
 	cleanup(&file);
 
-	/* ignore warnings for now until we get all the code cleaned up */
-	if (ret || warnings)
-		return 0;
+	if (ret < 0) {
+		/*
+		 *  Fatal error.  The binary is corrupt or otherwise broken in
+		 *  some way, or objtool itself is broken.  Fail the kernel
+		 *  build.
+		 */
+		return ret;
+	}
+
 	return 0;
 }
#4 lore raw

Re: [tip: core/objtool] objtool: Fail the kernel build on fatal errors

Quoted 15 lines · On Tue, Feb 11, 2020 at 12:47:38PM -0000, tip-bot2 for Josh Poimboeuf wrote:

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

Commit-ID:     644592d328370af4b3e027b7b1ae9f81613782d8
Gitweb:        https://git.kernel.org/tip/644592d328370af4b3e027b7b1ae9f81613782d8
Author:        Josh Poimboeuf <[email protected]>
AuthorDate:    Mon, 10 Feb 2020 12:32:38 -06:00
Committer:     Borislav Petkov <[email protected]>
CommitterDate: Tue, 11 Feb 2020 13:27:03 +01:00

objtool: Fail the kernel build on fatal errors

When objtool encounters a fatal error, it usually means the binary is corrupt or otherwise broken in some way. Up until now, such errors were just treated as warnings which didn't fail the kernel build.

Quoted 6 lines

However, objtool is now stable enough that if a fatal error is discovered, it most likely means something is seriously wrong and it should fail the kernel build.

Note that this doesn't apply to "normal" objtool warnings; only fatal ones.

Clang still has some toolchain issues which need to be sorted out, so upgrading the fatal errors is causing their CI to fail.

So I think we need to drop this one for now.

Boris, are you able to just drop it or should I send a revert?

Signature · 1 line
Josh
#5 lore raw

Re: [tip: core/objtool] objtool: Fail the kernel build on fatal errors

Quoted 20 lines · Josh Poimboeuf
Quoted 19 lines · On Tue, Feb 11, 2020 at 12:47:38PM -0000, tip-bot2 for Josh Poimboeuf wrote:

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

Commit-ID:     644592d328370af4b3e027b7b1ae9f81613782d8
Gitweb:        https://git.kernel.org/tip/644592d328370af4b3e027b7b1ae9f81613782d8
Author:        Josh Poimboeuf <[email protected]>
AuthorDate:    Mon, 10 Feb 2020 12:32:38 -06:00
Committer:     Borislav Petkov <[email protected]>
CommitterDate: Tue, 11 Feb 2020 13:27:03 +01:00

objtool: Fail the kernel build on fatal errors

When objtool encounters a fatal error, it usually means the binary is corrupt or otherwise broken in some way. Up until now, such errors were just treated as warnings which didn't fail the kernel build.

However, objtool is now stable enough that if a fatal error is discovered, it most likely means something is seriously wrong and it should fail the kernel build.

Quoted 5 lines
Quoted 2 lines

Note that this doesn't apply to "normal" objtool warnings; only fatal ones.

Clang still has some toolchain issues which need to be sorted out, so upgrading the fatal errors is causing their CI to fail.

Good. Last time we made it fail they just fixed their stuff.

Quoted 1 line

So I think we need to drop this one for now.

Why? It's our decision to define which level of toolchain brokeness is tolerable.

Quoted 1 line

Boris, are you able to just drop it or should I send a revert?

I really want to see a revert which has a proper justification why the issues of clang are tolerable along with a clear statement when this fatal error will come back. And 'when' means a date, not 'when clang is fixed'.

Thanks,

tglx

#6 lore raw

Re: [tip: core/objtool] objtool: Fail the kernel build on fatal errors

Quoted 34 lines · Thomas Gleixner
Quoted 20 lines · Josh Poimboeuf
Quoted 19 lines · On Tue, Feb 11, 2020 at 12:47:38PM -0000, tip-bot2 for Josh Poimboeuf wrote:

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

Commit-ID:     644592d328370af4b3e027b7b1ae9f81613782d8
Gitweb:        https://git.kernel.org/tip/644592d328370af4b3e027b7b1ae9f81613782d8
Author:        Josh Poimboeuf <[email protected]>
AuthorDate:    Mon, 10 Feb 2020 12:32:38 -06:00
Committer:     Borislav Petkov <[email protected]>
CommitterDate: Tue, 11 Feb 2020 13:27:03 +01:00

objtool: Fail the kernel build on fatal errors

When objtool encounters a fatal error, it usually means the binary is corrupt or otherwise broken in some way. Up until now, such errors were just treated as warnings which didn't fail the kernel build.

However, objtool is now stable enough that if a fatal error is discovered, it most likely means something is seriously wrong and it should fail the kernel build.

Quoted 5 lines
Quoted 2 lines

Note that this doesn't apply to "normal" objtool warnings; only fatal ones.

Clang still has some toolchain issues which need to be sorted out, so upgrading the fatal errors is causing their CI to fail.

Good. Last time we made it fail they just fixed their stuff.

Quoted 1 line

So I think we need to drop this one for now.

Why? It's our decision to define which level of toolchain brokeness is tolerable.

Quoted 6 lines
Quoted 1 line

Boris, are you able to just drop it or should I send a revert?

I really want to see a revert which has a proper justification why the issues of clang are tolerable along with a clear statement when this fatal error will come back. And 'when' means a date, not 'when clang is fixed'.

Fair enough. The root cause was actually a bug in binutils which gets triggered by a new clang feature. So instead of reverting the above patch, I think I've figured out a way to work around the binutils bug, while also improving objtool at the same time (win-win).

The binutils bug will be fixed in binutils 2.35.

BTW, to be fair, this was less "Clang has issues" and more "Josh is lazy". I didn't test the patch with Clang -- I tend to rely on 0-day bot reports because I don't have the bandwidth to test the kernel/config/toolchain combinations. Nick tells me Clang will soon be integrated with the 0-day bot, which should help prevent this type of thing in the future.

Signature · 1 line
Josh
#7 lore raw

Re: [tip: core/objtool] objtool: Fail the kernel build on fatal errors

Quoted 48 lines · Josh Poimboeuf
Quoted 34 lines · Thomas Gleixner
Quoted 20 lines · Josh Poimboeuf
Quoted 19 lines · On Tue, Feb 11, 2020 at 12:47:38PM -0000, tip-bot2 for Josh Poimboeuf wrote:

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

Commit-ID:     644592d328370af4b3e027b7b1ae9f81613782d8
Gitweb:        https://git.kernel.org/tip/644592d328370af4b3e027b7b1ae9f81613782d8
Author:        Josh Poimboeuf <[email protected]>
AuthorDate:    Mon, 10 Feb 2020 12:32:38 -06:00
Committer:     Borislav Petkov <[email protected]>
CommitterDate: Tue, 11 Feb 2020 13:27:03 +01:00

objtool: Fail the kernel build on fatal errors

When objtool encounters a fatal error, it usually means the binary is corrupt or otherwise broken in some way. Up until now, such errors were just treated as warnings which didn't fail the kernel build.

However, objtool is now stable enough that if a fatal error is discovered, it most likely means something is seriously wrong and it should fail the kernel build.

Quoted 5 lines
Quoted 2 lines

Note that this doesn't apply to "normal" objtool warnings; only fatal ones.

Clang still has some toolchain issues which need to be sorted out, so upgrading the fatal errors is causing their CI to fail.

Good. Last time we made it fail they just fixed their stuff.

Quoted 1 line

So I think we need to drop this one for now.

Why? It's our decision to define which level of toolchain brokeness is tolerable.

Quoted 6 lines
Quoted 1 line

Boris, are you able to just drop it or should I send a revert?

I really want to see a revert which has a proper justification why the issues of clang are tolerable along with a clear statement when this fatal error will come back. And 'when' means a date, not 'when clang is fixed'.

Fair enough. The root cause was actually a bug in binutils which gets triggered by a new clang feature. So instead of reverting the above patch, I think I've figured out a way to work around the binutils bug, while also improving objtool at the same time (win-win).

Quoted 8 lines

The binutils bug will be fixed in binutils 2.35.

BTW, to be fair, this was less "Clang has issues" and more "Josh is lazy". I didn't test the patch with Clang -- I tend to rely on 0-day bot reports because I don't have the bandwidth to test the kernel/config/toolchain combinations. Nick tells me Clang will soon be integrated with the 0-day bot, which should help prevent this type of thing in the future.

Hi Rong, Philip,
Do you have any status updates on turning on the 0day bot emails to the patch authors in production? It's been quite handy in helping us find issues, for the private mails we've been triaging daily.

Signature · 2 lines
Thanks,
~Nick Desaulniers
#8 lore raw

Re: [tip: core/objtool] objtool: Fail the kernel build on fatal errors

Quoted 58 lines · Nick Desaulniers
Quoted 48 lines · Josh Poimboeuf
Quoted 34 lines · Thomas Gleixner
Quoted 20 lines · Josh Poimboeuf
Quoted 19 lines · On Tue, Feb 11, 2020 at 12:47:38PM -0000, tip-bot2 for Josh Poimboeuf wrote:

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

Commit-ID:     644592d328370af4b3e027b7b1ae9f81613782d8
Gitweb:        https://git.kernel.org/tip/644592d328370af4b3e027b7b1ae9f81613782d8
Author:        Josh Poimboeuf <[email protected]>
AuthorDate:    Mon, 10 Feb 2020 12:32:38 -06:00
Committer:     Borislav Petkov <[email protected]>
CommitterDate: Tue, 11 Feb 2020 13:27:03 +01:00

objtool: Fail the kernel build on fatal errors

When objtool encounters a fatal error, it usually means the binary is corrupt or otherwise broken in some way. Up until now, such errors were just treated as warnings which didn't fail the kernel build.

However, objtool is now stable enough that if a fatal error is discovered, it most likely means something is seriously wrong and it should fail the kernel build.

Quoted 5 lines
Quoted 2 lines

Note that this doesn't apply to "normal" objtool warnings; only fatal ones.

Clang still has some toolchain issues which need to be sorted out, so upgrading the fatal errors is causing their CI to fail.

Good. Last time we made it fail they just fixed their stuff.

Quoted 1 line

So I think we need to drop this one for now.

Why? It's our decision to define which level of toolchain brokeness is tolerable.

Quoted 6 lines
Quoted 1 line

Boris, are you able to just drop it or should I send a revert?

I really want to see a revert which has a proper justification why the issues of clang are tolerable along with a clear statement when this fatal error will come back. And 'when' means a date, not 'when clang is fixed'.

Fair enough. The root cause was actually a bug in binutils which gets triggered by a new clang feature. So instead of reverting the above patch, I think I've figured out a way to work around the binutils bug, while also improving objtool at the same time (win-win).

Quoted 8 lines

The binutils bug will be fixed in binutils 2.35.

BTW, to be fair, this was less "Clang has issues" and more "Josh is lazy". I didn't test the patch with Clang -- I tend to rely on 0-day bot reports because I don't have the bandwidth to test the kernel/config/toolchain combinations. Nick tells me Clang will soon be integrated with the 0-day bot, which should help prevent this type of thing in the future.

Quoted 4 lines

Hi Rong, Philip,
Do you have any status updates on turning on the 0day bot emails to the patch authors in production? It's been quite handy in helping us find issues, for the private mails we've been triaging daily.

Hi Nick, this is on our schedule in a new 2-3 weeks, sorry not to update your in another mail loop earlier.

What I plan to do is to cc you for the clang reports when 0-day ci sends to kernel patch author. If you notice something may be related to clang (since we always integrate newer clang version), you can help filter it out. How do you think?

Quoted 3 lines
Signature · 2 lines
Thanks,
~Nick Desaulniers
#9 lore raw

Re: [tip: core/objtool] objtool: Fail the kernel build on fatal errors

(everyone else to bcc)

Quoted 60 lines · Philip Li
Quoted 58 lines · Nick Desaulniers
Quoted 56 lines · Josh Poimboeuf
Quoted 34 lines · Thomas Gleixner
Quoted 20 lines · Josh Poimboeuf
Quoted 19 lines · On Tue, Feb 11, 2020 at 12:47:38PM -0000, tip-bot2 for Josh Poimboeuf wrote:

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

Commit-ID:     644592d328370af4b3e027b7b1ae9f81613782d8
Gitweb:        https://git.kernel.org/tip/644592d328370af4b3e027b7b1ae9f81613782d8
Author:        Josh Poimboeuf <[email protected]>
AuthorDate:    Mon, 10 Feb 2020 12:32:38 -06:00
Committer:     Borislav Petkov <[email protected]>
CommitterDate: Tue, 11 Feb 2020 13:27:03 +01:00

objtool: Fail the kernel build on fatal errors

When objtool encounters a fatal error, it usually means the binary is corrupt or otherwise broken in some way. Up until now, such errors were just treated as warnings which didn't fail the kernel build.

However, objtool is now stable enough that if a fatal error is discovered, it most likely means something is seriously wrong and it should fail the kernel build.

Quoted 5 lines
Quoted 2 lines

Note that this doesn't apply to "normal" objtool warnings; only fatal ones.

Clang still has some toolchain issues which need to be sorted out, so upgrading the fatal errors is causing their CI to fail.

Good. Last time we made it fail they just fixed their stuff.

Quoted 1 line

So I think we need to drop this one for now.

Why? It's our decision to define which level of toolchain brokeness is tolerable.

Quoted 6 lines
Quoted 1 line

Boris, are you able to just drop it or should I send a revert?

I really want to see a revert which has a proper justification why the issues of clang are tolerable along with a clear statement when this fatal error will come back. And 'when' means a date, not 'when clang is fixed'.

Fair enough. The root cause was actually a bug in binutils which gets triggered by a new clang feature. So instead of reverting the above patch, I think I've figured out a way to work around the binutils bug, while also improving objtool at the same time (win-win).

The binutils bug will be fixed in binutils 2.35.

BTW, to be fair, this was less "Clang has issues" and more "Josh is lazy". I didn't test the patch with Clang -- I tend to rely on 0-day bot reports because I don't have the bandwidth to test the kernel/config/toolchain combinations. Nick tells me Clang will soon be integrated with the 0-day bot, which should help prevent this type of thing in the future.

Quoted 6 lines
Quoted 4 lines

Hi Rong, Philip,
Do you have any status updates on turning on the 0day bot emails to the patch authors in production? It's been quite handy in helping us find issues, for the private mails we've been triaging daily.

Hi Nick, this is on our schedule in a new 2-3 weeks, sorry not to update your in another mail loop earlier.

No worries.

Quoted 5 lines

What I plan to do is to cc you for the clang reports when 0-day ci sends to kernel patch author. If you notice something may be related to clang (since we always integrate newer clang version), you can help filter it out. How do you think?

If you would kindly cc our mailing list "clang-built-linux <[email protected]>" we'd be happy to continue to triage and provide suggestions. That level of indirection better allows us to deal with subscriptions and change of email addresses without having to disturb you.

Signature · 2 lines
Thanks,
~Nick Desaulniers
#10 lore raw

PATCH 2/3 objtool: Add is_static_jump() helper

In reply to Josh Poimboeuf

There are several places where objtool tests for a non-dynamic (aka direct) jump. Move the check to a helper function.

---
 tools/objtool/check.c | 15 +++++++++------
 1 file changed, 9 insertions(+), 6 deletions(-)
tools/objtool/check.c +9 -6
index 61d2d1877fd2..5ea2ce7ed8a3 100644
@@ -97,14 +97,19 @@ static struct instruction *next_insn_same_func(struct objtool_file *file,
 	for (insn = next_insn_same_sec(file, insn); insn;		\
 	     insn = next_insn_same_sec(file, insn))
 
+static bool is_static_jump(struct instruction *insn)
+{
+	return insn->type == INSN_JUMP_CONDITIONAL ||
+	       insn->type == INSN_JUMP_UNCONDITIONAL;
+}
+
 static bool is_sibling_call(struct instruction *insn)
 {
 	/* An indirect jump is either a sibling call or a jump to a table. */
 	if (insn->type == INSN_JUMP_DYNAMIC)
 		return list_empty(&insn->alts);
 
-	if (insn->type != INSN_JUMP_CONDITIONAL &&
-	    insn->type != INSN_JUMP_UNCONDITIONAL)
+	if (!is_static_jump(insn))
 		return false;
 
 	/* add_jump_destinations() sets insn->call_dest for sibling calls. */
@@ -571,8 +576,7 @@ static int add_jump_destinations(struct objtool_file *file)
 	unsigned long dest_off;
 
 	for_each_insn(file, insn) {
-		if (insn->type != INSN_JUMP_CONDITIONAL &&
-		    insn->type != INSN_JUMP_UNCONDITIONAL)
+		if (!is_static_jump(insn))
 			continue;
 
 		if (insn->ignore || insn->offset == FAKE_JUMP_OFFSET)
@@ -782,8 +786,7 @@ static int handle_group_alt(struct objtool_file *file,
 		insn->ignore = orig_insn->ignore_alts;
 		insn->func = orig_insn->func;
 
-		if (insn->type != INSN_JUMP_CONDITIONAL &&
-		    insn->type != INSN_JUMP_UNCONDITIONAL)
+		if (!is_static_jump(insn))
 			continue;
 
 		if (!insn->immediate)
Signature · 1 line
2.21.1
Reviewed-by on v1 2/3
#11 lore raw

Re: [PATCH 2/3] objtool: Add is_static_jump() helper

Quoted 3 lines · Josh Poimboeuf

There are several places where objtool tests for a non-dynamic (aka direct) jump. Move the check to a helper function.

Makes sense.

Quoted 1 line
Quoted 51 lines
---
  tools/objtool/check.c | 15 +++++++++------
  1 file changed, 9 insertions(+), 6 deletions(-)
tools/objtool/check.c +9 -6
index 61d2d1877fd2..5ea2ce7ed8a3 100644
@@ -97,14 +97,19 @@ static struct instruction *next_insn_same_func(struct objtool_file *file,
  	for (insn = next_insn_same_sec(file, insn); insn;		\
  	     insn = next_insn_same_sec(file, insn))
 
+static bool is_static_jump(struct instruction *insn)
+{
+	return insn->type == INSN_JUMP_CONDITIONAL ||
+	       insn->type == INSN_JUMP_UNCONDITIONAL;
+}
+
  static bool is_sibling_call(struct instruction *insn)
  {
  	/* An indirect jump is either a sibling call or a jump to a table. */
  	if (insn->type == INSN_JUMP_DYNAMIC)
  		return list_empty(&insn->alts);
 
-	if (insn->type != INSN_JUMP_CONDITIONAL &&
-	    insn->type != INSN_JUMP_UNCONDITIONAL)
+	if (!is_static_jump(insn))
  		return false;
 
  	/* add_jump_destinations() sets insn->call_dest for sibling calls. */
@@ -571,8 +576,7 @@ static int add_jump_destinations(struct objtool_file *file)
  	unsigned long dest_off;
 
  	for_each_insn(file, insn) {
-		if (insn->type != INSN_JUMP_CONDITIONAL &&
-		    insn->type != INSN_JUMP_UNCONDITIONAL)
+		if (!is_static_jump(insn))
  			continue;
 
  		if (insn->ignore || insn->offset == FAKE_JUMP_OFFSET)
@@ -782,8 +786,7 @@ static int handle_group_alt(struct objtool_file *file,
  		insn->ignore = orig_insn->ignore_alts;
  		insn->func = orig_insn->func;
 
-		if (insn->type != INSN_JUMP_CONDITIONAL &&
-		    insn->type != INSN_JUMP_UNCONDITIONAL)
+		if (!is_static_jump(insn))
  			continue;
 
  		if (!insn->immediate)
Signature · 1 line
Julien Thierry
merge bot Applied
#12 lore raw

[tip: core/objtool] objtool: Add is_static_jump() helper

In reply to Josh Poimboeuf
The following commit has been merged into the core/objtool branch of tip:

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

Commit-ID:     a22961409c02b93ffa7ed78f67fb57a1ba6c787d
Gitweb:        https://git.kernel.org/tip/a22961409c02b93ffa7ed78f67fb57a1ba6c787d
Author:        Josh Poimboeuf <[email protected]>
AuthorDate:    Mon, 10 Feb 2020 12:32:39 -06:00
Committer:     Borislav Petkov <[email protected]>
CommitterDate: Tue, 11 Feb 2020 13:36:01 +01:00

objtool: Add is_static_jump() helper

There are several places where objtool tests for a non-dynamic (aka direct) jump. Move the check to a helper function.

---
 tools/objtool/check.c | 15 +++++++++------
 1 file changed, 9 insertions(+), 6 deletions(-)
tools/objtool/check.c +9 -6
index 796f6a1..9016ae1 100644
@@ -97,14 +97,19 @@ static struct instruction *next_insn_same_func(struct objtool_file *file,
 	for (insn = next_insn_same_sec(file, insn); insn;		\
 	     insn = next_insn_same_sec(file, insn))
 
+static bool is_static_jump(struct instruction *insn)
+{
+	return insn->type == INSN_JUMP_CONDITIONAL ||
+	       insn->type == INSN_JUMP_UNCONDITIONAL;
+}
+
 static bool is_sibling_call(struct instruction *insn)
 {
 	/* An indirect jump is either a sibling call or a jump to a table. */
 	if (insn->type == INSN_JUMP_DYNAMIC)
 		return list_empty(&insn->alts);
 
-	if (insn->type != INSN_JUMP_CONDITIONAL &&
-	    insn->type != INSN_JUMP_UNCONDITIONAL)
+	if (!is_static_jump(insn))
 		return false;
 
 	/* add_jump_destinations() sets insn->call_dest for sibling calls. */
@@ -553,8 +558,7 @@ static int add_jump_destinations(struct objtool_file *file)
 	unsigned long dest_off;
 
 	for_each_insn(file, insn) {
-		if (insn->type != INSN_JUMP_CONDITIONAL &&
-		    insn->type != INSN_JUMP_UNCONDITIONAL)
+		if (!is_static_jump(insn))
 			continue;
 
 		if (insn->ignore || insn->offset == FAKE_JUMP_OFFSET)
@@ -764,8 +768,7 @@ static int handle_group_alt(struct objtool_file *file,
 		insn->ignore = orig_insn->ignore_alts;
 		insn->func = orig_insn->func;
 
-		if (insn->type != INSN_JUMP_CONDITIONAL &&
-		    insn->type != INSN_JUMP_UNCONDITIONAL)
+		if (!is_static_jump(insn))
 			continue;
 
 		if (!insn->immediate)
#13 lore raw

PATCH 3/3 objtool: Add relocation check for alternative sections

In reply to Josh Poimboeuf

Relocations in alternative code can be dangerous, because the code is copy/pasted to the text section after relocations have been resolved, which can corrupt PC-relative addresses.

However, relocations might be acceptable in some cases, depending on the architecture. For example, the x86 alternatives code manually fixes up the target addresses for PC-relative jumps and calls.

So disallow relocations in alternative code, except where the x86 arch code allows it.

This code may need to be tweaked for other arches when objtool gets support for them.

---
 tools/objtool/check.c | 21 +++++++++++++++++++++
 1 file changed, 21 insertions(+)
tools/objtool/check.c +21 -0
index 5ea2ce7ed8a3..2d52a40e6cb9 100644
@@ -786,6 +786,27 @@ static int handle_group_alt(struct objtool_file *file,
 		insn->ignore = orig_insn->ignore_alts;
 		insn->func = orig_insn->func;
 
+		/*
+		 * Since alternative replacement code is copy/pasted by the
+		 * kernel after applying relocations, generally such code can't
+		 * have relative-address relocation references to outside the
+		 * .altinstr_replacement section, unless the arch's
+		 * alternatives code can adjust the relative offsets
+		 * accordingly.
+		 *
+		 * The x86 alternatives code adjusts the offsets only when it
+		 * encounters a branch instruction at the very beginning of the
+		 * replacement group.
+		 */
+		if ((insn->offset != special_alt->new_off ||
+		    (insn->type != INSN_CALL && !is_static_jump(insn))) &&
+		    find_rela_by_dest_range(insn->sec, insn->offset, insn->len)) {
+
+			WARN_FUNC("unsupported relocation in alternatives section",
+				  insn->sec, insn->offset);
+			return -1;
+		}
+
 		if (!is_static_jump(insn))
 			continue;
 
Signature · 1 line
2.21.1
#14 lore raw

Re: [PATCH 3/3] objtool: Add relocation check for alternative sections

Quoted 3 lines · Josh Poimboeuf

So disallow relocations in alternative code, except where the x86 arch code allows it.

LGTM. Did this actually find anything? And if not, did you verify that it would by intentionally creating a bad alternative (perhaps the broken one from my patch?)

Linus

#15 lore raw

Re: [PATCH 3/3] objtool: Add relocation check for alternative sections

Quoted 4 lines · Linus Torvalds

On Mon, Feb 10, 2020 at 10:33 AM Josh Poimboeuf <[email protected]> wrote:
LGTM. Did this actually find anything? And if not, did you verify that it would by intentionally creating a bad alternative (perhaps the broken one from my patch?)

Yeah, we used your patch while playing with this. Lemme do some builds with this and see what fires. Nothing should, I would strongly assume.

Signature · 4 lines
Regards/Gruss,
    Boris.

SUSE Software Solutions Germany GmbH, GF: Felix Imendörffer, HRB 36809, AG Nürnberg
Reviewed-by on v1 3/3
#16 lore raw

Re: [PATCH 3/3] objtool: Add relocation check for alternative sections

In reply to Josh Poimboeuf
Quoted 29 lines · Josh Poimboeuf

Relocations in alternative code can be dangerous, because the code is copy/pasted to the text section after relocations have been resolved, which can corrupt PC-relative addresses.

However, relocations might be acceptable in some cases, depending on the architecture. For example, the x86 alternatives code manually fixes up the target addresses for PC-relative jumps and calls.

So disallow relocations in alternative code, except where the x86 arch code allows it.

This code may need to be tweaked for other arches when objtool gets support for them.

---
  tools/objtool/check.c | 21 +++++++++++++++++++++
  1 file changed, 21 insertions(+)
tools/objtool/check.c +1 -0
index 5ea2ce7ed8a3..2d52a40e6cb9 100644
@@ -786,6 +786,27 @@ static int handle_group_alt(struct objtool_file *file,
  		insn->ignore = orig_insn->ignore_alts;
  		insn->func = orig_insn->func;
 
+		/*

tools/objtool/check.c static int handle_group_alt(struct objtool_file *file, · line 799

Quoted 10 lines
tools/objtool/check.c +10 -0
+		 * Since alternative replacement code is copy/pasted by the
+		 * kernel after applying relocations, generally such code can't
+		 * have relative-address relocation references to outside the
+		 * .altinstr_replacement section, unless the arch's
+		 * alternatives code can adjust the relative offsets
+		 * accordingly.
+		 *
+		 * The x86 alternatives code adjusts the offsets only when it
+		 * encounters a branch instruction at the very beginning of the
+		 * replacement group.

Yes, arm64 is a bit more permissive regarding this although it does disallow some patterns.

I guess once we introduce other archs there should be some function:

bool arch_validate_alt_insn(struct instruction *new, struct instruction *new, struct special_alt *alt);

Quoted 4 lines
tools/objtool/check.c +3 -0
+		 */
+		if ((insn->offset != special_alt->new_off ||
+		    (insn->type != INSN_CALL && !is_static_jump(insn))) &&

tools/objtool/check.c static int handle_group_alt(struct objtool_file *file, · line 812

Quoted 10 lines
tools/objtool/check.c +7 -0
+		    find_rela_by_dest_range(insn->sec, insn->offset, insn->len)) {
+
+			WARN_FUNC("unsupported relocation in alternatives section",
+				  insn->sec, insn->offset);
+			return -1;
+		}
+
  		if (!is_static_jump(insn))
  			continue;
 

Cheers,

Signature · 1 line
Julien Thierry
merge bot Applied
#17 lore raw

[tip: core/objtool] objtool: Add relocation check for alternative sections

In reply to Josh Poimboeuf
The following commit has been merged into the core/objtool branch of tip:

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

Commit-ID:     dc4197236c20e761f2007c641afd193f81a00a74
Gitweb:        https://git.kernel.org/tip/dc4197236c20e761f2007c641afd193f81a00a74
Author:        Josh Poimboeuf <[email protected]>
AuthorDate:    Mon, 10 Feb 2020 12:32:40 -06:00
Committer:     Borislav Petkov <[email protected]>
CommitterDate: Tue, 11 Feb 2020 13:39:52 +01:00

objtool: Add relocation check for alternative sections

Relocations in alternative code can be dangerous, because the code is copy/pasted to the text section after relocations have been resolved, which can corrupt PC-relative addresses.

However, relocations might be acceptable in some cases, depending on the architecture. For example, the x86 alternatives code manually fixes up the target addresses for PC-relative jumps and calls.

So disallow relocations in alternative code, except where the x86 arch code allows it.

This code may need to be tweaked for other arches when objtool gets support for them.

---
 tools/objtool/check.c | 21 +++++++++++++++++++++
 1 file changed, 21 insertions(+)
tools/objtool/check.c +21 -0
index 9016ae1..b038de2 100644
@@ -768,6 +768,27 @@ static int handle_group_alt(struct objtool_file *file,
 		insn->ignore = orig_insn->ignore_alts;
 		insn->func = orig_insn->func;
 
+		/*
+		 * Since alternative replacement code is copy/pasted by the
+		 * kernel after applying relocations, generally such code can't
+		 * have relative-address relocation references to outside the
+		 * .altinstr_replacement section, unless the arch's
+		 * alternatives code can adjust the relative offsets
+		 * accordingly.
+		 *
+		 * The x86 alternatives code adjusts the offsets only when it
+		 * encounters a branch instruction at the very beginning of the
+		 * replacement group.
+		 */
+		if ((insn->offset != special_alt->new_off ||
+		    (insn->type != INSN_CALL && !is_static_jump(insn))) &&
+		    find_rela_by_dest_range(insn->sec, insn->offset, insn->len)) {
+
+			WARN_FUNC("unsupported relocation in alternatives section",
+				  insn->sec, insn->offset);
+			return -1;
+		}
+
 		if (!is_static_jump(insn))
 			continue;