Skip to content

Commit

Permalink
samples, selftests/bpf: add NULL check for ksym_search
Browse files Browse the repository at this point in the history
Since, ksym_search added with verification logic for symbols existence,
it could return NULL when the kernel symbols are not loaded.

This commit will add NULL check logic after ksym_search.

Signed-off-by: Daniel T. Lee <danieltimlee@gmail.com>
Signed-off-by: Daniel Borkmann <daniel@iogearbox.net>
  • Loading branch information
Daniel T. Lee authored and Daniel Borkmann committed Apr 4, 2019
1 parent 0979ff7 commit e67b2c7
Show file tree
Hide file tree
Showing 5 changed files with 23 additions and 3 deletions.
5 changes: 5 additions & 0 deletions samples/bpf/offwaketime_user.c
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,11 @@ static void print_ksym(__u64 addr)
if (!addr)
return;
sym = ksym_search(addr);
if (!sym) {
printf("ksym not found. Is kallsyms loaded?\n");
return;
}

if (PRINT_RAW_ADDR)
printf("%s/%llx;", sym->name, addr);
else
Expand Down
5 changes: 5 additions & 0 deletions samples/bpf/sampleip_user.c
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,11 @@ static void print_ip_map(int fd)
for (i = 0; i < max; i++) {
if (counts[i].ip > PAGE_OFFSET) {
sym = ksym_search(counts[i].ip);
if (!sym) {
printf("ksym not found. Is kallsyms loaded?\n");
continue;
}

printf("0x%-17llx %-32s %u\n", counts[i].ip, sym->name,
counts[i].count);
} else {
Expand Down
7 changes: 6 additions & 1 deletion samples/bpf/spintest_user.c
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,13 @@ int main(int ac, char **argv)
bpf_map_lookup_elem(map_fd[0], &next_key, &value);
assert(next_key == value);
sym = ksym_search(value);
printf(" %s", sym->name);
key = next_key;
if (!sym) {
printf("ksym not found. Is kallsyms loaded?\n");
continue;
}

printf(" %s", sym->name);
}
if (key)
printf("\n");
Expand Down
5 changes: 5 additions & 0 deletions samples/bpf/trace_event_user.c
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,11 @@ static void print_ksym(__u64 addr)
if (!addr)
return;
sym = ksym_search(addr);
if (!sym) {
printf("ksym not found. Is kallsyms loaded?\n");
return;
}

printf("%s;", sym->name);
if (!strcmp(sym->name, "sys_read"))
sys_read_seen = true;
Expand Down
4 changes: 2 additions & 2 deletions tools/testing/selftests/bpf/prog_tests/get_stack_raw_tp.c
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ static int get_stack_print_output(void *data, int size)
} else {
for (i = 0; i < num_stack; i++) {
ks = ksym_search(raw_data[i]);
if (strcmp(ks->name, nonjit_func) == 0) {
if (ks && (strcmp(ks->name, nonjit_func) == 0)) {
found = true;
break;
}
Expand All @@ -56,7 +56,7 @@ static int get_stack_print_output(void *data, int size)
} else {
for (i = 0; i < num_stack; i++) {
ks = ksym_search(e->kern_stack[i]);
if (strcmp(ks->name, nonjit_func) == 0) {
if (ks && (strcmp(ks->name, nonjit_func) == 0)) {
good_kern_stack = true;
break;
}
Expand Down

0 comments on commit e67b2c7

Please sign in to comment.