samples, selftests/bpf: add NULL check for ksym_search
authorDaniel T. Lee <danieltimlee@gmail.com>
Wed, 3 Apr 2019 22:17:56 +0000 (07:17 +0900)
committerDaniel Borkmann <daniel@iogearbox.net>
Thu, 4 Apr 2019 14:43:47 +0000 (16:43 +0200)
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>
samples/bpf/offwaketime_user.c
samples/bpf/sampleip_user.c
samples/bpf/spintest_user.c
samples/bpf/trace_event_user.c
tools/testing/selftests/bpf/prog_tests/get_stack_raw_tp.c

index f06063af9fcb9a9fe4ca2eb90c68669b2ad6ca11..bb315ce1b8660cf86fd70a2b3d917d0742064706 100644 (file)
@@ -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
index 216c7ecbbbe9ef201ff3e7225e0cbf71a4f44718..23b90a45c80251ca3765f75873d1a85d423cf1ab 100644 (file)
@@ -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 {
index 8d3e9cfa190978e56d00bde53a22d3f72f9e9b7e..2556af2d9b3e8e2725bfc2c062f99d588b7f727a 100644 (file)
@@ -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");
index d08046ab81f043505e0ea42a2e8c85661ae68f76..d4178f60e075f099efe340c4d22b05ce4df013ef 100644 (file)
@@ -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;
index d7bb5beb1c57c112092b5e76e0755185f9303f8d..c2a0a9d5591b4b5ea3436bfe490689c7756e50db 100644 (file)
@@ -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;
                                }
@@ -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;
                                }