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> |
Serge Hallyn | 18b6e04 | 2008-10-15 16:38:45 -0500 | [diff] [blame] | 12 | #include <linux/cred.h> |
Cedric Le Goater | acce292 | 2007-07-15 23:40:59 -0700 | [diff] [blame] | 13 | |
Serge E. Hallyn | 77ec739 | 2007-07-15 23:41:01 -0700 | [diff] [blame] | 14 | /* |
Serge Hallyn | 18b6e04 | 2008-10-15 16:38:45 -0500 | [diff] [blame] | 15 | * Create a new user namespace, deriving the creator from the user in the |
| 16 | * passed credentials, and replacing that user with the new root user for the |
| 17 | * new namespace. |
| 18 | * |
| 19 | * This is called by copy_creds(), which will finish setting the target task's |
| 20 | * credentials. |
Serge E. Hallyn | 77ec739 | 2007-07-15 23:41:01 -0700 | [diff] [blame] | 21 | */ |
Serge Hallyn | 18b6e04 | 2008-10-15 16:38:45 -0500 | [diff] [blame] | 22 | int create_user_ns(struct cred *new) |
Serge E. Hallyn | 77ec739 | 2007-07-15 23:41:01 -0700 | [diff] [blame] | 23 | { |
| 24 | struct user_namespace *ns; |
Serge Hallyn | 18b6e04 | 2008-10-15 16:38:45 -0500 | [diff] [blame] | 25 | struct user_struct *root_user; |
Serge E. Hallyn | 77ec739 | 2007-07-15 23:41:01 -0700 | [diff] [blame] | 26 | int n; |
| 27 | |
| 28 | ns = kmalloc(sizeof(struct user_namespace), GFP_KERNEL); |
| 29 | if (!ns) |
Serge Hallyn | 18b6e04 | 2008-10-15 16:38:45 -0500 | [diff] [blame] | 30 | return -ENOMEM; |
Serge E. Hallyn | 77ec739 | 2007-07-15 23:41:01 -0700 | [diff] [blame] | 31 | |
| 32 | kref_init(&ns->kref); |
| 33 | |
| 34 | for (n = 0; n < UIDHASH_SZ; ++n) |
Pavel Emelyanov | 735de22 | 2007-09-18 22:46:44 -0700 | [diff] [blame] | 35 | INIT_HLIST_HEAD(ns->uidhash_table + n); |
Serge E. Hallyn | 77ec739 | 2007-07-15 23:41:01 -0700 | [diff] [blame] | 36 | |
Serge Hallyn | 18b6e04 | 2008-10-15 16:38:45 -0500 | [diff] [blame] | 37 | /* Alloc new root user. */ |
| 38 | root_user = alloc_uid(ns, 0); |
| 39 | if (!root_user) { |
Serge E. Hallyn | 77ec739 | 2007-07-15 23:41:01 -0700 | [diff] [blame] | 40 | kfree(ns); |
Serge Hallyn | 18b6e04 | 2008-10-15 16:38:45 -0500 | [diff] [blame] | 41 | return -ENOMEM; |
Serge E. Hallyn | 77ec739 | 2007-07-15 23:41:01 -0700 | [diff] [blame] | 42 | } |
| 43 | |
Serge Hallyn | 18b6e04 | 2008-10-15 16:38:45 -0500 | [diff] [blame] | 44 | /* set the new root user in the credentials under preparation */ |
| 45 | ns->creator = new->user; |
| 46 | new->user = root_user; |
| 47 | new->uid = new->euid = new->suid = new->fsuid = 0; |
| 48 | new->gid = new->egid = new->sgid = new->fsgid = 0; |
| 49 | put_group_info(new->group_info); |
| 50 | new->group_info = get_group_info(&init_groups); |
| 51 | #ifdef CONFIG_KEYS |
| 52 | key_put(new->request_key_auth); |
| 53 | new->request_key_auth = NULL; |
| 54 | #endif |
| 55 | /* 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] | 56 | |
Serge Hallyn | 18b6e04 | 2008-10-15 16:38:45 -0500 | [diff] [blame] | 57 | /* alloc_uid() incremented the userns refcount. Just set it to 1 */ |
| 58 | kref_set(&ns->kref, 1); |
Serge E. Hallyn | 77ec739 | 2007-07-15 23:41:01 -0700 | [diff] [blame] | 59 | |
Serge Hallyn | 18b6e04 | 2008-10-15 16:38:45 -0500 | [diff] [blame] | 60 | return 0; |
Cedric Le Goater | acce292 | 2007-07-15 23:40:59 -0700 | [diff] [blame] | 61 | } |
| 62 | |
| 63 | void free_user_ns(struct kref *kref) |
| 64 | { |
| 65 | struct user_namespace *ns; |
| 66 | |
| 67 | ns = container_of(kref, struct user_namespace, kref); |
Serge Hallyn | 18b6e04 | 2008-10-15 16:38:45 -0500 | [diff] [blame] | 68 | free_uid(ns->creator); |
Cedric Le Goater | acce292 | 2007-07-15 23:40:59 -0700 | [diff] [blame] | 69 | kfree(ns); |
| 70 | } |
Michael Halcrow | 6a3fd92 | 2008-04-29 00:59:52 -0700 | [diff] [blame] | 71 | EXPORT_SYMBOL(free_user_ns); |