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),
54 static struct ipc_namespace *clone_ipc_ns(struct ipc_namespace *old_ns)
57 struct ipc_namespace *ns;
60 ns = kmalloc(sizeof(struct ipc_namespace), GFP_KERNEL);
64 err = sem_init_ns(ns);
67 err = msg_init_ns(ns);
70 err = shm_init_ns(ns);
87 struct ipc_namespace *copy_ipcs(unsigned long flags, struct ipc_namespace *ns)
89 struct ipc_namespace *new_ns;
94 if (!(flags & CLONE_NEWIPC))
97 new_ns = clone_ipc_ns(ns);
103 void free_ipc_ns(struct kref *kref)
105 struct ipc_namespace *ns;
107 ns = container_of(kref, struct ipc_namespace, kref);
114 struct ipc_namespace *copy_ipcs(unsigned long flags, struct ipc_namespace *ns)
116 if (flags & CLONE_NEWIPC)
117 return ERR_PTR(-EINVAL);
123 * ipc_init - initialise IPC subsystem
125 * The various system5 IPC resources (semaphores, messages and shared
126 * memory) are initialised
129 static int __init ipc_init(void)
136 __initcall(ipc_init);
139 * ipc_init_ids - initialise IPC identifiers
140 * @ids: Identifier set
141 * @size: Number of identifiers
143 * Given a size for the ipc identifier range (limited below IPCMNI)
144 * set up the sequence range to use then allocate and initialise the
148 void __ipc_init ipc_init_ids(struct ipc_ids* ids, int size)
152 mutex_init(&ids->mutex);
160 int seq_limit = INT_MAX/SEQ_MULTIPLIER;
161 if(seq_limit > USHRT_MAX)
162 ids->seq_max = USHRT_MAX;
164 ids->seq_max = seq_limit;
167 ids->entries = ipc_rcu_alloc(sizeof(struct kern_ipc_perm *)*size +
168 sizeof(struct ipc_id_ary));
170 if(ids->entries == NULL) {
171 printk(KERN_ERR "ipc_init_ids() failed, ipc service disabled.\n");
173 ids->entries = &ids->nullentry;
175 ids->entries->size = size;
177 ids->entries->p[i] = NULL;
180 #ifdef CONFIG_PROC_FS
181 static const struct file_operations sysvipc_proc_fops;
183 * ipc_init_proc_interface - Create a proc interface for sysipc types using a seq_file interface.
184 * @path: Path in procfs
185 * @header: Banner to be printed at the beginning of the file.
186 * @ids: ipc id table to iterate.
187 * @show: show routine.
189 void __init ipc_init_proc_interface(const char *path, const char *header,
190 int ids, int (*show)(struct seq_file *, void *))
192 struct proc_dir_entry *pde;
193 struct ipc_proc_iface *iface;
195 iface = kmalloc(sizeof(*iface), GFP_KERNEL);
199 iface->header = header;
203 pde = create_proc_entry(path,
204 S_IRUGO, /* world readable */
205 NULL /* parent dir */);
208 pde->proc_fops = &sysvipc_proc_fops;
216 * ipc_findkey - find a key in an ipc identifier set
217 * @ids: Identifier set
218 * @key: The key to find
220 * Requires ipc_ids.mutex locked.
221 * Returns the identifier if found or -1 if not.
224 int ipc_findkey(struct ipc_ids* ids, key_t key)
227 struct kern_ipc_perm* p;
228 int max_id = ids->max_id;
231 * rcu_dereference() is not needed here
232 * since ipc_ids.mutex is held
234 for (id = 0; id <= max_id; id++) {
235 p = ids->entries->p[id];
245 * Requires ipc_ids.mutex locked
247 static int grow_ary(struct ipc_ids* ids, int newsize)
249 struct ipc_id_ary* new;
250 struct ipc_id_ary* old;
252 int size = ids->entries->size;
259 new = ipc_rcu_alloc(sizeof(struct kern_ipc_perm *)*newsize +
260 sizeof(struct ipc_id_ary));
264 memcpy(new->p, ids->entries->p, sizeof(struct kern_ipc_perm *)*size);
265 for(i=size;i<newsize;i++) {
271 * Use rcu_assign_pointer() to make sure the memcpyed contents
272 * of the new array are visible before the new array becomes visible.
274 rcu_assign_pointer(ids->entries, new);
276 __ipc_fini_ids(ids, old);
281 * ipc_addid - add an IPC identifier
282 * @ids: IPC identifier set
283 * @new: new IPC permission set
284 * @size: new size limit for the id array
286 * Add an entry 'new' to the IPC arrays. The permissions object is
287 * initialised and the first free entry is set up and the id assigned
288 * is returned. The list is returned in a locked state on success.
289 * On failure the list is not locked and -1 is returned.
291 * Called with ipc_ids.mutex held.
294 int ipc_addid(struct ipc_ids* ids, struct kern_ipc_perm* new, int size)
298 size = grow_ary(ids,size);
301 * rcu_dereference()() is not needed here since
302 * ipc_ids.mutex is held
304 for (id = 0; id < size; id++) {
305 if(ids->entries->p[id] == NULL)
311 if (id > ids->max_id)
314 new->cuid = new->uid = current->euid;
315 new->gid = new->cgid = current->egid;
317 new->seq = ids->seq++;
318 if(ids->seq > ids->seq_max)
321 spin_lock_init(&new->lock);
324 spin_lock(&new->lock);
325 ids->entries->p[id] = new;
330 * ipc_rmid - remove an IPC identifier
331 * @ids: identifier set
332 * @id: Identifier to remove
334 * The identifier must be valid, and in use. The kernel will panic if
335 * fed an invalid identifier. The entry is removed and internal
336 * variables recomputed. The object associated with the identifier
338 * ipc_ids.mutex and the spinlock for this ID is hold before this function
339 * is called, and remain locked on the exit.
342 struct kern_ipc_perm* ipc_rmid(struct ipc_ids* ids, int id)
344 struct kern_ipc_perm* p;
345 int lid = id % SEQ_MULTIPLIER;
346 BUG_ON(lid >= ids->entries->size);
349 * do not need a rcu_dereference()() here to force ordering
350 * on Alpha, since the ipc_ids.mutex is held.
352 p = ids->entries->p[lid];
353 ids->entries->p[lid] = NULL;
357 if (lid == ids->max_id) {
362 } while (ids->entries->p[lid] == NULL);
370 * ipc_alloc - allocate ipc space
371 * @size: size desired
373 * Allocate memory from the appropriate pools and return a pointer to it.
374 * NULL is returned if the allocation fails
377 void* ipc_alloc(int size)
383 out = kmalloc(size, GFP_KERNEL);
388 * ipc_free - free ipc space
389 * @ptr: pointer returned by ipc_alloc
390 * @size: size of block
392 * Free a block created with ipc_alloc(). The caller must know the size
393 * used in the allocation call.
396 void ipc_free(void* ptr, int size)
406 * There are three headers that are prepended to the actual allocation:
407 * - during use: ipc_rcu_hdr.
408 * - during the rcu grace period: ipc_rcu_grace.
409 * - [only if vmalloc]: ipc_rcu_sched.
410 * Their lifetime doesn't overlap, thus the headers share the same memory.
411 * Unlike a normal union, they are right-aligned, thus some container_of
412 * forward/backward casting is necessary:
425 /* "void *" makes sure alignment of following data is sane. */
431 struct work_struct work;
432 /* "void *" makes sure alignment of following data is sane. */
436 #define HDRLEN_KMALLOC (sizeof(struct ipc_rcu_grace) > sizeof(struct ipc_rcu_hdr) ? \
437 sizeof(struct ipc_rcu_grace) : sizeof(struct ipc_rcu_hdr))
438 #define HDRLEN_VMALLOC (sizeof(struct ipc_rcu_sched) > HDRLEN_KMALLOC ? \
439 sizeof(struct ipc_rcu_sched) : HDRLEN_KMALLOC)
441 static inline int rcu_use_vmalloc(int size)
443 /* Too big for a single page? */
444 if (HDRLEN_KMALLOC + size > PAGE_SIZE)
450 * ipc_rcu_alloc - allocate ipc and rcu space
451 * @size: size desired
453 * Allocate memory for the rcu header structure + the object.
454 * Returns the pointer to the object.
455 * NULL is returned if the allocation fails.
458 void* ipc_rcu_alloc(int size)
462 * We prepend the allocation with the rcu struct, and
463 * workqueue if necessary (for vmalloc).
465 if (rcu_use_vmalloc(size)) {
466 out = vmalloc(HDRLEN_VMALLOC + size);
468 out += HDRLEN_VMALLOC;
469 container_of(out, struct ipc_rcu_hdr, data)->is_vmalloc = 1;
470 container_of(out, struct ipc_rcu_hdr, data)->refcount = 1;
473 out = kmalloc(HDRLEN_KMALLOC + size, GFP_KERNEL);
475 out += HDRLEN_KMALLOC;
476 container_of(out, struct ipc_rcu_hdr, data)->is_vmalloc = 0;
477 container_of(out, struct ipc_rcu_hdr, data)->refcount = 1;
484 void ipc_rcu_getref(void *ptr)
486 container_of(ptr, struct ipc_rcu_hdr, data)->refcount++;
489 static void ipc_do_vfree(struct work_struct *work)
491 vfree(container_of(work, struct ipc_rcu_sched, work));
495 * ipc_schedule_free - free ipc + rcu space
496 * @head: RCU callback structure for queued work
498 * Since RCU callback function is called in bh,
499 * we need to defer the vfree to schedule_work().
501 static void ipc_schedule_free(struct rcu_head *head)
503 struct ipc_rcu_grace *grace =
504 container_of(head, struct ipc_rcu_grace, rcu);
505 struct ipc_rcu_sched *sched =
506 container_of(&(grace->data[0]), struct ipc_rcu_sched, data[0]);
508 INIT_WORK(&sched->work, ipc_do_vfree);
509 schedule_work(&sched->work);
513 * ipc_immediate_free - free ipc + rcu space
514 * @head: RCU callback structure that contains pointer to be freed
516 * Free from the RCU callback context.
518 static void ipc_immediate_free(struct rcu_head *head)
520 struct ipc_rcu_grace *free =
521 container_of(head, struct ipc_rcu_grace, rcu);
525 void ipc_rcu_putref(void *ptr)
527 if (--container_of(ptr, struct ipc_rcu_hdr, data)->refcount > 0)
530 if (container_of(ptr, struct ipc_rcu_hdr, data)->is_vmalloc) {
531 call_rcu(&container_of(ptr, struct ipc_rcu_grace, data)->rcu,
534 call_rcu(&container_of(ptr, struct ipc_rcu_grace, data)->rcu,
540 * ipcperms - check IPC permissions
541 * @ipcp: IPC permission set
542 * @flag: desired permission set.
544 * Check user, group, other permissions for access
545 * to ipc resources. return 0 if allowed
548 int ipcperms (struct kern_ipc_perm *ipcp, short flag)
549 { /* flag will most probably be 0 or S_...UGO from <linux/stat.h> */
550 int requested_mode, granted_mode, err;
552 if (unlikely((err = audit_ipc_obj(ipcp))))
554 requested_mode = (flag >> 6) | (flag >> 3) | flag;
555 granted_mode = ipcp->mode;
556 if (current->euid == ipcp->cuid || current->euid == ipcp->uid)
558 else if (in_group_p(ipcp->cgid) || in_group_p(ipcp->gid))
560 /* is there some bit set in requested_mode but not in granted_mode? */
561 if ((requested_mode & ~granted_mode & 0007) &&
562 !capable(CAP_IPC_OWNER))
565 return security_ipc_permission(ipcp, flag);
569 * Functions to convert between the kern_ipc_perm structure and the
570 * old/new ipc_perm structures
574 * kernel_to_ipc64_perm - convert kernel ipc permissions to user
575 * @in: kernel permissions
576 * @out: new style IPC permissions
578 * Turn the kernel object @in into a set of permissions descriptions
579 * for returning to userspace (@out).
583 void kernel_to_ipc64_perm (struct kern_ipc_perm *in, struct ipc64_perm *out)
588 out->cuid = in->cuid;
589 out->cgid = in->cgid;
590 out->mode = in->mode;
595 * ipc64_perm_to_ipc_perm - convert old ipc permissions to new
596 * @in: new style IPC permissions
597 * @out: old style IPC permissions
599 * Turn the new style permissions object @in into a compatibility
600 * object and store it into the @out pointer.
603 void ipc64_perm_to_ipc_perm (struct ipc64_perm *in, struct ipc_perm *out)
606 SET_UID(out->uid, in->uid);
607 SET_GID(out->gid, in->gid);
608 SET_UID(out->cuid, in->cuid);
609 SET_GID(out->cgid, in->cgid);
610 out->mode = in->mode;
615 * So far only shm_get_stat() calls ipc_get() via shm_get(), so ipc_get()
616 * is called with shm_ids.mutex locked. Since grow_ary() is also called with
617 * shm_ids.mutex down(for Shared Memory), there is no need to add read
618 * barriers here to gurantee the writes in grow_ary() are seen in order
621 * However ipc_get() itself does not necessary require ipc_ids.mutex down. So
622 * if in the future ipc_get() is used by other places without ipc_ids.mutex
623 * down, then ipc_get() needs read memery barriers as ipc_lock() does.
625 struct kern_ipc_perm* ipc_get(struct ipc_ids* ids, int id)
627 struct kern_ipc_perm* out;
628 int lid = id % SEQ_MULTIPLIER;
629 if(lid >= ids->entries->size)
631 out = ids->entries->p[lid];
635 struct kern_ipc_perm* ipc_lock(struct ipc_ids* ids, int id)
637 struct kern_ipc_perm* out;
638 int lid = id % SEQ_MULTIPLIER;
639 struct ipc_id_ary* entries;
642 entries = rcu_dereference(ids->entries);
643 if(lid >= entries->size) {
647 out = entries->p[lid];
652 spin_lock(&out->lock);
654 /* ipc_rmid() may have already freed the ID while ipc_lock
655 * was spinning: here verify that the structure is still valid
658 spin_unlock(&out->lock);
665 void ipc_lock_by_ptr(struct kern_ipc_perm *perm)
668 spin_lock(&perm->lock);
671 void ipc_unlock(struct kern_ipc_perm* perm)
673 spin_unlock(&perm->lock);
677 int ipc_buildid(struct ipc_ids* ids, int id, int seq)
679 return SEQ_MULTIPLIER*seq + id;
682 int ipc_checkid(struct ipc_ids* ids, struct kern_ipc_perm* ipcp, int uid)
684 if(uid/SEQ_MULTIPLIER != ipcp->seq)
689 #ifdef __ARCH_WANT_IPC_PARSE_VERSION
693 * ipc_parse_version - IPC call version
694 * @cmd: pointer to command
696 * Return IPC_64 for new style IPC and IPC_OLD for old style IPC.
697 * The @cmd value is turned from an encoding command and version into
698 * just the command code.
701 int ipc_parse_version (int *cmd)
711 #endif /* __ARCH_WANT_IPC_PARSE_VERSION */
713 #ifdef CONFIG_PROC_FS
714 struct ipc_proc_iter {
715 struct ipc_namespace *ns;
716 struct ipc_proc_iface *iface;
719 static void *sysvipc_proc_next(struct seq_file *s, void *it, loff_t *pos)
721 struct ipc_proc_iter *iter = s->private;
722 struct ipc_proc_iface *iface = iter->iface;
723 struct kern_ipc_perm *ipc = it;
727 ids = iter->ns->ids[iface->ids];
729 /* If we had an ipc id locked before, unlock it */
730 if (ipc && ipc != SEQ_START_TOKEN)
734 * p = *pos - 1 (because id 0 starts at position 1)
735 * + 1 (because we increment the position by one)
737 for (p = *pos; p <= ids->max_id; p++) {
738 if ((ipc = ipc_lock(ids, p)) != NULL) {
744 /* Out of range - return NULL to terminate iteration */
749 * File positions: pos 0 -> header, pos n -> ipc id + 1.
750 * SeqFile iterator: iterator value locked shp or SEQ_TOKEN_START.
752 static void *sysvipc_proc_start(struct seq_file *s, loff_t *pos)
754 struct ipc_proc_iter *iter = s->private;
755 struct ipc_proc_iface *iface = iter->iface;
756 struct kern_ipc_perm *ipc;
760 ids = iter->ns->ids[iface->ids];
763 * Take the lock - this will be released by the corresponding
766 mutex_lock(&ids->mutex);
768 /* pos < 0 is invalid */
772 /* pos == 0 means header */
774 return SEQ_START_TOKEN;
776 /* Find the (pos-1)th ipc */
777 for (p = *pos - 1; p <= ids->max_id; p++) {
778 if ((ipc = ipc_lock(ids, p)) != NULL) {
786 static void sysvipc_proc_stop(struct seq_file *s, void *it)
788 struct kern_ipc_perm *ipc = it;
789 struct ipc_proc_iter *iter = s->private;
790 struct ipc_proc_iface *iface = iter->iface;
793 /* If we had a locked segment, release it */
794 if (ipc && ipc != SEQ_START_TOKEN)
797 ids = iter->ns->ids[iface->ids];
798 /* Release the lock we took in start() */
799 mutex_unlock(&ids->mutex);
802 static int sysvipc_proc_show(struct seq_file *s, void *it)
804 struct ipc_proc_iter *iter = s->private;
805 struct ipc_proc_iface *iface = iter->iface;
807 if (it == SEQ_START_TOKEN)
808 return seq_puts(s, iface->header);
810 return iface->show(s, it);
813 static struct seq_operations sysvipc_proc_seqops = {
814 .start = sysvipc_proc_start,
815 .stop = sysvipc_proc_stop,
816 .next = sysvipc_proc_next,
817 .show = sysvipc_proc_show,
820 static int sysvipc_proc_open(struct inode *inode, struct file *file)
823 struct seq_file *seq;
824 struct ipc_proc_iter *iter;
827 iter = kmalloc(sizeof(*iter), GFP_KERNEL);
831 ret = seq_open(file, &sysvipc_proc_seqops);
835 seq = file->private_data;
838 iter->iface = PDE(inode)->data;
839 iter->ns = get_ipc_ns(current->nsproxy->ipc_ns);
847 static int sysvipc_proc_release(struct inode *inode, struct file *file)
849 struct seq_file *seq = file->private_data;
850 struct ipc_proc_iter *iter = seq->private;
851 put_ipc_ns(iter->ns);
852 return seq_release_private(inode, file);
855 static const struct file_operations sysvipc_proc_fops = {
856 .open = sysvipc_proc_open,
859 .release = sysvipc_proc_release,
861 #endif /* CONFIG_PROC_FS */