blob: 0ed7dae7d4e8b4ba27e06a930362e898bcc94119 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * linux/ipc/msg.c
Ingo Molnar5a06a362006-07-30 03:04:11 -07003 * Copyright (C) 1992 Krishna Balasubramanian
Linus Torvalds1da177e2005-04-16 15:20:36 -07004 *
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
Christian Kujau624dffc2006-01-15 02:43:54 +010015 * (c) 1999 Manfred Spraul <manfred@colorfullife.com>
Steve Grubb073115d2006-04-02 17:07:33 -040016 *
17 * support for audit of ipc object properties and permission changes
18 * Dustin Kirkland <dustin.kirkland@us.ibm.com>
Kirill Korotaev1e786932006-10-02 02:18:21 -070019 *
20 * namespaces support
21 * OpenVZ, SWsoft Inc.
22 * Pavel Emelianov <xemul@openvz.org>
Linus Torvalds1da177e2005-04-16 15:20:36 -070023 */
24
Randy.Dunlapc59ede72006-01-11 12:17:46 -080025#include <linux/capability.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070026#include <linux/msg.h>
27#include <linux/spinlock.h>
28#include <linux/init.h>
Nadia Derbeyf7bf3df2008-04-29 01:00:39 -070029#include <linux/mm.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070030#include <linux/proc_fs.h>
31#include <linux/list.h>
32#include <linux/security.h>
Ingo Molnar84f001e2017-02-01 16:36:40 +010033#include <linux/sched/wake_q.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070034#include <linux/syscalls.h>
35#include <linux/audit.h>
Mike Waychison19b49462005-09-06 15:17:10 -070036#include <linux/seq_file.h>
Nadia Derbey3e148c72007-10-18 23:40:54 -070037#include <linux/rwsem.h>
Kirill Korotaev1e786932006-10-02 02:18:21 -070038#include <linux/nsproxy.h>
Pavel Emelyanovae5e1b22008-02-08 04:18:22 -080039#include <linux/ipc_namespace.h>
Ingo Molnar5f921ae2006-03-26 01:37:17 -080040
Linus Torvalds1da177e2005-04-16 15:20:36 -070041#include <asm/current.h>
Paul McQuade7153e402014-06-06 14:37:37 -070042#include <linux/uaccess.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070043#include "util.h"
44
Davidlohr Bueso4bb66572014-06-06 14:37:46 -070045/* one msg_receiver structure for each sleeping receiver */
Linus Torvalds1da177e2005-04-16 15:20:36 -070046struct msg_receiver {
Ingo Molnar5a06a362006-07-30 03:04:11 -070047 struct list_head r_list;
48 struct task_struct *r_tsk;
Linus Torvalds1da177e2005-04-16 15:20:36 -070049
Ingo Molnar5a06a362006-07-30 03:04:11 -070050 int r_mode;
51 long r_msgtype;
52 long r_maxsize;
Linus Torvalds1da177e2005-04-16 15:20:36 -070053
Sebastian Andrzej Siewioree516362016-10-11 13:54:53 -070054 struct msg_msg *r_msg;
Linus Torvalds1da177e2005-04-16 15:20:36 -070055};
56
57/* one msg_sender for each sleeping sender */
58struct msg_sender {
Ingo Molnar5a06a362006-07-30 03:04:11 -070059 struct list_head list;
60 struct task_struct *tsk;
Davidlohr Buesoed27f912016-10-11 13:55:02 -070061 size_t msgsz;
Linus Torvalds1da177e2005-04-16 15:20:36 -070062};
63
64#define SEARCH_ANY 1
65#define SEARCH_EQUAL 2
66#define SEARCH_NOTEQUAL 3
67#define SEARCH_LESSEQUAL 4
Peter Hurley8ac6ed52013-04-30 19:14:54 -070068#define SEARCH_NUMBER 5
Linus Torvalds1da177e2005-04-16 15:20:36 -070069
Pierre Peiffered2ddbf2008-02-08 04:18:57 -080070#define msg_ids(ns) ((ns)->ids[IPC_MSG_IDS])
Kirill Korotaev1e786932006-10-02 02:18:21 -070071
Davidlohr Buesoa5001a02013-07-08 16:01:15 -070072static inline struct msg_queue *msq_obtain_object(struct ipc_namespace *ns, int id)
73{
Davidlohr Bueso55b7ae52015-06-30 14:58:42 -070074 struct kern_ipc_perm *ipcp = ipc_obtain_object_idr(&msg_ids(ns), id);
Davidlohr Buesoa5001a02013-07-08 16:01:15 -070075
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
Nadia Derbey7ca7e562007-10-18 23:40:48 -070093static inline void msg_rmid(struct ipc_namespace *ns, struct msg_queue *s)
94{
95 ipc_rmid(&msg_ids(ns), &s->q_perm);
96}
97
Davidlohr Bueso53dad6d2013-09-23 17:04:45 -070098static void msg_rcu_free(struct rcu_head *head)
99{
Manfred Sprauldba4cdd2017-07-12 14:34:41 -0700100 struct kern_ipc_perm *p = container_of(head, struct kern_ipc_perm, rcu);
101 struct msg_queue *msq = container_of(p, struct msg_queue, q_perm);
Davidlohr Bueso53dad6d2013-09-23 17:04:45 -0700102
103 security_msg_queue_free(msq);
104 ipc_rcu_free(head);
105}
106
Nadia Derbeyf4566f02007-10-18 23:40:53 -0700107/**
108 * newque - Create a new msg queue
109 * @ns: namespace
110 * @params: ptr to the structure that contains the key and msgflg
111 *
Davidlohr Buesod9a605e2013-09-11 14:26:24 -0700112 * Called with msg_ids.rwsem held (writer)
Nadia Derbeyf4566f02007-10-18 23:40:53 -0700113 */
Nadia Derbey7748dbf2007-10-18 23:40:49 -0700114static int newque(struct ipc_namespace *ns, struct ipc_params *params)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700115{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700116 struct msg_queue *msq;
Ingo Molnar5a06a362006-07-30 03:04:11 -0700117 int id, retval;
Nadia Derbey7748dbf2007-10-18 23:40:49 -0700118 key_t key = params->key;
119 int msgflg = params->flg;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700120
Manfred Sprauldba4cdd2017-07-12 14:34:41 -0700121 BUILD_BUG_ON(offsetof(struct msg_queue, q_perm) != 0);
122
123 msq = container_of(ipc_rcu_alloc(sizeof(*msq)), struct msg_queue,
124 q_perm);
Ingo Molnar5a06a362006-07-30 03:04:11 -0700125 if (!msq)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700126 return -ENOMEM;
127
Ingo Molnar5a06a362006-07-30 03:04:11 -0700128 msq->q_perm.mode = msgflg & S_IRWXUGO;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700129 msq->q_perm.key = key;
130
131 msq->q_perm.security = NULL;
132 retval = security_msg_queue_alloc(msq);
133 if (retval) {
Manfred Sprauldba4cdd2017-07-12 14:34:41 -0700134 ipc_rcu_putref(&msq->q_perm, ipc_rcu_free);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700135 return retval;
136 }
137
Linus Torvalds1da177e2005-04-16 15:20:36 -0700138 msq->q_stime = msq->q_rtime = 0;
139 msq->q_ctime = get_seconds();
140 msq->q_cbytes = msq->q_qnum = 0;
Kirill Korotaev1e786932006-10-02 02:18:21 -0700141 msq->q_qbytes = ns->msg_ctlmnb;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700142 msq->q_lspid = msq->q_lrpid = 0;
143 INIT_LIST_HEAD(&msq->q_messages);
144 INIT_LIST_HEAD(&msq->q_receivers);
145 INIT_LIST_HEAD(&msq->q_senders);
Nadia Derbey7ca7e562007-10-18 23:40:48 -0700146
Linus Torvaldsb9a53222015-09-30 12:48:40 -0400147 /* ipc_addid() locks msq upon success. */
148 id = ipc_addid(&msg_ids(ns), &msq->q_perm, ns->msg_ctlmni);
149 if (id < 0) {
Manfred Sprauldba4cdd2017-07-12 14:34:41 -0700150 ipc_rcu_putref(&msq->q_perm, msg_rcu_free);
Linus Torvaldsb9a53222015-09-30 12:48:40 -0400151 return id;
152 }
153
Davidlohr Buesocf9d5d72013-07-08 16:01:11 -0700154 ipc_unlock_object(&msq->q_perm);
Davidlohr Buesodbfcd912013-07-08 16:01:09 -0700155 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700156
Nadia Derbey7ca7e562007-10-18 23:40:48 -0700157 return msq->q_perm.id;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700158}
159
Davidlohr Buesoed27f912016-10-11 13:55:02 -0700160static inline bool msg_fits_inqueue(struct msg_queue *msq, size_t msgsz)
161{
162 return msgsz + msq->q_cbytes <= msq->q_qbytes &&
163 1 + msq->q_qnum <= msq->q_qbytes;
164}
165
166static inline void ss_add(struct msg_queue *msq,
167 struct msg_sender *mss, size_t msgsz)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700168{
Ingo Molnar5a06a362006-07-30 03:04:11 -0700169 mss->tsk = current;
Davidlohr Buesoed27f912016-10-11 13:55:02 -0700170 mss->msgsz = msgsz;
Davidlohr Buesof75a2f32014-06-06 14:37:44 -0700171 __set_current_state(TASK_INTERRUPTIBLE);
Ingo Molnar5a06a362006-07-30 03:04:11 -0700172 list_add_tail(&mss->list, &msq->q_senders);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700173}
174
Ingo Molnar5a06a362006-07-30 03:04:11 -0700175static inline void ss_del(struct msg_sender *mss)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700176{
Davidlohr Buesoed27f912016-10-11 13:55:02 -0700177 if (mss->list.next)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700178 list_del(&mss->list);
179}
180
Davidlohr Buesoed27f912016-10-11 13:55:02 -0700181static void ss_wakeup(struct msg_queue *msq,
Davidlohr Buesod0d6a2a2016-10-11 13:54:59 -0700182 struct wake_q_head *wake_q, bool kill)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700183{
Nikola Pajkovsky41239fe2013-04-30 19:15:49 -0700184 struct msg_sender *mss, *t;
Davidlohr Buesoed27f912016-10-11 13:55:02 -0700185 struct task_struct *stop_tsk = NULL;
186 struct list_head *h = &msq->q_senders;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700187
Nikola Pajkovsky41239fe2013-04-30 19:15:49 -0700188 list_for_each_entry_safe(mss, t, h, list) {
Ingo Molnar5a06a362006-07-30 03:04:11 -0700189 if (kill)
190 mss->list.next = NULL;
Davidlohr Buesoed27f912016-10-11 13:55:02 -0700191
192 /*
193 * Stop at the first task we don't wakeup,
194 * we've already iterated the original
195 * sender queue.
196 */
197 else if (stop_tsk == mss->tsk)
198 break;
199 /*
200 * We are not in an EIDRM scenario here, therefore
201 * verify that we really need to wakeup the task.
202 * To maintain current semantics and wakeup order,
203 * move the sender to the tail on behalf of the
204 * blocked task.
205 */
206 else if (!msg_fits_inqueue(msq, mss->msgsz)) {
207 if (!stop_tsk)
208 stop_tsk = mss->tsk;
209
210 list_move_tail(&mss->list, &msq->q_senders);
211 continue;
212 }
213
Davidlohr Buesoe3658532016-10-11 13:54:56 -0700214 wake_q_add(wake_q, mss->tsk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700215 }
216}
217
Sebastian Andrzej Siewioree516362016-10-11 13:54:53 -0700218static void expunge_all(struct msg_queue *msq, int res,
219 struct wake_q_head *wake_q)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700220{
Nikola Pajkovsky41239fe2013-04-30 19:15:49 -0700221 struct msg_receiver *msr, *t;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700222
Nikola Pajkovsky41239fe2013-04-30 19:15:49 -0700223 list_for_each_entry_safe(msr, t, &msq->q_receivers, r_list) {
Sebastian Andrzej Siewioree516362016-10-11 13:54:53 -0700224 wake_q_add(wake_q, msr->r_tsk);
225 WRITE_ONCE(msr->r_msg, ERR_PTR(res));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700226 }
227}
Ingo Molnar5a06a362006-07-30 03:04:11 -0700228
229/*
230 * freeque() wakes up waiters on the sender and receiver waiting queue,
Nadia Derbeyf4566f02007-10-18 23:40:53 -0700231 * removes the message queue from message queue ID IDR, and cleans up all the
232 * messages associated with this queue.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700233 *
Davidlohr Buesod9a605e2013-09-11 14:26:24 -0700234 * msg_ids.rwsem (writer) and the spinlock for this message queue are held
235 * before freeque() is called. msg_ids.rwsem remains locked on exit.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700236 */
Pierre Peiffer01b8b072008-02-08 04:18:57 -0800237static void freeque(struct ipc_namespace *ns, struct kern_ipc_perm *ipcp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700238{
Nikola Pajkovsky41239fe2013-04-30 19:15:49 -0700239 struct msg_msg *msg, *t;
Pierre Peiffer01b8b072008-02-08 04:18:57 -0800240 struct msg_queue *msq = container_of(ipcp, struct msg_queue, q_perm);
Waiman Long194a6b52016-11-17 11:46:38 -0500241 DEFINE_WAKE_Q(wake_q);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700242
Sebastian Andrzej Siewioree516362016-10-11 13:54:53 -0700243 expunge_all(msq, -EIDRM, &wake_q);
Davidlohr Buesoed27f912016-10-11 13:55:02 -0700244 ss_wakeup(msq, &wake_q, true);
Nadia Derbey7ca7e562007-10-18 23:40:48 -0700245 msg_rmid(ns, msq);
Davidlohr Bueso47187872013-09-11 14:26:25 -0700246 ipc_unlock_object(&msq->q_perm);
Sebastian Andrzej Siewioree516362016-10-11 13:54:53 -0700247 wake_up_q(&wake_q);
Davidlohr Bueso47187872013-09-11 14:26:25 -0700248 rcu_read_unlock();
Ingo Molnar5a06a362006-07-30 03:04:11 -0700249
Nikola Pajkovsky41239fe2013-04-30 19:15:49 -0700250 list_for_each_entry_safe(msg, t, &msq->q_messages, m_list) {
Kirill Korotaev3ac88a42007-10-18 23:40:56 -0700251 atomic_dec(&ns->msg_hdrs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700252 free_msg(msg);
253 }
Kirill Korotaev3ac88a42007-10-18 23:40:56 -0700254 atomic_sub(msq->q_cbytes, &ns->msg_bytes);
Manfred Sprauldba4cdd2017-07-12 14:34:41 -0700255 ipc_rcu_putref(&msq->q_perm, msg_rcu_free);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700256}
257
Nadia Derbeyf4566f02007-10-18 23:40:53 -0700258/*
Davidlohr Buesod9a605e2013-09-11 14:26:24 -0700259 * Called with msg_ids.rwsem and ipcp locked.
Nadia Derbeyf4566f02007-10-18 23:40:53 -0700260 */
Nadia Derbey03f02c72007-10-18 23:40:51 -0700261static inline int msg_security(struct kern_ipc_perm *ipcp, int msgflg)
Nadia Derbey7748dbf2007-10-18 23:40:49 -0700262{
Nadia Derbey03f02c72007-10-18 23:40:51 -0700263 struct msg_queue *msq = container_of(ipcp, struct msg_queue, q_perm);
264
265 return security_msg_queue_associate(msq, msgflg);
Nadia Derbey7748dbf2007-10-18 23:40:49 -0700266}
267
Heiko Carstense48fbb62009-01-14 14:14:26 +0100268SYSCALL_DEFINE2(msgget, key_t, key, int, msgflg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700269{
Kirill Korotaev1e786932006-10-02 02:18:21 -0700270 struct ipc_namespace *ns;
Mathias Krauseeb66ec42014-06-06 14:37:36 -0700271 static const struct ipc_ops msg_ops = {
272 .getnew = newque,
273 .associate = msg_security,
274 };
Nadia Derbey7748dbf2007-10-18 23:40:49 -0700275 struct ipc_params msg_params;
Kirill Korotaev1e786932006-10-02 02:18:21 -0700276
277 ns = current->nsproxy->ipc_ns;
Ingo Molnar5a06a362006-07-30 03:04:11 -0700278
Nadia Derbey7748dbf2007-10-18 23:40:49 -0700279 msg_params.key = key;
280 msg_params.flg = msgflg;
Nadia Derbey7ca7e562007-10-18 23:40:48 -0700281
Nadia Derbey7748dbf2007-10-18 23:40:49 -0700282 return ipcget(ns, &msg_ids(ns), &msg_ops, &msg_params);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700283}
284
Ingo Molnar5a06a362006-07-30 03:04:11 -0700285static inline unsigned long
286copy_msqid_to_user(void __user *buf, struct msqid64_ds *in, int version)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700287{
Manfred Spraul239521f2014-01-27 17:07:04 -0800288 switch (version) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700289 case IPC_64:
Ingo Molnar5a06a362006-07-30 03:04:11 -0700290 return copy_to_user(buf, in, sizeof(*in));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700291 case IPC_OLD:
Ingo Molnar5a06a362006-07-30 03:04:11 -0700292 {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700293 struct msqid_ds out;
294
Ingo Molnar5a06a362006-07-30 03:04:11 -0700295 memset(&out, 0, sizeof(out));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700296
297 ipc64_perm_to_ipc_perm(&in->msg_perm, &out.msg_perm);
298
299 out.msg_stime = in->msg_stime;
300 out.msg_rtime = in->msg_rtime;
301 out.msg_ctime = in->msg_ctime;
302
Alexey Dobriyan4be929b2010-05-24 14:33:03 -0700303 if (in->msg_cbytes > USHRT_MAX)
304 out.msg_cbytes = USHRT_MAX;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700305 else
306 out.msg_cbytes = in->msg_cbytes;
307 out.msg_lcbytes = in->msg_cbytes;
308
Alexey Dobriyan4be929b2010-05-24 14:33:03 -0700309 if (in->msg_qnum > USHRT_MAX)
310 out.msg_qnum = USHRT_MAX;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700311 else
312 out.msg_qnum = in->msg_qnum;
313
Alexey Dobriyan4be929b2010-05-24 14:33:03 -0700314 if (in->msg_qbytes > USHRT_MAX)
315 out.msg_qbytes = USHRT_MAX;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700316 else
317 out.msg_qbytes = in->msg_qbytes;
318 out.msg_lqbytes = in->msg_qbytes;
319
320 out.msg_lspid = in->msg_lspid;
321 out.msg_lrpid = in->msg_lrpid;
322
Ingo Molnar5a06a362006-07-30 03:04:11 -0700323 return copy_to_user(buf, &out, sizeof(out));
324 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700325 default:
326 return -EINVAL;
327 }
328}
329
Ingo Molnar5a06a362006-07-30 03:04:11 -0700330static inline unsigned long
Pierre Peiffer016d7132008-04-29 01:00:50 -0700331copy_msqid_from_user(struct msqid64_ds *out, void __user *buf, int version)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700332{
Manfred Spraul239521f2014-01-27 17:07:04 -0800333 switch (version) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700334 case IPC_64:
Pierre Peiffer016d7132008-04-29 01:00:50 -0700335 if (copy_from_user(out, buf, sizeof(*out)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700336 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700337 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700338 case IPC_OLD:
Ingo Molnar5a06a362006-07-30 03:04:11 -0700339 {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700340 struct msqid_ds tbuf_old;
341
Ingo Molnar5a06a362006-07-30 03:04:11 -0700342 if (copy_from_user(&tbuf_old, buf, sizeof(tbuf_old)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700343 return -EFAULT;
344
Manfred Spraul239521f2014-01-27 17:07:04 -0800345 out->msg_perm.uid = tbuf_old.msg_perm.uid;
346 out->msg_perm.gid = tbuf_old.msg_perm.gid;
347 out->msg_perm.mode = tbuf_old.msg_perm.mode;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700348
Ingo Molnar5a06a362006-07-30 03:04:11 -0700349 if (tbuf_old.msg_qbytes == 0)
Pierre Peiffer016d7132008-04-29 01:00:50 -0700350 out->msg_qbytes = tbuf_old.msg_lqbytes;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700351 else
Pierre Peiffer016d7132008-04-29 01:00:50 -0700352 out->msg_qbytes = tbuf_old.msg_qbytes;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700353
354 return 0;
Ingo Molnar5a06a362006-07-30 03:04:11 -0700355 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700356 default:
357 return -EINVAL;
358 }
359}
360
Pierre Peiffera0d092f2008-04-29 01:00:48 -0700361/*
Davidlohr Buesod9a605e2013-09-11 14:26:24 -0700362 * This function handles some msgctl commands which require the rwsem
Pierre Peiffera0d092f2008-04-29 01:00:48 -0700363 * to be held in write mode.
Davidlohr Buesod9a605e2013-09-11 14:26:24 -0700364 * NOTE: no locks must be held, the rwsem is taken inside this function.
Pierre Peiffera0d092f2008-04-29 01:00:48 -0700365 */
366static int msgctl_down(struct ipc_namespace *ns, int msqid, int cmd,
367 struct msqid_ds __user *buf, int version)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700368{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700369 struct kern_ipc_perm *ipcp;
Felipe Contrerasf1970c482009-10-19 01:54:29 +0300370 struct msqid64_ds uninitialized_var(msqid64);
Pierre Peiffera0d092f2008-04-29 01:00:48 -0700371 struct msg_queue *msq;
372 int err;
373
374 if (cmd == IPC_SET) {
Pierre Peiffer016d7132008-04-29 01:00:50 -0700375 if (copy_msqid_from_user(&msqid64, buf, version))
Pierre Peiffera0d092f2008-04-29 01:00:48 -0700376 return -EFAULT;
377 }
378
Davidlohr Buesod9a605e2013-09-11 14:26:24 -0700379 down_write(&msg_ids(ns).rwsem);
Davidlohr Bueso7b4cc5d2013-07-08 16:01:12 -0700380 rcu_read_lock();
381
Davidlohr Bueso15724ec2013-07-08 16:01:13 -0700382 ipcp = ipcctl_pre_down_nolock(ns, &msg_ids(ns), msqid, cmd,
383 &msqid64.msg_perm, msqid64.msg_qbytes);
Davidlohr Bueso7b4cc5d2013-07-08 16:01:12 -0700384 if (IS_ERR(ipcp)) {
385 err = PTR_ERR(ipcp);
Davidlohr Bueso7b4cc5d2013-07-08 16:01:12 -0700386 goto out_unlock1;
387 }
Pierre Peiffera0d092f2008-04-29 01:00:48 -0700388
Pierre Peiffera5f75e72008-04-29 01:00:54 -0700389 msq = container_of(ipcp, struct msg_queue, q_perm);
Pierre Peiffera0d092f2008-04-29 01:00:48 -0700390
391 err = security_msg_queue_msgctl(msq, cmd);
392 if (err)
Davidlohr Bueso15724ec2013-07-08 16:01:13 -0700393 goto out_unlock1;
Pierre Peiffera0d092f2008-04-29 01:00:48 -0700394
395 switch (cmd) {
396 case IPC_RMID:
Davidlohr Bueso15724ec2013-07-08 16:01:13 -0700397 ipc_lock_object(&msq->q_perm);
Davidlohr Bueso7b4cc5d2013-07-08 16:01:12 -0700398 /* freeque unlocks the ipc object and rcu */
Pierre Peiffera0d092f2008-04-29 01:00:48 -0700399 freeque(ns, ipcp);
400 goto out_up;
401 case IPC_SET:
Davidlohr Buesoe3658532016-10-11 13:54:56 -0700402 {
Waiman Long194a6b52016-11-17 11:46:38 -0500403 DEFINE_WAKE_Q(wake_q);
Davidlohr Buesoe3658532016-10-11 13:54:56 -0700404
Pierre Peiffer016d7132008-04-29 01:00:50 -0700405 if (msqid64.msg_qbytes > ns->msg_ctlmnb &&
Pierre Peiffera0d092f2008-04-29 01:00:48 -0700406 !capable(CAP_SYS_RESOURCE)) {
407 err = -EPERM;
Davidlohr Bueso15724ec2013-07-08 16:01:13 -0700408 goto out_unlock1;
Pierre Peiffera0d092f2008-04-29 01:00:48 -0700409 }
410
Davidlohr Bueso15724ec2013-07-08 16:01:13 -0700411 ipc_lock_object(&msq->q_perm);
Eric W. Biederman1efdb692012-02-07 16:54:11 -0800412 err = ipc_update_perm(&msqid64.msg_perm, ipcp);
413 if (err)
Davidlohr Bueso7b4cc5d2013-07-08 16:01:12 -0700414 goto out_unlock0;
Eric W. Biederman1efdb692012-02-07 16:54:11 -0800415
Pierre Peiffer016d7132008-04-29 01:00:50 -0700416 msq->q_qbytes = msqid64.msg_qbytes;
Pierre Peiffera0d092f2008-04-29 01:00:48 -0700417
Pierre Peiffera0d092f2008-04-29 01:00:48 -0700418 msq->q_ctime = get_seconds();
Davidlohr Buesoe3658532016-10-11 13:54:56 -0700419 /*
420 * Sleeping receivers might be excluded by
Pierre Peiffera0d092f2008-04-29 01:00:48 -0700421 * stricter permissions.
422 */
Sebastian Andrzej Siewioree516362016-10-11 13:54:53 -0700423 expunge_all(msq, -EAGAIN, &wake_q);
Davidlohr Buesoe3658532016-10-11 13:54:56 -0700424 /*
425 * Sleeping senders might be able to send
Pierre Peiffera0d092f2008-04-29 01:00:48 -0700426 * due to a larger queue size.
427 */
Davidlohr Buesoed27f912016-10-11 13:55:02 -0700428 ss_wakeup(msq, &wake_q, false);
Davidlohr Buesoe3658532016-10-11 13:54:56 -0700429 ipc_unlock_object(&msq->q_perm);
430 wake_up_q(&wake_q);
431
432 goto out_unlock1;
433 }
Pierre Peiffera0d092f2008-04-29 01:00:48 -0700434 default:
435 err = -EINVAL;
Davidlohr Bueso15724ec2013-07-08 16:01:13 -0700436 goto out_unlock1;
Pierre Peiffera0d092f2008-04-29 01:00:48 -0700437 }
Davidlohr Bueso7b4cc5d2013-07-08 16:01:12 -0700438
439out_unlock0:
440 ipc_unlock_object(&msq->q_perm);
441out_unlock1:
442 rcu_read_unlock();
Pierre Peiffera0d092f2008-04-29 01:00:48 -0700443out_up:
Davidlohr Buesod9a605e2013-09-11 14:26:24 -0700444 up_write(&msg_ids(ns).rwsem);
Pierre Peiffera0d092f2008-04-29 01:00:48 -0700445 return err;
446}
447
Davidlohr Bueso2cafed32013-07-08 16:01:14 -0700448static int msgctl_nolock(struct ipc_namespace *ns, int msqid,
449 int cmd, int version, void __user *buf)
Pierre Peiffera0d092f2008-04-29 01:00:48 -0700450{
Davidlohr Bueso2cafed32013-07-08 16:01:14 -0700451 int err;
Ingo Molnar5a06a362006-07-30 03:04:11 -0700452 struct msg_queue *msq;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700453
454 switch (cmd) {
Ingo Molnar5a06a362006-07-30 03:04:11 -0700455 case IPC_INFO:
456 case MSG_INFO:
457 {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700458 struct msginfo msginfo;
459 int max_id;
Ingo Molnar5a06a362006-07-30 03:04:11 -0700460
Linus Torvalds1da177e2005-04-16 15:20:36 -0700461 if (!buf)
462 return -EFAULT;
Davidlohr Bueso2cafed32013-07-08 16:01:14 -0700463
Ingo Molnar5a06a362006-07-30 03:04:11 -0700464 /*
465 * We must not return kernel stack data.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700466 * due to padding, it's not enough
467 * to set all member fields.
468 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700469 err = security_msg_queue_msgctl(NULL, cmd);
470 if (err)
471 return err;
472
Ingo Molnar5a06a362006-07-30 03:04:11 -0700473 memset(&msginfo, 0, sizeof(msginfo));
Kirill Korotaev1e786932006-10-02 02:18:21 -0700474 msginfo.msgmni = ns->msg_ctlmni;
475 msginfo.msgmax = ns->msg_ctlmax;
476 msginfo.msgmnb = ns->msg_ctlmnb;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700477 msginfo.msgssz = MSGSSZ;
478 msginfo.msgseg = MSGSEG;
Davidlohr Buesod9a605e2013-09-11 14:26:24 -0700479 down_read(&msg_ids(ns).rwsem);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700480 if (cmd == MSG_INFO) {
Kirill Korotaev1e786932006-10-02 02:18:21 -0700481 msginfo.msgpool = msg_ids(ns).in_use;
Kirill Korotaev3ac88a42007-10-18 23:40:56 -0700482 msginfo.msgmap = atomic_read(&ns->msg_hdrs);
483 msginfo.msgtql = atomic_read(&ns->msg_bytes);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700484 } else {
485 msginfo.msgmap = MSGMAP;
486 msginfo.msgpool = MSGPOOL;
487 msginfo.msgtql = MSGTQL;
488 }
Nadia Derbey7ca7e562007-10-18 23:40:48 -0700489 max_id = ipc_get_maxid(&msg_ids(ns));
Davidlohr Buesod9a605e2013-09-11 14:26:24 -0700490 up_read(&msg_ids(ns).rwsem);
Ingo Molnar5a06a362006-07-30 03:04:11 -0700491 if (copy_to_user(buf, &msginfo, sizeof(struct msginfo)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700492 return -EFAULT;
Ingo Molnar5a06a362006-07-30 03:04:11 -0700493 return (max_id < 0) ? 0 : max_id;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700494 }
Davidlohr Bueso2cafed32013-07-08 16:01:14 -0700495
496 case MSG_STAT:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700497 case IPC_STAT:
498 {
499 struct msqid64_ds tbuf;
500 int success_return;
Ingo Molnar5a06a362006-07-30 03:04:11 -0700501
Linus Torvalds1da177e2005-04-16 15:20:36 -0700502 if (!buf)
503 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700504
Davidlohr Buesoac0ba202013-07-08 16:01:16 -0700505 memset(&tbuf, 0, sizeof(tbuf));
506
507 rcu_read_lock();
Ingo Molnar5a06a362006-07-30 03:04:11 -0700508 if (cmd == MSG_STAT) {
Davidlohr Buesoac0ba202013-07-08 16:01:16 -0700509 msq = msq_obtain_object(ns, msqid);
510 if (IS_ERR(msq)) {
511 err = PTR_ERR(msq);
512 goto out_unlock;
513 }
Nadia Derbey7ca7e562007-10-18 23:40:48 -0700514 success_return = msq->q_perm.id;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700515 } else {
Davidlohr Buesoac0ba202013-07-08 16:01:16 -0700516 msq = msq_obtain_object_check(ns, msqid);
517 if (IS_ERR(msq)) {
518 err = PTR_ERR(msq);
519 goto out_unlock;
520 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700521 success_return = 0;
522 }
Davidlohr Buesoac0ba202013-07-08 16:01:16 -0700523
Linus Torvalds1da177e2005-04-16 15:20:36 -0700524 err = -EACCES;
Serge E. Hallynb0e77592011-03-23 16:43:24 -0700525 if (ipcperms(ns, &msq->q_perm, S_IRUGO))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700526 goto out_unlock;
527
528 err = security_msg_queue_msgctl(msq, cmd);
529 if (err)
530 goto out_unlock;
531
532 kernel_to_ipc64_perm(&msq->q_perm, &tbuf.msg_perm);
533 tbuf.msg_stime = msq->q_stime;
534 tbuf.msg_rtime = msq->q_rtime;
535 tbuf.msg_ctime = msq->q_ctime;
536 tbuf.msg_cbytes = msq->q_cbytes;
537 tbuf.msg_qnum = msq->q_qnum;
538 tbuf.msg_qbytes = msq->q_qbytes;
539 tbuf.msg_lspid = msq->q_lspid;
540 tbuf.msg_lrpid = msq->q_lrpid;
Davidlohr Buesoac0ba202013-07-08 16:01:16 -0700541 rcu_read_unlock();
542
Linus Torvalds1da177e2005-04-16 15:20:36 -0700543 if (copy_msqid_to_user(buf, &tbuf, version))
544 return -EFAULT;
545 return success_return;
546 }
Davidlohr Bueso2cafed32013-07-08 16:01:14 -0700547
Linus Torvalds1da177e2005-04-16 15:20:36 -0700548 default:
Davidlohr Bueso2cafed32013-07-08 16:01:14 -0700549 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700550 }
551
Davidlohr Bueso2cafed32013-07-08 16:01:14 -0700552 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700553out_unlock:
Davidlohr Buesoac0ba202013-07-08 16:01:16 -0700554 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700555 return err;
556}
557
Davidlohr Bueso2cafed32013-07-08 16:01:14 -0700558SYSCALL_DEFINE3(msgctl, int, msqid, int, cmd, struct msqid_ds __user *, buf)
559{
560 int version;
561 struct ipc_namespace *ns;
562
563 if (msqid < 0 || cmd < 0)
564 return -EINVAL;
565
566 version = ipc_parse_version(&cmd);
567 ns = current->nsproxy->ipc_ns;
568
569 switch (cmd) {
570 case IPC_INFO:
571 case MSG_INFO:
572 case MSG_STAT: /* msqid is an index rather than a msg queue id */
573 case IPC_STAT:
574 return msgctl_nolock(ns, msqid, cmd, version, buf);
575 case IPC_SET:
576 case IPC_RMID:
577 return msgctl_down(ns, msqid, cmd, buf, version);
578 default:
579 return -EINVAL;
580 }
581}
582
Ingo Molnar5a06a362006-07-30 03:04:11 -0700583static int testmsg(struct msg_msg *msg, long type, int mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700584{
Paul McQuade46c0a8c2014-06-06 14:37:37 -0700585 switch (mode) {
586 case SEARCH_ANY:
587 case SEARCH_NUMBER:
588 return 1;
589 case SEARCH_LESSEQUAL:
590 if (msg->m_type <= type)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700591 return 1;
Paul McQuade46c0a8c2014-06-06 14:37:37 -0700592 break;
593 case SEARCH_EQUAL:
594 if (msg->m_type == type)
595 return 1;
596 break;
597 case SEARCH_NOTEQUAL:
598 if (msg->m_type != type)
599 return 1;
600 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700601 }
602 return 0;
603}
604
Sebastian Andrzej Siewioree516362016-10-11 13:54:53 -0700605static inline int pipelined_send(struct msg_queue *msq, struct msg_msg *msg,
606 struct wake_q_head *wake_q)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700607{
Nikola Pajkovsky41239fe2013-04-30 19:15:49 -0700608 struct msg_receiver *msr, *t;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700609
Nikola Pajkovsky41239fe2013-04-30 19:15:49 -0700610 list_for_each_entry_safe(msr, t, &msq->q_receivers, r_list) {
Ingo Molnar5a06a362006-07-30 03:04:11 -0700611 if (testmsg(msg, msr->r_msgtype, msr->r_mode) &&
612 !security_msg_queue_msgrcv(msq, msg, msr->r_tsk,
613 msr->r_msgtype, msr->r_mode)) {
614
Linus Torvalds1da177e2005-04-16 15:20:36 -0700615 list_del(&msr->r_list);
Ingo Molnar5a06a362006-07-30 03:04:11 -0700616 if (msr->r_maxsize < msg->m_ts) {
Sebastian Andrzej Siewioree516362016-10-11 13:54:53 -0700617 wake_q_add(wake_q, msr->r_tsk);
618 WRITE_ONCE(msr->r_msg, ERR_PTR(-E2BIG));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700619 } else {
Pavel Emelyanovb4888932007-10-18 23:40:14 -0700620 msq->q_lrpid = task_pid_vnr(msr->r_tsk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700621 msq->q_rtime = get_seconds();
Ingo Molnar5a06a362006-07-30 03:04:11 -0700622
Sebastian Andrzej Siewioree516362016-10-11 13:54:53 -0700623 wake_q_add(wake_q, msr->r_tsk);
624 WRITE_ONCE(msr->r_msg, msg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700625 return 1;
626 }
627 }
628 }
Davidlohr Buesoffa571d2014-01-27 17:07:10 -0800629
Linus Torvalds1da177e2005-04-16 15:20:36 -0700630 return 0;
631}
632
suzuki651971c2006-12-06 20:37:48 -0800633long do_msgsnd(int msqid, long mtype, void __user *mtext,
634 size_t msgsz, int msgflg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700635{
636 struct msg_queue *msq;
637 struct msg_msg *msg;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700638 int err;
Kirill Korotaev1e786932006-10-02 02:18:21 -0700639 struct ipc_namespace *ns;
Waiman Long194a6b52016-11-17 11:46:38 -0500640 DEFINE_WAKE_Q(wake_q);
Ingo Molnar5a06a362006-07-30 03:04:11 -0700641
Kirill Korotaev1e786932006-10-02 02:18:21 -0700642 ns = current->nsproxy->ipc_ns;
643
644 if (msgsz > ns->msg_ctlmax || (long) msgsz < 0 || msqid < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700645 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700646 if (mtype < 1)
647 return -EINVAL;
648
suzuki651971c2006-12-06 20:37:48 -0800649 msg = load_msg(mtext, msgsz);
Ingo Molnar5a06a362006-07-30 03:04:11 -0700650 if (IS_ERR(msg))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700651 return PTR_ERR(msg);
652
653 msg->m_type = mtype;
654 msg->m_ts = msgsz;
655
Davidlohr Bueso3dd1f782013-07-08 16:01:17 -0700656 rcu_read_lock();
657 msq = msq_obtain_object_check(ns, msqid);
Nadia Derbey023a5352007-10-18 23:40:51 -0700658 if (IS_ERR(msq)) {
659 err = PTR_ERR(msq);
Davidlohr Bueso3dd1f782013-07-08 16:01:17 -0700660 goto out_unlock1;
Nadia Derbey023a5352007-10-18 23:40:51 -0700661 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700662
Manfred Spraulbebcb922013-09-03 16:00:08 +0200663 ipc_lock_object(&msq->q_perm);
664
Linus Torvalds1da177e2005-04-16 15:20:36 -0700665 for (;;) {
666 struct msg_sender s;
667
Ingo Molnar5a06a362006-07-30 03:04:11 -0700668 err = -EACCES;
Serge E. Hallynb0e77592011-03-23 16:43:24 -0700669 if (ipcperms(ns, &msq->q_perm, S_IWUGO))
Manfred Spraulbebcb922013-09-03 16:00:08 +0200670 goto out_unlock0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700671
Davidlohr Bueso4271b052013-09-30 13:45:26 -0700672 /* raced with RMID? */
Rafael Aquini0f3d2b02014-01-27 17:07:01 -0800673 if (!ipc_valid_object(&msq->q_perm)) {
Davidlohr Bueso4271b052013-09-30 13:45:26 -0700674 err = -EIDRM;
675 goto out_unlock0;
676 }
677
Linus Torvalds1da177e2005-04-16 15:20:36 -0700678 err = security_msg_queue_msgsnd(msq, msg, msgflg);
679 if (err)
Manfred Spraulbebcb922013-09-03 16:00:08 +0200680 goto out_unlock0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700681
Davidlohr Buesoed27f912016-10-11 13:55:02 -0700682 if (msg_fits_inqueue(msq, msgsz))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700683 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700684
685 /* queue full, wait: */
Ingo Molnar5a06a362006-07-30 03:04:11 -0700686 if (msgflg & IPC_NOWAIT) {
687 err = -EAGAIN;
Manfred Spraulbebcb922013-09-03 16:00:08 +0200688 goto out_unlock0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700689 }
Davidlohr Bueso3dd1f782013-07-08 16:01:17 -0700690
Davidlohr Buesoffa571d2014-01-27 17:07:10 -0800691 /* enqueue the sender and prepare to block */
Davidlohr Buesoed27f912016-10-11 13:55:02 -0700692 ss_add(msq, &s, msgsz);
Rik van Riel6062a8d2013-04-30 19:15:44 -0700693
Manfred Sprauldba4cdd2017-07-12 14:34:41 -0700694 if (!ipc_rcu_getref(&msq->q_perm)) {
Rik van Riel6062a8d2013-04-30 19:15:44 -0700695 err = -EIDRM;
Davidlohr Bueso3dd1f782013-07-08 16:01:17 -0700696 goto out_unlock0;
Rik van Riel6062a8d2013-04-30 19:15:44 -0700697 }
698
Davidlohr Bueso3dd1f782013-07-08 16:01:17 -0700699 ipc_unlock_object(&msq->q_perm);
700 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700701 schedule();
702
Davidlohr Bueso3dd1f782013-07-08 16:01:17 -0700703 rcu_read_lock();
704 ipc_lock_object(&msq->q_perm);
705
Manfred Sprauldba4cdd2017-07-12 14:34:41 -0700706 ipc_rcu_putref(&msq->q_perm, msg_rcu_free);
Rafael Aquini0f3d2b02014-01-27 17:07:01 -0800707 /* raced with RMID? */
708 if (!ipc_valid_object(&msq->q_perm)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700709 err = -EIDRM;
Davidlohr Bueso3dd1f782013-07-08 16:01:17 -0700710 goto out_unlock0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700711 }
712 ss_del(&s);
Ingo Molnar5a06a362006-07-30 03:04:11 -0700713
Linus Torvalds1da177e2005-04-16 15:20:36 -0700714 if (signal_pending(current)) {
Ingo Molnar5a06a362006-07-30 03:04:11 -0700715 err = -ERESTARTNOHAND;
Davidlohr Bueso3dd1f782013-07-08 16:01:17 -0700716 goto out_unlock0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700717 }
Davidlohr Bueso3dd1f782013-07-08 16:01:17 -0700718
Linus Torvalds1da177e2005-04-16 15:20:36 -0700719 }
Davidlohr Buesoed27f912016-10-11 13:55:02 -0700720
Pavel Emelyanovb4888932007-10-18 23:40:14 -0700721 msq->q_lspid = task_tgid_vnr(current);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700722 msq->q_stime = get_seconds();
723
Sebastian Andrzej Siewioree516362016-10-11 13:54:53 -0700724 if (!pipelined_send(msq, msg, &wake_q)) {
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300725 /* no one is waiting for this message, enqueue it */
Ingo Molnar5a06a362006-07-30 03:04:11 -0700726 list_add_tail(&msg->m_list, &msq->q_messages);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700727 msq->q_cbytes += msgsz;
728 msq->q_qnum++;
Kirill Korotaev3ac88a42007-10-18 23:40:56 -0700729 atomic_add(msgsz, &ns->msg_bytes);
730 atomic_inc(&ns->msg_hdrs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700731 }
Ingo Molnar5a06a362006-07-30 03:04:11 -0700732
Linus Torvalds1da177e2005-04-16 15:20:36 -0700733 err = 0;
734 msg = NULL;
735
Davidlohr Bueso3dd1f782013-07-08 16:01:17 -0700736out_unlock0:
737 ipc_unlock_object(&msq->q_perm);
Sebastian Andrzej Siewioree516362016-10-11 13:54:53 -0700738 wake_up_q(&wake_q);
Davidlohr Bueso3dd1f782013-07-08 16:01:17 -0700739out_unlock1:
740 rcu_read_unlock();
Ingo Molnar5a06a362006-07-30 03:04:11 -0700741 if (msg != NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700742 free_msg(msg);
743 return err;
744}
745
Heiko Carstense48fbb62009-01-14 14:14:26 +0100746SYSCALL_DEFINE4(msgsnd, int, msqid, struct msgbuf __user *, msgp, size_t, msgsz,
747 int, msgflg)
suzuki651971c2006-12-06 20:37:48 -0800748{
749 long mtype;
750
751 if (get_user(mtype, &msgp->mtype))
752 return -EFAULT;
753 return do_msgsnd(msqid, mtype, msgp->mtext, msgsz, msgflg);
754}
755
Ingo Molnar5a06a362006-07-30 03:04:11 -0700756static inline int convert_mode(long *msgtyp, int msgflg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700757{
Peter Hurley8ac6ed52013-04-30 19:14:54 -0700758 if (msgflg & MSG_COPY)
759 return SEARCH_NUMBER;
Ingo Molnar5a06a362006-07-30 03:04:11 -0700760 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700761 * find message of correct type.
762 * msgtyp = 0 => get first.
763 * msgtyp > 0 => get first message of matching type.
Ingo Molnar5a06a362006-07-30 03:04:11 -0700764 * msgtyp < 0 => get message with least type must be < abs(msgtype).
Linus Torvalds1da177e2005-04-16 15:20:36 -0700765 */
Ingo Molnar5a06a362006-07-30 03:04:11 -0700766 if (*msgtyp == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700767 return SEARCH_ANY;
Ingo Molnar5a06a362006-07-30 03:04:11 -0700768 if (*msgtyp < 0) {
Jiri Slaby99989832016-12-14 15:06:07 -0800769 if (*msgtyp == LONG_MIN) /* -LONG_MIN is undefined */
770 *msgtyp = LONG_MAX;
771 else
772 *msgtyp = -*msgtyp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700773 return SEARCH_LESSEQUAL;
774 }
Ingo Molnar5a06a362006-07-30 03:04:11 -0700775 if (msgflg & MSG_EXCEPT)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700776 return SEARCH_NOTEQUAL;
777 return SEARCH_EQUAL;
778}
779
Stanislav Kinsburskyf9dd87f2013-01-04 15:34:52 -0800780static long do_msg_fill(void __user *dest, struct msg_msg *msg, size_t bufsz)
781{
782 struct msgbuf __user *msgp = dest;
783 size_t msgsz;
784
785 if (put_user(msg->m_type, &msgp->mtype))
786 return -EFAULT;
787
788 msgsz = (bufsz > msg->m_ts) ? msg->m_ts : bufsz;
789 if (store_msg(msgp->mtext, msg, msgsz))
790 return -EFAULT;
791 return msgsz;
792}
793
Stanislav Kinsbursky4a674f32013-01-04 15:34:55 -0800794#ifdef CONFIG_CHECKPOINT_RESTORE
Stanislav Kinsbursky3fcfe782013-01-04 15:35:03 -0800795/*
796 * This function creates new kernel message structure, large enough to store
797 * bufsz message bytes.
798 */
Peter Hurley8ac6ed52013-04-30 19:14:54 -0700799static inline struct msg_msg *prepare_copy(void __user *buf, size_t bufsz)
Stanislav Kinsbursky4a674f32013-01-04 15:34:55 -0800800{
801 struct msg_msg *copy;
802
Stanislav Kinsbursky4a674f32013-01-04 15:34:55 -0800803 /*
804 * Create dummy message to copy real message to.
805 */
806 copy = load_msg(buf, bufsz);
807 if (!IS_ERR(copy))
808 copy->m_ts = bufsz;
809 return copy;
810}
811
Stanislav Kinsbursky85398aa2013-01-04 15:34:58 -0800812static inline void free_copy(struct msg_msg *copy)
Stanislav Kinsbursky4a674f32013-01-04 15:34:55 -0800813{
Stanislav Kinsbursky85398aa2013-01-04 15:34:58 -0800814 if (copy)
Stanislav Kinsbursky4a674f32013-01-04 15:34:55 -0800815 free_msg(copy);
816}
817#else
Peter Hurley8ac6ed52013-04-30 19:14:54 -0700818static inline struct msg_msg *prepare_copy(void __user *buf, size_t bufsz)
Stanislav Kinsburskyb30efe22013-01-04 15:35:00 -0800819{
820 return ERR_PTR(-ENOSYS);
821}
822
Stanislav Kinsbursky85398aa2013-01-04 15:34:58 -0800823static inline void free_copy(struct msg_msg *copy)
824{
825}
Stanislav Kinsbursky4a674f32013-01-04 15:34:55 -0800826#endif
827
Peter Hurleydaaf74c2013-04-30 19:15:04 -0700828static struct msg_msg *find_msg(struct msg_queue *msq, long *msgtyp, int mode)
829{
Svenning Sørensen368ae532013-08-28 16:35:17 -0700830 struct msg_msg *msg, *found = NULL;
Peter Hurleydaaf74c2013-04-30 19:15:04 -0700831 long count = 0;
832
833 list_for_each_entry(msg, &msq->q_messages, m_list) {
834 if (testmsg(msg, *msgtyp, mode) &&
835 !security_msg_queue_msgrcv(msq, msg, current,
836 *msgtyp, mode)) {
837 if (mode == SEARCH_LESSEQUAL && msg->m_type != 1) {
838 *msgtyp = msg->m_type - 1;
Svenning Sørensen368ae532013-08-28 16:35:17 -0700839 found = msg;
Peter Hurleydaaf74c2013-04-30 19:15:04 -0700840 } else if (mode == SEARCH_NUMBER) {
841 if (*msgtyp == count)
842 return msg;
843 } else
844 return msg;
845 count++;
846 }
847 }
848
Svenning Sørensen368ae532013-08-28 16:35:17 -0700849 return found ?: ERR_PTR(-EAGAIN);
Peter Hurleydaaf74c2013-04-30 19:15:04 -0700850}
851
Davidlohr Bueso41a0d522013-07-08 16:01:18 -0700852long do_msgrcv(int msqid, void __user *buf, size_t bufsz, long msgtyp, int msgflg,
Stanislav Kinsburskyf9dd87f2013-01-04 15:34:52 -0800853 long (*msg_handler)(void __user *, struct msg_msg *, size_t))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700854{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700855 int mode;
Davidlohr Bueso41a0d522013-07-08 16:01:18 -0700856 struct msg_queue *msq;
Kirill Korotaev1e786932006-10-02 02:18:21 -0700857 struct ipc_namespace *ns;
Davidlohr Bueso41a0d522013-07-08 16:01:18 -0700858 struct msg_msg *msg, *copy = NULL;
Waiman Long194a6b52016-11-17 11:46:38 -0500859 DEFINE_WAKE_Q(wake_q);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700860
Peter Hurley88b9e452013-03-08 12:43:27 -0800861 ns = current->nsproxy->ipc_ns;
862
Stanislav Kinsburskyf9dd87f2013-01-04 15:34:52 -0800863 if (msqid < 0 || (long) bufsz < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700864 return -EINVAL;
Davidlohr Bueso41a0d522013-07-08 16:01:18 -0700865
Stanislav Kinsbursky4a674f32013-01-04 15:34:55 -0800866 if (msgflg & MSG_COPY) {
Michael Kerrisk4f87dac2014-03-10 14:46:07 +0100867 if ((msgflg & MSG_EXCEPT) || !(msgflg & IPC_NOWAIT))
868 return -EINVAL;
Peter Hurley8ac6ed52013-04-30 19:14:54 -0700869 copy = prepare_copy(buf, min_t(size_t, bufsz, ns->msg_ctlmax));
Stanislav Kinsbursky4a674f32013-01-04 15:34:55 -0800870 if (IS_ERR(copy))
871 return PTR_ERR(copy);
872 }
Ingo Molnar5a06a362006-07-30 03:04:11 -0700873 mode = convert_mode(&msgtyp, msgflg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700874
Davidlohr Bueso41a0d522013-07-08 16:01:18 -0700875 rcu_read_lock();
876 msq = msq_obtain_object_check(ns, msqid);
Stanislav Kinsbursky4a674f32013-01-04 15:34:55 -0800877 if (IS_ERR(msq)) {
Davidlohr Bueso41a0d522013-07-08 16:01:18 -0700878 rcu_read_unlock();
Stanislav Kinsbursky85398aa2013-01-04 15:34:58 -0800879 free_copy(copy);
Nadia Derbey023a5352007-10-18 23:40:51 -0700880 return PTR_ERR(msq);
Stanislav Kinsbursky4a674f32013-01-04 15:34:55 -0800881 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700882
883 for (;;) {
884 struct msg_receiver msr_d;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700885
886 msg = ERR_PTR(-EACCES);
Serge E. Hallynb0e77592011-03-23 16:43:24 -0700887 if (ipcperms(ns, &msq->q_perm, S_IRUGO))
Davidlohr Bueso41a0d522013-07-08 16:01:18 -0700888 goto out_unlock1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700889
Davidlohr Bueso41a0d522013-07-08 16:01:18 -0700890 ipc_lock_object(&msq->q_perm);
Davidlohr Bueso4271b052013-09-30 13:45:26 -0700891
892 /* raced with RMID? */
Rafael Aquini0f3d2b02014-01-27 17:07:01 -0800893 if (!ipc_valid_object(&msq->q_perm)) {
Davidlohr Bueso4271b052013-09-30 13:45:26 -0700894 msg = ERR_PTR(-EIDRM);
895 goto out_unlock0;
896 }
897
Peter Hurleydaaf74c2013-04-30 19:15:04 -0700898 msg = find_msg(msq, &msgtyp, mode);
Ingo Molnar5a06a362006-07-30 03:04:11 -0700899 if (!IS_ERR(msg)) {
900 /*
901 * Found a suitable message.
902 * Unlink it from the queue.
903 */
Stanislav Kinsburskyf9dd87f2013-01-04 15:34:52 -0800904 if ((bufsz < msg->m_ts) && !(msgflg & MSG_NOERROR)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700905 msg = ERR_PTR(-E2BIG);
Davidlohr Bueso41a0d522013-07-08 16:01:18 -0700906 goto out_unlock0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700907 }
Stanislav Kinsbursky3fcfe782013-01-04 15:35:03 -0800908 /*
909 * If we are copying, then do not unlink message and do
910 * not update queue parameters.
911 */
Peter Hurley852028a2013-04-30 19:14:48 -0700912 if (msgflg & MSG_COPY) {
913 msg = copy_msg(msg, copy);
Davidlohr Bueso41a0d522013-07-08 16:01:18 -0700914 goto out_unlock0;
Peter Hurley852028a2013-04-30 19:14:48 -0700915 }
Davidlohr Bueso41a0d522013-07-08 16:01:18 -0700916
Linus Torvalds1da177e2005-04-16 15:20:36 -0700917 list_del(&msg->m_list);
918 msq->q_qnum--;
919 msq->q_rtime = get_seconds();
Pavel Emelyanovb4888932007-10-18 23:40:14 -0700920 msq->q_lrpid = task_tgid_vnr(current);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700921 msq->q_cbytes -= msg->m_ts;
Kirill Korotaev3ac88a42007-10-18 23:40:56 -0700922 atomic_sub(msg->m_ts, &ns->msg_bytes);
923 atomic_dec(&ns->msg_hdrs);
Davidlohr Buesoed27f912016-10-11 13:55:02 -0700924 ss_wakeup(msq, &wake_q, false);
Davidlohr Bueso41a0d522013-07-08 16:01:18 -0700925
926 goto out_unlock0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700927 }
Davidlohr Bueso41a0d522013-07-08 16:01:18 -0700928
Linus Torvalds1da177e2005-04-16 15:20:36 -0700929 /* No message waiting. Wait for a message */
930 if (msgflg & IPC_NOWAIT) {
931 msg = ERR_PTR(-ENOMSG);
Davidlohr Bueso41a0d522013-07-08 16:01:18 -0700932 goto out_unlock0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700933 }
Davidlohr Bueso41a0d522013-07-08 16:01:18 -0700934
Ingo Molnar5a06a362006-07-30 03:04:11 -0700935 list_add_tail(&msr_d.r_list, &msq->q_receivers);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700936 msr_d.r_tsk = current;
937 msr_d.r_msgtype = msgtyp;
938 msr_d.r_mode = mode;
Ingo Molnar5a06a362006-07-30 03:04:11 -0700939 if (msgflg & MSG_NOERROR)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700940 msr_d.r_maxsize = INT_MAX;
Ingo Molnar5a06a362006-07-30 03:04:11 -0700941 else
Stanislav Kinsburskyf9dd87f2013-01-04 15:34:52 -0800942 msr_d.r_maxsize = bufsz;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700943 msr_d.r_msg = ERR_PTR(-EAGAIN);
Davidlohr Buesof75a2f32014-06-06 14:37:44 -0700944 __set_current_state(TASK_INTERRUPTIBLE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700945
Davidlohr Bueso41a0d522013-07-08 16:01:18 -0700946 ipc_unlock_object(&msq->q_perm);
947 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700948 schedule();
949
Sebastian Andrzej Siewioree516362016-10-11 13:54:53 -0700950 /*
951 * Lockless receive, part 1:
952 * We don't hold a reference to the queue and getting a
953 * reference would defeat the idea of a lockless operation,
954 * thus the code relies on rcu to guarantee the existence of
955 * msq:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700956 * Prior to destruction, expunge_all(-EIRDM) changes r_msg.
957 * Thus if r_msg is -EAGAIN, then the queue not yet destroyed.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700958 */
959 rcu_read_lock();
960
Sebastian Andrzej Siewioree516362016-10-11 13:54:53 -0700961 /*
962 * Lockless receive, part 2:
963 * The work in pipelined_send() and expunge_all():
964 * - Set pointer to message
965 * - Queue the receiver task for later wakeup
966 * - Wake up the process after the lock is dropped.
Davidlohr Buesoff35e5e2015-06-30 14:58:39 -0700967 *
Sebastian Andrzej Siewioree516362016-10-11 13:54:53 -0700968 * Should the process wake up before this wakeup (due to a
969 * signal) it will either see the message and continue ...
Linus Torvalds1da177e2005-04-16 15:20:36 -0700970 */
Sebastian Andrzej Siewioree516362016-10-11 13:54:53 -0700971 msg = READ_ONCE(msr_d.r_msg);
Davidlohr Bueso41a0d522013-07-08 16:01:18 -0700972 if (msg != ERR_PTR(-EAGAIN))
973 goto out_unlock1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700974
Sebastian Andrzej Siewioree516362016-10-11 13:54:53 -0700975 /*
976 * ... or see -EAGAIN, acquire the lock to check the message
977 * again.
978 */
Davidlohr Bueso41a0d522013-07-08 16:01:18 -0700979 ipc_lock_object(&msq->q_perm);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700980
Sebastian Andrzej Siewioree516362016-10-11 13:54:53 -0700981 msg = msr_d.r_msg;
Ingo Molnar5a06a362006-07-30 03:04:11 -0700982 if (msg != ERR_PTR(-EAGAIN))
Davidlohr Bueso41a0d522013-07-08 16:01:18 -0700983 goto out_unlock0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700984
985 list_del(&msr_d.r_list);
986 if (signal_pending(current)) {
987 msg = ERR_PTR(-ERESTARTNOHAND);
Davidlohr Bueso41a0d522013-07-08 16:01:18 -0700988 goto out_unlock0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700989 }
Davidlohr Bueso41a0d522013-07-08 16:01:18 -0700990
991 ipc_unlock_object(&msq->q_perm);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700992 }
Davidlohr Bueso41a0d522013-07-08 16:01:18 -0700993
994out_unlock0:
995 ipc_unlock_object(&msq->q_perm);
Davidlohr Buesoe3658532016-10-11 13:54:56 -0700996 wake_up_q(&wake_q);
Davidlohr Bueso41a0d522013-07-08 16:01:18 -0700997out_unlock1:
998 rcu_read_unlock();
Stanislav Kinsbursky4a674f32013-01-04 15:34:55 -0800999 if (IS_ERR(msg)) {
Stanislav Kinsbursky85398aa2013-01-04 15:34:58 -08001000 free_copy(copy);
Ingo Molnar5a06a362006-07-30 03:04:11 -07001001 return PTR_ERR(msg);
Stanislav Kinsbursky4a674f32013-01-04 15:34:55 -08001002 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001003
Stanislav Kinsburskyf9dd87f2013-01-04 15:34:52 -08001004 bufsz = msg_handler(buf, msg, bufsz);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001005 free_msg(msg);
Ingo Molnar5a06a362006-07-30 03:04:11 -07001006
Stanislav Kinsburskyf9dd87f2013-01-04 15:34:52 -08001007 return bufsz;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001008}
1009
Heiko Carstense48fbb62009-01-14 14:14:26 +01001010SYSCALL_DEFINE5(msgrcv, int, msqid, struct msgbuf __user *, msgp, size_t, msgsz,
1011 long, msgtyp, int, msgflg)
suzuki651971c2006-12-06 20:37:48 -08001012{
Stanislav Kinsburskyf9dd87f2013-01-04 15:34:52 -08001013 return do_msgrcv(msqid, msgp, msgsz, msgtyp, msgflg, do_msg_fill);
suzuki651971c2006-12-06 20:37:48 -08001014}
1015
Davidlohr Bueso3440a6b2014-06-06 14:37:45 -07001016
1017void msg_init_ns(struct ipc_namespace *ns)
1018{
1019 ns->msg_ctlmax = MSGMAX;
1020 ns->msg_ctlmnb = MSGMNB;
Manfred Spraul0050ee02014-12-12 16:58:17 -08001021 ns->msg_ctlmni = MSGMNI;
Davidlohr Bueso3440a6b2014-06-06 14:37:45 -07001022
1023 atomic_set(&ns->msg_bytes, 0);
1024 atomic_set(&ns->msg_hdrs, 0);
1025 ipc_init_ids(&ns->ids[IPC_MSG_IDS]);
1026}
1027
1028#ifdef CONFIG_IPC_NS
1029void msg_exit_ns(struct ipc_namespace *ns)
1030{
1031 free_ipcs(ns, &msg_ids(ns), freeque);
1032 idr_destroy(&ns->ids[IPC_MSG_IDS].ipcs_idr);
1033}
1034#endif
1035
Linus Torvalds1da177e2005-04-16 15:20:36 -07001036#ifdef CONFIG_PROC_FS
Mike Waychison19b49462005-09-06 15:17:10 -07001037static int sysvipc_msg_proc_show(struct seq_file *s, void *it)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001038{
Eric W. Biederman1efdb692012-02-07 16:54:11 -08001039 struct user_namespace *user_ns = seq_user_ns(s);
Mike Waychison19b49462005-09-06 15:17:10 -07001040 struct msg_queue *msq = it;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001041
Joe Perches7f032d62015-04-15 16:17:54 -07001042 seq_printf(s,
1043 "%10d %10d %4o %10lu %10lu %5u %5u %5u %5u %5u %5u %10lu %10lu %10lu\n",
1044 msq->q_perm.key,
1045 msq->q_perm.id,
1046 msq->q_perm.mode,
1047 msq->q_cbytes,
1048 msq->q_qnum,
1049 msq->q_lspid,
1050 msq->q_lrpid,
1051 from_kuid_munged(user_ns, msq->q_perm.uid),
1052 from_kgid_munged(user_ns, msq->q_perm.gid),
1053 from_kuid_munged(user_ns, msq->q_perm.cuid),
1054 from_kgid_munged(user_ns, msq->q_perm.cgid),
1055 msq->q_stime,
1056 msq->q_rtime,
1057 msq->q_ctime);
1058
1059 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001060}
1061#endif
Davidlohr Bueso3440a6b2014-06-06 14:37:45 -07001062
1063void __init msg_init(void)
1064{
1065 msg_init_ns(&init_ipc_ns);
1066
Davidlohr Bueso3440a6b2014-06-06 14:37:45 -07001067 ipc_init_proc_interface("sysvipc/msg",
1068 " key msqid perms cbytes qnum lspid lrpid uid gid cuid cgid stime rtime ctime\n",
1069 IPC_MSG_IDS, sysvipc_msg_proc_show);
1070}