Blame view
kernel/utsname.c
3.79 KB
b886d83c5
|
1 |
// SPDX-License-Identifier: GPL-2.0-only |
4865ecf13
|
2 3 4 5 |
/* * Copyright (C) 2004 IBM Corporation * * Author: Serge Hallyn <serue@us.ibm.com> |
4865ecf13
|
6 |
*/ |
9984de1a5
|
7 |
#include <linux/export.h> |
4865ecf13
|
8 9 |
#include <linux/uts.h> #include <linux/utsname.h> |
467e9f4b5
|
10 |
#include <linux/err.h> |
1aeb272cf
|
11 |
#include <linux/slab.h> |
5b825c3af
|
12 |
#include <linux/cred.h> |
59607db36
|
13 |
#include <linux/user_namespace.h> |
0bb80f240
|
14 |
#include <linux/proc_ns.h> |
f719ff9bc
|
15 |
#include <linux/sched/task.h> |
4865ecf13
|
16 |
|
3ea056c50
|
17 |
static struct kmem_cache *uts_ns_cache __ro_after_init; |
f7af3d1c0
|
18 19 20 21 22 23 24 25 26 |
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
|
27 28 29 |
static struct uts_namespace *create_uts_ns(void) { struct uts_namespace *uts_ns; |
3ea056c50
|
30 |
uts_ns = kmem_cache_alloc(uts_ns_cache, GFP_KERNEL); |
4c2a7e72d
|
31 32 33 34 |
if (uts_ns) kref_init(&uts_ns->kref); return uts_ns; } |
4865ecf13
|
35 |
/* |
071df104f
|
36 37 |
* Clone a new ns copying an original utsname, setting refcount to 1 * @old_ns: namespace to clone |
3ea056c50
|
38 |
* Return ERR_PTR(-ENOMEM) on error (failure to allocate), new ns otherwise |
071df104f
|
39 |
*/ |
bcf58e725
|
40 |
static struct uts_namespace *clone_uts_ns(struct user_namespace *user_ns, |
bb96a6f50
|
41 |
struct uts_namespace *old_ns) |
071df104f
|
42 43 |
{ struct uts_namespace *ns; |
f7af3d1c0
|
44 |
struct ucounts *ucounts; |
98f842e67
|
45 |
int err; |
071df104f
|
46 |
|
df75e7748
|
47 |
err = -ENOSPC; |
f7af3d1c0
|
48 49 50 51 52 |
ucounts = inc_uts_namespaces(user_ns); if (!ucounts) goto fail; err = -ENOMEM; |
4c2a7e72d
|
53 |
ns = create_uts_ns(); |
467e9f4b5
|
54 |
if (!ns) |
f7af3d1c0
|
55 |
goto fail_dec; |
467e9f4b5
|
56 |
|
6344c433a
|
57 |
err = ns_alloc_inum(&ns->ns); |
f7af3d1c0
|
58 59 |
if (err) goto fail_free; |
98f842e67
|
60 |
|
f7af3d1c0
|
61 |
ns->ucounts = ucounts; |
33c429405
|
62 |
ns->ns.ops = &utsns_operations; |
efc63c4fb
|
63 |
down_read(&uts_sem); |
467e9f4b5
|
64 |
memcpy(&ns->name, &old_ns->name, sizeof(ns->name)); |
bcf58e725
|
65 |
ns->user_ns = get_user_ns(user_ns); |
efc63c4fb
|
66 |
up_read(&uts_sem); |
071df104f
|
67 |
return ns; |
f7af3d1c0
|
68 69 |
fail_free: |
3ea056c50
|
70 |
kmem_cache_free(uts_ns_cache, ns); |
f7af3d1c0
|
71 72 73 74 |
fail_dec: dec_uts_namespaces(ucounts); fail: return ERR_PTR(err); |
071df104f
|
75 76 77 |
} /* |
4865ecf13
|
78 79 80 81 82 |
* 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
|
83 |
struct uts_namespace *copy_utsname(unsigned long flags, |
bcf58e725
|
84 |
struct user_namespace *user_ns, struct uts_namespace *old_ns) |
4865ecf13
|
85 |
{ |
071df104f
|
86 |
struct uts_namespace *new_ns; |
4865ecf13
|
87 |
|
e3222c4ec
|
88 |
BUG_ON(!old_ns); |
4865ecf13
|
89 |
get_uts_ns(old_ns); |
071df104f
|
90 |
if (!(flags & CLONE_NEWUTS)) |
e3222c4ec
|
91 |
return old_ns; |
071df104f
|
92 |
|
bcf58e725
|
93 |
new_ns = clone_uts_ns(user_ns, old_ns); |
071df104f
|
94 |
|
071df104f
|
95 |
put_uts_ns(old_ns); |
e3222c4ec
|
96 |
return new_ns; |
4865ecf13
|
97 98 99 100 101 102 103 |
} void free_uts_ns(struct kref *kref) { struct uts_namespace *ns; ns = container_of(kref, struct uts_namespace, kref); |
f7af3d1c0
|
104 |
dec_uts_namespaces(ns->ucounts); |
59607db36
|
105 |
put_user_ns(ns->user_ns); |
6344c433a
|
106 |
ns_free_inum(&ns->ns); |
3ea056c50
|
107 |
kmem_cache_free(uts_ns_cache, ns); |
4865ecf13
|
108 |
} |
34482e89a
|
109 |
|
3c0411846
|
110 111 112 113 |
static inline struct uts_namespace *to_uts_ns(struct ns_common *ns) { return container_of(ns, struct uts_namespace, ns); } |
64964528b
|
114 |
static struct ns_common *utsns_get(struct task_struct *task) |
34482e89a
|
115 116 117 |
{ struct uts_namespace *ns = NULL; struct nsproxy *nsproxy; |
728dba3a3
|
118 119 |
task_lock(task); nsproxy = task->nsproxy; |
34482e89a
|
120 121 122 123 |
if (nsproxy) { ns = nsproxy->uts_ns; get_uts_ns(ns); } |
728dba3a3
|
124 |
task_unlock(task); |
34482e89a
|
125 |
|
3c0411846
|
126 |
return ns ? &ns->ns : NULL; |
34482e89a
|
127 |
} |
64964528b
|
128 |
static void utsns_put(struct ns_common *ns) |
34482e89a
|
129 |
{ |
3c0411846
|
130 |
put_uts_ns(to_uts_ns(ns)); |
34482e89a
|
131 |
} |
64964528b
|
132 |
static int utsns_install(struct nsproxy *nsproxy, struct ns_common *new) |
34482e89a
|
133 |
{ |
3c0411846
|
134 |
struct uts_namespace *ns = to_uts_ns(new); |
142e1d1d5
|
135 |
|
5e4a08476
|
136 |
if (!ns_capable(ns->user_ns, CAP_SYS_ADMIN) || |
c7b96acf1
|
137 |
!ns_capable(current_user_ns(), CAP_SYS_ADMIN)) |
142e1d1d5
|
138 |
return -EPERM; |
34482e89a
|
139 140 141 142 143 |
get_uts_ns(ns); put_uts_ns(nsproxy->uts_ns); nsproxy->uts_ns = ns; return 0; } |
bcac25a58
|
144 145 146 147 |
static struct user_namespace *utsns_owner(struct ns_common *ns) { return to_uts_ns(ns)->user_ns; } |
34482e89a
|
148 149 150 151 152 153 |
const struct proc_ns_operations utsns_operations = { .name = "uts", .type = CLONE_NEWUTS, .get = utsns_get, .put = utsns_put, .install = utsns_install, |
bcac25a58
|
154 |
.owner = utsns_owner, |
34482e89a
|
155 |
}; |
3ea056c50
|
156 157 158 159 160 161 162 163 164 165 |
void __init uts_ns_init(void) { uts_ns_cache = kmem_cache_create_usercopy( "uts_namespace", sizeof(struct uts_namespace), 0, SLAB_PANIC|SLAB_ACCOUNT, offsetof(struct uts_namespace, name), sizeof_field(struct uts_namespace, name), NULL); } |