xref: /openbmc/linux/kernel/utsname.c (revision f7af3d1c03136275b876f58644599b120cf4ffdd)
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>
1759607db3SSerge E. Hallyn #include <linux/user_namespace.h>
180bb80f24SDavid Howells #include <linux/proc_ns.h>
194865ecf1SSerge E. Hallyn 
20*f7af3d1cSEric W. Biederman static struct ucounts *inc_uts_namespaces(struct user_namespace *ns)
21*f7af3d1cSEric W. Biederman {
22*f7af3d1cSEric W. Biederman 	return inc_ucount(ns, current_euid(), UCOUNT_UTS_NAMESPACES);
23*f7af3d1cSEric W. Biederman }
24*f7af3d1cSEric W. Biederman 
25*f7af3d1cSEric W. Biederman static void dec_uts_namespaces(struct ucounts *ucounts)
26*f7af3d1cSEric W. Biederman {
27*f7af3d1cSEric W. Biederman 	dec_ucount(ucounts, UCOUNT_UTS_NAMESPACES);
28*f7af3d1cSEric W. Biederman }
29*f7af3d1cSEric W. Biederman 
304c2a7e72SAlexey Dobriyan static struct uts_namespace *create_uts_ns(void)
314c2a7e72SAlexey Dobriyan {
324c2a7e72SAlexey Dobriyan 	struct uts_namespace *uts_ns;
334c2a7e72SAlexey Dobriyan 
344c2a7e72SAlexey Dobriyan 	uts_ns = kmalloc(sizeof(struct uts_namespace), GFP_KERNEL);
354c2a7e72SAlexey Dobriyan 	if (uts_ns)
364c2a7e72SAlexey Dobriyan 		kref_init(&uts_ns->kref);
374c2a7e72SAlexey Dobriyan 	return uts_ns;
384c2a7e72SAlexey Dobriyan }
394c2a7e72SAlexey Dobriyan 
404865ecf1SSerge E. Hallyn /*
41071df104SSerge E. Hallyn  * Clone a new ns copying an original utsname, setting refcount to 1
42071df104SSerge E. Hallyn  * @old_ns: namespace to clone
43bf531536SYuanhan Liu  * Return ERR_PTR(-ENOMEM) on error (failure to kmalloc), new ns otherwise
44071df104SSerge E. Hallyn  */
45bcf58e72SEric W. Biederman static struct uts_namespace *clone_uts_ns(struct user_namespace *user_ns,
46bb96a6f5SSerge E. Hallyn 					  struct uts_namespace *old_ns)
47071df104SSerge E. Hallyn {
48071df104SSerge E. Hallyn 	struct uts_namespace *ns;
49*f7af3d1cSEric W. Biederman 	struct ucounts *ucounts;
5098f842e6SEric W. Biederman 	int err;
51071df104SSerge E. Hallyn 
52*f7af3d1cSEric W. Biederman 	err = -ENFILE;
53*f7af3d1cSEric W. Biederman 	ucounts = inc_uts_namespaces(user_ns);
54*f7af3d1cSEric W. Biederman 	if (!ucounts)
55*f7af3d1cSEric W. Biederman 		goto fail;
56*f7af3d1cSEric W. Biederman 
57*f7af3d1cSEric W. Biederman 	err = -ENOMEM;
584c2a7e72SAlexey Dobriyan 	ns = create_uts_ns();
59467e9f4bSCedric Le Goater 	if (!ns)
60*f7af3d1cSEric W. Biederman 		goto fail_dec;
61467e9f4bSCedric Le Goater 
626344c433SAl Viro 	err = ns_alloc_inum(&ns->ns);
63*f7af3d1cSEric W. Biederman 	if (err)
64*f7af3d1cSEric W. Biederman 		goto fail_free;
6598f842e6SEric W. Biederman 
66*f7af3d1cSEric W. Biederman 	ns->ucounts = ucounts;
6733c42940SAl Viro 	ns->ns.ops = &utsns_operations;
6833c42940SAl Viro 
69efc63c4fSAlexey Dobriyan 	down_read(&uts_sem);
70071df104SSerge E. Hallyn 	memcpy(&ns->name, &old_ns->name, sizeof(ns->name));
71bcf58e72SEric W. Biederman 	ns->user_ns = get_user_ns(user_ns);
72efc63c4fSAlexey Dobriyan 	up_read(&uts_sem);
73071df104SSerge E. Hallyn 	return ns;
74*f7af3d1cSEric W. Biederman 
75*f7af3d1cSEric W. Biederman fail_free:
76*f7af3d1cSEric W. Biederman 	kfree(ns);
77*f7af3d1cSEric W. Biederman fail_dec:
78*f7af3d1cSEric W. Biederman 	dec_uts_namespaces(ucounts);
79*f7af3d1cSEric W. Biederman fail:
80*f7af3d1cSEric W. Biederman 	return ERR_PTR(err);
81071df104SSerge E. Hallyn }
82071df104SSerge E. Hallyn 
83071df104SSerge E. Hallyn /*
844865ecf1SSerge E. Hallyn  * Copy task tsk's utsname namespace, or clone it if flags
854865ecf1SSerge E. Hallyn  * specifies CLONE_NEWUTS.  In latter case, changes to the
864865ecf1SSerge E. Hallyn  * utsname of this process won't be seen by parent, and vice
874865ecf1SSerge E. Hallyn  * versa.
884865ecf1SSerge E. Hallyn  */
89bb96a6f5SSerge E. Hallyn struct uts_namespace *copy_utsname(unsigned long flags,
90bcf58e72SEric W. Biederman 	struct user_namespace *user_ns, struct uts_namespace *old_ns)
914865ecf1SSerge E. Hallyn {
92071df104SSerge E. Hallyn 	struct uts_namespace *new_ns;
934865ecf1SSerge E. Hallyn 
94e3222c4eSBadari Pulavarty 	BUG_ON(!old_ns);
954865ecf1SSerge E. Hallyn 	get_uts_ns(old_ns);
964865ecf1SSerge E. Hallyn 
97071df104SSerge E. Hallyn 	if (!(flags & CLONE_NEWUTS))
98e3222c4eSBadari Pulavarty 		return old_ns;
99071df104SSerge E. Hallyn 
100bcf58e72SEric W. Biederman 	new_ns = clone_uts_ns(user_ns, old_ns);
101071df104SSerge E. Hallyn 
102071df104SSerge E. Hallyn 	put_uts_ns(old_ns);
103e3222c4eSBadari Pulavarty 	return new_ns;
1044865ecf1SSerge E. Hallyn }
1054865ecf1SSerge E. Hallyn 
1064865ecf1SSerge E. Hallyn void free_uts_ns(struct kref *kref)
1074865ecf1SSerge E. Hallyn {
1084865ecf1SSerge E. Hallyn 	struct uts_namespace *ns;
1094865ecf1SSerge E. Hallyn 
1104865ecf1SSerge E. Hallyn 	ns = container_of(kref, struct uts_namespace, kref);
111*f7af3d1cSEric W. Biederman 	dec_uts_namespaces(ns->ucounts);
11259607db3SSerge E. Hallyn 	put_user_ns(ns->user_ns);
1136344c433SAl Viro 	ns_free_inum(&ns->ns);
1144865ecf1SSerge E. Hallyn 	kfree(ns);
1154865ecf1SSerge E. Hallyn }
11634482e89SEric W. Biederman 
1173c041184SAl Viro static inline struct uts_namespace *to_uts_ns(struct ns_common *ns)
1183c041184SAl Viro {
1193c041184SAl Viro 	return container_of(ns, struct uts_namespace, ns);
1203c041184SAl Viro }
1213c041184SAl Viro 
12264964528SAl Viro static struct ns_common *utsns_get(struct task_struct *task)
12334482e89SEric W. Biederman {
12434482e89SEric W. Biederman 	struct uts_namespace *ns = NULL;
12534482e89SEric W. Biederman 	struct nsproxy *nsproxy;
12634482e89SEric W. Biederman 
127728dba3aSEric W. Biederman 	task_lock(task);
128728dba3aSEric W. Biederman 	nsproxy = task->nsproxy;
12934482e89SEric W. Biederman 	if (nsproxy) {
13034482e89SEric W. Biederman 		ns = nsproxy->uts_ns;
13134482e89SEric W. Biederman 		get_uts_ns(ns);
13234482e89SEric W. Biederman 	}
133728dba3aSEric W. Biederman 	task_unlock(task);
13434482e89SEric W. Biederman 
1353c041184SAl Viro 	return ns ? &ns->ns : NULL;
13634482e89SEric W. Biederman }
13734482e89SEric W. Biederman 
13864964528SAl Viro static void utsns_put(struct ns_common *ns)
13934482e89SEric W. Biederman {
1403c041184SAl Viro 	put_uts_ns(to_uts_ns(ns));
14134482e89SEric W. Biederman }
14234482e89SEric W. Biederman 
14364964528SAl Viro static int utsns_install(struct nsproxy *nsproxy, struct ns_common *new)
14434482e89SEric W. Biederman {
1453c041184SAl Viro 	struct uts_namespace *ns = to_uts_ns(new);
146142e1d1dSEric W. Biederman 
1475e4a0847SEric W. Biederman 	if (!ns_capable(ns->user_ns, CAP_SYS_ADMIN) ||
148c7b96acfSEric W. Biederman 	    !ns_capable(current_user_ns(), CAP_SYS_ADMIN))
149142e1d1dSEric W. Biederman 		return -EPERM;
150142e1d1dSEric W. Biederman 
15134482e89SEric W. Biederman 	get_uts_ns(ns);
15234482e89SEric W. Biederman 	put_uts_ns(nsproxy->uts_ns);
15334482e89SEric W. Biederman 	nsproxy->uts_ns = ns;
15434482e89SEric W. Biederman 	return 0;
15534482e89SEric W. Biederman }
15634482e89SEric W. Biederman 
15734482e89SEric W. Biederman const struct proc_ns_operations utsns_operations = {
15834482e89SEric W. Biederman 	.name		= "uts",
15934482e89SEric W. Biederman 	.type		= CLONE_NEWUTS,
16034482e89SEric W. Biederman 	.get		= utsns_get,
16134482e89SEric W. Biederman 	.put		= utsns_put,
16234482e89SEric W. Biederman 	.install	= utsns_install,
16334482e89SEric W. Biederman };
164