|
1 | 1 | // SPDX-License-Identifier: GPL-2.0 |
2 | 2 | /* Copyright (c) 2020 Carlos Neira cneirabustos@gmail.com */ |
| 3 | + |
| 4 | +#define _GNU_SOURCE |
3 | 5 | #include <test_progs.h> |
| 6 | +#include "test_ns_current_pid_tgid.skel.h" |
4 | 7 | #include <sys/stat.h> |
5 | 8 | #include <sys/types.h> |
6 | 9 | #include <unistd.h> |
7 | 10 | #include <sys/syscall.h> |
| 11 | +#include <sched.h> |
| 12 | +#include <sys/wait.h> |
| 13 | +#include <sys/mount.h> |
| 14 | +#include <sys/fcntl.h> |
8 | 15 |
|
9 | | -struct bss { |
10 | | - __u64 dev; |
11 | | - __u64 ino; |
12 | | - __u64 pid_tgid; |
13 | | - __u64 user_pid_tgid; |
14 | | -}; |
| 16 | +#define STACK_SIZE (1024 * 1024) |
| 17 | +static char child_stack[STACK_SIZE]; |
15 | 18 |
|
16 | | -void test_ns_current_pid_tgid(void) |
| 19 | +static int test_current_pid_tgid(void *args) |
17 | 20 | { |
18 | | - const char *probe_name = "raw_tracepoint/sys_enter"; |
19 | | - const char *file = "test_ns_current_pid_tgid.o"; |
20 | | - int err, key = 0, duration = 0; |
21 | | - struct bpf_link *link = NULL; |
22 | | - struct bpf_program *prog; |
23 | | - struct bpf_map *bss_map; |
24 | | - struct bpf_object *obj; |
25 | | - struct bss bss; |
| 21 | + struct test_ns_current_pid_tgid__bss *bss; |
| 22 | + struct test_ns_current_pid_tgid *skel; |
| 23 | + int err = -1, duration = 0; |
| 24 | + pid_t tgid, pid; |
26 | 25 | struct stat st; |
27 | | - __u64 id; |
28 | | - |
29 | | - obj = bpf_object__open_file(file, NULL); |
30 | | - if (CHECK(IS_ERR(obj), "obj_open", "err %ld\n", PTR_ERR(obj))) |
31 | | - return; |
32 | 26 |
|
33 | | - err = bpf_object__load(obj); |
34 | | - if (CHECK(err, "obj_load", "err %d errno %d\n", err, errno)) |
| 27 | + skel = test_ns_current_pid_tgid__open_and_load(); |
| 28 | + if (CHECK(!skel, "skel_open_load", "failed to load skeleton\n")) |
35 | 29 | goto cleanup; |
36 | 30 |
|
37 | | - bss_map = bpf_object__find_map_by_name(obj, "test_ns_.bss"); |
38 | | - if (CHECK(!bss_map, "find_bss_map", "failed\n")) |
| 31 | + pid = syscall(SYS_gettid); |
| 32 | + tgid = getpid(); |
| 33 | + |
| 34 | + err = stat("/proc/self/ns/pid", &st); |
| 35 | + if (CHECK(err, "stat", "failed /proc/self/ns/pid: %d\n", err)) |
39 | 36 | goto cleanup; |
40 | 37 |
|
41 | | - prog = bpf_object__find_program_by_title(obj, probe_name); |
42 | | - if (CHECK(!prog, "find_prog", "prog '%s' not found\n", |
43 | | - probe_name)) |
| 38 | + bss = skel->bss; |
| 39 | + bss->dev = st.st_dev; |
| 40 | + bss->ino = st.st_ino; |
| 41 | + bss->user_pid = 0; |
| 42 | + bss->user_tgid = 0; |
| 43 | + |
| 44 | + err = test_ns_current_pid_tgid__attach(skel); |
| 45 | + if (CHECK(err, "skel_attach", "skeleton attach failed: %d\n", err)) |
44 | 46 | goto cleanup; |
45 | 47 |
|
46 | | - memset(&bss, 0, sizeof(bss)); |
47 | | - pid_t tid = syscall(SYS_gettid); |
48 | | - pid_t pid = getpid(); |
| 48 | + /* trigger tracepoint */ |
| 49 | + usleep(1); |
| 50 | + ASSERT_EQ(bss->user_pid, pid, "pid"); |
| 51 | + ASSERT_EQ(bss->user_tgid, tgid, "tgid"); |
| 52 | + err = 0; |
49 | 53 |
|
50 | | - id = (__u64) tid << 32 | pid; |
51 | | - bss.user_pid_tgid = id; |
| 54 | +cleanup: |
| 55 | + test_ns_current_pid_tgid__destroy(skel); |
52 | 56 |
|
53 | | - if (CHECK_FAIL(stat("/proc/self/ns/pid", &st))) { |
54 | | - perror("Failed to stat /proc/self/ns/pid"); |
55 | | - goto cleanup; |
56 | | - } |
| 57 | + return err; |
| 58 | +} |
57 | 59 |
|
58 | | - bss.dev = st.st_dev; |
59 | | - bss.ino = st.st_ino; |
| 60 | +static void test_ns_current_pid_tgid_new_ns(void) |
| 61 | +{ |
| 62 | + int wstatus, duration = 0; |
| 63 | + pid_t cpid; |
60 | 64 |
|
61 | | - err = bpf_map_update_elem(bpf_map__fd(bss_map), &key, &bss, 0); |
62 | | - if (CHECK(err, "setting_bss", "failed to set bss : %d\n", err)) |
63 | | - goto cleanup; |
| 65 | + /* Create a process in a new namespace, this process |
| 66 | + * will be the init process of this new namespace hence will be pid 1. |
| 67 | + */ |
| 68 | + cpid = clone(test_current_pid_tgid, child_stack + STACK_SIZE, |
| 69 | + CLONE_NEWPID | SIGCHLD, NULL); |
64 | 70 |
|
65 | | - link = bpf_program__attach_raw_tracepoint(prog, "sys_enter"); |
66 | | - if (CHECK(IS_ERR(link), "attach_raw_tp", "err %ld\n", |
67 | | - PTR_ERR(link))) { |
68 | | - link = NULL; |
69 | | - goto cleanup; |
70 | | - } |
| 71 | + if (CHECK(cpid == -1, "clone", strerror(errno))) |
| 72 | + return; |
71 | 73 |
|
72 | | - /* trigger some syscalls */ |
73 | | - usleep(1); |
| 74 | + if (CHECK(waitpid(cpid, &wstatus, 0) == -1, "waitpid", strerror(errno))) |
| 75 | + return; |
74 | 76 |
|
75 | | - err = bpf_map_lookup_elem(bpf_map__fd(bss_map), &key, &bss); |
76 | | - if (CHECK(err, "set_bss", "failed to get bss : %d\n", err)) |
77 | | - goto cleanup; |
| 77 | + if (CHECK(WEXITSTATUS(wstatus) != 0, "newns_pidtgid", "failed")) |
| 78 | + return; |
| 79 | +} |
78 | 80 |
|
79 | | - if (CHECK(id != bss.pid_tgid, "Compare user pid/tgid vs. bpf pid/tgid", |
80 | | - "User pid/tgid %llu BPF pid/tgid %llu\n", id, bss.pid_tgid)) |
81 | | - goto cleanup; |
82 | | -cleanup: |
83 | | - bpf_link__destroy(link); |
84 | | - bpf_object__close(obj); |
| 81 | +void test_ns_current_pid_tgid(void) |
| 82 | +{ |
| 83 | + if (test__start_subtest("ns_current_pid_tgid_root_ns")) |
| 84 | + test_current_pid_tgid(NULL); |
| 85 | + if (test__start_subtest("ns_current_pid_tgid_new_ns")) |
| 86 | + test_ns_current_pid_tgid_new_ns(); |
85 | 87 | } |
0 commit comments