blob: dcd6be1996fe7ffcdb2dd0d15d516ffaea52df4b [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
Alexey Dobriyan3ea056c2018-04-10 16:32:36 -070022static struct kmem_cache *uts_ns_cache __ro_after_init;
23
Eric W. Biedermanf7af3d12016-08-08 14:11:25 -050024static struct ucounts *inc_uts_namespaces(struct user_namespace *ns)
25{
26 return inc_ucount(ns, current_euid(), UCOUNT_UTS_NAMESPACES);
27}
28
29static void dec_uts_namespaces(struct ucounts *ucounts)
30{
31 dec_ucount(ucounts, UCOUNT_UTS_NAMESPACES);
32}
33
Alexey Dobriyan4c2a7e72009-06-17 16:27:54 -070034static struct uts_namespace *create_uts_ns(void)
35{
36 struct uts_namespace *uts_ns;
37
Alexey Dobriyan3ea056c2018-04-10 16:32:36 -070038 uts_ns = kmem_cache_alloc(uts_ns_cache, GFP_KERNEL);
Alexey Dobriyan4c2a7e72009-06-17 16:27:54 -070039 if (uts_ns)
40 kref_init(&uts_ns->kref);
41 return uts_ns;
42}
43
Serge E. Hallyn4865ecf2006-10-02 02:18:14 -070044/*
Serge E. Hallyn071df102006-10-02 02:18:17 -070045 * Clone a new ns copying an original utsname, setting refcount to 1
46 * @old_ns: namespace to clone
Alexey Dobriyan3ea056c2018-04-10 16:32:36 -070047 * Return ERR_PTR(-ENOMEM) on error (failure to allocate), new ns otherwise
Serge E. Hallyn071df102006-10-02 02:18:17 -070048 */
Eric W. Biedermanbcf58e72012-07-26 04:02:49 -070049static struct uts_namespace *clone_uts_ns(struct user_namespace *user_ns,
Serge E. Hallynbb96a6f2011-03-23 16:43:18 -070050 struct uts_namespace *old_ns)
Serge E. Hallyn071df102006-10-02 02:18:17 -070051{
52 struct uts_namespace *ns;
Eric W. Biedermanf7af3d12016-08-08 14:11:25 -050053 struct ucounts *ucounts;
Eric W. Biederman98f842e2011-06-15 10:21:48 -070054 int err;
Serge E. Hallyn071df102006-10-02 02:18:17 -070055
Eric W. Biedermandf75e772016-09-22 13:08:36 -050056 err = -ENOSPC;
Eric W. Biedermanf7af3d12016-08-08 14:11:25 -050057 ucounts = inc_uts_namespaces(user_ns);
58 if (!ucounts)
59 goto fail;
60
61 err = -ENOMEM;
Alexey Dobriyan4c2a7e72009-06-17 16:27:54 -070062 ns = create_uts_ns();
Cedric Le Goater467e9f42007-07-15 23:41:06 -070063 if (!ns)
Eric W. Biedermanf7af3d12016-08-08 14:11:25 -050064 goto fail_dec;
Cedric Le Goater467e9f42007-07-15 23:41:06 -070065
Al Viro6344c432014-11-01 00:45:45 -040066 err = ns_alloc_inum(&ns->ns);
Eric W. Biedermanf7af3d12016-08-08 14:11:25 -050067 if (err)
68 goto fail_free;
Eric W. Biederman98f842e2011-06-15 10:21:48 -070069
Eric W. Biedermanf7af3d12016-08-08 14:11:25 -050070 ns->ucounts = ucounts;
Al Viro33c42942014-11-01 02:32:53 -040071 ns->ns.ops = &utsns_operations;
72
Alexey Dobriyanefc63c42007-09-18 22:46:27 -070073 down_read(&uts_sem);
Cedric Le Goater467e9f42007-07-15 23:41:06 -070074 memcpy(&ns->name, &old_ns->name, sizeof(ns->name));
Eric W. Biedermanbcf58e72012-07-26 04:02:49 -070075 ns->user_ns = get_user_ns(user_ns);
Alexey Dobriyanefc63c42007-09-18 22:46:27 -070076 up_read(&uts_sem);
Serge E. Hallyn071df102006-10-02 02:18:17 -070077 return ns;
Eric W. Biedermanf7af3d12016-08-08 14:11:25 -050078
79fail_free:
Alexey Dobriyan3ea056c2018-04-10 16:32:36 -070080 kmem_cache_free(uts_ns_cache, ns);
Eric W. Biedermanf7af3d12016-08-08 14:11:25 -050081fail_dec:
82 dec_uts_namespaces(ucounts);
83fail:
84 return ERR_PTR(err);
Serge E. Hallyn071df102006-10-02 02:18:17 -070085}
86
87/*
Serge E. Hallyn4865ecf2006-10-02 02:18:14 -070088 * Copy task tsk's utsname namespace, or clone it if flags
89 * specifies CLONE_NEWUTS. In latter case, changes to the
90 * utsname of this process won't be seen by parent, and vice
91 * versa.
92 */
Serge E. Hallynbb96a6f2011-03-23 16:43:18 -070093struct uts_namespace *copy_utsname(unsigned long flags,
Eric W. Biedermanbcf58e72012-07-26 04:02:49 -070094 struct user_namespace *user_ns, struct uts_namespace *old_ns)
Serge E. Hallyn4865ecf2006-10-02 02:18:14 -070095{
Serge E. Hallyn071df102006-10-02 02:18:17 -070096 struct uts_namespace *new_ns;
Serge E. Hallyn4865ecf2006-10-02 02:18:14 -070097
Badari Pulavartye3222c42007-05-08 00:25:21 -070098 BUG_ON(!old_ns);
Serge E. Hallyn4865ecf2006-10-02 02:18:14 -070099 get_uts_ns(old_ns);
100
Serge E. Hallyn071df102006-10-02 02:18:17 -0700101 if (!(flags & CLONE_NEWUTS))
Badari Pulavartye3222c42007-05-08 00:25:21 -0700102 return old_ns;
Serge E. Hallyn071df102006-10-02 02:18:17 -0700103
Eric W. Biedermanbcf58e72012-07-26 04:02:49 -0700104 new_ns = clone_uts_ns(user_ns, old_ns);
Serge E. Hallyn071df102006-10-02 02:18:17 -0700105
Serge E. Hallyn071df102006-10-02 02:18:17 -0700106 put_uts_ns(old_ns);
Badari Pulavartye3222c42007-05-08 00:25:21 -0700107 return new_ns;
Serge E. Hallyn4865ecf2006-10-02 02:18:14 -0700108}
109
110void free_uts_ns(struct kref *kref)
111{
112 struct uts_namespace *ns;
113
114 ns = container_of(kref, struct uts_namespace, kref);
Eric W. Biedermanf7af3d12016-08-08 14:11:25 -0500115 dec_uts_namespaces(ns->ucounts);
Serge E. Hallyn59607db2011-03-23 16:43:16 -0700116 put_user_ns(ns->user_ns);
Al Viro6344c432014-11-01 00:45:45 -0400117 ns_free_inum(&ns->ns);
Alexey Dobriyan3ea056c2018-04-10 16:32:36 -0700118 kmem_cache_free(uts_ns_cache, ns);
Serge E. Hallyn4865ecf2006-10-02 02:18:14 -0700119}
Eric W. Biederman34482e82010-03-07 18:43:27 -0800120
Al Viro3c041182014-11-01 00:25:30 -0400121static inline struct uts_namespace *to_uts_ns(struct ns_common *ns)
122{
123 return container_of(ns, struct uts_namespace, ns);
124}
125
Al Viro64964522014-11-01 00:37:32 -0400126static struct ns_common *utsns_get(struct task_struct *task)
Eric W. Biederman34482e82010-03-07 18:43:27 -0800127{
128 struct uts_namespace *ns = NULL;
129 struct nsproxy *nsproxy;
130
Eric W. Biederman728dba32014-02-03 19:13:49 -0800131 task_lock(task);
132 nsproxy = task->nsproxy;
Eric W. Biederman34482e82010-03-07 18:43:27 -0800133 if (nsproxy) {
134 ns = nsproxy->uts_ns;
135 get_uts_ns(ns);
136 }
Eric W. Biederman728dba32014-02-03 19:13:49 -0800137 task_unlock(task);
Eric W. Biederman34482e82010-03-07 18:43:27 -0800138
Al Viro3c041182014-11-01 00:25:30 -0400139 return ns ? &ns->ns : NULL;
Eric W. Biederman34482e82010-03-07 18:43:27 -0800140}
141
Al Viro64964522014-11-01 00:37:32 -0400142static void utsns_put(struct ns_common *ns)
Eric W. Biederman34482e82010-03-07 18:43:27 -0800143{
Al Viro3c041182014-11-01 00:25:30 -0400144 put_uts_ns(to_uts_ns(ns));
Eric W. Biederman34482e82010-03-07 18:43:27 -0800145}
146
Al Viro64964522014-11-01 00:37:32 -0400147static int utsns_install(struct nsproxy *nsproxy, struct ns_common *new)
Eric W. Biederman34482e82010-03-07 18:43:27 -0800148{
Al Viro3c041182014-11-01 00:25:30 -0400149 struct uts_namespace *ns = to_uts_ns(new);
Eric W. Biederman142e1d12012-07-26 01:13:20 -0700150
Eric W. Biederman5e4a0842012-12-14 07:55:36 -0800151 if (!ns_capable(ns->user_ns, CAP_SYS_ADMIN) ||
Eric W. Biedermanc7b96ac2013-03-20 12:49:49 -0700152 !ns_capable(current_user_ns(), CAP_SYS_ADMIN))
Eric W. Biederman142e1d12012-07-26 01:13:20 -0700153 return -EPERM;
154
Eric W. Biederman34482e82010-03-07 18:43:27 -0800155 get_uts_ns(ns);
156 put_uts_ns(nsproxy->uts_ns);
157 nsproxy->uts_ns = ns;
158 return 0;
159}
160
Andrey Vaginbcac25a2016-09-06 00:47:13 -0700161static struct user_namespace *utsns_owner(struct ns_common *ns)
162{
163 return to_uts_ns(ns)->user_ns;
164}
165
Eric W. Biederman34482e82010-03-07 18:43:27 -0800166const struct proc_ns_operations utsns_operations = {
167 .name = "uts",
168 .type = CLONE_NEWUTS,
169 .get = utsns_get,
170 .put = utsns_put,
171 .install = utsns_install,
Andrey Vaginbcac25a2016-09-06 00:47:13 -0700172 .owner = utsns_owner,
Eric W. Biederman34482e82010-03-07 18:43:27 -0800173};
Alexey Dobriyan3ea056c2018-04-10 16:32:36 -0700174
175void __init uts_ns_init(void)
176{
177 uts_ns_cache = kmem_cache_create_usercopy(
178 "uts_namespace", sizeof(struct uts_namespace), 0,
179 SLAB_PANIC|SLAB_ACCOUNT,
180 offsetof(struct uts_namespace, name),
181 sizeof_field(struct uts_namespace, name),
182 NULL);
183}