Back to home page

LXR

 
 

    


0001 /*
0002  *  Copyright (C) 2004 IBM Corporation
0003  *
0004  *  Author: Serge Hallyn <serue@us.ibm.com>
0005  *
0006  *  This program is free software; you can redistribute it and/or
0007  *  modify it under the terms of the GNU General Public License as
0008  *  published by the Free Software Foundation, version 2 of the
0009  *  License.
0010  */
0011 
0012 #include <linux/export.h>
0013 #include <linux/uts.h>
0014 #include <linux/utsname.h>
0015 #include <linux/err.h>
0016 #include <linux/slab.h>
0017 #include <linux/user_namespace.h>
0018 #include <linux/proc_ns.h>
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 = kmalloc(sizeof(struct uts_namespace), GFP_KERNEL);
0035     if (uts_ns)
0036         kref_init(&uts_ns->kref);
0037     return uts_ns;
0038 }
0039 
0040 /*
0041  * Clone a new ns copying an original utsname, setting refcount to 1
0042  * @old_ns: namespace to clone
0043  * Return ERR_PTR(-ENOMEM) on error (failure to kmalloc), new ns otherwise
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     kfree(ns);
0077 fail_dec:
0078     dec_uts_namespaces(ucounts);
0079 fail:
0080     return ERR_PTR(err);
0081 }
0082 
0083 /*
0084  * Copy task tsk's utsname namespace, or clone it if flags
0085  * specifies CLONE_NEWUTS.  In latter case, changes to the
0086  * utsname of this process won't be seen by parent, and vice
0087  * versa.
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 kref *kref)
0107 {
0108     struct uts_namespace *ns;
0109 
0110     ns = container_of(kref, struct uts_namespace, kref);
0111     dec_uts_namespaces(ns->ucounts);
0112     put_user_ns(ns->user_ns);
0113     ns_free_inum(&ns->ns);
0114     kfree(ns);
0115 }
0116 
0117 static inline struct uts_namespace *to_uts_ns(struct ns_common *ns)
0118 {
0119     return container_of(ns, struct uts_namespace, ns);
0120 }
0121 
0122 static struct ns_common *utsns_get(struct task_struct *task)
0123 {
0124     struct uts_namespace *ns = NULL;
0125     struct nsproxy *nsproxy;
0126 
0127     task_lock(task);
0128     nsproxy = task->nsproxy;
0129     if (nsproxy) {
0130         ns = nsproxy->uts_ns;
0131         get_uts_ns(ns);
0132     }
0133     task_unlock(task);
0134 
0135     return ns ? &ns->ns : NULL;
0136 }
0137 
0138 static void utsns_put(struct ns_common *ns)
0139 {
0140     put_uts_ns(to_uts_ns(ns));
0141 }
0142 
0143 static int utsns_install(struct nsproxy *nsproxy, struct ns_common *new)
0144 {
0145     struct uts_namespace *ns = to_uts_ns(new);
0146 
0147     if (!ns_capable(ns->user_ns, CAP_SYS_ADMIN) ||
0148         !ns_capable(current_user_ns(), CAP_SYS_ADMIN))
0149         return -EPERM;
0150 
0151     get_uts_ns(ns);
0152     put_uts_ns(nsproxy->uts_ns);
0153     nsproxy->uts_ns = ns;
0154     return 0;
0155 }
0156 
0157 static struct user_namespace *utsns_owner(struct ns_common *ns)
0158 {
0159     return to_uts_ns(ns)->user_ns;
0160 }
0161 
0162 const struct proc_ns_operations utsns_operations = {
0163     .name       = "uts",
0164     .type       = CLONE_NEWUTS,
0165     .get        = utsns_get,
0166     .put        = utsns_put,
0167     .install    = utsns_install,
0168     .owner      = utsns_owner,
0169 };