Skip to content

Commit e25eea8

Browse files
Peter Zijlstraingomolnar
authored andcommitted
objtool: Introduce HINT_RET_OFFSET
Normally objtool ensures a function keeps the stack layout invariant. But there is a useful exception, it is possible to stuff the return stack in order to 'inject' a 'call': push $fun ret In this case the invariant mentioned above is violated. Add an objtool HINT to annotate this and allow a function exit with a modified stack frame. Signed-off-by: Peter Zijlstra (Intel) <peterz@infradead.org> Reviewed-by: Miroslav Benes <mbenes@suse.cz> Reviewed-by: Alexandre Chartre <alexandre.chartre@oracle.com> Acked-by: Josh Poimboeuf <jpoimboe@redhat.com> Link: https://lkml.kernel.org/r/20200416115118.690601403@infradead.org Signed-off-by: Ingo Molnar <mingo@kernel.org>
1 parent b746046 commit e25eea8

5 files changed

Lines changed: 31 additions & 9 deletions

File tree

‎arch/x86/include/asm/orc_types.h‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@
6060
#define ORC_TYPE_REGS_IRET 2
6161
#define UNWIND_HINT_TYPE_SAVE 3
6262
#define UNWIND_HINT_TYPE_RESTORE 4
63+
#define UNWIND_HINT_TYPE_RET_OFFSET 5
6364

6465
#ifndef __ASSEMBLY__
6566
/*

‎arch/x86/include/asm/unwind_hints.h‎

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,16 @@
9494
UNWIND_HINT type=UNWIND_HINT_TYPE_RESTORE
9595
.endm
9696

97+
98+
/*
99+
* RET_OFFSET: Used on instructions that terminate a function; mostly RETURN
100+
* and sibling calls. On these, sp_offset denotes the expected offset from
101+
* initial_func_cfi.
102+
*/
103+
.macro UNWIND_HINT_RET_OFFSET sp_offset=8
104+
UNWIND_HINT type=UNWIND_HINT_TYPE_RET_OFFSET sp_offset=\sp_offset
105+
.endm
106+
97107
#else /* !__ASSEMBLY__ */
98108

99109
#define UNWIND_HINT(sp_reg, sp_offset, type, end) \

‎tools/arch/x86/include/asm/orc_types.h‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@
6060
#define ORC_TYPE_REGS_IRET 2
6161
#define UNWIND_HINT_TYPE_SAVE 3
6262
#define UNWIND_HINT_TYPE_RESTORE 4
63+
#define UNWIND_HINT_TYPE_RET_OFFSET 5
6364

6465
#ifndef __ASSEMBLY__
6566
/*

‎tools/objtool/check.c‎

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1261,6 +1261,9 @@ static int read_unwind_hints(struct objtool_file *file)
12611261
} else if (hint->type == UNWIND_HINT_TYPE_RESTORE) {
12621262
insn->restore = true;
12631263
insn->hint = true;
1264+
1265+
} else if (hint->type == UNWIND_HINT_TYPE_RET_OFFSET) {
1266+
insn->ret_offset = hint->sp_offset;
12641267
continue;
12651268
}
12661269

@@ -1424,20 +1427,25 @@ static bool is_fentry_call(struct instruction *insn)
14241427
return false;
14251428
}
14261429

1427-
static bool has_modified_stack_frame(struct insn_state *state)
1430+
static bool has_modified_stack_frame(struct instruction *insn, struct insn_state *state)
14281431
{
1432+
u8 ret_offset = insn->ret_offset;
14291433
int i;
14301434

1431-
if (state->cfa.base != initial_func_cfi.cfa.base ||
1432-
state->cfa.offset != initial_func_cfi.cfa.offset ||
1433-
state->stack_size != initial_func_cfi.cfa.offset ||
1434-
state->drap)
1435+
if (state->cfa.base != initial_func_cfi.cfa.base || state->drap)
1436+
return true;
1437+
1438+
if (state->cfa.offset != initial_func_cfi.cfa.offset + ret_offset)
14351439
return true;
14361440

1437-
for (i = 0; i < CFI_NUM_REGS; i++)
1441+
if (state->stack_size != initial_func_cfi.cfa.offset + ret_offset)
1442+
return true;
1443+
1444+
for (i = 0; i < CFI_NUM_REGS; i++) {
14381445
if (state->regs[i].base != initial_func_cfi.regs[i].base ||
14391446
state->regs[i].offset != initial_func_cfi.regs[i].offset)
14401447
return true;
1448+
}
14411449

14421450
return false;
14431451
}
@@ -2014,7 +2022,7 @@ static int validate_call(struct instruction *insn, struct insn_state *state)
20142022

20152023
static int validate_sibling_call(struct instruction *insn, struct insn_state *state)
20162024
{
2017-
if (has_modified_stack_frame(state)) {
2025+
if (has_modified_stack_frame(insn, state)) {
20182026
WARN_FUNC("sibling call from callable instruction with modified stack frame",
20192027
insn->sec, insn->offset);
20202028
return 1;
@@ -2043,7 +2051,7 @@ static int validate_return(struct symbol *func, struct instruction *insn, struct
20432051
return 1;
20442052
}
20452053

2046-
if (func && has_modified_stack_frame(state)) {
2054+
if (func && has_modified_stack_frame(insn, state)) {
20472055
WARN_FUNC("return with modified stack frame",
20482056
insn->sec, insn->offset);
20492057
return 1;

‎tools/objtool/check.h‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,9 +33,11 @@ struct instruction {
3333
unsigned int len;
3434
enum insn_type type;
3535
unsigned long immediate;
36-
bool alt_group, dead_end, ignore, hint, save, restore, ignore_alts;
36+
bool alt_group, dead_end, ignore, ignore_alts;
37+
bool hint, save, restore;
3738
bool retpoline_safe;
3839
u8 visited;
40+
u8 ret_offset;
3941
struct symbol *call_dest;
4042
struct instruction *jump_dest;
4143
struct instruction *first_jump_src;

0 commit comments

Comments
 (0)