mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
* [PATCH] x86/cfi: Fix FineIBT hash offset in cfi_get_func_hash()
@ 2026-08-31 14:48 Soheil Hassas Yeganeh
  2026-09-08 13:22 ` Soheil Hassas Yeganeh
  2026-09-10  9:07 ` [tip: x86/urgent] " tip-bot2 for Soheil Hassas Yeganeh
  0 siblings, 2 replies; 5+ messages in thread
From: Soheil Hassas Yeganeh @ 2026-08-31 14:48 UTC (permalink / raw)
  To: Thomas Gleixner, Ingo Molnar, Borislav Petkov, Dave Hansen, x86,
	H. Peter Anvin, Peter Zijlstra
  Cc: linux-kernel, Alexei Starovoitov, Daniel Borkmann, stable,
	Soheil Hassas Yeganeh

The switch of the FineIBT preamble from "subl $hash, %r10d" to the
shorter "subl $hash, %eax" moved the hash immediate from offset 7 to
offset 5 of the preamble. fineibt_preamble_hash was updated to match,
but the open-coded offset in cfi_get_func_hash() was missed and it
still reads the hash at offset 7.

cfi_get_func_hash() is used by the BPF JIT to give a struct_ops
trampoline the CFI hash of the stub function it stands in for. With
FineIBT the trampoline now gets the upper half of the real hash
followed by the first two bytes of the next instruction, so the first
indirect call from the kernel into a struct_ops program,
tcp_init_congestion_control() calling ->init() of a BPF congestion
control for example, fails the FineIBT check and the kernel dies with
a CFI failure.

Move the FineIBT preamble template and its offset defines above
cfi_get_func_hash() and use fineibt_preamble_hash there, so every
reader of the preamble shares one definition of its layout. The
CFI_FINEIBT arm is only built with CONFIG_FINEIBT, the only
configuration in which cfi_mode can take that value.
cfi_get_func_arity() does not need the same treatment: the __bhi_args
call whose displacement it reads still ends at the function address.

Fixes: 85a2d4a890dc ("x86,ibt: Use UDB instead of 0xEA")
Cc: Peter Zijlstra <peterz@infradead.org>
Cc: Alexei Starovoitov <ast@kernel.org>
Cc: Daniel Borkmann <daniel@iogearbox.net>
Cc: stable@vger.kernel.org # 6.18+
Assisted-by: LLM
Signed-off-by: Soheil Hassas Yeganeh <soheil.kdev@gmail.com>
---
 arch/x86/kernel/alternative.c | 72 +++++++++++++++++++++++--------------------
 1 file changed, 38 insertions(+), 34 deletions(-)

diff --git a/arch/x86/kernel/alternative.c b/arch/x86/kernel/alternative.c
index 91b1cdd16569..1b22062cabfe 100644
--- a/arch/x86/kernel/alternative.c
+++ b/arch/x86/kernel/alternative.c
@@ -1198,6 +1198,41 @@ static bool cfi_debug __ro_after_init;
 bool cfi_bhi __ro_after_init = false;
 #endif
 
+#ifdef CONFIG_FINEIBT
+/*
+ * <fineibt_preamble_start>:
+ *  0:   f3 0f 1e fa             endbr64
+ *  4:   2d 78 56 34 12          sub    $0x12345678, %eax
+ *  9:   2e 0f 85 03 00 00 00    jne,pn 13 <fineibt_preamble_start+0x13>
+ * 10:   0f 1f 40 d6             nopl   -0x2a(%rax)
+ *
+ * Note that the JNE target is the 0xD6 byte inside the NOPL, this decodes as
+ * UDB on x86_64 and raises #UD.
+ */
+asm(	".pushsection .rodata				\n"
+	"fineibt_preamble_start:			\n"
+	"	endbr64					\n"
+	"	subl	$0x12345678, %eax		\n"
+	"fineibt_preamble_bhi:				\n"
+	"	cs jne.d32 fineibt_preamble_start+0x13	\n"
+	"#fineibt_func:					\n"
+	"	nopl	-42(%rax)			\n"
+	"fineibt_preamble_end:				\n"
+	".popsection\n"
+);
+
+extern u8 fineibt_preamble_start[];
+extern u8 fineibt_preamble_bhi[];
+extern u8 fineibt_preamble_end[];
+
+#define fineibt_preamble_size (fineibt_preamble_end - fineibt_preamble_start)
+#define fineibt_preamble_bhi  (fineibt_preamble_bhi - fineibt_preamble_start)
+#define fineibt_preamble_ud   0x13
+#define fineibt_preamble_hash 5
+
+#define fineibt_prefix_size (fineibt_preamble_size - ENDBR_INSN_SIZE)
+#endif /* CONFIG_FINEIBT */
+
 #ifdef CONFIG_CFI
 u32 cfi_get_func_hash(void *func)
 {
@@ -1205,9 +1240,11 @@ u32 cfi_get_func_hash(void *func)
 
 	func -= cfi_get_offset();
 	switch (cfi_mode) {
+#ifdef CONFIG_FINEIBT
 	case CFI_FINEIBT:
-		func += 7;
+		func += fineibt_preamble_hash;
 		break;
+#endif
 	case CFI_KCFI:
 		func += 1;
 		break;
@@ -1363,39 +1400,6 @@ early_param("cfi", cfi_parse_cmdline);
  * anyway.
  */
 
-/*
- * <fineibt_preamble_start>:
- *  0:   f3 0f 1e fa             endbr64
- *  4:   2d 78 56 34 12          sub    $0x12345678, %eax
- *  9:   2e 0f 85 03 00 00 00    jne,pn 13 <fineibt_preamble_start+0x13>
- * 10:   0f 1f 40 d6             nopl   -0x2a(%rax)
- *
- * Note that the JNE target is the 0xD6 byte inside the NOPL, this decodes as
- * UDB on x86_64 and raises #UD.
- */
-asm(	".pushsection .rodata				\n"
-	"fineibt_preamble_start:			\n"
-	"	endbr64					\n"
-	"	subl	$0x12345678, %eax		\n"
-	"fineibt_preamble_bhi:				\n"
-	"	cs jne.d32 fineibt_preamble_start+0x13	\n"
-	"#fineibt_func:					\n"
-	"	nopl	-42(%rax)			\n"
-	"fineibt_preamble_end:				\n"
-	".popsection\n"
-);
-
-extern u8 fineibt_preamble_start[];
-extern u8 fineibt_preamble_bhi[];
-extern u8 fineibt_preamble_end[];
-
-#define fineibt_preamble_size (fineibt_preamble_end - fineibt_preamble_start)
-#define fineibt_preamble_bhi  (fineibt_preamble_bhi - fineibt_preamble_start)
-#define fineibt_preamble_ud   0x13
-#define fineibt_preamble_hash 5
-
-#define fineibt_prefix_size (fineibt_preamble_size - ENDBR_INSN_SIZE)
-
 /*
  * <fineibt_caller_start>:
  *  0:   b8 78 56 34 12          mov    $0x12345678, %eax

---
base-commit: 08dbfad3f5040f5bdb6c529da20d6d4e81fefd72
change-id: 20260831-b4-x86-cfi-fineibt-func-hash-e160f6eee02c

Best regards,
--  
Soheil Hassas Yeganeh <soheil.kdev@gmail.com>


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

* Re: [PATCH] x86/cfi: Fix FineIBT hash offset in cfi_get_func_hash()
  2026-08-31 14:48 [PATCH] x86/cfi: Fix FineIBT hash offset in cfi_get_func_hash() Soheil Hassas Yeganeh
@ 2026-09-08 13:22 ` Soheil Hassas Yeganeh
  2026-09-08 16:37   ` Alexei Starovoitov
  2026-09-10  9:07 ` [tip: x86/urgent] " tip-bot2 for Soheil Hassas Yeganeh
  1 sibling, 1 reply; 5+ messages in thread
From: Soheil Hassas Yeganeh @ 2026-09-08 13:22 UTC (permalink / raw)
  To: Thomas Gleixner, Ingo Molnar, Borislav Petkov, Dave Hansen, x86,
	H. Peter Anvin, Peter Zijlstra
  Cc: linux-kernel, Alexei Starovoitov, Daniel Borkmann, stable

On Mon, Aug 31, 2026 at 10:49 AM Soheil Hassas Yeganeh
<soheil.kdev@gmail.com> wrote:
>
> The switch of the FineIBT preamble from "subl $hash, %r10d" to the
> shorter "subl $hash, %eax" moved the hash immediate from offset 7 to
> offset 5 of the preamble. fineibt_preamble_hash was updated to match,
> but the open-coded offset in cfi_get_func_hash() was missed and it
> still reads the hash at offset 7.
>
> cfi_get_func_hash() is used by the BPF JIT to give a struct_ops
> trampoline the CFI hash of the stub function it stands in for. With
> FineIBT the trampoline now gets the upper half of the real hash
> followed by the first two bytes of the next instruction, so the first
> indirect call from the kernel into a struct_ops program,
> tcp_init_congestion_control() calling ->init() of a BPF congestion
> control for example, fails the FineIBT check and the kernel dies with
> a CFI failure.
>
> Move the FineIBT preamble template and its offset defines above
> cfi_get_func_hash() and use fineibt_preamble_hash there, so every
> reader of the preamble shares one definition of its layout. The
> CFI_FINEIBT arm is only built with CONFIG_FINEIBT, the only
> configuration in which cfi_mode can take that value.
> cfi_get_func_arity() does not need the same treatment: the __bhi_args
> call whose displacement it reads still ends at the function address.
>
> Fixes: 85a2d4a890dc ("x86,ibt: Use UDB instead of 0xEA")

Gentle ping on this one. To recap: since 85a2d4a890dc the FineIBT preamble
stores the hash at +5, but cfi_get_func_hash() still reads it at +7, so BPF
struct_ops trampolines are emitted with a wrong hash and the first indirect call
through them (e.g. a bpf tcp_congestion_ops .cong_control) hits a FineIBT #CP.

We have large scale panics when we use BPF congestion control.
Happy to respin if a different shape is preferred (e.g. moving the
offset next to
the preamble definition), but this seemed like the best way to fix the issue.

Thanks,
Soheil

> Cc: Peter Zijlstra <peterz@infradead.org>
> Cc: Alexei Starovoitov <ast@kernel.org>
> Cc: Daniel Borkmann <daniel@iogearbox.net>
> Cc: stable@vger.kernel.org # 6.18+
> Assisted-by: LLM
> Signed-off-by: Soheil Hassas Yeganeh <soheil.kdev@gmail.com>
> ---
>  arch/x86/kernel/alternative.c | 72 +++++++++++++++++++++++--------------------
>  1 file changed, 38 insertions(+), 34 deletions(-)
>
> diff --git a/arch/x86/kernel/alternative.c b/arch/x86/kernel/alternative.c
> index 91b1cdd16569..1b22062cabfe 100644
> --- a/arch/x86/kernel/alternative.c
> +++ b/arch/x86/kernel/alternative.c
> @@ -1198,6 +1198,41 @@ static bool cfi_debug __ro_after_init;
>  bool cfi_bhi __ro_after_init = false;
>  #endif
>
> +#ifdef CONFIG_FINEIBT
> +/*
> + * <fineibt_preamble_start>:
> + *  0:   f3 0f 1e fa             endbr64
> + *  4:   2d 78 56 34 12          sub    $0x12345678, %eax
> + *  9:   2e 0f 85 03 00 00 00    jne,pn 13 <fineibt_preamble_start+0x13>
> + * 10:   0f 1f 40 d6             nopl   -0x2a(%rax)
> + *
> + * Note that the JNE target is the 0xD6 byte inside the NOPL, this decodes as
> + * UDB on x86_64 and raises #UD.
> + */
> +asm(   ".pushsection .rodata                           \n"
> +       "fineibt_preamble_start:                        \n"
> +       "       endbr64                                 \n"
> +       "       subl    $0x12345678, %eax               \n"
> +       "fineibt_preamble_bhi:                          \n"
> +       "       cs jne.d32 fineibt_preamble_start+0x13  \n"
> +       "#fineibt_func:                                 \n"
> +       "       nopl    -42(%rax)                       \n"
> +       "fineibt_preamble_end:                          \n"
> +       ".popsection\n"
> +);
> +
> +extern u8 fineibt_preamble_start[];
> +extern u8 fineibt_preamble_bhi[];
> +extern u8 fineibt_preamble_end[];
> +
> +#define fineibt_preamble_size (fineibt_preamble_end - fineibt_preamble_start)
> +#define fineibt_preamble_bhi  (fineibt_preamble_bhi - fineibt_preamble_start)
> +#define fineibt_preamble_ud   0x13
> +#define fineibt_preamble_hash 5
> +
> +#define fineibt_prefix_size (fineibt_preamble_size - ENDBR_INSN_SIZE)
> +#endif /* CONFIG_FINEIBT */
> +
>  #ifdef CONFIG_CFI
>  u32 cfi_get_func_hash(void *func)
>  {
> @@ -1205,9 +1240,11 @@ u32 cfi_get_func_hash(void *func)
>
>         func -= cfi_get_offset();
>         switch (cfi_mode) {
> +#ifdef CONFIG_FINEIBT
>         case CFI_FINEIBT:
> -               func += 7;
> +               func += fineibt_preamble_hash;
>                 break;
> +#endif
>         case CFI_KCFI:
>                 func += 1;
>                 break;
> @@ -1363,39 +1400,6 @@ early_param("cfi", cfi_parse_cmdline);
>   * anyway.
>   */
>
> -/*
> - * <fineibt_preamble_start>:
> - *  0:   f3 0f 1e fa             endbr64
> - *  4:   2d 78 56 34 12          sub    $0x12345678, %eax
> - *  9:   2e 0f 85 03 00 00 00    jne,pn 13 <fineibt_preamble_start+0x13>
> - * 10:   0f 1f 40 d6             nopl   -0x2a(%rax)
> - *
> - * Note that the JNE target is the 0xD6 byte inside the NOPL, this decodes as
> - * UDB on x86_64 and raises #UD.
> - */
> -asm(   ".pushsection .rodata                           \n"
> -       "fineibt_preamble_start:                        \n"
> -       "       endbr64                                 \n"
> -       "       subl    $0x12345678, %eax               \n"
> -       "fineibt_preamble_bhi:                          \n"
> -       "       cs jne.d32 fineibt_preamble_start+0x13  \n"
> -       "#fineibt_func:                                 \n"
> -       "       nopl    -42(%rax)                       \n"
> -       "fineibt_preamble_end:                          \n"
> -       ".popsection\n"
> -);
> -
> -extern u8 fineibt_preamble_start[];
> -extern u8 fineibt_preamble_bhi[];
> -extern u8 fineibt_preamble_end[];
> -
> -#define fineibt_preamble_size (fineibt_preamble_end - fineibt_preamble_start)
> -#define fineibt_preamble_bhi  (fineibt_preamble_bhi - fineibt_preamble_start)
> -#define fineibt_preamble_ud   0x13
> -#define fineibt_preamble_hash 5
> -
> -#define fineibt_prefix_size (fineibt_preamble_size - ENDBR_INSN_SIZE)
> -
>  /*
>   * <fineibt_caller_start>:
>   *  0:   b8 78 56 34 12          mov    $0x12345678, %eax
>
> ---
> base-commit: 08dbfad3f5040f5bdb6c529da20d6d4e81fefd72
> change-id: 20260831-b4-x86-cfi-fineibt-func-hash-e160f6eee02c
>
> Best regards,
> --
> Soheil Hassas Yeganeh <soheil.kdev@gmail.com>
>

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

* Re: [PATCH] x86/cfi: Fix FineIBT hash offset in cfi_get_func_hash()
  2026-09-08 13:22 ` Soheil Hassas Yeganeh
@ 2026-09-08 16:37   ` Alexei Starovoitov
  2026-09-08 22:13     ` Peter Zijlstra
  0 siblings, 1 reply; 5+ messages in thread
From: Alexei Starovoitov @ 2026-09-08 16:37 UTC (permalink / raw)
  To: Soheil Hassas Yeganeh
  Cc: Thomas Gleixner, Ingo Molnar, Borislav Petkov, Dave Hansen,
	X86 ML, H. Peter Anvin, Peter Zijlstra, LKML, Alexei Starovoitov,
	Daniel Borkmann, stable

On Tue, Sep 8, 2026 at 6:22 AM Soheil Hassas Yeganeh
<soheil.kdev@gmail.com> wrote:
>
> On Mon, Aug 31, 2026 at 10:49 AM Soheil Hassas Yeganeh
> <soheil.kdev@gmail.com> wrote:
> >
> > The switch of the FineIBT preamble from "subl $hash, %r10d" to the
> > shorter "subl $hash, %eax" moved the hash immediate from offset 7 to
> > offset 5 of the preamble. fineibt_preamble_hash was updated to match,
> > but the open-coded offset in cfi_get_func_hash() was missed and it
> > still reads the hash at offset 7.
> >
> > cfi_get_func_hash() is used by the BPF JIT to give a struct_ops
> > trampoline the CFI hash of the stub function it stands in for. With
> > FineIBT the trampoline now gets the upper half of the real hash
> > followed by the first two bytes of the next instruction, so the first
> > indirect call from the kernel into a struct_ops program,
> > tcp_init_congestion_control() calling ->init() of a BPF congestion
> > control for example, fails the FineIBT check and the kernel dies with
> > a CFI failure.
> >
> > Move the FineIBT preamble template and its offset defines above
> > cfi_get_func_hash() and use fineibt_preamble_hash there, so every
> > reader of the preamble shares one definition of its layout. The
> > CFI_FINEIBT arm is only built with CONFIG_FINEIBT, the only
> > configuration in which cfi_mode can take that value.
> > cfi_get_func_arity() does not need the same treatment: the __bhi_args
> > call whose displacement it reads still ends at the function address.
> >
> > Fixes: 85a2d4a890dc ("x86,ibt: Use UDB instead of 0xEA")
>
> Gentle ping on this one. To recap: since 85a2d4a890dc the FineIBT preamble
> stores the hash at +5, but cfi_get_func_hash() still reads it at +7, so BPF
> struct_ops trampolines are emitted with a wrong hash and the first indirect call
> through them (e.g. a bpf tcp_congestion_ops .cong_control) hits a FineIBT #CP.
>
> We have large scale panics when we use BPF congestion control.
> Happy to respin if a different shape is preferred (e.g. moving the
> offset next to
> the preamble definition), but this seemed like the best way to fix the issue.

Looks fine to me. I guess it will go via tip tree?

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

* Re: [PATCH] x86/cfi: Fix FineIBT hash offset in cfi_get_func_hash()
  2026-09-08 16:37   ` Alexei Starovoitov
@ 2026-09-08 22:13     ` Peter Zijlstra
  0 siblings, 0 replies; 5+ messages in thread
From: Peter Zijlstra @ 2026-09-08 22:13 UTC (permalink / raw)
  To: Alexei Starovoitov
  Cc: Soheil Hassas Yeganeh, Thomas Gleixner, Ingo Molnar,
	Borislav Petkov, Dave Hansen, X86 ML, H. Peter Anvin, LKML,
	Alexei Starovoitov, Daniel Borkmann, stable

On Tue, Sep 08, 2026 at 09:37:46AM -0700, Alexei Starovoitov wrote:
> On Tue, Sep 8, 2026 at 6:22 AM Soheil Hassas Yeganeh
> <soheil.kdev@gmail.com> wrote:
> >
> > On Mon, Aug 31, 2026 at 10:49 AM Soheil Hassas Yeganeh
> > <soheil.kdev@gmail.com> wrote:
> > >
> > > The switch of the FineIBT preamble from "subl $hash, %r10d" to the
> > > shorter "subl $hash, %eax" moved the hash immediate from offset 7 to
> > > offset 5 of the preamble. fineibt_preamble_hash was updated to match,
> > > but the open-coded offset in cfi_get_func_hash() was missed and it
> > > still reads the hash at offset 7.
> > >
> > > cfi_get_func_hash() is used by the BPF JIT to give a struct_ops
> > > trampoline the CFI hash of the stub function it stands in for. With
> > > FineIBT the trampoline now gets the upper half of the real hash
> > > followed by the first two bytes of the next instruction, so the first
> > > indirect call from the kernel into a struct_ops program,
> > > tcp_init_congestion_control() calling ->init() of a BPF congestion
> > > control for example, fails the FineIBT check and the kernel dies with
> > > a CFI failure.
> > >
> > > Move the FineIBT preamble template and its offset defines above
> > > cfi_get_func_hash() and use fineibt_preamble_hash there, so every
> > > reader of the preamble shares one definition of its layout. The
> > > CFI_FINEIBT arm is only built with CONFIG_FINEIBT, the only
> > > configuration in which cfi_mode can take that value.
> > > cfi_get_func_arity() does not need the same treatment: the __bhi_args
> > > call whose displacement it reads still ends at the function address.
> > >
> > > Fixes: 85a2d4a890dc ("x86,ibt: Use UDB instead of 0xEA")
> >
> > Gentle ping on this one. To recap: since 85a2d4a890dc the FineIBT preamble
> > stores the hash at +5, but cfi_get_func_hash() still reads it at +7, so BPF
> > struct_ops trampolines are emitted with a wrong hash and the first indirect call
> > through them (e.g. a bpf tcp_congestion_ops .cong_control) hits a FineIBT #CP.
> >
> > We have large scale panics when we use BPF congestion control.
> > Happy to respin if a different shape is preferred (e.g. moving the
> > offset next to
> > the preamble definition), but this seemed like the best way to fix the issue.
> 
> Looks fine to me. I guess it will go via tip tree?

Yes, I'll pick it up. Thanks!

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

* [tip: x86/urgent] x86/cfi: Fix FineIBT hash offset in cfi_get_func_hash()
  2026-08-31 14:48 [PATCH] x86/cfi: Fix FineIBT hash offset in cfi_get_func_hash() Soheil Hassas Yeganeh
  2026-09-08 13:22 ` Soheil Hassas Yeganeh
@ 2026-09-10  9:07 ` tip-bot2 for Soheil Hassas Yeganeh
  1 sibling, 0 replies; 5+ messages in thread
From: tip-bot2 for Soheil Hassas Yeganeh @ 2026-09-10  9:07 UTC (permalink / raw)
  To: linux-tip-commits
  Cc: Soheil Hassas Yeganeh, Peter Zijlstra (Intel), stable, #, 6.18+,
	x86, linux-kernel

The following commit has been merged into the x86/urgent branch of tip:

Commit-ID:     5a5d26f2cfe13467166219f6bf58099326912ddb
Gitweb:        https://git.kernel.org/tip/5a5d26f2cfe13467166219f6bf58099326912ddb
Author:        Soheil Hassas Yeganeh <soheil.kdev@gmail.com>
AuthorDate:    Mon, 31 Aug 2026 14:48:44 
Committer:     Peter Zijlstra <peterz@infradead.org>
CommitterDate: Thu, 10 Sep 2026 11:01:31 +02:00

x86/cfi: Fix FineIBT hash offset in cfi_get_func_hash()

The switch of the FineIBT preamble from "subl $hash, %r10d" to the
shorter "subl $hash, %eax" moved the hash immediate from offset 7 to
offset 5 of the preamble. fineibt_preamble_hash was updated to match,
but the open-coded offset in cfi_get_func_hash() was missed and it
still reads the hash at offset 7.

cfi_get_func_hash() is used by the BPF JIT to give a struct_ops
trampoline the CFI hash of the stub function it stands in for. With
FineIBT the trampoline now gets the upper half of the real hash
followed by the first two bytes of the next instruction, so the first
indirect call from the kernel into a struct_ops program,
tcp_init_congestion_control() calling ->init() of a BPF congestion
control for example, fails the FineIBT check and the kernel dies with
a CFI failure.

Move the FineIBT preamble template and its offset defines above
cfi_get_func_hash() and use fineibt_preamble_hash there, so every
reader of the preamble shares one definition of its layout. The
CFI_FINEIBT arm is only built with CONFIG_FINEIBT, the only
configuration in which cfi_mode can take that value.
cfi_get_func_arity() does not need the same treatment: the __bhi_args
call whose displacement it reads still ends at the function address.

Fixes: 85a2d4a890dc ("x86,ibt: Use UDB instead of 0xEA")
Assisted-by: LLM
Signed-off-by: Soheil Hassas Yeganeh <soheil.kdev@gmail.com>
Signed-off-by: Peter Zijlstra (Intel) <peterz@infradead.org>
Cc: stable@vger.kernel.org # 6.18+
Link: https://patch.msgid.link/20260831-b4-x86-cfi-fineibt-func-hash-v1-1-6ffc0af5c4ec@gmail.com
---
 arch/x86/kernel/alternative.c | 72 +++++++++++++++++-----------------
 1 file changed, 38 insertions(+), 34 deletions(-)

diff --git a/arch/x86/kernel/alternative.c b/arch/x86/kernel/alternative.c
index add62db..741d876 100644
--- a/arch/x86/kernel/alternative.c
+++ b/arch/x86/kernel/alternative.c
@@ -1201,6 +1201,41 @@ static bool cfi_debug __ro_after_init;
 bool cfi_bhi __ro_after_init = false;
 #endif
 
+#ifdef CONFIG_FINEIBT
+/*
+ * <fineibt_preamble_start>:
+ *  0:   f3 0f 1e fa             endbr64
+ *  4:   2d 78 56 34 12          sub    $0x12345678, %eax
+ *  9:   2e 0f 85 03 00 00 00    jne,pn 13 <fineibt_preamble_start+0x13>
+ * 10:   0f 1f 40 d6             nopl   -0x2a(%rax)
+ *
+ * Note that the JNE target is the 0xD6 byte inside the NOPL, this decodes as
+ * UDB on x86_64 and raises #UD.
+ */
+asm(	".pushsection .rodata				\n"
+	"fineibt_preamble_start:			\n"
+	"	endbr64					\n"
+	"	subl	$0x12345678, %eax		\n"
+	"fineibt_preamble_bhi:				\n"
+	"	cs jne.d32 fineibt_preamble_start+0x13	\n"
+	"#fineibt_func:					\n"
+	"	nopl	-42(%rax)			\n"
+	"fineibt_preamble_end:				\n"
+	".popsection\n"
+);
+
+extern u8 fineibt_preamble_start[];
+extern u8 fineibt_preamble_bhi[];
+extern u8 fineibt_preamble_end[];
+
+#define fineibt_preamble_size (fineibt_preamble_end - fineibt_preamble_start)
+#define fineibt_preamble_bhi  (fineibt_preamble_bhi - fineibt_preamble_start)
+#define fineibt_preamble_ud   0x13
+#define fineibt_preamble_hash 5
+
+#define fineibt_prefix_size (fineibt_preamble_size - ENDBR_INSN_SIZE)
+#endif /* CONFIG_FINEIBT */
+
 #ifdef CONFIG_CFI
 u32 cfi_get_func_hash(void *func)
 {
@@ -1208,9 +1243,11 @@ u32 cfi_get_func_hash(void *func)
 
 	func -= cfi_get_offset();
 	switch (cfi_mode) {
+#ifdef CONFIG_FINEIBT
 	case CFI_FINEIBT:
-		func += 7;
+		func += fineibt_preamble_hash;
 		break;
+#endif
 	case CFI_KCFI:
 		func += 1;
 		break;
@@ -1367,39 +1404,6 @@ early_param("cfi", cfi_parse_cmdline);
  */
 
 /*
- * <fineibt_preamble_start>:
- *  0:   f3 0f 1e fa             endbr64
- *  4:   2d 78 56 34 12          sub    $0x12345678, %eax
- *  9:   2e 0f 85 03 00 00 00    jne,pn 13 <fineibt_preamble_start+0x13>
- * 10:   0f 1f 40 d6             nopl   -0x2a(%rax)
- *
- * Note that the JNE target is the 0xD6 byte inside the NOPL, this decodes as
- * UDB on x86_64 and raises #UD.
- */
-asm(	".pushsection .rodata				\n"
-	"fineibt_preamble_start:			\n"
-	"	endbr64					\n"
-	"	subl	$0x12345678, %eax		\n"
-	"fineibt_preamble_bhi:				\n"
-	"	cs jne.d32 fineibt_preamble_start+0x13	\n"
-	"#fineibt_func:					\n"
-	"	nopl	-42(%rax)			\n"
-	"fineibt_preamble_end:				\n"
-	".popsection\n"
-);
-
-extern u8 fineibt_preamble_start[];
-extern u8 fineibt_preamble_bhi[];
-extern u8 fineibt_preamble_end[];
-
-#define fineibt_preamble_size (fineibt_preamble_end - fineibt_preamble_start)
-#define fineibt_preamble_bhi  (fineibt_preamble_bhi - fineibt_preamble_start)
-#define fineibt_preamble_ud   0x13
-#define fineibt_preamble_hash 5
-
-#define fineibt_prefix_size (fineibt_preamble_size - ENDBR_INSN_SIZE)
-
-/*
  * <fineibt_caller_start>:
  *  0:   b8 78 56 34 12          mov    $0x12345678, %eax
  *  5:   4d 8d 5b f0             lea    -0x10(%r11), %r11

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

end of thread, other threads:[~2026-09-10  9:07 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2026-08-31 14:48 [PATCH] x86/cfi: Fix FineIBT hash offset in cfi_get_func_hash() Soheil Hassas Yeganeh
2026-09-08 13:22 ` Soheil Hassas Yeganeh
2026-09-08 16:37   ` Alexei Starovoitov
2026-09-08 22:13     ` Peter Zijlstra
2026-09-10  9:07 ` [tip: x86/urgent] " tip-bot2 for Soheil Hassas Yeganeh

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox

all inboxes | Powered by JetHome®