From: Peter Zijlstra <peterz@infradead.org>
To: David Woodhouse <dwmw2@infradead.org>,
Josh Poimboeuf <jpoimboe@redhat.com>
Cc: linux-kernel@vger.kernel.org, Dave Hansen <dave.hansen@intel.com>,
Ashok Raj <ashok.raj@intel.com>,
Thomas Gleixner <tglx@linutronix.de>,
Tim Chen <tim.c.chen@linux.intel.com>,
Andy Lutomirski <luto@kernel.org>,
Linus Torvalds <torvalds@linux-foundation.org>,
Greg KH <gregkh@linuxfoundation.org>,
Andrea Arcangeli <aarcange@redhat.com>,
Andi Kleen <ak@linux.intel.com>,
Arjan Van De Ven <arjan.van.de.ven@intel.com>,
Dan Williams <dan.j.williams@intel.com>,
Paolo Bonzini <pbonzini@redhat.com>,
Jun Nakajima <jun.nakajima@intel.com>,
Asit Mallick <asit.k.mallick@intel.com>,
Peter Zijlstra <peterz@infradead.org>,
Borislav Petkov <bp@alien8.de>
Subject: [PATCH 1/4] objtool: Implement base jump_assert support
Date: Mon, 15 Jan 2018 17:44:29 +0100 [thread overview]
Message-ID: <20180115164909.774177737@infradead.org> (raw)
In-Reply-To: <20180115164428.498966552@infradead.org>
[-- Attachment #1: peter_zijlstra-q-objtool_vs_jump_label.patch --]
[-- Type: text/plain, Size: 3919 bytes --]
Implement a jump_label assertion that asserts that the code location
is indeed only reachable through a static_branch. Because if GCC is
absolutely retaded it could generate code like:
xor rax,rax
NOP/JMP 1f
mov $1, rax
1:
test rax,rax
jz 2f
<do-code>
2:
instead of the sensible:
NOP/JMP 1f
<do-code>
1:
This implements objtool infrastructure for ensuring the code ends up
sane, since we'll rely on that for correctness and security.
We tag the instructions after the static branch with br_static=true;
that is the instruction after the NOP and the instruction at the
JMP+disp site.
Then, when we read the .discard.jump_assert section, we assert that
each entry points to an instruction that has br_static set.
With this we can assert that the code emitted for the if statement
ends up at the static jump location and nothing untowards happened.
Cc: Josh Poimboeuf <jpoimboe@redhat.com>
Cc: Thomas Gleixner <tglx@linutronix.de>
Cc: Borislav Petkov <bp@alien8.de>
Signed-off-by: Peter Zijlstra (Intel) <peterz@infradead.org>
---
tools/objtool/check.c | 70 ++++++++++++++++++++++++++++++++++++++++++++++++--
tools/objtool/check.h | 1
2 files changed, 69 insertions(+), 2 deletions(-)
--- a/tools/objtool/check.c
+++ b/tools/objtool/check.c
@@ -687,8 +687,17 @@ static int handle_jump_alt(struct objtoo
struct instruction *orig_insn,
struct instruction **new_insn)
{
- if (orig_insn->type == INSN_NOP)
+ struct instruction *next_insn = list_next_entry(orig_insn, list);
+
+ if (orig_insn->type == INSN_NOP) {
+ /*
+ * If orig_insn is a NOP, then new_insn is the branch target
+ * for when it would've been a JMP.
+ */
+ next_insn->br_static = true;
+ (*new_insn)->br_static = true;
return 0;
+ }
if (orig_insn->type != INSN_JUMP_UNCONDITIONAL) {
WARN_FUNC("unsupported instruction at jump label",
@@ -696,7 +705,16 @@ static int handle_jump_alt(struct objtoo
return -1;
}
- *new_insn = list_next_entry(orig_insn, list);
+ /*
+ * Otherwise, orig_insn is a JMP and it will have orig_insn->jump_dest.
+ * In this case we'll effectively NOP the alt by pointing new_insn at
+ * next_insn.
+ */
+ orig_insn->jump_dest->br_static = true;
+ next_insn->br_static = true;
+
+ *new_insn = next_insn;
+
return 0;
}
@@ -1067,6 +1085,50 @@ static int read_unwind_hints(struct objt
return 0;
}
+static int read_jump_assertions(struct objtool_file *file)
+{
+ struct section *sec, *relasec;
+ struct instruction *insn;
+ struct rela *rela;
+ int i;
+
+ sec = find_section_by_name(file->elf, ".discard.jump_assert");
+ if (!sec)
+ return 0;
+
+ relasec = sec->rela;
+ if (!relasec) {
+ WARN("missing .rela.discard.jump_assert section");
+ return -1;
+ }
+
+ if (sec->len % sizeof(unsigned long)) {
+ WARN("jump_assert size mismatch: %d %ld", sec->len, sizeof(unsigned long));
+ return -1;
+ }
+
+ for (i = 0; i < sec->len / sizeof(unsigned long); i++) {
+ rela = find_rela_by_dest(sec, i * sizeof(unsigned long));
+ if (!rela) {
+ WARN("can't find rela for jump_assert[%d]", i);
+ return -1;
+ }
+
+ insn = find_insn(file, rela->sym->sec, rela->addend);
+ if (!insn) {
+ WARN("can't find insn for jump_assert[%d]", i);
+ return -1;
+ }
+
+ if (!insn->br_static) {
+ WARN_FUNC("static assert FAIL", insn->sec, insn->offset);
+ return -1;
+ }
+ }
+
+ return 0;
+}
+
static int decode_sections(struct objtool_file *file)
{
int ret;
@@ -1105,6 +1167,10 @@ static int decode_sections(struct objtoo
if (ret)
return ret;
+ ret = read_jump_assertions(file);
+ if (ret)
+ return ret;
+
return 0;
}
--- a/tools/objtool/check.h
+++ b/tools/objtool/check.h
@@ -45,6 +45,7 @@ struct instruction {
unsigned char type;
unsigned long immediate;
bool alt_group, visited, dead_end, ignore, hint, save, restore, ignore_alts;
+ bool br_static;
struct symbol *call_dest;
struct instruction *jump_dest;
struct list_head alts;
next prev parent reply other threads:[~2018-01-15 16:50 UTC|newest]
Thread overview: 21+ messages / expand[flat|nested] mbox.gz Atom feed top
2018-01-15 16:44 [PATCH 0/4] objtool validation of static branches Peter Zijlstra
2018-01-15 16:44 ` Peter Zijlstra [this message]
2018-01-15 17:39 ` [PATCH 1/4] objtool: Implement base jump_assert support Josh Poimboeuf
2018-01-15 18:00 ` Peter Zijlstra
2018-01-15 16:44 ` [PATCH 2/4] objtool: Implement jump_assert for _static_cpu_has() Peter Zijlstra
2018-01-15 18:04 ` Josh Poimboeuf
2018-01-15 18:12 ` Peter Zijlstra
2018-01-15 18:34 ` Josh Poimboeuf
2018-01-15 18:59 ` Borislav Petkov
2018-01-15 19:08 ` Josh Poimboeuf
2018-01-15 19:11 ` Josh Poimboeuf
2018-01-15 19:15 ` Borislav Petkov
2018-01-15 20:15 ` Peter Zijlstra
2018-01-15 20:30 ` Josh Poimboeuf
2018-01-15 16:44 ` [PATCH 3/4] x86/jump_label: Implement arch_static_assert() Peter Zijlstra
2018-01-15 17:22 ` Josh Poimboeuf
2018-01-15 17:41 ` David Woodhouse
2018-01-15 17:58 ` Peter Zijlstra
2018-01-15 18:28 ` Josh Poimboeuf
2018-01-15 16:44 ` [PATCH 4/4] x86: Reindent _static_cpu_has Peter Zijlstra
2018-01-15 17:15 ` Josh Poimboeuf
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=20180115164909.774177737@infradead.org \
--to=peterz@infradead.org \
--cc=aarcange@redhat.com \
--cc=ak@linux.intel.com \
--cc=arjan.van.de.ven@intel.com \
--cc=ashok.raj@intel.com \
--cc=asit.k.mallick@intel.com \
--cc=bp@alien8.de \
--cc=dan.j.williams@intel.com \
--cc=dave.hansen@intel.com \
--cc=dwmw2@infradead.org \
--cc=gregkh@linuxfoundation.org \
--cc=jpoimboe@redhat.com \
--cc=jun.nakajima@intel.com \
--cc=linux-kernel@vger.kernel.org \
--cc=luto@kernel.org \
--cc=pbonzini@redhat.com \
--cc=tglx@linutronix.de \
--cc=tim.c.chen@linux.intel.com \
--cc=torvalds@linux-foundation.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
Powered by JetHome