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),
55 atomic_t nr_ipc_ns
= ATOMIC_INIT(1);
59 * ipc_init - initialise IPC subsystem
61 * The various system5 IPC resources (semaphores, messages and shared
62 * memory) are initialised
65 static int __init
ipc_init(void)
75 * ipc_init_ids - initialise IPC identifiers
76 * @ids: Identifier set
78 * Set up the sequence range to use for the ipc identifier range (limited
79 * below IPCMNI) then initialise the ids idr.
82 void ipc_init_ids(struct ipc_ids
*ids
)
84 init_rwsem(&ids
->rw_mutex
);
89 int seq_limit
= INT_MAX
/SEQ_MULTIPLIER
;
90 if(seq_limit
> USHRT_MAX
)
91 ids
->seq_max
= USHRT_MAX
;
93 ids
->seq_max
= seq_limit
;
96 idr_init(&ids
->ipcs_idr
);
100 static const struct file_operations sysvipc_proc_fops
;
102 * ipc_init_proc_interface - Create a proc interface for sysipc types using a seq_file interface.
103 * @path: Path in procfs
104 * @header: Banner to be printed at the beginning of the file.
105 * @ids: ipc id table to iterate.
106 * @show: show routine.
108 void __init
ipc_init_proc_interface(const char *path
, const char *header
,
109 int ids
, int (*show
)(struct seq_file
*, void *))
111 struct proc_dir_entry
*pde
;
112 struct ipc_proc_iface
*iface
;
114 iface
= kmalloc(sizeof(*iface
), GFP_KERNEL
);
118 iface
->header
= header
;
122 pde
= create_proc_entry(path
,
123 S_IRUGO
, /* world readable */
124 NULL
/* parent dir */);
127 pde
->proc_fops
= &sysvipc_proc_fops
;
135 * ipc_findkey - find a key in an ipc identifier set
136 * @ids: Identifier set
137 * @key: The key to find
139 * Requires ipc_ids.rw_mutex locked.
140 * Returns the LOCKED pointer to the ipc structure if found or NULL
142 * If key is found ipc points to the owning ipc structure
145 static struct kern_ipc_perm
*ipc_findkey(struct ipc_ids
*ids
, key_t key
)
147 struct kern_ipc_perm
*ipc
;
151 for (total
= 0, next_id
= 0; total
< ids
->in_use
; next_id
++) {
152 ipc
= idr_find(&ids
->ipcs_idr
, next_id
);
157 if (ipc
->key
!= key
) {
162 ipc_lock_by_ptr(ipc
);
170 * ipc_get_maxid - get the last assigned id
171 * @ids: IPC identifier set
173 * Called with ipc_ids.rw_mutex held.
176 int ipc_get_maxid(struct ipc_ids
*ids
)
178 struct kern_ipc_perm
*ipc
;
182 if (ids
->in_use
== 0)
185 if (ids
->in_use
== IPCMNI
)
188 /* Look for the last assigned id */
190 for (id
= 0; id
< IPCMNI
&& total
< ids
->in_use
; id
++) {
191 ipc
= idr_find(&ids
->ipcs_idr
, id
);
201 * ipc_addid - add an IPC identifier
202 * @ids: IPC identifier set
203 * @new: new IPC permission set
204 * @size: limit for the number of used ids
206 * Add an entry 'new' to the IPC ids idr. The permissions object is
207 * initialised and the first free entry is set up and the id assigned
208 * is returned. The 'new' entry is returned in a locked state on success.
209 * On failure the entry is not locked and a negative err-code is returned.
211 * Called with ipc_ids.rw_mutex held as a writer.
214 int ipc_addid(struct ipc_ids
* ids
, struct kern_ipc_perm
* new, int size
)
221 if (ids
->in_use
>= size
)
224 err
= idr_get_new(&ids
->ipcs_idr
, new, &id
);
230 new->cuid
= new->uid
= current
->euid
;
231 new->gid
= new->cgid
= current
->egid
;
233 new->seq
= ids
->seq
++;
234 if(ids
->seq
> ids
->seq_max
)
237 new->id
= ipc_buildid(id
, new->seq
);
238 spin_lock_init(&new->lock
);
241 spin_lock(&new->lock
);
246 * ipcget_new - create a new ipc object
248 * @ids: IPC identifer set
249 * @ops: the actual creation routine to call
250 * @params: its parameters
252 * This routine is called by sys_msgget, sys_semget() and sys_shmget()
253 * when the key is IPC_PRIVATE.
255 static int ipcget_new(struct ipc_namespace
*ns
, struct ipc_ids
*ids
,
256 struct ipc_ops
*ops
, struct ipc_params
*params
)
260 err
= idr_pre_get(&ids
->ipcs_idr
, GFP_KERNEL
);
265 down_write(&ids
->rw_mutex
);
266 err
= ops
->getnew(ns
, params
);
267 up_write(&ids
->rw_mutex
);
276 * ipc_check_perms - check security and permissions for an IPC
277 * @ipcp: ipc permission set
278 * @ops: the actual security routine to call
279 * @params: its parameters
281 * This routine is called by sys_msgget(), sys_semget() and sys_shmget()
282 * when the key is not IPC_PRIVATE and that key already exists in the
285 * On success, the IPC id is returned.
287 * It is called with ipc_ids.rw_mutex and ipcp->lock held.
289 static int ipc_check_perms(struct kern_ipc_perm
*ipcp
, struct ipc_ops
*ops
,
290 struct ipc_params
*params
)
294 if (ipcperms(ipcp
, params
->flg
))
297 err
= ops
->associate(ipcp
, params
->flg
);
306 * ipcget_public - get an ipc object or create a new one
308 * @ids: IPC identifer set
309 * @ops: the actual creation routine to call
310 * @params: its parameters
312 * This routine is called by sys_msgget, sys_semget() and sys_shmget()
313 * when the key is not IPC_PRIVATE.
314 * It adds a new entry if the key is not found and does some permission
315 * / security checkings if the key is found.
317 * On success, the ipc id is returned.
319 static int ipcget_public(struct ipc_namespace
*ns
, struct ipc_ids
*ids
,
320 struct ipc_ops
*ops
, struct ipc_params
*params
)
322 struct kern_ipc_perm
*ipcp
;
323 int flg
= params
->flg
;
326 err
= idr_pre_get(&ids
->ipcs_idr
, GFP_KERNEL
);
329 * Take the lock as a writer since we are potentially going to add
330 * a new entry + read locks are not "upgradable"
332 down_write(&ids
->rw_mutex
);
333 ipcp
= ipc_findkey(ids
, params
->key
);
336 if (!(flg
& IPC_CREAT
))
341 err
= ops
->getnew(ns
, params
);
343 /* ipc object has been locked by ipc_findkey() */
345 if (flg
& IPC_CREAT
&& flg
& IPC_EXCL
)
349 if (ops
->more_checks
)
350 err
= ops
->more_checks(ipcp
, params
);
353 * ipc_check_perms returns the IPC id on
356 err
= ipc_check_perms(ipcp
, ops
, params
);
360 up_write(&ids
->rw_mutex
);
370 * ipc_rmid - remove an IPC identifier
371 * @ids: IPC identifier set
372 * @ipcp: ipc perm structure containing the identifier to remove
374 * ipc_ids.rw_mutex (as a writer) and the spinlock for this ID are held
375 * before this function is called, and remain locked on the exit.
378 void ipc_rmid(struct ipc_ids
*ids
, struct kern_ipc_perm
*ipcp
)
380 int lid
= ipcid_to_idx(ipcp
->id
);
382 idr_remove(&ids
->ipcs_idr
, lid
);
392 * ipc_alloc - allocate ipc space
393 * @size: size desired
395 * Allocate memory from the appropriate pools and return a pointer to it.
396 * NULL is returned if the allocation fails
399 void* ipc_alloc(int size
)
405 out
= kmalloc(size
, GFP_KERNEL
);
410 * ipc_free - free ipc space
411 * @ptr: pointer returned by ipc_alloc
412 * @size: size of block
414 * Free a block created with ipc_alloc(). The caller must know the size
415 * used in the allocation call.
418 void ipc_free(void* ptr
, int size
)
428 * There are three headers that are prepended to the actual allocation:
429 * - during use: ipc_rcu_hdr.
430 * - during the rcu grace period: ipc_rcu_grace.
431 * - [only if vmalloc]: ipc_rcu_sched.
432 * Their lifetime doesn't overlap, thus the headers share the same memory.
433 * Unlike a normal union, they are right-aligned, thus some container_of
434 * forward/backward casting is necessary:
447 /* "void *" makes sure alignment of following data is sane. */
453 struct work_struct work
;
454 /* "void *" makes sure alignment of following data is sane. */
458 #define HDRLEN_KMALLOC (sizeof(struct ipc_rcu_grace) > sizeof(struct ipc_rcu_hdr) ? \
459 sizeof(struct ipc_rcu_grace) : sizeof(struct ipc_rcu_hdr))
460 #define HDRLEN_VMALLOC (sizeof(struct ipc_rcu_sched) > HDRLEN_KMALLOC ? \
461 sizeof(struct ipc_rcu_sched) : HDRLEN_KMALLOC)
463 static inline int rcu_use_vmalloc(int size
)
465 /* Too big for a single page? */
466 if (HDRLEN_KMALLOC
+ size
> PAGE_SIZE
)
472 * ipc_rcu_alloc - allocate ipc and rcu space
473 * @size: size desired
475 * Allocate memory for the rcu header structure + the object.
476 * Returns the pointer to the object.
477 * NULL is returned if the allocation fails.
480 void* ipc_rcu_alloc(int size
)
484 * We prepend the allocation with the rcu struct, and
485 * workqueue if necessary (for vmalloc).
487 if (rcu_use_vmalloc(size
)) {
488 out
= vmalloc(HDRLEN_VMALLOC
+ size
);
490 out
+= HDRLEN_VMALLOC
;
491 container_of(out
, struct ipc_rcu_hdr
, data
)->is_vmalloc
= 1;
492 container_of(out
, struct ipc_rcu_hdr
, data
)->refcount
= 1;
495 out
= kmalloc(HDRLEN_KMALLOC
+ size
, GFP_KERNEL
);
497 out
+= HDRLEN_KMALLOC
;
498 container_of(out
, struct ipc_rcu_hdr
, data
)->is_vmalloc
= 0;
499 container_of(out
, struct ipc_rcu_hdr
, data
)->refcount
= 1;
506 void ipc_rcu_getref(void *ptr
)
508 container_of(ptr
, struct ipc_rcu_hdr
, data
)->refcount
++;
511 static void ipc_do_vfree(struct work_struct
*work
)
513 vfree(container_of(work
, struct ipc_rcu_sched
, work
));
517 * ipc_schedule_free - free ipc + rcu space
518 * @head: RCU callback structure for queued work
520 * Since RCU callback function is called in bh,
521 * we need to defer the vfree to schedule_work().
523 static void ipc_schedule_free(struct rcu_head
*head
)
525 struct ipc_rcu_grace
*grace
;
526 struct ipc_rcu_sched
*sched
;
528 grace
= container_of(head
, struct ipc_rcu_grace
, rcu
);
529 sched
= container_of(&(grace
->data
[0]), struct ipc_rcu_sched
,
532 INIT_WORK(&sched
->work
, ipc_do_vfree
);
533 schedule_work(&sched
->work
);
537 * ipc_immediate_free - free ipc + rcu space
538 * @head: RCU callback structure that contains pointer to be freed
540 * Free from the RCU callback context.
542 static void ipc_immediate_free(struct rcu_head
*head
)
544 struct ipc_rcu_grace
*free
=
545 container_of(head
, struct ipc_rcu_grace
, rcu
);
549 void ipc_rcu_putref(void *ptr
)
551 if (--container_of(ptr
, struct ipc_rcu_hdr
, data
)->refcount
> 0)
554 if (container_of(ptr
, struct ipc_rcu_hdr
, data
)->is_vmalloc
) {
555 call_rcu(&container_of(ptr
, struct ipc_rcu_grace
, data
)->rcu
,
558 call_rcu(&container_of(ptr
, struct ipc_rcu_grace
, data
)->rcu
,
564 * ipcperms - check IPC permissions
565 * @ipcp: IPC permission set
566 * @flag: desired permission set.
568 * Check user, group, other permissions for access
569 * to ipc resources. return 0 if allowed
572 int ipcperms (struct kern_ipc_perm
*ipcp
, short flag
)
573 { /* flag will most probably be 0 or S_...UGO from <linux/stat.h> */
574 int requested_mode
, granted_mode
, err
;
576 if (unlikely((err
= audit_ipc_obj(ipcp
))))
578 requested_mode
= (flag
>> 6) | (flag
>> 3) | flag
;
579 granted_mode
= ipcp
->mode
;
580 if (current
->euid
== ipcp
->cuid
|| current
->euid
== ipcp
->uid
)
582 else if (in_group_p(ipcp
->cgid
) || in_group_p(ipcp
->gid
))
584 /* is there some bit set in requested_mode but not in granted_mode? */
585 if ((requested_mode
& ~granted_mode
& 0007) &&
586 !capable(CAP_IPC_OWNER
))
589 return security_ipc_permission(ipcp
, flag
);
593 * Functions to convert between the kern_ipc_perm structure and the
594 * old/new ipc_perm structures
598 * kernel_to_ipc64_perm - convert kernel ipc permissions to user
599 * @in: kernel permissions
600 * @out: new style IPC permissions
602 * Turn the kernel object @in into a set of permissions descriptions
603 * for returning to userspace (@out).
607 void kernel_to_ipc64_perm (struct kern_ipc_perm
*in
, struct ipc64_perm
*out
)
612 out
->cuid
= in
->cuid
;
613 out
->cgid
= in
->cgid
;
614 out
->mode
= in
->mode
;
619 * ipc64_perm_to_ipc_perm - convert new ipc permissions to old
620 * @in: new style IPC permissions
621 * @out: old style IPC permissions
623 * Turn the new style permissions object @in into a compatibility
624 * object and store it into the @out pointer.
627 void ipc64_perm_to_ipc_perm (struct ipc64_perm
*in
, struct ipc_perm
*out
)
630 SET_UID(out
->uid
, in
->uid
);
631 SET_GID(out
->gid
, in
->gid
);
632 SET_UID(out
->cuid
, in
->cuid
);
633 SET_GID(out
->cgid
, in
->cgid
);
634 out
->mode
= in
->mode
;
639 * ipc_lock - Lock an ipc structure without rw_mutex held
640 * @ids: IPC identifier set
641 * @id: ipc id to look for
643 * Look for an id in the ipc ids idr and lock the associated ipc object.
645 * The ipc object is locked on exit.
647 * This is the routine that should be called when the rw_mutex is not already
648 * held, i.e. idr tree not protected: it protects the idr tree in read mode
649 * during the idr_find().
652 struct kern_ipc_perm
*ipc_lock(struct ipc_ids
*ids
, int id
)
654 struct kern_ipc_perm
*out
;
655 int lid
= ipcid_to_idx(id
);
657 down_read(&ids
->rw_mutex
);
660 out
= idr_find(&ids
->ipcs_idr
, lid
);
663 up_read(&ids
->rw_mutex
);
664 return ERR_PTR(-EINVAL
);
667 up_read(&ids
->rw_mutex
);
669 spin_lock(&out
->lock
);
671 /* ipc_rmid() may have already freed the ID while ipc_lock
672 * was spinning: here verify that the structure is still valid
675 spin_unlock(&out
->lock
);
677 return ERR_PTR(-EINVAL
);
684 * ipc_lock_down - Lock an ipc structure with rw_sem held
685 * @ids: IPC identifier set
686 * @id: ipc id to look for
688 * Look for an id in the ipc ids idr and lock the associated ipc object.
690 * The ipc object is locked on exit.
692 * This is the routine that should be called when the rw_mutex is already
693 * held, i.e. idr tree protected.
696 struct kern_ipc_perm
*ipc_lock_down(struct ipc_ids
*ids
, int id
)
698 struct kern_ipc_perm
*out
;
699 int lid
= ipcid_to_idx(id
);
702 out
= idr_find(&ids
->ipcs_idr
, lid
);
705 return ERR_PTR(-EINVAL
);
708 spin_lock(&out
->lock
);
711 * No need to verify that the structure is still valid since the
717 struct kern_ipc_perm
*ipc_lock_check_down(struct ipc_ids
*ids
, int id
)
719 struct kern_ipc_perm
*out
;
721 out
= ipc_lock_down(ids
, id
);
725 if (ipc_checkid(out
, id
)) {
727 return ERR_PTR(-EIDRM
);
733 struct kern_ipc_perm
*ipc_lock_check(struct ipc_ids
*ids
, int id
)
735 struct kern_ipc_perm
*out
;
737 out
= ipc_lock(ids
, id
);
741 if (ipc_checkid(out
, id
)) {
743 return ERR_PTR(-EIDRM
);
750 * ipcget - Common sys_*get() code
752 * @ids : IPC identifier set
753 * @ops : operations to be called on ipc object creation, permission checks
755 * @params : the parameters needed by the previous operations.
757 * Common routine called by sys_msgget(), sys_semget() and sys_shmget().
759 int ipcget(struct ipc_namespace
*ns
, struct ipc_ids
*ids
,
760 struct ipc_ops
*ops
, struct ipc_params
*params
)
762 if (params
->key
== IPC_PRIVATE
)
763 return ipcget_new(ns
, ids
, ops
, params
);
765 return ipcget_public(ns
, ids
, ops
, params
);
768 #ifdef __ARCH_WANT_IPC_PARSE_VERSION
772 * ipc_parse_version - IPC call version
773 * @cmd: pointer to command
775 * Return IPC_64 for new style IPC and IPC_OLD for old style IPC.
776 * The @cmd value is turned from an encoding command and version into
777 * just the command code.
780 int ipc_parse_version (int *cmd
)
790 #endif /* __ARCH_WANT_IPC_PARSE_VERSION */
792 #ifdef CONFIG_PROC_FS
793 struct ipc_proc_iter
{
794 struct ipc_namespace
*ns
;
795 struct ipc_proc_iface
*iface
;
799 * This routine locks the ipc structure found at least at position pos.
801 static struct kern_ipc_perm
*sysvipc_find_ipc(struct ipc_ids
*ids
, loff_t pos
,
804 struct kern_ipc_perm
*ipc
;
808 for (id
= 0; id
< pos
&& total
< ids
->in_use
; id
++) {
809 ipc
= idr_find(&ids
->ipcs_idr
, id
);
814 if (total
>= ids
->in_use
)
817 for ( ; pos
< IPCMNI
; pos
++) {
818 ipc
= idr_find(&ids
->ipcs_idr
, pos
);
821 ipc_lock_by_ptr(ipc
);
826 /* Out of range - return NULL to terminate iteration */
830 static void *sysvipc_proc_next(struct seq_file
*s
, void *it
, loff_t
*pos
)
832 struct ipc_proc_iter
*iter
= s
->private;
833 struct ipc_proc_iface
*iface
= iter
->iface
;
834 struct kern_ipc_perm
*ipc
= it
;
836 /* If we had an ipc id locked before, unlock it */
837 if (ipc
&& ipc
!= SEQ_START_TOKEN
)
840 return sysvipc_find_ipc(&iter
->ns
->ids
[iface
->ids
], *pos
, pos
);
844 * File positions: pos 0 -> header, pos n -> ipc id = n - 1.
845 * SeqFile iterator: iterator value locked ipc pointer or SEQ_TOKEN_START.
847 static void *sysvipc_proc_start(struct seq_file
*s
, loff_t
*pos
)
849 struct ipc_proc_iter
*iter
= s
->private;
850 struct ipc_proc_iface
*iface
= iter
->iface
;
853 ids
= &iter
->ns
->ids
[iface
->ids
];
856 * Take the lock - this will be released by the corresponding
859 down_read(&ids
->rw_mutex
);
861 /* pos < 0 is invalid */
865 /* pos == 0 means header */
867 return SEQ_START_TOKEN
;
869 /* Find the (pos-1)th ipc */
870 return sysvipc_find_ipc(ids
, *pos
- 1, pos
);
873 static void sysvipc_proc_stop(struct seq_file
*s
, void *it
)
875 struct kern_ipc_perm
*ipc
= it
;
876 struct ipc_proc_iter
*iter
= s
->private;
877 struct ipc_proc_iface
*iface
= iter
->iface
;
880 /* If we had a locked structure, release it */
881 if (ipc
&& ipc
!= SEQ_START_TOKEN
)
884 ids
= &iter
->ns
->ids
[iface
->ids
];
885 /* Release the lock we took in start() */
886 up_read(&ids
->rw_mutex
);
889 static int sysvipc_proc_show(struct seq_file
*s
, void *it
)
891 struct ipc_proc_iter
*iter
= s
->private;
892 struct ipc_proc_iface
*iface
= iter
->iface
;
894 if (it
== SEQ_START_TOKEN
)
895 return seq_puts(s
, iface
->header
);
897 return iface
->show(s
, it
);
900 static struct seq_operations sysvipc_proc_seqops
= {
901 .start
= sysvipc_proc_start
,
902 .stop
= sysvipc_proc_stop
,
903 .next
= sysvipc_proc_next
,
904 .show
= sysvipc_proc_show
,
907 static int sysvipc_proc_open(struct inode
*inode
, struct file
*file
)
910 struct seq_file
*seq
;
911 struct ipc_proc_iter
*iter
;
914 iter
= kmalloc(sizeof(*iter
), GFP_KERNEL
);
918 ret
= seq_open(file
, &sysvipc_proc_seqops
);
922 seq
= file
->private_data
;
925 iter
->iface
= PDE(inode
)->data
;
926 iter
->ns
= get_ipc_ns(current
->nsproxy
->ipc_ns
);
934 static int sysvipc_proc_release(struct inode
*inode
, struct file
*file
)
936 struct seq_file
*seq
= file
->private_data
;
937 struct ipc_proc_iter
*iter
= seq
->private;
938 put_ipc_ns(iter
->ns
);
939 return seq_release_private(inode
, file
);
942 static const struct file_operations sysvipc_proc_fops
= {
943 .open
= sysvipc_proc_open
,
946 .release
= sysvipc_proc_release
,
948 #endif /* CONFIG_PROC_FS */