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