2 * Copyright (C) 2004 IBM Corporation
4 * Author: Serge Hallyn <serue@us.ibm.com>
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License as
8 * published by the Free Software Foundation, version 2 of the
12 #include <linux/export.h>
13 #include <linux/uts.h>
14 #include <linux/utsname.h>
15 #include <linux/err.h>
16 #include <linux/slab.h>
17 #include <linux/cred.h>
18 #include <linux/user_namespace.h>
19 #include <linux/proc_ns.h>
21 static struct ucounts *inc_uts_namespaces(struct user_namespace *ns)
23 return inc_ucount(ns, current_euid(), UCOUNT_UTS_NAMESPACES);
26 static void dec_uts_namespaces(struct ucounts *ucounts)
28 dec_ucount(ucounts, UCOUNT_UTS_NAMESPACES);
31 static struct uts_namespace *create_uts_ns(void)
33 struct uts_namespace *uts_ns;
35 uts_ns = kmalloc(sizeof(struct uts_namespace), GFP_KERNEL);
37 kref_init(&uts_ns->kref);
42 * Clone a new ns copying an original utsname, setting refcount to 1
43 * @old_ns: namespace to clone
44 * Return ERR_PTR(-ENOMEM) on error (failure to kmalloc), new ns otherwise
46 static struct uts_namespace *clone_uts_ns(struct user_namespace *user_ns,
47 struct uts_namespace *old_ns)
49 struct uts_namespace *ns;
50 struct ucounts *ucounts;
54 ucounts = inc_uts_namespaces(user_ns);
63 err = ns_alloc_inum(&ns->ns);
67 ns->ucounts = ucounts;
68 ns->ns.ops = &utsns_operations;
71 memcpy(&ns->name, &old_ns->name, sizeof(ns->name));
72 ns->user_ns = get_user_ns(user_ns);
79 dec_uts_namespaces(ucounts);
85 * Copy task tsk's utsname namespace, or clone it if flags
86 * specifies CLONE_NEWUTS. In latter case, changes to the
87 * utsname of this process won't be seen by parent, and vice
90 struct uts_namespace *copy_utsname(unsigned long flags,
91 struct user_namespace *user_ns, struct uts_namespace *old_ns)
93 struct uts_namespace *new_ns;
98 if (!(flags & CLONE_NEWUTS))
101 new_ns = clone_uts_ns(user_ns, old_ns);
107 void free_uts_ns(struct kref *kref)
109 struct uts_namespace *ns;
111 ns = container_of(kref, struct uts_namespace, kref);
112 dec_uts_namespaces(ns->ucounts);
113 put_user_ns(ns->user_ns);
114 ns_free_inum(&ns->ns);
118 static inline struct uts_namespace *to_uts_ns(struct ns_common *ns)
120 return container_of(ns, struct uts_namespace, ns);
123 static struct ns_common *utsns_get(struct task_struct *task)
125 struct uts_namespace *ns = NULL;
126 struct nsproxy *nsproxy;
129 nsproxy = task->nsproxy;
131 ns = nsproxy->uts_ns;
136 return ns ? &ns->ns : NULL;
139 static void utsns_put(struct ns_common *ns)
141 put_uts_ns(to_uts_ns(ns));
144 static int utsns_install(struct nsproxy *nsproxy, struct ns_common *new)
146 struct uts_namespace *ns = to_uts_ns(new);
148 if (!ns_capable(ns->user_ns, CAP_SYS_ADMIN) ||
149 !ns_capable(current_user_ns(), CAP_SYS_ADMIN))
153 put_uts_ns(nsproxy->uts_ns);
154 nsproxy->uts_ns = ns;
158 static struct user_namespace *utsns_owner(struct ns_common *ns)
160 return to_uts_ns(ns)->user_ns;
163 const struct proc_ns_operations utsns_operations = {
165 .type = CLONE_NEWUTS,
168 .install = utsns_install,
169 .owner = utsns_owner,