Blame view

ipc/msg.c 30.6 KB
b24413180   Greg Kroah-Hartman   License cleanup: ...
1
  // SPDX-License-Identifier: GPL-2.0
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2
3
  /*
   * linux/ipc/msg.c
5a06a363e   Ingo Molnar   [PATCH] ipc/msg.c...
4
   * Copyright (C) 1992 Krishna Balasubramanian
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
5
6
7
8
9
10
11
12
13
14
15
   *
   * Removed all the remaining kerneld mess
   * Catch the -EFAULT stuff properly
   * Use GFP_KERNEL for messages as in 1.2
   * Fixed up the unchecked user space derefs
   * Copyright (C) 1998 Alan Cox & Andi Kleen
   *
   * /proc/sysvipc/msg support (c) 1999 Dragos Acostachioaie <dragos@iname.com>
   *
   * mostly rewritten, threaded and wake-one semantics added
   * MSGMAX limit removed, sysctl's added
624dffcbc   Christian Kujau   correct email add...
16
   * (c) 1999 Manfred Spraul <manfred@colorfullife.com>
073115d6b   Steve Grubb   [PATCH] Rework of...
17
18
19
   *
   * support for audit of ipc object properties and permission changes
   * Dustin Kirkland <dustin.kirkland@us.ibm.com>
1e7869373   Kirill Korotaev   [PATCH] IPC names...
20
21
22
23
   *
   * namespaces support
   * OpenVZ, SWsoft Inc.
   * Pavel Emelianov <xemul@openvz.org>
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
24
   */
c59ede7b7   Randy.Dunlap   [PATCH] move capa...
25
  #include <linux/capability.h>
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
26
27
28
  #include <linux/msg.h>
  #include <linux/spinlock.h>
  #include <linux/init.h>
f7bf3df8b   Nadia Derbey   ipc: scale msgmni...
29
  #include <linux/mm.h>
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
30
31
32
  #include <linux/proc_fs.h>
  #include <linux/list.h>
  #include <linux/security.h>
84f001e15   Ingo Molnar   sched/headers: Pr...
33
  #include <linux/sched/wake_q.h>
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
34
35
  #include <linux/syscalls.h>
  #include <linux/audit.h>
19b4946ca   Mike Waychison   [PATCH] ipc: conv...
36
  #include <linux/seq_file.h>
3e148c799   Nadia Derbey   fix idr_find() lo...
37
  #include <linux/rwsem.h>
1e7869373   Kirill Korotaev   [PATCH] IPC names...
38
  #include <linux/nsproxy.h>
ae5e1b22f   Pavel Emelyanov   namespaces: move ...
39
  #include <linux/ipc_namespace.h>
0eb71a9da   NeilBrown   rhashtable: split...
40
  #include <linux/rhashtable.h>
5f921ae96   Ingo Molnar   [PATCH] sem2mutex...
41

1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
42
  #include <asm/current.h>
7153e4027   Paul McQuade   ipc, kernel: use ...
43
  #include <linux/uaccess.h>
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
44
  #include "util.h"
34b56df92   Eric W. Biederman   msg: Move struct ...
45
46
47
48
49
50
51
52
53
  /* one msq_queue structure for each present queue on the system */
  struct msg_queue {
  	struct kern_ipc_perm q_perm;
  	time64_t q_stime;		/* last msgsnd time */
  	time64_t q_rtime;		/* last msgrcv time */
  	time64_t q_ctime;		/* last change time */
  	unsigned long q_cbytes;		/* current number of bytes on queue */
  	unsigned long q_qnum;		/* number of messages in queue */
  	unsigned long q_qbytes;		/* max number of bytes on queue */
39a4940ea   Eric W. Biederman   ipc/msg: Fix msgc...
54
55
  	struct pid *q_lspid;		/* pid of last msgsnd */
  	struct pid *q_lrpid;		/* last receive pid */
34b56df92   Eric W. Biederman   msg: Move struct ...
56
57
58
59
60
  
  	struct list_head q_messages;
  	struct list_head q_receivers;
  	struct list_head q_senders;
  } __randomize_layout;
4bb6657dd   Davidlohr Bueso   ipc,msg: document...
61
  /* one msg_receiver structure for each sleeping receiver */
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
62
  struct msg_receiver {
5a06a363e   Ingo Molnar   [PATCH] ipc/msg.c...
63
64
  	struct list_head	r_list;
  	struct task_struct	*r_tsk;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
65

5a06a363e   Ingo Molnar   [PATCH] ipc/msg.c...
66
67
68
  	int			r_mode;
  	long			r_msgtype;
  	long			r_maxsize;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
69

ee51636ca   Sebastian Andrzej Siewior   ipc/msg: implemen...
70
  	struct msg_msg		*r_msg;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
71
72
73
74
  };
  
  /* one msg_sender for each sleeping sender */
  struct msg_sender {
5a06a363e   Ingo Molnar   [PATCH] ipc/msg.c...
75
76
  	struct list_head	list;
  	struct task_struct	*tsk;
ed27f9122   Davidlohr Bueso   ipc/msg: avoid wa...
77
  	size_t                  msgsz;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
78
79
80
81
82
83
  };
  
  #define SEARCH_ANY		1
  #define SEARCH_EQUAL		2
  #define SEARCH_NOTEQUAL		3
  #define SEARCH_LESSEQUAL	4
8ac6ed585   Peter Hurley   ipc: implement MS...
84
  #define SEARCH_NUMBER		5
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
85

ed2ddbf88   Pierre Peiffer   IPC: make struct ...
86
  #define msg_ids(ns)	((ns)->ids[IPC_MSG_IDS])
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
87

a5001a0d9   Davidlohr Bueso   ipc,msg: introduc...
88
89
  static inline struct msg_queue *msq_obtain_object(struct ipc_namespace *ns, int id)
  {
55b7ae501   Davidlohr Bueso   ipc: rename ipc_o...
90
  	struct kern_ipc_perm *ipcp = ipc_obtain_object_idr(&msg_ids(ns), id);
a5001a0d9   Davidlohr Bueso   ipc,msg: introduc...
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
  
  	if (IS_ERR(ipcp))
  		return ERR_CAST(ipcp);
  
  	return container_of(ipcp, struct msg_queue, q_perm);
  }
  
  static inline struct msg_queue *msq_obtain_object_check(struct ipc_namespace *ns,
  							int id)
  {
  	struct kern_ipc_perm *ipcp = ipc_obtain_object_check(&msg_ids(ns), id);
  
  	if (IS_ERR(ipcp))
  		return ERR_CAST(ipcp);
  
  	return container_of(ipcp, struct msg_queue, q_perm);
  }
7ca7e564e   Nadia Derbey   ipc: store ipcs i...
108
109
110
111
  static inline void msg_rmid(struct ipc_namespace *ns, struct msg_queue *s)
  {
  	ipc_rmid(&msg_ids(ns), &s->q_perm);
  }
53dad6d3a   Davidlohr Bueso   ipc: fix race wit...
112
113
  static void msg_rcu_free(struct rcu_head *head)
  {
dba4cdd39   Manfred Spraul   ipc: merge ipc_rc...
114
115
  	struct kern_ipc_perm *p = container_of(head, struct kern_ipc_perm, rcu);
  	struct msg_queue *msq = container_of(p, struct msg_queue, q_perm);
53dad6d3a   Davidlohr Bueso   ipc: fix race wit...
116

d8c6e8543   Eric W. Biederman   msg/security: Pas...
117
  	security_msg_queue_free(&msq->q_perm);
fb259c310   Kees Cook   ipc/msg: remove s...
118
  	kvfree(msq);
52f908904   Kees Cook   ipc/msg: avoid ip...
119
  }
f4566f048   Nadia Derbey   ipc: fix wrong co...
120
121
122
123
124
  /**
   * newque - Create a new msg queue
   * @ns: namespace
   * @params: ptr to the structure that contains the key and msgflg
   *
d9a605e40   Davidlohr Bueso   ipc: rename ids->...
125
   * Called with msg_ids.rwsem held (writer)
f4566f048   Nadia Derbey   ipc: fix wrong co...
126
   */
7748dbfaa   Nadia Derbey   ipc: unify the sy...
127
  static int newque(struct ipc_namespace *ns, struct ipc_params *params)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
128
  {
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
129
  	struct msg_queue *msq;
51c23b7b7   Manfred Spraul   ipc/msg.c: avoid ...
130
  	int retval;
7748dbfaa   Nadia Derbey   ipc: unify the sy...
131
132
  	key_t key = params->key;
  	int msgflg = params->flg;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
133

fb259c310   Kees Cook   ipc/msg: remove s...
134
135
  	msq = kvmalloc(sizeof(*msq), GFP_KERNEL);
  	if (unlikely(!msq))
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
136
  		return -ENOMEM;
5a06a363e   Ingo Molnar   [PATCH] ipc/msg.c...
137
  	msq->q_perm.mode = msgflg & S_IRWXUGO;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
138
139
140
  	msq->q_perm.key = key;
  
  	msq->q_perm.security = NULL;
d8c6e8543   Eric W. Biederman   msg/security: Pas...
141
  	retval = security_msg_queue_alloc(&msq->q_perm);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
142
  	if (retval) {
fb259c310   Kees Cook   ipc/msg: remove s...
143
  		kvfree(msq);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
144
145
  		return retval;
  	}
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
146
  	msq->q_stime = msq->q_rtime = 0;
50578ea97   Deepa Dinamani   ipc: msg: Make ms...
147
  	msq->q_ctime = ktime_get_real_seconds();
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
148
  	msq->q_cbytes = msq->q_qnum = 0;
1e7869373   Kirill Korotaev   [PATCH] IPC names...
149
  	msq->q_qbytes = ns->msg_ctlmnb;
39a4940ea   Eric W. Biederman   ipc/msg: Fix msgc...
150
  	msq->q_lspid = msq->q_lrpid = NULL;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
151
152
153
  	INIT_LIST_HEAD(&msq->q_messages);
  	INIT_LIST_HEAD(&msq->q_receivers);
  	INIT_LIST_HEAD(&msq->q_senders);
7ca7e564e   Nadia Derbey   ipc: store ipcs i...
154

b9a532277   Linus Torvalds   Initialize msg/sh...
155
  	/* ipc_addid() locks msq upon success. */
51c23b7b7   Manfred Spraul   ipc/msg.c: avoid ...
156
157
  	retval = ipc_addid(&msg_ids(ns), &msq->q_perm, ns->msg_ctlmni);
  	if (retval < 0) {
39cfffd77   Manfred Spraul   ipc/util.c: use i...
158
  		ipc_rcu_putref(&msq->q_perm, msg_rcu_free);
51c23b7b7   Manfred Spraul   ipc/msg.c: avoid ...
159
  		return retval;
b9a532277   Linus Torvalds   Initialize msg/sh...
160
  	}
cf9d5d78d   Davidlohr Bueso   ipc: close open c...
161
  	ipc_unlock_object(&msq->q_perm);
dbfcd91f0   Davidlohr Bueso   ipc: move rcu loc...
162
  	rcu_read_unlock();
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
163

7ca7e564e   Nadia Derbey   ipc: store ipcs i...
164
  	return msq->q_perm.id;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
165
  }
ed27f9122   Davidlohr Bueso   ipc/msg: avoid wa...
166
167
168
169
170
171
172
173
  static inline bool msg_fits_inqueue(struct msg_queue *msq, size_t msgsz)
  {
  	return msgsz + msq->q_cbytes <= msq->q_qbytes &&
  		1 + msq->q_qnum <= msq->q_qbytes;
  }
  
  static inline void ss_add(struct msg_queue *msq,
  			  struct msg_sender *mss, size_t msgsz)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
174
  {
5a06a363e   Ingo Molnar   [PATCH] ipc/msg.c...
175
  	mss->tsk = current;
ed27f9122   Davidlohr Bueso   ipc/msg: avoid wa...
176
  	mss->msgsz = msgsz;
f75a2f358   Davidlohr Bueso   ipc,msg: use curr...
177
  	__set_current_state(TASK_INTERRUPTIBLE);
5a06a363e   Ingo Molnar   [PATCH] ipc/msg.c...
178
  	list_add_tail(&mss->list, &msq->q_senders);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
179
  }
5a06a363e   Ingo Molnar   [PATCH] ipc/msg.c...
180
  static inline void ss_del(struct msg_sender *mss)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
181
  {
ed27f9122   Davidlohr Bueso   ipc/msg: avoid wa...
182
  	if (mss->list.next)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
183
184
  		list_del(&mss->list);
  }
ed27f9122   Davidlohr Bueso   ipc/msg: avoid wa...
185
  static void ss_wakeup(struct msg_queue *msq,
d0d6a2a95   Davidlohr Bueso   ipc/msg: make ss_...
186
  		      struct wake_q_head *wake_q, bool kill)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
187
  {
41239fe82   Nikola Pajkovsky   ipc/msg.c: use li...
188
  	struct msg_sender *mss, *t;
ed27f9122   Davidlohr Bueso   ipc/msg: avoid wa...
189
190
  	struct task_struct *stop_tsk = NULL;
  	struct list_head *h = &msq->q_senders;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
191

41239fe82   Nikola Pajkovsky   ipc/msg.c: use li...
192
  	list_for_each_entry_safe(mss, t, h, list) {
5a06a363e   Ingo Molnar   [PATCH] ipc/msg.c...
193
194
  		if (kill)
  			mss->list.next = NULL;
ed27f9122   Davidlohr Bueso   ipc/msg: avoid wa...
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
  
  		/*
  		 * Stop at the first task we don't wakeup,
  		 * we've already iterated the original
  		 * sender queue.
  		 */
  		else if (stop_tsk == mss->tsk)
  			break;
  		/*
  		 * We are not in an EIDRM scenario here, therefore
  		 * verify that we really need to wakeup the task.
  		 * To maintain current semantics and wakeup order,
  		 * move the sender to the tail on behalf of the
  		 * blocked task.
  		 */
  		else if (!msg_fits_inqueue(msq, mss->msgsz)) {
  			if (!stop_tsk)
  				stop_tsk = mss->tsk;
  
  			list_move_tail(&mss->list, &msq->q_senders);
  			continue;
  		}
e3658538b   Davidlohr Bueso   ipc/msg: batch qu...
217
  		wake_q_add(wake_q, mss->tsk);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
218
219
  	}
  }
ee51636ca   Sebastian Andrzej Siewior   ipc/msg: implemen...
220
221
  static void expunge_all(struct msg_queue *msq, int res,
  			struct wake_q_head *wake_q)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
222
  {
41239fe82   Nikola Pajkovsky   ipc/msg.c: use li...
223
  	struct msg_receiver *msr, *t;
5a06a363e   Ingo Molnar   [PATCH] ipc/msg.c...
224

41239fe82   Nikola Pajkovsky   ipc/msg.c: use li...
225
  	list_for_each_entry_safe(msr, t, &msq->q_receivers, r_list) {
ee51636ca   Sebastian Andrzej Siewior   ipc/msg: implemen...
226
227
  		wake_q_add(wake_q, msr->r_tsk);
  		WRITE_ONCE(msr->r_msg, ERR_PTR(res));
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
228
229
  	}
  }
5a06a363e   Ingo Molnar   [PATCH] ipc/msg.c...
230
231
232
  
  /*
   * freeque() wakes up waiters on the sender and receiver waiting queue,
f4566f048   Nadia Derbey   ipc: fix wrong co...
233
234
   * removes the message queue from message queue ID IDR, and cleans up all the
   * messages associated with this queue.
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
235
   *
d9a605e40   Davidlohr Bueso   ipc: rename ids->...
236
237
   * msg_ids.rwsem (writer) and the spinlock for this message queue are held
   * before freeque() is called. msg_ids.rwsem remains locked on exit.
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
238
   */
01b8b07a5   Pierre Peiffer   IPC: consolidate ...
239
  static void freeque(struct ipc_namespace *ns, struct kern_ipc_perm *ipcp)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
240
  {
41239fe82   Nikola Pajkovsky   ipc/msg.c: use li...
241
  	struct msg_msg *msg, *t;
01b8b07a5   Pierre Peiffer   IPC: consolidate ...
242
  	struct msg_queue *msq = container_of(ipcp, struct msg_queue, q_perm);
194a6b5b9   Waiman Long   sched/wake_q: Ren...
243
  	DEFINE_WAKE_Q(wake_q);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
244

ee51636ca   Sebastian Andrzej Siewior   ipc/msg: implemen...
245
  	expunge_all(msq, -EIDRM, &wake_q);
ed27f9122   Davidlohr Bueso   ipc/msg: avoid wa...
246
  	ss_wakeup(msq, &wake_q, true);
7ca7e564e   Nadia Derbey   ipc: store ipcs i...
247
  	msg_rmid(ns, msq);
4718787d1   Davidlohr Bueso   ipc,msg: drop msg...
248
  	ipc_unlock_object(&msq->q_perm);
ee51636ca   Sebastian Andrzej Siewior   ipc/msg: implemen...
249
  	wake_up_q(&wake_q);
4718787d1   Davidlohr Bueso   ipc,msg: drop msg...
250
  	rcu_read_unlock();
5a06a363e   Ingo Molnar   [PATCH] ipc/msg.c...
251

41239fe82   Nikola Pajkovsky   ipc/msg.c: use li...
252
  	list_for_each_entry_safe(msg, t, &msq->q_messages, m_list) {
3ac88a41f   Kirill Korotaev   virtualization of...
253
  		atomic_dec(&ns->msg_hdrs);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
254
255
  		free_msg(msg);
  	}
3ac88a41f   Kirill Korotaev   virtualization of...
256
  	atomic_sub(msq->q_cbytes, &ns->msg_bytes);
39a4940ea   Eric W. Biederman   ipc/msg: Fix msgc...
257
258
  	ipc_update_pid(&msq->q_lspid, NULL);
  	ipc_update_pid(&msq->q_lrpid, NULL);
dba4cdd39   Manfred Spraul   ipc: merge ipc_rc...
259
  	ipc_rcu_putref(&msq->q_perm, msg_rcu_free);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
260
  }
3d65661a4   Dominik Brodowski   ipc: add msgget s...
261
  long ksys_msgget(key_t key, int msgflg)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
262
  {
1e7869373   Kirill Korotaev   [PATCH] IPC names...
263
  	struct ipc_namespace *ns;
eb66ec44f   Mathias Krause   ipc: constify ipc...
264
265
  	static const struct ipc_ops msg_ops = {
  		.getnew = newque,
50ab44b1c   Eric W. Biederman   ipc: Directly cal...
266
  		.associate = security_msg_queue_associate,
eb66ec44f   Mathias Krause   ipc: constify ipc...
267
  	};
7748dbfaa   Nadia Derbey   ipc: unify the sy...
268
  	struct ipc_params msg_params;
1e7869373   Kirill Korotaev   [PATCH] IPC names...
269
270
  
  	ns = current->nsproxy->ipc_ns;
7ca7e564e   Nadia Derbey   ipc: store ipcs i...
271

7748dbfaa   Nadia Derbey   ipc: unify the sy...
272
273
  	msg_params.key = key;
  	msg_params.flg = msgflg;
5a06a363e   Ingo Molnar   [PATCH] ipc/msg.c...
274

7748dbfaa   Nadia Derbey   ipc: unify the sy...
275
  	return ipcget(ns, &msg_ids(ns), &msg_ops, &msg_params);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
276
  }
3d65661a4   Dominik Brodowski   ipc: add msgget s...
277
278
279
280
  SYSCALL_DEFINE2(msgget, key_t, key, int, msgflg)
  {
  	return ksys_msgget(key, msgflg);
  }
5a06a363e   Ingo Molnar   [PATCH] ipc/msg.c...
281
282
  static inline unsigned long
  copy_msqid_to_user(void __user *buf, struct msqid64_ds *in, int version)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
283
  {
239521f31   Manfred Spraul   ipc: whitespace c...
284
  	switch (version) {
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
285
  	case IPC_64:
5a06a363e   Ingo Molnar   [PATCH] ipc/msg.c...
286
  		return copy_to_user(buf, in, sizeof(*in));
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
287
  	case IPC_OLD:
5a06a363e   Ingo Molnar   [PATCH] ipc/msg.c...
288
  	{
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
289
  		struct msqid_ds out;
5a06a363e   Ingo Molnar   [PATCH] ipc/msg.c...
290
  		memset(&out, 0, sizeof(out));
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
291
292
293
294
295
296
  
  		ipc64_perm_to_ipc_perm(&in->msg_perm, &out.msg_perm);
  
  		out.msg_stime		= in->msg_stime;
  		out.msg_rtime		= in->msg_rtime;
  		out.msg_ctime		= in->msg_ctime;
4be929be3   Alexey Dobriyan   kernel-wide: repl...
297
298
  		if (in->msg_cbytes > USHRT_MAX)
  			out.msg_cbytes	= USHRT_MAX;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
299
300
301
  		else
  			out.msg_cbytes	= in->msg_cbytes;
  		out.msg_lcbytes		= in->msg_cbytes;
4be929be3   Alexey Dobriyan   kernel-wide: repl...
302
303
  		if (in->msg_qnum > USHRT_MAX)
  			out.msg_qnum	= USHRT_MAX;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
304
305
  		else
  			out.msg_qnum	= in->msg_qnum;
4be929be3   Alexey Dobriyan   kernel-wide: repl...
306
307
  		if (in->msg_qbytes > USHRT_MAX)
  			out.msg_qbytes	= USHRT_MAX;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
308
309
310
311
312
313
  		else
  			out.msg_qbytes	= in->msg_qbytes;
  		out.msg_lqbytes		= in->msg_qbytes;
  
  		out.msg_lspid		= in->msg_lspid;
  		out.msg_lrpid		= in->msg_lrpid;
5a06a363e   Ingo Molnar   [PATCH] ipc/msg.c...
314
315
  		return copy_to_user(buf, &out, sizeof(out));
  	}
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
316
317
318
319
  	default:
  		return -EINVAL;
  	}
  }
5a06a363e   Ingo Molnar   [PATCH] ipc/msg.c...
320
  static inline unsigned long
016d7132f   Pierre Peiffer   IPC: get rid of t...
321
  copy_msqid_from_user(struct msqid64_ds *out, void __user *buf, int version)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
322
  {
239521f31   Manfred Spraul   ipc: whitespace c...
323
  	switch (version) {
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
324
  	case IPC_64:
016d7132f   Pierre Peiffer   IPC: get rid of t...
325
  		if (copy_from_user(out, buf, sizeof(*out)))
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
326
  			return -EFAULT;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
327
  		return 0;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
328
  	case IPC_OLD:
5a06a363e   Ingo Molnar   [PATCH] ipc/msg.c...
329
  	{
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
330
  		struct msqid_ds tbuf_old;
5a06a363e   Ingo Molnar   [PATCH] ipc/msg.c...
331
  		if (copy_from_user(&tbuf_old, buf, sizeof(tbuf_old)))
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
332
  			return -EFAULT;
239521f31   Manfred Spraul   ipc: whitespace c...
333
334
335
  		out->msg_perm.uid	= tbuf_old.msg_perm.uid;
  		out->msg_perm.gid	= tbuf_old.msg_perm.gid;
  		out->msg_perm.mode	= tbuf_old.msg_perm.mode;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
336

5a06a363e   Ingo Molnar   [PATCH] ipc/msg.c...
337
  		if (tbuf_old.msg_qbytes == 0)
016d7132f   Pierre Peiffer   IPC: get rid of t...
338
  			out->msg_qbytes	= tbuf_old.msg_lqbytes;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
339
  		else
016d7132f   Pierre Peiffer   IPC: get rid of t...
340
  			out->msg_qbytes	= tbuf_old.msg_qbytes;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
341
342
  
  		return 0;
5a06a363e   Ingo Molnar   [PATCH] ipc/msg.c...
343
  	}
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
344
345
346
347
  	default:
  		return -EINVAL;
  	}
  }
a0d092fc2   Pierre Peiffer   IPC/message queue...
348
  /*
d9a605e40   Davidlohr Bueso   ipc: rename ids->...
349
   * This function handles some msgctl commands which require the rwsem
a0d092fc2   Pierre Peiffer   IPC/message queue...
350
   * to be held in write mode.
d9a605e40   Davidlohr Bueso   ipc: rename ids->...
351
   * NOTE: no locks must be held, the rwsem is taken inside this function.
a0d092fc2   Pierre Peiffer   IPC/message queue...
352
353
   */
  static int msgctl_down(struct ipc_namespace *ns, int msqid, int cmd,
59b2e64b1   Lu Shuaibing   ipc/msg.c: consol...
354
  			struct ipc64_perm *perm, int msg_qbytes)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
355
  {
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
356
  	struct kern_ipc_perm *ipcp;
a0d092fc2   Pierre Peiffer   IPC/message queue...
357
358
  	struct msg_queue *msq;
  	int err;
d9a605e40   Davidlohr Bueso   ipc: rename ids->...
359
  	down_write(&msg_ids(ns).rwsem);
7b4cc5d84   Davidlohr Bueso   ipc: move locking...
360
  	rcu_read_lock();
4241c1a30   Manfred Spraul   ipc: rename ipcct...
361
  	ipcp = ipcctl_obtain_check(ns, &msg_ids(ns), msqid, cmd,
59b2e64b1   Lu Shuaibing   ipc/msg.c: consol...
362
  				      perm, msg_qbytes);
7b4cc5d84   Davidlohr Bueso   ipc: move locking...
363
364
  	if (IS_ERR(ipcp)) {
  		err = PTR_ERR(ipcp);
7b4cc5d84   Davidlohr Bueso   ipc: move locking...
365
366
  		goto out_unlock1;
  	}
a0d092fc2   Pierre Peiffer   IPC/message queue...
367

a5f75e7f2   Pierre Peiffer   IPC: consolidate ...
368
  	msq = container_of(ipcp, struct msg_queue, q_perm);
a0d092fc2   Pierre Peiffer   IPC/message queue...
369

d8c6e8543   Eric W. Biederman   msg/security: Pas...
370
  	err = security_msg_queue_msgctl(&msq->q_perm, cmd);
a0d092fc2   Pierre Peiffer   IPC/message queue...
371
  	if (err)
15724ecb7   Davidlohr Bueso   ipc,msg: shorten ...
372
  		goto out_unlock1;
a0d092fc2   Pierre Peiffer   IPC/message queue...
373
374
375
  
  	switch (cmd) {
  	case IPC_RMID:
15724ecb7   Davidlohr Bueso   ipc,msg: shorten ...
376
  		ipc_lock_object(&msq->q_perm);
7b4cc5d84   Davidlohr Bueso   ipc: move locking...
377
  		/* freeque unlocks the ipc object and rcu */
a0d092fc2   Pierre Peiffer   IPC/message queue...
378
379
380
  		freeque(ns, ipcp);
  		goto out_up;
  	case IPC_SET:
e3658538b   Davidlohr Bueso   ipc/msg: batch qu...
381
  	{
194a6b5b9   Waiman Long   sched/wake_q: Ren...
382
  		DEFINE_WAKE_Q(wake_q);
e3658538b   Davidlohr Bueso   ipc/msg: batch qu...
383

59b2e64b1   Lu Shuaibing   ipc/msg.c: consol...
384
  		if (msg_qbytes > ns->msg_ctlmnb &&
a0d092fc2   Pierre Peiffer   IPC/message queue...
385
386
  		    !capable(CAP_SYS_RESOURCE)) {
  			err = -EPERM;
15724ecb7   Davidlohr Bueso   ipc,msg: shorten ...
387
  			goto out_unlock1;
a0d092fc2   Pierre Peiffer   IPC/message queue...
388
  		}
15724ecb7   Davidlohr Bueso   ipc,msg: shorten ...
389
  		ipc_lock_object(&msq->q_perm);
59b2e64b1   Lu Shuaibing   ipc/msg.c: consol...
390
  		err = ipc_update_perm(perm, ipcp);
1efdb69b0   Eric W. Biederman   userns: Convert i...
391
  		if (err)
7b4cc5d84   Davidlohr Bueso   ipc: move locking...
392
  			goto out_unlock0;
1efdb69b0   Eric W. Biederman   userns: Convert i...
393

59b2e64b1   Lu Shuaibing   ipc/msg.c: consol...
394
  		msq->q_qbytes = msg_qbytes;
a0d092fc2   Pierre Peiffer   IPC/message queue...
395

50578ea97   Deepa Dinamani   ipc: msg: Make ms...
396
  		msq->q_ctime = ktime_get_real_seconds();
e3658538b   Davidlohr Bueso   ipc/msg: batch qu...
397
398
  		/*
  		 * Sleeping receivers might be excluded by
a0d092fc2   Pierre Peiffer   IPC/message queue...
399
400
  		 * stricter permissions.
  		 */
ee51636ca   Sebastian Andrzej Siewior   ipc/msg: implemen...
401
  		expunge_all(msq, -EAGAIN, &wake_q);
e3658538b   Davidlohr Bueso   ipc/msg: batch qu...
402
403
  		/*
  		 * Sleeping senders might be able to send
a0d092fc2   Pierre Peiffer   IPC/message queue...
404
405
  		 * due to a larger queue size.
  		 */
ed27f9122   Davidlohr Bueso   ipc/msg: avoid wa...
406
  		ss_wakeup(msq, &wake_q, false);
e3658538b   Davidlohr Bueso   ipc/msg: batch qu...
407
408
409
410
411
  		ipc_unlock_object(&msq->q_perm);
  		wake_up_q(&wake_q);
  
  		goto out_unlock1;
  	}
a0d092fc2   Pierre Peiffer   IPC/message queue...
412
413
  	default:
  		err = -EINVAL;
15724ecb7   Davidlohr Bueso   ipc,msg: shorten ...
414
  		goto out_unlock1;
a0d092fc2   Pierre Peiffer   IPC/message queue...
415
  	}
7b4cc5d84   Davidlohr Bueso   ipc: move locking...
416
417
418
419
420
  
  out_unlock0:
  	ipc_unlock_object(&msq->q_perm);
  out_unlock1:
  	rcu_read_unlock();
a0d092fc2   Pierre Peiffer   IPC/message queue...
421
  out_up:
d9a605e40   Davidlohr Bueso   ipc: rename ids->...
422
  	up_write(&msg_ids(ns).rwsem);
a0d092fc2   Pierre Peiffer   IPC/message queue...
423
424
  	return err;
  }
156d9ed12   Al Viro   msgctl(): split t...
425
426
  static int msgctl_info(struct ipc_namespace *ns, int msqid,
  			 int cmd, struct msginfo *msginfo)
a0d092fc2   Pierre Peiffer   IPC/message queue...
427
  {
2cafed30f   Davidlohr Bueso   ipc,msg: introduc...
428
  	int err;
27c331a17   Manfred Spraul   ipc/util.c: furth...
429
  	int max_idx;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
430

156d9ed12   Al Viro   msgctl(): split t...
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
  	/*
  	 * We must not return kernel stack data.
  	 * due to padding, it's not enough
  	 * to set all member fields.
  	 */
  	err = security_msg_queue_msgctl(NULL, cmd);
  	if (err)
  		return err;
  
  	memset(msginfo, 0, sizeof(*msginfo));
  	msginfo->msgmni = ns->msg_ctlmni;
  	msginfo->msgmax = ns->msg_ctlmax;
  	msginfo->msgmnb = ns->msg_ctlmnb;
  	msginfo->msgssz = MSGSSZ;
  	msginfo->msgseg = MSGSEG;
  	down_read(&msg_ids(ns).rwsem);
  	if (cmd == MSG_INFO) {
  		msginfo->msgpool = msg_ids(ns).in_use;
  		msginfo->msgmap = atomic_read(&ns->msg_hdrs);
  		msginfo->msgtql = atomic_read(&ns->msg_bytes);
  	} else {
  		msginfo->msgmap = MSGMAP;
  		msginfo->msgpool = MSGPOOL;
  		msginfo->msgtql = MSGTQL;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
455
  	}
27c331a17   Manfred Spraul   ipc/util.c: furth...
456
  	max_idx = ipc_get_maxidx(&msg_ids(ns));
156d9ed12   Al Viro   msgctl(): split t...
457
  	up_read(&msg_ids(ns).rwsem);
27c331a17   Manfred Spraul   ipc/util.c: furth...
458
  	return (max_idx < 0) ? 0 : max_idx;
156d9ed12   Al Viro   msgctl(): split t...
459
  }
2cafed30f   Davidlohr Bueso   ipc,msg: introduc...
460

156d9ed12   Al Viro   msgctl(): split t...
461
462
463
  static int msgctl_stat(struct ipc_namespace *ns, int msqid,
  			 int cmd, struct msqid64_ds *p)
  {
156d9ed12   Al Viro   msgctl(): split t...
464
  	struct msg_queue *msq;
87ad4b0d8   Philippe Mikoyan   ipc: fix ipc data...
465
  	int err;
ac0ba20ea   Davidlohr Bueso   ipc,msg: make msg...
466

156d9ed12   Al Viro   msgctl(): split t...
467
  	memset(p, 0, sizeof(*p));
ac0ba20ea   Davidlohr Bueso   ipc,msg: make msg...
468

156d9ed12   Al Viro   msgctl(): split t...
469
  	rcu_read_lock();
23c8cec8c   Davidlohr Bueso   ipc/msg: introduc...
470
  	if (cmd == MSG_STAT || cmd == MSG_STAT_ANY) {
156d9ed12   Al Viro   msgctl(): split t...
471
472
473
  		msq = msq_obtain_object(ns, msqid);
  		if (IS_ERR(msq)) {
  			err = PTR_ERR(msq);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
474
  			goto out_unlock;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
475
  		}
23c8cec8c   Davidlohr Bueso   ipc/msg: introduc...
476
  	} else { /* IPC_STAT */
156d9ed12   Al Viro   msgctl(): split t...
477
478
479
  		msq = msq_obtain_object_check(ns, msqid);
  		if (IS_ERR(msq)) {
  			err = PTR_ERR(msq);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
480
  			goto out_unlock;
156d9ed12   Al Viro   msgctl(): split t...
481
  		}
156d9ed12   Al Viro   msgctl(): split t...
482
  	}
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
483

23c8cec8c   Davidlohr Bueso   ipc/msg: introduc...
484
485
486
487
488
489
490
491
  	/* see comment for SHM_STAT_ANY */
  	if (cmd == MSG_STAT_ANY)
  		audit_ipc_obj(&msq->q_perm);
  	else {
  		err = -EACCES;
  		if (ipcperms(ns, &msq->q_perm, S_IRUGO))
  			goto out_unlock;
  	}
ac0ba20ea   Davidlohr Bueso   ipc,msg: make msg...
492

d8c6e8543   Eric W. Biederman   msg/security: Pas...
493
  	err = security_msg_queue_msgctl(&msq->q_perm, cmd);
156d9ed12   Al Viro   msgctl(): split t...
494
495
  	if (err)
  		goto out_unlock;
87ad4b0d8   Philippe Mikoyan   ipc: fix ipc data...
496
497
498
499
500
501
502
  	ipc_lock_object(&msq->q_perm);
  
  	if (!ipc_valid_object(&msq->q_perm)) {
  		ipc_unlock_object(&msq->q_perm);
  		err = -EIDRM;
  		goto out_unlock;
  	}
156d9ed12   Al Viro   msgctl(): split t...
503
504
505
506
  	kernel_to_ipc64_perm(&msq->q_perm, &p->msg_perm);
  	p->msg_stime  = msq->q_stime;
  	p->msg_rtime  = msq->q_rtime;
  	p->msg_ctime  = msq->q_ctime;
c2ab975c3   Arnd Bergmann   y2038: ipc: Repor...
507
508
509
510
511
  #ifndef CONFIG_64BIT
  	p->msg_stime_high = msq->q_stime >> 32;
  	p->msg_rtime_high = msq->q_rtime >> 32;
  	p->msg_ctime_high = msq->q_ctime >> 32;
  #endif
156d9ed12   Al Viro   msgctl(): split t...
512
513
514
  	p->msg_cbytes = msq->q_cbytes;
  	p->msg_qnum   = msq->q_qnum;
  	p->msg_qbytes = msq->q_qbytes;
39a4940ea   Eric W. Biederman   ipc/msg: Fix msgc...
515
516
  	p->msg_lspid  = pid_vnr(msq->q_lspid);
  	p->msg_lrpid  = pid_vnr(msq->q_lrpid);
2cafed30f   Davidlohr Bueso   ipc,msg: introduc...
517

615c999cd   Manfred Spraul   ipc: compute kern...
518
519
520
521
522
523
524
525
526
527
528
529
530
  	if (cmd == IPC_STAT) {
  		/*
  		 * As defined in SUS:
  		 * Return 0 on success
  		 */
  		err = 0;
  	} else {
  		/*
  		 * MSG_STAT and MSG_STAT_ANY (both Linux specific)
  		 * Return the full id, including the sequence number
  		 */
  		err = msq->q_perm.id;
  	}
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
531

615c999cd   Manfred Spraul   ipc: compute kern...
532
  	ipc_unlock_object(&msq->q_perm);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
533
  out_unlock:
ac0ba20ea   Davidlohr Bueso   ipc,msg: make msg...
534
  	rcu_read_unlock();
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
535
536
  	return err;
  }
275f22148   Arnd Bergmann   ipc: rename old-s...
537
  static long ksys_msgctl(int msqid, int cmd, struct msqid_ds __user *buf, int version)
2cafed30f   Davidlohr Bueso   ipc,msg: introduc...
538
  {
2cafed30f   Davidlohr Bueso   ipc,msg: introduc...
539
  	struct ipc_namespace *ns;
156d9ed12   Al Viro   msgctl(): split t...
540
541
  	struct msqid64_ds msqid64;
  	int err;
2cafed30f   Davidlohr Bueso   ipc,msg: introduc...
542
543
544
  
  	if (msqid < 0 || cmd < 0)
  		return -EINVAL;
2cafed30f   Davidlohr Bueso   ipc,msg: introduc...
545
546
547
548
  	ns = current->nsproxy->ipc_ns;
  
  	switch (cmd) {
  	case IPC_INFO:
156d9ed12   Al Viro   msgctl(): split t...
549
550
551
552
553
554
555
556
557
  	case MSG_INFO: {
  		struct msginfo msginfo;
  		err = msgctl_info(ns, msqid, cmd, &msginfo);
  		if (err < 0)
  			return err;
  		if (copy_to_user(buf, &msginfo, sizeof(struct msginfo)))
  			err = -EFAULT;
  		return err;
  	}
2cafed30f   Davidlohr Bueso   ipc,msg: introduc...
558
  	case MSG_STAT:	/* msqid is an index rather than a msg queue id */
23c8cec8c   Davidlohr Bueso   ipc/msg: introduc...
559
  	case MSG_STAT_ANY:
2cafed30f   Davidlohr Bueso   ipc,msg: introduc...
560
  	case IPC_STAT:
156d9ed12   Al Viro   msgctl(): split t...
561
562
563
564
565
566
  		err = msgctl_stat(ns, msqid, cmd, &msqid64);
  		if (err < 0)
  			return err;
  		if (copy_msqid_to_user(buf, &msqid64, version))
  			err = -EFAULT;
  		return err;
2cafed30f   Davidlohr Bueso   ipc,msg: introduc...
567
  	case IPC_SET:
156d9ed12   Al Viro   msgctl(): split t...
568
569
  		if (copy_msqid_from_user(&msqid64, buf, version))
  			return -EFAULT;
59b2e64b1   Lu Shuaibing   ipc/msg.c: consol...
570
571
  		return msgctl_down(ns, msqid, cmd, &msqid64.msg_perm,
  				   msqid64.msg_qbytes);
2cafed30f   Davidlohr Bueso   ipc,msg: introduc...
572
  	case IPC_RMID:
59b2e64b1   Lu Shuaibing   ipc/msg.c: consol...
573
  		return msgctl_down(ns, msqid, cmd, NULL, 0);
2cafed30f   Davidlohr Bueso   ipc,msg: introduc...
574
575
576
577
  	default:
  		return  -EINVAL;
  	}
  }
e340db564   Dominik Brodowski   ipc: add msgctl s...
578
579
  SYSCALL_DEFINE3(msgctl, int, msqid, int, cmd, struct msqid_ds __user *, buf)
  {
275f22148   Arnd Bergmann   ipc: rename old-s...
580
  	return ksys_msgctl(msqid, cmd, buf, IPC_64);
e340db564   Dominik Brodowski   ipc: add msgctl s...
581
  }
275f22148   Arnd Bergmann   ipc: rename old-s...
582
583
584
585
586
587
588
589
590
591
592
593
594
  #ifdef CONFIG_ARCH_WANT_IPC_PARSE_VERSION
  long ksys_old_msgctl(int msqid, int cmd, struct msqid_ds __user *buf)
  {
  	int version = ipc_parse_version(&cmd);
  
  	return ksys_msgctl(msqid, cmd, buf, version);
  }
  
  SYSCALL_DEFINE3(old_msgctl, int, msqid, int, cmd, struct msqid_ds __user *, buf)
  {
  	return ksys_old_msgctl(msqid, cmd, buf);
  }
  #endif
469391684   Al Viro   msgctl(): move co...
595
596
597
598
599
600
  #ifdef CONFIG_COMPAT
  
  struct compat_msqid_ds {
  	struct compat_ipc_perm msg_perm;
  	compat_uptr_t msg_first;
  	compat_uptr_t msg_last;
9afc5eee6   Arnd Bergmann   y2038: globally r...
601
602
603
  	old_time32_t msg_stime;
  	old_time32_t msg_rtime;
  	old_time32_t msg_ctime;
469391684   Al Viro   msgctl(): move co...
604
605
606
607
608
609
610
611
612
613
614
615
616
617
  	compat_ulong_t msg_lcbytes;
  	compat_ulong_t msg_lqbytes;
  	unsigned short msg_cbytes;
  	unsigned short msg_qnum;
  	unsigned short msg_qbytes;
  	compat_ipc_pid_t msg_lspid;
  	compat_ipc_pid_t msg_lrpid;
  };
  
  static int copy_compat_msqid_from_user(struct msqid64_ds *out, void __user *buf,
  					int version)
  {
  	memset(out, 0, sizeof(*out));
  	if (version == IPC_64) {
6aa211e8c   Linus Torvalds   fix address space...
618
  		struct compat_msqid64_ds __user *p = buf;
28327fae6   Al Viro   ipc: make use of ...
619
  		if (get_compat_ipc64_perm(&out->msg_perm, &p->msg_perm))
469391684   Al Viro   msgctl(): move co...
620
  			return -EFAULT;
469391684   Al Viro   msgctl(): move co...
621
622
623
  		if (get_user(out->msg_qbytes, &p->msg_qbytes))
  			return -EFAULT;
  	} else {
6aa211e8c   Linus Torvalds   fix address space...
624
  		struct compat_msqid_ds __user *p = buf;
28327fae6   Al Viro   ipc: make use of ...
625
  		if (get_compat_ipc_perm(&out->msg_perm, &p->msg_perm))
469391684   Al Viro   msgctl(): move co...
626
  			return -EFAULT;
469391684   Al Viro   msgctl(): move co...
627
628
629
630
631
632
633
634
635
636
637
638
  		if (get_user(out->msg_qbytes, &p->msg_qbytes))
  			return -EFAULT;
  	}
  	return 0;
  }
  
  static int copy_compat_msqid_to_user(void __user *buf, struct msqid64_ds *in,
  					int version)
  {
  	if (version == IPC_64) {
  		struct compat_msqid64_ds v;
  		memset(&v, 0, sizeof(v));
28327fae6   Al Viro   ipc: make use of ...
639
  		to_compat_ipc64_perm(&v.msg_perm, &in->msg_perm);
c2ab975c3   Arnd Bergmann   y2038: ipc: Repor...
640
641
642
643
644
645
  		v.msg_stime	 = lower_32_bits(in->msg_stime);
  		v.msg_stime_high = upper_32_bits(in->msg_stime);
  		v.msg_rtime	 = lower_32_bits(in->msg_rtime);
  		v.msg_rtime_high = upper_32_bits(in->msg_rtime);
  		v.msg_ctime	 = lower_32_bits(in->msg_ctime);
  		v.msg_ctime_high = upper_32_bits(in->msg_ctime);
469391684   Al Viro   msgctl(): move co...
646
647
648
649
650
651
652
653
654
  		v.msg_cbytes = in->msg_cbytes;
  		v.msg_qnum = in->msg_qnum;
  		v.msg_qbytes = in->msg_qbytes;
  		v.msg_lspid = in->msg_lspid;
  		v.msg_lrpid = in->msg_lrpid;
  		return copy_to_user(buf, &v, sizeof(v));
  	} else {
  		struct compat_msqid_ds v;
  		memset(&v, 0, sizeof(v));
28327fae6   Al Viro   ipc: make use of ...
655
  		to_compat_ipc_perm(&v.msg_perm, &in->msg_perm);
469391684   Al Viro   msgctl(): move co...
656
657
658
659
660
661
662
663
664
665
666
  		v.msg_stime = in->msg_stime;
  		v.msg_rtime = in->msg_rtime;
  		v.msg_ctime = in->msg_ctime;
  		v.msg_cbytes = in->msg_cbytes;
  		v.msg_qnum = in->msg_qnum;
  		v.msg_qbytes = in->msg_qbytes;
  		v.msg_lspid = in->msg_lspid;
  		v.msg_lrpid = in->msg_lrpid;
  		return copy_to_user(buf, &v, sizeof(v));
  	}
  }
275f22148   Arnd Bergmann   ipc: rename old-s...
667
  static long compat_ksys_msgctl(int msqid, int cmd, void __user *uptr, int version)
469391684   Al Viro   msgctl(): move co...
668
669
670
671
  {
  	struct ipc_namespace *ns;
  	int err;
  	struct msqid64_ds msqid64;
469391684   Al Viro   msgctl(): move co...
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
  
  	ns = current->nsproxy->ipc_ns;
  
  	if (msqid < 0 || cmd < 0)
  		return -EINVAL;
  
  	switch (cmd & (~IPC_64)) {
  	case IPC_INFO:
  	case MSG_INFO: {
  		struct msginfo msginfo;
  		err = msgctl_info(ns, msqid, cmd, &msginfo);
  		if (err < 0)
  			return err;
  		if (copy_to_user(uptr, &msginfo, sizeof(struct msginfo)))
  			err = -EFAULT;
  		return err;
  	}
  	case IPC_STAT:
  	case MSG_STAT:
23c8cec8c   Davidlohr Bueso   ipc/msg: introduc...
691
  	case MSG_STAT_ANY:
469391684   Al Viro   msgctl(): move co...
692
693
694
695
696
697
698
699
700
  		err = msgctl_stat(ns, msqid, cmd, &msqid64);
  		if (err < 0)
  			return err;
  		if (copy_compat_msqid_to_user(uptr, &msqid64, version))
  			err = -EFAULT;
  		return err;
  	case IPC_SET:
  		if (copy_compat_msqid_from_user(&msqid64, uptr, version))
  			return -EFAULT;
59b2e64b1   Lu Shuaibing   ipc/msg.c: consol...
701
  		return msgctl_down(ns, msqid, cmd, &msqid64.msg_perm, msqid64.msg_qbytes);
469391684   Al Viro   msgctl(): move co...
702
  	case IPC_RMID:
59b2e64b1   Lu Shuaibing   ipc/msg.c: consol...
703
  		return msgctl_down(ns, msqid, cmd, NULL, 0);
469391684   Al Viro   msgctl(): move co...
704
705
706
707
  	default:
  		return -EINVAL;
  	}
  }
e340db564   Dominik Brodowski   ipc: add msgctl s...
708
709
710
  
  COMPAT_SYSCALL_DEFINE3(msgctl, int, msqid, int, cmd, void __user *, uptr)
  {
275f22148   Arnd Bergmann   ipc: rename old-s...
711
  	return compat_ksys_msgctl(msqid, cmd, uptr, IPC_64);
e340db564   Dominik Brodowski   ipc: add msgctl s...
712
  }
275f22148   Arnd Bergmann   ipc: rename old-s...
713
714
715
716
717
718
719
720
721
722
723
724
725
726
  
  #ifdef CONFIG_ARCH_WANT_COMPAT_IPC_PARSE_VERSION
  long compat_ksys_old_msgctl(int msqid, int cmd, void __user *uptr)
  {
  	int version = compat_ipc_parse_version(&cmd);
  
  	return compat_ksys_msgctl(msqid, cmd, uptr, version);
  }
  
  COMPAT_SYSCALL_DEFINE3(old_msgctl, int, msqid, int, cmd, void __user *, uptr)
  {
  	return compat_ksys_old_msgctl(msqid, cmd, uptr);
  }
  #endif
469391684   Al Viro   msgctl(): move co...
727
  #endif
5a06a363e   Ingo Molnar   [PATCH] ipc/msg.c...
728
  static int testmsg(struct msg_msg *msg, long type, int mode)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
729
  {
46c0a8ca3   Paul McQuade   ipc, kernel: clea...
730
731
732
733
734
735
736
737
738
739
  	switch (mode) {
  	case SEARCH_ANY:
  	case SEARCH_NUMBER:
  		return 1;
  	case SEARCH_LESSEQUAL:
  		if (msg->m_type <= type)
  			return 1;
  		break;
  	case SEARCH_EQUAL:
  		if (msg->m_type == type)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
740
  			return 1;
46c0a8ca3   Paul McQuade   ipc, kernel: clea...
741
742
743
744
745
  		break;
  	case SEARCH_NOTEQUAL:
  		if (msg->m_type != type)
  			return 1;
  		break;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
746
747
748
  	}
  	return 0;
  }
ee51636ca   Sebastian Andrzej Siewior   ipc/msg: implemen...
749
750
  static inline int pipelined_send(struct msg_queue *msq, struct msg_msg *msg,
  				 struct wake_q_head *wake_q)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
751
  {
41239fe82   Nikola Pajkovsky   ipc/msg.c: use li...
752
  	struct msg_receiver *msr, *t;
5a06a363e   Ingo Molnar   [PATCH] ipc/msg.c...
753

41239fe82   Nikola Pajkovsky   ipc/msg.c: use li...
754
  	list_for_each_entry_safe(msr, t, &msq->q_receivers, r_list) {
5a06a363e   Ingo Molnar   [PATCH] ipc/msg.c...
755
  		if (testmsg(msg, msr->r_msgtype, msr->r_mode) &&
d8c6e8543   Eric W. Biederman   msg/security: Pas...
756
  		    !security_msg_queue_msgrcv(&msq->q_perm, msg, msr->r_tsk,
5a06a363e   Ingo Molnar   [PATCH] ipc/msg.c...
757
  					       msr->r_msgtype, msr->r_mode)) {
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
758
  			list_del(&msr->r_list);
5a06a363e   Ingo Molnar   [PATCH] ipc/msg.c...
759
  			if (msr->r_maxsize < msg->m_ts) {
ee51636ca   Sebastian Andrzej Siewior   ipc/msg: implemen...
760
761
  				wake_q_add(wake_q, msr->r_tsk);
  				WRITE_ONCE(msr->r_msg, ERR_PTR(-E2BIG));
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
762
  			} else {
39a4940ea   Eric W. Biederman   ipc/msg: Fix msgc...
763
  				ipc_update_pid(&msq->q_lrpid, task_pid(msr->r_tsk));
2a70b7879   Arnd Bergmann   y2038: ipc: Use k...
764
  				msq->q_rtime = ktime_get_real_seconds();
5a06a363e   Ingo Molnar   [PATCH] ipc/msg.c...
765

ee51636ca   Sebastian Andrzej Siewior   ipc/msg: implemen...
766
767
  				wake_q_add(wake_q, msr->r_tsk);
  				WRITE_ONCE(msr->r_msg, msg);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
768
769
770
771
  				return 1;
  			}
  		}
  	}
ffa571daf   Davidlohr Bueso   ipc,msg: document...
772

1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
773
774
  	return 0;
  }
9b1404c24   Al Viro   msgrcv(2), msgsnd...
775
  static long do_msgsnd(int msqid, long mtype, void __user *mtext,
651971cb7   suzuki   [PATCH] Fix the s...
776
  		size_t msgsz, int msgflg)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
777
778
779
  {
  	struct msg_queue *msq;
  	struct msg_msg *msg;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
780
  	int err;
1e7869373   Kirill Korotaev   [PATCH] IPC names...
781
  	struct ipc_namespace *ns;
194a6b5b9   Waiman Long   sched/wake_q: Ren...
782
  	DEFINE_WAKE_Q(wake_q);
1e7869373   Kirill Korotaev   [PATCH] IPC names...
783
784
  
  	ns = current->nsproxy->ipc_ns;
5a06a363e   Ingo Molnar   [PATCH] ipc/msg.c...
785

1e7869373   Kirill Korotaev   [PATCH] IPC names...
786
  	if (msgsz > ns->msg_ctlmax || (long) msgsz < 0 || msqid < 0)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
787
  		return -EINVAL;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
788
789
  	if (mtype < 1)
  		return -EINVAL;
651971cb7   suzuki   [PATCH] Fix the s...
790
  	msg = load_msg(mtext, msgsz);
5a06a363e   Ingo Molnar   [PATCH] ipc/msg.c...
791
  	if (IS_ERR(msg))
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
792
793
794
795
  		return PTR_ERR(msg);
  
  	msg->m_type = mtype;
  	msg->m_ts = msgsz;
3dd1f784e   Davidlohr Bueso   ipc,msg: shorten ...
796
797
  	rcu_read_lock();
  	msq = msq_obtain_object_check(ns, msqid);
023a53557   Nadia Derbey   ipc: integrate ip...
798
799
  	if (IS_ERR(msq)) {
  		err = PTR_ERR(msq);
3dd1f784e   Davidlohr Bueso   ipc,msg: shorten ...
800
  		goto out_unlock1;
023a53557   Nadia Derbey   ipc: integrate ip...
801
  	}
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
802

bebcb928c   Manfred Spraul   ipc/msg.c: Fix lo...
803
  	ipc_lock_object(&msq->q_perm);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
804
805
  	for (;;) {
  		struct msg_sender s;
5a06a363e   Ingo Molnar   [PATCH] ipc/msg.c...
806
  		err = -EACCES;
b0e77598f   Serge E. Hallyn   userns: user name...
807
  		if (ipcperms(ns, &msq->q_perm, S_IWUGO))
bebcb928c   Manfred Spraul   ipc/msg.c: Fix lo...
808
  			goto out_unlock0;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
809

4271b05a2   Davidlohr Bueso   ipc,msg: prevent ...
810
  		/* raced with RMID? */
0f3d2b013   Rafael Aquini   ipc: introduce ip...
811
  		if (!ipc_valid_object(&msq->q_perm)) {
4271b05a2   Davidlohr Bueso   ipc,msg: prevent ...
812
813
814
  			err = -EIDRM;
  			goto out_unlock0;
  		}
d8c6e8543   Eric W. Biederman   msg/security: Pas...
815
  		err = security_msg_queue_msgsnd(&msq->q_perm, msg, msgflg);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
816
  		if (err)
bebcb928c   Manfred Spraul   ipc/msg.c: Fix lo...
817
  			goto out_unlock0;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
818

ed27f9122   Davidlohr Bueso   ipc/msg: avoid wa...
819
  		if (msg_fits_inqueue(msq, msgsz))
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
820
  			break;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
821
822
  
  		/* queue full, wait: */
5a06a363e   Ingo Molnar   [PATCH] ipc/msg.c...
823
824
  		if (msgflg & IPC_NOWAIT) {
  			err = -EAGAIN;
bebcb928c   Manfred Spraul   ipc/msg.c: Fix lo...
825
  			goto out_unlock0;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
826
  		}
3dd1f784e   Davidlohr Bueso   ipc,msg: shorten ...
827

ffa571daf   Davidlohr Bueso   ipc,msg: document...
828
  		/* enqueue the sender and prepare to block */
ed27f9122   Davidlohr Bueso   ipc/msg: avoid wa...
829
  		ss_add(msq, &s, msgsz);
6062a8dc0   Rik van Riel   ipc,sem: fine gra...
830

dba4cdd39   Manfred Spraul   ipc: merge ipc_rc...
831
  		if (!ipc_rcu_getref(&msq->q_perm)) {
6062a8dc0   Rik van Riel   ipc,sem: fine gra...
832
  			err = -EIDRM;
3dd1f784e   Davidlohr Bueso   ipc,msg: shorten ...
833
  			goto out_unlock0;
6062a8dc0   Rik van Riel   ipc,sem: fine gra...
834
  		}
3dd1f784e   Davidlohr Bueso   ipc,msg: shorten ...
835
836
  		ipc_unlock_object(&msq->q_perm);
  		rcu_read_unlock();
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
837
  		schedule();
3dd1f784e   Davidlohr Bueso   ipc,msg: shorten ...
838
839
  		rcu_read_lock();
  		ipc_lock_object(&msq->q_perm);
dba4cdd39   Manfred Spraul   ipc: merge ipc_rc...
840
  		ipc_rcu_putref(&msq->q_perm, msg_rcu_free);
0f3d2b013   Rafael Aquini   ipc: introduce ip...
841
842
  		/* raced with RMID? */
  		if (!ipc_valid_object(&msq->q_perm)) {
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
843
  			err = -EIDRM;
3dd1f784e   Davidlohr Bueso   ipc,msg: shorten ...
844
  			goto out_unlock0;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
845
846
  		}
  		ss_del(&s);
5a06a363e   Ingo Molnar   [PATCH] ipc/msg.c...
847

1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
848
  		if (signal_pending(current)) {
5a06a363e   Ingo Molnar   [PATCH] ipc/msg.c...
849
  			err = -ERESTARTNOHAND;
3dd1f784e   Davidlohr Bueso   ipc,msg: shorten ...
850
  			goto out_unlock0;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
851
  		}
3dd1f784e   Davidlohr Bueso   ipc,msg: shorten ...
852

1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
853
  	}
ed27f9122   Davidlohr Bueso   ipc/msg: avoid wa...
854

39a4940ea   Eric W. Biederman   ipc/msg: Fix msgc...
855
  	ipc_update_pid(&msq->q_lspid, task_tgid(current));
2a70b7879   Arnd Bergmann   y2038: ipc: Use k...
856
  	msq->q_stime = ktime_get_real_seconds();
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
857

ee51636ca   Sebastian Andrzej Siewior   ipc/msg: implemen...
858
  	if (!pipelined_send(msq, msg, &wake_q)) {
25985edce   Lucas De Marchi   Fix common misspe...
859
  		/* no one is waiting for this message, enqueue it */
5a06a363e   Ingo Molnar   [PATCH] ipc/msg.c...
860
  		list_add_tail(&msg->m_list, &msq->q_messages);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
861
862
  		msq->q_cbytes += msgsz;
  		msq->q_qnum++;
3ac88a41f   Kirill Korotaev   virtualization of...
863
864
  		atomic_add(msgsz, &ns->msg_bytes);
  		atomic_inc(&ns->msg_hdrs);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
865
  	}
5a06a363e   Ingo Molnar   [PATCH] ipc/msg.c...
866

1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
867
868
  	err = 0;
  	msg = NULL;
3dd1f784e   Davidlohr Bueso   ipc,msg: shorten ...
869
870
  out_unlock0:
  	ipc_unlock_object(&msq->q_perm);
ee51636ca   Sebastian Andrzej Siewior   ipc/msg: implemen...
871
  	wake_up_q(&wake_q);
3dd1f784e   Davidlohr Bueso   ipc,msg: shorten ...
872
873
  out_unlock1:
  	rcu_read_unlock();
5a06a363e   Ingo Molnar   [PATCH] ipc/msg.c...
874
  	if (msg != NULL)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
875
876
877
  		free_msg(msg);
  	return err;
  }
31c213f21   Dominik Brodowski   ipc: add msgsnd s...
878
879
  long ksys_msgsnd(int msqid, struct msgbuf __user *msgp, size_t msgsz,
  		 int msgflg)
651971cb7   suzuki   [PATCH] Fix the s...
880
881
882
883
884
885
886
  {
  	long mtype;
  
  	if (get_user(mtype, &msgp->mtype))
  		return -EFAULT;
  	return do_msgsnd(msqid, mtype, msgp->mtext, msgsz, msgflg);
  }
31c213f21   Dominik Brodowski   ipc: add msgsnd s...
887
888
889
890
891
  SYSCALL_DEFINE4(msgsnd, int, msqid, struct msgbuf __user *, msgp, size_t, msgsz,
  		int, msgflg)
  {
  	return ksys_msgsnd(msqid, msgp, msgsz, msgflg);
  }
9b1404c24   Al Viro   msgrcv(2), msgsnd...
892
893
894
895
896
897
  #ifdef CONFIG_COMPAT
  
  struct compat_msgbuf {
  	compat_long_t mtype;
  	char mtext[1];
  };
31c213f21   Dominik Brodowski   ipc: add msgsnd s...
898
899
  long compat_ksys_msgsnd(int msqid, compat_uptr_t msgp,
  		       compat_ssize_t msgsz, int msgflg)
9b1404c24   Al Viro   msgrcv(2), msgsnd...
900
901
902
903
904
905
906
907
  {
  	struct compat_msgbuf __user *up = compat_ptr(msgp);
  	compat_long_t mtype;
  
  	if (get_user(mtype, &up->mtype))
  		return -EFAULT;
  	return do_msgsnd(msqid, mtype, up->mtext, (ssize_t)msgsz, msgflg);
  }
31c213f21   Dominik Brodowski   ipc: add msgsnd s...
908
909
910
911
912
913
  
  COMPAT_SYSCALL_DEFINE4(msgsnd, int, msqid, compat_uptr_t, msgp,
  		       compat_ssize_t, msgsz, int, msgflg)
  {
  	return compat_ksys_msgsnd(msqid, msgp, msgsz, msgflg);
  }
9b1404c24   Al Viro   msgrcv(2), msgsnd...
914
  #endif
5a06a363e   Ingo Molnar   [PATCH] ipc/msg.c...
915
  static inline int convert_mode(long *msgtyp, int msgflg)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
916
  {
8ac6ed585   Peter Hurley   ipc: implement MS...
917
918
  	if (msgflg & MSG_COPY)
  		return SEARCH_NUMBER;
5a06a363e   Ingo Molnar   [PATCH] ipc/msg.c...
919
  	/*
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
920
921
922
  	 *  find message of correct type.
  	 *  msgtyp = 0 => get first.
  	 *  msgtyp > 0 => get first message of matching type.
5a06a363e   Ingo Molnar   [PATCH] ipc/msg.c...
923
  	 *  msgtyp < 0 => get message with least type must be < abs(msgtype).
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
924
  	 */
5a06a363e   Ingo Molnar   [PATCH] ipc/msg.c...
925
  	if (*msgtyp == 0)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
926
  		return SEARCH_ANY;
5a06a363e   Ingo Molnar   [PATCH] ipc/msg.c...
927
  	if (*msgtyp < 0) {
999898355   Jiri Slaby   ipc: msg, make ms...
928
929
930
931
  		if (*msgtyp == LONG_MIN) /* -LONG_MIN is undefined */
  			*msgtyp = LONG_MAX;
  		else
  			*msgtyp = -*msgtyp;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
932
933
  		return SEARCH_LESSEQUAL;
  	}
5a06a363e   Ingo Molnar   [PATCH] ipc/msg.c...
934
  	if (msgflg & MSG_EXCEPT)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
935
936
937
  		return SEARCH_NOTEQUAL;
  	return SEARCH_EQUAL;
  }
f9dd87f47   Stanislav Kinsbursky   ipc: message queu...
938
939
940
941
942
943
944
945
946
947
948
949
950
  static long do_msg_fill(void __user *dest, struct msg_msg *msg, size_t bufsz)
  {
  	struct msgbuf __user *msgp = dest;
  	size_t msgsz;
  
  	if (put_user(msg->m_type, &msgp->mtype))
  		return -EFAULT;
  
  	msgsz = (bufsz > msg->m_ts) ? msg->m_ts : bufsz;
  	if (store_msg(msgp->mtext, msg, msgsz))
  		return -EFAULT;
  	return msgsz;
  }
4a674f34b   Stanislav Kinsbursky   ipc: introduce me...
951
  #ifdef CONFIG_CHECKPOINT_RESTORE
3fcfe7865   Stanislav Kinsbursky   ipc: add more com...
952
953
954
955
  /*
   * This function creates new kernel message structure, large enough to store
   * bufsz message bytes.
   */
8ac6ed585   Peter Hurley   ipc: implement MS...
956
  static inline struct msg_msg *prepare_copy(void __user *buf, size_t bufsz)
4a674f34b   Stanislav Kinsbursky   ipc: introduce me...
957
958
  {
  	struct msg_msg *copy;
4a674f34b   Stanislav Kinsbursky   ipc: introduce me...
959
960
961
962
963
964
965
966
  	/*
  	 * Create dummy message to copy real message to.
  	 */
  	copy = load_msg(buf, bufsz);
  	if (!IS_ERR(copy))
  		copy->m_ts = bufsz;
  	return copy;
  }
85398aa8d   Stanislav Kinsbursky   ipc: simplify fre...
967
  static inline void free_copy(struct msg_msg *copy)
4a674f34b   Stanislav Kinsbursky   ipc: introduce me...
968
  {
85398aa8d   Stanislav Kinsbursky   ipc: simplify fre...
969
  	if (copy)
4a674f34b   Stanislav Kinsbursky   ipc: introduce me...
970
971
972
  		free_msg(copy);
  }
  #else
8ac6ed585   Peter Hurley   ipc: implement MS...
973
  static inline struct msg_msg *prepare_copy(void __user *buf, size_t bufsz)
b30efe277   Stanislav Kinsbursky   ipc: convert prep...
974
975
976
  {
  	return ERR_PTR(-ENOSYS);
  }
85398aa8d   Stanislav Kinsbursky   ipc: simplify fre...
977
978
979
  static inline void free_copy(struct msg_msg *copy)
  {
  }
4a674f34b   Stanislav Kinsbursky   ipc: introduce me...
980
  #endif
daaf74cf0   Peter Hurley   ipc: refactor msg...
981
982
  static struct msg_msg *find_msg(struct msg_queue *msq, long *msgtyp, int mode)
  {
368ae537e   Svenning Sørensen   IPC: bugfix for m...
983
  	struct msg_msg *msg, *found = NULL;
daaf74cf0   Peter Hurley   ipc: refactor msg...
984
985
986
987
  	long count = 0;
  
  	list_for_each_entry(msg, &msq->q_messages, m_list) {
  		if (testmsg(msg, *msgtyp, mode) &&
d8c6e8543   Eric W. Biederman   msg/security: Pas...
988
  		    !security_msg_queue_msgrcv(&msq->q_perm, msg, current,
daaf74cf0   Peter Hurley   ipc: refactor msg...
989
990
991
  					       *msgtyp, mode)) {
  			if (mode == SEARCH_LESSEQUAL && msg->m_type != 1) {
  				*msgtyp = msg->m_type - 1;
368ae537e   Svenning Sørensen   IPC: bugfix for m...
992
  				found = msg;
daaf74cf0   Peter Hurley   ipc: refactor msg...
993
994
995
996
997
998
999
1000
  			} else if (mode == SEARCH_NUMBER) {
  				if (*msgtyp == count)
  					return msg;
  			} else
  				return msg;
  			count++;
  		}
  	}
368ae537e   Svenning Sørensen   IPC: bugfix for m...
1001
  	return found ?: ERR_PTR(-EAGAIN);
daaf74cf0   Peter Hurley   ipc: refactor msg...
1002
  }
9b1404c24   Al Viro   msgrcv(2), msgsnd...
1003
  static long do_msgrcv(int msqid, void __user *buf, size_t bufsz, long msgtyp, int msgflg,
f9dd87f47   Stanislav Kinsbursky   ipc: message queu...
1004
  	       long (*msg_handler)(void __user *, struct msg_msg *, size_t))
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1005
  {
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1006
  	int mode;
41a0d523d   Davidlohr Bueso   ipc,msg: shorten ...
1007
  	struct msg_queue *msq;
1e7869373   Kirill Korotaev   [PATCH] IPC names...
1008
  	struct ipc_namespace *ns;
41a0d523d   Davidlohr Bueso   ipc,msg: shorten ...
1009
  	struct msg_msg *msg, *copy = NULL;
194a6b5b9   Waiman Long   sched/wake_q: Ren...
1010
  	DEFINE_WAKE_Q(wake_q);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1011

88b9e456b   Peter Hurley   ipc: don't alloca...
1012
  	ns = current->nsproxy->ipc_ns;
f9dd87f47   Stanislav Kinsbursky   ipc: message queu...
1013
  	if (msqid < 0 || (long) bufsz < 0)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1014
  		return -EINVAL;
41a0d523d   Davidlohr Bueso   ipc,msg: shorten ...
1015

4a674f34b   Stanislav Kinsbursky   ipc: introduce me...
1016
  	if (msgflg & MSG_COPY) {
4f87dac38   Michael Kerrisk   ipc: Fix 2 bugs i...
1017
1018
  		if ((msgflg & MSG_EXCEPT) || !(msgflg & IPC_NOWAIT))
  			return -EINVAL;
8ac6ed585   Peter Hurley   ipc: implement MS...
1019
  		copy = prepare_copy(buf, min_t(size_t, bufsz, ns->msg_ctlmax));
4a674f34b   Stanislav Kinsbursky   ipc: introduce me...
1020
1021
1022
  		if (IS_ERR(copy))
  			return PTR_ERR(copy);
  	}
5a06a363e   Ingo Molnar   [PATCH] ipc/msg.c...
1023
  	mode = convert_mode(&msgtyp, msgflg);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1024

41a0d523d   Davidlohr Bueso   ipc,msg: shorten ...
1025
1026
  	rcu_read_lock();
  	msq = msq_obtain_object_check(ns, msqid);
4a674f34b   Stanislav Kinsbursky   ipc: introduce me...
1027
  	if (IS_ERR(msq)) {
41a0d523d   Davidlohr Bueso   ipc,msg: shorten ...
1028
  		rcu_read_unlock();
85398aa8d   Stanislav Kinsbursky   ipc: simplify fre...
1029
  		free_copy(copy);
023a53557   Nadia Derbey   ipc: integrate ip...
1030
  		return PTR_ERR(msq);
4a674f34b   Stanislav Kinsbursky   ipc: introduce me...
1031
  	}
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1032
1033
1034
  
  	for (;;) {
  		struct msg_receiver msr_d;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1035
1036
  
  		msg = ERR_PTR(-EACCES);
b0e77598f   Serge E. Hallyn   userns: user name...
1037
  		if (ipcperms(ns, &msq->q_perm, S_IRUGO))
41a0d523d   Davidlohr Bueso   ipc,msg: shorten ...
1038
  			goto out_unlock1;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1039

41a0d523d   Davidlohr Bueso   ipc,msg: shorten ...
1040
  		ipc_lock_object(&msq->q_perm);
4271b05a2   Davidlohr Bueso   ipc,msg: prevent ...
1041
1042
  
  		/* raced with RMID? */
0f3d2b013   Rafael Aquini   ipc: introduce ip...
1043
  		if (!ipc_valid_object(&msq->q_perm)) {
4271b05a2   Davidlohr Bueso   ipc,msg: prevent ...
1044
1045
1046
  			msg = ERR_PTR(-EIDRM);
  			goto out_unlock0;
  		}
daaf74cf0   Peter Hurley   ipc: refactor msg...
1047
  		msg = find_msg(msq, &msgtyp, mode);
5a06a363e   Ingo Molnar   [PATCH] ipc/msg.c...
1048
1049
1050
1051
1052
  		if (!IS_ERR(msg)) {
  			/*
  			 * Found a suitable message.
  			 * Unlink it from the queue.
  			 */
f9dd87f47   Stanislav Kinsbursky   ipc: message queu...
1053
  			if ((bufsz < msg->m_ts) && !(msgflg & MSG_NOERROR)) {
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1054
  				msg = ERR_PTR(-E2BIG);
41a0d523d   Davidlohr Bueso   ipc,msg: shorten ...
1055
  				goto out_unlock0;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1056
  			}
3fcfe7865   Stanislav Kinsbursky   ipc: add more com...
1057
1058
1059
1060
  			/*
  			 * If we are copying, then do not unlink message and do
  			 * not update queue parameters.
  			 */
852028af8   Peter Hurley   ipc: remove msg h...
1061
1062
  			if (msgflg & MSG_COPY) {
  				msg = copy_msg(msg, copy);
41a0d523d   Davidlohr Bueso   ipc,msg: shorten ...
1063
  				goto out_unlock0;
852028af8   Peter Hurley   ipc: remove msg h...
1064
  			}
41a0d523d   Davidlohr Bueso   ipc,msg: shorten ...
1065

1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1066
1067
  			list_del(&msg->m_list);
  			msq->q_qnum--;
2a70b7879   Arnd Bergmann   y2038: ipc: Use k...
1068
  			msq->q_rtime = ktime_get_real_seconds();
39a4940ea   Eric W. Biederman   ipc/msg: Fix msgc...
1069
  			ipc_update_pid(&msq->q_lrpid, task_tgid(current));
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1070
  			msq->q_cbytes -= msg->m_ts;
3ac88a41f   Kirill Korotaev   virtualization of...
1071
1072
  			atomic_sub(msg->m_ts, &ns->msg_bytes);
  			atomic_dec(&ns->msg_hdrs);
ed27f9122   Davidlohr Bueso   ipc/msg: avoid wa...
1073
  			ss_wakeup(msq, &wake_q, false);
41a0d523d   Davidlohr Bueso   ipc,msg: shorten ...
1074
1075
  
  			goto out_unlock0;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1076
  		}
41a0d523d   Davidlohr Bueso   ipc,msg: shorten ...
1077

1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1078
1079
1080
  		/* No message waiting. Wait for a message */
  		if (msgflg & IPC_NOWAIT) {
  			msg = ERR_PTR(-ENOMSG);
41a0d523d   Davidlohr Bueso   ipc,msg: shorten ...
1081
  			goto out_unlock0;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1082
  		}
41a0d523d   Davidlohr Bueso   ipc,msg: shorten ...
1083

5a06a363e   Ingo Molnar   [PATCH] ipc/msg.c...
1084
  		list_add_tail(&msr_d.r_list, &msq->q_receivers);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1085
1086
1087
  		msr_d.r_tsk = current;
  		msr_d.r_msgtype = msgtyp;
  		msr_d.r_mode = mode;
5a06a363e   Ingo Molnar   [PATCH] ipc/msg.c...
1088
  		if (msgflg & MSG_NOERROR)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1089
  			msr_d.r_maxsize = INT_MAX;
5a06a363e   Ingo Molnar   [PATCH] ipc/msg.c...
1090
  		else
f9dd87f47   Stanislav Kinsbursky   ipc: message queu...
1091
  			msr_d.r_maxsize = bufsz;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1092
  		msr_d.r_msg = ERR_PTR(-EAGAIN);
f75a2f358   Davidlohr Bueso   ipc,msg: use curr...
1093
  		__set_current_state(TASK_INTERRUPTIBLE);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1094

41a0d523d   Davidlohr Bueso   ipc,msg: shorten ...
1095
1096
  		ipc_unlock_object(&msq->q_perm);
  		rcu_read_unlock();
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1097
  		schedule();
ee51636ca   Sebastian Andrzej Siewior   ipc/msg: implemen...
1098
1099
1100
1101
1102
1103
  		/*
  		 * Lockless receive, part 1:
  		 * We don't hold a reference to the queue and getting a
  		 * reference would defeat the idea of a lockless operation,
  		 * thus the code relies on rcu to guarantee the existence of
  		 * msq:
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1104
1105
  		 * Prior to destruction, expunge_all(-EIRDM) changes r_msg.
  		 * Thus if r_msg is -EAGAIN, then the queue not yet destroyed.
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1106
1107
  		 */
  		rcu_read_lock();
ee51636ca   Sebastian Andrzej Siewior   ipc/msg: implemen...
1108
1109
1110
1111
1112
1113
  		/*
  		 * Lockless receive, part 2:
  		 * The work in pipelined_send() and expunge_all():
  		 * - Set pointer to message
  		 * - Queue the receiver task for later wakeup
  		 * - Wake up the process after the lock is dropped.
ff35e5ef8   Davidlohr Bueso   ipc,msg: provide ...
1114
  		 *
ee51636ca   Sebastian Andrzej Siewior   ipc/msg: implemen...
1115
1116
  		 * Should the process wake up before this wakeup (due to a
  		 * signal) it will either see the message and continue ...
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1117
  		 */
ee51636ca   Sebastian Andrzej Siewior   ipc/msg: implemen...
1118
  		msg = READ_ONCE(msr_d.r_msg);
41a0d523d   Davidlohr Bueso   ipc,msg: shorten ...
1119
1120
  		if (msg != ERR_PTR(-EAGAIN))
  			goto out_unlock1;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1121

ee51636ca   Sebastian Andrzej Siewior   ipc/msg: implemen...
1122
1123
1124
1125
  		 /*
  		  * ... or see -EAGAIN, acquire the lock to check the message
  		  * again.
  		  */
41a0d523d   Davidlohr Bueso   ipc,msg: shorten ...
1126
  		ipc_lock_object(&msq->q_perm);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1127

ee51636ca   Sebastian Andrzej Siewior   ipc/msg: implemen...
1128
  		msg = msr_d.r_msg;
5a06a363e   Ingo Molnar   [PATCH] ipc/msg.c...
1129
  		if (msg != ERR_PTR(-EAGAIN))
41a0d523d   Davidlohr Bueso   ipc,msg: shorten ...
1130
  			goto out_unlock0;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1131
1132
1133
1134
  
  		list_del(&msr_d.r_list);
  		if (signal_pending(current)) {
  			msg = ERR_PTR(-ERESTARTNOHAND);
41a0d523d   Davidlohr Bueso   ipc,msg: shorten ...
1135
  			goto out_unlock0;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1136
  		}
41a0d523d   Davidlohr Bueso   ipc,msg: shorten ...
1137
1138
  
  		ipc_unlock_object(&msq->q_perm);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1139
  	}
41a0d523d   Davidlohr Bueso   ipc,msg: shorten ...
1140
1141
1142
  
  out_unlock0:
  	ipc_unlock_object(&msq->q_perm);
e3658538b   Davidlohr Bueso   ipc/msg: batch qu...
1143
  	wake_up_q(&wake_q);
41a0d523d   Davidlohr Bueso   ipc,msg: shorten ...
1144
1145
  out_unlock1:
  	rcu_read_unlock();
4a674f34b   Stanislav Kinsbursky   ipc: introduce me...
1146
  	if (IS_ERR(msg)) {
85398aa8d   Stanislav Kinsbursky   ipc: simplify fre...
1147
  		free_copy(copy);
5a06a363e   Ingo Molnar   [PATCH] ipc/msg.c...
1148
  		return PTR_ERR(msg);
4a674f34b   Stanislav Kinsbursky   ipc: introduce me...
1149
  	}
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1150

f9dd87f47   Stanislav Kinsbursky   ipc: message queu...
1151
  	bufsz = msg_handler(buf, msg, bufsz);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1152
  	free_msg(msg);
5a06a363e   Ingo Molnar   [PATCH] ipc/msg.c...
1153

f9dd87f47   Stanislav Kinsbursky   ipc: message queu...
1154
  	return bufsz;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1155
  }
078faac9e   Dominik Brodowski   ipc: add msgrcv s...
1156
1157
1158
1159
1160
  long ksys_msgrcv(int msqid, struct msgbuf __user *msgp, size_t msgsz,
  		 long msgtyp, int msgflg)
  {
  	return do_msgrcv(msqid, msgp, msgsz, msgtyp, msgflg, do_msg_fill);
  }
e48fbb699   Heiko Carstens   [CVE-2009-0029] S...
1161
1162
  SYSCALL_DEFINE5(msgrcv, int, msqid, struct msgbuf __user *, msgp, size_t, msgsz,
  		long, msgtyp, int, msgflg)
651971cb7   suzuki   [PATCH] Fix the s...
1163
  {
078faac9e   Dominik Brodowski   ipc: add msgrcv s...
1164
  	return ksys_msgrcv(msqid, msgp, msgsz, msgtyp, msgflg);
651971cb7   suzuki   [PATCH] Fix the s...
1165
  }
9b1404c24   Al Viro   msgrcv(2), msgsnd...
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
  #ifdef CONFIG_COMPAT
  static long compat_do_msg_fill(void __user *dest, struct msg_msg *msg, size_t bufsz)
  {
  	struct compat_msgbuf __user *msgp = dest;
  	size_t msgsz;
  
  	if (put_user(msg->m_type, &msgp->mtype))
  		return -EFAULT;
  
  	msgsz = (bufsz > msg->m_ts) ? msg->m_ts : bufsz;
  	if (store_msg(msgp->mtext, msg, msgsz))
  		return -EFAULT;
  	return msgsz;
  }
078faac9e   Dominik Brodowski   ipc: add msgrcv s...
1180
1181
  long compat_ksys_msgrcv(int msqid, compat_uptr_t msgp, compat_ssize_t msgsz,
  			compat_long_t msgtyp, int msgflg)
9b1404c24   Al Viro   msgrcv(2), msgsnd...
1182
1183
1184
1185
  {
  	return do_msgrcv(msqid, compat_ptr(msgp), (ssize_t)msgsz, (long)msgtyp,
  			 msgflg, compat_do_msg_fill);
  }
078faac9e   Dominik Brodowski   ipc: add msgrcv s...
1186
1187
1188
1189
1190
1191
1192
  
  COMPAT_SYSCALL_DEFINE5(msgrcv, int, msqid, compat_uptr_t, msgp,
  		       compat_ssize_t, msgsz, compat_long_t, msgtyp,
  		       int, msgflg)
  {
  	return compat_ksys_msgrcv(msqid, msgp, msgsz, msgtyp, msgflg);
  }
9b1404c24   Al Viro   msgrcv(2), msgsnd...
1193
  #endif
3440a6bd1   Davidlohr Bueso   ipc,msg: move som...
1194

eae04d25a   Davidlohr Bueso   ipc: simplify ipc...
1195
  void msg_init_ns(struct ipc_namespace *ns)
3440a6bd1   Davidlohr Bueso   ipc,msg: move som...
1196
1197
1198
  {
  	ns->msg_ctlmax = MSGMAX;
  	ns->msg_ctlmnb = MSGMNB;
0050ee059   Manfred Spraul   ipc/msg: increase...
1199
  	ns->msg_ctlmni = MSGMNI;
3440a6bd1   Davidlohr Bueso   ipc,msg: move som...
1200
1201
1202
  
  	atomic_set(&ns->msg_bytes, 0);
  	atomic_set(&ns->msg_hdrs, 0);
eae04d25a   Davidlohr Bueso   ipc: simplify ipc...
1203
  	ipc_init_ids(&ns->ids[IPC_MSG_IDS]);
3440a6bd1   Davidlohr Bueso   ipc,msg: move som...
1204
1205
1206
1207
1208
1209
1210
  }
  
  #ifdef CONFIG_IPC_NS
  void msg_exit_ns(struct ipc_namespace *ns)
  {
  	free_ipcs(ns, &msg_ids(ns), freeque);
  	idr_destroy(&ns->ids[IPC_MSG_IDS].ipcs_idr);
0cfb6aee7   Guillaume Knispel   ipc: optimize sem...
1211
  	rhashtable_destroy(&ns->ids[IPC_MSG_IDS].key_ht);
3440a6bd1   Davidlohr Bueso   ipc,msg: move som...
1212
1213
  }
  #endif
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1214
  #ifdef CONFIG_PROC_FS
19b4946ca   Mike Waychison   [PATCH] ipc: conv...
1215
  static int sysvipc_msg_proc_show(struct seq_file *s, void *it)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1216
  {
39a4940ea   Eric W. Biederman   ipc/msg: Fix msgc...
1217
  	struct pid_namespace *pid_ns = ipc_seq_pid_ns(s);
1efdb69b0   Eric W. Biederman   userns: Convert i...
1218
  	struct user_namespace *user_ns = seq_user_ns(s);
ade9f91b3   Kees Cook   ipc: add missing ...
1219
1220
  	struct kern_ipc_perm *ipcp = it;
  	struct msg_queue *msq = container_of(ipcp, struct msg_queue, q_perm);
19b4946ca   Mike Waychison   [PATCH] ipc: conv...
1221

7f032d6ef   Joe Perches   ipc: remove use o...
1222
  	seq_printf(s,
50578ea97   Deepa Dinamani   ipc: msg: Make ms...
1223
1224
  		   "%10d %10d  %4o  %10lu %10lu %5u %5u %5u %5u %5u %5u %10llu %10llu %10llu
  ",
7f032d6ef   Joe Perches   ipc: remove use o...
1225
1226
1227
1228
1229
  		   msq->q_perm.key,
  		   msq->q_perm.id,
  		   msq->q_perm.mode,
  		   msq->q_cbytes,
  		   msq->q_qnum,
39a4940ea   Eric W. Biederman   ipc/msg: Fix msgc...
1230
1231
  		   pid_nr_ns(msq->q_lspid, pid_ns),
  		   pid_nr_ns(msq->q_lrpid, pid_ns),
7f032d6ef   Joe Perches   ipc: remove use o...
1232
1233
1234
1235
1236
1237
1238
1239
1240
  		   from_kuid_munged(user_ns, msq->q_perm.uid),
  		   from_kgid_munged(user_ns, msq->q_perm.gid),
  		   from_kuid_munged(user_ns, msq->q_perm.cuid),
  		   from_kgid_munged(user_ns, msq->q_perm.cgid),
  		   msq->q_stime,
  		   msq->q_rtime,
  		   msq->q_ctime);
  
  	return 0;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1241
1242
  }
  #endif
3440a6bd1   Davidlohr Bueso   ipc,msg: move som...
1243

eae04d25a   Davidlohr Bueso   ipc: simplify ipc...
1244
  void __init msg_init(void)
3440a6bd1   Davidlohr Bueso   ipc,msg: move som...
1245
  {
eae04d25a   Davidlohr Bueso   ipc: simplify ipc...
1246
  	msg_init_ns(&init_ipc_ns);
3440a6bd1   Davidlohr Bueso   ipc,msg: move som...
1247

3440a6bd1   Davidlohr Bueso   ipc,msg: move som...
1248
1249
1250
1251
1252
  	ipc_init_proc_interface("sysvipc/msg",
  				"       key      msqid perms      cbytes       qnum lspid lrpid   uid   gid  cuid  cgid      stime      rtime      ctime
  ",
  				IPC_MSG_IDS, sysvipc_msg_proc_show);
  }