blob: 2d545d7144a7b9cfe04edaeaedefd2472314000d [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * linux/ipc/util.c
3 * Copyright (C) 1992 Krishna Balasubramanian
4 *
5 * Sep 1997 - Call suser() last after "normal" permission checks so we
6 * get BSD style process accounting right.
7 * Occurs in several places in the IPC code.
8 * Chris Evans, <chris@ferret.lmh.ox.ac.uk>
9 * Nov 1999 - ipc helper functions, unified SMP locking
Christian Kujau624dffc2006-01-15 02:43:54 +010010 * Manfred Spraul <manfred@colorfullife.com>
Linus Torvalds1da177e2005-04-16 15:20:36 -070011 * Oct 2002 - One lock per IPC id. RCU ipc_free for lock-free grow_ary().
12 * Mingming Cao <cmm@us.ibm.com>
Steve Grubb073115d2006-04-02 17:07:33 -040013 * Mar 2006 - support for audit of ipc object properties
14 * Dustin Kirkland <dustin.kirkland@us.ibm.com>
Kirill Korotaev73ea4132006-10-02 02:18:20 -070015 * Jun 2006 - namespaces ssupport
16 * OpenVZ, SWsoft Inc.
17 * Pavel Emelianov <xemul@openvz.org>
Linus Torvalds1da177e2005-04-16 15:20:36 -070018 */
19
Linus Torvalds1da177e2005-04-16 15:20:36 -070020#include <linux/mm.h>
21#include <linux/shm.h>
22#include <linux/init.h>
23#include <linux/msg.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070024#include <linux/vmalloc.h>
25#include <linux/slab.h>
Randy.Dunlapc59ede72006-01-11 12:17:46 -080026#include <linux/capability.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070027#include <linux/highuid.h>
28#include <linux/security.h>
29#include <linux/rcupdate.h>
30#include <linux/workqueue.h>
Mike Waychisonae781772005-09-06 15:17:09 -070031#include <linux/seq_file.h>
32#include <linux/proc_fs.h>
Steve Grubb073115d2006-04-02 17:07:33 -040033#include <linux/audit.h>
Kirill Korotaev73ea4132006-10-02 02:18:20 -070034#include <linux/nsproxy.h>
Nadia Derbey3e148c72007-10-18 23:40:54 -070035#include <linux/rwsem.h>
Nadia Derbeyb6b337a2008-04-29 01:00:42 -070036#include <linux/memory.h>
Pavel Emelyanovae5e1b22008-02-08 04:18:22 -080037#include <linux/ipc_namespace.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070038
39#include <asm/unistd.h>
40
41#include "util.h"
42
Mike Waychisonae781772005-09-06 15:17:09 -070043struct ipc_proc_iface {
44 const char *path;
45 const char *header;
Kirill Korotaev73ea4132006-10-02 02:18:20 -070046 int ids;
Mike Waychisonae781772005-09-06 15:17:09 -070047 int (*show)(struct seq_file *, void *);
48};
49
Kirill Korotaev73ea4132006-10-02 02:18:20 -070050struct ipc_namespace init_ipc_ns = {
51 .kref = {
52 .refcount = ATOMIC_INIT(2),
53 },
54};
55
Nadia Derbey4d89dc62008-04-29 01:00:40 -070056atomic_t nr_ipc_ns = ATOMIC_INIT(1);
57
58
Nadia Derbeyb6b337a2008-04-29 01:00:42 -070059#ifdef CONFIG_MEMORY_HOTPLUG
60
61static int ipc_memory_callback(struct notifier_block *self,
62 unsigned long action, void *arg)
63{
64 switch (action) {
65 case MEM_ONLINE: /* memory successfully brought online */
66 case MEM_OFFLINE: /* or offline: it's time to recompute msgmni */
67 /*
68 * This is done by invoking the ipcns notifier chain with the
69 * IPC_MEMCHANGED event.
70 */
71 ipcns_notify(IPCNS_MEMCHANGED);
72 break;
73 case MEM_GOING_ONLINE:
74 case MEM_GOING_OFFLINE:
75 case MEM_CANCEL_ONLINE:
76 case MEM_CANCEL_OFFLINE:
77 default:
78 break;
79 }
80
81 return NOTIFY_OK;
82}
83
84#endif /* CONFIG_MEMORY_HOTPLUG */
85
Linus Torvalds1da177e2005-04-16 15:20:36 -070086/**
87 * ipc_init - initialise IPC subsystem
88 *
89 * The various system5 IPC resources (semaphores, messages and shared
Robert P. J. Day72fd4a32007-02-10 01:45:59 -080090 * memory) are initialised
Nadia Derbeyb6b337a2008-04-29 01:00:42 -070091 * A callback routine is registered into the memory hotplug notifier
92 * chain: since msgmni scales to lowmem this callback routine will be
93 * called upon successful memory add / remove to recompute msmgni.
Linus Torvalds1da177e2005-04-16 15:20:36 -070094 */
95
96static int __init ipc_init(void)
97{
98 sem_init();
99 msg_init();
100 shm_init();
Nadia Derbeyb6b337a2008-04-29 01:00:42 -0700101 hotplug_memory_notifier(ipc_memory_callback, IPC_CALLBACK_PRI);
102 register_ipcns_notifier(&init_ipc_ns);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700103 return 0;
104}
105__initcall(ipc_init);
106
107/**
108 * ipc_init_ids - initialise IPC identifiers
109 * @ids: Identifier set
Linus Torvalds1da177e2005-04-16 15:20:36 -0700110 *
Nadia Derbey7ca7e562007-10-18 23:40:48 -0700111 * Set up the sequence range to use for the ipc identifier range (limited
112 * below IPCMNI) then initialise the ids idr.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700113 */
114
Nadia Derbey7ca7e562007-10-18 23:40:48 -0700115void ipc_init_ids(struct ipc_ids *ids)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700116{
Nadia Derbey3e148c72007-10-18 23:40:54 -0700117 init_rwsem(&ids->rw_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700118
Linus Torvalds1da177e2005-04-16 15:20:36 -0700119 ids->in_use = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700120 ids->seq = 0;
121 {
122 int seq_limit = INT_MAX/SEQ_MULTIPLIER;
123 if(seq_limit > USHRT_MAX)
124 ids->seq_max = USHRT_MAX;
125 else
126 ids->seq_max = seq_limit;
127 }
128
Nadia Derbey7ca7e562007-10-18 23:40:48 -0700129 idr_init(&ids->ipcs_idr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700130}
131
Mike Waychisonae781772005-09-06 15:17:09 -0700132#ifdef CONFIG_PROC_FS
Arjan van de Ven9a321442007-02-12 00:55:35 -0800133static const struct file_operations sysvipc_proc_fops;
Mike Waychisonae781772005-09-06 15:17:09 -0700134/**
Robert P. J. Day72fd4a32007-02-10 01:45:59 -0800135 * ipc_init_proc_interface - Create a proc interface for sysipc types using a seq_file interface.
Mike Waychisonae781772005-09-06 15:17:09 -0700136 * @path: Path in procfs
137 * @header: Banner to be printed at the beginning of the file.
138 * @ids: ipc id table to iterate.
139 * @show: show routine.
140 */
141void __init ipc_init_proc_interface(const char *path, const char *header,
Kirill Korotaev73ea4132006-10-02 02:18:20 -0700142 int ids, int (*show)(struct seq_file *, void *))
Mike Waychisonae781772005-09-06 15:17:09 -0700143{
144 struct proc_dir_entry *pde;
145 struct ipc_proc_iface *iface;
146
147 iface = kmalloc(sizeof(*iface), GFP_KERNEL);
148 if (!iface)
149 return;
150 iface->path = path;
151 iface->header = header;
152 iface->ids = ids;
153 iface->show = show;
154
155 pde = create_proc_entry(path,
156 S_IRUGO, /* world readable */
157 NULL /* parent dir */);
158 if (pde) {
159 pde->data = iface;
160 pde->proc_fops = &sysvipc_proc_fops;
161 } else {
162 kfree(iface);
163 }
164}
165#endif
166
Linus Torvalds1da177e2005-04-16 15:20:36 -0700167/**
168 * ipc_findkey - find a key in an ipc identifier set
169 * @ids: Identifier set
170 * @key: The key to find
171 *
Nadia Derbey3e148c72007-10-18 23:40:54 -0700172 * Requires ipc_ids.rw_mutex locked.
Nadia Derbey7ca7e562007-10-18 23:40:48 -0700173 * Returns the LOCKED pointer to the ipc structure if found or NULL
174 * if not.
Nadia Derbeyf4566f02007-10-18 23:40:53 -0700175 * If key is found ipc points to the owning ipc structure
Linus Torvalds1da177e2005-04-16 15:20:36 -0700176 */
177
Nadia Derbey7748dbf2007-10-18 23:40:49 -0700178static struct kern_ipc_perm *ipc_findkey(struct ipc_ids *ids, key_t key)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700179{
Nadia Derbey7ca7e562007-10-18 23:40:48 -0700180 struct kern_ipc_perm *ipc;
181 int next_id;
182 int total;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700183
Nadia Derbey7ca7e562007-10-18 23:40:48 -0700184 for (total = 0, next_id = 0; total < ids->in_use; next_id++) {
185 ipc = idr_find(&ids->ipcs_idr, next_id);
186
187 if (ipc == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700188 continue;
Nadia Derbey7ca7e562007-10-18 23:40:48 -0700189
190 if (ipc->key != key) {
191 total++;
192 continue;
193 }
194
195 ipc_lock_by_ptr(ipc);
196 return ipc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700197 }
Nadia Derbey7ca7e562007-10-18 23:40:48 -0700198
199 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700200}
201
Nadia Derbey7ca7e562007-10-18 23:40:48 -0700202/**
203 * ipc_get_maxid - get the last assigned id
204 * @ids: IPC identifier set
205 *
Nadia Derbey3e148c72007-10-18 23:40:54 -0700206 * Called with ipc_ids.rw_mutex held.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700207 */
Nadia Derbey7ca7e562007-10-18 23:40:48 -0700208
209int ipc_get_maxid(struct ipc_ids *ids)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700210{
Nadia Derbey7ca7e562007-10-18 23:40:48 -0700211 struct kern_ipc_perm *ipc;
212 int max_id = -1;
213 int total, id;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700214
Nadia Derbey7ca7e562007-10-18 23:40:48 -0700215 if (ids->in_use == 0)
216 return -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700217
Nadia Derbey7ca7e562007-10-18 23:40:48 -0700218 if (ids->in_use == IPCMNI)
219 return IPCMNI - 1;
220
221 /* Look for the last assigned id */
222 total = 0;
223 for (id = 0; id < IPCMNI && total < ids->in_use; id++) {
224 ipc = idr_find(&ids->ipcs_idr, id);
225 if (ipc != NULL) {
226 max_id = id;
227 total++;
228 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700229 }
Nadia Derbey7ca7e562007-10-18 23:40:48 -0700230 return max_id;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700231}
232
233/**
234 * ipc_addid - add an IPC identifier
235 * @ids: IPC identifier set
236 * @new: new IPC permission set
Nadia Derbey7ca7e562007-10-18 23:40:48 -0700237 * @size: limit for the number of used ids
Linus Torvalds1da177e2005-04-16 15:20:36 -0700238 *
Nadia Derbeyf4566f02007-10-18 23:40:53 -0700239 * Add an entry 'new' to the IPC ids idr. The permissions object is
Linus Torvalds1da177e2005-04-16 15:20:36 -0700240 * initialised and the first free entry is set up and the id assigned
Nadia Derbeyf4566f02007-10-18 23:40:53 -0700241 * is returned. The 'new' entry is returned in a locked state on success.
Pierre Peiffer283bb7f2007-10-18 23:40:57 -0700242 * On failure the entry is not locked and a negative err-code is returned.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700243 *
Nadia Derbey3e148c72007-10-18 23:40:54 -0700244 * Called with ipc_ids.rw_mutex held as a writer.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700245 */
246
247int ipc_addid(struct ipc_ids* ids, struct kern_ipc_perm* new, int size)
248{
Nadia Derbey7ca7e562007-10-18 23:40:48 -0700249 int id, err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700250
Nadia Derbey7ca7e562007-10-18 23:40:48 -0700251 if (size > IPCMNI)
252 size = IPCMNI;
253
254 if (ids->in_use >= size)
Pierre Peiffer283bb7f2007-10-18 23:40:57 -0700255 return -ENOSPC;
Nadia Derbey7ca7e562007-10-18 23:40:48 -0700256
257 err = idr_get_new(&ids->ipcs_idr, new, &id);
258 if (err)
Pierre Peiffer283bb7f2007-10-18 23:40:57 -0700259 return err;
Nadia Derbey7ca7e562007-10-18 23:40:48 -0700260
Linus Torvalds1da177e2005-04-16 15:20:36 -0700261 ids->in_use++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700262
263 new->cuid = new->uid = current->euid;
264 new->gid = new->cgid = current->egid;
265
266 new->seq = ids->seq++;
267 if(ids->seq > ids->seq_max)
268 ids->seq = 0;
269
Pierre Peiffer48dea402008-04-29 01:00:35 -0700270 new->id = ipc_buildid(id, new->seq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700271 spin_lock_init(&new->lock);
272 new->deleted = 0;
273 rcu_read_lock();
274 spin_lock(&new->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700275 return id;
276}
277
278/**
Nadia Derbey7748dbf2007-10-18 23:40:49 -0700279 * ipcget_new - create a new ipc object
280 * @ns: namespace
Nadia Derbeyf4566f02007-10-18 23:40:53 -0700281 * @ids: IPC identifer set
Nadia Derbey7748dbf2007-10-18 23:40:49 -0700282 * @ops: the actual creation routine to call
283 * @params: its parameters
284 *
Nadia Derbeyf4566f02007-10-18 23:40:53 -0700285 * This routine is called by sys_msgget, sys_semget() and sys_shmget()
286 * when the key is IPC_PRIVATE.
Nadia Derbey7748dbf2007-10-18 23:40:49 -0700287 */
Pavel Emelyanovb2d75cd2008-02-08 04:18:54 -0800288static int ipcget_new(struct ipc_namespace *ns, struct ipc_ids *ids,
Nadia Derbey7748dbf2007-10-18 23:40:49 -0700289 struct ipc_ops *ops, struct ipc_params *params)
290{
291 int err;
Pierre Peiffer283bb7f2007-10-18 23:40:57 -0700292retry:
Nadia Derbey7748dbf2007-10-18 23:40:49 -0700293 err = idr_pre_get(&ids->ipcs_idr, GFP_KERNEL);
294
295 if (!err)
296 return -ENOMEM;
297
Nadia Derbey3e148c72007-10-18 23:40:54 -0700298 down_write(&ids->rw_mutex);
Nadia Derbey7748dbf2007-10-18 23:40:49 -0700299 err = ops->getnew(ns, params);
Nadia Derbey3e148c72007-10-18 23:40:54 -0700300 up_write(&ids->rw_mutex);
Nadia Derbey7748dbf2007-10-18 23:40:49 -0700301
Pierre Peiffer283bb7f2007-10-18 23:40:57 -0700302 if (err == -EAGAIN)
303 goto retry;
304
Nadia Derbey7748dbf2007-10-18 23:40:49 -0700305 return err;
306}
307
308/**
309 * ipc_check_perms - check security and permissions for an IPC
310 * @ipcp: ipc permission set
Nadia Derbey7748dbf2007-10-18 23:40:49 -0700311 * @ops: the actual security routine to call
312 * @params: its parameters
Nadia Derbeyf4566f02007-10-18 23:40:53 -0700313 *
314 * This routine is called by sys_msgget(), sys_semget() and sys_shmget()
315 * when the key is not IPC_PRIVATE and that key already exists in the
316 * ids IDR.
317 *
318 * On success, the IPC id is returned.
319 *
Nadia Derbey3e148c72007-10-18 23:40:54 -0700320 * It is called with ipc_ids.rw_mutex and ipcp->lock held.
Nadia Derbey7748dbf2007-10-18 23:40:49 -0700321 */
322static int ipc_check_perms(struct kern_ipc_perm *ipcp, struct ipc_ops *ops,
323 struct ipc_params *params)
324{
325 int err;
326
327 if (ipcperms(ipcp, params->flg))
328 err = -EACCES;
329 else {
330 err = ops->associate(ipcp, params->flg);
331 if (!err)
332 err = ipcp->id;
333 }
334
335 return err;
336}
337
338/**
339 * ipcget_public - get an ipc object or create a new one
340 * @ns: namespace
Nadia Derbeyf4566f02007-10-18 23:40:53 -0700341 * @ids: IPC identifer set
Nadia Derbey7748dbf2007-10-18 23:40:49 -0700342 * @ops: the actual creation routine to call
343 * @params: its parameters
344 *
Nadia Derbeyf4566f02007-10-18 23:40:53 -0700345 * This routine is called by sys_msgget, sys_semget() and sys_shmget()
346 * when the key is not IPC_PRIVATE.
347 * It adds a new entry if the key is not found and does some permission
348 * / security checkings if the key is found.
349 *
350 * On success, the ipc id is returned.
Nadia Derbey7748dbf2007-10-18 23:40:49 -0700351 */
Pavel Emelyanovb2d75cd2008-02-08 04:18:54 -0800352static int ipcget_public(struct ipc_namespace *ns, struct ipc_ids *ids,
Nadia Derbey7748dbf2007-10-18 23:40:49 -0700353 struct ipc_ops *ops, struct ipc_params *params)
354{
355 struct kern_ipc_perm *ipcp;
356 int flg = params->flg;
357 int err;
Pierre Peiffer283bb7f2007-10-18 23:40:57 -0700358retry:
Nadia Derbey7748dbf2007-10-18 23:40:49 -0700359 err = idr_pre_get(&ids->ipcs_idr, GFP_KERNEL);
360
Nadia Derbey3e148c72007-10-18 23:40:54 -0700361 /*
362 * Take the lock as a writer since we are potentially going to add
363 * a new entry + read locks are not "upgradable"
364 */
365 down_write(&ids->rw_mutex);
Nadia Derbey7748dbf2007-10-18 23:40:49 -0700366 ipcp = ipc_findkey(ids, params->key);
367 if (ipcp == NULL) {
368 /* key not used */
369 if (!(flg & IPC_CREAT))
370 err = -ENOENT;
371 else if (!err)
372 err = -ENOMEM;
373 else
374 err = ops->getnew(ns, params);
375 } else {
376 /* ipc object has been locked by ipc_findkey() */
377
378 if (flg & IPC_CREAT && flg & IPC_EXCL)
379 err = -EEXIST;
380 else {
381 err = 0;
382 if (ops->more_checks)
383 err = ops->more_checks(ipcp, params);
384 if (!err)
Nadia Derbeyf4566f02007-10-18 23:40:53 -0700385 /*
386 * ipc_check_perms returns the IPC id on
387 * success
388 */
Nadia Derbey7748dbf2007-10-18 23:40:49 -0700389 err = ipc_check_perms(ipcp, ops, params);
390 }
391 ipc_unlock(ipcp);
392 }
Nadia Derbey3e148c72007-10-18 23:40:54 -0700393 up_write(&ids->rw_mutex);
Nadia Derbey7748dbf2007-10-18 23:40:49 -0700394
Pierre Peiffer283bb7f2007-10-18 23:40:57 -0700395 if (err == -EAGAIN)
396 goto retry;
397
Nadia Derbey7748dbf2007-10-18 23:40:49 -0700398 return err;
399}
400
401
402/**
Linus Torvalds1da177e2005-04-16 15:20:36 -0700403 * ipc_rmid - remove an IPC identifier
Nadia Derbeyf4566f02007-10-18 23:40:53 -0700404 * @ids: IPC identifier set
405 * @ipcp: ipc perm structure containing the identifier to remove
Linus Torvalds1da177e2005-04-16 15:20:36 -0700406 *
Nadia Derbey3e148c72007-10-18 23:40:54 -0700407 * ipc_ids.rw_mutex (as a writer) and the spinlock for this ID are held
408 * before this function is called, and remain locked on the exit.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700409 */
410
Nadia Derbey7ca7e562007-10-18 23:40:48 -0700411void ipc_rmid(struct ipc_ids *ids, struct kern_ipc_perm *ipcp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700412{
Nadia Derbeyce621f52007-10-18 23:40:52 -0700413 int lid = ipcid_to_idx(ipcp->id);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700414
Nadia Derbey7ca7e562007-10-18 23:40:48 -0700415 idr_remove(&ids->ipcs_idr, lid);
416
Linus Torvalds1da177e2005-04-16 15:20:36 -0700417 ids->in_use--;
418
Nadia Derbey7ca7e562007-10-18 23:40:48 -0700419 ipcp->deleted = 1;
420
421 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700422}
423
424/**
425 * ipc_alloc - allocate ipc space
426 * @size: size desired
427 *
428 * Allocate memory from the appropriate pools and return a pointer to it.
429 * NULL is returned if the allocation fails
430 */
431
432void* ipc_alloc(int size)
433{
434 void* out;
435 if(size > PAGE_SIZE)
436 out = vmalloc(size);
437 else
438 out = kmalloc(size, GFP_KERNEL);
439 return out;
440}
441
442/**
443 * ipc_free - free ipc space
444 * @ptr: pointer returned by ipc_alloc
445 * @size: size of block
446 *
Robert P. J. Day72fd4a32007-02-10 01:45:59 -0800447 * Free a block created with ipc_alloc(). The caller must know the size
Linus Torvalds1da177e2005-04-16 15:20:36 -0700448 * used in the allocation call.
449 */
450
451void ipc_free(void* ptr, int size)
452{
453 if(size > PAGE_SIZE)
454 vfree(ptr);
455 else
456 kfree(ptr);
457}
458
459/*
460 * rcu allocations:
461 * There are three headers that are prepended to the actual allocation:
462 * - during use: ipc_rcu_hdr.
463 * - during the rcu grace period: ipc_rcu_grace.
464 * - [only if vmalloc]: ipc_rcu_sched.
465 * Their lifetime doesn't overlap, thus the headers share the same memory.
466 * Unlike a normal union, they are right-aligned, thus some container_of
467 * forward/backward casting is necessary:
468 */
469struct ipc_rcu_hdr
470{
471 int refcount;
472 int is_vmalloc;
473 void *data[0];
474};
475
476
477struct ipc_rcu_grace
478{
479 struct rcu_head rcu;
480 /* "void *" makes sure alignment of following data is sane. */
481 void *data[0];
482};
483
484struct ipc_rcu_sched
485{
486 struct work_struct work;
487 /* "void *" makes sure alignment of following data is sane. */
488 void *data[0];
489};
490
491#define HDRLEN_KMALLOC (sizeof(struct ipc_rcu_grace) > sizeof(struct ipc_rcu_hdr) ? \
492 sizeof(struct ipc_rcu_grace) : sizeof(struct ipc_rcu_hdr))
493#define HDRLEN_VMALLOC (sizeof(struct ipc_rcu_sched) > HDRLEN_KMALLOC ? \
494 sizeof(struct ipc_rcu_sched) : HDRLEN_KMALLOC)
495
496static inline int rcu_use_vmalloc(int size)
497{
498 /* Too big for a single page? */
499 if (HDRLEN_KMALLOC + size > PAGE_SIZE)
500 return 1;
501 return 0;
502}
503
504/**
505 * ipc_rcu_alloc - allocate ipc and rcu space
506 * @size: size desired
507 *
508 * Allocate memory for the rcu header structure + the object.
509 * Returns the pointer to the object.
510 * NULL is returned if the allocation fails.
511 */
512
513void* ipc_rcu_alloc(int size)
514{
515 void* out;
516 /*
517 * We prepend the allocation with the rcu struct, and
518 * workqueue if necessary (for vmalloc).
519 */
520 if (rcu_use_vmalloc(size)) {
521 out = vmalloc(HDRLEN_VMALLOC + size);
522 if (out) {
523 out += HDRLEN_VMALLOC;
524 container_of(out, struct ipc_rcu_hdr, data)->is_vmalloc = 1;
525 container_of(out, struct ipc_rcu_hdr, data)->refcount = 1;
526 }
527 } else {
528 out = kmalloc(HDRLEN_KMALLOC + size, GFP_KERNEL);
529 if (out) {
530 out += HDRLEN_KMALLOC;
531 container_of(out, struct ipc_rcu_hdr, data)->is_vmalloc = 0;
532 container_of(out, struct ipc_rcu_hdr, data)->refcount = 1;
533 }
534 }
535
536 return out;
537}
538
539void ipc_rcu_getref(void *ptr)
540{
541 container_of(ptr, struct ipc_rcu_hdr, data)->refcount++;
542}
543
David Howells65f27f32006-11-22 14:55:48 +0000544static void ipc_do_vfree(struct work_struct *work)
545{
546 vfree(container_of(work, struct ipc_rcu_sched, work));
547}
548
Linus Torvalds1da177e2005-04-16 15:20:36 -0700549/**
Randy Dunlap1e5d5332005-11-07 01:01:06 -0800550 * ipc_schedule_free - free ipc + rcu space
551 * @head: RCU callback structure for queued work
Linus Torvalds1da177e2005-04-16 15:20:36 -0700552 *
553 * Since RCU callback function is called in bh,
Robert P. J. Day72fd4a32007-02-10 01:45:59 -0800554 * we need to defer the vfree to schedule_work().
Linus Torvalds1da177e2005-04-16 15:20:36 -0700555 */
556static void ipc_schedule_free(struct rcu_head *head)
557{
Nadia Derbeyf4566f02007-10-18 23:40:53 -0700558 struct ipc_rcu_grace *grace;
559 struct ipc_rcu_sched *sched;
560
561 grace = container_of(head, struct ipc_rcu_grace, rcu);
562 sched = container_of(&(grace->data[0]), struct ipc_rcu_sched,
563 data[0]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700564
David Howells65f27f32006-11-22 14:55:48 +0000565 INIT_WORK(&sched->work, ipc_do_vfree);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700566 schedule_work(&sched->work);
567}
568
569/**
Randy Dunlap1e5d5332005-11-07 01:01:06 -0800570 * ipc_immediate_free - free ipc + rcu space
571 * @head: RCU callback structure that contains pointer to be freed
Linus Torvalds1da177e2005-04-16 15:20:36 -0700572 *
Robert P. J. Day72fd4a32007-02-10 01:45:59 -0800573 * Free from the RCU callback context.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700574 */
575static void ipc_immediate_free(struct rcu_head *head)
576{
577 struct ipc_rcu_grace *free =
578 container_of(head, struct ipc_rcu_grace, rcu);
579 kfree(free);
580}
581
582void ipc_rcu_putref(void *ptr)
583{
584 if (--container_of(ptr, struct ipc_rcu_hdr, data)->refcount > 0)
585 return;
586
587 if (container_of(ptr, struct ipc_rcu_hdr, data)->is_vmalloc) {
588 call_rcu(&container_of(ptr, struct ipc_rcu_grace, data)->rcu,
589 ipc_schedule_free);
590 } else {
591 call_rcu(&container_of(ptr, struct ipc_rcu_grace, data)->rcu,
592 ipc_immediate_free);
593 }
594}
595
596/**
597 * ipcperms - check IPC permissions
598 * @ipcp: IPC permission set
599 * @flag: desired permission set.
600 *
601 * Check user, group, other permissions for access
602 * to ipc resources. return 0 if allowed
603 */
604
605int ipcperms (struct kern_ipc_perm *ipcp, short flag)
606{ /* flag will most probably be 0 or S_...UGO from <linux/stat.h> */
Steve Grubb073115d2006-04-02 17:07:33 -0400607 int requested_mode, granted_mode, err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700608
Steve Grubb073115d2006-04-02 17:07:33 -0400609 if (unlikely((err = audit_ipc_obj(ipcp))))
610 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700611 requested_mode = (flag >> 6) | (flag >> 3) | flag;
612 granted_mode = ipcp->mode;
613 if (current->euid == ipcp->cuid || current->euid == ipcp->uid)
614 granted_mode >>= 6;
615 else if (in_group_p(ipcp->cgid) || in_group_p(ipcp->gid))
616 granted_mode >>= 3;
617 /* is there some bit set in requested_mode but not in granted_mode? */
618 if ((requested_mode & ~granted_mode & 0007) &&
619 !capable(CAP_IPC_OWNER))
620 return -1;
621
622 return security_ipc_permission(ipcp, flag);
623}
624
625/*
626 * Functions to convert between the kern_ipc_perm structure and the
627 * old/new ipc_perm structures
628 */
629
630/**
631 * kernel_to_ipc64_perm - convert kernel ipc permissions to user
632 * @in: kernel permissions
633 * @out: new style IPC permissions
634 *
Robert P. J. Day72fd4a32007-02-10 01:45:59 -0800635 * Turn the kernel object @in into a set of permissions descriptions
636 * for returning to userspace (@out).
Linus Torvalds1da177e2005-04-16 15:20:36 -0700637 */
638
639
640void kernel_to_ipc64_perm (struct kern_ipc_perm *in, struct ipc64_perm *out)
641{
642 out->key = in->key;
643 out->uid = in->uid;
644 out->gid = in->gid;
645 out->cuid = in->cuid;
646 out->cgid = in->cgid;
647 out->mode = in->mode;
648 out->seq = in->seq;
649}
650
651/**
Nadia Derbeyf4566f02007-10-18 23:40:53 -0700652 * ipc64_perm_to_ipc_perm - convert new ipc permissions to old
Linus Torvalds1da177e2005-04-16 15:20:36 -0700653 * @in: new style IPC permissions
654 * @out: old style IPC permissions
655 *
Robert P. J. Day72fd4a32007-02-10 01:45:59 -0800656 * Turn the new style permissions object @in into a compatibility
657 * object and store it into the @out pointer.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700658 */
659
660void ipc64_perm_to_ipc_perm (struct ipc64_perm *in, struct ipc_perm *out)
661{
662 out->key = in->key;
663 SET_UID(out->uid, in->uid);
664 SET_GID(out->gid, in->gid);
665 SET_UID(out->cuid, in->cuid);
666 SET_GID(out->cgid, in->cgid);
667 out->mode = in->mode;
668 out->seq = in->seq;
669}
670
Nadia Derbeyf4566f02007-10-18 23:40:53 -0700671/**
Nadia Derbey3e148c72007-10-18 23:40:54 -0700672 * ipc_lock - Lock an ipc structure without rw_mutex held
Nadia Derbeyf4566f02007-10-18 23:40:53 -0700673 * @ids: IPC identifier set
674 * @id: ipc id to look for
675 *
676 * Look for an id in the ipc ids idr and lock the associated ipc object.
677 *
Nadia Derbeyf4566f02007-10-18 23:40:53 -0700678 * The ipc object is locked on exit.
Nadia Derbey3e148c72007-10-18 23:40:54 -0700679 *
680 * This is the routine that should be called when the rw_mutex is not already
681 * held, i.e. idr tree not protected: it protects the idr tree in read mode
682 * during the idr_find().
Nadia Derbeyf4566f02007-10-18 23:40:53 -0700683 */
684
Nadia Derbey7ca7e562007-10-18 23:40:48 -0700685struct kern_ipc_perm *ipc_lock(struct ipc_ids *ids, int id)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700686{
Nadia Derbey7ca7e562007-10-18 23:40:48 -0700687 struct kern_ipc_perm *out;
Nadia Derbeyce621f52007-10-18 23:40:52 -0700688 int lid = ipcid_to_idx(id);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700689
Nadia Derbey3e148c72007-10-18 23:40:54 -0700690 down_read(&ids->rw_mutex);
691
Linus Torvalds1da177e2005-04-16 15:20:36 -0700692 rcu_read_lock();
Nadia Derbey7ca7e562007-10-18 23:40:48 -0700693 out = idr_find(&ids->ipcs_idr, lid);
694 if (out == NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700695 rcu_read_unlock();
Nadia Derbey3e148c72007-10-18 23:40:54 -0700696 up_read(&ids->rw_mutex);
Nadia Derbey023a5352007-10-18 23:40:51 -0700697 return ERR_PTR(-EINVAL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700698 }
Nadia Derbey7ca7e562007-10-18 23:40:48 -0700699
Nadia Derbey3e148c72007-10-18 23:40:54 -0700700 up_read(&ids->rw_mutex);
701
Linus Torvalds1da177e2005-04-16 15:20:36 -0700702 spin_lock(&out->lock);
703
704 /* ipc_rmid() may have already freed the ID while ipc_lock
705 * was spinning: here verify that the structure is still valid
706 */
707 if (out->deleted) {
708 spin_unlock(&out->lock);
709 rcu_read_unlock();
Nadia Derbey023a5352007-10-18 23:40:51 -0700710 return ERR_PTR(-EINVAL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700711 }
Nadia Derbey7ca7e562007-10-18 23:40:48 -0700712
Linus Torvalds1da177e2005-04-16 15:20:36 -0700713 return out;
714}
715
Nadia Derbey3e148c72007-10-18 23:40:54 -0700716/**
717 * ipc_lock_down - Lock an ipc structure with rw_sem held
718 * @ids: IPC identifier set
719 * @id: ipc id to look for
720 *
721 * Look for an id in the ipc ids idr and lock the associated ipc object.
722 *
723 * The ipc object is locked on exit.
724 *
725 * This is the routine that should be called when the rw_mutex is already
726 * held, i.e. idr tree protected.
727 */
728
729struct kern_ipc_perm *ipc_lock_down(struct ipc_ids *ids, int id)
730{
731 struct kern_ipc_perm *out;
732 int lid = ipcid_to_idx(id);
733
734 rcu_read_lock();
735 out = idr_find(&ids->ipcs_idr, lid);
736 if (out == NULL) {
737 rcu_read_unlock();
738 return ERR_PTR(-EINVAL);
739 }
740
741 spin_lock(&out->lock);
742
743 /*
744 * No need to verify that the structure is still valid since the
745 * rw_mutex is held.
746 */
747 return out;
748}
749
Pavel Emelyanovb2d75cd2008-02-08 04:18:54 -0800750struct kern_ipc_perm *ipc_lock_check_down(struct ipc_ids *ids, int id)
751{
752 struct kern_ipc_perm *out;
753
754 out = ipc_lock_down(ids, id);
755 if (IS_ERR(out))
756 return out;
757
758 if (ipc_checkid(out, id)) {
759 ipc_unlock(out);
760 return ERR_PTR(-EIDRM);
761 }
762
763 return out;
764}
765
766struct kern_ipc_perm *ipc_lock_check(struct ipc_ids *ids, int id)
767{
768 struct kern_ipc_perm *out;
769
770 out = ipc_lock(ids, id);
771 if (IS_ERR(out))
772 return out;
773
774 if (ipc_checkid(out, id)) {
775 ipc_unlock(out);
776 return ERR_PTR(-EIDRM);
777 }
778
779 return out;
780}
781
782/**
783 * ipcget - Common sys_*get() code
784 * @ns : namsepace
785 * @ids : IPC identifier set
786 * @ops : operations to be called on ipc object creation, permission checks
787 * and further checks
788 * @params : the parameters needed by the previous operations.
789 *
790 * Common routine called by sys_msgget(), sys_semget() and sys_shmget().
791 */
792int ipcget(struct ipc_namespace *ns, struct ipc_ids *ids,
793 struct ipc_ops *ops, struct ipc_params *params)
794{
795 if (params->key == IPC_PRIVATE)
796 return ipcget_new(ns, ids, ops, params);
797 else
798 return ipcget_public(ns, ids, ops, params);
799}
800
Linus Torvalds1da177e2005-04-16 15:20:36 -0700801#ifdef __ARCH_WANT_IPC_PARSE_VERSION
802
803
804/**
805 * ipc_parse_version - IPC call version
806 * @cmd: pointer to command
807 *
808 * Return IPC_64 for new style IPC and IPC_OLD for old style IPC.
Robert P. J. Day72fd4a32007-02-10 01:45:59 -0800809 * The @cmd value is turned from an encoding command and version into
Linus Torvalds1da177e2005-04-16 15:20:36 -0700810 * just the command code.
811 */
812
813int ipc_parse_version (int *cmd)
814{
815 if (*cmd & IPC_64) {
816 *cmd ^= IPC_64;
817 return IPC_64;
818 } else {
819 return IPC_OLD;
820 }
821}
822
823#endif /* __ARCH_WANT_IPC_PARSE_VERSION */
Mike Waychisonae781772005-09-06 15:17:09 -0700824
825#ifdef CONFIG_PROC_FS
Eric W. Biedermanbc1fc6d2007-02-12 00:52:10 -0800826struct ipc_proc_iter {
827 struct ipc_namespace *ns;
828 struct ipc_proc_iface *iface;
829};
830
Nadia Derbey7ca7e562007-10-18 23:40:48 -0700831/*
832 * This routine locks the ipc structure found at least at position pos.
833 */
Adrian Bunkb524b9a2008-02-06 01:36:28 -0800834static struct kern_ipc_perm *sysvipc_find_ipc(struct ipc_ids *ids, loff_t pos,
835 loff_t *new_pos)
Mike Waychisonae781772005-09-06 15:17:09 -0700836{
Nadia Derbey7ca7e562007-10-18 23:40:48 -0700837 struct kern_ipc_perm *ipc;
838 int total, id;
Kirill Korotaev73ea4132006-10-02 02:18:20 -0700839
Nadia Derbey7ca7e562007-10-18 23:40:48 -0700840 total = 0;
841 for (id = 0; id < pos && total < ids->in_use; id++) {
842 ipc = idr_find(&ids->ipcs_idr, id);
843 if (ipc != NULL)
844 total++;
845 }
Mike Waychisonae781772005-09-06 15:17:09 -0700846
Nadia Derbey7ca7e562007-10-18 23:40:48 -0700847 if (total >= ids->in_use)
848 return NULL;
Mike Waychisonae781772005-09-06 15:17:09 -0700849
Nadia Derbey7ca7e562007-10-18 23:40:48 -0700850 for ( ; pos < IPCMNI; pos++) {
851 ipc = idr_find(&ids->ipcs_idr, pos);
852 if (ipc != NULL) {
853 *new_pos = pos + 1;
854 ipc_lock_by_ptr(ipc);
Mike Waychisonae781772005-09-06 15:17:09 -0700855 return ipc;
856 }
857 }
858
859 /* Out of range - return NULL to terminate iteration */
860 return NULL;
861}
862
Nadia Derbey7ca7e562007-10-18 23:40:48 -0700863static void *sysvipc_proc_next(struct seq_file *s, void *it, loff_t *pos)
864{
865 struct ipc_proc_iter *iter = s->private;
866 struct ipc_proc_iface *iface = iter->iface;
867 struct kern_ipc_perm *ipc = it;
868
869 /* If we had an ipc id locked before, unlock it */
870 if (ipc && ipc != SEQ_START_TOKEN)
871 ipc_unlock(ipc);
872
Pierre Peiffered2ddbf2008-02-08 04:18:57 -0800873 return sysvipc_find_ipc(&iter->ns->ids[iface->ids], *pos, pos);
Nadia Derbey7ca7e562007-10-18 23:40:48 -0700874}
875
Mike Waychisonae781772005-09-06 15:17:09 -0700876/*
Nadia Derbeyf4566f02007-10-18 23:40:53 -0700877 * File positions: pos 0 -> header, pos n -> ipc id = n - 1.
878 * SeqFile iterator: iterator value locked ipc pointer or SEQ_TOKEN_START.
Mike Waychisonae781772005-09-06 15:17:09 -0700879 */
880static void *sysvipc_proc_start(struct seq_file *s, loff_t *pos)
881{
Eric W. Biedermanbc1fc6d2007-02-12 00:52:10 -0800882 struct ipc_proc_iter *iter = s->private;
883 struct ipc_proc_iface *iface = iter->iface;
Kirill Korotaev73ea4132006-10-02 02:18:20 -0700884 struct ipc_ids *ids;
885
Pierre Peiffered2ddbf2008-02-08 04:18:57 -0800886 ids = &iter->ns->ids[iface->ids];
Mike Waychisonae781772005-09-06 15:17:09 -0700887
888 /*
889 * Take the lock - this will be released by the corresponding
890 * call to stop().
891 */
Nadia Derbey3e148c72007-10-18 23:40:54 -0700892 down_read(&ids->rw_mutex);
Mike Waychisonae781772005-09-06 15:17:09 -0700893
894 /* pos < 0 is invalid */
895 if (*pos < 0)
896 return NULL;
897
898 /* pos == 0 means header */
899 if (*pos == 0)
900 return SEQ_START_TOKEN;
901
902 /* Find the (pos-1)th ipc */
Nadia Derbey7ca7e562007-10-18 23:40:48 -0700903 return sysvipc_find_ipc(ids, *pos - 1, pos);
Mike Waychisonae781772005-09-06 15:17:09 -0700904}
905
906static void sysvipc_proc_stop(struct seq_file *s, void *it)
907{
908 struct kern_ipc_perm *ipc = it;
Eric W. Biedermanbc1fc6d2007-02-12 00:52:10 -0800909 struct ipc_proc_iter *iter = s->private;
910 struct ipc_proc_iface *iface = iter->iface;
Kirill Korotaev73ea4132006-10-02 02:18:20 -0700911 struct ipc_ids *ids;
Mike Waychisonae781772005-09-06 15:17:09 -0700912
Nadia Derbeyf4566f02007-10-18 23:40:53 -0700913 /* If we had a locked structure, release it */
Mike Waychisonae781772005-09-06 15:17:09 -0700914 if (ipc && ipc != SEQ_START_TOKEN)
915 ipc_unlock(ipc);
916
Pierre Peiffered2ddbf2008-02-08 04:18:57 -0800917 ids = &iter->ns->ids[iface->ids];
Mike Waychisonae781772005-09-06 15:17:09 -0700918 /* Release the lock we took in start() */
Nadia Derbey3e148c72007-10-18 23:40:54 -0700919 up_read(&ids->rw_mutex);
Mike Waychisonae781772005-09-06 15:17:09 -0700920}
921
922static int sysvipc_proc_show(struct seq_file *s, void *it)
923{
Eric W. Biedermanbc1fc6d2007-02-12 00:52:10 -0800924 struct ipc_proc_iter *iter = s->private;
925 struct ipc_proc_iface *iface = iter->iface;
Mike Waychisonae781772005-09-06 15:17:09 -0700926
927 if (it == SEQ_START_TOKEN)
928 return seq_puts(s, iface->header);
929
930 return iface->show(s, it);
931}
932
933static struct seq_operations sysvipc_proc_seqops = {
934 .start = sysvipc_proc_start,
935 .stop = sysvipc_proc_stop,
936 .next = sysvipc_proc_next,
937 .show = sysvipc_proc_show,
938};
939
Eric W. Biedermanbc1fc6d2007-02-12 00:52:10 -0800940static int sysvipc_proc_open(struct inode *inode, struct file *file)
941{
Mike Waychisonae781772005-09-06 15:17:09 -0700942 int ret;
943 struct seq_file *seq;
Eric W. Biedermanbc1fc6d2007-02-12 00:52:10 -0800944 struct ipc_proc_iter *iter;
945
946 ret = -ENOMEM;
947 iter = kmalloc(sizeof(*iter), GFP_KERNEL);
948 if (!iter)
949 goto out;
Mike Waychisonae781772005-09-06 15:17:09 -0700950
951 ret = seq_open(file, &sysvipc_proc_seqops);
Eric W. Biedermanbc1fc6d2007-02-12 00:52:10 -0800952 if (ret)
953 goto out_kfree;
954
955 seq = file->private_data;
956 seq->private = iter;
957
958 iter->iface = PDE(inode)->data;
959 iter->ns = get_ipc_ns(current->nsproxy->ipc_ns);
960out:
Mike Waychisonae781772005-09-06 15:17:09 -0700961 return ret;
Eric W. Biedermanbc1fc6d2007-02-12 00:52:10 -0800962out_kfree:
963 kfree(iter);
964 goto out;
965}
966
967static int sysvipc_proc_release(struct inode *inode, struct file *file)
968{
969 struct seq_file *seq = file->private_data;
970 struct ipc_proc_iter *iter = seq->private;
971 put_ipc_ns(iter->ns);
972 return seq_release_private(inode, file);
Mike Waychisonae781772005-09-06 15:17:09 -0700973}
974
Arjan van de Ven9a321442007-02-12 00:55:35 -0800975static const struct file_operations sysvipc_proc_fops = {
Mike Waychisonae781772005-09-06 15:17:09 -0700976 .open = sysvipc_proc_open,
977 .read = seq_read,
978 .llseek = seq_lseek,
Eric W. Biedermanbc1fc6d2007-02-12 00:52:10 -0800979 .release = sysvipc_proc_release,
Mike Waychisonae781772005-09-06 15:17:09 -0700980};
981#endif /* CONFIG_PROC_FS */