Skip to contentSkip to conversation

Use correct destructor kfunc types

PATCH Superseded Sami Tolvanen 2 participants 10 messages Jul 25, 2025 – Jul 28, 2025

Subsystems (8)

v2 of 5 Superseded Latest is v5 →

lore
Share & tools
lore
#0 lore raw

v2 cover Use correct destructor kfunc types

Hi folks,

While running BPF self-tests with CONFIG_CFI_CLANG (Clang Control Flow Integrity) enabled, I ran into a couple of CFI failures in bpf_obj_free_fields() caused by type mismatches between the btf_dtor_kfunc_t function pointer type and the registered destructor functions.

It looks like we can't change the argument type for these functions to match btf_dtor_kfunc_t because the verifier doesn't like void pointer arguments for functions used in BPF programs, so this series fixes the issue by adding stubs with correct types to use as destructors for each instance of this I found in the kernel tree.

The last patch changes btf_check_dtor_kfuncs() to enforce the function type when CFI is enabled, so we don't end up registering destructors that panic the kernel. Perhaps this is something we could enforce even without CONFIG_CFI_CLANG?

Sami

---
v2:
- Annotated the stubs with CFI_NOSEAL to fix issues with IBT sealing on x86.
- Changed __bpf_kfunc to explicit __used __retain.

v1: https://lore.kernel.org/bpf/[email protected]/

---
Sami Tolvanen (4):
bpf: crypto: Use the correct destructor kfunc type
bpf: net_sched: Use the correct destructor kfunc type selftests/bpf: Use the correct destructor kfunc type bpf, btf: Enforce destructor kfunc type with CFI

 kernel/bpf/btf.c                                     | 7 +++++++
 kernel/bpf/crypto.c                                  | 9 ++++++++-
 net/sched/bpf_qdisc.c                                | 9 ++++++++-
 tools/testing/selftests/bpf/test_kmods/bpf_testmod.c | 9 ++++++++-
 4 files changed, 31 insertions(+), 3 deletions(-)

base-commit: 95993dc3039e29dabb9a50d074145d4cb757b08b

Signature · 1 line
2.50.1.470.g6ba607880d-goog
#1 lore raw

PATCH v2 1/4 bpf: crypto: Use the correct destructor kfunc type

With CONFIG_CFI_CLANG enabled, the kernel strictly enforces that indirect function calls use a function pointer type that matches the target function. I ran into the following type mismatch when running BPF self-tests:

CFI failure at bpf_obj_free_fields+0x190/0x238 (target:

    bpf_crypto_ctx_release+0x0/0x94; expected type: 0xa488ebfc)
  Internal error: Oops - CFI: 00000000f2008228 [#1]  SMP
  ...

As bpf_crypto_ctx_release() is also used in BPF programs and using a void pointer as the argument would make the verifier unhappy, add a simple stub function with the correct type and register it as the destructor kfunc instead.

---
 kernel/bpf/crypto.c | 9 ++++++++-
 1 file changed, 8 insertions(+), 1 deletion(-)
kernel/bpf/crypto.c +8 -1
index 94854cd9c4cc..f44aa454826b 100644
@@ -261,6 +261,13 @@ __bpf_kfunc void bpf_crypto_ctx_release(struct bpf_crypto_ctx *ctx)
 		call_rcu(&ctx->rcu, crypto_free_cb);
 }
 
+__used __retain void __bpf_crypto_ctx_release(void *ctx)
+{
+	bpf_crypto_ctx_release(ctx);
+}
+
+CFI_NOSEAL(__bpf_crypto_ctx_release);
+
 static int bpf_crypto_crypt(const struct bpf_crypto_ctx *ctx,
 			    const struct bpf_dynptr_kern *src,
 			    const struct bpf_dynptr_kern *dst,
@@ -368,7 +375,7 @@ static const struct btf_kfunc_id_set crypt_kfunc_set = {
 
 BTF_ID_LIST(bpf_crypto_dtor_ids)
 BTF_ID(struct, bpf_crypto_ctx)
-BTF_ID(func, bpf_crypto_ctx_release)
+BTF_ID(func, __bpf_crypto_ctx_release)
 
 static int __init crypto_kfunc_init(void)
 {
Signature · 1 line
2.50.1.470.g6ba607880d-goog
reviewer
#2 lore raw

Re: [PATCH bpf-next v2 1/4] bpf: crypto: Use the correct destructor kfunc type

Quoted 25 lines · Sami Tolvanen

With CONFIG_CFI_CLANG enabled, the kernel strictly enforces that indirect function calls use a function pointer type that matches the target function. I ran into the following type mismatch when running BPF self-tests:

   CFI failure at bpf_obj_free_fields+0x190/0x238 (target:
     bpf_crypto_ctx_release+0x0/0x94; expected type: 0xa488ebfc)
   Internal error: Oops - CFI: 00000000f2008228 [#1]  SMP
   ...

As bpf_crypto_ctx_release() is also used in BPF programs and using a void pointer as the argument would make the verifier unhappy, add a simple stub function with the correct type and register it as the destructor kfunc instead.

---
  kernel/bpf/crypto.c | 9 ++++++++-
  1 file changed, 8 insertions(+), 1 deletion(-)
kernel/bpf/crypto.c +0 -0
index 94854cd9c4cc..f44aa454826b 100644

kernel/bpf/crypto.c __bpf_kfunc void bpf_crypto_ctx_release(struct bpf_crypto_ctx *ctx) · line 269

Quoted 9 lines
kernel/bpf/crypto.c +6 -0
@@ -261,6 +261,13 @@ __bpf_kfunc void bpf_crypto_ctx_release(struct bpf_crypto_ctx *ctx)
  		call_rcu(&ctx->rcu, crypto_free_cb);
  }
 
+__used __retain void __bpf_crypto_ctx_release(void *ctx)
+{
+	bpf_crypto_ctx_release(ctx);
+}
+
+CFI_NOSEAL(__bpf_crypto_ctx_release);

Okay, looks like Peter has made similar changes before.
See https://lore.kernel.org/all/[email protected]/

To be consistent with existing code base, I think the following change is better:

kernel/bpf/crypto.c +7 -1
index 94854cd9c4cc..a267d9087d40 100644
@@ -261,6 +261,12 @@ __bpf_kfunc void bpf_crypto_ctx_release(struct bpf_crypto_ctx *ctx)
                call_rcu(&ctx->rcu, crypto_free_cb);
 }
 
+__bpf_kfunc void bpf_crypto_ctx_release_dtor(void *ctx)
+{
+       bpf_crypto_ctx_release(ctx);
+}
+CFI_NOSEAL(bpf_crypto_ctx_release_dtor);
+
 static int bpf_crypto_crypt(const struct bpf_crypto_ctx *ctx,
                            const struct bpf_dynptr_kern *src,
                            const struct bpf_dynptr_kern *dst,
@@ -368,7 +374,7 @@ static const struct btf_kfunc_id_set crypt_kfunc_set = {
 
 BTF_ID_LIST(bpf_crypto_dtor_ids)
 BTF_ID(struct, bpf_crypto_ctx)
-BTF_ID(func, bpf_crypto_ctx_release)
+BTF_ID(func, bpf_crypto_ctx_release_dtor)
 
 static int __init crypto_kfunc_init(void)
 {

The same code pattern can be done for patch 2 and patch 3.

Quoted 13 lines
kernel/bpf/crypto.c +2 -1
+
  static int bpf_crypto_crypt(const struct bpf_crypto_ctx *ctx,
  			    const struct bpf_dynptr_kern *src,
  			    const struct bpf_dynptr_kern *dst,
@@ -368,7 +375,7 @@ static const struct btf_kfunc_id_set crypt_kfunc_set = {
 
  BTF_ID_LIST(bpf_crypto_dtor_ids)
  BTF_ID(struct, bpf_crypto_ctx)
-BTF_ID(func, bpf_crypto_ctx_release)
+BTF_ID(func, __bpf_crypto_ctx_release)
 
  static int __init crypto_kfunc_init(void)
  {
#3 lore raw

Re: [PATCH bpf-next v2 1/4] bpf: crypto: Use the correct destructor kfunc type

Quoted 31 lines · Yonghong Song

Okay, looks like Peter has made similar changes before.
See https://lore.kernel.org/all/[email protected]/

To be consistent with existing code base, I think the following change is better:

kernel/bpf/crypto.c +7 -1
index 94854cd9c4cc..a267d9087d40 100644
@@ -261,6 +261,12 @@ __bpf_kfunc void bpf_crypto_ctx_release(struct bpf_crypto_ctx *ctx)
                call_rcu(&ctx->rcu, crypto_free_cb);
 }
+__bpf_kfunc void bpf_crypto_ctx_release_dtor(void *ctx)
+{
+       bpf_crypto_ctx_release(ctx);
+}
+CFI_NOSEAL(bpf_crypto_ctx_release_dtor);
+
 static int bpf_crypto_crypt(const struct bpf_crypto_ctx *ctx,
                            const struct bpf_dynptr_kern *src,
                            const struct bpf_dynptr_kern *dst,
@@ -368,7 +374,7 @@ static const struct btf_kfunc_id_set crypt_kfunc_set = {
 BTF_ID_LIST(bpf_crypto_dtor_ids)
 BTF_ID(struct, bpf_crypto_ctx)
-BTF_ID(func, bpf_crypto_ctx_release)
+BTF_ID(func, bpf_crypto_ctx_release_dtor)
 static int __init crypto_kfunc_init(void)
 {
 
Quoted 1 line

The same code pattern can be done for patch 2 and patch 3.

Sure, I'll update the patches and send v3.

Sami

#4 lore raw

PATCH v2 2/4 bpf: net_sched: Use the correct destructor kfunc type

In reply to Sami Tolvanen

With CONFIG_CFI_CLANG enabled, the kernel strictly enforces that indirect function calls use a function pointer type that matches the target function. As bpf_kfree_skb() signature differs from the btf_dtor_kfunc_t pointer type used for the destructor calls in bpf_obj_free_fields(), add a stub function with the correct type to fix the type mismatch.

---
 net/sched/bpf_qdisc.c | 9 ++++++++-
 1 file changed, 8 insertions(+), 1 deletion(-)
net/sched/bpf_qdisc.c +8 -1
index adcb618a2bfc..890edd7ab64b 100644
@@ -202,6 +202,13 @@ __bpf_kfunc void bpf_kfree_skb(struct sk_buff *skb)
 	kfree_skb(skb);
 }
 
+__used __retain void __bpf_kfree_skb(void *skb)
+{
+	bpf_kfree_skb(skb);
+}
+
+CFI_NOSEAL(__bpf_kfree_skb);
+
 /* bpf_qdisc_skb_drop - Drop an skb by adding it to a deferred free list.
  * @skb: The skb whose reference to be released and dropped.
  * @to_free_list: The list of skbs to be dropped.
@@ -449,7 +456,7 @@ static struct bpf_struct_ops bpf_Qdisc_ops = {
 	.owner = THIS_MODULE,
 };
 
-BTF_ID_LIST_SINGLE(bpf_sk_buff_dtor_ids, func, bpf_kfree_skb)
+BTF_ID_LIST_SINGLE(bpf_sk_buff_dtor_ids, func, __bpf_kfree_skb)
 
 static int __init bpf_qdisc_kfunc_init(void)
 {
Signature · 1 line
2.50.1.470.g6ba607880d-goog
#5 lore raw

PATCH v2 3/4 selftests/bpf: Use the correct destructor kfunc type

In reply to Sami Tolvanen

With CONFIG_CFI_CLANG enabled, the kernel strictly enforces that indirect function calls use a function pointer type that matches the target function. As bpf_testmod_ctx_release() signature differs from the btf_dtor_kfunc_t pointer type used for the destructor calls in bpf_obj_free_fields(), add a stub function with the correct type to fix the type mismatch.

---
 tools/testing/selftests/bpf/test_kmods/bpf_testmod.c | 9 ++++++++-
 1 file changed, 8 insertions(+), 1 deletion(-)
tools/testing/selftests/bpf/test_kmods/bpf_testmod.c +8 -1
index e9e918cdf31f..b086244f259a 100644
@@ -249,6 +249,13 @@ __bpf_kfunc void bpf_testmod_ctx_release(struct bpf_testmod_ctx *ctx)
 		call_rcu(&ctx->rcu, testmod_free_cb);
 }
 
+__used __retain void __bpf_testmod_ctx_release(void *ctx)
+{
+	bpf_testmod_ctx_release(ctx);
+}
+
+CFI_NOSEAL(__bpf_testmod_ctx_release);
+
 static struct bpf_testmod_ops3 *st_ops3;
 
 static int bpf_testmod_test_3(void)
@@ -631,7 +638,7 @@ BTF_KFUNCS_END(bpf_testmod_common_kfunc_ids)
 
 BTF_ID_LIST(bpf_testmod_dtor_ids)
 BTF_ID(struct, bpf_testmod_ctx)
-BTF_ID(func, bpf_testmod_ctx_release)
+BTF_ID(func, __bpf_testmod_ctx_release)
 
 static const struct btf_kfunc_id_set bpf_testmod_common_kfunc_set = {
 	.owner = THIS_MODULE,
Signature · 1 line
2.50.1.470.g6ba607880d-goog
#6 lore raw

PATCH v2 4/4 bpf, btf: Enforce destructor kfunc type with CFI

In reply to Sami Tolvanen

Ensure that registered destructor kfuncs have the same type as btf_dtor_kfunc_t to avoid a kernel panic on systems with CONFIG_CFI_CLANG enabled.

---
 kernel/bpf/btf.c | 7 +++++++
 1 file changed, 7 insertions(+)
kernel/bpf/btf.c +7 -0
index 0aff814cb53a..2b0ebd46db4a 100644
@@ -8856,6 +8856,13 @@ static int btf_check_dtor_kfuncs(struct btf *btf, const struct btf_id_dtor_kfunc
 		 */
 		if (!t || !btf_type_is_ptr(t))
 			return -EINVAL;
+
+		if (IS_ENABLED(CONFIG_CFI_CLANG)) {
+			/* Ensure the destructor kfunc type matches btf_dtor_kfunc_t */
+			t = btf_type_by_id(btf, t->type);
+			if (!btf_type_is_void(t))
+				return -EINVAL;
+		}
 	}
 	return 0;
 }
Signature · 1 line
2.50.1.470.g6ba607880d-goog
reviewer
#8 lore raw
In reply to Sami Tolvanen
Quoted 37 lines · Sami Tolvanen

Hi folks,

While running BPF self-tests with CONFIG_CFI_CLANG (Clang Control Flow Integrity) enabled, I ran into a couple of CFI failures in bpf_obj_free_fields() caused by type mismatches between the btf_dtor_kfunc_t function pointer type and the registered destructor functions.

It looks like we can't change the argument type for these functions to match btf_dtor_kfunc_t because the verifier doesn't like void pointer arguments for functions used in BPF programs, so this series fixes the issue by adding stubs with correct types to use as destructors for each instance of this I found in the kernel tree.

The last patch changes btf_check_dtor_kfuncs() to enforce the function type when CFI is enabled, so we don't end up registering destructors that panic the kernel. Perhaps this is something we could enforce even without CONFIG_CFI_CLANG?

Sami

---
v2:
- Annotated the stubs with CFI_NOSEAL to fix issues with IBT sealing on x86.
- Changed __bpf_kfunc to explicit __used __retain.

v1: https://lore.kernel.org/bpf/[email protected]/

---
Sami Tolvanen (4):
bpf: crypto: Use the correct destructor kfunc type
bpf: net_sched: Use the correct destructor kfunc type selftests/bpf: Use the correct destructor kfunc type bpf, btf: Enforce destructor kfunc type with CFI

Quoted 8 lines
  kernel/bpf/btf.c                                     | 7 +++++++
  kernel/bpf/crypto.c                                  | 9 ++++++++-
  net/sched/bpf_qdisc.c                                | 9 ++++++++-
  tools/testing/selftests/bpf/test_kmods/bpf_testmod.c | 9 ++++++++-
  4 files changed, 31 insertions(+), 3 deletions(-)

base-commit: 95993dc3039e29dabb9a50d074145d4cb757b08b

With this patch set and no CONFIG_CFI_CLANG in .config, the bpf selftests work okay. In bpf ci, CONFIG_CFI_CLANG is not enabled.

But if enabling CONFIG_CFI_CLANG, this patch set fixed ./test_progs run issue, but there are some test failures like

===
test_get_linfo:FAIL:check jited_linfo[1]:ffffffffa000d581 - ffffffffa000d558 > 39 processed 4 insns (limit 1000000) max_states_per_insn 0 total_states 0 peak_states 0 mark_read 0 #32/186 btf/line_info (No subprog):FAIL

test_get_linfo:FAIL:check jited_linfo[1]:ffffffffa000dee5 - ffffffffa000debc > 39 processed 4 insns (limit 1000000) max_states_per_insn 0 total_states 0 peak_states 0 mark_read 0 #32/189 btf/line_info (No subprog. zero tailing line_info:FAIL

...

test_get_linfo:FAIL:check jited_linfo[1]:ffffffffa000e069 - ffffffffa000e040 > 38 processed 9 insns (limit 1000000) max_states_per_insn 0 total_states 1 peak_states 1 mark_read 0 #32/202 btf/line_info (dead subprog + dead start w/ move):FAIL
#32 btf:FAIL
===

The failure probably not related to this patch, but rather related to CONFIG_CFI_CLANG itself. I will debug this separately.

#9 lore raw
Quoted 20 lines · Yonghong Song

With this patch set and no CONFIG_CFI_CLANG in .config, the bpf selftests work okay. In bpf ci, CONFIG_CFI_CLANG is not enabled.

But if enabling CONFIG_CFI_CLANG, this patch set fixed ./test_progs run issue, but there are some test failures like

===
test_get_linfo:FAIL:check jited_linfo[1]:ffffffffa000d581 - ffffffffa000d558 > 39 processed 4 insns (limit 1000000) max_states_per_insn 0 total_states 0 peak_states 0 mark_read 0 #32/186 btf/line_info (No subprog):FAIL

test_get_linfo:FAIL:check jited_linfo[1]:ffffffffa000dee5 - ffffffffa000debc > 39 processed 4 insns (limit 1000000) max_states_per_insn 0 total_states 0 peak_states 0 mark_read 0 #32/189 btf/line_info (No subprog. zero tailing line_info:FAIL

...

Quoted 8 lines

test_get_linfo:FAIL:check jited_linfo[1]:ffffffffa000e069 - ffffffffa000e040 > 38 processed 9 insns (limit 1000000) max_states_per_insn 0 total_states 1 peak_states 1 mark_read 0 #32/202 btf/line_info (dead subprog + dead start w/ move):FAIL
#32 btf:FAIL
===

The failure probably not related to this patch, but rather related to CONFIG_CFI_CLANG itself. I will debug this separately.

Agreed, that looks unrelated to this series.

Sami