xref: /linux/kernel/nscommon.c (revision f16d85cd3de9d0c761b74155b4cbe2f20d3dcf80)
1 // SPDX-License-Identifier: GPL-2.0-only
2 /* Copyright (c) 2025 Christian Brauner <brauner@kernel.org> */
3 
4 #include <linux/ns_common.h>
5 #include <linux/nstree.h>
6 #include <linux/proc_ns.h>
7 #include <linux/security.h>
8 #include <linux/user_namespace.h>
9 #include <linux/vfsdebug.h>
10 
11 #ifdef CONFIG_DEBUG_VFS
12 static void ns_debug(struct ns_common *ns, const struct proc_ns_operations *ops)
13 {
14 	switch (ns->ns_type) {
15 #ifdef CONFIG_CGROUPS
16 	case CLONE_NEWCGROUP:
17 		VFS_WARN_ON_ONCE(ops != &cgroupns_operations);
18 		break;
19 #endif
20 #ifdef CONFIG_IPC_NS
21 	case CLONE_NEWIPC:
22 		VFS_WARN_ON_ONCE(ops != &ipcns_operations);
23 		break;
24 #endif
25 	case CLONE_NEWNS:
26 		VFS_WARN_ON_ONCE(ops != &mntns_operations);
27 		break;
28 #ifdef CONFIG_NET_NS
29 	case CLONE_NEWNET:
30 		VFS_WARN_ON_ONCE(ops != &netns_operations);
31 		break;
32 #endif
33 #ifdef CONFIG_PID_NS
34 	case CLONE_NEWPID:
35 		VFS_WARN_ON_ONCE(ops != &pidns_operations);
36 		break;
37 #endif
38 #ifdef CONFIG_TIME_NS
39 	case CLONE_NEWTIME:
40 		VFS_WARN_ON_ONCE(ops != &timens_operations);
41 		break;
42 #endif
43 #ifdef CONFIG_USER_NS
44 	case CLONE_NEWUSER:
45 		VFS_WARN_ON_ONCE(ops != &userns_operations);
46 		break;
47 #endif
48 #ifdef CONFIG_UTS_NS
49 	case CLONE_NEWUTS:
50 		VFS_WARN_ON_ONCE(ops != &utsns_operations);
51 		break;
52 #endif
53 	}
54 }
55 #endif
56 
57 int __ns_common_init(struct ns_common *ns, u32 ns_type, const struct proc_ns_operations *ops,
58 		     int inum, u64 ns_id)
59 {
60 	int ret = 0;
61 
62 	refcount_set(&ns->__ns_ref, 1);
63 	ns->stashed = NULL;
64 #ifdef CONFIG_SECURITY
65 	ns->ns_security = NULL;
66 #endif
67 	ns->ops = ops;
68 	ns->ns_type = ns_type;
69 	ns_tree_node_init(&ns->ns_tree_node);
70 	ns_tree_node_init(&ns->ns_unified_node);
71 	ns_tree_node_init(&ns->ns_owner_node);
72 	ns_tree_root_init(&ns->ns_owner_root);
73 
74 #ifdef CONFIG_DEBUG_VFS
75 	ns_debug(ns, ops);
76 #endif
77 
78 	if (inum)
79 		ns->inum = inum;
80 	else
81 		ret = proc_alloc_inum(&ns->inum);
82 	if (ret)
83 		return ret;
84 
85 	/* Allocate the id before the LSM hook so it can track the namespace by it. */
86 	__ns_tree_gen_id(ns, ns_id);
87 
88 	ret = security_namespace_init(ns);
89 	if (ret) {
90 		if (!inum)
91 			proc_free_inum(ns->inum);
92 		return ret;
93 	}
94 
95 	/*
96 	 * Tree ref starts at 0. It's incremented when namespace enters
97 	 * active use (installed in nsproxy) and decremented when all
98 	 * active uses are gone. Initial namespaces are always active.
99 	 */
100 	if (is_ns_init_inum(ns))
101 		atomic_set(&ns->__ns_ref_active, 1);
102 	else
103 		atomic_set(&ns->__ns_ref_active, 0);
104 	return 0;
105 }
106 
107 void __ns_common_free(struct ns_common *ns)
108 {
109 	security_namespace_free(ns);
110 
111 	if (ns->inum > MNT_NS_INO_SPECIAL_MAX)
112 		proc_free_inum(ns->inum);
113 }
114 
115 struct ns_common *__must_check ns_owner(struct ns_common *ns)
116 {
117 	struct user_namespace *owner;
118 
119 	if (unlikely(!ns->ops))
120 		return NULL;
121 	VFS_WARN_ON_ONCE(!ns->ops->owner);
122 	owner = ns->ops->owner(ns);
123 	VFS_WARN_ON_ONCE(!owner && ns != to_ns_common(&init_user_ns));
124 	if (!owner)
125 		return NULL;
126 	/* Skip init_user_ns as it's always active */
127 	if (owner == &init_user_ns)
128 		return NULL;
129 	return to_ns_common(owner);
130 }
131 
132 /*
133  * The active reference count works by having each namespace that gets
134  * created take a single active reference on its owning user namespace.
135  * That single reference is only released once the child namespace's
136  * active count itself goes down.
137  *
138  * A regular namespace tree might look as follow:
139  * Legend:
140  * + : adding active reference
141  * - : dropping active reference
142  * x : always active (initial namespace)
143  *
144  *
145  *                 net_ns          pid_ns
146  *                       \        /
147  *                        +      +
148  *                        user_ns1 (2)
149  *                            |
150  *                 ipc_ns     |     uts_ns
151  *                       \    |    /
152  *                        +   +   +
153  *                        user_ns2 (3)
154  *                            |
155  *            cgroup_ns       |       mnt_ns
156  *                     \      |      /
157  *                      x     x     x
158  *                      init_user_ns (1)
159  *
160  * If both net_ns and pid_ns put their last active reference on
161  * themselves it will cascade to user_ns1 dropping its own active
162  * reference and dropping one active reference on user_ns2:
163  *
164  *                 net_ns          pid_ns
165  *                       \        /
166  *                        -      -
167  *                        user_ns1 (0)
168  *                            |
169  *                 ipc_ns     |     uts_ns
170  *                       \    |    /
171  *                        +   -   +
172  *                        user_ns2 (2)
173  *                            |
174  *            cgroup_ns       |       mnt_ns
175  *                     \      |      /
176  *                      x     x     x
177  *                      init_user_ns (1)
178  *
179  * The iteration stops once we reach a namespace that still has active
180  * references.
181  */
182 void __ns_ref_active_put(struct ns_common *ns)
183 {
184 	/* Initial namespaces are always active. */
185 	if (is_ns_init_id(ns))
186 		return;
187 
188 	if (!atomic_dec_and_test(&ns->__ns_ref_active)) {
189 		VFS_WARN_ON_ONCE(__ns_ref_active_read(ns) < 0);
190 		return;
191 	}
192 
193 	VFS_WARN_ON_ONCE(is_ns_init_id(ns));
194 	VFS_WARN_ON_ONCE(!__ns_ref_read(ns));
195 
196 	for (;;) {
197 		ns = ns_owner(ns);
198 		if (!ns)
199 			return;
200 		VFS_WARN_ON_ONCE(is_ns_init_id(ns));
201 		if (!atomic_dec_and_test(&ns->__ns_ref_active)) {
202 			VFS_WARN_ON_ONCE(__ns_ref_active_read(ns) < 0);
203 			return;
204 		}
205 	}
206 }
207 
208 /*
209  * The active reference count works by having each namespace that gets
210  * created take a single active reference on its owning user namespace.
211  * That single reference is only released once the child namespace's
212  * active count itself goes down. This makes it possible to efficiently
213  * resurrect a namespace tree:
214  *
215  * A regular namespace tree might look as follow:
216  * Legend:
217  * + : adding active reference
218  * - : dropping active reference
219  * x : always active (initial namespace)
220  *
221  *
222  *                 net_ns          pid_ns
223  *                       \        /
224  *                        +      +
225  *                        user_ns1 (2)
226  *                            |
227  *                 ipc_ns     |     uts_ns
228  *                       \    |    /
229  *                        +   +   +
230  *                        user_ns2 (3)
231  *                            |
232  *            cgroup_ns       |       mnt_ns
233  *                     \      |      /
234  *                      x     x     x
235  *                      init_user_ns (1)
236  *
237  * If both net_ns and pid_ns put their last active reference on
238  * themselves it will cascade to user_ns1 dropping its own active
239  * reference and dropping one active reference on user_ns2:
240  *
241  *                 net_ns          pid_ns
242  *                       \        /
243  *                        -      -
244  *                        user_ns1 (0)
245  *                            |
246  *                 ipc_ns     |     uts_ns
247  *                       \    |    /
248  *                        +   -   +
249  *                        user_ns2 (2)
250  *                            |
251  *            cgroup_ns       |       mnt_ns
252  *                     \      |      /
253  *                      x     x     x
254  *                      init_user_ns (1)
255  *
256  * Assume the whole tree is dead but all namespaces are still active:
257  *
258  *                 net_ns          pid_ns
259  *                       \        /
260  *                        -      -
261  *                        user_ns1 (0)
262  *                            |
263  *                 ipc_ns     |     uts_ns
264  *                       \    |    /
265  *                        -   -   -
266  *                        user_ns2 (0)
267  *                            |
268  *            cgroup_ns       |       mnt_ns
269  *                     \      |      /
270  *                      x     x     x
271  *                      init_user_ns (1)
272  *
273  * Now assume the net_ns gets resurrected (.e.g., via the SIOCGSKNS ioctl()):
274  *
275  *                 net_ns          pid_ns
276  *                       \        /
277  *                        +      -
278  *                        user_ns1 (0)
279  *                            |
280  *                 ipc_ns     |     uts_ns
281  *                       \    |    /
282  *                        -   +   -
283  *                        user_ns2 (0)
284  *                            |
285  *            cgroup_ns       |       mnt_ns
286  *                     \      |      /
287  *                      x     x     x
288  *                      init_user_ns (1)
289  *
290  * If net_ns had a zero reference count and we bumped it we also need to
291  * take another reference on its owning user namespace. Similarly, if
292  * pid_ns had a zero reference count it also needs to take another
293  * reference on its owning user namespace. So both net_ns and pid_ns
294  * will each have their own reference on the owning user namespace.
295  *
296  * If the owning user namespace user_ns1 had a zero reference count then
297  * it also needs to take another reference on its owning user namespace
298  * and so on.
299  */
300 void __ns_ref_active_get(struct ns_common *ns)
301 {
302 	int prev;
303 
304 	/* Initial namespaces are always active. */
305 	if (is_ns_init_id(ns))
306 		return;
307 
308 	/* If we didn't resurrect the namespace we're done. */
309 	prev = atomic_fetch_add(1, &ns->__ns_ref_active);
310 	VFS_WARN_ON_ONCE(prev < 0);
311 	if (likely(prev))
312 		return;
313 
314 	/*
315 	 * We did resurrect it. Walk the ownership hierarchy upwards
316 	 * until we found an owning user namespace that is active.
317 	 */
318 	for (;;) {
319 		ns = ns_owner(ns);
320 		if (!ns)
321 			return;
322 
323 		VFS_WARN_ON_ONCE(is_ns_init_id(ns));
324 		prev = atomic_fetch_add(1, &ns->__ns_ref_active);
325 		VFS_WARN_ON_ONCE(prev < 0);
326 		if (likely(prev))
327 			return;
328 	}
329 }
330 
331 bool may_see_all_namespaces(void)
332 {
333 	return (task_active_pid_ns(current) == &init_pid_ns) &&
334 	       ns_capable_noaudit(init_pid_ns.user_ns, CAP_SYS_ADMIN);
335 }
336