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/rwsem.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 struct ipc_ids init_msg_ids;
71 #define msg_ids(ns) (*((ns)->ids[IPC_MSG_IDS]))
73 #define msg_unlock(msq) ipc_unlock(&(msq)->q_perm)
74 #define msg_buildid(id, seq) ipc_buildid(id, seq)
76 static void freeque(struct ipc_namespace *, struct msg_queue *);
77 static int newque(struct ipc_namespace *, struct ipc_params *);
79 static int sysvipc_msg_proc_show(struct seq_file *s, void *it);
82 static void __msg_init_ns(struct ipc_namespace *ns, struct ipc_ids *ids)
84 ns->ids[IPC_MSG_IDS] = ids;
85 ns->msg_ctlmax = MSGMAX;
86 ns->msg_ctlmnb = MSGMNB;
87 ns->msg_ctlmni = MSGMNI;
88 atomic_set(&ns->msg_bytes, 0);
89 atomic_set(&ns->msg_hdrs, 0);
93 int msg_init_ns(struct ipc_namespace *ns)
97 ids = kmalloc(sizeof(struct ipc_ids), GFP_KERNEL);
101 __msg_init_ns(ns, ids);
105 void msg_exit_ns(struct ipc_namespace *ns)
107 struct msg_queue *msq;
108 struct kern_ipc_perm *perm;
112 down_write(&msg_ids(ns).rw_mutex);
114 in_use = msg_ids(ns).in_use;
116 for (total = 0, next_id = 0; total < in_use; next_id++) {
117 perm = idr_find(&msg_ids(ns).ipcs_idr, next_id);
120 ipc_lock_by_ptr(perm);
121 msq = container_of(perm, struct msg_queue, q_perm);
126 up_write(&msg_ids(ns).rw_mutex);
128 kfree(ns->ids[IPC_MSG_IDS]);
129 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);
141 * This routine is called in the paths where the rw_mutex is held to protect
142 * access to the idr tree.
144 static inline struct msg_queue *msg_lock_check_down(struct ipc_namespace *ns,
147 struct kern_ipc_perm *ipcp = ipc_lock_check_down(&msg_ids(ns), id);
150 return (struct msg_queue *)ipcp;
152 return container_of(ipcp, struct msg_queue, q_perm);
156 * msg_lock_(check_) routines are called in the paths where the rw_mutex
159 static inline struct msg_queue *msg_lock(struct ipc_namespace *ns, int id)
161 struct kern_ipc_perm *ipcp = ipc_lock(&msg_ids(ns), id);
164 return (struct msg_queue *)ipcp;
166 return container_of(ipcp, struct msg_queue, q_perm);
169 static inline struct msg_queue *msg_lock_check(struct ipc_namespace *ns,
172 struct kern_ipc_perm *ipcp = ipc_lock_check(&msg_ids(ns), id);
175 return (struct msg_queue *)ipcp;
177 return container_of(ipcp, struct msg_queue, q_perm);
180 static inline void msg_rmid(struct ipc_namespace *ns, struct msg_queue *s)
182 ipc_rmid(&msg_ids(ns), &s->q_perm);
186 * newque - Create a new msg queue
188 * @params: ptr to the structure that contains the key and msgflg
190 * Called with msg_ids.rw_mutex held (writer)
192 static int newque(struct ipc_namespace *ns, struct ipc_params *params)
194 struct msg_queue *msq;
196 key_t key = params->key;
197 int msgflg = params->flg;
199 msq = ipc_rcu_alloc(sizeof(*msq));
203 msq->q_perm.mode = msgflg & S_IRWXUGO;
204 msq->q_perm.key = key;
206 msq->q_perm.security = NULL;
207 retval = security_msg_queue_alloc(msq);
214 * ipc_addid() locks msq
216 id = ipc_addid(&msg_ids(ns), &msq->q_perm, ns->msg_ctlmni);
218 security_msg_queue_free(msq);
223 msq->q_perm.id = msg_buildid(id, msq->q_perm.seq);
224 msq->q_stime = msq->q_rtime = 0;
225 msq->q_ctime = get_seconds();
226 msq->q_cbytes = msq->q_qnum = 0;
227 msq->q_qbytes = ns->msg_ctlmnb;
228 msq->q_lspid = msq->q_lrpid = 0;
229 INIT_LIST_HEAD(&msq->q_messages);
230 INIT_LIST_HEAD(&msq->q_receivers);
231 INIT_LIST_HEAD(&msq->q_senders);
235 return msq->q_perm.id;
238 static inline void ss_add(struct msg_queue *msq, struct msg_sender *mss)
241 current->state = TASK_INTERRUPTIBLE;
242 list_add_tail(&mss->list, &msq->q_senders);
245 static inline void ss_del(struct msg_sender *mss)
247 if (mss->list.next != NULL)
248 list_del(&mss->list);
251 static void ss_wakeup(struct list_head *h, int kill)
253 struct list_head *tmp;
257 struct msg_sender *mss;
259 mss = list_entry(tmp, struct msg_sender, list);
262 mss->list.next = NULL;
263 wake_up_process(mss->tsk);
267 static void expunge_all(struct msg_queue *msq, int res)
269 struct list_head *tmp;
271 tmp = msq->q_receivers.next;
272 while (tmp != &msq->q_receivers) {
273 struct msg_receiver *msr;
275 msr = list_entry(tmp, struct msg_receiver, r_list);
278 wake_up_process(msr->r_tsk);
280 msr->r_msg = ERR_PTR(res);
285 * freeque() wakes up waiters on the sender and receiver waiting queue,
286 * removes the message queue from message queue ID IDR, and cleans up all the
287 * messages associated with this queue.
289 * msg_ids.rw_mutex (writer) and the spinlock for this message queue are held
290 * before freeque() is called. msg_ids.rw_mutex remains locked on exit.
292 static void freeque(struct ipc_namespace *ns, struct msg_queue *msq)
294 struct list_head *tmp;
296 expunge_all(msq, -EIDRM);
297 ss_wakeup(&msq->q_senders, 1);
301 tmp = msq->q_messages.next;
302 while (tmp != &msq->q_messages) {
303 struct msg_msg *msg = list_entry(tmp, struct msg_msg, m_list);
306 atomic_dec(&ns->msg_hdrs);
309 atomic_sub(msq->q_cbytes, &ns->msg_bytes);
310 security_msg_queue_free(msq);
315 * Called with msg_ids.rw_mutex and ipcp locked.
317 static inline int msg_security(struct kern_ipc_perm *ipcp, int msgflg)
319 struct msg_queue *msq = container_of(ipcp, struct msg_queue, q_perm);
321 return security_msg_queue_associate(msq, msgflg);
324 asmlinkage long sys_msgget(key_t key, int msgflg)
326 struct ipc_namespace *ns;
327 struct ipc_ops msg_ops;
328 struct ipc_params msg_params;
330 ns = current->nsproxy->ipc_ns;
332 msg_ops.getnew = newque;
333 msg_ops.associate = msg_security;
334 msg_ops.more_checks = NULL;
336 msg_params.key = key;
337 msg_params.flg = msgflg;
339 return ipcget(ns, &msg_ids(ns), &msg_ops, &msg_params);
342 static inline unsigned long
343 copy_msqid_to_user(void __user *buf, struct msqid64_ds *in, int version)
347 return copy_to_user(buf, in, sizeof(*in));
352 memset(&out, 0, sizeof(out));
354 ipc64_perm_to_ipc_perm(&in->msg_perm, &out.msg_perm);
356 out.msg_stime = in->msg_stime;
357 out.msg_rtime = in->msg_rtime;
358 out.msg_ctime = in->msg_ctime;
360 if (in->msg_cbytes > USHRT_MAX)
361 out.msg_cbytes = USHRT_MAX;
363 out.msg_cbytes = in->msg_cbytes;
364 out.msg_lcbytes = in->msg_cbytes;
366 if (in->msg_qnum > USHRT_MAX)
367 out.msg_qnum = USHRT_MAX;
369 out.msg_qnum = in->msg_qnum;
371 if (in->msg_qbytes > USHRT_MAX)
372 out.msg_qbytes = USHRT_MAX;
374 out.msg_qbytes = in->msg_qbytes;
375 out.msg_lqbytes = in->msg_qbytes;
377 out.msg_lspid = in->msg_lspid;
378 out.msg_lrpid = in->msg_lrpid;
380 return copy_to_user(buf, &out, sizeof(out));
388 unsigned long qbytes;
394 static inline unsigned long
395 copy_msqid_from_user(struct msq_setbuf *out, void __user *buf, int version)
400 struct msqid64_ds tbuf;
402 if (copy_from_user(&tbuf, buf, sizeof(tbuf)))
405 out->qbytes = tbuf.msg_qbytes;
406 out->uid = tbuf.msg_perm.uid;
407 out->gid = tbuf.msg_perm.gid;
408 out->mode = tbuf.msg_perm.mode;
414 struct msqid_ds tbuf_old;
416 if (copy_from_user(&tbuf_old, buf, sizeof(tbuf_old)))
419 out->uid = tbuf_old.msg_perm.uid;
420 out->gid = tbuf_old.msg_perm.gid;
421 out->mode = tbuf_old.msg_perm.mode;
423 if (tbuf_old.msg_qbytes == 0)
424 out->qbytes = tbuf_old.msg_lqbytes;
426 out->qbytes = tbuf_old.msg_qbytes;
435 asmlinkage long sys_msgctl(int msqid, int cmd, struct msqid_ds __user *buf)
437 struct kern_ipc_perm *ipcp;
438 struct msq_setbuf uninitialized_var(setbuf);
439 struct msg_queue *msq;
441 struct ipc_namespace *ns;
443 if (msqid < 0 || cmd < 0)
446 version = ipc_parse_version(&cmd);
447 ns = current->nsproxy->ipc_ns;
453 struct msginfo msginfo;
459 * We must not return kernel stack data.
460 * due to padding, it's not enough
461 * to set all member fields.
463 err = security_msg_queue_msgctl(NULL, cmd);
467 memset(&msginfo, 0, sizeof(msginfo));
468 msginfo.msgmni = ns->msg_ctlmni;
469 msginfo.msgmax = ns->msg_ctlmax;
470 msginfo.msgmnb = ns->msg_ctlmnb;
471 msginfo.msgssz = MSGSSZ;
472 msginfo.msgseg = MSGSEG;
473 down_read(&msg_ids(ns).rw_mutex);
474 if (cmd == MSG_INFO) {
475 msginfo.msgpool = msg_ids(ns).in_use;
476 msginfo.msgmap = atomic_read(&ns->msg_hdrs);
477 msginfo.msgtql = atomic_read(&ns->msg_bytes);
479 msginfo.msgmap = MSGMAP;
480 msginfo.msgpool = MSGPOOL;
481 msginfo.msgtql = MSGTQL;
483 max_id = ipc_get_maxid(&msg_ids(ns));
484 up_read(&msg_ids(ns).rw_mutex);
485 if (copy_to_user(buf, &msginfo, sizeof(struct msginfo)))
487 return (max_id < 0) ? 0 : max_id;
489 case MSG_STAT: /* msqid is an index rather than a msg queue id */
492 struct msqid64_ds tbuf;
498 if (cmd == MSG_STAT) {
499 msq = msg_lock(ns, msqid);
502 success_return = msq->q_perm.id;
504 msq = msg_lock_check(ns, msqid);
510 if (ipcperms(&msq->q_perm, S_IRUGO))
513 err = security_msg_queue_msgctl(msq, cmd);
517 memset(&tbuf, 0, sizeof(tbuf));
519 kernel_to_ipc64_perm(&msq->q_perm, &tbuf.msg_perm);
520 tbuf.msg_stime = msq->q_stime;
521 tbuf.msg_rtime = msq->q_rtime;
522 tbuf.msg_ctime = msq->q_ctime;
523 tbuf.msg_cbytes = msq->q_cbytes;
524 tbuf.msg_qnum = msq->q_qnum;
525 tbuf.msg_qbytes = msq->q_qbytes;
526 tbuf.msg_lspid = msq->q_lspid;
527 tbuf.msg_lrpid = msq->q_lrpid;
529 if (copy_msqid_to_user(buf, &tbuf, version))
531 return success_return;
536 if (copy_msqid_from_user(&setbuf, buf, version))
545 down_write(&msg_ids(ns).rw_mutex);
546 msq = msg_lock_check_down(ns, msqid);
554 err = audit_ipc_obj(ipcp);
557 if (cmd == IPC_SET) {
558 err = audit_ipc_set_perm(setbuf.qbytes, setbuf.uid, setbuf.gid,
565 if (current->euid != ipcp->cuid &&
566 current->euid != ipcp->uid && !capable(CAP_SYS_ADMIN))
567 /* We _could_ check for CAP_CHOWN above, but we don't */
570 err = security_msg_queue_msgctl(msq, cmd);
578 if (setbuf.qbytes > ns->msg_ctlmnb && !capable(CAP_SYS_RESOURCE))
581 msq->q_qbytes = setbuf.qbytes;
583 ipcp->uid = setbuf.uid;
584 ipcp->gid = setbuf.gid;
585 ipcp->mode = (ipcp->mode & ~S_IRWXUGO) |
586 (S_IRWXUGO & setbuf.mode);
587 msq->q_ctime = get_seconds();
588 /* sleeping receivers might be excluded by
589 * stricter permissions.
591 expunge_all(msq, -EAGAIN);
592 /* sleeping senders might be able to send
593 * due to a larger queue size.
595 ss_wakeup(&msq->q_senders, 0);
605 up_write(&msg_ids(ns).rw_mutex);
615 static int testmsg(struct msg_msg *msg, long type, int mode)
621 case SEARCH_LESSEQUAL:
622 if (msg->m_type <=type)
626 if (msg->m_type == type)
629 case SEARCH_NOTEQUAL:
630 if (msg->m_type != type)
637 static inline int pipelined_send(struct msg_queue *msq, struct msg_msg *msg)
639 struct list_head *tmp;
641 tmp = msq->q_receivers.next;
642 while (tmp != &msq->q_receivers) {
643 struct msg_receiver *msr;
645 msr = list_entry(tmp, struct msg_receiver, r_list);
647 if (testmsg(msg, msr->r_msgtype, msr->r_mode) &&
648 !security_msg_queue_msgrcv(msq, msg, msr->r_tsk,
649 msr->r_msgtype, msr->r_mode)) {
651 list_del(&msr->r_list);
652 if (msr->r_maxsize < msg->m_ts) {
654 wake_up_process(msr->r_tsk);
656 msr->r_msg = ERR_PTR(-E2BIG);
659 msq->q_lrpid = task_pid_vnr(msr->r_tsk);
660 msq->q_rtime = get_seconds();
661 wake_up_process(msr->r_tsk);
672 long do_msgsnd(int msqid, long mtype, void __user *mtext,
673 size_t msgsz, int msgflg)
675 struct msg_queue *msq;
678 struct ipc_namespace *ns;
680 ns = current->nsproxy->ipc_ns;
682 if (msgsz > ns->msg_ctlmax || (long) msgsz < 0 || msqid < 0)
687 msg = load_msg(mtext, msgsz);
694 msq = msg_lock_check(ns, msqid);
704 if (ipcperms(&msq->q_perm, S_IWUGO))
705 goto out_unlock_free;
707 err = security_msg_queue_msgsnd(msq, msg, msgflg);
709 goto out_unlock_free;
711 if (msgsz + msq->q_cbytes <= msq->q_qbytes &&
712 1 + msq->q_qnum <= msq->q_qbytes) {
716 /* queue full, wait: */
717 if (msgflg & IPC_NOWAIT) {
719 goto out_unlock_free;
726 ipc_lock_by_ptr(&msq->q_perm);
728 if (msq->q_perm.deleted) {
730 goto out_unlock_free;
734 if (signal_pending(current)) {
735 err = -ERESTARTNOHAND;
736 goto out_unlock_free;
740 msq->q_lspid = task_tgid_vnr(current);
741 msq->q_stime = get_seconds();
743 if (!pipelined_send(msq, msg)) {
744 /* noone is waiting for this message, enqueue it */
745 list_add_tail(&msg->m_list, &msq->q_messages);
746 msq->q_cbytes += msgsz;
748 atomic_add(msgsz, &ns->msg_bytes);
749 atomic_inc(&ns->msg_hdrs);
764 sys_msgsnd(int msqid, struct msgbuf __user *msgp, size_t msgsz, int msgflg)
768 if (get_user(mtype, &msgp->mtype))
770 return do_msgsnd(msqid, mtype, msgp->mtext, msgsz, msgflg);
773 static inline int convert_mode(long *msgtyp, int msgflg)
776 * find message of correct type.
777 * msgtyp = 0 => get first.
778 * msgtyp > 0 => get first message of matching type.
779 * msgtyp < 0 => get message with least type must be < abs(msgtype).
785 return SEARCH_LESSEQUAL;
787 if (msgflg & MSG_EXCEPT)
788 return SEARCH_NOTEQUAL;
792 long do_msgrcv(int msqid, long *pmtype, void __user *mtext,
793 size_t msgsz, long msgtyp, int msgflg)
795 struct msg_queue *msq;
798 struct ipc_namespace *ns;
800 if (msqid < 0 || (long) msgsz < 0)
802 mode = convert_mode(&msgtyp, msgflg);
803 ns = current->nsproxy->ipc_ns;
805 msq = msg_lock_check(ns, msqid);
810 struct msg_receiver msr_d;
811 struct list_head *tmp;
813 msg = ERR_PTR(-EACCES);
814 if (ipcperms(&msq->q_perm, S_IRUGO))
817 msg = ERR_PTR(-EAGAIN);
818 tmp = msq->q_messages.next;
819 while (tmp != &msq->q_messages) {
820 struct msg_msg *walk_msg;
822 walk_msg = list_entry(tmp, struct msg_msg, m_list);
823 if (testmsg(walk_msg, msgtyp, mode) &&
824 !security_msg_queue_msgrcv(msq, walk_msg, current,
828 if (mode == SEARCH_LESSEQUAL &&
829 walk_msg->m_type != 1) {
831 msgtyp = walk_msg->m_type - 1;
841 * Found a suitable message.
842 * Unlink it from the queue.
844 if ((msgsz < msg->m_ts) && !(msgflg & MSG_NOERROR)) {
845 msg = ERR_PTR(-E2BIG);
848 list_del(&msg->m_list);
850 msq->q_rtime = get_seconds();
851 msq->q_lrpid = task_tgid_vnr(current);
852 msq->q_cbytes -= msg->m_ts;
853 atomic_sub(msg->m_ts, &ns->msg_bytes);
854 atomic_dec(&ns->msg_hdrs);
855 ss_wakeup(&msq->q_senders, 0);
859 /* No message waiting. Wait for a message */
860 if (msgflg & IPC_NOWAIT) {
861 msg = ERR_PTR(-ENOMSG);
864 list_add_tail(&msr_d.r_list, &msq->q_receivers);
865 msr_d.r_tsk = current;
866 msr_d.r_msgtype = msgtyp;
868 if (msgflg & MSG_NOERROR)
869 msr_d.r_maxsize = INT_MAX;
871 msr_d.r_maxsize = msgsz;
872 msr_d.r_msg = ERR_PTR(-EAGAIN);
873 current->state = TASK_INTERRUPTIBLE;
878 /* Lockless receive, part 1:
879 * Disable preemption. We don't hold a reference to the queue
880 * and getting a reference would defeat the idea of a lockless
881 * operation, thus the code relies on rcu to guarantee the
883 * Prior to destruction, expunge_all(-EIRDM) changes r_msg.
884 * Thus if r_msg is -EAGAIN, then the queue not yet destroyed.
885 * rcu_read_lock() prevents preemption between reading r_msg
886 * and the spin_lock() inside ipc_lock_by_ptr().
890 /* Lockless receive, part 2:
891 * Wait until pipelined_send or expunge_all are outside of
892 * wake_up_process(). There is a race with exit(), see
893 * ipc/mqueue.c for the details.
895 msg = (struct msg_msg*)msr_d.r_msg;
896 while (msg == NULL) {
898 msg = (struct msg_msg *)msr_d.r_msg;
901 /* Lockless receive, part 3:
902 * If there is a message or an error then accept it without
905 if (msg != ERR_PTR(-EAGAIN)) {
910 /* Lockless receive, part 3:
911 * Acquire the queue spinlock.
913 ipc_lock_by_ptr(&msq->q_perm);
916 /* Lockless receive, part 4:
917 * Repeat test after acquiring the spinlock.
919 msg = (struct msg_msg*)msr_d.r_msg;
920 if (msg != ERR_PTR(-EAGAIN))
923 list_del(&msr_d.r_list);
924 if (signal_pending(current)) {
925 msg = ERR_PTR(-ERESTARTNOHAND);
934 msgsz = (msgsz > msg->m_ts) ? msg->m_ts : msgsz;
935 *pmtype = msg->m_type;
936 if (store_msg(mtext, msg, msgsz))
944 asmlinkage long sys_msgrcv(int msqid, struct msgbuf __user *msgp, size_t msgsz,
945 long msgtyp, int msgflg)
949 err = do_msgrcv(msqid, &mtype, msgp->mtext, msgsz, msgtyp, msgflg);
953 if (put_user(mtype, &msgp->mtype))
959 #ifdef CONFIG_PROC_FS
960 static int sysvipc_msg_proc_show(struct seq_file *s, void *it)
962 struct msg_queue *msq = it;
965 "%10d %10d %4o %10lu %10lu %5u %5u %5u %5u %5u %5u %10lu %10lu %10lu\n",