]> git.proxmox.com Git - mirror_ubuntu-artful-kernel.git/blame - kernel/utsname.c
sched/headers: Prepare to remove <linux/cred.h> inclusion from <linux/sched.h>
[mirror_ubuntu-artful-kernel.git] / kernel / utsname.c
CommitLineData
4865ecf1
SH
1/*
2 * Copyright (C) 2004 IBM Corporation
3 *
4 * Author: Serge Hallyn <serue@us.ibm.com>
5 *
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
9 * License.
10 */
11
9984de1a 12#include <linux/export.h>
4865ecf1
SH
13#include <linux/uts.h>
14#include <linux/utsname.h>
467e9f4b 15#include <linux/err.h>
1aeb272c 16#include <linux/slab.h>
5b825c3a 17#include <linux/cred.h>
59607db3 18#include <linux/user_namespace.h>
0bb80f24 19#include <linux/proc_ns.h>
4865ecf1 20
f7af3d1c
EB
21static struct ucounts *inc_uts_namespaces(struct user_namespace *ns)
22{
23 return inc_ucount(ns, current_euid(), UCOUNT_UTS_NAMESPACES);
24}
25
26static void dec_uts_namespaces(struct ucounts *ucounts)
27{
28 dec_ucount(ucounts, UCOUNT_UTS_NAMESPACES);
29}
30
4c2a7e72
AD
31static struct uts_namespace *create_uts_ns(void)
32{
33 struct uts_namespace *uts_ns;
34
35 uts_ns = kmalloc(sizeof(struct uts_namespace), GFP_KERNEL);
36 if (uts_ns)
37 kref_init(&uts_ns->kref);
38 return uts_ns;
39}
40
071df104
SH
41/*
42 * Clone a new ns copying an original utsname, setting refcount to 1
43 * @old_ns: namespace to clone
bf531536 44 * Return ERR_PTR(-ENOMEM) on error (failure to kmalloc), new ns otherwise
071df104 45 */
bcf58e72 46static struct uts_namespace *clone_uts_ns(struct user_namespace *user_ns,
bb96a6f5 47 struct uts_namespace *old_ns)
071df104
SH
48{
49 struct uts_namespace *ns;
f7af3d1c 50 struct ucounts *ucounts;
98f842e6 51 int err;
071df104 52
df75e774 53 err = -ENOSPC;
f7af3d1c
EB
54 ucounts = inc_uts_namespaces(user_ns);
55 if (!ucounts)
56 goto fail;
57
58 err = -ENOMEM;
4c2a7e72 59 ns = create_uts_ns();
467e9f4b 60 if (!ns)
f7af3d1c 61 goto fail_dec;
467e9f4b 62
6344c433 63 err = ns_alloc_inum(&ns->ns);
f7af3d1c
EB
64 if (err)
65 goto fail_free;
98f842e6 66
f7af3d1c 67 ns->ucounts = ucounts;
33c42940
AV
68 ns->ns.ops = &utsns_operations;
69
efc63c4f 70 down_read(&uts_sem);
467e9f4b 71 memcpy(&ns->name, &old_ns->name, sizeof(ns->name));
bcf58e72 72 ns->user_ns = get_user_ns(user_ns);
efc63c4f 73 up_read(&uts_sem);
071df104 74 return ns;
f7af3d1c
EB
75
76fail_free:
77 kfree(ns);
78fail_dec:
79 dec_uts_namespaces(ucounts);
80fail:
81 return ERR_PTR(err);
071df104
SH
82}
83
4865ecf1
SH
84/*
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
88 * versa.
89 */
bb96a6f5 90struct uts_namespace *copy_utsname(unsigned long flags,
bcf58e72 91 struct user_namespace *user_ns, struct uts_namespace *old_ns)
4865ecf1 92{
071df104 93 struct uts_namespace *new_ns;
4865ecf1 94
e3222c4e 95 BUG_ON(!old_ns);
4865ecf1
SH
96 get_uts_ns(old_ns);
97
071df104 98 if (!(flags & CLONE_NEWUTS))
e3222c4e 99 return old_ns;
071df104 100
bcf58e72 101 new_ns = clone_uts_ns(user_ns, old_ns);
071df104 102
071df104 103 put_uts_ns(old_ns);
e3222c4e 104 return new_ns;
4865ecf1
SH
105}
106
107void free_uts_ns(struct kref *kref)
108{
109 struct uts_namespace *ns;
110
111 ns = container_of(kref, struct uts_namespace, kref);
f7af3d1c 112 dec_uts_namespaces(ns->ucounts);
59607db3 113 put_user_ns(ns->user_ns);
6344c433 114 ns_free_inum(&ns->ns);
4865ecf1
SH
115 kfree(ns);
116}
34482e89 117
3c041184
AV
118static inline struct uts_namespace *to_uts_ns(struct ns_common *ns)
119{
120 return container_of(ns, struct uts_namespace, ns);
121}
122
64964528 123static struct ns_common *utsns_get(struct task_struct *task)
34482e89
EB
124{
125 struct uts_namespace *ns = NULL;
126 struct nsproxy *nsproxy;
127
728dba3a
EB
128 task_lock(task);
129 nsproxy = task->nsproxy;
34482e89
EB
130 if (nsproxy) {
131 ns = nsproxy->uts_ns;
132 get_uts_ns(ns);
133 }
728dba3a 134 task_unlock(task);
34482e89 135
3c041184 136 return ns ? &ns->ns : NULL;
34482e89
EB
137}
138
64964528 139static void utsns_put(struct ns_common *ns)
34482e89 140{
3c041184 141 put_uts_ns(to_uts_ns(ns));
34482e89
EB
142}
143
64964528 144static int utsns_install(struct nsproxy *nsproxy, struct ns_common *new)
34482e89 145{
3c041184 146 struct uts_namespace *ns = to_uts_ns(new);
142e1d1d 147
5e4a0847 148 if (!ns_capable(ns->user_ns, CAP_SYS_ADMIN) ||
c7b96acf 149 !ns_capable(current_user_ns(), CAP_SYS_ADMIN))
142e1d1d
EB
150 return -EPERM;
151
34482e89
EB
152 get_uts_ns(ns);
153 put_uts_ns(nsproxy->uts_ns);
154 nsproxy->uts_ns = ns;
155 return 0;
156}
157
bcac25a5
AV
158static struct user_namespace *utsns_owner(struct ns_common *ns)
159{
160 return to_uts_ns(ns)->user_ns;
161}
162
34482e89
EB
163const struct proc_ns_operations utsns_operations = {
164 .name = "uts",
165 .type = CLONE_NEWUTS,
166 .get = utsns_get,
167 .put = utsns_put,
168 .install = utsns_install,
bcac25a5 169 .owner = utsns_owner,
34482e89 170};