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)
71 sema_init(&ids->sem,1);
79 int seq_limit = INT_MAX/SEQ_MULTIPLIER;
80 if(seq_limit > USHRT_MAX)
81 ids->seq_max = USHRT_MAX;
83 ids->seq_max = seq_limit;
86 ids->entries = ipc_rcu_alloc(sizeof(struct kern_ipc_perm *)*size +
87 sizeof(struct ipc_id_ary));
89 if(ids->entries == NULL) {
90 printk(KERN_ERR "ipc_init_ids() failed, ipc service disabled.\n");
92 ids->entries = &ids->nullentry;
94 ids->entries->size = size;
96 ids->entries->p[i] = NULL;
100 static struct file_operations sysvipc_proc_fops;
102 * ipc_init_proc_interface - Create a proc interface for sysipc types
103 * using a seq_file interface.
104 * @path: Path in procfs
105 * @header: Banner to be printed at the beginning of the file.
106 * @ids: ipc id table to iterate.
107 * @show: show routine.
109 void __init ipc_init_proc_interface(const char *path, const char *header,
111 int (*show)(struct seq_file *, void *))
113 struct proc_dir_entry *pde;
114 struct ipc_proc_iface *iface;
116 iface = kmalloc(sizeof(*iface), GFP_KERNEL);
120 iface->header = header;
124 pde = create_proc_entry(path,
125 S_IRUGO, /* world readable */
126 NULL /* parent dir */);
129 pde->proc_fops = &sysvipc_proc_fops;
137 * ipc_findkey - find a key in an ipc identifier set
138 * @ids: Identifier set
139 * @key: The key to find
141 * Requires ipc_ids.sem locked.
142 * Returns the identifier if found or -1 if not.
145 int ipc_findkey(struct ipc_ids* ids, key_t key)
148 struct kern_ipc_perm* p;
149 int max_id = ids->max_id;
152 * rcu_dereference() is not needed here
153 * since ipc_ids.sem is held
155 for (id = 0; id <= max_id; id++) {
156 p = ids->entries->p[id];
159 if (!vx_check(p->xid, VX_IDENT))
168 * Requires ipc_ids.sem locked
170 static int grow_ary(struct ipc_ids* ids, int newsize)
172 struct ipc_id_ary* new;
173 struct ipc_id_ary* old;
175 int size = ids->entries->size;
182 new = ipc_rcu_alloc(sizeof(struct kern_ipc_perm *)*newsize +
183 sizeof(struct ipc_id_ary));
187 memcpy(new->p, ids->entries->p, sizeof(struct kern_ipc_perm *)*size);
188 for(i=size;i<newsize;i++) {
194 * Use rcu_assign_pointer() to make sure the memcpyed contents
195 * of the new array are visible before the new array becomes visible.
197 rcu_assign_pointer(ids->entries, new);
204 * ipc_addid - add an IPC identifier
205 * @ids: IPC identifier set
206 * @new: new IPC permission set
207 * @size: new size limit for the id array
209 * Add an entry 'new' to the IPC arrays. The permissions object is
210 * initialised and the first free entry is set up and the id assigned
211 * is returned. The list is returned in a locked state on success.
212 * On failure the list is not locked and -1 is returned.
214 * Called with ipc_ids.sem held.
217 int ipc_addid(struct ipc_ids* ids, struct kern_ipc_perm* new, int size)
221 size = grow_ary(ids,size);
224 * rcu_dereference()() is not needed here since
225 * ipc_ids.sem is held
227 for (id = 0; id < size; id++) {
228 if(ids->entries->p[id] == NULL)
234 if (id > ids->max_id)
237 new->cuid = new->uid = current->euid;
238 new->gid = new->cgid = current->egid;
240 new->seq = ids->seq++;
241 if(ids->seq > ids->seq_max)
244 spin_lock_init(&new->lock);
247 spin_lock(&new->lock);
248 ids->entries->p[id] = new;
253 * ipc_rmid - remove an IPC identifier
254 * @ids: identifier set
255 * @id: Identifier to remove
257 * The identifier must be valid, and in use. The kernel will panic if
258 * fed an invalid identifier. The entry is removed and internal
259 * variables recomputed. The object associated with the identifier
261 * ipc_ids.sem and the spinlock for this ID is hold before this function
262 * is called, and remain locked on the exit.
265 struct kern_ipc_perm* ipc_rmid(struct ipc_ids* ids, int id)
267 struct kern_ipc_perm* p;
268 int lid = id % SEQ_MULTIPLIER;
269 if(lid >= ids->entries->size)
273 * do not need a rcu_dereference()() here to force ordering
274 * on Alpha, since the ipc_ids.sem is held.
276 p = ids->entries->p[lid];
277 ids->entries->p[lid] = NULL;
282 if (lid == ids->max_id) {
287 } while (ids->entries->p[lid] == NULL);
295 * ipc_alloc - allocate ipc space
296 * @size: size desired
298 * Allocate memory from the appropriate pools and return a pointer to it.
299 * NULL is returned if the allocation fails
302 void* ipc_alloc(int size)
308 out = kmalloc(size, GFP_KERNEL);
313 * ipc_free - free ipc space
314 * @ptr: pointer returned by ipc_alloc
315 * @size: size of block
317 * Free a block created with ipc_alloc. The caller must know the size
318 * used in the allocation call.
321 void ipc_free(void* ptr, int size)
331 * There are three headers that are prepended to the actual allocation:
332 * - during use: ipc_rcu_hdr.
333 * - during the rcu grace period: ipc_rcu_grace.
334 * - [only if vmalloc]: ipc_rcu_sched.
335 * Their lifetime doesn't overlap, thus the headers share the same memory.
336 * Unlike a normal union, they are right-aligned, thus some container_of
337 * forward/backward casting is necessary:
350 /* "void *" makes sure alignment of following data is sane. */
356 struct work_struct work;
357 /* "void *" makes sure alignment of following data is sane. */
361 #define HDRLEN_KMALLOC (sizeof(struct ipc_rcu_grace) > sizeof(struct ipc_rcu_hdr) ? \
362 sizeof(struct ipc_rcu_grace) : sizeof(struct ipc_rcu_hdr))
363 #define HDRLEN_VMALLOC (sizeof(struct ipc_rcu_sched) > HDRLEN_KMALLOC ? \
364 sizeof(struct ipc_rcu_sched) : HDRLEN_KMALLOC)
366 static inline int rcu_use_vmalloc(int size)
368 /* Too big for a single page? */
369 if (HDRLEN_KMALLOC + size > PAGE_SIZE)
375 * ipc_rcu_alloc - allocate ipc and rcu space
376 * @size: size desired
378 * Allocate memory for the rcu header structure + the object.
379 * Returns the pointer to the object.
380 * NULL is returned if the allocation fails.
383 void* ipc_rcu_alloc(int size)
387 * We prepend the allocation with the rcu struct, and
388 * workqueue if necessary (for vmalloc).
390 if (rcu_use_vmalloc(size)) {
391 out = vmalloc(HDRLEN_VMALLOC + size);
393 out += HDRLEN_VMALLOC;
394 container_of(out, struct ipc_rcu_hdr, data)->is_vmalloc = 1;
395 container_of(out, struct ipc_rcu_hdr, data)->refcount = 1;
398 out = kmalloc(HDRLEN_KMALLOC + size, GFP_KERNEL);
400 out += HDRLEN_KMALLOC;
401 container_of(out, struct ipc_rcu_hdr, data)->is_vmalloc = 0;
402 container_of(out, struct ipc_rcu_hdr, data)->refcount = 1;
409 void ipc_rcu_getref(void *ptr)
411 container_of(ptr, struct ipc_rcu_hdr, data)->refcount++;
415 * ipc_schedule_free - free ipc + rcu space
416 * @head: RCU callback structure for queued work
418 * Since RCU callback function is called in bh,
419 * we need to defer the vfree to schedule_work
421 static void ipc_schedule_free(struct rcu_head *head)
423 struct ipc_rcu_grace *grace =
424 container_of(head, struct ipc_rcu_grace, rcu);
425 struct ipc_rcu_sched *sched =
426 container_of(&(grace->data[0]), struct ipc_rcu_sched, data[0]);
428 INIT_WORK(&sched->work, vfree, sched);
429 schedule_work(&sched->work);
433 * ipc_immediate_free - free ipc + rcu space
434 * @head: RCU callback structure that contains pointer to be freed
436 * Free from the RCU callback context
438 static void ipc_immediate_free(struct rcu_head *head)
440 struct ipc_rcu_grace *free =
441 container_of(head, struct ipc_rcu_grace, rcu);
445 void ipc_rcu_putref(void *ptr)
447 if (--container_of(ptr, struct ipc_rcu_hdr, data)->refcount > 0)
450 if (container_of(ptr, struct ipc_rcu_hdr, data)->is_vmalloc) {
451 call_rcu(&container_of(ptr, struct ipc_rcu_grace, data)->rcu,
454 call_rcu(&container_of(ptr, struct ipc_rcu_grace, data)->rcu,
460 * ipcperms - check IPC permissions
461 * @ipcp: IPC permission set
462 * @flag: desired permission set.
464 * Check user, group, other permissions for access
465 * to ipc resources. return 0 if allowed
468 int ipcperms (struct kern_ipc_perm *ipcp, short flag)
469 { /* flag will most probably be 0 or S_...UGO from <linux/stat.h> */
470 int requested_mode, granted_mode;
472 if (!vx_check(ipcp->xid, VX_ADMIN|VX_IDENT)) /* maybe just VX_IDENT? */
474 requested_mode = (flag >> 6) | (flag >> 3) | flag;
475 granted_mode = ipcp->mode;
476 if (current->euid == ipcp->cuid || current->euid == ipcp->uid)
478 else if (in_group_p(ipcp->cgid) || in_group_p(ipcp->gid))
480 /* is there some bit set in requested_mode but not in granted_mode? */
481 if ((requested_mode & ~granted_mode & 0007) &&
482 !capable(CAP_IPC_OWNER))
485 return security_ipc_permission(ipcp, flag);
489 * Functions to convert between the kern_ipc_perm structure and the
490 * old/new ipc_perm structures
494 * kernel_to_ipc64_perm - convert kernel ipc permissions to user
495 * @in: kernel permissions
496 * @out: new style IPC permissions
498 * Turn the kernel object 'in' into a set of permissions descriptions
499 * for returning to userspace (out).
503 void kernel_to_ipc64_perm (struct kern_ipc_perm *in, struct ipc64_perm *out)
508 out->cuid = in->cuid;
509 out->cgid = in->cgid;
510 out->mode = in->mode;
515 * ipc64_perm_to_ipc_perm - convert old ipc permissions to new
516 * @in: new style IPC permissions
517 * @out: old style IPC permissions
519 * Turn the new style permissions object in into a compatibility
520 * object and store it into the 'out' pointer.
523 void ipc64_perm_to_ipc_perm (struct ipc64_perm *in, struct ipc_perm *out)
526 SET_UID(out->uid, in->uid);
527 SET_GID(out->gid, in->gid);
528 SET_UID(out->cuid, in->cuid);
529 SET_GID(out->cgid, in->cgid);
530 out->mode = in->mode;
535 * So far only shm_get_stat() calls ipc_get() via shm_get(), so ipc_get()
536 * is called with shm_ids.sem locked. Since grow_ary() is also called with
537 * shm_ids.sem down(for Shared Memory), there is no need to add read
538 * barriers here to gurantee the writes in grow_ary() are seen in order
541 * However ipc_get() itself does not necessary require ipc_ids.sem down. So
542 * if in the future ipc_get() is used by other places without ipc_ids.sem
543 * down, then ipc_get() needs read memery barriers as ipc_lock() does.
545 struct kern_ipc_perm* ipc_get(struct ipc_ids* ids, int id)
547 struct kern_ipc_perm* out;
548 int lid = id % SEQ_MULTIPLIER;
549 if(lid >= ids->entries->size)
551 out = ids->entries->p[lid];
555 struct kern_ipc_perm* ipc_lock(struct ipc_ids* ids, int id)
557 struct kern_ipc_perm* out;
558 int lid = id % SEQ_MULTIPLIER;
559 struct ipc_id_ary* entries;
562 entries = rcu_dereference(ids->entries);
563 if(lid >= entries->size) {
567 out = entries->p[lid];
572 spin_lock(&out->lock);
574 /* ipc_rmid() may have already freed the ID while ipc_lock
575 * was spinning: here verify that the structure is still valid
578 spin_unlock(&out->lock);
585 void ipc_lock_by_ptr(struct kern_ipc_perm *perm)
588 spin_lock(&perm->lock);
591 void ipc_unlock(struct kern_ipc_perm* perm)
593 spin_unlock(&perm->lock);
597 int ipc_buildid(struct ipc_ids* ids, int id, int seq)
599 return SEQ_MULTIPLIER*seq + id;
602 int ipc_checkid(struct ipc_ids* ids, struct kern_ipc_perm* ipcp, int uid)
604 if(uid/SEQ_MULTIPLIER != ipcp->seq)
609 #ifdef __ARCH_WANT_IPC_PARSE_VERSION
613 * ipc_parse_version - IPC call version
614 * @cmd: pointer to command
616 * Return IPC_64 for new style IPC and IPC_OLD for old style IPC.
617 * The cmd value is turned from an encoding command and version into
618 * just the command code.
621 int ipc_parse_version (int *cmd)
631 #endif /* __ARCH_WANT_IPC_PARSE_VERSION */
633 #ifdef CONFIG_PROC_FS
634 static void *sysvipc_proc_next(struct seq_file *s, void *it, loff_t *pos)
636 struct ipc_proc_iface *iface = s->private;
637 struct kern_ipc_perm *ipc = it;
640 /* If we had an ipc id locked before, unlock it */
641 if (ipc && ipc != SEQ_START_TOKEN)
645 * p = *pos - 1 (because id 0 starts at position 1)
646 * + 1 (because we increment the position by one)
648 for (p = *pos; p <= iface->ids->max_id; p++) {
649 if ((ipc = ipc_lock(iface->ids, p)) != NULL) {
655 /* Out of range - return NULL to terminate iteration */
660 * File positions: pos 0 -> header, pos n -> ipc id + 1.
661 * SeqFile iterator: iterator value locked shp or SEQ_TOKEN_START.
663 static void *sysvipc_proc_start(struct seq_file *s, loff_t *pos)
665 struct ipc_proc_iface *iface = s->private;
666 struct kern_ipc_perm *ipc;
670 * Take the lock - this will be released by the corresponding
673 down(&iface->ids->sem);
675 /* pos < 0 is invalid */
679 /* pos == 0 means header */
681 return SEQ_START_TOKEN;
683 /* Find the (pos-1)th ipc */
684 for (p = *pos - 1; p <= iface->ids->max_id; p++) {
685 if ((ipc = ipc_lock(iface->ids, p)) != NULL) {
693 static void sysvipc_proc_stop(struct seq_file *s, void *it)
695 struct kern_ipc_perm *ipc = it;
696 struct ipc_proc_iface *iface = s->private;
698 /* If we had a locked segment, release it */
699 if (ipc && ipc != SEQ_START_TOKEN)
702 /* Release the lock we took in start() */
703 up(&iface->ids->sem);
706 static int sysvipc_proc_show(struct seq_file *s, void *it)
708 struct ipc_proc_iface *iface = s->private;
710 if (it == SEQ_START_TOKEN)
711 return seq_puts(s, iface->header);
713 return iface->show(s, it);
716 static struct seq_operations sysvipc_proc_seqops = {
717 .start = sysvipc_proc_start,
718 .stop = sysvipc_proc_stop,
719 .next = sysvipc_proc_next,
720 .show = sysvipc_proc_show,
723 static int sysvipc_proc_open(struct inode *inode, struct file *file) {
725 struct seq_file *seq;
727 ret = seq_open(file, &sysvipc_proc_seqops);
729 seq = file->private_data;
730 seq->private = PDE(inode)->data;
735 static struct file_operations sysvipc_proc_fops = {
736 .open = sysvipc_proc_open,
739 .release = seq_release,
741 #endif /* CONFIG_PROC_FS */