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>
35 #include <linux/rwsem.h>
36 #include <linux/memory.h>
37 #include <linux/ipc_namespace.h>
39 #include <asm/unistd.h>
43 struct ipc_proc_iface {
47 int (*show)(struct seq_file *, void *);
50 struct ipc_namespace init_ipc_ns = {
52 .refcount = ATOMIC_INIT(2),
56 atomic_t nr_ipc_ns = ATOMIC_INIT(1);
59 #ifdef CONFIG_MEMORY_HOTPLUG
61 static int ipc_memory_callback(struct notifier_block *self,
62 unsigned long action, void *arg)
65 case MEM_ONLINE: /* memory successfully brought online */
66 case MEM_OFFLINE: /* or offline: it's time to recompute msgmni */
68 * This is done by invoking the ipcns notifier chain with the
69 * IPC_MEMCHANGED event.
71 ipcns_notify(IPCNS_MEMCHANGED);
73 case MEM_GOING_ONLINE:
74 case MEM_GOING_OFFLINE:
75 case MEM_CANCEL_ONLINE:
76 case MEM_CANCEL_OFFLINE:
84 #endif /* CONFIG_MEMORY_HOTPLUG */
87 * ipc_init - initialise IPC subsystem
89 * The various system5 IPC resources (semaphores, messages and shared
90 * memory) are initialised
91 * A callback routine is registered into the memory hotplug notifier
92 * chain: since msgmni scales to lowmem this callback routine will be
93 * called upon successful memory add / remove to recompute msmgni.
96 static int __init ipc_init(void)
101 hotplug_memory_notifier(ipc_memory_callback, IPC_CALLBACK_PRI);
102 register_ipcns_notifier(&init_ipc_ns);
105 __initcall(ipc_init);
108 * ipc_init_ids - initialise IPC identifiers
109 * @ids: Identifier set
111 * Set up the sequence range to use for the ipc identifier range (limited
112 * below IPCMNI) then initialise the ids idr.
115 void ipc_init_ids(struct ipc_ids *ids)
117 init_rwsem(&ids->rw_mutex);
122 int seq_limit = INT_MAX/SEQ_MULTIPLIER;
123 if(seq_limit > USHRT_MAX)
124 ids->seq_max = USHRT_MAX;
126 ids->seq_max = seq_limit;
129 idr_init(&ids->ipcs_idr);
132 #ifdef CONFIG_PROC_FS
133 static const struct file_operations sysvipc_proc_fops;
135 * ipc_init_proc_interface - Create a proc interface for sysipc types using a seq_file interface.
136 * @path: Path in procfs
137 * @header: Banner to be printed at the beginning of the file.
138 * @ids: ipc id table to iterate.
139 * @show: show routine.
141 void __init ipc_init_proc_interface(const char *path, const char *header,
142 int ids, int (*show)(struct seq_file *, void *))
144 struct proc_dir_entry *pde;
145 struct ipc_proc_iface *iface;
147 iface = kmalloc(sizeof(*iface), GFP_KERNEL);
151 iface->header = header;
155 pde = create_proc_entry(path,
156 S_IRUGO, /* world readable */
157 NULL /* parent dir */);
160 pde->proc_fops = &sysvipc_proc_fops;
168 * ipc_findkey - find a key in an ipc identifier set
169 * @ids: Identifier set
170 * @key: The key to find
172 * Requires ipc_ids.rw_mutex locked.
173 * Returns the LOCKED pointer to the ipc structure if found or NULL
175 * If key is found ipc points to the owning ipc structure
178 static struct kern_ipc_perm *ipc_findkey(struct ipc_ids *ids, key_t key)
180 struct kern_ipc_perm *ipc;
184 for (total = 0, next_id = 0; total < ids->in_use; next_id++) {
185 ipc = idr_find(&ids->ipcs_idr, next_id);
190 if (ipc->key != key) {
195 ipc_lock_by_ptr(ipc);
203 * ipc_get_maxid - get the last assigned id
204 * @ids: IPC identifier set
206 * Called with ipc_ids.rw_mutex held.
209 int ipc_get_maxid(struct ipc_ids *ids)
211 struct kern_ipc_perm *ipc;
215 if (ids->in_use == 0)
218 if (ids->in_use == IPCMNI)
221 /* Look for the last assigned id */
223 for (id = 0; id < IPCMNI && total < ids->in_use; id++) {
224 ipc = idr_find(&ids->ipcs_idr, id);
234 * ipc_addid - add an IPC identifier
235 * @ids: IPC identifier set
236 * @new: new IPC permission set
237 * @size: limit for the number of used ids
239 * Add an entry 'new' to the IPC ids idr. The permissions object is
240 * initialised and the first free entry is set up and the id assigned
241 * is returned. The 'new' entry is returned in a locked state on success.
242 * On failure the entry is not locked and a negative err-code is returned.
244 * Called with ipc_ids.rw_mutex held as a writer.
247 int ipc_addid(struct ipc_ids* ids, struct kern_ipc_perm* new, int size)
254 if (ids->in_use >= size)
257 err = idr_get_new(&ids->ipcs_idr, new, &id);
263 new->cuid = new->uid = current->euid;
264 new->gid = new->cgid = current->egid;
266 new->seq = ids->seq++;
267 if(ids->seq > ids->seq_max)
270 new->id = ipc_buildid(id, new->seq);
271 spin_lock_init(&new->lock);
274 spin_lock(&new->lock);
279 * ipcget_new - create a new ipc object
281 * @ids: IPC identifer set
282 * @ops: the actual creation routine to call
283 * @params: its parameters
285 * This routine is called by sys_msgget, sys_semget() and sys_shmget()
286 * when the key is IPC_PRIVATE.
288 static int ipcget_new(struct ipc_namespace *ns, struct ipc_ids *ids,
289 struct ipc_ops *ops, struct ipc_params *params)
293 err = idr_pre_get(&ids->ipcs_idr, GFP_KERNEL);
298 down_write(&ids->rw_mutex);
299 err = ops->getnew(ns, params);
300 up_write(&ids->rw_mutex);
309 * ipc_check_perms - check security and permissions for an IPC
310 * @ipcp: ipc permission set
311 * @ops: the actual security routine to call
312 * @params: its parameters
314 * This routine is called by sys_msgget(), sys_semget() and sys_shmget()
315 * when the key is not IPC_PRIVATE and that key already exists in the
318 * On success, the IPC id is returned.
320 * It is called with ipc_ids.rw_mutex and ipcp->lock held.
322 static int ipc_check_perms(struct kern_ipc_perm *ipcp, struct ipc_ops *ops,
323 struct ipc_params *params)
327 if (ipcperms(ipcp, params->flg))
330 err = ops->associate(ipcp, params->flg);
339 * ipcget_public - get an ipc object or create a new one
341 * @ids: IPC identifer set
342 * @ops: the actual creation routine to call
343 * @params: its parameters
345 * This routine is called by sys_msgget, sys_semget() and sys_shmget()
346 * when the key is not IPC_PRIVATE.
347 * It adds a new entry if the key is not found and does some permission
348 * / security checkings if the key is found.
350 * On success, the ipc id is returned.
352 static int ipcget_public(struct ipc_namespace *ns, struct ipc_ids *ids,
353 struct ipc_ops *ops, struct ipc_params *params)
355 struct kern_ipc_perm *ipcp;
356 int flg = params->flg;
359 err = idr_pre_get(&ids->ipcs_idr, GFP_KERNEL);
362 * Take the lock as a writer since we are potentially going to add
363 * a new entry + read locks are not "upgradable"
365 down_write(&ids->rw_mutex);
366 ipcp = ipc_findkey(ids, params->key);
369 if (!(flg & IPC_CREAT))
374 err = ops->getnew(ns, params);
376 /* ipc object has been locked by ipc_findkey() */
378 if (flg & IPC_CREAT && flg & IPC_EXCL)
382 if (ops->more_checks)
383 err = ops->more_checks(ipcp, params);
386 * ipc_check_perms returns the IPC id on
389 err = ipc_check_perms(ipcp, ops, params);
393 up_write(&ids->rw_mutex);
403 * ipc_rmid - remove an IPC identifier
404 * @ids: IPC identifier set
405 * @ipcp: ipc perm structure containing the identifier to remove
407 * ipc_ids.rw_mutex (as a writer) and the spinlock for this ID are held
408 * before this function is called, and remain locked on the exit.
411 void ipc_rmid(struct ipc_ids *ids, struct kern_ipc_perm *ipcp)
413 int lid = ipcid_to_idx(ipcp->id);
415 idr_remove(&ids->ipcs_idr, lid);
425 * ipc_alloc - allocate ipc space
426 * @size: size desired
428 * Allocate memory from the appropriate pools and return a pointer to it.
429 * NULL is returned if the allocation fails
432 void* ipc_alloc(int size)
438 out = kmalloc(size, GFP_KERNEL);
443 * ipc_free - free ipc space
444 * @ptr: pointer returned by ipc_alloc
445 * @size: size of block
447 * Free a block created with ipc_alloc(). The caller must know the size
448 * used in the allocation call.
451 void ipc_free(void* ptr, int size)
461 * There are three headers that are prepended to the actual allocation:
462 * - during use: ipc_rcu_hdr.
463 * - during the rcu grace period: ipc_rcu_grace.
464 * - [only if vmalloc]: ipc_rcu_sched.
465 * Their lifetime doesn't overlap, thus the headers share the same memory.
466 * Unlike a normal union, they are right-aligned, thus some container_of
467 * forward/backward casting is necessary:
480 /* "void *" makes sure alignment of following data is sane. */
486 struct work_struct work;
487 /* "void *" makes sure alignment of following data is sane. */
491 #define HDRLEN_KMALLOC (sizeof(struct ipc_rcu_grace) > sizeof(struct ipc_rcu_hdr) ? \
492 sizeof(struct ipc_rcu_grace) : sizeof(struct ipc_rcu_hdr))
493 #define HDRLEN_VMALLOC (sizeof(struct ipc_rcu_sched) > HDRLEN_KMALLOC ? \
494 sizeof(struct ipc_rcu_sched) : HDRLEN_KMALLOC)
496 static inline int rcu_use_vmalloc(int size)
498 /* Too big for a single page? */
499 if (HDRLEN_KMALLOC + size > PAGE_SIZE)
505 * ipc_rcu_alloc - allocate ipc and rcu space
506 * @size: size desired
508 * Allocate memory for the rcu header structure + the object.
509 * Returns the pointer to the object.
510 * NULL is returned if the allocation fails.
513 void* ipc_rcu_alloc(int size)
517 * We prepend the allocation with the rcu struct, and
518 * workqueue if necessary (for vmalloc).
520 if (rcu_use_vmalloc(size)) {
521 out = vmalloc(HDRLEN_VMALLOC + size);
523 out += HDRLEN_VMALLOC;
524 container_of(out, struct ipc_rcu_hdr, data)->is_vmalloc = 1;
525 container_of(out, struct ipc_rcu_hdr, data)->refcount = 1;
528 out = kmalloc(HDRLEN_KMALLOC + size, GFP_KERNEL);
530 out += HDRLEN_KMALLOC;
531 container_of(out, struct ipc_rcu_hdr, data)->is_vmalloc = 0;
532 container_of(out, struct ipc_rcu_hdr, data)->refcount = 1;
539 void ipc_rcu_getref(void *ptr)
541 container_of(ptr, struct ipc_rcu_hdr, data)->refcount++;
544 static void ipc_do_vfree(struct work_struct *work)
546 vfree(container_of(work, struct ipc_rcu_sched, work));
550 * ipc_schedule_free - free ipc + rcu space
551 * @head: RCU callback structure for queued work
553 * Since RCU callback function is called in bh,
554 * we need to defer the vfree to schedule_work().
556 static void ipc_schedule_free(struct rcu_head *head)
558 struct ipc_rcu_grace *grace;
559 struct ipc_rcu_sched *sched;
561 grace = container_of(head, struct ipc_rcu_grace, rcu);
562 sched = container_of(&(grace->data[0]), struct ipc_rcu_sched,
565 INIT_WORK(&sched->work, ipc_do_vfree);
566 schedule_work(&sched->work);
570 * ipc_immediate_free - free ipc + rcu space
571 * @head: RCU callback structure that contains pointer to be freed
573 * Free from the RCU callback context.
575 static void ipc_immediate_free(struct rcu_head *head)
577 struct ipc_rcu_grace *free =
578 container_of(head, struct ipc_rcu_grace, rcu);
582 void ipc_rcu_putref(void *ptr)
584 if (--container_of(ptr, struct ipc_rcu_hdr, data)->refcount > 0)
587 if (container_of(ptr, struct ipc_rcu_hdr, data)->is_vmalloc) {
588 call_rcu(&container_of(ptr, struct ipc_rcu_grace, data)->rcu,
591 call_rcu(&container_of(ptr, struct ipc_rcu_grace, data)->rcu,
597 * ipcperms - check IPC permissions
598 * @ipcp: IPC permission set
599 * @flag: desired permission set.
601 * Check user, group, other permissions for access
602 * to ipc resources. return 0 if allowed
605 int ipcperms (struct kern_ipc_perm *ipcp, short flag)
606 { /* flag will most probably be 0 or S_...UGO from <linux/stat.h> */
607 int requested_mode, granted_mode, err;
609 if (unlikely((err = audit_ipc_obj(ipcp))))
611 requested_mode = (flag >> 6) | (flag >> 3) | flag;
612 granted_mode = ipcp->mode;
613 if (current->euid == ipcp->cuid || current->euid == ipcp->uid)
615 else if (in_group_p(ipcp->cgid) || in_group_p(ipcp->gid))
617 /* is there some bit set in requested_mode but not in granted_mode? */
618 if ((requested_mode & ~granted_mode & 0007) &&
619 !capable(CAP_IPC_OWNER))
622 return security_ipc_permission(ipcp, flag);
626 * Functions to convert between the kern_ipc_perm structure and the
627 * old/new ipc_perm structures
631 * kernel_to_ipc64_perm - convert kernel ipc permissions to user
632 * @in: kernel permissions
633 * @out: new style IPC permissions
635 * Turn the kernel object @in into a set of permissions descriptions
636 * for returning to userspace (@out).
640 void kernel_to_ipc64_perm (struct kern_ipc_perm *in, struct ipc64_perm *out)
645 out->cuid = in->cuid;
646 out->cgid = in->cgid;
647 out->mode = in->mode;
652 * ipc64_perm_to_ipc_perm - convert new ipc permissions to old
653 * @in: new style IPC permissions
654 * @out: old style IPC permissions
656 * Turn the new style permissions object @in into a compatibility
657 * object and store it into the @out pointer.
660 void ipc64_perm_to_ipc_perm (struct ipc64_perm *in, struct ipc_perm *out)
663 SET_UID(out->uid, in->uid);
664 SET_GID(out->gid, in->gid);
665 SET_UID(out->cuid, in->cuid);
666 SET_GID(out->cgid, in->cgid);
667 out->mode = in->mode;
672 * ipc_lock - Lock an ipc structure without rw_mutex held
673 * @ids: IPC identifier set
674 * @id: ipc id to look for
676 * Look for an id in the ipc ids idr and lock the associated ipc object.
678 * The ipc object is locked on exit.
680 * This is the routine that should be called when the rw_mutex is not already
681 * held, i.e. idr tree not protected: it protects the idr tree in read mode
682 * during the idr_find().
685 struct kern_ipc_perm *ipc_lock(struct ipc_ids *ids, int id)
687 struct kern_ipc_perm *out;
688 int lid = ipcid_to_idx(id);
690 down_read(&ids->rw_mutex);
693 out = idr_find(&ids->ipcs_idr, lid);
696 up_read(&ids->rw_mutex);
697 return ERR_PTR(-EINVAL);
700 up_read(&ids->rw_mutex);
702 spin_lock(&out->lock);
704 /* ipc_rmid() may have already freed the ID while ipc_lock
705 * was spinning: here verify that the structure is still valid
708 spin_unlock(&out->lock);
710 return ERR_PTR(-EINVAL);
717 * ipc_lock_down - Lock an ipc structure with rw_sem held
718 * @ids: IPC identifier set
719 * @id: ipc id to look for
721 * Look for an id in the ipc ids idr and lock the associated ipc object.
723 * The ipc object is locked on exit.
725 * This is the routine that should be called when the rw_mutex is already
726 * held, i.e. idr tree protected.
729 struct kern_ipc_perm *ipc_lock_down(struct ipc_ids *ids, int id)
731 struct kern_ipc_perm *out;
732 int lid = ipcid_to_idx(id);
735 out = idr_find(&ids->ipcs_idr, lid);
738 return ERR_PTR(-EINVAL);
741 spin_lock(&out->lock);
744 * No need to verify that the structure is still valid since the
750 struct kern_ipc_perm *ipc_lock_check_down(struct ipc_ids *ids, int id)
752 struct kern_ipc_perm *out;
754 out = ipc_lock_down(ids, id);
758 if (ipc_checkid(out, id)) {
760 return ERR_PTR(-EIDRM);
766 struct kern_ipc_perm *ipc_lock_check(struct ipc_ids *ids, int id)
768 struct kern_ipc_perm *out;
770 out = ipc_lock(ids, id);
774 if (ipc_checkid(out, id)) {
776 return ERR_PTR(-EIDRM);
783 * ipcget - Common sys_*get() code
785 * @ids : IPC identifier set
786 * @ops : operations to be called on ipc object creation, permission checks
788 * @params : the parameters needed by the previous operations.
790 * Common routine called by sys_msgget(), sys_semget() and sys_shmget().
792 int ipcget(struct ipc_namespace *ns, struct ipc_ids *ids,
793 struct ipc_ops *ops, struct ipc_params *params)
795 if (params->key == IPC_PRIVATE)
796 return ipcget_new(ns, ids, ops, params);
798 return ipcget_public(ns, ids, ops, params);
801 #ifdef __ARCH_WANT_IPC_PARSE_VERSION
805 * ipc_parse_version - IPC call version
806 * @cmd: pointer to command
808 * Return IPC_64 for new style IPC and IPC_OLD for old style IPC.
809 * The @cmd value is turned from an encoding command and version into
810 * just the command code.
813 int ipc_parse_version (int *cmd)
823 #endif /* __ARCH_WANT_IPC_PARSE_VERSION */
825 #ifdef CONFIG_PROC_FS
826 struct ipc_proc_iter {
827 struct ipc_namespace *ns;
828 struct ipc_proc_iface *iface;
832 * This routine locks the ipc structure found at least at position pos.
834 static struct kern_ipc_perm *sysvipc_find_ipc(struct ipc_ids *ids, loff_t pos,
837 struct kern_ipc_perm *ipc;
841 for (id = 0; id < pos && total < ids->in_use; id++) {
842 ipc = idr_find(&ids->ipcs_idr, id);
847 if (total >= ids->in_use)
850 for ( ; pos < IPCMNI; pos++) {
851 ipc = idr_find(&ids->ipcs_idr, pos);
854 ipc_lock_by_ptr(ipc);
859 /* Out of range - return NULL to terminate iteration */
863 static void *sysvipc_proc_next(struct seq_file *s, void *it, loff_t *pos)
865 struct ipc_proc_iter *iter = s->private;
866 struct ipc_proc_iface *iface = iter->iface;
867 struct kern_ipc_perm *ipc = it;
869 /* If we had an ipc id locked before, unlock it */
870 if (ipc && ipc != SEQ_START_TOKEN)
873 return sysvipc_find_ipc(&iter->ns->ids[iface->ids], *pos, pos);
877 * File positions: pos 0 -> header, pos n -> ipc id = n - 1.
878 * SeqFile iterator: iterator value locked ipc pointer or SEQ_TOKEN_START.
880 static void *sysvipc_proc_start(struct seq_file *s, loff_t *pos)
882 struct ipc_proc_iter *iter = s->private;
883 struct ipc_proc_iface *iface = iter->iface;
886 ids = &iter->ns->ids[iface->ids];
889 * Take the lock - this will be released by the corresponding
892 down_read(&ids->rw_mutex);
894 /* pos < 0 is invalid */
898 /* pos == 0 means header */
900 return SEQ_START_TOKEN;
902 /* Find the (pos-1)th ipc */
903 return sysvipc_find_ipc(ids, *pos - 1, pos);
906 static void sysvipc_proc_stop(struct seq_file *s, void *it)
908 struct kern_ipc_perm *ipc = it;
909 struct ipc_proc_iter *iter = s->private;
910 struct ipc_proc_iface *iface = iter->iface;
913 /* If we had a locked structure, release it */
914 if (ipc && ipc != SEQ_START_TOKEN)
917 ids = &iter->ns->ids[iface->ids];
918 /* Release the lock we took in start() */
919 up_read(&ids->rw_mutex);
922 static int sysvipc_proc_show(struct seq_file *s, void *it)
924 struct ipc_proc_iter *iter = s->private;
925 struct ipc_proc_iface *iface = iter->iface;
927 if (it == SEQ_START_TOKEN)
928 return seq_puts(s, iface->header);
930 return iface->show(s, it);
933 static struct seq_operations sysvipc_proc_seqops = {
934 .start = sysvipc_proc_start,
935 .stop = sysvipc_proc_stop,
936 .next = sysvipc_proc_next,
937 .show = sysvipc_proc_show,
940 static int sysvipc_proc_open(struct inode *inode, struct file *file)
943 struct seq_file *seq;
944 struct ipc_proc_iter *iter;
947 iter = kmalloc(sizeof(*iter), GFP_KERNEL);
951 ret = seq_open(file, &sysvipc_proc_seqops);
955 seq = file->private_data;
958 iter->iface = PDE(inode)->data;
959 iter->ns = get_ipc_ns(current->nsproxy->ipc_ns);
967 static int sysvipc_proc_release(struct inode *inode, struct file *file)
969 struct seq_file *seq = file->private_data;
970 struct ipc_proc_iter *iter = seq->private;
971 put_ipc_ns(iter->ns);
972 return seq_release_private(inode, file);
975 static const struct file_operations sysvipc_proc_fops = {
976 .open = sysvipc_proc_open,
979 .release = sysvipc_proc_release,
981 #endif /* CONFIG_PROC_FS */