Skip to content

Commit 0c314a8

Browse files
jpoimboeingomolnar
authored andcommitted
objtool: Fix stack overflow in validate_branch()
On an allmodconfig kernel compiled with Clang, objtool is segfaulting in drivers/scsi/qla2xxx/qla2xxx.o due to a stack overflow in validate_branch(). Due in part to KASAN being enabled, the qla2xxx code has a large number of conditional jumps, causing objtool to go quite deep in its recursion. By far the biggest offender of stack usage is the recently added 'prev_state' stack variable in validate_insn(), coming in at 328 bytes. Move that variable (and its tracing usage) to handle_insn_ops() and make handle_insn_ops() noinline to keep its stack frame outside the recursive call chain. Reported-by: Nathan Chancellor <nathan@kernel.org> Fixes: fcb268b ("objtool: Trace instruction state changes during function validation") Signed-off-by: Josh Poimboeuf <jpoimboe@kernel.org> Signed-off-by: Ingo Molnar <mingo@kernel.org> Link: https://patch.msgid.link/21bb161c23ca0d8c942a960505c0d327ca2dc7dc.1764691895.git.jpoimboe@kernel.org Closes: https://lore.kernel.org/20251201202329.GA3225984@ax162
1 parent 4a26e70 commit 0c314a8

1 file changed

Lines changed: 13 additions & 14 deletions

File tree

tools/objtool/check.c

Lines changed: 13 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -3282,18 +3282,19 @@ static int propagate_alt_cfi(struct objtool_file *file, struct instruction *insn
32823282
return 0;
32833283
}
32843284

3285-
static int handle_insn_ops(struct instruction *insn,
3286-
struct instruction *next_insn,
3287-
struct insn_state *state)
3285+
static int noinline handle_insn_ops(struct instruction *insn,
3286+
struct instruction *next_insn,
3287+
struct insn_state *state)
32883288
{
3289+
struct insn_state prev_state __maybe_unused = *state;
32893290
struct stack_op *op;
3290-
int ret;
3291+
int ret = 0;
32913292

32923293
for (op = insn->stack_ops; op; op = op->next) {
32933294

32943295
ret = update_cfi_state(insn, next_insn, &state->cfi, op);
32953296
if (ret)
3296-
return ret;
3297+
goto done;
32973298

32983299
if (!opts.uaccess || !insn->alt_group)
32993300
continue;
@@ -3303,7 +3304,8 @@ static int handle_insn_ops(struct instruction *insn,
33033304
state->uaccess_stack = 1;
33043305
} else if (state->uaccess_stack >> 31) {
33053306
WARN_INSN(insn, "PUSHF stack exhausted");
3306-
return 1;
3307+
ret = 1;
3308+
goto done;
33073309
}
33083310
state->uaccess_stack <<= 1;
33093311
state->uaccess_stack |= state->uaccess;
@@ -3319,7 +3321,10 @@ static int handle_insn_ops(struct instruction *insn,
33193321
}
33203322
}
33213323

3322-
return 0;
3324+
done:
3325+
TRACE_INSN_STATE(insn, &prev_state, state);
3326+
3327+
return ret;
33233328
}
33243329

33253330
static bool insn_cfi_match(struct instruction *insn, struct cfi_state *cfi2)
@@ -3694,8 +3699,6 @@ static int validate_insn(struct objtool_file *file, struct symbol *func,
36943699
struct instruction *prev_insn, struct instruction *next_insn,
36953700
bool *dead_end)
36963701
{
3697-
/* prev_state and alt_name are not used if there is no disassembly support */
3698-
struct insn_state prev_state __maybe_unused;
36993702
char *alt_name __maybe_unused = NULL;
37003703
struct alternative *alt;
37013704
u8 visited;
@@ -3798,11 +3801,7 @@ static int validate_insn(struct objtool_file *file, struct symbol *func,
37983801
if (skip_alt_group(insn))
37993802
return 0;
38003803

3801-
prev_state = *statep;
3802-
ret = handle_insn_ops(insn, next_insn, statep);
3803-
TRACE_INSN_STATE(insn, &prev_state, statep);
3804-
3805-
if (ret)
3804+
if (handle_insn_ops(insn, next_insn, statep))
38063805
return 1;
38073806

38083807
switch (insn->type) {

0 commit comments

Comments
 (0)