]>
Commit | Line | Data |
---|---|---|
b2441318 | 1 | // SPDX-License-Identifier: GPL-2.0 |
1da177e4 LT |
2 | /* |
3 | * linux/ipc/msg.c | |
5a06a363 | 4 | * Copyright (C) 1992 Krishna Balasubramanian |
1da177e4 LT |
5 | * |
6 | * Removed all the remaining kerneld mess | |
7 | * Catch the -EFAULT stuff properly | |
8 | * Use GFP_KERNEL for messages as in 1.2 | |
9 | * Fixed up the unchecked user space derefs | |
10 | * Copyright (C) 1998 Alan Cox & Andi Kleen | |
11 | * | |
12 | * /proc/sysvipc/msg support (c) 1999 Dragos Acostachioaie <[email protected]> | |
13 | * | |
14 | * mostly rewritten, threaded and wake-one semantics added | |
15 | * MSGMAX limit removed, sysctl's added | |
624dffcb | 16 | * (c) 1999 Manfred Spraul <[email protected]> |
073115d6 SG |
17 | * |
18 | * support for audit of ipc object properties and permission changes | |
19 | * Dustin Kirkland <[email protected]> | |
1e786937 KK |
20 | * |
21 | * namespaces support | |
22 | * OpenVZ, SWsoft Inc. | |
23 | * Pavel Emelianov <[email protected]> | |
1da177e4 LT |
24 | */ |
25 | ||
c59ede7b | 26 | #include <linux/capability.h> |
1da177e4 LT |
27 | #include <linux/msg.h> |
28 | #include <linux/spinlock.h> | |
29 | #include <linux/init.h> | |
f7bf3df8 | 30 | #include <linux/mm.h> |
1da177e4 LT |
31 | #include <linux/proc_fs.h> |
32 | #include <linux/list.h> | |
33 | #include <linux/security.h> | |
84f001e1 | 34 | #include <linux/sched/wake_q.h> |
1da177e4 LT |
35 | #include <linux/syscalls.h> |
36 | #include <linux/audit.h> | |
19b4946c | 37 | #include <linux/seq_file.h> |
3e148c79 | 38 | #include <linux/rwsem.h> |
1e786937 | 39 | #include <linux/nsproxy.h> |
ae5e1b22 | 40 | #include <linux/ipc_namespace.h> |
0eb71a9d | 41 | #include <linux/rhashtable.h> |
5f921ae9 | 42 | |
1da177e4 | 43 | #include <asm/current.h> |
7153e402 | 44 | #include <linux/uaccess.h> |
1da177e4 LT |
45 | #include "util.h" |
46 | ||
34b56df9 EB |
47 | /* one msq_queue structure for each present queue on the system */ |
48 | struct msg_queue { | |
49 | struct kern_ipc_perm q_perm; | |
50 | time64_t q_stime; /* last msgsnd time */ | |
51 | time64_t q_rtime; /* last msgrcv time */ | |
52 | time64_t q_ctime; /* last change time */ | |
53 | unsigned long q_cbytes; /* current number of bytes on queue */ | |
54 | unsigned long q_qnum; /* number of messages in queue */ | |
55 | unsigned long q_qbytes; /* max number of bytes on queue */ | |
39a4940e EB |
56 | struct pid *q_lspid; /* pid of last msgsnd */ |
57 | struct pid *q_lrpid; /* last receive pid */ | |
34b56df9 EB |
58 | |
59 | struct list_head q_messages; | |
60 | struct list_head q_receivers; | |
61 | struct list_head q_senders; | |
62 | } __randomize_layout; | |
63 | ||
0d97a82b MS |
64 | /* |
65 | * MSG_BARRIER Locking: | |
66 | * | |
67 | * Similar to the optimization used in ipc/mqueue.c, one syscall return path | |
68 | * does not acquire any locks when it sees that a message exists in | |
69 | * msg_receiver.r_msg. Therefore r_msg is set using smp_store_release() | |
70 | * and accessed using READ_ONCE()+smp_acquire__after_ctrl_dep(). In addition, | |
71 | * wake_q_add_safe() is used. See ipc/mqueue.c for more details | |
72 | */ | |
73 | ||
4bb6657d | 74 | /* one msg_receiver structure for each sleeping receiver */ |
1da177e4 | 75 | struct msg_receiver { |
5a06a363 IM |
76 | struct list_head r_list; |
77 | struct task_struct *r_tsk; | |
1da177e4 | 78 | |
5a06a363 IM |
79 | int r_mode; |
80 | long r_msgtype; | |
81 | long r_maxsize; | |
1da177e4 | 82 | |
ee51636c | 83 | struct msg_msg *r_msg; |
1da177e4 LT |
84 | }; |
85 | ||
86 | /* one msg_sender for each sleeping sender */ | |
87 | struct msg_sender { | |
5a06a363 IM |
88 | struct list_head list; |
89 | struct task_struct *tsk; | |
ed27f912 | 90 | size_t msgsz; |
1da177e4 LT |
91 | }; |
92 | ||
93 | #define SEARCH_ANY 1 | |
94 | #define SEARCH_EQUAL 2 | |
95 | #define SEARCH_NOTEQUAL 3 | |
96 | #define SEARCH_LESSEQUAL 4 | |
8ac6ed58 | 97 | #define SEARCH_NUMBER 5 |
1da177e4 | 98 | |
ed2ddbf8 | 99 | #define msg_ids(ns) ((ns)->ids[IPC_MSG_IDS]) |
1da177e4 | 100 | |
a5001a0d DB |
101 | static inline struct msg_queue *msq_obtain_object(struct ipc_namespace *ns, int id) |
102 | { | |
55b7ae50 | 103 | struct kern_ipc_perm *ipcp = ipc_obtain_object_idr(&msg_ids(ns), id); |
a5001a0d DB |
104 | |
105 | if (IS_ERR(ipcp)) | |
106 | return ERR_CAST(ipcp); | |
107 | ||
108 | return container_of(ipcp, struct msg_queue, q_perm); | |
109 | } | |
110 | ||
111 | static inline struct msg_queue *msq_obtain_object_check(struct ipc_namespace *ns, | |
112 | int id) | |
113 | { | |
114 | struct kern_ipc_perm *ipcp = ipc_obtain_object_check(&msg_ids(ns), id); | |
115 | ||
116 | if (IS_ERR(ipcp)) | |
117 | return ERR_CAST(ipcp); | |
118 | ||
119 | return container_of(ipcp, struct msg_queue, q_perm); | |
120 | } | |
121 | ||
7ca7e564 ND |
122 | static inline void msg_rmid(struct ipc_namespace *ns, struct msg_queue *s) |
123 | { | |
124 | ipc_rmid(&msg_ids(ns), &s->q_perm); | |
125 | } | |
126 | ||
53dad6d3 DB |
127 | static void msg_rcu_free(struct rcu_head *head) |
128 | { | |
dba4cdd3 MS |
129 | struct kern_ipc_perm *p = container_of(head, struct kern_ipc_perm, rcu); |
130 | struct msg_queue *msq = container_of(p, struct msg_queue, q_perm); | |
53dad6d3 | 131 | |
d8c6e854 | 132 | security_msg_queue_free(&msq->q_perm); |
fb259c31 | 133 | kvfree(msq); |
52f90890 KC |
134 | } |
135 | ||
f4566f04 ND |
136 | /** |
137 | * newque - Create a new msg queue | |
138 | * @ns: namespace | |
139 | * @params: ptr to the structure that contains the key and msgflg | |
140 | * | |
d9a605e4 | 141 | * Called with msg_ids.rwsem held (writer) |
f4566f04 | 142 | */ |
7748dbfa | 143 | static int newque(struct ipc_namespace *ns, struct ipc_params *params) |
1da177e4 | 144 | { |
1da177e4 | 145 | struct msg_queue *msq; |
51c23b7b | 146 | int retval; |
7748dbfa ND |
147 | key_t key = params->key; |
148 | int msgflg = params->flg; | |
1da177e4 | 149 | |
fb259c31 KC |
150 | msq = kvmalloc(sizeof(*msq), GFP_KERNEL); |
151 | if (unlikely(!msq)) | |
1da177e4 LT |
152 | return -ENOMEM; |
153 | ||
5a06a363 | 154 | msq->q_perm.mode = msgflg & S_IRWXUGO; |
1da177e4 LT |
155 | msq->q_perm.key = key; |
156 | ||
157 | msq->q_perm.security = NULL; | |
d8c6e854 | 158 | retval = security_msg_queue_alloc(&msq->q_perm); |
1da177e4 | 159 | if (retval) { |
fb259c31 | 160 | kvfree(msq); |
1da177e4 LT |
161 | return retval; |
162 | } | |
163 | ||
1da177e4 | 164 | msq->q_stime = msq->q_rtime = 0; |
50578ea9 | 165 | msq->q_ctime = ktime_get_real_seconds(); |
1da177e4 | 166 | msq->q_cbytes = msq->q_qnum = 0; |
1e786937 | 167 | msq->q_qbytes = ns->msg_ctlmnb; |
39a4940e | 168 | msq->q_lspid = msq->q_lrpid = NULL; |
1da177e4 LT |
169 | INIT_LIST_HEAD(&msq->q_messages); |
170 | INIT_LIST_HEAD(&msq->q_receivers); | |
171 | INIT_LIST_HEAD(&msq->q_senders); | |
7ca7e564 | 172 | |
b9a53227 | 173 | /* ipc_addid() locks msq upon success. */ |
51c23b7b MS |
174 | retval = ipc_addid(&msg_ids(ns), &msq->q_perm, ns->msg_ctlmni); |
175 | if (retval < 0) { | |
39cfffd7 | 176 | ipc_rcu_putref(&msq->q_perm, msg_rcu_free); |
51c23b7b | 177 | return retval; |
b9a53227 LT |
178 | } |
179 | ||
cf9d5d78 | 180 | ipc_unlock_object(&msq->q_perm); |
dbfcd91f | 181 | rcu_read_unlock(); |
1da177e4 | 182 | |
7ca7e564 | 183 | return msq->q_perm.id; |
1da177e4 LT |
184 | } |
185 | ||
ed27f912 DB |
186 | static inline bool msg_fits_inqueue(struct msg_queue *msq, size_t msgsz) |
187 | { | |
188 | return msgsz + msq->q_cbytes <= msq->q_qbytes && | |
189 | 1 + msq->q_qnum <= msq->q_qbytes; | |
190 | } | |
191 | ||
192 | static inline void ss_add(struct msg_queue *msq, | |
193 | struct msg_sender *mss, size_t msgsz) | |
1da177e4 | 194 | { |
5a06a363 | 195 | mss->tsk = current; |
ed27f912 | 196 | mss->msgsz = msgsz; |
0d97a82b MS |
197 | /* |
198 | * No memory barrier required: we did ipc_lock_object(), | |
199 | * and the waker obtains that lock before calling wake_q_add(). | |
200 | */ | |
f75a2f35 | 201 | __set_current_state(TASK_INTERRUPTIBLE); |
5a06a363 | 202 | list_add_tail(&mss->list, &msq->q_senders); |
1da177e4 LT |
203 | } |
204 | ||
5a06a363 | 205 | static inline void ss_del(struct msg_sender *mss) |
1da177e4 | 206 | { |
ed27f912 | 207 | if (mss->list.next) |
1da177e4 LT |
208 | list_del(&mss->list); |
209 | } | |
210 | ||
ed27f912 | 211 | static void ss_wakeup(struct msg_queue *msq, |
d0d6a2a9 | 212 | struct wake_q_head *wake_q, bool kill) |
1da177e4 | 213 | { |
41239fe8 | 214 | struct msg_sender *mss, *t; |
ed27f912 DB |
215 | struct task_struct *stop_tsk = NULL; |
216 | struct list_head *h = &msq->q_senders; | |
1da177e4 | 217 | |
41239fe8 | 218 | list_for_each_entry_safe(mss, t, h, list) { |
5a06a363 IM |
219 | if (kill) |
220 | mss->list.next = NULL; | |
ed27f912 DB |
221 | |
222 | /* | |
223 | * Stop at the first task we don't wakeup, | |
224 | * we've already iterated the original | |
225 | * sender queue. | |
226 | */ | |
227 | else if (stop_tsk == mss->tsk) | |
228 | break; | |
229 | /* | |
230 | * We are not in an EIDRM scenario here, therefore | |
231 | * verify that we really need to wakeup the task. | |
232 | * To maintain current semantics and wakeup order, | |
233 | * move the sender to the tail on behalf of the | |
234 | * blocked task. | |
235 | */ | |
236 | else if (!msg_fits_inqueue(msq, mss->msgsz)) { | |
237 | if (!stop_tsk) | |
238 | stop_tsk = mss->tsk; | |
239 | ||
240 | list_move_tail(&mss->list, &msq->q_senders); | |
241 | continue; | |
242 | } | |
243 | ||
e3658538 | 244 | wake_q_add(wake_q, mss->tsk); |
1da177e4 LT |
245 | } |
246 | } | |
247 | ||
ee51636c SAS |
248 | static void expunge_all(struct msg_queue *msq, int res, |
249 | struct wake_q_head *wake_q) | |
1da177e4 | 250 | { |
41239fe8 | 251 | struct msg_receiver *msr, *t; |
5a06a363 | 252 | |
41239fe8 | 253 | list_for_each_entry_safe(msr, t, &msq->q_receivers, r_list) { |
0d97a82b MS |
254 | get_task_struct(msr->r_tsk); |
255 | ||
256 | /* see MSG_BARRIER for purpose/pairing */ | |
257 | smp_store_release(&msr->r_msg, ERR_PTR(res)); | |
258 | wake_q_add_safe(wake_q, msr->r_tsk); | |
1da177e4 LT |
259 | } |
260 | } | |
5a06a363 IM |
261 | |
262 | /* | |
263 | * freeque() wakes up waiters on the sender and receiver waiting queue, | |
f4566f04 ND |
264 | * removes the message queue from message queue ID IDR, and cleans up all the |
265 | * messages associated with this queue. | |
1da177e4 | 266 | * |
d9a605e4 DB |
267 | * msg_ids.rwsem (writer) and the spinlock for this message queue are held |
268 | * before freeque() is called. msg_ids.rwsem remains locked on exit. | |
1da177e4 | 269 | */ |
01b8b07a | 270 | static void freeque(struct ipc_namespace *ns, struct kern_ipc_perm *ipcp) |
4b78e201 JI |
271 | __releases(RCU) |
272 | __releases(&msq->q_perm) | |
1da177e4 | 273 | { |
41239fe8 | 274 | struct msg_msg *msg, *t; |
01b8b07a | 275 | struct msg_queue *msq = container_of(ipcp, struct msg_queue, q_perm); |
194a6b5b | 276 | DEFINE_WAKE_Q(wake_q); |
1da177e4 | 277 | |
ee51636c | 278 | expunge_all(msq, -EIDRM, &wake_q); |
ed27f912 | 279 | ss_wakeup(msq, &wake_q, true); |
7ca7e564 | 280 | msg_rmid(ns, msq); |
4718787d | 281 | ipc_unlock_object(&msq->q_perm); |
ee51636c | 282 | wake_up_q(&wake_q); |
4718787d | 283 | rcu_read_unlock(); |
5a06a363 | 284 | |
41239fe8 | 285 | list_for_each_entry_safe(msg, t, &msq->q_messages, m_list) { |
3ac88a41 | 286 | atomic_dec(&ns->msg_hdrs); |
1da177e4 LT |
287 | free_msg(msg); |
288 | } | |
3ac88a41 | 289 | atomic_sub(msq->q_cbytes, &ns->msg_bytes); |
39a4940e EB |
290 | ipc_update_pid(&msq->q_lspid, NULL); |
291 | ipc_update_pid(&msq->q_lrpid, NULL); | |
dba4cdd3 | 292 | ipc_rcu_putref(&msq->q_perm, msg_rcu_free); |
1da177e4 LT |
293 | } |
294 | ||
3d65661a | 295 | long ksys_msgget(key_t key, int msgflg) |
1da177e4 | 296 | { |
1e786937 | 297 | struct ipc_namespace *ns; |
eb66ec44 MK |
298 | static const struct ipc_ops msg_ops = { |
299 | .getnew = newque, | |
50ab44b1 | 300 | .associate = security_msg_queue_associate, |
eb66ec44 | 301 | }; |
7748dbfa | 302 | struct ipc_params msg_params; |
1e786937 KK |
303 | |
304 | ns = current->nsproxy->ipc_ns; | |
7ca7e564 | 305 | |
7748dbfa ND |
306 | msg_params.key = key; |
307 | msg_params.flg = msgflg; | |
5a06a363 | 308 | |
7748dbfa | 309 | return ipcget(ns, &msg_ids(ns), &msg_ops, &msg_params); |
1da177e4 LT |
310 | } |
311 | ||
3d65661a DB |
312 | SYSCALL_DEFINE2(msgget, key_t, key, int, msgflg) |
313 | { | |
314 | return ksys_msgget(key, msgflg); | |
315 | } | |
316 | ||
5a06a363 IM |
317 | static inline unsigned long |
318 | copy_msqid_to_user(void __user *buf, struct msqid64_ds *in, int version) | |
1da177e4 | 319 | { |
239521f3 | 320 | switch (version) { |
1da177e4 | 321 | case IPC_64: |
5a06a363 | 322 | return copy_to_user(buf, in, sizeof(*in)); |
1da177e4 | 323 | case IPC_OLD: |
5a06a363 | 324 | { |
1da177e4 LT |
325 | struct msqid_ds out; |
326 | ||
5a06a363 | 327 | memset(&out, 0, sizeof(out)); |
1da177e4 LT |
328 | |
329 | ipc64_perm_to_ipc_perm(&in->msg_perm, &out.msg_perm); | |
330 | ||
331 | out.msg_stime = in->msg_stime; | |
332 | out.msg_rtime = in->msg_rtime; | |
333 | out.msg_ctime = in->msg_ctime; | |
334 | ||
4be929be AD |
335 | if (in->msg_cbytes > USHRT_MAX) |
336 | out.msg_cbytes = USHRT_MAX; | |
1da177e4 LT |
337 | else |
338 | out.msg_cbytes = in->msg_cbytes; | |
339 | out.msg_lcbytes = in->msg_cbytes; | |
340 | ||
4be929be AD |
341 | if (in->msg_qnum > USHRT_MAX) |
342 | out.msg_qnum = USHRT_MAX; | |
1da177e4 LT |
343 | else |
344 | out.msg_qnum = in->msg_qnum; | |
345 | ||
4be929be AD |
346 | if (in->msg_qbytes > USHRT_MAX) |
347 | out.msg_qbytes = USHRT_MAX; | |
1da177e4 LT |
348 | else |
349 | out.msg_qbytes = in->msg_qbytes; | |
350 | out.msg_lqbytes = in->msg_qbytes; | |
351 | ||
352 | out.msg_lspid = in->msg_lspid; | |
353 | out.msg_lrpid = in->msg_lrpid; | |
354 | ||
5a06a363 IM |
355 | return copy_to_user(buf, &out, sizeof(out)); |
356 | } | |
1da177e4 LT |
357 | default: |
358 | return -EINVAL; | |
359 | } | |
360 | } | |
361 | ||
5a06a363 | 362 | static inline unsigned long |
016d7132 | 363 | copy_msqid_from_user(struct msqid64_ds *out, void __user *buf, int version) |
1da177e4 | 364 | { |
239521f3 | 365 | switch (version) { |
1da177e4 | 366 | case IPC_64: |
016d7132 | 367 | if (copy_from_user(out, buf, sizeof(*out))) |
1da177e4 | 368 | return -EFAULT; |
1da177e4 | 369 | return 0; |
1da177e4 | 370 | case IPC_OLD: |
5a06a363 | 371 | { |
1da177e4 LT |
372 | struct msqid_ds tbuf_old; |
373 | ||
5a06a363 | 374 | if (copy_from_user(&tbuf_old, buf, sizeof(tbuf_old))) |
1da177e4 LT |
375 | return -EFAULT; |
376 | ||
239521f3 MS |
377 | out->msg_perm.uid = tbuf_old.msg_perm.uid; |
378 | out->msg_perm.gid = tbuf_old.msg_perm.gid; | |
379 | out->msg_perm.mode = tbuf_old.msg_perm.mode; | |
1da177e4 | 380 | |
5a06a363 | 381 | if (tbuf_old.msg_qbytes == 0) |
016d7132 | 382 | out->msg_qbytes = tbuf_old.msg_lqbytes; |
1da177e4 | 383 | else |
016d7132 | 384 | out->msg_qbytes = tbuf_old.msg_qbytes; |
1da177e4 LT |
385 | |
386 | return 0; | |
5a06a363 | 387 | } |
1da177e4 LT |
388 | default: |
389 | return -EINVAL; | |
390 | } | |
391 | } | |
392 | ||
a0d092fc | 393 | /* |
d9a605e4 | 394 | * This function handles some msgctl commands which require the rwsem |
a0d092fc | 395 | * to be held in write mode. |
d9a605e4 | 396 | * NOTE: no locks must be held, the rwsem is taken inside this function. |
a0d092fc PP |
397 | */ |
398 | static int msgctl_down(struct ipc_namespace *ns, int msqid, int cmd, | |
889b3317 | 399 | struct ipc64_perm *perm, int msg_qbytes) |
1da177e4 | 400 | { |
1da177e4 | 401 | struct kern_ipc_perm *ipcp; |
a0d092fc PP |
402 | struct msg_queue *msq; |
403 | int err; | |
404 | ||
d9a605e4 | 405 | down_write(&msg_ids(ns).rwsem); |
7b4cc5d8 DB |
406 | rcu_read_lock(); |
407 | ||
4241c1a3 | 408 | ipcp = ipcctl_obtain_check(ns, &msg_ids(ns), msqid, cmd, |
889b3317 | 409 | perm, msg_qbytes); |
7b4cc5d8 DB |
410 | if (IS_ERR(ipcp)) { |
411 | err = PTR_ERR(ipcp); | |
7b4cc5d8 DB |
412 | goto out_unlock1; |
413 | } | |
a0d092fc | 414 | |
a5f75e7f | 415 | msq = container_of(ipcp, struct msg_queue, q_perm); |
a0d092fc | 416 | |
d8c6e854 | 417 | err = security_msg_queue_msgctl(&msq->q_perm, cmd); |
a0d092fc | 418 | if (err) |
15724ecb | 419 | goto out_unlock1; |
a0d092fc PP |
420 | |
421 | switch (cmd) { | |
422 | case IPC_RMID: | |
15724ecb | 423 | ipc_lock_object(&msq->q_perm); |
7b4cc5d8 | 424 | /* freeque unlocks the ipc object and rcu */ |
a0d092fc PP |
425 | freeque(ns, ipcp); |
426 | goto out_up; | |
427 | case IPC_SET: | |
e3658538 | 428 | { |
194a6b5b | 429 | DEFINE_WAKE_Q(wake_q); |
e3658538 | 430 | |
889b3317 | 431 | if (msg_qbytes > ns->msg_ctlmnb && |
a0d092fc PP |
432 | !capable(CAP_SYS_RESOURCE)) { |
433 | err = -EPERM; | |
15724ecb | 434 | goto out_unlock1; |
a0d092fc PP |
435 | } |
436 | ||
15724ecb | 437 | ipc_lock_object(&msq->q_perm); |
889b3317 | 438 | err = ipc_update_perm(perm, ipcp); |
1efdb69b | 439 | if (err) |
7b4cc5d8 | 440 | goto out_unlock0; |
1efdb69b | 441 | |
889b3317 | 442 | msq->q_qbytes = msg_qbytes; |
a0d092fc | 443 | |
50578ea9 | 444 | msq->q_ctime = ktime_get_real_seconds(); |
e3658538 DB |
445 | /* |
446 | * Sleeping receivers might be excluded by | |
a0d092fc PP |
447 | * stricter permissions. |
448 | */ | |
ee51636c | 449 | expunge_all(msq, -EAGAIN, &wake_q); |
e3658538 DB |
450 | /* |
451 | * Sleeping senders might be able to send | |
a0d092fc PP |
452 | * due to a larger queue size. |
453 | */ | |
ed27f912 | 454 | ss_wakeup(msq, &wake_q, false); |
e3658538 DB |
455 | ipc_unlock_object(&msq->q_perm); |
456 | wake_up_q(&wake_q); | |
457 | ||
458 | goto out_unlock1; | |
459 | } | |
a0d092fc PP |
460 | default: |
461 | err = -EINVAL; | |
15724ecb | 462 | goto out_unlock1; |
a0d092fc | 463 | } |
7b4cc5d8 DB |
464 | |
465 | out_unlock0: | |
466 | ipc_unlock_object(&msq->q_perm); | |
467 | out_unlock1: | |
468 | rcu_read_unlock(); | |
a0d092fc | 469 | out_up: |
d9a605e4 | 470 | up_write(&msg_ids(ns).rwsem); |
a0d092fc PP |
471 | return err; |
472 | } | |
473 | ||
156d9ed1 AV |
474 | static int msgctl_info(struct ipc_namespace *ns, int msqid, |
475 | int cmd, struct msginfo *msginfo) | |
a0d092fc | 476 | { |
2cafed30 | 477 | int err; |
27c331a1 | 478 | int max_idx; |
1da177e4 | 479 | |
156d9ed1 AV |
480 | /* |
481 | * We must not return kernel stack data. | |
482 | * due to padding, it's not enough | |
483 | * to set all member fields. | |
484 | */ | |
485 | err = security_msg_queue_msgctl(NULL, cmd); | |
486 | if (err) | |
487 | return err; | |
488 | ||
489 | memset(msginfo, 0, sizeof(*msginfo)); | |
490 | msginfo->msgmni = ns->msg_ctlmni; | |
491 | msginfo->msgmax = ns->msg_ctlmax; | |
492 | msginfo->msgmnb = ns->msg_ctlmnb; | |
493 | msginfo->msgssz = MSGSSZ; | |
494 | msginfo->msgseg = MSGSEG; | |
495 | down_read(&msg_ids(ns).rwsem); | |
496 | if (cmd == MSG_INFO) { | |
497 | msginfo->msgpool = msg_ids(ns).in_use; | |
498 | msginfo->msgmap = atomic_read(&ns->msg_hdrs); | |
499 | msginfo->msgtql = atomic_read(&ns->msg_bytes); | |
500 | } else { | |
501 | msginfo->msgmap = MSGMAP; | |
502 | msginfo->msgpool = MSGPOOL; | |
503 | msginfo->msgtql = MSGTQL; | |
1da177e4 | 504 | } |
27c331a1 | 505 | max_idx = ipc_get_maxidx(&msg_ids(ns)); |
156d9ed1 | 506 | up_read(&msg_ids(ns).rwsem); |
27c331a1 | 507 | return (max_idx < 0) ? 0 : max_idx; |
156d9ed1 | 508 | } |
2cafed30 | 509 | |
156d9ed1 AV |
510 | static int msgctl_stat(struct ipc_namespace *ns, int msqid, |
511 | int cmd, struct msqid64_ds *p) | |
512 | { | |
156d9ed1 | 513 | struct msg_queue *msq; |
87ad4b0d | 514 | int err; |
ac0ba20e | 515 | |
156d9ed1 | 516 | memset(p, 0, sizeof(*p)); |
ac0ba20e | 517 | |
156d9ed1 | 518 | rcu_read_lock(); |
23c8cec8 | 519 | if (cmd == MSG_STAT || cmd == MSG_STAT_ANY) { |
156d9ed1 AV |
520 | msq = msq_obtain_object(ns, msqid); |
521 | if (IS_ERR(msq)) { | |
522 | err = PTR_ERR(msq); | |
1da177e4 | 523 | goto out_unlock; |
1da177e4 | 524 | } |
23c8cec8 | 525 | } else { /* IPC_STAT */ |
156d9ed1 AV |
526 | msq = msq_obtain_object_check(ns, msqid); |
527 | if (IS_ERR(msq)) { | |
528 | err = PTR_ERR(msq); | |
1da177e4 | 529 | goto out_unlock; |
156d9ed1 | 530 | } |
156d9ed1 | 531 | } |
1da177e4 | 532 | |
23c8cec8 DB |
533 | /* see comment for SHM_STAT_ANY */ |
534 | if (cmd == MSG_STAT_ANY) | |
535 | audit_ipc_obj(&msq->q_perm); | |
536 | else { | |
537 | err = -EACCES; | |
538 | if (ipcperms(ns, &msq->q_perm, S_IRUGO)) | |
539 | goto out_unlock; | |
540 | } | |
ac0ba20e | 541 | |
d8c6e854 | 542 | err = security_msg_queue_msgctl(&msq->q_perm, cmd); |
156d9ed1 AV |
543 | if (err) |
544 | goto out_unlock; | |
545 | ||
87ad4b0d PM |
546 | ipc_lock_object(&msq->q_perm); |
547 | ||
548 | if (!ipc_valid_object(&msq->q_perm)) { | |
549 | ipc_unlock_object(&msq->q_perm); | |
550 | err = -EIDRM; | |
551 | goto out_unlock; | |
552 | } | |
553 | ||
156d9ed1 AV |
554 | kernel_to_ipc64_perm(&msq->q_perm, &p->msg_perm); |
555 | p->msg_stime = msq->q_stime; | |
556 | p->msg_rtime = msq->q_rtime; | |
557 | p->msg_ctime = msq->q_ctime; | |
c2ab975c AB |
558 | #ifndef CONFIG_64BIT |
559 | p->msg_stime_high = msq->q_stime >> 32; | |
560 | p->msg_rtime_high = msq->q_rtime >> 32; | |
561 | p->msg_ctime_high = msq->q_ctime >> 32; | |
562 | #endif | |
156d9ed1 AV |
563 | p->msg_cbytes = msq->q_cbytes; |
564 | p->msg_qnum = msq->q_qnum; | |
565 | p->msg_qbytes = msq->q_qbytes; | |
39a4940e EB |
566 | p->msg_lspid = pid_vnr(msq->q_lspid); |
567 | p->msg_lrpid = pid_vnr(msq->q_lrpid); | |
2cafed30 | 568 | |
615c999c MS |
569 | if (cmd == IPC_STAT) { |
570 | /* | |
571 | * As defined in SUS: | |
572 | * Return 0 on success | |
573 | */ | |
574 | err = 0; | |
575 | } else { | |
576 | /* | |
577 | * MSG_STAT and MSG_STAT_ANY (both Linux specific) | |
578 | * Return the full id, including the sequence number | |
579 | */ | |
580 | err = msq->q_perm.id; | |
581 | } | |
1da177e4 | 582 | |
615c999c | 583 | ipc_unlock_object(&msq->q_perm); |
1da177e4 | 584 | out_unlock: |
ac0ba20e | 585 | rcu_read_unlock(); |
1da177e4 LT |
586 | return err; |
587 | } | |
588 | ||
275f2214 | 589 | static long ksys_msgctl(int msqid, int cmd, struct msqid_ds __user *buf, int version) |
2cafed30 | 590 | { |
2cafed30 | 591 | struct ipc_namespace *ns; |
156d9ed1 AV |
592 | struct msqid64_ds msqid64; |
593 | int err; | |
2cafed30 DB |
594 | |
595 | if (msqid < 0 || cmd < 0) | |
596 | return -EINVAL; | |
597 | ||
2cafed30 DB |
598 | ns = current->nsproxy->ipc_ns; |
599 | ||
600 | switch (cmd) { | |
601 | case IPC_INFO: | |
156d9ed1 AV |
602 | case MSG_INFO: { |
603 | struct msginfo msginfo; | |
604 | err = msgctl_info(ns, msqid, cmd, &msginfo); | |
605 | if (err < 0) | |
606 | return err; | |
607 | if (copy_to_user(buf, &msginfo, sizeof(struct msginfo))) | |
608 | err = -EFAULT; | |
609 | return err; | |
610 | } | |
2cafed30 | 611 | case MSG_STAT: /* msqid is an index rather than a msg queue id */ |
23c8cec8 | 612 | case MSG_STAT_ANY: |
2cafed30 | 613 | case IPC_STAT: |
156d9ed1 AV |
614 | err = msgctl_stat(ns, msqid, cmd, &msqid64); |
615 | if (err < 0) | |
616 | return err; | |
617 | if (copy_msqid_to_user(buf, &msqid64, version)) | |
618 | err = -EFAULT; | |
619 | return err; | |
2cafed30 | 620 | case IPC_SET: |
156d9ed1 AV |
621 | if (copy_msqid_from_user(&msqid64, buf, version)) |
622 | return -EFAULT; | |
889b3317 LS |
623 | return msgctl_down(ns, msqid, cmd, &msqid64.msg_perm, |
624 | msqid64.msg_qbytes); | |
2cafed30 | 625 | case IPC_RMID: |
889b3317 | 626 | return msgctl_down(ns, msqid, cmd, NULL, 0); |
2cafed30 DB |
627 | default: |
628 | return -EINVAL; | |
629 | } | |
630 | } | |
631 | ||
e340db56 DB |
632 | SYSCALL_DEFINE3(msgctl, int, msqid, int, cmd, struct msqid_ds __user *, buf) |
633 | { | |
275f2214 | 634 | return ksys_msgctl(msqid, cmd, buf, IPC_64); |
e340db56 DB |
635 | } |
636 | ||
275f2214 AB |
637 | #ifdef CONFIG_ARCH_WANT_IPC_PARSE_VERSION |
638 | long ksys_old_msgctl(int msqid, int cmd, struct msqid_ds __user *buf) | |
639 | { | |
640 | int version = ipc_parse_version(&cmd); | |
641 | ||
642 | return ksys_msgctl(msqid, cmd, buf, version); | |
643 | } | |
644 | ||
645 | SYSCALL_DEFINE3(old_msgctl, int, msqid, int, cmd, struct msqid_ds __user *, buf) | |
646 | { | |
647 | return ksys_old_msgctl(msqid, cmd, buf); | |
648 | } | |
649 | #endif | |
650 | ||
46939168 AV |
651 | #ifdef CONFIG_COMPAT |
652 | ||
653 | struct compat_msqid_ds { | |
654 | struct compat_ipc_perm msg_perm; | |
655 | compat_uptr_t msg_first; | |
656 | compat_uptr_t msg_last; | |
9afc5eee AB |
657 | old_time32_t msg_stime; |
658 | old_time32_t msg_rtime; | |
659 | old_time32_t msg_ctime; | |
46939168 AV |
660 | compat_ulong_t msg_lcbytes; |
661 | compat_ulong_t msg_lqbytes; | |
662 | unsigned short msg_cbytes; | |
663 | unsigned short msg_qnum; | |
664 | unsigned short msg_qbytes; | |
665 | compat_ipc_pid_t msg_lspid; | |
666 | compat_ipc_pid_t msg_lrpid; | |
667 | }; | |
668 | ||
669 | static int copy_compat_msqid_from_user(struct msqid64_ds *out, void __user *buf, | |
670 | int version) | |
671 | { | |
672 | memset(out, 0, sizeof(*out)); | |
673 | if (version == IPC_64) { | |
6aa211e8 | 674 | struct compat_msqid64_ds __user *p = buf; |
28327fae | 675 | if (get_compat_ipc64_perm(&out->msg_perm, &p->msg_perm)) |
46939168 | 676 | return -EFAULT; |
46939168 AV |
677 | if (get_user(out->msg_qbytes, &p->msg_qbytes)) |
678 | return -EFAULT; | |
679 | } else { | |
6aa211e8 | 680 | struct compat_msqid_ds __user *p = buf; |
28327fae | 681 | if (get_compat_ipc_perm(&out->msg_perm, &p->msg_perm)) |
46939168 | 682 | return -EFAULT; |
46939168 AV |
683 | if (get_user(out->msg_qbytes, &p->msg_qbytes)) |
684 | return -EFAULT; | |
685 | } | |
686 | return 0; | |
687 | } | |
688 | ||
689 | static int copy_compat_msqid_to_user(void __user *buf, struct msqid64_ds *in, | |
690 | int version) | |
691 | { | |
692 | if (version == IPC_64) { | |
693 | struct compat_msqid64_ds v; | |
694 | memset(&v, 0, sizeof(v)); | |
28327fae | 695 | to_compat_ipc64_perm(&v.msg_perm, &in->msg_perm); |
c2ab975c AB |
696 | v.msg_stime = lower_32_bits(in->msg_stime); |
697 | v.msg_stime_high = upper_32_bits(in->msg_stime); | |
698 | v.msg_rtime = lower_32_bits(in->msg_rtime); | |
699 | v.msg_rtime_high = upper_32_bits(in->msg_rtime); | |
700 | v.msg_ctime = lower_32_bits(in->msg_ctime); | |
701 | v.msg_ctime_high = upper_32_bits(in->msg_ctime); | |
46939168 AV |
702 | v.msg_cbytes = in->msg_cbytes; |
703 | v.msg_qnum = in->msg_qnum; | |
704 | v.msg_qbytes = in->msg_qbytes; | |
705 | v.msg_lspid = in->msg_lspid; | |
706 | v.msg_lrpid = in->msg_lrpid; | |
707 | return copy_to_user(buf, &v, sizeof(v)); | |
708 | } else { | |
709 | struct compat_msqid_ds v; | |
710 | memset(&v, 0, sizeof(v)); | |
28327fae | 711 | to_compat_ipc_perm(&v.msg_perm, &in->msg_perm); |
46939168 AV |
712 | v.msg_stime = in->msg_stime; |
713 | v.msg_rtime = in->msg_rtime; | |
714 | v.msg_ctime = in->msg_ctime; | |
715 | v.msg_cbytes = in->msg_cbytes; | |
716 | v.msg_qnum = in->msg_qnum; | |
717 | v.msg_qbytes = in->msg_qbytes; | |
718 | v.msg_lspid = in->msg_lspid; | |
719 | v.msg_lrpid = in->msg_lrpid; | |
720 | return copy_to_user(buf, &v, sizeof(v)); | |
721 | } | |
722 | } | |
723 | ||
275f2214 | 724 | static long compat_ksys_msgctl(int msqid, int cmd, void __user *uptr, int version) |
46939168 AV |
725 | { |
726 | struct ipc_namespace *ns; | |
727 | int err; | |
728 | struct msqid64_ds msqid64; | |
46939168 AV |
729 | |
730 | ns = current->nsproxy->ipc_ns; | |
731 | ||
732 | if (msqid < 0 || cmd < 0) | |
733 | return -EINVAL; | |
734 | ||
735 | switch (cmd & (~IPC_64)) { | |
736 | case IPC_INFO: | |
737 | case MSG_INFO: { | |
738 | struct msginfo msginfo; | |
739 | err = msgctl_info(ns, msqid, cmd, &msginfo); | |
740 | if (err < 0) | |
741 | return err; | |
742 | if (copy_to_user(uptr, &msginfo, sizeof(struct msginfo))) | |
743 | err = -EFAULT; | |
744 | return err; | |
745 | } | |
746 | case IPC_STAT: | |
747 | case MSG_STAT: | |
23c8cec8 | 748 | case MSG_STAT_ANY: |
46939168 AV |
749 | err = msgctl_stat(ns, msqid, cmd, &msqid64); |
750 | if (err < 0) | |
751 | return err; | |
752 | if (copy_compat_msqid_to_user(uptr, &msqid64, version)) | |
753 | err = -EFAULT; | |
754 | return err; | |
755 | case IPC_SET: | |
756 | if (copy_compat_msqid_from_user(&msqid64, uptr, version)) | |
757 | return -EFAULT; | |
889b3317 | 758 | return msgctl_down(ns, msqid, cmd, &msqid64.msg_perm, msqid64.msg_qbytes); |
46939168 | 759 | case IPC_RMID: |
889b3317 | 760 | return msgctl_down(ns, msqid, cmd, NULL, 0); |
46939168 AV |
761 | default: |
762 | return -EINVAL; | |
763 | } | |
764 | } | |
e340db56 DB |
765 | |
766 | COMPAT_SYSCALL_DEFINE3(msgctl, int, msqid, int, cmd, void __user *, uptr) | |
767 | { | |
275f2214 | 768 | return compat_ksys_msgctl(msqid, cmd, uptr, IPC_64); |
e340db56 | 769 | } |
275f2214 AB |
770 | |
771 | #ifdef CONFIG_ARCH_WANT_COMPAT_IPC_PARSE_VERSION | |
772 | long compat_ksys_old_msgctl(int msqid, int cmd, void __user *uptr) | |
773 | { | |
774 | int version = compat_ipc_parse_version(&cmd); | |
775 | ||
776 | return compat_ksys_msgctl(msqid, cmd, uptr, version); | |
777 | } | |
778 | ||
779 | COMPAT_SYSCALL_DEFINE3(old_msgctl, int, msqid, int, cmd, void __user *, uptr) | |
780 | { | |
781 | return compat_ksys_old_msgctl(msqid, cmd, uptr); | |
782 | } | |
783 | #endif | |
46939168 AV |
784 | #endif |
785 | ||
5a06a363 | 786 | static int testmsg(struct msg_msg *msg, long type, int mode) |
1da177e4 | 787 | { |
46c0a8ca PM |
788 | switch (mode) { |
789 | case SEARCH_ANY: | |
790 | case SEARCH_NUMBER: | |
791 | return 1; | |
792 | case SEARCH_LESSEQUAL: | |
793 | if (msg->m_type <= type) | |
794 | return 1; | |
795 | break; | |
796 | case SEARCH_EQUAL: | |
797 | if (msg->m_type == type) | |
1da177e4 | 798 | return 1; |
46c0a8ca PM |
799 | break; |
800 | case SEARCH_NOTEQUAL: | |
801 | if (msg->m_type != type) | |
802 | return 1; | |
803 | break; | |
1da177e4 LT |
804 | } |
805 | return 0; | |
806 | } | |
807 | ||
ee51636c SAS |
808 | static inline int pipelined_send(struct msg_queue *msq, struct msg_msg *msg, |
809 | struct wake_q_head *wake_q) | |
1da177e4 | 810 | { |
41239fe8 | 811 | struct msg_receiver *msr, *t; |
5a06a363 | 812 | |
41239fe8 | 813 | list_for_each_entry_safe(msr, t, &msq->q_receivers, r_list) { |
5a06a363 | 814 | if (testmsg(msg, msr->r_msgtype, msr->r_mode) && |
d8c6e854 | 815 | !security_msg_queue_msgrcv(&msq->q_perm, msg, msr->r_tsk, |
5a06a363 IM |
816 | msr->r_msgtype, msr->r_mode)) { |
817 | ||
1da177e4 | 818 | list_del(&msr->r_list); |
5a06a363 | 819 | if (msr->r_maxsize < msg->m_ts) { |
ee51636c | 820 | wake_q_add(wake_q, msr->r_tsk); |
0d97a82b MS |
821 | |
822 | /* See expunge_all regarding memory barrier */ | |
823 | smp_store_release(&msr->r_msg, ERR_PTR(-E2BIG)); | |
1da177e4 | 824 | } else { |
39a4940e | 825 | ipc_update_pid(&msq->q_lrpid, task_pid(msr->r_tsk)); |
2a70b787 | 826 | msq->q_rtime = ktime_get_real_seconds(); |
5a06a363 | 827 | |
ee51636c | 828 | wake_q_add(wake_q, msr->r_tsk); |
0d97a82b MS |
829 | |
830 | /* See expunge_all regarding memory barrier */ | |
831 | smp_store_release(&msr->r_msg, msg); | |
1da177e4 LT |
832 | return 1; |
833 | } | |
834 | } | |
835 | } | |
ffa571da | 836 | |
1da177e4 LT |
837 | return 0; |
838 | } | |
839 | ||
9b1404c2 | 840 | static long do_msgsnd(int msqid, long mtype, void __user *mtext, |
651971cb | 841 | size_t msgsz, int msgflg) |
1da177e4 LT |
842 | { |
843 | struct msg_queue *msq; | |
844 | struct msg_msg *msg; | |
1da177e4 | 845 | int err; |
1e786937 | 846 | struct ipc_namespace *ns; |
194a6b5b | 847 | DEFINE_WAKE_Q(wake_q); |
1e786937 KK |
848 | |
849 | ns = current->nsproxy->ipc_ns; | |
5a06a363 | 850 | |
1e786937 | 851 | if (msgsz > ns->msg_ctlmax || (long) msgsz < 0 || msqid < 0) |
1da177e4 | 852 | return -EINVAL; |
1da177e4 LT |
853 | if (mtype < 1) |
854 | return -EINVAL; | |
855 | ||
651971cb | 856 | msg = load_msg(mtext, msgsz); |
5a06a363 | 857 | if (IS_ERR(msg)) |
1da177e4 LT |
858 | return PTR_ERR(msg); |
859 | ||
860 | msg->m_type = mtype; | |
861 | msg->m_ts = msgsz; | |
862 | ||
3dd1f784 DB |
863 | rcu_read_lock(); |
864 | msq = msq_obtain_object_check(ns, msqid); | |
023a5355 ND |
865 | if (IS_ERR(msq)) { |
866 | err = PTR_ERR(msq); | |
3dd1f784 | 867 | goto out_unlock1; |
023a5355 | 868 | } |
1da177e4 | 869 | |
bebcb928 MS |
870 | ipc_lock_object(&msq->q_perm); |
871 | ||
1da177e4 LT |
872 | for (;;) { |
873 | struct msg_sender s; | |
874 | ||
5a06a363 | 875 | err = -EACCES; |
b0e77598 | 876 | if (ipcperms(ns, &msq->q_perm, S_IWUGO)) |
bebcb928 | 877 | goto out_unlock0; |
1da177e4 | 878 | |
4271b05a | 879 | /* raced with RMID? */ |
0f3d2b01 | 880 | if (!ipc_valid_object(&msq->q_perm)) { |
4271b05a DB |
881 | err = -EIDRM; |
882 | goto out_unlock0; | |
883 | } | |
884 | ||
d8c6e854 | 885 | err = security_msg_queue_msgsnd(&msq->q_perm, msg, msgflg); |
1da177e4 | 886 | if (err) |
bebcb928 | 887 | goto out_unlock0; |
1da177e4 | 888 | |
ed27f912 | 889 | if (msg_fits_inqueue(msq, msgsz)) |
1da177e4 | 890 | break; |
1da177e4 LT |
891 | |
892 | /* queue full, wait: */ | |
5a06a363 IM |
893 | if (msgflg & IPC_NOWAIT) { |
894 | err = -EAGAIN; | |
bebcb928 | 895 | goto out_unlock0; |
1da177e4 | 896 | } |
3dd1f784 | 897 | |
ffa571da | 898 | /* enqueue the sender and prepare to block */ |
ed27f912 | 899 | ss_add(msq, &s, msgsz); |
6062a8dc | 900 | |
dba4cdd3 | 901 | if (!ipc_rcu_getref(&msq->q_perm)) { |
6062a8dc | 902 | err = -EIDRM; |
3dd1f784 | 903 | goto out_unlock0; |
6062a8dc RR |
904 | } |
905 | ||
3dd1f784 DB |
906 | ipc_unlock_object(&msq->q_perm); |
907 | rcu_read_unlock(); | |
1da177e4 LT |
908 | schedule(); |
909 | ||
3dd1f784 DB |
910 | rcu_read_lock(); |
911 | ipc_lock_object(&msq->q_perm); | |
912 | ||
dba4cdd3 | 913 | ipc_rcu_putref(&msq->q_perm, msg_rcu_free); |
0f3d2b01 RA |
914 | /* raced with RMID? */ |
915 | if (!ipc_valid_object(&msq->q_perm)) { | |
1da177e4 | 916 | err = -EIDRM; |
3dd1f784 | 917 | goto out_unlock0; |
1da177e4 LT |
918 | } |
919 | ss_del(&s); | |
5a06a363 | 920 | |
1da177e4 | 921 | if (signal_pending(current)) { |
5a06a363 | 922 | err = -ERESTARTNOHAND; |
3dd1f784 | 923 | goto out_unlock0; |
1da177e4 | 924 | } |
3dd1f784 | 925 | |
1da177e4 | 926 | } |
ed27f912 | 927 | |
39a4940e | 928 | ipc_update_pid(&msq->q_lspid, task_tgid(current)); |
2a70b787 | 929 | msq->q_stime = ktime_get_real_seconds(); |
1da177e4 | 930 | |
ee51636c | 931 | if (!pipelined_send(msq, msg, &wake_q)) { |
25985edc | 932 | /* no one is waiting for this message, enqueue it */ |
5a06a363 | 933 | list_add_tail(&msg->m_list, &msq->q_messages); |
1da177e4 LT |
934 | msq->q_cbytes += msgsz; |
935 | msq->q_qnum++; | |
3ac88a41 KK |
936 | atomic_add(msgsz, &ns->msg_bytes); |
937 | atomic_inc(&ns->msg_hdrs); | |
1da177e4 | 938 | } |
5a06a363 | 939 | |
1da177e4 LT |
940 | err = 0; |
941 | msg = NULL; | |
942 | ||
3dd1f784 DB |
943 | out_unlock0: |
944 | ipc_unlock_object(&msq->q_perm); | |
ee51636c | 945 | wake_up_q(&wake_q); |
3dd1f784 DB |
946 | out_unlock1: |
947 | rcu_read_unlock(); | |
5a06a363 | 948 | if (msg != NULL) |
1da177e4 LT |
949 | free_msg(msg); |
950 | return err; | |
951 | } | |
952 | ||
31c213f2 DB |
953 | long ksys_msgsnd(int msqid, struct msgbuf __user *msgp, size_t msgsz, |
954 | int msgflg) | |
651971cb | 955 | { |
956 | long mtype; | |
957 | ||
958 | if (get_user(mtype, &msgp->mtype)) | |
959 | return -EFAULT; | |
960 | return do_msgsnd(msqid, mtype, msgp->mtext, msgsz, msgflg); | |
961 | } | |
962 | ||
31c213f2 DB |
963 | SYSCALL_DEFINE4(msgsnd, int, msqid, struct msgbuf __user *, msgp, size_t, msgsz, |
964 | int, msgflg) | |
965 | { | |
966 | return ksys_msgsnd(msqid, msgp, msgsz, msgflg); | |
967 | } | |
968 | ||
9b1404c2 AV |
969 | #ifdef CONFIG_COMPAT |
970 | ||
971 | struct compat_msgbuf { | |
972 | compat_long_t mtype; | |
973 | char mtext[1]; | |
974 | }; | |
975 | ||
31c213f2 DB |
976 | long compat_ksys_msgsnd(int msqid, compat_uptr_t msgp, |
977 | compat_ssize_t msgsz, int msgflg) | |
9b1404c2 AV |
978 | { |
979 | struct compat_msgbuf __user *up = compat_ptr(msgp); | |
980 | compat_long_t mtype; | |
981 | ||
982 | if (get_user(mtype, &up->mtype)) | |
983 | return -EFAULT; | |
984 | return do_msgsnd(msqid, mtype, up->mtext, (ssize_t)msgsz, msgflg); | |
985 | } | |
31c213f2 DB |
986 | |
987 | COMPAT_SYSCALL_DEFINE4(msgsnd, int, msqid, compat_uptr_t, msgp, | |
988 | compat_ssize_t, msgsz, int, msgflg) | |
989 | { | |
990 | return compat_ksys_msgsnd(msqid, msgp, msgsz, msgflg); | |
991 | } | |
9b1404c2 AV |
992 | #endif |
993 | ||
5a06a363 | 994 | static inline int convert_mode(long *msgtyp, int msgflg) |
1da177e4 | 995 | { |
8ac6ed58 PH |
996 | if (msgflg & MSG_COPY) |
997 | return SEARCH_NUMBER; | |
5a06a363 | 998 | /* |
1da177e4 LT |
999 | * find message of correct type. |
1000 | * msgtyp = 0 => get first. | |
1001 | * msgtyp > 0 => get first message of matching type. | |
5a06a363 | 1002 | * msgtyp < 0 => get message with least type must be < abs(msgtype). |
1da177e4 | 1003 | */ |
5a06a363 | 1004 | if (*msgtyp == 0) |
1da177e4 | 1005 | return SEARCH_ANY; |
5a06a363 | 1006 | if (*msgtyp < 0) { |
99989835 JS |
1007 | if (*msgtyp == LONG_MIN) /* -LONG_MIN is undefined */ |
1008 | *msgtyp = LONG_MAX; | |
1009 | else | |
1010 | *msgtyp = -*msgtyp; | |
1da177e4 LT |
1011 | return SEARCH_LESSEQUAL; |
1012 | } | |
5a06a363 | 1013 | if (msgflg & MSG_EXCEPT) |
1da177e4 LT |
1014 | return SEARCH_NOTEQUAL; |
1015 | return SEARCH_EQUAL; | |
1016 | } | |
1017 | ||
f9dd87f4 SK |
1018 | static long do_msg_fill(void __user *dest, struct msg_msg *msg, size_t bufsz) |
1019 | { | |
1020 | struct msgbuf __user *msgp = dest; | |
1021 | size_t msgsz; | |
1022 | ||
1023 | if (put_user(msg->m_type, &msgp->mtype)) | |
1024 | return -EFAULT; | |
1025 | ||
1026 | msgsz = (bufsz > msg->m_ts) ? msg->m_ts : bufsz; | |
1027 | if (store_msg(msgp->mtext, msg, msgsz)) | |
1028 | return -EFAULT; | |
1029 | return msgsz; | |
1030 | } | |
1031 | ||
4a674f34 | 1032 | #ifdef CONFIG_CHECKPOINT_RESTORE |
3fcfe786 SK |
1033 | /* |
1034 | * This function creates new kernel message structure, large enough to store | |
1035 | * bufsz message bytes. | |
1036 | */ | |
8ac6ed58 | 1037 | static inline struct msg_msg *prepare_copy(void __user *buf, size_t bufsz) |
4a674f34 SK |
1038 | { |
1039 | struct msg_msg *copy; | |
1040 | ||
4a674f34 SK |
1041 | /* |
1042 | * Create dummy message to copy real message to. | |
1043 | */ | |
1044 | copy = load_msg(buf, bufsz); | |
1045 | if (!IS_ERR(copy)) | |
1046 | copy->m_ts = bufsz; | |
1047 | return copy; | |
1048 | } | |
1049 | ||
85398aa8 | 1050 | static inline void free_copy(struct msg_msg *copy) |
4a674f34 | 1051 | { |
85398aa8 | 1052 | if (copy) |
4a674f34 SK |
1053 | free_msg(copy); |
1054 | } | |
1055 | #else | |
8ac6ed58 | 1056 | static inline struct msg_msg *prepare_copy(void __user *buf, size_t bufsz) |
b30efe27 SK |
1057 | { |
1058 | return ERR_PTR(-ENOSYS); | |
1059 | } | |
1060 | ||
85398aa8 SK |
1061 | static inline void free_copy(struct msg_msg *copy) |
1062 | { | |
1063 | } | |
4a674f34 SK |
1064 | #endif |
1065 | ||
daaf74cf PH |
1066 | static struct msg_msg *find_msg(struct msg_queue *msq, long *msgtyp, int mode) |
1067 | { | |
368ae537 | 1068 | struct msg_msg *msg, *found = NULL; |
daaf74cf PH |
1069 | long count = 0; |
1070 | ||
1071 | list_for_each_entry(msg, &msq->q_messages, m_list) { | |
1072 | if (testmsg(msg, *msgtyp, mode) && | |
d8c6e854 | 1073 | !security_msg_queue_msgrcv(&msq->q_perm, msg, current, |
daaf74cf PH |
1074 | *msgtyp, mode)) { |
1075 | if (mode == SEARCH_LESSEQUAL && msg->m_type != 1) { | |
1076 | *msgtyp = msg->m_type - 1; | |
368ae537 | 1077 | found = msg; |
daaf74cf PH |
1078 | } else if (mode == SEARCH_NUMBER) { |
1079 | if (*msgtyp == count) | |
1080 | return msg; | |
1081 | } else | |
1082 | return msg; | |
1083 | count++; | |
1084 | } | |
1085 | } | |
1086 | ||
368ae537 | 1087 | return found ?: ERR_PTR(-EAGAIN); |
daaf74cf PH |
1088 | } |
1089 | ||
9b1404c2 | 1090 | static long do_msgrcv(int msqid, void __user *buf, size_t bufsz, long msgtyp, int msgflg, |
f9dd87f4 | 1091 | long (*msg_handler)(void __user *, struct msg_msg *, size_t)) |
1da177e4 | 1092 | { |
1da177e4 | 1093 | int mode; |
41a0d523 | 1094 | struct msg_queue *msq; |
1e786937 | 1095 | struct ipc_namespace *ns; |
41a0d523 | 1096 | struct msg_msg *msg, *copy = NULL; |
194a6b5b | 1097 | DEFINE_WAKE_Q(wake_q); |
1da177e4 | 1098 | |
88b9e456 PH |
1099 | ns = current->nsproxy->ipc_ns; |
1100 | ||
f9dd87f4 | 1101 | if (msqid < 0 || (long) bufsz < 0) |
1da177e4 | 1102 | return -EINVAL; |
41a0d523 | 1103 | |
4a674f34 | 1104 | if (msgflg & MSG_COPY) { |
4f87dac3 MK |
1105 | if ((msgflg & MSG_EXCEPT) || !(msgflg & IPC_NOWAIT)) |
1106 | return -EINVAL; | |
8ac6ed58 | 1107 | copy = prepare_copy(buf, min_t(size_t, bufsz, ns->msg_ctlmax)); |
4a674f34 SK |
1108 | if (IS_ERR(copy)) |
1109 | return PTR_ERR(copy); | |
1110 | } | |
5a06a363 | 1111 | mode = convert_mode(&msgtyp, msgflg); |
1da177e4 | 1112 | |
41a0d523 DB |
1113 | rcu_read_lock(); |
1114 | msq = msq_obtain_object_check(ns, msqid); | |
4a674f34 | 1115 | if (IS_ERR(msq)) { |
41a0d523 | 1116 | rcu_read_unlock(); |
85398aa8 | 1117 | free_copy(copy); |
023a5355 | 1118 | return PTR_ERR(msq); |
4a674f34 | 1119 | } |
1da177e4 LT |
1120 | |
1121 | for (;;) { | |
1122 | struct msg_receiver msr_d; | |
1da177e4 LT |
1123 | |
1124 | msg = ERR_PTR(-EACCES); | |
b0e77598 | 1125 | if (ipcperms(ns, &msq->q_perm, S_IRUGO)) |
41a0d523 | 1126 | goto out_unlock1; |
1da177e4 | 1127 | |
41a0d523 | 1128 | ipc_lock_object(&msq->q_perm); |
4271b05a DB |
1129 | |
1130 | /* raced with RMID? */ | |
0f3d2b01 | 1131 | if (!ipc_valid_object(&msq->q_perm)) { |
4271b05a DB |
1132 | msg = ERR_PTR(-EIDRM); |
1133 | goto out_unlock0; | |
1134 | } | |
1135 | ||
daaf74cf | 1136 | msg = find_msg(msq, &msgtyp, mode); |
5a06a363 IM |
1137 | if (!IS_ERR(msg)) { |
1138 | /* | |
1139 | * Found a suitable message. | |
1140 | * Unlink it from the queue. | |
1141 | */ | |
f9dd87f4 | 1142 | if ((bufsz < msg->m_ts) && !(msgflg & MSG_NOERROR)) { |
1da177e4 | 1143 | msg = ERR_PTR(-E2BIG); |
41a0d523 | 1144 | goto out_unlock0; |
1da177e4 | 1145 | } |
3fcfe786 SK |
1146 | /* |
1147 | * If we are copying, then do not unlink message and do | |
1148 | * not update queue parameters. | |
1149 | */ | |
852028af PH |
1150 | if (msgflg & MSG_COPY) { |
1151 | msg = copy_msg(msg, copy); | |
41a0d523 | 1152 | goto out_unlock0; |
852028af | 1153 | } |
41a0d523 | 1154 | |
1da177e4 LT |
1155 | list_del(&msg->m_list); |
1156 | msq->q_qnum--; | |
2a70b787 | 1157 | msq->q_rtime = ktime_get_real_seconds(); |
39a4940e | 1158 | ipc_update_pid(&msq->q_lrpid, task_tgid(current)); |
1da177e4 | 1159 | msq->q_cbytes -= msg->m_ts; |
3ac88a41 KK |
1160 | atomic_sub(msg->m_ts, &ns->msg_bytes); |
1161 | atomic_dec(&ns->msg_hdrs); | |
ed27f912 | 1162 | ss_wakeup(msq, &wake_q, false); |
41a0d523 DB |
1163 | |
1164 | goto out_unlock0; | |
1da177e4 | 1165 | } |
41a0d523 | 1166 | |
1da177e4 LT |
1167 | /* No message waiting. Wait for a message */ |
1168 | if (msgflg & IPC_NOWAIT) { | |
1169 | msg = ERR_PTR(-ENOMSG); | |
41a0d523 | 1170 | goto out_unlock0; |
1da177e4 | 1171 | } |
41a0d523 | 1172 | |
5a06a363 | 1173 | list_add_tail(&msr_d.r_list, &msq->q_receivers); |
1da177e4 LT |
1174 | msr_d.r_tsk = current; |
1175 | msr_d.r_msgtype = msgtyp; | |
1176 | msr_d.r_mode = mode; | |
5a06a363 | 1177 | if (msgflg & MSG_NOERROR) |
1da177e4 | 1178 | msr_d.r_maxsize = INT_MAX; |
5a06a363 | 1179 | else |
f9dd87f4 | 1180 | msr_d.r_maxsize = bufsz; |
0d97a82b MS |
1181 | |
1182 | /* memory barrier not require due to ipc_lock_object() */ | |
1183 | WRITE_ONCE(msr_d.r_msg, ERR_PTR(-EAGAIN)); | |
1184 | ||
1185 | /* memory barrier not required, we own ipc_lock_object() */ | |
f75a2f35 | 1186 | __set_current_state(TASK_INTERRUPTIBLE); |
1da177e4 | 1187 | |
41a0d523 DB |
1188 | ipc_unlock_object(&msq->q_perm); |
1189 | rcu_read_unlock(); | |
1da177e4 LT |
1190 | schedule(); |
1191 | ||
ee51636c SAS |
1192 | /* |
1193 | * Lockless receive, part 1: | |
1194 | * We don't hold a reference to the queue and getting a | |
1195 | * reference would defeat the idea of a lockless operation, | |
1196 | * thus the code relies on rcu to guarantee the existence of | |
1197 | * msq: | |
1da177e4 LT |
1198 | * Prior to destruction, expunge_all(-EIRDM) changes r_msg. |
1199 | * Thus if r_msg is -EAGAIN, then the queue not yet destroyed. | |
1da177e4 LT |
1200 | */ |
1201 | rcu_read_lock(); | |
1202 | ||
ee51636c SAS |
1203 | /* |
1204 | * Lockless receive, part 2: | |
1205 | * The work in pipelined_send() and expunge_all(): | |
1206 | * - Set pointer to message | |
1207 | * - Queue the receiver task for later wakeup | |
1208 | * - Wake up the process after the lock is dropped. | |
ff35e5ef | 1209 | * |
ee51636c SAS |
1210 | * Should the process wake up before this wakeup (due to a |
1211 | * signal) it will either see the message and continue ... | |
1da177e4 | 1212 | */ |
ee51636c | 1213 | msg = READ_ONCE(msr_d.r_msg); |
0d97a82b MS |
1214 | if (msg != ERR_PTR(-EAGAIN)) { |
1215 | /* see MSG_BARRIER for purpose/pairing */ | |
1216 | smp_acquire__after_ctrl_dep(); | |
1217 | ||
41a0d523 | 1218 | goto out_unlock1; |
0d97a82b | 1219 | } |
1da177e4 | 1220 | |
ee51636c SAS |
1221 | /* |
1222 | * ... or see -EAGAIN, acquire the lock to check the message | |
1223 | * again. | |
1224 | */ | |
41a0d523 | 1225 | ipc_lock_object(&msq->q_perm); |
1da177e4 | 1226 | |
0d97a82b | 1227 | msg = READ_ONCE(msr_d.r_msg); |
5a06a363 | 1228 | if (msg != ERR_PTR(-EAGAIN)) |
41a0d523 | 1229 | goto out_unlock0; |
1da177e4 LT |
1230 | |
1231 | list_del(&msr_d.r_list); | |
1232 | if (signal_pending(current)) { | |
1233 | msg = ERR_PTR(-ERESTARTNOHAND); | |
41a0d523 | 1234 | goto out_unlock0; |
1da177e4 | 1235 | } |
41a0d523 DB |
1236 | |
1237 | ipc_unlock_object(&msq->q_perm); | |
1da177e4 | 1238 | } |
41a0d523 DB |
1239 | |
1240 | out_unlock0: | |
1241 | ipc_unlock_object(&msq->q_perm); | |
e3658538 | 1242 | wake_up_q(&wake_q); |
41a0d523 DB |
1243 | out_unlock1: |
1244 | rcu_read_unlock(); | |
4a674f34 | 1245 | if (IS_ERR(msg)) { |
85398aa8 | 1246 | free_copy(copy); |
5a06a363 | 1247 | return PTR_ERR(msg); |
4a674f34 | 1248 | } |
1da177e4 | 1249 | |
f9dd87f4 | 1250 | bufsz = msg_handler(buf, msg, bufsz); |
1da177e4 | 1251 | free_msg(msg); |
5a06a363 | 1252 | |
f9dd87f4 | 1253 | return bufsz; |
1da177e4 LT |
1254 | } |
1255 | ||
078faac9 DB |
1256 | long ksys_msgrcv(int msqid, struct msgbuf __user *msgp, size_t msgsz, |
1257 | long msgtyp, int msgflg) | |
1258 | { | |
1259 | return do_msgrcv(msqid, msgp, msgsz, msgtyp, msgflg, do_msg_fill); | |
1260 | } | |
1261 | ||
e48fbb69 HC |
1262 | SYSCALL_DEFINE5(msgrcv, int, msqid, struct msgbuf __user *, msgp, size_t, msgsz, |
1263 | long, msgtyp, int, msgflg) | |
651971cb | 1264 | { |
078faac9 | 1265 | return ksys_msgrcv(msqid, msgp, msgsz, msgtyp, msgflg); |
651971cb | 1266 | } |
1267 | ||
9b1404c2 AV |
1268 | #ifdef CONFIG_COMPAT |
1269 | static long compat_do_msg_fill(void __user *dest, struct msg_msg *msg, size_t bufsz) | |
1270 | { | |
1271 | struct compat_msgbuf __user *msgp = dest; | |
1272 | size_t msgsz; | |
1273 | ||
1274 | if (put_user(msg->m_type, &msgp->mtype)) | |
1275 | return -EFAULT; | |
1276 | ||
1277 | msgsz = (bufsz > msg->m_ts) ? msg->m_ts : bufsz; | |
1278 | if (store_msg(msgp->mtext, msg, msgsz)) | |
1279 | return -EFAULT; | |
1280 | return msgsz; | |
1281 | } | |
1282 | ||
078faac9 DB |
1283 | long compat_ksys_msgrcv(int msqid, compat_uptr_t msgp, compat_ssize_t msgsz, |
1284 | compat_long_t msgtyp, int msgflg) | |
9b1404c2 AV |
1285 | { |
1286 | return do_msgrcv(msqid, compat_ptr(msgp), (ssize_t)msgsz, (long)msgtyp, | |
1287 | msgflg, compat_do_msg_fill); | |
1288 | } | |
078faac9 DB |
1289 | |
1290 | COMPAT_SYSCALL_DEFINE5(msgrcv, int, msqid, compat_uptr_t, msgp, | |
1291 | compat_ssize_t, msgsz, compat_long_t, msgtyp, | |
1292 | int, msgflg) | |
1293 | { | |
1294 | return compat_ksys_msgrcv(msqid, msgp, msgsz, msgtyp, msgflg); | |
1295 | } | |
9b1404c2 | 1296 | #endif |
3440a6bd | 1297 | |
eae04d25 | 1298 | void msg_init_ns(struct ipc_namespace *ns) |
3440a6bd DB |
1299 | { |
1300 | ns->msg_ctlmax = MSGMAX; | |
1301 | ns->msg_ctlmnb = MSGMNB; | |
0050ee05 | 1302 | ns->msg_ctlmni = MSGMNI; |
3440a6bd DB |
1303 | |
1304 | atomic_set(&ns->msg_bytes, 0); | |
1305 | atomic_set(&ns->msg_hdrs, 0); | |
eae04d25 | 1306 | ipc_init_ids(&ns->ids[IPC_MSG_IDS]); |
3440a6bd DB |
1307 | } |
1308 | ||
1309 | #ifdef CONFIG_IPC_NS | |
1310 | void msg_exit_ns(struct ipc_namespace *ns) | |
1311 | { | |
1312 | free_ipcs(ns, &msg_ids(ns), freeque); | |
1313 | idr_destroy(&ns->ids[IPC_MSG_IDS].ipcs_idr); | |
0cfb6aee | 1314 | rhashtable_destroy(&ns->ids[IPC_MSG_IDS].key_ht); |
3440a6bd DB |
1315 | } |
1316 | #endif | |
1317 | ||
1da177e4 | 1318 | #ifdef CONFIG_PROC_FS |
19b4946c | 1319 | static int sysvipc_msg_proc_show(struct seq_file *s, void *it) |
1da177e4 | 1320 | { |
39a4940e | 1321 | struct pid_namespace *pid_ns = ipc_seq_pid_ns(s); |
1efdb69b | 1322 | struct user_namespace *user_ns = seq_user_ns(s); |
ade9f91b KC |
1323 | struct kern_ipc_perm *ipcp = it; |
1324 | struct msg_queue *msq = container_of(ipcp, struct msg_queue, q_perm); | |
19b4946c | 1325 | |
7f032d6e | 1326 | seq_printf(s, |
50578ea9 | 1327 | "%10d %10d %4o %10lu %10lu %5u %5u %5u %5u %5u %5u %10llu %10llu %10llu\n", |
7f032d6e JP |
1328 | msq->q_perm.key, |
1329 | msq->q_perm.id, | |
1330 | msq->q_perm.mode, | |
1331 | msq->q_cbytes, | |
1332 | msq->q_qnum, | |
39a4940e EB |
1333 | pid_nr_ns(msq->q_lspid, pid_ns), |
1334 | pid_nr_ns(msq->q_lrpid, pid_ns), | |
7f032d6e JP |
1335 | from_kuid_munged(user_ns, msq->q_perm.uid), |
1336 | from_kgid_munged(user_ns, msq->q_perm.gid), | |
1337 | from_kuid_munged(user_ns, msq->q_perm.cuid), | |
1338 | from_kgid_munged(user_ns, msq->q_perm.cgid), | |
1339 | msq->q_stime, | |
1340 | msq->q_rtime, | |
1341 | msq->q_ctime); | |
1342 | ||
1343 | return 0; | |
1da177e4 LT |
1344 | } |
1345 | #endif | |
3440a6bd | 1346 | |
eae04d25 | 1347 | void __init msg_init(void) |
3440a6bd | 1348 | { |
eae04d25 | 1349 | msg_init_ns(&init_ipc_ns); |
3440a6bd | 1350 | |
3440a6bd DB |
1351 | ipc_init_proc_interface("sysvipc/msg", |
1352 | " key msqid perms cbytes qnum lspid lrpid uid gid cuid cgid stime rtime ctime\n", | |
1353 | IPC_MSG_IDS, sysvipc_msg_proc_show); | |
1354 | } |