msg.c revision 3d3653f9
1/*
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/wake_q.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 <linux/uaccess.h>
43#include "util.h"
44
45/* one msg_receiver structure for each sleeping receiver */
46struct msg_receiver {
47	struct list_head	r_list;
48	struct task_struct	*r_tsk;
49
50	int			r_mode;
51	long			r_msgtype;
52	long			r_maxsize;
53
54	struct msg_msg		*r_msg;
55};
56
57/* one msg_sender for each sleeping sender */
58struct msg_sender {
59	struct list_head	list;
60	struct task_struct	*tsk;
61	size_t                  msgsz;
62};
63
64#define SEARCH_ANY		1
65#define SEARCH_EQUAL		2
66#define SEARCH_NOTEQUAL		3
67#define SEARCH_LESSEQUAL	4
68#define SEARCH_NUMBER		5
69
70#define msg_ids(ns)	((ns)->ids[IPC_MSG_IDS])
71
72static inline struct msg_queue *msq_obtain_object(struct ipc_namespace *ns, int id)
73{
74	struct kern_ipc_perm *ipcp = ipc_obtain_object_idr(&msg_ids(ns), id);
75
76	if (IS_ERR(ipcp))
77		return ERR_CAST(ipcp);
78
79	return container_of(ipcp, struct msg_queue, q_perm);
80}
81
82static inline struct msg_queue *msq_obtain_object_check(struct ipc_namespace *ns,
83							int id)
84{
85	struct kern_ipc_perm *ipcp = ipc_obtain_object_check(&msg_ids(ns), id);
86
87	if (IS_ERR(ipcp))
88		return ERR_CAST(ipcp);
89
90	return container_of(ipcp, struct msg_queue, q_perm);
91}
92
93static inline void msg_rmid(struct ipc_namespace *ns, struct msg_queue *s)
94{
95	ipc_rmid(&msg_ids(ns), &s->q_perm);
96}
97
98static void __msg_free(struct msg_queue *msq)
99{
100	kvfree(msq);
101}
102
103static void msg_rcu_free(struct rcu_head *head)
104{
105	struct kern_ipc_perm *p = container_of(head, struct kern_ipc_perm, rcu);
106	struct msg_queue *msq = container_of(p, struct msg_queue, q_perm);
107
108	security_msg_queue_free(msq);
109	__msg_free(msq);
110}
111
112static struct msg_queue *msg_alloc(void)
113{
114	struct msg_queue *msq;
115
116	msq = kvmalloc(sizeof(*msq), GFP_KERNEL);
117	if (unlikely(!msq))
118		return NULL;
119
120	return msq;
121}
122
123/**
124 * newque - Create a new msg queue
125 * @ns: namespace
126 * @params: ptr to the structure that contains the key and msgflg
127 *
128 * Called with msg_ids.rwsem held (writer)
129 */
130static int newque(struct ipc_namespace *ns, struct ipc_params *params)
131{
132	struct msg_queue *msq;
133	int retval;
134	key_t key = params->key;
135	int msgflg = params->flg;
136
137	msq = msg_alloc();
138	if (!msq)
139		return -ENOMEM;
140
141	msq->q_perm.mode = msgflg & S_IRWXUGO;
142	msq->q_perm.key = key;
143
144	msq->q_perm.security = NULL;
145	retval = security_msg_queue_alloc(msq);
146	if (retval) {
147		__msg_free(msq);
148		return retval;
149	}
150
151	msq->q_stime = msq->q_rtime = 0;
152	msq->q_ctime = get_seconds();
153	msq->q_cbytes = msq->q_qnum = 0;
154	msq->q_qbytes = ns->msg_ctlmnb;
155	msq->q_lspid = msq->q_lrpid = 0;
156	INIT_LIST_HEAD(&msq->q_messages);
157	INIT_LIST_HEAD(&msq->q_receivers);
158	INIT_LIST_HEAD(&msq->q_senders);
159
160	/* ipc_addid() locks msq upon success. */
161	retval = ipc_addid(&msg_ids(ns), &msq->q_perm, ns->msg_ctlmni);
162	if (retval < 0) {
163		call_rcu(&msq->q_perm.rcu, msg_rcu_free);
164		return retval;
165	}
166
167	ipc_unlock_object(&msq->q_perm);
168	rcu_read_unlock();
169
170	return msq->q_perm.id;
171}
172
173static inline bool msg_fits_inqueue(struct msg_queue *msq, size_t msgsz)
174{
175	return msgsz + msq->q_cbytes <= msq->q_qbytes &&
176		1 + msq->q_qnum <= msq->q_qbytes;
177}
178
179static inline void ss_add(struct msg_queue *msq,
180			  struct msg_sender *mss, size_t msgsz)
181{
182	mss->tsk = current;
183	mss->msgsz = msgsz;
184	__set_current_state(TASK_INTERRUPTIBLE);
185	list_add_tail(&mss->list, &msq->q_senders);
186}
187
188static inline void ss_del(struct msg_sender *mss)
189{
190	if (mss->list.next)
191		list_del(&mss->list);
192}
193
194static void ss_wakeup(struct msg_queue *msq,
195		      struct wake_q_head *wake_q, bool kill)
196{
197	struct msg_sender *mss, *t;
198	struct task_struct *stop_tsk = NULL;
199	struct list_head *h = &msq->q_senders;
200
201	list_for_each_entry_safe(mss, t, h, list) {
202		if (kill)
203			mss->list.next = NULL;
204
205		/*
206		 * Stop at the first task we don't wakeup,
207		 * we've already iterated the original
208		 * sender queue.
209		 */
210		else if (stop_tsk == mss->tsk)
211			break;
212		/*
213		 * We are not in an EIDRM scenario here, therefore
214		 * verify that we really need to wakeup the task.
215		 * To maintain current semantics and wakeup order,
216		 * move the sender to the tail on behalf of the
217		 * blocked task.
218		 */
219		else if (!msg_fits_inqueue(msq, mss->msgsz)) {
220			if (!stop_tsk)
221				stop_tsk = mss->tsk;
222
223			list_move_tail(&mss->list, &msq->q_senders);
224			continue;
225		}
226
227		wake_q_add(wake_q, mss->tsk);
228	}
229}
230
231static void expunge_all(struct msg_queue *msq, int res,
232			struct wake_q_head *wake_q)
233{
234	struct msg_receiver *msr, *t;
235
236	list_for_each_entry_safe(msr, t, &msq->q_receivers, r_list) {
237		wake_q_add(wake_q, msr->r_tsk);
238		WRITE_ONCE(msr->r_msg, ERR_PTR(res));
239	}
240}
241
242/*
243 * freeque() wakes up waiters on the sender and receiver waiting queue,
244 * removes the message queue from message queue ID IDR, and cleans up all the
245 * messages associated with this queue.
246 *
247 * msg_ids.rwsem (writer) and the spinlock for this message queue are held
248 * before freeque() is called. msg_ids.rwsem remains locked on exit.
249 */
250static void freeque(struct ipc_namespace *ns, struct kern_ipc_perm *ipcp)
251{
252	struct msg_msg *msg, *t;
253	struct msg_queue *msq = container_of(ipcp, struct msg_queue, q_perm);
254	DEFINE_WAKE_Q(wake_q);
255
256	expunge_all(msq, -EIDRM, &wake_q);
257	ss_wakeup(msq, &wake_q, true);
258	msg_rmid(ns, msq);
259	ipc_unlock_object(&msq->q_perm);
260	wake_up_q(&wake_q);
261	rcu_read_unlock();
262
263	list_for_each_entry_safe(msg, t, &msq->q_messages, m_list) {
264		atomic_dec(&ns->msg_hdrs);
265		free_msg(msg);
266	}
267	atomic_sub(msq->q_cbytes, &ns->msg_bytes);
268	ipc_rcu_putref(&msq->q_perm, msg_rcu_free);
269}
270
271/*
272 * Called with msg_ids.rwsem and ipcp locked.
273 */
274static inline int msg_security(struct kern_ipc_perm *ipcp, int msgflg)
275{
276	struct msg_queue *msq = container_of(ipcp, struct msg_queue, q_perm);
277
278	return security_msg_queue_associate(msq, msgflg);
279}
280
281SYSCALL_DEFINE2(msgget, key_t, key, int, msgflg)
282{
283	struct ipc_namespace *ns;
284	static const struct ipc_ops msg_ops = {
285		.getnew = newque,
286		.associate = msg_security,
287	};
288	struct ipc_params msg_params;
289
290	ns = current->nsproxy->ipc_ns;
291
292	msg_params.key = key;
293	msg_params.flg = msgflg;
294
295	return ipcget(ns, &msg_ids(ns), &msg_ops, &msg_params);
296}
297
298static inline unsigned long
299copy_msqid_to_user(void __user *buf, struct msqid64_ds *in, int version)
300{
301	switch (version) {
302	case IPC_64:
303		return copy_to_user(buf, in, sizeof(*in));
304	case IPC_OLD:
305	{
306		struct msqid_ds out;
307
308		memset(&out, 0, sizeof(out));
309
310		ipc64_perm_to_ipc_perm(&in->msg_perm, &out.msg_perm);
311
312		out.msg_stime		= in->msg_stime;
313		out.msg_rtime		= in->msg_rtime;
314		out.msg_ctime		= in->msg_ctime;
315
316		if (in->msg_cbytes > USHRT_MAX)
317			out.msg_cbytes	= USHRT_MAX;
318		else
319			out.msg_cbytes	= in->msg_cbytes;
320		out.msg_lcbytes		= in->msg_cbytes;
321
322		if (in->msg_qnum > USHRT_MAX)
323			out.msg_qnum	= USHRT_MAX;
324		else
325			out.msg_qnum	= in->msg_qnum;
326
327		if (in->msg_qbytes > USHRT_MAX)
328			out.msg_qbytes	= USHRT_MAX;
329		else
330			out.msg_qbytes	= in->msg_qbytes;
331		out.msg_lqbytes		= in->msg_qbytes;
332
333		out.msg_lspid		= in->msg_lspid;
334		out.msg_lrpid		= in->msg_lrpid;
335
336		return copy_to_user(buf, &out, sizeof(out));
337	}
338	default:
339		return -EINVAL;
340	}
341}
342
343static inline unsigned long
344copy_msqid_from_user(struct msqid64_ds *out, void __user *buf, int version)
345{
346	switch (version) {
347	case IPC_64:
348		if (copy_from_user(out, buf, sizeof(*out)))
349			return -EFAULT;
350		return 0;
351	case IPC_OLD:
352	{
353		struct msqid_ds tbuf_old;
354
355		if (copy_from_user(&tbuf_old, buf, sizeof(tbuf_old)))
356			return -EFAULT;
357
358		out->msg_perm.uid	= tbuf_old.msg_perm.uid;
359		out->msg_perm.gid	= tbuf_old.msg_perm.gid;
360		out->msg_perm.mode	= tbuf_old.msg_perm.mode;
361
362		if (tbuf_old.msg_qbytes == 0)
363			out->msg_qbytes	= tbuf_old.msg_lqbytes;
364		else
365			out->msg_qbytes	= tbuf_old.msg_qbytes;
366
367		return 0;
368	}
369	default:
370		return -EINVAL;
371	}
372}
373
374/*
375 * This function handles some msgctl commands which require the rwsem
376 * to be held in write mode.
377 * NOTE: no locks must be held, the rwsem is taken inside this function.
378 */
379static int msgctl_down(struct ipc_namespace *ns, int msqid, int cmd,
380		       struct msqid_ds __user *buf, int version)
381{
382	struct kern_ipc_perm *ipcp;
383	struct msqid64_ds uninitialized_var(msqid64);
384	struct msg_queue *msq;
385	int err;
386
387	if (cmd == IPC_SET) {
388		if (copy_msqid_from_user(&msqid64, buf, version))
389			return -EFAULT;
390	}
391
392	down_write(&msg_ids(ns).rwsem);
393	rcu_read_lock();
394
395	ipcp = ipcctl_pre_down_nolock(ns, &msg_ids(ns), msqid, cmd,
396				      &msqid64.msg_perm, msqid64.msg_qbytes);
397	if (IS_ERR(ipcp)) {
398		err = PTR_ERR(ipcp);
399		goto out_unlock1;
400	}
401
402	msq = container_of(ipcp, struct msg_queue, q_perm);
403
404	err = security_msg_queue_msgctl(msq, cmd);
405	if (err)
406		goto out_unlock1;
407
408	switch (cmd) {
409	case IPC_RMID:
410		ipc_lock_object(&msq->q_perm);
411		/* freeque unlocks the ipc object and rcu */
412		freeque(ns, ipcp);
413		goto out_up;
414	case IPC_SET:
415	{
416		DEFINE_WAKE_Q(wake_q);
417
418		if (msqid64.msg_qbytes > ns->msg_ctlmnb &&
419		    !capable(CAP_SYS_RESOURCE)) {
420			err = -EPERM;
421			goto out_unlock1;
422		}
423
424		ipc_lock_object(&msq->q_perm);
425		err = ipc_update_perm(&msqid64.msg_perm, ipcp);
426		if (err)
427			goto out_unlock0;
428
429		msq->q_qbytes = msqid64.msg_qbytes;
430
431		msq->q_ctime = get_seconds();
432		/*
433		 * Sleeping receivers might be excluded by
434		 * stricter permissions.
435		 */
436		expunge_all(msq, -EAGAIN, &wake_q);
437		/*
438		 * Sleeping senders might be able to send
439		 * due to a larger queue size.
440		 */
441		ss_wakeup(msq, &wake_q, false);
442		ipc_unlock_object(&msq->q_perm);
443		wake_up_q(&wake_q);
444
445		goto out_unlock1;
446	}
447	default:
448		err = -EINVAL;
449		goto out_unlock1;
450	}
451
452out_unlock0:
453	ipc_unlock_object(&msq->q_perm);
454out_unlock1:
455	rcu_read_unlock();
456out_up:
457	up_write(&msg_ids(ns).rwsem);
458	return err;
459}
460
461static int msgctl_nolock(struct ipc_namespace *ns, int msqid,
462			 int cmd, int version, void __user *buf)
463{
464	int err;
465	struct msg_queue *msq;
466
467	switch (cmd) {
468	case IPC_INFO:
469	case MSG_INFO:
470	{
471		struct msginfo msginfo;
472		int max_id;
473
474		if (!buf)
475			return -EFAULT;
476
477		/*
478		 * We must not return kernel stack data.
479		 * due to padding, it's not enough
480		 * to set all member fields.
481		 */
482		err = security_msg_queue_msgctl(NULL, cmd);
483		if (err)
484			return err;
485
486		memset(&msginfo, 0, sizeof(msginfo));
487		msginfo.msgmni = ns->msg_ctlmni;
488		msginfo.msgmax = ns->msg_ctlmax;
489		msginfo.msgmnb = ns->msg_ctlmnb;
490		msginfo.msgssz = MSGSSZ;
491		msginfo.msgseg = MSGSEG;
492		down_read(&msg_ids(ns).rwsem);
493		if (cmd == MSG_INFO) {
494			msginfo.msgpool = msg_ids(ns).in_use;
495			msginfo.msgmap = atomic_read(&ns->msg_hdrs);
496			msginfo.msgtql = atomic_read(&ns->msg_bytes);
497		} else {
498			msginfo.msgmap = MSGMAP;
499			msginfo.msgpool = MSGPOOL;
500			msginfo.msgtql = MSGTQL;
501		}
502		max_id = ipc_get_maxid(&msg_ids(ns));
503		up_read(&msg_ids(ns).rwsem);
504		if (copy_to_user(buf, &msginfo, sizeof(struct msginfo)))
505			return -EFAULT;
506		return (max_id < 0) ? 0 : max_id;
507	}
508
509	case MSG_STAT:
510	case IPC_STAT:
511	{
512		struct msqid64_ds tbuf;
513		int success_return;
514
515		if (!buf)
516			return -EFAULT;
517
518		memset(&tbuf, 0, sizeof(tbuf));
519
520		rcu_read_lock();
521		if (cmd == MSG_STAT) {
522			msq = msq_obtain_object(ns, msqid);
523			if (IS_ERR(msq)) {
524				err = PTR_ERR(msq);
525				goto out_unlock;
526			}
527			success_return = msq->q_perm.id;
528		} else {
529			msq = msq_obtain_object_check(ns, msqid);
530			if (IS_ERR(msq)) {
531				err = PTR_ERR(msq);
532				goto out_unlock;
533			}
534			success_return = 0;
535		}
536
537		err = -EACCES;
538		if (ipcperms(ns, &msq->q_perm, S_IRUGO))
539			goto out_unlock;
540
541		err = security_msg_queue_msgctl(msq, cmd);
542		if (err)
543			goto out_unlock;
544
545		kernel_to_ipc64_perm(&msq->q_perm, &tbuf.msg_perm);
546		tbuf.msg_stime  = msq->q_stime;
547		tbuf.msg_rtime  = msq->q_rtime;
548		tbuf.msg_ctime  = msq->q_ctime;
549		tbuf.msg_cbytes = msq->q_cbytes;
550		tbuf.msg_qnum   = msq->q_qnum;
551		tbuf.msg_qbytes = msq->q_qbytes;
552		tbuf.msg_lspid  = msq->q_lspid;
553		tbuf.msg_lrpid  = msq->q_lrpid;
554		rcu_read_unlock();
555
556		if (copy_msqid_to_user(buf, &tbuf, version))
557			return -EFAULT;
558		return success_return;
559	}
560
561	default:
562		return -EINVAL;
563	}
564
565	return err;
566out_unlock:
567	rcu_read_unlock();
568	return err;
569}
570
571SYSCALL_DEFINE3(msgctl, int, msqid, int, cmd, struct msqid_ds __user *, buf)
572{
573	int version;
574	struct ipc_namespace *ns;
575
576	if (msqid < 0 || cmd < 0)
577		return -EINVAL;
578
579	version = ipc_parse_version(&cmd);
580	ns = current->nsproxy->ipc_ns;
581
582	switch (cmd) {
583	case IPC_INFO:
584	case MSG_INFO:
585	case MSG_STAT:	/* msqid is an index rather than a msg queue id */
586	case IPC_STAT:
587		return msgctl_nolock(ns, msqid, cmd, version, buf);
588	case IPC_SET:
589	case IPC_RMID:
590		return msgctl_down(ns, msqid, cmd, buf, version);
591	default:
592		return  -EINVAL;
593	}
594}
595
596static int testmsg(struct msg_msg *msg, long type, int mode)
597{
598	switch (mode) {
599	case SEARCH_ANY:
600	case SEARCH_NUMBER:
601		return 1;
602	case SEARCH_LESSEQUAL:
603		if (msg->m_type <= type)
604			return 1;
605		break;
606	case SEARCH_EQUAL:
607		if (msg->m_type == type)
608			return 1;
609		break;
610	case SEARCH_NOTEQUAL:
611		if (msg->m_type != type)
612			return 1;
613		break;
614	}
615	return 0;
616}
617
618static inline int pipelined_send(struct msg_queue *msq, struct msg_msg *msg,
619				 struct wake_q_head *wake_q)
620{
621	struct msg_receiver *msr, *t;
622
623	list_for_each_entry_safe(msr, t, &msq->q_receivers, r_list) {
624		if (testmsg(msg, msr->r_msgtype, msr->r_mode) &&
625		    !security_msg_queue_msgrcv(msq, msg, msr->r_tsk,
626					       msr->r_msgtype, msr->r_mode)) {
627
628			list_del(&msr->r_list);
629			if (msr->r_maxsize < msg->m_ts) {
630				wake_q_add(wake_q, msr->r_tsk);
631				WRITE_ONCE(msr->r_msg, ERR_PTR(-E2BIG));
632			} else {
633				msq->q_lrpid = task_pid_vnr(msr->r_tsk);
634				msq->q_rtime = get_seconds();
635
636				wake_q_add(wake_q, msr->r_tsk);
637				WRITE_ONCE(msr->r_msg, msg);
638				return 1;
639			}
640		}
641	}
642
643	return 0;
644}
645
646long do_msgsnd(int msqid, long mtype, void __user *mtext,
647		size_t msgsz, int msgflg)
648{
649	struct msg_queue *msq;
650	struct msg_msg *msg;
651	int err;
652	struct ipc_namespace *ns;
653	DEFINE_WAKE_Q(wake_q);
654
655	ns = current->nsproxy->ipc_ns;
656
657	if (msgsz > ns->msg_ctlmax || (long) msgsz < 0 || msqid < 0)
658		return -EINVAL;
659	if (mtype < 1)
660		return -EINVAL;
661
662	msg = load_msg(mtext, msgsz);
663	if (IS_ERR(msg))
664		return PTR_ERR(msg);
665
666	msg->m_type = mtype;
667	msg->m_ts = msgsz;
668
669	rcu_read_lock();
670	msq = msq_obtain_object_check(ns, msqid);
671	if (IS_ERR(msq)) {
672		err = PTR_ERR(msq);
673		goto out_unlock1;
674	}
675
676	ipc_lock_object(&msq->q_perm);
677
678	for (;;) {
679		struct msg_sender s;
680
681		err = -EACCES;
682		if (ipcperms(ns, &msq->q_perm, S_IWUGO))
683			goto out_unlock0;
684
685		/* raced with RMID? */
686		if (!ipc_valid_object(&msq->q_perm)) {
687			err = -EIDRM;
688			goto out_unlock0;
689		}
690
691		err = security_msg_queue_msgsnd(msq, msg, msgflg);
692		if (err)
693			goto out_unlock0;
694
695		if (msg_fits_inqueue(msq, msgsz))
696			break;
697
698		/* queue full, wait: */
699		if (msgflg & IPC_NOWAIT) {
700			err = -EAGAIN;
701			goto out_unlock0;
702		}
703
704		/* enqueue the sender and prepare to block */
705		ss_add(msq, &s, msgsz);
706
707		if (!ipc_rcu_getref(&msq->q_perm)) {
708			err = -EIDRM;
709			goto out_unlock0;
710		}
711
712		ipc_unlock_object(&msq->q_perm);
713		rcu_read_unlock();
714		schedule();
715
716		rcu_read_lock();
717		ipc_lock_object(&msq->q_perm);
718
719		ipc_rcu_putref(&msq->q_perm, msg_rcu_free);
720		/* raced with RMID? */
721		if (!ipc_valid_object(&msq->q_perm)) {
722			err = -EIDRM;
723			goto out_unlock0;
724		}
725		ss_del(&s);
726
727		if (signal_pending(current)) {
728			err = -ERESTARTNOHAND;
729			goto out_unlock0;
730		}
731
732	}
733
734	msq->q_lspid = task_tgid_vnr(current);
735	msq->q_stime = get_seconds();
736
737	if (!pipelined_send(msq, msg, &wake_q)) {
738		/* no one is waiting for this message, enqueue it */
739		list_add_tail(&msg->m_list, &msq->q_messages);
740		msq->q_cbytes += msgsz;
741		msq->q_qnum++;
742		atomic_add(msgsz, &ns->msg_bytes);
743		atomic_inc(&ns->msg_hdrs);
744	}
745
746	err = 0;
747	msg = NULL;
748
749out_unlock0:
750	ipc_unlock_object(&msq->q_perm);
751	wake_up_q(&wake_q);
752out_unlock1:
753	rcu_read_unlock();
754	if (msg != NULL)
755		free_msg(msg);
756	return err;
757}
758
759SYSCALL_DEFINE4(msgsnd, int, msqid, struct msgbuf __user *, msgp, size_t, msgsz,
760		int, msgflg)
761{
762	long mtype;
763
764	if (get_user(mtype, &msgp->mtype))
765		return -EFAULT;
766	return do_msgsnd(msqid, mtype, msgp->mtext, msgsz, msgflg);
767}
768
769static inline int convert_mode(long *msgtyp, int msgflg)
770{
771	if (msgflg & MSG_COPY)
772		return SEARCH_NUMBER;
773	/*
774	 *  find message of correct type.
775	 *  msgtyp = 0 => get first.
776	 *  msgtyp > 0 => get first message of matching type.
777	 *  msgtyp < 0 => get message with least type must be < abs(msgtype).
778	 */
779	if (*msgtyp == 0)
780		return SEARCH_ANY;
781	if (*msgtyp < 0) {
782		if (*msgtyp == LONG_MIN) /* -LONG_MIN is undefined */
783			*msgtyp = LONG_MAX;
784		else
785			*msgtyp = -*msgtyp;
786		return SEARCH_LESSEQUAL;
787	}
788	if (msgflg & MSG_EXCEPT)
789		return SEARCH_NOTEQUAL;
790	return SEARCH_EQUAL;
791}
792
793static long do_msg_fill(void __user *dest, struct msg_msg *msg, size_t bufsz)
794{
795	struct msgbuf __user *msgp = dest;
796	size_t msgsz;
797
798	if (put_user(msg->m_type, &msgp->mtype))
799		return -EFAULT;
800
801	msgsz = (bufsz > msg->m_ts) ? msg->m_ts : bufsz;
802	if (store_msg(msgp->mtext, msg, msgsz))
803		return -EFAULT;
804	return msgsz;
805}
806
807#ifdef CONFIG_CHECKPOINT_RESTORE
808/*
809 * This function creates new kernel message structure, large enough to store
810 * bufsz message bytes.
811 */
812static inline struct msg_msg *prepare_copy(void __user *buf, size_t bufsz)
813{
814	struct msg_msg *copy;
815
816	/*
817	 * Create dummy message to copy real message to.
818	 */
819	copy = load_msg(buf, bufsz);
820	if (!IS_ERR(copy))
821		copy->m_ts = bufsz;
822	return copy;
823}
824
825static inline void free_copy(struct msg_msg *copy)
826{
827	if (copy)
828		free_msg(copy);
829}
830#else
831static inline struct msg_msg *prepare_copy(void __user *buf, size_t bufsz)
832{
833	return ERR_PTR(-ENOSYS);
834}
835
836static inline void free_copy(struct msg_msg *copy)
837{
838}
839#endif
840
841static struct msg_msg *find_msg(struct msg_queue *msq, long *msgtyp, int mode)
842{
843	struct msg_msg *msg, *found = NULL;
844	long count = 0;
845
846	list_for_each_entry(msg, &msq->q_messages, m_list) {
847		if (testmsg(msg, *msgtyp, mode) &&
848		    !security_msg_queue_msgrcv(msq, msg, current,
849					       *msgtyp, mode)) {
850			if (mode == SEARCH_LESSEQUAL && msg->m_type != 1) {
851				*msgtyp = msg->m_type - 1;
852				found = msg;
853			} else if (mode == SEARCH_NUMBER) {
854				if (*msgtyp == count)
855					return msg;
856			} else
857				return msg;
858			count++;
859		}
860	}
861
862	return found ?: ERR_PTR(-EAGAIN);
863}
864
865long do_msgrcv(int msqid, void __user *buf, size_t bufsz, long msgtyp, int msgflg,
866	       long (*msg_handler)(void __user *, struct msg_msg *, size_t))
867{
868	int mode;
869	struct msg_queue *msq;
870	struct ipc_namespace *ns;
871	struct msg_msg *msg, *copy = NULL;
872	DEFINE_WAKE_Q(wake_q);
873
874	ns = current->nsproxy->ipc_ns;
875
876	if (msqid < 0 || (long) bufsz < 0)
877		return -EINVAL;
878
879	if (msgflg & MSG_COPY) {
880		if ((msgflg & MSG_EXCEPT) || !(msgflg & IPC_NOWAIT))
881			return -EINVAL;
882		copy = prepare_copy(buf, min_t(size_t, bufsz, ns->msg_ctlmax));
883		if (IS_ERR(copy))
884			return PTR_ERR(copy);
885	}
886	mode = convert_mode(&msgtyp, msgflg);
887
888	rcu_read_lock();
889	msq = msq_obtain_object_check(ns, msqid);
890	if (IS_ERR(msq)) {
891		rcu_read_unlock();
892		free_copy(copy);
893		return PTR_ERR(msq);
894	}
895
896	for (;;) {
897		struct msg_receiver msr_d;
898
899		msg = ERR_PTR(-EACCES);
900		if (ipcperms(ns, &msq->q_perm, S_IRUGO))
901			goto out_unlock1;
902
903		ipc_lock_object(&msq->q_perm);
904
905		/* raced with RMID? */
906		if (!ipc_valid_object(&msq->q_perm)) {
907			msg = ERR_PTR(-EIDRM);
908			goto out_unlock0;
909		}
910
911		msg = find_msg(msq, &msgtyp, mode);
912		if (!IS_ERR(msg)) {
913			/*
914			 * Found a suitable message.
915			 * Unlink it from the queue.
916			 */
917			if ((bufsz < msg->m_ts) && !(msgflg & MSG_NOERROR)) {
918				msg = ERR_PTR(-E2BIG);
919				goto out_unlock0;
920			}
921			/*
922			 * If we are copying, then do not unlink message and do
923			 * not update queue parameters.
924			 */
925			if (msgflg & MSG_COPY) {
926				msg = copy_msg(msg, copy);
927				goto out_unlock0;
928			}
929
930			list_del(&msg->m_list);
931			msq->q_qnum--;
932			msq->q_rtime = get_seconds();
933			msq->q_lrpid = task_tgid_vnr(current);
934			msq->q_cbytes -= msg->m_ts;
935			atomic_sub(msg->m_ts, &ns->msg_bytes);
936			atomic_dec(&ns->msg_hdrs);
937			ss_wakeup(msq, &wake_q, false);
938
939			goto out_unlock0;
940		}
941
942		/* No message waiting. Wait for a message */
943		if (msgflg & IPC_NOWAIT) {
944			msg = ERR_PTR(-ENOMSG);
945			goto out_unlock0;
946		}
947
948		list_add_tail(&msr_d.r_list, &msq->q_receivers);
949		msr_d.r_tsk = current;
950		msr_d.r_msgtype = msgtyp;
951		msr_d.r_mode = mode;
952		if (msgflg & MSG_NOERROR)
953			msr_d.r_maxsize = INT_MAX;
954		else
955			msr_d.r_maxsize = bufsz;
956		msr_d.r_msg = ERR_PTR(-EAGAIN);
957		__set_current_state(TASK_INTERRUPTIBLE);
958
959		ipc_unlock_object(&msq->q_perm);
960		rcu_read_unlock();
961		schedule();
962
963		/*
964		 * Lockless receive, part 1:
965		 * We don't hold a reference to the queue and getting a
966		 * reference would defeat the idea of a lockless operation,
967		 * thus the code relies on rcu to guarantee the existence of
968		 * msq:
969		 * Prior to destruction, expunge_all(-EIRDM) changes r_msg.
970		 * Thus if r_msg is -EAGAIN, then the queue not yet destroyed.
971		 */
972		rcu_read_lock();
973
974		/*
975		 * Lockless receive, part 2:
976		 * The work in pipelined_send() and expunge_all():
977		 * - Set pointer to message
978		 * - Queue the receiver task for later wakeup
979		 * - Wake up the process after the lock is dropped.
980		 *
981		 * Should the process wake up before this wakeup (due to a
982		 * signal) it will either see the message and continue ...
983		 */
984		msg = READ_ONCE(msr_d.r_msg);
985		if (msg != ERR_PTR(-EAGAIN))
986			goto out_unlock1;
987
988		 /*
989		  * ... or see -EAGAIN, acquire the lock to check the message
990		  * again.
991		  */
992		ipc_lock_object(&msq->q_perm);
993
994		msg = msr_d.r_msg;
995		if (msg != ERR_PTR(-EAGAIN))
996			goto out_unlock0;
997
998		list_del(&msr_d.r_list);
999		if (signal_pending(current)) {
1000			msg = ERR_PTR(-ERESTARTNOHAND);
1001			goto out_unlock0;
1002		}
1003
1004		ipc_unlock_object(&msq->q_perm);
1005	}
1006
1007out_unlock0:
1008	ipc_unlock_object(&msq->q_perm);
1009	wake_up_q(&wake_q);
1010out_unlock1:
1011	rcu_read_unlock();
1012	if (IS_ERR(msg)) {
1013		free_copy(copy);
1014		return PTR_ERR(msg);
1015	}
1016
1017	bufsz = msg_handler(buf, msg, bufsz);
1018	free_msg(msg);
1019
1020	return bufsz;
1021}
1022
1023SYSCALL_DEFINE5(msgrcv, int, msqid, struct msgbuf __user *, msgp, size_t, msgsz,
1024		long, msgtyp, int, msgflg)
1025{
1026	return do_msgrcv(msqid, msgp, msgsz, msgtyp, msgflg, do_msg_fill);
1027}
1028
1029
1030void msg_init_ns(struct ipc_namespace *ns)
1031{
1032	ns->msg_ctlmax = MSGMAX;
1033	ns->msg_ctlmnb = MSGMNB;
1034	ns->msg_ctlmni = MSGMNI;
1035
1036	atomic_set(&ns->msg_bytes, 0);
1037	atomic_set(&ns->msg_hdrs, 0);
1038	ipc_init_ids(&ns->ids[IPC_MSG_IDS]);
1039}
1040
1041#ifdef CONFIG_IPC_NS
1042void msg_exit_ns(struct ipc_namespace *ns)
1043{
1044	free_ipcs(ns, &msg_ids(ns), freeque);
1045	idr_destroy(&ns->ids[IPC_MSG_IDS].ipcs_idr);
1046}
1047#endif
1048
1049#ifdef CONFIG_PROC_FS
1050static int sysvipc_msg_proc_show(struct seq_file *s, void *it)
1051{
1052	struct user_namespace *user_ns = seq_user_ns(s);
1053	struct msg_queue *msq = it;
1054
1055	seq_printf(s,
1056		   "%10d %10d  %4o  %10lu %10lu %5u %5u %5u %5u %5u %5u %10lu %10lu %10lu\n",
1057		   msq->q_perm.key,
1058		   msq->q_perm.id,
1059		   msq->q_perm.mode,
1060		   msq->q_cbytes,
1061		   msq->q_qnum,
1062		   msq->q_lspid,
1063		   msq->q_lrpid,
1064		   from_kuid_munged(user_ns, msq->q_perm.uid),
1065		   from_kgid_munged(user_ns, msq->q_perm.gid),
1066		   from_kuid_munged(user_ns, msq->q_perm.cuid),
1067		   from_kgid_munged(user_ns, msq->q_perm.cgid),
1068		   msq->q_stime,
1069		   msq->q_rtime,
1070		   msq->q_ctime);
1071
1072	return 0;
1073}
1074#endif
1075
1076void __init msg_init(void)
1077{
1078	msg_init_ns(&init_ipc_ns);
1079
1080	ipc_init_proc_interface("sysvipc/msg",
1081				"       key      msqid perms      cbytes       qnum lspid lrpid   uid   gid  cuid  cgid      stime      rtime      ctime\n",
1082				IPC_MSG_IDS, sysvipc_msg_proc_show);
1083}
1084