History

objtool: Add straight-line-speculation validation

Teach objtool to validate the straight-line-speculation constraints: - speculation trap after indirect calls - speculation trap after RET Notable: when an instruction is annotated RETPOLINE_SAFE, indicating speculation isn't a problem, also don't care about sls for that instruction.

Peter Zijlstra committed 4 years ago

Commit 1cc1e4c1 parent b17c2ba5 files · +27 −6Browse files

Unified · Split

5 files
  1. tools/objtool/arch/x86/decode.c +9 −4
  2. tools/objtool/builtin-check.c +2 −1
  3. tools/objtool/check.c +14 −0
  4. tools/objtool/include/objtool/arch.h +1 −0
  5. tools/objtool/include/objtool/builtin.h +1 −1
tools/objtool/arch/x86/decode.c+9−4
@@ -531,6 +531,11 @@ int arch_decode_instruction(struct objtool_file *file, const struct section *sec · 530 unmodified lines
531531 }
532532 break;
533533
534+ case 0xcc:
535+ /* int3 */
536+ *type = INSN_TRAP;
537+ break;
538+
534539 case 0xe3:
535540 /* jecxz/jrcxz */
536541 *type = INSN_JUMP_CONDITIONAL;
@@ -697,10 +702,10 @@ const char *arch_ret_insn(int len) · 160 unmodified lines
697702 {
698703 static const char ret[5][5] = {
699704 { BYTE_RET },
700− { BYTE_RET, BYTES_NOP1 },
701− { BYTE_RET, BYTES_NOP2 },
702− { BYTE_RET, BYTES_NOP3 },
703− { BYTE_RET, BYTES_NOP4 },
705+ { BYTE_RET, 0xcc },
706+ { BYTE_RET, 0xcc, BYTES_NOP1 },
707+ { BYTE_RET, 0xcc, BYTES_NOP2 },
708+ { BYTE_RET, 0xcc, BYTES_NOP3 },
704709 };
705710
706711 if (len < 1 || len > 5) {
tools/objtool/builtin-check.c+2−1
@@ -20,7 +20,7 @@ · 19 unmodified lines
2020 #include <objtool/objtool.h>
2121
2222 bool no_fp, no_unreachable, retpoline, module, backtrace, uaccess, stats,
23− validate_dup, vmlinux, mcount, noinstr, backup;
23+ validate_dup, vmlinux, mcount, noinstr, backup, sls;
2424
2525 static const char * const check_usage[] = {
2626 "objtool check [<options>] file.o",
@@ -45,6 +45,7 @@ const struct option check_options[] = { · 18 unmodified lines
4545 OPT_BOOLEAN('l', "vmlinux", &vmlinux, "vmlinux.o validation"),
4646 OPT_BOOLEAN('M', "mcount", &mcount, "generate __mcount_loc"),
4747 OPT_BOOLEAN('B', "backup", &backup, "create .orig files before modification"),
48+ OPT_BOOLEAN('S', "sls", &sls, "validate straight-line-speculation"),
4849 OPT_END(),
4950 };
5051
tools/objtool/check.c+14−0
@@ -3084,6 +3084,12 @@ static int validate_branch(struct objtool_file *file, struct symbol *func, · 3083 unmodified lines
30843084 switch (insn->type) {
30853085
30863086 case INSN_RETURN:
3087+ if (next_insn && next_insn->type == INSN_TRAP) {
3088+ next_insn->ignore = true;
3089+ } else if (sls && !insn->retpoline_safe) {
3090+ WARN_FUNC("missing int3 after ret",
3091+ insn->sec, insn->offset);
3092+ }
30873093 return validate_return(func, insn, &state);
30883094
30893095 case INSN_CALL:
@@ -3127,6 +3133,14 @@ static int validate_branch(struct objtool_file *file, struct symbol *func, · 37 unmodified lines
31273133 break;
31283134
31293135 case INSN_JUMP_DYNAMIC:
3136+ if (next_insn && next_insn->type == INSN_TRAP) {
3137+ next_insn->ignore = true;
3138+ } else if (sls && !insn->retpoline_safe) {
3139+ WARN_FUNC("missing int3 after indirect jump",
3140+ insn->sec, insn->offset);
3141+ }
3142+
3143+ /* fallthrough */
31303144 case INSN_JUMP_DYNAMIC_CONDITIONAL:
31313145 if (is_sibling_call(insn)) {
31323146 ret = validate_sibling_call(file, insn, &state);
tools/objtool/include/objtool/arch.h+1−0
@@ -26,6 +26,7 @@ enum insn_type { · 25 unmodified lines
2626 INSN_CLAC,
2727 INSN_STD,
2828 INSN_CLD,
29+ INSN_TRAP,
2930 INSN_OTHER,
3031 };
3132
tools/objtool/include/objtool/builtin.h+1−1
@@ -9,7 +9,7 @@ · 8 unmodified lines
99
1010 extern const struct option check_options[];
1111 extern bool no_fp, no_unreachable, retpoline, module, backtrace, uaccess, stats,
12− validate_dup, vmlinux, mcount, noinstr, backup;
12+ validate_dup, vmlinux, mcount, noinstr, backup, sls;
1313
1414 extern int cmd_parse_options(int argc, const char **argv, const char * const usage[]);
1515