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;
111 down_write(&msg_ids(ns).rw_mutex);
113 in_use = msg_ids(ns).in_use;
115 for (total = 0, next_id = 0; total < in_use; next_id++) {
116 msq = idr_find(&msg_ids(ns).ipcs_idr, next_id);
119 ipc_lock_by_ptr(&msq->q_perm);
124 up_write(&msg_ids(ns).rw_mutex);
126 kfree(ns->ids[IPC_MSG_IDS]);
127 ns->ids[IPC_MSG_IDS] = NULL;
130 void __init msg_init(void)
132 __msg_init_ns(&init_ipc_ns, &init_msg_ids);
133 ipc_init_proc_interface("sysvipc/msg",
134 " key msqid perms cbytes qnum lspid lrpid uid gid cuid cgid stime rtime ctime\n",
135 IPC_MSG_IDS, sysvipc_msg_proc_show);
139 * This routine is called in the paths where the rw_mutex is held to protect
140 * access to the idr tree.
142 static inline struct msg_queue *msg_lock_check_down(struct ipc_namespace *ns,
145 struct kern_ipc_perm *ipcp = ipc_lock_check_down(&msg_ids(ns), id);
147 return container_of(ipcp, struct msg_queue, q_perm);
151 * msg_lock_(check_) routines are called in the paths where the rw_mutex
154 static inline struct msg_queue *msg_lock(struct ipc_namespace *ns, int id)
156 struct kern_ipc_perm *ipcp = ipc_lock(&msg_ids(ns), id);
158 return container_of(ipcp, struct msg_queue, q_perm);
161 static inline struct msg_queue *msg_lock_check(struct ipc_namespace *ns,
164 struct kern_ipc_perm *ipcp = ipc_lock_check(&msg_ids(ns), id);
166 return container_of(ipcp, struct msg_queue, q_perm);
169 static inline void msg_rmid(struct ipc_namespace *ns, struct msg_queue *s)
171 ipc_rmid(&msg_ids(ns), &s->q_perm);
175 * newque - Create a new msg queue
177 * @params: ptr to the structure that contains the key and msgflg
179 * Called with msg_ids.rw_mutex held (writer)
181 static int newque(struct ipc_namespace *ns, struct ipc_params *params)
183 struct msg_queue *msq;
185 key_t key = params->key;
186 int msgflg = params->flg;
188 msq = ipc_rcu_alloc(sizeof(*msq));
192 msq->q_perm.mode = msgflg & S_IRWXUGO;
193 msq->q_perm.key = key;
195 msq->q_perm.security = NULL;
196 retval = security_msg_queue_alloc(msq);
203 * ipc_addid() locks msq
205 id = ipc_addid(&msg_ids(ns), &msq->q_perm, ns->msg_ctlmni);
207 security_msg_queue_free(msq);
212 msq->q_perm.id = msg_buildid(id, msq->q_perm.seq);
213 msq->q_stime = msq->q_rtime = 0;
214 msq->q_ctime = get_seconds();
215 msq->q_cbytes = msq->q_qnum = 0;
216 msq->q_qbytes = ns->msg_ctlmnb;
217 msq->q_lspid = msq->q_lrpid = 0;
218 INIT_LIST_HEAD(&msq->q_messages);
219 INIT_LIST_HEAD(&msq->q_receivers);
220 INIT_LIST_HEAD(&msq->q_senders);
224 return msq->q_perm.id;
227 static inline void ss_add(struct msg_queue *msq, struct msg_sender *mss)
230 current->state = TASK_INTERRUPTIBLE;
231 list_add_tail(&mss->list, &msq->q_senders);
234 static inline void ss_del(struct msg_sender *mss)
236 if (mss->list.next != NULL)
237 list_del(&mss->list);
240 static void ss_wakeup(struct list_head *h, int kill)
242 struct list_head *tmp;
246 struct msg_sender *mss;
248 mss = list_entry(tmp, struct msg_sender, list);
251 mss->list.next = NULL;
252 wake_up_process(mss->tsk);
256 static void expunge_all(struct msg_queue *msq, int res)
258 struct list_head *tmp;
260 tmp = msq->q_receivers.next;
261 while (tmp != &msq->q_receivers) {
262 struct msg_receiver *msr;
264 msr = list_entry(tmp, struct msg_receiver, r_list);
267 wake_up_process(msr->r_tsk);
269 msr->r_msg = ERR_PTR(res);
274 * freeque() wakes up waiters on the sender and receiver waiting queue,
275 * removes the message queue from message queue ID IDR, and cleans up all the
276 * messages associated with this queue.
278 * msg_ids.rw_mutex (writer) and the spinlock for this message queue are held
279 * before freeque() is called. msg_ids.rw_mutex remains locked on exit.
281 static void freeque(struct ipc_namespace *ns, struct msg_queue *msq)
283 struct list_head *tmp;
285 expunge_all(msq, -EIDRM);
286 ss_wakeup(&msq->q_senders, 1);
290 tmp = msq->q_messages.next;
291 while (tmp != &msq->q_messages) {
292 struct msg_msg *msg = list_entry(tmp, struct msg_msg, m_list);
295 atomic_dec(&ns->msg_hdrs);
298 atomic_sub(msq->q_cbytes, &ns->msg_bytes);
299 security_msg_queue_free(msq);
304 * Called with msg_ids.rw_mutex and ipcp locked.
306 static inline int msg_security(struct kern_ipc_perm *ipcp, int msgflg)
308 struct msg_queue *msq = container_of(ipcp, struct msg_queue, q_perm);
310 return security_msg_queue_associate(msq, msgflg);
313 asmlinkage long sys_msgget(key_t key, int msgflg)
315 struct ipc_namespace *ns;
316 struct ipc_ops msg_ops;
317 struct ipc_params msg_params;
319 ns = current->nsproxy->ipc_ns;
321 msg_ops.getnew = newque;
322 msg_ops.associate = msg_security;
323 msg_ops.more_checks = NULL;
325 msg_params.key = key;
326 msg_params.flg = msgflg;
328 return ipcget(ns, &msg_ids(ns), &msg_ops, &msg_params);
331 static inline unsigned long
332 copy_msqid_to_user(void __user *buf, struct msqid64_ds *in, int version)
336 return copy_to_user(buf, in, sizeof(*in));
341 memset(&out, 0, sizeof(out));
343 ipc64_perm_to_ipc_perm(&in->msg_perm, &out.msg_perm);
345 out.msg_stime = in->msg_stime;
346 out.msg_rtime = in->msg_rtime;
347 out.msg_ctime = in->msg_ctime;
349 if (in->msg_cbytes > USHRT_MAX)
350 out.msg_cbytes = USHRT_MAX;
352 out.msg_cbytes = in->msg_cbytes;
353 out.msg_lcbytes = in->msg_cbytes;
355 if (in->msg_qnum > USHRT_MAX)
356 out.msg_qnum = USHRT_MAX;
358 out.msg_qnum = in->msg_qnum;
360 if (in->msg_qbytes > USHRT_MAX)
361 out.msg_qbytes = USHRT_MAX;
363 out.msg_qbytes = in->msg_qbytes;
364 out.msg_lqbytes = in->msg_qbytes;
366 out.msg_lspid = in->msg_lspid;
367 out.msg_lrpid = in->msg_lrpid;
369 return copy_to_user(buf, &out, sizeof(out));
377 unsigned long qbytes;
383 static inline unsigned long
384 copy_msqid_from_user(struct msq_setbuf *out, void __user *buf, int version)
389 struct msqid64_ds tbuf;
391 if (copy_from_user(&tbuf, buf, sizeof(tbuf)))
394 out->qbytes = tbuf.msg_qbytes;
395 out->uid = tbuf.msg_perm.uid;
396 out->gid = tbuf.msg_perm.gid;
397 out->mode = tbuf.msg_perm.mode;
403 struct msqid_ds tbuf_old;
405 if (copy_from_user(&tbuf_old, buf, sizeof(tbuf_old)))
408 out->uid = tbuf_old.msg_perm.uid;
409 out->gid = tbuf_old.msg_perm.gid;
410 out->mode = tbuf_old.msg_perm.mode;
412 if (tbuf_old.msg_qbytes == 0)
413 out->qbytes = tbuf_old.msg_lqbytes;
415 out->qbytes = tbuf_old.msg_qbytes;
424 asmlinkage long sys_msgctl(int msqid, int cmd, struct msqid_ds __user *buf)
426 struct kern_ipc_perm *ipcp;
427 struct msq_setbuf uninitialized_var(setbuf);
428 struct msg_queue *msq;
430 struct ipc_namespace *ns;
432 if (msqid < 0 || cmd < 0)
435 version = ipc_parse_version(&cmd);
436 ns = current->nsproxy->ipc_ns;
442 struct msginfo msginfo;
448 * We must not return kernel stack data.
449 * due to padding, it's not enough
450 * to set all member fields.
452 err = security_msg_queue_msgctl(NULL, cmd);
456 memset(&msginfo, 0, sizeof(msginfo));
457 msginfo.msgmni = ns->msg_ctlmni;
458 msginfo.msgmax = ns->msg_ctlmax;
459 msginfo.msgmnb = ns->msg_ctlmnb;
460 msginfo.msgssz = MSGSSZ;
461 msginfo.msgseg = MSGSEG;
462 down_read(&msg_ids(ns).rw_mutex);
463 if (cmd == MSG_INFO) {
464 msginfo.msgpool = msg_ids(ns).in_use;
465 msginfo.msgmap = atomic_read(&ns->msg_hdrs);
466 msginfo.msgtql = atomic_read(&ns->msg_bytes);
468 msginfo.msgmap = MSGMAP;
469 msginfo.msgpool = MSGPOOL;
470 msginfo.msgtql = MSGTQL;
472 max_id = ipc_get_maxid(&msg_ids(ns));
473 up_read(&msg_ids(ns).rw_mutex);
474 if (copy_to_user(buf, &msginfo, sizeof(struct msginfo)))
476 return (max_id < 0) ? 0 : max_id;
478 case MSG_STAT: /* msqid is an index rather than a msg queue id */
481 struct msqid64_ds tbuf;
487 if (cmd == MSG_STAT) {
488 msq = msg_lock(ns, msqid);
491 success_return = msq->q_perm.id;
493 msq = msg_lock_check(ns, msqid);
499 if (ipcperms(&msq->q_perm, S_IRUGO))
502 err = security_msg_queue_msgctl(msq, cmd);
506 memset(&tbuf, 0, sizeof(tbuf));
508 kernel_to_ipc64_perm(&msq->q_perm, &tbuf.msg_perm);
509 tbuf.msg_stime = msq->q_stime;
510 tbuf.msg_rtime = msq->q_rtime;
511 tbuf.msg_ctime = msq->q_ctime;
512 tbuf.msg_cbytes = msq->q_cbytes;
513 tbuf.msg_qnum = msq->q_qnum;
514 tbuf.msg_qbytes = msq->q_qbytes;
515 tbuf.msg_lspid = msq->q_lspid;
516 tbuf.msg_lrpid = msq->q_lrpid;
518 if (copy_msqid_to_user(buf, &tbuf, version))
520 return success_return;
525 if (copy_msqid_from_user(&setbuf, buf, version))
534 down_write(&msg_ids(ns).rw_mutex);
535 msq = msg_lock_check_down(ns, msqid);
543 err = audit_ipc_obj(ipcp);
546 if (cmd == IPC_SET) {
547 err = audit_ipc_set_perm(setbuf.qbytes, setbuf.uid, setbuf.gid,
554 if (current->euid != ipcp->cuid &&
555 current->euid != ipcp->uid && !capable(CAP_SYS_ADMIN))
556 /* We _could_ check for CAP_CHOWN above, but we don't */
559 err = security_msg_queue_msgctl(msq, cmd);
567 if (setbuf.qbytes > ns->msg_ctlmnb && !capable(CAP_SYS_RESOURCE))
570 msq->q_qbytes = setbuf.qbytes;
572 ipcp->uid = setbuf.uid;
573 ipcp->gid = setbuf.gid;
574 ipcp->mode = (ipcp->mode & ~S_IRWXUGO) |
575 (S_IRWXUGO & setbuf.mode);
576 msq->q_ctime = get_seconds();
577 /* sleeping receivers might be excluded by
578 * stricter permissions.
580 expunge_all(msq, -EAGAIN);
581 /* sleeping senders might be able to send
582 * due to a larger queue size.
584 ss_wakeup(&msq->q_senders, 0);
594 up_write(&msg_ids(ns).rw_mutex);
604 static int testmsg(struct msg_msg *msg, long type, int mode)
610 case SEARCH_LESSEQUAL:
611 if (msg->m_type <=type)
615 if (msg->m_type == type)
618 case SEARCH_NOTEQUAL:
619 if (msg->m_type != type)
626 static inline int pipelined_send(struct msg_queue *msq, struct msg_msg *msg)
628 struct list_head *tmp;
630 tmp = msq->q_receivers.next;
631 while (tmp != &msq->q_receivers) {
632 struct msg_receiver *msr;
634 msr = list_entry(tmp, struct msg_receiver, r_list);
636 if (testmsg(msg, msr->r_msgtype, msr->r_mode) &&
637 !security_msg_queue_msgrcv(msq, msg, msr->r_tsk,
638 msr->r_msgtype, msr->r_mode)) {
640 list_del(&msr->r_list);
641 if (msr->r_maxsize < msg->m_ts) {
643 wake_up_process(msr->r_tsk);
645 msr->r_msg = ERR_PTR(-E2BIG);
648 msq->q_lrpid = task_pid_vnr(msr->r_tsk);
649 msq->q_rtime = get_seconds();
650 wake_up_process(msr->r_tsk);
661 long do_msgsnd(int msqid, long mtype, void __user *mtext,
662 size_t msgsz, int msgflg)
664 struct msg_queue *msq;
667 struct ipc_namespace *ns;
669 ns = current->nsproxy->ipc_ns;
671 if (msgsz > ns->msg_ctlmax || (long) msgsz < 0 || msqid < 0)
676 msg = load_msg(mtext, msgsz);
683 msq = msg_lock_check(ns, msqid);
693 if (ipcperms(&msq->q_perm, S_IWUGO))
694 goto out_unlock_free;
696 err = security_msg_queue_msgsnd(msq, msg, msgflg);
698 goto out_unlock_free;
700 if (msgsz + msq->q_cbytes <= msq->q_qbytes &&
701 1 + msq->q_qnum <= msq->q_qbytes) {
705 /* queue full, wait: */
706 if (msgflg & IPC_NOWAIT) {
708 goto out_unlock_free;
715 ipc_lock_by_ptr(&msq->q_perm);
717 if (msq->q_perm.deleted) {
719 goto out_unlock_free;
723 if (signal_pending(current)) {
724 err = -ERESTARTNOHAND;
725 goto out_unlock_free;
729 msq->q_lspid = task_tgid_vnr(current);
730 msq->q_stime = get_seconds();
732 if (!pipelined_send(msq, msg)) {
733 /* noone is waiting for this message, enqueue it */
734 list_add_tail(&msg->m_list, &msq->q_messages);
735 msq->q_cbytes += msgsz;
737 atomic_add(msgsz, &ns->msg_bytes);
738 atomic_inc(&ns->msg_hdrs);
753 sys_msgsnd(int msqid, struct msgbuf __user *msgp, size_t msgsz, int msgflg)
757 if (get_user(mtype, &msgp->mtype))
759 return do_msgsnd(msqid, mtype, msgp->mtext, msgsz, msgflg);
762 static inline int convert_mode(long *msgtyp, int msgflg)
765 * find message of correct type.
766 * msgtyp = 0 => get first.
767 * msgtyp > 0 => get first message of matching type.
768 * msgtyp < 0 => get message with least type must be < abs(msgtype).
774 return SEARCH_LESSEQUAL;
776 if (msgflg & MSG_EXCEPT)
777 return SEARCH_NOTEQUAL;
781 long do_msgrcv(int msqid, long *pmtype, void __user *mtext,
782 size_t msgsz, long msgtyp, int msgflg)
784 struct msg_queue *msq;
787 struct ipc_namespace *ns;
789 if (msqid < 0 || (long) msgsz < 0)
791 mode = convert_mode(&msgtyp, msgflg);
792 ns = current->nsproxy->ipc_ns;
794 msq = msg_lock_check(ns, msqid);
799 struct msg_receiver msr_d;
800 struct list_head *tmp;
802 msg = ERR_PTR(-EACCES);
803 if (ipcperms(&msq->q_perm, S_IRUGO))
806 msg = ERR_PTR(-EAGAIN);
807 tmp = msq->q_messages.next;
808 while (tmp != &msq->q_messages) {
809 struct msg_msg *walk_msg;
811 walk_msg = list_entry(tmp, struct msg_msg, m_list);
812 if (testmsg(walk_msg, msgtyp, mode) &&
813 !security_msg_queue_msgrcv(msq, walk_msg, current,
817 if (mode == SEARCH_LESSEQUAL &&
818 walk_msg->m_type != 1) {
820 msgtyp = walk_msg->m_type - 1;
830 * Found a suitable message.
831 * Unlink it from the queue.
833 if ((msgsz < msg->m_ts) && !(msgflg & MSG_NOERROR)) {
834 msg = ERR_PTR(-E2BIG);
837 list_del(&msg->m_list);
839 msq->q_rtime = get_seconds();
840 msq->q_lrpid = task_tgid_vnr(current);
841 msq->q_cbytes -= msg->m_ts;
842 atomic_sub(msg->m_ts, &ns->msg_bytes);
843 atomic_dec(&ns->msg_hdrs);
844 ss_wakeup(&msq->q_senders, 0);
848 /* No message waiting. Wait for a message */
849 if (msgflg & IPC_NOWAIT) {
850 msg = ERR_PTR(-ENOMSG);
853 list_add_tail(&msr_d.r_list, &msq->q_receivers);
854 msr_d.r_tsk = current;
855 msr_d.r_msgtype = msgtyp;
857 if (msgflg & MSG_NOERROR)
858 msr_d.r_maxsize = INT_MAX;
860 msr_d.r_maxsize = msgsz;
861 msr_d.r_msg = ERR_PTR(-EAGAIN);
862 current->state = TASK_INTERRUPTIBLE;
867 /* Lockless receive, part 1:
868 * Disable preemption. We don't hold a reference to the queue
869 * and getting a reference would defeat the idea of a lockless
870 * operation, thus the code relies on rcu to guarantee the
872 * Prior to destruction, expunge_all(-EIRDM) changes r_msg.
873 * Thus if r_msg is -EAGAIN, then the queue not yet destroyed.
874 * rcu_read_lock() prevents preemption between reading r_msg
875 * and the spin_lock() inside ipc_lock_by_ptr().
879 /* Lockless receive, part 2:
880 * Wait until pipelined_send or expunge_all are outside of
881 * wake_up_process(). There is a race with exit(), see
882 * ipc/mqueue.c for the details.
884 msg = (struct msg_msg*)msr_d.r_msg;
885 while (msg == NULL) {
887 msg = (struct msg_msg *)msr_d.r_msg;
890 /* Lockless receive, part 3:
891 * If there is a message or an error then accept it without
894 if (msg != ERR_PTR(-EAGAIN)) {
899 /* Lockless receive, part 3:
900 * Acquire the queue spinlock.
902 ipc_lock_by_ptr(&msq->q_perm);
905 /* Lockless receive, part 4:
906 * Repeat test after acquiring the spinlock.
908 msg = (struct msg_msg*)msr_d.r_msg;
909 if (msg != ERR_PTR(-EAGAIN))
912 list_del(&msr_d.r_list);
913 if (signal_pending(current)) {
914 msg = ERR_PTR(-ERESTARTNOHAND);
923 msgsz = (msgsz > msg->m_ts) ? msg->m_ts : msgsz;
924 *pmtype = msg->m_type;
925 if (store_msg(mtext, msg, msgsz))
933 asmlinkage long sys_msgrcv(int msqid, struct msgbuf __user *msgp, size_t msgsz,
934 long msgtyp, int msgflg)
938 err = do_msgrcv(msqid, &mtype, msgp->mtext, msgsz, msgtyp, msgflg);
942 if (put_user(mtype, &msgp->mtype))
948 #ifdef CONFIG_PROC_FS
949 static int sysvipc_msg_proc_show(struct seq_file *s, void *it)
951 struct msg_queue *msq = it;
954 "%10d %10d %4o %10lu %10lu %5u %5u %5u %5u %5u %5u %10lu %10lu %10lu\n",