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>
21 #include <linux/capability.h>
22 #include <linux/config.h>
23 #include <linux/slab.h>
24 #include <linux/msg.h>
25 #include <linux/spinlock.h>
26 #include <linux/init.h>
27 #include <linux/proc_fs.h>
28 #include <linux/list.h>
29 #include <linux/security.h>
30 #include <linux/sched.h>
31 #include <linux/syscalls.h>
32 #include <linux/audit.h>
33 #include <linux/seq_file.h>
34 #include <linux/mutex.h>
36 #include <asm/current.h>
37 #include <asm/uaccess.h>
41 int msg_ctlmax = MSGMAX;
42 int msg_ctlmnb = MSGMNB;
43 int msg_ctlmni = MSGMNI;
45 /* one msg_receiver structure for each sleeping receiver */
47 struct list_head r_list;
48 struct task_struct* r_tsk;
54 struct msg_msg* volatile r_msg;
57 /* one msg_sender for each sleeping sender */
59 struct list_head list;
60 struct task_struct* tsk;
64 #define SEARCH_EQUAL 2
65 #define SEARCH_NOTEQUAL 3
66 #define SEARCH_LESSEQUAL 4
68 static atomic_t msg_bytes = ATOMIC_INIT(0);
69 static atomic_t msg_hdrs = ATOMIC_INIT(0);
71 static struct ipc_ids msg_ids;
73 #define msg_lock(id) ((struct msg_queue*)ipc_lock(&msg_ids,id))
74 #define msg_unlock(msq) ipc_unlock(&(msq)->q_perm)
75 #define msg_rmid(id) ((struct msg_queue*)ipc_rmid(&msg_ids,id))
76 #define msg_checkid(msq, msgid) \
77 ipc_checkid(&msg_ids,&msq->q_perm,msgid)
78 #define msg_buildid(id, seq) \
79 ipc_buildid(&msg_ids, id, seq)
81 static void freeque (struct msg_queue *msq, int id);
82 static int newque (key_t key, int msgflg);
84 static int sysvipc_msg_proc_show(struct seq_file *s, void *it);
87 void __init msg_init (void)
89 ipc_init_ids(&msg_ids,msg_ctlmni);
90 ipc_init_proc_interface("sysvipc/msg",
91 " key msqid perms cbytes qnum lspid lrpid uid gid cuid cgid stime rtime ctime\n",
93 sysvipc_msg_proc_show);
96 static int newque (key_t key, int msgflg)
100 struct msg_queue *msq;
102 msq = ipc_rcu_alloc(sizeof(*msq));
106 msq->q_perm.mode = (msgflg & S_IRWXUGO);
107 msq->q_perm.key = key;
109 msq->q_perm.security = NULL;
110 retval = security_msg_queue_alloc(msq);
116 id = ipc_addid(&msg_ids, &msq->q_perm, msg_ctlmni);
118 security_msg_queue_free(msq);
123 msq->q_id = msg_buildid(id,msq->q_perm.seq);
124 msq->q_stime = msq->q_rtime = 0;
125 msq->q_ctime = get_seconds();
126 msq->q_cbytes = msq->q_qnum = 0;
127 msq->q_qbytes = msg_ctlmnb;
128 msq->q_lspid = msq->q_lrpid = 0;
129 INIT_LIST_HEAD(&msq->q_messages);
130 INIT_LIST_HEAD(&msq->q_receivers);
131 INIT_LIST_HEAD(&msq->q_senders);
137 static inline void ss_add(struct msg_queue* msq, struct msg_sender* mss)
140 current->state=TASK_INTERRUPTIBLE;
141 list_add_tail(&mss->list,&msq->q_senders);
144 static inline void ss_del(struct msg_sender* mss)
146 if(mss->list.next != NULL)
147 list_del(&mss->list);
150 static void ss_wakeup(struct list_head* h, int kill)
152 struct list_head *tmp;
156 struct msg_sender* mss;
158 mss = list_entry(tmp,struct msg_sender,list);
162 wake_up_process(mss->tsk);
166 static void expunge_all(struct msg_queue* msq, int res)
168 struct list_head *tmp;
170 tmp = msq->q_receivers.next;
171 while (tmp != &msq->q_receivers) {
172 struct msg_receiver* msr;
174 msr = list_entry(tmp,struct msg_receiver,r_list);
177 wake_up_process(msr->r_tsk);
179 msr->r_msg = ERR_PTR(res);
183 * freeque() wakes up waiters on the sender and receiver waiting queue,
184 * removes the message queue from message queue ID
185 * array, and cleans up all the messages associated with this queue.
187 * msg_ids.mutex and the spinlock for this message queue is hold
188 * before freeque() is called. msg_ids.mutex remains locked on exit.
190 static void freeque (struct msg_queue *msq, int id)
192 struct list_head *tmp;
194 expunge_all(msq,-EIDRM);
195 ss_wakeup(&msq->q_senders,1);
199 tmp = msq->q_messages.next;
200 while(tmp != &msq->q_messages) {
201 struct msg_msg* msg = list_entry(tmp,struct msg_msg,m_list);
203 atomic_dec(&msg_hdrs);
206 atomic_sub(msq->q_cbytes, &msg_bytes);
207 security_msg_queue_free(msq);
211 asmlinkage long sys_msgget (key_t key, int msgflg)
213 int id, ret = -EPERM;
214 struct msg_queue *msq;
216 mutex_lock(&msg_ids.mutex);
217 if (key == IPC_PRIVATE)
218 ret = newque(key, msgflg);
219 else if ((id = ipc_findkey(&msg_ids, key)) == -1) { /* key not used */
220 if (!(msgflg & IPC_CREAT))
223 ret = newque(key, msgflg);
224 } else if (msgflg & IPC_CREAT && msgflg & IPC_EXCL) {
229 if (ipcperms(&msq->q_perm, msgflg))
232 int qid = msg_buildid(id, msq->q_perm.seq);
233 ret = security_msg_queue_associate(msq, msgflg);
239 mutex_unlock(&msg_ids.mutex);
243 static inline unsigned long copy_msqid_to_user(void __user *buf, struct msqid64_ds *in, int version)
247 return copy_to_user (buf, in, sizeof(*in));
252 memset(&out,0,sizeof(out));
254 ipc64_perm_to_ipc_perm(&in->msg_perm, &out.msg_perm);
256 out.msg_stime = in->msg_stime;
257 out.msg_rtime = in->msg_rtime;
258 out.msg_ctime = in->msg_ctime;
260 if(in->msg_cbytes > USHRT_MAX)
261 out.msg_cbytes = USHRT_MAX;
263 out.msg_cbytes = in->msg_cbytes;
264 out.msg_lcbytes = in->msg_cbytes;
266 if(in->msg_qnum > USHRT_MAX)
267 out.msg_qnum = USHRT_MAX;
269 out.msg_qnum = in->msg_qnum;
271 if(in->msg_qbytes > USHRT_MAX)
272 out.msg_qbytes = USHRT_MAX;
274 out.msg_qbytes = in->msg_qbytes;
275 out.msg_lqbytes = in->msg_qbytes;
277 out.msg_lspid = in->msg_lspid;
278 out.msg_lrpid = in->msg_lrpid;
280 return copy_to_user (buf, &out, sizeof(out));
288 unsigned long qbytes;
294 static inline unsigned long copy_msqid_from_user(struct msq_setbuf *out, void __user *buf, int version)
299 struct msqid64_ds tbuf;
301 if (copy_from_user (&tbuf, buf, sizeof (tbuf)))
304 out->qbytes = tbuf.msg_qbytes;
305 out->uid = tbuf.msg_perm.uid;
306 out->gid = tbuf.msg_perm.gid;
307 out->mode = tbuf.msg_perm.mode;
313 struct msqid_ds tbuf_old;
315 if (copy_from_user (&tbuf_old, buf, sizeof (tbuf_old)))
318 out->uid = tbuf_old.msg_perm.uid;
319 out->gid = tbuf_old.msg_perm.gid;
320 out->mode = tbuf_old.msg_perm.mode;
322 if(tbuf_old.msg_qbytes == 0)
323 out->qbytes = tbuf_old.msg_lqbytes;
325 out->qbytes = tbuf_old.msg_qbytes;
334 asmlinkage long sys_msgctl (int msqid, int cmd, struct msqid_ds __user *buf)
337 struct msg_queue *msq;
338 struct msq_setbuf setbuf;
339 struct kern_ipc_perm *ipcp;
341 if (msqid < 0 || cmd < 0)
344 version = ipc_parse_version(&cmd);
350 struct msginfo msginfo;
354 /* We must not return kernel stack data.
355 * due to padding, it's not enough
356 * to set all member fields.
359 err = security_msg_queue_msgctl(NULL, cmd);
363 memset(&msginfo,0,sizeof(msginfo));
364 msginfo.msgmni = msg_ctlmni;
365 msginfo.msgmax = msg_ctlmax;
366 msginfo.msgmnb = msg_ctlmnb;
367 msginfo.msgssz = MSGSSZ;
368 msginfo.msgseg = MSGSEG;
369 mutex_lock(&msg_ids.mutex);
370 if (cmd == MSG_INFO) {
371 msginfo.msgpool = msg_ids.in_use;
372 msginfo.msgmap = atomic_read(&msg_hdrs);
373 msginfo.msgtql = atomic_read(&msg_bytes);
375 msginfo.msgmap = MSGMAP;
376 msginfo.msgpool = MSGPOOL;
377 msginfo.msgtql = MSGTQL;
379 max_id = msg_ids.max_id;
380 mutex_unlock(&msg_ids.mutex);
381 if (copy_to_user (buf, &msginfo, sizeof(struct msginfo)))
383 return (max_id < 0) ? 0: max_id;
388 struct msqid64_ds tbuf;
392 if(cmd == MSG_STAT && msqid >= msg_ids.entries->size)
395 memset(&tbuf,0,sizeof(tbuf));
397 msq = msg_lock(msqid);
401 if(cmd == MSG_STAT) {
402 success_return = msg_buildid(msqid, msq->q_perm.seq);
405 if (msg_checkid(msq,msqid))
410 if (ipcperms (&msq->q_perm, S_IRUGO))
413 err = security_msg_queue_msgctl(msq, cmd);
417 kernel_to_ipc64_perm(&msq->q_perm, &tbuf.msg_perm);
418 tbuf.msg_stime = msq->q_stime;
419 tbuf.msg_rtime = msq->q_rtime;
420 tbuf.msg_ctime = msq->q_ctime;
421 tbuf.msg_cbytes = msq->q_cbytes;
422 tbuf.msg_qnum = msq->q_qnum;
423 tbuf.msg_qbytes = msq->q_qbytes;
424 tbuf.msg_lspid = msq->q_lspid;
425 tbuf.msg_lrpid = msq->q_lrpid;
427 if (copy_msqid_to_user(buf, &tbuf, version))
429 return success_return;
434 if (copy_msqid_from_user (&setbuf, buf, version))
443 mutex_lock(&msg_ids.mutex);
444 msq = msg_lock(msqid);
450 if (msg_checkid(msq,msqid))
454 err = audit_ipc_obj(ipcp);
458 err = audit_ipc_set_perm(setbuf.qbytes, setbuf.uid, setbuf.gid, setbuf.mode);
464 if (current->euid != ipcp->cuid &&
465 current->euid != ipcp->uid && !capable(CAP_SYS_ADMIN))
466 /* We _could_ check for CAP_CHOWN above, but we don't */
469 err = security_msg_queue_msgctl(msq, cmd);
477 if (setbuf.qbytes > msg_ctlmnb && !capable(CAP_SYS_RESOURCE))
480 msq->q_qbytes = setbuf.qbytes;
482 ipcp->uid = setbuf.uid;
483 ipcp->gid = setbuf.gid;
484 ipcp->mode = (ipcp->mode & ~S_IRWXUGO) |
485 (S_IRWXUGO & setbuf.mode);
486 msq->q_ctime = get_seconds();
487 /* sleeping receivers might be excluded by
488 * stricter permissions.
490 expunge_all(msq,-EAGAIN);
491 /* sleeping senders might be able to send
492 * due to a larger queue size.
494 ss_wakeup(&msq->q_senders,0);
499 freeque (msq, msqid);
504 mutex_unlock(&msg_ids.mutex);
514 static int testmsg(struct msg_msg* msg,long type,int mode)
520 case SEARCH_LESSEQUAL:
521 if(msg->m_type <=type)
525 if(msg->m_type == type)
528 case SEARCH_NOTEQUAL:
529 if(msg->m_type != type)
536 static inline int pipelined_send(struct msg_queue* msq, struct msg_msg* msg)
538 struct list_head* tmp;
540 tmp = msq->q_receivers.next;
541 while (tmp != &msq->q_receivers) {
542 struct msg_receiver* msr;
543 msr = list_entry(tmp,struct msg_receiver,r_list);
545 if(testmsg(msg,msr->r_msgtype,msr->r_mode) &&
546 !security_msg_queue_msgrcv(msq, msg, msr->r_tsk, msr->r_msgtype, msr->r_mode)) {
547 list_del(&msr->r_list);
548 if(msr->r_maxsize < msg->m_ts) {
550 wake_up_process(msr->r_tsk);
552 msr->r_msg = ERR_PTR(-E2BIG);
555 msq->q_lrpid = msr->r_tsk->pid;
556 msq->q_rtime = get_seconds();
557 wake_up_process(msr->r_tsk);
567 asmlinkage long sys_msgsnd (int msqid, struct msgbuf __user *msgp, size_t msgsz, int msgflg)
569 struct msg_queue *msq;
574 if (msgsz > msg_ctlmax || (long) msgsz < 0 || msqid < 0)
576 if (get_user(mtype, &msgp->mtype))
581 msg = load_msg(msgp->mtext, msgsz);
588 msq = msg_lock(msqid);
594 if (msg_checkid(msq,msqid))
595 goto out_unlock_free;
601 if (ipcperms(&msq->q_perm, S_IWUGO))
602 goto out_unlock_free;
604 err = security_msg_queue_msgsnd(msq, msg, msgflg);
606 goto out_unlock_free;
608 if(msgsz + msq->q_cbytes <= msq->q_qbytes &&
609 1 + msq->q_qnum <= msq->q_qbytes) {
613 /* queue full, wait: */
614 if(msgflg&IPC_NOWAIT) {
616 goto out_unlock_free;
623 ipc_lock_by_ptr(&msq->q_perm);
625 if (msq->q_perm.deleted) {
627 goto out_unlock_free;
631 if (signal_pending(current)) {
633 goto out_unlock_free;
637 msq->q_lspid = current->tgid;
638 msq->q_stime = get_seconds();
640 if(!pipelined_send(msq,msg)) {
641 /* noone is waiting for this message, enqueue it */
642 list_add_tail(&msg->m_list,&msq->q_messages);
643 msq->q_cbytes += msgsz;
645 atomic_add(msgsz,&msg_bytes);
646 atomic_inc(&msg_hdrs);
660 static inline int convert_mode(long* msgtyp, int msgflg)
663 * find message of correct type.
664 * msgtyp = 0 => get first.
665 * msgtyp > 0 => get first message of matching type.
666 * msgtyp < 0 => get message with least type must be < abs(msgtype).
672 return SEARCH_LESSEQUAL;
674 if(msgflg & MSG_EXCEPT)
675 return SEARCH_NOTEQUAL;
679 asmlinkage long sys_msgrcv (int msqid, struct msgbuf __user *msgp, size_t msgsz,
680 long msgtyp, int msgflg)
682 struct msg_queue *msq;
686 if (msqid < 0 || (long) msgsz < 0)
688 mode = convert_mode(&msgtyp,msgflg);
690 msq = msg_lock(msqid);
694 msg = ERR_PTR(-EIDRM);
695 if (msg_checkid(msq,msqid))
699 struct msg_receiver msr_d;
700 struct list_head* tmp;
702 msg = ERR_PTR(-EACCES);
703 if (ipcperms (&msq->q_perm, S_IRUGO))
706 msg = ERR_PTR(-EAGAIN);
707 tmp = msq->q_messages.next;
708 while (tmp != &msq->q_messages) {
709 struct msg_msg *walk_msg;
710 walk_msg = list_entry(tmp,struct msg_msg,m_list);
711 if(testmsg(walk_msg,msgtyp,mode) &&
712 !security_msg_queue_msgrcv(msq, walk_msg, current, msgtyp, mode)) {
714 if(mode == SEARCH_LESSEQUAL && walk_msg->m_type != 1) {
716 msgtyp=walk_msg->m_type-1;
725 /* Found a suitable message. Unlink it from the queue. */
726 if ((msgsz < msg->m_ts) && !(msgflg & MSG_NOERROR)) {
727 msg = ERR_PTR(-E2BIG);
730 list_del(&msg->m_list);
732 msq->q_rtime = get_seconds();
733 msq->q_lrpid = current->tgid;
734 msq->q_cbytes -= msg->m_ts;
735 atomic_sub(msg->m_ts,&msg_bytes);
736 atomic_dec(&msg_hdrs);
737 ss_wakeup(&msq->q_senders,0);
741 /* No message waiting. Wait for a message */
742 if (msgflg & IPC_NOWAIT) {
743 msg = ERR_PTR(-ENOMSG);
746 list_add_tail(&msr_d.r_list,&msq->q_receivers);
747 msr_d.r_tsk = current;
748 msr_d.r_msgtype = msgtyp;
750 if(msgflg & MSG_NOERROR)
751 msr_d.r_maxsize = INT_MAX;
753 msr_d.r_maxsize = msgsz;
754 msr_d.r_msg = ERR_PTR(-EAGAIN);
755 current->state = TASK_INTERRUPTIBLE;
760 /* Lockless receive, part 1:
761 * Disable preemption. We don't hold a reference to the queue
762 * and getting a reference would defeat the idea of a lockless
763 * operation, thus the code relies on rcu to guarantee the
765 * Prior to destruction, expunge_all(-EIRDM) changes r_msg.
766 * Thus if r_msg is -EAGAIN, then the queue not yet destroyed.
767 * rcu_read_lock() prevents preemption between reading r_msg
768 * and the spin_lock() inside ipc_lock_by_ptr().
772 /* Lockless receive, part 2:
773 * Wait until pipelined_send or expunge_all are outside of
774 * wake_up_process(). There is a race with exit(), see
775 * ipc/mqueue.c for the details.
777 msg = (struct msg_msg*) msr_d.r_msg;
778 while (msg == NULL) {
780 msg = (struct msg_msg*) msr_d.r_msg;
783 /* Lockless receive, part 3:
784 * If there is a message or an error then accept it without
787 if(msg != ERR_PTR(-EAGAIN)) {
792 /* Lockless receive, part 3:
793 * Acquire the queue spinlock.
795 ipc_lock_by_ptr(&msq->q_perm);
798 /* Lockless receive, part 4:
799 * Repeat test after acquiring the spinlock.
801 msg = (struct msg_msg*)msr_d.r_msg;
802 if(msg != ERR_PTR(-EAGAIN))
805 list_del(&msr_d.r_list);
806 if (signal_pending(current)) {
807 msg = ERR_PTR(-ERESTARTNOHAND);
816 msgsz = (msgsz > msg->m_ts) ? msg->m_ts : msgsz;
817 if (put_user (msg->m_type, &msgp->mtype) ||
818 store_msg(msgp->mtext, msg, msgsz)) {
825 #ifdef CONFIG_PROC_FS
826 static int sysvipc_msg_proc_show(struct seq_file *s, void *it)
828 struct msg_queue *msq = it;
831 "%10d %10d %4o %10lu %10lu %5u %5u %5u %5u %5u %5u %10lu %10lu %10lu\n",