3 * Copyright (C) 1992 Krishna Balasubramanian
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 <manfred@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 * Mar 2006 - support for audit of ipc object properties
14 * Dustin Kirkland <dustin.kirkland@us.ibm.com>
15 * Jun 2006 - namespaces ssupport
17 * Pavel Emelianov <xemul@openvz.org>
21 #include <linux/shm.h>
22 #include <linux/init.h>
23 #include <linux/msg.h>
24 #include <linux/vmalloc.h>
25 #include <linux/slab.h>
26 #include <linux/capability.h>
27 #include <linux/highuid.h>
28 #include <linux/security.h>
29 #include <linux/rcupdate.h>
30 #include <linux/workqueue.h>
31 #include <linux/seq_file.h>
32 #include <linux/proc_fs.h>
33 #include <linux/audit.h>
34 #include <linux/nsproxy.h>
36 #include <asm/unistd.h>
40 struct ipc_proc_iface {
44 int (*show)(struct seq_file *, void *);
47 struct ipc_namespace init_ipc_ns = {
49 .refcount = ATOMIC_INIT(2),
53 static struct ipc_namespace *clone_ipc_ns(struct ipc_namespace *old_ns)
56 struct ipc_namespace *ns;
59 ns = kmalloc(sizeof(struct ipc_namespace), GFP_KERNEL);
63 err = sem_init_ns(ns);
66 err = msg_init_ns(ns);
69 err = shm_init_ns(ns);
86 struct ipc_namespace *copy_ipcs(unsigned long flags, struct ipc_namespace *ns)
88 struct ipc_namespace *new_ns;
93 if (!(flags & CLONE_NEWIPC))
96 new_ns = clone_ipc_ns(ns);
102 void free_ipc_ns(struct kref *kref)
104 struct ipc_namespace *ns;
106 ns = container_of(kref, struct ipc_namespace, kref);
114 * ipc_init - initialise IPC subsystem
116 * The various system5 IPC resources (semaphores, messages and shared
117 * memory) are initialised
120 static int __init ipc_init(void)
127 __initcall(ipc_init);
130 * ipc_init_ids - initialise IPC identifiers
131 * @ids: Identifier set
132 * @size: Number of identifiers
134 * Given a size for the ipc identifier range (limited below IPCMNI)
135 * set up the sequence range to use then allocate and initialise the
139 void ipc_init_ids(struct ipc_ids* ids, int size)
143 mutex_init(&ids->mutex);
151 int seq_limit = INT_MAX/SEQ_MULTIPLIER;
152 if(seq_limit > USHRT_MAX)
153 ids->seq_max = USHRT_MAX;
155 ids->seq_max = seq_limit;
158 ids->entries = ipc_rcu_alloc(sizeof(struct kern_ipc_perm *)*size +
159 sizeof(struct ipc_id_ary));
161 if(ids->entries == NULL) {
162 printk(KERN_ERR "ipc_init_ids() failed, ipc service disabled.\n");
164 ids->entries = &ids->nullentry;
166 ids->entries->size = size;
168 ids->entries->p[i] = NULL;
171 #ifdef CONFIG_PROC_FS
172 static const struct file_operations sysvipc_proc_fops;
174 * ipc_init_proc_interface - Create a proc interface for sysipc types using a seq_file interface.
175 * @path: Path in procfs
176 * @header: Banner to be printed at the beginning of the file.
177 * @ids: ipc id table to iterate.
178 * @show: show routine.
180 void __init ipc_init_proc_interface(const char *path, const char *header,
181 int ids, int (*show)(struct seq_file *, void *))
183 struct proc_dir_entry *pde;
184 struct ipc_proc_iface *iface;
186 iface = kmalloc(sizeof(*iface), GFP_KERNEL);
190 iface->header = header;
194 pde = create_proc_entry(path,
195 S_IRUGO, /* world readable */
196 NULL /* parent dir */);
199 pde->proc_fops = &sysvipc_proc_fops;
207 * ipc_findkey - find a key in an ipc identifier set
208 * @ids: Identifier set
209 * @key: The key to find
211 * Requires ipc_ids.mutex locked.
212 * Returns the identifier if found or -1 if not.
215 int ipc_findkey(struct ipc_ids* ids, key_t key)
218 struct kern_ipc_perm* p;
219 int max_id = ids->max_id;
222 * rcu_dereference() is not needed here
223 * since ipc_ids.mutex is held
225 for (id = 0; id <= max_id; id++) {
226 p = ids->entries->p[id];
236 * Requires ipc_ids.mutex locked
238 static int grow_ary(struct ipc_ids* ids, int newsize)
240 struct ipc_id_ary* new;
241 struct ipc_id_ary* old;
243 int size = ids->entries->size;
250 new = ipc_rcu_alloc(sizeof(struct kern_ipc_perm *)*newsize +
251 sizeof(struct ipc_id_ary));
255 memcpy(new->p, ids->entries->p, sizeof(struct kern_ipc_perm *)*size);
256 for(i=size;i<newsize;i++) {
262 * Use rcu_assign_pointer() to make sure the memcpyed contents
263 * of the new array are visible before the new array becomes visible.
265 rcu_assign_pointer(ids->entries, new);
267 __ipc_fini_ids(ids, old);
272 * ipc_addid - add an IPC identifier
273 * @ids: IPC identifier set
274 * @new: new IPC permission set
275 * @size: new size limit for the id array
277 * Add an entry 'new' to the IPC arrays. The permissions object is
278 * initialised and the first free entry is set up and the id assigned
279 * is returned. The list is returned in a locked state on success.
280 * On failure the list is not locked and -1 is returned.
282 * Called with ipc_ids.mutex held.
285 int ipc_addid(struct ipc_ids* ids, struct kern_ipc_perm* new, int size)
289 size = grow_ary(ids,size);
292 * rcu_dereference()() is not needed here since
293 * ipc_ids.mutex is held
295 for (id = 0; id < size; id++) {
296 if(ids->entries->p[id] == NULL)
302 if (id > ids->max_id)
305 new->cuid = new->uid = current->euid;
306 new->gid = new->cgid = current->egid;
308 new->seq = ids->seq++;
309 if(ids->seq > ids->seq_max)
312 spin_lock_init(&new->lock);
315 spin_lock(&new->lock);
316 ids->entries->p[id] = new;
321 * ipc_rmid - remove an IPC identifier
322 * @ids: identifier set
323 * @id: Identifier to remove
325 * The identifier must be valid, and in use. The kernel will panic if
326 * fed an invalid identifier. The entry is removed and internal
327 * variables recomputed. The object associated with the identifier
329 * ipc_ids.mutex and the spinlock for this ID is hold before this function
330 * is called, and remain locked on the exit.
333 struct kern_ipc_perm* ipc_rmid(struct ipc_ids* ids, int id)
335 struct kern_ipc_perm* p;
336 int lid = id % SEQ_MULTIPLIER;
337 BUG_ON(lid >= ids->entries->size);
340 * do not need a rcu_dereference()() here to force ordering
341 * on Alpha, since the ipc_ids.mutex is held.
343 p = ids->entries->p[lid];
344 ids->entries->p[lid] = NULL;
348 if (lid == ids->max_id) {
353 } while (ids->entries->p[lid] == NULL);
361 * ipc_alloc - allocate ipc space
362 * @size: size desired
364 * Allocate memory from the appropriate pools and return a pointer to it.
365 * NULL is returned if the allocation fails
368 void* ipc_alloc(int size)
374 out = kmalloc(size, GFP_KERNEL);
379 * ipc_free - free ipc space
380 * @ptr: pointer returned by ipc_alloc
381 * @size: size of block
383 * Free a block created with ipc_alloc(). The caller must know the size
384 * used in the allocation call.
387 void ipc_free(void* ptr, int size)
397 * There are three headers that are prepended to the actual allocation:
398 * - during use: ipc_rcu_hdr.
399 * - during the rcu grace period: ipc_rcu_grace.
400 * - [only if vmalloc]: ipc_rcu_sched.
401 * Their lifetime doesn't overlap, thus the headers share the same memory.
402 * Unlike a normal union, they are right-aligned, thus some container_of
403 * forward/backward casting is necessary:
416 /* "void *" makes sure alignment of following data is sane. */
422 struct work_struct work;
423 /* "void *" makes sure alignment of following data is sane. */
427 #define HDRLEN_KMALLOC (sizeof(struct ipc_rcu_grace) > sizeof(struct ipc_rcu_hdr) ? \
428 sizeof(struct ipc_rcu_grace) : sizeof(struct ipc_rcu_hdr))
429 #define HDRLEN_VMALLOC (sizeof(struct ipc_rcu_sched) > HDRLEN_KMALLOC ? \
430 sizeof(struct ipc_rcu_sched) : HDRLEN_KMALLOC)
432 static inline int rcu_use_vmalloc(int size)
434 /* Too big for a single page? */
435 if (HDRLEN_KMALLOC + size > PAGE_SIZE)
441 * ipc_rcu_alloc - allocate ipc and rcu space
442 * @size: size desired
444 * Allocate memory for the rcu header structure + the object.
445 * Returns the pointer to the object.
446 * NULL is returned if the allocation fails.
449 void* ipc_rcu_alloc(int size)
453 * We prepend the allocation with the rcu struct, and
454 * workqueue if necessary (for vmalloc).
456 if (rcu_use_vmalloc(size)) {
457 out = vmalloc(HDRLEN_VMALLOC + size);
459 out += HDRLEN_VMALLOC;
460 container_of(out, struct ipc_rcu_hdr, data)->is_vmalloc = 1;
461 container_of(out, struct ipc_rcu_hdr, data)->refcount = 1;
464 out = kmalloc(HDRLEN_KMALLOC + size, GFP_KERNEL);
466 out += HDRLEN_KMALLOC;
467 container_of(out, struct ipc_rcu_hdr, data)->is_vmalloc = 0;
468 container_of(out, struct ipc_rcu_hdr, data)->refcount = 1;
475 void ipc_rcu_getref(void *ptr)
477 container_of(ptr, struct ipc_rcu_hdr, data)->refcount++;
480 static void ipc_do_vfree(struct work_struct *work)
482 vfree(container_of(work, struct ipc_rcu_sched, work));
486 * ipc_schedule_free - free ipc + rcu space
487 * @head: RCU callback structure for queued work
489 * Since RCU callback function is called in bh,
490 * we need to defer the vfree to schedule_work().
492 static void ipc_schedule_free(struct rcu_head *head)
494 struct ipc_rcu_grace *grace =
495 container_of(head, struct ipc_rcu_grace, rcu);
496 struct ipc_rcu_sched *sched =
497 container_of(&(grace->data[0]), struct ipc_rcu_sched, data[0]);
499 INIT_WORK(&sched->work, ipc_do_vfree);
500 schedule_work(&sched->work);
504 * ipc_immediate_free - free ipc + rcu space
505 * @head: RCU callback structure that contains pointer to be freed
507 * Free from the RCU callback context.
509 static void ipc_immediate_free(struct rcu_head *head)
511 struct ipc_rcu_grace *free =
512 container_of(head, struct ipc_rcu_grace, rcu);
516 void ipc_rcu_putref(void *ptr)
518 if (--container_of(ptr, struct ipc_rcu_hdr, data)->refcount > 0)
521 if (container_of(ptr, struct ipc_rcu_hdr, data)->is_vmalloc) {
522 call_rcu(&container_of(ptr, struct ipc_rcu_grace, data)->rcu,
525 call_rcu(&container_of(ptr, struct ipc_rcu_grace, data)->rcu,
531 * ipcperms - check IPC permissions
532 * @ipcp: IPC permission set
533 * @flag: desired permission set.
535 * Check user, group, other permissions for access
536 * to ipc resources. return 0 if allowed
539 int ipcperms (struct kern_ipc_perm *ipcp, short flag)
540 { /* flag will most probably be 0 or S_...UGO from <linux/stat.h> */
541 int requested_mode, granted_mode, err;
543 if (unlikely((err = audit_ipc_obj(ipcp))))
545 requested_mode = (flag >> 6) | (flag >> 3) | flag;
546 granted_mode = ipcp->mode;
547 if (current->euid == ipcp->cuid || current->euid == ipcp->uid)
549 else if (in_group_p(ipcp->cgid) || in_group_p(ipcp->gid))
551 /* is there some bit set in requested_mode but not in granted_mode? */
552 if ((requested_mode & ~granted_mode & 0007) &&
553 !capable(CAP_IPC_OWNER))
556 return security_ipc_permission(ipcp, flag);
560 * Functions to convert between the kern_ipc_perm structure and the
561 * old/new ipc_perm structures
565 * kernel_to_ipc64_perm - convert kernel ipc permissions to user
566 * @in: kernel permissions
567 * @out: new style IPC permissions
569 * Turn the kernel object @in into a set of permissions descriptions
570 * for returning to userspace (@out).
574 void kernel_to_ipc64_perm (struct kern_ipc_perm *in, struct ipc64_perm *out)
579 out->cuid = in->cuid;
580 out->cgid = in->cgid;
581 out->mode = in->mode;
586 * ipc64_perm_to_ipc_perm - convert old ipc permissions to new
587 * @in: new style IPC permissions
588 * @out: old style IPC permissions
590 * Turn the new style permissions object @in into a compatibility
591 * object and store it into the @out pointer.
594 void ipc64_perm_to_ipc_perm (struct ipc64_perm *in, struct ipc_perm *out)
597 SET_UID(out->uid, in->uid);
598 SET_GID(out->gid, in->gid);
599 SET_UID(out->cuid, in->cuid);
600 SET_GID(out->cgid, in->cgid);
601 out->mode = in->mode;
606 * So far only shm_get_stat() calls ipc_get() via shm_get(), so ipc_get()
607 * is called with shm_ids.mutex locked. Since grow_ary() is also called with
608 * shm_ids.mutex down(for Shared Memory), there is no need to add read
609 * barriers here to gurantee the writes in grow_ary() are seen in order
612 * However ipc_get() itself does not necessary require ipc_ids.mutex down. So
613 * if in the future ipc_get() is used by other places without ipc_ids.mutex
614 * down, then ipc_get() needs read memery barriers as ipc_lock() does.
616 struct kern_ipc_perm* ipc_get(struct ipc_ids* ids, int id)
618 struct kern_ipc_perm* out;
619 int lid = id % SEQ_MULTIPLIER;
620 if(lid >= ids->entries->size)
622 out = ids->entries->p[lid];
626 struct kern_ipc_perm* ipc_lock(struct ipc_ids* ids, int id)
628 struct kern_ipc_perm* out;
629 int lid = id % SEQ_MULTIPLIER;
630 struct ipc_id_ary* entries;
633 entries = rcu_dereference(ids->entries);
634 if(lid >= entries->size) {
638 out = entries->p[lid];
643 spin_lock(&out->lock);
645 /* ipc_rmid() may have already freed the ID while ipc_lock
646 * was spinning: here verify that the structure is still valid
649 spin_unlock(&out->lock);
656 void ipc_lock_by_ptr(struct kern_ipc_perm *perm)
659 spin_lock(&perm->lock);
662 void ipc_unlock(struct kern_ipc_perm* perm)
664 spin_unlock(&perm->lock);
668 int ipc_buildid(struct ipc_ids* ids, int id, int seq)
670 return SEQ_MULTIPLIER*seq + id;
673 int ipc_checkid(struct ipc_ids* ids, struct kern_ipc_perm* ipcp, int uid)
675 if(uid/SEQ_MULTIPLIER != ipcp->seq)
680 #ifdef __ARCH_WANT_IPC_PARSE_VERSION
684 * ipc_parse_version - IPC call version
685 * @cmd: pointer to command
687 * Return IPC_64 for new style IPC and IPC_OLD for old style IPC.
688 * The @cmd value is turned from an encoding command and version into
689 * just the command code.
692 int ipc_parse_version (int *cmd)
702 #endif /* __ARCH_WANT_IPC_PARSE_VERSION */
704 #ifdef CONFIG_PROC_FS
705 struct ipc_proc_iter {
706 struct ipc_namespace *ns;
707 struct ipc_proc_iface *iface;
710 static void *sysvipc_proc_next(struct seq_file *s, void *it, loff_t *pos)
712 struct ipc_proc_iter *iter = s->private;
713 struct ipc_proc_iface *iface = iter->iface;
714 struct kern_ipc_perm *ipc = it;
718 ids = iter->ns->ids[iface->ids];
720 /* If we had an ipc id locked before, unlock it */
721 if (ipc && ipc != SEQ_START_TOKEN)
725 * p = *pos - 1 (because id 0 starts at position 1)
726 * + 1 (because we increment the position by one)
728 for (p = *pos; p <= ids->max_id; p++) {
729 if ((ipc = ipc_lock(ids, p)) != NULL) {
735 /* Out of range - return NULL to terminate iteration */
740 * File positions: pos 0 -> header, pos n -> ipc id + 1.
741 * SeqFile iterator: iterator value locked shp or SEQ_TOKEN_START.
743 static void *sysvipc_proc_start(struct seq_file *s, loff_t *pos)
745 struct ipc_proc_iter *iter = s->private;
746 struct ipc_proc_iface *iface = iter->iface;
747 struct kern_ipc_perm *ipc;
751 ids = iter->ns->ids[iface->ids];
754 * Take the lock - this will be released by the corresponding
757 mutex_lock(&ids->mutex);
759 /* pos < 0 is invalid */
763 /* pos == 0 means header */
765 return SEQ_START_TOKEN;
767 /* Find the (pos-1)th ipc */
768 for (p = *pos - 1; p <= ids->max_id; p++) {
769 if ((ipc = ipc_lock(ids, p)) != NULL) {
777 static void sysvipc_proc_stop(struct seq_file *s, void *it)
779 struct kern_ipc_perm *ipc = it;
780 struct ipc_proc_iter *iter = s->private;
781 struct ipc_proc_iface *iface = iter->iface;
784 /* If we had a locked segment, release it */
785 if (ipc && ipc != SEQ_START_TOKEN)
788 ids = iter->ns->ids[iface->ids];
789 /* Release the lock we took in start() */
790 mutex_unlock(&ids->mutex);
793 static int sysvipc_proc_show(struct seq_file *s, void *it)
795 struct ipc_proc_iter *iter = s->private;
796 struct ipc_proc_iface *iface = iter->iface;
798 if (it == SEQ_START_TOKEN)
799 return seq_puts(s, iface->header);
801 return iface->show(s, it);
804 static struct seq_operations sysvipc_proc_seqops = {
805 .start = sysvipc_proc_start,
806 .stop = sysvipc_proc_stop,
807 .next = sysvipc_proc_next,
808 .show = sysvipc_proc_show,
811 static int sysvipc_proc_open(struct inode *inode, struct file *file)
814 struct seq_file *seq;
815 struct ipc_proc_iter *iter;
818 iter = kmalloc(sizeof(*iter), GFP_KERNEL);
822 ret = seq_open(file, &sysvipc_proc_seqops);
826 seq = file->private_data;
829 iter->iface = PDE(inode)->data;
830 iter->ns = get_ipc_ns(current->nsproxy->ipc_ns);
838 static int sysvipc_proc_release(struct inode *inode, struct file *file)
840 struct seq_file *seq = file->private_data;
841 struct ipc_proc_iter *iter = seq->private;
842 put_ipc_ns(iter->ns);
843 return seq_release_private(inode, file);
846 static const struct file_operations sysvipc_proc_fops = {
847 .open = sysvipc_proc_open,
850 .release = sysvipc_proc_release,
852 #endif /* CONFIG_PROC_FS */