Blame view

kernel/user_namespace.c 1.76 KB
acce292c8   Cedric Le Goater   user namespace: a...
1
2
3
4
5
6
7
8
  /*
   *  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.
   */
  
  #include <linux/module.h>
acce292c8   Cedric Le Goater   user namespace: a...
9
  #include <linux/nsproxy.h>
1aeb272cf   Robert P. J. Day   kernel: explicitl...
10
  #include <linux/slab.h>
acce292c8   Cedric Le Goater   user namespace: a...
11
  #include <linux/user_namespace.h>
18b6e0414   Serge Hallyn   User namespaces: ...
12
  #include <linux/cred.h>
acce292c8   Cedric Le Goater   user namespace: a...
13

77ec739d8   Serge E. Hallyn   user namespace: a...
14
  /*
18b6e0414   Serge Hallyn   User namespaces: ...
15
16
17
18
19
20
   * 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...
21
   */
18b6e0414   Serge Hallyn   User namespaces: ...
22
  int create_user_ns(struct cred *new)
77ec739d8   Serge E. Hallyn   user namespace: a...
23
24
  {
  	struct user_namespace *ns;
18b6e0414   Serge Hallyn   User namespaces: ...
25
  	struct user_struct *root_user;
77ec739d8   Serge E. Hallyn   user namespace: a...
26
27
28
29
  	int n;
  
  	ns = kmalloc(sizeof(struct user_namespace), GFP_KERNEL);
  	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) {
77ec739d8   Serge E. Hallyn   user namespace: a...
40
  		kfree(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

18b6e0414   Serge Hallyn   User namespaces: ...
56
57
  	/* alloc_uid() incremented the userns refcount.  Just set it to 1 */
  	kref_set(&ns->kref, 1);
77ec739d8   Serge E. Hallyn   user namespace: a...
58

18b6e0414   Serge Hallyn   User namespaces: ...
59
  	return 0;
acce292c8   Cedric Le Goater   user namespace: a...
60
61
62
63
64
65
66
  }
  
  void free_user_ns(struct kref *kref)
  {
  	struct user_namespace *ns;
  
  	ns = container_of(kref, struct user_namespace, kref);
18b6e0414   Serge Hallyn   User namespaces: ...
67
  	free_uid(ns->creator);
acce292c8   Cedric Le Goater   user namespace: a...
68
69
  	kfree(ns);
  }
6a3fd92e7   Michael Halcrow   eCryptfs: make ke...
70
  EXPORT_SYMBOL(free_user_ns);