Commit ee2eb063 authored by Andrii Nakryiko's avatar Andrii Nakryiko Committed by Daniel Borkmann

selftests/bpf: Add BPF_CORE_READ and BPF_CORE_READ_STR_INTO macro tests

Validate BPF_CORE_READ correctness and handling of up to 9 levels of
nestedness using cyclic task->(group_leader->)*->tgid chains.

Also add a test of maximum-dpeth BPF_CORE_READ_STR_INTO() macro.
Signed-off-by: default avatarAndrii Nakryiko <andriin@fb.com>
Signed-off-by: default avatarDaniel Borkmann <daniel@iogearbox.net>
Acked-by: default avatarJohn Fastabend <john.fastabend@gmail.com>
Acked-by: default avatarSong Liu <songliubraving@fb.com>
Link: https://lore.kernel.org/bpf/20191008175942.1769476-8-andriin@fb.com
parent 7db3822a
...@@ -193,8 +193,12 @@ static struct core_reloc_test_case test_cases[] = { ...@@ -193,8 +193,12 @@ static struct core_reloc_test_case test_cases[] = {
.btf_src_file = NULL, /* load from /lib/modules/$(uname -r) */ .btf_src_file = NULL, /* load from /lib/modules/$(uname -r) */
.input = "", .input = "",
.input_len = 0, .input_len = 0,
.output = "\1", /* true */ .output = STRUCT_TO_CHAR_PTR(core_reloc_kernel_output) {
.output_len = 1, .valid = { 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, },
.comm = "test_progs\0\0\0\0\0",
.comm_len = 11,
},
.output_len = sizeof(struct core_reloc_kernel_output),
}, },
/* validate BPF program can use multiple flavors to match against /* validate BPF program can use multiple flavors to match against
......
#include <stdint.h> #include <stdint.h>
#include <stdbool.h> #include <stdbool.h>
/*
* KERNEL
*/
struct core_reloc_kernel_output {
int valid[10];
char comm[16];
int comm_len;
};
/* /*
* FLAVORS * FLAVORS
......
...@@ -13,9 +13,17 @@ static volatile struct data { ...@@ -13,9 +13,17 @@ static volatile struct data {
char out[256]; char out[256];
} data; } data;
struct core_reloc_kernel_output {
int valid[10];
char comm[16];
int comm_len;
};
struct task_struct { struct task_struct {
int pid; int pid;
int tgid; int tgid;
char comm[16];
struct task_struct *group_leader;
}; };
#define CORE_READ(dst, src) bpf_core_read(dst, sizeof(*(dst)), src) #define CORE_READ(dst, src) bpf_core_read(dst, sizeof(*(dst)), src)
...@@ -24,7 +32,9 @@ SEC("raw_tracepoint/sys_enter") ...@@ -24,7 +32,9 @@ SEC("raw_tracepoint/sys_enter")
int test_core_kernel(void *ctx) int test_core_kernel(void *ctx)
{ {
struct task_struct *task = (void *)bpf_get_current_task(); struct task_struct *task = (void *)bpf_get_current_task();
struct core_reloc_kernel_output *out = (void *)&data.out;
uint64_t pid_tgid = bpf_get_current_pid_tgid(); uint64_t pid_tgid = bpf_get_current_pid_tgid();
uint32_t real_tgid = (uint32_t)pid_tgid;
int pid, tgid; int pid, tgid;
if (CORE_READ(&pid, &task->pid) || if (CORE_READ(&pid, &task->pid) ||
...@@ -32,7 +42,49 @@ int test_core_kernel(void *ctx) ...@@ -32,7 +42,49 @@ int test_core_kernel(void *ctx)
return 1; return 1;
/* validate pid + tgid matches */ /* validate pid + tgid matches */
data.out[0] = (((uint64_t)pid << 32) | tgid) == pid_tgid; out->valid[0] = (((uint64_t)pid << 32) | tgid) == pid_tgid;
/* test variadic BPF_CORE_READ macros */
out->valid[1] = BPF_CORE_READ(task,
tgid) == real_tgid;
out->valid[2] = BPF_CORE_READ(task,
group_leader,
tgid) == real_tgid;
out->valid[3] = BPF_CORE_READ(task,
group_leader, group_leader,
tgid) == real_tgid;
out->valid[4] = BPF_CORE_READ(task,
group_leader, group_leader, group_leader,
tgid) == real_tgid;
out->valid[5] = BPF_CORE_READ(task,
group_leader, group_leader, group_leader,
group_leader,
tgid) == real_tgid;
out->valid[6] = BPF_CORE_READ(task,
group_leader, group_leader, group_leader,
group_leader, group_leader,
tgid) == real_tgid;
out->valid[7] = BPF_CORE_READ(task,
group_leader, group_leader, group_leader,
group_leader, group_leader, group_leader,
tgid) == real_tgid;
out->valid[8] = BPF_CORE_READ(task,
group_leader, group_leader, group_leader,
group_leader, group_leader, group_leader,
group_leader,
tgid) == real_tgid;
out->valid[9] = BPF_CORE_READ(task,
group_leader, group_leader, group_leader,
group_leader, group_leader, group_leader,
group_leader, group_leader,
tgid) == real_tgid;
/* test BPF_CORE_READ_STR_INTO() returns correct code and contents */
out->comm_len = BPF_CORE_READ_STR_INTO(
&out->comm, task,
group_leader, group_leader, group_leader, group_leader,
group_leader, group_leader, group_leader, group_leader,
comm);
return 0; return 0;
} }
......
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