blob: ed08836558e038bd287ab14be81eca78482ac6e0 [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;
Eric W. Biederman783291e2011-11-17 01:32:59 -080030 kuid_t owner = make_kuid(new->user_ns, new->euid);
31 kgid_t group = make_kgid(new->user_ns, new->egid);
32
33 /* The creator needs a mapping in the parent user namespace
34 * or else we won't be able to reasonably tell userspace who
35 * created a user_namespace.
36 */
37 if (!kuid_has_mapping(parent_ns, owner) ||
38 !kgid_has_mapping(parent_ns, group))
39 return -EPERM;
Serge E. Hallyn77ec7392007-07-15 23:41:01 -070040
Pavel Emelyanov61642812011-01-12 17:00:46 -080041 ns = kmem_cache_alloc(user_ns_cachep, GFP_KERNEL);
Serge E. Hallyn77ec7392007-07-15 23:41:01 -070042 if (!ns)
Serge Hallyn18b6e042008-10-15 16:38:45 -050043 return -ENOMEM;
Serge E. Hallyn77ec7392007-07-15 23:41:01 -070044
45 kref_init(&ns->kref);
46
Serge Hallyn18b6e042008-10-15 16:38:45 -050047 /* Alloc new root user. */
Eric W. Biederman7b44ab92011-11-16 23:20:58 -080048 root_user = alloc_uid(make_kuid(ns, 0));
Serge Hallyn18b6e042008-10-15 16:38:45 -050049 if (!root_user) {
Pavel Emelyanov61642812011-01-12 17:00:46 -080050 kmem_cache_free(user_ns_cachep, ns);
Serge Hallyn18b6e042008-10-15 16:38:45 -050051 return -ENOMEM;
Serge E. Hallyn77ec7392007-07-15 23:41:01 -070052 }
53
Serge Hallyn18b6e042008-10-15 16:38:45 -050054 /* set the new root user in the credentials under preparation */
Eric W. Biedermanaeb3ae92011-11-16 21:59:43 -080055 ns->parent = parent_ns;
Eric W. Biederman783291e2011-11-17 01:32:59 -080056 ns->owner = owner;
57 ns->group = group;
58 free_uid(new->user);
Serge Hallyn18b6e042008-10-15 16:38:45 -050059 new->user = root_user;
60 new->uid = new->euid = new->suid = new->fsuid = 0;
61 new->gid = new->egid = new->sgid = new->fsgid = 0;
62 put_group_info(new->group_info);
63 new->group_info = get_group_info(&init_groups);
Eric W. Biederman973c5912011-11-17 01:59:07 -080064 /* Start with the same capabilities as init but useless for doing
65 * anything as the capabilities are bound to the new user namespace.
66 */
67 new->securebits = SECUREBITS_DEFAULT;
68 new->cap_inheritable = CAP_EMPTY_SET;
69 new->cap_permitted = CAP_FULL_SET;
70 new->cap_effective = CAP_FULL_SET;
71 new->cap_bset = CAP_FULL_SET;
Serge Hallyn18b6e042008-10-15 16:38:45 -050072#ifdef CONFIG_KEYS
73 key_put(new->request_key_auth);
74 new->request_key_auth = NULL;
75#endif
76 /* tgcred will be cleared in our caller bc CLONE_THREAD won't be set */
Serge E. Hallyn77ec7392007-07-15 23:41:01 -070077
Eric W. Biederman783291e2011-11-17 01:32:59 -080078 /* Leave the new->user_ns reference with the new user namespace. */
79 /* Leave the reference to our user_ns with the new cred. */
Eric W. Biederman0093ccb2011-11-16 21:52:53 -080080 new->user_ns = ns;
81
Serge Hallyn18b6e042008-10-15 16:38:45 -050082 return 0;
Cedric Le Goateracce2922007-07-15 23:40:59 -070083}
84
David Howells51708362009-02-27 14:03:03 -080085void free_user_ns(struct kref *kref)
86{
Eric W. Biederman783291e2011-11-17 01:32:59 -080087 struct user_namespace *parent, *ns =
David Howells51708362009-02-27 14:03:03 -080088 container_of(kref, struct user_namespace, kref);
89
Eric W. Biederman783291e2011-11-17 01:32:59 -080090 parent = ns->parent;
91 kmem_cache_free(user_ns_cachep, ns);
92 put_user_ns(parent);
David Howells51708362009-02-27 14:03:03 -080093}
Michael Halcrow6a3fd922008-04-29 00:59:52 -070094EXPORT_SYMBOL(free_user_ns);
Eric W. Biederman5c1469d2010-06-13 03:28:03 +000095
96uid_t user_ns_map_uid(struct user_namespace *to, const struct cred *cred, uid_t uid)
97{
98 struct user_namespace *tmp;
99
Eric W. Biedermanc4a4d602011-11-16 23:15:31 -0800100 if (likely(to == cred->user_ns))
Eric W. Biederman5c1469d2010-06-13 03:28:03 +0000101 return uid;
102
Eric W. Biederman5c1469d2010-06-13 03:28:03 +0000103 /* Is cred->user the creator of the target user_ns
104 * or the creator of one of it's parents?
105 */
Eric W. Biedermanaeb3ae92011-11-16 21:59:43 -0800106 for ( tmp = to; tmp != &init_user_ns; tmp = tmp->parent ) {
Eric W. Biederman783291e2011-11-17 01:32:59 -0800107 if (uid_eq(cred->user->uid, tmp->owner)) {
Eric W. Biederman5c1469d2010-06-13 03:28:03 +0000108 return (uid_t)0;
109 }
110 }
111
112 /* No useful relationship so no mapping */
113 return overflowuid;
114}
115
116gid_t user_ns_map_gid(struct user_namespace *to, const struct cred *cred, gid_t gid)
117{
118 struct user_namespace *tmp;
119
Eric W. Biedermanc4a4d602011-11-16 23:15:31 -0800120 if (likely(to == cred->user_ns))
Eric W. Biederman5c1469d2010-06-13 03:28:03 +0000121 return gid;
122
123 /* Is cred->user the creator of the target user_ns
124 * or the creator of one of it's parents?
125 */
Eric W. Biedermanaeb3ae92011-11-16 21:59:43 -0800126 for ( tmp = to; tmp != &init_user_ns; tmp = tmp->parent ) {
Eric W. Biederman783291e2011-11-17 01:32:59 -0800127 if (uid_eq(cred->user->uid, tmp->owner)) {
Eric W. Biederman5c1469d2010-06-13 03:28:03 +0000128 return (gid_t)0;
129 }
130 }
131
132 /* No useful relationship so no mapping */
133 return overflowgid;
134}
Pavel Emelyanov61642812011-01-12 17:00:46 -0800135
136static __init int user_namespaces_init(void)
137{
138 user_ns_cachep = KMEM_CACHE(user_namespace, SLAB_PANIC);
139 return 0;
140}
141module_init(user_namespaces_init);