Serge E. Hallyn | 4865ecf | 2006-10-02 02:18:14 -0700 | [diff] [blame] | 1 | /* |
| 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 | |
| 12 | #include <linux/module.h> |
| 13 | #include <linux/uts.h> |
| 14 | #include <linux/utsname.h> |
Cedric Le Goater | 467e9f4 | 2007-07-15 23:41:06 -0700 | [diff] [blame] | 15 | #include <linux/err.h> |
Robert P. J. Day | 1aeb272 | 2008-04-29 00:59:25 -0700 | [diff] [blame] | 16 | #include <linux/slab.h> |
Serge E. Hallyn | 59607db | 2011-03-23 16:43:16 -0700 | [diff] [blame] | 17 | #include <linux/user_namespace.h> |
Serge E. Hallyn | 4865ecf | 2006-10-02 02:18:14 -0700 | [diff] [blame] | 18 | |
Alexey Dobriyan | 4c2a7e7 | 2009-06-17 16:27:54 -0700 | [diff] [blame] | 19 | static struct uts_namespace *create_uts_ns(void) |
| 20 | { |
| 21 | struct uts_namespace *uts_ns; |
| 22 | |
| 23 | uts_ns = kmalloc(sizeof(struct uts_namespace), GFP_KERNEL); |
| 24 | if (uts_ns) |
| 25 | kref_init(&uts_ns->kref); |
| 26 | return uts_ns; |
| 27 | } |
| 28 | |
Serge E. Hallyn | 4865ecf | 2006-10-02 02:18:14 -0700 | [diff] [blame] | 29 | /* |
Serge E. Hallyn | 071df10 | 2006-10-02 02:18:17 -0700 | [diff] [blame] | 30 | * Clone a new ns copying an original utsname, setting refcount to 1 |
| 31 | * @old_ns: namespace to clone |
| 32 | * Return NULL on error (failure to kmalloc), new ns otherwise |
| 33 | */ |
Serge E. Hallyn | bb96a6f | 2011-03-23 16:43:18 -0700 | [diff] [blame^] | 34 | static struct uts_namespace *clone_uts_ns(struct task_struct *tsk, |
| 35 | struct uts_namespace *old_ns) |
Serge E. Hallyn | 071df10 | 2006-10-02 02:18:17 -0700 | [diff] [blame] | 36 | { |
| 37 | struct uts_namespace *ns; |
| 38 | |
Alexey Dobriyan | 4c2a7e7 | 2009-06-17 16:27:54 -0700 | [diff] [blame] | 39 | ns = create_uts_ns(); |
Cedric Le Goater | 467e9f4 | 2007-07-15 23:41:06 -0700 | [diff] [blame] | 40 | if (!ns) |
| 41 | return ERR_PTR(-ENOMEM); |
| 42 | |
Alexey Dobriyan | efc63c4 | 2007-09-18 22:46:27 -0700 | [diff] [blame] | 43 | down_read(&uts_sem); |
Cedric Le Goater | 467e9f4 | 2007-07-15 23:41:06 -0700 | [diff] [blame] | 44 | memcpy(&ns->name, &old_ns->name, sizeof(ns->name)); |
Serge E. Hallyn | bb96a6f | 2011-03-23 16:43:18 -0700 | [diff] [blame^] | 45 | ns->user_ns = get_user_ns(task_cred_xxx(tsk, user)->user_ns); |
Alexey Dobriyan | efc63c4 | 2007-09-18 22:46:27 -0700 | [diff] [blame] | 46 | up_read(&uts_sem); |
Serge E. Hallyn | 071df10 | 2006-10-02 02:18:17 -0700 | [diff] [blame] | 47 | return ns; |
| 48 | } |
| 49 | |
| 50 | /* |
Serge E. Hallyn | 4865ecf | 2006-10-02 02:18:14 -0700 | [diff] [blame] | 51 | * Copy task tsk's utsname namespace, or clone it if flags |
| 52 | * specifies CLONE_NEWUTS. In latter case, changes to the |
| 53 | * utsname of this process won't be seen by parent, and vice |
| 54 | * versa. |
| 55 | */ |
Serge E. Hallyn | bb96a6f | 2011-03-23 16:43:18 -0700 | [diff] [blame^] | 56 | struct uts_namespace *copy_utsname(unsigned long flags, |
| 57 | struct task_struct *tsk) |
Serge E. Hallyn | 4865ecf | 2006-10-02 02:18:14 -0700 | [diff] [blame] | 58 | { |
Serge E. Hallyn | bb96a6f | 2011-03-23 16:43:18 -0700 | [diff] [blame^] | 59 | struct uts_namespace *old_ns = tsk->nsproxy->uts_ns; |
Serge E. Hallyn | 071df10 | 2006-10-02 02:18:17 -0700 | [diff] [blame] | 60 | struct uts_namespace *new_ns; |
Serge E. Hallyn | 4865ecf | 2006-10-02 02:18:14 -0700 | [diff] [blame] | 61 | |
Badari Pulavarty | e3222c4 | 2007-05-08 00:25:21 -0700 | [diff] [blame] | 62 | BUG_ON(!old_ns); |
Serge E. Hallyn | 4865ecf | 2006-10-02 02:18:14 -0700 | [diff] [blame] | 63 | get_uts_ns(old_ns); |
| 64 | |
Serge E. Hallyn | 071df10 | 2006-10-02 02:18:17 -0700 | [diff] [blame] | 65 | if (!(flags & CLONE_NEWUTS)) |
Badari Pulavarty | e3222c4 | 2007-05-08 00:25:21 -0700 | [diff] [blame] | 66 | return old_ns; |
Serge E. Hallyn | 071df10 | 2006-10-02 02:18:17 -0700 | [diff] [blame] | 67 | |
Serge E. Hallyn | bb96a6f | 2011-03-23 16:43:18 -0700 | [diff] [blame^] | 68 | new_ns = clone_uts_ns(tsk, old_ns); |
Serge E. Hallyn | 071df10 | 2006-10-02 02:18:17 -0700 | [diff] [blame] | 69 | |
Serge E. Hallyn | 071df10 | 2006-10-02 02:18:17 -0700 | [diff] [blame] | 70 | put_uts_ns(old_ns); |
Badari Pulavarty | e3222c4 | 2007-05-08 00:25:21 -0700 | [diff] [blame] | 71 | return new_ns; |
Serge E. Hallyn | 4865ecf | 2006-10-02 02:18:14 -0700 | [diff] [blame] | 72 | } |
| 73 | |
| 74 | void free_uts_ns(struct kref *kref) |
| 75 | { |
| 76 | struct uts_namespace *ns; |
| 77 | |
| 78 | ns = container_of(kref, struct uts_namespace, kref); |
Serge E. Hallyn | 59607db | 2011-03-23 16:43:16 -0700 | [diff] [blame] | 79 | put_user_ns(ns->user_ns); |
Serge E. Hallyn | 4865ecf | 2006-10-02 02:18:14 -0700 | [diff] [blame] | 80 | kfree(ns); |
| 81 | } |