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/smp_lock.h>
25 #include <linux/vmalloc.h>
26 #include <linux/slab.h>
27 #include <linux/capability.h>
28 #include <linux/highuid.h>
29 #include <linux/security.h>
30 #include <linux/rcupdate.h>
31 #include <linux/workqueue.h>
32 #include <linux/seq_file.h>
33 #include <linux/proc_fs.h>
34 #include <linux/audit.h>
35 #include <linux/nsproxy.h>
37 #include <asm/unistd.h>
41 struct ipc_proc_iface {
45 int (*show)(struct seq_file *, void *);
48 struct ipc_namespace init_ipc_ns = {
50 .refcount = ATOMIC_INIT(2),
55 static struct ipc_namespace *clone_ipc_ns(struct ipc_namespace *old_ns)
58 struct ipc_namespace *ns;
61 ns = kmalloc(sizeof(struct ipc_namespace), GFP_KERNEL);
65 err = sem_init_ns(ns);
68 err = msg_init_ns(ns);
71 err = shm_init_ns(ns);
88 struct ipc_namespace *copy_ipcs(unsigned long flags, struct ipc_namespace *ns)
90 struct ipc_namespace *new_ns;
95 if (!(flags & CLONE_NEWIPC))
98 new_ns = clone_ipc_ns(ns);
104 void free_ipc_ns(struct kref *kref)
106 struct ipc_namespace *ns;
108 ns = container_of(kref, struct ipc_namespace, kref);
115 struct ipc_namespace *copy_ipcs(unsigned long flags, struct ipc_namespace *ns)
117 if (flags & CLONE_NEWIPC)
118 return ERR_PTR(-EINVAL);
124 * ipc_init - initialise IPC subsystem
126 * The various system5 IPC resources (semaphores, messages and shared
127 * memory) are initialised
130 static int __init ipc_init(void)
137 __initcall(ipc_init);
140 * ipc_init_ids - initialise IPC identifiers
141 * @ids: Identifier set
142 * @size: Number of identifiers
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
149 void __ipc_init ipc_init_ids(struct ipc_ids* ids, int size)
153 mutex_init(&ids->mutex);
161 int seq_limit = INT_MAX/SEQ_MULTIPLIER;
162 if(seq_limit > USHRT_MAX)
163 ids->seq_max = USHRT_MAX;
165 ids->seq_max = seq_limit;
168 ids->entries = ipc_rcu_alloc(sizeof(struct kern_ipc_perm *)*size +
169 sizeof(struct ipc_id_ary));
171 if(ids->entries == NULL) {
172 printk(KERN_ERR "ipc_init_ids() failed, ipc service disabled.\n");
174 ids->entries = &ids->nullentry;
176 ids->entries->size = size;
178 ids->entries->p[i] = NULL;
181 #ifdef CONFIG_PROC_FS
182 static const struct file_operations sysvipc_proc_fops;
184 * ipc_init_proc_interface - Create a proc interface for sysipc types using a seq_file interface.
185 * @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.
190 void __init ipc_init_proc_interface(const char *path, const char *header,
191 int ids, int (*show)(struct seq_file *, void *))
193 struct proc_dir_entry *pde;
194 struct ipc_proc_iface *iface;
196 iface = kmalloc(sizeof(*iface), GFP_KERNEL);
200 iface->header = header;
204 pde = create_proc_entry(path,
205 S_IRUGO, /* world readable */
206 NULL /* parent dir */);
209 pde->proc_fops = &sysvipc_proc_fops;
217 * ipc_findkey - find a key in an ipc identifier set
218 * @ids: Identifier set
219 * @key: The key to find
221 * Requires ipc_ids.mutex locked.
222 * Returns the identifier if found or -1 if not.
225 int ipc_findkey(struct ipc_ids* ids, key_t key)
228 struct kern_ipc_perm* p;
229 int max_id = ids->max_id;
232 * rcu_dereference() is not needed here
233 * since ipc_ids.mutex is held
235 for (id = 0; id <= max_id; id++) {
236 p = ids->entries->p[id];
246 * Requires ipc_ids.mutex locked
248 static int grow_ary(struct ipc_ids* ids, int newsize)
250 struct ipc_id_ary* new;
251 struct ipc_id_ary* old;
253 int size = ids->entries->size;
260 new = ipc_rcu_alloc(sizeof(struct kern_ipc_perm *)*newsize +
261 sizeof(struct ipc_id_ary));
265 memcpy(new->p, ids->entries->p, sizeof(struct kern_ipc_perm *)*size);
266 for(i=size;i<newsize;i++) {
272 * Use rcu_assign_pointer() to make sure the memcpyed contents
273 * of the new array are visible before the new array becomes visible.
275 rcu_assign_pointer(ids->entries, new);
277 __ipc_fini_ids(ids, old);
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
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.
292 * Called with ipc_ids.mutex held.
295 int ipc_addid(struct ipc_ids* ids, struct kern_ipc_perm* new, int size)
299 size = grow_ary(ids,size);
302 * rcu_dereference()() is not needed here since
303 * ipc_ids.mutex is held
305 for (id = 0; id < size; id++) {
306 if(ids->entries->p[id] == NULL)
312 if (id > ids->max_id)
315 new->cuid = new->uid = current->euid;
316 new->gid = new->cgid = current->egid;
318 new->seq = ids->seq++;
319 if(ids->seq > ids->seq_max)
322 spin_lock_init(&new->lock);
325 spin_lock(&new->lock);
326 ids->entries->p[id] = new;
331 * ipc_rmid - remove an IPC identifier
332 * @ids: identifier set
333 * @id: Identifier to remove
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
339 * ipc_ids.mutex and the spinlock for this ID is hold before this function
340 * is called, and remain locked on the exit.
343 struct kern_ipc_perm* ipc_rmid(struct ipc_ids* ids, int id)
345 struct kern_ipc_perm* p;
346 int lid = id % SEQ_MULTIPLIER;
347 BUG_ON(lid >= ids->entries->size);
350 * do not need a rcu_dereference()() here to force ordering
351 * on Alpha, since the ipc_ids.mutex is held.
353 p = ids->entries->p[lid];
354 ids->entries->p[lid] = NULL;
358 if (lid == ids->max_id) {
363 } while (ids->entries->p[lid] == NULL);
371 * ipc_alloc - allocate ipc space
372 * @size: size desired
374 * Allocate memory from the appropriate pools and return a pointer to it.
375 * NULL is returned if the allocation fails
378 void* ipc_alloc(int size)
384 out = kmalloc(size, GFP_KERNEL);
389 * ipc_free - free ipc space
390 * @ptr: pointer returned by ipc_alloc
391 * @size: size of block
393 * Free a block created with ipc_alloc(). The caller must know the size
394 * used in the allocation call.
397 void ipc_free(void* ptr, int size)
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:
426 /* "void *" makes sure alignment of following data is sane. */
432 struct work_struct work;
433 /* "void *" makes sure alignment of following data is sane. */
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)
442 static inline int rcu_use_vmalloc(int size)
444 /* Too big for a single page? */
445 if (HDRLEN_KMALLOC + size > PAGE_SIZE)
451 * ipc_rcu_alloc - allocate ipc and rcu space
452 * @size: size desired
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.
459 void* ipc_rcu_alloc(int size)
463 * We prepend the allocation with the rcu struct, and
464 * workqueue if necessary (for vmalloc).
466 if (rcu_use_vmalloc(size)) {
467 out = vmalloc(HDRLEN_VMALLOC + size);
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;
474 out = kmalloc(HDRLEN_KMALLOC + size, GFP_KERNEL);
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;
485 void ipc_rcu_getref(void *ptr)
487 container_of(ptr, struct ipc_rcu_hdr, data)->refcount++;
490 static void ipc_do_vfree(struct work_struct *work)
492 vfree(container_of(work, struct ipc_rcu_sched, work));
496 * ipc_schedule_free - free ipc + rcu space
497 * @head: RCU callback structure for queued work
499 * Since RCU callback function is called in bh,
500 * we need to defer the vfree to schedule_work().
502 static void ipc_schedule_free(struct rcu_head *head)
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]);
509 INIT_WORK(&sched->work, ipc_do_vfree);
510 schedule_work(&sched->work);
514 * ipc_immediate_free - free ipc + rcu space
515 * @head: RCU callback structure that contains pointer to be freed
517 * Free from the RCU callback context.
519 static void ipc_immediate_free(struct rcu_head *head)
521 struct ipc_rcu_grace *free =
522 container_of(head, struct ipc_rcu_grace, rcu);
526 void ipc_rcu_putref(void *ptr)
528 if (--container_of(ptr, struct ipc_rcu_hdr, data)->refcount > 0)
531 if (container_of(ptr, struct ipc_rcu_hdr, data)->is_vmalloc) {
532 call_rcu(&container_of(ptr, struct ipc_rcu_grace, data)->rcu,
535 call_rcu(&container_of(ptr, struct ipc_rcu_grace, data)->rcu,
541 * ipcperms - check IPC permissions
542 * @ipcp: IPC permission set
543 * @flag: desired permission set.
545 * Check user, group, other permissions for access
546 * to ipc resources. return 0 if allowed
549 int ipcperms (struct kern_ipc_perm *ipcp, short flag)
550 { /* flag will most probably be 0 or S_...UGO from <linux/stat.h> */
551 int requested_mode, granted_mode, err;
553 if (unlikely((err = audit_ipc_obj(ipcp))))
555 requested_mode = (flag >> 6) | (flag >> 3) | flag;
556 granted_mode = ipcp->mode;
557 if (current->euid == ipcp->cuid || current->euid == ipcp->uid)
559 else if (in_group_p(ipcp->cgid) || in_group_p(ipcp->gid))
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))
566 return security_ipc_permission(ipcp, flag);
570 * Functions to convert between the kern_ipc_perm structure and the
571 * old/new ipc_perm structures
575 * kernel_to_ipc64_perm - convert kernel ipc permissions to user
576 * @in: kernel permissions
577 * @out: new style IPC permissions
579 * Turn the kernel object @in into a set of permissions descriptions
580 * for returning to userspace (@out).
584 void kernel_to_ipc64_perm (struct kern_ipc_perm *in, struct ipc64_perm *out)
589 out->cuid = in->cuid;
590 out->cgid = in->cgid;
591 out->mode = in->mode;
596 * ipc64_perm_to_ipc_perm - convert old ipc permissions to new
597 * @in: new style IPC permissions
598 * @out: old style IPC permissions
600 * Turn the new style permissions object @in into a compatibility
601 * object and store it into the @out pointer.
604 void ipc64_perm_to_ipc_perm (struct ipc64_perm *in, struct ipc_perm *out)
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;
616 * So far only shm_get_stat() calls ipc_get() via shm_get(), so ipc_get()
617 * 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
619 * barriers here to gurantee the writes in grow_ary() are seen in order
622 * 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
624 * down, then ipc_get() needs read memery barriers as ipc_lock() does.
626 struct kern_ipc_perm* ipc_get(struct ipc_ids* ids, int id)
628 struct kern_ipc_perm* out;
629 int lid = id % SEQ_MULTIPLIER;
630 if(lid >= ids->entries->size)
632 out = ids->entries->p[lid];
636 struct kern_ipc_perm* ipc_lock(struct ipc_ids* ids, int id)
638 struct kern_ipc_perm* out;
639 int lid = id % SEQ_MULTIPLIER;
640 struct ipc_id_ary* entries;
643 entries = rcu_dereference(ids->entries);
644 if(lid >= entries->size) {
648 out = entries->p[lid];
653 spin_lock(&out->lock);
655 /* ipc_rmid() may have already freed the ID while ipc_lock
656 * was spinning: here verify that the structure is still valid
659 spin_unlock(&out->lock);
666 void ipc_lock_by_ptr(struct kern_ipc_perm *perm)
669 spin_lock(&perm->lock);
672 void ipc_unlock(struct kern_ipc_perm* perm)
674 spin_unlock(&perm->lock);
678 int ipc_buildid(struct ipc_ids* ids, int id, int seq)
680 return SEQ_MULTIPLIER*seq + id;
683 int ipc_checkid(struct ipc_ids* ids, struct kern_ipc_perm* ipcp, int uid)
685 if(uid/SEQ_MULTIPLIER != ipcp->seq)
690 #ifdef __ARCH_WANT_IPC_PARSE_VERSION
694 * ipc_parse_version - IPC call version
695 * @cmd: pointer to command
697 * Return IPC_64 for new style IPC and IPC_OLD for old style IPC.
698 * The @cmd value is turned from an encoding command and version into
699 * just the command code.
702 int ipc_parse_version (int *cmd)
712 #endif /* __ARCH_WANT_IPC_PARSE_VERSION */
714 #ifdef CONFIG_PROC_FS
715 struct ipc_proc_iter {
716 struct ipc_namespace *ns;
717 struct ipc_proc_iface *iface;
720 static void *sysvipc_proc_next(struct seq_file *s, void *it, loff_t *pos)
722 struct ipc_proc_iter *iter = s->private;
723 struct ipc_proc_iface *iface = iter->iface;
724 struct kern_ipc_perm *ipc = it;
728 ids = iter->ns->ids[iface->ids];
730 /* If we had an ipc id locked before, unlock it */
731 if (ipc && ipc != SEQ_START_TOKEN)
735 * p = *pos - 1 (because id 0 starts at position 1)
736 * + 1 (because we increment the position by one)
738 for (p = *pos; p <= ids->max_id; p++) {
739 if ((ipc = ipc_lock(ids, p)) != NULL) {
745 /* Out of range - return NULL to terminate iteration */
750 * File positions: pos 0 -> header, pos n -> ipc id + 1.
751 * SeqFile iterator: iterator value locked shp or SEQ_TOKEN_START.
753 static void *sysvipc_proc_start(struct seq_file *s, loff_t *pos)
755 struct ipc_proc_iter *iter = s->private;
756 struct ipc_proc_iface *iface = iter->iface;
757 struct kern_ipc_perm *ipc;
761 ids = iter->ns->ids[iface->ids];
764 * Take the lock - this will be released by the corresponding
767 mutex_lock(&ids->mutex);
769 /* pos < 0 is invalid */
773 /* pos == 0 means header */
775 return SEQ_START_TOKEN;
777 /* Find the (pos-1)th ipc */
778 for (p = *pos - 1; p <= ids->max_id; p++) {
779 if ((ipc = ipc_lock(ids, p)) != NULL) {
787 static void sysvipc_proc_stop(struct seq_file *s, void *it)
789 struct kern_ipc_perm *ipc = it;
790 struct ipc_proc_iter *iter = s->private;
791 struct ipc_proc_iface *iface = iter->iface;
794 /* If we had a locked segment, release it */
795 if (ipc && ipc != SEQ_START_TOKEN)
798 ids = iter->ns->ids[iface->ids];
799 /* Release the lock we took in start() */
800 mutex_unlock(&ids->mutex);
803 static int sysvipc_proc_show(struct seq_file *s, void *it)
805 struct ipc_proc_iter *iter = s->private;
806 struct ipc_proc_iface *iface = iter->iface;
808 if (it == SEQ_START_TOKEN)
809 return seq_puts(s, iface->header);
811 return iface->show(s, it);
814 static 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,
821 static int sysvipc_proc_open(struct inode *inode, struct file *file)
824 struct seq_file *seq;
825 struct ipc_proc_iter *iter;
828 iter = kmalloc(sizeof(*iter), GFP_KERNEL);
832 ret = seq_open(file, &sysvipc_proc_seqops);
836 seq = file->private_data;
839 iter->iface = PDE(inode)->data;
840 iter->ns = get_ipc_ns(current->nsproxy->ipc_ns);
848 static int sysvipc_proc_release(struct inode *inode, struct file *file)
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);
856 static const struct file_operations sysvipc_proc_fops = {
857 .open = sysvipc_proc_open,
860 .release = sysvipc_proc_release,
862 #endif /* CONFIG_PROC_FS */