1b886d83cSThomas Gleixner // SPDX-License-Identifier: GPL-2.0-only
24865ecf1SSerge E. Hallyn /*
34865ecf1SSerge E. Hallyn * Copyright (C) 2004 IBM Corporation
44865ecf1SSerge E. Hallyn *
54865ecf1SSerge E. Hallyn * Author: Serge Hallyn <serue@us.ibm.com>
64865ecf1SSerge E. Hallyn */
74865ecf1SSerge E. Hallyn
89984de1aSPaul Gortmaker #include <linux/export.h>
94865ecf1SSerge E. Hallyn #include <linux/uts.h>
104865ecf1SSerge E. Hallyn #include <linux/utsname.h>
11467e9f4bSCedric Le Goater #include <linux/err.h>
121aeb272cSRobert P. J. Day #include <linux/slab.h>
135b825c3aSIngo Molnar #include <linux/cred.h>
1459607db3SSerge E. Hallyn #include <linux/user_namespace.h>
150bb80f24SDavid Howells #include <linux/proc_ns.h>
16f719ff9bSIngo Molnar #include <linux/sched/task.h>
174865ecf1SSerge E. Hallyn
183ea056c5SAlexey Dobriyan static struct kmem_cache *uts_ns_cache __ro_after_init;
193ea056c5SAlexey Dobriyan
inc_uts_namespaces(struct user_namespace * ns)20f7af3d1cSEric W. Biederman static struct ucounts *inc_uts_namespaces(struct user_namespace *ns)
21f7af3d1cSEric W. Biederman {
22f7af3d1cSEric W. Biederman return inc_ucount(ns, current_euid(), UCOUNT_UTS_NAMESPACES);
23f7af3d1cSEric W. Biederman }
24f7af3d1cSEric W. Biederman
dec_uts_namespaces(struct ucounts * ucounts)25f7af3d1cSEric W. Biederman static void dec_uts_namespaces(struct ucounts *ucounts)
26f7af3d1cSEric W. Biederman {
27f7af3d1cSEric W. Biederman dec_ucount(ucounts, UCOUNT_UTS_NAMESPACES);
28f7af3d1cSEric W. Biederman }
29f7af3d1cSEric W. Biederman
create_uts_ns(void)304c2a7e72SAlexey Dobriyan static struct uts_namespace *create_uts_ns(void)
314c2a7e72SAlexey Dobriyan {
324c2a7e72SAlexey Dobriyan struct uts_namespace *uts_ns;
334c2a7e72SAlexey Dobriyan
343ea056c5SAlexey Dobriyan uts_ns = kmem_cache_alloc(uts_ns_cache, GFP_KERNEL);
354c2a7e72SAlexey Dobriyan if (uts_ns)
36*9a56493fSKirill Tkhai refcount_set(&uts_ns->ns.count, 1);
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
433ea056c5SAlexey Dobriyan * Return ERR_PTR(-ENOMEM) on error (failure to allocate), new ns otherwise
44071df104SSerge E. Hallyn */
clone_uts_ns(struct user_namespace * user_ns,struct uts_namespace * old_ns)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;
49f7af3d1cSEric W. Biederman struct ucounts *ucounts;
5098f842e6SEric W. Biederman int err;
51071df104SSerge E. Hallyn
52df75e774SEric W. Biederman err = -ENOSPC;
53f7af3d1cSEric W. Biederman ucounts = inc_uts_namespaces(user_ns);
54f7af3d1cSEric W. Biederman if (!ucounts)
55f7af3d1cSEric W. Biederman goto fail;
56f7af3d1cSEric W. Biederman
57f7af3d1cSEric W. Biederman err = -ENOMEM;
584c2a7e72SAlexey Dobriyan ns = create_uts_ns();
59467e9f4bSCedric Le Goater if (!ns)
60f7af3d1cSEric W. Biederman goto fail_dec;
61467e9f4bSCedric Le Goater
626344c433SAl Viro err = ns_alloc_inum(&ns->ns);
63f7af3d1cSEric W. Biederman if (err)
64f7af3d1cSEric W. Biederman goto fail_free;
6598f842e6SEric W. Biederman
66f7af3d1cSEric 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;
74f7af3d1cSEric W. Biederman
75f7af3d1cSEric W. Biederman fail_free:
763ea056c5SAlexey Dobriyan kmem_cache_free(uts_ns_cache, ns);
77f7af3d1cSEric W. Biederman fail_dec:
78f7af3d1cSEric W. Biederman dec_uts_namespaces(ucounts);
79f7af3d1cSEric W. Biederman fail:
80f7af3d1cSEric 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 */
copy_utsname(unsigned long flags,struct user_namespace * user_ns,struct uts_namespace * old_ns)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
free_uts_ns(struct uts_namespace * ns)106*9a56493fSKirill Tkhai void free_uts_ns(struct uts_namespace *ns)
1074865ecf1SSerge E. Hallyn {
108f7af3d1cSEric W. Biederman dec_uts_namespaces(ns->ucounts);
10959607db3SSerge E. Hallyn put_user_ns(ns->user_ns);
1106344c433SAl Viro ns_free_inum(&ns->ns);
1113ea056c5SAlexey Dobriyan kmem_cache_free(uts_ns_cache, ns);
1124865ecf1SSerge E. Hallyn }
11334482e89SEric W. Biederman
to_uts_ns(struct ns_common * ns)1143c041184SAl Viro static inline struct uts_namespace *to_uts_ns(struct ns_common *ns)
1153c041184SAl Viro {
1163c041184SAl Viro return container_of(ns, struct uts_namespace, ns);
1173c041184SAl Viro }
1183c041184SAl Viro
utsns_get(struct task_struct * task)11964964528SAl Viro static struct ns_common *utsns_get(struct task_struct *task)
12034482e89SEric W. Biederman {
12134482e89SEric W. Biederman struct uts_namespace *ns = NULL;
12234482e89SEric W. Biederman struct nsproxy *nsproxy;
12334482e89SEric W. Biederman
124728dba3aSEric W. Biederman task_lock(task);
125728dba3aSEric W. Biederman nsproxy = task->nsproxy;
12634482e89SEric W. Biederman if (nsproxy) {
12734482e89SEric W. Biederman ns = nsproxy->uts_ns;
12834482e89SEric W. Biederman get_uts_ns(ns);
12934482e89SEric W. Biederman }
130728dba3aSEric W. Biederman task_unlock(task);
13134482e89SEric W. Biederman
1323c041184SAl Viro return ns ? &ns->ns : NULL;
13334482e89SEric W. Biederman }
13434482e89SEric W. Biederman
utsns_put(struct ns_common * ns)13564964528SAl Viro static void utsns_put(struct ns_common *ns)
13634482e89SEric W. Biederman {
1373c041184SAl Viro put_uts_ns(to_uts_ns(ns));
13834482e89SEric W. Biederman }
13934482e89SEric W. Biederman
utsns_install(struct nsset * nsset,struct ns_common * new)140f2a8d52eSChristian Brauner static int utsns_install(struct nsset *nsset, struct ns_common *new)
14134482e89SEric W. Biederman {
142f2a8d52eSChristian Brauner struct nsproxy *nsproxy = nsset->nsproxy;
1433c041184SAl Viro struct uts_namespace *ns = to_uts_ns(new);
144142e1d1dSEric W. Biederman
1455e4a0847SEric W. Biederman if (!ns_capable(ns->user_ns, CAP_SYS_ADMIN) ||
146f2a8d52eSChristian Brauner !ns_capable(nsset->cred->user_ns, CAP_SYS_ADMIN))
147142e1d1dSEric W. Biederman return -EPERM;
148142e1d1dSEric W. Biederman
14934482e89SEric W. Biederman get_uts_ns(ns);
15034482e89SEric W. Biederman put_uts_ns(nsproxy->uts_ns);
15134482e89SEric W. Biederman nsproxy->uts_ns = ns;
15234482e89SEric W. Biederman return 0;
15334482e89SEric W. Biederman }
15434482e89SEric W. Biederman
utsns_owner(struct ns_common * ns)155bcac25a5SAndrey Vagin static struct user_namespace *utsns_owner(struct ns_common *ns)
156bcac25a5SAndrey Vagin {
157bcac25a5SAndrey Vagin return to_uts_ns(ns)->user_ns;
158bcac25a5SAndrey Vagin }
159bcac25a5SAndrey Vagin
16034482e89SEric W. Biederman const struct proc_ns_operations utsns_operations = {
16134482e89SEric W. Biederman .name = "uts",
16234482e89SEric W. Biederman .type = CLONE_NEWUTS,
16334482e89SEric W. Biederman .get = utsns_get,
16434482e89SEric W. Biederman .put = utsns_put,
16534482e89SEric W. Biederman .install = utsns_install,
166bcac25a5SAndrey Vagin .owner = utsns_owner,
16734482e89SEric W. Biederman };
1683ea056c5SAlexey Dobriyan
uts_ns_init(void)1693ea056c5SAlexey Dobriyan void __init uts_ns_init(void)
1703ea056c5SAlexey Dobriyan {
1713ea056c5SAlexey Dobriyan uts_ns_cache = kmem_cache_create_usercopy(
1723ea056c5SAlexey Dobriyan "uts_namespace", sizeof(struct uts_namespace), 0,
1733ea056c5SAlexey Dobriyan SLAB_PANIC|SLAB_ACCOUNT,
1743ea056c5SAlexey Dobriyan offsetof(struct uts_namespace, name),
1753ea056c5SAlexey Dobriyan sizeof_field(struct uts_namespace, name),
1763ea056c5SAlexey Dobriyan NULL);
1773ea056c5SAlexey Dobriyan }
178