Cedric Le Goater | acce292 | 2007-07-15 23:40:59 -0700 | [diff] [blame] | 1 | /* |
| 2 | * This program is free software; you can redistribute it and/or |
| 3 | * modify it under the terms of the GNU General Public License as |
| 4 | * published by the Free Software Foundation, version 2 of the |
| 5 | * License. |
| 6 | */ |
| 7 | |
Paul Gortmaker | 9984de1 | 2011-05-23 14:51:41 -0400 | [diff] [blame] | 8 | #include <linux/export.h> |
Cedric Le Goater | acce292 | 2007-07-15 23:40:59 -0700 | [diff] [blame] | 9 | #include <linux/nsproxy.h> |
Robert P. J. Day | 1aeb272 | 2008-04-29 00:59:25 -0700 | [diff] [blame] | 10 | #include <linux/slab.h> |
Cedric Le Goater | acce292 | 2007-07-15 23:40:59 -0700 | [diff] [blame] | 11 | #include <linux/user_namespace.h> |
David Howells | 0bb80f2 | 2013-04-12 01:50:06 +0100 | [diff] [blame] | 12 | #include <linux/proc_ns.h> |
Eric W. Biederman | 5c1469d | 2010-06-13 03:28:03 +0000 | [diff] [blame] | 13 | #include <linux/highuid.h> |
Serge Hallyn | 18b6e04 | 2008-10-15 16:38:45 -0500 | [diff] [blame] | 14 | #include <linux/cred.h> |
Eric W. Biederman | 973c591 | 2011-11-17 01:59:07 -0800 | [diff] [blame] | 15 | #include <linux/securebits.h> |
Eric W. Biederman | 22d917d | 2011-11-17 00:11:58 -0800 | [diff] [blame] | 16 | #include <linux/keyctl.h> |
| 17 | #include <linux/key-type.h> |
| 18 | #include <keys/user-type.h> |
| 19 | #include <linux/seq_file.h> |
| 20 | #include <linux/fs.h> |
| 21 | #include <linux/uaccess.h> |
| 22 | #include <linux/ctype.h> |
Eric W. Biederman | f76d207 | 2012-08-30 01:24:05 -0700 | [diff] [blame] | 23 | #include <linux/projid.h> |
Eric W. Biederman | e66eded | 2013-03-13 11:51:49 -0700 | [diff] [blame] | 24 | #include <linux/fs_struct.h> |
Cedric Le Goater | acce292 | 2007-07-15 23:40:59 -0700 | [diff] [blame] | 25 | |
Pavel Emelyanov | 6164281 | 2011-01-12 17:00:46 -0800 | [diff] [blame] | 26 | static struct kmem_cache *user_ns_cachep __read_mostly; |
Eric W. Biederman | f0d62ae | 2014-12-09 14:03:14 -0600 | [diff] [blame] | 27 | static DEFINE_MUTEX(userns_state_mutex); |
Pavel Emelyanov | 6164281 | 2011-01-12 17:00:46 -0800 | [diff] [blame] | 28 | |
Eric W. Biederman | 6708075 | 2013-04-14 13:47:02 -0700 | [diff] [blame] | 29 | static bool new_idmap_permitted(const struct file *file, |
| 30 | struct user_namespace *ns, int cap_setid, |
Eric W. Biederman | 22d917d | 2011-11-17 00:11:58 -0800 | [diff] [blame] | 31 | struct uid_gid_map *map); |
Eric W. Biederman | b032132 | 2016-07-30 13:53:37 -0500 | [diff] [blame^] | 32 | static void free_user_ns(struct work_struct *work); |
Eric W. Biederman | 22d917d | 2011-11-17 00:11:58 -0800 | [diff] [blame] | 33 | |
Eric W. Biederman | cde1975 | 2012-07-26 06:24:06 -0700 | [diff] [blame] | 34 | static void set_cred_user_ns(struct cred *cred, struct user_namespace *user_ns) |
| 35 | { |
| 36 | /* Start with the same capabilities as init but useless for doing |
| 37 | * anything as the capabilities are bound to the new user namespace. |
| 38 | */ |
| 39 | cred->securebits = SECUREBITS_DEFAULT; |
| 40 | cred->cap_inheritable = CAP_EMPTY_SET; |
| 41 | cred->cap_permitted = CAP_FULL_SET; |
| 42 | cred->cap_effective = CAP_FULL_SET; |
Andy Lutomirski | 5831905 | 2015-09-04 15:42:45 -0700 | [diff] [blame] | 43 | cred->cap_ambient = CAP_EMPTY_SET; |
Eric W. Biederman | cde1975 | 2012-07-26 06:24:06 -0700 | [diff] [blame] | 44 | cred->cap_bset = CAP_FULL_SET; |
| 45 | #ifdef CONFIG_KEYS |
| 46 | key_put(cred->request_key_auth); |
| 47 | cred->request_key_auth = NULL; |
| 48 | #endif |
| 49 | /* tgcred will be cleared in our caller bc CLONE_THREAD won't be set */ |
| 50 | cred->user_ns = user_ns; |
| 51 | } |
| 52 | |
Serge E. Hallyn | 77ec739 | 2007-07-15 23:41:01 -0700 | [diff] [blame] | 53 | /* |
Serge Hallyn | 18b6e04 | 2008-10-15 16:38:45 -0500 | [diff] [blame] | 54 | * Create a new user namespace, deriving the creator from the user in the |
| 55 | * passed credentials, and replacing that user with the new root user for the |
| 56 | * new namespace. |
| 57 | * |
| 58 | * This is called by copy_creds(), which will finish setting the target task's |
| 59 | * credentials. |
Serge E. Hallyn | 77ec739 | 2007-07-15 23:41:01 -0700 | [diff] [blame] | 60 | */ |
Serge Hallyn | 18b6e04 | 2008-10-15 16:38:45 -0500 | [diff] [blame] | 61 | int create_user_ns(struct cred *new) |
Serge E. Hallyn | 77ec739 | 2007-07-15 23:41:01 -0700 | [diff] [blame] | 62 | { |
Eric W. Biederman | 0093ccb | 2011-11-16 21:52:53 -0800 | [diff] [blame] | 63 | struct user_namespace *ns, *parent_ns = new->user_ns; |
Eric W. Biederman | 078de5f | 2012-02-08 07:00:08 -0800 | [diff] [blame] | 64 | kuid_t owner = new->euid; |
| 65 | kgid_t group = new->egid; |
Eric W. Biederman | 98f842e | 2011-06-15 10:21:48 -0700 | [diff] [blame] | 66 | int ret; |
Eric W. Biederman | 783291e | 2011-11-17 01:32:59 -0800 | [diff] [blame] | 67 | |
Oleg Nesterov | 8742f22 | 2013-08-08 18:55:32 +0200 | [diff] [blame] | 68 | if (parent_ns->level > 32) |
| 69 | return -EUSERS; |
| 70 | |
Eric W. Biederman | 3151527 | 2013-03-15 01:45:51 -0700 | [diff] [blame] | 71 | /* |
| 72 | * Verify that we can not violate the policy of which files |
| 73 | * may be accessed that is specified by the root directory, |
| 74 | * by verifing that the root directory is at the root of the |
| 75 | * mount namespace which allows all files to be accessed. |
| 76 | */ |
| 77 | if (current_chrooted()) |
| 78 | return -EPERM; |
| 79 | |
Eric W. Biederman | 783291e | 2011-11-17 01:32:59 -0800 | [diff] [blame] | 80 | /* The creator needs a mapping in the parent user namespace |
| 81 | * or else we won't be able to reasonably tell userspace who |
| 82 | * created a user_namespace. |
| 83 | */ |
| 84 | if (!kuid_has_mapping(parent_ns, owner) || |
| 85 | !kgid_has_mapping(parent_ns, group)) |
| 86 | return -EPERM; |
Serge E. Hallyn | 77ec739 | 2007-07-15 23:41:01 -0700 | [diff] [blame] | 87 | |
Eric W. Biederman | 22d917d | 2011-11-17 00:11:58 -0800 | [diff] [blame] | 88 | ns = kmem_cache_zalloc(user_ns_cachep, GFP_KERNEL); |
Serge E. Hallyn | 77ec739 | 2007-07-15 23:41:01 -0700 | [diff] [blame] | 89 | if (!ns) |
Serge Hallyn | 18b6e04 | 2008-10-15 16:38:45 -0500 | [diff] [blame] | 90 | return -ENOMEM; |
Serge E. Hallyn | 77ec739 | 2007-07-15 23:41:01 -0700 | [diff] [blame] | 91 | |
Al Viro | 6344c43 | 2014-11-01 00:45:45 -0400 | [diff] [blame] | 92 | ret = ns_alloc_inum(&ns->ns); |
Eric W. Biederman | 98f842e | 2011-06-15 10:21:48 -0700 | [diff] [blame] | 93 | if (ret) { |
| 94 | kmem_cache_free(user_ns_cachep, ns); |
| 95 | return ret; |
| 96 | } |
Al Viro | 33c4294 | 2014-11-01 02:32:53 -0400 | [diff] [blame] | 97 | ns->ns.ops = &userns_operations; |
Eric W. Biederman | 98f842e | 2011-06-15 10:21:48 -0700 | [diff] [blame] | 98 | |
Eric W. Biederman | c61a281 | 2012-12-28 18:58:39 -0800 | [diff] [blame] | 99 | atomic_set(&ns->count, 1); |
Eric W. Biederman | cde1975 | 2012-07-26 06:24:06 -0700 | [diff] [blame] | 100 | /* Leave the new->user_ns reference with the new user namespace. */ |
Eric W. Biederman | aeb3ae9 | 2011-11-16 21:59:43 -0800 | [diff] [blame] | 101 | ns->parent = parent_ns; |
Oleg Nesterov | 8742f22 | 2013-08-08 18:55:32 +0200 | [diff] [blame] | 102 | ns->level = parent_ns->level + 1; |
Eric W. Biederman | 783291e | 2011-11-17 01:32:59 -0800 | [diff] [blame] | 103 | ns->owner = owner; |
| 104 | ns->group = group; |
Eric W. Biederman | b032132 | 2016-07-30 13:53:37 -0500 | [diff] [blame^] | 105 | INIT_WORK(&ns->work, free_user_ns); |
Eric W. Biederman | 22d917d | 2011-11-17 00:11:58 -0800 | [diff] [blame] | 106 | |
Eric W. Biederman | 9cc4651 | 2014-12-02 12:27:26 -0600 | [diff] [blame] | 107 | /* Inherit USERNS_SETGROUPS_ALLOWED from our parent */ |
| 108 | mutex_lock(&userns_state_mutex); |
| 109 | ns->flags = parent_ns->flags; |
| 110 | mutex_unlock(&userns_state_mutex); |
| 111 | |
Eric W. Biederman | cde1975 | 2012-07-26 06:24:06 -0700 | [diff] [blame] | 112 | set_cred_user_ns(new, ns); |
Eric W. Biederman | 0093ccb | 2011-11-16 21:52:53 -0800 | [diff] [blame] | 113 | |
David Howells | f36f8c7 | 2013-09-24 10:35:19 +0100 | [diff] [blame] | 114 | #ifdef CONFIG_PERSISTENT_KEYRINGS |
| 115 | init_rwsem(&ns->persistent_keyring_register_sem); |
| 116 | #endif |
Serge Hallyn | 18b6e04 | 2008-10-15 16:38:45 -0500 | [diff] [blame] | 117 | return 0; |
Cedric Le Goater | acce292 | 2007-07-15 23:40:59 -0700 | [diff] [blame] | 118 | } |
| 119 | |
Eric W. Biederman | b2e0d987 | 2012-07-26 05:15:35 -0700 | [diff] [blame] | 120 | int unshare_userns(unsigned long unshare_flags, struct cred **new_cred) |
| 121 | { |
| 122 | struct cred *cred; |
Oleg Nesterov | 6160968 | 2013-08-06 19:38:55 +0200 | [diff] [blame] | 123 | int err = -ENOMEM; |
Eric W. Biederman | b2e0d987 | 2012-07-26 05:15:35 -0700 | [diff] [blame] | 124 | |
| 125 | if (!(unshare_flags & CLONE_NEWUSER)) |
| 126 | return 0; |
| 127 | |
| 128 | cred = prepare_creds(); |
Oleg Nesterov | 6160968 | 2013-08-06 19:38:55 +0200 | [diff] [blame] | 129 | if (cred) { |
| 130 | err = create_user_ns(cred); |
| 131 | if (err) |
| 132 | put_cred(cred); |
| 133 | else |
| 134 | *new_cred = cred; |
| 135 | } |
Eric W. Biederman | b2e0d987 | 2012-07-26 05:15:35 -0700 | [diff] [blame] | 136 | |
Oleg Nesterov | 6160968 | 2013-08-06 19:38:55 +0200 | [diff] [blame] | 137 | return err; |
Eric W. Biederman | b2e0d987 | 2012-07-26 05:15:35 -0700 | [diff] [blame] | 138 | } |
| 139 | |
Eric W. Biederman | b032132 | 2016-07-30 13:53:37 -0500 | [diff] [blame^] | 140 | static void free_user_ns(struct work_struct *work) |
David Howells | 5170836 | 2009-02-27 14:03:03 -0800 | [diff] [blame] | 141 | { |
Eric W. Biederman | b032132 | 2016-07-30 13:53:37 -0500 | [diff] [blame^] | 142 | struct user_namespace *parent, *ns = |
| 143 | container_of(work, struct user_namespace, work); |
David Howells | 5170836 | 2009-02-27 14:03:03 -0800 | [diff] [blame] | 144 | |
Eric W. Biederman | c61a281 | 2012-12-28 18:58:39 -0800 | [diff] [blame] | 145 | do { |
| 146 | parent = ns->parent; |
David Howells | f36f8c7 | 2013-09-24 10:35:19 +0100 | [diff] [blame] | 147 | #ifdef CONFIG_PERSISTENT_KEYRINGS |
| 148 | key_put(ns->persistent_keyring_register); |
| 149 | #endif |
Al Viro | 6344c43 | 2014-11-01 00:45:45 -0400 | [diff] [blame] | 150 | ns_free_inum(&ns->ns); |
Eric W. Biederman | c61a281 | 2012-12-28 18:58:39 -0800 | [diff] [blame] | 151 | kmem_cache_free(user_ns_cachep, ns); |
| 152 | ns = parent; |
| 153 | } while (atomic_dec_and_test(&parent->count)); |
David Howells | 5170836 | 2009-02-27 14:03:03 -0800 | [diff] [blame] | 154 | } |
Eric W. Biederman | b032132 | 2016-07-30 13:53:37 -0500 | [diff] [blame^] | 155 | |
| 156 | void __put_user_ns(struct user_namespace *ns) |
| 157 | { |
| 158 | schedule_work(&ns->work); |
| 159 | } |
| 160 | EXPORT_SYMBOL(__put_user_ns); |
Eric W. Biederman | 5c1469d | 2010-06-13 03:28:03 +0000 | [diff] [blame] | 161 | |
Eric W. Biederman | 22d917d | 2011-11-17 00:11:58 -0800 | [diff] [blame] | 162 | static u32 map_id_range_down(struct uid_gid_map *map, u32 id, u32 count) |
Eric W. Biederman | 5c1469d | 2010-06-13 03:28:03 +0000 | [diff] [blame] | 163 | { |
Eric W. Biederman | 22d917d | 2011-11-17 00:11:58 -0800 | [diff] [blame] | 164 | unsigned idx, extents; |
| 165 | u32 first, last, id2; |
Eric W. Biederman | 5c1469d | 2010-06-13 03:28:03 +0000 | [diff] [blame] | 166 | |
Eric W. Biederman | 22d917d | 2011-11-17 00:11:58 -0800 | [diff] [blame] | 167 | id2 = id + count - 1; |
Eric W. Biederman | 5c1469d | 2010-06-13 03:28:03 +0000 | [diff] [blame] | 168 | |
Eric W. Biederman | 22d917d | 2011-11-17 00:11:58 -0800 | [diff] [blame] | 169 | /* Find the matching extent */ |
| 170 | extents = map->nr_extents; |
Mikulas Patocka | e79323b | 2014-04-14 16:58:55 -0400 | [diff] [blame] | 171 | smp_rmb(); |
Eric W. Biederman | 22d917d | 2011-11-17 00:11:58 -0800 | [diff] [blame] | 172 | for (idx = 0; idx < extents; idx++) { |
| 173 | first = map->extent[idx].first; |
| 174 | last = first + map->extent[idx].count - 1; |
| 175 | if (id >= first && id <= last && |
| 176 | (id2 >= first && id2 <= last)) |
| 177 | break; |
Eric W. Biederman | 5c1469d | 2010-06-13 03:28:03 +0000 | [diff] [blame] | 178 | } |
Eric W. Biederman | 22d917d | 2011-11-17 00:11:58 -0800 | [diff] [blame] | 179 | /* Map the id or note failure */ |
| 180 | if (idx < extents) |
| 181 | id = (id - first) + map->extent[idx].lower_first; |
| 182 | else |
| 183 | id = (u32) -1; |
Eric W. Biederman | 5c1469d | 2010-06-13 03:28:03 +0000 | [diff] [blame] | 184 | |
Eric W. Biederman | 22d917d | 2011-11-17 00:11:58 -0800 | [diff] [blame] | 185 | return id; |
Eric W. Biederman | 5c1469d | 2010-06-13 03:28:03 +0000 | [diff] [blame] | 186 | } |
| 187 | |
Eric W. Biederman | 22d917d | 2011-11-17 00:11:58 -0800 | [diff] [blame] | 188 | static u32 map_id_down(struct uid_gid_map *map, u32 id) |
Eric W. Biederman | 5c1469d | 2010-06-13 03:28:03 +0000 | [diff] [blame] | 189 | { |
Eric W. Biederman | 22d917d | 2011-11-17 00:11:58 -0800 | [diff] [blame] | 190 | unsigned idx, extents; |
| 191 | u32 first, last; |
Eric W. Biederman | 5c1469d | 2010-06-13 03:28:03 +0000 | [diff] [blame] | 192 | |
Eric W. Biederman | 22d917d | 2011-11-17 00:11:58 -0800 | [diff] [blame] | 193 | /* Find the matching extent */ |
| 194 | extents = map->nr_extents; |
Mikulas Patocka | e79323b | 2014-04-14 16:58:55 -0400 | [diff] [blame] | 195 | smp_rmb(); |
Eric W. Biederman | 22d917d | 2011-11-17 00:11:58 -0800 | [diff] [blame] | 196 | for (idx = 0; idx < extents; idx++) { |
| 197 | first = map->extent[idx].first; |
| 198 | last = first + map->extent[idx].count - 1; |
| 199 | if (id >= first && id <= last) |
| 200 | break; |
| 201 | } |
| 202 | /* Map the id or note failure */ |
| 203 | if (idx < extents) |
| 204 | id = (id - first) + map->extent[idx].lower_first; |
| 205 | else |
| 206 | id = (u32) -1; |
Eric W. Biederman | 5c1469d | 2010-06-13 03:28:03 +0000 | [diff] [blame] | 207 | |
Eric W. Biederman | 22d917d | 2011-11-17 00:11:58 -0800 | [diff] [blame] | 208 | return id; |
| 209 | } |
| 210 | |
| 211 | static u32 map_id_up(struct uid_gid_map *map, u32 id) |
| 212 | { |
| 213 | unsigned idx, extents; |
| 214 | u32 first, last; |
| 215 | |
| 216 | /* Find the matching extent */ |
| 217 | extents = map->nr_extents; |
Mikulas Patocka | e79323b | 2014-04-14 16:58:55 -0400 | [diff] [blame] | 218 | smp_rmb(); |
Eric W. Biederman | 22d917d | 2011-11-17 00:11:58 -0800 | [diff] [blame] | 219 | for (idx = 0; idx < extents; idx++) { |
| 220 | first = map->extent[idx].lower_first; |
| 221 | last = first + map->extent[idx].count - 1; |
| 222 | if (id >= first && id <= last) |
| 223 | break; |
| 224 | } |
| 225 | /* Map the id or note failure */ |
| 226 | if (idx < extents) |
| 227 | id = (id - first) + map->extent[idx].first; |
| 228 | else |
| 229 | id = (u32) -1; |
| 230 | |
| 231 | return id; |
| 232 | } |
| 233 | |
| 234 | /** |
| 235 | * make_kuid - Map a user-namespace uid pair into a kuid. |
| 236 | * @ns: User namespace that the uid is in |
| 237 | * @uid: User identifier |
| 238 | * |
| 239 | * Maps a user-namespace uid pair into a kernel internal kuid, |
| 240 | * and returns that kuid. |
| 241 | * |
| 242 | * When there is no mapping defined for the user-namespace uid |
| 243 | * pair INVALID_UID is returned. Callers are expected to test |
Brian Campbell | b080e04 | 2014-02-16 22:58:12 -0500 | [diff] [blame] | 244 | * for and handle INVALID_UID being returned. INVALID_UID |
Eric W. Biederman | 22d917d | 2011-11-17 00:11:58 -0800 | [diff] [blame] | 245 | * may be tested for using uid_valid(). |
| 246 | */ |
| 247 | kuid_t make_kuid(struct user_namespace *ns, uid_t uid) |
| 248 | { |
| 249 | /* Map the uid to a global kernel uid */ |
| 250 | return KUIDT_INIT(map_id_down(&ns->uid_map, uid)); |
| 251 | } |
| 252 | EXPORT_SYMBOL(make_kuid); |
| 253 | |
| 254 | /** |
| 255 | * from_kuid - Create a uid from a kuid user-namespace pair. |
| 256 | * @targ: The user namespace we want a uid in. |
| 257 | * @kuid: The kernel internal uid to start with. |
| 258 | * |
| 259 | * Map @kuid into the user-namespace specified by @targ and |
| 260 | * return the resulting uid. |
| 261 | * |
| 262 | * There is always a mapping into the initial user_namespace. |
| 263 | * |
| 264 | * If @kuid has no mapping in @targ (uid_t)-1 is returned. |
| 265 | */ |
| 266 | uid_t from_kuid(struct user_namespace *targ, kuid_t kuid) |
| 267 | { |
| 268 | /* Map the uid from a global kernel uid */ |
| 269 | return map_id_up(&targ->uid_map, __kuid_val(kuid)); |
| 270 | } |
| 271 | EXPORT_SYMBOL(from_kuid); |
| 272 | |
| 273 | /** |
| 274 | * from_kuid_munged - Create a uid from a kuid user-namespace pair. |
| 275 | * @targ: The user namespace we want a uid in. |
| 276 | * @kuid: The kernel internal uid to start with. |
| 277 | * |
| 278 | * Map @kuid into the user-namespace specified by @targ and |
| 279 | * return the resulting uid. |
| 280 | * |
| 281 | * There is always a mapping into the initial user_namespace. |
| 282 | * |
| 283 | * Unlike from_kuid from_kuid_munged never fails and always |
| 284 | * returns a valid uid. This makes from_kuid_munged appropriate |
| 285 | * for use in syscalls like stat and getuid where failing the |
| 286 | * system call and failing to provide a valid uid are not an |
| 287 | * options. |
| 288 | * |
| 289 | * If @kuid has no mapping in @targ overflowuid is returned. |
| 290 | */ |
| 291 | uid_t from_kuid_munged(struct user_namespace *targ, kuid_t kuid) |
| 292 | { |
| 293 | uid_t uid; |
| 294 | uid = from_kuid(targ, kuid); |
| 295 | |
| 296 | if (uid == (uid_t) -1) |
| 297 | uid = overflowuid; |
| 298 | return uid; |
| 299 | } |
| 300 | EXPORT_SYMBOL(from_kuid_munged); |
| 301 | |
| 302 | /** |
| 303 | * make_kgid - Map a user-namespace gid pair into a kgid. |
| 304 | * @ns: User namespace that the gid is in |
Fabian Frederick | 68a9a43 | 2014-06-06 14:37:21 -0700 | [diff] [blame] | 305 | * @gid: group identifier |
Eric W. Biederman | 22d917d | 2011-11-17 00:11:58 -0800 | [diff] [blame] | 306 | * |
| 307 | * Maps a user-namespace gid pair into a kernel internal kgid, |
| 308 | * and returns that kgid. |
| 309 | * |
| 310 | * When there is no mapping defined for the user-namespace gid |
| 311 | * pair INVALID_GID is returned. Callers are expected to test |
| 312 | * for and handle INVALID_GID being returned. INVALID_GID may be |
| 313 | * tested for using gid_valid(). |
| 314 | */ |
| 315 | kgid_t make_kgid(struct user_namespace *ns, gid_t gid) |
| 316 | { |
| 317 | /* Map the gid to a global kernel gid */ |
| 318 | return KGIDT_INIT(map_id_down(&ns->gid_map, gid)); |
| 319 | } |
| 320 | EXPORT_SYMBOL(make_kgid); |
| 321 | |
| 322 | /** |
| 323 | * from_kgid - Create a gid from a kgid user-namespace pair. |
| 324 | * @targ: The user namespace we want a gid in. |
| 325 | * @kgid: The kernel internal gid to start with. |
| 326 | * |
| 327 | * Map @kgid into the user-namespace specified by @targ and |
| 328 | * return the resulting gid. |
| 329 | * |
| 330 | * There is always a mapping into the initial user_namespace. |
| 331 | * |
| 332 | * If @kgid has no mapping in @targ (gid_t)-1 is returned. |
| 333 | */ |
| 334 | gid_t from_kgid(struct user_namespace *targ, kgid_t kgid) |
| 335 | { |
| 336 | /* Map the gid from a global kernel gid */ |
| 337 | return map_id_up(&targ->gid_map, __kgid_val(kgid)); |
| 338 | } |
| 339 | EXPORT_SYMBOL(from_kgid); |
| 340 | |
| 341 | /** |
| 342 | * from_kgid_munged - Create a gid from a kgid user-namespace pair. |
| 343 | * @targ: The user namespace we want a gid in. |
| 344 | * @kgid: The kernel internal gid to start with. |
| 345 | * |
| 346 | * Map @kgid into the user-namespace specified by @targ and |
| 347 | * return the resulting gid. |
| 348 | * |
| 349 | * There is always a mapping into the initial user_namespace. |
| 350 | * |
| 351 | * Unlike from_kgid from_kgid_munged never fails and always |
| 352 | * returns a valid gid. This makes from_kgid_munged appropriate |
| 353 | * for use in syscalls like stat and getgid where failing the |
| 354 | * system call and failing to provide a valid gid are not options. |
| 355 | * |
| 356 | * If @kgid has no mapping in @targ overflowgid is returned. |
| 357 | */ |
| 358 | gid_t from_kgid_munged(struct user_namespace *targ, kgid_t kgid) |
| 359 | { |
| 360 | gid_t gid; |
| 361 | gid = from_kgid(targ, kgid); |
| 362 | |
| 363 | if (gid == (gid_t) -1) |
| 364 | gid = overflowgid; |
| 365 | return gid; |
| 366 | } |
| 367 | EXPORT_SYMBOL(from_kgid_munged); |
| 368 | |
Eric W. Biederman | f76d207 | 2012-08-30 01:24:05 -0700 | [diff] [blame] | 369 | /** |
| 370 | * make_kprojid - Map a user-namespace projid pair into a kprojid. |
| 371 | * @ns: User namespace that the projid is in |
| 372 | * @projid: Project identifier |
| 373 | * |
| 374 | * Maps a user-namespace uid pair into a kernel internal kuid, |
| 375 | * and returns that kuid. |
| 376 | * |
| 377 | * When there is no mapping defined for the user-namespace projid |
| 378 | * pair INVALID_PROJID is returned. Callers are expected to test |
| 379 | * for and handle handle INVALID_PROJID being returned. INVALID_PROJID |
| 380 | * may be tested for using projid_valid(). |
| 381 | */ |
| 382 | kprojid_t make_kprojid(struct user_namespace *ns, projid_t projid) |
| 383 | { |
| 384 | /* Map the uid to a global kernel uid */ |
| 385 | return KPROJIDT_INIT(map_id_down(&ns->projid_map, projid)); |
| 386 | } |
| 387 | EXPORT_SYMBOL(make_kprojid); |
| 388 | |
| 389 | /** |
| 390 | * from_kprojid - Create a projid from a kprojid user-namespace pair. |
| 391 | * @targ: The user namespace we want a projid in. |
| 392 | * @kprojid: The kernel internal project identifier to start with. |
| 393 | * |
| 394 | * Map @kprojid into the user-namespace specified by @targ and |
| 395 | * return the resulting projid. |
| 396 | * |
| 397 | * There is always a mapping into the initial user_namespace. |
| 398 | * |
| 399 | * If @kprojid has no mapping in @targ (projid_t)-1 is returned. |
| 400 | */ |
| 401 | projid_t from_kprojid(struct user_namespace *targ, kprojid_t kprojid) |
| 402 | { |
| 403 | /* Map the uid from a global kernel uid */ |
| 404 | return map_id_up(&targ->projid_map, __kprojid_val(kprojid)); |
| 405 | } |
| 406 | EXPORT_SYMBOL(from_kprojid); |
| 407 | |
| 408 | /** |
| 409 | * from_kprojid_munged - Create a projiid from a kprojid user-namespace pair. |
| 410 | * @targ: The user namespace we want a projid in. |
| 411 | * @kprojid: The kernel internal projid to start with. |
| 412 | * |
| 413 | * Map @kprojid into the user-namespace specified by @targ and |
| 414 | * return the resulting projid. |
| 415 | * |
| 416 | * There is always a mapping into the initial user_namespace. |
| 417 | * |
| 418 | * Unlike from_kprojid from_kprojid_munged never fails and always |
| 419 | * returns a valid projid. This makes from_kprojid_munged |
| 420 | * appropriate for use in syscalls like stat and where |
| 421 | * failing the system call and failing to provide a valid projid are |
| 422 | * not an options. |
| 423 | * |
| 424 | * If @kprojid has no mapping in @targ OVERFLOW_PROJID is returned. |
| 425 | */ |
| 426 | projid_t from_kprojid_munged(struct user_namespace *targ, kprojid_t kprojid) |
| 427 | { |
| 428 | projid_t projid; |
| 429 | projid = from_kprojid(targ, kprojid); |
| 430 | |
| 431 | if (projid == (projid_t) -1) |
| 432 | projid = OVERFLOW_PROJID; |
| 433 | return projid; |
| 434 | } |
| 435 | EXPORT_SYMBOL(from_kprojid_munged); |
| 436 | |
| 437 | |
Eric W. Biederman | 22d917d | 2011-11-17 00:11:58 -0800 | [diff] [blame] | 438 | static int uid_m_show(struct seq_file *seq, void *v) |
| 439 | { |
| 440 | struct user_namespace *ns = seq->private; |
| 441 | struct uid_gid_extent *extent = v; |
| 442 | struct user_namespace *lower_ns; |
| 443 | uid_t lower; |
| 444 | |
Eric W. Biederman | c450f37 | 2012-08-14 21:25:13 -0700 | [diff] [blame] | 445 | lower_ns = seq_user_ns(seq); |
Eric W. Biederman | 22d917d | 2011-11-17 00:11:58 -0800 | [diff] [blame] | 446 | if ((lower_ns == ns) && lower_ns->parent) |
| 447 | lower_ns = lower_ns->parent; |
| 448 | |
| 449 | lower = from_kuid(lower_ns, KUIDT_INIT(extent->lower_first)); |
| 450 | |
| 451 | seq_printf(seq, "%10u %10u %10u\n", |
| 452 | extent->first, |
| 453 | lower, |
| 454 | extent->count); |
| 455 | |
| 456 | return 0; |
| 457 | } |
| 458 | |
| 459 | static int gid_m_show(struct seq_file *seq, void *v) |
| 460 | { |
| 461 | struct user_namespace *ns = seq->private; |
| 462 | struct uid_gid_extent *extent = v; |
| 463 | struct user_namespace *lower_ns; |
| 464 | gid_t lower; |
| 465 | |
Eric W. Biederman | c450f37 | 2012-08-14 21:25:13 -0700 | [diff] [blame] | 466 | lower_ns = seq_user_ns(seq); |
Eric W. Biederman | 22d917d | 2011-11-17 00:11:58 -0800 | [diff] [blame] | 467 | if ((lower_ns == ns) && lower_ns->parent) |
| 468 | lower_ns = lower_ns->parent; |
| 469 | |
| 470 | lower = from_kgid(lower_ns, KGIDT_INIT(extent->lower_first)); |
| 471 | |
| 472 | seq_printf(seq, "%10u %10u %10u\n", |
| 473 | extent->first, |
| 474 | lower, |
| 475 | extent->count); |
| 476 | |
| 477 | return 0; |
| 478 | } |
| 479 | |
Eric W. Biederman | f76d207 | 2012-08-30 01:24:05 -0700 | [diff] [blame] | 480 | static int projid_m_show(struct seq_file *seq, void *v) |
| 481 | { |
| 482 | struct user_namespace *ns = seq->private; |
| 483 | struct uid_gid_extent *extent = v; |
| 484 | struct user_namespace *lower_ns; |
| 485 | projid_t lower; |
| 486 | |
| 487 | lower_ns = seq_user_ns(seq); |
| 488 | if ((lower_ns == ns) && lower_ns->parent) |
| 489 | lower_ns = lower_ns->parent; |
| 490 | |
| 491 | lower = from_kprojid(lower_ns, KPROJIDT_INIT(extent->lower_first)); |
| 492 | |
| 493 | seq_printf(seq, "%10u %10u %10u\n", |
| 494 | extent->first, |
| 495 | lower, |
| 496 | extent->count); |
| 497 | |
| 498 | return 0; |
| 499 | } |
| 500 | |
Fabian Frederick | 68a9a43 | 2014-06-06 14:37:21 -0700 | [diff] [blame] | 501 | static void *m_start(struct seq_file *seq, loff_t *ppos, |
| 502 | struct uid_gid_map *map) |
Eric W. Biederman | 22d917d | 2011-11-17 00:11:58 -0800 | [diff] [blame] | 503 | { |
| 504 | struct uid_gid_extent *extent = NULL; |
| 505 | loff_t pos = *ppos; |
| 506 | |
| 507 | if (pos < map->nr_extents) |
| 508 | extent = &map->extent[pos]; |
| 509 | |
| 510 | return extent; |
| 511 | } |
| 512 | |
| 513 | static void *uid_m_start(struct seq_file *seq, loff_t *ppos) |
| 514 | { |
| 515 | struct user_namespace *ns = seq->private; |
| 516 | |
| 517 | return m_start(seq, ppos, &ns->uid_map); |
| 518 | } |
| 519 | |
| 520 | static void *gid_m_start(struct seq_file *seq, loff_t *ppos) |
| 521 | { |
| 522 | struct user_namespace *ns = seq->private; |
| 523 | |
| 524 | return m_start(seq, ppos, &ns->gid_map); |
| 525 | } |
| 526 | |
Eric W. Biederman | f76d207 | 2012-08-30 01:24:05 -0700 | [diff] [blame] | 527 | static void *projid_m_start(struct seq_file *seq, loff_t *ppos) |
| 528 | { |
| 529 | struct user_namespace *ns = seq->private; |
| 530 | |
| 531 | return m_start(seq, ppos, &ns->projid_map); |
| 532 | } |
| 533 | |
Eric W. Biederman | 22d917d | 2011-11-17 00:11:58 -0800 | [diff] [blame] | 534 | static void *m_next(struct seq_file *seq, void *v, loff_t *pos) |
| 535 | { |
| 536 | (*pos)++; |
| 537 | return seq->op->start(seq, pos); |
| 538 | } |
| 539 | |
| 540 | static void m_stop(struct seq_file *seq, void *v) |
| 541 | { |
| 542 | return; |
| 543 | } |
| 544 | |
Fabian Frederick | ccf94f1 | 2014-08-08 14:21:22 -0700 | [diff] [blame] | 545 | const struct seq_operations proc_uid_seq_operations = { |
Eric W. Biederman | 22d917d | 2011-11-17 00:11:58 -0800 | [diff] [blame] | 546 | .start = uid_m_start, |
| 547 | .stop = m_stop, |
| 548 | .next = m_next, |
| 549 | .show = uid_m_show, |
| 550 | }; |
| 551 | |
Fabian Frederick | ccf94f1 | 2014-08-08 14:21:22 -0700 | [diff] [blame] | 552 | const struct seq_operations proc_gid_seq_operations = { |
Eric W. Biederman | 22d917d | 2011-11-17 00:11:58 -0800 | [diff] [blame] | 553 | .start = gid_m_start, |
| 554 | .stop = m_stop, |
| 555 | .next = m_next, |
| 556 | .show = gid_m_show, |
| 557 | }; |
| 558 | |
Fabian Frederick | ccf94f1 | 2014-08-08 14:21:22 -0700 | [diff] [blame] | 559 | const struct seq_operations proc_projid_seq_operations = { |
Eric W. Biederman | f76d207 | 2012-08-30 01:24:05 -0700 | [diff] [blame] | 560 | .start = projid_m_start, |
| 561 | .stop = m_stop, |
| 562 | .next = m_next, |
| 563 | .show = projid_m_show, |
| 564 | }; |
| 565 | |
Fabian Frederick | 68a9a43 | 2014-06-06 14:37:21 -0700 | [diff] [blame] | 566 | static bool mappings_overlap(struct uid_gid_map *new_map, |
| 567 | struct uid_gid_extent *extent) |
Eric W. Biederman | 0bd14b4 | 2012-12-27 22:27:29 -0800 | [diff] [blame] | 568 | { |
| 569 | u32 upper_first, lower_first, upper_last, lower_last; |
| 570 | unsigned idx; |
| 571 | |
| 572 | upper_first = extent->first; |
| 573 | lower_first = extent->lower_first; |
| 574 | upper_last = upper_first + extent->count - 1; |
| 575 | lower_last = lower_first + extent->count - 1; |
| 576 | |
| 577 | for (idx = 0; idx < new_map->nr_extents; idx++) { |
| 578 | u32 prev_upper_first, prev_lower_first; |
| 579 | u32 prev_upper_last, prev_lower_last; |
| 580 | struct uid_gid_extent *prev; |
| 581 | |
| 582 | prev = &new_map->extent[idx]; |
| 583 | |
| 584 | prev_upper_first = prev->first; |
| 585 | prev_lower_first = prev->lower_first; |
| 586 | prev_upper_last = prev_upper_first + prev->count - 1; |
| 587 | prev_lower_last = prev_lower_first + prev->count - 1; |
| 588 | |
| 589 | /* Does the upper range intersect a previous extent? */ |
| 590 | if ((prev_upper_first <= upper_last) && |
| 591 | (prev_upper_last >= upper_first)) |
| 592 | return true; |
| 593 | |
| 594 | /* Does the lower range intersect a previous extent? */ |
| 595 | if ((prev_lower_first <= lower_last) && |
| 596 | (prev_lower_last >= lower_first)) |
| 597 | return true; |
| 598 | } |
| 599 | return false; |
| 600 | } |
| 601 | |
Eric W. Biederman | 22d917d | 2011-11-17 00:11:58 -0800 | [diff] [blame] | 602 | static ssize_t map_write(struct file *file, const char __user *buf, |
| 603 | size_t count, loff_t *ppos, |
| 604 | int cap_setid, |
| 605 | struct uid_gid_map *map, |
| 606 | struct uid_gid_map *parent_map) |
| 607 | { |
| 608 | struct seq_file *seq = file->private_data; |
| 609 | struct user_namespace *ns = seq->private; |
| 610 | struct uid_gid_map new_map; |
| 611 | unsigned idx; |
Eric W. Biederman | 0bd14b4 | 2012-12-27 22:27:29 -0800 | [diff] [blame] | 612 | struct uid_gid_extent *extent = NULL; |
Al Viro | 70f6cbb | 2015-12-24 00:13:10 -0500 | [diff] [blame] | 613 | char *kbuf = NULL, *pos, *next_line; |
Eric W. Biederman | 22d917d | 2011-11-17 00:11:58 -0800 | [diff] [blame] | 614 | ssize_t ret = -EINVAL; |
| 615 | |
| 616 | /* |
Eric W. Biederman | f0d62ae | 2014-12-09 14:03:14 -0600 | [diff] [blame] | 617 | * The userns_state_mutex serializes all writes to any given map. |
Eric W. Biederman | 22d917d | 2011-11-17 00:11:58 -0800 | [diff] [blame] | 618 | * |
| 619 | * Any map is only ever written once. |
| 620 | * |
| 621 | * An id map fits within 1 cache line on most architectures. |
| 622 | * |
| 623 | * On read nothing needs to be done unless you are on an |
| 624 | * architecture with a crazy cache coherency model like alpha. |
| 625 | * |
| 626 | * There is a one time data dependency between reading the |
| 627 | * count of the extents and the values of the extents. The |
| 628 | * desired behavior is to see the values of the extents that |
| 629 | * were written before the count of the extents. |
| 630 | * |
| 631 | * To achieve this smp_wmb() is used on guarantee the write |
Mikulas Patocka | e79323b | 2014-04-14 16:58:55 -0400 | [diff] [blame] | 632 | * order and smp_rmb() is guaranteed that we don't have crazy |
| 633 | * architectures returning stale data. |
Eric W. Biederman | 5c1469d | 2010-06-13 03:28:03 +0000 | [diff] [blame] | 634 | */ |
Eric W. Biederman | f0d62ae | 2014-12-09 14:03:14 -0600 | [diff] [blame] | 635 | mutex_lock(&userns_state_mutex); |
Eric W. Biederman | 22d917d | 2011-11-17 00:11:58 -0800 | [diff] [blame] | 636 | |
| 637 | ret = -EPERM; |
| 638 | /* Only allow one successful write to the map */ |
| 639 | if (map->nr_extents != 0) |
| 640 | goto out; |
| 641 | |
Andy Lutomirski | 41c21e3 | 2013-04-14 11:44:04 -0700 | [diff] [blame] | 642 | /* |
| 643 | * Adjusting namespace settings requires capabilities on the target. |
Eric W. Biederman | 22d917d | 2011-11-17 00:11:58 -0800 | [diff] [blame] | 644 | */ |
Andy Lutomirski | 41c21e3 | 2013-04-14 11:44:04 -0700 | [diff] [blame] | 645 | if (cap_valid(cap_setid) && !file_ns_capable(file, ns, CAP_SYS_ADMIN)) |
Eric W. Biederman | 22d917d | 2011-11-17 00:11:58 -0800 | [diff] [blame] | 646 | goto out; |
| 647 | |
Eric W. Biederman | 36476be | 2014-12-05 20:03:28 -0600 | [diff] [blame] | 648 | /* Only allow < page size writes at the beginning of the file */ |
Eric W. Biederman | 22d917d | 2011-11-17 00:11:58 -0800 | [diff] [blame] | 649 | ret = -EINVAL; |
| 650 | if ((*ppos != 0) || (count >= PAGE_SIZE)) |
| 651 | goto out; |
| 652 | |
| 653 | /* Slurp in the user data */ |
Al Viro | 70f6cbb | 2015-12-24 00:13:10 -0500 | [diff] [blame] | 654 | kbuf = memdup_user_nul(buf, count); |
| 655 | if (IS_ERR(kbuf)) { |
| 656 | ret = PTR_ERR(kbuf); |
| 657 | kbuf = NULL; |
Eric W. Biederman | 22d917d | 2011-11-17 00:11:58 -0800 | [diff] [blame] | 658 | goto out; |
Al Viro | 70f6cbb | 2015-12-24 00:13:10 -0500 | [diff] [blame] | 659 | } |
Eric W. Biederman | 22d917d | 2011-11-17 00:11:58 -0800 | [diff] [blame] | 660 | |
| 661 | /* Parse the user data */ |
| 662 | ret = -EINVAL; |
| 663 | pos = kbuf; |
| 664 | new_map.nr_extents = 0; |
Fabian Frederick | 68a9a43 | 2014-06-06 14:37:21 -0700 | [diff] [blame] | 665 | for (; pos; pos = next_line) { |
Eric W. Biederman | 22d917d | 2011-11-17 00:11:58 -0800 | [diff] [blame] | 666 | extent = &new_map.extent[new_map.nr_extents]; |
| 667 | |
| 668 | /* Find the end of line and ensure I don't look past it */ |
| 669 | next_line = strchr(pos, '\n'); |
| 670 | if (next_line) { |
| 671 | *next_line = '\0'; |
| 672 | next_line++; |
| 673 | if (*next_line == '\0') |
| 674 | next_line = NULL; |
Eric W. Biederman | 5c1469d | 2010-06-13 03:28:03 +0000 | [diff] [blame] | 675 | } |
Eric W. Biederman | 22d917d | 2011-11-17 00:11:58 -0800 | [diff] [blame] | 676 | |
| 677 | pos = skip_spaces(pos); |
| 678 | extent->first = simple_strtoul(pos, &pos, 10); |
| 679 | if (!isspace(*pos)) |
| 680 | goto out; |
| 681 | |
| 682 | pos = skip_spaces(pos); |
| 683 | extent->lower_first = simple_strtoul(pos, &pos, 10); |
| 684 | if (!isspace(*pos)) |
| 685 | goto out; |
| 686 | |
| 687 | pos = skip_spaces(pos); |
| 688 | extent->count = simple_strtoul(pos, &pos, 10); |
| 689 | if (*pos && !isspace(*pos)) |
| 690 | goto out; |
| 691 | |
| 692 | /* Verify there is not trailing junk on the line */ |
| 693 | pos = skip_spaces(pos); |
| 694 | if (*pos != '\0') |
| 695 | goto out; |
| 696 | |
| 697 | /* Verify we have been given valid starting values */ |
| 698 | if ((extent->first == (u32) -1) || |
Fabian Frederick | 68a9a43 | 2014-06-06 14:37:21 -0700 | [diff] [blame] | 699 | (extent->lower_first == (u32) -1)) |
Eric W. Biederman | 22d917d | 2011-11-17 00:11:58 -0800 | [diff] [blame] | 700 | goto out; |
| 701 | |
Fabian Frederick | 68a9a43 | 2014-06-06 14:37:21 -0700 | [diff] [blame] | 702 | /* Verify count is not zero and does not cause the |
| 703 | * extent to wrap |
| 704 | */ |
Eric W. Biederman | 22d917d | 2011-11-17 00:11:58 -0800 | [diff] [blame] | 705 | if ((extent->first + extent->count) <= extent->first) |
| 706 | goto out; |
Fabian Frederick | 68a9a43 | 2014-06-06 14:37:21 -0700 | [diff] [blame] | 707 | if ((extent->lower_first + extent->count) <= |
| 708 | extent->lower_first) |
Eric W. Biederman | 22d917d | 2011-11-17 00:11:58 -0800 | [diff] [blame] | 709 | goto out; |
| 710 | |
Eric W. Biederman | 0bd14b4 | 2012-12-27 22:27:29 -0800 | [diff] [blame] | 711 | /* Do the ranges in extent overlap any previous extents? */ |
| 712 | if (mappings_overlap(&new_map, extent)) |
Eric W. Biederman | 22d917d | 2011-11-17 00:11:58 -0800 | [diff] [blame] | 713 | goto out; |
| 714 | |
| 715 | new_map.nr_extents++; |
Eric W. Biederman | 22d917d | 2011-11-17 00:11:58 -0800 | [diff] [blame] | 716 | |
| 717 | /* Fail if the file contains too many extents */ |
| 718 | if ((new_map.nr_extents == UID_GID_MAP_MAX_EXTENTS) && |
| 719 | (next_line != NULL)) |
| 720 | goto out; |
| 721 | } |
| 722 | /* Be very certaint the new map actually exists */ |
| 723 | if (new_map.nr_extents == 0) |
| 724 | goto out; |
| 725 | |
| 726 | ret = -EPERM; |
| 727 | /* Validate the user is allowed to use user id's mapped to. */ |
Eric W. Biederman | 6708075 | 2013-04-14 13:47:02 -0700 | [diff] [blame] | 728 | if (!new_idmap_permitted(file, ns, cap_setid, &new_map)) |
Eric W. Biederman | 22d917d | 2011-11-17 00:11:58 -0800 | [diff] [blame] | 729 | goto out; |
| 730 | |
| 731 | /* Map the lower ids from the parent user namespace to the |
| 732 | * kernel global id space. |
| 733 | */ |
| 734 | for (idx = 0; idx < new_map.nr_extents; idx++) { |
| 735 | u32 lower_first; |
| 736 | extent = &new_map.extent[idx]; |
| 737 | |
| 738 | lower_first = map_id_range_down(parent_map, |
| 739 | extent->lower_first, |
| 740 | extent->count); |
| 741 | |
| 742 | /* Fail if we can not map the specified extent to |
| 743 | * the kernel global id space. |
| 744 | */ |
| 745 | if (lower_first == (u32) -1) |
| 746 | goto out; |
| 747 | |
| 748 | extent->lower_first = lower_first; |
Eric W. Biederman | 5c1469d | 2010-06-13 03:28:03 +0000 | [diff] [blame] | 749 | } |
| 750 | |
Eric W. Biederman | 22d917d | 2011-11-17 00:11:58 -0800 | [diff] [blame] | 751 | /* Install the map */ |
| 752 | memcpy(map->extent, new_map.extent, |
| 753 | new_map.nr_extents*sizeof(new_map.extent[0])); |
| 754 | smp_wmb(); |
| 755 | map->nr_extents = new_map.nr_extents; |
| 756 | |
| 757 | *ppos = count; |
| 758 | ret = count; |
| 759 | out: |
Eric W. Biederman | f0d62ae | 2014-12-09 14:03:14 -0600 | [diff] [blame] | 760 | mutex_unlock(&userns_state_mutex); |
Al Viro | 70f6cbb | 2015-12-24 00:13:10 -0500 | [diff] [blame] | 761 | kfree(kbuf); |
Eric W. Biederman | 22d917d | 2011-11-17 00:11:58 -0800 | [diff] [blame] | 762 | return ret; |
| 763 | } |
| 764 | |
Fabian Frederick | 68a9a43 | 2014-06-06 14:37:21 -0700 | [diff] [blame] | 765 | ssize_t proc_uid_map_write(struct file *file, const char __user *buf, |
| 766 | size_t size, loff_t *ppos) |
Eric W. Biederman | 22d917d | 2011-11-17 00:11:58 -0800 | [diff] [blame] | 767 | { |
| 768 | struct seq_file *seq = file->private_data; |
| 769 | struct user_namespace *ns = seq->private; |
Eric W. Biederman | c450f37 | 2012-08-14 21:25:13 -0700 | [diff] [blame] | 770 | struct user_namespace *seq_ns = seq_user_ns(seq); |
Eric W. Biederman | 22d917d | 2011-11-17 00:11:58 -0800 | [diff] [blame] | 771 | |
| 772 | if (!ns->parent) |
| 773 | return -EPERM; |
| 774 | |
Eric W. Biederman | c450f37 | 2012-08-14 21:25:13 -0700 | [diff] [blame] | 775 | if ((seq_ns != ns) && (seq_ns != ns->parent)) |
| 776 | return -EPERM; |
| 777 | |
Eric W. Biederman | 22d917d | 2011-11-17 00:11:58 -0800 | [diff] [blame] | 778 | return map_write(file, buf, size, ppos, CAP_SETUID, |
| 779 | &ns->uid_map, &ns->parent->uid_map); |
| 780 | } |
| 781 | |
Fabian Frederick | 68a9a43 | 2014-06-06 14:37:21 -0700 | [diff] [blame] | 782 | ssize_t proc_gid_map_write(struct file *file, const char __user *buf, |
| 783 | size_t size, loff_t *ppos) |
Eric W. Biederman | 22d917d | 2011-11-17 00:11:58 -0800 | [diff] [blame] | 784 | { |
| 785 | struct seq_file *seq = file->private_data; |
| 786 | struct user_namespace *ns = seq->private; |
Eric W. Biederman | c450f37 | 2012-08-14 21:25:13 -0700 | [diff] [blame] | 787 | struct user_namespace *seq_ns = seq_user_ns(seq); |
Eric W. Biederman | 22d917d | 2011-11-17 00:11:58 -0800 | [diff] [blame] | 788 | |
| 789 | if (!ns->parent) |
| 790 | return -EPERM; |
| 791 | |
Eric W. Biederman | c450f37 | 2012-08-14 21:25:13 -0700 | [diff] [blame] | 792 | if ((seq_ns != ns) && (seq_ns != ns->parent)) |
| 793 | return -EPERM; |
| 794 | |
Eric W. Biederman | 22d917d | 2011-11-17 00:11:58 -0800 | [diff] [blame] | 795 | return map_write(file, buf, size, ppos, CAP_SETGID, |
| 796 | &ns->gid_map, &ns->parent->gid_map); |
| 797 | } |
| 798 | |
Fabian Frederick | 68a9a43 | 2014-06-06 14:37:21 -0700 | [diff] [blame] | 799 | ssize_t proc_projid_map_write(struct file *file, const char __user *buf, |
| 800 | size_t size, loff_t *ppos) |
Eric W. Biederman | f76d207 | 2012-08-30 01:24:05 -0700 | [diff] [blame] | 801 | { |
| 802 | struct seq_file *seq = file->private_data; |
| 803 | struct user_namespace *ns = seq->private; |
| 804 | struct user_namespace *seq_ns = seq_user_ns(seq); |
| 805 | |
| 806 | if (!ns->parent) |
| 807 | return -EPERM; |
| 808 | |
| 809 | if ((seq_ns != ns) && (seq_ns != ns->parent)) |
| 810 | return -EPERM; |
| 811 | |
| 812 | /* Anyone can set any valid project id no capability needed */ |
| 813 | return map_write(file, buf, size, ppos, -1, |
| 814 | &ns->projid_map, &ns->parent->projid_map); |
| 815 | } |
| 816 | |
Fabian Frederick | 68a9a43 | 2014-06-06 14:37:21 -0700 | [diff] [blame] | 817 | static bool new_idmap_permitted(const struct file *file, |
Eric W. Biederman | 6708075 | 2013-04-14 13:47:02 -0700 | [diff] [blame] | 818 | struct user_namespace *ns, int cap_setid, |
Eric W. Biederman | 22d917d | 2011-11-17 00:11:58 -0800 | [diff] [blame] | 819 | struct uid_gid_map *new_map) |
| 820 | { |
Eric W. Biederman | f95d791 | 2014-11-26 23:22:14 -0600 | [diff] [blame] | 821 | const struct cred *cred = file->f_cred; |
Eric W. Biederman | 0542f17 | 2014-12-05 17:51:47 -0600 | [diff] [blame] | 822 | /* Don't allow mappings that would allow anything that wouldn't |
| 823 | * be allowed without the establishment of unprivileged mappings. |
| 824 | */ |
Eric W. Biederman | f95d791 | 2014-11-26 23:22:14 -0600 | [diff] [blame] | 825 | if ((new_map->nr_extents == 1) && (new_map->extent[0].count == 1) && |
| 826 | uid_eq(ns->owner, cred->euid)) { |
Eric W. Biederman | 37657da | 2012-07-27 06:21:27 -0700 | [diff] [blame] | 827 | u32 id = new_map->extent[0].lower_first; |
| 828 | if (cap_setid == CAP_SETUID) { |
| 829 | kuid_t uid = make_kuid(ns->parent, id); |
Eric W. Biederman | f95d791 | 2014-11-26 23:22:14 -0600 | [diff] [blame] | 830 | if (uid_eq(uid, cred->euid)) |
Eric W. Biederman | 37657da | 2012-07-27 06:21:27 -0700 | [diff] [blame] | 831 | return true; |
Fabian Frederick | 68a9a43 | 2014-06-06 14:37:21 -0700 | [diff] [blame] | 832 | } else if (cap_setid == CAP_SETGID) { |
Eric W. Biederman | 37657da | 2012-07-27 06:21:27 -0700 | [diff] [blame] | 833 | kgid_t gid = make_kgid(ns->parent, id); |
Eric W. Biederman | 66d2f33 | 2014-12-05 19:36:04 -0600 | [diff] [blame] | 834 | if (!(ns->flags & USERNS_SETGROUPS_ALLOWED) && |
| 835 | gid_eq(gid, cred->egid)) |
Eric W. Biederman | 37657da | 2012-07-27 06:21:27 -0700 | [diff] [blame] | 836 | return true; |
| 837 | } |
| 838 | } |
| 839 | |
Eric W. Biederman | f76d207 | 2012-08-30 01:24:05 -0700 | [diff] [blame] | 840 | /* Allow anyone to set a mapping that doesn't require privilege */ |
| 841 | if (!cap_valid(cap_setid)) |
| 842 | return true; |
| 843 | |
Eric W. Biederman | 22d917d | 2011-11-17 00:11:58 -0800 | [diff] [blame] | 844 | /* Allow the specified ids if we have the appropriate capability |
| 845 | * (CAP_SETUID or CAP_SETGID) over the parent user namespace. |
Eric W. Biederman | 6708075 | 2013-04-14 13:47:02 -0700 | [diff] [blame] | 846 | * And the opener of the id file also had the approprpiate capability. |
Eric W. Biederman | 22d917d | 2011-11-17 00:11:58 -0800 | [diff] [blame] | 847 | */ |
Eric W. Biederman | 6708075 | 2013-04-14 13:47:02 -0700 | [diff] [blame] | 848 | if (ns_capable(ns->parent, cap_setid) && |
| 849 | file_ns_capable(file, ns->parent, cap_setid)) |
Eric W. Biederman | 22d917d | 2011-11-17 00:11:58 -0800 | [diff] [blame] | 850 | return true; |
| 851 | |
| 852 | return false; |
Eric W. Biederman | 5c1469d | 2010-06-13 03:28:03 +0000 | [diff] [blame] | 853 | } |
Pavel Emelyanov | 6164281 | 2011-01-12 17:00:46 -0800 | [diff] [blame] | 854 | |
Eric W. Biederman | 9cc4651 | 2014-12-02 12:27:26 -0600 | [diff] [blame] | 855 | int proc_setgroups_show(struct seq_file *seq, void *v) |
| 856 | { |
| 857 | struct user_namespace *ns = seq->private; |
| 858 | unsigned long userns_flags = ACCESS_ONCE(ns->flags); |
| 859 | |
| 860 | seq_printf(seq, "%s\n", |
| 861 | (userns_flags & USERNS_SETGROUPS_ALLOWED) ? |
| 862 | "allow" : "deny"); |
| 863 | return 0; |
| 864 | } |
| 865 | |
| 866 | ssize_t proc_setgroups_write(struct file *file, const char __user *buf, |
| 867 | size_t count, loff_t *ppos) |
| 868 | { |
| 869 | struct seq_file *seq = file->private_data; |
| 870 | struct user_namespace *ns = seq->private; |
| 871 | char kbuf[8], *pos; |
| 872 | bool setgroups_allowed; |
| 873 | ssize_t ret; |
| 874 | |
| 875 | /* Only allow a very narrow range of strings to be written */ |
| 876 | ret = -EINVAL; |
| 877 | if ((*ppos != 0) || (count >= sizeof(kbuf))) |
| 878 | goto out; |
| 879 | |
| 880 | /* What was written? */ |
| 881 | ret = -EFAULT; |
| 882 | if (copy_from_user(kbuf, buf, count)) |
| 883 | goto out; |
| 884 | kbuf[count] = '\0'; |
| 885 | pos = kbuf; |
| 886 | |
| 887 | /* What is being requested? */ |
| 888 | ret = -EINVAL; |
| 889 | if (strncmp(pos, "allow", 5) == 0) { |
| 890 | pos += 5; |
| 891 | setgroups_allowed = true; |
| 892 | } |
| 893 | else if (strncmp(pos, "deny", 4) == 0) { |
| 894 | pos += 4; |
| 895 | setgroups_allowed = false; |
| 896 | } |
| 897 | else |
| 898 | goto out; |
| 899 | |
| 900 | /* Verify there is not trailing junk on the line */ |
| 901 | pos = skip_spaces(pos); |
| 902 | if (*pos != '\0') |
| 903 | goto out; |
| 904 | |
| 905 | ret = -EPERM; |
| 906 | mutex_lock(&userns_state_mutex); |
| 907 | if (setgroups_allowed) { |
| 908 | /* Enabling setgroups after setgroups has been disabled |
| 909 | * is not allowed. |
| 910 | */ |
| 911 | if (!(ns->flags & USERNS_SETGROUPS_ALLOWED)) |
| 912 | goto out_unlock; |
| 913 | } else { |
| 914 | /* Permanently disabling setgroups after setgroups has |
| 915 | * been enabled by writing the gid_map is not allowed. |
| 916 | */ |
| 917 | if (ns->gid_map.nr_extents != 0) |
| 918 | goto out_unlock; |
| 919 | ns->flags &= ~USERNS_SETGROUPS_ALLOWED; |
| 920 | } |
| 921 | mutex_unlock(&userns_state_mutex); |
| 922 | |
| 923 | /* Report a successful write */ |
| 924 | *ppos = count; |
| 925 | ret = count; |
| 926 | out: |
| 927 | return ret; |
| 928 | out_unlock: |
| 929 | mutex_unlock(&userns_state_mutex); |
| 930 | goto out; |
| 931 | } |
| 932 | |
Eric W. Biederman | 273d2c6 | 2014-12-05 18:01:11 -0600 | [diff] [blame] | 933 | bool userns_may_setgroups(const struct user_namespace *ns) |
| 934 | { |
| 935 | bool allowed; |
| 936 | |
Eric W. Biederman | f0d62ae | 2014-12-09 14:03:14 -0600 | [diff] [blame] | 937 | mutex_lock(&userns_state_mutex); |
Eric W. Biederman | 273d2c6 | 2014-12-05 18:01:11 -0600 | [diff] [blame] | 938 | /* It is not safe to use setgroups until a gid mapping in |
| 939 | * the user namespace has been established. |
| 940 | */ |
| 941 | allowed = ns->gid_map.nr_extents != 0; |
Eric W. Biederman | 9cc4651 | 2014-12-02 12:27:26 -0600 | [diff] [blame] | 942 | /* Is setgroups allowed? */ |
| 943 | allowed = allowed && (ns->flags & USERNS_SETGROUPS_ALLOWED); |
Eric W. Biederman | f0d62ae | 2014-12-09 14:03:14 -0600 | [diff] [blame] | 944 | mutex_unlock(&userns_state_mutex); |
Eric W. Biederman | 273d2c6 | 2014-12-05 18:01:11 -0600 | [diff] [blame] | 945 | |
| 946 | return allowed; |
| 947 | } |
| 948 | |
Seth Forshee | d07b846 | 2015-09-23 15:16:04 -0500 | [diff] [blame] | 949 | /* |
| 950 | * Returns true if @ns is the same namespace as or a descendant of |
| 951 | * @target_ns. |
| 952 | */ |
| 953 | bool current_in_userns(const struct user_namespace *target_ns) |
| 954 | { |
| 955 | struct user_namespace *ns; |
| 956 | for (ns = current_user_ns(); ns; ns = ns->parent) { |
| 957 | if (ns == target_ns) |
| 958 | return true; |
| 959 | } |
| 960 | return false; |
| 961 | } |
| 962 | |
Al Viro | 3c04118 | 2014-11-01 00:25:30 -0400 | [diff] [blame] | 963 | static inline struct user_namespace *to_user_ns(struct ns_common *ns) |
| 964 | { |
| 965 | return container_of(ns, struct user_namespace, ns); |
| 966 | } |
| 967 | |
Al Viro | 6496452 | 2014-11-01 00:37:32 -0400 | [diff] [blame] | 968 | static struct ns_common *userns_get(struct task_struct *task) |
Eric W. Biederman | cde1975 | 2012-07-26 06:24:06 -0700 | [diff] [blame] | 969 | { |
| 970 | struct user_namespace *user_ns; |
| 971 | |
| 972 | rcu_read_lock(); |
| 973 | user_ns = get_user_ns(__task_cred(task)->user_ns); |
| 974 | rcu_read_unlock(); |
| 975 | |
Al Viro | 3c04118 | 2014-11-01 00:25:30 -0400 | [diff] [blame] | 976 | return user_ns ? &user_ns->ns : NULL; |
Eric W. Biederman | cde1975 | 2012-07-26 06:24:06 -0700 | [diff] [blame] | 977 | } |
| 978 | |
Al Viro | 6496452 | 2014-11-01 00:37:32 -0400 | [diff] [blame] | 979 | static void userns_put(struct ns_common *ns) |
Eric W. Biederman | cde1975 | 2012-07-26 06:24:06 -0700 | [diff] [blame] | 980 | { |
Al Viro | 3c04118 | 2014-11-01 00:25:30 -0400 | [diff] [blame] | 981 | put_user_ns(to_user_ns(ns)); |
Eric W. Biederman | cde1975 | 2012-07-26 06:24:06 -0700 | [diff] [blame] | 982 | } |
| 983 | |
Al Viro | 6496452 | 2014-11-01 00:37:32 -0400 | [diff] [blame] | 984 | static int userns_install(struct nsproxy *nsproxy, struct ns_common *ns) |
Eric W. Biederman | cde1975 | 2012-07-26 06:24:06 -0700 | [diff] [blame] | 985 | { |
Al Viro | 3c04118 | 2014-11-01 00:25:30 -0400 | [diff] [blame] | 986 | struct user_namespace *user_ns = to_user_ns(ns); |
Eric W. Biederman | cde1975 | 2012-07-26 06:24:06 -0700 | [diff] [blame] | 987 | struct cred *cred; |
| 988 | |
| 989 | /* Don't allow gaining capabilities by reentering |
| 990 | * the same user namespace. |
| 991 | */ |
| 992 | if (user_ns == current_user_ns()) |
| 993 | return -EINVAL; |
| 994 | |
Eric W. Biederman | faf00da | 2015-08-10 18:25:44 -0500 | [diff] [blame] | 995 | /* Tasks that share a thread group must share a user namespace */ |
| 996 | if (!thread_group_empty(current)) |
Eric W. Biederman | cde1975 | 2012-07-26 06:24:06 -0700 | [diff] [blame] | 997 | return -EINVAL; |
| 998 | |
Eric W. Biederman | e66eded | 2013-03-13 11:51:49 -0700 | [diff] [blame] | 999 | if (current->fs->users != 1) |
| 1000 | return -EINVAL; |
| 1001 | |
Eric W. Biederman | cde1975 | 2012-07-26 06:24:06 -0700 | [diff] [blame] | 1002 | if (!ns_capable(user_ns, CAP_SYS_ADMIN)) |
| 1003 | return -EPERM; |
| 1004 | |
| 1005 | cred = prepare_creds(); |
| 1006 | if (!cred) |
| 1007 | return -ENOMEM; |
| 1008 | |
| 1009 | put_user_ns(cred->user_ns); |
| 1010 | set_cred_user_ns(cred, get_user_ns(user_ns)); |
| 1011 | |
| 1012 | return commit_creds(cred); |
| 1013 | } |
| 1014 | |
| 1015 | const struct proc_ns_operations userns_operations = { |
| 1016 | .name = "user", |
| 1017 | .type = CLONE_NEWUSER, |
| 1018 | .get = userns_get, |
| 1019 | .put = userns_put, |
| 1020 | .install = userns_install, |
| 1021 | }; |
| 1022 | |
Pavel Emelyanov | 6164281 | 2011-01-12 17:00:46 -0800 | [diff] [blame] | 1023 | static __init int user_namespaces_init(void) |
| 1024 | { |
| 1025 | user_ns_cachep = KMEM_CACHE(user_namespace, SLAB_PANIC); |
| 1026 | return 0; |
| 1027 | } |
Paul Gortmaker | c96d666 | 2014-04-03 14:48:35 -0700 | [diff] [blame] | 1028 | subsys_initcall(user_namespaces_init); |