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>
18 #include <linux/shm.h>
19 #include <linux/init.h>
20 #include <linux/msg.h>
21 #include <linux/smp_lock.h>
22 #include <linux/vmalloc.h>
23 #include <linux/slab.h>
24 #include <linux/capability.h>
25 #include <linux/highuid.h>
26 #include <linux/security.h>
27 #include <linux/rcupdate.h>
28 #include <linux/workqueue.h>
29 #include <linux/seq_file.h>
30 #include <linux/proc_fs.h>
31 #include <linux/audit.h>
33 #include <asm/unistd.h>
37 struct ipc_proc_iface
{
41 int (*show
)(struct seq_file
*, void *);
45 * ipc_init - initialise IPC subsystem
47 * The various system5 IPC resources (semaphores, messages and shared
48 * memory are initialised
51 static int __init
ipc_init(void)
61 * ipc_init_ids - initialise IPC identifiers
62 * @ids: Identifier set
63 * @size: Number of identifiers
65 * Given a size for the ipc identifier range (limited below IPCMNI)
66 * set up the sequence range to use then allocate and initialise the
70 void __init
ipc_init_ids(struct ipc_ids
* ids
, int size
)
74 mutex_init(&ids
->mutex
);
82 int seq_limit
= INT_MAX
/SEQ_MULTIPLIER
;
83 if(seq_limit
> USHRT_MAX
)
84 ids
->seq_max
= USHRT_MAX
;
86 ids
->seq_max
= seq_limit
;
89 ids
->entries
= ipc_rcu_alloc(sizeof(struct kern_ipc_perm
*)*size
+
90 sizeof(struct ipc_id_ary
));
92 if(ids
->entries
== NULL
) {
93 printk(KERN_ERR
"ipc_init_ids() failed, ipc service disabled.\n");
95 ids
->entries
= &ids
->nullentry
;
97 ids
->entries
->size
= size
;
99 ids
->entries
->p
[i
] = NULL
;
102 #ifdef CONFIG_PROC_FS
103 static struct file_operations sysvipc_proc_fops
;
105 * ipc_init_proc_interface - Create a proc interface for sysipc types
106 * using a seq_file interface.
107 * @path: Path in procfs
108 * @header: Banner to be printed at the beginning of the file.
109 * @ids: ipc id table to iterate.
110 * @show: show routine.
112 void __init
ipc_init_proc_interface(const char *path
, const char *header
,
114 int (*show
)(struct seq_file
*, void *))
116 struct proc_dir_entry
*pde
;
117 struct ipc_proc_iface
*iface
;
119 iface
= kmalloc(sizeof(*iface
), GFP_KERNEL
);
123 iface
->header
= header
;
127 pde
= create_proc_entry(path
,
128 S_IRUGO
, /* world readable */
129 NULL
/* parent dir */);
132 pde
->proc_fops
= &sysvipc_proc_fops
;
140 * ipc_findkey - find a key in an ipc identifier set
141 * @ids: Identifier set
142 * @key: The key to find
144 * Requires ipc_ids.mutex locked.
145 * Returns the identifier if found or -1 if not.
148 int ipc_findkey(struct ipc_ids
* ids
, key_t key
)
151 struct kern_ipc_perm
* p
;
152 int max_id
= ids
->max_id
;
155 * rcu_dereference() is not needed here
156 * since ipc_ids.mutex is held
158 for (id
= 0; id
<= max_id
; id
++) {
159 p
= ids
->entries
->p
[id
];
169 * Requires ipc_ids.mutex locked
171 static int grow_ary(struct ipc_ids
* ids
, int newsize
)
173 struct ipc_id_ary
* new;
174 struct ipc_id_ary
* old
;
176 int size
= ids
->entries
->size
;
183 new = ipc_rcu_alloc(sizeof(struct kern_ipc_perm
*)*newsize
+
184 sizeof(struct ipc_id_ary
));
188 memcpy(new->p
, ids
->entries
->p
, sizeof(struct kern_ipc_perm
*)*size
);
189 for(i
=size
;i
<newsize
;i
++) {
195 * Use rcu_assign_pointer() to make sure the memcpyed contents
196 * of the new array are visible before the new array becomes visible.
198 rcu_assign_pointer(ids
->entries
, new);
205 * ipc_addid - add an IPC identifier
206 * @ids: IPC identifier set
207 * @new: new IPC permission set
208 * @size: new size limit for the id array
210 * Add an entry 'new' to the IPC arrays. The permissions object is
211 * initialised and the first free entry is set up and the id assigned
212 * is returned. The list is returned in a locked state on success.
213 * On failure the list is not locked and -1 is returned.
215 * Called with ipc_ids.mutex held.
218 int ipc_addid(struct ipc_ids
* ids
, struct kern_ipc_perm
* new, int size
)
222 size
= grow_ary(ids
,size
);
225 * rcu_dereference()() is not needed here since
226 * ipc_ids.mutex is held
228 for (id
= 0; id
< size
; id
++) {
229 if(ids
->entries
->p
[id
] == NULL
)
235 if (id
> ids
->max_id
)
238 new->cuid
= new->uid
= current
->euid
;
239 new->gid
= new->cgid
= current
->egid
;
241 new->seq
= ids
->seq
++;
242 if(ids
->seq
> ids
->seq_max
)
245 spin_lock_init(&new->lock
);
248 spin_lock(&new->lock
);
249 ids
->entries
->p
[id
] = new;
254 * ipc_rmid - remove an IPC identifier
255 * @ids: identifier set
256 * @id: Identifier to remove
258 * The identifier must be valid, and in use. The kernel will panic if
259 * fed an invalid identifier. The entry is removed and internal
260 * variables recomputed. The object associated with the identifier
262 * ipc_ids.mutex and the spinlock for this ID is hold before this function
263 * is called, and remain locked on the exit.
266 struct kern_ipc_perm
* ipc_rmid(struct ipc_ids
* ids
, int id
)
268 struct kern_ipc_perm
* p
;
269 int lid
= id
% SEQ_MULTIPLIER
;
270 BUG_ON(lid
>= ids
->entries
->size
);
273 * do not need a rcu_dereference()() here to force ordering
274 * on Alpha, since the ipc_ids.mutex is held.
276 p
= ids
->entries
->p
[lid
];
277 ids
->entries
->p
[lid
] = NULL
;
281 if (lid
== ids
->max_id
) {
286 } while (ids
->entries
->p
[lid
] == NULL
);
294 * ipc_alloc - allocate ipc space
295 * @size: size desired
297 * Allocate memory from the appropriate pools and return a pointer to it.
298 * NULL is returned if the allocation fails
301 void* ipc_alloc(int size
)
307 out
= kmalloc(size
, GFP_KERNEL
);
312 * ipc_free - free ipc space
313 * @ptr: pointer returned by ipc_alloc
314 * @size: size of block
316 * Free a block created with ipc_alloc. The caller must know the size
317 * used in the allocation call.
320 void ipc_free(void* ptr
, int size
)
330 * There are three headers that are prepended to the actual allocation:
331 * - during use: ipc_rcu_hdr.
332 * - during the rcu grace period: ipc_rcu_grace.
333 * - [only if vmalloc]: ipc_rcu_sched.
334 * Their lifetime doesn't overlap, thus the headers share the same memory.
335 * Unlike a normal union, they are right-aligned, thus some container_of
336 * forward/backward casting is necessary:
349 /* "void *" makes sure alignment of following data is sane. */
355 struct work_struct work
;
356 /* "void *" makes sure alignment of following data is sane. */
360 #define HDRLEN_KMALLOC (sizeof(struct ipc_rcu_grace) > sizeof(struct ipc_rcu_hdr) ? \
361 sizeof(struct ipc_rcu_grace) : sizeof(struct ipc_rcu_hdr))
362 #define HDRLEN_VMALLOC (sizeof(struct ipc_rcu_sched) > HDRLEN_KMALLOC ? \
363 sizeof(struct ipc_rcu_sched) : HDRLEN_KMALLOC)
365 static inline int rcu_use_vmalloc(int size
)
367 /* Too big for a single page? */
368 if (HDRLEN_KMALLOC
+ size
> PAGE_SIZE
)
374 * ipc_rcu_alloc - allocate ipc and rcu space
375 * @size: size desired
377 * Allocate memory for the rcu header structure + the object.
378 * Returns the pointer to the object.
379 * NULL is returned if the allocation fails.
382 void* ipc_rcu_alloc(int size
)
386 * We prepend the allocation with the rcu struct, and
387 * workqueue if necessary (for vmalloc).
389 if (rcu_use_vmalloc(size
)) {
390 out
= vmalloc(HDRLEN_VMALLOC
+ size
);
392 out
+= HDRLEN_VMALLOC
;
393 container_of(out
, struct ipc_rcu_hdr
, data
)->is_vmalloc
= 1;
394 container_of(out
, struct ipc_rcu_hdr
, data
)->refcount
= 1;
397 out
= kmalloc(HDRLEN_KMALLOC
+ size
, GFP_KERNEL
);
399 out
+= HDRLEN_KMALLOC
;
400 container_of(out
, struct ipc_rcu_hdr
, data
)->is_vmalloc
= 0;
401 container_of(out
, struct ipc_rcu_hdr
, data
)->refcount
= 1;
408 void ipc_rcu_getref(void *ptr
)
410 container_of(ptr
, struct ipc_rcu_hdr
, data
)->refcount
++;
414 * ipc_schedule_free - free ipc + rcu space
415 * @head: RCU callback structure for queued work
417 * Since RCU callback function is called in bh,
418 * we need to defer the vfree to schedule_work
420 static void ipc_schedule_free(struct rcu_head
*head
)
422 struct ipc_rcu_grace
*grace
=
423 container_of(head
, struct ipc_rcu_grace
, rcu
);
424 struct ipc_rcu_sched
*sched
=
425 container_of(&(grace
->data
[0]), struct ipc_rcu_sched
, data
[0]);
427 INIT_WORK(&sched
->work
, vfree
, sched
);
428 schedule_work(&sched
->work
);
432 * ipc_immediate_free - free ipc + rcu space
433 * @head: RCU callback structure that contains pointer to be freed
435 * Free from the RCU callback context
437 static void ipc_immediate_free(struct rcu_head
*head
)
439 struct ipc_rcu_grace
*free
=
440 container_of(head
, struct ipc_rcu_grace
, rcu
);
444 void ipc_rcu_putref(void *ptr
)
446 if (--container_of(ptr
, struct ipc_rcu_hdr
, data
)->refcount
> 0)
449 if (container_of(ptr
, struct ipc_rcu_hdr
, data
)->is_vmalloc
) {
450 call_rcu(&container_of(ptr
, struct ipc_rcu_grace
, data
)->rcu
,
453 call_rcu(&container_of(ptr
, struct ipc_rcu_grace
, data
)->rcu
,
459 * ipcperms - check IPC permissions
460 * @ipcp: IPC permission set
461 * @flag: desired permission set.
463 * Check user, group, other permissions for access
464 * to ipc resources. return 0 if allowed
467 int ipcperms (struct kern_ipc_perm
*ipcp
, short flag
)
468 { /* flag will most probably be 0 or S_...UGO from <linux/stat.h> */
469 int requested_mode
, granted_mode
, err
;
471 if (unlikely((err
= audit_ipc_obj(ipcp
))))
473 requested_mode
= (flag
>> 6) | (flag
>> 3) | flag
;
474 granted_mode
= ipcp
->mode
;
475 if (current
->euid
== ipcp
->cuid
|| current
->euid
== ipcp
->uid
)
477 else if (in_group_p(ipcp
->cgid
) || in_group_p(ipcp
->gid
))
479 /* is there some bit set in requested_mode but not in granted_mode? */
480 if ((requested_mode
& ~granted_mode
& 0007) &&
481 !capable(CAP_IPC_OWNER
))
484 return security_ipc_permission(ipcp
, flag
);
488 * Functions to convert between the kern_ipc_perm structure and the
489 * old/new ipc_perm structures
493 * kernel_to_ipc64_perm - convert kernel ipc permissions to user
494 * @in: kernel permissions
495 * @out: new style IPC permissions
497 * Turn the kernel object 'in' into a set of permissions descriptions
498 * for returning to userspace (out).
502 void kernel_to_ipc64_perm (struct kern_ipc_perm
*in
, struct ipc64_perm
*out
)
507 out
->cuid
= in
->cuid
;
508 out
->cgid
= in
->cgid
;
509 out
->mode
= in
->mode
;
514 * ipc64_perm_to_ipc_perm - convert old ipc permissions to new
515 * @in: new style IPC permissions
516 * @out: old style IPC permissions
518 * Turn the new style permissions object in into a compatibility
519 * object and store it into the 'out' pointer.
522 void ipc64_perm_to_ipc_perm (struct ipc64_perm
*in
, struct ipc_perm
*out
)
525 SET_UID(out
->uid
, in
->uid
);
526 SET_GID(out
->gid
, in
->gid
);
527 SET_UID(out
->cuid
, in
->cuid
);
528 SET_GID(out
->cgid
, in
->cgid
);
529 out
->mode
= in
->mode
;
534 * So far only shm_get_stat() calls ipc_get() via shm_get(), so ipc_get()
535 * is called with shm_ids.mutex locked. Since grow_ary() is also called with
536 * shm_ids.mutex down(for Shared Memory), there is no need to add read
537 * barriers here to gurantee the writes in grow_ary() are seen in order
540 * However ipc_get() itself does not necessary require ipc_ids.mutex down. So
541 * if in the future ipc_get() is used by other places without ipc_ids.mutex
542 * down, then ipc_get() needs read memery barriers as ipc_lock() does.
544 struct kern_ipc_perm
* ipc_get(struct ipc_ids
* ids
, int id
)
546 struct kern_ipc_perm
* out
;
547 int lid
= id
% SEQ_MULTIPLIER
;
548 if(lid
>= ids
->entries
->size
)
550 out
= ids
->entries
->p
[lid
];
554 struct kern_ipc_perm
* ipc_lock(struct ipc_ids
* ids
, int id
)
556 struct kern_ipc_perm
* out
;
557 int lid
= id
% SEQ_MULTIPLIER
;
558 struct ipc_id_ary
* entries
;
561 entries
= rcu_dereference(ids
->entries
);
562 if(lid
>= entries
->size
) {
566 out
= entries
->p
[lid
];
571 spin_lock(&out
->lock
);
573 /* ipc_rmid() may have already freed the ID while ipc_lock
574 * was spinning: here verify that the structure is still valid
577 spin_unlock(&out
->lock
);
584 void ipc_lock_by_ptr(struct kern_ipc_perm
*perm
)
587 spin_lock(&perm
->lock
);
590 void ipc_unlock(struct kern_ipc_perm
* perm
)
592 spin_unlock(&perm
->lock
);
596 int ipc_buildid(struct ipc_ids
* ids
, int id
, int seq
)
598 return SEQ_MULTIPLIER
*seq
+ id
;
601 int ipc_checkid(struct ipc_ids
* ids
, struct kern_ipc_perm
* ipcp
, int uid
)
603 if(uid
/SEQ_MULTIPLIER
!= ipcp
->seq
)
608 #ifdef __ARCH_WANT_IPC_PARSE_VERSION
612 * ipc_parse_version - IPC call version
613 * @cmd: pointer to command
615 * Return IPC_64 for new style IPC and IPC_OLD for old style IPC.
616 * The cmd value is turned from an encoding command and version into
617 * just the command code.
620 int ipc_parse_version (int *cmd
)
630 #endif /* __ARCH_WANT_IPC_PARSE_VERSION */
632 #ifdef CONFIG_PROC_FS
633 static void *sysvipc_proc_next(struct seq_file
*s
, void *it
, loff_t
*pos
)
635 struct ipc_proc_iface
*iface
= s
->private;
636 struct kern_ipc_perm
*ipc
= it
;
639 /* If we had an ipc id locked before, unlock it */
640 if (ipc
&& ipc
!= SEQ_START_TOKEN
)
644 * p = *pos - 1 (because id 0 starts at position 1)
645 * + 1 (because we increment the position by one)
647 for (p
= *pos
; p
<= iface
->ids
->max_id
; p
++) {
648 if ((ipc
= ipc_lock(iface
->ids
, p
)) != NULL
) {
654 /* Out of range - return NULL to terminate iteration */
659 * File positions: pos 0 -> header, pos n -> ipc id + 1.
660 * SeqFile iterator: iterator value locked shp or SEQ_TOKEN_START.
662 static void *sysvipc_proc_start(struct seq_file
*s
, loff_t
*pos
)
664 struct ipc_proc_iface
*iface
= s
->private;
665 struct kern_ipc_perm
*ipc
;
669 * Take the lock - this will be released by the corresponding
672 mutex_lock(&iface
->ids
->mutex
);
674 /* pos < 0 is invalid */
678 /* pos == 0 means header */
680 return SEQ_START_TOKEN
;
682 /* Find the (pos-1)th ipc */
683 for (p
= *pos
- 1; p
<= iface
->ids
->max_id
; p
++) {
684 if ((ipc
= ipc_lock(iface
->ids
, p
)) != NULL
) {
692 static void sysvipc_proc_stop(struct seq_file
*s
, void *it
)
694 struct kern_ipc_perm
*ipc
= it
;
695 struct ipc_proc_iface
*iface
= s
->private;
697 /* If we had a locked segment, release it */
698 if (ipc
&& ipc
!= SEQ_START_TOKEN
)
701 /* Release the lock we took in start() */
702 mutex_unlock(&iface
->ids
->mutex
);
705 static int sysvipc_proc_show(struct seq_file
*s
, void *it
)
707 struct ipc_proc_iface
*iface
= s
->private;
709 if (it
== SEQ_START_TOKEN
)
710 return seq_puts(s
, iface
->header
);
712 return iface
->show(s
, it
);
715 static struct seq_operations sysvipc_proc_seqops
= {
716 .start
= sysvipc_proc_start
,
717 .stop
= sysvipc_proc_stop
,
718 .next
= sysvipc_proc_next
,
719 .show
= sysvipc_proc_show
,
722 static int sysvipc_proc_open(struct inode
*inode
, struct file
*file
) {
724 struct seq_file
*seq
;
726 ret
= seq_open(file
, &sysvipc_proc_seqops
);
728 seq
= file
->private_data
;
729 seq
->private = PDE(inode
)->data
;
734 static struct file_operations sysvipc_proc_fops
= {
735 .open
= sysvipc_proc_open
,
738 .release
= seq_release
,
740 #endif /* CONFIG_PROC_FS */