| 1 | |
| 2 | |
| 3 | * Copyright (C) 2004 IBM Corporation
|
| 4 | *
|
| 5 | * Author: Serge Hallyn <[email protected]>
|
| 6 |
|
| 7 |
|
| 8 | #include <linux/export.h>
|
| 9 | #include <linux/uts.h>
|
| 10 | #include <linux/utsname.h>
|
| 11 | #include <linux/err.h>
|
| 12 | #include <linux/slab.h>
|
| 13 | #include <linux/cred.h>
|
| 14 | #include <linux/user_namespace.h>
|
| 15 | #include <linux/proc_ns.h>
|
| 16 | #include <linux/nstree.h>
|
| 17 | #include <linux/sched/task.h>
|
| 18 |
|
| 19 | static struct kmem_cache *uts_ns_cache __ro_after_init;
|
| 20 |
|
| 21 | static struct ucounts *inc_uts_namespaces(struct user_namespace *ns)
|
| 22 | {
|
| 23 | return inc_ucount(ns, current_euid(), UCOUNT_UTS_NAMESPACES);
|
| 24 | }
|
| 25 |
|
| 26 | static void dec_uts_namespaces(struct ucounts *ucounts)
|
| 27 | {
|
| 28 | dec_ucount(ucounts, UCOUNT_UTS_NAMESPACES);
|
| 29 | }
|
| 30 |
|
| 31 | |
| 32 | * Clone a new ns copying an original utsname, setting refcount to 1
|
| 33 | * @old_ns: namespace to clone
|
| 34 | * Return ERR_PTR(-ENOMEM) on error (failure to allocate), new ns otherwise
|
| 35 |
|
| 36 | static struct uts_namespace *clone_uts_ns(struct user_namespace *user_ns,
|
| 37 | struct uts_namespace *old_ns)
|
| 38 | {
|
| 39 | struct uts_namespace *ns;
|
| 40 | struct ucounts *ucounts;
|
| 41 | int err;
|
| 42 |
|
| 43 | err = -ENOSPC;
|
| 44 | ucounts = inc_uts_namespaces(user_ns);
|
| 45 | if (!ucounts)
|
| 46 | goto fail;
|
| 47 |
|
| 48 | err = -ENOMEM;
|
| 49 | ns = kmem_cache_zalloc(uts_ns_cache, GFP_KERNEL);
|
| 50 | if (!ns)
|
| 51 | goto fail_dec;
|
| 52 |
|
| 53 | err = ns_common_init(ns);
|
| 54 | if (err)
|
| 55 | goto fail_free;
|
| 56 |
|
| 57 | ns->ucounts = ucounts;
|
| 58 | down_read(&uts_sem);
|
| 59 | memcpy(&ns->name, &old_ns->name, sizeof(ns->name));
|
| 60 | ns->user_ns = get_user_ns(user_ns);
|
| 61 | up_read(&uts_sem);
|
| 62 | ns_tree_add(ns);
|
| 63 | return ns;
|
| 64 |
|
| 65 | fail_free:
|
| 66 | kmem_cache_free(uts_ns_cache, ns);
|
| 67 | fail_dec:
|
| 68 | dec_uts_namespaces(ucounts);
|
| 69 | fail:
|
| 70 | return ERR_PTR(err);
|
| 71 | }
|
| 72 |
|
| 73 | |
| 74 | * Copy task tsk's utsname namespace, or clone it if flags
|
| 75 | * specifies CLONE_NEWUTS. In latter case, changes to the
|
| 76 | * utsname of this process won't be seen by parent, and vice
|
| 77 | * versa.
|
| 78 |
|
| 79 | struct uts_namespace *copy_utsname(u64 flags,
|
| 80 | struct user_namespace *user_ns, struct uts_namespace *old_ns)
|
| 81 | {
|
| 82 | struct uts_namespace *new_ns;
|
| 83 |
|
| 84 | get_uts_ns(old_ns);
|
| 85 |
|
| 86 | if (!(flags & CLONE_NEWUTS))
|
| 87 | return old_ns;
|
| 88 |
|
| 89 | new_ns = clone_uts_ns(user_ns, old_ns);
|
| 90 |
|
| 91 | put_uts_ns(old_ns);
|
| 92 | return new_ns;
|
| 93 | }
|
| 94 |
|
| 95 | void free_uts_ns(struct uts_namespace *ns)
|
| 96 | {
|
| 97 | ns_tree_remove(ns);
|
| 98 | dec_uts_namespaces(ns->ucounts);
|
| 99 | put_user_ns(ns->user_ns);
|
| 100 | ns_common_free(ns);
|
| 101 |
|
| 102 | kfree_rcu(ns, ns.ns_rcu);
|
| 103 | }
|
| 104 |
|
| 105 | static struct ns_common *utsns_get(struct task_struct *task)
|
| 106 | {
|
| 107 | struct uts_namespace *ns = NULL;
|
| 108 | struct nsproxy *nsproxy;
|
| 109 |
|
| 110 | task_lock(task);
|
| 111 | nsproxy = task->nsproxy;
|
| 112 | if (nsproxy) {
|
| 113 | ns = nsproxy->uts_ns;
|
| 114 | get_uts_ns(ns);
|
| 115 | }
|
| 116 | task_unlock(task);
|
| 117 |
|
| 118 | return ns ? &ns->ns : NULL;
|
| 119 | }
|
| 120 |
|
| 121 | static void utsns_put(struct ns_common *ns)
|
| 122 | {
|
| 123 | put_uts_ns(to_uts_ns(ns));
|
| 124 | }
|
| 125 |
|
| 126 | static int utsns_install(struct nsset *nsset, struct ns_common *new)
|
| 127 | {
|
| 128 | struct nsproxy *nsproxy = nsset->nsproxy;
|
| 129 | struct uts_namespace *ns = to_uts_ns(new);
|
| 130 |
|
| 131 | if (!ns_capable(ns->user_ns, CAP_SYS_ADMIN) ||
|
| 132 | !ns_capable(nsset->cred->user_ns, CAP_SYS_ADMIN))
|
| 133 | return -EPERM;
|
| 134 |
|
| 135 | get_uts_ns(ns);
|
| 136 | put_uts_ns(nsproxy->uts_ns);
|
| 137 | nsproxy->uts_ns = ns;
|
| 138 | return 0;
|
| 139 | }
|
| 140 |
|
| 141 | static struct user_namespace *utsns_owner(struct ns_common *ns)
|
| 142 | {
|
| 143 | return to_uts_ns(ns)->user_ns;
|
| 144 | }
|
| 145 |
|
| 146 | const struct proc_ns_operations utsns_operations = {
|
| 147 | .name = "uts",
|
| 148 | .get = utsns_get,
|
| 149 | .put = utsns_put,
|
| 150 | .install = utsns_install,
|
| 151 | .owner = utsns_owner,
|
| 152 | };
|
| 153 |
|
| 154 | void __init uts_ns_init(void)
|
| 155 | {
|
| 156 | uts_ns_cache = kmem_cache_create_usercopy(
|
| 157 | "uts_namespace", sizeof(struct uts_namespace), 0,
|
| 158 | SLAB_PANIC|SLAB_ACCOUNT,
|
| 159 | offsetof(struct uts_namespace, name),
|
| 160 | sizeof_field(struct uts_namespace, name),
|
| 161 | NULL);
|
| 162 | ns_tree_add(&init_uts_ns);
|
| 163 | }
|