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>
15 #include <linux/config.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>
23 #include <linux/capability.h>
24 #include <linux/highuid.h>
25 #include <linux/security.h>
26 #include <linux/rcupdate.h>
27 #include <linux/workqueue.h>
28 #include <linux/seq_file.h>
29 #include <linux/proc_fs.h>
31 #include <asm/unistd.h>
35 struct ipc_proc_iface {
39 int (*show)(struct seq_file *, void *);
43 * ipc_init - initialise IPC subsystem
45 * The various system5 IPC resources (semaphores, messages and shared
46 * memory are initialised
49 static int __init ipc_init(void)
59 * ipc_init_ids - initialise IPC identifiers
60 * @ids: Identifier set
61 * @size: Number of identifiers
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
68 void __init ipc_init_ids(struct ipc_ids* ids, int size)
72 mutex_init(&ids->mutex);
80 int seq_limit = INT_MAX/SEQ_MULTIPLIER;
81 if(seq_limit > USHRT_MAX)
82 ids->seq_max = USHRT_MAX;
84 ids->seq_max = seq_limit;
87 ids->entries = ipc_rcu_alloc(sizeof(struct kern_ipc_perm *)*size +
88 sizeof(struct ipc_id_ary));
90 if(ids->entries == NULL) {
91 printk(KERN_ERR "ipc_init_ids() failed, ipc service disabled.\n");
93 ids->entries = &ids->nullentry;
95 ids->entries->size = size;
97 ids->entries->p[i] = NULL;
100 #ifdef CONFIG_PROC_FS
101 static struct file_operations sysvipc_proc_fops;
103 * ipc_init_proc_interface - Create a proc interface for sysipc types
104 * using a seq_file interface.
105 * @path: Path in procfs
106 * @header: Banner to be printed at the beginning of the file.
107 * @ids: ipc id table to iterate.
108 * @show: show routine.
110 void __init ipc_init_proc_interface(const char *path, const char *header,
112 int (*show)(struct seq_file *, void *))
114 struct proc_dir_entry *pde;
115 struct ipc_proc_iface *iface;
117 iface = kmalloc(sizeof(*iface), GFP_KERNEL);
121 iface->header = header;
125 pde = create_proc_entry(path,
126 S_IRUGO, /* world readable */
127 NULL /* parent dir */);
130 pde->proc_fops = &sysvipc_proc_fops;
138 * ipc_findkey - find a key in an ipc identifier set
139 * @ids: Identifier set
140 * @key: The key to find
142 * Requires ipc_ids.mutex locked.
143 * Returns the identifier if found or -1 if not.
146 int ipc_findkey(struct ipc_ids* ids, key_t key)
149 struct kern_ipc_perm* p;
150 int max_id = ids->max_id;
153 * rcu_dereference() is not needed here
154 * since ipc_ids.mutex is held
156 for (id = 0; id <= max_id; id++) {
157 p = ids->entries->p[id];
167 * Requires ipc_ids.mutex locked
169 static int grow_ary(struct ipc_ids* ids, int newsize)
171 struct ipc_id_ary* new;
172 struct ipc_id_ary* old;
174 int size = ids->entries->size;
181 new = ipc_rcu_alloc(sizeof(struct kern_ipc_perm *)*newsize +
182 sizeof(struct ipc_id_ary));
186 memcpy(new->p, ids->entries->p, sizeof(struct kern_ipc_perm *)*size);
187 for(i=size;i<newsize;i++) {
193 * Use rcu_assign_pointer() to make sure the memcpyed contents
194 * of the new array are visible before the new array becomes visible.
196 rcu_assign_pointer(ids->entries, new);
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
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.
213 * Called with ipc_ids.mutex held.
216 int ipc_addid(struct ipc_ids* ids, struct kern_ipc_perm* new, int size)
220 size = grow_ary(ids,size);
223 * rcu_dereference()() is not needed here since
224 * ipc_ids.mutex is held
226 for (id = 0; id < size; id++) {
227 if(ids->entries->p[id] == NULL)
233 if (id > ids->max_id)
236 new->cuid = new->uid = current->euid;
237 new->gid = new->cgid = current->egid;
239 new->seq = ids->seq++;
240 if(ids->seq > ids->seq_max)
243 spin_lock_init(&new->lock);
246 spin_lock(&new->lock);
247 ids->entries->p[id] = new;
252 * ipc_rmid - remove an IPC identifier
253 * @ids: identifier set
254 * @id: Identifier to remove
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
260 * ipc_ids.mutex and the spinlock for this ID is hold before this function
261 * is called, and remain locked on the exit.
264 struct kern_ipc_perm* ipc_rmid(struct ipc_ids* ids, int id)
266 struct kern_ipc_perm* p;
267 int lid = id % SEQ_MULTIPLIER;
268 BUG_ON(lid >= ids->entries->size);
271 * do not need a rcu_dereference()() here to force ordering
272 * on Alpha, since the ipc_ids.mutex is held.
274 p = ids->entries->p[lid];
275 ids->entries->p[lid] = NULL;
279 if (lid == ids->max_id) {
284 } while (ids->entries->p[lid] == NULL);
292 * ipc_alloc - allocate ipc space
293 * @size: size desired
295 * Allocate memory from the appropriate pools and return a pointer to it.
296 * NULL is returned if the allocation fails
299 void* ipc_alloc(int size)
305 out = kmalloc(size, GFP_KERNEL);
310 * ipc_free - free ipc space
311 * @ptr: pointer returned by ipc_alloc
312 * @size: size of block
314 * Free a block created with ipc_alloc. The caller must know the size
315 * used in the allocation call.
318 void ipc_free(void* ptr, int size)
328 * There are three headers that are prepended to the actual allocation:
329 * - during use: ipc_rcu_hdr.
330 * - during the rcu grace period: ipc_rcu_grace.
331 * - [only if vmalloc]: ipc_rcu_sched.
332 * Their lifetime doesn't overlap, thus the headers share the same memory.
333 * Unlike a normal union, they are right-aligned, thus some container_of
334 * forward/backward casting is necessary:
347 /* "void *" makes sure alignment of following data is sane. */
353 struct work_struct work;
354 /* "void *" makes sure alignment of following data is sane. */
358 #define HDRLEN_KMALLOC (sizeof(struct ipc_rcu_grace) > sizeof(struct ipc_rcu_hdr) ? \
359 sizeof(struct ipc_rcu_grace) : sizeof(struct ipc_rcu_hdr))
360 #define HDRLEN_VMALLOC (sizeof(struct ipc_rcu_sched) > HDRLEN_KMALLOC ? \
361 sizeof(struct ipc_rcu_sched) : HDRLEN_KMALLOC)
363 static inline int rcu_use_vmalloc(int size)
365 /* Too big for a single page? */
366 if (HDRLEN_KMALLOC + size > PAGE_SIZE)
372 * ipc_rcu_alloc - allocate ipc and rcu space
373 * @size: size desired
375 * Allocate memory for the rcu header structure + the object.
376 * Returns the pointer to the object.
377 * NULL is returned if the allocation fails.
380 void* ipc_rcu_alloc(int size)
384 * We prepend the allocation with the rcu struct, and
385 * workqueue if necessary (for vmalloc).
387 if (rcu_use_vmalloc(size)) {
388 out = vmalloc(HDRLEN_VMALLOC + size);
390 out += HDRLEN_VMALLOC;
391 container_of(out, struct ipc_rcu_hdr, data)->is_vmalloc = 1;
392 container_of(out, struct ipc_rcu_hdr, data)->refcount = 1;
395 out = kmalloc(HDRLEN_KMALLOC + size, GFP_KERNEL);
397 out += HDRLEN_KMALLOC;
398 container_of(out, struct ipc_rcu_hdr, data)->is_vmalloc = 0;
399 container_of(out, struct ipc_rcu_hdr, data)->refcount = 1;
406 void ipc_rcu_getref(void *ptr)
408 container_of(ptr, struct ipc_rcu_hdr, data)->refcount++;
412 * ipc_schedule_free - free ipc + rcu space
413 * @head: RCU callback structure for queued work
415 * Since RCU callback function is called in bh,
416 * we need to defer the vfree to schedule_work
418 static void ipc_schedule_free(struct rcu_head *head)
420 struct ipc_rcu_grace *grace =
421 container_of(head, struct ipc_rcu_grace, rcu);
422 struct ipc_rcu_sched *sched =
423 container_of(&(grace->data[0]), struct ipc_rcu_sched, data[0]);
425 INIT_WORK(&sched->work, vfree, sched);
426 schedule_work(&sched->work);
430 * ipc_immediate_free - free ipc + rcu space
431 * @head: RCU callback structure that contains pointer to be freed
433 * Free from the RCU callback context
435 static void ipc_immediate_free(struct rcu_head *head)
437 struct ipc_rcu_grace *free =
438 container_of(head, struct ipc_rcu_grace, rcu);
442 void ipc_rcu_putref(void *ptr)
444 if (--container_of(ptr, struct ipc_rcu_hdr, data)->refcount > 0)
447 if (container_of(ptr, struct ipc_rcu_hdr, data)->is_vmalloc) {
448 call_rcu(&container_of(ptr, struct ipc_rcu_grace, data)->rcu,
451 call_rcu(&container_of(ptr, struct ipc_rcu_grace, data)->rcu,
457 * ipcperms - check IPC permissions
458 * @ipcp: IPC permission set
459 * @flag: desired permission set.
461 * Check user, group, other permissions for access
462 * to ipc resources. return 0 if allowed
465 int ipcperms (struct kern_ipc_perm *ipcp, short flag)
466 { /* flag will most probably be 0 or S_...UGO from <linux/stat.h> */
467 int requested_mode, granted_mode;
469 requested_mode = (flag >> 6) | (flag >> 3) | flag;
470 granted_mode = ipcp->mode;
471 if (current->euid == ipcp->cuid || current->euid == ipcp->uid)
473 else if (in_group_p(ipcp->cgid) || in_group_p(ipcp->gid))
475 /* is there some bit set in requested_mode but not in granted_mode? */
476 if ((requested_mode & ~granted_mode & 0007) &&
477 !capable(CAP_IPC_OWNER))
480 return security_ipc_permission(ipcp, flag);
484 * Functions to convert between the kern_ipc_perm structure and the
485 * old/new ipc_perm structures
489 * kernel_to_ipc64_perm - convert kernel ipc permissions to user
490 * @in: kernel permissions
491 * @out: new style IPC permissions
493 * Turn the kernel object 'in' into a set of permissions descriptions
494 * for returning to userspace (out).
498 void kernel_to_ipc64_perm (struct kern_ipc_perm *in, struct ipc64_perm *out)
503 out->cuid = in->cuid;
504 out->cgid = in->cgid;
505 out->mode = in->mode;
510 * ipc64_perm_to_ipc_perm - convert old ipc permissions to new
511 * @in: new style IPC permissions
512 * @out: old style IPC permissions
514 * Turn the new style permissions object in into a compatibility
515 * object and store it into the 'out' pointer.
518 void ipc64_perm_to_ipc_perm (struct ipc64_perm *in, struct ipc_perm *out)
521 SET_UID(out->uid, in->uid);
522 SET_GID(out->gid, in->gid);
523 SET_UID(out->cuid, in->cuid);
524 SET_GID(out->cgid, in->cgid);
525 out->mode = in->mode;
530 * So far only shm_get_stat() calls ipc_get() via shm_get(), so ipc_get()
531 * is called with shm_ids.mutex locked. Since grow_ary() is also called with
532 * shm_ids.mutex down(for Shared Memory), there is no need to add read
533 * barriers here to gurantee the writes in grow_ary() are seen in order
536 * However ipc_get() itself does not necessary require ipc_ids.mutex down. So
537 * if in the future ipc_get() is used by other places without ipc_ids.mutex
538 * down, then ipc_get() needs read memery barriers as ipc_lock() does.
540 struct kern_ipc_perm* ipc_get(struct ipc_ids* ids, int id)
542 struct kern_ipc_perm* out;
543 int lid = id % SEQ_MULTIPLIER;
544 if(lid >= ids->entries->size)
546 out = ids->entries->p[lid];
550 struct kern_ipc_perm* ipc_lock(struct ipc_ids* ids, int id)
552 struct kern_ipc_perm* out;
553 int lid = id % SEQ_MULTIPLIER;
554 struct ipc_id_ary* entries;
557 entries = rcu_dereference(ids->entries);
558 if(lid >= entries->size) {
562 out = entries->p[lid];
567 spin_lock(&out->lock);
569 /* ipc_rmid() may have already freed the ID while ipc_lock
570 * was spinning: here verify that the structure is still valid
573 spin_unlock(&out->lock);
580 void ipc_lock_by_ptr(struct kern_ipc_perm *perm)
583 spin_lock(&perm->lock);
586 void ipc_unlock(struct kern_ipc_perm* perm)
588 spin_unlock(&perm->lock);
592 int ipc_buildid(struct ipc_ids* ids, int id, int seq)
594 return SEQ_MULTIPLIER*seq + id;
597 int ipc_checkid(struct ipc_ids* ids, struct kern_ipc_perm* ipcp, int uid)
599 if(uid/SEQ_MULTIPLIER != ipcp->seq)
604 #ifdef __ARCH_WANT_IPC_PARSE_VERSION
608 * ipc_parse_version - IPC call version
609 * @cmd: pointer to command
611 * Return IPC_64 for new style IPC and IPC_OLD for old style IPC.
612 * The cmd value is turned from an encoding command and version into
613 * just the command code.
616 int ipc_parse_version (int *cmd)
626 #endif /* __ARCH_WANT_IPC_PARSE_VERSION */
628 #ifdef CONFIG_PROC_FS
629 static void *sysvipc_proc_next(struct seq_file *s, void *it, loff_t *pos)
631 struct ipc_proc_iface *iface = s->private;
632 struct kern_ipc_perm *ipc = it;
635 /* If we had an ipc id locked before, unlock it */
636 if (ipc && ipc != SEQ_START_TOKEN)
640 * p = *pos - 1 (because id 0 starts at position 1)
641 * + 1 (because we increment the position by one)
643 for (p = *pos; p <= iface->ids->max_id; p++) {
644 if ((ipc = ipc_lock(iface->ids, p)) != NULL) {
650 /* Out of range - return NULL to terminate iteration */
655 * File positions: pos 0 -> header, pos n -> ipc id + 1.
656 * SeqFile iterator: iterator value locked shp or SEQ_TOKEN_START.
658 static void *sysvipc_proc_start(struct seq_file *s, loff_t *pos)
660 struct ipc_proc_iface *iface = s->private;
661 struct kern_ipc_perm *ipc;
665 * Take the lock - this will be released by the corresponding
668 mutex_lock(&iface->ids->mutex);
670 /* pos < 0 is invalid */
674 /* pos == 0 means header */
676 return SEQ_START_TOKEN;
678 /* Find the (pos-1)th ipc */
679 for (p = *pos - 1; p <= iface->ids->max_id; p++) {
680 if ((ipc = ipc_lock(iface->ids, p)) != NULL) {
688 static void sysvipc_proc_stop(struct seq_file *s, void *it)
690 struct kern_ipc_perm *ipc = it;
691 struct ipc_proc_iface *iface = s->private;
693 /* If we had a locked segment, release it */
694 if (ipc && ipc != SEQ_START_TOKEN)
697 /* Release the lock we took in start() */
698 mutex_unlock(&iface->ids->mutex);
701 static int sysvipc_proc_show(struct seq_file *s, void *it)
703 struct ipc_proc_iface *iface = s->private;
705 if (it == SEQ_START_TOKEN)
706 return seq_puts(s, iface->header);
708 return iface->show(s, it);
711 static struct seq_operations sysvipc_proc_seqops = {
712 .start = sysvipc_proc_start,
713 .stop = sysvipc_proc_stop,
714 .next = sysvipc_proc_next,
715 .show = sysvipc_proc_show,
718 static int sysvipc_proc_open(struct inode *inode, struct file *file) {
720 struct seq_file *seq;
722 ret = seq_open(file, &sysvipc_proc_seqops);
724 seq = file->private_data;
725 seq->private = PDE(inode)->data;
730 static struct file_operations sysvipc_proc_fops = {
731 .open = sysvipc_proc_open,
734 .release = seq_release,
736 #endif /* CONFIG_PROC_FS */