1 // SPDX-License-Identifier: GPL-2.0
7 #include <linux/types.h>
14 #include <sys/prctl.h>
17 #include <sys/socket.h>
21 #include "../clone3/clone3_selftests.h"
22 #include "../kselftest_harness.h"
37 const struct ns_info {
41 [PIDFD_NS_USER] = { "user", CLONE_NEWUSER, },
42 [PIDFD_NS_MNT] = { "mnt", CLONE_NEWNS, },
43 [PIDFD_NS_PID] = { "pid", CLONE_NEWPID, },
44 [PIDFD_NS_UTS] = { "uts", CLONE_NEWUTS, },
45 [PIDFD_NS_IPC] = { "ipc", CLONE_NEWIPC, },
46 [PIDFD_NS_NET] = { "net", CLONE_NEWNET, },
47 [PIDFD_NS_CGROUP] = { "cgroup", CLONE_NEWCGROUP, },
48 [PIDFD_NS_PIDCLD] = { "pid_for_children", 0, },
49 [PIDFD_NS_TIME] = { "time", CLONE_NEWTIME, },
52 FIXTURE(current_nsset)
56 int nsfds[PIDFD_NS_MAX];
58 pid_t child_pid_exited;
59 int child_pidfd_exited;
63 int child_nsfds1[PIDFD_NS_MAX];
67 int child_nsfds2[PIDFD_NS_MAX];
70 static int sys_waitid(int which, pid_t pid, int options)
72 return syscall(__NR_waitid, which, pid, NULL, options, NULL);
75 pid_t create_child(int *pidfd, unsigned flags)
77 struct __clone_args args = {
78 .flags = CLONE_PIDFD | flags,
79 .exit_signal = SIGCHLD,
80 .pidfd = ptr_to_u64(pidfd),
83 return sys_clone3(&args, sizeof(struct clone_args));
86 static bool switch_timens(void)
90 if (unshare(CLONE_NEWTIME))
93 fd = open("/proc/self/ns/time_for_children", O_RDONLY | O_CLOEXEC);
97 ret = setns(fd, CLONE_NEWTIME);
102 static ssize_t read_nointr(int fd, void *buf, size_t count)
107 ret = read(fd, buf, count);
108 } while (ret < 0 && errno == EINTR);
113 static ssize_t write_nointr(int fd, const void *buf, size_t count)
118 ret = write(fd, buf, count);
119 } while (ret < 0 && errno == EINTR);
124 FIXTURE_SETUP(current_nsset)
130 for (i = 0; i < PIDFD_NS_MAX; i++) {
131 self->nsfds[i] = -EBADF;
132 self->child_nsfds1[i] = -EBADF;
133 self->child_nsfds2[i] = -EBADF;
136 proc_fd = open("/proc/self/ns", O_DIRECTORY | O_CLOEXEC);
137 ASSERT_GE(proc_fd, 0) {
138 TH_LOG("%m - Failed to open /proc/self/ns");
141 self->pid = getpid();
142 for (i = 0; i < PIDFD_NS_MAX; i++) {
143 const struct ns_info *info = &ns_info[i];
144 self->nsfds[i] = openat(proc_fd, info->name, O_RDONLY | O_CLOEXEC);
145 if (self->nsfds[i] < 0) {
146 EXPECT_EQ(errno, ENOENT) {
147 TH_LOG("%m - Failed to open %s namespace for process %d",
148 info->name, self->pid);
153 self->pidfd = sys_pidfd_open(self->pid, 0);
154 EXPECT_GT(self->pidfd, 0) {
155 TH_LOG("%m - Failed to open pidfd for process %d", self->pid);
158 /* Create task that exits right away. */
159 self->child_pid_exited = create_child(&self->child_pidfd_exited,
160 CLONE_NEWUSER | CLONE_NEWNET);
161 EXPECT_GT(self->child_pid_exited, 0);
163 if (self->child_pid_exited == 0)
166 ASSERT_EQ(sys_waitid(P_PID, self->child_pid_exited, WEXITED | WNOWAIT), 0);
168 self->pidfd = sys_pidfd_open(self->pid, 0);
169 EXPECT_GE(self->pidfd, 0) {
170 TH_LOG("%m - Failed to open pidfd for process %d", self->pid);
173 ret = socketpair(AF_LOCAL, SOCK_STREAM | SOCK_CLOEXEC, 0, ipc_sockets);
176 /* Create tasks that will be stopped. */
177 self->child_pid1 = create_child(&self->child_pidfd1,
178 CLONE_NEWUSER | CLONE_NEWNS |
179 CLONE_NEWCGROUP | CLONE_NEWIPC |
180 CLONE_NEWUTS | CLONE_NEWPID |
182 EXPECT_GE(self->child_pid1, 0);
184 if (self->child_pid1 == 0) {
185 close(ipc_sockets[0]);
187 if (!switch_timens())
190 if (write_nointr(ipc_sockets[1], "1", 1) < 0)
193 close(ipc_sockets[1]);
199 close(ipc_sockets[1]);
200 ASSERT_EQ(read_nointr(ipc_sockets[0], &c, 1), 1);
201 close(ipc_sockets[0]);
203 ret = socketpair(AF_LOCAL, SOCK_STREAM | SOCK_CLOEXEC, 0, ipc_sockets);
206 self->child_pid2 = create_child(&self->child_pidfd2,
207 CLONE_NEWUSER | CLONE_NEWNS |
208 CLONE_NEWCGROUP | CLONE_NEWIPC |
209 CLONE_NEWUTS | CLONE_NEWPID |
211 EXPECT_GE(self->child_pid2, 0);
213 if (self->child_pid2 == 0) {
214 close(ipc_sockets[0]);
216 if (!switch_timens())
219 if (write_nointr(ipc_sockets[1], "1", 1) < 0)
222 close(ipc_sockets[1]);
228 close(ipc_sockets[1]);
229 ASSERT_EQ(read_nointr(ipc_sockets[0], &c, 1), 1);
230 close(ipc_sockets[0]);
232 for (i = 0; i < PIDFD_NS_MAX; i++) {
235 const struct ns_info *info = &ns_info[i];
237 self->nsfds[i] = openat(proc_fd, info->name, O_RDONLY | O_CLOEXEC);
238 if (self->nsfds[i] < 0) {
239 EXPECT_EQ(errno, ENOENT) {
240 TH_LOG("%m - Failed to open %s namespace for process %d",
241 info->name, self->pid);
245 ret = snprintf(p, sizeof(p), "/proc/%d/ns/%s",
246 self->child_pid1, info->name);
248 EXPECT_LT(ret, sizeof(p));
250 self->child_nsfds1[i] = open(p, O_RDONLY | O_CLOEXEC);
251 if (self->child_nsfds1[i] < 0) {
252 EXPECT_EQ(errno, ENOENT) {
253 TH_LOG("%m - Failed to open %s namespace for process %d",
254 info->name, self->child_pid1);
258 ret = snprintf(p, sizeof(p), "/proc/%d/ns/%s",
259 self->child_pid2, info->name);
261 EXPECT_LT(ret, sizeof(p));
263 self->child_nsfds2[i] = open(p, O_RDONLY | O_CLOEXEC);
264 if (self->child_nsfds2[i] < 0) {
265 EXPECT_EQ(errno, ENOENT) {
266 TH_LOG("%m - Failed to open %s namespace for process %d",
267 info->name, self->child_pid1);
275 FIXTURE_TEARDOWN(current_nsset)
279 ASSERT_EQ(sys_pidfd_send_signal(self->child_pidfd1,
280 SIGKILL, NULL, 0), 0);
281 ASSERT_EQ(sys_pidfd_send_signal(self->child_pidfd2,
282 SIGKILL, NULL, 0), 0);
284 for (i = 0; i < PIDFD_NS_MAX; i++) {
285 if (self->nsfds[i] >= 0)
286 close(self->nsfds[i]);
287 if (self->child_nsfds1[i] >= 0)
288 close(self->child_nsfds1[i]);
289 if (self->child_nsfds2[i] >= 0)
290 close(self->child_nsfds2[i]);
293 if (self->child_pidfd1 >= 0)
294 EXPECT_EQ(0, close(self->child_pidfd1));
295 if (self->child_pidfd2 >= 0)
296 EXPECT_EQ(0, close(self->child_pidfd2));
297 ASSERT_EQ(sys_waitid(P_PID, self->child_pid_exited, WEXITED), 0);
298 ASSERT_EQ(sys_waitid(P_PID, self->child_pid1, WEXITED), 0);
299 ASSERT_EQ(sys_waitid(P_PID, self->child_pid2, WEXITED), 0);
302 static int preserve_ns(const int pid, const char *ns)
307 ret = snprintf(path, sizeof(path), "/proc/%d/ns/%s", pid, ns);
308 if (ret < 0 || (size_t)ret >= sizeof(path))
311 return open(path, O_RDONLY | O_CLOEXEC);
314 static int in_same_namespace(int ns_fd1, pid_t pid2, const char *ns)
318 struct stat ns_st1, ns_st2;
320 ret = fstat(ns_fd1, &ns_st1);
324 ns_fd2 = preserve_ns(pid2, ns);
328 ret = fstat(ns_fd2, &ns_st2);
333 /* processes are in the same namespace */
334 if ((ns_st1.st_dev == ns_st2.st_dev) &&
335 (ns_st1.st_ino == ns_st2.st_ino))
338 /* processes are in different namespaces */
342 /* Test that we can't pass garbage to the kernel. */
343 TEST_F(current_nsset, invalid_flags)
345 ASSERT_NE(setns(self->pidfd, 0), 0);
346 EXPECT_EQ(errno, EINVAL);
348 ASSERT_NE(setns(self->pidfd, -1), 0);
349 EXPECT_EQ(errno, EINVAL);
351 ASSERT_NE(setns(self->pidfd, CLONE_VM), 0);
352 EXPECT_EQ(errno, EINVAL);
354 ASSERT_NE(setns(self->pidfd, CLONE_NEWUSER | CLONE_VM), 0);
355 EXPECT_EQ(errno, EINVAL);
358 /* Test that we can't attach to a task that has already exited. */
359 TEST_F(current_nsset, pidfd_exited_child)
364 ASSERT_NE(setns(self->child_pidfd_exited, CLONE_NEWUSER | CLONE_NEWNET),
366 EXPECT_EQ(errno, ESRCH);
369 for (i = 0; i < PIDFD_NS_MAX; i++) {
370 const struct ns_info *info = &ns_info[i];
371 /* Verify that we haven't changed any namespaces. */
372 if (self->nsfds[i] >= 0)
373 ASSERT_EQ(in_same_namespace(self->nsfds[i], pid, info->name), 1);
377 TEST_F(current_nsset, pidfd_incremental_setns)
383 for (i = 0; i < PIDFD_NS_MAX; i++) {
384 const struct ns_info *info = &ns_info[i];
387 if (self->child_nsfds1[i] < 0)
391 ASSERT_EQ(setns(self->child_pidfd1, info->flag), 0) {
392 TH_LOG("%m - Failed to setns to %s namespace of %d via pidfd %d",
393 info->name, self->child_pid1,
398 /* Verify that we have changed to the correct namespaces. */
399 if (info->flag == CLONE_NEWPID)
400 nsfd = self->nsfds[i];
402 nsfd = self->child_nsfds1[i];
403 ASSERT_EQ(in_same_namespace(nsfd, pid, info->name), 1) {
404 TH_LOG("setns failed to place us correctly into %s namespace of %d via pidfd %d",
405 info->name, self->child_pid1,
408 TH_LOG("Managed to correctly setns to %s namespace of %d via pidfd %d",
409 info->name, self->child_pid1, self->child_pidfd1);
413 TEST_F(current_nsset, nsfd_incremental_setns)
419 for (i = 0; i < PIDFD_NS_MAX; i++) {
420 const struct ns_info *info = &ns_info[i];
423 if (self->child_nsfds1[i] < 0)
427 ASSERT_EQ(setns(self->child_nsfds1[i], info->flag), 0) {
428 TH_LOG("%m - Failed to setns to %s namespace of %d via nsfd %d",
429 info->name, self->child_pid1,
430 self->child_nsfds1[i]);
434 /* Verify that we have changed to the correct namespaces. */
435 if (info->flag == CLONE_NEWPID)
436 nsfd = self->nsfds[i];
438 nsfd = self->child_nsfds1[i];
439 ASSERT_EQ(in_same_namespace(nsfd, pid, info->name), 1) {
440 TH_LOG("setns failed to place us correctly into %s namespace of %d via nsfd %d",
441 info->name, self->child_pid1,
442 self->child_nsfds1[i]);
444 TH_LOG("Managed to correctly setns to %s namespace of %d via nsfd %d",
445 info->name, self->child_pid1, self->child_nsfds1[i]);
449 TEST_F(current_nsset, pidfd_one_shot_setns)
455 for (i = 0; i < PIDFD_NS_MAX; i++) {
456 const struct ns_info *info = &ns_info[i];
458 if (self->child_nsfds1[i] < 0)
462 TH_LOG("Adding %s namespace of %d to list of namespaces to attach to",
463 info->name, self->child_pid1);
466 ASSERT_EQ(setns(self->child_pidfd1, flags), 0) {
467 TH_LOG("%m - Failed to setns to namespaces of %d",
472 for (i = 0; i < PIDFD_NS_MAX; i++) {
473 const struct ns_info *info = &ns_info[i];
476 if (self->child_nsfds1[i] < 0)
479 /* Verify that we have changed to the correct namespaces. */
480 if (info->flag == CLONE_NEWPID)
481 nsfd = self->nsfds[i];
483 nsfd = self->child_nsfds1[i];
484 ASSERT_EQ(in_same_namespace(nsfd, pid, info->name), 1) {
485 TH_LOG("setns failed to place us correctly into %s namespace of %d",
486 info->name, self->child_pid1);
488 TH_LOG("Managed to correctly setns to %s namespace of %d",
489 info->name, self->child_pid1);
493 TEST_F(current_nsset, no_foul_play)
498 for (i = 0; i < PIDFD_NS_MAX; i++) {
499 const struct ns_info *info = &ns_info[i];
501 if (self->child_nsfds1[i] < 0)
505 if (info->flag) /* No use logging pid_for_children. */
506 TH_LOG("Adding %s namespace of %d to list of namespaces to attach to",
507 info->name, self->child_pid1);
510 ASSERT_EQ(setns(self->child_pidfd1, flags), 0) {
511 TH_LOG("%m - Failed to setns to namespaces of %d vid pidfd %d",
512 self->child_pid1, self->child_pidfd1);
516 * Can't setns to a user namespace outside of our hierarchy since we
517 * don't have caps in there and didn't create it. That means that under
518 * no circumstances should we be able to setns to any of the other
519 * ones since they aren't owned by our user namespace.
521 for (i = 0; i < PIDFD_NS_MAX; i++) {
522 const struct ns_info *info = &ns_info[i];
524 if (self->child_nsfds2[i] < 0 || !info->flag)
527 ASSERT_NE(setns(self->child_pidfd2, info->flag), 0) {
528 TH_LOG("Managed to setns to %s namespace of %d via pidfd %d",
529 info->name, self->child_pid2,
532 TH_LOG("%m - Correctly failed to setns to %s namespace of %d via pidfd %d",
533 info->name, self->child_pid2,
536 ASSERT_NE(setns(self->child_nsfds2[i], info->flag), 0) {
537 TH_LOG("Managed to setns to %s namespace of %d via nsfd %d",
538 info->name, self->child_pid2,
539 self->child_nsfds2[i]);
541 TH_LOG("%m - Correctly failed to setns to %s namespace of %d via nsfd %d",
542 info->name, self->child_pid2,
543 self->child_nsfds2[i]);
551 fd = sys_memfd_create("rostock", 0);
554 ASSERT_NE(setns(fd, 0), 0);
555 EXPECT_EQ(errno, EINVAL);