]> www.infradead.org Git - users/dwmw2/linux.git/commitdiff
objtool: Add straight-line-speculation validation
authorPeter Zijlstra <peterz@infradead.org>
Sat, 4 Dec 2021 13:43:42 +0000 (14:43 +0100)
committerGreg Kroah-Hartman <gregkh@linuxfoundation.org>
Mon, 25 Jul 2022 09:26:30 +0000 (11:26 +0200)
commit 1cc1e4c8aab4213bd4e6353dec2620476a233d6d upstream.

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.

Signed-off-by: Peter Zijlstra (Intel) <peterz@infradead.org>
Signed-off-by: Borislav Petkov <bp@suse.de>
Link: https://lore.kernel.org/r/20211204134908.023037659@infradead.org
Signed-off-by: Sasha Levin <sashal@kernel.org>
Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
[bwh: Backported to 5.10: adjust filenames, context]
Signed-off-by: Ben Hutchings <ben@decadent.org.uk>
Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
tools/objtool/arch.h
tools/objtool/arch/x86/decode.c
tools/objtool/builtin-check.c
tools/objtool/builtin.h
tools/objtool/check.c

index 0031a27b6ad00b88a5c5733b8adcc1edbc9615e3..e15f6e932b4fdc08080cff3b6d88fdcd5e8b1f86 100644 (file)
@@ -26,6 +26,7 @@ enum insn_type {
        INSN_CLAC,
        INSN_STD,
        INSN_CLD,
+       INSN_TRAP,
        INSN_OTHER,
 };
 
index 32a810429306636ec4d0afef0095ec4d18054fe2..f7154241a2a5348470383834389f0d5d89747a3e 100644 (file)
@@ -456,6 +456,11 @@ int arch_decode_instruction(const struct elf *elf, const struct section *sec,
 
                break;
 
+       case 0xcc:
+               /* int3 */
+               *type = INSN_TRAP;
+               break;
+
        case 0xe3:
                /* jecxz/jrcxz */
                *type = INSN_JUMP_CONDITIONAL;
@@ -592,10 +597,10 @@ const char *arch_ret_insn(int len)
 {
        static const char ret[5][5] = {
                { BYTE_RET },
-               { BYTE_RET, 0x90 },
-               { BYTE_RET, 0x66, 0x90 },
-               { BYTE_RET, 0x0f, 0x1f, 0x00 },
-               { BYTE_RET, 0x0f, 0x1f, 0x40, 0x00 },
+               { BYTE_RET, 0xcc },
+               { BYTE_RET, 0xcc, 0x90 },
+               { BYTE_RET, 0xcc, 0x66, 0x90 },
+               { BYTE_RET, 0xcc, 0x0f, 0x1f, 0x00 },
        };
 
        if (len < 1 || len > 5) {
index c6d199bfd0ae2a725826d7f2950aec98ca3dda83..758baf918d83aea433bb2cbff4c69e349697c173 100644 (file)
@@ -18,7 +18,8 @@
 #include "builtin.h"
 #include "objtool.h"
 
-bool no_fp, no_unreachable, retpoline, module, backtrace, uaccess, stats, validate_dup, vmlinux;
+bool no_fp, no_unreachable, retpoline, module, backtrace, uaccess, stats,
+     validate_dup, vmlinux, sls;
 
 static const char * const check_usage[] = {
        "objtool check [<options>] file.o",
@@ -35,6 +36,7 @@ const struct option check_options[] = {
        OPT_BOOLEAN('s', "stats", &stats, "print statistics"),
        OPT_BOOLEAN('d', "duplicate", &validate_dup, "duplicate validation for vmlinux.o"),
        OPT_BOOLEAN('l', "vmlinux", &vmlinux, "vmlinux.o validation"),
+       OPT_BOOLEAN('S', "sls", &sls, "validate straight-line-speculation"),
        OPT_END(),
 };
 
index 85c979caa36779b5ddc5afb321e9f3c411d7a6ce..33043fcb16db740bd2f763c93e79ab22a09d58f2 100644 (file)
@@ -8,7 +8,8 @@
 #include <subcmd/parse-options.h>
 
 extern const struct option check_options[];
-extern bool no_fp, no_unreachable, retpoline, module, backtrace, uaccess, stats, validate_dup, vmlinux;
+extern bool no_fp, no_unreachable, retpoline, module, backtrace, uaccess, stats,
+            validate_dup, vmlinux, sls;
 
 extern int cmd_check(int argc, const char **argv);
 extern int cmd_orc(int argc, const char **argv);
index 09e7807f83ee98949bda4e908cc04c92b87a1dc2..9943987b24a9d749a2547814d9d122c97eda246e 100644 (file)
@@ -2775,6 +2775,12 @@ static int validate_branch(struct objtool_file *file, struct symbol *func,
                switch (insn->type) {
 
                case INSN_RETURN:
+                       if (next_insn && next_insn->type == INSN_TRAP) {
+                               next_insn->ignore = true;
+                       } else if (sls && !insn->retpoline_safe) {
+                               WARN_FUNC("missing int3 after ret",
+                                         insn->sec, insn->offset);
+                       }
                        return validate_return(func, insn, &state);
 
                case INSN_CALL:
@@ -2818,6 +2824,14 @@ static int validate_branch(struct objtool_file *file, struct symbol *func,
                        break;
 
                case INSN_JUMP_DYNAMIC:
+                       if (next_insn && next_insn->type == INSN_TRAP) {
+                               next_insn->ignore = true;
+                       } else if (sls && !insn->retpoline_safe) {
+                               WARN_FUNC("missing int3 after indirect jump",
+                                         insn->sec, insn->offset);
+                       }
+
+                       /* fallthrough */
                case INSN_JUMP_DYNAMIC_CONDITIONAL:
                        if (is_sibling_call(insn)) {
                                ret = validate_sibling_call(insn, &state);