1 /* SPDX-License-Identifier: GPL-2.0 */
2 #ifndef _LINUX_USER_NAMESPACE_H
3 #define _LINUX_USER_NAMESPACE_H
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>
16 #define UID_GID_MAP_MAX_BASE_EXTENTS 5
17 #define UID_GID_MAP_MAX_EXTENTS 340
19 struct uid_gid_extent {
25 struct uid_gid_map { /* 64 bytes -- 1 cache line */
28 struct uid_gid_extent extent[UID_GID_MAP_MAX_BASE_EXTENTS];
32 struct uid_gid_extent *forward;
33 struct uid_gid_extent *reverse;
38 #define USERNS_SETGROUPS_ALLOWED 1UL
40 #define USERNS_INIT_FLAGS USERNS_SETGROUPS_ALLOWED
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,
57 #ifdef CONFIG_FANOTIFY
58 UCOUNT_FANOTIFY_GROUPS,
59 UCOUNT_FANOTIFY_MARKS,
66 UCOUNT_RLIMIT_MSGQUEUE,
67 UCOUNT_RLIMIT_SIGPENDING,
68 UCOUNT_RLIMIT_MEMLOCK,
72 #if IS_ENABLED(CONFIG_BINFMT_MISC)
76 struct user_namespace {
77 struct uid_gid_map uid_map;
78 struct uid_gid_map gid_map;
79 struct uid_gid_map projid_map;
80 struct user_namespace *parent;
86 /* parent_could_setfcap: true if the creator if this ns had CAP_SETFCAP
87 * in its effective capability set at the child ns creation time. */
88 bool parent_could_setfcap;
91 /* List of joinable keyrings in this namespace. Modification access of
92 * these pointers is controlled by keyring_sem. Once
93 * user_keyring_register is set, it won't be changed, so it can be
94 * accessed directly with READ_ONCE().
96 struct list_head keyring_name_list;
97 struct key *user_keyring_register;
98 struct rw_semaphore keyring_sem;
101 /* Register of per-UID persistent keyrings for this namespace */
102 #ifdef CONFIG_PERSISTENT_KEYRINGS
103 struct key *persistent_keyring_register;
105 struct work_struct work;
107 struct ctl_table_set set;
108 struct ctl_table_header *sysctls;
110 struct ucounts *ucounts;
111 long ucount_max[UCOUNT_COUNTS];
112 long rlimit_max[UCOUNT_RLIMIT_COUNTS];
114 #if IS_ENABLED(CONFIG_BINFMT_MISC)
115 struct binfmt_misc *binfmt_misc;
117 } __randomize_layout;
120 struct hlist_nulls_node node;
121 struct user_namespace *ns;
125 atomic_long_t ucount[UCOUNT_COUNTS];
126 atomic_long_t rlimit[UCOUNT_RLIMIT_COUNTS];
129 extern struct user_namespace init_user_ns;
130 extern struct ucounts init_ucounts;
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 void put_ucounts(struct ucounts *ucounts);
139 static inline struct ucounts * __must_check get_ucounts(struct ucounts *ucounts)
141 if (rcuref_get(&ucounts->count))
146 static inline long get_rlimit_value(struct ucounts *ucounts, enum rlimit_type type)
148 return atomic_long_read(&ucounts->rlimit[type]);
151 long inc_rlimit_ucounts(struct ucounts *ucounts, enum rlimit_type type, long v);
152 bool dec_rlimit_ucounts(struct ucounts *ucounts, enum rlimit_type type, long v);
153 long inc_rlimit_get_ucounts(struct ucounts *ucounts, enum rlimit_type type,
154 bool override_rlimit);
155 void dec_rlimit_put_ucounts(struct ucounts *ucounts, enum rlimit_type type);
156 bool is_rlimit_overlimit(struct ucounts *ucounts, enum rlimit_type type, unsigned long max);
158 static inline long get_userns_rlimit_max(struct user_namespace *ns, enum rlimit_type type)
160 return READ_ONCE(ns->rlimit_max[type]);
163 static inline void set_userns_rlimit_max(struct user_namespace *ns,
164 enum rlimit_type type, unsigned long max)
166 ns->rlimit_max[type] = max <= LONG_MAX ? max : LONG_MAX;
169 #ifdef CONFIG_USER_NS
171 static inline struct user_namespace *get_user_ns(struct user_namespace *ns)
174 refcount_inc(&ns->ns.count);
178 extern int create_user_ns(struct cred *new);
179 extern int unshare_userns(unsigned long unshare_flags, struct cred **new_cred);
180 extern void __put_user_ns(struct user_namespace *ns);
182 static inline void put_user_ns(struct user_namespace *ns)
184 if (ns && refcount_dec_and_test(&ns->ns.count))
188 struct seq_operations;
189 extern const struct seq_operations proc_uid_seq_operations;
190 extern const struct seq_operations proc_gid_seq_operations;
191 extern const struct seq_operations proc_projid_seq_operations;
192 extern ssize_t proc_uid_map_write(struct file *, const char __user *, size_t, loff_t *);
193 extern ssize_t proc_gid_map_write(struct file *, const char __user *, size_t, loff_t *);
194 extern ssize_t proc_projid_map_write(struct file *, const char __user *, size_t, loff_t *);
195 extern ssize_t proc_setgroups_write(struct file *, const char __user *, size_t, loff_t *);
196 extern int proc_setgroups_show(struct seq_file *m, void *v);
197 extern bool userns_may_setgroups(const struct user_namespace *ns);
198 extern bool in_userns(const struct user_namespace *ancestor,
199 const struct user_namespace *child);
200 extern bool current_in_userns(const struct user_namespace *target_ns);
201 struct ns_common *ns_get_owner(struct ns_common *ns);
204 static inline struct user_namespace *get_user_ns(struct user_namespace *ns)
206 return &init_user_ns;
209 static inline int create_user_ns(struct cred *new)
214 static inline int unshare_userns(unsigned long unshare_flags,
215 struct cred **new_cred)
217 if (unshare_flags & CLONE_NEWUSER)
222 static inline void put_user_ns(struct user_namespace *ns)
226 static inline bool userns_may_setgroups(const struct user_namespace *ns)
231 static inline bool in_userns(const struct user_namespace *ancestor,
232 const struct user_namespace *child)
237 static inline bool current_in_userns(const struct user_namespace *target_ns)
242 static inline struct ns_common *ns_get_owner(struct ns_common *ns)
244 return ERR_PTR(-EPERM);
248 #endif /* _LINUX_USER_H */