blob: 913fe4336d2b75a0d7cd6697d3bcdf5436b9c5ed [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>
Ingo Molnarf719ff9b2017-02-06 10:57:33 +010020#include <linux/sched/task.h>
Serge E. Hallyn4865ecf2006-10-02 02:18:14 -070021
Eric W. Biedermanf7af3d12016-08-08 14:11:25 -050022static struct ucounts *inc_uts_namespaces(struct user_namespace *ns)
23{
24 return inc_ucount(ns, current_euid(), UCOUNT_UTS_NAMESPACES);
25}
26
27static void dec_uts_namespaces(struct ucounts *ucounts)
28{
29 dec_ucount(ucounts, UCOUNT_UTS_NAMESPACES);
30}
31
Alexey Dobriyan4c2a7e72009-06-17 16:27:54 -070032static struct uts_namespace *create_uts_ns(void)
33{
34 struct uts_namespace *uts_ns;
35
36 uts_ns = kmalloc(sizeof(struct uts_namespace), GFP_KERNEL);
37 if (uts_ns)
38 kref_init(&uts_ns->kref);
39 return uts_ns;
40}
41
Serge E. Hallyn4865ecf2006-10-02 02:18:14 -070042/*
Serge E. Hallyn071df102006-10-02 02:18:17 -070043 * Clone a new ns copying an original utsname, setting refcount to 1
44 * @old_ns: namespace to clone
Yuanhan Liubf531532013-02-27 17:05:30 -080045 * Return ERR_PTR(-ENOMEM) on error (failure to kmalloc), new ns otherwise
Serge E. Hallyn071df102006-10-02 02:18:17 -070046 */
Eric W. Biedermanbcf58e72012-07-26 04:02:49 -070047static struct uts_namespace *clone_uts_ns(struct user_namespace *user_ns,
Serge E. Hallynbb96a6f2011-03-23 16:43:18 -070048 struct uts_namespace *old_ns)
Serge E. Hallyn071df102006-10-02 02:18:17 -070049{
50 struct uts_namespace *ns;
Eric W. Biedermanf7af3d12016-08-08 14:11:25 -050051 struct ucounts *ucounts;
Eric W. Biederman98f842e2011-06-15 10:21:48 -070052 int err;
Serge E. Hallyn071df102006-10-02 02:18:17 -070053
Eric W. Biedermandf75e772016-09-22 13:08:36 -050054 err = -ENOSPC;
Eric W. Biedermanf7af3d12016-08-08 14:11:25 -050055 ucounts = inc_uts_namespaces(user_ns);
56 if (!ucounts)
57 goto fail;
58
59 err = -ENOMEM;
Alexey Dobriyan4c2a7e72009-06-17 16:27:54 -070060 ns = create_uts_ns();
Cedric Le Goater467e9f42007-07-15 23:41:06 -070061 if (!ns)
Eric W. Biedermanf7af3d12016-08-08 14:11:25 -050062 goto fail_dec;
Cedric Le Goater467e9f42007-07-15 23:41:06 -070063
Al Viro6344c432014-11-01 00:45:45 -040064 err = ns_alloc_inum(&ns->ns);
Eric W. Biedermanf7af3d12016-08-08 14:11:25 -050065 if (err)
66 goto fail_free;
Eric W. Biederman98f842e2011-06-15 10:21:48 -070067
Eric W. Biedermanf7af3d12016-08-08 14:11:25 -050068 ns->ucounts = ucounts;
Al Viro33c42942014-11-01 02:32:53 -040069 ns->ns.ops = &utsns_operations;
70
Alexey Dobriyanefc63c42007-09-18 22:46:27 -070071 down_read(&uts_sem);
Cedric Le Goater467e9f42007-07-15 23:41:06 -070072 memcpy(&ns->name, &old_ns->name, sizeof(ns->name));
Eric W. Biedermanbcf58e72012-07-26 04:02:49 -070073 ns->user_ns = get_user_ns(user_ns);
Alexey Dobriyanefc63c42007-09-18 22:46:27 -070074 up_read(&uts_sem);
Serge E. Hallyn071df102006-10-02 02:18:17 -070075 return ns;
Eric W. Biedermanf7af3d12016-08-08 14:11:25 -050076
77fail_free:
78 kfree(ns);
79fail_dec:
80 dec_uts_namespaces(ucounts);
81fail:
82 return ERR_PTR(err);
Serge E. Hallyn071df102006-10-02 02:18:17 -070083}
84
85/*
Serge E. Hallyn4865ecf2006-10-02 02:18:14 -070086 * Copy task tsk's utsname namespace, or clone it if flags
87 * specifies CLONE_NEWUTS. In latter case, changes to the
88 * utsname of this process won't be seen by parent, and vice
89 * versa.
90 */
Serge E. Hallynbb96a6f2011-03-23 16:43:18 -070091struct uts_namespace *copy_utsname(unsigned long flags,
Eric W. Biedermanbcf58e72012-07-26 04:02:49 -070092 struct user_namespace *user_ns, struct uts_namespace *old_ns)
Serge E. Hallyn4865ecf2006-10-02 02:18:14 -070093{
Serge E. Hallyn071df102006-10-02 02:18:17 -070094 struct uts_namespace *new_ns;
Serge E. Hallyn4865ecf2006-10-02 02:18:14 -070095
Badari Pulavartye3222c42007-05-08 00:25:21 -070096 BUG_ON(!old_ns);
Serge E. Hallyn4865ecf2006-10-02 02:18:14 -070097 get_uts_ns(old_ns);
98
Serge E. Hallyn071df102006-10-02 02:18:17 -070099 if (!(flags & CLONE_NEWUTS))
Badari Pulavartye3222c42007-05-08 00:25:21 -0700100 return old_ns;
Serge E. Hallyn071df102006-10-02 02:18:17 -0700101
Eric W. Biedermanbcf58e72012-07-26 04:02:49 -0700102 new_ns = clone_uts_ns(user_ns, old_ns);
Serge E. Hallyn071df102006-10-02 02:18:17 -0700103
Serge E. Hallyn071df102006-10-02 02:18:17 -0700104 put_uts_ns(old_ns);
Badari Pulavartye3222c42007-05-08 00:25:21 -0700105 return new_ns;
Serge E. Hallyn4865ecf2006-10-02 02:18:14 -0700106}
107
108void free_uts_ns(struct kref *kref)
109{
110 struct uts_namespace *ns;
111
112 ns = container_of(kref, struct uts_namespace, kref);
Eric W. Biedermanf7af3d12016-08-08 14:11:25 -0500113 dec_uts_namespaces(ns->ucounts);
Serge E. Hallyn59607db2011-03-23 16:43:16 -0700114 put_user_ns(ns->user_ns);
Al Viro6344c432014-11-01 00:45:45 -0400115 ns_free_inum(&ns->ns);
Serge E. Hallyn4865ecf2006-10-02 02:18:14 -0700116 kfree(ns);
117}
Eric W. Biederman34482e82010-03-07 18:43:27 -0800118
Al Viro3c041182014-11-01 00:25:30 -0400119static inline struct uts_namespace *to_uts_ns(struct ns_common *ns)
120{
121 return container_of(ns, struct uts_namespace, ns);
122}
123
Al Viro64964522014-11-01 00:37:32 -0400124static struct ns_common *utsns_get(struct task_struct *task)
Eric W. Biederman34482e82010-03-07 18:43:27 -0800125{
126 struct uts_namespace *ns = NULL;
127 struct nsproxy *nsproxy;
128
Eric W. Biederman728dba32014-02-03 19:13:49 -0800129 task_lock(task);
130 nsproxy = task->nsproxy;
Eric W. Biederman34482e82010-03-07 18:43:27 -0800131 if (nsproxy) {
132 ns = nsproxy->uts_ns;
133 get_uts_ns(ns);
134 }
Eric W. Biederman728dba32014-02-03 19:13:49 -0800135 task_unlock(task);
Eric W. Biederman34482e82010-03-07 18:43:27 -0800136
Al Viro3c041182014-11-01 00:25:30 -0400137 return ns ? &ns->ns : NULL;
Eric W. Biederman34482e82010-03-07 18:43:27 -0800138}
139
Al Viro64964522014-11-01 00:37:32 -0400140static void utsns_put(struct ns_common *ns)
Eric W. Biederman34482e82010-03-07 18:43:27 -0800141{
Al Viro3c041182014-11-01 00:25:30 -0400142 put_uts_ns(to_uts_ns(ns));
Eric W. Biederman34482e82010-03-07 18:43:27 -0800143}
144
Al Viro64964522014-11-01 00:37:32 -0400145static int utsns_install(struct nsproxy *nsproxy, struct ns_common *new)
Eric W. Biederman34482e82010-03-07 18:43:27 -0800146{
Al Viro3c041182014-11-01 00:25:30 -0400147 struct uts_namespace *ns = to_uts_ns(new);
Eric W. Biederman142e1d12012-07-26 01:13:20 -0700148
Eric W. Biederman5e4a0842012-12-14 07:55:36 -0800149 if (!ns_capable(ns->user_ns, CAP_SYS_ADMIN) ||
Eric W. Biedermanc7b96ac2013-03-20 12:49:49 -0700150 !ns_capable(current_user_ns(), CAP_SYS_ADMIN))
Eric W. Biederman142e1d12012-07-26 01:13:20 -0700151 return -EPERM;
152
Eric W. Biederman34482e82010-03-07 18:43:27 -0800153 get_uts_ns(ns);
154 put_uts_ns(nsproxy->uts_ns);
155 nsproxy->uts_ns = ns;
156 return 0;
157}
158
Andrey Vaginbcac25a2016-09-06 00:47:13 -0700159static struct user_namespace *utsns_owner(struct ns_common *ns)
160{
161 return to_uts_ns(ns)->user_ns;
162}
163
Eric W. Biederman34482e82010-03-07 18:43:27 -0800164const struct proc_ns_operations utsns_operations = {
165 .name = "uts",
166 .type = CLONE_NEWUTS,
167 .get = utsns_get,
168 .put = utsns_put,
169 .install = utsns_install,
Andrey Vaginbcac25a2016-09-06 00:47:13 -0700170 .owner = utsns_owner,
Eric W. Biederman34482e82010-03-07 18:43:27 -0800171};