blob: 10e836d0d89e86fb36de703364a0afdeb0f21c7e [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
10 * Manfred Spraul <manfreds@colorfullife.com>
11 * Oct 2002 - One lock per IPC id. RCU ipc_free for lock-free grow_ary().
12 * Mingming Cao <cmm@us.ibm.com>
13 */
14
15#include <linux/config.h>
16#include <linux/mm.h>
17#include <linux/shm.h>
18#include <linux/init.h>
19#include <linux/msg.h>
20#include <linux/smp_lock.h>
21#include <linux/vmalloc.h>
22#include <linux/slab.h>
23#include <linux/highuid.h>
24#include <linux/security.h>
25#include <linux/rcupdate.h>
26#include <linux/workqueue.h>
Mike Waychisonae781772005-09-06 15:17:09 -070027#include <linux/seq_file.h>
28#include <linux/proc_fs.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070029
30#include <asm/unistd.h>
31
32#include "util.h"
33
Mike Waychisonae781772005-09-06 15:17:09 -070034struct ipc_proc_iface {
35 const char *path;
36 const char *header;
37 struct ipc_ids *ids;
38 int (*show)(struct seq_file *, void *);
39};
40
Linus Torvalds1da177e2005-04-16 15:20:36 -070041/**
42 * ipc_init - initialise IPC subsystem
43 *
44 * The various system5 IPC resources (semaphores, messages and shared
45 * memory are initialised
46 */
47
48static int __init ipc_init(void)
49{
50 sem_init();
51 msg_init();
52 shm_init();
53 return 0;
54}
55__initcall(ipc_init);
56
57/**
58 * ipc_init_ids - initialise IPC identifiers
59 * @ids: Identifier set
60 * @size: Number of identifiers
61 *
62 * Given a size for the ipc identifier range (limited below IPCMNI)
63 * set up the sequence range to use then allocate and initialise the
64 * array itself.
65 */
66
67void __init ipc_init_ids(struct ipc_ids* ids, int size)
68{
69 int i;
70 sema_init(&ids->sem,1);
71
72 if(size > IPCMNI)
73 size = IPCMNI;
74 ids->in_use = 0;
75 ids->max_id = -1;
76 ids->seq = 0;
77 {
78 int seq_limit = INT_MAX/SEQ_MULTIPLIER;
79 if(seq_limit > USHRT_MAX)
80 ids->seq_max = USHRT_MAX;
81 else
82 ids->seq_max = seq_limit;
83 }
84
85 ids->entries = ipc_rcu_alloc(sizeof(struct kern_ipc_perm *)*size +
86 sizeof(struct ipc_id_ary));
87
88 if(ids->entries == NULL) {
89 printk(KERN_ERR "ipc_init_ids() failed, ipc service disabled.\n");
90 size = 0;
91 ids->entries = &ids->nullentry;
92 }
93 ids->entries->size = size;
94 for(i=0;i<size;i++)
95 ids->entries->p[i] = NULL;
96}
97
Mike Waychisonae781772005-09-06 15:17:09 -070098#ifdef CONFIG_PROC_FS
99static struct file_operations sysvipc_proc_fops;
100/**
101 * ipc_init_proc_interface - Create a proc interface for sysipc types
102 * using a seq_file interface.
103 * @path: Path in procfs
104 * @header: Banner to be printed at the beginning of the file.
105 * @ids: ipc id table to iterate.
106 * @show: show routine.
107 */
108void __init ipc_init_proc_interface(const char *path, const char *header,
109 struct ipc_ids *ids,
110 int (*show)(struct seq_file *, void *))
111{
112 struct proc_dir_entry *pde;
113 struct ipc_proc_iface *iface;
114
115 iface = kmalloc(sizeof(*iface), GFP_KERNEL);
116 if (!iface)
117 return;
118 iface->path = path;
119 iface->header = header;
120 iface->ids = ids;
121 iface->show = show;
122
123 pde = create_proc_entry(path,
124 S_IRUGO, /* world readable */
125 NULL /* parent dir */);
126 if (pde) {
127 pde->data = iface;
128 pde->proc_fops = &sysvipc_proc_fops;
129 } else {
130 kfree(iface);
131 }
132}
133#endif
134
Linus Torvalds1da177e2005-04-16 15:20:36 -0700135/**
136 * ipc_findkey - find a key in an ipc identifier set
137 * @ids: Identifier set
138 * @key: The key to find
139 *
140 * Requires ipc_ids.sem locked.
141 * Returns the identifier if found or -1 if not.
142 */
143
144int ipc_findkey(struct ipc_ids* ids, key_t key)
145{
146 int id;
147 struct kern_ipc_perm* p;
148 int max_id = ids->max_id;
149
150 /*
151 * rcu_dereference() is not needed here
152 * since ipc_ids.sem is held
153 */
154 for (id = 0; id <= max_id; id++) {
155 p = ids->entries->p[id];
156 if(p==NULL)
157 continue;
158 if (key == p->key)
159 return id;
160 }
161 return -1;
162}
163
164/*
165 * Requires ipc_ids.sem locked
166 */
167static int grow_ary(struct ipc_ids* ids, int newsize)
168{
169 struct ipc_id_ary* new;
170 struct ipc_id_ary* old;
171 int i;
172 int size = ids->entries->size;
173
174 if(newsize > IPCMNI)
175 newsize = IPCMNI;
176 if(newsize <= size)
177 return newsize;
178
179 new = ipc_rcu_alloc(sizeof(struct kern_ipc_perm *)*newsize +
180 sizeof(struct ipc_id_ary));
181 if(new == NULL)
182 return size;
183 new->size = newsize;
184 memcpy(new->p, ids->entries->p, sizeof(struct kern_ipc_perm *)*size +
185 sizeof(struct ipc_id_ary));
186 for(i=size;i<newsize;i++) {
187 new->p[i] = NULL;
188 }
189 old = ids->entries;
190
191 /*
192 * Use rcu_assign_pointer() to make sure the memcpyed contents
193 * of the new array are visible before the new array becomes visible.
194 */
195 rcu_assign_pointer(ids->entries, new);
196
197 ipc_rcu_putref(old);
198 return newsize;
199}
200
201/**
202 * ipc_addid - add an IPC identifier
203 * @ids: IPC identifier set
204 * @new: new IPC permission set
205 * @size: new size limit for the id array
206 *
207 * Add an entry 'new' to the IPC arrays. The permissions object is
208 * initialised and the first free entry is set up and the id assigned
209 * is returned. The list is returned in a locked state on success.
210 * On failure the list is not locked and -1 is returned.
211 *
212 * Called with ipc_ids.sem held.
213 */
214
215int ipc_addid(struct ipc_ids* ids, struct kern_ipc_perm* new, int size)
216{
217 int id;
218
219 size = grow_ary(ids,size);
220
221 /*
222 * rcu_dereference()() is not needed here since
223 * ipc_ids.sem is held
224 */
225 for (id = 0; id < size; id++) {
226 if(ids->entries->p[id] == NULL)
227 goto found;
228 }
229 return -1;
230found:
231 ids->in_use++;
232 if (id > ids->max_id)
233 ids->max_id = id;
234
235 new->cuid = new->uid = current->euid;
236 new->gid = new->cgid = current->egid;
237
238 new->seq = ids->seq++;
239 if(ids->seq > ids->seq_max)
240 ids->seq = 0;
241
242 spin_lock_init(&new->lock);
243 new->deleted = 0;
244 rcu_read_lock();
245 spin_lock(&new->lock);
246 ids->entries->p[id] = new;
247 return id;
248}
249
250/**
251 * ipc_rmid - remove an IPC identifier
252 * @ids: identifier set
253 * @id: Identifier to remove
254 *
255 * The identifier must be valid, and in use. The kernel will panic if
256 * fed an invalid identifier. The entry is removed and internal
257 * variables recomputed. The object associated with the identifier
258 * is returned.
259 * ipc_ids.sem and the spinlock for this ID is hold before this function
260 * is called, and remain locked on the exit.
261 */
262
263struct kern_ipc_perm* ipc_rmid(struct ipc_ids* ids, int id)
264{
265 struct kern_ipc_perm* p;
266 int lid = id % SEQ_MULTIPLIER;
267 if(lid >= ids->entries->size)
268 BUG();
269
270 /*
271 * do not need a rcu_dereference()() here to force ordering
272 * on Alpha, since the ipc_ids.sem is held.
273 */
274 p = ids->entries->p[lid];
275 ids->entries->p[lid] = NULL;
276 if(p==NULL)
277 BUG();
278 ids->in_use--;
279
280 if (lid == ids->max_id) {
281 do {
282 lid--;
283 if(lid == -1)
284 break;
285 } while (ids->entries->p[lid] == NULL);
286 ids->max_id = lid;
287 }
288 p->deleted = 1;
289 return p;
290}
291
292/**
293 * ipc_alloc - allocate ipc space
294 * @size: size desired
295 *
296 * Allocate memory from the appropriate pools and return a pointer to it.
297 * NULL is returned if the allocation fails
298 */
299
300void* ipc_alloc(int size)
301{
302 void* out;
303 if(size > PAGE_SIZE)
304 out = vmalloc(size);
305 else
306 out = kmalloc(size, GFP_KERNEL);
307 return out;
308}
309
310/**
311 * ipc_free - free ipc space
312 * @ptr: pointer returned by ipc_alloc
313 * @size: size of block
314 *
315 * Free a block created with ipc_alloc. The caller must know the size
316 * used in the allocation call.
317 */
318
319void ipc_free(void* ptr, int size)
320{
321 if(size > PAGE_SIZE)
322 vfree(ptr);
323 else
324 kfree(ptr);
325}
326
327/*
328 * rcu allocations:
329 * There are three headers that are prepended to the actual allocation:
330 * - during use: ipc_rcu_hdr.
331 * - during the rcu grace period: ipc_rcu_grace.
332 * - [only if vmalloc]: ipc_rcu_sched.
333 * Their lifetime doesn't overlap, thus the headers share the same memory.
334 * Unlike a normal union, they are right-aligned, thus some container_of
335 * forward/backward casting is necessary:
336 */
337struct ipc_rcu_hdr
338{
339 int refcount;
340 int is_vmalloc;
341 void *data[0];
342};
343
344
345struct ipc_rcu_grace
346{
347 struct rcu_head rcu;
348 /* "void *" makes sure alignment of following data is sane. */
349 void *data[0];
350};
351
352struct ipc_rcu_sched
353{
354 struct work_struct work;
355 /* "void *" makes sure alignment of following data is sane. */
356 void *data[0];
357};
358
359#define HDRLEN_KMALLOC (sizeof(struct ipc_rcu_grace) > sizeof(struct ipc_rcu_hdr) ? \
360 sizeof(struct ipc_rcu_grace) : sizeof(struct ipc_rcu_hdr))
361#define HDRLEN_VMALLOC (sizeof(struct ipc_rcu_sched) > HDRLEN_KMALLOC ? \
362 sizeof(struct ipc_rcu_sched) : HDRLEN_KMALLOC)
363
364static inline int rcu_use_vmalloc(int size)
365{
366 /* Too big for a single page? */
367 if (HDRLEN_KMALLOC + size > PAGE_SIZE)
368 return 1;
369 return 0;
370}
371
372/**
373 * ipc_rcu_alloc - allocate ipc and rcu space
374 * @size: size desired
375 *
376 * Allocate memory for the rcu header structure + the object.
377 * Returns the pointer to the object.
378 * NULL is returned if the allocation fails.
379 */
380
381void* ipc_rcu_alloc(int size)
382{
383 void* out;
384 /*
385 * We prepend the allocation with the rcu struct, and
386 * workqueue if necessary (for vmalloc).
387 */
388 if (rcu_use_vmalloc(size)) {
389 out = vmalloc(HDRLEN_VMALLOC + size);
390 if (out) {
391 out += HDRLEN_VMALLOC;
392 container_of(out, struct ipc_rcu_hdr, data)->is_vmalloc = 1;
393 container_of(out, struct ipc_rcu_hdr, data)->refcount = 1;
394 }
395 } else {
396 out = kmalloc(HDRLEN_KMALLOC + size, GFP_KERNEL);
397 if (out) {
398 out += HDRLEN_KMALLOC;
399 container_of(out, struct ipc_rcu_hdr, data)->is_vmalloc = 0;
400 container_of(out, struct ipc_rcu_hdr, data)->refcount = 1;
401 }
402 }
403
404 return out;
405}
406
407void ipc_rcu_getref(void *ptr)
408{
409 container_of(ptr, struct ipc_rcu_hdr, data)->refcount++;
410}
411
412/**
413 * ipc_schedule_free - free ipc + rcu space
414 *
415 * Since RCU callback function is called in bh,
416 * we need to defer the vfree to schedule_work
417 */
418static void ipc_schedule_free(struct rcu_head *head)
419{
420 struct ipc_rcu_grace *grace =
421 container_of(head, struct ipc_rcu_grace, rcu);
422 struct ipc_rcu_sched *sched =
423 container_of(&(grace->data[0]), struct ipc_rcu_sched, data[0]);
424
425 INIT_WORK(&sched->work, vfree, sched);
426 schedule_work(&sched->work);
427}
428
429/**
430 * ipc_immediate_free - free ipc + rcu space
431 *
432 * Free from the RCU callback context
433 *
434 */
435static void ipc_immediate_free(struct rcu_head *head)
436{
437 struct ipc_rcu_grace *free =
438 container_of(head, struct ipc_rcu_grace, rcu);
439 kfree(free);
440}
441
442void ipc_rcu_putref(void *ptr)
443{
444 if (--container_of(ptr, struct ipc_rcu_hdr, data)->refcount > 0)
445 return;
446
447 if (container_of(ptr, struct ipc_rcu_hdr, data)->is_vmalloc) {
448 call_rcu(&container_of(ptr, struct ipc_rcu_grace, data)->rcu,
449 ipc_schedule_free);
450 } else {
451 call_rcu(&container_of(ptr, struct ipc_rcu_grace, data)->rcu,
452 ipc_immediate_free);
453 }
454}
455
456/**
457 * ipcperms - check IPC permissions
458 * @ipcp: IPC permission set
459 * @flag: desired permission set.
460 *
461 * Check user, group, other permissions for access
462 * to ipc resources. return 0 if allowed
463 */
464
465int ipcperms (struct kern_ipc_perm *ipcp, short flag)
466{ /* flag will most probably be 0 or S_...UGO from <linux/stat.h> */
467 int requested_mode, granted_mode;
468
469 requested_mode = (flag >> 6) | (flag >> 3) | flag;
470 granted_mode = ipcp->mode;
471 if (current->euid == ipcp->cuid || current->euid == ipcp->uid)
472 granted_mode >>= 6;
473 else if (in_group_p(ipcp->cgid) || in_group_p(ipcp->gid))
474 granted_mode >>= 3;
475 /* is there some bit set in requested_mode but not in granted_mode? */
476 if ((requested_mode & ~granted_mode & 0007) &&
477 !capable(CAP_IPC_OWNER))
478 return -1;
479
480 return security_ipc_permission(ipcp, flag);
481}
482
483/*
484 * Functions to convert between the kern_ipc_perm structure and the
485 * old/new ipc_perm structures
486 */
487
488/**
489 * kernel_to_ipc64_perm - convert kernel ipc permissions to user
490 * @in: kernel permissions
491 * @out: new style IPC permissions
492 *
493 * Turn the kernel object 'in' into a set of permissions descriptions
494 * for returning to userspace (out).
495 */
496
497
498void kernel_to_ipc64_perm (struct kern_ipc_perm *in, struct ipc64_perm *out)
499{
500 out->key = in->key;
501 out->uid = in->uid;
502 out->gid = in->gid;
503 out->cuid = in->cuid;
504 out->cgid = in->cgid;
505 out->mode = in->mode;
506 out->seq = in->seq;
507}
508
509/**
510 * ipc64_perm_to_ipc_perm - convert old ipc permissions to new
511 * @in: new style IPC permissions
512 * @out: old style IPC permissions
513 *
514 * Turn the new style permissions object in into a compatibility
515 * object and store it into the 'out' pointer.
516 */
517
518void ipc64_perm_to_ipc_perm (struct ipc64_perm *in, struct ipc_perm *out)
519{
520 out->key = in->key;
521 SET_UID(out->uid, in->uid);
522 SET_GID(out->gid, in->gid);
523 SET_UID(out->cuid, in->cuid);
524 SET_GID(out->cgid, in->cgid);
525 out->mode = in->mode;
526 out->seq = in->seq;
527}
528
529/*
530 * So far only shm_get_stat() calls ipc_get() via shm_get(), so ipc_get()
531 * is called with shm_ids.sem locked. Since grow_ary() is also called with
532 * shm_ids.sem down(for Shared Memory), there is no need to add read
533 * barriers here to gurantee the writes in grow_ary() are seen in order
534 * here (for Alpha).
535 *
536 * However ipc_get() itself does not necessary require ipc_ids.sem down. So
537 * if in the future ipc_get() is used by other places without ipc_ids.sem
538 * down, then ipc_get() needs read memery barriers as ipc_lock() does.
539 */
540struct kern_ipc_perm* ipc_get(struct ipc_ids* ids, int id)
541{
542 struct kern_ipc_perm* out;
543 int lid = id % SEQ_MULTIPLIER;
544 if(lid >= ids->entries->size)
545 return NULL;
546 out = ids->entries->p[lid];
547 return out;
548}
549
550struct kern_ipc_perm* ipc_lock(struct ipc_ids* ids, int id)
551{
552 struct kern_ipc_perm* out;
553 int lid = id % SEQ_MULTIPLIER;
554 struct ipc_id_ary* entries;
555
556 rcu_read_lock();
557 entries = rcu_dereference(ids->entries);
558 if(lid >= entries->size) {
559 rcu_read_unlock();
560 return NULL;
561 }
562 out = entries->p[lid];
563 if(out == NULL) {
564 rcu_read_unlock();
565 return NULL;
566 }
567 spin_lock(&out->lock);
568
569 /* ipc_rmid() may have already freed the ID while ipc_lock
570 * was spinning: here verify that the structure is still valid
571 */
572 if (out->deleted) {
573 spin_unlock(&out->lock);
574 rcu_read_unlock();
575 return NULL;
576 }
577 return out;
578}
579
580void ipc_lock_by_ptr(struct kern_ipc_perm *perm)
581{
582 rcu_read_lock();
583 spin_lock(&perm->lock);
584}
585
586void ipc_unlock(struct kern_ipc_perm* perm)
587{
588 spin_unlock(&perm->lock);
589 rcu_read_unlock();
590}
591
592int ipc_buildid(struct ipc_ids* ids, int id, int seq)
593{
594 return SEQ_MULTIPLIER*seq + id;
595}
596
597int ipc_checkid(struct ipc_ids* ids, struct kern_ipc_perm* ipcp, int uid)
598{
599 if(uid/SEQ_MULTIPLIER != ipcp->seq)
600 return 1;
601 return 0;
602}
603
604#ifdef __ARCH_WANT_IPC_PARSE_VERSION
605
606
607/**
608 * ipc_parse_version - IPC call version
609 * @cmd: pointer to command
610 *
611 * Return IPC_64 for new style IPC and IPC_OLD for old style IPC.
612 * The cmd value is turned from an encoding command and version into
613 * just the command code.
614 */
615
616int ipc_parse_version (int *cmd)
617{
618 if (*cmd & IPC_64) {
619 *cmd ^= IPC_64;
620 return IPC_64;
621 } else {
622 return IPC_OLD;
623 }
624}
625
626#endif /* __ARCH_WANT_IPC_PARSE_VERSION */
Mike Waychisonae781772005-09-06 15:17:09 -0700627
628#ifdef CONFIG_PROC_FS
629static void *sysvipc_proc_next(struct seq_file *s, void *it, loff_t *pos)
630{
631 struct ipc_proc_iface *iface = s->private;
632 struct kern_ipc_perm *ipc = it;
633 loff_t p;
634
635 /* If we had an ipc id locked before, unlock it */
636 if (ipc && ipc != SEQ_START_TOKEN)
637 ipc_unlock(ipc);
638
639 /*
640 * p = *pos - 1 (because id 0 starts at position 1)
641 * + 1 (because we increment the position by one)
642 */
643 for (p = *pos; p <= iface->ids->max_id; p++) {
644 if ((ipc = ipc_lock(iface->ids, p)) != NULL) {
645 *pos = p + 1;
646 return ipc;
647 }
648 }
649
650 /* Out of range - return NULL to terminate iteration */
651 return NULL;
652}
653
654/*
655 * File positions: pos 0 -> header, pos n -> ipc id + 1.
656 * SeqFile iterator: iterator value locked shp or SEQ_TOKEN_START.
657 */
658static void *sysvipc_proc_start(struct seq_file *s, loff_t *pos)
659{
660 struct ipc_proc_iface *iface = s->private;
661 struct kern_ipc_perm *ipc;
662 loff_t p;
663
664 /*
665 * Take the lock - this will be released by the corresponding
666 * call to stop().
667 */
668 down(&iface->ids->sem);
669
670 /* pos < 0 is invalid */
671 if (*pos < 0)
672 return NULL;
673
674 /* pos == 0 means header */
675 if (*pos == 0)
676 return SEQ_START_TOKEN;
677
678 /* Find the (pos-1)th ipc */
679 for (p = *pos - 1; p <= iface->ids->max_id; p++) {
680 if ((ipc = ipc_lock(iface->ids, p)) != NULL) {
681 *pos = p + 1;
682 return ipc;
683 }
684 }
685 return NULL;
686}
687
688static void sysvipc_proc_stop(struct seq_file *s, void *it)
689{
690 struct kern_ipc_perm *ipc = it;
691 struct ipc_proc_iface *iface = s->private;
692
693 /* If we had a locked segment, release it */
694 if (ipc && ipc != SEQ_START_TOKEN)
695 ipc_unlock(ipc);
696
697 /* Release the lock we took in start() */
698 up(&iface->ids->sem);
699}
700
701static int sysvipc_proc_show(struct seq_file *s, void *it)
702{
703 struct ipc_proc_iface *iface = s->private;
704
705 if (it == SEQ_START_TOKEN)
706 return seq_puts(s, iface->header);
707
708 return iface->show(s, it);
709}
710
711static struct seq_operations sysvipc_proc_seqops = {
712 .start = sysvipc_proc_start,
713 .stop = sysvipc_proc_stop,
714 .next = sysvipc_proc_next,
715 .show = sysvipc_proc_show,
716};
717
718static int sysvipc_proc_open(struct inode *inode, struct file *file) {
719 int ret;
720 struct seq_file *seq;
721
722 ret = seq_open(file, &sysvipc_proc_seqops);
723 if (!ret) {
724 seq = file->private_data;
725 seq->private = PDE(inode)->data;
726 }
727 return ret;
728}
729
730static struct file_operations sysvipc_proc_fops = {
731 .open = sysvipc_proc_open,
732 .read = seq_read,
733 .llseek = seq_lseek,
734 .release = seq_release,
735};
736#endif /* CONFIG_PROC_FS */