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