3 * Copyright (C) 1992 Krishna Balasubramanian
5 * Removed all the remaining kerneld mess
6 * Catch the -EFAULT stuff properly
7 * Use GFP_KERNEL for messages as in 1.2
8 * Fixed up the unchecked user space derefs
9 * Copyright (C) 1998 Alan Cox & Andi Kleen
11 * /proc/sysvipc/msg support (c) 1999 Dragos Acostachioaie <dragos@iname.com>
13 * mostly rewritten, threaded and wake-one semantics added
14 * MSGMAX limit removed, sysctl's added
15 * (c) 1999 Manfred Spraul <manfred@colorfullife.com>
17 * support for audit of ipc object properties and permission changes
18 * Dustin Kirkland <dustin.kirkland@us.ibm.com>
22 * Pavel Emelianov <xemul@openvz.org>
25 #include <linux/capability.h>
26 #include <linux/slab.h>
27 #include <linux/msg.h>
28 #include <linux/spinlock.h>
29 #include <linux/init.h>
30 #include <linux/proc_fs.h>
31 #include <linux/list.h>
32 #include <linux/security.h>
33 #include <linux/sched.h>
34 #include <linux/syscalls.h>
35 #include <linux/audit.h>
36 #include <linux/seq_file.h>
37 #include <linux/mutex.h>
38 #include <linux/nsproxy.h>
40 #include <asm/current.h>
41 #include <asm/uaccess.h>
45 * one msg_receiver structure for each sleeping receiver:
48 struct list_head r_list
;
49 struct task_struct
*r_tsk
;
55 volatile struct msg_msg
*r_msg
;
58 /* one msg_sender for each sleeping sender */
60 struct list_head list
;
61 struct task_struct
*tsk
;
65 #define SEARCH_EQUAL 2
66 #define SEARCH_NOTEQUAL 3
67 #define SEARCH_LESSEQUAL 4
69 static atomic_t msg_bytes
= ATOMIC_INIT(0);
70 static atomic_t msg_hdrs
= ATOMIC_INIT(0);
72 static struct ipc_ids init_msg_ids
;
74 #define msg_ids(ns) (*((ns)->ids[IPC_MSG_IDS]))
76 #define msg_lock(ns, id) ((struct msg_queue*)ipc_lock(&msg_ids(ns), id))
77 #define msg_unlock(msq) ipc_unlock(&(msq)->q_perm)
78 #define msg_rmid(ns, id) ((struct msg_queue*)ipc_rmid(&msg_ids(ns), id))
79 #define msg_checkid(ns, msq, msgid) \
80 ipc_checkid(&msg_ids(ns), &msq->q_perm, msgid)
81 #define msg_buildid(ns, id, seq) \
82 ipc_buildid(&msg_ids(ns), id, seq)
84 static void freeque (struct ipc_namespace
*ns
, struct msg_queue
*msq
, int id
);
85 static int newque (struct ipc_namespace
*ns
, key_t key
, int msgflg
);
87 static int sysvipc_msg_proc_show(struct seq_file
*s
, void *it
);
90 static void __ipc_init
__msg_init_ns(struct ipc_namespace
*ns
, struct ipc_ids
*ids
)
92 ns
->ids
[IPC_MSG_IDS
] = ids
;
93 ns
->msg_ctlmax
= MSGMAX
;
94 ns
->msg_ctlmnb
= MSGMNB
;
95 ns
->msg_ctlmni
= MSGMNI
;
96 ipc_init_ids(ids
, ns
->msg_ctlmni
);
100 int msg_init_ns(struct ipc_namespace
*ns
)
104 ids
= kmalloc(sizeof(struct ipc_ids
), GFP_KERNEL
);
108 __msg_init_ns(ns
, ids
);
112 void msg_exit_ns(struct ipc_namespace
*ns
)
115 struct msg_queue
*msq
;
117 mutex_lock(&msg_ids(ns
).mutex
);
118 for (i
= 0; i
<= msg_ids(ns
).max_id
; i
++) {
119 msq
= msg_lock(ns
, i
);
125 mutex_unlock(&msg_ids(ns
).mutex
);
127 kfree(ns
->ids
[IPC_MSG_IDS
]);
128 ns
->ids
[IPC_MSG_IDS
] = NULL
;
132 void __init
msg_init(void)
134 __msg_init_ns(&init_ipc_ns
, &init_msg_ids
);
135 ipc_init_proc_interface("sysvipc/msg",
136 " key msqid perms cbytes qnum lspid lrpid uid gid cuid cgid stime rtime ctime\n",
137 IPC_MSG_IDS
, sysvipc_msg_proc_show
);
140 static int newque (struct ipc_namespace
*ns
, key_t key
, int msgflg
)
142 struct msg_queue
*msq
;
145 msq
= ipc_rcu_alloc(sizeof(*msq
));
149 msq
->q_perm
.mode
= msgflg
& S_IRWXUGO
;
150 msq
->q_perm
.key
= key
;
152 msq
->q_perm
.security
= NULL
;
153 retval
= security_msg_queue_alloc(msq
);
159 id
= ipc_addid(&msg_ids(ns
), &msq
->q_perm
, ns
->msg_ctlmni
);
161 security_msg_queue_free(msq
);
166 msq
->q_id
= msg_buildid(ns
, id
, msq
->q_perm
.seq
);
167 msq
->q_stime
= msq
->q_rtime
= 0;
168 msq
->q_ctime
= get_seconds();
169 msq
->q_cbytes
= msq
->q_qnum
= 0;
170 msq
->q_qbytes
= ns
->msg_ctlmnb
;
171 msq
->q_lspid
= msq
->q_lrpid
= 0;
172 INIT_LIST_HEAD(&msq
->q_messages
);
173 INIT_LIST_HEAD(&msq
->q_receivers
);
174 INIT_LIST_HEAD(&msq
->q_senders
);
180 static inline void ss_add(struct msg_queue
*msq
, struct msg_sender
*mss
)
183 current
->state
= TASK_INTERRUPTIBLE
;
184 list_add_tail(&mss
->list
, &msq
->q_senders
);
187 static inline void ss_del(struct msg_sender
*mss
)
189 if (mss
->list
.next
!= NULL
)
190 list_del(&mss
->list
);
193 static void ss_wakeup(struct list_head
*h
, int kill
)
195 struct list_head
*tmp
;
199 struct msg_sender
*mss
;
201 mss
= list_entry(tmp
, struct msg_sender
, list
);
204 mss
->list
.next
= NULL
;
205 wake_up_process(mss
->tsk
);
209 static void expunge_all(struct msg_queue
*msq
, int res
)
211 struct list_head
*tmp
;
213 tmp
= msq
->q_receivers
.next
;
214 while (tmp
!= &msq
->q_receivers
) {
215 struct msg_receiver
*msr
;
217 msr
= list_entry(tmp
, struct msg_receiver
, r_list
);
220 wake_up_process(msr
->r_tsk
);
222 msr
->r_msg
= ERR_PTR(res
);
227 * freeque() wakes up waiters on the sender and receiver waiting queue,
228 * removes the message queue from message queue ID
229 * array, and cleans up all the messages associated with this queue.
231 * msg_ids.mutex and the spinlock for this message queue is hold
232 * before freeque() is called. msg_ids.mutex remains locked on exit.
234 static void freeque(struct ipc_namespace
*ns
, struct msg_queue
*msq
, int id
)
236 struct list_head
*tmp
;
238 expunge_all(msq
, -EIDRM
);
239 ss_wakeup(&msq
->q_senders
, 1);
240 msq
= msg_rmid(ns
, id
);
243 tmp
= msq
->q_messages
.next
;
244 while (tmp
!= &msq
->q_messages
) {
245 struct msg_msg
*msg
= list_entry(tmp
, struct msg_msg
, m_list
);
248 atomic_dec(&msg_hdrs
);
251 atomic_sub(msq
->q_cbytes
, &msg_bytes
);
252 security_msg_queue_free(msq
);
256 asmlinkage
long sys_msgget(key_t key
, int msgflg
)
258 struct msg_queue
*msq
;
259 int id
, ret
= -EPERM
;
260 struct ipc_namespace
*ns
;
262 ns
= current
->nsproxy
->ipc_ns
;
264 mutex_lock(&msg_ids(ns
).mutex
);
265 if (key
== IPC_PRIVATE
)
266 ret
= newque(ns
, key
, msgflg
);
267 else if ((id
= ipc_findkey(&msg_ids(ns
), key
)) == -1) { /* key not used */
268 if (!(msgflg
& IPC_CREAT
))
271 ret
= newque(ns
, key
, msgflg
);
272 } else if (msgflg
& IPC_CREAT
&& msgflg
& IPC_EXCL
) {
275 msq
= msg_lock(ns
, id
);
277 if (ipcperms(&msq
->q_perm
, msgflg
))
280 int qid
= msg_buildid(ns
, id
, msq
->q_perm
.seq
);
282 ret
= security_msg_queue_associate(msq
, msgflg
);
288 mutex_unlock(&msg_ids(ns
).mutex
);
293 static inline unsigned long
294 copy_msqid_to_user(void __user
*buf
, struct msqid64_ds
*in
, int version
)
298 return copy_to_user(buf
, in
, sizeof(*in
));
303 memset(&out
, 0, sizeof(out
));
305 ipc64_perm_to_ipc_perm(&in
->msg_perm
, &out
.msg_perm
);
307 out
.msg_stime
= in
->msg_stime
;
308 out
.msg_rtime
= in
->msg_rtime
;
309 out
.msg_ctime
= in
->msg_ctime
;
311 if (in
->msg_cbytes
> USHRT_MAX
)
312 out
.msg_cbytes
= USHRT_MAX
;
314 out
.msg_cbytes
= in
->msg_cbytes
;
315 out
.msg_lcbytes
= in
->msg_cbytes
;
317 if (in
->msg_qnum
> USHRT_MAX
)
318 out
.msg_qnum
= USHRT_MAX
;
320 out
.msg_qnum
= in
->msg_qnum
;
322 if (in
->msg_qbytes
> USHRT_MAX
)
323 out
.msg_qbytes
= USHRT_MAX
;
325 out
.msg_qbytes
= in
->msg_qbytes
;
326 out
.msg_lqbytes
= in
->msg_qbytes
;
328 out
.msg_lspid
= in
->msg_lspid
;
329 out
.msg_lrpid
= in
->msg_lrpid
;
331 return copy_to_user(buf
, &out
, sizeof(out
));
339 unsigned long qbytes
;
345 static inline unsigned long
346 copy_msqid_from_user(struct msq_setbuf
*out
, void __user
*buf
, int version
)
351 struct msqid64_ds tbuf
;
353 if (copy_from_user(&tbuf
, buf
, sizeof(tbuf
)))
356 out
->qbytes
= tbuf
.msg_qbytes
;
357 out
->uid
= tbuf
.msg_perm
.uid
;
358 out
->gid
= tbuf
.msg_perm
.gid
;
359 out
->mode
= tbuf
.msg_perm
.mode
;
365 struct msqid_ds tbuf_old
;
367 if (copy_from_user(&tbuf_old
, buf
, sizeof(tbuf_old
)))
370 out
->uid
= tbuf_old
.msg_perm
.uid
;
371 out
->gid
= tbuf_old
.msg_perm
.gid
;
372 out
->mode
= tbuf_old
.msg_perm
.mode
;
374 if (tbuf_old
.msg_qbytes
== 0)
375 out
->qbytes
= tbuf_old
.msg_lqbytes
;
377 out
->qbytes
= tbuf_old
.msg_qbytes
;
386 asmlinkage
long sys_msgctl(int msqid
, int cmd
, struct msqid_ds __user
*buf
)
388 struct kern_ipc_perm
*ipcp
;
389 struct msq_setbuf setbuf
;
390 struct msg_queue
*msq
;
392 struct ipc_namespace
*ns
;
394 if (msqid
< 0 || cmd
< 0)
397 version
= ipc_parse_version(&cmd
);
398 ns
= current
->nsproxy
->ipc_ns
;
404 struct msginfo msginfo
;
410 * We must not return kernel stack data.
411 * due to padding, it's not enough
412 * to set all member fields.
414 err
= security_msg_queue_msgctl(NULL
, cmd
);
418 memset(&msginfo
, 0, sizeof(msginfo
));
419 msginfo
.msgmni
= ns
->msg_ctlmni
;
420 msginfo
.msgmax
= ns
->msg_ctlmax
;
421 msginfo
.msgmnb
= ns
->msg_ctlmnb
;
422 msginfo
.msgssz
= MSGSSZ
;
423 msginfo
.msgseg
= MSGSEG
;
424 mutex_lock(&msg_ids(ns
).mutex
);
425 if (cmd
== MSG_INFO
) {
426 msginfo
.msgpool
= msg_ids(ns
).in_use
;
427 msginfo
.msgmap
= atomic_read(&msg_hdrs
);
428 msginfo
.msgtql
= atomic_read(&msg_bytes
);
430 msginfo
.msgmap
= MSGMAP
;
431 msginfo
.msgpool
= MSGPOOL
;
432 msginfo
.msgtql
= MSGTQL
;
434 max_id
= msg_ids(ns
).max_id
;
435 mutex_unlock(&msg_ids(ns
).mutex
);
436 if (copy_to_user(buf
, &msginfo
, sizeof(struct msginfo
)))
438 return (max_id
< 0) ? 0 : max_id
;
443 struct msqid64_ds tbuf
;
448 if (cmd
== MSG_STAT
&& msqid
>= msg_ids(ns
).entries
->size
)
451 memset(&tbuf
, 0, sizeof(tbuf
));
453 msq
= msg_lock(ns
, msqid
);
457 if (cmd
== MSG_STAT
) {
458 success_return
= msg_buildid(ns
, msqid
, msq
->q_perm
.seq
);
461 if (msg_checkid(ns
, msq
, msqid
))
466 if (ipcperms(&msq
->q_perm
, S_IRUGO
))
469 err
= security_msg_queue_msgctl(msq
, cmd
);
473 kernel_to_ipc64_perm(&msq
->q_perm
, &tbuf
.msg_perm
);
474 tbuf
.msg_stime
= msq
->q_stime
;
475 tbuf
.msg_rtime
= msq
->q_rtime
;
476 tbuf
.msg_ctime
= msq
->q_ctime
;
477 tbuf
.msg_cbytes
= msq
->q_cbytes
;
478 tbuf
.msg_qnum
= msq
->q_qnum
;
479 tbuf
.msg_qbytes
= msq
->q_qbytes
;
480 tbuf
.msg_lspid
= msq
->q_lspid
;
481 tbuf
.msg_lrpid
= msq
->q_lrpid
;
483 if (copy_msqid_to_user(buf
, &tbuf
, version
))
485 return success_return
;
490 if (copy_msqid_from_user(&setbuf
, buf
, version
))
499 mutex_lock(&msg_ids(ns
).mutex
);
500 msq
= msg_lock(ns
, msqid
);
506 if (msg_checkid(ns
, msq
, msqid
))
510 err
= audit_ipc_obj(ipcp
);
514 err
= audit_ipc_set_perm(setbuf
.qbytes
, setbuf
.uid
, setbuf
.gid
,
521 if (current
->euid
!= ipcp
->cuid
&&
522 current
->euid
!= ipcp
->uid
&& !capable(CAP_SYS_ADMIN
))
523 /* We _could_ check for CAP_CHOWN above, but we don't */
526 err
= security_msg_queue_msgctl(msq
, cmd
);
534 if (setbuf
.qbytes
> ns
->msg_ctlmnb
&& !capable(CAP_SYS_RESOURCE
))
537 msq
->q_qbytes
= setbuf
.qbytes
;
539 ipcp
->uid
= setbuf
.uid
;
540 ipcp
->gid
= setbuf
.gid
;
541 ipcp
->mode
= (ipcp
->mode
& ~S_IRWXUGO
) |
542 (S_IRWXUGO
& setbuf
.mode
);
543 msq
->q_ctime
= get_seconds();
544 /* sleeping receivers might be excluded by
545 * stricter permissions.
547 expunge_all(msq
, -EAGAIN
);
548 /* sleeping senders might be able to send
549 * due to a larger queue size.
551 ss_wakeup(&msq
->q_senders
, 0);
556 freeque(ns
, msq
, msqid
);
561 mutex_unlock(&msg_ids(ns
).mutex
);
571 static int testmsg(struct msg_msg
*msg
, long type
, int mode
)
577 case SEARCH_LESSEQUAL
:
578 if (msg
->m_type
<=type
)
582 if (msg
->m_type
== type
)
585 case SEARCH_NOTEQUAL
:
586 if (msg
->m_type
!= type
)
593 static inline int pipelined_send(struct msg_queue
*msq
, struct msg_msg
*msg
)
595 struct list_head
*tmp
;
597 tmp
= msq
->q_receivers
.next
;
598 while (tmp
!= &msq
->q_receivers
) {
599 struct msg_receiver
*msr
;
601 msr
= list_entry(tmp
, struct msg_receiver
, r_list
);
603 if (testmsg(msg
, msr
->r_msgtype
, msr
->r_mode
) &&
604 !security_msg_queue_msgrcv(msq
, msg
, msr
->r_tsk
,
605 msr
->r_msgtype
, msr
->r_mode
)) {
607 list_del(&msr
->r_list
);
608 if (msr
->r_maxsize
< msg
->m_ts
) {
610 wake_up_process(msr
->r_tsk
);
612 msr
->r_msg
= ERR_PTR(-E2BIG
);
615 msq
->q_lrpid
= msr
->r_tsk
->pid
;
616 msq
->q_rtime
= get_seconds();
617 wake_up_process(msr
->r_tsk
);
629 sys_msgsnd(int msqid
, struct msgbuf __user
*msgp
, size_t msgsz
, int msgflg
)
631 struct msg_queue
*msq
;
635 struct ipc_namespace
*ns
;
637 ns
= current
->nsproxy
->ipc_ns
;
639 if (msgsz
> ns
->msg_ctlmax
|| (long) msgsz
< 0 || msqid
< 0)
641 if (get_user(mtype
, &msgp
->mtype
))
646 msg
= load_msg(msgp
->mtext
, msgsz
);
653 msq
= msg_lock(ns
, msqid
);
659 if (msg_checkid(ns
, msq
, msqid
))
660 goto out_unlock_free
;
666 if (ipcperms(&msq
->q_perm
, S_IWUGO
))
667 goto out_unlock_free
;
669 err
= security_msg_queue_msgsnd(msq
, msg
, msgflg
);
671 goto out_unlock_free
;
673 if (msgsz
+ msq
->q_cbytes
<= msq
->q_qbytes
&&
674 1 + msq
->q_qnum
<= msq
->q_qbytes
) {
678 /* queue full, wait: */
679 if (msgflg
& IPC_NOWAIT
) {
681 goto out_unlock_free
;
688 ipc_lock_by_ptr(&msq
->q_perm
);
690 if (msq
->q_perm
.deleted
) {
692 goto out_unlock_free
;
696 if (signal_pending(current
)) {
697 err
= -ERESTARTNOHAND
;
698 goto out_unlock_free
;
702 msq
->q_lspid
= current
->tgid
;
703 msq
->q_stime
= get_seconds();
705 if (!pipelined_send(msq
, msg
)) {
706 /* noone is waiting for this message, enqueue it */
707 list_add_tail(&msg
->m_list
, &msq
->q_messages
);
708 msq
->q_cbytes
+= msgsz
;
710 atomic_add(msgsz
, &msg_bytes
);
711 atomic_inc(&msg_hdrs
);
725 static inline int convert_mode(long *msgtyp
, int msgflg
)
728 * find message of correct type.
729 * msgtyp = 0 => get first.
730 * msgtyp > 0 => get first message of matching type.
731 * msgtyp < 0 => get message with least type must be < abs(msgtype).
737 return SEARCH_LESSEQUAL
;
739 if (msgflg
& MSG_EXCEPT
)
740 return SEARCH_NOTEQUAL
;
744 asmlinkage
long sys_msgrcv(int msqid
, struct msgbuf __user
*msgp
, size_t msgsz
,
745 long msgtyp
, int msgflg
)
747 struct msg_queue
*msq
;
750 struct ipc_namespace
*ns
;
752 if (msqid
< 0 || (long) msgsz
< 0)
754 mode
= convert_mode(&msgtyp
, msgflg
);
755 ns
= current
->nsproxy
->ipc_ns
;
757 msq
= msg_lock(ns
, msqid
);
761 msg
= ERR_PTR(-EIDRM
);
762 if (msg_checkid(ns
, msq
, msqid
))
766 struct msg_receiver msr_d
;
767 struct list_head
*tmp
;
769 msg
= ERR_PTR(-EACCES
);
770 if (ipcperms(&msq
->q_perm
, S_IRUGO
))
773 msg
= ERR_PTR(-EAGAIN
);
774 tmp
= msq
->q_messages
.next
;
775 while (tmp
!= &msq
->q_messages
) {
776 struct msg_msg
*walk_msg
;
778 walk_msg
= list_entry(tmp
, struct msg_msg
, m_list
);
779 if (testmsg(walk_msg
, msgtyp
, mode
) &&
780 !security_msg_queue_msgrcv(msq
, walk_msg
, current
,
784 if (mode
== SEARCH_LESSEQUAL
&&
785 walk_msg
->m_type
!= 1) {
787 msgtyp
= walk_msg
->m_type
- 1;
797 * Found a suitable message.
798 * Unlink it from the queue.
800 if ((msgsz
< msg
->m_ts
) && !(msgflg
& MSG_NOERROR
)) {
801 msg
= ERR_PTR(-E2BIG
);
804 list_del(&msg
->m_list
);
806 msq
->q_rtime
= get_seconds();
807 msq
->q_lrpid
= current
->tgid
;
808 msq
->q_cbytes
-= msg
->m_ts
;
809 atomic_sub(msg
->m_ts
, &msg_bytes
);
810 atomic_dec(&msg_hdrs
);
811 ss_wakeup(&msq
->q_senders
, 0);
815 /* No message waiting. Wait for a message */
816 if (msgflg
& IPC_NOWAIT
) {
817 msg
= ERR_PTR(-ENOMSG
);
820 list_add_tail(&msr_d
.r_list
, &msq
->q_receivers
);
821 msr_d
.r_tsk
= current
;
822 msr_d
.r_msgtype
= msgtyp
;
824 if (msgflg
& MSG_NOERROR
)
825 msr_d
.r_maxsize
= INT_MAX
;
827 msr_d
.r_maxsize
= msgsz
;
828 msr_d
.r_msg
= ERR_PTR(-EAGAIN
);
829 current
->state
= TASK_INTERRUPTIBLE
;
834 /* Lockless receive, part 1:
835 * Disable preemption. We don't hold a reference to the queue
836 * and getting a reference would defeat the idea of a lockless
837 * operation, thus the code relies on rcu to guarantee the
839 * Prior to destruction, expunge_all(-EIRDM) changes r_msg.
840 * Thus if r_msg is -EAGAIN, then the queue not yet destroyed.
841 * rcu_read_lock() prevents preemption between reading r_msg
842 * and the spin_lock() inside ipc_lock_by_ptr().
846 /* Lockless receive, part 2:
847 * Wait until pipelined_send or expunge_all are outside of
848 * wake_up_process(). There is a race with exit(), see
849 * ipc/mqueue.c for the details.
851 msg
= (struct msg_msg
*)msr_d
.r_msg
;
852 while (msg
== NULL
) {
854 msg
= (struct msg_msg
*)msr_d
.r_msg
;
857 /* Lockless receive, part 3:
858 * If there is a message or an error then accept it without
861 if (msg
!= ERR_PTR(-EAGAIN
)) {
866 /* Lockless receive, part 3:
867 * Acquire the queue spinlock.
869 ipc_lock_by_ptr(&msq
->q_perm
);
872 /* Lockless receive, part 4:
873 * Repeat test after acquiring the spinlock.
875 msg
= (struct msg_msg
*)msr_d
.r_msg
;
876 if (msg
!= ERR_PTR(-EAGAIN
))
879 list_del(&msr_d
.r_list
);
880 if (signal_pending(current
)) {
881 msg
= ERR_PTR(-ERESTARTNOHAND
);
890 msgsz
= (msgsz
> msg
->m_ts
) ? msg
->m_ts
: msgsz
;
891 if (put_user (msg
->m_type
, &msgp
->mtype
) ||
892 store_msg(msgp
->mtext
, msg
, msgsz
)) {
900 #ifdef CONFIG_PROC_FS
901 static int sysvipc_msg_proc_show(struct seq_file
*s
, void *it
)
903 struct msg_queue
*msq
= it
;
906 "%10d %10d %4o %10lu %10lu %5u %5u %5u %5u %5u %5u %10lu %10lu %10lu\n",