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/ipc_namespace.h>
38 #include <asm/unistd.h>
42 struct ipc_proc_iface
{
46 int (*show
)(struct seq_file
*, void *);
49 struct ipc_namespace init_ipc_ns
= {
51 .refcount
= ATOMIC_INIT(2),
56 * ipc_init - initialise IPC subsystem
58 * The various system5 IPC resources (semaphores, messages and shared
59 * memory) are initialised
62 static int __init
ipc_init(void)
72 * ipc_init_ids - initialise IPC identifiers
73 * @ids: Identifier set
75 * Set up the sequence range to use for the ipc identifier range (limited
76 * below IPCMNI) then initialise the ids idr.
79 void ipc_init_ids(struct ipc_ids
*ids
)
81 init_rwsem(&ids
->rw_mutex
);
86 int seq_limit
= INT_MAX
/SEQ_MULTIPLIER
;
87 if(seq_limit
> USHRT_MAX
)
88 ids
->seq_max
= USHRT_MAX
;
90 ids
->seq_max
= seq_limit
;
93 idr_init(&ids
->ipcs_idr
);
97 static const struct file_operations sysvipc_proc_fops
;
99 * ipc_init_proc_interface - Create a proc interface for sysipc types using a seq_file interface.
100 * @path: Path in procfs
101 * @header: Banner to be printed at the beginning of the file.
102 * @ids: ipc id table to iterate.
103 * @show: show routine.
105 void __init
ipc_init_proc_interface(const char *path
, const char *header
,
106 int ids
, int (*show
)(struct seq_file
*, void *))
108 struct proc_dir_entry
*pde
;
109 struct ipc_proc_iface
*iface
;
111 iface
= kmalloc(sizeof(*iface
), GFP_KERNEL
);
115 iface
->header
= header
;
119 pde
= create_proc_entry(path
,
120 S_IRUGO
, /* world readable */
121 NULL
/* parent dir */);
124 pde
->proc_fops
= &sysvipc_proc_fops
;
132 * ipc_findkey - find a key in an ipc identifier set
133 * @ids: Identifier set
134 * @key: The key to find
136 * Requires ipc_ids.rw_mutex locked.
137 * Returns the LOCKED pointer to the ipc structure if found or NULL
139 * If key is found ipc points to the owning ipc structure
142 static struct kern_ipc_perm
*ipc_findkey(struct ipc_ids
*ids
, key_t key
)
144 struct kern_ipc_perm
*ipc
;
148 for (total
= 0, next_id
= 0; total
< ids
->in_use
; next_id
++) {
149 ipc
= idr_find(&ids
->ipcs_idr
, next_id
);
154 if (ipc
->key
!= key
) {
159 ipc_lock_by_ptr(ipc
);
167 * ipc_get_maxid - get the last assigned id
168 * @ids: IPC identifier set
170 * Called with ipc_ids.rw_mutex held.
173 int ipc_get_maxid(struct ipc_ids
*ids
)
175 struct kern_ipc_perm
*ipc
;
179 if (ids
->in_use
== 0)
182 if (ids
->in_use
== IPCMNI
)
185 /* Look for the last assigned id */
187 for (id
= 0; id
< IPCMNI
&& total
< ids
->in_use
; id
++) {
188 ipc
= idr_find(&ids
->ipcs_idr
, id
);
198 * ipc_addid - add an IPC identifier
199 * @ids: IPC identifier set
200 * @new: new IPC permission set
201 * @size: limit for the number of used ids
203 * Add an entry 'new' to the IPC ids idr. The permissions object is
204 * initialised and the first free entry is set up and the id assigned
205 * is returned. The 'new' entry is returned in a locked state on success.
206 * On failure the entry is not locked and a negative err-code is returned.
208 * Called with ipc_ids.rw_mutex held as a writer.
211 int ipc_addid(struct ipc_ids
* ids
, struct kern_ipc_perm
* new, int size
)
218 if (ids
->in_use
>= size
)
221 err
= idr_get_new(&ids
->ipcs_idr
, new, &id
);
227 new->cuid
= new->uid
= current
->euid
;
228 new->gid
= new->cgid
= current
->egid
;
230 new->seq
= ids
->seq
++;
231 if(ids
->seq
> ids
->seq_max
)
234 new->id
= ipc_buildid(id
, new->seq
);
235 spin_lock_init(&new->lock
);
238 spin_lock(&new->lock
);
243 * ipcget_new - create a new ipc object
245 * @ids: IPC identifer set
246 * @ops: the actual creation routine to call
247 * @params: its parameters
249 * This routine is called by sys_msgget, sys_semget() and sys_shmget()
250 * when the key is IPC_PRIVATE.
252 static int ipcget_new(struct ipc_namespace
*ns
, struct ipc_ids
*ids
,
253 struct ipc_ops
*ops
, struct ipc_params
*params
)
257 err
= idr_pre_get(&ids
->ipcs_idr
, GFP_KERNEL
);
262 down_write(&ids
->rw_mutex
);
263 err
= ops
->getnew(ns
, params
);
264 up_write(&ids
->rw_mutex
);
273 * ipc_check_perms - check security and permissions for an IPC
274 * @ipcp: ipc permission set
275 * @ops: the actual security routine to call
276 * @params: its parameters
278 * This routine is called by sys_msgget(), sys_semget() and sys_shmget()
279 * when the key is not IPC_PRIVATE and that key already exists in the
282 * On success, the IPC id is returned.
284 * It is called with ipc_ids.rw_mutex and ipcp->lock held.
286 static int ipc_check_perms(struct kern_ipc_perm
*ipcp
, struct ipc_ops
*ops
,
287 struct ipc_params
*params
)
291 if (ipcperms(ipcp
, params
->flg
))
294 err
= ops
->associate(ipcp
, params
->flg
);
303 * ipcget_public - get an ipc object or create a new one
305 * @ids: IPC identifer set
306 * @ops: the actual creation routine to call
307 * @params: its parameters
309 * This routine is called by sys_msgget, sys_semget() and sys_shmget()
310 * when the key is not IPC_PRIVATE.
311 * It adds a new entry if the key is not found and does some permission
312 * / security checkings if the key is found.
314 * On success, the ipc id is returned.
316 static int ipcget_public(struct ipc_namespace
*ns
, struct ipc_ids
*ids
,
317 struct ipc_ops
*ops
, struct ipc_params
*params
)
319 struct kern_ipc_perm
*ipcp
;
320 int flg
= params
->flg
;
323 err
= idr_pre_get(&ids
->ipcs_idr
, GFP_KERNEL
);
326 * Take the lock as a writer since we are potentially going to add
327 * a new entry + read locks are not "upgradable"
329 down_write(&ids
->rw_mutex
);
330 ipcp
= ipc_findkey(ids
, params
->key
);
333 if (!(flg
& IPC_CREAT
))
338 err
= ops
->getnew(ns
, params
);
340 /* ipc object has been locked by ipc_findkey() */
342 if (flg
& IPC_CREAT
&& flg
& IPC_EXCL
)
346 if (ops
->more_checks
)
347 err
= ops
->more_checks(ipcp
, params
);
350 * ipc_check_perms returns the IPC id on
353 err
= ipc_check_perms(ipcp
, ops
, params
);
357 up_write(&ids
->rw_mutex
);
367 * ipc_rmid - remove an IPC identifier
368 * @ids: IPC identifier set
369 * @ipcp: ipc perm structure containing the identifier to remove
371 * ipc_ids.rw_mutex (as a writer) and the spinlock for this ID are held
372 * before this function is called, and remain locked on the exit.
375 void ipc_rmid(struct ipc_ids
*ids
, struct kern_ipc_perm
*ipcp
)
377 int lid
= ipcid_to_idx(ipcp
->id
);
379 idr_remove(&ids
->ipcs_idr
, lid
);
389 * ipc_alloc - allocate ipc space
390 * @size: size desired
392 * Allocate memory from the appropriate pools and return a pointer to it.
393 * NULL is returned if the allocation fails
396 void* ipc_alloc(int size
)
402 out
= kmalloc(size
, GFP_KERNEL
);
407 * ipc_free - free ipc space
408 * @ptr: pointer returned by ipc_alloc
409 * @size: size of block
411 * Free a block created with ipc_alloc(). The caller must know the size
412 * used in the allocation call.
415 void ipc_free(void* ptr
, int size
)
425 * There are three headers that are prepended to the actual allocation:
426 * - during use: ipc_rcu_hdr.
427 * - during the rcu grace period: ipc_rcu_grace.
428 * - [only if vmalloc]: ipc_rcu_sched.
429 * Their lifetime doesn't overlap, thus the headers share the same memory.
430 * Unlike a normal union, they are right-aligned, thus some container_of
431 * forward/backward casting is necessary:
444 /* "void *" makes sure alignment of following data is sane. */
450 struct work_struct work
;
451 /* "void *" makes sure alignment of following data is sane. */
455 #define HDRLEN_KMALLOC (sizeof(struct ipc_rcu_grace) > sizeof(struct ipc_rcu_hdr) ? \
456 sizeof(struct ipc_rcu_grace) : sizeof(struct ipc_rcu_hdr))
457 #define HDRLEN_VMALLOC (sizeof(struct ipc_rcu_sched) > HDRLEN_KMALLOC ? \
458 sizeof(struct ipc_rcu_sched) : HDRLEN_KMALLOC)
460 static inline int rcu_use_vmalloc(int size
)
462 /* Too big for a single page? */
463 if (HDRLEN_KMALLOC
+ size
> PAGE_SIZE
)
469 * ipc_rcu_alloc - allocate ipc and rcu space
470 * @size: size desired
472 * Allocate memory for the rcu header structure + the object.
473 * Returns the pointer to the object.
474 * NULL is returned if the allocation fails.
477 void* ipc_rcu_alloc(int size
)
481 * We prepend the allocation with the rcu struct, and
482 * workqueue if necessary (for vmalloc).
484 if (rcu_use_vmalloc(size
)) {
485 out
= vmalloc(HDRLEN_VMALLOC
+ size
);
487 out
+= HDRLEN_VMALLOC
;
488 container_of(out
, struct ipc_rcu_hdr
, data
)->is_vmalloc
= 1;
489 container_of(out
, struct ipc_rcu_hdr
, data
)->refcount
= 1;
492 out
= kmalloc(HDRLEN_KMALLOC
+ size
, GFP_KERNEL
);
494 out
+= HDRLEN_KMALLOC
;
495 container_of(out
, struct ipc_rcu_hdr
, data
)->is_vmalloc
= 0;
496 container_of(out
, struct ipc_rcu_hdr
, data
)->refcount
= 1;
503 void ipc_rcu_getref(void *ptr
)
505 container_of(ptr
, struct ipc_rcu_hdr
, data
)->refcount
++;
508 static void ipc_do_vfree(struct work_struct
*work
)
510 vfree(container_of(work
, struct ipc_rcu_sched
, work
));
514 * ipc_schedule_free - free ipc + rcu space
515 * @head: RCU callback structure for queued work
517 * Since RCU callback function is called in bh,
518 * we need to defer the vfree to schedule_work().
520 static void ipc_schedule_free(struct rcu_head
*head
)
522 struct ipc_rcu_grace
*grace
;
523 struct ipc_rcu_sched
*sched
;
525 grace
= container_of(head
, struct ipc_rcu_grace
, rcu
);
526 sched
= container_of(&(grace
->data
[0]), struct ipc_rcu_sched
,
529 INIT_WORK(&sched
->work
, ipc_do_vfree
);
530 schedule_work(&sched
->work
);
534 * ipc_immediate_free - free ipc + rcu space
535 * @head: RCU callback structure that contains pointer to be freed
537 * Free from the RCU callback context.
539 static void ipc_immediate_free(struct rcu_head
*head
)
541 struct ipc_rcu_grace
*free
=
542 container_of(head
, struct ipc_rcu_grace
, rcu
);
546 void ipc_rcu_putref(void *ptr
)
548 if (--container_of(ptr
, struct ipc_rcu_hdr
, data
)->refcount
> 0)
551 if (container_of(ptr
, struct ipc_rcu_hdr
, data
)->is_vmalloc
) {
552 call_rcu(&container_of(ptr
, struct ipc_rcu_grace
, data
)->rcu
,
555 call_rcu(&container_of(ptr
, struct ipc_rcu_grace
, data
)->rcu
,
561 * ipcperms - check IPC permissions
562 * @ipcp: IPC permission set
563 * @flag: desired permission set.
565 * Check user, group, other permissions for access
566 * to ipc resources. return 0 if allowed
569 int ipcperms (struct kern_ipc_perm
*ipcp
, short flag
)
570 { /* flag will most probably be 0 or S_...UGO from <linux/stat.h> */
571 int requested_mode
, granted_mode
, err
;
573 if (unlikely((err
= audit_ipc_obj(ipcp
))))
575 requested_mode
= (flag
>> 6) | (flag
>> 3) | flag
;
576 granted_mode
= ipcp
->mode
;
577 if (current
->euid
== ipcp
->cuid
|| current
->euid
== ipcp
->uid
)
579 else if (in_group_p(ipcp
->cgid
) || in_group_p(ipcp
->gid
))
581 /* is there some bit set in requested_mode but not in granted_mode? */
582 if ((requested_mode
& ~granted_mode
& 0007) &&
583 !capable(CAP_IPC_OWNER
))
586 return security_ipc_permission(ipcp
, flag
);
590 * Functions to convert between the kern_ipc_perm structure and the
591 * old/new ipc_perm structures
595 * kernel_to_ipc64_perm - convert kernel ipc permissions to user
596 * @in: kernel permissions
597 * @out: new style IPC permissions
599 * Turn the kernel object @in into a set of permissions descriptions
600 * for returning to userspace (@out).
604 void kernel_to_ipc64_perm (struct kern_ipc_perm
*in
, struct ipc64_perm
*out
)
609 out
->cuid
= in
->cuid
;
610 out
->cgid
= in
->cgid
;
611 out
->mode
= in
->mode
;
616 * ipc64_perm_to_ipc_perm - convert new ipc permissions to old
617 * @in: new style IPC permissions
618 * @out: old style IPC permissions
620 * Turn the new style permissions object @in into a compatibility
621 * object and store it into the @out pointer.
624 void ipc64_perm_to_ipc_perm (struct ipc64_perm
*in
, struct ipc_perm
*out
)
627 SET_UID(out
->uid
, in
->uid
);
628 SET_GID(out
->gid
, in
->gid
);
629 SET_UID(out
->cuid
, in
->cuid
);
630 SET_GID(out
->cgid
, in
->cgid
);
631 out
->mode
= in
->mode
;
636 * ipc_lock - Lock an ipc structure without rw_mutex held
637 * @ids: IPC identifier set
638 * @id: ipc id to look for
640 * Look for an id in the ipc ids idr and lock the associated ipc object.
642 * The ipc object is locked on exit.
644 * This is the routine that should be called when the rw_mutex is not already
645 * held, i.e. idr tree not protected: it protects the idr tree in read mode
646 * during the idr_find().
649 struct kern_ipc_perm
*ipc_lock(struct ipc_ids
*ids
, int id
)
651 struct kern_ipc_perm
*out
;
652 int lid
= ipcid_to_idx(id
);
654 down_read(&ids
->rw_mutex
);
657 out
= idr_find(&ids
->ipcs_idr
, lid
);
660 up_read(&ids
->rw_mutex
);
661 return ERR_PTR(-EINVAL
);
664 up_read(&ids
->rw_mutex
);
666 spin_lock(&out
->lock
);
668 /* ipc_rmid() may have already freed the ID while ipc_lock
669 * was spinning: here verify that the structure is still valid
672 spin_unlock(&out
->lock
);
674 return ERR_PTR(-EINVAL
);
681 * ipc_lock_down - Lock an ipc structure with rw_sem held
682 * @ids: IPC identifier set
683 * @id: ipc id to look for
685 * Look for an id in the ipc ids idr and lock the associated ipc object.
687 * The ipc object is locked on exit.
689 * This is the routine that should be called when the rw_mutex is already
690 * held, i.e. idr tree protected.
693 struct kern_ipc_perm
*ipc_lock_down(struct ipc_ids
*ids
, int id
)
695 struct kern_ipc_perm
*out
;
696 int lid
= ipcid_to_idx(id
);
699 out
= idr_find(&ids
->ipcs_idr
, lid
);
702 return ERR_PTR(-EINVAL
);
705 spin_lock(&out
->lock
);
708 * No need to verify that the structure is still valid since the
714 struct kern_ipc_perm
*ipc_lock_check_down(struct ipc_ids
*ids
, int id
)
716 struct kern_ipc_perm
*out
;
718 out
= ipc_lock_down(ids
, id
);
722 if (ipc_checkid(out
, id
)) {
724 return ERR_PTR(-EIDRM
);
730 struct kern_ipc_perm
*ipc_lock_check(struct ipc_ids
*ids
, int id
)
732 struct kern_ipc_perm
*out
;
734 out
= ipc_lock(ids
, id
);
738 if (ipc_checkid(out
, id
)) {
740 return ERR_PTR(-EIDRM
);
747 * ipcget - Common sys_*get() code
749 * @ids : IPC identifier set
750 * @ops : operations to be called on ipc object creation, permission checks
752 * @params : the parameters needed by the previous operations.
754 * Common routine called by sys_msgget(), sys_semget() and sys_shmget().
756 int ipcget(struct ipc_namespace
*ns
, struct ipc_ids
*ids
,
757 struct ipc_ops
*ops
, struct ipc_params
*params
)
759 if (params
->key
== IPC_PRIVATE
)
760 return ipcget_new(ns
, ids
, ops
, params
);
762 return ipcget_public(ns
, ids
, ops
, params
);
765 #ifdef __ARCH_WANT_IPC_PARSE_VERSION
769 * ipc_parse_version - IPC call version
770 * @cmd: pointer to command
772 * Return IPC_64 for new style IPC and IPC_OLD for old style IPC.
773 * The @cmd value is turned from an encoding command and version into
774 * just the command code.
777 int ipc_parse_version (int *cmd
)
787 #endif /* __ARCH_WANT_IPC_PARSE_VERSION */
789 #ifdef CONFIG_PROC_FS
790 struct ipc_proc_iter
{
791 struct ipc_namespace
*ns
;
792 struct ipc_proc_iface
*iface
;
796 * This routine locks the ipc structure found at least at position pos.
798 static struct kern_ipc_perm
*sysvipc_find_ipc(struct ipc_ids
*ids
, loff_t pos
,
801 struct kern_ipc_perm
*ipc
;
805 for (id
= 0; id
< pos
&& total
< ids
->in_use
; id
++) {
806 ipc
= idr_find(&ids
->ipcs_idr
, id
);
811 if (total
>= ids
->in_use
)
814 for ( ; pos
< IPCMNI
; pos
++) {
815 ipc
= idr_find(&ids
->ipcs_idr
, pos
);
818 ipc_lock_by_ptr(ipc
);
823 /* Out of range - return NULL to terminate iteration */
827 static void *sysvipc_proc_next(struct seq_file
*s
, void *it
, loff_t
*pos
)
829 struct ipc_proc_iter
*iter
= s
->private;
830 struct ipc_proc_iface
*iface
= iter
->iface
;
831 struct kern_ipc_perm
*ipc
= it
;
833 /* If we had an ipc id locked before, unlock it */
834 if (ipc
&& ipc
!= SEQ_START_TOKEN
)
837 return sysvipc_find_ipc(&iter
->ns
->ids
[iface
->ids
], *pos
, pos
);
841 * File positions: pos 0 -> header, pos n -> ipc id = n - 1.
842 * SeqFile iterator: iterator value locked ipc pointer or SEQ_TOKEN_START.
844 static void *sysvipc_proc_start(struct seq_file
*s
, loff_t
*pos
)
846 struct ipc_proc_iter
*iter
= s
->private;
847 struct ipc_proc_iface
*iface
= iter
->iface
;
850 ids
= &iter
->ns
->ids
[iface
->ids
];
853 * Take the lock - this will be released by the corresponding
856 down_read(&ids
->rw_mutex
);
858 /* pos < 0 is invalid */
862 /* pos == 0 means header */
864 return SEQ_START_TOKEN
;
866 /* Find the (pos-1)th ipc */
867 return sysvipc_find_ipc(ids
, *pos
- 1, pos
);
870 static void sysvipc_proc_stop(struct seq_file
*s
, void *it
)
872 struct kern_ipc_perm
*ipc
= it
;
873 struct ipc_proc_iter
*iter
= s
->private;
874 struct ipc_proc_iface
*iface
= iter
->iface
;
877 /* If we had a locked structure, release it */
878 if (ipc
&& ipc
!= SEQ_START_TOKEN
)
881 ids
= &iter
->ns
->ids
[iface
->ids
];
882 /* Release the lock we took in start() */
883 up_read(&ids
->rw_mutex
);
886 static int sysvipc_proc_show(struct seq_file
*s
, void *it
)
888 struct ipc_proc_iter
*iter
= s
->private;
889 struct ipc_proc_iface
*iface
= iter
->iface
;
891 if (it
== SEQ_START_TOKEN
)
892 return seq_puts(s
, iface
->header
);
894 return iface
->show(s
, it
);
897 static struct seq_operations sysvipc_proc_seqops
= {
898 .start
= sysvipc_proc_start
,
899 .stop
= sysvipc_proc_stop
,
900 .next
= sysvipc_proc_next
,
901 .show
= sysvipc_proc_show
,
904 static int sysvipc_proc_open(struct inode
*inode
, struct file
*file
)
907 struct seq_file
*seq
;
908 struct ipc_proc_iter
*iter
;
911 iter
= kmalloc(sizeof(*iter
), GFP_KERNEL
);
915 ret
= seq_open(file
, &sysvipc_proc_seqops
);
919 seq
= file
->private_data
;
922 iter
->iface
= PDE(inode
)->data
;
923 iter
->ns
= get_ipc_ns(current
->nsproxy
->ipc_ns
);
931 static int sysvipc_proc_release(struct inode
*inode
, struct file
*file
)
933 struct seq_file
*seq
= file
->private_data
;
934 struct ipc_proc_iter
*iter
= seq
->private;
935 put_ipc_ns(iter
->ns
);
936 return seq_release_private(inode
, file
);
939 static const struct file_operations sysvipc_proc_fops
= {
940 .open
= sysvipc_proc_open
,
943 .release
= sysvipc_proc_release
,
945 #endif /* CONFIG_PROC_FS */