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_checkid(ns, msq, msgid) \
79 ipc_checkid(&msg_ids(ns), &msq->q_perm, msgid)
80 #define msg_buildid(ns, id, seq) \
81 ipc_buildid(&msg_ids(ns), id, seq)
83 static void freeque(struct ipc_namespace *, struct msg_queue *);
84 static int newque (struct ipc_namespace *ns, key_t key, int msgflg);
86 static int sysvipc_msg_proc_show(struct seq_file *s, void *it);
89 static void __msg_init_ns(struct ipc_namespace *ns, struct ipc_ids *ids)
91 ns->ids[IPC_MSG_IDS] = ids;
92 ns->msg_ctlmax = MSGMAX;
93 ns->msg_ctlmnb = MSGMNB;
94 ns->msg_ctlmni = MSGMNI;
98 int msg_init_ns(struct ipc_namespace *ns)
102 ids = kmalloc(sizeof(struct ipc_ids), GFP_KERNEL);
106 __msg_init_ns(ns, ids);
110 void msg_exit_ns(struct ipc_namespace *ns)
112 struct msg_queue *msq;
116 mutex_lock(&msg_ids(ns).mutex);
118 in_use = msg_ids(ns).in_use;
120 for (total = 0, next_id = 0; total < in_use; next_id++) {
121 msq = idr_find(&msg_ids(ns).ipcs_idr, next_id);
124 ipc_lock_by_ptr(&msq->q_perm);
128 mutex_unlock(&msg_ids(ns).mutex);
130 kfree(ns->ids[IPC_MSG_IDS]);
131 ns->ids[IPC_MSG_IDS] = NULL;
134 void __init msg_init(void)
136 __msg_init_ns(&init_ipc_ns, &init_msg_ids);
137 ipc_init_proc_interface("sysvipc/msg",
138 " key msqid perms cbytes qnum lspid lrpid uid gid cuid cgid stime rtime ctime\n",
139 IPC_MSG_IDS, sysvipc_msg_proc_show);
142 static inline void msg_rmid(struct ipc_namespace *ns, struct msg_queue *s)
144 ipc_rmid(&msg_ids(ns), &s->q_perm);
147 static int newque (struct ipc_namespace *ns, key_t key, int msgflg)
149 struct msg_queue *msq;
152 msq = ipc_rcu_alloc(sizeof(*msq));
156 msq->q_perm.mode = msgflg & S_IRWXUGO;
157 msq->q_perm.key = key;
159 msq->q_perm.security = NULL;
160 retval = security_msg_queue_alloc(msq);
167 * ipc_addid() locks msq
169 id = ipc_addid(&msg_ids(ns), &msq->q_perm, ns->msg_ctlmni);
171 security_msg_queue_free(msq);
176 msq->q_perm.id = msg_buildid(ns, id, msq->q_perm.seq);
177 msq->q_stime = msq->q_rtime = 0;
178 msq->q_ctime = get_seconds();
179 msq->q_cbytes = msq->q_qnum = 0;
180 msq->q_qbytes = ns->msg_ctlmnb;
181 msq->q_lspid = msq->q_lrpid = 0;
182 INIT_LIST_HEAD(&msq->q_messages);
183 INIT_LIST_HEAD(&msq->q_receivers);
184 INIT_LIST_HEAD(&msq->q_senders);
188 return msq->q_perm.id;
191 static inline void ss_add(struct msg_queue *msq, struct msg_sender *mss)
194 current->state = TASK_INTERRUPTIBLE;
195 list_add_tail(&mss->list, &msq->q_senders);
198 static inline void ss_del(struct msg_sender *mss)
200 if (mss->list.next != NULL)
201 list_del(&mss->list);
204 static void ss_wakeup(struct list_head *h, int kill)
206 struct list_head *tmp;
210 struct msg_sender *mss;
212 mss = list_entry(tmp, struct msg_sender, list);
215 mss->list.next = NULL;
216 wake_up_process(mss->tsk);
220 static void expunge_all(struct msg_queue *msq, int res)
222 struct list_head *tmp;
224 tmp = msq->q_receivers.next;
225 while (tmp != &msq->q_receivers) {
226 struct msg_receiver *msr;
228 msr = list_entry(tmp, struct msg_receiver, r_list);
231 wake_up_process(msr->r_tsk);
233 msr->r_msg = ERR_PTR(res);
238 * freeque() wakes up waiters on the sender and receiver waiting queue,
239 * removes the message queue from message queue ID
240 * IDR, and cleans up all the messages associated with this queue.
242 * msg_ids.mutex and the spinlock for this message queue are held
243 * before freeque() is called. msg_ids.mutex remains locked on exit.
245 static void freeque(struct ipc_namespace *ns, struct msg_queue *msq)
247 struct list_head *tmp;
249 expunge_all(msq, -EIDRM);
250 ss_wakeup(&msq->q_senders, 1);
254 tmp = msq->q_messages.next;
255 while (tmp != &msq->q_messages) {
256 struct msg_msg *msg = list_entry(tmp, struct msg_msg, m_list);
259 atomic_dec(&msg_hdrs);
262 atomic_sub(msq->q_cbytes, &msg_bytes);
263 security_msg_queue_free(msq);
267 asmlinkage long sys_msgget(key_t key, int msgflg)
269 struct msg_queue *msq;
271 struct ipc_namespace *ns;
273 ns = current->nsproxy->ipc_ns;
275 ret = idr_pre_get(&msg_ids(ns).ipcs_idr, GFP_KERNEL);
277 if (key == IPC_PRIVATE) {
281 mutex_lock(&msg_ids(ns).mutex);
282 ret = newque(ns, key, msgflg);
283 mutex_unlock(&msg_ids(ns).mutex);
286 mutex_lock(&msg_ids(ns).mutex);
287 msq = (struct msg_queue *) ipc_findkey(&msg_ids(ns), key);
290 if (!(msgflg & IPC_CREAT))
295 ret = newque(ns, key, msgflg);
297 /* msq has been locked by ipc_findkey() */
299 if (msgflg & IPC_CREAT && msgflg & IPC_EXCL)
302 if (ipcperms(&msq->q_perm, msgflg))
305 ret = security_msg_queue_associate(
308 ret = msq->q_perm.id;
313 mutex_unlock(&msg_ids(ns).mutex);
319 static inline unsigned long
320 copy_msqid_to_user(void __user *buf, struct msqid64_ds *in, int version)
324 return copy_to_user(buf, in, sizeof(*in));
329 memset(&out, 0, sizeof(out));
331 ipc64_perm_to_ipc_perm(&in->msg_perm, &out.msg_perm);
333 out.msg_stime = in->msg_stime;
334 out.msg_rtime = in->msg_rtime;
335 out.msg_ctime = in->msg_ctime;
337 if (in->msg_cbytes > USHRT_MAX)
338 out.msg_cbytes = USHRT_MAX;
340 out.msg_cbytes = in->msg_cbytes;
341 out.msg_lcbytes = in->msg_cbytes;
343 if (in->msg_qnum > USHRT_MAX)
344 out.msg_qnum = USHRT_MAX;
346 out.msg_qnum = in->msg_qnum;
348 if (in->msg_qbytes > USHRT_MAX)
349 out.msg_qbytes = USHRT_MAX;
351 out.msg_qbytes = in->msg_qbytes;
352 out.msg_lqbytes = in->msg_qbytes;
354 out.msg_lspid = in->msg_lspid;
355 out.msg_lrpid = in->msg_lrpid;
357 return copy_to_user(buf, &out, sizeof(out));
365 unsigned long qbytes;
371 static inline unsigned long
372 copy_msqid_from_user(struct msq_setbuf *out, void __user *buf, int version)
377 struct msqid64_ds tbuf;
379 if (copy_from_user(&tbuf, buf, sizeof(tbuf)))
382 out->qbytes = tbuf.msg_qbytes;
383 out->uid = tbuf.msg_perm.uid;
384 out->gid = tbuf.msg_perm.gid;
385 out->mode = tbuf.msg_perm.mode;
391 struct msqid_ds tbuf_old;
393 if (copy_from_user(&tbuf_old, buf, sizeof(tbuf_old)))
396 out->uid = tbuf_old.msg_perm.uid;
397 out->gid = tbuf_old.msg_perm.gid;
398 out->mode = tbuf_old.msg_perm.mode;
400 if (tbuf_old.msg_qbytes == 0)
401 out->qbytes = tbuf_old.msg_lqbytes;
403 out->qbytes = tbuf_old.msg_qbytes;
412 asmlinkage long sys_msgctl(int msqid, int cmd, struct msqid_ds __user *buf)
414 struct kern_ipc_perm *ipcp;
415 struct msq_setbuf uninitialized_var(setbuf);
416 struct msg_queue *msq;
418 struct ipc_namespace *ns;
420 if (msqid < 0 || cmd < 0)
423 version = ipc_parse_version(&cmd);
424 ns = current->nsproxy->ipc_ns;
430 struct msginfo msginfo;
436 * We must not return kernel stack data.
437 * due to padding, it's not enough
438 * to set all member fields.
440 err = security_msg_queue_msgctl(NULL, cmd);
444 memset(&msginfo, 0, sizeof(msginfo));
445 msginfo.msgmni = ns->msg_ctlmni;
446 msginfo.msgmax = ns->msg_ctlmax;
447 msginfo.msgmnb = ns->msg_ctlmnb;
448 msginfo.msgssz = MSGSSZ;
449 msginfo.msgseg = MSGSEG;
450 mutex_lock(&msg_ids(ns).mutex);
451 if (cmd == MSG_INFO) {
452 msginfo.msgpool = msg_ids(ns).in_use;
453 msginfo.msgmap = atomic_read(&msg_hdrs);
454 msginfo.msgtql = atomic_read(&msg_bytes);
456 msginfo.msgmap = MSGMAP;
457 msginfo.msgpool = MSGPOOL;
458 msginfo.msgtql = MSGTQL;
460 max_id = ipc_get_maxid(&msg_ids(ns));
461 mutex_unlock(&msg_ids(ns).mutex);
462 if (copy_to_user(buf, &msginfo, sizeof(struct msginfo)))
464 return (max_id < 0) ? 0 : max_id;
466 case MSG_STAT: /* msqid is an index rather than a msg queue id */
469 struct msqid64_ds tbuf;
475 memset(&tbuf, 0, sizeof(tbuf));
477 msq = msg_lock(ns, msqid);
481 if (cmd == MSG_STAT) {
482 success_return = msq->q_perm.id;
485 if (msg_checkid(ns, msq, msqid))
490 if (ipcperms(&msq->q_perm, S_IRUGO))
493 err = security_msg_queue_msgctl(msq, cmd);
497 kernel_to_ipc64_perm(&msq->q_perm, &tbuf.msg_perm);
498 tbuf.msg_stime = msq->q_stime;
499 tbuf.msg_rtime = msq->q_rtime;
500 tbuf.msg_ctime = msq->q_ctime;
501 tbuf.msg_cbytes = msq->q_cbytes;
502 tbuf.msg_qnum = msq->q_qnum;
503 tbuf.msg_qbytes = msq->q_qbytes;
504 tbuf.msg_lspid = msq->q_lspid;
505 tbuf.msg_lrpid = msq->q_lrpid;
507 if (copy_msqid_to_user(buf, &tbuf, version))
509 return success_return;
514 if (copy_msqid_from_user(&setbuf, buf, version))
523 mutex_lock(&msg_ids(ns).mutex);
524 msq = msg_lock(ns, msqid);
530 if (msg_checkid(ns, msq, msqid))
534 err = audit_ipc_obj(ipcp);
537 if (cmd == IPC_SET) {
538 err = audit_ipc_set_perm(setbuf.qbytes, setbuf.uid, setbuf.gid,
545 if (current->euid != ipcp->cuid &&
546 current->euid != ipcp->uid && !capable(CAP_SYS_ADMIN))
547 /* We _could_ check for CAP_CHOWN above, but we don't */
550 err = security_msg_queue_msgctl(msq, cmd);
558 if (setbuf.qbytes > ns->msg_ctlmnb && !capable(CAP_SYS_RESOURCE))
561 msq->q_qbytes = setbuf.qbytes;
563 ipcp->uid = setbuf.uid;
564 ipcp->gid = setbuf.gid;
565 ipcp->mode = (ipcp->mode & ~S_IRWXUGO) |
566 (S_IRWXUGO & setbuf.mode);
567 msq->q_ctime = get_seconds();
568 /* sleeping receivers might be excluded by
569 * stricter permissions.
571 expunge_all(msq, -EAGAIN);
572 /* sleeping senders might be able to send
573 * due to a larger queue size.
575 ss_wakeup(&msq->q_senders, 0);
585 mutex_unlock(&msg_ids(ns).mutex);
595 static int testmsg(struct msg_msg *msg, long type, int mode)
601 case SEARCH_LESSEQUAL:
602 if (msg->m_type <=type)
606 if (msg->m_type == type)
609 case SEARCH_NOTEQUAL:
610 if (msg->m_type != type)
617 static inline int pipelined_send(struct msg_queue *msq, struct msg_msg *msg)
619 struct list_head *tmp;
621 tmp = msq->q_receivers.next;
622 while (tmp != &msq->q_receivers) {
623 struct msg_receiver *msr;
625 msr = list_entry(tmp, struct msg_receiver, r_list);
627 if (testmsg(msg, msr->r_msgtype, msr->r_mode) &&
628 !security_msg_queue_msgrcv(msq, msg, msr->r_tsk,
629 msr->r_msgtype, msr->r_mode)) {
631 list_del(&msr->r_list);
632 if (msr->r_maxsize < msg->m_ts) {
634 wake_up_process(msr->r_tsk);
636 msr->r_msg = ERR_PTR(-E2BIG);
639 msq->q_lrpid = task_pid_vnr(msr->r_tsk);
640 msq->q_rtime = get_seconds();
641 wake_up_process(msr->r_tsk);
652 long do_msgsnd(int msqid, long mtype, void __user *mtext,
653 size_t msgsz, int msgflg)
655 struct msg_queue *msq;
658 struct ipc_namespace *ns;
660 ns = current->nsproxy->ipc_ns;
662 if (msgsz > ns->msg_ctlmax || (long) msgsz < 0 || msqid < 0)
667 msg = load_msg(mtext, msgsz);
674 msq = msg_lock(ns, msqid);
680 if (msg_checkid(ns, msq, msqid))
681 goto out_unlock_free;
687 if (ipcperms(&msq->q_perm, S_IWUGO))
688 goto out_unlock_free;
690 err = security_msg_queue_msgsnd(msq, msg, msgflg);
692 goto out_unlock_free;
694 if (msgsz + msq->q_cbytes <= msq->q_qbytes &&
695 1 + msq->q_qnum <= msq->q_qbytes) {
699 /* queue full, wait: */
700 if (msgflg & IPC_NOWAIT) {
702 goto out_unlock_free;
709 ipc_lock_by_ptr(&msq->q_perm);
711 if (msq->q_perm.deleted) {
713 goto out_unlock_free;
717 if (signal_pending(current)) {
718 err = -ERESTARTNOHAND;
719 goto out_unlock_free;
723 msq->q_lspid = task_tgid_vnr(current);
724 msq->q_stime = get_seconds();
726 if (!pipelined_send(msq, msg)) {
727 /* noone is waiting for this message, enqueue it */
728 list_add_tail(&msg->m_list, &msq->q_messages);
729 msq->q_cbytes += msgsz;
731 atomic_add(msgsz, &msg_bytes);
732 atomic_inc(&msg_hdrs);
747 sys_msgsnd(int msqid, struct msgbuf __user *msgp, size_t msgsz, int msgflg)
751 if (get_user(mtype, &msgp->mtype))
753 return do_msgsnd(msqid, mtype, msgp->mtext, msgsz, msgflg);
756 static inline int convert_mode(long *msgtyp, int msgflg)
759 * find message of correct type.
760 * msgtyp = 0 => get first.
761 * msgtyp > 0 => get first message of matching type.
762 * msgtyp < 0 => get message with least type must be < abs(msgtype).
768 return SEARCH_LESSEQUAL;
770 if (msgflg & MSG_EXCEPT)
771 return SEARCH_NOTEQUAL;
775 long do_msgrcv(int msqid, long *pmtype, void __user *mtext,
776 size_t msgsz, long msgtyp, int msgflg)
778 struct msg_queue *msq;
781 struct ipc_namespace *ns;
783 if (msqid < 0 || (long) msgsz < 0)
785 mode = convert_mode(&msgtyp, msgflg);
786 ns = current->nsproxy->ipc_ns;
788 msq = msg_lock(ns, msqid);
792 msg = ERR_PTR(-EIDRM);
793 if (msg_checkid(ns, msq, msqid))
797 struct msg_receiver msr_d;
798 struct list_head *tmp;
800 msg = ERR_PTR(-EACCES);
801 if (ipcperms(&msq->q_perm, S_IRUGO))
804 msg = ERR_PTR(-EAGAIN);
805 tmp = msq->q_messages.next;
806 while (tmp != &msq->q_messages) {
807 struct msg_msg *walk_msg;
809 walk_msg = list_entry(tmp, struct msg_msg, m_list);
810 if (testmsg(walk_msg, msgtyp, mode) &&
811 !security_msg_queue_msgrcv(msq, walk_msg, current,
815 if (mode == SEARCH_LESSEQUAL &&
816 walk_msg->m_type != 1) {
818 msgtyp = walk_msg->m_type - 1;
828 * Found a suitable message.
829 * Unlink it from the queue.
831 if ((msgsz < msg->m_ts) && !(msgflg & MSG_NOERROR)) {
832 msg = ERR_PTR(-E2BIG);
835 list_del(&msg->m_list);
837 msq->q_rtime = get_seconds();
838 msq->q_lrpid = task_tgid_vnr(current);
839 msq->q_cbytes -= msg->m_ts;
840 atomic_sub(msg->m_ts, &msg_bytes);
841 atomic_dec(&msg_hdrs);
842 ss_wakeup(&msq->q_senders, 0);
846 /* No message waiting. Wait for a message */
847 if (msgflg & IPC_NOWAIT) {
848 msg = ERR_PTR(-ENOMSG);
851 list_add_tail(&msr_d.r_list, &msq->q_receivers);
852 msr_d.r_tsk = current;
853 msr_d.r_msgtype = msgtyp;
855 if (msgflg & MSG_NOERROR)
856 msr_d.r_maxsize = INT_MAX;
858 msr_d.r_maxsize = msgsz;
859 msr_d.r_msg = ERR_PTR(-EAGAIN);
860 current->state = TASK_INTERRUPTIBLE;
865 /* Lockless receive, part 1:
866 * Disable preemption. We don't hold a reference to the queue
867 * and getting a reference would defeat the idea of a lockless
868 * operation, thus the code relies on rcu to guarantee the
870 * Prior to destruction, expunge_all(-EIRDM) changes r_msg.
871 * Thus if r_msg is -EAGAIN, then the queue not yet destroyed.
872 * rcu_read_lock() prevents preemption between reading r_msg
873 * and the spin_lock() inside ipc_lock_by_ptr().
877 /* Lockless receive, part 2:
878 * Wait until pipelined_send or expunge_all are outside of
879 * wake_up_process(). There is a race with exit(), see
880 * ipc/mqueue.c for the details.
882 msg = (struct msg_msg*)msr_d.r_msg;
883 while (msg == NULL) {
885 msg = (struct msg_msg *)msr_d.r_msg;
888 /* Lockless receive, part 3:
889 * If there is a message or an error then accept it without
892 if (msg != ERR_PTR(-EAGAIN)) {
897 /* Lockless receive, part 3:
898 * Acquire the queue spinlock.
900 ipc_lock_by_ptr(&msq->q_perm);
903 /* Lockless receive, part 4:
904 * Repeat test after acquiring the spinlock.
906 msg = (struct msg_msg*)msr_d.r_msg;
907 if (msg != ERR_PTR(-EAGAIN))
910 list_del(&msr_d.r_list);
911 if (signal_pending(current)) {
912 msg = ERR_PTR(-ERESTARTNOHAND);
921 msgsz = (msgsz > msg->m_ts) ? msg->m_ts : msgsz;
922 *pmtype = msg->m_type;
923 if (store_msg(mtext, msg, msgsz))
931 asmlinkage long sys_msgrcv(int msqid, struct msgbuf __user *msgp, size_t msgsz,
932 long msgtyp, int msgflg)
936 err = do_msgrcv(msqid, &mtype, msgp->mtext, msgsz, msgtyp, msgflg);
940 if (put_user(mtype, &msgp->mtype))
946 #ifdef CONFIG_PROC_FS
947 static int sysvipc_msg_proc_show(struct seq_file *s, void *it)
949 struct msg_queue *msq = it;
952 "%10d %10d %4o %10lu %10lu %5u %5u %5u %5u %5u %5u %10lu %10lu %10lu\n",