1 // SPDX-License-Identifier: GPL-2.0
7 #include <linux/types.h>
18 #include "../kselftest.h"
25 static int error_set(struct error *err, int code, const char *fmt, ...)
30 if (code == PIDFD_PASS || !err || err->code != PIDFD_PASS)
35 r = vsnprintf(err->msg, sizeof(err->msg), fmt, args);
36 assert((size_t)r < sizeof(err->msg));
42 static void error_report(struct error *err, const char *test_name)
46 ksft_exit_fail_msg("%s test: Fatal: %s\n", test_name, err->msg);
50 /* will be: not ok %d # error %s test: %s */
51 ksft_test_result_error("%s test: %s\n", test_name, err->msg);
55 /* will be: not ok %d # SKIP %s test: %s */
56 ksft_test_result_skip("%s test: %s\n", test_name, err->msg);
60 ksft_test_result_pass("%s test: Expected failure: %s\n",
65 ksft_test_result_pass("%s test: Passed\n", test_name);
69 ksft_exit_fail_msg("%s test: Unknown code: %d %s\n",
70 test_name, err->code, err->msg);
75 static inline int error_check(struct error *err, const char *test_name)
77 /* In case of error we bail out and terminate the test program */
78 if (err->code == PIDFD_ERROR)
79 error_report(err, test_name);
84 #define CHILD_STACK_SIZE 8192
92 static struct child clone_newns(int (*fn)(void *), void *args,
95 static int flags = CLONE_PIDFD | CLONE_NEWPID | CLONE_NEWNS | SIGCHLD;
98 if (!(flags & CLONE_NEWUSER) && geteuid() != 0)
99 flags |= CLONE_NEWUSER;
101 ret.stack = mmap(NULL, CHILD_STACK_SIZE, PROT_READ | PROT_WRITE,
102 MAP_PRIVATE | MAP_ANONYMOUS | MAP_STACK, -1, 0);
103 if (ret.stack == MAP_FAILED) {
104 error_set(err, -1, "mmap of stack failed (errno %d)", errno);
109 ret.pid = __clone2(fn, ret.stack, CHILD_STACK_SIZE, flags, args, &ret.fd);
111 ret.pid = clone(fn, ret.stack + CHILD_STACK_SIZE, flags, args, &ret.fd);
115 error_set(err, PIDFD_ERROR, "clone failed (ret %d, errno %d)",
120 ksft_print_msg("New child: %d, fd: %d\n", ret.pid, ret.fd);
125 static inline void child_close(struct child *child)
130 static inline int child_join(struct child *child, struct error *err)
134 r = wait_for_pid(child->pid);
136 error_set(err, PIDFD_ERROR, "waitpid failed (ret %d, errno %d)",
139 error_set(err, r, "child %d reported: %d", child->pid, r);
141 if (munmap(child->stack, CHILD_STACK_SIZE)) {
142 error_set(err, -1, "munmap of child stack failed (errno %d)", errno);
146 ksft_print_msg("waitpid WEXITSTATUS=%d\n", r);
150 static inline int child_join_close(struct child *child, struct error *err)
153 return child_join(child, err);
156 static inline void trim_newline(char *str)
158 char *pos = strrchr(str, '\n');
164 static int verify_fdinfo(int pidfd, struct error *err, const char *prefix,
165 size_t prefix_len, const char *expect, ...)
167 char buffer[512] = {0, };
168 char path[512] = {0, };
176 va_start(args, expect);
177 r = vsnprintf(buffer, sizeof(buffer), expect, args);
178 assert((size_t)r < sizeof(buffer));
181 snprintf(path, sizeof(path), "/proc/self/fdinfo/%d", pidfd);
182 f = fopen(path, "re");
184 return error_set(err, PIDFD_ERROR, "fdinfo open failed for %d",
187 while (getline(&line, &n, f) != -1) {
190 if (strncmp(line, prefix, prefix_len))
195 val = line + prefix_len;
196 r = strcmp(val, buffer);
199 trim_newline(buffer);
200 error_set(err, PIDFD_FAIL, "%s '%s' != '%s'",
201 prefix, val, buffer);
210 return error_set(err, PIDFD_FAIL, "%s not found for fd %d",
216 static int child_fdinfo_nspid_test(void *args)
222 /* if we got no fd for the sibling, we are done */
226 /* verify that we can not resolve the pidfd for a process
227 * in a sibling pid namespace, i.e. a pid namespace it is
228 * not in our or a descended namespace
230 r = mount(NULL, "/", NULL, MS_REC | MS_PRIVATE, 0);
232 ksft_print_msg("Failed to remount / private\n");
236 (void)umount2("/proc", MNT_DETACH);
237 r = mount("proc", "/proc", "proc", 0, NULL);
239 ksft_print_msg("Failed to remount /proc\n");
243 pidfd = *(int *)args;
244 r = verify_fdinfo(pidfd, &err, "NSpid:", 6, "\t0\n");
247 ksft_print_msg("NSpid fdinfo check failed: %s\n", err.msg);
252 static void test_pidfd_fdinfo_nspid(void)
255 struct error err = {0, };
256 const char *test_name = "pidfd check for NSpid in fdinfo";
258 /* Create a new child in a new pid and mount namespace */
259 a = clone_newns(child_fdinfo_nspid_test, NULL, &err);
260 error_check(&err, test_name);
262 /* Pass the pidfd representing the first child to the
263 * second child, which will be in a sibling pid namespace,
264 * which means that the fdinfo NSpid entry for the pidfd
265 * should only contain '0'.
267 b = clone_newns(child_fdinfo_nspid_test, &a.fd, &err);
268 error_check(&err, test_name);
270 /* The children will have pid 1 in the new pid namespace,
271 * so the line must be 'NSPid:\t<pid>\t1'.
273 verify_fdinfo(a.fd, &err, "NSpid:", 6, "\t%d\t%d\n", a.pid, 1);
274 verify_fdinfo(b.fd, &err, "NSpid:", 6, "\t%d\t%d\n", b.pid, 1);
276 /* wait for the process, check the exit status and set
277 * 'err' accordingly, if it is not already set.
279 child_join_close(&a, &err);
280 child_join_close(&b, &err);
282 error_report(&err, test_name);
285 static void test_pidfd_dead_fdinfo(void)
288 struct error err = {0, };
289 const char *test_name = "pidfd check fdinfo for dead process";
291 /* Create a new child in a new pid and mount namespace */
292 a = clone_newns(child_fdinfo_nspid_test, NULL, &err);
293 error_check(&err, test_name);
294 child_join(&a, &err);
296 verify_fdinfo(a.fd, &err, "Pid:", 4, "\t-1\n");
297 verify_fdinfo(a.fd, &err, "NSpid:", 6, "\t-1\n");
299 error_report(&err, test_name);
302 int main(int argc, char **argv)
307 test_pidfd_fdinfo_nspid();
308 test_pidfd_dead_fdinfo();