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>
39 #include <linux/ipc_namespace.h>
41 #include <asm/current.h>
42 #include <asm/uaccess.h>
46 * one msg_receiver structure for each sleeping receiver:
49 struct list_head r_list;
50 struct task_struct *r_tsk;
56 struct msg_msg *volatile r_msg;
59 /* one msg_sender for each sleeping sender */
61 struct list_head list;
62 struct task_struct *tsk;
66 #define SEARCH_EQUAL 2
67 #define SEARCH_NOTEQUAL 3
68 #define SEARCH_LESSEQUAL 4
70 #define msg_ids(ns) ((ns)->ids[IPC_MSG_IDS])
72 #define msg_unlock(msq) ipc_unlock(&(msq)->q_perm)
73 #define msg_buildid(id, seq) ipc_buildid(id, seq)
75 static void freeque(struct ipc_namespace *, struct kern_ipc_perm *);
76 static int newque(struct ipc_namespace *, struct ipc_params *);
78 static int sysvipc_msg_proc_show(struct seq_file *s, void *it);
81 void msg_init_ns(struct ipc_namespace *ns)
83 ns->msg_ctlmax = MSGMAX;
84 ns->msg_ctlmnb = MSGMNB;
85 ns->msg_ctlmni = MSGMNI;
86 atomic_set(&ns->msg_bytes, 0);
87 atomic_set(&ns->msg_hdrs, 0);
88 ipc_init_ids(&ns->ids[IPC_MSG_IDS]);
92 void msg_exit_ns(struct ipc_namespace *ns)
94 free_ipcs(ns, &msg_ids(ns), freeque);
98 void __init msg_init(void)
100 msg_init_ns(&init_ipc_ns);
101 ipc_init_proc_interface("sysvipc/msg",
102 " key msqid perms cbytes qnum lspid lrpid uid gid cuid cgid stime rtime ctime\n",
103 IPC_MSG_IDS, sysvipc_msg_proc_show);
107 * This routine is called in the paths where the rw_mutex is held to protect
108 * access to the idr tree.
110 static inline struct msg_queue *msg_lock_check_down(struct ipc_namespace *ns,
113 struct kern_ipc_perm *ipcp = ipc_lock_check_down(&msg_ids(ns), id);
116 return (struct msg_queue *)ipcp;
118 return container_of(ipcp, struct msg_queue, q_perm);
122 * msg_lock_(check_) routines are called in the paths where the rw_mutex
125 static inline struct msg_queue *msg_lock(struct ipc_namespace *ns, int id)
127 struct kern_ipc_perm *ipcp = ipc_lock(&msg_ids(ns), id);
130 return (struct msg_queue *)ipcp;
132 return container_of(ipcp, struct msg_queue, q_perm);
135 static inline struct msg_queue *msg_lock_check(struct ipc_namespace *ns,
138 struct kern_ipc_perm *ipcp = ipc_lock_check(&msg_ids(ns), id);
141 return (struct msg_queue *)ipcp;
143 return container_of(ipcp, struct msg_queue, q_perm);
146 static inline void msg_rmid(struct ipc_namespace *ns, struct msg_queue *s)
148 ipc_rmid(&msg_ids(ns), &s->q_perm);
152 * newque - Create a new msg queue
154 * @params: ptr to the structure that contains the key and msgflg
156 * Called with msg_ids.rw_mutex held (writer)
158 static int newque(struct ipc_namespace *ns, struct ipc_params *params)
160 struct msg_queue *msq;
162 key_t key = params->key;
163 int msgflg = params->flg;
165 msq = ipc_rcu_alloc(sizeof(*msq));
169 msq->q_perm.mode = msgflg & S_IRWXUGO;
170 msq->q_perm.key = key;
172 msq->q_perm.security = NULL;
173 retval = security_msg_queue_alloc(msq);
180 * ipc_addid() locks msq
182 id = ipc_addid(&msg_ids(ns), &msq->q_perm, ns->msg_ctlmni);
184 security_msg_queue_free(msq);
189 msq->q_perm.id = msg_buildid(id, msq->q_perm.seq);
190 msq->q_stime = msq->q_rtime = 0;
191 msq->q_ctime = get_seconds();
192 msq->q_cbytes = msq->q_qnum = 0;
193 msq->q_qbytes = ns->msg_ctlmnb;
194 msq->q_lspid = msq->q_lrpid = 0;
195 INIT_LIST_HEAD(&msq->q_messages);
196 INIT_LIST_HEAD(&msq->q_receivers);
197 INIT_LIST_HEAD(&msq->q_senders);
201 return msq->q_perm.id;
204 static inline void ss_add(struct msg_queue *msq, struct msg_sender *mss)
207 current->state = TASK_INTERRUPTIBLE;
208 list_add_tail(&mss->list, &msq->q_senders);
211 static inline void ss_del(struct msg_sender *mss)
213 if (mss->list.next != NULL)
214 list_del(&mss->list);
217 static void ss_wakeup(struct list_head *h, int kill)
219 struct list_head *tmp;
223 struct msg_sender *mss;
225 mss = list_entry(tmp, struct msg_sender, list);
228 mss->list.next = NULL;
229 wake_up_process(mss->tsk);
233 static void expunge_all(struct msg_queue *msq, int res)
235 struct list_head *tmp;
237 tmp = msq->q_receivers.next;
238 while (tmp != &msq->q_receivers) {
239 struct msg_receiver *msr;
241 msr = list_entry(tmp, struct msg_receiver, r_list);
244 wake_up_process(msr->r_tsk);
246 msr->r_msg = ERR_PTR(res);
251 * freeque() wakes up waiters on the sender and receiver waiting queue,
252 * removes the message queue from message queue ID IDR, and cleans up all the
253 * messages associated with this queue.
255 * msg_ids.rw_mutex (writer) and the spinlock for this message queue are held
256 * before freeque() is called. msg_ids.rw_mutex remains locked on exit.
258 static void freeque(struct ipc_namespace *ns, struct kern_ipc_perm *ipcp)
260 struct list_head *tmp;
261 struct msg_queue *msq = container_of(ipcp, struct msg_queue, q_perm);
263 expunge_all(msq, -EIDRM);
264 ss_wakeup(&msq->q_senders, 1);
268 tmp = msq->q_messages.next;
269 while (tmp != &msq->q_messages) {
270 struct msg_msg *msg = list_entry(tmp, struct msg_msg, m_list);
273 atomic_dec(&ns->msg_hdrs);
276 atomic_sub(msq->q_cbytes, &ns->msg_bytes);
277 security_msg_queue_free(msq);
282 * Called with msg_ids.rw_mutex and ipcp locked.
284 static inline int msg_security(struct kern_ipc_perm *ipcp, int msgflg)
286 struct msg_queue *msq = container_of(ipcp, struct msg_queue, q_perm);
288 return security_msg_queue_associate(msq, msgflg);
291 asmlinkage long sys_msgget(key_t key, int msgflg)
293 struct ipc_namespace *ns;
294 struct ipc_ops msg_ops;
295 struct ipc_params msg_params;
297 ns = current->nsproxy->ipc_ns;
299 msg_ops.getnew = newque;
300 msg_ops.associate = msg_security;
301 msg_ops.more_checks = NULL;
303 msg_params.key = key;
304 msg_params.flg = msgflg;
306 return ipcget(ns, &msg_ids(ns), &msg_ops, &msg_params);
309 static inline unsigned long
310 copy_msqid_to_user(void __user *buf, struct msqid64_ds *in, int version)
314 return copy_to_user(buf, in, sizeof(*in));
319 memset(&out, 0, sizeof(out));
321 ipc64_perm_to_ipc_perm(&in->msg_perm, &out.msg_perm);
323 out.msg_stime = in->msg_stime;
324 out.msg_rtime = in->msg_rtime;
325 out.msg_ctime = in->msg_ctime;
327 if (in->msg_cbytes > USHRT_MAX)
328 out.msg_cbytes = USHRT_MAX;
330 out.msg_cbytes = in->msg_cbytes;
331 out.msg_lcbytes = in->msg_cbytes;
333 if (in->msg_qnum > USHRT_MAX)
334 out.msg_qnum = USHRT_MAX;
336 out.msg_qnum = in->msg_qnum;
338 if (in->msg_qbytes > USHRT_MAX)
339 out.msg_qbytes = USHRT_MAX;
341 out.msg_qbytes = in->msg_qbytes;
342 out.msg_lqbytes = in->msg_qbytes;
344 out.msg_lspid = in->msg_lspid;
345 out.msg_lrpid = in->msg_lrpid;
347 return copy_to_user(buf, &out, sizeof(out));
355 unsigned long qbytes;
361 static inline unsigned long
362 copy_msqid_from_user(struct msq_setbuf *out, void __user *buf, int version)
367 struct msqid64_ds tbuf;
369 if (copy_from_user(&tbuf, buf, sizeof(tbuf)))
372 out->qbytes = tbuf.msg_qbytes;
373 out->uid = tbuf.msg_perm.uid;
374 out->gid = tbuf.msg_perm.gid;
375 out->mode = tbuf.msg_perm.mode;
381 struct msqid_ds tbuf_old;
383 if (copy_from_user(&tbuf_old, buf, sizeof(tbuf_old)))
386 out->uid = tbuf_old.msg_perm.uid;
387 out->gid = tbuf_old.msg_perm.gid;
388 out->mode = tbuf_old.msg_perm.mode;
390 if (tbuf_old.msg_qbytes == 0)
391 out->qbytes = tbuf_old.msg_lqbytes;
393 out->qbytes = tbuf_old.msg_qbytes;
402 asmlinkage long sys_msgctl(int msqid, int cmd, struct msqid_ds __user *buf)
404 struct kern_ipc_perm *ipcp;
405 struct msq_setbuf uninitialized_var(setbuf);
406 struct msg_queue *msq;
408 struct ipc_namespace *ns;
410 if (msqid < 0 || cmd < 0)
413 version = ipc_parse_version(&cmd);
414 ns = current->nsproxy->ipc_ns;
420 struct msginfo msginfo;
426 * We must not return kernel stack data.
427 * due to padding, it's not enough
428 * to set all member fields.
430 err = security_msg_queue_msgctl(NULL, cmd);
434 memset(&msginfo, 0, sizeof(msginfo));
435 msginfo.msgmni = ns->msg_ctlmni;
436 msginfo.msgmax = ns->msg_ctlmax;
437 msginfo.msgmnb = ns->msg_ctlmnb;
438 msginfo.msgssz = MSGSSZ;
439 msginfo.msgseg = MSGSEG;
440 down_read(&msg_ids(ns).rw_mutex);
441 if (cmd == MSG_INFO) {
442 msginfo.msgpool = msg_ids(ns).in_use;
443 msginfo.msgmap = atomic_read(&ns->msg_hdrs);
444 msginfo.msgtql = atomic_read(&ns->msg_bytes);
446 msginfo.msgmap = MSGMAP;
447 msginfo.msgpool = MSGPOOL;
448 msginfo.msgtql = MSGTQL;
450 max_id = ipc_get_maxid(&msg_ids(ns));
451 up_read(&msg_ids(ns).rw_mutex);
452 if (copy_to_user(buf, &msginfo, sizeof(struct msginfo)))
454 return (max_id < 0) ? 0 : max_id;
456 case MSG_STAT: /* msqid is an index rather than a msg queue id */
459 struct msqid64_ds tbuf;
465 if (cmd == MSG_STAT) {
466 msq = msg_lock(ns, msqid);
469 success_return = msq->q_perm.id;
471 msq = msg_lock_check(ns, msqid);
477 if (ipcperms(&msq->q_perm, S_IRUGO))
480 err = security_msg_queue_msgctl(msq, cmd);
484 memset(&tbuf, 0, sizeof(tbuf));
486 kernel_to_ipc64_perm(&msq->q_perm, &tbuf.msg_perm);
487 tbuf.msg_stime = msq->q_stime;
488 tbuf.msg_rtime = msq->q_rtime;
489 tbuf.msg_ctime = msq->q_ctime;
490 tbuf.msg_cbytes = msq->q_cbytes;
491 tbuf.msg_qnum = msq->q_qnum;
492 tbuf.msg_qbytes = msq->q_qbytes;
493 tbuf.msg_lspid = msq->q_lspid;
494 tbuf.msg_lrpid = msq->q_lrpid;
496 if (copy_msqid_to_user(buf, &tbuf, version))
498 return success_return;
503 if (copy_msqid_from_user(&setbuf, buf, version))
512 down_write(&msg_ids(ns).rw_mutex);
513 msq = msg_lock_check_down(ns, msqid);
521 err = audit_ipc_obj(ipcp);
524 if (cmd == IPC_SET) {
525 err = audit_ipc_set_perm(setbuf.qbytes, setbuf.uid, setbuf.gid,
532 if (current->euid != ipcp->cuid &&
533 current->euid != ipcp->uid && !capable(CAP_SYS_ADMIN))
534 /* We _could_ check for CAP_CHOWN above, but we don't */
537 err = security_msg_queue_msgctl(msq, cmd);
545 if (setbuf.qbytes > ns->msg_ctlmnb && !capable(CAP_SYS_RESOURCE))
548 msq->q_qbytes = setbuf.qbytes;
550 ipcp->uid = setbuf.uid;
551 ipcp->gid = setbuf.gid;
552 ipcp->mode = (ipcp->mode & ~S_IRWXUGO) |
553 (S_IRWXUGO & setbuf.mode);
554 msq->q_ctime = get_seconds();
555 /* sleeping receivers might be excluded by
556 * stricter permissions.
558 expunge_all(msq, -EAGAIN);
559 /* sleeping senders might be able to send
560 * due to a larger queue size.
562 ss_wakeup(&msq->q_senders, 0);
567 freeque(ns, &msq->q_perm);
572 up_write(&msg_ids(ns).rw_mutex);
582 static int testmsg(struct msg_msg *msg, long type, int mode)
588 case SEARCH_LESSEQUAL:
589 if (msg->m_type <=type)
593 if (msg->m_type == type)
596 case SEARCH_NOTEQUAL:
597 if (msg->m_type != type)
604 static inline int pipelined_send(struct msg_queue *msq, struct msg_msg *msg)
606 struct list_head *tmp;
608 tmp = msq->q_receivers.next;
609 while (tmp != &msq->q_receivers) {
610 struct msg_receiver *msr;
612 msr = list_entry(tmp, struct msg_receiver, r_list);
614 if (testmsg(msg, msr->r_msgtype, msr->r_mode) &&
615 !security_msg_queue_msgrcv(msq, msg, msr->r_tsk,
616 msr->r_msgtype, msr->r_mode)) {
618 list_del(&msr->r_list);
619 if (msr->r_maxsize < msg->m_ts) {
621 wake_up_process(msr->r_tsk);
623 msr->r_msg = ERR_PTR(-E2BIG);
626 msq->q_lrpid = task_pid_vnr(msr->r_tsk);
627 msq->q_rtime = get_seconds();
628 wake_up_process(msr->r_tsk);
639 long do_msgsnd(int msqid, long mtype, void __user *mtext,
640 size_t msgsz, int msgflg)
642 struct msg_queue *msq;
645 struct ipc_namespace *ns;
647 ns = current->nsproxy->ipc_ns;
649 if (msgsz > ns->msg_ctlmax || (long) msgsz < 0 || msqid < 0)
654 msg = load_msg(mtext, msgsz);
661 msq = msg_lock_check(ns, msqid);
671 if (ipcperms(&msq->q_perm, S_IWUGO))
672 goto out_unlock_free;
674 err = security_msg_queue_msgsnd(msq, msg, msgflg);
676 goto out_unlock_free;
678 if (msgsz + msq->q_cbytes <= msq->q_qbytes &&
679 1 + msq->q_qnum <= msq->q_qbytes) {
683 /* queue full, wait: */
684 if (msgflg & IPC_NOWAIT) {
686 goto out_unlock_free;
693 ipc_lock_by_ptr(&msq->q_perm);
695 if (msq->q_perm.deleted) {
697 goto out_unlock_free;
701 if (signal_pending(current)) {
702 err = -ERESTARTNOHAND;
703 goto out_unlock_free;
707 msq->q_lspid = task_tgid_vnr(current);
708 msq->q_stime = get_seconds();
710 if (!pipelined_send(msq, msg)) {
711 /* noone is waiting for this message, enqueue it */
712 list_add_tail(&msg->m_list, &msq->q_messages);
713 msq->q_cbytes += msgsz;
715 atomic_add(msgsz, &ns->msg_bytes);
716 atomic_inc(&ns->msg_hdrs);
731 sys_msgsnd(int msqid, struct msgbuf __user *msgp, size_t msgsz, int msgflg)
735 if (get_user(mtype, &msgp->mtype))
737 return do_msgsnd(msqid, mtype, msgp->mtext, msgsz, msgflg);
740 static inline int convert_mode(long *msgtyp, int msgflg)
743 * find message of correct type.
744 * msgtyp = 0 => get first.
745 * msgtyp > 0 => get first message of matching type.
746 * msgtyp < 0 => get message with least type must be < abs(msgtype).
752 return SEARCH_LESSEQUAL;
754 if (msgflg & MSG_EXCEPT)
755 return SEARCH_NOTEQUAL;
759 long do_msgrcv(int msqid, long *pmtype, void __user *mtext,
760 size_t msgsz, long msgtyp, int msgflg)
762 struct msg_queue *msq;
765 struct ipc_namespace *ns;
767 if (msqid < 0 || (long) msgsz < 0)
769 mode = convert_mode(&msgtyp, msgflg);
770 ns = current->nsproxy->ipc_ns;
772 msq = msg_lock_check(ns, msqid);
777 struct msg_receiver msr_d;
778 struct list_head *tmp;
780 msg = ERR_PTR(-EACCES);
781 if (ipcperms(&msq->q_perm, S_IRUGO))
784 msg = ERR_PTR(-EAGAIN);
785 tmp = msq->q_messages.next;
786 while (tmp != &msq->q_messages) {
787 struct msg_msg *walk_msg;
789 walk_msg = list_entry(tmp, struct msg_msg, m_list);
790 if (testmsg(walk_msg, msgtyp, mode) &&
791 !security_msg_queue_msgrcv(msq, walk_msg, current,
795 if (mode == SEARCH_LESSEQUAL &&
796 walk_msg->m_type != 1) {
798 msgtyp = walk_msg->m_type - 1;
808 * Found a suitable message.
809 * Unlink it from the queue.
811 if ((msgsz < msg->m_ts) && !(msgflg & MSG_NOERROR)) {
812 msg = ERR_PTR(-E2BIG);
815 list_del(&msg->m_list);
817 msq->q_rtime = get_seconds();
818 msq->q_lrpid = task_tgid_vnr(current);
819 msq->q_cbytes -= msg->m_ts;
820 atomic_sub(msg->m_ts, &ns->msg_bytes);
821 atomic_dec(&ns->msg_hdrs);
822 ss_wakeup(&msq->q_senders, 0);
826 /* No message waiting. Wait for a message */
827 if (msgflg & IPC_NOWAIT) {
828 msg = ERR_PTR(-ENOMSG);
831 list_add_tail(&msr_d.r_list, &msq->q_receivers);
832 msr_d.r_tsk = current;
833 msr_d.r_msgtype = msgtyp;
835 if (msgflg & MSG_NOERROR)
836 msr_d.r_maxsize = INT_MAX;
838 msr_d.r_maxsize = msgsz;
839 msr_d.r_msg = ERR_PTR(-EAGAIN);
840 current->state = TASK_INTERRUPTIBLE;
845 /* Lockless receive, part 1:
846 * Disable preemption. We don't hold a reference to the queue
847 * and getting a reference would defeat the idea of a lockless
848 * operation, thus the code relies on rcu to guarantee the
850 * Prior to destruction, expunge_all(-EIRDM) changes r_msg.
851 * Thus if r_msg is -EAGAIN, then the queue not yet destroyed.
852 * rcu_read_lock() prevents preemption between reading r_msg
853 * and the spin_lock() inside ipc_lock_by_ptr().
857 /* Lockless receive, part 2:
858 * Wait until pipelined_send or expunge_all are outside of
859 * wake_up_process(). There is a race with exit(), see
860 * ipc/mqueue.c for the details.
862 msg = (struct msg_msg*)msr_d.r_msg;
863 while (msg == NULL) {
865 msg = (struct msg_msg *)msr_d.r_msg;
868 /* Lockless receive, part 3:
869 * If there is a message or an error then accept it without
872 if (msg != ERR_PTR(-EAGAIN)) {
877 /* Lockless receive, part 3:
878 * Acquire the queue spinlock.
880 ipc_lock_by_ptr(&msq->q_perm);
883 /* Lockless receive, part 4:
884 * Repeat test after acquiring the spinlock.
886 msg = (struct msg_msg*)msr_d.r_msg;
887 if (msg != ERR_PTR(-EAGAIN))
890 list_del(&msr_d.r_list);
891 if (signal_pending(current)) {
892 msg = ERR_PTR(-ERESTARTNOHAND);
901 msgsz = (msgsz > msg->m_ts) ? msg->m_ts : msgsz;
902 *pmtype = msg->m_type;
903 if (store_msg(mtext, msg, msgsz))
911 asmlinkage long sys_msgrcv(int msqid, struct msgbuf __user *msgp, size_t msgsz,
912 long msgtyp, int msgflg)
916 err = do_msgrcv(msqid, &mtype, msgp->mtext, msgsz, msgtyp, msgflg);
920 if (put_user(mtype, &msgp->mtype))
926 #ifdef CONFIG_PROC_FS
927 static int sysvipc_msg_proc_show(struct seq_file *s, void *it)
929 struct msg_queue *msq = it;
932 "%10d %10d %4o %10lu %10lu %5u %5u %5u %5u %5u %5u %10lu %10lu %10lu\n",