blob: 7a20536c3a50698c4c88f5ae15e86003df6f75c3 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * linux/ipc/msg.c
Ingo Molnar5a06a362006-07-30 03:04:11 -07003 * Copyright (C) 1992 Krishna Balasubramanian
Linus Torvalds1da177e2005-04-16 15:20:36 -07004 *
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
Christian Kujau624dffc2006-01-15 02:43:54 +010015 * (c) 1999 Manfred Spraul <manfred@colorfullife.com>
Steve Grubb073115d2006-04-02 17:07:33 -040016 *
17 * support for audit of ipc object properties and permission changes
18 * Dustin Kirkland <dustin.kirkland@us.ibm.com>
Kirill Korotaev1e786932006-10-02 02:18:21 -070019 *
20 * namespaces support
21 * OpenVZ, SWsoft Inc.
22 * Pavel Emelianov <xemul@openvz.org>
Linus Torvalds1da177e2005-04-16 15:20:36 -070023 */
24
Randy.Dunlapc59ede72006-01-11 12:17:46 -080025#include <linux/capability.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070026#include <linux/msg.h>
27#include <linux/spinlock.h>
28#include <linux/init.h>
Nadia Derbeyf7bf3df2008-04-29 01:00:39 -070029#include <linux/mm.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070030#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>
Mike Waychison19b49462005-09-06 15:17:10 -070036#include <linux/seq_file.h>
Nadia Derbey3e148c72007-10-18 23:40:54 -070037#include <linux/rwsem.h>
Kirill Korotaev1e786932006-10-02 02:18:21 -070038#include <linux/nsproxy.h>
Pavel Emelyanovae5e1b22008-02-08 04:18:22 -080039#include <linux/ipc_namespace.h>
Ingo Molnar5f921ae2006-03-26 01:37:17 -080040
Linus Torvalds1da177e2005-04-16 15:20:36 -070041#include <asm/current.h>
42#include <asm/uaccess.h>
43#include "util.h"
44
Ingo Molnar5a06a362006-07-30 03:04:11 -070045/*
46 * one msg_receiver structure for each sleeping receiver:
47 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070048struct msg_receiver {
Ingo Molnar5a06a362006-07-30 03:04:11 -070049 struct list_head r_list;
50 struct task_struct *r_tsk;
Linus Torvalds1da177e2005-04-16 15:20:36 -070051
Ingo Molnar5a06a362006-07-30 03:04:11 -070052 int r_mode;
53 long r_msgtype;
54 long r_maxsize;
Linus Torvalds1da177e2005-04-16 15:20:36 -070055
Linus Torvalds80491eb2006-11-04 09:55:00 -080056 struct msg_msg *volatile r_msg;
Linus Torvalds1da177e2005-04-16 15:20:36 -070057};
58
59/* one msg_sender for each sleeping sender */
60struct msg_sender {
Ingo Molnar5a06a362006-07-30 03:04:11 -070061 struct list_head list;
62 struct task_struct *tsk;
Linus Torvalds1da177e2005-04-16 15:20:36 -070063};
64
65#define SEARCH_ANY 1
66#define SEARCH_EQUAL 2
67#define SEARCH_NOTEQUAL 3
68#define SEARCH_LESSEQUAL 4
69
Pierre Peiffered2ddbf2008-02-08 04:18:57 -080070#define msg_ids(ns) ((ns)->ids[IPC_MSG_IDS])
Kirill Korotaev1e786932006-10-02 02:18:21 -070071
Ingo Molnar5a06a362006-07-30 03:04:11 -070072#define msg_unlock(msq) ipc_unlock(&(msq)->q_perm)
Linus Torvalds1da177e2005-04-16 15:20:36 -070073
Pierre Peiffer01b8b072008-02-08 04:18:57 -080074static void freeque(struct ipc_namespace *, struct kern_ipc_perm *);
Nadia Derbey7748dbf2007-10-18 23:40:49 -070075static int newque(struct ipc_namespace *, struct ipc_params *);
Linus Torvalds1da177e2005-04-16 15:20:36 -070076#ifdef CONFIG_PROC_FS
Mike Waychison19b49462005-09-06 15:17:10 -070077static int sysvipc_msg_proc_show(struct seq_file *s, void *it);
Linus Torvalds1da177e2005-04-16 15:20:36 -070078#endif
79
Nadia Derbeyf7bf3df2008-04-29 01:00:39 -070080/*
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.
Nadia Derbey4d89dc62008-04-29 01:00:40 -070083 * 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.
Nadia Derbeyf7bf3df2008-04-29 01:00:39 -070085 */
Nadia Derbeyb6b337a2008-04-29 01:00:42 -070086void recompute_msgmni(struct ipc_namespace *ns)
Nadia Derbeyf7bf3df2008-04-29 01:00:39 -070087{
88 struct sysinfo i;
89 unsigned long allowed;
Nadia Derbey4d89dc62008-04-29 01:00:40 -070090 int nb_ns;
Nadia Derbeyf7bf3df2008-04-29 01:00:39 -070091
92 si_meminfo(&i);
93 allowed = (((i.totalram - i.totalhigh) / MSG_MEM_SCALE) * i.mem_unit)
94 / MSGMNB;
Nadia Derbey4d89dc62008-04-29 01:00:40 -070095 nb_ns = atomic_read(&nr_ipc_ns);
96 allowed /= nb_ns;
Nadia Derbeyf7bf3df2008-04-29 01:00:39 -070097
98 if (allowed < MSGMNI) {
99 ns->msg_ctlmni = MSGMNI;
Nadia Derbeydfcceb22008-06-05 22:46:38 -0700100 return;
Nadia Derbeyf7bf3df2008-04-29 01:00:39 -0700101 }
102
Nadia Derbey4d89dc62008-04-29 01:00:40 -0700103 if (allowed > IPCMNI / nb_ns) {
104 ns->msg_ctlmni = IPCMNI / nb_ns;
Nadia Derbeydfcceb22008-06-05 22:46:38 -0700105 return;
Nadia Derbeyf7bf3df2008-04-29 01:00:39 -0700106 }
107
108 ns->msg_ctlmni = allowed;
Nadia Derbeyf7bf3df2008-04-29 01:00:39 -0700109}
110
Pierre Peiffered2ddbf2008-02-08 04:18:57 -0800111void msg_init_ns(struct ipc_namespace *ns)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700112{
Kirill Korotaev1e786932006-10-02 02:18:21 -0700113 ns->msg_ctlmax = MSGMAX;
114 ns->msg_ctlmnb = MSGMNB;
Nadia Derbeyf7bf3df2008-04-29 01:00:39 -0700115
116 recompute_msgmni(ns);
117
Kirill Korotaev3ac88a42007-10-18 23:40:56 -0700118 atomic_set(&ns->msg_bytes, 0);
119 atomic_set(&ns->msg_hdrs, 0);
Pierre Peiffered2ddbf2008-02-08 04:18:57 -0800120 ipc_init_ids(&ns->ids[IPC_MSG_IDS]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700121}
122
Pavel Emelyanovae5e1b22008-02-08 04:18:22 -0800123#ifdef CONFIG_IPC_NS
Kirill Korotaev1e786932006-10-02 02:18:21 -0700124void msg_exit_ns(struct ipc_namespace *ns)
125{
Pierre Peiffer01b8b072008-02-08 04:18:57 -0800126 free_ipcs(ns, &msg_ids(ns), freeque);
Serge E. Hallyn7d6feeb2009-12-15 16:47:27 -0800127 idr_destroy(&ns->ids[IPC_MSG_IDS].ipcs_idr);
Kirill Korotaev1e786932006-10-02 02:18:21 -0700128}
Pavel Emelyanovae5e1b22008-02-08 04:18:22 -0800129#endif
Kirill Korotaev1e786932006-10-02 02:18:21 -0700130
131void __init msg_init(void)
132{
Pierre Peiffered2ddbf2008-02-08 04:18:57 -0800133 msg_init_ns(&init_ipc_ns);
Nadia Derbeydfcceb22008-06-05 22:46:38 -0700134
135 printk(KERN_INFO "msgmni has been set to %d\n",
136 init_ipc_ns.msg_ctlmni);
137
Kirill Korotaev1e786932006-10-02 02:18:21 -0700138 ipc_init_proc_interface("sysvipc/msg",
139 " key msqid perms cbytes qnum lspid lrpid uid gid cuid cgid stime rtime ctime\n",
140 IPC_MSG_IDS, sysvipc_msg_proc_show);
141}
142
Nadia Derbey3e148c72007-10-18 23:40:54 -0700143/*
Nadia Derbey3e148c72007-10-18 23:40:54 -0700144 * msg_lock_(check_) routines are called in the paths where the rw_mutex
145 * is not held.
146 */
Nadia Derbey023a5352007-10-18 23:40:51 -0700147static inline struct msg_queue *msg_lock(struct ipc_namespace *ns, int id)
148{
Nadia Derbey03f02c72007-10-18 23:40:51 -0700149 struct kern_ipc_perm *ipcp = ipc_lock(&msg_ids(ns), id);
150
Pierre Peifferb1ed88b2008-02-06 01:36:23 -0800151 if (IS_ERR(ipcp))
152 return (struct msg_queue *)ipcp;
153
Nadia Derbey03f02c72007-10-18 23:40:51 -0700154 return container_of(ipcp, struct msg_queue, q_perm);
Nadia Derbey023a5352007-10-18 23:40:51 -0700155}
156
157static inline struct msg_queue *msg_lock_check(struct ipc_namespace *ns,
158 int id)
159{
Nadia Derbey03f02c72007-10-18 23:40:51 -0700160 struct kern_ipc_perm *ipcp = ipc_lock_check(&msg_ids(ns), id);
161
Pierre Peifferb1ed88b2008-02-06 01:36:23 -0800162 if (IS_ERR(ipcp))
163 return (struct msg_queue *)ipcp;
164
Nadia Derbey03f02c72007-10-18 23:40:51 -0700165 return container_of(ipcp, struct msg_queue, q_perm);
Nadia Derbey023a5352007-10-18 23:40:51 -0700166}
167
Nadia Derbey7ca7e562007-10-18 23:40:48 -0700168static inline void msg_rmid(struct ipc_namespace *ns, struct msg_queue *s)
169{
170 ipc_rmid(&msg_ids(ns), &s->q_perm);
171}
172
Nadia Derbeyf4566f02007-10-18 23:40:53 -0700173/**
174 * newque - Create a new msg queue
175 * @ns: namespace
176 * @params: ptr to the structure that contains the key and msgflg
177 *
Nadia Derbey3e148c72007-10-18 23:40:54 -0700178 * Called with msg_ids.rw_mutex held (writer)
Nadia Derbeyf4566f02007-10-18 23:40:53 -0700179 */
Nadia Derbey7748dbf2007-10-18 23:40:49 -0700180static int newque(struct ipc_namespace *ns, struct ipc_params *params)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700181{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700182 struct msg_queue *msq;
Ingo Molnar5a06a362006-07-30 03:04:11 -0700183 int id, retval;
Nadia Derbey7748dbf2007-10-18 23:40:49 -0700184 key_t key = params->key;
185 int msgflg = params->flg;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700186
Ingo Molnar5a06a362006-07-30 03:04:11 -0700187 msq = ipc_rcu_alloc(sizeof(*msq));
188 if (!msq)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700189 return -ENOMEM;
190
Ingo Molnar5a06a362006-07-30 03:04:11 -0700191 msq->q_perm.mode = msgflg & S_IRWXUGO;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700192 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
Nadia Derbey7ca7e562007-10-18 23:40:48 -0700201 /*
202 * ipc_addid() locks msq
203 */
Kirill Korotaev1e786932006-10-02 02:18:21 -0700204 id = ipc_addid(&msg_ids(ns), &msq->q_perm, ns->msg_ctlmni);
Pierre Peiffer283bb7f2007-10-18 23:40:57 -0700205 if (id < 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700206 security_msg_queue_free(msq);
207 ipc_rcu_putref(msq);
Pierre Peiffer283bb7f2007-10-18 23:40:57 -0700208 return id;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700209 }
210
211 msq->q_stime = msq->q_rtime = 0;
212 msq->q_ctime = get_seconds();
213 msq->q_cbytes = msq->q_qnum = 0;
Kirill Korotaev1e786932006-10-02 02:18:21 -0700214 msq->q_qbytes = ns->msg_ctlmnb;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700215 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);
Nadia Derbey7ca7e562007-10-18 23:40:48 -0700219
Linus Torvalds1da177e2005-04-16 15:20:36 -0700220 msg_unlock(msq);
221
Nadia Derbey7ca7e562007-10-18 23:40:48 -0700222 return msq->q_perm.id;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700223}
224
Ingo Molnar5a06a362006-07-30 03:04:11 -0700225static inline void ss_add(struct msg_queue *msq, struct msg_sender *mss)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700226{
Ingo Molnar5a06a362006-07-30 03:04:11 -0700227 mss->tsk = current;
228 current->state = TASK_INTERRUPTIBLE;
229 list_add_tail(&mss->list, &msq->q_senders);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700230}
231
Ingo Molnar5a06a362006-07-30 03:04:11 -0700232static inline void ss_del(struct msg_sender *mss)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700233{
Ingo Molnar5a06a362006-07-30 03:04:11 -0700234 if (mss->list.next != NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700235 list_del(&mss->list);
236}
237
Ingo Molnar5a06a362006-07-30 03:04:11 -0700238static void ss_wakeup(struct list_head *h, int kill)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700239{
240 struct list_head *tmp;
241
242 tmp = h->next;
243 while (tmp != h) {
Ingo Molnar5a06a362006-07-30 03:04:11 -0700244 struct msg_sender *mss;
245
246 mss = list_entry(tmp, struct msg_sender, list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700247 tmp = tmp->next;
Ingo Molnar5a06a362006-07-30 03:04:11 -0700248 if (kill)
249 mss->list.next = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700250 wake_up_process(mss->tsk);
251 }
252}
253
Ingo Molnar5a06a362006-07-30 03:04:11 -0700254static void expunge_all(struct msg_queue *msq, int res)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700255{
256 struct list_head *tmp;
257
258 tmp = msq->q_receivers.next;
259 while (tmp != &msq->q_receivers) {
Ingo Molnar5a06a362006-07-30 03:04:11 -0700260 struct msg_receiver *msr;
261
262 msr = list_entry(tmp, struct msg_receiver, r_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700263 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}
Ingo Molnar5a06a362006-07-30 03:04:11 -0700270
271/*
272 * freeque() wakes up waiters on the sender and receiver waiting queue,
Nadia Derbeyf4566f02007-10-18 23:40:53 -0700273 * removes the message queue from message queue ID IDR, and cleans up all the
274 * messages associated with this queue.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700275 *
Nadia Derbey3e148c72007-10-18 23:40:54 -0700276 * 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.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700278 */
Pierre Peiffer01b8b072008-02-08 04:18:57 -0800279static void freeque(struct ipc_namespace *ns, struct kern_ipc_perm *ipcp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700280{
281 struct list_head *tmp;
Pierre Peiffer01b8b072008-02-08 04:18:57 -0800282 struct msg_queue *msq = container_of(ipcp, struct msg_queue, q_perm);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700283
Ingo Molnar5a06a362006-07-30 03:04:11 -0700284 expunge_all(msq, -EIDRM);
285 ss_wakeup(&msq->q_senders, 1);
Nadia Derbey7ca7e562007-10-18 23:40:48 -0700286 msg_rmid(ns, msq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700287 msg_unlock(msq);
Ingo Molnar5a06a362006-07-30 03:04:11 -0700288
Linus Torvalds1da177e2005-04-16 15:20:36 -0700289 tmp = msq->q_messages.next;
Ingo Molnar5a06a362006-07-30 03:04:11 -0700290 while (tmp != &msq->q_messages) {
291 struct msg_msg *msg = list_entry(tmp, struct msg_msg, m_list);
292
Linus Torvalds1da177e2005-04-16 15:20:36 -0700293 tmp = tmp->next;
Kirill Korotaev3ac88a42007-10-18 23:40:56 -0700294 atomic_dec(&ns->msg_hdrs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700295 free_msg(msg);
296 }
Kirill Korotaev3ac88a42007-10-18 23:40:56 -0700297 atomic_sub(msq->q_cbytes, &ns->msg_bytes);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700298 security_msg_queue_free(msq);
299 ipc_rcu_putref(msq);
300}
301
Nadia Derbeyf4566f02007-10-18 23:40:53 -0700302/*
Nadia Derbey3e148c72007-10-18 23:40:54 -0700303 * Called with msg_ids.rw_mutex and ipcp locked.
Nadia Derbeyf4566f02007-10-18 23:40:53 -0700304 */
Nadia Derbey03f02c72007-10-18 23:40:51 -0700305static inline int msg_security(struct kern_ipc_perm *ipcp, int msgflg)
Nadia Derbey7748dbf2007-10-18 23:40:49 -0700306{
Nadia Derbey03f02c72007-10-18 23:40:51 -0700307 struct msg_queue *msq = container_of(ipcp, struct msg_queue, q_perm);
308
309 return security_msg_queue_associate(msq, msgflg);
Nadia Derbey7748dbf2007-10-18 23:40:49 -0700310}
311
Heiko Carstense48fbb62009-01-14 14:14:26 +0100312SYSCALL_DEFINE2(msgget, key_t, key, int, msgflg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700313{
Kirill Korotaev1e786932006-10-02 02:18:21 -0700314 struct ipc_namespace *ns;
Nadia Derbey7748dbf2007-10-18 23:40:49 -0700315 struct ipc_ops msg_ops;
316 struct ipc_params msg_params;
Kirill Korotaev1e786932006-10-02 02:18:21 -0700317
318 ns = current->nsproxy->ipc_ns;
Ingo Molnar5a06a362006-07-30 03:04:11 -0700319
Nadia Derbey7748dbf2007-10-18 23:40:49 -0700320 msg_ops.getnew = newque;
321 msg_ops.associate = msg_security;
322 msg_ops.more_checks = NULL;
Nadia Derbey7ca7e562007-10-18 23:40:48 -0700323
Nadia Derbey7748dbf2007-10-18 23:40:49 -0700324 msg_params.key = key;
325 msg_params.flg = msgflg;
Nadia Derbey7ca7e562007-10-18 23:40:48 -0700326
Nadia Derbey7748dbf2007-10-18 23:40:49 -0700327 return ipcget(ns, &msg_ids(ns), &msg_ops, &msg_params);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700328}
329
Ingo Molnar5a06a362006-07-30 03:04:11 -0700330static inline unsigned long
331copy_msqid_to_user(void __user *buf, struct msqid64_ds *in, int version)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700332{
333 switch(version) {
334 case IPC_64:
Ingo Molnar5a06a362006-07-30 03:04:11 -0700335 return copy_to_user(buf, in, sizeof(*in));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700336 case IPC_OLD:
Ingo Molnar5a06a362006-07-30 03:04:11 -0700337 {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700338 struct msqid_ds out;
339
Ingo Molnar5a06a362006-07-30 03:04:11 -0700340 memset(&out, 0, sizeof(out));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700341
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
Alexey Dobriyan4be929b2010-05-24 14:33:03 -0700348 if (in->msg_cbytes > USHRT_MAX)
349 out.msg_cbytes = USHRT_MAX;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700350 else
351 out.msg_cbytes = in->msg_cbytes;
352 out.msg_lcbytes = in->msg_cbytes;
353
Alexey Dobriyan4be929b2010-05-24 14:33:03 -0700354 if (in->msg_qnum > USHRT_MAX)
355 out.msg_qnum = USHRT_MAX;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700356 else
357 out.msg_qnum = in->msg_qnum;
358
Alexey Dobriyan4be929b2010-05-24 14:33:03 -0700359 if (in->msg_qbytes > USHRT_MAX)
360 out.msg_qbytes = USHRT_MAX;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700361 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
Ingo Molnar5a06a362006-07-30 03:04:11 -0700368 return copy_to_user(buf, &out, sizeof(out));
369 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700370 default:
371 return -EINVAL;
372 }
373}
374
Ingo Molnar5a06a362006-07-30 03:04:11 -0700375static inline unsigned long
Pierre Peiffer016d7132008-04-29 01:00:50 -0700376copy_msqid_from_user(struct msqid64_ds *out, void __user *buf, int version)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700377{
378 switch(version) {
379 case IPC_64:
Pierre Peiffer016d7132008-04-29 01:00:50 -0700380 if (copy_from_user(out, buf, sizeof(*out)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700381 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700382 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700383 case IPC_OLD:
Ingo Molnar5a06a362006-07-30 03:04:11 -0700384 {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700385 struct msqid_ds tbuf_old;
386
Ingo Molnar5a06a362006-07-30 03:04:11 -0700387 if (copy_from_user(&tbuf_old, buf, sizeof(tbuf_old)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700388 return -EFAULT;
389
Pierre Peiffer016d7132008-04-29 01:00:50 -0700390 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;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700393
Ingo Molnar5a06a362006-07-30 03:04:11 -0700394 if (tbuf_old.msg_qbytes == 0)
Pierre Peiffer016d7132008-04-29 01:00:50 -0700395 out->msg_qbytes = tbuf_old.msg_lqbytes;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700396 else
Pierre Peiffer016d7132008-04-29 01:00:50 -0700397 out->msg_qbytes = tbuf_old.msg_qbytes;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700398
399 return 0;
Ingo Molnar5a06a362006-07-30 03:04:11 -0700400 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700401 default:
402 return -EINVAL;
403 }
404}
405
Pierre Peiffera0d092f2008-04-29 01:00:48 -0700406/*
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 */
411static int msgctl_down(struct ipc_namespace *ns, int msqid, int cmd,
412 struct msqid_ds __user *buf, int version)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700413{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700414 struct kern_ipc_perm *ipcp;
Felipe Contrerasf1970c42009-10-19 01:54:29 +0300415 struct msqid64_ds uninitialized_var(msqid64);
Pierre Peiffera0d092f2008-04-29 01:00:48 -0700416 struct msg_queue *msq;
417 int err;
418
419 if (cmd == IPC_SET) {
Pierre Peiffer016d7132008-04-29 01:00:50 -0700420 if (copy_msqid_from_user(&msqid64, buf, version))
Pierre Peiffera0d092f2008-04-29 01:00:48 -0700421 return -EFAULT;
422 }
423
Serge E. Hallynb0e77592011-03-23 16:43:24 -0700424 ipcp = ipcctl_pre_down(ns, &msg_ids(ns), msqid, cmd,
Pierre Peiffera5f75e72008-04-29 01:00:54 -0700425 &msqid64.msg_perm, msqid64.msg_qbytes);
426 if (IS_ERR(ipcp))
427 return PTR_ERR(ipcp);
Pierre Peiffera0d092f2008-04-29 01:00:48 -0700428
Pierre Peiffera5f75e72008-04-29 01:00:54 -0700429 msq = container_of(ipcp, struct msg_queue, q_perm);
Pierre Peiffera0d092f2008-04-29 01:00:48 -0700430
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:
Pierre Peiffer016d7132008-04-29 01:00:50 -0700440 if (msqid64.msg_qbytes > ns->msg_ctlmnb &&
Pierre Peiffera0d092f2008-04-29 01:00:48 -0700441 !capable(CAP_SYS_RESOURCE)) {
442 err = -EPERM;
443 goto out_unlock;
444 }
445
Eric W. Biederman1efdb692012-02-07 16:54:11 -0800446 err = ipc_update_perm(&msqid64.msg_perm, ipcp);
447 if (err)
448 goto out_unlock;
449
Pierre Peiffer016d7132008-04-29 01:00:50 -0700450 msq->q_qbytes = msqid64.msg_qbytes;
Pierre Peiffera0d092f2008-04-29 01:00:48 -0700451
Pierre Peiffera0d092f2008-04-29 01:00:48 -0700452 msq->q_ctime = get_seconds();
453 /* sleeping receivers might be excluded by
454 * stricter permissions.
455 */
456 expunge_all(msq, -EAGAIN);
457 /* sleeping senders might be able to send
458 * due to a larger queue size.
459 */
460 ss_wakeup(&msq->q_senders, 0);
461 break;
462 default:
463 err = -EINVAL;
464 }
465out_unlock:
466 msg_unlock(msq);
467out_up:
468 up_write(&msg_ids(ns).rw_mutex);
469 return err;
470}
471
Heiko Carstense48fbb62009-01-14 14:14:26 +0100472SYSCALL_DEFINE3(msgctl, int, msqid, int, cmd, struct msqid_ds __user *, buf)
Pierre Peiffera0d092f2008-04-29 01:00:48 -0700473{
Ingo Molnar5a06a362006-07-30 03:04:11 -0700474 struct msg_queue *msq;
475 int err, version;
Kirill Korotaev1e786932006-10-02 02:18:21 -0700476 struct ipc_namespace *ns;
Ingo Molnar5a06a362006-07-30 03:04:11 -0700477
Linus Torvalds1da177e2005-04-16 15:20:36 -0700478 if (msqid < 0 || cmd < 0)
479 return -EINVAL;
480
481 version = ipc_parse_version(&cmd);
Kirill Korotaev1e786932006-10-02 02:18:21 -0700482 ns = current->nsproxy->ipc_ns;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700483
484 switch (cmd) {
Ingo Molnar5a06a362006-07-30 03:04:11 -0700485 case IPC_INFO:
486 case MSG_INFO:
487 {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700488 struct msginfo msginfo;
489 int max_id;
Ingo Molnar5a06a362006-07-30 03:04:11 -0700490
Linus Torvalds1da177e2005-04-16 15:20:36 -0700491 if (!buf)
492 return -EFAULT;
Ingo Molnar5a06a362006-07-30 03:04:11 -0700493 /*
494 * We must not return kernel stack data.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700495 * due to padding, it's not enough
496 * to set all member fields.
497 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700498 err = security_msg_queue_msgctl(NULL, cmd);
499 if (err)
500 return err;
501
Ingo Molnar5a06a362006-07-30 03:04:11 -0700502 memset(&msginfo, 0, sizeof(msginfo));
Kirill Korotaev1e786932006-10-02 02:18:21 -0700503 msginfo.msgmni = ns->msg_ctlmni;
504 msginfo.msgmax = ns->msg_ctlmax;
505 msginfo.msgmnb = ns->msg_ctlmnb;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700506 msginfo.msgssz = MSGSSZ;
507 msginfo.msgseg = MSGSEG;
Nadia Derbey3e148c72007-10-18 23:40:54 -0700508 down_read(&msg_ids(ns).rw_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700509 if (cmd == MSG_INFO) {
Kirill Korotaev1e786932006-10-02 02:18:21 -0700510 msginfo.msgpool = msg_ids(ns).in_use;
Kirill Korotaev3ac88a42007-10-18 23:40:56 -0700511 msginfo.msgmap = atomic_read(&ns->msg_hdrs);
512 msginfo.msgtql = atomic_read(&ns->msg_bytes);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700513 } else {
514 msginfo.msgmap = MSGMAP;
515 msginfo.msgpool = MSGPOOL;
516 msginfo.msgtql = MSGTQL;
517 }
Nadia Derbey7ca7e562007-10-18 23:40:48 -0700518 max_id = ipc_get_maxid(&msg_ids(ns));
Nadia Derbey3e148c72007-10-18 23:40:54 -0700519 up_read(&msg_ids(ns).rw_mutex);
Ingo Molnar5a06a362006-07-30 03:04:11 -0700520 if (copy_to_user(buf, &msginfo, sizeof(struct msginfo)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700521 return -EFAULT;
Ingo Molnar5a06a362006-07-30 03:04:11 -0700522 return (max_id < 0) ? 0 : max_id;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700523 }
Nadia Derbey7ca7e562007-10-18 23:40:48 -0700524 case MSG_STAT: /* msqid is an index rather than a msg queue id */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700525 case IPC_STAT:
526 {
527 struct msqid64_ds tbuf;
528 int success_return;
Ingo Molnar5a06a362006-07-30 03:04:11 -0700529
Linus Torvalds1da177e2005-04-16 15:20:36 -0700530 if (!buf)
531 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700532
Ingo Molnar5a06a362006-07-30 03:04:11 -0700533 if (cmd == MSG_STAT) {
Nadia Derbey023a5352007-10-18 23:40:51 -0700534 msq = msg_lock(ns, msqid);
535 if (IS_ERR(msq))
536 return PTR_ERR(msq);
Nadia Derbey7ca7e562007-10-18 23:40:48 -0700537 success_return = msq->q_perm.id;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700538 } else {
Nadia Derbey023a5352007-10-18 23:40:51 -0700539 msq = msg_lock_check(ns, msqid);
540 if (IS_ERR(msq))
541 return PTR_ERR(msq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700542 success_return = 0;
543 }
544 err = -EACCES;
Serge E. Hallynb0e77592011-03-23 16:43:24 -0700545 if (ipcperms(ns, &msq->q_perm, S_IRUGO))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700546 goto out_unlock;
547
548 err = security_msg_queue_msgctl(msq, cmd);
549 if (err)
550 goto out_unlock;
551
Nadia Derbey023a5352007-10-18 23:40:51 -0700552 memset(&tbuf, 0, sizeof(tbuf));
553
Linus Torvalds1da177e2005-04-16 15:20:36 -0700554 kernel_to_ipc64_perm(&msq->q_perm, &tbuf.msg_perm);
555 tbuf.msg_stime = msq->q_stime;
556 tbuf.msg_rtime = msq->q_rtime;
557 tbuf.msg_ctime = msq->q_ctime;
558 tbuf.msg_cbytes = msq->q_cbytes;
559 tbuf.msg_qnum = msq->q_qnum;
560 tbuf.msg_qbytes = msq->q_qbytes;
561 tbuf.msg_lspid = msq->q_lspid;
562 tbuf.msg_lrpid = msq->q_lrpid;
563 msg_unlock(msq);
564 if (copy_msqid_to_user(buf, &tbuf, version))
565 return -EFAULT;
566 return success_return;
567 }
568 case IPC_SET:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700569 case IPC_RMID:
Pierre Peiffera0d092f2008-04-29 01:00:48 -0700570 err = msgctl_down(ns, msqid, cmd, buf, version);
571 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700572 default:
573 return -EINVAL;
574 }
575
Linus Torvalds1da177e2005-04-16 15:20:36 -0700576out_unlock:
577 msg_unlock(msq);
578 return err;
579}
580
Ingo Molnar5a06a362006-07-30 03:04:11 -0700581static int testmsg(struct msg_msg *msg, long type, int mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700582{
583 switch(mode)
584 {
585 case SEARCH_ANY:
586 return 1;
587 case SEARCH_LESSEQUAL:
Ingo Molnar5a06a362006-07-30 03:04:11 -0700588 if (msg->m_type <=type)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700589 return 1;
590 break;
591 case SEARCH_EQUAL:
Ingo Molnar5a06a362006-07-30 03:04:11 -0700592 if (msg->m_type == type)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700593 return 1;
594 break;
595 case SEARCH_NOTEQUAL:
Ingo Molnar5a06a362006-07-30 03:04:11 -0700596 if (msg->m_type != type)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700597 return 1;
598 break;
599 }
600 return 0;
601}
602
Ingo Molnar5a06a362006-07-30 03:04:11 -0700603static inline int pipelined_send(struct msg_queue *msq, struct msg_msg *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700604{
Ingo Molnar5a06a362006-07-30 03:04:11 -0700605 struct list_head *tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700606
607 tmp = msq->q_receivers.next;
608 while (tmp != &msq->q_receivers) {
Ingo Molnar5a06a362006-07-30 03:04:11 -0700609 struct msg_receiver *msr;
610
611 msr = list_entry(tmp, struct msg_receiver, r_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700612 tmp = tmp->next;
Ingo Molnar5a06a362006-07-30 03:04:11 -0700613 if (testmsg(msg, msr->r_msgtype, msr->r_mode) &&
614 !security_msg_queue_msgrcv(msq, msg, msr->r_tsk,
615 msr->r_msgtype, msr->r_mode)) {
616
Linus Torvalds1da177e2005-04-16 15:20:36 -0700617 list_del(&msr->r_list);
Ingo Molnar5a06a362006-07-30 03:04:11 -0700618 if (msr->r_maxsize < msg->m_ts) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700619 msr->r_msg = NULL;
620 wake_up_process(msr->r_tsk);
621 smp_mb();
622 msr->r_msg = ERR_PTR(-E2BIG);
623 } else {
624 msr->r_msg = NULL;
Pavel Emelyanovb4888932007-10-18 23:40:14 -0700625 msq->q_lrpid = task_pid_vnr(msr->r_tsk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700626 msq->q_rtime = get_seconds();
627 wake_up_process(msr->r_tsk);
628 smp_mb();
629 msr->r_msg = msg;
Ingo Molnar5a06a362006-07-30 03:04:11 -0700630
Linus Torvalds1da177e2005-04-16 15:20:36 -0700631 return 1;
632 }
633 }
634 }
635 return 0;
636}
637
suzuki651971c2006-12-06 20:37:48 -0800638long do_msgsnd(int msqid, long mtype, void __user *mtext,
639 size_t msgsz, int msgflg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700640{
641 struct msg_queue *msq;
642 struct msg_msg *msg;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700643 int err;
Kirill Korotaev1e786932006-10-02 02:18:21 -0700644 struct ipc_namespace *ns;
Ingo Molnar5a06a362006-07-30 03:04:11 -0700645
Kirill Korotaev1e786932006-10-02 02:18:21 -0700646 ns = current->nsproxy->ipc_ns;
647
648 if (msgsz > ns->msg_ctlmax || (long) msgsz < 0 || msqid < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700649 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700650 if (mtype < 1)
651 return -EINVAL;
652
suzuki651971c2006-12-06 20:37:48 -0800653 msg = load_msg(mtext, msgsz);
Ingo Molnar5a06a362006-07-30 03:04:11 -0700654 if (IS_ERR(msg))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700655 return PTR_ERR(msg);
656
657 msg->m_type = mtype;
658 msg->m_ts = msgsz;
659
Nadia Derbey023a5352007-10-18 23:40:51 -0700660 msq = msg_lock_check(ns, msqid);
661 if (IS_ERR(msq)) {
662 err = PTR_ERR(msq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700663 goto out_free;
Nadia Derbey023a5352007-10-18 23:40:51 -0700664 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700665
666 for (;;) {
667 struct msg_sender s;
668
Ingo Molnar5a06a362006-07-30 03:04:11 -0700669 err = -EACCES;
Serge E. Hallynb0e77592011-03-23 16:43:24 -0700670 if (ipcperms(ns, &msq->q_perm, S_IWUGO))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700671 goto out_unlock_free;
672
673 err = security_msg_queue_msgsnd(msq, msg, msgflg);
674 if (err)
675 goto out_unlock_free;
676
Ingo Molnar5a06a362006-07-30 03:04:11 -0700677 if (msgsz + msq->q_cbytes <= msq->q_qbytes &&
Linus Torvalds1da177e2005-04-16 15:20:36 -0700678 1 + msq->q_qnum <= msq->q_qbytes) {
679 break;
680 }
681
682 /* queue full, wait: */
Ingo Molnar5a06a362006-07-30 03:04:11 -0700683 if (msgflg & IPC_NOWAIT) {
684 err = -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700685 goto out_unlock_free;
686 }
687 ss_add(msq, &s);
688 ipc_rcu_getref(msq);
689 msg_unlock(msq);
690 schedule();
691
692 ipc_lock_by_ptr(&msq->q_perm);
693 ipc_rcu_putref(msq);
694 if (msq->q_perm.deleted) {
695 err = -EIDRM;
696 goto out_unlock_free;
697 }
698 ss_del(&s);
Ingo Molnar5a06a362006-07-30 03:04:11 -0700699
Linus Torvalds1da177e2005-04-16 15:20:36 -0700700 if (signal_pending(current)) {
Ingo Molnar5a06a362006-07-30 03:04:11 -0700701 err = -ERESTARTNOHAND;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700702 goto out_unlock_free;
703 }
704 }
705
Pavel Emelyanovb4888932007-10-18 23:40:14 -0700706 msq->q_lspid = task_tgid_vnr(current);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700707 msq->q_stime = get_seconds();
708
Ingo Molnar5a06a362006-07-30 03:04:11 -0700709 if (!pipelined_send(msq, msg)) {
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300710 /* no one is waiting for this message, enqueue it */
Ingo Molnar5a06a362006-07-30 03:04:11 -0700711 list_add_tail(&msg->m_list, &msq->q_messages);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700712 msq->q_cbytes += msgsz;
713 msq->q_qnum++;
Kirill Korotaev3ac88a42007-10-18 23:40:56 -0700714 atomic_add(msgsz, &ns->msg_bytes);
715 atomic_inc(&ns->msg_hdrs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700716 }
Ingo Molnar5a06a362006-07-30 03:04:11 -0700717
Linus Torvalds1da177e2005-04-16 15:20:36 -0700718 err = 0;
719 msg = NULL;
720
721out_unlock_free:
722 msg_unlock(msq);
723out_free:
Ingo Molnar5a06a362006-07-30 03:04:11 -0700724 if (msg != NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700725 free_msg(msg);
726 return err;
727}
728
Heiko Carstense48fbb62009-01-14 14:14:26 +0100729SYSCALL_DEFINE4(msgsnd, int, msqid, struct msgbuf __user *, msgp, size_t, msgsz,
730 int, msgflg)
suzuki651971c2006-12-06 20:37:48 -0800731{
732 long mtype;
733
734 if (get_user(mtype, &msgp->mtype))
735 return -EFAULT;
736 return do_msgsnd(msqid, mtype, msgp->mtext, msgsz, msgflg);
737}
738
Ingo Molnar5a06a362006-07-30 03:04:11 -0700739static inline int convert_mode(long *msgtyp, int msgflg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700740{
Ingo Molnar5a06a362006-07-30 03:04:11 -0700741 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700742 * find message of correct type.
743 * msgtyp = 0 => get first.
744 * msgtyp > 0 => get first message of matching type.
Ingo Molnar5a06a362006-07-30 03:04:11 -0700745 * msgtyp < 0 => get message with least type must be < abs(msgtype).
Linus Torvalds1da177e2005-04-16 15:20:36 -0700746 */
Ingo Molnar5a06a362006-07-30 03:04:11 -0700747 if (*msgtyp == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700748 return SEARCH_ANY;
Ingo Molnar5a06a362006-07-30 03:04:11 -0700749 if (*msgtyp < 0) {
750 *msgtyp = -*msgtyp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700751 return SEARCH_LESSEQUAL;
752 }
Ingo Molnar5a06a362006-07-30 03:04:11 -0700753 if (msgflg & MSG_EXCEPT)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700754 return SEARCH_NOTEQUAL;
755 return SEARCH_EQUAL;
756}
757
Stanislav Kinsburskyf9dd87f2013-01-04 15:34:52 -0800758static long do_msg_fill(void __user *dest, struct msg_msg *msg, size_t bufsz)
759{
760 struct msgbuf __user *msgp = dest;
761 size_t msgsz;
762
763 if (put_user(msg->m_type, &msgp->mtype))
764 return -EFAULT;
765
766 msgsz = (bufsz > msg->m_ts) ? msg->m_ts : bufsz;
767 if (store_msg(msgp->mtext, msg, msgsz))
768 return -EFAULT;
769 return msgsz;
770}
771
Stanislav Kinsbursky4a674f32013-01-04 15:34:55 -0800772#ifdef CONFIG_CHECKPOINT_RESTORE
773static inline struct msg_msg *fill_copy(unsigned long copy_nr,
774 unsigned long msg_nr,
775 struct msg_msg *msg,
776 struct msg_msg *copy)
777{
778 if (copy_nr == msg_nr)
779 return copy_msg(msg, copy);
780 return NULL;
781}
782
783static inline struct msg_msg *prepare_copy(void __user *buf, size_t bufsz,
784 int msgflg, long *msgtyp,
785 unsigned long *copy_number)
786{
787 struct msg_msg *copy;
788
789 *copy_number = *msgtyp;
790 *msgtyp = 0;
791 /*
792 * Create dummy message to copy real message to.
793 */
794 copy = load_msg(buf, bufsz);
795 if (!IS_ERR(copy))
796 copy->m_ts = bufsz;
797 return copy;
798}
799
Stanislav Kinsbursky85398aa2013-01-04 15:34:58 -0800800static inline void free_copy(struct msg_msg *copy)
Stanislav Kinsbursky4a674f32013-01-04 15:34:55 -0800801{
Stanislav Kinsbursky85398aa2013-01-04 15:34:58 -0800802 if (copy)
Stanislav Kinsbursky4a674f32013-01-04 15:34:55 -0800803 free_msg(copy);
804}
805#else
Stanislav Kinsbursky4a674f32013-01-04 15:34:55 -0800806#define prepare_copy(buf, sz, msgflg, msgtyp, copy_nr) ERR_PTR(-ENOSYS)
807#define fill_copy(copy_nr, msg_nr, msg, copy) NULL
Stanislav Kinsbursky85398aa2013-01-04 15:34:58 -0800808static inline void free_copy(struct msg_msg *copy)
809{
810}
Stanislav Kinsbursky4a674f32013-01-04 15:34:55 -0800811#endif
812
Stanislav Kinsburskyf9dd87f2013-01-04 15:34:52 -0800813long do_msgrcv(int msqid, void __user *buf, size_t bufsz, long msgtyp,
814 int msgflg,
815 long (*msg_handler)(void __user *, struct msg_msg *, size_t))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700816{
817 struct msg_queue *msq;
818 struct msg_msg *msg;
819 int mode;
Kirill Korotaev1e786932006-10-02 02:18:21 -0700820 struct ipc_namespace *ns;
Stanislav Kinsbursky85398aa2013-01-04 15:34:58 -0800821 struct msg_msg *copy = NULL;
Stanislav Kinsbursky4a674f32013-01-04 15:34:55 -0800822 unsigned long __maybe_unused copy_number;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700823
Stanislav Kinsburskyf9dd87f2013-01-04 15:34:52 -0800824 if (msqid < 0 || (long) bufsz < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700825 return -EINVAL;
Stanislav Kinsbursky4a674f32013-01-04 15:34:55 -0800826 if (msgflg & MSG_COPY) {
827 copy = prepare_copy(buf, bufsz, msgflg, &msgtyp, &copy_number);
828 if (IS_ERR(copy))
829 return PTR_ERR(copy);
830 }
Ingo Molnar5a06a362006-07-30 03:04:11 -0700831 mode = convert_mode(&msgtyp, msgflg);
Kirill Korotaev1e786932006-10-02 02:18:21 -0700832 ns = current->nsproxy->ipc_ns;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700833
Nadia Derbey023a5352007-10-18 23:40:51 -0700834 msq = msg_lock_check(ns, msqid);
Stanislav Kinsbursky4a674f32013-01-04 15:34:55 -0800835 if (IS_ERR(msq)) {
Stanislav Kinsbursky85398aa2013-01-04 15:34:58 -0800836 free_copy(copy);
Nadia Derbey023a5352007-10-18 23:40:51 -0700837 return PTR_ERR(msq);
Stanislav Kinsbursky4a674f32013-01-04 15:34:55 -0800838 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700839
840 for (;;) {
841 struct msg_receiver msr_d;
Ingo Molnar5a06a362006-07-30 03:04:11 -0700842 struct list_head *tmp;
Stanislav Kinsbursky4a674f32013-01-04 15:34:55 -0800843 long msg_counter = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700844
845 msg = ERR_PTR(-EACCES);
Serge E. Hallynb0e77592011-03-23 16:43:24 -0700846 if (ipcperms(ns, &msq->q_perm, S_IRUGO))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700847 goto out_unlock;
848
849 msg = ERR_PTR(-EAGAIN);
850 tmp = msq->q_messages.next;
851 while (tmp != &msq->q_messages) {
852 struct msg_msg *walk_msg;
Ingo Molnar5a06a362006-07-30 03:04:11 -0700853
854 walk_msg = list_entry(tmp, struct msg_msg, m_list);
855 if (testmsg(walk_msg, msgtyp, mode) &&
856 !security_msg_queue_msgrcv(msq, walk_msg, current,
857 msgtyp, mode)) {
858
Linus Torvalds1da177e2005-04-16 15:20:36 -0700859 msg = walk_msg;
Ingo Molnar5a06a362006-07-30 03:04:11 -0700860 if (mode == SEARCH_LESSEQUAL &&
861 walk_msg->m_type != 1) {
Ingo Molnar5a06a362006-07-30 03:04:11 -0700862 msgtyp = walk_msg->m_type - 1;
Stanislav Kinsbursky4a674f32013-01-04 15:34:55 -0800863 } else if (msgflg & MSG_COPY) {
864 msg = fill_copy(copy_number,
865 msg_counter,
866 walk_msg, copy);
867 if (msg)
868 break;
Stanislav Kinsbursky9afdacd2013-01-04 15:34:47 -0800869 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700870 break;
Stanislav Kinsbursky4a674f32013-01-04 15:34:55 -0800871 msg_counter++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700872 }
873 tmp = tmp->next;
874 }
Ingo Molnar5a06a362006-07-30 03:04:11 -0700875 if (!IS_ERR(msg)) {
876 /*
877 * Found a suitable message.
878 * Unlink it from the queue.
879 */
Stanislav Kinsburskyf9dd87f2013-01-04 15:34:52 -0800880 if ((bufsz < msg->m_ts) && !(msgflg & MSG_NOERROR)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700881 msg = ERR_PTR(-E2BIG);
882 goto out_unlock;
883 }
Stanislav Kinsbursky4a674f32013-01-04 15:34:55 -0800884 if (msgflg & MSG_COPY)
885 goto out_unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700886 list_del(&msg->m_list);
887 msq->q_qnum--;
888 msq->q_rtime = get_seconds();
Pavel Emelyanovb4888932007-10-18 23:40:14 -0700889 msq->q_lrpid = task_tgid_vnr(current);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700890 msq->q_cbytes -= msg->m_ts;
Kirill Korotaev3ac88a42007-10-18 23:40:56 -0700891 atomic_sub(msg->m_ts, &ns->msg_bytes);
892 atomic_dec(&ns->msg_hdrs);
Ingo Molnar5a06a362006-07-30 03:04:11 -0700893 ss_wakeup(&msq->q_senders, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700894 msg_unlock(msq);
895 break;
896 }
897 /* No message waiting. Wait for a message */
898 if (msgflg & IPC_NOWAIT) {
899 msg = ERR_PTR(-ENOMSG);
900 goto out_unlock;
901 }
Ingo Molnar5a06a362006-07-30 03:04:11 -0700902 list_add_tail(&msr_d.r_list, &msq->q_receivers);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700903 msr_d.r_tsk = current;
904 msr_d.r_msgtype = msgtyp;
905 msr_d.r_mode = mode;
Ingo Molnar5a06a362006-07-30 03:04:11 -0700906 if (msgflg & MSG_NOERROR)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700907 msr_d.r_maxsize = INT_MAX;
Ingo Molnar5a06a362006-07-30 03:04:11 -0700908 else
Stanislav Kinsburskyf9dd87f2013-01-04 15:34:52 -0800909 msr_d.r_maxsize = bufsz;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700910 msr_d.r_msg = ERR_PTR(-EAGAIN);
911 current->state = TASK_INTERRUPTIBLE;
912 msg_unlock(msq);
913
914 schedule();
915
916 /* Lockless receive, part 1:
917 * Disable preemption. We don't hold a reference to the queue
918 * and getting a reference would defeat the idea of a lockless
919 * operation, thus the code relies on rcu to guarantee the
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300920 * existence of msq:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700921 * Prior to destruction, expunge_all(-EIRDM) changes r_msg.
922 * Thus if r_msg is -EAGAIN, then the queue not yet destroyed.
923 * rcu_read_lock() prevents preemption between reading r_msg
924 * and the spin_lock() inside ipc_lock_by_ptr().
925 */
926 rcu_read_lock();
927
928 /* Lockless receive, part 2:
929 * Wait until pipelined_send or expunge_all are outside of
930 * wake_up_process(). There is a race with exit(), see
931 * ipc/mqueue.c for the details.
932 */
Ingo Molnar5a06a362006-07-30 03:04:11 -0700933 msg = (struct msg_msg*)msr_d.r_msg;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700934 while (msg == NULL) {
935 cpu_relax();
Ingo Molnar5a06a362006-07-30 03:04:11 -0700936 msg = (struct msg_msg *)msr_d.r_msg;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700937 }
938
939 /* Lockless receive, part 3:
940 * If there is a message or an error then accept it without
941 * locking.
942 */
Ingo Molnar5a06a362006-07-30 03:04:11 -0700943 if (msg != ERR_PTR(-EAGAIN)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700944 rcu_read_unlock();
945 break;
946 }
947
948 /* Lockless receive, part 3:
949 * Acquire the queue spinlock.
950 */
951 ipc_lock_by_ptr(&msq->q_perm);
952 rcu_read_unlock();
953
954 /* Lockless receive, part 4:
955 * Repeat test after acquiring the spinlock.
956 */
957 msg = (struct msg_msg*)msr_d.r_msg;
Ingo Molnar5a06a362006-07-30 03:04:11 -0700958 if (msg != ERR_PTR(-EAGAIN))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700959 goto out_unlock;
960
961 list_del(&msr_d.r_list);
962 if (signal_pending(current)) {
963 msg = ERR_PTR(-ERESTARTNOHAND);
964out_unlock:
965 msg_unlock(msq);
966 break;
967 }
968 }
Stanislav Kinsbursky4a674f32013-01-04 15:34:55 -0800969 if (IS_ERR(msg)) {
Stanislav Kinsbursky85398aa2013-01-04 15:34:58 -0800970 free_copy(copy);
Ingo Molnar5a06a362006-07-30 03:04:11 -0700971 return PTR_ERR(msg);
Stanislav Kinsbursky4a674f32013-01-04 15:34:55 -0800972 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700973
Stanislav Kinsburskyf9dd87f2013-01-04 15:34:52 -0800974 bufsz = msg_handler(buf, msg, bufsz);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700975 free_msg(msg);
Ingo Molnar5a06a362006-07-30 03:04:11 -0700976
Stanislav Kinsburskyf9dd87f2013-01-04 15:34:52 -0800977 return bufsz;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700978}
979
Heiko Carstense48fbb62009-01-14 14:14:26 +0100980SYSCALL_DEFINE5(msgrcv, int, msqid, struct msgbuf __user *, msgp, size_t, msgsz,
981 long, msgtyp, int, msgflg)
suzuki651971c2006-12-06 20:37:48 -0800982{
Stanislav Kinsburskyf9dd87f2013-01-04 15:34:52 -0800983 return do_msgrcv(msqid, msgp, msgsz, msgtyp, msgflg, do_msg_fill);
suzuki651971c2006-12-06 20:37:48 -0800984}
985
Linus Torvalds1da177e2005-04-16 15:20:36 -0700986#ifdef CONFIG_PROC_FS
Mike Waychison19b49462005-09-06 15:17:10 -0700987static int sysvipc_msg_proc_show(struct seq_file *s, void *it)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700988{
Eric W. Biederman1efdb692012-02-07 16:54:11 -0800989 struct user_namespace *user_ns = seq_user_ns(s);
Mike Waychison19b49462005-09-06 15:17:10 -0700990 struct msg_queue *msq = it;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700991
Mike Waychison19b49462005-09-06 15:17:10 -0700992 return seq_printf(s,
Ingo Molnar5a06a362006-07-30 03:04:11 -0700993 "%10d %10d %4o %10lu %10lu %5u %5u %5u %5u %5u %5u %10lu %10lu %10lu\n",
994 msq->q_perm.key,
Nadia Derbey7ca7e562007-10-18 23:40:48 -0700995 msq->q_perm.id,
Ingo Molnar5a06a362006-07-30 03:04:11 -0700996 msq->q_perm.mode,
997 msq->q_cbytes,
998 msq->q_qnum,
999 msq->q_lspid,
1000 msq->q_lrpid,
Eric W. Biederman1efdb692012-02-07 16:54:11 -08001001 from_kuid_munged(user_ns, msq->q_perm.uid),
1002 from_kgid_munged(user_ns, msq->q_perm.gid),
1003 from_kuid_munged(user_ns, msq->q_perm.cuid),
1004 from_kgid_munged(user_ns, msq->q_perm.cgid),
Ingo Molnar5a06a362006-07-30 03:04:11 -07001005 msq->q_stime,
1006 msq->q_rtime,
1007 msq->q_ctime);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001008}
1009#endif