Skip to content

Commit 8c79b35

Browse files
Jakub Kicinskiborkmann
authored andcommitted
tools: bpftool: fix crash with un-owned prog arrays
Prog arrays don't have 'owner_prog_type' and 'owner_jited' fields in their fdinfo when they are created. Those fields are set and reported when first program is checked for compatibility by bpf_prog_array_compatible(). This means that bpftool cannot expect the fields to always be there. Currently trying to show maps on a system with an un-owned prog array leads to a crash: $ bpftool map show 389: prog_array name tail_call_map flags 0x0 Error: key 'owner_prog_type' not found in fdinfo Error: key 'owner_jited' not found in fdinfo key 4B value 4B max_entries 4 memlock 4096B Segmentation fault (core dumped) We pass a NULL pointer to atoi(). Remove the assumption that fdinfo keys are always present. Add missing validations and remove the p_err() calls which may lead to broken JSON output as caller will not propagate the failure. Fixes: 99a44be ("tools: bpftool: add owner_prog_type and owner_jited to bpftool output") Signed-off-by: Jakub Kicinski <[email protected]> Reviewed-by: Quentin Monnet <[email protected]> Acked-by: Song Liu <[email protected]> Signed-off-by: Daniel Borkmann <[email protected]>
1 parent c9e4576 commit 8c79b35

File tree

2 files changed

+9
-14
lines changed

2 files changed

+9
-14
lines changed

tools/bpf/bpftool/common.c

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -297,10 +297,8 @@ char *get_fdinfo(int fd, const char *key)
297297
snprintf(path, sizeof(path), "/proc/self/fdinfo/%d", fd);
298298

299299
fdi = fopen(path, "r");
300-
if (!fdi) {
301-
p_err("can't open fdinfo: %s", strerror(errno));
300+
if (!fdi)
302301
return NULL;
303-
}
304302

305303
while ((n = getline(&line, &line_n, fdi)) > 0) {
306304
char *value;
@@ -313,7 +311,6 @@ char *get_fdinfo(int fd, const char *key)
313311

314312
value = strchr(line, '\t');
315313
if (!value || !value[1]) {
316-
p_err("malformed fdinfo!?");
317314
free(line);
318315
return NULL;
319316
}
@@ -326,7 +323,6 @@ char *get_fdinfo(int fd, const char *key)
326323
return line;
327324
}
328325

329-
p_err("key '%s' not found in fdinfo", key);
330326
free(line);
331327
fclose(fdi);
332328
return NULL;

tools/bpf/bpftool/map.c

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -513,10 +513,9 @@ static int show_map_close_json(int fd, struct bpf_map_info *info)
513513
jsonw_uint_field(json_wtr, "owner_prog_type",
514514
prog_type);
515515
}
516-
if (atoi(owner_jited))
517-
jsonw_bool_field(json_wtr, "owner_jited", true);
518-
else
519-
jsonw_bool_field(json_wtr, "owner_jited", false);
516+
if (owner_jited)
517+
jsonw_bool_field(json_wtr, "owner_jited",
518+
!!atoi(owner_jited));
520519

521520
free(owner_prog_type);
522521
free(owner_jited);
@@ -569,7 +568,8 @@ static int show_map_close_plain(int fd, struct bpf_map_info *info)
569568
char *owner_prog_type = get_fdinfo(fd, "owner_prog_type");
570569
char *owner_jited = get_fdinfo(fd, "owner_jited");
571570

572-
printf("\n\t");
571+
if (owner_prog_type || owner_jited)
572+
printf("\n\t");
573573
if (owner_prog_type) {
574574
unsigned int prog_type = atoi(owner_prog_type);
575575

@@ -579,10 +579,9 @@ static int show_map_close_plain(int fd, struct bpf_map_info *info)
579579
else
580580
printf("owner_prog_type %d ", prog_type);
581581
}
582-
if (atoi(owner_jited))
583-
printf("owner jited");
584-
else
585-
printf("owner not jited");
582+
if (owner_jited)
583+
printf("owner%s jited",
584+
atoi(owner_jited) ? "" : " not");
586585

587586
free(owner_prog_type);
588587
free(owner_jited);

0 commit comments

Comments
 (0)