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 | |
| 8 | #include <linux/module.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> |
Eric W. Biederman | 5c1469d | 2010-06-13 03:28:03 +0000 | [diff] [blame] | 12 | #include <linux/highuid.h> |
Serge Hallyn | 18b6e04 | 2008-10-15 16:38:45 -0500 | [diff] [blame] | 13 | #include <linux/cred.h> |
Cedric Le Goater | acce292 | 2007-07-15 23:40:59 -0700 | [diff] [blame] | 14 | |
Serge E. Hallyn | 77ec739 | 2007-07-15 23:41:01 -0700 | [diff] [blame] | 15 | /* |
Serge Hallyn | 18b6e04 | 2008-10-15 16:38:45 -0500 | [diff] [blame] | 16 | * Create a new user namespace, deriving the creator from the user in the |
| 17 | * passed credentials, and replacing that user with the new root user for the |
| 18 | * new namespace. |
| 19 | * |
| 20 | * This is called by copy_creds(), which will finish setting the target task's |
| 21 | * credentials. |
Serge E. Hallyn | 77ec739 | 2007-07-15 23:41:01 -0700 | [diff] [blame] | 22 | */ |
Serge Hallyn | 18b6e04 | 2008-10-15 16:38:45 -0500 | [diff] [blame] | 23 | int create_user_ns(struct cred *new) |
Serge E. Hallyn | 77ec739 | 2007-07-15 23:41:01 -0700 | [diff] [blame] | 24 | { |
| 25 | struct user_namespace *ns; |
Serge Hallyn | 18b6e04 | 2008-10-15 16:38:45 -0500 | [diff] [blame] | 26 | struct user_struct *root_user; |
Serge E. Hallyn | 77ec739 | 2007-07-15 23:41:01 -0700 | [diff] [blame] | 27 | int n; |
| 28 | |
| 29 | ns = kmalloc(sizeof(struct user_namespace), GFP_KERNEL); |
| 30 | if (!ns) |
Serge Hallyn | 18b6e04 | 2008-10-15 16:38:45 -0500 | [diff] [blame] | 31 | return -ENOMEM; |
Serge E. Hallyn | 77ec739 | 2007-07-15 23:41:01 -0700 | [diff] [blame] | 32 | |
| 33 | kref_init(&ns->kref); |
| 34 | |
| 35 | for (n = 0; n < UIDHASH_SZ; ++n) |
Pavel Emelyanov | 735de22 | 2007-09-18 22:46:44 -0700 | [diff] [blame] | 36 | INIT_HLIST_HEAD(ns->uidhash_table + n); |
Serge E. Hallyn | 77ec739 | 2007-07-15 23:41:01 -0700 | [diff] [blame] | 37 | |
Serge Hallyn | 18b6e04 | 2008-10-15 16:38:45 -0500 | [diff] [blame] | 38 | /* Alloc new root user. */ |
| 39 | root_user = alloc_uid(ns, 0); |
| 40 | if (!root_user) { |
Serge E. Hallyn | 77ec739 | 2007-07-15 23:41:01 -0700 | [diff] [blame] | 41 | kfree(ns); |
Serge Hallyn | 18b6e04 | 2008-10-15 16:38:45 -0500 | [diff] [blame] | 42 | return -ENOMEM; |
Serge E. Hallyn | 77ec739 | 2007-07-15 23:41:01 -0700 | [diff] [blame] | 43 | } |
| 44 | |
Serge Hallyn | 18b6e04 | 2008-10-15 16:38:45 -0500 | [diff] [blame] | 45 | /* set the new root user in the credentials under preparation */ |
| 46 | ns->creator = new->user; |
| 47 | new->user = root_user; |
| 48 | new->uid = new->euid = new->suid = new->fsuid = 0; |
| 49 | new->gid = new->egid = new->sgid = new->fsgid = 0; |
| 50 | put_group_info(new->group_info); |
| 51 | new->group_info = get_group_info(&init_groups); |
| 52 | #ifdef CONFIG_KEYS |
| 53 | key_put(new->request_key_auth); |
| 54 | new->request_key_auth = NULL; |
| 55 | #endif |
| 56 | /* tgcred will be cleared in our caller bc CLONE_THREAD won't be set */ |
Serge E. Hallyn | 77ec739 | 2007-07-15 23:41:01 -0700 | [diff] [blame] | 57 | |
NeilBrown | db1afff | 2010-03-16 15:14:51 +1100 | [diff] [blame] | 58 | /* root_user holds a reference to ns, our reference can be dropped */ |
| 59 | put_user_ns(ns); |
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 | return 0; |
Cedric Le Goater | acce292 | 2007-07-15 23:40:59 -0700 | [diff] [blame] | 62 | } |
| 63 | |
David Howells | 5170836 | 2009-02-27 14:03:03 -0800 | [diff] [blame] | 64 | /* |
| 65 | * Deferred destructor for a user namespace. This is required because |
| 66 | * free_user_ns() may be called with uidhash_lock held, but we need to call |
| 67 | * back to free_uid() which will want to take the lock again. |
| 68 | */ |
| 69 | static void free_user_ns_work(struct work_struct *work) |
Cedric Le Goater | acce292 | 2007-07-15 23:40:59 -0700 | [diff] [blame] | 70 | { |
David Howells | 5170836 | 2009-02-27 14:03:03 -0800 | [diff] [blame] | 71 | struct user_namespace *ns = |
| 72 | container_of(work, struct user_namespace, destroyer); |
Serge Hallyn | 18b6e04 | 2008-10-15 16:38:45 -0500 | [diff] [blame] | 73 | free_uid(ns->creator); |
Cedric Le Goater | acce292 | 2007-07-15 23:40:59 -0700 | [diff] [blame] | 74 | kfree(ns); |
| 75 | } |
David Howells | 5170836 | 2009-02-27 14:03:03 -0800 | [diff] [blame] | 76 | |
| 77 | void free_user_ns(struct kref *kref) |
| 78 | { |
| 79 | struct user_namespace *ns = |
| 80 | container_of(kref, struct user_namespace, kref); |
| 81 | |
| 82 | INIT_WORK(&ns->destroyer, free_user_ns_work); |
| 83 | schedule_work(&ns->destroyer); |
| 84 | } |
Michael Halcrow | 6a3fd92 | 2008-04-29 00:59:52 -0700 | [diff] [blame] | 85 | EXPORT_SYMBOL(free_user_ns); |
Eric W. Biederman | 5c1469d | 2010-06-13 03:28:03 +0000 | [diff] [blame] | 86 | |
| 87 | uid_t user_ns_map_uid(struct user_namespace *to, const struct cred *cred, uid_t uid) |
| 88 | { |
| 89 | struct user_namespace *tmp; |
| 90 | |
| 91 | if (likely(to == cred->user->user_ns)) |
| 92 | return uid; |
| 93 | |
| 94 | |
| 95 | /* Is cred->user the creator of the target user_ns |
| 96 | * or the creator of one of it's parents? |
| 97 | */ |
| 98 | for ( tmp = to; tmp != &init_user_ns; |
| 99 | tmp = tmp->creator->user_ns ) { |
| 100 | if (cred->user == tmp->creator) { |
| 101 | return (uid_t)0; |
| 102 | } |
| 103 | } |
| 104 | |
| 105 | /* No useful relationship so no mapping */ |
| 106 | return overflowuid; |
| 107 | } |
| 108 | |
| 109 | gid_t user_ns_map_gid(struct user_namespace *to, const struct cred *cred, gid_t gid) |
| 110 | { |
| 111 | struct user_namespace *tmp; |
| 112 | |
| 113 | if (likely(to == cred->user->user_ns)) |
| 114 | return gid; |
| 115 | |
| 116 | /* Is cred->user the creator of the target user_ns |
| 117 | * or the creator of one of it's parents? |
| 118 | */ |
| 119 | for ( tmp = to; tmp != &init_user_ns; |
| 120 | tmp = tmp->creator->user_ns ) { |
| 121 | if (cred->user == tmp->creator) { |
| 122 | return (gid_t)0; |
| 123 | } |
| 124 | } |
| 125 | |
| 126 | /* No useful relationship so no mapping */ |
| 127 | return overflowgid; |
| 128 | } |