xref: /openbmc/linux/kernel/utsname.c (revision 5b825c3af1d8a0af4deb4a5eb349d0d0050c62e5)
14865ecf1SSerge E. Hallyn /*
24865ecf1SSerge E. Hallyn  *  Copyright (C) 2004 IBM Corporation
34865ecf1SSerge E. Hallyn  *
44865ecf1SSerge E. Hallyn  *  Author: Serge Hallyn <serue@us.ibm.com>
54865ecf1SSerge E. Hallyn  *
64865ecf1SSerge E. Hallyn  *  This program is free software; you can redistribute it and/or
74865ecf1SSerge E. Hallyn  *  modify it under the terms of the GNU General Public License as
84865ecf1SSerge E. Hallyn  *  published by the Free Software Foundation, version 2 of the
94865ecf1SSerge E. Hallyn  *  License.
104865ecf1SSerge E. Hallyn  */
114865ecf1SSerge E. Hallyn 
129984de1aSPaul Gortmaker #include <linux/export.h>
134865ecf1SSerge E. Hallyn #include <linux/uts.h>
144865ecf1SSerge E. Hallyn #include <linux/utsname.h>
15467e9f4bSCedric Le Goater #include <linux/err.h>
161aeb272cSRobert P. J. Day #include <linux/slab.h>
17*5b825c3aSIngo Molnar #include <linux/cred.h>
1859607db3SSerge E. Hallyn #include <linux/user_namespace.h>
190bb80f24SDavid Howells #include <linux/proc_ns.h>
204865ecf1SSerge E. Hallyn 
21f7af3d1cSEric W. Biederman static struct ucounts *inc_uts_namespaces(struct user_namespace *ns)
22f7af3d1cSEric W. Biederman {
23f7af3d1cSEric W. Biederman 	return inc_ucount(ns, current_euid(), UCOUNT_UTS_NAMESPACES);
24f7af3d1cSEric W. Biederman }
25f7af3d1cSEric W. Biederman 
26f7af3d1cSEric W. Biederman static void dec_uts_namespaces(struct ucounts *ucounts)
27f7af3d1cSEric W. Biederman {
28f7af3d1cSEric W. Biederman 	dec_ucount(ucounts, UCOUNT_UTS_NAMESPACES);
29f7af3d1cSEric W. Biederman }
30f7af3d1cSEric W. Biederman 
314c2a7e72SAlexey Dobriyan static struct uts_namespace *create_uts_ns(void)
324c2a7e72SAlexey Dobriyan {
334c2a7e72SAlexey Dobriyan 	struct uts_namespace *uts_ns;
344c2a7e72SAlexey Dobriyan 
354c2a7e72SAlexey Dobriyan 	uts_ns = kmalloc(sizeof(struct uts_namespace), GFP_KERNEL);
364c2a7e72SAlexey Dobriyan 	if (uts_ns)
374c2a7e72SAlexey Dobriyan 		kref_init(&uts_ns->kref);
384c2a7e72SAlexey Dobriyan 	return uts_ns;
394c2a7e72SAlexey Dobriyan }
404c2a7e72SAlexey Dobriyan 
414865ecf1SSerge E. Hallyn /*
42071df104SSerge E. Hallyn  * Clone a new ns copying an original utsname, setting refcount to 1
43071df104SSerge E. Hallyn  * @old_ns: namespace to clone
44bf531536SYuanhan Liu  * Return ERR_PTR(-ENOMEM) on error (failure to kmalloc), new ns otherwise
45071df104SSerge E. Hallyn  */
46bcf58e72SEric W. Biederman static struct uts_namespace *clone_uts_ns(struct user_namespace *user_ns,
47bb96a6f5SSerge E. Hallyn 					  struct uts_namespace *old_ns)
48071df104SSerge E. Hallyn {
49071df104SSerge E. Hallyn 	struct uts_namespace *ns;
50f7af3d1cSEric W. Biederman 	struct ucounts *ucounts;
5198f842e6SEric W. Biederman 	int err;
52071df104SSerge E. Hallyn 
53df75e774SEric W. Biederman 	err = -ENOSPC;
54f7af3d1cSEric W. Biederman 	ucounts = inc_uts_namespaces(user_ns);
55f7af3d1cSEric W. Biederman 	if (!ucounts)
56f7af3d1cSEric W. Biederman 		goto fail;
57f7af3d1cSEric W. Biederman 
58f7af3d1cSEric W. Biederman 	err = -ENOMEM;
594c2a7e72SAlexey Dobriyan 	ns = create_uts_ns();
60467e9f4bSCedric Le Goater 	if (!ns)
61f7af3d1cSEric W. Biederman 		goto fail_dec;
62467e9f4bSCedric Le Goater 
636344c433SAl Viro 	err = ns_alloc_inum(&ns->ns);
64f7af3d1cSEric W. Biederman 	if (err)
65f7af3d1cSEric W. Biederman 		goto fail_free;
6698f842e6SEric W. Biederman 
67f7af3d1cSEric W. Biederman 	ns->ucounts = ucounts;
6833c42940SAl Viro 	ns->ns.ops = &utsns_operations;
6933c42940SAl Viro 
70efc63c4fSAlexey Dobriyan 	down_read(&uts_sem);
71071df104SSerge E. Hallyn 	memcpy(&ns->name, &old_ns->name, sizeof(ns->name));
72bcf58e72SEric W. Biederman 	ns->user_ns = get_user_ns(user_ns);
73efc63c4fSAlexey Dobriyan 	up_read(&uts_sem);
74071df104SSerge E. Hallyn 	return ns;
75f7af3d1cSEric W. Biederman 
76f7af3d1cSEric W. Biederman fail_free:
77f7af3d1cSEric W. Biederman 	kfree(ns);
78f7af3d1cSEric W. Biederman fail_dec:
79f7af3d1cSEric W. Biederman 	dec_uts_namespaces(ucounts);
80f7af3d1cSEric W. Biederman fail:
81f7af3d1cSEric W. Biederman 	return ERR_PTR(err);
82071df104SSerge E. Hallyn }
83071df104SSerge E. Hallyn 
84071df104SSerge E. Hallyn /*
854865ecf1SSerge E. Hallyn  * Copy task tsk's utsname namespace, or clone it if flags
864865ecf1SSerge E. Hallyn  * specifies CLONE_NEWUTS.  In latter case, changes to the
874865ecf1SSerge E. Hallyn  * utsname of this process won't be seen by parent, and vice
884865ecf1SSerge E. Hallyn  * versa.
894865ecf1SSerge E. Hallyn  */
90bb96a6f5SSerge E. Hallyn struct uts_namespace *copy_utsname(unsigned long flags,
91bcf58e72SEric W. Biederman 	struct user_namespace *user_ns, struct uts_namespace *old_ns)
924865ecf1SSerge E. Hallyn {
93071df104SSerge E. Hallyn 	struct uts_namespace *new_ns;
944865ecf1SSerge E. Hallyn 
95e3222c4eSBadari Pulavarty 	BUG_ON(!old_ns);
964865ecf1SSerge E. Hallyn 	get_uts_ns(old_ns);
974865ecf1SSerge E. Hallyn 
98071df104SSerge E. Hallyn 	if (!(flags & CLONE_NEWUTS))
99e3222c4eSBadari Pulavarty 		return old_ns;
100071df104SSerge E. Hallyn 
101bcf58e72SEric W. Biederman 	new_ns = clone_uts_ns(user_ns, old_ns);
102071df104SSerge E. Hallyn 
103071df104SSerge E. Hallyn 	put_uts_ns(old_ns);
104e3222c4eSBadari Pulavarty 	return new_ns;
1054865ecf1SSerge E. Hallyn }
1064865ecf1SSerge E. Hallyn 
1074865ecf1SSerge E. Hallyn void free_uts_ns(struct kref *kref)
1084865ecf1SSerge E. Hallyn {
1094865ecf1SSerge E. Hallyn 	struct uts_namespace *ns;
1104865ecf1SSerge E. Hallyn 
1114865ecf1SSerge E. Hallyn 	ns = container_of(kref, struct uts_namespace, kref);
112f7af3d1cSEric W. Biederman 	dec_uts_namespaces(ns->ucounts);
11359607db3SSerge E. Hallyn 	put_user_ns(ns->user_ns);
1146344c433SAl Viro 	ns_free_inum(&ns->ns);
1154865ecf1SSerge E. Hallyn 	kfree(ns);
1164865ecf1SSerge E. Hallyn }
11734482e89SEric W. Biederman 
1183c041184SAl Viro static inline struct uts_namespace *to_uts_ns(struct ns_common *ns)
1193c041184SAl Viro {
1203c041184SAl Viro 	return container_of(ns, struct uts_namespace, ns);
1213c041184SAl Viro }
1223c041184SAl Viro 
12364964528SAl Viro static struct ns_common *utsns_get(struct task_struct *task)
12434482e89SEric W. Biederman {
12534482e89SEric W. Biederman 	struct uts_namespace *ns = NULL;
12634482e89SEric W. Biederman 	struct nsproxy *nsproxy;
12734482e89SEric W. Biederman 
128728dba3aSEric W. Biederman 	task_lock(task);
129728dba3aSEric W. Biederman 	nsproxy = task->nsproxy;
13034482e89SEric W. Biederman 	if (nsproxy) {
13134482e89SEric W. Biederman 		ns = nsproxy->uts_ns;
13234482e89SEric W. Biederman 		get_uts_ns(ns);
13334482e89SEric W. Biederman 	}
134728dba3aSEric W. Biederman 	task_unlock(task);
13534482e89SEric W. Biederman 
1363c041184SAl Viro 	return ns ? &ns->ns : NULL;
13734482e89SEric W. Biederman }
13834482e89SEric W. Biederman 
13964964528SAl Viro static void utsns_put(struct ns_common *ns)
14034482e89SEric W. Biederman {
1413c041184SAl Viro 	put_uts_ns(to_uts_ns(ns));
14234482e89SEric W. Biederman }
14334482e89SEric W. Biederman 
14464964528SAl Viro static int utsns_install(struct nsproxy *nsproxy, struct ns_common *new)
14534482e89SEric W. Biederman {
1463c041184SAl Viro 	struct uts_namespace *ns = to_uts_ns(new);
147142e1d1dSEric W. Biederman 
1485e4a0847SEric W. Biederman 	if (!ns_capable(ns->user_ns, CAP_SYS_ADMIN) ||
149c7b96acfSEric W. Biederman 	    !ns_capable(current_user_ns(), CAP_SYS_ADMIN))
150142e1d1dSEric W. Biederman 		return -EPERM;
151142e1d1dSEric W. Biederman 
15234482e89SEric W. Biederman 	get_uts_ns(ns);
15334482e89SEric W. Biederman 	put_uts_ns(nsproxy->uts_ns);
15434482e89SEric W. Biederman 	nsproxy->uts_ns = ns;
15534482e89SEric W. Biederman 	return 0;
15634482e89SEric W. Biederman }
15734482e89SEric W. Biederman 
158bcac25a5SAndrey Vagin static struct user_namespace *utsns_owner(struct ns_common *ns)
159bcac25a5SAndrey Vagin {
160bcac25a5SAndrey Vagin 	return to_uts_ns(ns)->user_ns;
161bcac25a5SAndrey Vagin }
162bcac25a5SAndrey Vagin 
16334482e89SEric W. Biederman const struct proc_ns_operations utsns_operations = {
16434482e89SEric W. Biederman 	.name		= "uts",
16534482e89SEric W. Biederman 	.type		= CLONE_NEWUTS,
16634482e89SEric W. Biederman 	.get		= utsns_get,
16734482e89SEric W. Biederman 	.put		= utsns_put,
16834482e89SEric W. Biederman 	.install	= utsns_install,
169bcac25a5SAndrey Vagin 	.owner		= utsns_owner,
17034482e89SEric W. Biederman };
171