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 struct msg_msg
*volatile 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 ipc_fini_ids(ns
->ids
[IPC_MSG_IDS
]);
128 kfree(ns
->ids
[IPC_MSG_IDS
]);
129 ns
->ids
[IPC_MSG_IDS
] = NULL
;
133 void __init
msg_init(void)
135 __msg_init_ns(&init_ipc_ns
, &init_msg_ids
);
136 ipc_init_proc_interface("sysvipc/msg",
137 " key msqid perms cbytes qnum lspid lrpid uid gid cuid cgid stime rtime ctime\n",
138 IPC_MSG_IDS
, sysvipc_msg_proc_show
);
141 static int newque (struct ipc_namespace
*ns
, key_t key
, int msgflg
)
143 struct msg_queue
*msq
;
146 msq
= ipc_rcu_alloc(sizeof(*msq
));
150 msq
->q_perm
.mode
= msgflg
& S_IRWXUGO
;
151 msq
->q_perm
.key
= key
;
153 msq
->q_perm
.security
= NULL
;
154 retval
= security_msg_queue_alloc(msq
);
160 id
= ipc_addid(&msg_ids(ns
), &msq
->q_perm
, ns
->msg_ctlmni
);
162 security_msg_queue_free(msq
);
167 msq
->q_id
= msg_buildid(ns
, id
, msq
->q_perm
.seq
);
168 msq
->q_stime
= msq
->q_rtime
= 0;
169 msq
->q_ctime
= get_seconds();
170 msq
->q_cbytes
= msq
->q_qnum
= 0;
171 msq
->q_qbytes
= ns
->msg_ctlmnb
;
172 msq
->q_lspid
= msq
->q_lrpid
= 0;
173 INIT_LIST_HEAD(&msq
->q_messages
);
174 INIT_LIST_HEAD(&msq
->q_receivers
);
175 INIT_LIST_HEAD(&msq
->q_senders
);
181 static inline void ss_add(struct msg_queue
*msq
, struct msg_sender
*mss
)
184 current
->state
= TASK_INTERRUPTIBLE
;
185 list_add_tail(&mss
->list
, &msq
->q_senders
);
188 static inline void ss_del(struct msg_sender
*mss
)
190 if (mss
->list
.next
!= NULL
)
191 list_del(&mss
->list
);
194 static void ss_wakeup(struct list_head
*h
, int kill
)
196 struct list_head
*tmp
;
200 struct msg_sender
*mss
;
202 mss
= list_entry(tmp
, struct msg_sender
, list
);
205 mss
->list
.next
= NULL
;
206 wake_up_process(mss
->tsk
);
210 static void expunge_all(struct msg_queue
*msq
, int res
)
212 struct list_head
*tmp
;
214 tmp
= msq
->q_receivers
.next
;
215 while (tmp
!= &msq
->q_receivers
) {
216 struct msg_receiver
*msr
;
218 msr
= list_entry(tmp
, struct msg_receiver
, r_list
);
221 wake_up_process(msr
->r_tsk
);
223 msr
->r_msg
= ERR_PTR(res
);
228 * freeque() wakes up waiters on the sender and receiver waiting queue,
229 * removes the message queue from message queue ID
230 * array, and cleans up all the messages associated with this queue.
232 * msg_ids.mutex and the spinlock for this message queue is hold
233 * before freeque() is called. msg_ids.mutex remains locked on exit.
235 static void freeque(struct ipc_namespace
*ns
, struct msg_queue
*msq
, int id
)
237 struct list_head
*tmp
;
239 expunge_all(msq
, -EIDRM
);
240 ss_wakeup(&msq
->q_senders
, 1);
241 msq
= msg_rmid(ns
, id
);
244 tmp
= msq
->q_messages
.next
;
245 while (tmp
!= &msq
->q_messages
) {
246 struct msg_msg
*msg
= list_entry(tmp
, struct msg_msg
, m_list
);
249 atomic_dec(&msg_hdrs
);
252 atomic_sub(msq
->q_cbytes
, &msg_bytes
);
253 security_msg_queue_free(msq
);
257 asmlinkage
long sys_msgget(key_t key
, int msgflg
)
259 struct msg_queue
*msq
;
260 int id
, ret
= -EPERM
;
261 struct ipc_namespace
*ns
;
263 ns
= current
->nsproxy
->ipc_ns
;
265 mutex_lock(&msg_ids(ns
).mutex
);
266 if (key
== IPC_PRIVATE
)
267 ret
= newque(ns
, key
, msgflg
);
268 else if ((id
= ipc_findkey(&msg_ids(ns
), key
)) == -1) { /* key not used */
269 if (!(msgflg
& IPC_CREAT
))
272 ret
= newque(ns
, key
, msgflg
);
273 } else if (msgflg
& IPC_CREAT
&& msgflg
& IPC_EXCL
) {
276 msq
= msg_lock(ns
, id
);
278 if (ipcperms(&msq
->q_perm
, msgflg
))
281 int qid
= msg_buildid(ns
, id
, msq
->q_perm
.seq
);
283 ret
= security_msg_queue_associate(msq
, msgflg
);
289 mutex_unlock(&msg_ids(ns
).mutex
);
294 static inline unsigned long
295 copy_msqid_to_user(void __user
*buf
, struct msqid64_ds
*in
, int version
)
299 return copy_to_user(buf
, in
, sizeof(*in
));
304 memset(&out
, 0, sizeof(out
));
306 ipc64_perm_to_ipc_perm(&in
->msg_perm
, &out
.msg_perm
);
308 out
.msg_stime
= in
->msg_stime
;
309 out
.msg_rtime
= in
->msg_rtime
;
310 out
.msg_ctime
= in
->msg_ctime
;
312 if (in
->msg_cbytes
> USHRT_MAX
)
313 out
.msg_cbytes
= USHRT_MAX
;
315 out
.msg_cbytes
= in
->msg_cbytes
;
316 out
.msg_lcbytes
= in
->msg_cbytes
;
318 if (in
->msg_qnum
> USHRT_MAX
)
319 out
.msg_qnum
= USHRT_MAX
;
321 out
.msg_qnum
= in
->msg_qnum
;
323 if (in
->msg_qbytes
> USHRT_MAX
)
324 out
.msg_qbytes
= USHRT_MAX
;
326 out
.msg_qbytes
= in
->msg_qbytes
;
327 out
.msg_lqbytes
= in
->msg_qbytes
;
329 out
.msg_lspid
= in
->msg_lspid
;
330 out
.msg_lrpid
= in
->msg_lrpid
;
332 return copy_to_user(buf
, &out
, sizeof(out
));
340 unsigned long qbytes
;
346 static inline unsigned long
347 copy_msqid_from_user(struct msq_setbuf
*out
, void __user
*buf
, int version
)
352 struct msqid64_ds tbuf
;
354 if (copy_from_user(&tbuf
, buf
, sizeof(tbuf
)))
357 out
->qbytes
= tbuf
.msg_qbytes
;
358 out
->uid
= tbuf
.msg_perm
.uid
;
359 out
->gid
= tbuf
.msg_perm
.gid
;
360 out
->mode
= tbuf
.msg_perm
.mode
;
366 struct msqid_ds tbuf_old
;
368 if (copy_from_user(&tbuf_old
, buf
, sizeof(tbuf_old
)))
371 out
->uid
= tbuf_old
.msg_perm
.uid
;
372 out
->gid
= tbuf_old
.msg_perm
.gid
;
373 out
->mode
= tbuf_old
.msg_perm
.mode
;
375 if (tbuf_old
.msg_qbytes
== 0)
376 out
->qbytes
= tbuf_old
.msg_lqbytes
;
378 out
->qbytes
= tbuf_old
.msg_qbytes
;
387 asmlinkage
long sys_msgctl(int msqid
, int cmd
, struct msqid_ds __user
*buf
)
389 struct kern_ipc_perm
*ipcp
;
390 struct msq_setbuf setbuf
;
391 struct msg_queue
*msq
;
393 struct ipc_namespace
*ns
;
395 if (msqid
< 0 || cmd
< 0)
398 version
= ipc_parse_version(&cmd
);
399 ns
= current
->nsproxy
->ipc_ns
;
405 struct msginfo msginfo
;
411 * We must not return kernel stack data.
412 * due to padding, it's not enough
413 * to set all member fields.
415 err
= security_msg_queue_msgctl(NULL
, cmd
);
419 memset(&msginfo
, 0, sizeof(msginfo
));
420 msginfo
.msgmni
= ns
->msg_ctlmni
;
421 msginfo
.msgmax
= ns
->msg_ctlmax
;
422 msginfo
.msgmnb
= ns
->msg_ctlmnb
;
423 msginfo
.msgssz
= MSGSSZ
;
424 msginfo
.msgseg
= MSGSEG
;
425 mutex_lock(&msg_ids(ns
).mutex
);
426 if (cmd
== MSG_INFO
) {
427 msginfo
.msgpool
= msg_ids(ns
).in_use
;
428 msginfo
.msgmap
= atomic_read(&msg_hdrs
);
429 msginfo
.msgtql
= atomic_read(&msg_bytes
);
431 msginfo
.msgmap
= MSGMAP
;
432 msginfo
.msgpool
= MSGPOOL
;
433 msginfo
.msgtql
= MSGTQL
;
435 max_id
= msg_ids(ns
).max_id
;
436 mutex_unlock(&msg_ids(ns
).mutex
);
437 if (copy_to_user(buf
, &msginfo
, sizeof(struct msginfo
)))
439 return (max_id
< 0) ? 0 : max_id
;
444 struct msqid64_ds tbuf
;
449 if (cmd
== MSG_STAT
&& msqid
>= msg_ids(ns
).entries
->size
)
452 memset(&tbuf
, 0, sizeof(tbuf
));
454 msq
= msg_lock(ns
, msqid
);
458 if (cmd
== MSG_STAT
) {
459 success_return
= msg_buildid(ns
, msqid
, msq
->q_perm
.seq
);
462 if (msg_checkid(ns
, msq
, msqid
))
467 if (ipcperms(&msq
->q_perm
, S_IRUGO
))
470 err
= security_msg_queue_msgctl(msq
, cmd
);
474 kernel_to_ipc64_perm(&msq
->q_perm
, &tbuf
.msg_perm
);
475 tbuf
.msg_stime
= msq
->q_stime
;
476 tbuf
.msg_rtime
= msq
->q_rtime
;
477 tbuf
.msg_ctime
= msq
->q_ctime
;
478 tbuf
.msg_cbytes
= msq
->q_cbytes
;
479 tbuf
.msg_qnum
= msq
->q_qnum
;
480 tbuf
.msg_qbytes
= msq
->q_qbytes
;
481 tbuf
.msg_lspid
= msq
->q_lspid
;
482 tbuf
.msg_lrpid
= msq
->q_lrpid
;
484 if (copy_msqid_to_user(buf
, &tbuf
, version
))
486 return success_return
;
491 if (copy_msqid_from_user(&setbuf
, buf
, version
))
500 mutex_lock(&msg_ids(ns
).mutex
);
501 msq
= msg_lock(ns
, msqid
);
507 if (msg_checkid(ns
, msq
, msqid
))
511 err
= audit_ipc_obj(ipcp
);
515 err
= audit_ipc_set_perm(setbuf
.qbytes
, setbuf
.uid
, setbuf
.gid
,
522 if (current
->euid
!= ipcp
->cuid
&&
523 current
->euid
!= ipcp
->uid
&& !capable(CAP_SYS_ADMIN
))
524 /* We _could_ check for CAP_CHOWN above, but we don't */
527 err
= security_msg_queue_msgctl(msq
, cmd
);
535 if (setbuf
.qbytes
> ns
->msg_ctlmnb
&& !capable(CAP_SYS_RESOURCE
))
538 msq
->q_qbytes
= setbuf
.qbytes
;
540 ipcp
->uid
= setbuf
.uid
;
541 ipcp
->gid
= setbuf
.gid
;
542 ipcp
->mode
= (ipcp
->mode
& ~S_IRWXUGO
) |
543 (S_IRWXUGO
& setbuf
.mode
);
544 msq
->q_ctime
= get_seconds();
545 /* sleeping receivers might be excluded by
546 * stricter permissions.
548 expunge_all(msq
, -EAGAIN
);
549 /* sleeping senders might be able to send
550 * due to a larger queue size.
552 ss_wakeup(&msq
->q_senders
, 0);
557 freeque(ns
, msq
, msqid
);
562 mutex_unlock(&msg_ids(ns
).mutex
);
572 static int testmsg(struct msg_msg
*msg
, long type
, int mode
)
578 case SEARCH_LESSEQUAL
:
579 if (msg
->m_type
<=type
)
583 if (msg
->m_type
== type
)
586 case SEARCH_NOTEQUAL
:
587 if (msg
->m_type
!= type
)
594 static inline int pipelined_send(struct msg_queue
*msq
, struct msg_msg
*msg
)
596 struct list_head
*tmp
;
598 tmp
= msq
->q_receivers
.next
;
599 while (tmp
!= &msq
->q_receivers
) {
600 struct msg_receiver
*msr
;
602 msr
= list_entry(tmp
, struct msg_receiver
, r_list
);
604 if (testmsg(msg
, msr
->r_msgtype
, msr
->r_mode
) &&
605 !security_msg_queue_msgrcv(msq
, msg
, msr
->r_tsk
,
606 msr
->r_msgtype
, msr
->r_mode
)) {
608 list_del(&msr
->r_list
);
609 if (msr
->r_maxsize
< msg
->m_ts
) {
611 wake_up_process(msr
->r_tsk
);
613 msr
->r_msg
= ERR_PTR(-E2BIG
);
616 msq
->q_lrpid
= msr
->r_tsk
->pid
;
617 msq
->q_rtime
= get_seconds();
618 wake_up_process(msr
->r_tsk
);
630 sys_msgsnd(int msqid
, struct msgbuf __user
*msgp
, size_t msgsz
, int msgflg
)
632 struct msg_queue
*msq
;
636 struct ipc_namespace
*ns
;
638 ns
= current
->nsproxy
->ipc_ns
;
640 if (msgsz
> ns
->msg_ctlmax
|| (long) msgsz
< 0 || msqid
< 0)
642 if (get_user(mtype
, &msgp
->mtype
))
647 msg
= load_msg(msgp
->mtext
, msgsz
);
654 msq
= msg_lock(ns
, msqid
);
660 if (msg_checkid(ns
, msq
, msqid
))
661 goto out_unlock_free
;
667 if (ipcperms(&msq
->q_perm
, S_IWUGO
))
668 goto out_unlock_free
;
670 err
= security_msg_queue_msgsnd(msq
, msg
, msgflg
);
672 goto out_unlock_free
;
674 if (msgsz
+ msq
->q_cbytes
<= msq
->q_qbytes
&&
675 1 + msq
->q_qnum
<= msq
->q_qbytes
) {
679 /* queue full, wait: */
680 if (msgflg
& IPC_NOWAIT
) {
682 goto out_unlock_free
;
689 ipc_lock_by_ptr(&msq
->q_perm
);
691 if (msq
->q_perm
.deleted
) {
693 goto out_unlock_free
;
697 if (signal_pending(current
)) {
698 err
= -ERESTARTNOHAND
;
699 goto out_unlock_free
;
703 msq
->q_lspid
= current
->tgid
;
704 msq
->q_stime
= get_seconds();
706 if (!pipelined_send(msq
, msg
)) {
707 /* noone is waiting for this message, enqueue it */
708 list_add_tail(&msg
->m_list
, &msq
->q_messages
);
709 msq
->q_cbytes
+= msgsz
;
711 atomic_add(msgsz
, &msg_bytes
);
712 atomic_inc(&msg_hdrs
);
726 static inline int convert_mode(long *msgtyp
, int msgflg
)
729 * find message of correct type.
730 * msgtyp = 0 => get first.
731 * msgtyp > 0 => get first message of matching type.
732 * msgtyp < 0 => get message with least type must be < abs(msgtype).
738 return SEARCH_LESSEQUAL
;
740 if (msgflg
& MSG_EXCEPT
)
741 return SEARCH_NOTEQUAL
;
745 asmlinkage
long sys_msgrcv(int msqid
, struct msgbuf __user
*msgp
, size_t msgsz
,
746 long msgtyp
, int msgflg
)
748 struct msg_queue
*msq
;
751 struct ipc_namespace
*ns
;
753 if (msqid
< 0 || (long) msgsz
< 0)
755 mode
= convert_mode(&msgtyp
, msgflg
);
756 ns
= current
->nsproxy
->ipc_ns
;
758 msq
= msg_lock(ns
, msqid
);
762 msg
= ERR_PTR(-EIDRM
);
763 if (msg_checkid(ns
, msq
, msqid
))
767 struct msg_receiver msr_d
;
768 struct list_head
*tmp
;
770 msg
= ERR_PTR(-EACCES
);
771 if (ipcperms(&msq
->q_perm
, S_IRUGO
))
774 msg
= ERR_PTR(-EAGAIN
);
775 tmp
= msq
->q_messages
.next
;
776 while (tmp
!= &msq
->q_messages
) {
777 struct msg_msg
*walk_msg
;
779 walk_msg
= list_entry(tmp
, struct msg_msg
, m_list
);
780 if (testmsg(walk_msg
, msgtyp
, mode
) &&
781 !security_msg_queue_msgrcv(msq
, walk_msg
, current
,
785 if (mode
== SEARCH_LESSEQUAL
&&
786 walk_msg
->m_type
!= 1) {
788 msgtyp
= walk_msg
->m_type
- 1;
798 * Found a suitable message.
799 * Unlink it from the queue.
801 if ((msgsz
< msg
->m_ts
) && !(msgflg
& MSG_NOERROR
)) {
802 msg
= ERR_PTR(-E2BIG
);
805 list_del(&msg
->m_list
);
807 msq
->q_rtime
= get_seconds();
808 msq
->q_lrpid
= current
->tgid
;
809 msq
->q_cbytes
-= msg
->m_ts
;
810 atomic_sub(msg
->m_ts
, &msg_bytes
);
811 atomic_dec(&msg_hdrs
);
812 ss_wakeup(&msq
->q_senders
, 0);
816 /* No message waiting. Wait for a message */
817 if (msgflg
& IPC_NOWAIT
) {
818 msg
= ERR_PTR(-ENOMSG
);
821 list_add_tail(&msr_d
.r_list
, &msq
->q_receivers
);
822 msr_d
.r_tsk
= current
;
823 msr_d
.r_msgtype
= msgtyp
;
825 if (msgflg
& MSG_NOERROR
)
826 msr_d
.r_maxsize
= INT_MAX
;
828 msr_d
.r_maxsize
= msgsz
;
829 msr_d
.r_msg
= ERR_PTR(-EAGAIN
);
830 current
->state
= TASK_INTERRUPTIBLE
;
835 /* Lockless receive, part 1:
836 * Disable preemption. We don't hold a reference to the queue
837 * and getting a reference would defeat the idea of a lockless
838 * operation, thus the code relies on rcu to guarantee the
840 * Prior to destruction, expunge_all(-EIRDM) changes r_msg.
841 * Thus if r_msg is -EAGAIN, then the queue not yet destroyed.
842 * rcu_read_lock() prevents preemption between reading r_msg
843 * and the spin_lock() inside ipc_lock_by_ptr().
847 /* Lockless receive, part 2:
848 * Wait until pipelined_send or expunge_all are outside of
849 * wake_up_process(). There is a race with exit(), see
850 * ipc/mqueue.c for the details.
852 msg
= (struct msg_msg
*)msr_d
.r_msg
;
853 while (msg
== NULL
) {
855 msg
= (struct msg_msg
*)msr_d
.r_msg
;
858 /* Lockless receive, part 3:
859 * If there is a message or an error then accept it without
862 if (msg
!= ERR_PTR(-EAGAIN
)) {
867 /* Lockless receive, part 3:
868 * Acquire the queue spinlock.
870 ipc_lock_by_ptr(&msq
->q_perm
);
873 /* Lockless receive, part 4:
874 * Repeat test after acquiring the spinlock.
876 msg
= (struct msg_msg
*)msr_d
.r_msg
;
877 if (msg
!= ERR_PTR(-EAGAIN
))
880 list_del(&msr_d
.r_list
);
881 if (signal_pending(current
)) {
882 msg
= ERR_PTR(-ERESTARTNOHAND
);
891 msgsz
= (msgsz
> msg
->m_ts
) ? msg
->m_ts
: msgsz
;
892 if (put_user (msg
->m_type
, &msgp
->mtype
) ||
893 store_msg(msgp
->mtext
, msg
, msgsz
)) {
901 #ifdef CONFIG_PROC_FS
902 static int sysvipc_msg_proc_show(struct seq_file
*s
, void *it
)
904 struct msg_queue
*msq
= it
;
907 "%10d %10d %4o %10lu %10lu %5u %5u %5u %5u %5u %5u %10lu %10lu %10lu\n",