Skip to contentSkip to conversation

selftests/bpf: ksym_search won't check symbols exists

PATCH Superseded Daniel T. Lee 2 participants 3 messages Mar 15, 2019 – Mar 25, 2019

Subsystems (5)

v1 of 3 Superseded Latest is v3 →

lore
Share & tools
lore
#0 lore raw

PATCH 1/2 selftests/bpf: ksym_search won't check symbols exists

Currently, ksym_search located at trace_helpers won't check symbols are existing or not.

In ksym_search, when symbol is not found, it will return &syms[0](_stext). But when the kernel symbols are not loaded, it will return NULL, which is not a desired action.

This commit will add verification logic whether symbols are loaded prior to the symbol search.

---
 tools/testing/selftests/bpf/trace_helpers.c | 4 ++++
 1 file changed, 4 insertions(+)
tools/testing/selftests/bpf/trace_helpers.c +4 -0
index 4cdb63bf0521..9a9fc6c9b70b 100644
@@ -52,6 +52,10 @@ struct ksym *ksym_search(long key)
 	int start = 0, end = sym_cnt;
 	int result;
 
+	/* kallsyms not loaded. return NULL */
+	if (sym_cnt <= 0)
+		return NULL;
+
 	while (start < end) {
 		size_t mid = start + (end - start) / 2;
 
Signature · 1 line
2.17.1
#1 lore raw

PATCH 2/2 samples/bpf: add NULL check for ksym_search

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.

---
 samples/bpf/offwaketime_user.c | 5 +++++
 samples/bpf/sampleip_user.c    | 5 +++++
 samples/bpf/trace_event_user.c | 5 +++++
 3 files changed, 15 insertions(+)
samples/bpf/offwaketime_user.c +5 -0
index f06063af9fcb..52e29467a509 100644
@@ -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
samples/bpf/sampleip_user.c +5 -0
index 216c7ecbbbe9..23b90a45c802 100644
@@ -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 {
samples/bpf/trace_event_user.c +5 -0
index d08046ab81f0..bc8e6afdae6d 100644
@@ -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;
Signature · 1 line
2.17.1
maintainer
#2 lore raw

Re: [PATCH 2/2] samples/bpf: add NULL check for ksym_search

Quoted 11 lines · Daniel T. Lee

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.

---
 samples/bpf/offwaketime_user.c | 5 +++++
 samples/bpf/sampleip_user.c    | 5 +++++
 samples/bpf/trace_event_user.c | 5 +++++
 3 files changed, 15 insertions(+)

This would also need fixing to check for NULL in:

tools/testing/selftests/bpf/prog_tests/get_stack_raw_tp.c

Quoted 49 lines
samples/bpf/offwaketime_user.c +5 -0
index f06063af9fcb..52e29467a509 100644
@@ -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
samples/bpf/sampleip_user.c +5 -0
index 216c7ecbbbe9..23b90a45c802 100644
@@ -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 {
samples/bpf/trace_event_user.c +5 -0
index d08046ab81f0..bc8e6afdae6d 100644
@@ -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;