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