blob: 06585ad296ffc27764d7cca0f9a84f2a89b2c8d9 [file] [log] [blame]
Serge E. Hallyn4865ecf2006-10-02 02:18:14 -07001/*
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
Paul Gortmaker9984de12011-05-23 14:51:41 -040012#include <linux/export.h>
Serge E. Hallyn4865ecf2006-10-02 02:18:14 -070013#include <linux/uts.h>
14#include <linux/utsname.h>
Cedric Le Goater467e9f42007-07-15 23:41:06 -070015#include <linux/err.h>
Robert P. J. Day1aeb2722008-04-29 00:59:25 -070016#include <linux/slab.h>
Ingo Molnar5b825c32017-02-02 17:54:15 +010017#include <linux/cred.h>
Serge E. Hallyn59607db2011-03-23 16:43:16 -070018#include <linux/user_namespace.h>
David Howells0bb80f22013-04-12 01:50:06 +010019#include <linux/proc_ns.h>
Serge E. Hallyn4865ecf2006-10-02 02:18:14 -070020
Eric W. Biedermanf7af3d12016-08-08 14:11:25 -050021static 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
Alexey Dobriyan4c2a7e72009-06-17 16:27:54 -070031static 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
Serge E. Hallyn4865ecf2006-10-02 02:18:14 -070041/*
Serge E. Hallyn071df102006-10-02 02:18:17 -070042 * Clone a new ns copying an original utsname, setting refcount to 1
43 * @old_ns: namespace to clone
Yuanhan Liubf531532013-02-27 17:05:30 -080044 * Return ERR_PTR(-ENOMEM) on error (failure to kmalloc), new ns otherwise
Serge E. Hallyn071df102006-10-02 02:18:17 -070045 */
Eric W. Biedermanbcf58e72012-07-26 04:02:49 -070046static struct uts_namespace *clone_uts_ns(struct user_namespace *user_ns,
Serge E. Hallynbb96a6f52011-03-23 16:43:18 -070047 struct uts_namespace *old_ns)
Serge E. Hallyn071df102006-10-02 02:18:17 -070048{
49 struct uts_namespace *ns;
Eric W. Biedermanf7af3d12016-08-08 14:11:25 -050050 struct ucounts *ucounts;
Eric W. Biederman98f842e2011-06-15 10:21:48 -070051 int err;
Serge E. Hallyn071df102006-10-02 02:18:17 -070052
Eric W. Biedermandf75e772016-09-22 13:08:36 -050053 err = -ENOSPC;
Eric W. Biedermanf7af3d12016-08-08 14:11:25 -050054 ucounts = inc_uts_namespaces(user_ns);
55 if (!ucounts)
56 goto fail;
57
58 err = -ENOMEM;
Alexey Dobriyan4c2a7e72009-06-17 16:27:54 -070059 ns = create_uts_ns();
Cedric Le Goater467e9f42007-07-15 23:41:06 -070060 if (!ns)
Eric W. Biedermanf7af3d12016-08-08 14:11:25 -050061 goto fail_dec;
Cedric Le Goater467e9f42007-07-15 23:41:06 -070062
Al Viro6344c432014-11-01 00:45:45 -040063 err = ns_alloc_inum(&ns->ns);
Eric W. Biedermanf7af3d12016-08-08 14:11:25 -050064 if (err)
65 goto fail_free;
Eric W. Biederman98f842e2011-06-15 10:21:48 -070066
Eric W. Biedermanf7af3d12016-08-08 14:11:25 -050067 ns->ucounts = ucounts;
Al Viro33c42942014-11-01 02:32:53 -040068 ns->ns.ops = &utsns_operations;
69
Alexey Dobriyanefc63c42007-09-18 22:46:27 -070070 down_read(&uts_sem);
Cedric Le Goater467e9f42007-07-15 23:41:06 -070071 memcpy(&ns->name, &old_ns->name, sizeof(ns->name));
Eric W. Biedermanbcf58e72012-07-26 04:02:49 -070072 ns->user_ns = get_user_ns(user_ns);
Alexey Dobriyanefc63c42007-09-18 22:46:27 -070073 up_read(&uts_sem);
Serge E. Hallyn071df102006-10-02 02:18:17 -070074 return ns;
Eric W. Biedermanf7af3d12016-08-08 14:11:25 -050075
76fail_free:
77 kfree(ns);
78fail_dec:
79 dec_uts_namespaces(ucounts);
80fail:
81 return ERR_PTR(err);
Serge E. Hallyn071df102006-10-02 02:18:17 -070082}
83
84/*
Serge E. Hallyn4865ecf2006-10-02 02:18:14 -070085 * 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 */
Serge E. Hallynbb96a6f52011-03-23 16:43:18 -070090struct uts_namespace *copy_utsname(unsigned long flags,
Eric W. Biedermanbcf58e72012-07-26 04:02:49 -070091 struct user_namespace *user_ns, struct uts_namespace *old_ns)
Serge E. Hallyn4865ecf2006-10-02 02:18:14 -070092{
Serge E. Hallyn071df102006-10-02 02:18:17 -070093 struct uts_namespace *new_ns;
Serge E. Hallyn4865ecf2006-10-02 02:18:14 -070094
Badari Pulavartye3222c42007-05-08 00:25:21 -070095 BUG_ON(!old_ns);
Serge E. Hallyn4865ecf2006-10-02 02:18:14 -070096 get_uts_ns(old_ns);
97
Serge E. Hallyn071df102006-10-02 02:18:17 -070098 if (!(flags & CLONE_NEWUTS))
Badari Pulavartye3222c42007-05-08 00:25:21 -070099 return old_ns;
Serge E. Hallyn071df102006-10-02 02:18:17 -0700100
Eric W. Biedermanbcf58e72012-07-26 04:02:49 -0700101 new_ns = clone_uts_ns(user_ns, old_ns);
Serge E. Hallyn071df102006-10-02 02:18:17 -0700102
Serge E. Hallyn071df102006-10-02 02:18:17 -0700103 put_uts_ns(old_ns);
Badari Pulavartye3222c42007-05-08 00:25:21 -0700104 return new_ns;
Serge E. Hallyn4865ecf2006-10-02 02:18:14 -0700105}
106
107void free_uts_ns(struct kref *kref)
108{
109 struct uts_namespace *ns;
110
111 ns = container_of(kref, struct uts_namespace, kref);
Eric W. Biedermanf7af3d12016-08-08 14:11:25 -0500112 dec_uts_namespaces(ns->ucounts);
Serge E. Hallyn59607db2011-03-23 16:43:16 -0700113 put_user_ns(ns->user_ns);
Al Viro6344c432014-11-01 00:45:45 -0400114 ns_free_inum(&ns->ns);
Serge E. Hallyn4865ecf2006-10-02 02:18:14 -0700115 kfree(ns);
116}
Eric W. Biederman34482e82010-03-07 18:43:27 -0800117
Al Viro3c041182014-11-01 00:25:30 -0400118static inline struct uts_namespace *to_uts_ns(struct ns_common *ns)
119{
120 return container_of(ns, struct uts_namespace, ns);
121}
122
Al Viro64964522014-11-01 00:37:32 -0400123static struct ns_common *utsns_get(struct task_struct *task)
Eric W. Biederman34482e82010-03-07 18:43:27 -0800124{
125 struct uts_namespace *ns = NULL;
126 struct nsproxy *nsproxy;
127
Eric W. Biederman728dba32014-02-03 19:13:49 -0800128 task_lock(task);
129 nsproxy = task->nsproxy;
Eric W. Biederman34482e82010-03-07 18:43:27 -0800130 if (nsproxy) {
131 ns = nsproxy->uts_ns;
132 get_uts_ns(ns);
133 }
Eric W. Biederman728dba32014-02-03 19:13:49 -0800134 task_unlock(task);
Eric W. Biederman34482e82010-03-07 18:43:27 -0800135
Al Viro3c041182014-11-01 00:25:30 -0400136 return ns ? &ns->ns : NULL;
Eric W. Biederman34482e82010-03-07 18:43:27 -0800137}
138
Al Viro64964522014-11-01 00:37:32 -0400139static void utsns_put(struct ns_common *ns)
Eric W. Biederman34482e82010-03-07 18:43:27 -0800140{
Al Viro3c041182014-11-01 00:25:30 -0400141 put_uts_ns(to_uts_ns(ns));
Eric W. Biederman34482e82010-03-07 18:43:27 -0800142}
143
Al Viro64964522014-11-01 00:37:32 -0400144static int utsns_install(struct nsproxy *nsproxy, struct ns_common *new)
Eric W. Biederman34482e82010-03-07 18:43:27 -0800145{
Al Viro3c041182014-11-01 00:25:30 -0400146 struct uts_namespace *ns = to_uts_ns(new);
Eric W. Biederman142e1d12012-07-26 01:13:20 -0700147
Eric W. Biederman5e4a0842012-12-14 07:55:36 -0800148 if (!ns_capable(ns->user_ns, CAP_SYS_ADMIN) ||
Eric W. Biedermanc7b96ac2013-03-20 12:49:49 -0700149 !ns_capable(current_user_ns(), CAP_SYS_ADMIN))
Eric W. Biederman142e1d12012-07-26 01:13:20 -0700150 return -EPERM;
151
Eric W. Biederman34482e82010-03-07 18:43:27 -0800152 get_uts_ns(ns);
153 put_uts_ns(nsproxy->uts_ns);
154 nsproxy->uts_ns = ns;
155 return 0;
156}
157
Andrey Vaginbcac25a2016-09-06 00:47:13 -0700158static struct user_namespace *utsns_owner(struct ns_common *ns)
159{
160 return to_uts_ns(ns)->user_ns;
161}
162
Eric W. Biederman34482e82010-03-07 18:43:27 -0800163const 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,
Andrey Vaginbcac25a2016-09-06 00:47:13 -0700169 .owner = utsns_owner,
Eric W. Biederman34482e82010-03-07 18:43:27 -0800170};