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