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