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