1 // SPDX-License-Identifier: GPL-2.0
2 /* Copyright (c) 2020 Carlos Neira cneirabustos@gmail.com */
3 
4 #define _GNU_SOURCE
5 #include <test_progs.h>
6 #include "test_ns_current_pid_tgid.skel.h"
7 #include <sys/stat.h>
8 #include <sys/types.h>
9 #include <unistd.h>
10 #include <sys/syscall.h>
11 #include <sched.h>
12 #include <sys/wait.h>
13 #include <sys/mount.h>
14 #include <fcntl.h>
15 #include "network_helpers.h"
16 
17 #define STACK_SIZE (1024 * 1024)
18 static char child_stack[STACK_SIZE];
19 
get_pid_tgid(pid_t * pid,pid_t * tgid,struct test_ns_current_pid_tgid__bss * bss)20 static int get_pid_tgid(pid_t *pid, pid_t *tgid,
21 			struct test_ns_current_pid_tgid__bss *bss)
22 {
23 	struct stat st;
24 	int err;
25 
26 	*pid = syscall(SYS_gettid);
27 	*tgid = getpid();
28 
29 	err = stat("/proc/self/ns/pid", &st);
30 	if (!ASSERT_OK(err, "stat /proc/self/ns/pid"))
31 		return err;
32 
33 	bss->dev = st.st_dev;
34 	bss->ino = st.st_ino;
35 	bss->user_pid = 0;
36 	bss->user_tgid = 0;
37 	return 0;
38 }
39 
test_current_pid_tgid_tp(void * args)40 static int test_current_pid_tgid_tp(void *args)
41 {
42 	struct test_ns_current_pid_tgid__bss  *bss;
43 	struct test_ns_current_pid_tgid *skel;
44 	int ret = -1, err;
45 	pid_t tgid, pid;
46 
47 	skel = test_ns_current_pid_tgid__open();
48 	if (!ASSERT_OK_PTR(skel, "test_ns_current_pid_tgid__open"))
49 		return ret;
50 
51 	bpf_program__set_autoload(skel->progs.tp_handler, true);
52 
53 	err = test_ns_current_pid_tgid__load(skel);
54 	if (!ASSERT_OK(err, "test_ns_current_pid_tgid__load"))
55 		goto cleanup;
56 
57 	bss = skel->bss;
58 	if (get_pid_tgid(&pid, &tgid, bss))
59 		goto cleanup;
60 
61 	err = test_ns_current_pid_tgid__attach(skel);
62 	if (!ASSERT_OK(err, "test_ns_current_pid_tgid__attach"))
63 		goto cleanup;
64 
65 	/* trigger tracepoint */
66 	usleep(1);
67 	if (!ASSERT_EQ(bss->user_pid, pid, "pid"))
68 		goto cleanup;
69 	if (!ASSERT_EQ(bss->user_tgid, tgid, "tgid"))
70 		goto cleanup;
71 	ret = 0;
72 
73 cleanup:
74 	test_ns_current_pid_tgid__destroy(skel);
75 	return ret;
76 }
77 
test_current_pid_tgid_cgrp(void * args)78 static int test_current_pid_tgid_cgrp(void *args)
79 {
80 	struct test_ns_current_pid_tgid__bss *bss;
81 	struct test_ns_current_pid_tgid *skel;
82 	int server_fd = -1, ret = -1, err;
83 	int cgroup_fd = *(int *)args;
84 	pid_t tgid, pid;
85 
86 	skel = test_ns_current_pid_tgid__open();
87 	if (!ASSERT_OK_PTR(skel, "test_ns_current_pid_tgid__open"))
88 		return ret;
89 
90 	bpf_program__set_autoload(skel->progs.cgroup_bind4, true);
91 
92 	err = test_ns_current_pid_tgid__load(skel);
93 	if (!ASSERT_OK(err, "test_ns_current_pid_tgid__load"))
94 		goto cleanup;
95 
96 	bss = skel->bss;
97 	if (get_pid_tgid(&pid, &tgid, bss))
98 		goto cleanup;
99 
100 	skel->links.cgroup_bind4 = bpf_program__attach_cgroup(
101 		skel->progs.cgroup_bind4, cgroup_fd);
102 	if (!ASSERT_OK_PTR(skel->links.cgroup_bind4, "bpf_program__attach_cgroup"))
103 		goto cleanup;
104 
105 	server_fd = start_server(AF_INET, SOCK_STREAM, NULL, 0, 0);
106 	if (!ASSERT_GE(server_fd, 0, "start_server"))
107 		goto cleanup;
108 
109 	if (!ASSERT_EQ(bss->user_pid, pid, "pid"))
110 		goto cleanup;
111 	if (!ASSERT_EQ(bss->user_tgid, tgid, "tgid"))
112 		goto cleanup;
113 	ret = 0;
114 
115 cleanup:
116 	if (server_fd >= 0)
117 		close(server_fd);
118 	test_ns_current_pid_tgid__destroy(skel);
119 	return ret;
120 }
121 
test_ns_current_pid_tgid_new_ns(int (* fn)(void *),void * arg)122 static void test_ns_current_pid_tgid_new_ns(int (*fn)(void *), void *arg)
123 {
124 	int wstatus;
125 	pid_t cpid;
126 
127 	/* Create a process in a new namespace, this process
128 	 * will be the init process of this new namespace hence will be pid 1.
129 	 */
130 	cpid = clone(fn, child_stack + STACK_SIZE,
131 		     CLONE_NEWPID | SIGCHLD, arg);
132 
133 	if (!ASSERT_NEQ(cpid, -1, "clone"))
134 		return;
135 
136 	if (!ASSERT_NEQ(waitpid(cpid, &wstatus, 0), -1, "waitpid"))
137 		return;
138 
139 	if (!ASSERT_OK(WEXITSTATUS(wstatus), "newns_pidtgid"))
140 		return;
141 }
142 
test_in_netns(int (* fn)(void *),void * arg)143 static void test_in_netns(int (*fn)(void *), void *arg)
144 {
145 	struct nstoken *nstoken = NULL;
146 
147 	SYS(cleanup, "ip netns add ns_current_pid_tgid");
148 	SYS(cleanup, "ip -net ns_current_pid_tgid link set dev lo up");
149 
150 	nstoken = open_netns("ns_current_pid_tgid");
151 	if (!ASSERT_OK_PTR(nstoken, "open_netns"))
152 		goto cleanup;
153 
154 	test_ns_current_pid_tgid_new_ns(fn, arg);
155 
156 cleanup:
157 	if (nstoken)
158 		close_netns(nstoken);
159 	SYS_NOFAIL("ip netns del ns_current_pid_tgid");
160 }
161 
162 /* TODO: use a different tracepoint */
serial_test_ns_current_pid_tgid(void)163 void serial_test_ns_current_pid_tgid(void)
164 {
165 	if (test__start_subtest("root_ns_tp"))
166 		test_current_pid_tgid_tp(NULL);
167 	if (test__start_subtest("new_ns_tp"))
168 		test_ns_current_pid_tgid_new_ns(test_current_pid_tgid_tp, NULL);
169 	if (test__start_subtest("new_ns_cgrp")) {
170 		int cgroup_fd = -1;
171 
172 		cgroup_fd = test__join_cgroup("/sock_addr");
173 		if (ASSERT_GE(cgroup_fd, 0, "join_cgroup")) {
174 			test_in_netns(test_current_pid_tgid_cgrp, &cgroup_fd);
175 			close(cgroup_fd);
176 		}
177 	}
178 }
179