Skip to content

Commit e699314

Browse files
Peter Zijlstragregkh
authored andcommitted
objtool: Add retpoline validation
commit b5bc223 upstream. David requested a objtool validation pass for CONFIG_RETPOLINE=y enabled builds, where it validates no unannotated indirect jumps or calls are left. Add an additional .discard.retpoline_safe section to allow annotating the few indirect sites that are required and safe. Requested-by: David Woodhouse <dwmw2@infradead.org> Signed-off-by: Peter Zijlstra (Intel) <peterz@infradead.org> Reviewed-by: David Woodhouse <dwmw@amazon.co.uk> Acked-by: Thomas Gleixner <tglx@linutronix.de> Acked-by: Josh Poimboeuf <jpoimboe@redhat.com> Cc: Andy Lutomirski <luto@kernel.org> Cc: Arjan van de Ven <arjan@linux.intel.com> Cc: Borislav Petkov <bp@alien8.de> Cc: Dan Williams <dan.j.williams@intel.com> Cc: Dave Hansen <dave.hansen@linux.intel.com> Cc: Greg Kroah-Hartman <gregkh@linuxfoundation.org> Cc: Linus Torvalds <torvalds@linux-foundation.org> Cc: Peter Zijlstra <peterz@infradead.org> Signed-off-by: Ingo Molnar <mingo@kernel.org> Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
1 parent 8c27681 commit e699314

5 files changed

Lines changed: 93 additions & 3 deletions

File tree

‎scripts/Makefile.build‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -269,6 +269,10 @@ objtool_args += --no-unreachable
269269
else
270270
objtool_args += $(call cc-ifversion, -lt, 0405, --no-unreachable)
271271
endif
272+
ifdef CONFIG_RETPOLINE
273+
objtool_args += --retpoline
274+
endif
275+
272276

273277
ifdef CONFIG_MODVERSIONS
274278
objtool_o = $(@D)/.tmp_$(@F)

‎tools/objtool/builtin-check.c‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@
2929
#include "builtin.h"
3030
#include "check.h"
3131

32-
bool no_fp, no_unreachable;
32+
bool no_fp, no_unreachable, retpoline;
3333

3434
static const char * const check_usage[] = {
3535
"objtool check [<options>] file.o",
@@ -39,6 +39,7 @@ static const char * const check_usage[] = {
3939
const struct option check_options[] = {
4040
OPT_BOOLEAN('f', "no-fp", &no_fp, "Skip frame pointer validation"),
4141
OPT_BOOLEAN('u', "no-unreachable", &no_unreachable, "Skip 'unreachable instruction' warnings"),
42+
OPT_BOOLEAN('r', "retpoline", &retpoline, "Validate retpoline assumptions"),
4243
OPT_END(),
4344
};
4445

‎tools/objtool/builtin.h‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
#include <subcmd/parse-options.h>
2121

2222
extern const struct option check_options[];
23-
extern bool no_fp, no_unreachable;
23+
extern bool no_fp, no_unreachable, retpoline;
2424

2525
extern int cmd_check(int argc, const char **argv);
2626
extern int cmd_orc(int argc, const char **argv);

‎tools/objtool/check.c‎

Lines changed: 85 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -496,6 +496,7 @@ static int add_jump_destinations(struct objtool_file *file)
496496
* disguise, so convert them accordingly.
497497
*/
498498
insn->type = INSN_JUMP_DYNAMIC;
499+
insn->retpoline_safe = true;
499500
continue;
500501
} else {
501502
/* sibling call */
@@ -547,7 +548,8 @@ static int add_call_destinations(struct objtool_file *file)
547548
if (!insn->call_dest && !insn->ignore) {
548549
WARN_FUNC("unsupported intra-function call",
549550
insn->sec, insn->offset);
550-
WARN("If this is a retpoline, please patch it in with alternatives and annotate it with ANNOTATE_NOSPEC_ALTERNATIVE.");
551+
if (retpoline)
552+
WARN("If this is a retpoline, please patch it in with alternatives and annotate it with ANNOTATE_NOSPEC_ALTERNATIVE.");
551553
return -1;
552554
}
553555

@@ -1107,6 +1109,54 @@ static int read_unwind_hints(struct objtool_file *file)
11071109
return 0;
11081110
}
11091111

1112+
static int read_retpoline_hints(struct objtool_file *file)
1113+
{
1114+
struct section *sec, *relasec;
1115+
struct instruction *insn;
1116+
struct rela *rela;
1117+
int i;
1118+
1119+
sec = find_section_by_name(file->elf, ".discard.retpoline_safe");
1120+
if (!sec)
1121+
return 0;
1122+
1123+
relasec = sec->rela;
1124+
if (!relasec) {
1125+
WARN("missing .rela.discard.retpoline_safe section");
1126+
return -1;
1127+
}
1128+
1129+
if (sec->len % sizeof(unsigned long)) {
1130+
WARN("retpoline_safe size mismatch: %d %ld", sec->len, sizeof(unsigned long));
1131+
return -1;
1132+
}
1133+
1134+
for (i = 0; i < sec->len / sizeof(unsigned long); i++) {
1135+
rela = find_rela_by_dest(sec, i * sizeof(unsigned long));
1136+
if (!rela) {
1137+
WARN("can't find rela for retpoline_safe[%d]", i);
1138+
return -1;
1139+
}
1140+
1141+
insn = find_insn(file, rela->sym->sec, rela->addend);
1142+
if (!insn) {
1143+
WARN("can't find insn for retpoline_safe[%d]", i);
1144+
return -1;
1145+
}
1146+
1147+
if (insn->type != INSN_JUMP_DYNAMIC &&
1148+
insn->type != INSN_CALL_DYNAMIC) {
1149+
WARN_FUNC("retpoline_safe hint not a indirect jump/call",
1150+
insn->sec, insn->offset);
1151+
return -1;
1152+
}
1153+
1154+
insn->retpoline_safe = true;
1155+
}
1156+
1157+
return 0;
1158+
}
1159+
11101160
static int decode_sections(struct objtool_file *file)
11111161
{
11121162
int ret;
@@ -1145,6 +1195,10 @@ static int decode_sections(struct objtool_file *file)
11451195
if (ret)
11461196
return ret;
11471197

1198+
ret = read_retpoline_hints(file);
1199+
if (ret)
1200+
return ret;
1201+
11481202
return 0;
11491203
}
11501204

@@ -1890,6 +1944,29 @@ static int validate_unwind_hints(struct objtool_file *file)
18901944
return warnings;
18911945
}
18921946

1947+
static int validate_retpoline(struct objtool_file *file)
1948+
{
1949+
struct instruction *insn;
1950+
int warnings = 0;
1951+
1952+
for_each_insn(file, insn) {
1953+
if (insn->type != INSN_JUMP_DYNAMIC &&
1954+
insn->type != INSN_CALL_DYNAMIC)
1955+
continue;
1956+
1957+
if (insn->retpoline_safe)
1958+
continue;
1959+
1960+
WARN_FUNC("indirect %s found in RETPOLINE build",
1961+
insn->sec, insn->offset,
1962+
insn->type == INSN_JUMP_DYNAMIC ? "jump" : "call");
1963+
1964+
warnings++;
1965+
}
1966+
1967+
return warnings;
1968+
}
1969+
18931970
static bool is_kasan_insn(struct instruction *insn)
18941971
{
18951972
return (insn->type == INSN_CALL &&
@@ -2050,6 +2127,13 @@ int check(const char *_objname, bool orc)
20502127
if (list_empty(&file.insn_list))
20512128
goto out;
20522129

2130+
if (retpoline) {
2131+
ret = validate_retpoline(&file);
2132+
if (ret < 0)
2133+
return ret;
2134+
warnings += ret;
2135+
}
2136+
20532137
ret = validate_functions(&file);
20542138
if (ret < 0)
20552139
goto out;

‎tools/objtool/check.h‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ struct instruction {
4545
unsigned char type;
4646
unsigned long immediate;
4747
bool alt_group, visited, dead_end, ignore, hint, save, restore, ignore_alts;
48+
bool retpoline_safe;
4849
struct symbol *call_dest;
4950
struct instruction *jump_dest;
5051
struct instruction *first_jump_src;

0 commit comments

Comments
 (0)