objtool: Extend CFA updating/checking
cover objtool: Extend CFA updating/checking
Hi,
The following patches are the result of limitation found on the CFA management code when trying to validate arm64 frames. I tried to keep things simple and not contradict current CFA management logic nor introduce too many corner cases.
The patches apply on top of the cleanup series[1] I sent previously.
[1] https://lkml.org/lkml/2020/9/15/199
Thanks,
Julien
-->
Julien Thierry (3):
objtool: check: Fully validate the stack frame
objtool: check: Support addition to set CFA base
objtool: check: Make SP memory operation match PUSH/POP semantics
tools/objtool/arch/x86/include/cfi_regs.h | 4 ++ tools/objtool/check.c | 47 +++++++++++++++++++++-- 2 files changed, 47 insertions(+), 4 deletions(-)
--
2.21.3
PATCH 1/3 objtool: check: Fully validate the stack frame
A valid stack frame should contain both the return address and the previous frame pointer value.
On x86, the return value is placed on the stack by the calling instructions. On other architectures, the callee need to explicitly save the return value on the stack.
Add the necessary checks to verify a function properly sets up the all the elements of the stack frame.
--- tools/objtool/arch/x86/include/cfi_regs.h | 4 ++++ tools/objtool/check.c | 17 +++++++++++++---- 2 files changed, 17 insertions(+), 4 deletions(-)
tools/objtool/arch/x86/include/cfi_regs.h +4 -0
@@ -22,4 +22,8 @@#define CFI_RA 16#define CFI_NUM_REGS 17 +#define CFA_SIZE 16 +#define CFA_BP_OFFSET -16 +#define CFA_RA_OFFSET -8 +#endif /* _OBJTOOL_CFI_REGS_H */
tools/objtool/check.c +13 -4
@@ -1669,12 +1669,20 @@ static bool has_modified_stack_frame(struct instruction *insn, struct insn_statereturn false; } +static bool check_reg_frame_pos (const struct cfi_reg *reg,int cfa_start, +int expected_offset) +{ +return reg->base ==CFI_CFA && + reg->offset == cfa_start + expected_offset; +} +static bool has_valid_stack_frame (struct insn_state *state) {struct cfi_state *cfi = &state->cfi; -if (cfi->cfa.base ==CFI_BP && cfi->regs[CFI_BP ].base ==CFI_CFA && - cfi->regs[CFI_BP ].offset ==-16 ) +if (cfi->cfa.base ==CFI_BP && cfi->cfa.offset >=CFA_SIZE && +check_reg_frame_pos (&cfi->regs[CFI_BP ], -cfi->cfa.offset +CFA_SIZE ,CFA_BP_OFFSET ) && +check_reg_frame_pos (&cfi->regs[CFI_RA ], -cfi->cfa.offset +CFA_SIZE ,CFA_RA_OFFSET ))return true;if (cfi->drap && cfi->regs[CFI_BP ].base ==CFI_BP )
@@ -1803,8 +1811,9 @@ static int update_cfi_state(struct instruction *insn, struct cfi_state *cfi,case OP_SRC_REG :if (op->src.reg ==CFI_SP && op->dest.reg ==CFI_BP && cfa->base ==CFI_SP && - regs[CFI_BP ].base ==CFI_CFA && - regs[CFI_BP ].offset == -cfa->offset) { +check_reg_frame_pos (®s[CFI_BP ], + -cfa->offset +CFA_SIZE , +CFA_BP_OFFSET )) {/* mov %rsp, %rbp */ cfa->base = op->dest.reg;
Signature · 1 line
2.21.3
Re: [PATCH 1/3] objtool: check: Fully validate the stack frame
Quoted 6 lines · Julien Thierry
A valid stack frame should contain both the return address and the previous frame pointer value.
On x86, the return value is placed on the stack by the calling instructions. On other architectures, the callee need to explicitly save the return value on the stack.
s/return value/return address/g
Quoted 2 lines
Add the necessary checks to verify a function properly sets up the all
s/the all/all the/
Quoted 13 lines
the elements of the stack frame.
Signed-off-by Julien Thierry <[email protected]>--- tools/objtool/arch/x86/include/cfi_regs.h | 4 ++++ tools/objtool/check.c | 17 +++++++++++++---- 2 files changed, 17 insertions(+), 4 deletions(-)tools/objtool/arch/x86/include/cfi_regs.h +0
-0
tools/objtool/arch/x86/include/cfi_regs.h line 25
Quoted 4 lines
tools/objtool/arch/x86/include/cfi_regs.h +1
-0@@ -22,4 +22,8 @@#define CFI_RA 16#define CFI_NUM_REGS 17 +#define CFA_SIZE 16
If I remember correctly, CFA (stolen from DWARF) is "Caller Frame Address". It's the stack address of the caller, before the call.
I get the feeling CFA_SIZE is the wrong name, because CFA is an address, and its size isn't 16 bytes. But I'm not quite sure what this is supposed to represent. Is it supposed to be the size of the frame pointer + return address? Isn't that always going to be 16 bytes for both arches?
Quoted 16 lines
tools/objtool/arch/x86/include/cfi_regs.h +3
-0+#define CFA_BP_OFFSET -16 +#define CFA_RA_OFFSET -8 +#endif /* _OBJTOOL_CFI_REGS_H */ tools/objtool/check.c +4
-0@@ -1669,12 +1669,20 @@ static bool has_modified_stack_frame(struct instruction *insn, struct insn_statereturn false; } +static bool check_reg_frame_pos (const struct cfi_reg *reg,int cfa_start, +int expected_offset) +{ + return reg->base == CFI_CFA &&
tools/objtool/check.c static bool has_modified_stack_frame(struct instruction *insn, struct insn_state · line 1683
Quoted 10 lines
tools/objtool/check.c +4
-2+ reg->offset == cfa_start + expected_offset; +} +static bool has_valid_stack_frame (struct insn_state *state) {struct cfi_state *cfi = &state->cfi; -if (cfi->cfa.base ==CFI_BP && cfi->regs[CFI_BP ].base ==CFI_CFA && - cfi->regs[CFI_BP ].offset ==-16 ) + if (cfi->cfa.base == CFI_BP && cfi->cfa.offset >= CFA_SIZE &&
Why '>=' rather than '=='?
tools/objtool/check.c static bool has_modified_stack_frame(struct instruction *insn, struct insn_state · line 1685
Quoted 2 lines
tools/objtool/check.c +2
-0+check_reg_frame_pos (&cfi->regs[CFI_BP ], -cfi->cfa.offset +CFA_SIZE ,CFA_BP_OFFSET ) && + check_reg_frame_pos(&cfi->regs[CFI_RA], -cfi->cfa.offset + CFA_SIZE, CFA_RA_OFFSET))
Isn't '-cfi->cfa.offset + CFA_SIZE' always going to be zero?
Signature · 1 line
Josh
Re: [PATCH 1/3] objtool: check: Fully validate the stack frame
Quoted 31 lines · Josh Poimboeuf
Quoted 6 lines · Julien Thierry
A valid stack frame should contain both the return address and the previous frame pointer value.
On x86, the return value is placed on the stack by the calling instructions. On other architectures, the callee need to explicitly save the return value on the stack.
s/return value/return address/g
Quoted 2 lines
Add the necessary checks to verify a function properly sets up the all
s/the all/all the/
Quoted 16 lines
the elements of the stack frame.
Signed-off-by Julien Thierry <[email protected]>--- tools/objtool/arch/x86/include/cfi_regs.h | 4 ++++ tools/objtool/check.c | 17 +++++++++++++---- 2 files changed, 17 insertions(+), 4 deletions(-)tools/objtool/arch/x86/include/cfi_regs.h +0
-0@@ -22,4 +22,8 @@#define CFI_RA 16#define CFI_NUM_REGS 17
Quoted 5 lines
tools/objtool/arch/x86/include/cfi_regs.h line 25
Quoted 1 line
tools/objtool/arch/x86/include/cfi_regs.h +1
-0+#define CFA_SIZE 16If I remember correctly, CFA (stolen from DWARF) is "Caller Frame Address". It's the stack address of the caller, before the call.
Ok, so maybe I'm mixing Call Frame and Stack Frame (frame pointer + return address).
Quoted 6 lines
I get the feeling
CFA_SIZEis the wrong name, because CFA is an address, and its size isn't 16 bytes. But I'm not quite sure what this is supposed to represent. Is it supposed to be the size of the frame pointer + return address? Isn't that always going to be 16 bytes for both arches?
For arm64 and x86_64 it is. Maybe it is a bit early to consider it might differ for other arches (e.g. 32bit arches?).
Quoted 23 lines
Quoted 23 lines
tools/objtool/arch/x86/include/cfi_regs.h +3
-0+#define CFA_BP_OFFSET -16 +#define CFA_RA_OFFSET -8 +#endif /* _OBJTOOL_CFI_REGS_H */ tools/objtool/check.c +7
-0@@ -1669,12 +1669,20 @@ static bool has_modified_stack_frame(struct instruction *insn, struct insn_statereturn false; } +static bool check_reg_frame_pos (const struct cfi_reg *reg,int cfa_start, +int expected_offset) +{ +return reg->base ==CFI_CFA && + reg->offset == cfa_start + expected_offset; +} +static bool has_valid_stack_frame (struct insn_state *state) {struct cfi_state *cfi = &state->cfi;
Quoted 6 lines
tools/objtool/check.c static bool has_modified_stack_frame(struct instruction *insn, struct insn_state · line 1683
Quoted 3 lines
tools/objtool/check.c +1
-2-if (cfi->cfa.base ==CFI_BP && cfi->regs[CFI_BP ].base ==CFI_CFA && - cfi->regs[CFI_BP ].offset ==-16 ) + if (cfi->cfa.base == CFI_BP && cfi->cfa.offset >= CFA_SIZE &&Why '>=' rather than '=='?
Because on arm64 the stack frame is not necessarily the first thing put on the stack by the callee. The callee is free to create the stack frame where it wants (on its part of the stack of course) as long as it appropriately sets the frame pointer before making a call.
You could have something like:
+------------+ | | | | +------------+----> f1() called | | | some callee| | saved regs | | | +------------+ | RA | +------------+ | BP/FP | +------------+----> Set new BP/FP value
Quoted 5 lines
tools/objtool/check.c static bool has_modified_stack_frame(struct instruction *insn, struct insn_state · line 1685
Quoted 2 lines
tools/objtool/check.c +2
-0+check_reg_frame_pos (&cfi->regs[CFI_BP ], -cfi->cfa.offset +CFA_SIZE ,CFA_BP_OFFSET ) && + check_reg_frame_pos(&cfi->regs[CFI_RA], -cfi->cfa.offset + CFA_SIZE, CFA_RA_OFFSET))Isn't '-cfi->cfa.offset +
CFA_SIZE' always going to be zero?
For x86 yes, for arm64 it cfa.offset can be > 16.
Signature · 1 line
Julien Thierry
Re: [PATCH 1/3] objtool: check: Fully validate the stack frame
Quoted 20 lines · Julien Thierry
Quoted 9 lines
tools/objtool/arch/x86/include/cfi_regs.h line 24
Quoted 5 lines
+++
b/tools/objtool/arch/x86/include/cfi_regs.htools/objtool/arch/x86/include/cfi_regs.h +1
-0@@ -22,4 +22,8 @@#define CFI_RA 16#define CFI_NUM_REGS 17 +#define CFA_SIZE 16If I remember correctly, CFA (stolen from DWARF) is "Caller Frame Address". It's the stack address of the caller, before the call.
Ok, so maybe I'm mixing Call Frame and Stack Frame (frame pointer + return address).
Quoted 6 lines
I get the feeling
CFA_SIZEis the wrong name, because CFA is an address, and its size isn't 16 bytes. But I'm not quite sure what this is supposed to represent. Is it supposed to be the size of the frame pointer + return address? Isn't that always going to be 16 bytes for both arches?
Quoted 2 lines
For arm64 and
x86_64it is. Maybe it is a bit early to consider it might differ for other arches (e.g. 32bit arches?).
I'd rather not consider other arches yet. Even in the 32-bit case it wouldn't necessarily have to be an arch-specific value since it would presumably be 'size(long) * 2'.
Quoted 18 lines
Quoted 10 lines
tools/objtool/check.c static bool has_modified_stack_frame(struct instruction *insn, struct insn_state · line 1683
Quoted 7 lines
tools/objtool/check.c +1
-2static bool has_valid_stack_frame (struct insn_state *state) {struct cfi_state *cfi = &state->cfi; -if (cfi->cfa.base ==CFI_BP && cfi->regs[CFI_BP ].base ==CFI_CFA && - cfi->regs[CFI_BP ].offset ==-16 ) + if (cfi->cfa.base == CFI_BP && cfi->cfa.offset >= CFA_SIZE &&Why '>=' rather than '=='?
Because on arm64 the stack frame is not necessarily the first thing put on the stack by the callee. The callee is free to create the stack frame where it wants (on its part of the stack of course) as long as it appropriately sets the frame pointer before making a call.
You could have something like:
Quoted 13 lines
+------------+ | | | | +------------+----> f1() called | | | some callee| | saved regs | | | +------------+ | RA | +------------+ | BP/FP | +------------+----> Set new BP/FP value
I see, thanks.
Signature · 1 line
Josh
PATCH 2/3 objtool: check: Support addition to set CFA base
In reply to Julien ThierryInstead of "mov SP, BP", a compiler could simply set BP to SP + constant. Handle changing the CFA base in such cases.
--- tools/objtool/check.c | 13 +++++++++++++ 1 file changed, 13 insertions(+)
tools/objtool/check.c +13 -0
@@ -1898,6 +1898,19 @@ static int update_cfi_state(struct instruction *insn, struct cfi_state *cfi,break ; } +if (!cfi->drap && op->src.reg ==CFI_SP && + op->dest.reg ==CFI_BP && cfa->base ==CFI_SP && +check_reg_frame_pos (®s[CFI_BP ], + -cfa->offset + op->src.offset +CFA_SIZE , +CFA_BP_OFFSET )) { + +/* lea disp(%rsp), %rbp */ + cfa->base =CFI_BP ; + cfa->offset -= op->src.offset; + cfi->bp_scratch = false; +break ; + } +if (op->src.reg ==CFI_SP && cfa->base ==CFI_SP ) {/* drap: lea disp(%rsp), %drap */
Signature · 1 line
2.21.3
Re: [PATCH 2/3] objtool: check: Support addition to set CFA base
Quoted 2 lines · Julien Thierry
Instead of "mov SP, BP", a compiler could simply set BP to SP + constant. Handle changing the CFA base in such cases.
Rather than what a compiler _could_ do, it would be good to be specific about when this can happen (presumably arm64).
Signature · 1 line
Josh
PATCH 3/3 objtool: check: Make SP memory operation match PUSH/POP semantics
In reply to Julien ThierryArchitectures without PUSH/POP instructions will always access the stack though memory operations (SRC/DEST_INDIRECT). Make those operations have the same effect on the CFA as PUSH/POP, with no stack pointer modification.
--- tools/objtool/check.c | 17 +++++++++++++++++ 1 file changed, 17 insertions(+)
tools/objtool/check.c +17 -0
@@ -2005,6 +2005,13 @@ static int update_cfi_state(struct instruction *insn, struct cfi_state *cfi,break ;case OP_SRC_REG_INDIRECT : +if (!cfi->drap && op->dest.reg == cfa->base) { + +/* mov disp(%rsp), %rbp */ + cfa->base =CFI_SP ; + cfa->offset = cfi->stack_size; + } +if (cfi->drap && op->src.reg ==CFI_BP && op->src.offset == cfi->drap_offset) {
@@ -2026,6 +2033,11 @@ static int update_cfi_state(struct instruction *insn, struct cfi_state *cfi,/* mov disp(%rbp), %reg */ /* mov disp(%rsp), %reg */ restore_reg (cfi, op->dest.reg); + }else if (op->src.reg ==CFI_SP && + op->src.offset == regs[op->dest.reg].offset + cfi->stack_size) { + +/* mov disp(%rsp), %reg */ +restore_reg (cfi, op->dest.reg); }break ;
@@ -2103,6 +2115,11 @@ static int update_cfi_state(struct instruction *insn, struct cfi_state *cfi,/* mov reg, disp(%rsp) */ save_reg (cfi, op->src.reg,CFI_CFA , op->dest.offset - cfi->cfa.offset); + }else if (op->dest.reg ==CFI_SP ) { + +/* mov reg, disp(%rsp) */ +save_reg (cfi, op->src.reg,CFI_CFA , + op->dest.offset - cfi->stack_size); }break ;
Signature · 1 line
2.21.3
Re: [PATCH 3/3] objtool: check: Make SP memory operation match PUSH/POP semantics
Quoted 15 lines · Julien Thierry
Architectures without PUSH/POP instructions will always access the stack though memory operations (SRC/DEST_INDIRECT). Make those operations have the same effect on the CFA as PUSH/POP, with no stack pointer modification.
Signed-off-by Julien Thierry <[email protected]>--- tools/objtool/check.c | 17 +++++++++++++++++ 1 file changed, 17 insertions(+)tools/objtool/check.c +0
-0
tools/objtool/check.c static int update_cfi_state(struct instruction *insn, struct cfi_state *cfi, · line 2008
Quoted 4 lines
tools/objtool/check.c +1
-0@@ -2005,6 +2005,13 @@ static int update_cfi_state(struct instruction *insn, struct cfi_state *cfi,break ;case OP_SRC_REG_INDIRECT : +if (!cfi->drap && op->dest.reg == cfa->base) {
&& op->dest.reg == CFI_BP ?
Quoted 10 lines
tools/objtool/check.c +6
-0+ +/* mov disp(%rsp), %rbp */ + cfa->base =CFI_SP ; + cfa->offset = cfi->stack_size; + } +if (cfi->drap && op->src.reg ==CFI_BP && op->src.offset == cfi->drap_offset) {
tools/objtool/check.c static int update_cfi_state(struct instruction *insn, struct cfi_state *cfi, · line 2036
Quoted 4 lines
tools/objtool/check.c +1
-0@@ -2026,6 +2033,11 @@ static int update_cfi_state(struct instruction *insn, struct cfi_state *cfi,/* mov disp(%rbp), %reg */ /* mov disp(%rsp), %reg */ restore_reg (cfi, op->dest.reg); + } else if (op->src.reg == CFI_SP &&
An empty line above the else would help readability.
tools/objtool/check.c static int update_cfi_state(struct instruction *insn, struct cfi_state *cfi, · line 2118
Quoted 13 lines
tools/objtool/check.c +4
-0+ op->src.offset == regs[op->dest.reg].offset + cfi->stack_size) { + +/* mov disp(%rsp), %reg */ +restore_reg (cfi, op->dest.reg); }tools/objtool/check.c +1
-0break ;@@ -2103,6 +2115,11 @@ static int update_cfi_state(struct instruction *insn, struct cfi_state *cfi,/* mov reg, disp(%rsp) */ save_reg (cfi, op->src.reg,CFI_CFA , op->dest.offset - cfi->cfa.offset); + }else if (op->dest.reg ==CFI_SP ) {
Same here.
Quoted 10 lines
tools/objtool/check.c +4
-0+ +/* mov reg, disp(%rsp) */ +save_reg (cfi, op->src.reg,CFI_CFA , + op->dest.offset - cfi->stack_size); }break ;Signature · 1 line
2.21.3
Signature · 1 line
Josh
Re: [PATCH 3/3] objtool: check: Make SP memory operation match PUSH/POP semantics
Quoted 18 lines · Josh Poimboeuf
Quoted 17 lines · Julien Thierry
Architectures without PUSH/POP instructions will always access the stack though memory operations (SRC/DEST_INDIRECT). Make those operations have the same effect on the CFA as PUSH/POP, with no stack pointer modification.
Signed-off-by Julien Thierry <[email protected]>--- tools/objtool/check.c | 17 +++++++++++++++++ 1 file changed, 17 insertions(+)tools/objtool/check.c +0
-0@@ -2005,6 +2005,13 @@ static int update_cfi_state(struct instruction *insn, struct cfi_state *cfi,break ;
Quoted 5 lines
tools/objtool/check.c static int update_cfi_state(struct instruction *insn, struct cfi_state *cfi, · line 2008
Quoted 2 lines
tools/objtool/check.c +1
-0case OP_SRC_REG_INDIRECT : +if (!cfi->drap && op->dest.reg == cfa->base) {&&
op->dest.reg==CFI_BP?
Does it matter? My unstandig was that the register used to point to the CFA is getting overwritten, so we need to fallback to something known which is the offset from the stack pointer.
Was that not the case?
Quoted 24 lines
Quoted 10 lines
tools/objtool/check.c +6
-0+ +/* mov disp(%rsp), %rbp */ + cfa->base =CFI_SP ; + cfa->offset = cfi->stack_size; + } +if (cfi->drap && op->src.reg ==CFI_BP && op->src.offset == cfi->drap_offset) {tools/objtool/check.c static int update_cfi_state(struct instruction *insn, struct cfi_state *cfi, · line 2036
Quoted 4 lines
tools/objtool/check.c +1
-0@@ -2026,6 +2033,11 @@ static int update_cfi_state(struct instruction *insn, struct cfi_state *cfi,/* mov disp(%rbp), %reg */ /* mov disp(%rsp), %reg */ restore_reg (cfi, op->dest.reg); + } else if (op->src.reg == CFI_SP &&An empty line above the else would help readability.
Quoted 7 lines
tools/objtool/check.c +4
-0+ op->src.offset == regs[op->dest.reg].offset + cfi->stack_size) { + +/* mov disp(%rsp), %reg */ +restore_reg (cfi, op->dest.reg); }
Quoted 9 lines
tools/objtool/check.c static int update_cfi_state(struct instruction *insn, struct cfi_state *cfi, · line 2118
Quoted 6 lines
break;
tools/objtool/check.c +1
-0@@ -2103,6 +2115,11 @@ static int update_cfi_state(struct instruction *insn, struct cfi_state *cfi,/* mov reg, disp(%rsp) */ save_reg (cfi, op->src.reg,CFI_CFA , op->dest.offset - cfi->cfa.offset); + }else if (op->dest.reg ==CFI_SP ) {Same here.
I'll add those.
Quoted 11 lines
Quoted 10 lines
tools/objtool/check.c +4
-0+ +/* mov reg, disp(%rsp) */ +save_reg (cfi, op->src.reg,CFI_CFA , + op->dest.offset - cfi->stack_size); }break ;Signature · 1 line
2.21.3
Signature · 1 line
Julien Thierry
Re: [PATCH 3/3] objtool: check: Make SP memory operation match PUSH/POP semantics
Quoted 12 lines · Julien Thierry
Quoted 11 lines
tools/objtool/check.c static int update_cfi_state(struct instruction *insn, struct cfi_state *cfi, · line 2007
Quoted 8 lines
tools/objtool/check.c +1
-0@@ -2005,6 +2005,13 @@ static int update_cfi_state(struct instruction *insn, struct cfi_state *cfi,break ;case OP_SRC_REG_INDIRECT : +if (!cfi->drap && op->dest.reg == cfa->base) {&&
op->dest.reg==CFI_BP?
Quoted 5 lines
Does it matter? My unstandig was that the register used to point to the CFA is getting overwritten, so we need to fallback to something known which is the offset from the stack pointer.
Was that not the case?
Maybe. I was wondering if it would be possible to overwrite the stack pointer, like 'mov disp(%rsp), %rsp', which could be possible in asm.
Though I suppose the below code would be harmless, since the CFA base/offset would already be CFI_SP/cfi->stack_size respectively.
Still, no harm in making the condition more precise.
Quoted 5 lines
Quoted 5 lines
Quoted 5 lines
tools/objtool/check.c +5
-0+ +/* mov disp(%rsp), %rbp */ + cfa->base =CFI_SP ; + cfa->offset = cfi->stack_size; + }
Signature · 1 line
Josh