• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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/sched.h>
9 #include <linux/workqueue.h>
10 #include <linux/rwsem.h>
11 #include <linux/sysctl.h>
12 #include <linux/err.h>
13 #include <linux/android_kabi.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 	ANDROID_KABI_RESERVE(1);
117 	ANDROID_KABI_RESERVE(2);
118 } __randomize_layout;
119 
120 struct ucounts {
121 	struct hlist_node node;
122 	struct user_namespace *ns;
123 	kuid_t uid;
124 	atomic_t count;
125 	atomic_long_t ucount[UCOUNT_COUNTS];
126 	atomic_long_t rlimit[UCOUNT_RLIMIT_COUNTS];
127 };
128 
129 extern struct user_namespace init_user_ns;
130 extern struct ucounts init_ucounts;
131 
132 bool setup_userns_sysctls(struct user_namespace *ns);
133 void retire_userns_sysctls(struct user_namespace *ns);
134 struct ucounts *inc_ucount(struct user_namespace *ns, kuid_t uid, enum ucount_type type);
135 void dec_ucount(struct ucounts *ucounts, enum ucount_type type);
136 struct ucounts *alloc_ucounts(struct user_namespace *ns, kuid_t uid);
137 struct ucounts * __must_check get_ucounts(struct ucounts *ucounts);
138 void put_ucounts(struct ucounts *ucounts);
139 
get_rlimit_value(struct ucounts * ucounts,enum rlimit_type type)140 static inline long get_rlimit_value(struct ucounts *ucounts, enum rlimit_type type)
141 {
142 	return atomic_long_read(&ucounts->rlimit[type]);
143 }
144 
145 long inc_rlimit_ucounts(struct ucounts *ucounts, enum rlimit_type type, long v);
146 bool dec_rlimit_ucounts(struct ucounts *ucounts, enum rlimit_type type, long v);
147 long inc_rlimit_get_ucounts(struct ucounts *ucounts, enum rlimit_type type,
148 			    bool override_rlimit);
149 void dec_rlimit_put_ucounts(struct ucounts *ucounts, enum rlimit_type type);
150 bool is_rlimit_overlimit(struct ucounts *ucounts, enum rlimit_type type, unsigned long max);
151 
get_userns_rlimit_max(struct user_namespace * ns,enum rlimit_type type)152 static inline long get_userns_rlimit_max(struct user_namespace *ns, enum rlimit_type type)
153 {
154 	return READ_ONCE(ns->rlimit_max[type]);
155 }
156 
set_userns_rlimit_max(struct user_namespace * ns,enum rlimit_type type,unsigned long max)157 static inline void set_userns_rlimit_max(struct user_namespace *ns,
158 		enum rlimit_type type, unsigned long max)
159 {
160 	ns->rlimit_max[type] = max <= LONG_MAX ? max : LONG_MAX;
161 }
162 
163 #ifdef CONFIG_USER_NS
164 
get_user_ns(struct user_namespace * ns)165 static inline struct user_namespace *get_user_ns(struct user_namespace *ns)
166 {
167 	if (ns)
168 		refcount_inc(&ns->ns.count);
169 	return ns;
170 }
171 
172 extern int create_user_ns(struct cred *new);
173 extern int unshare_userns(unsigned long unshare_flags, struct cred **new_cred);
174 extern void __put_user_ns(struct user_namespace *ns);
175 
put_user_ns(struct user_namespace * ns)176 static inline void put_user_ns(struct user_namespace *ns)
177 {
178 	if (ns && refcount_dec_and_test(&ns->ns.count))
179 		__put_user_ns(ns);
180 }
181 
182 struct seq_operations;
183 extern const struct seq_operations proc_uid_seq_operations;
184 extern const struct seq_operations proc_gid_seq_operations;
185 extern const struct seq_operations proc_projid_seq_operations;
186 extern ssize_t proc_uid_map_write(struct file *, const char __user *, size_t, loff_t *);
187 extern ssize_t proc_gid_map_write(struct file *, const char __user *, size_t, loff_t *);
188 extern ssize_t proc_projid_map_write(struct file *, const char __user *, size_t, loff_t *);
189 extern ssize_t proc_setgroups_write(struct file *, const char __user *, size_t, loff_t *);
190 extern int proc_setgroups_show(struct seq_file *m, void *v);
191 extern bool userns_may_setgroups(const struct user_namespace *ns);
192 extern bool in_userns(const struct user_namespace *ancestor,
193 		       const struct user_namespace *child);
194 extern bool current_in_userns(const struct user_namespace *target_ns);
195 struct ns_common *ns_get_owner(struct ns_common *ns);
196 #else
197 
get_user_ns(struct user_namespace * ns)198 static inline struct user_namespace *get_user_ns(struct user_namespace *ns)
199 {
200 	return &init_user_ns;
201 }
202 
create_user_ns(struct cred * new)203 static inline int create_user_ns(struct cred *new)
204 {
205 	return -EINVAL;
206 }
207 
unshare_userns(unsigned long unshare_flags,struct cred ** new_cred)208 static inline int unshare_userns(unsigned long unshare_flags,
209 				 struct cred **new_cred)
210 {
211 	if (unshare_flags & CLONE_NEWUSER)
212 		return -EINVAL;
213 	return 0;
214 }
215 
put_user_ns(struct user_namespace * ns)216 static inline void put_user_ns(struct user_namespace *ns)
217 {
218 }
219 
userns_may_setgroups(const struct user_namespace * ns)220 static inline bool userns_may_setgroups(const struct user_namespace *ns)
221 {
222 	return true;
223 }
224 
in_userns(const struct user_namespace * ancestor,const struct user_namespace * child)225 static inline bool in_userns(const struct user_namespace *ancestor,
226 			     const struct user_namespace *child)
227 {
228 	return true;
229 }
230 
current_in_userns(const struct user_namespace * target_ns)231 static inline bool current_in_userns(const struct user_namespace *target_ns)
232 {
233 	return true;
234 }
235 
ns_get_owner(struct ns_common * ns)236 static inline struct ns_common *ns_get_owner(struct ns_common *ns)
237 {
238 	return ERR_PTR(-EPERM);
239 }
240 #endif
241 
242 #endif /* _LINUX_USER_H */
243