Skip to contentSkip to conversation

libbpf: Initialize st_ops->tname with strdup()

PATCH Superseded Soma Nakata 3 participants 5 messages Aug 21, 2024

Subsystems (2)

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 libbpf: Initialize st_ops->tname with strdup()

tname is returned by btf__name_by_offset() as well as var_name, and these addresses point to strings in the btf. Since their locations may change while loading the bpf program, using strdup() ensures tname is safely stored.

---
 tools/lib/bpf/libbpf.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
tools/lib/bpf/libbpf.c +1 -1
index a3be6f8fac09..ece1f1af2cd4 100644
@@ -1423,7 +1423,7 @@ static int init_struct_ops_maps(struct bpf_object *obj, const char *sec_name,
 		memcpy(st_ops->data,
 		       data->d_buf + vsi->offset,
 		       type->size);
-		st_ops->tname = tname;
+		st_ops->tname = strdup(tname);
 		st_ops->type = type;
 		st_ops->type_id = type_id;
 
Signature · 1 line
2.46.0
#1 lore raw

Le 21/08/2024 à 08:46, Soma Nakata a écrit :

Quoted 15 lines

tname is returned by btf__name_by_offset() as well as var_name, and these addresses point to strings in the btf. Since their locations may change while loading the bpf program, using strdup() ensures tname is safely stored.

---
  tools/lib/bpf/libbpf.c | 2 +-
  1 file changed, 1 insertion(+), 1 deletion(-)
tools/lib/bpf/libbpf.c +0 -0
index a3be6f8fac09..ece1f1af2cd4 100644

tools/lib/bpf/libbpf.c static int init_struct_ops_maps(struct bpf_object *obj, const char *sec_name, · line 1426

Quoted 5 lines
tools/lib/bpf/libbpf.c +1 -1
@@ -1423,7 +1423,7 @@ static int init_struct_ops_maps(struct bpf_object *obj, const char *sec_name,
  		memcpy(st_ops->data,
  		       data->d_buf + vsi->offset,
  		       type->size);
-		st_ops->tname = tname;
+		st_ops->tname = strdup(tname);

Hi,

Should a NULL check be added (as done a few lines above for the [cm]alloc()) and bpf_map__destroy() updated with a zfree(&map->st_ops->tname) ?

CJ

Quoted 3 lines
tools/lib/bpf/libbpf.c +0 -0
  		st_ops->type = type;
  		st_ops->type_id = type_id;
 
#2 lore raw

Hi,

You're correct, I should add a NULL check.

zfree(&map->st_ops->tname) causes error because tname has const qualifier.
Also, I found st_ops->type has the same issue. Therefore, I propose removing const from tname and type fields of struct bpf_struct_ops, and duplicating them from btf.

Quoted 35 lines · Soma Nakata
Quoted 15 lines

tname is returned by btf__name_by_offset() as well as var_name, and these addresses point to strings in the btf. Since their locations may change while loading the bpf program, using strdup() ensures tname is safely stored.

---
  tools/lib/bpf/libbpf.c | 2 +-
  1 file changed, 1 insertion(+), 1 deletion(-)
tools/lib/bpf/libbpf.c +0 -0
index a3be6f8fac09..ece1f1af2cd4 100644

tools/lib/bpf/libbpf.c static int init_struct_ops_maps(struct bpf_object *obj, const char *sec_name, · line 1426

Quoted 5 lines
tools/lib/bpf/libbpf.c +1 -1
@@ -1423,7 +1423,7 @@ static int init_struct_ops_maps(struct bpf_object *obj, const char *sec_name,
              memcpy(st_ops->data,
                     data->d_buf + vsi->offset,
                     type->size);
-             st_ops->tname = tname;
+             st_ops->tname = strdup(tname);

Hi,

Should a NULL check be added (as done a few lines above for the [cm]alloc()) and bpf_map__destroy() updated with a zfree(&map->st_ops->tname) ?

CJ

Quoted 3 lines
tools/lib/bpf/libbpf.c +0 -0
              st_ops->type = type;
              st_ops->type_id = type_id;
 
#3 lore raw

type field is supposed to directly point to the btf in functions like btf_members(). So I will make change only to tname field in this patch.

Quoted 46 lines · Soma Nakata

Hi,

You're correct, I should add a NULL check.

zfree(&map->st_ops->tname) causes error because tname has const qualifier.
Also, I found st_ops->type has the same issue. Therefore, I propose removing const from tname and type fields of struct bpf_struct_ops, and duplicating them from btf.

Quoted 35 lines · Soma Nakata
Quoted 15 lines

tname is returned by btf__name_by_offset() as well as var_name, and these addresses point to strings in the btf. Since their locations may change while loading the bpf program, using strdup() ensures tname is safely stored.

---
  tools/lib/bpf/libbpf.c | 2 +-
  1 file changed, 1 insertion(+), 1 deletion(-)
tools/lib/bpf/libbpf.c +0 -0
index a3be6f8fac09..ece1f1af2cd4 100644

tools/lib/bpf/libbpf.c static int init_struct_ops_maps(struct bpf_object *obj, const char *sec_name, · line 1426

Quoted 5 lines
tools/lib/bpf/libbpf.c +1 -1
@@ -1423,7 +1423,7 @@ static int init_struct_ops_maps(struct bpf_object *obj, const char *sec_name,
              memcpy(st_ops->data,
                     data->d_buf + vsi->offset,
                     type->size);
-             st_ops->tname = tname;
+             st_ops->tname = strdup(tname);

Hi,

Should a NULL check be added (as done a few lines above for the [cm]alloc()) and bpf_map__destroy() updated with a zfree(&map->st_ops->tname) ?

CJ

Quoted 3 lines
tools/lib/bpf/libbpf.c +0 -0
              st_ops->type = type;
              st_ops->type_id = type_id;
 
#4 lore raw
In reply to Soma Nakata
Quoted 16 lines · Soma Nakata

tname is returned by btf__name_by_offset() as well as var_name, and these addresses point to strings in the btf. Since their locations may change while loading the bpf program, using strdup() ensures tname is safely stored.

---
 tools/lib/bpf/libbpf.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
tools/lib/bpf/libbpf.c +0 -0
index a3be6f8fac09..ece1f1af2cd4 100644

tools/lib/bpf/libbpf.c static int init_struct_ops_maps(struct bpf_object *obj, const char *sec_name, · line 1429

Quoted 8 lines
tools/lib/bpf/libbpf.c +1 -1
@@ -1423,7 +1423,7 @@ static int init_struct_ops_maps(struct bpf_object *obj, const char *sec_name,
                memcpy(st_ops->data,
                       data->d_buf + vsi->offset,
                       type->size);
-               st_ops->tname = tname;
+               st_ops->tname = strdup(tname);
                st_ops->type = type;
                st_ops->type_id = type_id;
 

Thanks for the fix, but this has been fixed already ([0]). Please make sure that you always reproduce the issue on bpf-next/master and send a fix against that branch.

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

pw-bot: cr

Quoted 3 lines

--
2.46.0