tools/bpf: fix selftest get_cgroup_id_user
authorYonghong Song <yhs@fb.com>
Wed, 6 Jun 2018 16:12:44 +0000 (09:12 -0700)
committerDaniel Borkmann <daniel@iogearbox.net>
Thu, 7 Jun 2018 22:10:07 +0000 (00:10 +0200)
Commit f269099a7e7a ("tools/bpf: add a selftest for
bpf_get_current_cgroup_id() helper") added a test
for bpf_get_current_cgroup_id() helper. The bpf program
is attached to tracepoint syscalls/sys_enter_nanosleep
and will record the cgroup id if the tracepoint is hit.
The test program creates a cgroup and attachs itself to
this cgroup and expects that the test program process
cgroup id is the same as the cgroup_id retrieved
by the bpf program.

In a light system where no other processes called
nanosleep syscall, the test case can pass.
In a busy system where many different processes can hit
syscalls/sys_enter_nanosleep tracepoint, the cgroup id
recorded by bpf program may not match the test program
process cgroup_id.

This patch fixed an issue by communicating the test program
pid to bpf program. The bpf program only records
cgroup id if the current task pid is the same as
passed-in pid. This ensures that the recorded cgroup_id
is for the cgroup within which the test program resides.

Fixes: f269099a7e7a ("tools/bpf: add a selftest for bpf_get_current_cgroup_id() helper")
Signed-off-by: Yonghong Song <yhs@fb.com>
Signed-off-by: Daniel Borkmann <daniel@iogearbox.net>
tools/testing/selftests/bpf/get_cgroup_id_kern.c
tools/testing/selftests/bpf/get_cgroup_id_user.c

index 2cf8cb23f209505b7f13e650861fc4cdc71988b1..014dba10b8a5dc1b6bb78ef54439c84c127bc13b 100644 (file)
@@ -11,12 +11,24 @@ struct bpf_map_def SEC("maps") cg_ids = {
        .max_entries = 1,
 };
 
+struct bpf_map_def SEC("maps") pidmap = {
+       .type = BPF_MAP_TYPE_ARRAY,
+       .key_size = sizeof(__u32),
+       .value_size = sizeof(__u32),
+       .max_entries = 1,
+};
+
 SEC("tracepoint/syscalls/sys_enter_nanosleep")
 int trace(void *ctx)
 {
-       __u32 key = 0;
+       __u32 pid = bpf_get_current_pid_tgid();
+       __u32 key = 0, *expected_pid;
        __u64 *val;
 
+       expected_pid = bpf_map_lookup_elem(&pidmap, &key);
+       if (!expected_pid || *expected_pid != pid)
+               return 0;
+
        val = bpf_map_lookup_elem(&cg_ids, &key);
        if (val)
                *val = bpf_get_current_cgroup_id();
index ea19a42e589405a0a6b3b1ee73207a48765047a1..e8da7b39158d415a125b28891cb345f8db34a212 100644 (file)
@@ -50,13 +50,13 @@ int main(int argc, char **argv)
        const char *probe_name = "syscalls/sys_enter_nanosleep";
        const char *file = "get_cgroup_id_kern.o";
        int err, bytes, efd, prog_fd, pmu_fd;
+       int cgroup_fd, cgidmap_fd, pidmap_fd;
        struct perf_event_attr attr = {};
-       int cgroup_fd, cgidmap_fd;
        struct bpf_object *obj;
        __u64 kcgid = 0, ucgid;
+       __u32 key = 0, pid;
        int exit_code = 1;
        char buf[256];
-       __u32 key = 0;
 
        err = setup_cgroup_environment();
        if (CHECK(err, "setup_cgroup_environment", "err %d errno %d\n", err,
@@ -81,6 +81,14 @@ int main(int argc, char **argv)
                  cgidmap_fd, errno))
                goto close_prog;
 
+       pidmap_fd = bpf_find_map(__func__, obj, "pidmap");
+       if (CHECK(pidmap_fd < 0, "bpf_find_map", "err %d errno %d\n",
+                 pidmap_fd, errno))
+               goto close_prog;
+
+       pid = getpid();
+       bpf_map_update_elem(pidmap_fd, &key, &pid, 0);
+
        snprintf(buf, sizeof(buf),
                 "/sys/kernel/debug/tracing/events/%s/id", probe_name);
        efd = open(buf, O_RDONLY, 0);