Skip to content

Commit b5bc223

Browse files
Peter Zijlstraingomolnar
authored andcommitted
objtool: Add retpoline validation
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>
1 parent 43a4525 commit b5bc223

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
@@ -264,6 +264,10 @@ objtool_args += --no-unreachable
264264
else
265265
objtool_args += $(call cc-ifversion, -lt, 0405, --no-unreachable)
266266
endif
267+
ifdef CONFIG_RETPOLINE
268+
objtool_args += --retpoline
269+
endif
270+
267271

268272
ifdef CONFIG_MODVERSIONS
269273
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
@@ -497,6 +497,7 @@ static int add_jump_destinations(struct objtool_file *file)
497497
* disguise, so convert them accordingly.
498498
*/
499499
insn->type = INSN_JUMP_DYNAMIC;
500+
insn->retpoline_safe = true;
500501
continue;
501502
} else {
502503
/* sibling call */
@@ -548,7 +549,8 @@ static int add_call_destinations(struct objtool_file *file)
548549
if (!insn->call_dest && !insn->ignore) {
549550
WARN_FUNC("unsupported intra-function call",
550551
insn->sec, insn->offset);
551-
WARN("If this is a retpoline, please patch it in with alternatives and annotate it with ANNOTATE_NOSPEC_ALTERNATIVE.");
552+
if (retpoline)
553+
WARN("If this is a retpoline, please patch it in with alternatives and annotate it with ANNOTATE_NOSPEC_ALTERNATIVE.");
552554
return -1;
553555
}
554556

@@ -1108,6 +1110,54 @@ static int read_unwind_hints(struct objtool_file *file)
11081110
return 0;
11091111
}
11101112

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

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

@@ -1891,6 +1945,29 @@ static int validate_unwind_hints(struct objtool_file *file)
18911945
return warnings;
18921946
}
18931947

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

2131+
if (retpoline) {
2132+
ret = validate_retpoline(&file);
2133+
if (ret < 0)
2134+
return ret;
2135+
warnings += ret;
2136+
}
2137+
20542138
ret = validate_functions(&file);
20552139
if (ret < 0)
20562140
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)