Skip to content

Commit 34de4fe

Browse files
jpoimboebp3tk0v
authored andcommitted
objtool: Fix return thunk patching in retpolines
With CONFIG_RETHUNK enabled, the compiler replaces every RET with a tail call to a return thunk ('JMP __x86_return_thunk'). Objtool annotates all such return sites so they can be patched during boot by apply_returns(). The implementation of __x86_return_thunk() is just a bare RET. It's only meant to be used temporarily until apply_returns() patches all return sites with either a JMP to another return thunk or an actual RET. Removing the .text..__x86.return_thunk section would break objtool's detection of return sites in retpolines. Since retpolines and return thunks would land in the same section, the compiler no longer uses relocations for the intra-section jumps between the retpolines and the return thunk, causing objtool to overlook them. As a result, none of the retpolines' return sites would get patched. Each one stays at 'JMP __x86_return_thunk', effectively a bare RET. Fix it by teaching objtool to detect when a non-relocated jump target is a return thunk (or retpoline). [ bp: Massage the commit message now that the offending commit removing the .text..__x86.return_thunk section has been zapped. Still keep the objtool change here as it makes objtool more robust wrt handling such intra-TU jumps without relocations, should some toolchain and/or config generate them in the future. ] Reported-by: David Kaplan <[email protected]> Signed-off-by: Josh Poimboeuf <[email protected]> Signed-off-by: Ingo Molnar <[email protected]> Signed-off-by: Borislav Petkov (AMD) <[email protected]> Acked-by: Peter Zijlstra (Intel) <[email protected]> Link: https://lore.kernel.org/r/20231012024737.eg5phclogp67ik6x@treble
1 parent 904e1dd commit 34de4fe

File tree

1 file changed

+16
-0
lines changed

1 file changed

+16
-0
lines changed

tools/objtool/check.c

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1610,6 +1610,22 @@ static int add_jump_destinations(struct objtool_file *file)
16101610
return -1;
16111611
}
16121612

1613+
/*
1614+
* An intra-TU jump in retpoline.o might not have a relocation
1615+
* for its jump dest, in which case the above
1616+
* add_{retpoline,return}_call() didn't happen.
1617+
*/
1618+
if (jump_dest->sym && jump_dest->offset == jump_dest->sym->offset) {
1619+
if (jump_dest->sym->retpoline_thunk) {
1620+
add_retpoline_call(file, insn);
1621+
continue;
1622+
}
1623+
if (jump_dest->sym->return_thunk) {
1624+
add_return_call(file, insn, true);
1625+
continue;
1626+
}
1627+
}
1628+
16131629
/*
16141630
* Cross-function jump.
16151631
*/

0 commit comments

Comments
 (0)