0001
0002
0003
0004
0005
0006
0007
0008 #include <linux/export.h>
0009 #include <linux/uts.h>
0010 #include <linux/utsname.h>
0011 #include <linux/err.h>
0012 #include <linux/slab.h>
0013 #include <linux/cred.h>
0014 #include <linux/user_namespace.h>
0015 #include <linux/proc_ns.h>
0016 #include <linux/sched/task.h>
0017
0018 static struct kmem_cache *uts_ns_cache __ro_after_init;
0019
0020 static struct ucounts *inc_uts_namespaces(struct user_namespace *ns)
0021 {
0022 return inc_ucount(ns, current_euid(), UCOUNT_UTS_NAMESPACES);
0023 }
0024
0025 static void dec_uts_namespaces(struct ucounts *ucounts)
0026 {
0027 dec_ucount(ucounts, UCOUNT_UTS_NAMESPACES);
0028 }
0029
0030 static struct uts_namespace *create_uts_ns(void)
0031 {
0032 struct uts_namespace *uts_ns;
0033
0034 uts_ns = kmem_cache_alloc(uts_ns_cache, GFP_KERNEL);
0035 if (uts_ns)
0036 refcount_set(&uts_ns->ns.count, 1);
0037 return uts_ns;
0038 }
0039
0040
0041
0042
0043
0044
0045 static struct uts_namespace *clone_uts_ns(struct user_namespace *user_ns,
0046 struct uts_namespace *old_ns)
0047 {
0048 struct uts_namespace *ns;
0049 struct ucounts *ucounts;
0050 int err;
0051
0052 err = -ENOSPC;
0053 ucounts = inc_uts_namespaces(user_ns);
0054 if (!ucounts)
0055 goto fail;
0056
0057 err = -ENOMEM;
0058 ns = create_uts_ns();
0059 if (!ns)
0060 goto fail_dec;
0061
0062 err = ns_alloc_inum(&ns->ns);
0063 if (err)
0064 goto fail_free;
0065
0066 ns->ucounts = ucounts;
0067 ns->ns.ops = &utsns_operations;
0068
0069 down_read(&uts_sem);
0070 memcpy(&ns->name, &old_ns->name, sizeof(ns->name));
0071 ns->user_ns = get_user_ns(user_ns);
0072 up_read(&uts_sem);
0073 return ns;
0074
0075 fail_free:
0076 kmem_cache_free(uts_ns_cache, ns);
0077 fail_dec:
0078 dec_uts_namespaces(ucounts);
0079 fail:
0080 return ERR_PTR(err);
0081 }
0082
0083
0084
0085
0086
0087
0088
0089 struct uts_namespace *copy_utsname(unsigned long flags,
0090 struct user_namespace *user_ns, struct uts_namespace *old_ns)
0091 {
0092 struct uts_namespace *new_ns;
0093
0094 BUG_ON(!old_ns);
0095 get_uts_ns(old_ns);
0096
0097 if (!(flags & CLONE_NEWUTS))
0098 return old_ns;
0099
0100 new_ns = clone_uts_ns(user_ns, old_ns);
0101
0102 put_uts_ns(old_ns);
0103 return new_ns;
0104 }
0105
0106 void free_uts_ns(struct uts_namespace *ns)
0107 {
0108 dec_uts_namespaces(ns->ucounts);
0109 put_user_ns(ns->user_ns);
0110 ns_free_inum(&ns->ns);
0111 kmem_cache_free(uts_ns_cache, ns);
0112 }
0113
0114 static inline struct uts_namespace *to_uts_ns(struct ns_common *ns)
0115 {
0116 return container_of(ns, struct uts_namespace, ns);
0117 }
0118
0119 static struct ns_common *utsns_get(struct task_struct *task)
0120 {
0121 struct uts_namespace *ns = NULL;
0122 struct nsproxy *nsproxy;
0123
0124 task_lock(task);
0125 nsproxy = task->nsproxy;
0126 if (nsproxy) {
0127 ns = nsproxy->uts_ns;
0128 get_uts_ns(ns);
0129 }
0130 task_unlock(task);
0131
0132 return ns ? &ns->ns : NULL;
0133 }
0134
0135 static void utsns_put(struct ns_common *ns)
0136 {
0137 put_uts_ns(to_uts_ns(ns));
0138 }
0139
0140 static int utsns_install(struct nsset *nsset, struct ns_common *new)
0141 {
0142 struct nsproxy *nsproxy = nsset->nsproxy;
0143 struct uts_namespace *ns = to_uts_ns(new);
0144
0145 if (!ns_capable(ns->user_ns, CAP_SYS_ADMIN) ||
0146 !ns_capable(nsset->cred->user_ns, CAP_SYS_ADMIN))
0147 return -EPERM;
0148
0149 get_uts_ns(ns);
0150 put_uts_ns(nsproxy->uts_ns);
0151 nsproxy->uts_ns = ns;
0152 return 0;
0153 }
0154
0155 static struct user_namespace *utsns_owner(struct ns_common *ns)
0156 {
0157 return to_uts_ns(ns)->user_ns;
0158 }
0159
0160 const struct proc_ns_operations utsns_operations = {
0161 .name = "uts",
0162 .type = CLONE_NEWUTS,
0163 .get = utsns_get,
0164 .put = utsns_put,
0165 .install = utsns_install,
0166 .owner = utsns_owner,
0167 };
0168
0169 void __init uts_ns_init(void)
0170 {
0171 uts_ns_cache = kmem_cache_create_usercopy(
0172 "uts_namespace", sizeof(struct uts_namespace), 0,
0173 SLAB_PANIC|SLAB_ACCOUNT,
0174 offsetof(struct uts_namespace, name),
0175 sizeof_field(struct uts_namespace, name),
0176 NULL);
0177 }