• Josh Poimboeuf's avatar
    objtool: Fix return thunk patching in retpolines · 34de4fe7
    Josh Poimboeuf authored
    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: default avatarDavid Kaplan <david.kaplan@amd.com>
    Signed-off-by: default avatarJosh Poimboeuf <jpoimboe@kernel.org>
    Signed-off-by: default avatarIngo Molnar <mingo@kernel.org>
    Signed-off-by: default avatarBorislav Petkov (AMD) <bp@alien8.de>
    Acked-by: default avatarPeter Zijlstra (Intel) <peterz@infradead.org>
    Link: https://lore.kernel.org/r/20231012024737.eg5phclogp67ik6x@treble
    34de4fe7
check.c 110 KB