proc: Fix the namespace inode permission checks.
[linux-2.6-block.git] / ipc / namespace.c
CommitLineData
ae5e1b22
PE
1/*
2 * linux/ipc/namespace.c
3 * Copyright (C) 2006 Pavel Emelyanov <xemul@openvz.org> OpenVZ, SWsoft Inc.
4 */
5
6#include <linux/ipc.h>
7#include <linux/msg.h>
8#include <linux/ipc_namespace.h>
9#include <linux/rcupdate.h>
10#include <linux/nsproxy.h>
11#include <linux/slab.h>
7eafd7c7
SH
12#include <linux/fs.h>
13#include <linux/mount.h>
b515498f 14#include <linux/user_namespace.h>
a00eaf11 15#include <linux/proc_fs.h>
ae5e1b22
PE
16
17#include "util.h"
18
bcf58e72 19static struct ipc_namespace *create_ipc_ns(struct user_namespace *user_ns,
b0e77598 20 struct ipc_namespace *old_ns)
ae5e1b22 21{
ae5e1b22 22 struct ipc_namespace *ns;
7eafd7c7 23 int err;
ae5e1b22 24
ae5e1b22
PE
25 ns = kmalloc(sizeof(struct ipc_namespace), GFP_KERNEL);
26 if (ns == NULL)
ed2ddbf8 27 return ERR_PTR(-ENOMEM);
ae5e1b22 28
7eafd7c7
SH
29 atomic_set(&ns->count, 1);
30 err = mq_init_ns(ns);
31 if (err) {
32 kfree(ns);
33 return ERR_PTR(err);
34 }
4d89dc6a
ND
35 atomic_inc(&nr_ipc_ns);
36
ed2ddbf8
PP
37 sem_init_ns(ns);
38 msg_init_ns(ns);
39 shm_init_ns(ns);
ae5e1b22 40
e2c284d8
ND
41 /*
42 * msgmni has already been computed for the new ipc ns.
43 * Thus, do the ipcns creation notification before registering that
44 * new ipcns in the chain.
45 */
46 ipcns_notify(IPCNS_CREATED);
b6b337ad
ND
47 register_ipcns_notifier(ns);
48
bcf58e72 49 ns->user_ns = get_user_ns(user_ns);
b515498f 50
ae5e1b22 51 return ns;
ae5e1b22
PE
52}
53
b0e77598 54struct ipc_namespace *copy_ipcs(unsigned long flags,
bcf58e72 55 struct user_namespace *user_ns, struct ipc_namespace *ns)
ae5e1b22 56{
ae5e1b22 57 if (!(flags & CLONE_NEWIPC))
64424289 58 return get_ipc_ns(ns);
bcf58e72 59 return create_ipc_ns(user_ns, ns);
ae5e1b22
PE
60}
61
01b8b07a
PP
62/*
63 * free_ipcs - free all ipcs of one type
64 * @ns: the namespace to remove the ipcs from
65 * @ids: the table of ipcs to free
66 * @free: the function called to free each individual ipc
67 *
68 * Called for each kind of ipc when an ipc_namespace exits.
69 */
70void free_ipcs(struct ipc_namespace *ns, struct ipc_ids *ids,
71 void (*free)(struct ipc_namespace *, struct kern_ipc_perm *))
72{
73 struct kern_ipc_perm *perm;
74 int next_id;
75 int total, in_use;
76
77 down_write(&ids->rw_mutex);
78
79 in_use = ids->in_use;
80
81 for (total = 0, next_id = 0; total < in_use; next_id++) {
82 perm = idr_find(&ids->ipcs_idr, next_id);
83 if (perm == NULL)
84 continue;
85 ipc_lock_by_ptr(perm);
86 free(ns, perm);
87 total++;
88 }
89 up_write(&ids->rw_mutex);
90}
91
b4188def
AD
92static void free_ipc_ns(struct ipc_namespace *ns)
93{
94 /*
95 * Unregistering the hotplug notifier at the beginning guarantees
96 * that the ipc namespace won't be freed while we are inside the
97 * callback routine. Since the blocking_notifier_chain_XXX routines
98 * hold a rw lock on the notifier list, unregister_ipcns_notifier()
99 * won't take the rw lock before blocking_notifier_call_chain() has
100 * released the rd lock.
101 */
102 unregister_ipcns_notifier(ns);
103 sem_exit_ns(ns);
104 msg_exit_ns(ns);
105 shm_exit_ns(ns);
b4188def
AD
106 atomic_dec(&nr_ipc_ns);
107
108 /*
109 * Do the ipcns removal notification after decrementing nr_ipc_ns in
110 * order to have a correct value when recomputing msgmni.
111 */
112 ipcns_notify(IPCNS_REMOVED);
b515498f 113 put_user_ns(ns->user_ns);
be4d250a 114 kfree(ns);
b4188def
AD
115}
116
7eafd7c7
SH
117/*
118 * put_ipc_ns - drop a reference to an ipc namespace.
119 * @ns: the namespace to put
120 *
121 * If this is the last task in the namespace exiting, and
122 * it is dropping the refcount to 0, then it can race with
123 * a task in another ipc namespace but in a mounts namespace
124 * which has this ipcns's mqueuefs mounted, doing some action
125 * with one of the mqueuefs files. That can raise the refcount.
126 * So dropping the refcount, and raising the refcount when
127 * accessing it through the VFS, are protected with mq_lock.
128 *
129 * (Clearly, a task raising the refcount on its own ipc_ns
130 * needn't take mq_lock since it can't race with the last task
131 * in the ipcns exiting).
132 */
133void put_ipc_ns(struct ipc_namespace *ns)
ae5e1b22 134{
7eafd7c7
SH
135 if (atomic_dec_and_lock(&ns->count, &mq_lock)) {
136 mq_clear_sbinfo(ns);
137 spin_unlock(&mq_lock);
138 mq_put_mnt(ns);
139 free_ipc_ns(ns);
140 }
141}
a00eaf11
EB
142
143static void *ipcns_get(struct task_struct *task)
144{
145 struct ipc_namespace *ns = NULL;
146 struct nsproxy *nsproxy;
147
148 rcu_read_lock();
149 nsproxy = task_nsproxy(task);
150 if (nsproxy)
151 ns = get_ipc_ns(nsproxy->ipc_ns);
152 rcu_read_unlock();
153
154 return ns;
155}
156
157static void ipcns_put(void *ns)
158{
159 return put_ipc_ns(ns);
160}
161
142e1d1d 162static int ipcns_install(struct nsproxy *nsproxy, void *new)
a00eaf11 163{
142e1d1d
EB
164 struct ipc_namespace *ns = new;
165 if (!ns_capable(ns->user_ns, CAP_SYS_ADMIN))
166 return -EPERM;
167
a00eaf11
EB
168 /* Ditch state from the old ipc namespace */
169 exit_sem(current);
170 put_ipc_ns(nsproxy->ipc_ns);
171 nsproxy->ipc_ns = get_ipc_ns(ns);
172 return 0;
173}
174
175const struct proc_ns_operations ipcns_operations = {
176 .name = "ipc",
177 .type = CLONE_NEWIPC,
178 .get = ipcns_get,
179 .put = ipcns_put,
180 .install = ipcns_install,
181};