xref: /linux-6.15/include/linux/nsproxy.h (revision 85e4daae)
1b2441318SGreg Kroah-Hartman /* SPDX-License-Identifier: GPL-2.0 */
2ab516013SSerge E. Hallyn #ifndef _LINUX_NSPROXY_H
3ab516013SSerge E. Hallyn #define _LINUX_NSPROXY_H
4ab516013SSerge E. Hallyn 
56a2623b1SKent Overstreet #include <linux/refcount.h>
6ab516013SSerge E. Hallyn #include <linux/spinlock.h>
7ab516013SSerge E. Hallyn #include <linux/sched.h>
8ab516013SSerge E. Hallyn 
96b3286edSKirill Korotaev struct mnt_namespace;
104865ecf1SSerge E. Hallyn struct uts_namespace;
1125b21cb2SKirill Korotaev struct ipc_namespace;
129a575a92SCedric Le Goater struct pid_namespace;
13a79a908fSAditya Kali struct cgroup_namespace;
145ad4e53bSAl Viro struct fs_struct;
151651e14eSSerge E. Hallyn 
16ab516013SSerge E. Hallyn /*
17ab516013SSerge E. Hallyn  * A structure to contain pointers to all per-process
18ab516013SSerge E. Hallyn  * namespaces - fs (mount), uts, network, sysvipc, etc.
19ab516013SSerge E. Hallyn  *
20c2b1df2eSAndy Lutomirski  * The pid namespace is an exception -- it's accessed using
21c2b1df2eSAndy Lutomirski  * task_active_pid_ns.  The pid namespace here is the
22c2b1df2eSAndy Lutomirski  * namespace that children will use.
23c2b1df2eSAndy Lutomirski  *
24ab516013SSerge E. Hallyn  * 'count' is the number of tasks holding a reference.
25ab516013SSerge E. Hallyn  * The count for each namespace, then, will be the number
26ab516013SSerge E. Hallyn  * of nsproxies pointing to it, not the number of tasks.
27ab516013SSerge E. Hallyn  *
28ab516013SSerge E. Hallyn  * The nsproxy is shared by tasks which share all namespaces.
29ab516013SSerge E. Hallyn  * As soon as a single namespace is cloned or unshared, the
30ab516013SSerge E. Hallyn  * nsproxy is copied.
31ab516013SSerge E. Hallyn  */
32ab516013SSerge E. Hallyn struct nsproxy {
332ddd3cacSElena Reshetova 	refcount_t count;
344865ecf1SSerge E. Hallyn 	struct uts_namespace *uts_ns;
3525b21cb2SKirill Korotaev 	struct ipc_namespace *ipc_ns;
366b3286edSKirill Korotaev 	struct mnt_namespace *mnt_ns;
37c2b1df2eSAndy Lutomirski 	struct pid_namespace *pid_ns_for_children;
38772698f6SEric W. Biederman 	struct net 	     *net_ns;
39769071acSAndrei Vagin 	struct time_namespace *time_ns;
40769071acSAndrei Vagin 	struct time_namespace *time_ns_for_children;
41a79a908fSAditya Kali 	struct cgroup_namespace *cgroup_ns;
42ab516013SSerge E. Hallyn };
43ab516013SSerge E. Hallyn extern struct nsproxy init_nsproxy;
44ab516013SSerge E. Hallyn 
45*85e4daaeSChristian Brauner #define to_ns_common(__ns)                              \
46*85e4daaeSChristian Brauner 	_Generic((__ns),                                \
47*85e4daaeSChristian Brauner 		struct cgroup_namespace *: &(__ns->ns), \
48*85e4daaeSChristian Brauner 		struct ipc_namespace *:    &(__ns->ns), \
49*85e4daaeSChristian Brauner 		struct net *:              &(__ns->ns), \
50*85e4daaeSChristian Brauner 		struct pid_namespace *:    &(__ns->ns), \
51*85e4daaeSChristian Brauner 		struct mnt_namespace *:    &(__ns->ns), \
52*85e4daaeSChristian Brauner 		struct time_namespace *:   &(__ns->ns), \
53*85e4daaeSChristian Brauner 		struct user_namespace *:   &(__ns->ns), \
54*85e4daaeSChristian Brauner 		struct uts_namespace *:    &(__ns->ns))
55*85e4daaeSChristian Brauner 
56cf7b708cSPavel Emelyanov /*
57f2a8d52eSChristian Brauner  * A structure to encompass all bits needed to install
58f2a8d52eSChristian Brauner  * a partial or complete new set of namespaces.
59f2a8d52eSChristian Brauner  *
60f2a8d52eSChristian Brauner  * If a new user namespace is requested cred will
61f2a8d52eSChristian Brauner  * point to a modifiable set of credentials. If a pointer
62f2a8d52eSChristian Brauner  * to a modifiable set is needed nsset_cred() must be
63f2a8d52eSChristian Brauner  * used and tested.
64f2a8d52eSChristian Brauner  */
65f2a8d52eSChristian Brauner struct nsset {
66f2a8d52eSChristian Brauner 	unsigned flags;
67f2a8d52eSChristian Brauner 	struct nsproxy *nsproxy;
68f2a8d52eSChristian Brauner 	struct fs_struct *fs;
69f2a8d52eSChristian Brauner 	const struct cred *cred;
70f2a8d52eSChristian Brauner };
71f2a8d52eSChristian Brauner 
nsset_cred(struct nsset * set)72f2a8d52eSChristian Brauner static inline struct cred *nsset_cred(struct nsset *set)
73f2a8d52eSChristian Brauner {
74f2a8d52eSChristian Brauner 	if (set->flags & CLONE_NEWUSER)
75f2a8d52eSChristian Brauner 		return (struct cred *)set->cred;
76f2a8d52eSChristian Brauner 
77f2a8d52eSChristian Brauner 	return NULL;
78f2a8d52eSChristian Brauner }
79f2a8d52eSChristian Brauner 
80f2a8d52eSChristian Brauner /*
81cf7b708cSPavel Emelyanov  * the namespaces access rules are:
82cf7b708cSPavel Emelyanov  *
83cf7b708cSPavel Emelyanov  *  1. only current task is allowed to change tsk->nsproxy pointer or
84728dba3aSEric W. Biederman  *     any pointer on the nsproxy itself.  Current must hold the task_lock
85728dba3aSEric W. Biederman  *     when changing tsk->nsproxy.
86cf7b708cSPavel Emelyanov  *
87cf7b708cSPavel Emelyanov  *  2. when accessing (i.e. reading) current task's namespaces - no
88cf7b708cSPavel Emelyanov  *     precautions should be taken - just dereference the pointers
89cf7b708cSPavel Emelyanov  *
90cf7b708cSPavel Emelyanov  *  3. the access to other task namespaces is performed like this
91728dba3aSEric W. Biederman  *     task_lock(task);
92728dba3aSEric W. Biederman  *     nsproxy = task->nsproxy;
93cf7b708cSPavel Emelyanov  *     if (nsproxy != NULL) {
94cf7b708cSPavel Emelyanov  *             / *
95cf7b708cSPavel Emelyanov  *               * work with the namespaces here
96cf7b708cSPavel Emelyanov  *               * e.g. get the reference on one of them
97cf7b708cSPavel Emelyanov  *               * /
98cf7b708cSPavel Emelyanov  *     } / *
99728dba3aSEric W. Biederman  *         * NULL task->nsproxy means that this task is
100cf7b708cSPavel Emelyanov  *         * almost dead (zombie)
101cf7b708cSPavel Emelyanov  *         * /
102728dba3aSEric W. Biederman  *     task_unlock(task);
103cf7b708cSPavel Emelyanov  *
104cf7b708cSPavel Emelyanov  */
105cf7b708cSPavel Emelyanov 
106213dd266SEric W. Biederman int copy_namespaces(unsigned long flags, struct task_struct *tsk);
107cf7b708cSPavel Emelyanov void exit_task_namespaces(struct task_struct *tsk);
108cf7b708cSPavel Emelyanov void switch_task_namespaces(struct task_struct *tsk, struct nsproxy *new);
1092b5f9dadSAndrei Vagin int exec_task_namespaces(void);
110ab516013SSerge E. Hallyn void free_nsproxy(struct nsproxy *ns);
111e3222c4eSBadari Pulavarty int unshare_nsproxy_namespaces(unsigned long, struct nsproxy **,
112b2e0d987SEric W. Biederman 	struct cred *, struct fs_struct *);
11366577193SAl Viro int __init nsproxy_cache_init(void);
114ab516013SSerge E. Hallyn 
put_nsproxy(struct nsproxy * ns)115444f378bSLinus Torvalds static inline void put_nsproxy(struct nsproxy *ns)
116ab516013SSerge E. Hallyn {
1172ddd3cacSElena Reshetova 	if (refcount_dec_and_test(&ns->count))
118ab516013SSerge E. Hallyn 		free_nsproxy(ns);
119ab516013SSerge E. Hallyn }
120ab516013SSerge E. Hallyn 
get_nsproxy(struct nsproxy * ns)121cf7b708cSPavel Emelyanov static inline void get_nsproxy(struct nsproxy *ns)
122ab516013SSerge E. Hallyn {
1232ddd3cacSElena Reshetova 	refcount_inc(&ns->count);
124ab516013SSerge E. Hallyn }
125858d72eaSSerge E. Hallyn 
126d057c108SChristian Brauner DEFINE_FREE(put_nsproxy, struct nsproxy *, if (_T) put_nsproxy(_T))
127d057c108SChristian Brauner 
128ab516013SSerge E. Hallyn #endif
129