• 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 #ifdef CONFIG_INOTIFY_USER
50 	UCOUNT_INOTIFY_INSTANCES,
51 	UCOUNT_INOTIFY_WATCHES,
52 #endif
53 	UCOUNT_COUNTS,
54 };
55 
56 struct user_namespace {
57 	struct uid_gid_map	uid_map;
58 	struct uid_gid_map	gid_map;
59 	struct uid_gid_map	projid_map;
60 	atomic_t		count;
61 	struct user_namespace	*parent;
62 	int			level;
63 	kuid_t			owner;
64 	kgid_t			group;
65 	struct ns_common	ns;
66 	unsigned long		flags;
67 
68 #ifdef CONFIG_KEYS
69 	/* List of joinable keyrings in this namespace.  Modification access of
70 	 * these pointers is controlled by keyring_sem.  Once
71 	 * user_keyring_register is set, it won't be changed, so it can be
72 	 * accessed directly with READ_ONCE().
73 	 */
74 	struct list_head	keyring_name_list;
75 	struct key		*user_keyring_register;
76 	struct rw_semaphore	keyring_sem;
77 #endif
78 
79 	/* Register of per-UID persistent keyrings for this namespace */
80 #ifdef CONFIG_PERSISTENT_KEYRINGS
81 	struct key		*persistent_keyring_register;
82 #endif
83 	struct work_struct	work;
84 #ifdef CONFIG_SYSCTL
85 	struct ctl_table_set	set;
86 	struct ctl_table_header *sysctls;
87 #endif
88 	struct ucounts		*ucounts;
89 	int ucount_max[UCOUNT_COUNTS];
90 
91 	ANDROID_KABI_RESERVE(1);
92 	ANDROID_KABI_RESERVE(2);
93 } __randomize_layout;
94 
95 struct ucounts {
96 	struct hlist_node node;
97 	struct user_namespace *ns;
98 	kuid_t uid;
99 	int count;
100 	atomic_t ucount[UCOUNT_COUNTS];
101 };
102 
103 extern struct user_namespace init_user_ns;
104 
105 bool setup_userns_sysctls(struct user_namespace *ns);
106 void retire_userns_sysctls(struct user_namespace *ns);
107 struct ucounts *inc_ucount(struct user_namespace *ns, kuid_t uid, enum ucount_type type);
108 void dec_ucount(struct ucounts *ucounts, enum ucount_type type);
109 
110 #ifdef CONFIG_USER_NS
111 
get_user_ns(struct user_namespace * ns)112 static inline struct user_namespace *get_user_ns(struct user_namespace *ns)
113 {
114 	if (ns)
115 		atomic_inc(&ns->count);
116 	return ns;
117 }
118 
119 extern int create_user_ns(struct cred *new);
120 extern int unshare_userns(unsigned long unshare_flags, struct cred **new_cred);
121 extern void __put_user_ns(struct user_namespace *ns);
122 
put_user_ns(struct user_namespace * ns)123 static inline void put_user_ns(struct user_namespace *ns)
124 {
125 	if (ns && atomic_dec_and_test(&ns->count))
126 		__put_user_ns(ns);
127 }
128 
129 struct seq_operations;
130 extern const struct seq_operations proc_uid_seq_operations;
131 extern const struct seq_operations proc_gid_seq_operations;
132 extern const struct seq_operations proc_projid_seq_operations;
133 extern ssize_t proc_uid_map_write(struct file *, const char __user *, size_t, loff_t *);
134 extern ssize_t proc_gid_map_write(struct file *, const char __user *, size_t, loff_t *);
135 extern ssize_t proc_projid_map_write(struct file *, const char __user *, size_t, loff_t *);
136 extern ssize_t proc_setgroups_write(struct file *, const char __user *, size_t, loff_t *);
137 extern int proc_setgroups_show(struct seq_file *m, void *v);
138 extern bool userns_may_setgroups(const struct user_namespace *ns);
139 extern bool in_userns(const struct user_namespace *ancestor,
140 		       const struct user_namespace *child);
141 extern bool current_in_userns(const struct user_namespace *target_ns);
142 struct ns_common *ns_get_owner(struct ns_common *ns);
143 #else
144 
get_user_ns(struct user_namespace * ns)145 static inline struct user_namespace *get_user_ns(struct user_namespace *ns)
146 {
147 	return &init_user_ns;
148 }
149 
create_user_ns(struct cred * new)150 static inline int create_user_ns(struct cred *new)
151 {
152 	return -EINVAL;
153 }
154 
unshare_userns(unsigned long unshare_flags,struct cred ** new_cred)155 static inline int unshare_userns(unsigned long unshare_flags,
156 				 struct cred **new_cred)
157 {
158 	if (unshare_flags & CLONE_NEWUSER)
159 		return -EINVAL;
160 	return 0;
161 }
162 
put_user_ns(struct user_namespace * ns)163 static inline void put_user_ns(struct user_namespace *ns)
164 {
165 }
166 
userns_may_setgroups(const struct user_namespace * ns)167 static inline bool userns_may_setgroups(const struct user_namespace *ns)
168 {
169 	return true;
170 }
171 
in_userns(const struct user_namespace * ancestor,const struct user_namespace * child)172 static inline bool in_userns(const struct user_namespace *ancestor,
173 			     const struct user_namespace *child)
174 {
175 	return true;
176 }
177 
current_in_userns(const struct user_namespace * target_ns)178 static inline bool current_in_userns(const struct user_namespace *target_ns)
179 {
180 	return true;
181 }
182 
ns_get_owner(struct ns_common * ns)183 static inline struct ns_common *ns_get_owner(struct ns_common *ns)
184 {
185 	return ERR_PTR(-EPERM);
186 }
187 #endif
188 
189 #endif /* _LINUX_USER_H */
190