Serge E. Hallyn | ab51601 | 2006-10-02 02:18:06 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2006 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. |
Kirill Korotaev | 25b21cb | 2006-10-02 02:18:19 -0700 | [diff] [blame] | 10 | * |
| 11 | * Jun 2006 - namespaces support |
| 12 | * OpenVZ, SWsoft Inc. |
| 13 | * Pavel Emelianov <xemul@openvz.org> |
Serge E. Hallyn | ab51601 | 2006-10-02 02:18:06 -0700 | [diff] [blame] | 14 | */ |
| 15 | |
| 16 | #include <linux/module.h> |
| 17 | #include <linux/version.h> |
| 18 | #include <linux/nsproxy.h> |
Serge E. Hallyn | 0437eb5 | 2006-10-02 02:18:07 -0700 | [diff] [blame] | 19 | #include <linux/init_task.h> |
Kirill Korotaev | 6b3286e | 2006-12-08 02:37:56 -0800 | [diff] [blame] | 20 | #include <linux/mnt_namespace.h> |
Serge E. Hallyn | 4865ecf | 2006-10-02 02:18:14 -0700 | [diff] [blame] | 21 | #include <linux/utsname.h> |
Cedric Le Goater | 9a575a9 | 2006-12-08 02:37:59 -0800 | [diff] [blame] | 22 | #include <linux/pid_namespace.h> |
Serge E. Hallyn | 0437eb5 | 2006-10-02 02:18:07 -0700 | [diff] [blame] | 23 | |
| 24 | struct nsproxy init_nsproxy = INIT_NSPROXY(init_nsproxy); |
Serge E. Hallyn | ab51601 | 2006-10-02 02:18:06 -0700 | [diff] [blame] | 25 | |
| 26 | static inline void get_nsproxy(struct nsproxy *ns) |
| 27 | { |
| 28 | atomic_inc(&ns->count); |
| 29 | } |
| 30 | |
| 31 | void get_task_namespaces(struct task_struct *tsk) |
| 32 | { |
| 33 | struct nsproxy *ns = tsk->nsproxy; |
| 34 | if (ns) { |
| 35 | get_nsproxy(ns); |
| 36 | } |
| 37 | } |
| 38 | |
| 39 | /* |
| 40 | * creates a copy of "orig" with refcount 1. |
| 41 | * This does not grab references to the contained namespaces, |
| 42 | * so that needs to be done by dup_namespaces. |
| 43 | */ |
| 44 | static inline struct nsproxy *clone_namespaces(struct nsproxy *orig) |
| 45 | { |
| 46 | struct nsproxy *ns; |
| 47 | |
Alexey Dobriyan | e05d722 | 2006-10-19 23:29:12 -0700 | [diff] [blame] | 48 | ns = kmemdup(orig, sizeof(struct nsproxy), GFP_KERNEL); |
Eric W. Biederman | 5f8442e | 2006-12-13 00:34:04 -0800 | [diff] [blame] | 49 | if (ns) |
Serge E. Hallyn | ab51601 | 2006-10-02 02:18:06 -0700 | [diff] [blame] | 50 | atomic_set(&ns->count, 1); |
Serge E. Hallyn | ab51601 | 2006-10-02 02:18:06 -0700 | [diff] [blame] | 51 | return ns; |
| 52 | } |
| 53 | |
| 54 | /* |
| 55 | * copies the nsproxy, setting refcount to 1, and grabbing a |
| 56 | * reference to all contained namespaces. Called from |
| 57 | * sys_unshare() |
| 58 | */ |
| 59 | struct nsproxy *dup_namespaces(struct nsproxy *orig) |
| 60 | { |
| 61 | struct nsproxy *ns = clone_namespaces(orig); |
| 62 | |
Serge E. Hallyn | 1651e14 | 2006-10-02 02:18:08 -0700 | [diff] [blame] | 63 | if (ns) { |
Kirill Korotaev | 6b3286e | 2006-12-08 02:37:56 -0800 | [diff] [blame] | 64 | if (ns->mnt_ns) |
| 65 | get_mnt_ns(ns->mnt_ns); |
Serge E. Hallyn | 4865ecf | 2006-10-02 02:18:14 -0700 | [diff] [blame] | 66 | if (ns->uts_ns) |
| 67 | get_uts_ns(ns->uts_ns); |
Kirill Korotaev | 25b21cb | 2006-10-02 02:18:19 -0700 | [diff] [blame] | 68 | if (ns->ipc_ns) |
| 69 | get_ipc_ns(ns->ipc_ns); |
Cedric Le Goater | 9a575a9 | 2006-12-08 02:37:59 -0800 | [diff] [blame] | 70 | if (ns->pid_ns) |
| 71 | get_pid_ns(ns->pid_ns); |
Serge E. Hallyn | 1651e14 | 2006-10-02 02:18:08 -0700 | [diff] [blame] | 72 | } |
| 73 | |
Serge E. Hallyn | ab51601 | 2006-10-02 02:18:06 -0700 | [diff] [blame] | 74 | return ns; |
| 75 | } |
| 76 | |
| 77 | /* |
| 78 | * called from clone. This now handles copy for nsproxy and all |
| 79 | * namespaces therein. |
| 80 | */ |
| 81 | int copy_namespaces(int flags, struct task_struct *tsk) |
| 82 | { |
| 83 | struct nsproxy *old_ns = tsk->nsproxy; |
Serge E. Hallyn | 1651e14 | 2006-10-02 02:18:08 -0700 | [diff] [blame] | 84 | struct nsproxy *new_ns; |
| 85 | int err = 0; |
Serge E. Hallyn | ab51601 | 2006-10-02 02:18:06 -0700 | [diff] [blame] | 86 | |
| 87 | if (!old_ns) |
| 88 | return 0; |
| 89 | |
| 90 | get_nsproxy(old_ns); |
| 91 | |
Kirill Korotaev | 25b21cb | 2006-10-02 02:18:19 -0700 | [diff] [blame] | 92 | if (!(flags & (CLONE_NEWNS | CLONE_NEWUTS | CLONE_NEWIPC))) |
Serge E. Hallyn | 1651e14 | 2006-10-02 02:18:08 -0700 | [diff] [blame] | 93 | return 0; |
| 94 | |
| 95 | new_ns = clone_namespaces(old_ns); |
| 96 | if (!new_ns) { |
| 97 | err = -ENOMEM; |
| 98 | goto out; |
| 99 | } |
| 100 | |
| 101 | tsk->nsproxy = new_ns; |
| 102 | |
Kirill Korotaev | 6b3286e | 2006-12-08 02:37:56 -0800 | [diff] [blame] | 103 | err = copy_mnt_ns(flags, tsk); |
Kirill Korotaev | 25b21cb | 2006-10-02 02:18:19 -0700 | [diff] [blame] | 104 | if (err) |
| 105 | goto out_ns; |
Serge E. Hallyn | 1651e14 | 2006-10-02 02:18:08 -0700 | [diff] [blame] | 106 | |
Serge E. Hallyn | 4865ecf | 2006-10-02 02:18:14 -0700 | [diff] [blame] | 107 | err = copy_utsname(flags, tsk); |
Kirill Korotaev | 25b21cb | 2006-10-02 02:18:19 -0700 | [diff] [blame] | 108 | if (err) |
| 109 | goto out_uts; |
| 110 | |
| 111 | err = copy_ipcs(flags, tsk); |
| 112 | if (err) |
| 113 | goto out_ipc; |
Serge E. Hallyn | 4865ecf | 2006-10-02 02:18:14 -0700 | [diff] [blame] | 114 | |
Cedric Le Goater | 9a575a9 | 2006-12-08 02:37:59 -0800 | [diff] [blame] | 115 | err = copy_pid_ns(flags, tsk); |
| 116 | if (err) |
| 117 | goto out_pid; |
| 118 | |
Serge E. Hallyn | 1651e14 | 2006-10-02 02:18:08 -0700 | [diff] [blame] | 119 | out: |
| 120 | put_nsproxy(old_ns); |
| 121 | return err; |
Kirill Korotaev | 25b21cb | 2006-10-02 02:18:19 -0700 | [diff] [blame] | 122 | |
Cedric Le Goater | 9a575a9 | 2006-12-08 02:37:59 -0800 | [diff] [blame] | 123 | out_pid: |
| 124 | if (new_ns->ipc_ns) |
| 125 | put_ipc_ns(new_ns->ipc_ns); |
Kirill Korotaev | 25b21cb | 2006-10-02 02:18:19 -0700 | [diff] [blame] | 126 | out_ipc: |
| 127 | if (new_ns->uts_ns) |
| 128 | put_uts_ns(new_ns->uts_ns); |
| 129 | out_uts: |
Kirill Korotaev | 6b3286e | 2006-12-08 02:37:56 -0800 | [diff] [blame] | 130 | if (new_ns->mnt_ns) |
| 131 | put_mnt_ns(new_ns->mnt_ns); |
Kirill Korotaev | 25b21cb | 2006-10-02 02:18:19 -0700 | [diff] [blame] | 132 | out_ns: |
| 133 | tsk->nsproxy = old_ns; |
Pavel | 5d124e9 | 2006-10-02 02:18:24 -0700 | [diff] [blame] | 134 | kfree(new_ns); |
Kirill Korotaev | 25b21cb | 2006-10-02 02:18:19 -0700 | [diff] [blame] | 135 | goto out; |
Serge E. Hallyn | ab51601 | 2006-10-02 02:18:06 -0700 | [diff] [blame] | 136 | } |
| 137 | |
| 138 | void free_nsproxy(struct nsproxy *ns) |
| 139 | { |
Cedric Le Goater | 9a575a9 | 2006-12-08 02:37:59 -0800 | [diff] [blame] | 140 | if (ns->mnt_ns) |
| 141 | put_mnt_ns(ns->mnt_ns); |
| 142 | if (ns->uts_ns) |
| 143 | put_uts_ns(ns->uts_ns); |
| 144 | if (ns->ipc_ns) |
| 145 | put_ipc_ns(ns->ipc_ns); |
| 146 | if (ns->pid_ns) |
| 147 | put_pid_ns(ns->pid_ns); |
| 148 | kfree(ns); |
Serge E. Hallyn | ab51601 | 2006-10-02 02:18:06 -0700 | [diff] [blame] | 149 | } |