Merge tag 'xfs-6.4-rc1-fixes' of git://git.kernel.org/pub/scm/fs/xfs/xfs-linux
[linux-block.git] / include / linux / user_namespace.h
CommitLineData
b2441318 1/* SPDX-License-Identifier: GPL-2.0 */
acce292c
CLG
2#ifndef _LINUX_USER_NAMESPACE_H
3#define _LINUX_USER_NAMESPACE_H
4
5#include <linux/kref.h>
6#include <linux/nsproxy.h>
435d5f4b 7#include <linux/ns_common.h>
acce292c 8#include <linux/sched.h>
b2d5bfea 9#include <linux/workqueue.h>
cd9c513b 10#include <linux/rwsem.h>
cc5efc23 11#include <linux/sysctl.h>
77ec739d 12#include <linux/err.h>
acce292c 13
6397fac4
CB
14#define UID_GID_MAP_MAX_BASE_EXTENTS 5
15#define UID_GID_MAP_MAX_EXTENTS 340
22d917d8 16
aa4bf44d
CB
17struct uid_gid_extent {
18 u32 first;
19 u32 lower_first;
20 u32 count;
21};
22
6397fac4 23struct uid_gid_map { /* 64 bytes -- 1 cache line */
22d917d8 24 u32 nr_extents;
aa4bf44d 25 union {
6397fac4 26 struct uid_gid_extent extent[UID_GID_MAP_MAX_BASE_EXTENTS];
aa4bf44d
CB
27 struct {
28 struct uid_gid_extent *forward;
29 struct uid_gid_extent *reverse;
30 };
31 };
22d917d8
EB
32};
33
9cc46516
EB
34#define USERNS_SETGROUPS_ALLOWED 1UL
35
36#define USERNS_INIT_FLAGS USERNS_SETGROUPS_ALLOWED
37
f6b2db1a 38struct ucounts;
25f9c081
EB
39
40enum ucount_type {
41 UCOUNT_USER_NAMESPACES,
f333c700 42 UCOUNT_PID_NAMESPACES,
f7af3d1c 43 UCOUNT_UTS_NAMESPACES,
aba35661 44 UCOUNT_IPC_NAMESPACES,
70328660 45 UCOUNT_NET_NAMESPACES,
537f7ccb 46 UCOUNT_MNT_NAMESPACES,
d08311dd 47 UCOUNT_CGROUP_NAMESPACES,
769071ac 48 UCOUNT_TIME_NAMESPACES,
1cce1eea
NB
49#ifdef CONFIG_INOTIFY_USER
50 UCOUNT_INOTIFY_INSTANCES,
51 UCOUNT_INOTIFY_WATCHES,
5b8fea65
AG
52#endif
53#ifdef CONFIG_FANOTIFY
54 UCOUNT_FANOTIFY_GROUPS,
55 UCOUNT_FANOTIFY_MARKS,
1cce1eea 56#endif
de399236
AG
57 UCOUNT_COUNTS,
58};
59
60enum rlimit_type {
21d1c5e3 61 UCOUNT_RLIMIT_NPROC,
6e52a9f0 62 UCOUNT_RLIMIT_MSGQUEUE,
d6469690 63 UCOUNT_RLIMIT_SIGPENDING,
d7c9e99a 64 UCOUNT_RLIMIT_MEMLOCK,
de399236 65 UCOUNT_RLIMIT_COUNTS,
25f9c081
EB
66};
67
acce292c 68struct user_namespace {
22d917d8
EB
69 struct uid_gid_map uid_map;
70 struct uid_gid_map gid_map;
f76d207a 71 struct uid_gid_map projid_map;
aeb3ae9d 72 struct user_namespace *parent;
8742f229 73 int level;
783291e6
EB
74 kuid_t owner;
75 kgid_t group;
435d5f4b 76 struct ns_common ns;
9cc46516 77 unsigned long flags;
db2e718a
SH
78 /* parent_could_setfcap: true if the creator if this ns had CAP_SETFCAP
79 * in its effective capability set at the child ns creation time. */
80 bool parent_could_setfcap;
f36f8c75 81
b206f281 82#ifdef CONFIG_KEYS
0f44e4d9
DH
83 /* List of joinable keyrings in this namespace. Modification access of
84 * these pointers is controlled by keyring_sem. Once
85 * user_keyring_register is set, it won't be changed, so it can be
86 * accessed directly with READ_ONCE().
87 */
b206f281 88 struct list_head keyring_name_list;
0f44e4d9
DH
89 struct key *user_keyring_register;
90 struct rw_semaphore keyring_sem;
b206f281
DH
91#endif
92
f36f8c75
DH
93 /* Register of per-UID persistent keyrings for this namespace */
94#ifdef CONFIG_PERSISTENT_KEYRINGS
95 struct key *persistent_keyring_register;
f36f8c75 96#endif
b032132c 97 struct work_struct work;
dbec2846
EB
98#ifdef CONFIG_SYSCTL
99 struct ctl_table_set set;
100 struct ctl_table_header *sysctls;
101#endif
f6b2db1a 102 struct ucounts *ucounts;
f9c82a4e 103 long ucount_max[UCOUNT_COUNTS];
de399236 104 long rlimit_max[UCOUNT_RLIMIT_COUNTS];
3859a271 105} __randomize_layout;
f6b2db1a
EB
106
107struct ucounts {
108 struct hlist_node node;
109 struct user_namespace *ns;
110 kuid_t uid;
b6c33652 111 atomic_t count;
f9c82a4e 112 atomic_long_t ucount[UCOUNT_COUNTS];
de399236 113 atomic_long_t rlimit[UCOUNT_RLIMIT_COUNTS];
acce292c
CLG
114};
115
116extern struct user_namespace init_user_ns;
905ae01c 117extern struct ucounts init_ucounts;
f6b2db1a
EB
118
119bool setup_userns_sysctls(struct user_namespace *ns);
120void retire_userns_sysctls(struct user_namespace *ns);
25f9c081
EB
121struct ucounts *inc_ucount(struct user_namespace *ns, kuid_t uid, enum ucount_type type);
122void dec_ucount(struct ucounts *ucounts, enum ucount_type type);
905ae01c 123struct ucounts *alloc_ucounts(struct user_namespace *ns, kuid_t uid);
b6c33652 124struct ucounts * __must_check get_ucounts(struct ucounts *ucounts);
905ae01c 125void put_ucounts(struct ucounts *ucounts);
acce292c 126
de399236 127static inline long get_rlimit_value(struct ucounts *ucounts, enum rlimit_type type)
21d1c5e3 128{
de399236 129 return atomic_long_read(&ucounts->rlimit[type]);
21d1c5e3
AG
130}
131
de399236
AG
132long inc_rlimit_ucounts(struct ucounts *ucounts, enum rlimit_type type, long v);
133bool dec_rlimit_ucounts(struct ucounts *ucounts, enum rlimit_type type, long v);
134long inc_rlimit_get_ucounts(struct ucounts *ucounts, enum rlimit_type type);
135void dec_rlimit_put_ucounts(struct ucounts *ucounts, enum rlimit_type type);
136bool is_rlimit_overlimit(struct ucounts *ucounts, enum rlimit_type type, unsigned long max);
137
138static inline long get_userns_rlimit_max(struct user_namespace *ns, enum rlimit_type type)
139{
140 return READ_ONCE(ns->rlimit_max[type]);
141}
21d1c5e3 142
de399236
AG
143static inline void set_userns_rlimit_max(struct user_namespace *ns,
144 enum rlimit_type type, unsigned long max)
c1ada3dc 145{
de399236 146 ns->rlimit_max[type] = max <= LONG_MAX ? max : LONG_MAX;
c1ada3dc 147}
acce292c
CLG
148
149#ifdef CONFIG_USER_NS
150
151static inline struct user_namespace *get_user_ns(struct user_namespace *ns)
152{
153 if (ns)
265cbd62 154 refcount_inc(&ns->ns.count);
acce292c
CLG
155 return ns;
156}
157
18b6e041 158extern int create_user_ns(struct cred *new);
b2e0d987 159extern int unshare_userns(unsigned long unshare_flags, struct cred **new_cred);
b032132c 160extern void __put_user_ns(struct user_namespace *ns);
acce292c
CLG
161
162static inline void put_user_ns(struct user_namespace *ns)
163{
265cbd62 164 if (ns && refcount_dec_and_test(&ns->ns.count))
b032132c 165 __put_user_ns(ns);
acce292c
CLG
166}
167
22d917d8 168struct seq_operations;
ccf94f1b
FF
169extern const struct seq_operations proc_uid_seq_operations;
170extern const struct seq_operations proc_gid_seq_operations;
171extern const struct seq_operations proc_projid_seq_operations;
22d917d8
EB
172extern ssize_t proc_uid_map_write(struct file *, const char __user *, size_t, loff_t *);
173extern ssize_t proc_gid_map_write(struct file *, const char __user *, size_t, loff_t *);
f76d207a 174extern ssize_t proc_projid_map_write(struct file *, const char __user *, size_t, loff_t *);
9cc46516
EB
175extern ssize_t proc_setgroups_write(struct file *, const char __user *, size_t, loff_t *);
176extern int proc_setgroups_show(struct seq_file *m, void *v);
273d2c67 177extern bool userns_may_setgroups(const struct user_namespace *ns);
a2b42626
EB
178extern bool in_userns(const struct user_namespace *ancestor,
179 const struct user_namespace *child);
d07b846f 180extern bool current_in_userns(const struct user_namespace *target_ns);
bcac25a5 181struct ns_common *ns_get_owner(struct ns_common *ns);
acce292c
CLG
182#else
183
184static inline struct user_namespace *get_user_ns(struct user_namespace *ns)
185{
186 return &init_user_ns;
187}
188
18b6e041 189static inline int create_user_ns(struct cred *new)
acce292c 190{
18b6e041 191 return -EINVAL;
acce292c
CLG
192}
193
b2e0d987
EB
194static inline int unshare_userns(unsigned long unshare_flags,
195 struct cred **new_cred)
196{
197 if (unshare_flags & CLONE_NEWUSER)
198 return -EINVAL;
199 return 0;
200}
201
acce292c
CLG
202static inline void put_user_ns(struct user_namespace *ns)
203{
204}
205
273d2c67
EB
206static inline bool userns_may_setgroups(const struct user_namespace *ns)
207{
208 return true;
209}
d07b846f 210
a2b42626
EB
211static inline bool in_userns(const struct user_namespace *ancestor,
212 const struct user_namespace *child)
213{
214 return true;
215}
216
d07b846f
SF
217static inline bool current_in_userns(const struct user_namespace *target_ns)
218{
219 return true;
220}
bcac25a5
AV
221
222static inline struct ns_common *ns_get_owner(struct ns_common *ns)
223{
224 return ERR_PTR(-EPERM);
225}
22d917d8
EB
226#endif
227
acce292c 228#endif /* _LINUX_USER_H */