blob: a1cf543783feaf818a8b30baa6744a81895d4fa4 [file] [log] [blame]
lh9ed821d2023-04-07 01:36:19 -07001/*
2 * linux/ipc/msg.c
3 * Copyright (C) 1992 Krishna Balasubramanian
4 *
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
10 *
11 * /proc/sysvipc/msg support (c) 1999 Dragos Acostachioaie <dragos@iname.com>
12 *
13 * mostly rewritten, threaded and wake-one semantics added
14 * MSGMAX limit removed, sysctl's added
15 * (c) 1999 Manfred Spraul <manfred@colorfullife.com>
16 *
17 * support for audit of ipc object properties and permission changes
18 * Dustin Kirkland <dustin.kirkland@us.ibm.com>
19 *
20 * namespaces support
21 * OpenVZ, SWsoft Inc.
22 * Pavel Emelianov <xemul@openvz.org>
23 */
24
25#include <linux/capability.h>
26#include <linux/msg.h>
27#include <linux/spinlock.h>
28#include <linux/init.h>
29#include <linux/mm.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>
40
41#include <asm/current.h>
42#include <asm/uaccess.h>
43#include "util.h"
44
45/*
46 * one msg_receiver structure for each sleeping receiver:
47 */
48struct msg_receiver {
49 struct list_head r_list;
50 struct task_struct *r_tsk;
51
52 int r_mode;
53 long r_msgtype;
54 long r_maxsize;
55
56 struct msg_msg *volatile r_msg;
57};
58
59/* one msg_sender for each sleeping sender */
60struct msg_sender {
61 struct list_head list;
62 struct task_struct *tsk;
63};
64
65#define SEARCH_ANY 1
66#define SEARCH_EQUAL 2
67#define SEARCH_NOTEQUAL 3
68#define SEARCH_LESSEQUAL 4
69
70#define msg_ids(ns) ((ns)->ids[IPC_MSG_IDS])
71
72#define msg_unlock(msq) ipc_unlock(&(msq)->q_perm)
73
74static void freeque(struct ipc_namespace *, struct kern_ipc_perm *);
75static int newque(struct ipc_namespace *, struct ipc_params *);
76#ifdef CONFIG_PROC_FS
77static int sysvipc_msg_proc_show(struct seq_file *s, void *it);
78#endif
79
80/*
81 * Scale msgmni with the available lowmem size: the memory dedicated to msg
82 * queues should occupy at most 1/MSG_MEM_SCALE of lowmem.
83 * Also take into account the number of nsproxies created so far.
84 * This should be done staying within the (MSGMNI , IPCMNI/nr_ipc_ns) range.
85 */
86void recompute_msgmni(struct ipc_namespace *ns)
87{
88 struct sysinfo i;
89 unsigned long allowed;
90 int nb_ns;
91
92 si_meminfo(&i);
93 allowed = (((i.totalram - i.totalhigh) / MSG_MEM_SCALE) * i.mem_unit)
94 / MSGMNB;
95 nb_ns = atomic_read(&nr_ipc_ns);
96 allowed /= nb_ns;
97
98 if (allowed < MSGMNI) {
99 ns->msg_ctlmni = MSGMNI;
100 return;
101 }
102
103 if (allowed > IPCMNI / nb_ns) {
104 ns->msg_ctlmni = IPCMNI / nb_ns;
105 return;
106 }
107
108 ns->msg_ctlmni = allowed;
109}
110
111void msg_init_ns(struct ipc_namespace *ns)
112{
113 ns->msg_ctlmax = MSGMAX;
114 ns->msg_ctlmnb = MSGMNB;
115
116 recompute_msgmni(ns);
117
118 atomic_set(&ns->msg_bytes, 0);
119 atomic_set(&ns->msg_hdrs, 0);
120 ipc_init_ids(&ns->ids[IPC_MSG_IDS]);
121}
122
123#ifdef CONFIG_IPC_NS
124void msg_exit_ns(struct ipc_namespace *ns)
125{
126 free_ipcs(ns, &msg_ids(ns), freeque);
127 idr_destroy(&ns->ids[IPC_MSG_IDS].ipcs_idr);
128}
129#endif
130
131void __init msg_init(void)
132{
133 msg_init_ns(&init_ipc_ns);
134
135 printk(KERN_INFO "msgmni has been set to %d\n",
136 init_ipc_ns.msg_ctlmni);
137
138 if (IS_ENABLED(CONFIG_PROC_STRIPPED))
139 return 0;
140
141 ipc_init_proc_interface("sysvipc/msg",
142 " key msqid perms cbytes qnum lspid lrpid uid gid cuid cgid stime rtime ctime\n",
143 IPC_MSG_IDS, sysvipc_msg_proc_show);
144}
145
146/*
147 * msg_lock_(check_) routines are called in the paths where the rw_mutex
148 * is not held.
149 */
150static inline struct msg_queue *msg_lock(struct ipc_namespace *ns, int id)
151{
152 struct kern_ipc_perm *ipcp = ipc_lock(&msg_ids(ns), id);
153
154 if (IS_ERR(ipcp))
155 return (struct msg_queue *)ipcp;
156
157 return container_of(ipcp, struct msg_queue, q_perm);
158}
159
160static inline struct msg_queue *msg_lock_check(struct ipc_namespace *ns,
161 int id)
162{
163 struct kern_ipc_perm *ipcp = ipc_lock_check(&msg_ids(ns), id);
164
165 if (IS_ERR(ipcp))
166 return (struct msg_queue *)ipcp;
167
168 return container_of(ipcp, struct msg_queue, q_perm);
169}
170
171static inline void msg_rmid(struct ipc_namespace *ns, struct msg_queue *s)
172{
173 ipc_rmid(&msg_ids(ns), &s->q_perm);
174}
175
176/**
177 * newque - Create a new msg queue
178 * @ns: namespace
179 * @params: ptr to the structure that contains the key and msgflg
180 *
181 * Called with msg_ids.rw_mutex held (writer)
182 */
183static int newque(struct ipc_namespace *ns, struct ipc_params *params)
184{
185 struct msg_queue *msq;
186 int id, retval;
187 key_t key = params->key;
188 int msgflg = params->flg;
189
190 msq = ipc_rcu_alloc(sizeof(*msq));
191 if (!msq)
192 return -ENOMEM;
193
194 msq->q_perm.mode = msgflg & S_IRWXUGO;
195 msq->q_perm.key = key;
196
197 msq->q_perm.security = NULL;
198 retval = security_msg_queue_alloc(msq);
199 if (retval) {
200 ipc_rcu_putref(msq);
201 return retval;
202 }
203
204 /*
205 * ipc_addid() locks msq
206 */
207 id = ipc_addid(&msg_ids(ns), &msq->q_perm, ns->msg_ctlmni);
208 if (id < 0) {
209 security_msg_queue_free(msq);
210 ipc_rcu_putref(msq);
211 return id;
212 }
213
214 msq->q_stime = msq->q_rtime = 0;
215 msq->q_ctime = get_seconds();
216 msq->q_cbytes = msq->q_qnum = 0;
217 msq->q_qbytes = ns->msg_ctlmnb;
218 msq->q_lspid = msq->q_lrpid = 0;
219 INIT_LIST_HEAD(&msq->q_messages);
220 INIT_LIST_HEAD(&msq->q_receivers);
221 INIT_LIST_HEAD(&msq->q_senders);
222
223 msg_unlock(msq);
224
225 return msq->q_perm.id;
226}
227
228static inline void ss_add(struct msg_queue *msq, struct msg_sender *mss)
229{
230 mss->tsk = current;
231 current->state = TASK_INTERRUPTIBLE;
232 list_add_tail(&mss->list, &msq->q_senders);
233}
234
235static inline void ss_del(struct msg_sender *mss)
236{
237 if (mss->list.next != NULL)
238 list_del(&mss->list);
239}
240
241static void ss_wakeup(struct list_head *h, int kill)
242{
243 struct list_head *tmp;
244
245 tmp = h->next;
246 while (tmp != h) {
247 struct msg_sender *mss;
248
249 mss = list_entry(tmp, struct msg_sender, list);
250 tmp = tmp->next;
251 if (kill)
252 mss->list.next = NULL;
253 wake_up_process(mss->tsk);
254 }
255}
256
257static void expunge_all(struct msg_queue *msq, int res)
258{
259 struct list_head *tmp;
260
261 tmp = msq->q_receivers.next;
262 while (tmp != &msq->q_receivers) {
263 struct msg_receiver *msr;
264
265 /*
266 * Make sure that the wakeup doesnt preempt
267 * this CPU prematurely. (on PREEMPT_RT)
268 */
269 preempt_disable_rt();
270
271 msr = list_entry(tmp, struct msg_receiver, r_list);
272 tmp = tmp->next;
273 msr->r_msg = NULL;
274 wake_up_process(msr->r_tsk);
275 smp_mb();
276 msr->r_msg = ERR_PTR(res);
277
278 preempt_enable_rt();
279 }
280}
281
282/*
283 * freeque() wakes up waiters on the sender and receiver waiting queue,
284 * removes the message queue from message queue ID IDR, and cleans up all the
285 * messages associated with this queue.
286 *
287 * msg_ids.rw_mutex (writer) and the spinlock for this message queue are held
288 * before freeque() is called. msg_ids.rw_mutex remains locked on exit.
289 */
290static void freeque(struct ipc_namespace *ns, struct kern_ipc_perm *ipcp)
291{
292 struct list_head *tmp;
293 struct msg_queue *msq = container_of(ipcp, struct msg_queue, q_perm);
294
295 expunge_all(msq, -EIDRM);
296 ss_wakeup(&msq->q_senders, 1);
297 msg_rmid(ns, msq);
298 msg_unlock(msq);
299
300 tmp = msq->q_messages.next;
301 while (tmp != &msq->q_messages) {
302 struct msg_msg *msg = list_entry(tmp, struct msg_msg, m_list);
303
304 tmp = tmp->next;
305 atomic_dec(&ns->msg_hdrs);
306 free_msg(msg);
307 }
308 atomic_sub(msq->q_cbytes, &ns->msg_bytes);
309 security_msg_queue_free(msq);
310 ipc_lock_by_ptr(&msq->q_perm);
311 ipc_rcu_putref(msq);
312 ipc_unlock(&msq->q_perm);
313}
314
315/*
316 * Called with msg_ids.rw_mutex and ipcp locked.
317 */
318static inline int msg_security(struct kern_ipc_perm *ipcp, int msgflg)
319{
320 struct msg_queue *msq = container_of(ipcp, struct msg_queue, q_perm);
321
322 return security_msg_queue_associate(msq, msgflg);
323}
324
325SYSCALL_DEFINE2(msgget, key_t, key, int, msgflg)
326{
327 struct ipc_namespace *ns;
328 struct ipc_ops msg_ops;
329 struct ipc_params msg_params;
330
331 ns = current->nsproxy->ipc_ns;
332
333 msg_ops.getnew = newque;
334 msg_ops.associate = msg_security;
335 msg_ops.more_checks = NULL;
336
337 msg_params.key = key;
338 msg_params.flg = msgflg;
339
340 return ipcget(ns, &msg_ids(ns), &msg_ops, &msg_params);
341}
342
343static inline unsigned long
344copy_msqid_to_user(void __user *buf, struct msqid64_ds *in, int version)
345{
346 switch(version) {
347 case IPC_64:
348 return copy_to_user(buf, in, sizeof(*in));
349 case IPC_OLD:
350 {
351 struct msqid_ds out;
352
353 memset(&out, 0, sizeof(out));
354
355 ipc64_perm_to_ipc_perm(&in->msg_perm, &out.msg_perm);
356
357 out.msg_stime = in->msg_stime;
358 out.msg_rtime = in->msg_rtime;
359 out.msg_ctime = in->msg_ctime;
360
361 if (in->msg_cbytes > USHRT_MAX)
362 out.msg_cbytes = USHRT_MAX;
363 else
364 out.msg_cbytes = in->msg_cbytes;
365 out.msg_lcbytes = in->msg_cbytes;
366
367 if (in->msg_qnum > USHRT_MAX)
368 out.msg_qnum = USHRT_MAX;
369 else
370 out.msg_qnum = in->msg_qnum;
371
372 if (in->msg_qbytes > USHRT_MAX)
373 out.msg_qbytes = USHRT_MAX;
374 else
375 out.msg_qbytes = in->msg_qbytes;
376 out.msg_lqbytes = in->msg_qbytes;
377
378 out.msg_lspid = in->msg_lspid;
379 out.msg_lrpid = in->msg_lrpid;
380
381 return copy_to_user(buf, &out, sizeof(out));
382 }
383 default:
384 return -EINVAL;
385 }
386}
387
388static inline unsigned long
389copy_msqid_from_user(struct msqid64_ds *out, void __user *buf, int version)
390{
391 switch(version) {
392 case IPC_64:
393 if (copy_from_user(out, buf, sizeof(*out)))
394 return -EFAULT;
395 return 0;
396 case IPC_OLD:
397 {
398 struct msqid_ds tbuf_old;
399
400 if (copy_from_user(&tbuf_old, buf, sizeof(tbuf_old)))
401 return -EFAULT;
402
403 out->msg_perm.uid = tbuf_old.msg_perm.uid;
404 out->msg_perm.gid = tbuf_old.msg_perm.gid;
405 out->msg_perm.mode = tbuf_old.msg_perm.mode;
406
407 if (tbuf_old.msg_qbytes == 0)
408 out->msg_qbytes = tbuf_old.msg_lqbytes;
409 else
410 out->msg_qbytes = tbuf_old.msg_qbytes;
411
412 return 0;
413 }
414 default:
415 return -EINVAL;
416 }
417}
418
419/*
420 * This function handles some msgctl commands which require the rw_mutex
421 * to be held in write mode.
422 * NOTE: no locks must be held, the rw_mutex is taken inside this function.
423 */
424static int msgctl_down(struct ipc_namespace *ns, int msqid, int cmd,
425 struct msqid_ds __user *buf, int version)
426{
427 struct kern_ipc_perm *ipcp;
428 struct msqid64_ds uninitialized_var(msqid64);
429 struct msg_queue *msq;
430 int err;
431
432 if (cmd == IPC_SET) {
433 if (copy_msqid_from_user(&msqid64, buf, version))
434 return -EFAULT;
435 }
436
437 ipcp = ipcctl_pre_down(ns, &msg_ids(ns), msqid, cmd,
438 &msqid64.msg_perm, msqid64.msg_qbytes);
439 if (IS_ERR(ipcp))
440 return PTR_ERR(ipcp);
441
442 msq = container_of(ipcp, struct msg_queue, q_perm);
443
444 err = security_msg_queue_msgctl(msq, cmd);
445 if (err)
446 goto out_unlock;
447
448 switch (cmd) {
449 case IPC_RMID:
450 freeque(ns, ipcp);
451 goto out_up;
452 case IPC_SET:
453 if (msqid64.msg_qbytes > ns->msg_ctlmnb &&
454 !capable(CAP_SYS_RESOURCE)) {
455 err = -EPERM;
456 goto out_unlock;
457 }
458
459 msq->q_qbytes = msqid64.msg_qbytes;
460
461 ipc_update_perm(&msqid64.msg_perm, ipcp);
462 msq->q_ctime = get_seconds();
463 /* sleeping receivers might be excluded by
464 * stricter permissions.
465 */
466 expunge_all(msq, -EAGAIN);
467 /* sleeping senders might be able to send
468 * due to a larger queue size.
469 */
470 ss_wakeup(&msq->q_senders, 0);
471 break;
472 default:
473 err = -EINVAL;
474 }
475out_unlock:
476 msg_unlock(msq);
477out_up:
478 up_write(&msg_ids(ns).rw_mutex);
479 return err;
480}
481
482SYSCALL_DEFINE3(msgctl, int, msqid, int, cmd, struct msqid_ds __user *, buf)
483{
484 struct msg_queue *msq;
485 int err, version;
486 struct ipc_namespace *ns;
487
488 if (msqid < 0 || cmd < 0)
489 return -EINVAL;
490
491 version = ipc_parse_version(&cmd);
492 ns = current->nsproxy->ipc_ns;
493
494 switch (cmd) {
495 case IPC_INFO:
496 case MSG_INFO:
497 {
498 struct msginfo msginfo;
499 int max_id;
500
501 if (!buf)
502 return -EFAULT;
503 /*
504 * We must not return kernel stack data.
505 * due to padding, it's not enough
506 * to set all member fields.
507 */
508 err = security_msg_queue_msgctl(NULL, cmd);
509 if (err)
510 return err;
511
512 memset(&msginfo, 0, sizeof(msginfo));
513 msginfo.msgmni = ns->msg_ctlmni;
514 msginfo.msgmax = ns->msg_ctlmax;
515 msginfo.msgmnb = ns->msg_ctlmnb;
516 msginfo.msgssz = MSGSSZ;
517 msginfo.msgseg = MSGSEG;
518 down_read(&msg_ids(ns).rw_mutex);
519 if (cmd == MSG_INFO) {
520 msginfo.msgpool = msg_ids(ns).in_use;
521 msginfo.msgmap = atomic_read(&ns->msg_hdrs);
522 msginfo.msgtql = atomic_read(&ns->msg_bytes);
523 } else {
524 msginfo.msgmap = MSGMAP;
525 msginfo.msgpool = MSGPOOL;
526 msginfo.msgtql = MSGTQL;
527 }
528 max_id = ipc_get_maxid(&msg_ids(ns));
529 up_read(&msg_ids(ns).rw_mutex);
530 if (copy_to_user(buf, &msginfo, sizeof(struct msginfo)))
531 return -EFAULT;
532 return (max_id < 0) ? 0 : max_id;
533 }
534 case MSG_STAT: /* msqid is an index rather than a msg queue id */
535 case IPC_STAT:
536 {
537 struct msqid64_ds tbuf;
538 int success_return;
539
540 if (!buf)
541 return -EFAULT;
542
543 if (cmd == MSG_STAT) {
544 msq = msg_lock(ns, msqid);
545 if (IS_ERR(msq))
546 return PTR_ERR(msq);
547 success_return = msq->q_perm.id;
548 } else {
549 msq = msg_lock_check(ns, msqid);
550 if (IS_ERR(msq))
551 return PTR_ERR(msq);
552 success_return = 0;
553 }
554 err = -EACCES;
555 if (ipcperms(ns, &msq->q_perm, S_IRUGO))
556 goto out_unlock;
557
558 err = security_msg_queue_msgctl(msq, cmd);
559 if (err)
560 goto out_unlock;
561
562 memset(&tbuf, 0, sizeof(tbuf));
563
564 kernel_to_ipc64_perm(&msq->q_perm, &tbuf.msg_perm);
565 tbuf.msg_stime = msq->q_stime;
566 tbuf.msg_rtime = msq->q_rtime;
567 tbuf.msg_ctime = msq->q_ctime;
568 tbuf.msg_cbytes = msq->q_cbytes;
569 tbuf.msg_qnum = msq->q_qnum;
570 tbuf.msg_qbytes = msq->q_qbytes;
571 tbuf.msg_lspid = msq->q_lspid;
572 tbuf.msg_lrpid = msq->q_lrpid;
573 msg_unlock(msq);
574 if (copy_msqid_to_user(buf, &tbuf, version))
575 return -EFAULT;
576 return success_return;
577 }
578 case IPC_SET:
579 case IPC_RMID:
580 err = msgctl_down(ns, msqid, cmd, buf, version);
581 return err;
582 default:
583 return -EINVAL;
584 }
585
586out_unlock:
587 msg_unlock(msq);
588 return err;
589}
590
591static int testmsg(struct msg_msg *msg, long type, int mode)
592{
593 switch(mode)
594 {
595 case SEARCH_ANY:
596 return 1;
597 case SEARCH_LESSEQUAL:
598 if (msg->m_type <=type)
599 return 1;
600 break;
601 case SEARCH_EQUAL:
602 if (msg->m_type == type)
603 return 1;
604 break;
605 case SEARCH_NOTEQUAL:
606 if (msg->m_type != type)
607 return 1;
608 break;
609 }
610 return 0;
611}
612
613static inline int pipelined_send(struct msg_queue *msq, struct msg_msg *msg)
614{
615 struct list_head *tmp;
616
617 tmp = msq->q_receivers.next;
618 while (tmp != &msq->q_receivers) {
619 struct msg_receiver *msr;
620
621 msr = list_entry(tmp, struct msg_receiver, r_list);
622 tmp = tmp->next;
623 if (testmsg(msg, msr->r_msgtype, msr->r_mode) &&
624 !security_msg_queue_msgrcv(msq, msg, msr->r_tsk,
625 msr->r_msgtype, msr->r_mode)) {
626
627 /*
628 * Make sure that the wakeup doesnt preempt
629 * this CPU prematurely. (on PREEMPT_RT)
630 */
631 preempt_disable_rt();
632
633 list_del(&msr->r_list);
634 if (msr->r_maxsize < msg->m_ts) {
635 msr->r_msg = NULL;
636 wake_up_process(msr->r_tsk);
637 smp_mb();
638 msr->r_msg = ERR_PTR(-E2BIG);
639 } else {
640 msr->r_msg = NULL;
641 msq->q_lrpid = task_pid_vnr(msr->r_tsk);
642 msq->q_rtime = get_seconds();
643 wake_up_process(msr->r_tsk);
644 smp_mb();
645 msr->r_msg = msg;
646 preempt_enable_rt();
647
648 return 1;
649 }
650 preempt_enable_rt();
651 }
652 }
653 return 0;
654}
655
656long do_msgsnd(int msqid, long mtype, void __user *mtext,
657 size_t msgsz, int msgflg)
658{
659 struct msg_queue *msq;
660 struct msg_msg *msg;
661 int err;
662 struct ipc_namespace *ns;
663
664 ns = current->nsproxy->ipc_ns;
665
666 if (msgsz > ns->msg_ctlmax || (long) msgsz < 0 || msqid < 0)
667 return -EINVAL;
668 if (mtype < 1)
669 return -EINVAL;
670
671 msg = load_msg(mtext, msgsz);
672 if (IS_ERR(msg))
673 return PTR_ERR(msg);
674
675 msg->m_type = mtype;
676 msg->m_ts = msgsz;
677
678 msq = msg_lock_check(ns, msqid);
679 if (IS_ERR(msq)) {
680 err = PTR_ERR(msq);
681 goto out_free;
682 }
683
684 for (;;) {
685 struct msg_sender s;
686
687 err = -EACCES;
688 if (ipcperms(ns, &msq->q_perm, S_IWUGO))
689 goto out_unlock_free;
690
691 err = security_msg_queue_msgsnd(msq, msg, msgflg);
692 if (err)
693 goto out_unlock_free;
694
695 if (msgsz + msq->q_cbytes <= msq->q_qbytes &&
696 1 + msq->q_qnum <= msq->q_qbytes) {
697 break;
698 }
699
700 /* queue full, wait: */
701 if (msgflg & IPC_NOWAIT) {
702 err = -EAGAIN;
703 goto out_unlock_free;
704 }
705 ss_add(msq, &s);
706 ipc_rcu_getref(msq);
707 msg_unlock(msq);
708 schedule();
709
710 ipc_lock_by_ptr(&msq->q_perm);
711 ipc_rcu_putref(msq);
712 if (msq->q_perm.deleted) {
713 err = -EIDRM;
714 goto out_unlock_free;
715 }
716 ss_del(&s);
717
718 if (signal_pending(current)) {
719 err = -ERESTARTNOHAND;
720 goto out_unlock_free;
721 }
722 }
723
724 msq->q_lspid = task_tgid_vnr(current);
725 msq->q_stime = get_seconds();
726
727 if (!pipelined_send(msq, msg)) {
728 /* no one is waiting for this message, enqueue it */
729 list_add_tail(&msg->m_list, &msq->q_messages);
730 msq->q_cbytes += msgsz;
731 msq->q_qnum++;
732 atomic_add(msgsz, &ns->msg_bytes);
733 atomic_inc(&ns->msg_hdrs);
734 }
735
736 err = 0;
737 msg = NULL;
738
739out_unlock_free:
740 msg_unlock(msq);
741out_free:
742 if (msg != NULL)
743 free_msg(msg);
744 return err;
745}
746
747SYSCALL_DEFINE4(msgsnd, int, msqid, struct msgbuf __user *, msgp, size_t, msgsz,
748 int, msgflg)
749{
750 long mtype;
751
752 if (get_user(mtype, &msgp->mtype))
753 return -EFAULT;
754 return do_msgsnd(msqid, mtype, msgp->mtext, msgsz, msgflg);
755}
756
757static inline int convert_mode(long *msgtyp, int msgflg)
758{
759 /*
760 * find message of correct type.
761 * msgtyp = 0 => get first.
762 * msgtyp > 0 => get first message of matching type.
763 * msgtyp < 0 => get message with least type must be < abs(msgtype).
764 */
765 if (*msgtyp == 0)
766 return SEARCH_ANY;
767 if (*msgtyp < 0) {
768 *msgtyp = -*msgtyp;
769 return SEARCH_LESSEQUAL;
770 }
771 if (msgflg & MSG_EXCEPT)
772 return SEARCH_NOTEQUAL;
773 return SEARCH_EQUAL;
774}
775
776long do_msgrcv(int msqid, long *pmtype, void __user *mtext,
777 size_t msgsz, long msgtyp, int msgflg)
778{
779 struct msg_queue *msq;
780 struct msg_msg *msg;
781 int mode;
782 struct ipc_namespace *ns;
783
784 if (msqid < 0 || (long) msgsz < 0)
785 return -EINVAL;
786 mode = convert_mode(&msgtyp, msgflg);
787 ns = current->nsproxy->ipc_ns;
788
789 msq = msg_lock_check(ns, msqid);
790 if (IS_ERR(msq))
791 return PTR_ERR(msq);
792
793 for (;;) {
794 struct msg_receiver msr_d;
795 struct list_head *tmp;
796
797 msg = ERR_PTR(-EACCES);
798 if (ipcperms(ns, &msq->q_perm, S_IRUGO))
799 goto out_unlock;
800
801 msg = ERR_PTR(-EAGAIN);
802 tmp = msq->q_messages.next;
803 while (tmp != &msq->q_messages) {
804 struct msg_msg *walk_msg;
805
806 walk_msg = list_entry(tmp, struct msg_msg, m_list);
807 if (testmsg(walk_msg, msgtyp, mode) &&
808 !security_msg_queue_msgrcv(msq, walk_msg, current,
809 msgtyp, mode)) {
810
811 msg = walk_msg;
812 if (mode == SEARCH_LESSEQUAL &&
813 walk_msg->m_type != 1) {
814 msg = walk_msg;
815 msgtyp = walk_msg->m_type - 1;
816 } else {
817 msg = walk_msg;
818 break;
819 }
820 }
821 tmp = tmp->next;
822 }
823 if (!IS_ERR(msg)) {
824 /*
825 * Found a suitable message.
826 * Unlink it from the queue.
827 */
828 if ((msgsz < msg->m_ts) && !(msgflg & MSG_NOERROR)) {
829 msg = ERR_PTR(-E2BIG);
830 goto out_unlock;
831 }
832 list_del(&msg->m_list);
833 msq->q_qnum--;
834 msq->q_rtime = get_seconds();
835 msq->q_lrpid = task_tgid_vnr(current);
836 msq->q_cbytes -= msg->m_ts;
837 atomic_sub(msg->m_ts, &ns->msg_bytes);
838 atomic_dec(&ns->msg_hdrs);
839 ss_wakeup(&msq->q_senders, 0);
840 msg_unlock(msq);
841 break;
842 }
843 /* No message waiting. Wait for a message */
844 if (msgflg & IPC_NOWAIT) {
845 msg = ERR_PTR(-ENOMSG);
846 goto out_unlock;
847 }
848 list_add_tail(&msr_d.r_list, &msq->q_receivers);
849 msr_d.r_tsk = current;
850 msr_d.r_msgtype = msgtyp;
851 msr_d.r_mode = mode;
852 if (msgflg & MSG_NOERROR)
853 msr_d.r_maxsize = INT_MAX;
854 else
855 msr_d.r_maxsize = msgsz;
856 msr_d.r_msg = ERR_PTR(-EAGAIN);
857 current->state = TASK_INTERRUPTIBLE;
858 msg_unlock(msq);
859
860 schedule();
861
862 /* Lockless receive, part 1:
863 * Disable preemption. We don't hold a reference to the queue
864 * and getting a reference would defeat the idea of a lockless
865 * operation, thus the code relies on rcu to guarantee the
866 * existence of msq:
867 * Prior to destruction, expunge_all(-EIRDM) changes r_msg.
868 * Thus if r_msg is -EAGAIN, then the queue not yet destroyed.
869 * rcu_read_lock() prevents preemption between reading r_msg
870 * and the spin_lock() inside ipc_lock_by_ptr().
871 */
872 rcu_read_lock();
873
874 /* Lockless receive, part 2:
875 * Wait until pipelined_send or expunge_all are outside of
876 * wake_up_process(). There is a race with exit(), see
877 * ipc/mqueue.c for the details.
878 */
879 msg = (struct msg_msg*)msr_d.r_msg;
880 while (msg == NULL) {
881 cpu_relax();
882 msg = (struct msg_msg *)msr_d.r_msg;
883 }
884
885 /* Lockless receive, part 3:
886 * If there is a message or an error then accept it without
887 * locking.
888 */
889 if (msg != ERR_PTR(-EAGAIN)) {
890 rcu_read_unlock();
891 break;
892 }
893
894 /* Lockless receive, part 3:
895 * Acquire the queue spinlock.
896 */
897 ipc_lock_by_ptr(&msq->q_perm);
898 rcu_read_unlock();
899
900 /* Lockless receive, part 4:
901 * Repeat test after acquiring the spinlock.
902 */
903 msg = (struct msg_msg*)msr_d.r_msg;
904 if (msg != ERR_PTR(-EAGAIN))
905 goto out_unlock;
906
907 list_del(&msr_d.r_list);
908 if (signal_pending(current)) {
909 msg = ERR_PTR(-ERESTARTNOHAND);
910out_unlock:
911 msg_unlock(msq);
912 break;
913 }
914 }
915 if (IS_ERR(msg))
916 return PTR_ERR(msg);
917
918 msgsz = (msgsz > msg->m_ts) ? msg->m_ts : msgsz;
919 *pmtype = msg->m_type;
920 if (store_msg(mtext, msg, msgsz))
921 msgsz = -EFAULT;
922
923 free_msg(msg);
924
925 return msgsz;
926}
927
928SYSCALL_DEFINE5(msgrcv, int, msqid, struct msgbuf __user *, msgp, size_t, msgsz,
929 long, msgtyp, int, msgflg)
930{
931 long err, mtype;
932
933 err = do_msgrcv(msqid, &mtype, msgp->mtext, msgsz, msgtyp, msgflg);
934 if (err < 0)
935 goto out;
936
937 if (put_user(mtype, &msgp->mtype))
938 err = -EFAULT;
939out:
940 return err;
941}
942
943#ifdef CONFIG_PROC_FS
944static int sysvipc_msg_proc_show(struct seq_file *s, void *it)
945{
946 struct msg_queue *msq = it;
947
948 return seq_printf(s,
949 "%10d %10d %4o %10lu %10lu %5u %5u %5u %5u %5u %5u %10lu %10lu %10lu\n",
950 msq->q_perm.key,
951 msq->q_perm.id,
952 msq->q_perm.mode,
953 msq->q_cbytes,
954 msq->q_qnum,
955 msq->q_lspid,
956 msq->q_lrpid,
957 msq->q_perm.uid,
958 msq->q_perm.gid,
959 msq->q_perm.cuid,
960 msq->q_perm.cgid,
961 msq->q_stime,
962 msq->q_rtime,
963 msq->q_ctime);
964}
965#endif