xref: /openbmc/linux/kernel/utsname.c (revision f719ff9bcee2a422647790f12d53d3755f47c727)
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>
175b825c3aSIngo Molnar #include <linux/cred.h>
1859607db3SSerge E. Hallyn #include <linux/user_namespace.h>
190bb80f24SDavid Howells #include <linux/proc_ns.h>
20*f719ff9bSIngo Molnar #include <linux/sched/task.h>
214865ecf1SSerge E. Hallyn 
22f7af3d1cSEric W. Biederman static struct ucounts *inc_uts_namespaces(struct user_namespace *ns)
23f7af3d1cSEric W. Biederman {
24f7af3d1cSEric W. Biederman 	return inc_ucount(ns, current_euid(), UCOUNT_UTS_NAMESPACES);
25f7af3d1cSEric W. Biederman }
26f7af3d1cSEric W. Biederman 
27f7af3d1cSEric W. Biederman static void dec_uts_namespaces(struct ucounts *ucounts)
28f7af3d1cSEric W. Biederman {
29f7af3d1cSEric W. Biederman 	dec_ucount(ucounts, UCOUNT_UTS_NAMESPACES);
30f7af3d1cSEric W. Biederman }
31f7af3d1cSEric W. Biederman 
324c2a7e72SAlexey Dobriyan static struct uts_namespace *create_uts_ns(void)
334c2a7e72SAlexey Dobriyan {
344c2a7e72SAlexey Dobriyan 	struct uts_namespace *uts_ns;
354c2a7e72SAlexey Dobriyan 
364c2a7e72SAlexey Dobriyan 	uts_ns = kmalloc(sizeof(struct uts_namespace), GFP_KERNEL);
374c2a7e72SAlexey Dobriyan 	if (uts_ns)
384c2a7e72SAlexey Dobriyan 		kref_init(&uts_ns->kref);
394c2a7e72SAlexey Dobriyan 	return uts_ns;
404c2a7e72SAlexey Dobriyan }
414c2a7e72SAlexey Dobriyan 
424865ecf1SSerge E. Hallyn /*
43071df104SSerge E. Hallyn  * Clone a new ns copying an original utsname, setting refcount to 1
44071df104SSerge E. Hallyn  * @old_ns: namespace to clone
45bf531536SYuanhan Liu  * Return ERR_PTR(-ENOMEM) on error (failure to kmalloc), new ns otherwise
46071df104SSerge E. Hallyn  */
47bcf58e72SEric W. Biederman static struct uts_namespace *clone_uts_ns(struct user_namespace *user_ns,
48bb96a6f5SSerge E. Hallyn 					  struct uts_namespace *old_ns)
49071df104SSerge E. Hallyn {
50071df104SSerge E. Hallyn 	struct uts_namespace *ns;
51f7af3d1cSEric W. Biederman 	struct ucounts *ucounts;
5298f842e6SEric W. Biederman 	int err;
53071df104SSerge E. Hallyn 
54df75e774SEric W. Biederman 	err = -ENOSPC;
55f7af3d1cSEric W. Biederman 	ucounts = inc_uts_namespaces(user_ns);
56f7af3d1cSEric W. Biederman 	if (!ucounts)
57f7af3d1cSEric W. Biederman 		goto fail;
58f7af3d1cSEric W. Biederman 
59f7af3d1cSEric W. Biederman 	err = -ENOMEM;
604c2a7e72SAlexey Dobriyan 	ns = create_uts_ns();
61467e9f4bSCedric Le Goater 	if (!ns)
62f7af3d1cSEric W. Biederman 		goto fail_dec;
63467e9f4bSCedric Le Goater 
646344c433SAl Viro 	err = ns_alloc_inum(&ns->ns);
65f7af3d1cSEric W. Biederman 	if (err)
66f7af3d1cSEric W. Biederman 		goto fail_free;
6798f842e6SEric W. Biederman 
68f7af3d1cSEric W. Biederman 	ns->ucounts = ucounts;
6933c42940SAl Viro 	ns->ns.ops = &utsns_operations;
7033c42940SAl Viro 
71efc63c4fSAlexey Dobriyan 	down_read(&uts_sem);
72071df104SSerge E. Hallyn 	memcpy(&ns->name, &old_ns->name, sizeof(ns->name));
73bcf58e72SEric W. Biederman 	ns->user_ns = get_user_ns(user_ns);
74efc63c4fSAlexey Dobriyan 	up_read(&uts_sem);
75071df104SSerge E. Hallyn 	return ns;
76f7af3d1cSEric W. Biederman 
77f7af3d1cSEric W. Biederman fail_free:
78f7af3d1cSEric W. Biederman 	kfree(ns);
79f7af3d1cSEric W. Biederman fail_dec:
80f7af3d1cSEric W. Biederman 	dec_uts_namespaces(ucounts);
81f7af3d1cSEric W. Biederman fail:
82f7af3d1cSEric W. Biederman 	return ERR_PTR(err);
83071df104SSerge E. Hallyn }
84071df104SSerge E. Hallyn 
85071df104SSerge E. Hallyn /*
864865ecf1SSerge E. Hallyn  * Copy task tsk's utsname namespace, or clone it if flags
874865ecf1SSerge E. Hallyn  * specifies CLONE_NEWUTS.  In latter case, changes to the
884865ecf1SSerge E. Hallyn  * utsname of this process won't be seen by parent, and vice
894865ecf1SSerge E. Hallyn  * versa.
904865ecf1SSerge E. Hallyn  */
91bb96a6f5SSerge E. Hallyn struct uts_namespace *copy_utsname(unsigned long flags,
92bcf58e72SEric W. Biederman 	struct user_namespace *user_ns, struct uts_namespace *old_ns)
934865ecf1SSerge E. Hallyn {
94071df104SSerge E. Hallyn 	struct uts_namespace *new_ns;
954865ecf1SSerge E. Hallyn 
96e3222c4eSBadari Pulavarty 	BUG_ON(!old_ns);
974865ecf1SSerge E. Hallyn 	get_uts_ns(old_ns);
984865ecf1SSerge E. Hallyn 
99071df104SSerge E. Hallyn 	if (!(flags & CLONE_NEWUTS))
100e3222c4eSBadari Pulavarty 		return old_ns;
101071df104SSerge E. Hallyn 
102bcf58e72SEric W. Biederman 	new_ns = clone_uts_ns(user_ns, old_ns);
103071df104SSerge E. Hallyn 
104071df104SSerge E. Hallyn 	put_uts_ns(old_ns);
105e3222c4eSBadari Pulavarty 	return new_ns;
1064865ecf1SSerge E. Hallyn }
1074865ecf1SSerge E. Hallyn 
1084865ecf1SSerge E. Hallyn void free_uts_ns(struct kref *kref)
1094865ecf1SSerge E. Hallyn {
1104865ecf1SSerge E. Hallyn 	struct uts_namespace *ns;
1114865ecf1SSerge E. Hallyn 
1124865ecf1SSerge E. Hallyn 	ns = container_of(kref, struct uts_namespace, kref);
113f7af3d1cSEric W. Biederman 	dec_uts_namespaces(ns->ucounts);
11459607db3SSerge E. Hallyn 	put_user_ns(ns->user_ns);
1156344c433SAl Viro 	ns_free_inum(&ns->ns);
1164865ecf1SSerge E. Hallyn 	kfree(ns);
1174865ecf1SSerge E. Hallyn }
11834482e89SEric W. Biederman 
1193c041184SAl Viro static inline struct uts_namespace *to_uts_ns(struct ns_common *ns)
1203c041184SAl Viro {
1213c041184SAl Viro 	return container_of(ns, struct uts_namespace, ns);
1223c041184SAl Viro }
1233c041184SAl Viro 
12464964528SAl Viro static struct ns_common *utsns_get(struct task_struct *task)
12534482e89SEric W. Biederman {
12634482e89SEric W. Biederman 	struct uts_namespace *ns = NULL;
12734482e89SEric W. Biederman 	struct nsproxy *nsproxy;
12834482e89SEric W. Biederman 
129728dba3aSEric W. Biederman 	task_lock(task);
130728dba3aSEric W. Biederman 	nsproxy = task->nsproxy;
13134482e89SEric W. Biederman 	if (nsproxy) {
13234482e89SEric W. Biederman 		ns = nsproxy->uts_ns;
13334482e89SEric W. Biederman 		get_uts_ns(ns);
13434482e89SEric W. Biederman 	}
135728dba3aSEric W. Biederman 	task_unlock(task);
13634482e89SEric W. Biederman 
1373c041184SAl Viro 	return ns ? &ns->ns : NULL;
13834482e89SEric W. Biederman }
13934482e89SEric W. Biederman 
14064964528SAl Viro static void utsns_put(struct ns_common *ns)
14134482e89SEric W. Biederman {
1423c041184SAl Viro 	put_uts_ns(to_uts_ns(ns));
14334482e89SEric W. Biederman }
14434482e89SEric W. Biederman 
14564964528SAl Viro static int utsns_install(struct nsproxy *nsproxy, struct ns_common *new)
14634482e89SEric W. Biederman {
1473c041184SAl Viro 	struct uts_namespace *ns = to_uts_ns(new);
148142e1d1dSEric W. Biederman 
1495e4a0847SEric W. Biederman 	if (!ns_capable(ns->user_ns, CAP_SYS_ADMIN) ||
150c7b96acfSEric W. Biederman 	    !ns_capable(current_user_ns(), CAP_SYS_ADMIN))
151142e1d1dSEric W. Biederman 		return -EPERM;
152142e1d1dSEric W. Biederman 
15334482e89SEric W. Biederman 	get_uts_ns(ns);
15434482e89SEric W. Biederman 	put_uts_ns(nsproxy->uts_ns);
15534482e89SEric W. Biederman 	nsproxy->uts_ns = ns;
15634482e89SEric W. Biederman 	return 0;
15734482e89SEric W. Biederman }
15834482e89SEric W. Biederman 
159bcac25a5SAndrey Vagin static struct user_namespace *utsns_owner(struct ns_common *ns)
160bcac25a5SAndrey Vagin {
161bcac25a5SAndrey Vagin 	return to_uts_ns(ns)->user_ns;
162bcac25a5SAndrey Vagin }
163bcac25a5SAndrey Vagin 
16434482e89SEric W. Biederman const struct proc_ns_operations utsns_operations = {
16534482e89SEric W. Biederman 	.name		= "uts",
16634482e89SEric W. Biederman 	.type		= CLONE_NEWUTS,
16734482e89SEric W. Biederman 	.get		= utsns_get,
16834482e89SEric W. Biederman 	.put		= utsns_put,
16934482e89SEric W. Biederman 	.install	= utsns_install,
170bcac25a5SAndrey Vagin 	.owner		= utsns_owner,
17134482e89SEric W. Biederman };
172