btf: reject to register duplicated kfunc
PATCH v4 1/1 btf: reject to register duplicated kfunc
I had an ebpf program which calls a kfunc defined and implemented in one of my kernel modules, it was working fine in 6.16, but was rejected to run by libbpf in 6.19, the error message was:
libbpf: extern (func ksym) 'bpf_strstr': func_proto [5] incompatible with vmlinux [94389]
The reason is there is a new added kfunc in kernel 6.19 which happens to be the same name with my kfunc. However the error message is not obvious enough to address problem immediately.
Therefore, this patches searches duplicated kfunc in both btf_vmlinux and btf_modules list before a kernel module attempts to register kfuncs through register_btf_kfunc_id_set, it prints clear error message and returns error code if same name kfunc has already implemented and registered, then developer knows at the first place.
---
changelog:
v1 --- v2:
libbpf has already specified which module this kfunc belongs to as ebpf code onwer's expectation, then verifier uses find_kallsyms_symbol_value to search kfunc's addr.
v2 --- v3:
After v2, I tried a new idea of introducing a namespace in libbpf to specify kfunc owner in an ebpf program suggested by Kaitao Cheng, please see [1]. Alex suggested to go back to report an error during kmod load on conflicting kfunc name for now. What's more, v2 only searched bpf_vmlinux, v3 also traverses btf_modules list.
v3 --- v4
Fixed some coding style problems suggested from Kaitao Cheng.
--- kernel/bpf/btf.c | 38 +++++++++++++++++++++++++++++++++++++- 1 file changed, 37 insertions(+), 1 deletion(-)
kernel/bpf/btf.c +37 -1
@@ -8618,6 +8618,41 @@ static int btf_check_iter_kfuncs(struct btf *btf, const char *func_name,return 0 ; } +static int btf_check_kfunc_name (struct btf *btf,const char *func_name,u32 kind) +{ +#ifdef CONFIG_DEBUG_INFO_BTF_MODULES +struct btf_module *btf_mod, *tmp; +#endif +s32 id; + +if (!btf_is_module (btf)) +return 0 ; + + id =btf_find_by_name_kind (bpf_get_btf_vmlinux (), func_name, kind); +if (id >=0 ) { +pr_err ("kfunc %s (id: %d) is already present in vmlinux.\n" , + func_name, id); +return -EINVAL ; + } + +#ifdef CONFIG_DEBUG_INFO_BTF_MODULES +mutex_lock (&btf_module_mutex); +list_for_each_entry_safe (btf_mod, tmp, &btf_modules, list) { +if (btf_mod->btf == btf) +continue ; + id =btf_find_by_name_kind (btf_mod->btf, func_name, kind); +if (id >=0 ) { +pr_err ("kfunc %s (id: %d) is already present in module %s.\n" , + func_name, id, btf_mod->module->name); +mutex_unlock (&btf_module_mutex); +return -EINVAL ; + } + } +mutex_unlock (&btf_module_mutex); +#endif +return 0 ; +} +static int btf_check_kfunc_protos (struct btf *btf,u32 func_id,u32 func_flags) {const struct btf_type *func;
@@ -8631,7 +8666,8 @@ static int btf_check_kfunc_protos(struct btf *btf, u32 func_id, u32 func_flags)/* sanity check kfunc name */ func_name =btf_name_by_offset (btf, func->name_off); -if (!func_name || !func_name[0 ]) +if (!func_name || !func_name[0 ] || +btf_check_kfunc_name (btf, func_name,BTF_INFO_KIND (func->info)))return -EINVAL ; func =btf_type_by_id (btf, func->type);
Signature · 1 line
2.43.0
[email protected]
AI reviewThank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider: - [High] Kfunc registration incorrectly fails if the kfunc name matches ANY internal or static function in…
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [High] Kfunc registration incorrectly fails if the kfunc name matches ANY internal or static function in vmlinux or loaded modules, introducing load-order dependencies. --
commit 8a8189baf0633350e5ca2b793c2340e48f4787df
Author: Song Chen <[email protected]>
btf: reject to register duplicated kfunc
This commit attempts to prevent duplicate BPF kfunc registrations by searching for existing kfunc names in vmlinux and other modules' BTF data during kfunc registration.
Quoted 7 lines
kernel/bpf/btf.c +0
-0@@ -8684,6 +8684,41 @@ static int btf_check_iter_kfuncs(struct btf *btf, const char *func_name,return 0 ; }
kernel/bpf/btf.c static int btf_check_iter_kfuncs(struct btf *btf, const char *func_name, · line 8695
Quoted 10 lines
kernel/bpf/btf.c +9
-0+static int btf_check_kfunc_name (struct btf *btf,const char *func_name,u32 kind) +{ +#ifdef CONFIG_DEBUG_INFO_BTF_MODULES +struct btf_module *btf_mod, *tmp; +#endif +s32 id; + +if (!btf_is_module (btf)) +return 0 ;
[Severity: High]
Does this bypass collision checks for built-in vmlinux kfuncs? It looks like this might fail to catch intra-kernel name collisions.
kernel/bpf/btf.c static int btf_check_iter_kfuncs(struct btf *btf, const char *func_name, · line 8697
Quoted 2 lines
kernel/bpf/btf.c +2
-0+ + id =btf_find_by_name_kind (bpf_get_btf_vmlinux (), func_name, kind);
[Severity: High]
Since modern kernels generate BTF for all global and static functions, does btf_find_by_name_kind() with BTF_KIND_FUNC search the entire BTF data rather than just registered kfuncs?
Will this incorrectly reject a valid kfunc registration if its name happens to match any unrelated static or internal function anywhere in vmlinux?
kernel/bpf/btf.c static int btf_check_iter_kfuncs(struct btf *btf, const char *func_name, · line 8642
Quoted 11 lines
kernel/bpf/btf.c +11
-0+if (id >=0 ) { +pr_err ("kfunc %s (id: %d) is already present in vmlinux.\n" , + func_name, id); +return -EINVAL ; + } + +#ifdef CONFIG_DEBUG_INFO_BTF_MODULES +mutex_lock (&btf_module_mutex); +list_for_each_entry_safe (btf_mod, tmp, &btf_modules, list) { +if (btf_mod->btf == btf) +continue ;
[Severity: High]
Are we skipping collision detection within the module's own BTF here?
kernel/bpf/btf.c static int btf_check_iter_kfuncs(struct btf *btf, const char *func_name, · line 8643
Quoted 1 line
kernel/bpf/btf.c +1
-0+ id =btf_find_by_name_kind (btf_mod->btf, func_name, kind);
[Severity: High]
Can this create unpredictable load-order dependencies between unrelated modules?
If module A has a static function named "foo", and module B registers a kfunc named "foo", it appears B will fail to load if A is already loaded, but will succeed if B is loaded first. Should this code check against actually registered kfunc sets rather than all functions in every module's BTF?
Quoted 12 lines
kernel/bpf/btf.c +12
-0+if (id >=0 ) { +pr_err ("kfunc %s (id: %d) is already present in module %s.\n" , + func_name, id, btf_mod->module->name); +mutex_unlock (&btf_module_mutex); +return -EINVAL ; + } + } +mutex_unlock (&btf_module_mutex); +#endif +return 0 ; +} +
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1
Quoted 92 lines · Song Chen
I had an ebpf program which calls a kfunc defined and implemented in one of my kernel modules, it was working fine in 6.16, but was rejected to run by libbpf in 6.19, the error message was:
libbpf: extern (func ksym) '
bpf_strstr':func_proto[5] incompatible with vmlinux [94389]The reason is there is a new added kfunc in kernel 6.19 which happens to be the same name with my kfunc. However the error message is not obvious enough to address problem immediately.
Therefore, this patches searches duplicated kfunc in both
btf_vmlinuxandbtf_moduleslist before a kernel module attempts to register kfuncs throughregister_btf_kfunc_id_set, it prints clear error message and returns error code if same name kfunc has already implemented and registered, then developer knows at the first place.Suggested-by Alexei Starovoitov <[email protected]>Suggested-by Kaitao Cheng <[email protected]>Reviewed-by Yonghong Song <[email protected]>Signed-off-by Song Chen <[email protected]>---
changelog:
v1 --- v2:
libbpf has already specified which module this kfunc belongs to as ebpf code onwer's expectation, then verifier usesfind_kallsyms_symbol_valueto search kfunc's addr.v2 --- v3:
After v2, I tried a new idea of introducing a namespace in libbpf to specify kfunc owner in an ebpf program suggested by Kaitao Cheng, please see [1]. Alex suggested to go back to report an error during kmod load on conflicting kfunc name for now. What's more, v2 only searchedbpf_vmlinux, v3 also traversesbtf_moduleslist.v3 --- v4
Fixed some coding style problems suggested from Kaitao Cheng.--- kernel/bpf/btf.c | 38 +++++++++++++++++++++++++++++++++++++- 1 file changed, 37 insertions(+), 1 deletion(-)kernel/bpf/btf.c +35
-0@@ -8618,6 +8618,41 @@ static int btf_check_iter_kfuncs(struct btf *btf, const char *func_name,return 0 ; } +static int btf_check_kfunc_name (struct btf *btf,const char *func_name,u32 kind) +{ +#ifdef CONFIG_DEBUG_INFO_BTF_MODULES +struct btf_module *btf_mod, *tmp; +#endif +s32 id; + +if (!btf_is_module (btf)) +return 0 ; + + id =btf_find_by_name_kind (bpf_get_btf_vmlinux (), func_name, kind); +if (id >=0 ) { +pr_err ("kfunc %s (id: %d) is already present in vmlinux.\n" , + func_name, id); +return -EINVAL ; + } + +#ifdef CONFIG_DEBUG_INFO_BTF_MODULES +mutex_lock (&btf_module_mutex); +list_for_each_entry_safe (btf_mod, tmp, &btf_modules, list) { +if (btf_mod->btf == btf) +continue ; + id =btf_find_by_name_kind (btf_mod->btf, func_name, kind); +if (id >=0 ) { +pr_err ("kfunc %s (id: %d) is already present in module %s.\n" , + func_name, id, btf_mod->module->name); +mutex_unlock (&btf_module_mutex); +return -EINVAL ; + } + } +mutex_unlock (&btf_module_mutex); +#endif +return 0 ; +} +static int btf_check_kfunc_protos (struct btf *btf,u32 func_id,u32 func_flags) {const struct btf_type *func;
kernel/bpf/btf.c static int btf_check_kfunc_protos(struct btf *btf, u32 func_id, u32 func_flags) · line 8673
Quoted 9 lines
kernel/bpf/btf.c +2
-1@@ -8631,7 +8666,8 @@ static int btf_check_kfunc_protos(struct btf *btf, u32 func_id, u32 func_flags)/* sanity check kfunc name */ func_name =btf_name_by_offset (btf, func->name_off); -if (!func_name || !func_name[0 ]) +if (!func_name || !func_name[0 ] || +btf_check_kfunc_name (btf, func_name,BTF_INFO_KIND (func->info)))return -EINVAL ; func =btf_type_by_id (btf, func->type);
Signature · 2 lines
Thanks Kaitao Cheng
Yonghong Song
reviewerQuoted 20 lines · Song Chen
I had an ebpf program which calls a kfunc defined and implemented in one of my kernel modules, it was working fine in 6.16, but was rejected to run by libbpf in 6.19, the error message was:
libbpf: extern (func ksym) '
bpf_strstr':func_proto[5] incompatible with vmlinux [94389]The reason is there is a new added kfunc in kernel 6.19 which happens to be the same name with my kfunc. However the error message is not obvious enough to address problem immediately.
Therefore, this patches searches duplicated kfunc in both
btf_vmlinuxandbtf_moduleslist before a kernel module attempts to register kfuncs throughregister_btf_kfunc_id_set, it prints clear error message and returns error code if same name kfunc has already implemented and registered, then developer knows at the first place.
Quoted 4 lines
Suggested-by Alexei Starovoitov <[email protected]>Suggested-by Kaitao Cheng <[email protected]>Reviewed-by Yonghong Song <[email protected]>Signed-off-by Song Chen <[email protected]>
The subject can be [PATCH bpf v4] bpf: Reject to register duplicated kfunc
Quoted 26 lines
---
changelog:
v1 --- v2:
libbpf has already specified which module this kfunc belongs to as ebpf code onwer's expectation, then verifier usesfind_kallsyms_symbol_valueto search kfunc's addr.v2 --- v3:
After v2, I tried a new idea of introducing a namespace in libbpf to specify kfunc owner in an ebpf program suggested by Kaitao Cheng, please see [1]. Alex suggested to go back to report an error during kmod load on conflicting kfunc name for now. What's more, v2 only searchedbpf_vmlinux, v3 also traversesbtf_moduleslist.v3 --- v4
Fixed some coding style problems suggested from Kaitao Cheng.--- kernel/bpf/btf.c | 38 +++++++++++++++++++++++++++++++++++++- 1 file changed, 37 insertions(+), 1 deletion(-)kernel/bpf/btf.c +0
-0
kernel/bpf/btf.c static int btf_check_iter_kfuncs(struct btf *btf, const char *func_name, · line 8626
Quoted 9 lines
kernel/bpf/btf.c +6
-0@@ -8618,6 +8618,41 @@ static int btf_check_iter_kfuncs(struct btf *btf, const char *func_name,return 0 ; } +static int btf_check_kfunc_name (struct btf *btf,const char *func_name,u32 kind) +{ +#ifdef CONFIG_DEBUG_INFO_BTF_MODULES +struct btf_module *btf_mod, *tmp; +#endif +s32 id;
s32 err = 0, id;
Quoted 12 lines
kernel/bpf/btf.c +12
-0+ +if (!btf_is_module (btf)) +return 0 ; + + id =btf_find_by_name_kind (bpf_get_btf_vmlinux (), func_name, kind); +if (id >=0 ) { +pr_err ("kfunc %s (id: %d) is already present in vmlinux.\n" , + func_name, id); +return -EINVAL ; + } + +#ifdef CONFIG_DEBUG_INFO_BTF_MODULES
kernel/bpf/btf.c static int btf_check_iter_kfuncs(struct btf *btf, const char *func_name, · line 8648
Quoted 10 lines
kernel/bpf/btf.c +10
-0+mutex_lock (&btf_module_mutex); +list_for_each_entry_safe (btf_mod, tmp, &btf_modules, list) { +if (btf_mod->btf == btf) +continue ; + id =btf_find_by_name_kind (btf_mod->btf, func_name, kind); +if (id >=0 ) { +pr_err ("kfunc %s (id: %d) is already present in module %s.\n" , + func_name, id, btf_mod->module->name); +mutex_unlock (&btf_module_mutex); +return -EINVAL ;
Let us avoid the above mutex_unlock and 'return -EINVAL', just do
err = -EINVAL; break;
kernel/bpf/btf.c static int btf_check_iter_kfuncs(struct btf *btf, const char *func_name, · line 8653
Quoted 5 lines
kernel/bpf/btf.c +5
-0+ } + } +mutex_unlock (&btf_module_mutex); +#endif +return 0 ;
return err;
kernel/bpf/btf.c static int btf_check_kfunc_protos(struct btf *btf, u32 func_id, u32 func_flags) · line 8670
Quoted 12 lines
kernel/bpf/btf.c +4
-1+} +static int btf_check_kfunc_protos (struct btf *btf,u32 func_id,u32 func_flags) {const struct btf_type *func;@@ -8631,7 +8666,8 @@ static int btf_check_kfunc_protos(struct btf *btf, u32 func_id, u32 func_flags)/* sanity check kfunc name */ func_name =btf_name_by_offset (btf, func->name_off); -if (!func_name || !func_name[0 ]) +if (!func_name || !func_name[0 ] || +btf_check_kfunc_name (btf, func_name,BTF_INFO_KIND (func->info)))
format issue:
if (!func_name || !func_name[0] || btf_check_kfunc_name(btf, func_name, BTF_INFO_KIND(func->info))) ...
Quoted 3 lines
kernel/bpf/btf.c +0
-0return -EINVAL ; func =btf_type_by_id (btf, func->type);
Quoted 3 lines · Yonghong Song
On 6/2/26 4:07 AM, Song Chen wrote:
[...]
Quoted 9 lines
Quoted 9 lines
kernel/bpf/btf.c +9
-0+ +#ifdef CONFIG_DEBUG_INFO_BTF_MODULES + mutex_lock(&btf_module_mutex); + list_for_each_entry_safe(btf_mod, tmp, &btf_modules, list) { + if (btf_mod->btf == btf) + continue; + id = btf_find_by_name_kind(btf_mod->btf, func_name, kind); + if (id >= 0) { + pr_err("kfunc %s (id: %d) is already present in module
Quoted 9 lines
Quoted 4 lines
%s.\n",
+func_name, id,btf_mod->module->name); +mutex_unlock(&btf_module_mutex);
+ return-EINVAL;Let us avoid the above
mutex_unlockand 'return-EINVAL', just do err =-EINVAL;
break;
Better to use guard(mutex)(&btf_module_mutex)?
Thanks,
Leon
Quoted 1 line
Quoted 1 line
[...]
Hi,
Quoted 24 lines · Leon Hwang
Quoted 3 lines · Yonghong Song
On 6/2/26 4:07 AM, Song Chen wrote:
[...]
Quoted 9 lines
Quoted 9 lines
kernel/bpf/btf.c +9
-0+ +#ifdef CONFIG_DEBUG_INFO_BTF_MODULES + mutex_lock(&btf_module_mutex); + list_for_each_entry_safe(btf_mod, tmp, &btf_modules, list) { + if (btf_mod->btf == btf) + continue; + id = btf_find_by_name_kind(btf_mod->btf, func_name, kind); + if (id >= 0) { + pr_err("kfunc %s (id: %d) is already present in moduleQuoted 9 lines
Quoted 4 lines
%s.\n",
+func_name, id,btf_mod->module->name); +mutex_unlock(&btf_module_mutex);
+ return-EINVAL;Let us avoid the above
mutex_unlockand 'return-EINVAL', just do
err =-EINVAL;
break;
Quoted 2 lines
Better to use guard(mutex)(&btf_module_mutex)?
This is the first time i used guard and it's awesome. I have tested in my code and will send a new patch later, many thanks.
Quoted 4 lines
Thanks,
LeonQuoted 1 line
Quoted 1 line
[...]
Song
Hi,
Quoted 97 lines · Yonghong Song
Quoted 20 lines · Song Chen
I had an ebpf program which calls a kfunc defined and implemented in one of my kernel modules, it was working fine in 6.16, but was rejected to run by libbpf in 6.19, the error message was:
libbpf: extern (func ksym) '
bpf_strstr':func_proto[5] incompatible with vmlinux [94389]The reason is there is a new added kfunc in kernel 6.19 which happens to be the same name with my kfunc. However the error message is not obvious enough to address problem immediately.
Therefore, this patches searches duplicated kfunc in both
btf_vmlinuxandbtf_moduleslist before a kernel module attempts to register kfuncs throughregister_btf_kfunc_id_set, it prints clear error message and returns error code if same name kfunc has already implemented and registered, then developer knows at the first place.Quoted 4 lines
Suggested-by Alexei Starovoitov <[email protected]>Suggested-by Kaitao Cheng <[email protected]>Reviewed-by Yonghong Song <[email protected]>Signed-off-by Song Chen <[email protected]>The subject can be [PATCH bpf v4] bpf: Reject to register duplicated kfunc
Quoted 26 lines
---
changelog:
v1 --- v2:
libbpf has already specified which module this kfunc belongs to as ebpf code onwer's expectation, then verifier usesfind_kallsyms_symbol_valueto search kfunc's addr.v2 --- v3:
After v2, I tried a new idea of introducing a namespace in libbpf to specify kfunc owner in an ebpf program suggested by Kaitao Cheng, please see [1]. Alex suggested to go back to report an error during kmod load on conflicting kfunc name for now. What's more, v2 only searchedbpf_vmlinux, v3 also traversesbtf_moduleslist.v3 --- v4
Fixed some coding style problems suggested from Kaitao Cheng.
---
kernel/bpf/btf.c| 38 +++++++++++++++++++++++++++++++++++++-1 file changed, 37 insertions(+), 1 deletion(-)kernel/bpf/btf.c +0
-0kernel/bpf/btf.c static int btf_check_iter_kfuncs(struct btf · line 8627
Quoted 10 lines
kernel/bpf/btf.c +6
-0@@ -8618,6 +8618,41 @@ static int btf_check_iter_kfuncs(struct btf *btf, const char *func_name, return 0; } +static int btf_check_kfunc_name (struct btf *btf,const char *func_name,u32 kind) +{ +#ifdef CONFIG_DEBUG_INFO_BTF_MODULES + struct btf_module *btf_mod, *tmp; +#endif + s32 id;s32 err = 0, id;
Quoted 13 lines
kernel/bpf/btf.c +13
-0+ + if (!btf_is_module(btf)) + return 0; + + id = btf_find_by_name_kind(bpf_get_btf_vmlinux(), func_name, kind); + if (id >= 0) { + pr_err("kfunc %s (id: %d) is already present in vmlinux.\n", + func_name, id); + return -EINVAL; + } + +#ifdef CONFIG_DEBUG_INFO_BTF_MODULES + mutex_lock(&btf_module_mutex);kernel/bpf/btf.c static int btf_check_iter_kfuncs(struct btf *btf, const char *func_name, · line 8648
Quoted 10 lines
kernel/bpf/btf.c +9
-0+ list_for_each_entry_safe(btf_mod, tmp, &btf_modules, list) { + if (btf_mod->btf == btf) + continue; + id = btf_find_by_name_kind(btf_mod->btf, func_name, kind); + if (id >= 0) { + pr_err("kfunc %s (id: %d) is already present in module %s.\n", + func_name, id, btf_mod->module->name); + mutex_unlock(&btf_module_mutex); + return -EINVAL;Let us avoid the above
mutex_unlockand 'return-EINVAL', just do
err =-EINVAL;
break;
Quoted 8 lines
kernel/bpf/btf.c static int btf_check_iter_kfuncs(struct btf *btf, const char *func_name, · line 8653
Quoted 5 lines
kernel/bpf/btf.c +5
-0+ } + } + mutex_unlock(&btf_module_mutex); +#endif + return 0;return err;
I will go with guard(mutex), please review my next submit and let me know if you're ok with it or not. Thanks.
Quoted 9 lines
Quoted 9 lines
kernel/bpf/btf.c +2
-0+} + static int btf_check_kfunc_protos(struct btf *btf, u32 func_id, u32 func_flags){
const structbtf_type*func;kernel/bpf/btf.c +0
-0@@ -8631,7 +8666,8 @@ static int btf_check_kfunc_protos(struct btf *btf, u32 func_id, u32 func_flags) /* sanity check kfunc name */ func_name = btf_name_by_offset(btf, func->name_off);
Quoted 9 lines
kernel/bpf/btf.c static int btf_check_kfunc_protos(struct btf *btf, u32 func_id, u32 func_flags) · line 8670
Quoted 3 lines
kernel/bpf/btf.c +2
-1- if (!func_name || !func_name[0]) + if (!func_name || !func_name[0] || + btf_check_kfunc_name(btf, func_name, BTF_INFO_KIND(func->info)))format issue:
if (!func_name || !func_name[0] ||btf_check_kfunc_name(btf, func_name, BTF_INFO_KIND(func->info)))
...
I will fix it with indentation plus space.
Quoted 3 lines
kernel/bpf/btf.c static int btf_check_kfunc_protos(struct btf *btf, u32 func_id, u32 func_flags) · line 8673
Quoted 2 lines
kernel/bpf/btf.c +0
-0return -EINVAL; func = btf_type_by_id(btf, func->type);
many thanks.
Song