xref: /linux/kernel/utsname.c (revision 18b19abc3709b109676ffd1f48dcd332c2e477d4)
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  *  Copyright (C) 2004 IBM Corporation
4  *
5  *  Author: Serge Hallyn <serue@us.ibm.com>
6  */
7 
8 #include <linux/export.h>
9 #include <linux/uts.h>
10 #include <linux/utsname.h>
11 #include <linux/err.h>
12 #include <linux/slab.h>
13 #include <linux/cred.h>
14 #include <linux/user_namespace.h>
15 #include <linux/proc_ns.h>
16 #include <linux/nstree.h>
17 #include <linux/sched/task.h>
18 
19 static struct kmem_cache *uts_ns_cache __ro_after_init;
20 
inc_uts_namespaces(struct user_namespace * ns)21 static struct ucounts *inc_uts_namespaces(struct user_namespace *ns)
22 {
23 	return inc_ucount(ns, current_euid(), UCOUNT_UTS_NAMESPACES);
24 }
25 
dec_uts_namespaces(struct ucounts * ucounts)26 static void dec_uts_namespaces(struct ucounts *ucounts)
27 {
28 	dec_ucount(ucounts, UCOUNT_UTS_NAMESPACES);
29 }
30 
31 /*
32  * Clone a new ns copying an original utsname, setting refcount to 1
33  * @old_ns: namespace to clone
34  * Return ERR_PTR(-ENOMEM) on error (failure to allocate), new ns otherwise
35  */
clone_uts_ns(struct user_namespace * user_ns,struct uts_namespace * old_ns)36 static struct uts_namespace *clone_uts_ns(struct user_namespace *user_ns,
37 					  struct uts_namespace *old_ns)
38 {
39 	struct uts_namespace *ns;
40 	struct ucounts *ucounts;
41 	int err;
42 
43 	err = -ENOSPC;
44 	ucounts = inc_uts_namespaces(user_ns);
45 	if (!ucounts)
46 		goto fail;
47 
48 	err = -ENOMEM;
49 	ns = kmem_cache_zalloc(uts_ns_cache, GFP_KERNEL);
50 	if (!ns)
51 		goto fail_dec;
52 
53 	err = ns_common_init(ns);
54 	if (err)
55 		goto fail_free;
56 
57 	ns->ucounts = ucounts;
58 	down_read(&uts_sem);
59 	memcpy(&ns->name, &old_ns->name, sizeof(ns->name));
60 	ns->user_ns = get_user_ns(user_ns);
61 	up_read(&uts_sem);
62 	ns_tree_add(ns);
63 	return ns;
64 
65 fail_free:
66 	kmem_cache_free(uts_ns_cache, ns);
67 fail_dec:
68 	dec_uts_namespaces(ucounts);
69 fail:
70 	return ERR_PTR(err);
71 }
72 
73 /*
74  * Copy task tsk's utsname namespace, or clone it if flags
75  * specifies CLONE_NEWUTS.  In latter case, changes to the
76  * utsname of this process won't be seen by parent, and vice
77  * versa.
78  */
copy_utsname(u64 flags,struct user_namespace * user_ns,struct uts_namespace * old_ns)79 struct uts_namespace *copy_utsname(u64 flags,
80 	struct user_namespace *user_ns, struct uts_namespace *old_ns)
81 {
82 	struct uts_namespace *new_ns;
83 
84 	BUG_ON(!old_ns);
85 	get_uts_ns(old_ns);
86 
87 	if (!(flags & CLONE_NEWUTS))
88 		return old_ns;
89 
90 	new_ns = clone_uts_ns(user_ns, old_ns);
91 
92 	put_uts_ns(old_ns);
93 	return new_ns;
94 }
95 
free_uts_ns(struct uts_namespace * ns)96 void free_uts_ns(struct uts_namespace *ns)
97 {
98 	ns_tree_remove(ns);
99 	dec_uts_namespaces(ns->ucounts);
100 	put_user_ns(ns->user_ns);
101 	ns_common_free(ns);
102 	/* Concurrent nstree traversal depends on a grace period. */
103 	kfree_rcu(ns, ns.ns_rcu);
104 }
105 
utsns_get(struct task_struct * task)106 static struct ns_common *utsns_get(struct task_struct *task)
107 {
108 	struct uts_namespace *ns = NULL;
109 	struct nsproxy *nsproxy;
110 
111 	task_lock(task);
112 	nsproxy = task->nsproxy;
113 	if (nsproxy) {
114 		ns = nsproxy->uts_ns;
115 		get_uts_ns(ns);
116 	}
117 	task_unlock(task);
118 
119 	return ns ? &ns->ns : NULL;
120 }
121 
utsns_put(struct ns_common * ns)122 static void utsns_put(struct ns_common *ns)
123 {
124 	put_uts_ns(to_uts_ns(ns));
125 }
126 
utsns_install(struct nsset * nsset,struct ns_common * new)127 static int utsns_install(struct nsset *nsset, struct ns_common *new)
128 {
129 	struct nsproxy *nsproxy = nsset->nsproxy;
130 	struct uts_namespace *ns = to_uts_ns(new);
131 
132 	if (!ns_capable(ns->user_ns, CAP_SYS_ADMIN) ||
133 	    !ns_capable(nsset->cred->user_ns, CAP_SYS_ADMIN))
134 		return -EPERM;
135 
136 	get_uts_ns(ns);
137 	put_uts_ns(nsproxy->uts_ns);
138 	nsproxy->uts_ns = ns;
139 	return 0;
140 }
141 
utsns_owner(struct ns_common * ns)142 static struct user_namespace *utsns_owner(struct ns_common *ns)
143 {
144 	return to_uts_ns(ns)->user_ns;
145 }
146 
147 const struct proc_ns_operations utsns_operations = {
148 	.name		= "uts",
149 	.get		= utsns_get,
150 	.put		= utsns_put,
151 	.install	= utsns_install,
152 	.owner		= utsns_owner,
153 };
154 
uts_ns_init(void)155 void __init uts_ns_init(void)
156 {
157 	uts_ns_cache = kmem_cache_create_usercopy(
158 			"uts_namespace", sizeof(struct uts_namespace), 0,
159 			SLAB_PANIC|SLAB_ACCOUNT,
160 			offsetof(struct uts_namespace, name),
161 			sizeof_field(struct uts_namespace, name),
162 			NULL);
163 	ns_tree_add(&init_uts_ns);
164 }
165