perf annotate-data: Add stack canary type
authorNamhyung Kim <namhyung@kernel.org>
Tue, 19 Mar 2024 05:51:13 +0000 (22:51 -0700)
committerArnaldo Carvalho de Melo <acme@redhat.com>
Thu, 21 Mar 2024 13:41:29 +0000 (10:41 -0300)
When the stack protector is enabled, compiler would generate code to
check stack overflow with a special value called 'stack carary' at
runtime.  On x86_64, GCC hard-codes the stack canary as %gs:40.

While there's a definition of fixed_percpu_data in asm/processor.h,
it seems that the header is not included everywhere and many places
it cannot find the type info.  As it's in the well-known location (at
%gs:40), let's add a pseudo stack canary type to handle it specially.

Signed-off-by: Namhyung Kim <namhyung@kernel.org>
Cc: Adrian Hunter <adrian.hunter@intel.com>
Cc: Ian Rogers <irogers@google.com>
Cc: Ingo Molnar <mingo@kernel.org>
Cc: Jiri Olsa <jolsa@kernel.org>
Cc: Linus Torvalds <torvalds@linux-foundation.org>
Cc: Masami Hiramatsu <mhiramat@kernel.org>
Cc: Peter Zijlstra <peterz@infradead.org>
Cc: Stephane Eranian <eranian@google.com>
Link: https://lore.kernel.org/r/20240319055115.4063940-22-namhyung@kernel.org
Signed-off-by: Arnaldo Carvalho de Melo <acme@redhat.com>
tools/perf/util/annotate-data.c
tools/perf/util/annotate-data.h
tools/perf/util/annotate.c

index bd10a576cfbfea6b8cdde273617191afdb6ef3d2..633fe125fcd87979b6110ac81a15c70b205175d2 100644 (file)
@@ -30,6 +30,7 @@ enum type_state_kind {
        TSR_KIND_PERCPU_BASE,
        TSR_KIND_CONST,
        TSR_KIND_POINTER,
+       TSR_KIND_CANARY,
 };
 
 #define pr_debug_dtp(fmt, ...)                                 \
@@ -62,6 +63,9 @@ static void pr_debug_type_name(Dwarf_Die *die, enum type_state_kind kind)
                pr_info(" pointer");
                /* it also prints the type info */
                break;
+       case TSR_KIND_CANARY:
+               pr_info(" stack canary\n");
+               return;
        case TSR_KIND_TYPE:
        default:
                break;
@@ -676,6 +680,15 @@ static void update_insn_state_x86(struct type_state *state,
                         */
                        var_addr = src->offset;
 
+                       if (var_addr == 40) {
+                               tsr->kind = TSR_KIND_CANARY;
+                               tsr->ok = true;
+
+                               pr_debug_dtp("mov [%x] stack canary -> reg%d\n",
+                                            insn_offset, dst->reg1);
+                               return;
+                       }
+
                        if (!get_global_var_type(cu_die, dloc, ip, var_addr,
                                                 &offset, &type_die) ||
                            !die_get_member_type(&type_die, offset, &type_die)) {
@@ -991,6 +1004,16 @@ static void delete_var_types(struct die_var_type *var_types)
        }
 }
 
+/* should match to is_stack_canary() in util/annotate.c */
+static void setup_stack_canary(struct data_loc_info *dloc)
+{
+       if (arch__is(dloc->arch, "x86")) {
+               dloc->op->segment = INSN_SEG_X86_GS;
+               dloc->op->imm = true;
+               dloc->op->offset = 40;
+       }
+}
+
 /* It's at the target address, check if it has a matching type */
 static bool check_matching_type(struct type_state *state,
                                struct data_loc_info *dloc, int reg,
@@ -1038,6 +1061,11 @@ static bool check_matching_type(struct type_state *state,
                if (stack == NULL)
                        return false;
 
+               if (stack->kind == TSR_KIND_CANARY) {
+                       setup_stack_canary(dloc);
+                       return false;
+               }
+
                *type_die = stack->type;
                /* Update the type offset from the start of slot */
                dloc->type_offset -= stack->offset;
@@ -1062,6 +1090,11 @@ static bool check_matching_type(struct type_state *state,
                if (stack == NULL)
                        return false;
 
+               if (stack->kind == TSR_KIND_CANARY) {
+                       setup_stack_canary(dloc);
+                       return false;
+               }
+
                *type_die = stack->type;
                /* Update the type offset from the start of slot */
                dloc->type_offset -= fboff + stack->offset;
@@ -1102,6 +1135,19 @@ static bool check_matching_type(struct type_state *state,
                return true;
        }
 
+       if (state->regs[reg].ok && state->regs[reg].kind == TSR_KIND_CANARY) {
+               pr_debug_dtp(" stack canary\n");
+
+               /*
+                * This is a saved value of the stack canary which will be handled
+                * in the outer logic when it returns failure here.  Pretend it's
+                * from the stack canary directly.
+                */
+               setup_stack_canary(dloc);
+
+               return false;
+       }
+
        if (map__dso(dloc->ms->map)->kernel && arch__is(dloc->arch, "x86")) {
                u64 addr;
                int offset;
index ae0f87aed80482c6b6f83bcddc33df17532a4ca9..1b5a152163b5950bca54278274d6f337bd623f1c 100644 (file)
@@ -73,6 +73,7 @@ struct annotated_data_type {
 
 extern struct annotated_data_type unknown_type;
 extern struct annotated_data_type stackop_type;
+extern struct annotated_data_type canary_type;
 
 /**
  * struct data_loc_info - Data location information
index e4121acb4f88c5305452095a1dfa0e7a8240033c..64e54ff1aa1d122d1ed55bce5f4381d6f11a95f1 100644 (file)
@@ -118,6 +118,13 @@ struct annotated_data_type stackop_type = {
        },
 };
 
+struct annotated_data_type canary_type = {
+       .self = {
+               .type_name = (char *)"(stack canary)",
+               .children = LIST_HEAD_INIT(canary_type.self.children),
+       },
+};
+
 static int arch__grow_instructions(struct arch *arch)
 {
        struct ins *new_instructions;
@@ -3803,6 +3810,18 @@ static bool is_stack_operation(struct arch *arch, struct disasm_line *dl)
        return false;
 }
 
+static bool is_stack_canary(struct arch *arch, struct annotated_op_loc *loc)
+{
+       /* On x86_64, %gs:40 is used for stack canary */
+       if (arch__is(arch, "x86")) {
+               if (loc->segment == INSN_SEG_X86_GS && loc->imm &&
+                   loc->offset == 40)
+                       return true;
+       }
+
+       return false;
+}
+
 u64 annotate_calc_pcrel(struct map_symbol *ms, u64 ip, int offset,
                        struct disasm_line *dl)
 {
@@ -3929,6 +3948,12 @@ retry:
                }
 
                mem_type = find_data_type(&dloc);
+
+               if (mem_type == NULL && is_stack_canary(arch, op_loc)) {
+                       mem_type = &canary_type;
+                       dloc.type_offset = 0;
+               }
+
                if (mem_type)
                        istat->good++;
                else