All of lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH] uprobes/x86: Recognize user ENDBR64 without kernel IBT
@ 2026-11-30 12:20 David Windsor
  2026-11-30 12:27 ` sashiko-bot
                   ` (2 more replies)
  0 siblings, 3 replies; 5+ messages in thread
From: David Windsor @ 2026-11-30 12:20 UTC (permalink / raw)
  To: peterz, oleg, mhiramat, x86
  Cc: tglx, mingo, bp, dave.hansen, hpa, andrii, jolsa,
	linux-trace-kernel, linux-perf-users, linux-kernel, David Windsor

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@gmail.com>
---
 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


^ permalink raw reply related	[flat|nested] 5+ messages in thread

* Re: [PATCH] uprobes/x86: Recognize user ENDBR64 without kernel IBT
  2026-11-30 12:20 [PATCH] uprobes/x86: Recognize user ENDBR64 without kernel IBT David Windsor
@ 2026-11-30 12:27 ` sashiko-bot
  2026-11-30 20:53 ` Masami Hiramatsu (Google)
  2026-12-01 20:40 ` Andrii Nakryiko
  2 siblings, 0 replies; 5+ messages in thread
From: sashiko-bot @ 2026-11-30 12:27 UTC (permalink / raw)
  To: David Windsor; +Cc: linux-perf-users

> 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@gmail.com>

Sashiko has reviewed this patch and found no issues.

-- 
Sashiko AI review · https://sashiko.dev/#/patchset/20261130122035.1586780-1-dwindsor@gmail.com?part=1

^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: [PATCH] uprobes/x86: Recognize user ENDBR64 without kernel IBT
  2026-11-30 12:20 [PATCH] uprobes/x86: Recognize user ENDBR64 without kernel IBT David Windsor
  2026-11-30 12:27 ` sashiko-bot
@ 2026-11-30 20:53 ` Masami Hiramatsu (Google)
  2026-12-01 20:40 ` Andrii Nakryiko
  2 siblings, 0 replies; 5+ messages in thread
From: Masami Hiramatsu (Google) @ 2026-11-30 20:53 UTC (permalink / raw)
  To: David Windsor
  Cc: peterz, oleg, x86, tglx, mingo, bp, dave.hansen, hpa, andrii,
	jolsa, linux-trace-kernel, linux-perf-users, linux-kernel

On Mon, 30 Nov 2026 07:20:35 -0500, David Windsor <dwindsor@gmail.com> 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@gmail.com>

This looks good to me.

Acked-by: Masami Hiramatsu (Google) <mhiramat@kernel.org>

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@kernel.org>

^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: [PATCH] uprobes/x86: Recognize user ENDBR64 without kernel IBT
  2026-11-30 12:20 [PATCH] uprobes/x86: Recognize user ENDBR64 without kernel IBT David Windsor
  2026-11-30 12:27 ` sashiko-bot
  2026-11-30 20:53 ` Masami Hiramatsu (Google)
@ 2026-12-01 20:40 ` Andrii Nakryiko
  2026-12-01 21:18   ` David Windsor
  2 siblings, 1 reply; 5+ messages in thread
From: Andrii Nakryiko @ 2026-12-01 20:40 UTC (permalink / raw)
  To: David Windsor
  Cc: peterz, oleg, mhiramat, x86, tglx, mingo, bp, dave.hansen, hpa,
	andrii, jolsa, linux-trace-kernel, linux-perf-users, linux-kernel

On Mon, Nov 30, 2026 at 5:20 AM David Windsor <dwindsor@gmail.com> 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@gmail.com>
> ---
>  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;
> -}
> -

would anything break if is_endbr() detect endb64 instruction even if
!HAS_KERNEL_IBT?


>  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
>

^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: [PATCH] uprobes/x86: Recognize user ENDBR64 without kernel IBT
  2026-12-01 20:40 ` Andrii Nakryiko
@ 2026-12-01 21:18   ` David Windsor
  0 siblings, 0 replies; 5+ messages in thread
From: David Windsor @ 2026-12-01 21:18 UTC (permalink / raw)
  To: Andrii Nakryiko
  Cc: peterz, oleg, mhiramat, x86, tglx, mingo, bp, dave.hansen, hpa,
	andrii, jolsa, linux-trace-kernel, linux-perf-users, linux-kernel

On Tue, Dec 1, 2026 at 3:40 PM Andrii Nakryiko
<andrii.nakryiko@gmail.com> wrote:
>
>
> would anything break if is_endbr() detect endb64 instruction even if
> !HAS_KERNEL_IBT?
>

Shouldn't break anything, AFAICT. Reason I went this way is that if we
update is_endbr(), we'd be doing the check unnecessarily for kprobes,
fprobes and JIT, which will never contain endbr64 if !HAS_KERNEL_IBT.
uprobe case is the only one that can possibly have CFI landing pad
here when !HAS_KERNEL_IBT.

Also is_endbr() checks for poison and endbr32, neither of which are
valid for userspace.

That said, if folks want it in is_endbr(), happy to respin it that way,

^ permalink raw reply	[flat|nested] 5+ messages in thread

end of thread, other threads:[~2026-12-01 21:18 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-11-30 12:20 [PATCH] uprobes/x86: Recognize user ENDBR64 without kernel IBT David Windsor
2026-11-30 12:27 ` sashiko-bot
2026-11-30 20:53 ` Masami Hiramatsu (Google)
2026-12-01 20:40 ` Andrii Nakryiko
2026-12-01 21:18   ` David Windsor

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.