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