Skip to content

Commit 78d206f

Browse files
authored
[eBPF] Add check for kernel symbol's address (#4049)
1 parent e1d677d commit 78d206f

File tree

1 file changed

+45
-0
lines changed

1 file changed

+45
-0
lines changed

agent/src/ebpf/user/perf_profiler.c

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -857,6 +857,43 @@ static void print_profiler_status(struct bpf_tracer *t, u64 iter_count,
857857
h->hit_hash_count, msg_h->hit_hash_count);
858858
}
859859

860+
/*
861+
* View kernel addresses exposed via /proc and other interfaces
862+
* when /proc/sys/kernel/kptr_restrict has the value 1, it is
863+
* necessary to set the CAP_SYSLOG capability, otherwise all k-
864+
* ernel addresses are set to 0.
865+
*
866+
* This function is used to check if the kernel address is 0.
867+
*/
868+
static bool check_kallsyms_addr_is_zero(void)
869+
{
870+
const int check_num = 100;
871+
const int max_line_len = 256;
872+
const char *check_str = "0000000000000000";
873+
874+
FILE *file = fopen("/proc/kallsyms", "r");
875+
if (file == NULL) {
876+
ebpf_warning(LOG_CP_TAG "Error opening /proc/kallsyms");
877+
return false;
878+
}
879+
880+
char line[max_line_len];
881+
int count = 0;
882+
883+
while (fgets(line, sizeof(line), file) != NULL && count < check_num) {
884+
char address[17]; // 16 characters + null terminator
885+
sscanf(line, "%16s", address);
886+
887+
if (strcmp(address, check_str) == 0) {
888+
count++;
889+
}
890+
}
891+
892+
fclose(file);
893+
894+
return (count == check_num);
895+
}
896+
860897
/*
861898
* start continuous profiler
862899
* @freq sample frequency, Hertz. (e.g. 99 profile stack traces at 99 Hertz)
@@ -880,6 +917,14 @@ int start_continuous_profiler(int freq,
880917
return (-1);
881918
}
882919

920+
if (check_kallsyms_addr_is_zero()) {
921+
ebpf_warning(LOG_CP_TAG
922+
"All kernel addresses in /proc/kallsyms are 0. Please add"
923+
" 'CAP_SYSLOG' permission to the container to solve the "
924+
"problem.\n");
925+
return (-1);
926+
}
927+
883928
atomic64_init(&process_lost_count);
884929

885930
profiler_stop = 0;

0 commit comments

Comments
 (0)