blob: 0ad5b1c3ca0128470f39458f6d29386e116d0ba8 [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>
24#include <linux/smp_lock.h>
25#include <linux/vmalloc.h>
26#include <linux/slab.h>
Randy.Dunlapc59ede72006-01-11 12:17:46 -080027#include <linux/capability.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070028#include <linux/highuid.h>
29#include <linux/security.h>
30#include <linux/rcupdate.h>
31#include <linux/workqueue.h>
Mike Waychisonae781772005-09-06 15:17:09 -070032#include <linux/seq_file.h>
33#include <linux/proc_fs.h>
Steve Grubb073115d2006-04-02 17:07:33 -040034#include <linux/audit.h>
Kirill Korotaev73ea4132006-10-02 02:18:20 -070035#include <linux/nsproxy.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070036
37#include <asm/unistd.h>
38
39#include "util.h"
40
Mike Waychisonae781772005-09-06 15:17:09 -070041struct ipc_proc_iface {
42 const char *path;
43 const char *header;
Kirill Korotaev73ea4132006-10-02 02:18:20 -070044 int ids;
Mike Waychisonae781772005-09-06 15:17:09 -070045 int (*show)(struct seq_file *, void *);
46};
47
Kirill Korotaev73ea4132006-10-02 02:18:20 -070048struct ipc_namespace init_ipc_ns = {
49 .kref = {
50 .refcount = ATOMIC_INIT(2),
51 },
52};
53
54#ifdef CONFIG_IPC_NS
55static struct ipc_namespace *clone_ipc_ns(struct ipc_namespace *old_ns)
56{
57 int err;
58 struct ipc_namespace *ns;
59
60 err = -ENOMEM;
61 ns = kmalloc(sizeof(struct ipc_namespace), GFP_KERNEL);
62 if (ns == NULL)
63 goto err_mem;
64
65 err = sem_init_ns(ns);
66 if (err)
67 goto err_sem;
68 err = msg_init_ns(ns);
69 if (err)
70 goto err_msg;
71 err = shm_init_ns(ns);
72 if (err)
73 goto err_shm;
74
75 kref_init(&ns->kref);
76 return ns;
77
78err_shm:
79 msg_exit_ns(ns);
80err_msg:
81 sem_exit_ns(ns);
82err_sem:
83 kfree(ns);
84err_mem:
85 return ERR_PTR(err);
86}
87
Badari Pulavartye3222c42007-05-08 00:25:21 -070088struct ipc_namespace *copy_ipcs(unsigned long flags, struct ipc_namespace *ns)
Kirill Korotaev73ea4132006-10-02 02:18:20 -070089{
Kirill Korotaev73ea4132006-10-02 02:18:20 -070090 struct ipc_namespace *new_ns;
Kirill Korotaev73ea4132006-10-02 02:18:20 -070091
Badari Pulavartye3222c42007-05-08 00:25:21 -070092 BUG_ON(!ns);
93 get_ipc_ns(ns);
Kirill Korotaev73ea4132006-10-02 02:18:20 -070094
95 if (!(flags & CLONE_NEWIPC))
Badari Pulavartye3222c42007-05-08 00:25:21 -070096 return ns;
Kirill Korotaev73ea4132006-10-02 02:18:20 -070097
Badari Pulavartye3222c42007-05-08 00:25:21 -070098 new_ns = clone_ipc_ns(ns);
Kirill Korotaev73ea4132006-10-02 02:18:20 -070099
Badari Pulavartye3222c42007-05-08 00:25:21 -0700100 put_ipc_ns(ns);
101 return new_ns;
Kirill Korotaev73ea4132006-10-02 02:18:20 -0700102}
103
104void free_ipc_ns(struct kref *kref)
105{
106 struct ipc_namespace *ns;
107
108 ns = container_of(kref, struct ipc_namespace, kref);
109 sem_exit_ns(ns);
110 msg_exit_ns(ns);
111 shm_exit_ns(ns);
112 kfree(ns);
113}
Serge E. Hallyna28d1932007-03-26 21:32:31 -0800114#else
Badari Pulavartye3222c42007-05-08 00:25:21 -0700115struct ipc_namespace *copy_ipcs(unsigned long flags, struct ipc_namespace *ns)
Serge E. Hallyna28d1932007-03-26 21:32:31 -0800116{
117 if (flags & CLONE_NEWIPC)
Badari Pulavartye3222c42007-05-08 00:25:21 -0700118 return ERR_PTR(-EINVAL);
119 return ns;
Serge E. Hallyna28d1932007-03-26 21:32:31 -0800120}
Kirill Korotaev73ea4132006-10-02 02:18:20 -0700121#endif
122
Linus Torvalds1da177e2005-04-16 15:20:36 -0700123/**
124 * ipc_init - initialise IPC subsystem
125 *
126 * The various system5 IPC resources (semaphores, messages and shared
Robert P. J. Day72fd4a32007-02-10 01:45:59 -0800127 * memory) are initialised
Linus Torvalds1da177e2005-04-16 15:20:36 -0700128 */
129
130static int __init ipc_init(void)
131{
132 sem_init();
133 msg_init();
134 shm_init();
135 return 0;
136}
137__initcall(ipc_init);
138
139/**
140 * ipc_init_ids - initialise IPC identifiers
141 * @ids: Identifier set
142 * @size: Number of identifiers
143 *
144 * Given a size for the ipc identifier range (limited below IPCMNI)
145 * set up the sequence range to use then allocate and initialise the
146 * array itself.
147 */
148
Kirill Korotaev73ea4132006-10-02 02:18:20 -0700149void __ipc_init ipc_init_ids(struct ipc_ids* ids, int size)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700150{
151 int i;
Ingo Molnar5f921ae2006-03-26 01:37:17 -0800152
153 mutex_init(&ids->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700154
155 if(size > IPCMNI)
156 size = IPCMNI;
157 ids->in_use = 0;
158 ids->max_id = -1;
159 ids->seq = 0;
160 {
161 int seq_limit = INT_MAX/SEQ_MULTIPLIER;
162 if(seq_limit > USHRT_MAX)
163 ids->seq_max = USHRT_MAX;
164 else
165 ids->seq_max = seq_limit;
166 }
167
168 ids->entries = ipc_rcu_alloc(sizeof(struct kern_ipc_perm *)*size +
169 sizeof(struct ipc_id_ary));
170
171 if(ids->entries == NULL) {
172 printk(KERN_ERR "ipc_init_ids() failed, ipc service disabled.\n");
173 size = 0;
174 ids->entries = &ids->nullentry;
175 }
176 ids->entries->size = size;
177 for(i=0;i<size;i++)
178 ids->entries->p[i] = NULL;
179}
180
Mike Waychisonae781772005-09-06 15:17:09 -0700181#ifdef CONFIG_PROC_FS
Arjan van de Ven9a321442007-02-12 00:55:35 -0800182static const struct file_operations sysvipc_proc_fops;
Mike Waychisonae781772005-09-06 15:17:09 -0700183/**
Robert P. J. Day72fd4a32007-02-10 01:45:59 -0800184 * ipc_init_proc_interface - Create a proc interface for sysipc types using a seq_file interface.
Mike Waychisonae781772005-09-06 15:17:09 -0700185 * @path: Path in procfs
186 * @header: Banner to be printed at the beginning of the file.
187 * @ids: ipc id table to iterate.
188 * @show: show routine.
189 */
190void __init ipc_init_proc_interface(const char *path, const char *header,
Kirill Korotaev73ea4132006-10-02 02:18:20 -0700191 int ids, int (*show)(struct seq_file *, void *))
Mike Waychisonae781772005-09-06 15:17:09 -0700192{
193 struct proc_dir_entry *pde;
194 struct ipc_proc_iface *iface;
195
196 iface = kmalloc(sizeof(*iface), GFP_KERNEL);
197 if (!iface)
198 return;
199 iface->path = path;
200 iface->header = header;
201 iface->ids = ids;
202 iface->show = show;
203
204 pde = create_proc_entry(path,
205 S_IRUGO, /* world readable */
206 NULL /* parent dir */);
207 if (pde) {
208 pde->data = iface;
209 pde->proc_fops = &sysvipc_proc_fops;
210 } else {
211 kfree(iface);
212 }
213}
214#endif
215
Linus Torvalds1da177e2005-04-16 15:20:36 -0700216/**
217 * ipc_findkey - find a key in an ipc identifier set
218 * @ids: Identifier set
219 * @key: The key to find
220 *
Ingo Molnar5f921ae2006-03-26 01:37:17 -0800221 * Requires ipc_ids.mutex locked.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700222 * Returns the identifier if found or -1 if not.
223 */
224
225int ipc_findkey(struct ipc_ids* ids, key_t key)
226{
227 int id;
228 struct kern_ipc_perm* p;
229 int max_id = ids->max_id;
230
231 /*
232 * rcu_dereference() is not needed here
Ingo Molnar5f921ae2006-03-26 01:37:17 -0800233 * since ipc_ids.mutex is held
Linus Torvalds1da177e2005-04-16 15:20:36 -0700234 */
235 for (id = 0; id <= max_id; id++) {
236 p = ids->entries->p[id];
237 if(p==NULL)
238 continue;
239 if (key == p->key)
240 return id;
241 }
242 return -1;
243}
244
245/*
Ingo Molnar5f921ae2006-03-26 01:37:17 -0800246 * Requires ipc_ids.mutex locked
Linus Torvalds1da177e2005-04-16 15:20:36 -0700247 */
248static int grow_ary(struct ipc_ids* ids, int newsize)
249{
250 struct ipc_id_ary* new;
251 struct ipc_id_ary* old;
252 int i;
253 int size = ids->entries->size;
254
255 if(newsize > IPCMNI)
256 newsize = IPCMNI;
257 if(newsize <= size)
258 return newsize;
259
260 new = ipc_rcu_alloc(sizeof(struct kern_ipc_perm *)*newsize +
261 sizeof(struct ipc_id_ary));
262 if(new == NULL)
263 return size;
264 new->size = newsize;
Alexey Kuznetsova9a5cd52006-04-17 15:39:23 +0400265 memcpy(new->p, ids->entries->p, sizeof(struct kern_ipc_perm *)*size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700266 for(i=size;i<newsize;i++) {
267 new->p[i] = NULL;
268 }
269 old = ids->entries;
270
271 /*
272 * Use rcu_assign_pointer() to make sure the memcpyed contents
273 * of the new array are visible before the new array becomes visible.
274 */
275 rcu_assign_pointer(ids->entries, new);
276
Pavel Emelianovc7e12b82006-11-02 22:07:03 -0800277 __ipc_fini_ids(ids, old);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700278 return newsize;
279}
280
281/**
282 * ipc_addid - add an IPC identifier
283 * @ids: IPC identifier set
284 * @new: new IPC permission set
285 * @size: new size limit for the id array
286 *
287 * Add an entry 'new' to the IPC arrays. The permissions object is
288 * initialised and the first free entry is set up and the id assigned
289 * is returned. The list is returned in a locked state on success.
290 * On failure the list is not locked and -1 is returned.
291 *
Ingo Molnar5f921ae2006-03-26 01:37:17 -0800292 * Called with ipc_ids.mutex held.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700293 */
294
295int ipc_addid(struct ipc_ids* ids, struct kern_ipc_perm* new, int size)
296{
297 int id;
298
299 size = grow_ary(ids,size);
300
301 /*
302 * rcu_dereference()() is not needed here since
Ingo Molnar5f921ae2006-03-26 01:37:17 -0800303 * ipc_ids.mutex is held
Linus Torvalds1da177e2005-04-16 15:20:36 -0700304 */
305 for (id = 0; id < size; id++) {
306 if(ids->entries->p[id] == NULL)
307 goto found;
308 }
309 return -1;
310found:
311 ids->in_use++;
312 if (id > ids->max_id)
313 ids->max_id = id;
314
315 new->cuid = new->uid = current->euid;
316 new->gid = new->cgid = current->egid;
317
318 new->seq = ids->seq++;
319 if(ids->seq > ids->seq_max)
320 ids->seq = 0;
321
322 spin_lock_init(&new->lock);
323 new->deleted = 0;
324 rcu_read_lock();
325 spin_lock(&new->lock);
326 ids->entries->p[id] = new;
327 return id;
328}
329
330/**
331 * ipc_rmid - remove an IPC identifier
332 * @ids: identifier set
333 * @id: Identifier to remove
334 *
335 * The identifier must be valid, and in use. The kernel will panic if
336 * fed an invalid identifier. The entry is removed and internal
337 * variables recomputed. The object associated with the identifier
338 * is returned.
Ingo Molnar5f921ae2006-03-26 01:37:17 -0800339 * ipc_ids.mutex and the spinlock for this ID is hold before this function
Linus Torvalds1da177e2005-04-16 15:20:36 -0700340 * is called, and remain locked on the exit.
341 */
342
343struct kern_ipc_perm* ipc_rmid(struct ipc_ids* ids, int id)
344{
345 struct kern_ipc_perm* p;
346 int lid = id % SEQ_MULTIPLIER;
Eric Sesterhenn9bc98fc2006-04-01 01:20:23 +0200347 BUG_ON(lid >= ids->entries->size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700348
349 /*
350 * do not need a rcu_dereference()() here to force ordering
Ingo Molnar5f921ae2006-03-26 01:37:17 -0800351 * on Alpha, since the ipc_ids.mutex is held.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700352 */
353 p = ids->entries->p[lid];
354 ids->entries->p[lid] = NULL;
Eric Sesterhenn9bc98fc2006-04-01 01:20:23 +0200355 BUG_ON(p==NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700356 ids->in_use--;
357
358 if (lid == ids->max_id) {
359 do {
360 lid--;
361 if(lid == -1)
362 break;
363 } while (ids->entries->p[lid] == NULL);
364 ids->max_id = lid;
365 }
366 p->deleted = 1;
367 return p;
368}
369
370/**
371 * ipc_alloc - allocate ipc space
372 * @size: size desired
373 *
374 * Allocate memory from the appropriate pools and return a pointer to it.
375 * NULL is returned if the allocation fails
376 */
377
378void* ipc_alloc(int size)
379{
380 void* out;
381 if(size > PAGE_SIZE)
382 out = vmalloc(size);
383 else
384 out = kmalloc(size, GFP_KERNEL);
385 return out;
386}
387
388/**
389 * ipc_free - free ipc space
390 * @ptr: pointer returned by ipc_alloc
391 * @size: size of block
392 *
Robert P. J. Day72fd4a32007-02-10 01:45:59 -0800393 * Free a block created with ipc_alloc(). The caller must know the size
Linus Torvalds1da177e2005-04-16 15:20:36 -0700394 * used in the allocation call.
395 */
396
397void ipc_free(void* ptr, int size)
398{
399 if(size > PAGE_SIZE)
400 vfree(ptr);
401 else
402 kfree(ptr);
403}
404
405/*
406 * rcu allocations:
407 * There are three headers that are prepended to the actual allocation:
408 * - during use: ipc_rcu_hdr.
409 * - during the rcu grace period: ipc_rcu_grace.
410 * - [only if vmalloc]: ipc_rcu_sched.
411 * Their lifetime doesn't overlap, thus the headers share the same memory.
412 * Unlike a normal union, they are right-aligned, thus some container_of
413 * forward/backward casting is necessary:
414 */
415struct ipc_rcu_hdr
416{
417 int refcount;
418 int is_vmalloc;
419 void *data[0];
420};
421
422
423struct ipc_rcu_grace
424{
425 struct rcu_head rcu;
426 /* "void *" makes sure alignment of following data is sane. */
427 void *data[0];
428};
429
430struct ipc_rcu_sched
431{
432 struct work_struct work;
433 /* "void *" makes sure alignment of following data is sane. */
434 void *data[0];
435};
436
437#define HDRLEN_KMALLOC (sizeof(struct ipc_rcu_grace) > sizeof(struct ipc_rcu_hdr) ? \
438 sizeof(struct ipc_rcu_grace) : sizeof(struct ipc_rcu_hdr))
439#define HDRLEN_VMALLOC (sizeof(struct ipc_rcu_sched) > HDRLEN_KMALLOC ? \
440 sizeof(struct ipc_rcu_sched) : HDRLEN_KMALLOC)
441
442static inline int rcu_use_vmalloc(int size)
443{
444 /* Too big for a single page? */
445 if (HDRLEN_KMALLOC + size > PAGE_SIZE)
446 return 1;
447 return 0;
448}
449
450/**
451 * ipc_rcu_alloc - allocate ipc and rcu space
452 * @size: size desired
453 *
454 * Allocate memory for the rcu header structure + the object.
455 * Returns the pointer to the object.
456 * NULL is returned if the allocation fails.
457 */
458
459void* ipc_rcu_alloc(int size)
460{
461 void* out;
462 /*
463 * We prepend the allocation with the rcu struct, and
464 * workqueue if necessary (for vmalloc).
465 */
466 if (rcu_use_vmalloc(size)) {
467 out = vmalloc(HDRLEN_VMALLOC + size);
468 if (out) {
469 out += HDRLEN_VMALLOC;
470 container_of(out, struct ipc_rcu_hdr, data)->is_vmalloc = 1;
471 container_of(out, struct ipc_rcu_hdr, data)->refcount = 1;
472 }
473 } else {
474 out = kmalloc(HDRLEN_KMALLOC + size, GFP_KERNEL);
475 if (out) {
476 out += HDRLEN_KMALLOC;
477 container_of(out, struct ipc_rcu_hdr, data)->is_vmalloc = 0;
478 container_of(out, struct ipc_rcu_hdr, data)->refcount = 1;
479 }
480 }
481
482 return out;
483}
484
485void ipc_rcu_getref(void *ptr)
486{
487 container_of(ptr, struct ipc_rcu_hdr, data)->refcount++;
488}
489
David Howells65f27f32006-11-22 14:55:48 +0000490static void ipc_do_vfree(struct work_struct *work)
491{
492 vfree(container_of(work, struct ipc_rcu_sched, work));
493}
494
Linus Torvalds1da177e2005-04-16 15:20:36 -0700495/**
Randy Dunlap1e5d5332005-11-07 01:01:06 -0800496 * ipc_schedule_free - free ipc + rcu space
497 * @head: RCU callback structure for queued work
Linus Torvalds1da177e2005-04-16 15:20:36 -0700498 *
499 * Since RCU callback function is called in bh,
Robert P. J. Day72fd4a32007-02-10 01:45:59 -0800500 * we need to defer the vfree to schedule_work().
Linus Torvalds1da177e2005-04-16 15:20:36 -0700501 */
502static void ipc_schedule_free(struct rcu_head *head)
503{
504 struct ipc_rcu_grace *grace =
505 container_of(head, struct ipc_rcu_grace, rcu);
506 struct ipc_rcu_sched *sched =
507 container_of(&(grace->data[0]), struct ipc_rcu_sched, data[0]);
508
David Howells65f27f32006-11-22 14:55:48 +0000509 INIT_WORK(&sched->work, ipc_do_vfree);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700510 schedule_work(&sched->work);
511}
512
513/**
Randy Dunlap1e5d5332005-11-07 01:01:06 -0800514 * ipc_immediate_free - free ipc + rcu space
515 * @head: RCU callback structure that contains pointer to be freed
Linus Torvalds1da177e2005-04-16 15:20:36 -0700516 *
Robert P. J. Day72fd4a32007-02-10 01:45:59 -0800517 * Free from the RCU callback context.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700518 */
519static void ipc_immediate_free(struct rcu_head *head)
520{
521 struct ipc_rcu_grace *free =
522 container_of(head, struct ipc_rcu_grace, rcu);
523 kfree(free);
524}
525
526void ipc_rcu_putref(void *ptr)
527{
528 if (--container_of(ptr, struct ipc_rcu_hdr, data)->refcount > 0)
529 return;
530
531 if (container_of(ptr, struct ipc_rcu_hdr, data)->is_vmalloc) {
532 call_rcu(&container_of(ptr, struct ipc_rcu_grace, data)->rcu,
533 ipc_schedule_free);
534 } else {
535 call_rcu(&container_of(ptr, struct ipc_rcu_grace, data)->rcu,
536 ipc_immediate_free);
537 }
538}
539
540/**
541 * ipcperms - check IPC permissions
542 * @ipcp: IPC permission set
543 * @flag: desired permission set.
544 *
545 * Check user, group, other permissions for access
546 * to ipc resources. return 0 if allowed
547 */
548
549int ipcperms (struct kern_ipc_perm *ipcp, short flag)
550{ /* flag will most probably be 0 or S_...UGO from <linux/stat.h> */
Steve Grubb073115d2006-04-02 17:07:33 -0400551 int requested_mode, granted_mode, err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700552
Steve Grubb073115d2006-04-02 17:07:33 -0400553 if (unlikely((err = audit_ipc_obj(ipcp))))
554 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700555 requested_mode = (flag >> 6) | (flag >> 3) | flag;
556 granted_mode = ipcp->mode;
557 if (current->euid == ipcp->cuid || current->euid == ipcp->uid)
558 granted_mode >>= 6;
559 else if (in_group_p(ipcp->cgid) || in_group_p(ipcp->gid))
560 granted_mode >>= 3;
561 /* is there some bit set in requested_mode but not in granted_mode? */
562 if ((requested_mode & ~granted_mode & 0007) &&
563 !capable(CAP_IPC_OWNER))
564 return -1;
565
566 return security_ipc_permission(ipcp, flag);
567}
568
569/*
570 * Functions to convert between the kern_ipc_perm structure and the
571 * old/new ipc_perm structures
572 */
573
574/**
575 * kernel_to_ipc64_perm - convert kernel ipc permissions to user
576 * @in: kernel permissions
577 * @out: new style IPC permissions
578 *
Robert P. J. Day72fd4a32007-02-10 01:45:59 -0800579 * Turn the kernel object @in into a set of permissions descriptions
580 * for returning to userspace (@out).
Linus Torvalds1da177e2005-04-16 15:20:36 -0700581 */
582
583
584void kernel_to_ipc64_perm (struct kern_ipc_perm *in, struct ipc64_perm *out)
585{
586 out->key = in->key;
587 out->uid = in->uid;
588 out->gid = in->gid;
589 out->cuid = in->cuid;
590 out->cgid = in->cgid;
591 out->mode = in->mode;
592 out->seq = in->seq;
593}
594
595/**
596 * ipc64_perm_to_ipc_perm - convert old ipc permissions to new
597 * @in: new style IPC permissions
598 * @out: old style IPC permissions
599 *
Robert P. J. Day72fd4a32007-02-10 01:45:59 -0800600 * Turn the new style permissions object @in into a compatibility
601 * object and store it into the @out pointer.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700602 */
603
604void ipc64_perm_to_ipc_perm (struct ipc64_perm *in, struct ipc_perm *out)
605{
606 out->key = in->key;
607 SET_UID(out->uid, in->uid);
608 SET_GID(out->gid, in->gid);
609 SET_UID(out->cuid, in->cuid);
610 SET_GID(out->cgid, in->cgid);
611 out->mode = in->mode;
612 out->seq = in->seq;
613}
614
615/*
616 * So far only shm_get_stat() calls ipc_get() via shm_get(), so ipc_get()
Ingo Molnar5f921ae2006-03-26 01:37:17 -0800617 * is called with shm_ids.mutex locked. Since grow_ary() is also called with
618 * shm_ids.mutex down(for Shared Memory), there is no need to add read
Linus Torvalds1da177e2005-04-16 15:20:36 -0700619 * barriers here to gurantee the writes in grow_ary() are seen in order
620 * here (for Alpha).
621 *
Ingo Molnar5f921ae2006-03-26 01:37:17 -0800622 * However ipc_get() itself does not necessary require ipc_ids.mutex down. So
623 * if in the future ipc_get() is used by other places without ipc_ids.mutex
Linus Torvalds1da177e2005-04-16 15:20:36 -0700624 * down, then ipc_get() needs read memery barriers as ipc_lock() does.
625 */
626struct kern_ipc_perm* ipc_get(struct ipc_ids* ids, int id)
627{
628 struct kern_ipc_perm* out;
629 int lid = id % SEQ_MULTIPLIER;
630 if(lid >= ids->entries->size)
631 return NULL;
632 out = ids->entries->p[lid];
633 return out;
634}
635
636struct kern_ipc_perm* ipc_lock(struct ipc_ids* ids, int id)
637{
638 struct kern_ipc_perm* out;
639 int lid = id % SEQ_MULTIPLIER;
640 struct ipc_id_ary* entries;
641
642 rcu_read_lock();
643 entries = rcu_dereference(ids->entries);
644 if(lid >= entries->size) {
645 rcu_read_unlock();
646 return NULL;
647 }
648 out = entries->p[lid];
649 if(out == NULL) {
650 rcu_read_unlock();
651 return NULL;
652 }
653 spin_lock(&out->lock);
654
655 /* ipc_rmid() may have already freed the ID while ipc_lock
656 * was spinning: here verify that the structure is still valid
657 */
658 if (out->deleted) {
659 spin_unlock(&out->lock);
660 rcu_read_unlock();
661 return NULL;
662 }
663 return out;
664}
665
666void ipc_lock_by_ptr(struct kern_ipc_perm *perm)
667{
668 rcu_read_lock();
669 spin_lock(&perm->lock);
670}
671
672void ipc_unlock(struct kern_ipc_perm* perm)
673{
674 spin_unlock(&perm->lock);
675 rcu_read_unlock();
676}
677
678int ipc_buildid(struct ipc_ids* ids, int id, int seq)
679{
680 return SEQ_MULTIPLIER*seq + id;
681}
682
683int ipc_checkid(struct ipc_ids* ids, struct kern_ipc_perm* ipcp, int uid)
684{
685 if(uid/SEQ_MULTIPLIER != ipcp->seq)
686 return 1;
687 return 0;
688}
689
690#ifdef __ARCH_WANT_IPC_PARSE_VERSION
691
692
693/**
694 * ipc_parse_version - IPC call version
695 * @cmd: pointer to command
696 *
697 * Return IPC_64 for new style IPC and IPC_OLD for old style IPC.
Robert P. J. Day72fd4a32007-02-10 01:45:59 -0800698 * The @cmd value is turned from an encoding command and version into
Linus Torvalds1da177e2005-04-16 15:20:36 -0700699 * just the command code.
700 */
701
702int ipc_parse_version (int *cmd)
703{
704 if (*cmd & IPC_64) {
705 *cmd ^= IPC_64;
706 return IPC_64;
707 } else {
708 return IPC_OLD;
709 }
710}
711
712#endif /* __ARCH_WANT_IPC_PARSE_VERSION */
Mike Waychisonae781772005-09-06 15:17:09 -0700713
714#ifdef CONFIG_PROC_FS
Eric W. Biedermanbc1fc6d2007-02-12 00:52:10 -0800715struct ipc_proc_iter {
716 struct ipc_namespace *ns;
717 struct ipc_proc_iface *iface;
718};
719
Mike Waychisonae781772005-09-06 15:17:09 -0700720static void *sysvipc_proc_next(struct seq_file *s, void *it, loff_t *pos)
721{
Eric W. Biedermanbc1fc6d2007-02-12 00:52:10 -0800722 struct ipc_proc_iter *iter = s->private;
723 struct ipc_proc_iface *iface = iter->iface;
Mike Waychisonae781772005-09-06 15:17:09 -0700724 struct kern_ipc_perm *ipc = it;
725 loff_t p;
Kirill Korotaev73ea4132006-10-02 02:18:20 -0700726 struct ipc_ids *ids;
727
Eric W. Biedermanbc1fc6d2007-02-12 00:52:10 -0800728 ids = iter->ns->ids[iface->ids];
Mike Waychisonae781772005-09-06 15:17:09 -0700729
730 /* If we had an ipc id locked before, unlock it */
731 if (ipc && ipc != SEQ_START_TOKEN)
732 ipc_unlock(ipc);
733
734 /*
735 * p = *pos - 1 (because id 0 starts at position 1)
736 * + 1 (because we increment the position by one)
737 */
Kirill Korotaev73ea4132006-10-02 02:18:20 -0700738 for (p = *pos; p <= ids->max_id; p++) {
739 if ((ipc = ipc_lock(ids, p)) != NULL) {
Mike Waychisonae781772005-09-06 15:17:09 -0700740 *pos = p + 1;
741 return ipc;
742 }
743 }
744
745 /* Out of range - return NULL to terminate iteration */
746 return NULL;
747}
748
749/*
750 * File positions: pos 0 -> header, pos n -> ipc id + 1.
751 * SeqFile iterator: iterator value locked shp or SEQ_TOKEN_START.
752 */
753static void *sysvipc_proc_start(struct seq_file *s, loff_t *pos)
754{
Eric W. Biedermanbc1fc6d2007-02-12 00:52:10 -0800755 struct ipc_proc_iter *iter = s->private;
756 struct ipc_proc_iface *iface = iter->iface;
Mike Waychisonae781772005-09-06 15:17:09 -0700757 struct kern_ipc_perm *ipc;
758 loff_t p;
Kirill Korotaev73ea4132006-10-02 02:18:20 -0700759 struct ipc_ids *ids;
760
Eric W. Biedermanbc1fc6d2007-02-12 00:52:10 -0800761 ids = iter->ns->ids[iface->ids];
Mike Waychisonae781772005-09-06 15:17:09 -0700762
763 /*
764 * Take the lock - this will be released by the corresponding
765 * call to stop().
766 */
Kirill Korotaev73ea4132006-10-02 02:18:20 -0700767 mutex_lock(&ids->mutex);
Mike Waychisonae781772005-09-06 15:17:09 -0700768
769 /* pos < 0 is invalid */
770 if (*pos < 0)
771 return NULL;
772
773 /* pos == 0 means header */
774 if (*pos == 0)
775 return SEQ_START_TOKEN;
776
777 /* Find the (pos-1)th ipc */
Kirill Korotaev73ea4132006-10-02 02:18:20 -0700778 for (p = *pos - 1; p <= ids->max_id; p++) {
779 if ((ipc = ipc_lock(ids, p)) != NULL) {
Mike Waychisonae781772005-09-06 15:17:09 -0700780 *pos = p + 1;
781 return ipc;
782 }
783 }
784 return NULL;
785}
786
787static void sysvipc_proc_stop(struct seq_file *s, void *it)
788{
789 struct kern_ipc_perm *ipc = it;
Eric W. Biedermanbc1fc6d2007-02-12 00:52:10 -0800790 struct ipc_proc_iter *iter = s->private;
791 struct ipc_proc_iface *iface = iter->iface;
Kirill Korotaev73ea4132006-10-02 02:18:20 -0700792 struct ipc_ids *ids;
Mike Waychisonae781772005-09-06 15:17:09 -0700793
794 /* If we had a locked segment, release it */
795 if (ipc && ipc != SEQ_START_TOKEN)
796 ipc_unlock(ipc);
797
Eric W. Biedermanbc1fc6d2007-02-12 00:52:10 -0800798 ids = iter->ns->ids[iface->ids];
Mike Waychisonae781772005-09-06 15:17:09 -0700799 /* Release the lock we took in start() */
Kirill Korotaev73ea4132006-10-02 02:18:20 -0700800 mutex_unlock(&ids->mutex);
Mike Waychisonae781772005-09-06 15:17:09 -0700801}
802
803static int sysvipc_proc_show(struct seq_file *s, void *it)
804{
Eric W. Biedermanbc1fc6d2007-02-12 00:52:10 -0800805 struct ipc_proc_iter *iter = s->private;
806 struct ipc_proc_iface *iface = iter->iface;
Mike Waychisonae781772005-09-06 15:17:09 -0700807
808 if (it == SEQ_START_TOKEN)
809 return seq_puts(s, iface->header);
810
811 return iface->show(s, it);
812}
813
814static struct seq_operations sysvipc_proc_seqops = {
815 .start = sysvipc_proc_start,
816 .stop = sysvipc_proc_stop,
817 .next = sysvipc_proc_next,
818 .show = sysvipc_proc_show,
819};
820
Eric W. Biedermanbc1fc6d2007-02-12 00:52:10 -0800821static int sysvipc_proc_open(struct inode *inode, struct file *file)
822{
Mike Waychisonae781772005-09-06 15:17:09 -0700823 int ret;
824 struct seq_file *seq;
Eric W. Biedermanbc1fc6d2007-02-12 00:52:10 -0800825 struct ipc_proc_iter *iter;
826
827 ret = -ENOMEM;
828 iter = kmalloc(sizeof(*iter), GFP_KERNEL);
829 if (!iter)
830 goto out;
Mike Waychisonae781772005-09-06 15:17:09 -0700831
832 ret = seq_open(file, &sysvipc_proc_seqops);
Eric W. Biedermanbc1fc6d2007-02-12 00:52:10 -0800833 if (ret)
834 goto out_kfree;
835
836 seq = file->private_data;
837 seq->private = iter;
838
839 iter->iface = PDE(inode)->data;
840 iter->ns = get_ipc_ns(current->nsproxy->ipc_ns);
841out:
Mike Waychisonae781772005-09-06 15:17:09 -0700842 return ret;
Eric W. Biedermanbc1fc6d2007-02-12 00:52:10 -0800843out_kfree:
844 kfree(iter);
845 goto out;
846}
847
848static int sysvipc_proc_release(struct inode *inode, struct file *file)
849{
850 struct seq_file *seq = file->private_data;
851 struct ipc_proc_iter *iter = seq->private;
852 put_ipc_ns(iter->ns);
853 return seq_release_private(inode, file);
Mike Waychisonae781772005-09-06 15:17:09 -0700854}
855
Arjan van de Ven9a321442007-02-12 00:55:35 -0800856static const struct file_operations sysvipc_proc_fops = {
Mike Waychisonae781772005-09-06 15:17:09 -0700857 .open = sysvipc_proc_open,
858 .read = seq_read,
859 .llseek = seq_lseek,
Eric W. Biedermanbc1fc6d2007-02-12 00:52:10 -0800860 .release = sysvipc_proc_release,
Mike Waychisonae781772005-09-06 15:17:09 -0700861};
862#endif /* CONFIG_PROC_FS */