blob: 72c868277793456ee39b7c37cf518e61b60597dd [file] [log] [blame]
Pavel Emelyanovae5e1b22008-02-08 04:18:22 -08001/*
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>
Serge E. Hallyn7eafd7c2009-04-06 19:01:10 -070012#include <linux/fs.h>
13#include <linux/mount.h>
Serge E. Hallynb5154982011-03-23 16:43:23 -070014#include <linux/user_namespace.h>
Eric W. Biedermana00eaf12010-03-07 18:48:39 -080015#include <linux/proc_fs.h>
Pavel Emelyanovae5e1b22008-02-08 04:18:22 -080016
17#include "util.h"
18
Eric W. Biedermanbcf58e72012-07-26 04:02:49 -070019static struct ipc_namespace *create_ipc_ns(struct user_namespace *user_ns,
Serge E. Hallynb0e77592011-03-23 16:43:24 -070020 struct ipc_namespace *old_ns)
Pavel Emelyanovae5e1b22008-02-08 04:18:22 -080021{
Pavel Emelyanovae5e1b22008-02-08 04:18:22 -080022 struct ipc_namespace *ns;
Serge E. Hallyn7eafd7c2009-04-06 19:01:10 -070023 int err;
Pavel Emelyanovae5e1b22008-02-08 04:18:22 -080024
Pavel Emelyanovae5e1b22008-02-08 04:18:22 -080025 ns = kmalloc(sizeof(struct ipc_namespace), GFP_KERNEL);
26 if (ns == NULL)
Pierre Peiffered2ddbf2008-02-08 04:18:57 -080027 return ERR_PTR(-ENOMEM);
Pavel Emelyanovae5e1b22008-02-08 04:18:22 -080028
Serge E. Hallyn7eafd7c2009-04-06 19:01:10 -070029 atomic_set(&ns->count, 1);
30 err = mq_init_ns(ns);
31 if (err) {
32 kfree(ns);
33 return ERR_PTR(err);
34 }
Nadia Derbey4d89dc62008-04-29 01:00:40 -070035 atomic_inc(&nr_ipc_ns);
36
Pierre Peiffered2ddbf2008-02-08 04:18:57 -080037 sem_init_ns(ns);
38 msg_init_ns(ns);
39 shm_init_ns(ns);
Pavel Emelyanovae5e1b22008-02-08 04:18:22 -080040
Nadia Derbeye2c284d2008-04-29 01:00:44 -070041 /*
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);
Nadia Derbeyb6b337a2008-04-29 01:00:42 -070047 register_ipcns_notifier(ns);
48
Eric W. Biedermanbcf58e72012-07-26 04:02:49 -070049 ns->user_ns = get_user_ns(user_ns);
Serge E. Hallynb5154982011-03-23 16:43:23 -070050
Pavel Emelyanovae5e1b22008-02-08 04:18:22 -080051 return ns;
Pavel Emelyanovae5e1b22008-02-08 04:18:22 -080052}
53
Serge E. Hallynb0e77592011-03-23 16:43:24 -070054struct ipc_namespace *copy_ipcs(unsigned long flags,
Eric W. Biedermanbcf58e72012-07-26 04:02:49 -070055 struct user_namespace *user_ns, struct ipc_namespace *ns)
Pavel Emelyanovae5e1b22008-02-08 04:18:22 -080056{
Pavel Emelyanovae5e1b22008-02-08 04:18:22 -080057 if (!(flags & CLONE_NEWIPC))
Alexey Dobriyan64424282009-06-17 16:27:54 -070058 return get_ipc_ns(ns);
Eric W. Biedermanbcf58e72012-07-26 04:02:49 -070059 return create_ipc_ns(user_ns, ns);
Pavel Emelyanovae5e1b22008-02-08 04:18:22 -080060}
61
Pierre Peiffer01b8b072008-02-08 04:18:57 -080062/*
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
Alexey Dobriyanb4188de2009-06-17 16:27:56 -070092static 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);
Alexey Dobriyanb4188de2009-06-17 16:27:56 -0700106 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);
Serge E. Hallynb5154982011-03-23 16:43:23 -0700113 put_user_ns(ns->user_ns);
Xiaotian Fengbe4d2502011-03-25 01:57:01 -0700114 kfree(ns);
Alexey Dobriyanb4188de2009-06-17 16:27:56 -0700115}
116
Serge E. Hallyn7eafd7c2009-04-06 19:01:10 -0700117/*
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)
Pavel Emelyanovae5e1b22008-02-08 04:18:22 -0800134{
Serge E. Hallyn7eafd7c2009-04-06 19:01:10 -0700135 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}
Eric W. Biedermana00eaf12010-03-07 18:48:39 -0800142
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
Eric W. Biederman142e1d12012-07-26 01:13:20 -0700162static int ipcns_install(struct nsproxy *nsproxy, void *new)
Eric W. Biedermana00eaf12010-03-07 18:48:39 -0800163{
Eric W. Biederman142e1d12012-07-26 01:13:20 -0700164 struct ipc_namespace *ns = new;
165 if (!ns_capable(ns->user_ns, CAP_SYS_ADMIN))
166 return -EPERM;
167
Eric W. Biedermana00eaf12010-03-07 18:48:39 -0800168 /* 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};