The Linux Kernel Mailing List
 help / color / mirror / Atom feed
* [PATCH] libbpf: check the btf_type kind to prevent error
@ 2024-08-06 10:51 Ma Ke
  2024-08-06 11:24 ` Jiri Olsa
  2024-08-06 17:38 ` Yonghong Song
  0 siblings, 2 replies; 4+ messages in thread
From: Ma Ke @ 2024-08-06 10:51 UTC (permalink / raw)
  To: andrii, eddyz87, ast, daniel, martin.lau, song, yonghong.song,
	john.fastabend, kpsingh, sdf, haoluo, jolsa, delyank
  Cc: bpf, linux-kernel, Ma Ke, stable

To prevent potential error return values, it is necessary to check the
return value of btf__type_by_id. We can add a kind checking to fix the
issue.

Cc: stable@vger.kernel.org
Fixes: 430025e5dca5 ("libbpf: Add subskeleton scaffolding")
Signed-off-by: Ma Ke <make24@iscas.ac.cn>
---
 tools/lib/bpf/libbpf.c | 3 +++
 1 file changed, 3 insertions(+)

diff --git a/tools/lib/bpf/libbpf.c b/tools/lib/bpf/libbpf.c
index a3be6f8fac09..d1eb45d16054 100644
--- a/tools/lib/bpf/libbpf.c
+++ b/tools/lib/bpf/libbpf.c
@@ -13850,6 +13850,9 @@ int bpf_object__open_subskeleton(struct bpf_object_subskeleton *s)
 		var = btf_var_secinfos(map_type);
 		for (i = 0; i < len; i++, var++) {
 			var_type = btf__type_by_id(btf, var->type);
+			if (!var_type)
+				return libbpf_err(-ENOENT);
+
 			var_name = btf__name_by_offset(btf, var_type->name_off);
 			if (strcmp(var_name, var_skel->name) == 0) {
 				*var_skel->addr = map->mmaped + var->offset;
-- 
2.25.1


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

* Re: [PATCH] libbpf: check the btf_type kind to prevent error
  2024-08-06 10:51 [PATCH] libbpf: check the btf_type kind to prevent error Ma Ke
@ 2024-08-06 11:24 ` Jiri Olsa
  2024-08-06 17:38 ` Yonghong Song
  1 sibling, 0 replies; 4+ messages in thread
From: Jiri Olsa @ 2024-08-06 11:24 UTC (permalink / raw)
  To: Ma Ke
  Cc: andrii, eddyz87, ast, daniel, martin.lau, song, yonghong.song,
	john.fastabend, kpsingh, sdf, haoluo, delyank, bpf, linux-kernel,
	stable

On Tue, Aug 06, 2024 at 06:51:42PM +0800, Ma Ke wrote:
> To prevent potential error return values, it is necessary to check the
> return value of btf__type_by_id. We can add a kind checking to fix the
> issue.
> 
> Cc: stable@vger.kernel.org
> Fixes: 430025e5dca5 ("libbpf: Add subskeleton scaffolding")
> Signed-off-by: Ma Ke <make24@iscas.ac.cn>
> ---
>  tools/lib/bpf/libbpf.c | 3 +++
>  1 file changed, 3 insertions(+)
> 
> diff --git a/tools/lib/bpf/libbpf.c b/tools/lib/bpf/libbpf.c
> index a3be6f8fac09..d1eb45d16054 100644
> --- a/tools/lib/bpf/libbpf.c
> +++ b/tools/lib/bpf/libbpf.c
> @@ -13850,6 +13850,9 @@ int bpf_object__open_subskeleton(struct bpf_object_subskeleton *s)
>  		var = btf_var_secinfos(map_type);
>  		for (i = 0; i < len; i++, var++) {
>  			var_type = btf__type_by_id(btf, var->type);
> +			if (!var_type)
> +				return libbpf_err(-ENOENT);

hum btf__type_by_id sets errno to EINVAL in case of error

I think we should keep that or just pass errno like we do earlier in the function

  libbpf_err(-errno)

jirka

> +
>  			var_name = btf__name_by_offset(btf, var_type->name_off);
>  			if (strcmp(var_name, var_skel->name) == 0) {
>  				*var_skel->addr = map->mmaped + var->offset;
> -- 
> 2.25.1
> 

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

* Re: [PATCH] libbpf: check the btf_type kind to prevent error
  2024-08-06 10:51 [PATCH] libbpf: check the btf_type kind to prevent error Ma Ke
  2024-08-06 11:24 ` Jiri Olsa
@ 2024-08-06 17:38 ` Yonghong Song
  2024-08-06 19:46   ` Eduard Zingerman
  1 sibling, 1 reply; 4+ messages in thread
From: Yonghong Song @ 2024-08-06 17:38 UTC (permalink / raw)
  To: Ma Ke, andrii, eddyz87, ast, daniel, martin.lau, song,
	john.fastabend, kpsingh, sdf, haoluo, jolsa, delyank
  Cc: bpf, linux-kernel, stable


On 8/6/24 3:51 AM, Ma Ke wrote:
> To prevent potential error return values, it is necessary to check the
> return value of btf__type_by_id. We can add a kind checking to fix the
> issue.
>
> Cc: stable@vger.kernel.org
> Fixes: 430025e5dca5 ("libbpf: Add subskeleton scaffolding")
> Signed-off-by: Ma Ke <make24@iscas.ac.cn>
> ---
>   tools/lib/bpf/libbpf.c | 3 +++
>   1 file changed, 3 insertions(+)
>
> diff --git a/tools/lib/bpf/libbpf.c b/tools/lib/bpf/libbpf.c
> index a3be6f8fac09..d1eb45d16054 100644
> --- a/tools/lib/bpf/libbpf.c
> +++ b/tools/lib/bpf/libbpf.c
> @@ -13850,6 +13850,9 @@ int bpf_object__open_subskeleton(struct bpf_object_subskeleton *s)
>   		var = btf_var_secinfos(map_type);
>   		for (i = 0; i < len; i++, var++) {
>   			var_type = btf__type_by_id(btf, var->type);
> +			if (!var_type)
> +				return libbpf_err(-ENOENT);

Could you give a detailed example when this error could be triggered?

> +
>   			var_name = btf__name_by_offset(btf, var_type->name_off);
>   			if (strcmp(var_name, var_skel->name) == 0) {
>   				*var_skel->addr = map->mmaped + var->offset;

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

* Re: [PATCH] libbpf: check the btf_type kind to prevent error
  2024-08-06 17:38 ` Yonghong Song
@ 2024-08-06 19:46   ` Eduard Zingerman
  0 siblings, 0 replies; 4+ messages in thread
From: Eduard Zingerman @ 2024-08-06 19:46 UTC (permalink / raw)
  To: Ma Ke; +Cc: bpf, linux-kernel, stable, Yonghong Song, jolsa

On Tue, 2024-08-06 at 10:38 -0700, Yonghong Song wrote:
> On 8/6/24 3:51 AM, Ma Ke wrote:
> > To prevent potential error return values, it is necessary to check the
> > return value of btf__type_by_id. We can add a kind checking to fix the
> > issue.
> > 
> > Cc: stable@vger.kernel.org
> > Fixes: 430025e5dca5 ("libbpf: Add subskeleton scaffolding")
> > Signed-off-by: Ma Ke <make24@iscas.ac.cn>
> > ---
> >   tools/lib/bpf/libbpf.c | 3 +++
> >   1 file changed, 3 insertions(+)
> > 
> > diff --git a/tools/lib/bpf/libbpf.c b/tools/lib/bpf/libbpf.c
> > index a3be6f8fac09..d1eb45d16054 100644
> > --- a/tools/lib/bpf/libbpf.c
> > +++ b/tools/lib/bpf/libbpf.c
> > @@ -13850,6 +13850,9 @@ int bpf_object__open_subskeleton(struct bpf_object_subskeleton *s)
> >   		var = btf_var_secinfos(map_type);
> >   		for (i = 0; i < len; i++, var++) {
> >   			var_type = btf__type_by_id(btf, var->type);
> > +			if (!var_type)
> > +				return libbpf_err(-ENOENT);
> 
> Could you give a detailed example when this error could be triggered?

I'm curious as well, tools/lib/bpf/btf.c:btf_validate_type() has the following code:

static int btf_validate_type(const struct btf *btf, const struct btf_type *t, __u32 id)
{
	...
	switch (kind) {
	...
	case ...
	case BTF_KIND_VAR:
	case ...
		err = btf_validate_id(btf, t->type, id);
		if (err)
			return err;
		break;

That should catch situations exactly like this one.

> 
> > +
> >   			var_name = btf__name_by_offset(btf, var_type->name_off);
> >   			if (strcmp(var_name, var_skel->name) == 0) {
> >   				*var_skel->addr = map->mmaped + var->offset;


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

end of thread, other threads:[~2024-08-06 19:46 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2024-08-06 10:51 [PATCH] libbpf: check the btf_type kind to prevent error Ma Ke
2024-08-06 11:24 ` Jiri Olsa
2024-08-06 17:38 ` Yonghong Song
2024-08-06 19:46   ` Eduard Zingerman

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