Greg Kroah-Hartman | b244131 | 2017-11-01 15:07:57 +0100 | [diff] [blame] | 1 | /* SPDX-License-Identifier: GPL-2.0 */ |
Cedric Le Goater | acce292 | 2007-07-15 23:40:59 -0700 | [diff] [blame] | 2 | #ifndef _LINUX_USER_NAMESPACE_H |
| 3 | #define _LINUX_USER_NAMESPACE_H |
| 4 | |
| 5 | #include <linux/kref.h> |
| 6 | #include <linux/nsproxy.h> |
Al Viro | 435d5f4 | 2014-10-31 22:56:04 -0400 | [diff] [blame] | 7 | #include <linux/ns_common.h> |
Cedric Le Goater | acce292 | 2007-07-15 23:40:59 -0700 | [diff] [blame] | 8 | #include <linux/sched.h> |
Ingo Molnar | b2d5bfe | 2017-02-06 09:56:40 +0100 | [diff] [blame] | 9 | #include <linux/workqueue.h> |
Ingo Molnar | cd9c513 | 2017-02-08 18:51:58 +0100 | [diff] [blame] | 10 | #include <linux/rwsem.h> |
Ingo Molnar | cc5efc2 | 2017-02-03 10:06:45 +0100 | [diff] [blame] | 11 | #include <linux/sysctl.h> |
Serge E. Hallyn | 77ec739 | 2007-07-15 23:41:01 -0700 | [diff] [blame] | 12 | #include <linux/err.h> |
Cedric Le Goater | acce292 | 2007-07-15 23:40:59 -0700 | [diff] [blame] | 13 | |
Christian Brauner | 6397fac | 2017-10-25 00:04:41 +0200 | [diff] [blame] | 14 | #define UID_GID_MAP_MAX_BASE_EXTENTS 5 |
| 15 | #define UID_GID_MAP_MAX_EXTENTS 340 |
Eric W. Biederman | 22d917d | 2011-11-17 00:11:58 -0800 | [diff] [blame] | 16 | |
Christian Brauner | aa4bf44 | 2017-10-25 00:04:40 +0200 | [diff] [blame] | 17 | struct uid_gid_extent { |
| 18 | u32 first; |
| 19 | u32 lower_first; |
| 20 | u32 count; |
| 21 | }; |
| 22 | |
Christian Brauner | 6397fac | 2017-10-25 00:04:41 +0200 | [diff] [blame] | 23 | struct uid_gid_map { /* 64 bytes -- 1 cache line */ |
Eric W. Biederman | 22d917d | 2011-11-17 00:11:58 -0800 | [diff] [blame] | 24 | u32 nr_extents; |
Christian Brauner | aa4bf44 | 2017-10-25 00:04:40 +0200 | [diff] [blame] | 25 | union { |
Christian Brauner | 6397fac | 2017-10-25 00:04:41 +0200 | [diff] [blame] | 26 | struct uid_gid_extent extent[UID_GID_MAP_MAX_BASE_EXTENTS]; |
Christian Brauner | aa4bf44 | 2017-10-25 00:04:40 +0200 | [diff] [blame] | 27 | struct { |
| 28 | struct uid_gid_extent *forward; |
| 29 | struct uid_gid_extent *reverse; |
| 30 | }; |
| 31 | }; |
Eric W. Biederman | 22d917d | 2011-11-17 00:11:58 -0800 | [diff] [blame] | 32 | }; |
| 33 | |
Eric W. Biederman | 9cc4651 | 2014-12-02 12:27:26 -0600 | [diff] [blame] | 34 | #define USERNS_SETGROUPS_ALLOWED 1UL |
| 35 | |
| 36 | #define USERNS_INIT_FLAGS USERNS_SETGROUPS_ALLOWED |
| 37 | |
Eric W. Biederman | f6b2db1 | 2016-08-08 13:54:50 -0500 | [diff] [blame] | 38 | struct ucounts; |
Eric W. Biederman | 25f9c08 | 2016-08-08 14:41:52 -0500 | [diff] [blame] | 39 | |
| 40 | enum ucount_type { |
| 41 | UCOUNT_USER_NAMESPACES, |
Eric W. Biederman | f333c70 | 2016-08-08 14:08:36 -0500 | [diff] [blame] | 42 | UCOUNT_PID_NAMESPACES, |
Eric W. Biederman | f7af3d1 | 2016-08-08 14:11:25 -0500 | [diff] [blame] | 43 | UCOUNT_UTS_NAMESPACES, |
Eric W. Biederman | aba3566 | 2016-08-08 14:20:23 -0500 | [diff] [blame] | 44 | UCOUNT_IPC_NAMESPACES, |
Eric W. Biederman | 7032866 | 2016-08-08 14:33:23 -0500 | [diff] [blame] | 45 | UCOUNT_NET_NAMESPACES, |
Eric W. Biederman | 537f7cc | 2016-08-08 14:37:37 -0500 | [diff] [blame] | 46 | UCOUNT_MNT_NAMESPACES, |
Eric W. Biederman | d08311d | 2016-08-08 14:25:30 -0500 | [diff] [blame] | 47 | UCOUNT_CGROUP_NAMESPACES, |
Nikolay Borisov | 1cce1ee | 2016-12-14 15:56:33 +0200 | [diff] [blame] | 48 | #ifdef CONFIG_INOTIFY_USER |
| 49 | UCOUNT_INOTIFY_INSTANCES, |
| 50 | UCOUNT_INOTIFY_WATCHES, |
| 51 | #endif |
Eric W. Biederman | 25f9c08 | 2016-08-08 14:41:52 -0500 | [diff] [blame] | 52 | UCOUNT_COUNTS, |
| 53 | }; |
| 54 | |
Cedric Le Goater | acce292 | 2007-07-15 23:40:59 -0700 | [diff] [blame] | 55 | struct user_namespace { |
Eric W. Biederman | 22d917d | 2011-11-17 00:11:58 -0800 | [diff] [blame] | 56 | struct uid_gid_map uid_map; |
| 57 | struct uid_gid_map gid_map; |
Eric W. Biederman | f76d207 | 2012-08-30 01:24:05 -0700 | [diff] [blame] | 58 | struct uid_gid_map projid_map; |
Eric W. Biederman | c61a281 | 2012-12-28 18:58:39 -0800 | [diff] [blame] | 59 | atomic_t count; |
Eric W. Biederman | aeb3ae9 | 2011-11-16 21:59:43 -0800 | [diff] [blame] | 60 | struct user_namespace *parent; |
Oleg Nesterov | 8742f22 | 2013-08-08 18:55:32 +0200 | [diff] [blame] | 61 | int level; |
Eric W. Biederman | 783291e | 2011-11-17 01:32:59 -0800 | [diff] [blame] | 62 | kuid_t owner; |
| 63 | kgid_t group; |
Al Viro | 435d5f4 | 2014-10-31 22:56:04 -0400 | [diff] [blame] | 64 | struct ns_common ns; |
Eric W. Biederman | 9cc4651 | 2014-12-02 12:27:26 -0600 | [diff] [blame] | 65 | unsigned long flags; |
David Howells | f36f8c7 | 2013-09-24 10:35:19 +0100 | [diff] [blame] | 66 | |
David Howells | b206f28 | 2019-06-26 21:02:32 +0100 | [diff] [blame] | 67 | #ifdef CONFIG_KEYS |
David Howells | 0f44e4d | 2019-06-26 21:02:32 +0100 | [diff] [blame] | 68 | /* List of joinable keyrings in this namespace. Modification access of |
| 69 | * these pointers is controlled by keyring_sem. Once |
| 70 | * user_keyring_register is set, it won't be changed, so it can be |
| 71 | * accessed directly with READ_ONCE(). |
| 72 | */ |
David Howells | b206f28 | 2019-06-26 21:02:32 +0100 | [diff] [blame] | 73 | struct list_head keyring_name_list; |
David Howells | 0f44e4d | 2019-06-26 21:02:32 +0100 | [diff] [blame] | 74 | struct key *user_keyring_register; |
| 75 | struct rw_semaphore keyring_sem; |
David Howells | b206f28 | 2019-06-26 21:02:32 +0100 | [diff] [blame] | 76 | #endif |
| 77 | |
David Howells | f36f8c7 | 2013-09-24 10:35:19 +0100 | [diff] [blame] | 78 | /* Register of per-UID persistent keyrings for this namespace */ |
| 79 | #ifdef CONFIG_PERSISTENT_KEYRINGS |
| 80 | struct key *persistent_keyring_register; |
David Howells | f36f8c7 | 2013-09-24 10:35:19 +0100 | [diff] [blame] | 81 | #endif |
Eric W. Biederman | b032132 | 2016-07-30 13:53:37 -0500 | [diff] [blame] | 82 | struct work_struct work; |
Eric W. Biederman | dbec284 | 2016-07-30 13:58:49 -0500 | [diff] [blame] | 83 | #ifdef CONFIG_SYSCTL |
| 84 | struct ctl_table_set set; |
| 85 | struct ctl_table_header *sysctls; |
| 86 | #endif |
Eric W. Biederman | f6b2db1 | 2016-08-08 13:54:50 -0500 | [diff] [blame] | 87 | struct ucounts *ucounts; |
Eric W. Biederman | 25f9c08 | 2016-08-08 14:41:52 -0500 | [diff] [blame] | 88 | int ucount_max[UCOUNT_COUNTS]; |
Kees Cook | 3859a27 | 2016-10-28 01:22:25 -0700 | [diff] [blame] | 89 | } __randomize_layout; |
Eric W. Biederman | f6b2db1 | 2016-08-08 13:54:50 -0500 | [diff] [blame] | 90 | |
| 91 | struct ucounts { |
| 92 | struct hlist_node node; |
| 93 | struct user_namespace *ns; |
| 94 | kuid_t uid; |
Eric W. Biederman | 040757f | 2017-03-05 15:03:22 -0600 | [diff] [blame] | 95 | int count; |
Eric W. Biederman | 25f9c08 | 2016-08-08 14:41:52 -0500 | [diff] [blame] | 96 | atomic_t ucount[UCOUNT_COUNTS]; |
Cedric Le Goater | acce292 | 2007-07-15 23:40:59 -0700 | [diff] [blame] | 97 | }; |
| 98 | |
| 99 | extern struct user_namespace init_user_ns; |
Eric W. Biederman | f6b2db1 | 2016-08-08 13:54:50 -0500 | [diff] [blame] | 100 | |
| 101 | bool setup_userns_sysctls(struct user_namespace *ns); |
| 102 | void retire_userns_sysctls(struct user_namespace *ns); |
Eric W. Biederman | 25f9c08 | 2016-08-08 14:41:52 -0500 | [diff] [blame] | 103 | struct ucounts *inc_ucount(struct user_namespace *ns, kuid_t uid, enum ucount_type type); |
| 104 | void dec_ucount(struct ucounts *ucounts, enum ucount_type type); |
Cedric Le Goater | acce292 | 2007-07-15 23:40:59 -0700 | [diff] [blame] | 105 | |
| 106 | #ifdef CONFIG_USER_NS |
| 107 | |
| 108 | static inline struct user_namespace *get_user_ns(struct user_namespace *ns) |
| 109 | { |
| 110 | if (ns) |
Eric W. Biederman | c61a281 | 2012-12-28 18:58:39 -0800 | [diff] [blame] | 111 | atomic_inc(&ns->count); |
Cedric Le Goater | acce292 | 2007-07-15 23:40:59 -0700 | [diff] [blame] | 112 | return ns; |
| 113 | } |
| 114 | |
Serge Hallyn | 18b6e04 | 2008-10-15 16:38:45 -0500 | [diff] [blame] | 115 | extern int create_user_ns(struct cred *new); |
Eric W. Biederman | b2e0d987 | 2012-07-26 05:15:35 -0700 | [diff] [blame] | 116 | extern int unshare_userns(unsigned long unshare_flags, struct cred **new_cred); |
Eric W. Biederman | b032132 | 2016-07-30 13:53:37 -0500 | [diff] [blame] | 117 | extern void __put_user_ns(struct user_namespace *ns); |
Cedric Le Goater | acce292 | 2007-07-15 23:40:59 -0700 | [diff] [blame] | 118 | |
| 119 | static inline void put_user_ns(struct user_namespace *ns) |
| 120 | { |
Eric W. Biederman | c61a281 | 2012-12-28 18:58:39 -0800 | [diff] [blame] | 121 | if (ns && atomic_dec_and_test(&ns->count)) |
Eric W. Biederman | b032132 | 2016-07-30 13:53:37 -0500 | [diff] [blame] | 122 | __put_user_ns(ns); |
Cedric Le Goater | acce292 | 2007-07-15 23:40:59 -0700 | [diff] [blame] | 123 | } |
| 124 | |
Eric W. Biederman | 22d917d | 2011-11-17 00:11:58 -0800 | [diff] [blame] | 125 | struct seq_operations; |
Fabian Frederick | ccf94f1 | 2014-08-08 14:21:22 -0700 | [diff] [blame] | 126 | extern const struct seq_operations proc_uid_seq_operations; |
| 127 | extern const struct seq_operations proc_gid_seq_operations; |
| 128 | extern const struct seq_operations proc_projid_seq_operations; |
Eric W. Biederman | 22d917d | 2011-11-17 00:11:58 -0800 | [diff] [blame] | 129 | extern ssize_t proc_uid_map_write(struct file *, const char __user *, size_t, loff_t *); |
| 130 | extern ssize_t proc_gid_map_write(struct file *, const char __user *, size_t, loff_t *); |
Eric W. Biederman | f76d207 | 2012-08-30 01:24:05 -0700 | [diff] [blame] | 131 | extern ssize_t proc_projid_map_write(struct file *, const char __user *, size_t, loff_t *); |
Eric W. Biederman | 9cc4651 | 2014-12-02 12:27:26 -0600 | [diff] [blame] | 132 | extern ssize_t proc_setgroups_write(struct file *, const char __user *, size_t, loff_t *); |
| 133 | extern int proc_setgroups_show(struct seq_file *m, void *v); |
Eric W. Biederman | 273d2c6 | 2014-12-05 18:01:11 -0600 | [diff] [blame] | 134 | extern bool userns_may_setgroups(const struct user_namespace *ns); |
Eric W. Biederman | a2b4262 | 2017-04-29 14:12:15 -0500 | [diff] [blame] | 135 | extern bool in_userns(const struct user_namespace *ancestor, |
| 136 | const struct user_namespace *child); |
Seth Forshee | d07b846 | 2015-09-23 15:16:04 -0500 | [diff] [blame] | 137 | extern bool current_in_userns(const struct user_namespace *target_ns); |
Andrey Vagin | bcac25a | 2016-09-06 00:47:13 -0700 | [diff] [blame] | 138 | struct ns_common *ns_get_owner(struct ns_common *ns); |
Cedric Le Goater | acce292 | 2007-07-15 23:40:59 -0700 | [diff] [blame] | 139 | #else |
| 140 | |
| 141 | static inline struct user_namespace *get_user_ns(struct user_namespace *ns) |
| 142 | { |
| 143 | return &init_user_ns; |
| 144 | } |
| 145 | |
Serge Hallyn | 18b6e04 | 2008-10-15 16:38:45 -0500 | [diff] [blame] | 146 | static inline int create_user_ns(struct cred *new) |
Cedric Le Goater | acce292 | 2007-07-15 23:40:59 -0700 | [diff] [blame] | 147 | { |
Serge Hallyn | 18b6e04 | 2008-10-15 16:38:45 -0500 | [diff] [blame] | 148 | return -EINVAL; |
Cedric Le Goater | acce292 | 2007-07-15 23:40:59 -0700 | [diff] [blame] | 149 | } |
| 150 | |
Eric W. Biederman | b2e0d987 | 2012-07-26 05:15:35 -0700 | [diff] [blame] | 151 | static inline int unshare_userns(unsigned long unshare_flags, |
| 152 | struct cred **new_cred) |
| 153 | { |
| 154 | if (unshare_flags & CLONE_NEWUSER) |
| 155 | return -EINVAL; |
| 156 | return 0; |
| 157 | } |
| 158 | |
Cedric Le Goater | acce292 | 2007-07-15 23:40:59 -0700 | [diff] [blame] | 159 | static inline void put_user_ns(struct user_namespace *ns) |
| 160 | { |
| 161 | } |
| 162 | |
Eric W. Biederman | 273d2c6 | 2014-12-05 18:01:11 -0600 | [diff] [blame] | 163 | static inline bool userns_may_setgroups(const struct user_namespace *ns) |
| 164 | { |
| 165 | return true; |
| 166 | } |
Seth Forshee | d07b846 | 2015-09-23 15:16:04 -0500 | [diff] [blame] | 167 | |
Eric W. Biederman | a2b4262 | 2017-04-29 14:12:15 -0500 | [diff] [blame] | 168 | static inline bool in_userns(const struct user_namespace *ancestor, |
| 169 | const struct user_namespace *child) |
| 170 | { |
| 171 | return true; |
| 172 | } |
| 173 | |
Seth Forshee | d07b846 | 2015-09-23 15:16:04 -0500 | [diff] [blame] | 174 | static inline bool current_in_userns(const struct user_namespace *target_ns) |
| 175 | { |
| 176 | return true; |
| 177 | } |
Andrey Vagin | bcac25a | 2016-09-06 00:47:13 -0700 | [diff] [blame] | 178 | |
| 179 | static inline struct ns_common *ns_get_owner(struct ns_common *ns) |
| 180 | { |
| 181 | return ERR_PTR(-EPERM); |
| 182 | } |
Eric W. Biederman | 22d917d | 2011-11-17 00:11:58 -0800 | [diff] [blame] | 183 | #endif |
| 184 | |
Cedric Le Goater | acce292 | 2007-07-15 23:40:59 -0700 | [diff] [blame] | 185 | #endif /* _LINUX_USER_H */ |