[PATCH bpf-next v4 1/3] bpf, btf: Fix error checks for btf_get_module_btf

Geliang Tang posted 3 patches 7 months ago
There is a newer version of this series
[PATCH bpf-next v4 1/3] bpf, btf: Fix error checks for btf_get_module_btf
Posted by Geliang Tang 7 months ago
From: Geliang Tang <tanggeliang@kylinos.cn>

To let the modules loaded, commit 3de4d22cc9ac ("bpf, btf: Warn but
return no error for NULL btf from __register_btf_kfunc_id_set()")
changes the return value of __register_btf_kfunc_id_set() from -ENOENT
to 0 when btf is NULL.

A better way to do this is to enable CONFIG_MODULE_ALLOW_BTF_MISMATCH.

An error code -ENOENT should indeed be returned when kernel module BTF
mismatch detected except CONFIG_MODULE_ALLOW_BTF_MISMATCH is enabled in
__register_btf_kfunc_id_set().

The same in register_btf_id_dtor_kfuncs(), give the modules a chance
to be loaded when CONFIG_MODULE_ALLOW_BTF_MISMATCH is enabled.

Fixes: 3de4d22cc9ac ("bpf, btf: Warn but return no error for NULL btf from __register_btf_kfunc_id_set()")
Signed-off-by: Geliang Tang <tanggeliang@kylinos.cn>
---
 kernel/bpf/btf.c | 8 ++++++--
 1 file changed, 6 insertions(+), 2 deletions(-)

diff --git a/kernel/bpf/btf.c b/kernel/bpf/btf.c
index f7725cb6e564..203391e61d93 100644
--- a/kernel/bpf/btf.c
+++ b/kernel/bpf/btf.c
@@ -8103,8 +8103,11 @@ static int __register_btf_kfunc_id_set(enum btf_kfunc_hook hook,
 			pr_err("missing vmlinux BTF, cannot register kfuncs\n");
 			return -ENOENT;
 		}
-		if (kset->owner && IS_ENABLED(CONFIG_DEBUG_INFO_BTF_MODULES))
+		if (kset->owner && IS_ENABLED(CONFIG_DEBUG_INFO_BTF_MODULES) &&
+		    !IS_ENABLED(CONFIG_MODULE_ALLOW_BTF_MISMATCH)) {
 			pr_warn("missing module BTF, cannot register kfuncs\n");
+			return -ENOENT;
+		}
 		return 0;
 	}
 	if (IS_ERR(btf))
@@ -8219,7 +8222,8 @@ int register_btf_id_dtor_kfuncs(const struct btf_id_dtor_kfunc *dtors, u32 add_c
 			pr_err("missing vmlinux BTF, cannot register dtor kfuncs\n");
 			return -ENOENT;
 		}
-		if (owner && IS_ENABLED(CONFIG_DEBUG_INFO_BTF_MODULES)) {
+		if (owner && IS_ENABLED(CONFIG_DEBUG_INFO_BTF_MODULES) &&
+		    !IS_ENABLED(CONFIG_MODULE_ALLOW_BTF_MISMATCH)) {
 			pr_err("missing module BTF, cannot register dtor kfuncs\n");
 			return -ENOENT;
 		}
-- 
2.40.1
Re: [PATCH bpf-next v4 1/3] bpf, btf: Fix error checks for btf_get_module_btf
Posted by Martin KaFai Lau 7 months ago
On 2/7/24 6:07 AM, Geliang Tang wrote:
> From: Geliang Tang <tanggeliang@kylinos.cn>
> 
> To let the modules loaded, commit 3de4d22cc9ac ("bpf, btf: Warn but
> return no error for NULL btf from __register_btf_kfunc_id_set()")
> changes the return value of __register_btf_kfunc_id_set() from -ENOENT
> to 0 when btf is NULL.
> 
> A better way to do this is to enable CONFIG_MODULE_ALLOW_BTF_MISMATCH.
> 
> An error code -ENOENT should indeed be returned when kernel module BTF
> mismatch detected except CONFIG_MODULE_ALLOW_BTF_MISMATCH is enabled in
> __register_btf_kfunc_id_set().
> 
> The same in register_btf_id_dtor_kfuncs(), give the modules a chance
> to be loaded when CONFIG_MODULE_ALLOW_BTF_MISMATCH is enabled.
> 
> Fixes: 3de4d22cc9ac ("bpf, btf: Warn but return no error for NULL btf from __register_btf_kfunc_id_set()")
> Signed-off-by: Geliang Tang <tanggeliang@kylinos.cn>
> ---
>   kernel/bpf/btf.c | 8 ++++++--
>   1 file changed, 6 insertions(+), 2 deletions(-)
> 
> diff --git a/kernel/bpf/btf.c b/kernel/bpf/btf.c
> index f7725cb6e564..203391e61d93 100644
> --- a/kernel/bpf/btf.c
> +++ b/kernel/bpf/btf.c
> @@ -8103,8 +8103,11 @@ static int __register_btf_kfunc_id_set(enum btf_kfunc_hook hook,
>   			pr_err("missing vmlinux BTF, cannot register kfuncs\n");
>   			return -ENOENT;
>   		}
> -		if (kset->owner && IS_ENABLED(CONFIG_DEBUG_INFO_BTF_MODULES))
> +		if (kset->owner && IS_ENABLED(CONFIG_DEBUG_INFO_BTF_MODULES) &&
> +		    !IS_ENABLED(CONFIG_MODULE_ALLOW_BTF_MISMATCH)) {

I am not sure this new test can be added here now. Otherwise, the existing 
kconfig that does not have CONFIG_MODULE_ALLOW_BTF_MISMATCH set will fail to 
load a module with btf section stripped out as described in commit 3de4d22cc9ac.

A module with stripped btf section does not mean BTF_MISMATCH also.

>   			pr_warn("missing module BTF, cannot register kfuncs\n");
> +			return -ENOENT;
> +		}
>   		return 0;
>   	}
>   	if (IS_ERR(btf))
> @@ -8219,7 +8222,8 @@ int register_btf_id_dtor_kfuncs(const struct btf_id_dtor_kfunc *dtors, u32 add_c
>   			pr_err("missing vmlinux BTF, cannot register dtor kfuncs\n");
>   			return -ENOENT;
>   		}
> -		if (owner && IS_ENABLED(CONFIG_DEBUG_INFO_BTF_MODULES)) {
> +		if (owner && IS_ENABLED(CONFIG_DEBUG_INFO_BTF_MODULES) &&
> +		    !IS_ENABLED(CONFIG_MODULE_ALLOW_BTF_MISMATCH)) {
>   			pr_err("missing module BTF, cannot register dtor kfuncs\n");
>   			return -ENOENT;
>   		}