xref: /linux/tools/testing/selftests/filesystems/fscontext_ns/fscontext_ns_test.c (revision 146cc263e457ff6055fe7829e4f4f4b0b5d5dd86)
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * Copyright (C) 2026 Christian Brauner <brauner@kernel.org>
4  *
5  * Test that completing a filesystem context from another user namespace
6  * doesn't warn.
7  *
8  * fsopen() records the caller's user namespace in fc->user_ns and hands
9  * back an ordinary file descriptor. The task that issues
10  * FSCONFIG_CMD_CREATE need not be the one that created the context: the fd
11  * is inherited across fork() and exec() and it can be passed over a unix
12  * socket. vfs_cmd_create() authorizes the create with mount_capable(),
13  * which for FS_USERNS_MOUNT checks ns_capable(fc->user_ns, CAP_SYS_ADMIN),
14  * and that succeeds for a task holding CAP_SYS_ADMIN in an ancestor of
15  * fc->user_ns.
16  *
17  * binfmt_misc and overlayfs used to WARN_ON() that mismatch, which let an
18  * unprivileged user taint the kernel, flood the log and panic a kernel
19  * booted with panic_on_warn. The mount must still be refused, but it must
20  * not warn.
21  */
22 #define _GNU_SOURCE
23 
24 #include <errno.h>
25 #include <sched.h>
26 #include <stdio.h>
27 #include <stdlib.h>
28 #include <string.h>
29 #include <sys/socket.h>
30 #include <sys/wait.h>
31 #include <unistd.h>
32 
33 #include "../wrappers.h"
34 #include "../utils.h"
35 #include "../../kselftest_harness.h"
36 
37 #ifndef FSCONFIG_CMD_CREATE
38 #define FSCONFIG_CMD_CREATE	6
39 #endif
40 
41 /* TAINT_WARN, i.e. bit 9 of /proc/sys/kernel/tainted. */
42 #define TAINT_WARN_BIT		9
43 
44 static bool taint_warn_set(void)
45 {
46 	unsigned long taint = 0;
47 	FILE *f;
48 
49 	f = fopen("/proc/sys/kernel/tainted", "r");
50 	if (!f)
51 		return false;
52 	if (fscanf(f, "%lu", &taint) != 1)
53 		taint = 0;
54 	fclose(f);
55 
56 	return taint & (1UL << TAINT_WARN_BIT);
57 }
58 
59 static int send_fd(int sock, int fd)
60 {
61 	char cmsgbuf[CMSG_SPACE(sizeof(int))] = {};
62 	char b[1] = { 'x' };
63 	struct iovec iov = { .iov_base = b, .iov_len = sizeof(b) };
64 	struct msghdr msg = {
65 		.msg_iov	= &iov,
66 		.msg_iovlen	= 1,
67 		.msg_control	= cmsgbuf,
68 		.msg_controllen	= sizeof(cmsgbuf),
69 	};
70 	struct cmsghdr *cmsg;
71 
72 	cmsg = CMSG_FIRSTHDR(&msg);
73 	cmsg->cmsg_level = SOL_SOCKET;
74 	cmsg->cmsg_type = SCM_RIGHTS;
75 	cmsg->cmsg_len = CMSG_LEN(sizeof(int));
76 	memcpy(CMSG_DATA(cmsg), &fd, sizeof(int));
77 
78 	return sendmsg(sock, &msg, 0) < 0 ? -1 : 0;
79 }
80 
81 static int recv_fd(int sock)
82 {
83 	char cmsgbuf[CMSG_SPACE(sizeof(int))] = {};
84 	char b[1];
85 	struct iovec iov = { .iov_base = b, .iov_len = sizeof(b) };
86 	struct msghdr msg = {
87 		.msg_iov	= &iov,
88 		.msg_iovlen	= 1,
89 		.msg_control	= cmsgbuf,
90 		.msg_controllen	= sizeof(cmsgbuf),
91 	};
92 	struct cmsghdr *cmsg;
93 	int fd = -1;
94 
95 	if (recvmsg(sock, &msg, 0) <= 0)
96 		return -1;
97 
98 	cmsg = CMSG_FIRSTHDR(&msg);
99 	if (!cmsg || cmsg->cmsg_type != SCM_RIGHTS)
100 		return -1;
101 	memcpy(&fd, CMSG_DATA(cmsg), sizeof(int));
102 
103 	return fd;
104 }
105 
106 /*
107  * Create a context for @fsname in a child and complete it here. With @nest
108  * the child first creates its own user namespace, so that the context is
109  * created in a descendant of the namespace completing it. The child needs a
110  * mount namespace of its own as well: fsopen() gates on may_mount(), which
111  * asks for CAP_SYS_ADMIN in the user namespace owning the caller's mount
112  * namespace.
113  *
114  * Returns the result of FSCONFIG_CMD_CREATE with errno set, or -ENODATA if
115  * the child could not create the context at all.
116  */
117 static int create_from_child(const char *fsname, bool nest)
118 {
119 	int sock[2], fd, ret, status;
120 	pid_t pid;
121 
122 	if (socketpair(AF_UNIX, SOCK_STREAM, 0, sock))
123 		return -ENODATA;
124 
125 	pid = fork();
126 	if (pid < 0) {
127 		close(sock[0]);
128 		close(sock[1]);
129 		return -ENODATA;
130 	}
131 
132 	if (pid == 0) {
133 		close(sock[0]);
134 
135 		if (nest && unshare(CLONE_NEWUSER | CLONE_NEWNS))
136 			_exit(1);
137 
138 		fd = sys_fsopen(fsname, 0);
139 		if (fd < 0)
140 			_exit(1);
141 		if (send_fd(sock[1], fd))
142 			_exit(1);
143 		_exit(0);
144 	}
145 
146 	close(sock[1]);
147 	fd = recv_fd(sock[0]);
148 	close(sock[0]);
149 	wait_for_pid(pid);
150 	waitpid(pid, &status, WNOHANG);
151 
152 	if (fd < 0)
153 		return -ENODATA;
154 
155 	errno = 0;
156 	ret = sys_fsconfig(fd, FSCONFIG_CMD_CREATE, NULL, NULL, 0);
157 	status = errno;
158 	close(fd);
159 	errno = status;
160 
161 	return ret;
162 }
163 
164 FIXTURE(fscontext_ns) {
165 	bool warn_before;
166 };
167 
168 FIXTURE_SETUP(fscontext_ns)
169 {
170 	self->warn_before = taint_warn_set();
171 
172 	if (setup_userns() != 0)
173 		SKIP(return, "setup_userns failed");
174 }
175 
176 FIXTURE_TEARDOWN(fscontext_ns)
177 {
178 }
179 
180 /*
181  * The condition the kernel used to WARN about. It has to be refused, and it
182  * has to be refused quietly: an unprivileged task reaches this.
183  */
184 FIXTURE_VARIANT(fscontext_ns) {
185 	const char *fsname;
186 	int expected_errno;
187 };
188 
189 FIXTURE_VARIANT_ADD(fscontext_ns, binfmt_misc) {
190 	.fsname = "binfmt_misc",
191 	.expected_errno = EINVAL,
192 };
193 
194 FIXTURE_VARIANT_ADD(fscontext_ns, overlay) {
195 	.fsname = "overlay",
196 	.expected_errno = EIO,
197 };
198 
199 TEST_F(fscontext_ns, create_from_descendant_userns)
200 {
201 	int ret;
202 
203 	ret = create_from_child(variant->fsname, true);
204 	if (ret == -ENODATA)
205 		SKIP(return, "%s unavailable", variant->fsname);
206 
207 	ASSERT_EQ(-1, ret);
208 	ASSERT_EQ(variant->expected_errno, errno);
209 
210 	/*
211 	 * Only meaningful if nothing had warned before us. Note that an
212 	 * unrelated warning racing this test would look like a failure.
213 	 */
214 	if (self->warn_before)
215 		TH_LOG("TAINT_WARN already set, not checking for a new warning");
216 	else
217 		ASSERT_FALSE(taint_warn_set());
218 }
219 
220 /*
221  * The same handover within one user namespace is a supported thing to do and
222  * has to keep working. binfmt_misc takes no options, so the create succeeds
223  * outright and this also shows the test really drives the create path.
224  */
225 TEST(create_from_same_userns)
226 {
227 	int ret;
228 
229 	if (setup_userns() != 0)
230 		SKIP(return, "setup_userns failed");
231 
232 	ret = create_from_child("binfmt_misc", false);
233 	if (ret == -ENODATA)
234 		SKIP(return, "binfmt_misc unavailable");
235 
236 	ASSERT_EQ(0, ret);
237 }
238 
239 TEST_HARNESS_MAIN
240