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