From: Alexandre Chartre <alexandre.chartre@oracle.com>
To: x86@kernel.org
Cc: linux-kernel@vger.kernel.org, jpoimboe@redhat.com,
peterz@infradead.org, jthierry@redhat.com, tglx@linutronix.de,
alexandre.chartre@oracle.com
Subject: [PATCH 4/7] objtool: Add support for return trampoline call
Date: Thu, 2 Apr 2020 10:22:17 +0200 [thread overview]
Message-ID: <20200402082220.808-5-alexandre.chartre@oracle.com> (raw)
In-Reply-To: <20200402082220.808-1-alexandre.chartre@oracle.com>
With retpoline, the return instruction is used to branch to an address
stored on the stack. So, unlike a regular return instruction, when a
retpoline return instruction is reached the stack has been modified
compared to what we have when the function was entered.
Provide the mechanism to explicitly call-out such return instruction
so that objtool can correctly handle them.
Signed-off-by: Alexandre Chartre <alexandre.chartre@oracle.com>
---
tools/objtool/check.c | 78 +++++++++++++++++++++++++++++++++++++++++--
tools/objtool/check.h | 1 +
2 files changed, 76 insertions(+), 3 deletions(-)
diff --git a/tools/objtool/check.c b/tools/objtool/check.c
index 0cec91291d46..ed8e3ea1d8da 100644
--- a/tools/objtool/check.c
+++ b/tools/objtool/check.c
@@ -1344,6 +1344,48 @@ static int read_intra_function_call(struct objtool_file *file)
return 0;
}
+static int read_retpoline_ret(struct objtool_file *file)
+{
+ struct section *sec;
+ struct instruction *insn;
+ struct rela *rela;
+
+ sec = find_section_by_name(file->elf, ".rela.discard.retpoline_ret");
+ if (!sec)
+ return 0;
+
+ list_for_each_entry(rela, &sec->rela_list, list) {
+ if (rela->sym->type != STT_SECTION) {
+ WARN("unexpected relocation symbol type in %s",
+ sec->name);
+ return -1;
+ }
+
+ insn = find_insn(file, rela->sym->sec, rela->addend);
+ if (!insn) {
+ WARN("bad .discard.retpoline_ret entry");
+ return -1;
+ }
+
+ if (insn->type != INSN_RETURN) {
+ WARN_FUNC("retpoline_ret not a return",
+ insn->sec, insn->offset);
+ return -1;
+ }
+
+ insn->retpoline_ret = true;
+ /*
+ * For the impact on the stack, make a return trampoline
+ * behaves like a pop of the return address.
+ */
+ insn->stack_op.src.type = OP_SRC_POP;
+ insn->stack_op.dest.type = OP_DEST_REG;
+ insn->stack_op.dest.reg = CFI_RA;
+ }
+
+ return 0;
+}
+
static void mark_rodata(struct objtool_file *file)
{
struct section *sec;
@@ -1403,6 +1445,10 @@ static int decode_sections(struct objtool_file *file)
if (ret)
return ret;
+ ret = read_retpoline_ret(file);
+ if (ret)
+ return ret;
+
ret = add_call_destinations(file);
if (ret)
return ret;
@@ -1432,7 +1478,8 @@ static bool is_fentry_call(struct instruction *insn)
return false;
}
-static bool has_modified_stack_frame(struct insn_state *state)
+static bool has_modified_stack_frame(struct insn_state *state,
+ bool check_registers)
{
int i;
@@ -1442,6 +1489,9 @@ static bool has_modified_stack_frame(struct insn_state *state)
state->drap)
return true;
+ if (!check_registers)
+ return false;
+
for (i = 0; i < CFI_NUM_REGS; i++)
if (state->regs[i].base != initial_func_cfi.regs[i].base ||
state->regs[i].offset != initial_func_cfi.regs[i].offset)
@@ -1987,7 +2037,7 @@ static int validate_call(struct instruction *insn, struct insn_state *state)
static int validate_sibling_call(struct instruction *insn, struct insn_state *state)
{
- if (has_modified_stack_frame(state)) {
+ if (has_modified_stack_frame(state, true)) {
WARN_FUNC("sibling call from callable instruction with modified stack frame",
insn->sec, insn->offset);
return 1;
@@ -2009,6 +2059,7 @@ static int validate_branch(struct objtool_file *file, struct symbol *func,
struct alternative *alt;
struct instruction *insn, *next_insn;
struct section *sec;
+ bool check_registers;
u8 visited;
int ret;
@@ -2130,7 +2181,28 @@ static int validate_branch(struct objtool_file *file, struct symbol *func,
return 1;
}
- if (func && has_modified_stack_frame(&state)) {
+ /*
+ * With retpoline, the return instruction is used
+ * to branch to an address stored on the stack.
+ * So when we reach the ret instruction, the stack
+ * frame has been modified with the address to
+ * branch to and we need update the stack state.
+ *
+ * The retpoline address to branch to is typically
+ * pushed on the stack from a register, but this
+ * confuses the logic which checks callee saved
+ * registers. So we don't check if registers have
+ * been modified if we have a return trampoline.
+ */
+ if (insn->retpoline_ret) {
+ update_insn_state(insn, &state);
+ check_registers = false;
+ } else {
+ check_registers = true;
+ }
+
+ if (func && has_modified_stack_frame(&state,
+ check_registers)) {
WARN_FUNC("return with modified stack frame",
sec, insn->offset);
return 1;
diff --git a/tools/objtool/check.h b/tools/objtool/check.h
index 2bd6d2f46baa..5ecd16ad71a8 100644
--- a/tools/objtool/check.h
+++ b/tools/objtool/check.h
@@ -37,6 +37,7 @@ struct instruction {
bool dead_end, ignore, hint, save, restore, ignore_alts;
bool intra_function_call;
bool retpoline_safe;
+ bool retpoline_ret;
u8 visited;
struct symbol *call_dest;
struct instruction *jump_dest;
--
2.18.2
next prev parent reply other threads:[~2020-04-02 8:18 UTC|newest]
Thread overview: 51+ messages / expand[flat|nested] mbox.gz Atom feed top
2020-04-02 8:22 [PATCH 0/7] objtool changes to remove most ANNOTATE_NOSPEC_ALTERNATIVE Alexandre Chartre
2020-04-02 8:22 ` [PATCH 1/7] objtool: is_fentry_call() crashes if call has no destination Alexandre Chartre
2020-04-02 8:22 ` [PATCH 2/7] objtool: Allow branches within the same alternative Alexandre Chartre
2020-04-02 12:03 ` Julien Thierry
2020-04-02 12:38 ` Alexandre Chartre
2020-04-02 8:22 ` [PATCH 3/7] objtool: Add support for intra-function calls Alexandre Chartre
2020-04-02 12:53 ` Julien Thierry
2020-04-02 13:24 ` Alexandre Chartre
2020-04-02 13:38 ` Julien Thierry
2020-04-02 14:56 ` Alexandre Chartre
2020-04-02 15:04 ` Peter Zijlstra
2020-04-02 15:54 ` Josh Poimboeuf
2020-04-03 7:06 ` Alexandre Chartre
2020-04-02 15:49 ` Josh Poimboeuf
2020-04-02 17:27 ` Josh Poimboeuf
2020-04-03 8:01 ` Julien Thierry
2020-04-03 12:41 ` Peter Zijlstra
2020-04-03 12:49 ` Julien Thierry
2020-04-03 14:37 ` Peter Zijlstra
2020-04-03 14:44 ` Josh Poimboeuf
2020-04-02 8:22 ` Alexandre Chartre [this message]
2020-04-02 13:26 ` [PATCH 4/7] objtool: Add support for return trampoline call Julien Thierry
2020-04-02 14:46 ` Alexandre Chartre
2020-04-02 15:31 ` Julien Thierry
2020-04-02 15:40 ` Peter Zijlstra
2020-04-03 8:11 ` Julien Thierry
2020-04-03 15:17 ` Josh Poimboeuf
2020-04-03 15:22 ` Josh Poimboeuf
2020-04-03 15:32 ` Josh Poimboeuf
2020-04-03 15:46 ` Peter Zijlstra
2020-04-03 15:55 ` Josh Poimboeuf
2020-04-04 13:32 ` Peter Zijlstra
2020-04-04 14:22 ` Josh Poimboeuf
2020-04-04 15:51 ` Peter Zijlstra
2020-04-06 8:19 ` Alexandre Chartre
2020-04-06 9:31 ` Peter Zijlstra
2020-04-06 11:03 ` Alexandre Chartre
2020-04-06 14:16 ` Josh Poimboeuf
2020-04-02 15:27 ` Peter Zijlstra
2020-04-03 7:19 ` Alexandre Chartre
2020-04-06 14:34 ` Alexandre Chartre
2020-04-06 14:55 ` Alexandre Chartre
2020-04-02 8:22 ` [PATCH 5/7] x86/speculation: Annotate intra-function calls Alexandre Chartre
2020-04-03 16:05 ` Josh Poimboeuf
2020-04-03 16:16 ` Josh Poimboeuf
2020-04-03 17:14 ` Alexandre Chartre
2020-04-03 17:18 ` Peter Zijlstra
2020-04-03 17:24 ` Josh Poimboeuf
2020-04-03 18:20 ` Peter Zijlstra
2020-04-02 8:22 ` [PATCH 6/7] x86/speculation: Annotate retpoline return instructions Alexandre Chartre
2020-04-02 8:22 ` [PATCH 7/7] x86/speculation: Remove most ANNOTATE_NOSPEC_ALTERNATIVE Alexandre Chartre
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=20200402082220.808-5-alexandre.chartre@oracle.com \
--to=alexandre.chartre@oracle.com \
--cc=jpoimboe@redhat.com \
--cc=jthierry@redhat.com \
--cc=linux-kernel@vger.kernel.org \
--cc=peterz@infradead.org \
--cc=tglx@linutronix.de \
--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
all inboxes | Powered by JetHome®