|
| 1 | +// SPDX-License-Identifier: GPL-2.0 |
| 2 | +/* Copyright (c) 2020 Carlos Neira [email protected] */ |
| 3 | +#include <test_progs.h> |
| 4 | +#include <sys/stat.h> |
| 5 | +#include <sys/types.h> |
| 6 | +#include <unistd.h> |
| 7 | +#include <sys/syscall.h> |
| 8 | + |
| 9 | +struct bss { |
| 10 | + __u64 dev; |
| 11 | + __u64 ino; |
| 12 | + __u64 pid_tgid; |
| 13 | + __u64 user_pid_tgid; |
| 14 | +}; |
| 15 | + |
| 16 | +void test_ns_current_pid_tgid(void) |
| 17 | +{ |
| 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; |
| 26 | + 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 | + |
| 33 | + err = bpf_object__load(obj); |
| 34 | + if (CHECK(err, "obj_load", "err %d errno %d\n", err, errno)) |
| 35 | + goto cleanup; |
| 36 | + |
| 37 | + bss_map = bpf_object__find_map_by_name(obj, "test_ns_.bss"); |
| 38 | + if (CHECK(!bss_map, "find_bss_map", "failed\n")) |
| 39 | + goto cleanup; |
| 40 | + |
| 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)) |
| 44 | + goto cleanup; |
| 45 | + |
| 46 | + memset(&bss, 0, sizeof(bss)); |
| 47 | + pid_t tid = syscall(SYS_gettid); |
| 48 | + pid_t pid = getpid(); |
| 49 | + |
| 50 | + id = (__u64) tid << 32 | pid; |
| 51 | + bss.user_pid_tgid = id; |
| 52 | + |
| 53 | + if (CHECK_FAIL(stat("/proc/self/ns/pid", &st))) { |
| 54 | + perror("Failed to stat /proc/self/ns/pid"); |
| 55 | + goto cleanup; |
| 56 | + } |
| 57 | + |
| 58 | + bss.dev = st.st_dev; |
| 59 | + bss.ino = st.st_ino; |
| 60 | + |
| 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; |
| 64 | + |
| 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 | + |
| 72 | + /* trigger some syscalls */ |
| 73 | + usleep(1); |
| 74 | + |
| 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; |
| 78 | + |
| 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 | + if (!link) { |
| 84 | + bpf_link__destroy(link); |
| 85 | + link = NULL; |
| 86 | + } |
| 87 | + bpf_object__close(obj); |
| 88 | +} |
0 commit comments