blob: 898e973bd1e8fbda60c816c2ca810488c214db0d [file] [log] [blame]
Cedric Le Goateracce2922007-07-15 23:40:59 -07001/*
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 Gortmaker9984de12011-05-23 14:51:41 -04008#include <linux/export.h>
Cedric Le Goateracce2922007-07-15 23:40:59 -07009#include <linux/nsproxy.h>
Robert P. J. Day1aeb2722008-04-29 00:59:25 -070010#include <linux/slab.h>
Cedric Le Goateracce2922007-07-15 23:40:59 -070011#include <linux/user_namespace.h>
Eric W. Biederman5c1469d2010-06-13 03:28:03 +000012#include <linux/highuid.h>
Serge Hallyn18b6e042008-10-15 16:38:45 -050013#include <linux/cred.h>
Eric W. Biederman973c5912011-11-17 01:59:07 -080014#include <linux/securebits.h>
Cedric Le Goateracce2922007-07-15 23:40:59 -070015
Pavel Emelyanov61642812011-01-12 17:00:46 -080016static struct kmem_cache *user_ns_cachep __read_mostly;
17
Serge E. Hallyn77ec7392007-07-15 23:41:01 -070018/*
Serge Hallyn18b6e042008-10-15 16:38:45 -050019 * Create a new user namespace, deriving the creator from the user in the
20 * passed credentials, and replacing that user with the new root user for the
21 * new namespace.
22 *
23 * This is called by copy_creds(), which will finish setting the target task's
24 * credentials.
Serge E. Hallyn77ec7392007-07-15 23:41:01 -070025 */
Serge Hallyn18b6e042008-10-15 16:38:45 -050026int create_user_ns(struct cred *new)
Serge E. Hallyn77ec7392007-07-15 23:41:01 -070027{
Eric W. Biederman0093ccb2011-11-16 21:52:53 -080028 struct user_namespace *ns, *parent_ns = new->user_ns;
Serge Hallyn18b6e042008-10-15 16:38:45 -050029 struct user_struct *root_user;
Serge E. Hallyn77ec7392007-07-15 23:41:01 -070030
Pavel Emelyanov61642812011-01-12 17:00:46 -080031 ns = kmem_cache_alloc(user_ns_cachep, GFP_KERNEL);
Serge E. Hallyn77ec7392007-07-15 23:41:01 -070032 if (!ns)
Serge Hallyn18b6e042008-10-15 16:38:45 -050033 return -ENOMEM;
Serge E. Hallyn77ec7392007-07-15 23:41:01 -070034
35 kref_init(&ns->kref);
36
Serge Hallyn18b6e042008-10-15 16:38:45 -050037 /* Alloc new root user. */
Eric W. Biederman7b44ab92011-11-16 23:20:58 -080038 root_user = alloc_uid(make_kuid(ns, 0));
Serge Hallyn18b6e042008-10-15 16:38:45 -050039 if (!root_user) {
Pavel Emelyanov61642812011-01-12 17:00:46 -080040 kmem_cache_free(user_ns_cachep, ns);
Serge Hallyn18b6e042008-10-15 16:38:45 -050041 return -ENOMEM;
Serge E. Hallyn77ec7392007-07-15 23:41:01 -070042 }
43
Serge Hallyn18b6e042008-10-15 16:38:45 -050044 /* set the new root user in the credentials under preparation */
Eric W. Biedermanaeb3ae92011-11-16 21:59:43 -080045 ns->parent = parent_ns;
Serge Hallyn18b6e042008-10-15 16:38:45 -050046 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);
Eric W. Biederman973c5912011-11-17 01:59:07 -080052 /* Start with the same capabilities as init but useless for doing
53 * anything as the capabilities are bound to the new user namespace.
54 */
55 new->securebits = SECUREBITS_DEFAULT;
56 new->cap_inheritable = CAP_EMPTY_SET;
57 new->cap_permitted = CAP_FULL_SET;
58 new->cap_effective = CAP_FULL_SET;
59 new->cap_bset = CAP_FULL_SET;
Serge Hallyn18b6e042008-10-15 16:38:45 -050060#ifdef CONFIG_KEYS
61 key_put(new->request_key_auth);
62 new->request_key_auth = NULL;
63#endif
64 /* tgcred will be cleared in our caller bc CLONE_THREAD won't be set */
Serge E. Hallyn77ec7392007-07-15 23:41:01 -070065
Eric W. Biederman0093ccb2011-11-16 21:52:53 -080066 /* Leave the reference to our user_ns with the new cred */
67 new->user_ns = ns;
68
Serge Hallyn18b6e042008-10-15 16:38:45 -050069 return 0;
Cedric Le Goateracce2922007-07-15 23:40:59 -070070}
71
David Howells51708362009-02-27 14:03:03 -080072/*
73 * Deferred destructor for a user namespace. This is required because
74 * free_user_ns() may be called with uidhash_lock held, but we need to call
75 * back to free_uid() which will want to take the lock again.
76 */
77static void free_user_ns_work(struct work_struct *work)
Cedric Le Goateracce2922007-07-15 23:40:59 -070078{
Eric W. Biedermanaeb3ae92011-11-16 21:59:43 -080079 struct user_namespace *parent, *ns =
David Howells51708362009-02-27 14:03:03 -080080 container_of(work, struct user_namespace, destroyer);
Eric W. Biedermanaeb3ae92011-11-16 21:59:43 -080081 parent = ns->parent;
Serge Hallyn18b6e042008-10-15 16:38:45 -050082 free_uid(ns->creator);
Pavel Emelyanov61642812011-01-12 17:00:46 -080083 kmem_cache_free(user_ns_cachep, ns);
Eric W. Biedermanaeb3ae92011-11-16 21:59:43 -080084 put_user_ns(parent);
Cedric Le Goateracce2922007-07-15 23:40:59 -070085}
David Howells51708362009-02-27 14:03:03 -080086
87void free_user_ns(struct kref *kref)
88{
89 struct user_namespace *ns =
90 container_of(kref, struct user_namespace, kref);
91
92 INIT_WORK(&ns->destroyer, free_user_ns_work);
93 schedule_work(&ns->destroyer);
94}
Michael Halcrow6a3fd922008-04-29 00:59:52 -070095EXPORT_SYMBOL(free_user_ns);
Eric W. Biederman5c1469d2010-06-13 03:28:03 +000096
97uid_t user_ns_map_uid(struct user_namespace *to, const struct cred *cred, uid_t uid)
98{
99 struct user_namespace *tmp;
100
Eric W. Biedermanc4a4d602011-11-16 23:15:31 -0800101 if (likely(to == cred->user_ns))
Eric W. Biederman5c1469d2010-06-13 03:28:03 +0000102 return uid;
103
104
105 /* Is cred->user the creator of the target user_ns
106 * or the creator of one of it's parents?
107 */
Eric W. Biedermanaeb3ae92011-11-16 21:59:43 -0800108 for ( tmp = to; tmp != &init_user_ns; tmp = tmp->parent ) {
Eric W. Biederman5c1469d2010-06-13 03:28:03 +0000109 if (cred->user == tmp->creator) {
110 return (uid_t)0;
111 }
112 }
113
114 /* No useful relationship so no mapping */
115 return overflowuid;
116}
117
118gid_t user_ns_map_gid(struct user_namespace *to, const struct cred *cred, gid_t gid)
119{
120 struct user_namespace *tmp;
121
Eric W. Biedermanc4a4d602011-11-16 23:15:31 -0800122 if (likely(to == cred->user_ns))
Eric W. Biederman5c1469d2010-06-13 03:28:03 +0000123 return gid;
124
125 /* Is cred->user the creator of the target user_ns
126 * or the creator of one of it's parents?
127 */
Eric W. Biedermanaeb3ae92011-11-16 21:59:43 -0800128 for ( tmp = to; tmp != &init_user_ns; tmp = tmp->parent ) {
Eric W. Biederman5c1469d2010-06-13 03:28:03 +0000129 if (cred->user == tmp->creator) {
130 return (gid_t)0;
131 }
132 }
133
134 /* No useful relationship so no mapping */
135 return overflowgid;
136}
Pavel Emelyanov61642812011-01-12 17:00:46 -0800137
138static __init int user_namespaces_init(void)
139{
140 user_ns_cachep = KMEM_CACHE(user_namespace, SLAB_PANIC);
141 return 0;
142}
143module_init(user_namespaces_init);