Commit | Line | Data |
---|---|---|
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 | |
22d917d8 EB |
14 | #define UID_GID_MAP_MAX_EXTENTS 5 |
15 | ||
16 | struct uid_gid_map { /* 64 bytes -- 1 cache line */ | |
17 | u32 nr_extents; | |
18 | struct uid_gid_extent { | |
19 | u32 first; | |
20 | u32 lower_first; | |
21 | u32 count; | |
22 | } extent[UID_GID_MAP_MAX_EXTENTS]; | |
23 | }; | |
24 | ||
9cc46516 EB |
25 | #define USERNS_SETGROUPS_ALLOWED 1UL |
26 | ||
27 | #define USERNS_INIT_FLAGS USERNS_SETGROUPS_ALLOWED | |
28 | ||
f6b2db1a | 29 | struct ucounts; |
25f9c081 EB |
30 | |
31 | enum ucount_type { | |
32 | UCOUNT_USER_NAMESPACES, | |
f333c700 | 33 | UCOUNT_PID_NAMESPACES, |
f7af3d1c | 34 | UCOUNT_UTS_NAMESPACES, |
aba35661 | 35 | UCOUNT_IPC_NAMESPACES, |
70328660 | 36 | UCOUNT_NET_NAMESPACES, |
537f7ccb | 37 | UCOUNT_MNT_NAMESPACES, |
d08311dd | 38 | UCOUNT_CGROUP_NAMESPACES, |
1cce1eea NB |
39 | #ifdef CONFIG_INOTIFY_USER |
40 | UCOUNT_INOTIFY_INSTANCES, | |
41 | UCOUNT_INOTIFY_WATCHES, | |
42 | #endif | |
25f9c081 EB |
43 | UCOUNT_COUNTS, |
44 | }; | |
45 | ||
acce292c | 46 | struct user_namespace { |
22d917d8 EB |
47 | struct uid_gid_map uid_map; |
48 | struct uid_gid_map gid_map; | |
f76d207a | 49 | struct uid_gid_map projid_map; |
c61a2810 | 50 | atomic_t count; |
aeb3ae9d | 51 | struct user_namespace *parent; |
8742f229 | 52 | int level; |
783291e6 EB |
53 | kuid_t owner; |
54 | kgid_t group; | |
435d5f4b | 55 | struct ns_common ns; |
9cc46516 | 56 | unsigned long flags; |
f36f8c75 DH |
57 | |
58 | /* Register of per-UID persistent keyrings for this namespace */ | |
59 | #ifdef CONFIG_PERSISTENT_KEYRINGS | |
60 | struct key *persistent_keyring_register; | |
61 | struct rw_semaphore persistent_keyring_register_sem; | |
62 | #endif | |
b032132c | 63 | struct work_struct work; |
dbec2846 EB |
64 | #ifdef CONFIG_SYSCTL |
65 | struct ctl_table_set set; | |
66 | struct ctl_table_header *sysctls; | |
67 | #endif | |
f6b2db1a | 68 | struct ucounts *ucounts; |
25f9c081 | 69 | int ucount_max[UCOUNT_COUNTS]; |
3859a271 | 70 | } __randomize_layout; |
f6b2db1a EB |
71 | |
72 | struct ucounts { | |
73 | struct hlist_node node; | |
74 | struct user_namespace *ns; | |
75 | kuid_t uid; | |
040757f7 | 76 | int count; |
25f9c081 | 77 | atomic_t ucount[UCOUNT_COUNTS]; |
acce292c CLG |
78 | }; |
79 | ||
80 | extern struct user_namespace init_user_ns; | |
f6b2db1a EB |
81 | |
82 | bool setup_userns_sysctls(struct user_namespace *ns); | |
83 | void retire_userns_sysctls(struct user_namespace *ns); | |
25f9c081 EB |
84 | struct ucounts *inc_ucount(struct user_namespace *ns, kuid_t uid, enum ucount_type type); |
85 | void dec_ucount(struct ucounts *ucounts, enum ucount_type type); | |
acce292c CLG |
86 | |
87 | #ifdef CONFIG_USER_NS | |
88 | ||
89 | static inline struct user_namespace *get_user_ns(struct user_namespace *ns) | |
90 | { | |
91 | if (ns) | |
c61a2810 | 92 | atomic_inc(&ns->count); |
acce292c CLG |
93 | return ns; |
94 | } | |
95 | ||
18b6e041 | 96 | extern int create_user_ns(struct cred *new); |
b2e0d987 | 97 | extern int unshare_userns(unsigned long unshare_flags, struct cred **new_cred); |
b032132c | 98 | extern void __put_user_ns(struct user_namespace *ns); |
acce292c CLG |
99 | |
100 | static inline void put_user_ns(struct user_namespace *ns) | |
101 | { | |
c61a2810 | 102 | if (ns && atomic_dec_and_test(&ns->count)) |
b032132c | 103 | __put_user_ns(ns); |
acce292c CLG |
104 | } |
105 | ||
22d917d8 | 106 | struct seq_operations; |
ccf94f1b FF |
107 | extern const struct seq_operations proc_uid_seq_operations; |
108 | extern const struct seq_operations proc_gid_seq_operations; | |
109 | extern const struct seq_operations proc_projid_seq_operations; | |
22d917d8 EB |
110 | extern ssize_t proc_uid_map_write(struct file *, const char __user *, size_t, loff_t *); |
111 | extern ssize_t proc_gid_map_write(struct file *, const char __user *, size_t, loff_t *); | |
f76d207a | 112 | extern ssize_t proc_projid_map_write(struct file *, const char __user *, size_t, loff_t *); |
9cc46516 EB |
113 | extern ssize_t proc_setgroups_write(struct file *, const char __user *, size_t, loff_t *); |
114 | extern int proc_setgroups_show(struct seq_file *m, void *v); | |
273d2c67 | 115 | extern bool userns_may_setgroups(const struct user_namespace *ns); |
a2b42626 EB |
116 | extern bool in_userns(const struct user_namespace *ancestor, |
117 | const struct user_namespace *child); | |
d07b846f | 118 | extern bool current_in_userns(const struct user_namespace *target_ns); |
bcac25a5 | 119 | struct ns_common *ns_get_owner(struct ns_common *ns); |
acce292c CLG |
120 | #else |
121 | ||
122 | static inline struct user_namespace *get_user_ns(struct user_namespace *ns) | |
123 | { | |
124 | return &init_user_ns; | |
125 | } | |
126 | ||
18b6e041 | 127 | static inline int create_user_ns(struct cred *new) |
acce292c | 128 | { |
18b6e041 | 129 | return -EINVAL; |
acce292c CLG |
130 | } |
131 | ||
b2e0d987 EB |
132 | static inline int unshare_userns(unsigned long unshare_flags, |
133 | struct cred **new_cred) | |
134 | { | |
135 | if (unshare_flags & CLONE_NEWUSER) | |
136 | return -EINVAL; | |
137 | return 0; | |
138 | } | |
139 | ||
acce292c CLG |
140 | static inline void put_user_ns(struct user_namespace *ns) |
141 | { | |
142 | } | |
143 | ||
273d2c67 EB |
144 | static inline bool userns_may_setgroups(const struct user_namespace *ns) |
145 | { | |
146 | return true; | |
147 | } | |
d07b846f | 148 | |
a2b42626 EB |
149 | static inline bool in_userns(const struct user_namespace *ancestor, |
150 | const struct user_namespace *child) | |
151 | { | |
152 | return true; | |
153 | } | |
154 | ||
d07b846f SF |
155 | static inline bool current_in_userns(const struct user_namespace *target_ns) |
156 | { | |
157 | return true; | |
158 | } | |
bcac25a5 AV |
159 | |
160 | static inline struct ns_common *ns_get_owner(struct ns_common *ns) | |
161 | { | |
162 | return ERR_PTR(-EPERM); | |
163 | } | |
22d917d8 EB |
164 | #endif |
165 | ||
acce292c | 166 | #endif /* _LINUX_USER_H */ |