Blame view

kernel/user_namespace.c 3.41 KB
acce292c8   Cedric Le Goater   user namespace: a...
1
2
3
4
5
6
  /*
   *  This program is free software; you can redistribute it and/or
   *  modify it under the terms of the GNU General Public License as
   *  published by the Free Software Foundation, version 2 of the
   *  License.
   */
9984de1a5   Paul Gortmaker   kernel: Map most ...
7
  #include <linux/export.h>
acce292c8   Cedric Le Goater   user namespace: a...
8
  #include <linux/nsproxy.h>
1aeb272cf   Robert P. J. Day   kernel: explicitl...
9
  #include <linux/slab.h>
acce292c8   Cedric Le Goater   user namespace: a...
10
  #include <linux/user_namespace.h>
5c1469de7   Eric W. Biederman   user_ns: Introduc...
11
  #include <linux/highuid.h>
18b6e0414   Serge Hallyn   User namespaces: ...
12
  #include <linux/cred.h>
acce292c8   Cedric Le Goater   user namespace: a...
13

6164281ab   Pavel Emelyanov   user_ns: improve ...
14
  static struct kmem_cache *user_ns_cachep __read_mostly;
77ec739d8   Serge E. Hallyn   user namespace: a...
15
  /*
18b6e0414   Serge Hallyn   User namespaces: ...
16
17
18
19
20
21
   * Create a new user namespace, deriving the creator from the user in the
   * passed credentials, and replacing that user with the new root user for the
   * new namespace.
   *
   * This is called by copy_creds(), which will finish setting the target task's
   * credentials.
77ec739d8   Serge E. Hallyn   user namespace: a...
22
   */
18b6e0414   Serge Hallyn   User namespaces: ...
23
  int create_user_ns(struct cred *new)
77ec739d8   Serge E. Hallyn   user namespace: a...
24
25
  {
  	struct user_namespace *ns;
18b6e0414   Serge Hallyn   User namespaces: ...
26
  	struct user_struct *root_user;
77ec739d8   Serge E. Hallyn   user namespace: a...
27
  	int n;
6164281ab   Pavel Emelyanov   user_ns: improve ...
28
  	ns = kmem_cache_alloc(user_ns_cachep, GFP_KERNEL);
77ec739d8   Serge E. Hallyn   user namespace: a...
29
  	if (!ns)
18b6e0414   Serge Hallyn   User namespaces: ...
30
  		return -ENOMEM;
77ec739d8   Serge E. Hallyn   user namespace: a...
31
32
33
34
  
  	kref_init(&ns->kref);
  
  	for (n = 0; n < UIDHASH_SZ; ++n)
735de2230   Pavel Emelyanov   Convert uid hash ...
35
  		INIT_HLIST_HEAD(ns->uidhash_table + n);
77ec739d8   Serge E. Hallyn   user namespace: a...
36

18b6e0414   Serge Hallyn   User namespaces: ...
37
38
39
  	/* Alloc new root user.  */
  	root_user = alloc_uid(ns, 0);
  	if (!root_user) {
6164281ab   Pavel Emelyanov   user_ns: improve ...
40
  		kmem_cache_free(user_ns_cachep, ns);
18b6e0414   Serge Hallyn   User namespaces: ...
41
  		return -ENOMEM;
77ec739d8   Serge E. Hallyn   user namespace: a...
42
  	}
18b6e0414   Serge Hallyn   User namespaces: ...
43
44
45
46
47
48
49
50
51
52
53
54
  	/* set the new root user in the credentials under preparation */
  	ns->creator = new->user;
  	new->user = root_user;
  	new->uid = new->euid = new->suid = new->fsuid = 0;
  	new->gid = new->egid = new->sgid = new->fsgid = 0;
  	put_group_info(new->group_info);
  	new->group_info = get_group_info(&init_groups);
  #ifdef CONFIG_KEYS
  	key_put(new->request_key_auth);
  	new->request_key_auth = NULL;
  #endif
  	/* tgcred will be cleared in our caller bc CLONE_THREAD won't be set */
77ec739d8   Serge E. Hallyn   user namespace: a...
55

db1afffab   NeilBrown   kref: remove kref...
56
57
  	/* root_user holds a reference to ns, our reference can be dropped */
  	put_user_ns(ns);
77ec739d8   Serge E. Hallyn   user namespace: a...
58

18b6e0414   Serge Hallyn   User namespaces: ...
59
  	return 0;
acce292c8   Cedric Le Goater   user namespace: a...
60
  }
517083667   David Howells   Fix recursive loc...
61
62
63
64
65
66
  /*
   * Deferred destructor for a user namespace.  This is required because
   * free_user_ns() may be called with uidhash_lock held, but we need to call
   * back to free_uid() which will want to take the lock again.
   */
  static void free_user_ns_work(struct work_struct *work)
acce292c8   Cedric Le Goater   user namespace: a...
67
  {
517083667   David Howells   Fix recursive loc...
68
69
  	struct user_namespace *ns =
  		container_of(work, struct user_namespace, destroyer);
18b6e0414   Serge Hallyn   User namespaces: ...
70
  	free_uid(ns->creator);
6164281ab   Pavel Emelyanov   user_ns: improve ...
71
  	kmem_cache_free(user_ns_cachep, ns);
acce292c8   Cedric Le Goater   user namespace: a...
72
  }
517083667   David Howells   Fix recursive loc...
73
74
75
76
77
78
79
80
81
  
  void free_user_ns(struct kref *kref)
  {
  	struct user_namespace *ns =
  		container_of(kref, struct user_namespace, kref);
  
  	INIT_WORK(&ns->destroyer, free_user_ns_work);
  	schedule_work(&ns->destroyer);
  }
6a3fd92e7   Michael Halcrow   eCryptfs: make ke...
82
  EXPORT_SYMBOL(free_user_ns);
5c1469de7   Eric W. Biederman   user_ns: Introduc...
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
  
  uid_t user_ns_map_uid(struct user_namespace *to, const struct cred *cred, uid_t uid)
  {
  	struct user_namespace *tmp;
  
  	if (likely(to == cred->user->user_ns))
  		return uid;
  
  
  	/* Is cred->user the creator of the target user_ns
  	 * or the creator of one of it's parents?
  	 */
  	for ( tmp = to; tmp != &init_user_ns;
  	      tmp = tmp->creator->user_ns ) {
  		if (cred->user == tmp->creator) {
  			return (uid_t)0;
  		}
  	}
  
  	/* No useful relationship so no mapping */
  	return overflowuid;
  }
  
  gid_t user_ns_map_gid(struct user_namespace *to, const struct cred *cred, gid_t gid)
  {
  	struct user_namespace *tmp;
  
  	if (likely(to == cred->user->user_ns))
  		return gid;
  
  	/* Is cred->user the creator of the target user_ns
  	 * or the creator of one of it's parents?
  	 */
  	for ( tmp = to; tmp != &init_user_ns;
  	      tmp = tmp->creator->user_ns ) {
  		if (cred->user == tmp->creator) {
  			return (gid_t)0;
  		}
  	}
  
  	/* No useful relationship so no mapping */
  	return overflowgid;
  }
6164281ab   Pavel Emelyanov   user_ns: improve ...
126
127
128
129
130
131
132
  
  static __init int user_namespaces_init(void)
  {
  	user_ns_cachep = KMEM_CACHE(user_namespace, SLAB_PANIC);
  	return 0;
  }
  module_init(user_namespaces_init);