Commit 6147a140 authored by Andrii Nakryiko's avatar Andrii Nakryiko Committed by Alexei Starovoitov

selftests/bpf: Ensure core_reloc_kernel is reading test_progs's data only

test_core_reloc_kernel.c selftest is the only CO-RE test that reads and
returns for validation calling thread's information (pid, tgid, comm). Thus it
has to make sure that only test_prog's invocations are honored.

Fixes: df36e621 ("selftests/bpf: add CO-RE relocs testing setup")
Reported-by: default avatarAlexei Starovoitov <ast@kernel.org>
Signed-off-by: default avatarAndrii Nakryiko <andriin@fb.com>
Signed-off-by: default avatarAlexei Starovoitov <ast@kernel.org>
Acked-by: default avatarJohn Fastabend <john.fastabend@gmail.com>
Link: https://lore.kernel.org/bpf/20191121175900.3486133-1-andriin@fb.com
parent 1aace10f
...@@ -2,6 +2,7 @@ ...@@ -2,6 +2,7 @@
#include <test_progs.h> #include <test_progs.h>
#include "progs/core_reloc_types.h" #include "progs/core_reloc_types.h"
#include <sys/mman.h> #include <sys/mman.h>
#include <sys/syscall.h>
#define STRUCT_TO_CHAR_PTR(struct_name) (const char *)&(struct struct_name) #define STRUCT_TO_CHAR_PTR(struct_name) (const char *)&(struct struct_name)
...@@ -452,6 +453,7 @@ static struct core_reloc_test_case test_cases[] = { ...@@ -452,6 +453,7 @@ static struct core_reloc_test_case test_cases[] = {
struct data { struct data {
char in[256]; char in[256];
char out[256]; char out[256];
uint64_t my_pid_tgid;
}; };
static size_t roundup_page(size_t sz) static size_t roundup_page(size_t sz)
...@@ -471,9 +473,12 @@ void test_core_reloc(void) ...@@ -471,9 +473,12 @@ void test_core_reloc(void)
struct bpf_map *data_map; struct bpf_map *data_map;
struct bpf_program *prog; struct bpf_program *prog;
struct bpf_object *obj; struct bpf_object *obj;
uint64_t my_pid_tgid;
struct data *data; struct data *data;
void *mmap_data = NULL; void *mmap_data = NULL;
my_pid_tgid = getpid() | ((uint64_t)syscall(SYS_gettid) << 32);
for (i = 0; i < ARRAY_SIZE(test_cases); i++) { for (i = 0; i < ARRAY_SIZE(test_cases); i++) {
test_case = &test_cases[i]; test_case = &test_cases[i];
if (!test__start_subtest(test_case->case_name)) if (!test__start_subtest(test_case->case_name))
...@@ -517,11 +522,6 @@ void test_core_reloc(void) ...@@ -517,11 +522,6 @@ void test_core_reloc(void)
goto cleanup; goto cleanup;
} }
link = bpf_program__attach_raw_tracepoint(prog, tp_name);
if (CHECK(IS_ERR(link), "attach_raw_tp", "err %ld\n",
PTR_ERR(link)))
goto cleanup;
data_map = bpf_object__find_map_by_name(obj, "test_cor.bss"); data_map = bpf_object__find_map_by_name(obj, "test_cor.bss");
if (CHECK(!data_map, "find_data_map", "data map not found\n")) if (CHECK(!data_map, "find_data_map", "data map not found\n"))
goto cleanup; goto cleanup;
...@@ -537,6 +537,12 @@ void test_core_reloc(void) ...@@ -537,6 +537,12 @@ void test_core_reloc(void)
memset(mmap_data, 0, sizeof(*data)); memset(mmap_data, 0, sizeof(*data));
memcpy(data->in, test_case->input, test_case->input_len); memcpy(data->in, test_case->input, test_case->input_len);
data->my_pid_tgid = my_pid_tgid;
link = bpf_program__attach_raw_tracepoint(prog, tp_name);
if (CHECK(IS_ERR(link), "attach_raw_tp", "err %ld\n",
PTR_ERR(link)))
goto cleanup;
/* trigger test run */ /* trigger test run */
usleep(1); usleep(1);
......
...@@ -11,6 +11,7 @@ char _license[] SEC("license") = "GPL"; ...@@ -11,6 +11,7 @@ char _license[] SEC("license") = "GPL";
struct { struct {
char in[256]; char in[256];
char out[256]; char out[256];
uint64_t my_pid_tgid;
} data = {}; } data = {};
struct core_reloc_kernel_output { struct core_reloc_kernel_output {
...@@ -38,6 +39,9 @@ int test_core_kernel(void *ctx) ...@@ -38,6 +39,9 @@ int test_core_kernel(void *ctx)
uint32_t real_tgid = (uint32_t)pid_tgid; uint32_t real_tgid = (uint32_t)pid_tgid;
int pid, tgid; int pid, tgid;
if (data.my_pid_tgid != pid_tgid)
return 0;
if (CORE_READ(&pid, &task->pid) || if (CORE_READ(&pid, &task->pid) ||
CORE_READ(&tgid, &task->tgid)) CORE_READ(&tgid, &task->tgid))
return 1; return 1;
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment