Blame view
kernel/utsname.c
3.61 KB
4865ecf13
|
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
|
11 |
#include <linux/export.h> |
4865ecf13
|
12 13 |
#include <linux/uts.h> #include <linux/utsname.h> |
467e9f4b5
|
14 |
#include <linux/err.h> |
1aeb272cf
|
15 |
#include <linux/slab.h> |
5b825c3af
|
16 |
#include <linux/cred.h> |
59607db36
|
17 |
#include <linux/user_namespace.h> |
0bb80f240
|
18 |
#include <linux/proc_ns.h> |
f719ff9bc
|
19 |
#include <linux/sched/task.h> |
4865ecf13
|
20 |
|
f7af3d1c0
|
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
|
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
|
39 |
/* |
071df104f
|
40 41 |
* Clone a new ns copying an original utsname, setting refcount to 1 * @old_ns: namespace to clone |
bf5315366
|
42 |
* Return ERR_PTR(-ENOMEM) on error (failure to kmalloc), new ns otherwise |
071df104f
|
43 |
*/ |
bcf58e725
|
44 |
static struct uts_namespace *clone_uts_ns(struct user_namespace *user_ns, |
bb96a6f50
|
45 |
struct uts_namespace *old_ns) |
071df104f
|
46 47 |
{ struct uts_namespace *ns; |
f7af3d1c0
|
48 |
struct ucounts *ucounts; |
98f842e67
|
49 |
int err; |
071df104f
|
50 |
|
df75e7748
|
51 |
err = -ENOSPC; |
f7af3d1c0
|
52 53 54 55 56 |
ucounts = inc_uts_namespaces(user_ns); if (!ucounts) goto fail; err = -ENOMEM; |
4c2a7e72d
|
57 |
ns = create_uts_ns(); |
467e9f4b5
|
58 |
if (!ns) |
f7af3d1c0
|
59 |
goto fail_dec; |
467e9f4b5
|
60 |
|
6344c433a
|
61 |
err = ns_alloc_inum(&ns->ns); |
f7af3d1c0
|
62 63 |
if (err) goto fail_free; |
98f842e67
|
64 |
|
f7af3d1c0
|
65 |
ns->ucounts = ucounts; |
33c429405
|
66 |
ns->ns.ops = &utsns_operations; |
efc63c4fb
|
67 |
down_read(&uts_sem); |
467e9f4b5
|
68 |
memcpy(&ns->name, &old_ns->name, sizeof(ns->name)); |
bcf58e725
|
69 |
ns->user_ns = get_user_ns(user_ns); |
efc63c4fb
|
70 |
up_read(&uts_sem); |
071df104f
|
71 |
return ns; |
f7af3d1c0
|
72 73 74 75 76 77 78 |
fail_free: kfree(ns); fail_dec: dec_uts_namespaces(ucounts); fail: return ERR_PTR(err); |
071df104f
|
79 80 81 |
} /* |
4865ecf13
|
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
|
87 |
struct uts_namespace *copy_utsname(unsigned long flags, |
bcf58e725
|
88 |
struct user_namespace *user_ns, struct uts_namespace *old_ns) |
4865ecf13
|
89 |
{ |
071df104f
|
90 |
struct uts_namespace *new_ns; |
4865ecf13
|
91 |
|
e3222c4ec
|
92 |
BUG_ON(!old_ns); |
4865ecf13
|
93 |
get_uts_ns(old_ns); |
071df104f
|
94 |
if (!(flags & CLONE_NEWUTS)) |
e3222c4ec
|
95 |
return old_ns; |
071df104f
|
96 |
|
bcf58e725
|
97 |
new_ns = clone_uts_ns(user_ns, old_ns); |
071df104f
|
98 |
|
071df104f
|
99 |
put_uts_ns(old_ns); |
e3222c4ec
|
100 |
return new_ns; |
4865ecf13
|
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
|
108 |
dec_uts_namespaces(ns->ucounts); |
59607db36
|
109 |
put_user_ns(ns->user_ns); |
6344c433a
|
110 |
ns_free_inum(&ns->ns); |
4865ecf13
|
111 112 |
kfree(ns); } |
34482e89a
|
113 |
|
3c0411846
|
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
|
118 |
static struct ns_common *utsns_get(struct task_struct *task) |
34482e89a
|
119 120 121 |
{ struct uts_namespace *ns = NULL; struct nsproxy *nsproxy; |
728dba3a3
|
122 123 |
task_lock(task); nsproxy = task->nsproxy; |
34482e89a
|
124 125 126 127 |
if (nsproxy) { ns = nsproxy->uts_ns; get_uts_ns(ns); } |
728dba3a3
|
128 |
task_unlock(task); |
34482e89a
|
129 |
|
3c0411846
|
130 |
return ns ? &ns->ns : NULL; |
34482e89a
|
131 |
} |
64964528b
|
132 |
static void utsns_put(struct ns_common *ns) |
34482e89a
|
133 |
{ |
3c0411846
|
134 |
put_uts_ns(to_uts_ns(ns)); |
34482e89a
|
135 |
} |
64964528b
|
136 |
static int utsns_install(struct nsproxy *nsproxy, struct ns_common *new) |
34482e89a
|
137 |
{ |
3c0411846
|
138 |
struct uts_namespace *ns = to_uts_ns(new); |
142e1d1d5
|
139 |
|
5e4a08476
|
140 |
if (!ns_capable(ns->user_ns, CAP_SYS_ADMIN) || |
c7b96acf1
|
141 |
!ns_capable(current_user_ns(), CAP_SYS_ADMIN)) |
142e1d1d5
|
142 |
return -EPERM; |
34482e89a
|
143 144 145 146 147 |
get_uts_ns(ns); put_uts_ns(nsproxy->uts_ns); nsproxy->uts_ns = ns; return 0; } |
bcac25a58
|
148 149 150 151 |
static struct user_namespace *utsns_owner(struct ns_common *ns) { return to_uts_ns(ns)->user_ns; } |
34482e89a
|
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
|
158 |
.owner = utsns_owner, |
34482e89a
|
159 |
}; |