xref: /linux/kernel/nstree.c (revision 4c8b94e59b5f5bfdc0b36d080b6481c9d4ee3baf)
1 // SPDX-License-Identifier: GPL-2.0-only
2 /* Copyright (c) 2025 Christian Brauner <brauner@kernel.org> */
3 
4 #include <linux/nstree.h>
5 #include <linux/proc_ns.h>
6 #include <linux/rculist.h>
7 #include <linux/vfsdebug.h>
8 #include <linux/syscalls.h>
9 #include <linux/user_namespace.h>
10 
11 static __cacheline_aligned_in_smp DEFINE_SEQLOCK(ns_tree_lock);
12 
13 DEFINE_LOCK_GUARD_0(ns_tree_writer,
14 		    write_seqlock(&ns_tree_lock),
15 		    write_sequnlock(&ns_tree_lock))
16 
17 DEFINE_LOCK_GUARD_0(ns_tree_locked_reader,
18 		    read_seqlock_excl(&ns_tree_lock),
19 		    read_sequnlock_excl(&ns_tree_lock))
20 
21 static struct ns_tree_root ns_unified_root = { /* protected by ns_tree_lock */
22 	.ns_rb = RB_ROOT,
23 	.ns_list_head = LIST_HEAD_INIT(ns_unified_root.ns_list_head),
24 };
25 
26 struct ns_tree_root mnt_ns_tree = {
27 	.ns_rb = RB_ROOT,
28 	.ns_list_head = LIST_HEAD_INIT(mnt_ns_tree.ns_list_head),
29 };
30 
31 struct ns_tree_root net_ns_tree = {
32 	.ns_rb = RB_ROOT,
33 	.ns_list_head = LIST_HEAD_INIT(net_ns_tree.ns_list_head),
34 };
35 EXPORT_SYMBOL_GPL(net_ns_tree);
36 
37 struct ns_tree_root uts_ns_tree = {
38 	.ns_rb = RB_ROOT,
39 	.ns_list_head = LIST_HEAD_INIT(uts_ns_tree.ns_list_head),
40 };
41 
42 struct ns_tree_root user_ns_tree = {
43 	.ns_rb = RB_ROOT,
44 	.ns_list_head = LIST_HEAD_INIT(user_ns_tree.ns_list_head),
45 };
46 
47 struct ns_tree_root ipc_ns_tree = {
48 	.ns_rb = RB_ROOT,
49 	.ns_list_head = LIST_HEAD_INIT(ipc_ns_tree.ns_list_head),
50 };
51 
52 struct ns_tree_root pid_ns_tree = {
53 	.ns_rb = RB_ROOT,
54 	.ns_list_head = LIST_HEAD_INIT(pid_ns_tree.ns_list_head),
55 };
56 
57 struct ns_tree_root cgroup_ns_tree = {
58 	.ns_rb = RB_ROOT,
59 	.ns_list_head = LIST_HEAD_INIT(cgroup_ns_tree.ns_list_head),
60 };
61 
62 struct ns_tree_root time_ns_tree = {
63 	.ns_rb = RB_ROOT,
64 	.ns_list_head = LIST_HEAD_INIT(time_ns_tree.ns_list_head),
65 };
66 
67 /**
68  * ns_tree_node_init - Initialize a namespace tree node
69  * @node: The node to initialize
70  *
71  * Initializes both the rbtree node and list entry.
72  */
73 void ns_tree_node_init(struct ns_tree_node *node)
74 {
75 	RB_CLEAR_NODE(&node->ns_node);
76 	INIT_LIST_HEAD(&node->ns_list_entry);
77 }
78 
79 /**
80  * ns_tree_root_init - Initialize a namespace tree root
81  * @root: The root to initialize
82  *
83  * Initializes both the rbtree root and list head.
84  */
85 void ns_tree_root_init(struct ns_tree_root *root)
86 {
87 	root->ns_rb = RB_ROOT;
88 	INIT_LIST_HEAD(&root->ns_list_head);
89 }
90 
91 /**
92  * ns_tree_node_empty - Check if a namespace tree node is empty
93  * @node: The node to check
94  *
95  * Returns true if the node is not in any tree.
96  */
97 bool ns_tree_node_empty(const struct ns_tree_node *node)
98 {
99 	return RB_EMPTY_NODE(&node->ns_node);
100 }
101 
102 /**
103  * ns_tree_node_add - Add a node to a namespace tree
104  * @node: The node to add
105  * @root: The tree root to add to
106  * @cmp: Comparison function for rbtree insertion
107  *
108  * Adds the node to both the rbtree and the list, maintaining sorted order.
109  * The list is maintained in the same order as the rbtree to enable efficient
110  * iteration.
111  *
112  * Returns: NULL if insertion succeeded, existing node if duplicate found
113  */
114 struct rb_node *ns_tree_node_add(struct ns_tree_node *node,
115 				  struct ns_tree_root *root,
116 				  int (*cmp)(struct rb_node *, const struct rb_node *))
117 {
118 	struct rb_node *ret, *prev;
119 
120 	/* Add to rbtree */
121 	ret = rb_find_add_rcu(&node->ns_node, &root->ns_rb, cmp);
122 
123 	/* Add to list in sorted order */
124 	prev = rb_prev(&node->ns_node);
125 	if (!prev) {
126 		/* No previous node, add at head */
127 		list_add_rcu(&node->ns_list_entry, &root->ns_list_head);
128 	} else {
129 		/* Add after previous node */
130 		struct ns_tree_node *prev_node;
131 		prev_node = rb_entry(prev, struct ns_tree_node, ns_node);
132 		list_add_rcu(&node->ns_list_entry, &prev_node->ns_list_entry);
133 	}
134 
135 	return ret;
136 }
137 
138 /**
139  * ns_tree_node_del - Remove a node from a namespace tree
140  * @node: The node to remove
141  * @root: The tree root to remove from
142  *
143  * Removes the node from both the rbtree and the list atomically.
144  */
145 void ns_tree_node_del(struct ns_tree_node *node, struct ns_tree_root *root)
146 {
147 	rb_erase(&node->ns_node, &root->ns_rb);
148 	RB_CLEAR_NODE(&node->ns_node);
149 	list_bidir_del_rcu(&node->ns_list_entry);
150 }
151 
152 static inline struct ns_common *node_to_ns(const struct rb_node *node)
153 {
154 	if (!node)
155 		return NULL;
156 	return rb_entry(node, struct ns_common, ns_tree_node.ns_node);
157 }
158 
159 static inline struct ns_common *node_to_ns_unified(const struct rb_node *node)
160 {
161 	if (!node)
162 		return NULL;
163 	return rb_entry(node, struct ns_common, ns_unified_node.ns_node);
164 }
165 
166 static inline struct ns_common *node_to_ns_owner(const struct rb_node *node)
167 {
168 	if (!node)
169 		return NULL;
170 	return rb_entry(node, struct ns_common, ns_owner_node.ns_node);
171 }
172 
173 static int ns_id_cmp(u64 id_a, u64 id_b)
174 {
175 	if (id_a < id_b)
176 		return -1;
177 	if (id_a > id_b)
178 		return 1;
179 	return 0;
180 }
181 
182 static int ns_cmp(struct rb_node *a, const struct rb_node *b)
183 {
184 	return ns_id_cmp(node_to_ns(a)->ns_id, node_to_ns(b)->ns_id);
185 }
186 
187 static int ns_cmp_unified(struct rb_node *a, const struct rb_node *b)
188 {
189 	return ns_id_cmp(node_to_ns_unified(a)->ns_id, node_to_ns_unified(b)->ns_id);
190 }
191 
192 static int ns_cmp_owner(struct rb_node *a, const struct rb_node *b)
193 {
194 	return ns_id_cmp(node_to_ns_owner(a)->ns_id, node_to_ns_owner(b)->ns_id);
195 }
196 
197 void __ns_tree_add(struct ns_common *ns, struct ns_tree_root *ns_tree)
198 {
199 	struct rb_node *node;
200 	const struct proc_ns_operations *ops = ns->ops;
201 
202 	VFS_WARN_ON_ONCE(!ns->ns_id);
203 
204 	guard(ns_tree_writer)();
205 
206 	/* Add to per-type tree and list */
207 	node = ns_tree_node_add(&ns->ns_tree_node, ns_tree, ns_cmp);
208 
209 	/* Add to unified tree and list */
210 	ns_tree_node_add(&ns->ns_unified_node, &ns_unified_root, ns_cmp_unified);
211 
212 	/* Add to owner's tree if applicable */
213 	if (ops) {
214 		struct user_namespace *user_ns;
215 
216 		VFS_WARN_ON_ONCE(!ops->owner);
217 		user_ns = ops->owner(ns);
218 		if (user_ns) {
219 			struct ns_common *owner = &user_ns->ns;
220 			VFS_WARN_ON_ONCE(owner->ns_type != CLONE_NEWUSER);
221 
222 			/* Insert into owner's tree and list */
223 			ns_tree_node_add(&ns->ns_owner_node, &owner->ns_owner_root, ns_cmp_owner);
224 		} else {
225 			/* Only the initial user namespace doesn't have an owner. */
226 			VFS_WARN_ON_ONCE(ns != to_ns_common(&init_user_ns));
227 		}
228 	}
229 
230 	VFS_WARN_ON_ONCE(node);
231 }
232 
233 void __ns_tree_remove(struct ns_common *ns, struct ns_tree_root *ns_tree)
234 {
235 	const struct proc_ns_operations *ops = ns->ops;
236 	struct user_namespace *user_ns;
237 
238 	VFS_WARN_ON_ONCE(ns_tree_node_empty(&ns->ns_tree_node));
239 	VFS_WARN_ON_ONCE(list_empty(&ns->ns_tree_node.ns_list_entry));
240 
241 	write_seqlock(&ns_tree_lock);
242 
243 	/* Remove from per-type tree and list */
244 	ns_tree_node_del(&ns->ns_tree_node, ns_tree);
245 
246 	/* Remove from unified tree and list */
247 	ns_tree_node_del(&ns->ns_unified_node, &ns_unified_root);
248 
249 	/* Remove from owner's tree if applicable */
250 	if (ops) {
251 		user_ns = ops->owner(ns);
252 		if (user_ns) {
253 			struct ns_common *owner = &user_ns->ns;
254 			ns_tree_node_del(&ns->ns_owner_node, &owner->ns_owner_root);
255 		}
256 	}
257 
258 	write_sequnlock(&ns_tree_lock);
259 }
260 EXPORT_SYMBOL_GPL(__ns_tree_remove);
261 
262 static int ns_find(const void *key, const struct rb_node *node)
263 {
264 	const u64 ns_id = *(u64 *)key;
265 	const struct ns_common *ns = node_to_ns(node);
266 
267 	if (ns_id < ns->ns_id)
268 		return -1;
269 	if (ns_id > ns->ns_id)
270 		return 1;
271 	return 0;
272 }
273 
274 static int ns_find_unified(const void *key, const struct rb_node *node)
275 {
276 	const u64 ns_id = *(u64 *)key;
277 	const struct ns_common *ns = node_to_ns_unified(node);
278 
279 	if (ns_id < ns->ns_id)
280 		return -1;
281 	if (ns_id > ns->ns_id)
282 		return 1;
283 	return 0;
284 }
285 
286 static struct ns_tree_root *ns_tree_from_type(int ns_type)
287 {
288 	switch (ns_type) {
289 	case CLONE_NEWCGROUP:
290 		return &cgroup_ns_tree;
291 	case CLONE_NEWIPC:
292 		return &ipc_ns_tree;
293 	case CLONE_NEWNS:
294 		return &mnt_ns_tree;
295 	case CLONE_NEWNET:
296 		return &net_ns_tree;
297 	case CLONE_NEWPID:
298 		return &pid_ns_tree;
299 	case CLONE_NEWUSER:
300 		return &user_ns_tree;
301 	case CLONE_NEWUTS:
302 		return &uts_ns_tree;
303 	case CLONE_NEWTIME:
304 		return &time_ns_tree;
305 	}
306 
307 	return NULL;
308 }
309 
310 static struct ns_common *__ns_unified_tree_lookup_rcu(u64 ns_id)
311 {
312 	struct rb_node *node;
313 	unsigned int seq;
314 
315 	do {
316 		seq = read_seqbegin(&ns_tree_lock);
317 		node = rb_find_rcu(&ns_id, &ns_unified_root.ns_rb, ns_find_unified);
318 		if (node)
319 			break;
320 	} while (read_seqretry(&ns_tree_lock, seq));
321 
322 	return node_to_ns_unified(node);
323 }
324 
325 static struct ns_common *__ns_tree_lookup_rcu(u64 ns_id, int ns_type)
326 {
327 	struct ns_tree_root *ns_tree;
328 	struct rb_node *node;
329 	unsigned int seq;
330 
331 	ns_tree = ns_tree_from_type(ns_type);
332 	if (!ns_tree)
333 		return NULL;
334 
335 	do {
336 		seq = read_seqbegin(&ns_tree_lock);
337 		node = rb_find_rcu(&ns_id, &ns_tree->ns_rb, ns_find);
338 		if (node)
339 			break;
340 	} while (read_seqretry(&ns_tree_lock, seq));
341 
342 	return node_to_ns(node);
343 }
344 
345 struct ns_common *ns_tree_lookup_rcu(u64 ns_id, int ns_type)
346 {
347 	RCU_LOCKDEP_WARN(!rcu_read_lock_held(), "suspicious ns_tree_lookup_rcu() usage");
348 
349 	if (ns_type)
350 		return __ns_tree_lookup_rcu(ns_id, ns_type);
351 
352 	return __ns_unified_tree_lookup_rcu(ns_id);
353 }
354 
355 /**
356  * __ns_tree_adjoined_rcu - find the next/previous namespace in the same
357  * tree
358  * @ns: namespace to start from
359  * @ns_tree: namespace tree to search in
360  * @previous: if true find the previous namespace, otherwise the next
361  *
362  * Find the next or previous namespace in the same tree as @ns. If
363  * there is no next/previous namespace, -ENOENT is returned.
364  */
365 struct ns_common *__ns_tree_adjoined_rcu(struct ns_common *ns,
366 					 struct ns_tree_root *ns_tree, bool previous)
367 {
368 	struct list_head *list;
369 
370 	RCU_LOCKDEP_WARN(!rcu_read_lock_held(), "suspicious ns_tree_adjoined_rcu() usage");
371 
372 	if (previous)
373 		list = rcu_dereference(list_bidir_prev_rcu(&ns->ns_tree_node.ns_list_entry));
374 	else
375 		list = rcu_dereference(list_next_rcu(&ns->ns_tree_node.ns_list_entry));
376 	if (list_is_head(list, &ns_tree->ns_list_head))
377 		return ERR_PTR(-ENOENT);
378 
379 	return list_entry_rcu(list, struct ns_common, ns_tree_node.ns_list_entry);
380 }
381 
382 /**
383  * __ns_tree_gen_id - generate a new namespace id
384  * @ns: namespace to generate id for
385  * @id: if non-zero, this is the initial namespace and this is a fixed id
386  *
387  * Generates a new namespace id and assigns it to the namespace. All
388  * namespaces types share the same id space and thus can be compared
389  * directly. IOW, when two ids of two namespace are equal, they are
390  * identical.
391  */
392 u64 __ns_tree_gen_id(struct ns_common *ns, u64 id)
393 {
394 	static atomic64_t namespace_cookie = ATOMIC64_INIT(NS_LAST_INIT_ID + 1);
395 
396 	if (id)
397 		ns->ns_id = id;
398 	else
399 		ns->ns_id = atomic64_inc_return(&namespace_cookie);
400 	return ns->ns_id;
401 }
402 
403 struct klistns {
404 	u64 __user *uns_ids;
405 	u32 nr_ns_ids;
406 	u64 last_ns_id;
407 	u64 user_ns_id;
408 	u32 ns_type;
409 	struct user_namespace *user_ns;
410 	bool userns_capable;
411 };
412 
413 static void __free_klistns_free(const struct klistns *kls)
414 {
415 	if (kls->user_ns_id != LISTNS_CURRENT_USER)
416 		put_user_ns(kls->user_ns);
417 }
418 
419 #define NS_ALL (PID_NS | USER_NS | MNT_NS | UTS_NS | IPC_NS | NET_NS | CGROUP_NS | TIME_NS)
420 
421 static int copy_ns_id_req(const struct ns_id_req __user *req,
422 			  struct ns_id_req *kreq)
423 {
424 	int ret;
425 	size_t usize;
426 
427 	BUILD_BUG_ON(sizeof(struct ns_id_req) != NS_ID_REQ_SIZE_VER0);
428 
429 	ret = get_user(usize, &req->size);
430 	if (ret)
431 		return -EFAULT;
432 	if (unlikely(usize > PAGE_SIZE))
433 		return -E2BIG;
434 	if (unlikely(usize < NS_ID_REQ_SIZE_VER0))
435 		return -EINVAL;
436 	memset(kreq, 0, sizeof(*kreq));
437 	ret = copy_struct_from_user(kreq, sizeof(*kreq), req, usize);
438 	if (ret)
439 		return ret;
440 	if (kreq->spare != 0)
441 		return -EINVAL;
442 	if (kreq->ns_type & ~NS_ALL)
443 		return -EOPNOTSUPP;
444 	return 0;
445 }
446 
447 static inline int prepare_klistns(struct klistns *kls, struct ns_id_req *kreq,
448 				  u64 __user *ns_ids, size_t nr_ns_ids)
449 {
450 	kls->last_ns_id = kreq->ns_id;
451 	kls->user_ns_id = kreq->user_ns_id;
452 	kls->nr_ns_ids	= nr_ns_ids;
453 	kls->ns_type	= kreq->ns_type;
454 	kls->uns_ids	= ns_ids;
455 	return 0;
456 }
457 
458 /*
459  * Lookup a namespace owned by owner with id > ns_id.
460  * Returns the namespace with the smallest id that is > ns_id.
461  *
462  * No reference is taken. The caller must hold rcu_read_lock() and may only
463  * use the result as the starting point of an RCU list walk.
464  */
465 static struct ns_common *lookup_ns_owner_after(u64 ns_id, struct ns_common *owner)
466 {
467 	struct ns_common *ret = NULL;
468 	struct rb_node *node;
469 
470 	VFS_WARN_ON_ONCE(owner->ns_type != CLONE_NEWUSER);
471 
472 	guard(ns_tree_locked_reader)();
473 
474 	node = owner->ns_owner_root.ns_rb.rb_node;
475 	while (node) {
476 		struct ns_common *ns;
477 
478 		ns = node_to_ns_owner(node);
479 		if (ns_id < ns->ns_id) {
480 			ret = ns;
481 			node = node->rb_left;
482 		} else {
483 			node = node->rb_right;
484 		}
485 	}
486 
487 	return ret;
488 }
489 
490 static struct ns_common *lookup_ns_id(u64 mnt_ns_id, int ns_type)
491 {
492 	struct ns_common *ns;
493 
494 	guard(rcu)();
495 	ns = ns_tree_lookup_rcu(mnt_ns_id, ns_type);
496 	if (!ns)
497 		return NULL;
498 
499 	if (!ns_get_unless_inactive(ns))
500 		return NULL;
501 
502 	return ns;
503 }
504 
505 static inline bool __must_check ns_requested(const struct klistns *kls,
506 					     const struct ns_common *ns)
507 {
508 	return !kls->ns_type || (kls->ns_type & ns->ns_type);
509 }
510 
511 static inline bool __must_check may_list_ns(const struct klistns *kls,
512 					    struct ns_common *ns)
513 {
514 	if (kls->user_ns && kls->userns_capable)
515 		return true;
516 	if (is_current_namespace(ns))
517 		return true;
518 	return may_see_all_namespaces();
519 }
520 
521 static inline void ns_put(struct ns_common *ns)
522 {
523 	if (ns && ns->ops)
524 		ns->ops->put(ns);
525 }
526 
527 DEFINE_FREE(ns_put, struct ns_common *, if (!IS_ERR_OR_NULL(_T)) ns_put(_T))
528 
529 static inline struct ns_common *__must_check legitimize_ns(const struct klistns *kls,
530 							   struct ns_common *candidate)
531 {
532 	if (!ns_requested(kls, candidate))
533 		return NULL;
534 
535 	if (!may_list_ns(kls, candidate))
536 		return NULL;
537 
538 	return ns_get_unless_inactive(candidate);
539 }
540 
541 static ssize_t do_listns_userns(struct klistns *kls)
542 {
543 	u64 __user *ns_ids = kls->uns_ids;
544 	size_t nr_ns_ids = kls->nr_ns_ids;
545 	struct ns_common *ns = NULL, *first_ns = NULL, *prev = NULL;
546 	const struct list_head *head;
547 	ssize_t ret;
548 
549 	VFS_WARN_ON_ONCE(!kls->user_ns_id);
550 
551 	if (kls->user_ns_id == LISTNS_CURRENT_USER)
552 		ns = to_ns_common(current_user_ns());
553 	else if (kls->user_ns_id)
554 		ns = lookup_ns_id(kls->user_ns_id, CLONE_NEWUSER);
555 	if (!ns)
556 		return -EINVAL;
557 	kls->user_ns = to_user_ns(ns);
558 
559 	ret = 0;
560 	head = &to_ns_common(kls->user_ns)->ns_owner_root.ns_list_head;
561 	kls->userns_capable = may_see_all_namespaces();
562 
563 	rcu_read_lock();
564 
565 	/*
566 	 * Use the rbtree to find the first namespace we care about and
567 	 * then use it's list entry to iterate from there. That namespace
568 	 * is filtered like any other, so the walk doesn't end early just
569 	 * because it is inactive or already dying.
570 	 */
571 	if (kls->last_ns_id) {
572 		first_ns = lookup_ns_owner_after(kls->last_ns_id, ns);
573 		if (!first_ns) {
574 			rcu_read_unlock();
575 			return -ENOENT;
576 		}
577 	} else {
578 		first_ns = list_entry_rcu(head->next, typeof(*first_ns), ns_owner_node.ns_list_entry);
579 	}
580 
581 	ns = first_ns;
582 	list_for_each_entry_from_rcu(ns, head, ns_owner_node.ns_list_entry) {
583 		struct ns_common *valid;
584 
585 		if (!nr_ns_ids)
586 			break;
587 
588 		valid = legitimize_ns(kls, ns);
589 		if (!valid)
590 			continue;
591 
592 		rcu_read_unlock();
593 
594 		ns_put(prev);
595 		prev = valid;
596 
597 		if (put_user(valid->ns_id, ns_ids + ret)) {
598 			ns_put(prev);
599 			return -EFAULT;
600 		}
601 
602 		nr_ns_ids--;
603 		ret++;
604 
605 		rcu_read_lock();
606 	}
607 
608 	rcu_read_unlock();
609 	ns_put(prev);
610 	return ret;
611 }
612 
613 /*
614  * Lookup a namespace with id > ns_id in either the unified tree or a type-specific tree.
615  * Returns the namespace with the smallest id that is > ns_id.
616  *
617  * No reference is taken. The caller must hold rcu_read_lock() and may only
618  * use the result as the starting point of an RCU list walk.
619  */
620 static struct ns_common *lookup_ns_id_after(u64 ns_id, int ns_type)
621 {
622 	struct ns_common *ret = NULL;
623 	struct ns_tree_root *ns_tree = NULL;
624 	struct rb_node *node;
625 
626 	if (ns_type) {
627 		ns_tree = ns_tree_from_type(ns_type);
628 		if (!ns_tree)
629 			return NULL;
630 	}
631 
632 	guard(ns_tree_locked_reader)();
633 
634 	if (ns_tree)
635 		node = ns_tree->ns_rb.rb_node;
636 	else
637 		node = ns_unified_root.ns_rb.rb_node;
638 
639 	while (node) {
640 		struct ns_common *ns;
641 
642 		if (ns_type)
643 			ns = node_to_ns(node);
644 		else
645 			ns = node_to_ns_unified(node);
646 
647 		if (ns_id < ns->ns_id) {
648 			ret = ns;
649 			node = node->rb_left;
650 		} else {
651 			node = node->rb_right;
652 		}
653 	}
654 
655 	return ret;
656 }
657 
658 static inline struct ns_common *first_ns_common(const struct list_head *head,
659 						struct ns_tree_root *ns_tree)
660 {
661 	if (ns_tree)
662 		return list_entry_rcu(head->next, struct ns_common, ns_tree_node.ns_list_entry);
663 	return list_entry_rcu(head->next, struct ns_common, ns_unified_node.ns_list_entry);
664 }
665 
666 static inline struct ns_common *next_ns_common(struct ns_common *ns,
667 					       struct ns_tree_root *ns_tree)
668 {
669 	if (ns_tree)
670 		return list_entry_rcu(ns->ns_tree_node.ns_list_entry.next, struct ns_common, ns_tree_node.ns_list_entry);
671 	return list_entry_rcu(ns->ns_unified_node.ns_list_entry.next, struct ns_common, ns_unified_node.ns_list_entry);
672 }
673 
674 static inline bool ns_common_is_head(struct ns_common *ns,
675 				     const struct list_head *head,
676 				     struct ns_tree_root *ns_tree)
677 {
678 	if (ns_tree)
679 		return &ns->ns_tree_node.ns_list_entry == head;
680 	return &ns->ns_unified_node.ns_list_entry == head;
681 }
682 
683 static ssize_t do_listns(struct klistns *kls)
684 {
685 	u64 __user *ns_ids = kls->uns_ids;
686 	size_t nr_ns_ids = kls->nr_ns_ids;
687 	struct ns_common *ns, *first_ns = NULL, *prev = NULL;
688 	struct ns_tree_root *ns_tree = NULL;
689 	const struct list_head *head;
690 	u32 ns_type;
691 	ssize_t ret;
692 
693 	if (hweight32(kls->ns_type) == 1)
694 		ns_type = kls->ns_type;
695 	else
696 		ns_type = 0;
697 
698 	if (ns_type) {
699 		ns_tree = ns_tree_from_type(ns_type);
700 		if (!ns_tree)
701 			return -EINVAL;
702 	}
703 
704 	ret = 0;
705 	if (ns_tree)
706 		head = &ns_tree->ns_list_head;
707 	else
708 		head = &ns_unified_root.ns_list_head;
709 
710 	rcu_read_lock();
711 
712 	if (kls->last_ns_id) {
713 		first_ns = lookup_ns_id_after(kls->last_ns_id, ns_type);
714 		if (!first_ns) {
715 			rcu_read_unlock();
716 			return -ENOENT;
717 		}
718 	} else {
719 		first_ns = first_ns_common(head, ns_tree);
720 	}
721 
722 	for (ns = first_ns; !ns_common_is_head(ns, head, ns_tree) && nr_ns_ids;
723 	     ns = next_ns_common(ns, ns_tree)) {
724 		struct ns_common *valid;
725 
726 		valid = legitimize_ns(kls, ns);
727 		if (!valid)
728 			continue;
729 
730 		rcu_read_unlock();
731 
732 		ns_put(prev);
733 		prev = valid;
734 
735 		if (put_user(valid->ns_id, ns_ids + ret)) {
736 			ns_put(prev);
737 			return -EFAULT;
738 		}
739 
740 		nr_ns_ids--;
741 		ret++;
742 
743 		rcu_read_lock();
744 	}
745 
746 	rcu_read_unlock();
747 	ns_put(prev);
748 	return ret;
749 }
750 
751 SYSCALL_DEFINE4(listns, const struct ns_id_req __user *, req,
752 		u64 __user *, ns_ids, size_t, nr_ns_ids, unsigned int, flags)
753 {
754 	struct klistns klns __free(klistns_free) = {};
755 	const size_t maxcount = 1000000;
756 	struct ns_id_req kreq;
757 	ssize_t ret;
758 
759 	if (flags)
760 		return -EINVAL;
761 
762 	if (unlikely(nr_ns_ids > maxcount))
763 		return -EOVERFLOW;
764 
765 	if (!access_ok(ns_ids, nr_ns_ids * sizeof(*ns_ids)))
766 		return -EFAULT;
767 
768 	ret = copy_ns_id_req(req, &kreq);
769 	if (ret)
770 		return ret;
771 
772 	ret = prepare_klistns(&klns, &kreq, ns_ids, nr_ns_ids);
773 	if (ret)
774 		return ret;
775 
776 	if (kreq.user_ns_id)
777 		return do_listns_userns(&klns);
778 
779 	return do_listns(&klns);
780 }
781