xref: /linux-6.15/include/linux/user_namespace.h (revision 5f01a22c)
1 /* SPDX-License-Identifier: GPL-2.0 */
2 #ifndef _LINUX_USER_NAMESPACE_H
3 #define _LINUX_USER_NAMESPACE_H
4 
5 #include <linux/kref.h>
6 #include <linux/nsproxy.h>
7 #include <linux/ns_common.h>
8 #include <linux/rculist_nulls.h>
9 #include <linux/sched.h>
10 #include <linux/workqueue.h>
11 #include <linux/rwsem.h>
12 #include <linux/sysctl.h>
13 #include <linux/err.h>
14 
15 #define UID_GID_MAP_MAX_BASE_EXTENTS 5
16 #define UID_GID_MAP_MAX_EXTENTS 340
17 
18 struct uid_gid_extent {
19 	u32 first;
20 	u32 lower_first;
21 	u32 count;
22 };
23 
24 struct uid_gid_map { /* 64 bytes -- 1 cache line */
25 	union {
26 		struct {
27 			struct uid_gid_extent extent[UID_GID_MAP_MAX_BASE_EXTENTS];
28 			u32 nr_extents;
29 		};
30 		struct {
31 			struct uid_gid_extent *forward;
32 			struct uid_gid_extent *reverse;
33 		};
34 	};
35 };
36 
37 #define USERNS_SETGROUPS_ALLOWED 1UL
38 
39 #define USERNS_INIT_FLAGS USERNS_SETGROUPS_ALLOWED
40 
41 struct ucounts;
42 
43 enum ucount_type {
44 	UCOUNT_USER_NAMESPACES,
45 	UCOUNT_PID_NAMESPACES,
46 	UCOUNT_UTS_NAMESPACES,
47 	UCOUNT_IPC_NAMESPACES,
48 	UCOUNT_NET_NAMESPACES,
49 	UCOUNT_MNT_NAMESPACES,
50 	UCOUNT_CGROUP_NAMESPACES,
51 	UCOUNT_TIME_NAMESPACES,
52 #ifdef CONFIG_INOTIFY_USER
53 	UCOUNT_INOTIFY_INSTANCES,
54 	UCOUNT_INOTIFY_WATCHES,
55 #endif
56 #ifdef CONFIG_FANOTIFY
57 	UCOUNT_FANOTIFY_GROUPS,
58 	UCOUNT_FANOTIFY_MARKS,
59 #endif
60 	UCOUNT_COUNTS,
61 };
62 
63 enum rlimit_type {
64 	UCOUNT_RLIMIT_NPROC,
65 	UCOUNT_RLIMIT_MSGQUEUE,
66 	UCOUNT_RLIMIT_SIGPENDING,
67 	UCOUNT_RLIMIT_MEMLOCK,
68 	UCOUNT_RLIMIT_COUNTS,
69 };
70 
71 #if IS_ENABLED(CONFIG_BINFMT_MISC)
72 struct binfmt_misc;
73 #endif
74 
75 struct user_namespace {
76 	struct uid_gid_map	uid_map;
77 	struct uid_gid_map	gid_map;
78 	struct uid_gid_map	projid_map;
79 	struct user_namespace	*parent;
80 	int			level;
81 	kuid_t			owner;
82 	kgid_t			group;
83 	struct ns_common	ns;
84 	unsigned long		flags;
85 	/* parent_could_setfcap: true if the creator if this ns had CAP_SETFCAP
86 	 * in its effective capability set at the child ns creation time. */
87 	bool			parent_could_setfcap;
88 
89 #ifdef CONFIG_KEYS
90 	/* List of joinable keyrings in this namespace.  Modification access of
91 	 * these pointers is controlled by keyring_sem.  Once
92 	 * user_keyring_register is set, it won't be changed, so it can be
93 	 * accessed directly with READ_ONCE().
94 	 */
95 	struct list_head	keyring_name_list;
96 	struct key		*user_keyring_register;
97 	struct rw_semaphore	keyring_sem;
98 #endif
99 
100 	/* Register of per-UID persistent keyrings for this namespace */
101 #ifdef CONFIG_PERSISTENT_KEYRINGS
102 	struct key		*persistent_keyring_register;
103 #endif
104 	struct work_struct	work;
105 #ifdef CONFIG_SYSCTL
106 	struct ctl_table_set	set;
107 	struct ctl_table_header *sysctls;
108 #endif
109 	struct ucounts		*ucounts;
110 	long ucount_max[UCOUNT_COUNTS];
111 	long rlimit_max[UCOUNT_RLIMIT_COUNTS];
112 
113 #if IS_ENABLED(CONFIG_BINFMT_MISC)
114 	struct binfmt_misc *binfmt_misc;
115 #endif
116 } __randomize_layout;
117 
118 struct ucounts {
119 	struct hlist_nulls_node node;
120 	struct user_namespace *ns;
121 	kuid_t uid;
122 	struct rcu_head rcu;
123 	atomic_t count;
124 	atomic_long_t ucount[UCOUNT_COUNTS];
125 	atomic_long_t rlimit[UCOUNT_RLIMIT_COUNTS];
126 };
127 
128 extern struct user_namespace init_user_ns;
129 extern struct ucounts init_ucounts;
130 
131 bool setup_userns_sysctls(struct user_namespace *ns);
132 void retire_userns_sysctls(struct user_namespace *ns);
133 struct ucounts *inc_ucount(struct user_namespace *ns, kuid_t uid, enum ucount_type type);
134 void dec_ucount(struct ucounts *ucounts, enum ucount_type type);
135 struct ucounts *alloc_ucounts(struct user_namespace *ns, kuid_t uid);
136 struct ucounts * __must_check get_ucounts(struct ucounts *ucounts);
137 void put_ucounts(struct ucounts *ucounts);
138 
139 static inline long get_rlimit_value(struct ucounts *ucounts, enum rlimit_type type)
140 {
141 	return atomic_long_read(&ucounts->rlimit[type]);
142 }
143 
144 long inc_rlimit_ucounts(struct ucounts *ucounts, enum rlimit_type type, long v);
145 bool dec_rlimit_ucounts(struct ucounts *ucounts, enum rlimit_type type, long v);
146 long inc_rlimit_get_ucounts(struct ucounts *ucounts, enum rlimit_type type,
147 			    bool override_rlimit);
148 void dec_rlimit_put_ucounts(struct ucounts *ucounts, enum rlimit_type type);
149 bool is_rlimit_overlimit(struct ucounts *ucounts, enum rlimit_type type, unsigned long max);
150 
151 static inline long get_userns_rlimit_max(struct user_namespace *ns, enum rlimit_type type)
152 {
153 	return READ_ONCE(ns->rlimit_max[type]);
154 }
155 
156 static inline void set_userns_rlimit_max(struct user_namespace *ns,
157 		enum rlimit_type type, unsigned long max)
158 {
159 	ns->rlimit_max[type] = max <= LONG_MAX ? max : LONG_MAX;
160 }
161 
162 #ifdef CONFIG_USER_NS
163 
164 static inline struct user_namespace *get_user_ns(struct user_namespace *ns)
165 {
166 	if (ns)
167 		refcount_inc(&ns->ns.count);
168 	return ns;
169 }
170 
171 extern int create_user_ns(struct cred *new);
172 extern int unshare_userns(unsigned long unshare_flags, struct cred **new_cred);
173 extern void __put_user_ns(struct user_namespace *ns);
174 
175 static inline void put_user_ns(struct user_namespace *ns)
176 {
177 	if (ns && refcount_dec_and_test(&ns->ns.count))
178 		__put_user_ns(ns);
179 }
180 
181 struct seq_operations;
182 extern const struct seq_operations proc_uid_seq_operations;
183 extern const struct seq_operations proc_gid_seq_operations;
184 extern const struct seq_operations proc_projid_seq_operations;
185 extern ssize_t proc_uid_map_write(struct file *, const char __user *, size_t, loff_t *);
186 extern ssize_t proc_gid_map_write(struct file *, const char __user *, size_t, loff_t *);
187 extern ssize_t proc_projid_map_write(struct file *, const char __user *, size_t, loff_t *);
188 extern ssize_t proc_setgroups_write(struct file *, const char __user *, size_t, loff_t *);
189 extern int proc_setgroups_show(struct seq_file *m, void *v);
190 extern bool userns_may_setgroups(const struct user_namespace *ns);
191 extern bool in_userns(const struct user_namespace *ancestor,
192 		       const struct user_namespace *child);
193 extern bool current_in_userns(const struct user_namespace *target_ns);
194 struct ns_common *ns_get_owner(struct ns_common *ns);
195 #else
196 
197 static inline struct user_namespace *get_user_ns(struct user_namespace *ns)
198 {
199 	return &init_user_ns;
200 }
201 
202 static inline int create_user_ns(struct cred *new)
203 {
204 	return -EINVAL;
205 }
206 
207 static inline int unshare_userns(unsigned long unshare_flags,
208 				 struct cred **new_cred)
209 {
210 	if (unshare_flags & CLONE_NEWUSER)
211 		return -EINVAL;
212 	return 0;
213 }
214 
215 static inline void put_user_ns(struct user_namespace *ns)
216 {
217 }
218 
219 static inline bool userns_may_setgroups(const struct user_namespace *ns)
220 {
221 	return true;
222 }
223 
224 static inline bool in_userns(const struct user_namespace *ancestor,
225 			     const struct user_namespace *child)
226 {
227 	return true;
228 }
229 
230 static inline bool current_in_userns(const struct user_namespace *target_ns)
231 {
232 	return true;
233 }
234 
235 static inline struct ns_common *ns_get_owner(struct ns_common *ns)
236 {
237 	return ERR_PTR(-EPERM);
238 }
239 #endif
240 
241 #endif /* _LINUX_USER_H */
242