2 * Check decoding of NS_* commands of ioctl syscall.
4 * Copyright (c) 2017 Nikolay Marchuk <marchuk.nikolay.a@gmail.com>
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * 3. The name of the author may not be used to endorse or promote products
16 * derived from this software without specific prior written permission.
18 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
19 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
20 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
21 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
22 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
23 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
27 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
36 #include <sys/ioctl.h>
42 # define CLONE_NEWUSER 0x10000000
46 test_no_namespace(void)
48 ioctl(-1, NS_GET_USERNS);
49 printf("ioctl(-1, NS_GET_USERNS) = -1 EBADF (%m)\n");
50 ioctl(-1, NS_GET_PARENT);
51 printf("ioctl(-1, NS_GET_PARENT) = -1 EBADF (%m)\n");
52 ioctl(-1, NS_GET_NSTYPE);
53 printf("ioctl(-1, NS_GET_NSTYPE) = -1 EBADF (%m)\n");
54 ioctl(-1, NS_GET_OWNER_UID, NULL);
55 printf("ioctl(-1, NS_GET_OWNER_UID, NULL) = -1 EBADF (%m)\n");
61 char path[sizeof("/proc/%d/ns/user") + sizeof(int)*3];
62 snprintf(path, sizeof(path), "/proc/%d/ns/user", pid);
64 int ns_fd = open(path, O_RDONLY);
66 perror_msg_and_skip("open: %s", path);
68 int userns_fd = ioctl(ns_fd, NS_GET_USERNS);
69 printf("ioctl(%d, NS_GET_USERNS) = %s\n", ns_fd, sprintrc(userns_fd));
71 int parent_ns_fd = ioctl(userns_fd, NS_GET_PARENT);
72 printf("ioctl(%d, NS_GET_PARENT) = %s\n",
73 userns_fd, sprintrc(parent_ns_fd));
75 int nstype = ioctl(userns_fd, NS_GET_NSTYPE);
77 printf("ioctl(%d, NS_GET_NSTYPE) = %s\n",
78 userns_fd, sprintrc(nstype));
80 printf("ioctl(%d, NS_GET_NSTYPE) = %d (CLONE_NEWUSER)\n",
84 TAIL_ALLOC_OBJECT_CONST_PTR(unsigned int, uid);
85 int rc = ioctl(userns_fd, NS_GET_OWNER_UID, uid);
87 printf("ioctl(%d, NS_GET_OWNER_UID, %p) = %s\n",
88 userns_fd, uid, sprintrc(rc));
90 printf("ioctl(%d, NS_GET_OWNER_UID, [%u]) = %d\n",
98 int *pipefd = (int *) arg;
100 /* Wait for EOF from pipe. */
101 if (read(pipefd[0], &pipefd[1], 1))
102 perror_msg_and_fail("read");
107 extern int __clone2(int (*)(void *), void *, size_t, int, void *, ...);
108 # define clone(fn, child_stack, flags, arg) \
109 __clone2(fn, child_stack, get_page_size() / 2, flags, arg)
113 test_user_namespace(void)
120 perror_msg_and_fail("pipe");
122 pid = clone(child, tail_alloc(get_page_size() / 2),
123 CLONE_NEWUSER | CLONE_UNTRACED | SIGCHLD, pipefd);
131 if (wait(&status) != pid) {
132 perror_msg_and_fail("wait");
133 } else if (status != 0) {
134 error_msg_and_fail("unexpected child exit status %d", status);
142 test_user_namespace();
143 puts("+++ exited with 0 +++");