blob: 5432b8e34c9b9506757174ba513a7c079641baab [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>
Pavel Emelyanovae5e1b22008-02-08 04:18:22 -080036#include <linux/ipc_namespace.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070037
38#include <asm/unistd.h>
39
40#include "util.h"
41
Mike Waychisonae781772005-09-06 15:17:09 -070042struct ipc_proc_iface {
43 const char *path;
44 const char *header;
Kirill Korotaev73ea4132006-10-02 02:18:20 -070045 int ids;
Mike Waychisonae781772005-09-06 15:17:09 -070046 int (*show)(struct seq_file *, void *);
47};
48
Kirill Korotaev73ea4132006-10-02 02:18:20 -070049struct ipc_namespace init_ipc_ns = {
50 .kref = {
51 .refcount = ATOMIC_INIT(2),
52 },
53};
54
Linus Torvalds1da177e2005-04-16 15:20:36 -070055/**
56 * ipc_init - initialise IPC subsystem
57 *
58 * The various system5 IPC resources (semaphores, messages and shared
Robert P. J. Day72fd4a32007-02-10 01:45:59 -080059 * memory) are initialised
Linus Torvalds1da177e2005-04-16 15:20:36 -070060 */
61
62static int __init ipc_init(void)
63{
64 sem_init();
65 msg_init();
66 shm_init();
67 return 0;
68}
69__initcall(ipc_init);
70
71/**
72 * ipc_init_ids - initialise IPC identifiers
73 * @ids: Identifier set
Linus Torvalds1da177e2005-04-16 15:20:36 -070074 *
Nadia Derbey7ca7e562007-10-18 23:40:48 -070075 * Set up the sequence range to use for the ipc identifier range (limited
76 * below IPCMNI) then initialise the ids idr.
Linus Torvalds1da177e2005-04-16 15:20:36 -070077 */
78
Nadia Derbey7ca7e562007-10-18 23:40:48 -070079void ipc_init_ids(struct ipc_ids *ids)
Linus Torvalds1da177e2005-04-16 15:20:36 -070080{
Nadia Derbey3e148c72007-10-18 23:40:54 -070081 init_rwsem(&ids->rw_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -070082
Linus Torvalds1da177e2005-04-16 15:20:36 -070083 ids->in_use = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070084 ids->seq = 0;
85 {
86 int seq_limit = INT_MAX/SEQ_MULTIPLIER;
87 if(seq_limit > USHRT_MAX)
88 ids->seq_max = USHRT_MAX;
89 else
90 ids->seq_max = seq_limit;
91 }
92
Nadia Derbey7ca7e562007-10-18 23:40:48 -070093 idr_init(&ids->ipcs_idr);
Linus Torvalds1da177e2005-04-16 15:20:36 -070094}
95
Mike Waychisonae781772005-09-06 15:17:09 -070096#ifdef CONFIG_PROC_FS
Arjan van de Ven9a321442007-02-12 00:55:35 -080097static const struct file_operations sysvipc_proc_fops;
Mike Waychisonae781772005-09-06 15:17:09 -070098/**
Robert P. J. Day72fd4a32007-02-10 01:45:59 -080099 * ipc_init_proc_interface - Create a proc interface for sysipc types using a seq_file interface.
Mike Waychisonae781772005-09-06 15:17:09 -0700100 * @path: Path in procfs
101 * @header: Banner to be printed at the beginning of the file.
102 * @ids: ipc id table to iterate.
103 * @show: show routine.
104 */
105void __init ipc_init_proc_interface(const char *path, const char *header,
Kirill Korotaev73ea4132006-10-02 02:18:20 -0700106 int ids, int (*show)(struct seq_file *, void *))
Mike Waychisonae781772005-09-06 15:17:09 -0700107{
108 struct proc_dir_entry *pde;
109 struct ipc_proc_iface *iface;
110
111 iface = kmalloc(sizeof(*iface), GFP_KERNEL);
112 if (!iface)
113 return;
114 iface->path = path;
115 iface->header = header;
116 iface->ids = ids;
117 iface->show = show;
118
119 pde = create_proc_entry(path,
120 S_IRUGO, /* world readable */
121 NULL /* parent dir */);
122 if (pde) {
123 pde->data = iface;
124 pde->proc_fops = &sysvipc_proc_fops;
125 } else {
126 kfree(iface);
127 }
128}
129#endif
130
Linus Torvalds1da177e2005-04-16 15:20:36 -0700131/**
132 * ipc_findkey - find a key in an ipc identifier set
133 * @ids: Identifier set
134 * @key: The key to find
135 *
Nadia Derbey3e148c72007-10-18 23:40:54 -0700136 * Requires ipc_ids.rw_mutex locked.
Nadia Derbey7ca7e562007-10-18 23:40:48 -0700137 * Returns the LOCKED pointer to the ipc structure if found or NULL
138 * if not.
Nadia Derbeyf4566f02007-10-18 23:40:53 -0700139 * If key is found ipc points to the owning ipc structure
Linus Torvalds1da177e2005-04-16 15:20:36 -0700140 */
141
Nadia Derbey7748dbf2007-10-18 23:40:49 -0700142static struct kern_ipc_perm *ipc_findkey(struct ipc_ids *ids, key_t key)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700143{
Nadia Derbey7ca7e562007-10-18 23:40:48 -0700144 struct kern_ipc_perm *ipc;
145 int next_id;
146 int total;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700147
Nadia Derbey7ca7e562007-10-18 23:40:48 -0700148 for (total = 0, next_id = 0; total < ids->in_use; next_id++) {
149 ipc = idr_find(&ids->ipcs_idr, next_id);
150
151 if (ipc == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700152 continue;
Nadia Derbey7ca7e562007-10-18 23:40:48 -0700153
154 if (ipc->key != key) {
155 total++;
156 continue;
157 }
158
159 ipc_lock_by_ptr(ipc);
160 return ipc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700161 }
Nadia Derbey7ca7e562007-10-18 23:40:48 -0700162
163 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700164}
165
Nadia Derbey7ca7e562007-10-18 23:40:48 -0700166/**
167 * ipc_get_maxid - get the last assigned id
168 * @ids: IPC identifier set
169 *
Nadia Derbey3e148c72007-10-18 23:40:54 -0700170 * Called with ipc_ids.rw_mutex held.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700171 */
Nadia Derbey7ca7e562007-10-18 23:40:48 -0700172
173int ipc_get_maxid(struct ipc_ids *ids)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700174{
Nadia Derbey7ca7e562007-10-18 23:40:48 -0700175 struct kern_ipc_perm *ipc;
176 int max_id = -1;
177 int total, id;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700178
Nadia Derbey7ca7e562007-10-18 23:40:48 -0700179 if (ids->in_use == 0)
180 return -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700181
Nadia Derbey7ca7e562007-10-18 23:40:48 -0700182 if (ids->in_use == IPCMNI)
183 return IPCMNI - 1;
184
185 /* Look for the last assigned id */
186 total = 0;
187 for (id = 0; id < IPCMNI && total < ids->in_use; id++) {
188 ipc = idr_find(&ids->ipcs_idr, id);
189 if (ipc != NULL) {
190 max_id = id;
191 total++;
192 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700193 }
Nadia Derbey7ca7e562007-10-18 23:40:48 -0700194 return max_id;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700195}
196
197/**
198 * ipc_addid - add an IPC identifier
199 * @ids: IPC identifier set
200 * @new: new IPC permission set
Nadia Derbey7ca7e562007-10-18 23:40:48 -0700201 * @size: limit for the number of used ids
Linus Torvalds1da177e2005-04-16 15:20:36 -0700202 *
Nadia Derbeyf4566f02007-10-18 23:40:53 -0700203 * Add an entry 'new' to the IPC ids idr. The permissions object is
Linus Torvalds1da177e2005-04-16 15:20:36 -0700204 * initialised and the first free entry is set up and the id assigned
Nadia Derbeyf4566f02007-10-18 23:40:53 -0700205 * is returned. The 'new' entry is returned in a locked state on success.
Pierre Peiffer283bb7f2007-10-18 23:40:57 -0700206 * On failure the entry is not locked and a negative err-code is returned.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700207 *
Nadia Derbey3e148c72007-10-18 23:40:54 -0700208 * Called with ipc_ids.rw_mutex held as a writer.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700209 */
210
211int ipc_addid(struct ipc_ids* ids, struct kern_ipc_perm* new, int size)
212{
Nadia Derbey7ca7e562007-10-18 23:40:48 -0700213 int id, err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700214
Nadia Derbey7ca7e562007-10-18 23:40:48 -0700215 if (size > IPCMNI)
216 size = IPCMNI;
217
218 if (ids->in_use >= size)
Pierre Peiffer283bb7f2007-10-18 23:40:57 -0700219 return -ENOSPC;
Nadia Derbey7ca7e562007-10-18 23:40:48 -0700220
221 err = idr_get_new(&ids->ipcs_idr, new, &id);
222 if (err)
Pierre Peiffer283bb7f2007-10-18 23:40:57 -0700223 return err;
Nadia Derbey7ca7e562007-10-18 23:40:48 -0700224
Linus Torvalds1da177e2005-04-16 15:20:36 -0700225 ids->in_use++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700226
227 new->cuid = new->uid = current->euid;
228 new->gid = new->cgid = current->egid;
229
230 new->seq = ids->seq++;
231 if(ids->seq > ids->seq_max)
232 ids->seq = 0;
233
234 spin_lock_init(&new->lock);
235 new->deleted = 0;
236 rcu_read_lock();
237 spin_lock(&new->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700238 return id;
239}
240
241/**
Nadia Derbey7748dbf2007-10-18 23:40:49 -0700242 * ipcget_new - create a new ipc object
243 * @ns: namespace
Nadia Derbeyf4566f02007-10-18 23:40:53 -0700244 * @ids: IPC identifer set
Nadia Derbey7748dbf2007-10-18 23:40:49 -0700245 * @ops: the actual creation routine to call
246 * @params: its parameters
247 *
Nadia Derbeyf4566f02007-10-18 23:40:53 -0700248 * This routine is called by sys_msgget, sys_semget() and sys_shmget()
249 * when the key is IPC_PRIVATE.
Nadia Derbey7748dbf2007-10-18 23:40:49 -0700250 */
251int ipcget_new(struct ipc_namespace *ns, struct ipc_ids *ids,
252 struct ipc_ops *ops, struct ipc_params *params)
253{
254 int err;
Pierre Peiffer283bb7f2007-10-18 23:40:57 -0700255retry:
Nadia Derbey7748dbf2007-10-18 23:40:49 -0700256 err = idr_pre_get(&ids->ipcs_idr, GFP_KERNEL);
257
258 if (!err)
259 return -ENOMEM;
260
Nadia Derbey3e148c72007-10-18 23:40:54 -0700261 down_write(&ids->rw_mutex);
Nadia Derbey7748dbf2007-10-18 23:40:49 -0700262 err = ops->getnew(ns, params);
Nadia Derbey3e148c72007-10-18 23:40:54 -0700263 up_write(&ids->rw_mutex);
Nadia Derbey7748dbf2007-10-18 23:40:49 -0700264
Pierre Peiffer283bb7f2007-10-18 23:40:57 -0700265 if (err == -EAGAIN)
266 goto retry;
267
Nadia Derbey7748dbf2007-10-18 23:40:49 -0700268 return err;
269}
270
271/**
272 * ipc_check_perms - check security and permissions for an IPC
273 * @ipcp: ipc permission set
Nadia Derbey7748dbf2007-10-18 23:40:49 -0700274 * @ops: the actual security routine to call
275 * @params: its parameters
Nadia Derbeyf4566f02007-10-18 23:40:53 -0700276 *
277 * This routine is called by sys_msgget(), sys_semget() and sys_shmget()
278 * when the key is not IPC_PRIVATE and that key already exists in the
279 * ids IDR.
280 *
281 * On success, the IPC id is returned.
282 *
Nadia Derbey3e148c72007-10-18 23:40:54 -0700283 * It is called with ipc_ids.rw_mutex and ipcp->lock held.
Nadia Derbey7748dbf2007-10-18 23:40:49 -0700284 */
285static int ipc_check_perms(struct kern_ipc_perm *ipcp, struct ipc_ops *ops,
286 struct ipc_params *params)
287{
288 int err;
289
290 if (ipcperms(ipcp, params->flg))
291 err = -EACCES;
292 else {
293 err = ops->associate(ipcp, params->flg);
294 if (!err)
295 err = ipcp->id;
296 }
297
298 return err;
299}
300
301/**
302 * ipcget_public - get an ipc object or create a new one
303 * @ns: namespace
Nadia Derbeyf4566f02007-10-18 23:40:53 -0700304 * @ids: IPC identifer set
Nadia Derbey7748dbf2007-10-18 23:40:49 -0700305 * @ops: the actual creation routine to call
306 * @params: its parameters
307 *
Nadia Derbeyf4566f02007-10-18 23:40:53 -0700308 * This routine is called by sys_msgget, sys_semget() and sys_shmget()
309 * when the key is not IPC_PRIVATE.
310 * It adds a new entry if the key is not found and does some permission
311 * / security checkings if the key is found.
312 *
313 * On success, the ipc id is returned.
Nadia Derbey7748dbf2007-10-18 23:40:49 -0700314 */
315int ipcget_public(struct ipc_namespace *ns, struct ipc_ids *ids,
316 struct ipc_ops *ops, struct ipc_params *params)
317{
318 struct kern_ipc_perm *ipcp;
319 int flg = params->flg;
320 int err;
Pierre Peiffer283bb7f2007-10-18 23:40:57 -0700321retry:
Nadia Derbey7748dbf2007-10-18 23:40:49 -0700322 err = idr_pre_get(&ids->ipcs_idr, GFP_KERNEL);
323
Nadia Derbey3e148c72007-10-18 23:40:54 -0700324 /*
325 * Take the lock as a writer since we are potentially going to add
326 * a new entry + read locks are not "upgradable"
327 */
328 down_write(&ids->rw_mutex);
Nadia Derbey7748dbf2007-10-18 23:40:49 -0700329 ipcp = ipc_findkey(ids, params->key);
330 if (ipcp == NULL) {
331 /* key not used */
332 if (!(flg & IPC_CREAT))
333 err = -ENOENT;
334 else if (!err)
335 err = -ENOMEM;
336 else
337 err = ops->getnew(ns, params);
338 } else {
339 /* ipc object has been locked by ipc_findkey() */
340
341 if (flg & IPC_CREAT && flg & IPC_EXCL)
342 err = -EEXIST;
343 else {
344 err = 0;
345 if (ops->more_checks)
346 err = ops->more_checks(ipcp, params);
347 if (!err)
Nadia Derbeyf4566f02007-10-18 23:40:53 -0700348 /*
349 * ipc_check_perms returns the IPC id on
350 * success
351 */
Nadia Derbey7748dbf2007-10-18 23:40:49 -0700352 err = ipc_check_perms(ipcp, ops, params);
353 }
354 ipc_unlock(ipcp);
355 }
Nadia Derbey3e148c72007-10-18 23:40:54 -0700356 up_write(&ids->rw_mutex);
Nadia Derbey7748dbf2007-10-18 23:40:49 -0700357
Pierre Peiffer283bb7f2007-10-18 23:40:57 -0700358 if (err == -EAGAIN)
359 goto retry;
360
Nadia Derbey7748dbf2007-10-18 23:40:49 -0700361 return err;
362}
363
364
365/**
Linus Torvalds1da177e2005-04-16 15:20:36 -0700366 * ipc_rmid - remove an IPC identifier
Nadia Derbeyf4566f02007-10-18 23:40:53 -0700367 * @ids: IPC identifier set
368 * @ipcp: ipc perm structure containing the identifier to remove
Linus Torvalds1da177e2005-04-16 15:20:36 -0700369 *
Nadia Derbey3e148c72007-10-18 23:40:54 -0700370 * ipc_ids.rw_mutex (as a writer) and the spinlock for this ID are held
371 * before this function is called, and remain locked on the exit.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700372 */
373
Nadia Derbey7ca7e562007-10-18 23:40:48 -0700374void ipc_rmid(struct ipc_ids *ids, struct kern_ipc_perm *ipcp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700375{
Nadia Derbeyce621f52007-10-18 23:40:52 -0700376 int lid = ipcid_to_idx(ipcp->id);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700377
Nadia Derbey7ca7e562007-10-18 23:40:48 -0700378 idr_remove(&ids->ipcs_idr, lid);
379
Linus Torvalds1da177e2005-04-16 15:20:36 -0700380 ids->in_use--;
381
Nadia Derbey7ca7e562007-10-18 23:40:48 -0700382 ipcp->deleted = 1;
383
384 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700385}
386
387/**
388 * ipc_alloc - allocate ipc space
389 * @size: size desired
390 *
391 * Allocate memory from the appropriate pools and return a pointer to it.
392 * NULL is returned if the allocation fails
393 */
394
395void* ipc_alloc(int size)
396{
397 void* out;
398 if(size > PAGE_SIZE)
399 out = vmalloc(size);
400 else
401 out = kmalloc(size, GFP_KERNEL);
402 return out;
403}
404
405/**
406 * ipc_free - free ipc space
407 * @ptr: pointer returned by ipc_alloc
408 * @size: size of block
409 *
Robert P. J. Day72fd4a32007-02-10 01:45:59 -0800410 * Free a block created with ipc_alloc(). The caller must know the size
Linus Torvalds1da177e2005-04-16 15:20:36 -0700411 * used in the allocation call.
412 */
413
414void ipc_free(void* ptr, int size)
415{
416 if(size > PAGE_SIZE)
417 vfree(ptr);
418 else
419 kfree(ptr);
420}
421
422/*
423 * rcu allocations:
424 * There are three headers that are prepended to the actual allocation:
425 * - during use: ipc_rcu_hdr.
426 * - during the rcu grace period: ipc_rcu_grace.
427 * - [only if vmalloc]: ipc_rcu_sched.
428 * Their lifetime doesn't overlap, thus the headers share the same memory.
429 * Unlike a normal union, they are right-aligned, thus some container_of
430 * forward/backward casting is necessary:
431 */
432struct ipc_rcu_hdr
433{
434 int refcount;
435 int is_vmalloc;
436 void *data[0];
437};
438
439
440struct ipc_rcu_grace
441{
442 struct rcu_head rcu;
443 /* "void *" makes sure alignment of following data is sane. */
444 void *data[0];
445};
446
447struct ipc_rcu_sched
448{
449 struct work_struct work;
450 /* "void *" makes sure alignment of following data is sane. */
451 void *data[0];
452};
453
454#define HDRLEN_KMALLOC (sizeof(struct ipc_rcu_grace) > sizeof(struct ipc_rcu_hdr) ? \
455 sizeof(struct ipc_rcu_grace) : sizeof(struct ipc_rcu_hdr))
456#define HDRLEN_VMALLOC (sizeof(struct ipc_rcu_sched) > HDRLEN_KMALLOC ? \
457 sizeof(struct ipc_rcu_sched) : HDRLEN_KMALLOC)
458
459static inline int rcu_use_vmalloc(int size)
460{
461 /* Too big for a single page? */
462 if (HDRLEN_KMALLOC + size > PAGE_SIZE)
463 return 1;
464 return 0;
465}
466
467/**
468 * ipc_rcu_alloc - allocate ipc and rcu space
469 * @size: size desired
470 *
471 * Allocate memory for the rcu header structure + the object.
472 * Returns the pointer to the object.
473 * NULL is returned if the allocation fails.
474 */
475
476void* ipc_rcu_alloc(int size)
477{
478 void* out;
479 /*
480 * We prepend the allocation with the rcu struct, and
481 * workqueue if necessary (for vmalloc).
482 */
483 if (rcu_use_vmalloc(size)) {
484 out = vmalloc(HDRLEN_VMALLOC + size);
485 if (out) {
486 out += HDRLEN_VMALLOC;
487 container_of(out, struct ipc_rcu_hdr, data)->is_vmalloc = 1;
488 container_of(out, struct ipc_rcu_hdr, data)->refcount = 1;
489 }
490 } else {
491 out = kmalloc(HDRLEN_KMALLOC + size, GFP_KERNEL);
492 if (out) {
493 out += HDRLEN_KMALLOC;
494 container_of(out, struct ipc_rcu_hdr, data)->is_vmalloc = 0;
495 container_of(out, struct ipc_rcu_hdr, data)->refcount = 1;
496 }
497 }
498
499 return out;
500}
501
502void ipc_rcu_getref(void *ptr)
503{
504 container_of(ptr, struct ipc_rcu_hdr, data)->refcount++;
505}
506
David Howells65f27f32006-11-22 14:55:48 +0000507static void ipc_do_vfree(struct work_struct *work)
508{
509 vfree(container_of(work, struct ipc_rcu_sched, work));
510}
511
Linus Torvalds1da177e2005-04-16 15:20:36 -0700512/**
Randy Dunlap1e5d5332005-11-07 01:01:06 -0800513 * ipc_schedule_free - free ipc + rcu space
514 * @head: RCU callback structure for queued work
Linus Torvalds1da177e2005-04-16 15:20:36 -0700515 *
516 * Since RCU callback function is called in bh,
Robert P. J. Day72fd4a32007-02-10 01:45:59 -0800517 * we need to defer the vfree to schedule_work().
Linus Torvalds1da177e2005-04-16 15:20:36 -0700518 */
519static void ipc_schedule_free(struct rcu_head *head)
520{
Nadia Derbeyf4566f02007-10-18 23:40:53 -0700521 struct ipc_rcu_grace *grace;
522 struct ipc_rcu_sched *sched;
523
524 grace = container_of(head, struct ipc_rcu_grace, rcu);
525 sched = container_of(&(grace->data[0]), struct ipc_rcu_sched,
526 data[0]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700527
David Howells65f27f32006-11-22 14:55:48 +0000528 INIT_WORK(&sched->work, ipc_do_vfree);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700529 schedule_work(&sched->work);
530}
531
532/**
Randy Dunlap1e5d5332005-11-07 01:01:06 -0800533 * ipc_immediate_free - free ipc + rcu space
534 * @head: RCU callback structure that contains pointer to be freed
Linus Torvalds1da177e2005-04-16 15:20:36 -0700535 *
Robert P. J. Day72fd4a32007-02-10 01:45:59 -0800536 * Free from the RCU callback context.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700537 */
538static void ipc_immediate_free(struct rcu_head *head)
539{
540 struct ipc_rcu_grace *free =
541 container_of(head, struct ipc_rcu_grace, rcu);
542 kfree(free);
543}
544
545void ipc_rcu_putref(void *ptr)
546{
547 if (--container_of(ptr, struct ipc_rcu_hdr, data)->refcount > 0)
548 return;
549
550 if (container_of(ptr, struct ipc_rcu_hdr, data)->is_vmalloc) {
551 call_rcu(&container_of(ptr, struct ipc_rcu_grace, data)->rcu,
552 ipc_schedule_free);
553 } else {
554 call_rcu(&container_of(ptr, struct ipc_rcu_grace, data)->rcu,
555 ipc_immediate_free);
556 }
557}
558
559/**
560 * ipcperms - check IPC permissions
561 * @ipcp: IPC permission set
562 * @flag: desired permission set.
563 *
564 * Check user, group, other permissions for access
565 * to ipc resources. return 0 if allowed
566 */
567
568int ipcperms (struct kern_ipc_perm *ipcp, short flag)
569{ /* flag will most probably be 0 or S_...UGO from <linux/stat.h> */
Steve Grubb073115d2006-04-02 17:07:33 -0400570 int requested_mode, granted_mode, err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700571
Steve Grubb073115d2006-04-02 17:07:33 -0400572 if (unlikely((err = audit_ipc_obj(ipcp))))
573 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700574 requested_mode = (flag >> 6) | (flag >> 3) | flag;
575 granted_mode = ipcp->mode;
576 if (current->euid == ipcp->cuid || current->euid == ipcp->uid)
577 granted_mode >>= 6;
578 else if (in_group_p(ipcp->cgid) || in_group_p(ipcp->gid))
579 granted_mode >>= 3;
580 /* is there some bit set in requested_mode but not in granted_mode? */
581 if ((requested_mode & ~granted_mode & 0007) &&
582 !capable(CAP_IPC_OWNER))
583 return -1;
584
585 return security_ipc_permission(ipcp, flag);
586}
587
588/*
589 * Functions to convert between the kern_ipc_perm structure and the
590 * old/new ipc_perm structures
591 */
592
593/**
594 * kernel_to_ipc64_perm - convert kernel ipc permissions to user
595 * @in: kernel permissions
596 * @out: new style IPC permissions
597 *
Robert P. J. Day72fd4a32007-02-10 01:45:59 -0800598 * Turn the kernel object @in into a set of permissions descriptions
599 * for returning to userspace (@out).
Linus Torvalds1da177e2005-04-16 15:20:36 -0700600 */
601
602
603void kernel_to_ipc64_perm (struct kern_ipc_perm *in, struct ipc64_perm *out)
604{
605 out->key = in->key;
606 out->uid = in->uid;
607 out->gid = in->gid;
608 out->cuid = in->cuid;
609 out->cgid = in->cgid;
610 out->mode = in->mode;
611 out->seq = in->seq;
612}
613
614/**
Nadia Derbeyf4566f02007-10-18 23:40:53 -0700615 * ipc64_perm_to_ipc_perm - convert new ipc permissions to old
Linus Torvalds1da177e2005-04-16 15:20:36 -0700616 * @in: new style IPC permissions
617 * @out: old style IPC permissions
618 *
Robert P. J. Day72fd4a32007-02-10 01:45:59 -0800619 * Turn the new style permissions object @in into a compatibility
620 * object and store it into the @out pointer.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700621 */
622
623void ipc64_perm_to_ipc_perm (struct ipc64_perm *in, struct ipc_perm *out)
624{
625 out->key = in->key;
626 SET_UID(out->uid, in->uid);
627 SET_GID(out->gid, in->gid);
628 SET_UID(out->cuid, in->cuid);
629 SET_GID(out->cgid, in->cgid);
630 out->mode = in->mode;
631 out->seq = in->seq;
632}
633
Nadia Derbeyf4566f02007-10-18 23:40:53 -0700634/**
Nadia Derbey3e148c72007-10-18 23:40:54 -0700635 * ipc_lock - Lock an ipc structure without rw_mutex held
Nadia Derbeyf4566f02007-10-18 23:40:53 -0700636 * @ids: IPC identifier set
637 * @id: ipc id to look for
638 *
639 * Look for an id in the ipc ids idr and lock the associated ipc object.
640 *
Nadia Derbeyf4566f02007-10-18 23:40:53 -0700641 * The ipc object is locked on exit.
Nadia Derbey3e148c72007-10-18 23:40:54 -0700642 *
643 * This is the routine that should be called when the rw_mutex is not already
644 * held, i.e. idr tree not protected: it protects the idr tree in read mode
645 * during the idr_find().
Nadia Derbeyf4566f02007-10-18 23:40:53 -0700646 */
647
Nadia Derbey7ca7e562007-10-18 23:40:48 -0700648struct kern_ipc_perm *ipc_lock(struct ipc_ids *ids, int id)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700649{
Nadia Derbey7ca7e562007-10-18 23:40:48 -0700650 struct kern_ipc_perm *out;
Nadia Derbeyce621f52007-10-18 23:40:52 -0700651 int lid = ipcid_to_idx(id);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700652
Nadia Derbey3e148c72007-10-18 23:40:54 -0700653 down_read(&ids->rw_mutex);
654
Linus Torvalds1da177e2005-04-16 15:20:36 -0700655 rcu_read_lock();
Nadia Derbey7ca7e562007-10-18 23:40:48 -0700656 out = idr_find(&ids->ipcs_idr, lid);
657 if (out == NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700658 rcu_read_unlock();
Nadia Derbey3e148c72007-10-18 23:40:54 -0700659 up_read(&ids->rw_mutex);
Nadia Derbey023a5352007-10-18 23:40:51 -0700660 return ERR_PTR(-EINVAL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700661 }
Nadia Derbey7ca7e562007-10-18 23:40:48 -0700662
Nadia Derbey3e148c72007-10-18 23:40:54 -0700663 up_read(&ids->rw_mutex);
664
Linus Torvalds1da177e2005-04-16 15:20:36 -0700665 spin_lock(&out->lock);
666
667 /* ipc_rmid() may have already freed the ID while ipc_lock
668 * was spinning: here verify that the structure is still valid
669 */
670 if (out->deleted) {
671 spin_unlock(&out->lock);
672 rcu_read_unlock();
Nadia Derbey023a5352007-10-18 23:40:51 -0700673 return ERR_PTR(-EINVAL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700674 }
Nadia Derbey7ca7e562007-10-18 23:40:48 -0700675
Linus Torvalds1da177e2005-04-16 15:20:36 -0700676 return out;
677}
678
Nadia Derbey3e148c72007-10-18 23:40:54 -0700679/**
680 * ipc_lock_down - Lock an ipc structure with rw_sem held
681 * @ids: IPC identifier set
682 * @id: ipc id to look for
683 *
684 * Look for an id in the ipc ids idr and lock the associated ipc object.
685 *
686 * The ipc object is locked on exit.
687 *
688 * This is the routine that should be called when the rw_mutex is already
689 * held, i.e. idr tree protected.
690 */
691
692struct kern_ipc_perm *ipc_lock_down(struct ipc_ids *ids, int id)
693{
694 struct kern_ipc_perm *out;
695 int lid = ipcid_to_idx(id);
696
697 rcu_read_lock();
698 out = idr_find(&ids->ipcs_idr, lid);
699 if (out == NULL) {
700 rcu_read_unlock();
701 return ERR_PTR(-EINVAL);
702 }
703
704 spin_lock(&out->lock);
705
706 /*
707 * No need to verify that the structure is still valid since the
708 * rw_mutex is held.
709 */
710 return out;
711}
712
Linus Torvalds1da177e2005-04-16 15:20:36 -0700713#ifdef __ARCH_WANT_IPC_PARSE_VERSION
714
715
716/**
717 * ipc_parse_version - IPC call version
718 * @cmd: pointer to command
719 *
720 * Return IPC_64 for new style IPC and IPC_OLD for old style IPC.
Robert P. J. Day72fd4a32007-02-10 01:45:59 -0800721 * The @cmd value is turned from an encoding command and version into
Linus Torvalds1da177e2005-04-16 15:20:36 -0700722 * just the command code.
723 */
724
725int ipc_parse_version (int *cmd)
726{
727 if (*cmd & IPC_64) {
728 *cmd ^= IPC_64;
729 return IPC_64;
730 } else {
731 return IPC_OLD;
732 }
733}
734
735#endif /* __ARCH_WANT_IPC_PARSE_VERSION */
Mike Waychisonae781772005-09-06 15:17:09 -0700736
737#ifdef CONFIG_PROC_FS
Eric W. Biedermanbc1fc6d2007-02-12 00:52:10 -0800738struct ipc_proc_iter {
739 struct ipc_namespace *ns;
740 struct ipc_proc_iface *iface;
741};
742
Nadia Derbey7ca7e562007-10-18 23:40:48 -0700743/*
744 * This routine locks the ipc structure found at least at position pos.
745 */
Adrian Bunkb524b9a2008-02-06 01:36:28 -0800746static struct kern_ipc_perm *sysvipc_find_ipc(struct ipc_ids *ids, loff_t pos,
747 loff_t *new_pos)
Mike Waychisonae781772005-09-06 15:17:09 -0700748{
Nadia Derbey7ca7e562007-10-18 23:40:48 -0700749 struct kern_ipc_perm *ipc;
750 int total, id;
Kirill Korotaev73ea4132006-10-02 02:18:20 -0700751
Nadia Derbey7ca7e562007-10-18 23:40:48 -0700752 total = 0;
753 for (id = 0; id < pos && total < ids->in_use; id++) {
754 ipc = idr_find(&ids->ipcs_idr, id);
755 if (ipc != NULL)
756 total++;
757 }
Mike Waychisonae781772005-09-06 15:17:09 -0700758
Nadia Derbey7ca7e562007-10-18 23:40:48 -0700759 if (total >= ids->in_use)
760 return NULL;
Mike Waychisonae781772005-09-06 15:17:09 -0700761
Nadia Derbey7ca7e562007-10-18 23:40:48 -0700762 for ( ; pos < IPCMNI; pos++) {
763 ipc = idr_find(&ids->ipcs_idr, pos);
764 if (ipc != NULL) {
765 *new_pos = pos + 1;
766 ipc_lock_by_ptr(ipc);
Mike Waychisonae781772005-09-06 15:17:09 -0700767 return ipc;
768 }
769 }
770
771 /* Out of range - return NULL to terminate iteration */
772 return NULL;
773}
774
Nadia Derbey7ca7e562007-10-18 23:40:48 -0700775static void *sysvipc_proc_next(struct seq_file *s, void *it, loff_t *pos)
776{
777 struct ipc_proc_iter *iter = s->private;
778 struct ipc_proc_iface *iface = iter->iface;
779 struct kern_ipc_perm *ipc = it;
780
781 /* If we had an ipc id locked before, unlock it */
782 if (ipc && ipc != SEQ_START_TOKEN)
783 ipc_unlock(ipc);
784
785 return sysvipc_find_ipc(iter->ns->ids[iface->ids], *pos, pos);
786}
787
Mike Waychisonae781772005-09-06 15:17:09 -0700788/*
Nadia Derbeyf4566f02007-10-18 23:40:53 -0700789 * File positions: pos 0 -> header, pos n -> ipc id = n - 1.
790 * SeqFile iterator: iterator value locked ipc pointer or SEQ_TOKEN_START.
Mike Waychisonae781772005-09-06 15:17:09 -0700791 */
792static void *sysvipc_proc_start(struct seq_file *s, loff_t *pos)
793{
Eric W. Biedermanbc1fc6d2007-02-12 00:52:10 -0800794 struct ipc_proc_iter *iter = s->private;
795 struct ipc_proc_iface *iface = iter->iface;
Kirill Korotaev73ea4132006-10-02 02:18:20 -0700796 struct ipc_ids *ids;
797
Eric W. Biedermanbc1fc6d2007-02-12 00:52:10 -0800798 ids = iter->ns->ids[iface->ids];
Mike Waychisonae781772005-09-06 15:17:09 -0700799
800 /*
801 * Take the lock - this will be released by the corresponding
802 * call to stop().
803 */
Nadia Derbey3e148c72007-10-18 23:40:54 -0700804 down_read(&ids->rw_mutex);
Mike Waychisonae781772005-09-06 15:17:09 -0700805
806 /* pos < 0 is invalid */
807 if (*pos < 0)
808 return NULL;
809
810 /* pos == 0 means header */
811 if (*pos == 0)
812 return SEQ_START_TOKEN;
813
814 /* Find the (pos-1)th ipc */
Nadia Derbey7ca7e562007-10-18 23:40:48 -0700815 return sysvipc_find_ipc(ids, *pos - 1, pos);
Mike Waychisonae781772005-09-06 15:17:09 -0700816}
817
818static void sysvipc_proc_stop(struct seq_file *s, void *it)
819{
820 struct kern_ipc_perm *ipc = it;
Eric W. Biedermanbc1fc6d2007-02-12 00:52:10 -0800821 struct ipc_proc_iter *iter = s->private;
822 struct ipc_proc_iface *iface = iter->iface;
Kirill Korotaev73ea4132006-10-02 02:18:20 -0700823 struct ipc_ids *ids;
Mike Waychisonae781772005-09-06 15:17:09 -0700824
Nadia Derbeyf4566f02007-10-18 23:40:53 -0700825 /* If we had a locked structure, release it */
Mike Waychisonae781772005-09-06 15:17:09 -0700826 if (ipc && ipc != SEQ_START_TOKEN)
827 ipc_unlock(ipc);
828
Eric W. Biedermanbc1fc6d2007-02-12 00:52:10 -0800829 ids = iter->ns->ids[iface->ids];
Mike Waychisonae781772005-09-06 15:17:09 -0700830 /* Release the lock we took in start() */
Nadia Derbey3e148c72007-10-18 23:40:54 -0700831 up_read(&ids->rw_mutex);
Mike Waychisonae781772005-09-06 15:17:09 -0700832}
833
834static int sysvipc_proc_show(struct seq_file *s, void *it)
835{
Eric W. Biedermanbc1fc6d2007-02-12 00:52:10 -0800836 struct ipc_proc_iter *iter = s->private;
837 struct ipc_proc_iface *iface = iter->iface;
Mike Waychisonae781772005-09-06 15:17:09 -0700838
839 if (it == SEQ_START_TOKEN)
840 return seq_puts(s, iface->header);
841
842 return iface->show(s, it);
843}
844
845static struct seq_operations sysvipc_proc_seqops = {
846 .start = sysvipc_proc_start,
847 .stop = sysvipc_proc_stop,
848 .next = sysvipc_proc_next,
849 .show = sysvipc_proc_show,
850};
851
Eric W. Biedermanbc1fc6d2007-02-12 00:52:10 -0800852static int sysvipc_proc_open(struct inode *inode, struct file *file)
853{
Mike Waychisonae781772005-09-06 15:17:09 -0700854 int ret;
855 struct seq_file *seq;
Eric W. Biedermanbc1fc6d2007-02-12 00:52:10 -0800856 struct ipc_proc_iter *iter;
857
858 ret = -ENOMEM;
859 iter = kmalloc(sizeof(*iter), GFP_KERNEL);
860 if (!iter)
861 goto out;
Mike Waychisonae781772005-09-06 15:17:09 -0700862
863 ret = seq_open(file, &sysvipc_proc_seqops);
Eric W. Biedermanbc1fc6d2007-02-12 00:52:10 -0800864 if (ret)
865 goto out_kfree;
866
867 seq = file->private_data;
868 seq->private = iter;
869
870 iter->iface = PDE(inode)->data;
871 iter->ns = get_ipc_ns(current->nsproxy->ipc_ns);
872out:
Mike Waychisonae781772005-09-06 15:17:09 -0700873 return ret;
Eric W. Biedermanbc1fc6d2007-02-12 00:52:10 -0800874out_kfree:
875 kfree(iter);
876 goto out;
877}
878
879static int sysvipc_proc_release(struct inode *inode, struct file *file)
880{
881 struct seq_file *seq = file->private_data;
882 struct ipc_proc_iter *iter = seq->private;
883 put_ipc_ns(iter->ns);
884 return seq_release_private(inode, file);
Mike Waychisonae781772005-09-06 15:17:09 -0700885}
886
Arjan van de Ven9a321442007-02-12 00:55:35 -0800887static const struct file_operations sysvipc_proc_fops = {
Mike Waychisonae781772005-09-06 15:17:09 -0700888 .open = sysvipc_proc_open,
889 .read = seq_read,
890 .llseek = seq_lseek,
Eric W. Biedermanbc1fc6d2007-02-12 00:52:10 -0800891 .release = sysvipc_proc_release,
Mike Waychisonae781772005-09-06 15:17:09 -0700892};
893#endif /* CONFIG_PROC_FS */