Re: [PATCH] uprobes/x86: Recognize user ENDBR64 without kernel IBT
From: Masami Hiramatsu (Google)
Date: Mon Nov 30 2026 - 15:53:27 EST
On Mon, 30 Nov 2026 07:20:35 -0500, David Windsor <dwindsor@xxxxxxxxx> wrote:
> When a uprobe is placed at the start of a function, the prologue has not
> run yet and the return address is still at *regs->sp. To capture the
> caller, the unwinder checks is_uprobe_at_func_entry(), which looks at
> the probed instruction for a push %rbp or an endbr64.
>
> The endbr64 check uses is_endbr(). On a kernel built without
> CONFIG_X86_KERNEL_IBT that is a stub returning false, so a uprobe on a
> function starting with endbr64 is not treated as a function entry and
> the caller is missing from the stack trace. Most binaries start their
> functions with endbr64 since -fcf-protection=branch is on by default.
>
> Whether the user binary contains endbr64 has nothing to do with whether
> the kernel was built with IBT. Compare the probed instruction against
> gen_endbr() instead, and move gen_endbr() out of the HAS_KERNEL_IBT
> block so it is available without kernel IBT. is_endbr() is unchanged.
>
> Fixes: cfa7f3d2c526 ("perf,x86: avoid missing caller address in stack traces captured in uprobe")
> Signed-off-by: David Windsor <dwindsor@xxxxxxxxx>
This looks good to me.
Acked-by: Masami Hiramatsu (Google) <mhiramat@xxxxxxxxxx>
Peter, can you pick this via tip tree?
Thank you,
> ---
> arch/x86/include/asm/ibt.h | 39 +++++++++++++++++++++++---------------
> arch/x86/kernel/uprobes.c | 11 +++++++++--
> 2 files changed, 33 insertions(+), 17 deletions(-)
>
> diff --git a/arch/x86/include/asm/ibt.h b/arch/x86/include/asm/ibt.h
> index 5e45d6424722..c0cf481d50d5 100644
> --- a/arch/x86/include/asm/ibt.h
> +++ b/arch/x86/include/asm/ibt.h
> @@ -17,6 +17,30 @@
> *
> * When all the above are satisfied, HAS_KERNEL_IBT will be 1, otherwise 0.
> */
> +
> +#ifndef __ASSEMBLER__
> +
> +/*
> + * Not dependent on HAS_KERNEL_IBT: userspace can contain ENDBR64 regardless
> + * of whether the kernel itself is built with IBT.
> + */
> +static __always_inline __attribute_const__ u32 gen_endbr(void)
> +{
> + u32 endbr;
> +
> + /*
> + * Generate ENDBR64 in a way that is sure to not result in
> + * an ENDBR64 instruction as immediate.
> + */
> + asm ( "mov $~0xfa1e0ff3, %[endbr]\n\t"
> + "not %[endbr]\n\t"
> + : [endbr] "=&r" (endbr) );
> +
> + return endbr;
> +}
> +
> +#endif /* __ASSEMBLER__ */
> +
> #if defined(CONFIG_X86_KERNEL_IBT) && !defined(__DISABLE_EXPORTS)
>
> #define HAS_KERNEL_IBT 1
> @@ -41,21 +65,6 @@
> _ASM_PTR fname "\n\t" \
> ".popsection\n\t"
>
> -static __always_inline __attribute_const__ u32 gen_endbr(void)
> -{
> - u32 endbr;
> -
> - /*
> - * Generate ENDBR64 in a way that is sure to not result in
> - * an ENDBR64 instruction as immediate.
> - */
> - asm ( "mov $~0xfa1e0ff3, %[endbr]\n\t"
> - "not %[endbr]\n\t"
> - : [endbr] "=&r" (endbr) );
> -
> - return endbr;
> -}
> -
> static __always_inline __attribute_const__ u32 gen_endbr_poison(void)
> {
> /*
> diff --git a/arch/x86/kernel/uprobes.c b/arch/x86/kernel/uprobes.c
> index fc07bcd0f462..bbb71ec44c6a 100644
> --- a/arch/x86/kernel/uprobes.c
> +++ b/arch/x86/kernel/uprobes.c
> @@ -15,9 +15,11 @@
> #include <linux/syscalls.h>
>
> #include <linux/kdebug.h>
> +#include <linux/unaligned.h>
> #include <asm/processor.h>
> #include <asm/insn.h>
> #include <asm/insn-eval.h>
> +#include <asm/ibt.h>
> #include <asm/mmu_context.h>
> #include <asm/nops.h>
>
> @@ -1928,8 +1930,13 @@ bool is_uprobe_at_func_entry(struct pt_regs *regs)
> if (auprobe->insn[0] == 0x55)
> return true;
>
> - /* endbr64 (64-bit only) */
> - if (user_64bit_mode(regs) && is_endbr((u32 *)auprobe->insn))
> + /*
> + * endbr64 (64-bit only). Compare against gen_endbr() rather than
> + * is_endbr(): the latter is a stub when CONFIG_X86_KERNEL_IBT is
> + * unset, but userspace can contain ENDBR64 regardless.
> + */
> + if (user_64bit_mode(regs) &&
> + get_unaligned((u32 *)auprobe->insn) == gen_endbr())
> return true;
>
> return false;
> --
> 2.53.0
>
>
>
--
Masami Hiramatsu (Google) <mhiramat@xxxxxxxxxx>