Blame view
kernel/utsname.c
3.61 KB
4865ecf13 [PATCH] namespace... |
1 2 3 4 5 6 7 8 9 10 |
/* * Copyright (C) 2004 IBM Corporation * * Author: Serge Hallyn <serue@us.ibm.com> * * 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 kernel: Map most ... |
11 |
#include <linux/export.h> |
4865ecf13 [PATCH] namespace... |
12 13 |
#include <linux/uts.h> #include <linux/utsname.h> |
467e9f4b5 fix create_new_na... |
14 |
#include <linux/err.h> |
1aeb272cf kernel: explicitl... |
15 |
#include <linux/slab.h> |
5b825c3af sched/headers: Pr... |
16 |
#include <linux/cred.h> |
59607db36 userns: add a use... |
17 |
#include <linux/user_namespace.h> |
0bb80f240 proc: Split the n... |
18 |
#include <linux/proc_ns.h> |
f719ff9bc sched/headers: Pr... |
19 |
#include <linux/sched/task.h> |
4865ecf13 [PATCH] namespace... |
20 |
|
f7af3d1c0 utsns: Add a limi... |
21 22 23 24 25 26 27 28 29 |
static struct ucounts *inc_uts_namespaces(struct user_namespace *ns) { return inc_ucount(ns, current_euid(), UCOUNT_UTS_NAMESPACES); } static void dec_uts_namespaces(struct ucounts *ucounts) { dec_ucount(ucounts, UCOUNT_UTS_NAMESPACES); } |
4c2a7e72d utsns: extract cr... |
30 31 32 33 34 35 36 37 38 |
static struct uts_namespace *create_uts_ns(void) { struct uts_namespace *uts_ns; uts_ns = kmalloc(sizeof(struct uts_namespace), GFP_KERNEL); if (uts_ns) kref_init(&uts_ns->kref); return uts_ns; } |
4865ecf13 [PATCH] namespace... |
39 |
/* |
071df104f [PATCH] namespace... |
40 41 |
* Clone a new ns copying an original utsname, setting refcount to 1 * @old_ns: namespace to clone |
bf5315366 kernel/utsname.c:... |
42 |
* Return ERR_PTR(-ENOMEM) on error (failure to kmalloc), new ns otherwise |
071df104f [PATCH] namespace... |
43 |
*/ |
bcf58e725 userns: Make crea... |
44 |
static struct uts_namespace *clone_uts_ns(struct user_namespace *user_ns, |
bb96a6f50 userns: allow set... |
45 |
struct uts_namespace *old_ns) |
071df104f [PATCH] namespace... |
46 47 |
{ struct uts_namespace *ns; |
f7af3d1c0 utsns: Add a limi... |
48 |
struct ucounts *ucounts; |
98f842e67 proc: Usable inod... |
49 |
int err; |
071df104f [PATCH] namespace... |
50 |
|
df75e7748 userns: When the ... |
51 |
err = -ENOSPC; |
f7af3d1c0 utsns: Add a limi... |
52 53 54 55 56 |
ucounts = inc_uts_namespaces(user_ns); if (!ucounts) goto fail; err = -ENOMEM; |
4c2a7e72d utsns: extract cr... |
57 |
ns = create_uts_ns(); |
467e9f4b5 fix create_new_na... |
58 |
if (!ns) |
f7af3d1c0 utsns: Add a limi... |
59 |
goto fail_dec; |
467e9f4b5 fix create_new_na... |
60 |
|
6344c433a new helpers: ns_a... |
61 |
err = ns_alloc_inum(&ns->ns); |
f7af3d1c0 utsns: Add a limi... |
62 63 |
if (err) goto fail_free; |
98f842e67 proc: Usable inod... |
64 |
|
f7af3d1c0 utsns: Add a limi... |
65 |
ns->ucounts = ucounts; |
33c429405 copy address of p... |
66 |
ns->ns.ops = &utsns_operations; |
efc63c4fb Fix UTS corruptio... |
67 |
down_read(&uts_sem); |
467e9f4b5 fix create_new_na... |
68 |
memcpy(&ns->name, &old_ns->name, sizeof(ns->name)); |
bcf58e725 userns: Make crea... |
69 |
ns->user_ns = get_user_ns(user_ns); |
efc63c4fb Fix UTS corruptio... |
70 |
up_read(&uts_sem); |
071df104f [PATCH] namespace... |
71 |
return ns; |
f7af3d1c0 utsns: Add a limi... |
72 73 74 75 76 77 78 |
fail_free: kfree(ns); fail_dec: dec_uts_namespaces(ucounts); fail: return ERR_PTR(err); |
071df104f [PATCH] namespace... |
79 80 81 |
} /* |
4865ecf13 [PATCH] namespace... |
82 83 84 85 86 |
* Copy task tsk's utsname namespace, or clone it if flags * specifies CLONE_NEWUTS. In latter case, changes to the * utsname of this process won't be seen by parent, and vice * versa. */ |
bb96a6f50 userns: allow set... |
87 |
struct uts_namespace *copy_utsname(unsigned long flags, |
bcf58e725 userns: Make crea... |
88 |
struct user_namespace *user_ns, struct uts_namespace *old_ns) |
4865ecf13 [PATCH] namespace... |
89 |
{ |
071df104f [PATCH] namespace... |
90 |
struct uts_namespace *new_ns; |
4865ecf13 [PATCH] namespace... |
91 |
|
e3222c4ec Merge sys_clone()... |
92 |
BUG_ON(!old_ns); |
4865ecf13 [PATCH] namespace... |
93 |
get_uts_ns(old_ns); |
071df104f [PATCH] namespace... |
94 |
if (!(flags & CLONE_NEWUTS)) |
e3222c4ec Merge sys_clone()... |
95 |
return old_ns; |
071df104f [PATCH] namespace... |
96 |
|
bcf58e725 userns: Make crea... |
97 |
new_ns = clone_uts_ns(user_ns, old_ns); |
071df104f [PATCH] namespace... |
98 |
|
071df104f [PATCH] namespace... |
99 |
put_uts_ns(old_ns); |
e3222c4ec Merge sys_clone()... |
100 |
return new_ns; |
4865ecf13 [PATCH] namespace... |
101 102 103 104 105 106 107 |
} void free_uts_ns(struct kref *kref) { struct uts_namespace *ns; ns = container_of(kref, struct uts_namespace, kref); |
f7af3d1c0 utsns: Add a limi... |
108 |
dec_uts_namespaces(ns->ucounts); |
59607db36 userns: add a use... |
109 |
put_user_ns(ns->user_ns); |
6344c433a new helpers: ns_a... |
110 |
ns_free_inum(&ns->ns); |
4865ecf13 [PATCH] namespace... |
111 112 |
kfree(ns); } |
34482e89a ns proc: Add supp... |
113 |
|
3c0411846 switch the rest o... |
114 115 116 117 |
static inline struct uts_namespace *to_uts_ns(struct ns_common *ns) { return container_of(ns, struct uts_namespace, ns); } |
64964528b make proc_ns_oper... |
118 |
static struct ns_common *utsns_get(struct task_struct *task) |
34482e89a ns proc: Add supp... |
119 120 121 |
{ struct uts_namespace *ns = NULL; struct nsproxy *nsproxy; |
728dba3a3 namespaces: Use t... |
122 123 |
task_lock(task); nsproxy = task->nsproxy; |
34482e89a ns proc: Add supp... |
124 125 126 127 |
if (nsproxy) { ns = nsproxy->uts_ns; get_uts_ns(ns); } |
728dba3a3 namespaces: Use t... |
128 |
task_unlock(task); |
34482e89a ns proc: Add supp... |
129 |
|
3c0411846 switch the rest o... |
130 |
return ns ? &ns->ns : NULL; |
34482e89a ns proc: Add supp... |
131 |
} |
64964528b make proc_ns_oper... |
132 |
static void utsns_put(struct ns_common *ns) |
34482e89a ns proc: Add supp... |
133 |
{ |
3c0411846 switch the rest o... |
134 |
put_uts_ns(to_uts_ns(ns)); |
34482e89a ns proc: Add supp... |
135 |
} |
64964528b make proc_ns_oper... |
136 |
static int utsns_install(struct nsproxy *nsproxy, struct ns_common *new) |
34482e89a ns proc: Add supp... |
137 |
{ |
3c0411846 switch the rest o... |
138 |
struct uts_namespace *ns = to_uts_ns(new); |
142e1d1d5 userns: Allow unp... |
139 |
|
5e4a08476 userns: Require C... |
140 |
if (!ns_capable(ns->user_ns, CAP_SYS_ADMIN) || |
c7b96acf1 userns: Kill nso... |
141 |
!ns_capable(current_user_ns(), CAP_SYS_ADMIN)) |
142e1d1d5 userns: Allow unp... |
142 |
return -EPERM; |
34482e89a ns proc: Add supp... |
143 144 145 146 147 |
get_uts_ns(ns); put_uts_ns(nsproxy->uts_ns); nsproxy->uts_ns = ns; return 0; } |
bcac25a58 kernel: add a hel... |
148 149 150 151 |
static struct user_namespace *utsns_owner(struct ns_common *ns) { return to_uts_ns(ns)->user_ns; } |
34482e89a ns proc: Add supp... |
152 153 154 155 156 157 |
const struct proc_ns_operations utsns_operations = { .name = "uts", .type = CLONE_NEWUTS, .get = utsns_get, .put = utsns_put, .install = utsns_install, |
bcac25a58 kernel: add a hel... |
158 |
.owner = utsns_owner, |
34482e89a ns proc: Add supp... |
159 |
}; |