Blame view

ipc/msg.c 31.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;
0d97a82ba   Manfred Spraul   ipc/msg.c: update...
61
62
63
64
65
66
67
68
69
  /*
   * MSG_BARRIER Locking:
   *
   * Similar to the optimization used in ipc/mqueue.c, one syscall return path
   * does not acquire any locks when it sees that a message exists in
   * msg_receiver.r_msg. Therefore r_msg is set using smp_store_release()
   * and accessed using READ_ONCE()+smp_acquire__after_ctrl_dep(). In addition,
   * wake_q_add_safe() is used. See ipc/mqueue.c for more details
   */
4bb6657dd   Davidlohr Bueso   ipc,msg: document...
70
  /* one msg_receiver structure for each sleeping receiver */
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
71
  struct msg_receiver {
5a06a363e   Ingo Molnar   [PATCH] ipc/msg.c...
72
73
  	struct list_head	r_list;
  	struct task_struct	*r_tsk;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
74

5a06a363e   Ingo Molnar   [PATCH] ipc/msg.c...
75
76
77
  	int			r_mode;
  	long			r_msgtype;
  	long			r_maxsize;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
78

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

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

a5001a0d9   Davidlohr Bueso   ipc,msg: introduc...
97
98
  static inline struct msg_queue *msq_obtain_object(struct ipc_namespace *ns, int id)
  {
55b7ae501   Davidlohr Bueso   ipc: rename ipc_o...
99
  	struct kern_ipc_perm *ipcp = ipc_obtain_object_idr(&msg_ids(ns), id);
a5001a0d9   Davidlohr Bueso   ipc,msg: introduc...
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
  
  	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...
117
118
119
120
  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...
121
122
  static void msg_rcu_free(struct rcu_head *head)
  {
dba4cdd39   Manfred Spraul   ipc: merge ipc_rc...
123
124
  	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...
125

d8c6e8543   Eric W. Biederman   msg/security: Pas...
126
  	security_msg_queue_free(&msq->q_perm);
bc8136a54   Vasily Averin   ipc: use kmalloc ...
127
  	kfree(msq);
52f908904   Kees Cook   ipc/msg: avoid ip...
128
  }
f4566f048   Nadia Derbey   ipc: fix wrong co...
129
130
131
132
133
  /**
   * 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->...
134
   * Called with msg_ids.rwsem held (writer)
f4566f048   Nadia Derbey   ipc: fix wrong co...
135
   */
7748dbfaa   Nadia Derbey   ipc: unify the sy...
136
  static int newque(struct ipc_namespace *ns, struct ipc_params *params)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
137
  {
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
138
  	struct msg_queue *msq;
51c23b7b7   Manfred Spraul   ipc/msg.c: avoid ...
139
  	int retval;
7748dbfaa   Nadia Derbey   ipc: unify the sy...
140
141
  	key_t key = params->key;
  	int msgflg = params->flg;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
142

18319498f   Vasily Averin   memcg: enable acc...
143
  	msq = kmalloc(sizeof(*msq), GFP_KERNEL_ACCOUNT);
fb259c310   Kees Cook   ipc/msg: remove s...
144
  	if (unlikely(!msq))
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
145
  		return -ENOMEM;
5a06a363e   Ingo Molnar   [PATCH] ipc/msg.c...
146
  	msq->q_perm.mode = msgflg & S_IRWXUGO;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
147
148
149
  	msq->q_perm.key = key;
  
  	msq->q_perm.security = NULL;
d8c6e8543   Eric W. Biederman   msg/security: Pas...
150
  	retval = security_msg_queue_alloc(&msq->q_perm);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
151
  	if (retval) {
bc8136a54   Vasily Averin   ipc: use kmalloc ...
152
  		kfree(msq);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
153
154
  		return retval;
  	}
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
155
  	msq->q_stime = msq->q_rtime = 0;
50578ea97   Deepa Dinamani   ipc: msg: Make ms...
156
  	msq->q_ctime = ktime_get_real_seconds();
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
157
  	msq->q_cbytes = msq->q_qnum = 0;
1e7869373   Kirill Korotaev   [PATCH] IPC names...
158
  	msq->q_qbytes = ns->msg_ctlmnb;
39a4940ea   Eric W. Biederman   ipc/msg: Fix msgc...
159
  	msq->q_lspid = msq->q_lrpid = NULL;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
160
161
162
  	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...
163

b9a532277   Linus Torvalds   Initialize msg/sh...
164
  	/* ipc_addid() locks msq upon success. */
51c23b7b7   Manfred Spraul   ipc/msg.c: avoid ...
165
166
  	retval = ipc_addid(&msg_ids(ns), &msq->q_perm, ns->msg_ctlmni);
  	if (retval < 0) {
39cfffd77   Manfred Spraul   ipc/util.c: use i...
167
  		ipc_rcu_putref(&msq->q_perm, msg_rcu_free);
51c23b7b7   Manfred Spraul   ipc/msg.c: avoid ...
168
  		return retval;
b9a532277   Linus Torvalds   Initialize msg/sh...
169
  	}
cf9d5d78d   Davidlohr Bueso   ipc: close open c...
170
  	ipc_unlock_object(&msq->q_perm);
dbfcd91f0   Davidlohr Bueso   ipc: move rcu loc...
171
  	rcu_read_unlock();
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
172

7ca7e564e   Nadia Derbey   ipc: store ipcs i...
173
  	return msq->q_perm.id;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
174
  }
ed27f9122   Davidlohr Bueso   ipc/msg: avoid wa...
175
176
177
178
179
180
181
182
  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
183
  {
5a06a363e   Ingo Molnar   [PATCH] ipc/msg.c...
184
  	mss->tsk = current;
ed27f9122   Davidlohr Bueso   ipc/msg: avoid wa...
185
  	mss->msgsz = msgsz;
0d97a82ba   Manfred Spraul   ipc/msg.c: update...
186
187
188
189
  	/*
  	 * No memory barrier required: we did ipc_lock_object(),
  	 * and the waker obtains that lock before calling wake_q_add().
  	 */
f75a2f358   Davidlohr Bueso   ipc,msg: use curr...
190
  	__set_current_state(TASK_INTERRUPTIBLE);
5a06a363e   Ingo Molnar   [PATCH] ipc/msg.c...
191
  	list_add_tail(&mss->list, &msq->q_senders);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
192
  }
5a06a363e   Ingo Molnar   [PATCH] ipc/msg.c...
193
  static inline void ss_del(struct msg_sender *mss)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
194
  {
ed27f9122   Davidlohr Bueso   ipc/msg: avoid wa...
195
  	if (mss->list.next)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
196
197
  		list_del(&mss->list);
  }
ed27f9122   Davidlohr Bueso   ipc/msg: avoid wa...
198
  static void ss_wakeup(struct msg_queue *msq,
d0d6a2a95   Davidlohr Bueso   ipc/msg: make ss_...
199
  		      struct wake_q_head *wake_q, bool kill)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
200
  {
41239fe82   Nikola Pajkovsky   ipc/msg.c: use li...
201
  	struct msg_sender *mss, *t;
ed27f9122   Davidlohr Bueso   ipc/msg: avoid wa...
202
203
  	struct task_struct *stop_tsk = NULL;
  	struct list_head *h = &msq->q_senders;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
204

41239fe82   Nikola Pajkovsky   ipc/msg.c: use li...
205
  	list_for_each_entry_safe(mss, t, h, list) {
5a06a363e   Ingo Molnar   [PATCH] ipc/msg.c...
206
207
  		if (kill)
  			mss->list.next = NULL;
ed27f9122   Davidlohr Bueso   ipc/msg: avoid wa...
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
  
  		/*
  		 * 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...
230
  		wake_q_add(wake_q, mss->tsk);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
231
232
  	}
  }
ee51636ca   Sebastian Andrzej Siewior   ipc/msg: implemen...
233
234
  static void expunge_all(struct msg_queue *msq, int res,
  			struct wake_q_head *wake_q)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
235
  {
41239fe82   Nikola Pajkovsky   ipc/msg.c: use li...
236
  	struct msg_receiver *msr, *t;
5a06a363e   Ingo Molnar   [PATCH] ipc/msg.c...
237

41239fe82   Nikola Pajkovsky   ipc/msg.c: use li...
238
  	list_for_each_entry_safe(msr, t, &msq->q_receivers, r_list) {
a11ddb37b   Varad Gautam   ipc/mqueue, msg, ...
239
240
241
  		struct task_struct *r_tsk;
  
  		r_tsk = get_task_struct(msr->r_tsk);
0d97a82ba   Manfred Spraul   ipc/msg.c: update...
242
243
244
  
  		/* see MSG_BARRIER for purpose/pairing */
  		smp_store_release(&msr->r_msg, ERR_PTR(res));
a11ddb37b   Varad Gautam   ipc/mqueue, msg, ...
245
  		wake_q_add_safe(wake_q, r_tsk);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
246
247
  	}
  }
5a06a363e   Ingo Molnar   [PATCH] ipc/msg.c...
248
249
250
  
  /*
   * freeque() wakes up waiters on the sender and receiver waiting queue,
f4566f048   Nadia Derbey   ipc: fix wrong co...
251
252
   * 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
253
   *
d9a605e40   Davidlohr Bueso   ipc: rename ids->...
254
255
   * 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
256
   */
01b8b07a5   Pierre Peiffer   IPC: consolidate ...
257
  static void freeque(struct ipc_namespace *ns, struct kern_ipc_perm *ipcp)
4b78e2013   Jules Irenge   ipc/msg: add miss...
258
259
  	__releases(RCU)
  	__releases(&msq->q_perm)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
260
  {
41239fe82   Nikola Pajkovsky   ipc/msg.c: use li...
261
  	struct msg_msg *msg, *t;
01b8b07a5   Pierre Peiffer   IPC: consolidate ...
262
  	struct msg_queue *msq = container_of(ipcp, struct msg_queue, q_perm);
194a6b5b9   Waiman Long   sched/wake_q: Ren...
263
  	DEFINE_WAKE_Q(wake_q);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
264

ee51636ca   Sebastian Andrzej Siewior   ipc/msg: implemen...
265
  	expunge_all(msq, -EIDRM, &wake_q);
ed27f9122   Davidlohr Bueso   ipc/msg: avoid wa...
266
  	ss_wakeup(msq, &wake_q, true);
7ca7e564e   Nadia Derbey   ipc: store ipcs i...
267
  	msg_rmid(ns, msq);
4718787d1   Davidlohr Bueso   ipc,msg: drop msg...
268
  	ipc_unlock_object(&msq->q_perm);
ee51636ca   Sebastian Andrzej Siewior   ipc/msg: implemen...
269
  	wake_up_q(&wake_q);
4718787d1   Davidlohr Bueso   ipc,msg: drop msg...
270
  	rcu_read_unlock();
5a06a363e   Ingo Molnar   [PATCH] ipc/msg.c...
271

41239fe82   Nikola Pajkovsky   ipc/msg.c: use li...
272
  	list_for_each_entry_safe(msg, t, &msq->q_messages, m_list) {
3ac88a41f   Kirill Korotaev   virtualization of...
273
  		atomic_dec(&ns->msg_hdrs);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
274
275
  		free_msg(msg);
  	}
3ac88a41f   Kirill Korotaev   virtualization of...
276
  	atomic_sub(msq->q_cbytes, &ns->msg_bytes);
39a4940ea   Eric W. Biederman   ipc/msg: Fix msgc...
277
278
  	ipc_update_pid(&msq->q_lspid, NULL);
  	ipc_update_pid(&msq->q_lrpid, NULL);
dba4cdd39   Manfred Spraul   ipc: merge ipc_rc...
279
  	ipc_rcu_putref(&msq->q_perm, msg_rcu_free);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
280
  }
3d65661a4   Dominik Brodowski   ipc: add msgget s...
281
  long ksys_msgget(key_t key, int msgflg)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
282
  {
1e7869373   Kirill Korotaev   [PATCH] IPC names...
283
  	struct ipc_namespace *ns;
eb66ec44f   Mathias Krause   ipc: constify ipc...
284
285
  	static const struct ipc_ops msg_ops = {
  		.getnew = newque,
50ab44b1c   Eric W. Biederman   ipc: Directly cal...
286
  		.associate = security_msg_queue_associate,
eb66ec44f   Mathias Krause   ipc: constify ipc...
287
  	};
7748dbfaa   Nadia Derbey   ipc: unify the sy...
288
  	struct ipc_params msg_params;
1e7869373   Kirill Korotaev   [PATCH] IPC names...
289
290
  
  	ns = current->nsproxy->ipc_ns;
7ca7e564e   Nadia Derbey   ipc: store ipcs i...
291

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

7748dbfaa   Nadia Derbey   ipc: unify the sy...
295
  	return ipcget(ns, &msg_ids(ns), &msg_ops, &msg_params);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
296
  }
3d65661a4   Dominik Brodowski   ipc: add msgget s...
297
298
299
300
  SYSCALL_DEFINE2(msgget, key_t, key, int, msgflg)
  {
  	return ksys_msgget(key, msgflg);
  }
5a06a363e   Ingo Molnar   [PATCH] ipc/msg.c...
301
302
  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
303
  {
239521f31   Manfred Spraul   ipc: whitespace c...
304
  	switch (version) {
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
305
  	case IPC_64:
5a06a363e   Ingo Molnar   [PATCH] ipc/msg.c...
306
  		return copy_to_user(buf, in, sizeof(*in));
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
307
  	case IPC_OLD:
5a06a363e   Ingo Molnar   [PATCH] ipc/msg.c...
308
  	{
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
309
  		struct msqid_ds out;
5a06a363e   Ingo Molnar   [PATCH] ipc/msg.c...
310
  		memset(&out, 0, sizeof(out));
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
311
312
313
314
315
316
  
  		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...
317
318
  		if (in->msg_cbytes > USHRT_MAX)
  			out.msg_cbytes	= USHRT_MAX;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
319
320
321
  		else
  			out.msg_cbytes	= in->msg_cbytes;
  		out.msg_lcbytes		= in->msg_cbytes;
4be929be3   Alexey Dobriyan   kernel-wide: repl...
322
323
  		if (in->msg_qnum > USHRT_MAX)
  			out.msg_qnum	= USHRT_MAX;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
324
325
  		else
  			out.msg_qnum	= in->msg_qnum;
4be929be3   Alexey Dobriyan   kernel-wide: repl...
326
327
  		if (in->msg_qbytes > USHRT_MAX)
  			out.msg_qbytes	= USHRT_MAX;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
328
329
330
331
332
333
  		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...
334
335
  		return copy_to_user(buf, &out, sizeof(out));
  	}
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
336
337
338
339
  	default:
  		return -EINVAL;
  	}
  }
5a06a363e   Ingo Molnar   [PATCH] ipc/msg.c...
340
  static inline unsigned long
016d7132f   Pierre Peiffer   IPC: get rid of t...
341
  copy_msqid_from_user(struct msqid64_ds *out, void __user *buf, int version)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
342
  {
239521f31   Manfred Spraul   ipc: whitespace c...
343
  	switch (version) {
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
344
  	case IPC_64:
016d7132f   Pierre Peiffer   IPC: get rid of t...
345
  		if (copy_from_user(out, buf, sizeof(*out)))
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
346
  			return -EFAULT;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
347
  		return 0;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
348
  	case IPC_OLD:
5a06a363e   Ingo Molnar   [PATCH] ipc/msg.c...
349
  	{
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
350
  		struct msqid_ds tbuf_old;
5a06a363e   Ingo Molnar   [PATCH] ipc/msg.c...
351
  		if (copy_from_user(&tbuf_old, buf, sizeof(tbuf_old)))
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
352
  			return -EFAULT;
239521f31   Manfred Spraul   ipc: whitespace c...
353
354
355
  		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
356

5a06a363e   Ingo Molnar   [PATCH] ipc/msg.c...
357
  		if (tbuf_old.msg_qbytes == 0)
016d7132f   Pierre Peiffer   IPC: get rid of t...
358
  			out->msg_qbytes	= tbuf_old.msg_lqbytes;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
359
  		else
016d7132f   Pierre Peiffer   IPC: get rid of t...
360
  			out->msg_qbytes	= tbuf_old.msg_qbytes;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
361
362
  
  		return 0;
5a06a363e   Ingo Molnar   [PATCH] ipc/msg.c...
363
  	}
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
364
365
366
367
  	default:
  		return -EINVAL;
  	}
  }
a0d092fc2   Pierre Peiffer   IPC/message queue...
368
  /*
d9a605e40   Davidlohr Bueso   ipc: rename ids->...
369
   * This function handles some msgctl commands which require the rwsem
a0d092fc2   Pierre Peiffer   IPC/message queue...
370
   * to be held in write mode.
d9a605e40   Davidlohr Bueso   ipc: rename ids->...
371
   * NOTE: no locks must be held, the rwsem is taken inside this function.
a0d092fc2   Pierre Peiffer   IPC/message queue...
372
373
   */
  static int msgctl_down(struct ipc_namespace *ns, int msqid, int cmd,
889b33172   Lu Shuaibing   ipc/msg.c: consol...
374
  			struct ipc64_perm *perm, int msg_qbytes)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
375
  {
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
376
  	struct kern_ipc_perm *ipcp;
a0d092fc2   Pierre Peiffer   IPC/message queue...
377
378
  	struct msg_queue *msq;
  	int err;
d9a605e40   Davidlohr Bueso   ipc: rename ids->...
379
  	down_write(&msg_ids(ns).rwsem);
7b4cc5d84   Davidlohr Bueso   ipc: move locking...
380
  	rcu_read_lock();
4241c1a30   Manfred Spraul   ipc: rename ipcct...
381
  	ipcp = ipcctl_obtain_check(ns, &msg_ids(ns), msqid, cmd,
889b33172   Lu Shuaibing   ipc/msg.c: consol...
382
  				      perm, msg_qbytes);
7b4cc5d84   Davidlohr Bueso   ipc: move locking...
383
384
  	if (IS_ERR(ipcp)) {
  		err = PTR_ERR(ipcp);
7b4cc5d84   Davidlohr Bueso   ipc: move locking...
385
386
  		goto out_unlock1;
  	}
a0d092fc2   Pierre Peiffer   IPC/message queue...
387

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

d8c6e8543   Eric W. Biederman   msg/security: Pas...
390
  	err = security_msg_queue_msgctl(&msq->q_perm, cmd);
a0d092fc2   Pierre Peiffer   IPC/message queue...
391
  	if (err)
15724ecb7   Davidlohr Bueso   ipc,msg: shorten ...
392
  		goto out_unlock1;
a0d092fc2   Pierre Peiffer   IPC/message queue...
393
394
395
  
  	switch (cmd) {
  	case IPC_RMID:
15724ecb7   Davidlohr Bueso   ipc,msg: shorten ...
396
  		ipc_lock_object(&msq->q_perm);
7b4cc5d84   Davidlohr Bueso   ipc: move locking...
397
  		/* freeque unlocks the ipc object and rcu */
a0d092fc2   Pierre Peiffer   IPC/message queue...
398
399
400
  		freeque(ns, ipcp);
  		goto out_up;
  	case IPC_SET:
e3658538b   Davidlohr Bueso   ipc/msg: batch qu...
401
  	{
194a6b5b9   Waiman Long   sched/wake_q: Ren...
402
  		DEFINE_WAKE_Q(wake_q);
e3658538b   Davidlohr Bueso   ipc/msg: batch qu...
403

889b33172   Lu Shuaibing   ipc/msg.c: consol...
404
  		if (msg_qbytes > ns->msg_ctlmnb &&
a0d092fc2   Pierre Peiffer   IPC/message queue...
405
406
  		    !capable(CAP_SYS_RESOURCE)) {
  			err = -EPERM;
15724ecb7   Davidlohr Bueso   ipc,msg: shorten ...
407
  			goto out_unlock1;
a0d092fc2   Pierre Peiffer   IPC/message queue...
408
  		}
15724ecb7   Davidlohr Bueso   ipc,msg: shorten ...
409
  		ipc_lock_object(&msq->q_perm);
889b33172   Lu Shuaibing   ipc/msg.c: consol...
410
  		err = ipc_update_perm(perm, ipcp);
1efdb69b0   Eric W. Biederman   userns: Convert i...
411
  		if (err)
7b4cc5d84   Davidlohr Bueso   ipc: move locking...
412
  			goto out_unlock0;
1efdb69b0   Eric W. Biederman   userns: Convert i...
413

889b33172   Lu Shuaibing   ipc/msg.c: consol...
414
  		msq->q_qbytes = msg_qbytes;
a0d092fc2   Pierre Peiffer   IPC/message queue...
415

50578ea97   Deepa Dinamani   ipc: msg: Make ms...
416
  		msq->q_ctime = ktime_get_real_seconds();
e3658538b   Davidlohr Bueso   ipc/msg: batch qu...
417
418
  		/*
  		 * Sleeping receivers might be excluded by
a0d092fc2   Pierre Peiffer   IPC/message queue...
419
420
  		 * stricter permissions.
  		 */
ee51636ca   Sebastian Andrzej Siewior   ipc/msg: implemen...
421
  		expunge_all(msq, -EAGAIN, &wake_q);
e3658538b   Davidlohr Bueso   ipc/msg: batch qu...
422
423
  		/*
  		 * Sleeping senders might be able to send
a0d092fc2   Pierre Peiffer   IPC/message queue...
424
425
  		 * due to a larger queue size.
  		 */
ed27f9122   Davidlohr Bueso   ipc/msg: avoid wa...
426
  		ss_wakeup(msq, &wake_q, false);
e3658538b   Davidlohr Bueso   ipc/msg: batch qu...
427
428
429
430
431
  		ipc_unlock_object(&msq->q_perm);
  		wake_up_q(&wake_q);
  
  		goto out_unlock1;
  	}
a0d092fc2   Pierre Peiffer   IPC/message queue...
432
433
  	default:
  		err = -EINVAL;
15724ecb7   Davidlohr Bueso   ipc,msg: shorten ...
434
  		goto out_unlock1;
a0d092fc2   Pierre Peiffer   IPC/message queue...
435
  	}
7b4cc5d84   Davidlohr Bueso   ipc: move locking...
436
437
438
439
440
  
  out_unlock0:
  	ipc_unlock_object(&msq->q_perm);
  out_unlock1:
  	rcu_read_unlock();
a0d092fc2   Pierre Peiffer   IPC/message queue...
441
  out_up:
d9a605e40   Davidlohr Bueso   ipc: rename ids->...
442
  	up_write(&msg_ids(ns).rwsem);
a0d092fc2   Pierre Peiffer   IPC/message queue...
443
444
  	return err;
  }
156d9ed12   Al Viro   msgctl(): split t...
445
446
  static int msgctl_info(struct ipc_namespace *ns, int msqid,
  			 int cmd, struct msginfo *msginfo)
a0d092fc2   Pierre Peiffer   IPC/message queue...
447
  {
2cafed30f   Davidlohr Bueso   ipc,msg: introduc...
448
  	int err;
27c331a17   Manfred Spraul   ipc/util.c: furth...
449
  	int max_idx;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
450

156d9ed12   Al Viro   msgctl(): split t...
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
  	/*
  	 * 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
475
  	}
27c331a17   Manfred Spraul   ipc/util.c: furth...
476
  	max_idx = ipc_get_maxidx(&msg_ids(ns));
156d9ed12   Al Viro   msgctl(): split t...
477
  	up_read(&msg_ids(ns).rwsem);
27c331a17   Manfred Spraul   ipc/util.c: furth...
478
  	return (max_idx < 0) ? 0 : max_idx;
156d9ed12   Al Viro   msgctl(): split t...
479
  }
2cafed30f   Davidlohr Bueso   ipc,msg: introduc...
480

156d9ed12   Al Viro   msgctl(): split t...
481
482
483
  static int msgctl_stat(struct ipc_namespace *ns, int msqid,
  			 int cmd, struct msqid64_ds *p)
  {
156d9ed12   Al Viro   msgctl(): split t...
484
  	struct msg_queue *msq;
87ad4b0d8   Philippe Mikoyan   ipc: fix ipc data...
485
  	int err;
ac0ba20ea   Davidlohr Bueso   ipc,msg: make msg...
486

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

156d9ed12   Al Viro   msgctl(): split t...
489
  	rcu_read_lock();
23c8cec8c   Davidlohr Bueso   ipc/msg: introduc...
490
  	if (cmd == MSG_STAT || cmd == MSG_STAT_ANY) {
156d9ed12   Al Viro   msgctl(): split t...
491
492
493
  		msq = msq_obtain_object(ns, msqid);
  		if (IS_ERR(msq)) {
  			err = PTR_ERR(msq);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
494
  			goto out_unlock;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
495
  		}
23c8cec8c   Davidlohr Bueso   ipc/msg: introduc...
496
  	} else { /* IPC_STAT */
156d9ed12   Al Viro   msgctl(): split t...
497
498
499
  		msq = msq_obtain_object_check(ns, msqid);
  		if (IS_ERR(msq)) {
  			err = PTR_ERR(msq);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
500
  			goto out_unlock;
156d9ed12   Al Viro   msgctl(): split t...
501
  		}
156d9ed12   Al Viro   msgctl(): split t...
502
  	}
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
503

23c8cec8c   Davidlohr Bueso   ipc/msg: introduc...
504
505
506
507
508
509
510
511
  	/* 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...
512

d8c6e8543   Eric W. Biederman   msg/security: Pas...
513
  	err = security_msg_queue_msgctl(&msq->q_perm, cmd);
156d9ed12   Al Viro   msgctl(): split t...
514
515
  	if (err)
  		goto out_unlock;
87ad4b0d8   Philippe Mikoyan   ipc: fix ipc data...
516
517
518
519
520
521
522
  	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...
523
524
525
526
  	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...
527
528
529
530
531
  #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...
532
533
534
  	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...
535
536
  	p->msg_lspid  = pid_vnr(msq->q_lspid);
  	p->msg_lrpid  = pid_vnr(msq->q_lrpid);
2cafed30f   Davidlohr Bueso   ipc,msg: introduc...
537

615c999cd   Manfred Spraul   ipc: compute kern...
538
539
540
541
542
543
544
545
546
547
548
549
550
  	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
551

615c999cd   Manfred Spraul   ipc: compute kern...
552
  	ipc_unlock_object(&msq->q_perm);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
553
  out_unlock:
ac0ba20ea   Davidlohr Bueso   ipc,msg: make msg...
554
  	rcu_read_unlock();
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
555
556
  	return err;
  }
275f22148   Arnd Bergmann   ipc: rename old-s...
557
  static long ksys_msgctl(int msqid, int cmd, struct msqid_ds __user *buf, int version)
2cafed30f   Davidlohr Bueso   ipc,msg: introduc...
558
  {
2cafed30f   Davidlohr Bueso   ipc,msg: introduc...
559
  	struct ipc_namespace *ns;
156d9ed12   Al Viro   msgctl(): split t...
560
561
  	struct msqid64_ds msqid64;
  	int err;
2cafed30f   Davidlohr Bueso   ipc,msg: introduc...
562
563
564
  
  	if (msqid < 0 || cmd < 0)
  		return -EINVAL;
2cafed30f   Davidlohr Bueso   ipc,msg: introduc...
565
566
567
568
  	ns = current->nsproxy->ipc_ns;
  
  	switch (cmd) {
  	case IPC_INFO:
156d9ed12   Al Viro   msgctl(): split t...
569
570
571
572
573
574
575
576
577
  	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...
578
  	case MSG_STAT:	/* msqid is an index rather than a msg queue id */
23c8cec8c   Davidlohr Bueso   ipc/msg: introduc...
579
  	case MSG_STAT_ANY:
2cafed30f   Davidlohr Bueso   ipc,msg: introduc...
580
  	case IPC_STAT:
156d9ed12   Al Viro   msgctl(): split t...
581
582
583
584
585
586
  		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...
587
  	case IPC_SET:
156d9ed12   Al Viro   msgctl(): split t...
588
589
  		if (copy_msqid_from_user(&msqid64, buf, version))
  			return -EFAULT;
889b33172   Lu Shuaibing   ipc/msg.c: consol...
590
591
  		return msgctl_down(ns, msqid, cmd, &msqid64.msg_perm,
  				   msqid64.msg_qbytes);
2cafed30f   Davidlohr Bueso   ipc,msg: introduc...
592
  	case IPC_RMID:
889b33172   Lu Shuaibing   ipc/msg.c: consol...
593
  		return msgctl_down(ns, msqid, cmd, NULL, 0);
2cafed30f   Davidlohr Bueso   ipc,msg: introduc...
594
595
596
597
  	default:
  		return  -EINVAL;
  	}
  }
e340db564   Dominik Brodowski   ipc: add msgctl s...
598
599
  SYSCALL_DEFINE3(msgctl, int, msqid, int, cmd, struct msqid_ds __user *, buf)
  {
275f22148   Arnd Bergmann   ipc: rename old-s...
600
  	return ksys_msgctl(msqid, cmd, buf, IPC_64);
e340db564   Dominik Brodowski   ipc: add msgctl s...
601
  }
275f22148   Arnd Bergmann   ipc: rename old-s...
602
603
604
605
606
607
608
609
610
611
612
613
614
  #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...
615
616
617
618
619
620
  #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...
621
622
623
  	old_time32_t msg_stime;
  	old_time32_t msg_rtime;
  	old_time32_t msg_ctime;
469391684   Al Viro   msgctl(): move co...
624
625
626
627
628
629
630
631
632
633
634
635
636
637
  	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...
638
  		struct compat_msqid64_ds __user *p = buf;
28327fae6   Al Viro   ipc: make use of ...
639
  		if (get_compat_ipc64_perm(&out->msg_perm, &p->msg_perm))
469391684   Al Viro   msgctl(): move co...
640
  			return -EFAULT;
469391684   Al Viro   msgctl(): move co...
641
642
643
  		if (get_user(out->msg_qbytes, &p->msg_qbytes))
  			return -EFAULT;
  	} else {
6aa211e8c   Linus Torvalds   fix address space...
644
  		struct compat_msqid_ds __user *p = buf;
28327fae6   Al Viro   ipc: make use of ...
645
  		if (get_compat_ipc_perm(&out->msg_perm, &p->msg_perm))
469391684   Al Viro   msgctl(): move co...
646
  			return -EFAULT;
469391684   Al Viro   msgctl(): move co...
647
648
649
650
651
652
653
654
655
656
657
658
  		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 ...
659
  		to_compat_ipc64_perm(&v.msg_perm, &in->msg_perm);
c2ab975c3   Arnd Bergmann   y2038: ipc: Repor...
660
661
662
663
664
665
  		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...
666
667
668
669
670
671
672
673
674
  		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 ...
675
  		to_compat_ipc_perm(&v.msg_perm, &in->msg_perm);
469391684   Al Viro   msgctl(): move co...
676
677
678
679
680
681
682
683
684
685
686
  		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...
687
  static long compat_ksys_msgctl(int msqid, int cmd, void __user *uptr, int version)
469391684   Al Viro   msgctl(): move co...
688
689
690
691
  {
  	struct ipc_namespace *ns;
  	int err;
  	struct msqid64_ds msqid64;
469391684   Al Viro   msgctl(): move co...
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
  
  	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...
711
  	case MSG_STAT_ANY:
469391684   Al Viro   msgctl(): move co...
712
713
714
715
716
717
718
719
720
  		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;
889b33172   Lu Shuaibing   ipc/msg.c: consol...
721
  		return msgctl_down(ns, msqid, cmd, &msqid64.msg_perm, msqid64.msg_qbytes);
469391684   Al Viro   msgctl(): move co...
722
  	case IPC_RMID:
889b33172   Lu Shuaibing   ipc/msg.c: consol...
723
  		return msgctl_down(ns, msqid, cmd, NULL, 0);
469391684   Al Viro   msgctl(): move co...
724
725
726
727
  	default:
  		return -EINVAL;
  	}
  }
e340db564   Dominik Brodowski   ipc: add msgctl s...
728
729
730
  
  COMPAT_SYSCALL_DEFINE3(msgctl, int, msqid, int, cmd, void __user *, uptr)
  {
275f22148   Arnd Bergmann   ipc: rename old-s...
731
  	return compat_ksys_msgctl(msqid, cmd, uptr, IPC_64);
e340db564   Dominik Brodowski   ipc: add msgctl s...
732
  }
275f22148   Arnd Bergmann   ipc: rename old-s...
733
734
735
736
737
738
739
740
741
742
743
744
745
746
  
  #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...
747
  #endif
5a06a363e   Ingo Molnar   [PATCH] ipc/msg.c...
748
  static int testmsg(struct msg_msg *msg, long type, int mode)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
749
  {
46c0a8ca3   Paul McQuade   ipc, kernel: clea...
750
751
752
753
754
755
756
757
758
759
  	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
760
  			return 1;
46c0a8ca3   Paul McQuade   ipc, kernel: clea...
761
762
763
764
765
  		break;
  	case SEARCH_NOTEQUAL:
  		if (msg->m_type != type)
  			return 1;
  		break;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
766
767
768
  	}
  	return 0;
  }
ee51636ca   Sebastian Andrzej Siewior   ipc/msg: implemen...
769
770
  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
771
  {
41239fe82   Nikola Pajkovsky   ipc/msg.c: use li...
772
  	struct msg_receiver *msr, *t;
5a06a363e   Ingo Molnar   [PATCH] ipc/msg.c...
773

41239fe82   Nikola Pajkovsky   ipc/msg.c: use li...
774
  	list_for_each_entry_safe(msr, t, &msq->q_receivers, r_list) {
5a06a363e   Ingo Molnar   [PATCH] ipc/msg.c...
775
  		if (testmsg(msg, msr->r_msgtype, msr->r_mode) &&
d8c6e8543   Eric W. Biederman   msg/security: Pas...
776
  		    !security_msg_queue_msgrcv(&msq->q_perm, msg, msr->r_tsk,
5a06a363e   Ingo Molnar   [PATCH] ipc/msg.c...
777
  					       msr->r_msgtype, msr->r_mode)) {
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
778
  			list_del(&msr->r_list);
5a06a363e   Ingo Molnar   [PATCH] ipc/msg.c...
779
  			if (msr->r_maxsize < msg->m_ts) {
ee51636ca   Sebastian Andrzej Siewior   ipc/msg: implemen...
780
  				wake_q_add(wake_q, msr->r_tsk);
0d97a82ba   Manfred Spraul   ipc/msg.c: update...
781
782
783
  
  				/* See expunge_all regarding memory barrier */
  				smp_store_release(&msr->r_msg, ERR_PTR(-E2BIG));
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
784
  			} else {
39a4940ea   Eric W. Biederman   ipc/msg: Fix msgc...
785
  				ipc_update_pid(&msq->q_lrpid, task_pid(msr->r_tsk));
2a70b7879   Arnd Bergmann   y2038: ipc: Use k...
786
  				msq->q_rtime = ktime_get_real_seconds();
5a06a363e   Ingo Molnar   [PATCH] ipc/msg.c...
787

ee51636ca   Sebastian Andrzej Siewior   ipc/msg: implemen...
788
  				wake_q_add(wake_q, msr->r_tsk);
0d97a82ba   Manfred Spraul   ipc/msg.c: update...
789
790
791
  
  				/* See expunge_all regarding memory barrier */
  				smp_store_release(&msr->r_msg, msg);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
792
793
794
795
  				return 1;
  			}
  		}
  	}
ffa571daf   Davidlohr Bueso   ipc,msg: document...
796

1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
797
798
  	return 0;
  }
9b1404c24   Al Viro   msgrcv(2), msgsnd...
799
  static long do_msgsnd(int msqid, long mtype, void __user *mtext,
651971cb7   suzuki   [PATCH] Fix the s...
800
  		size_t msgsz, int msgflg)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
801
802
803
  {
  	struct msg_queue *msq;
  	struct msg_msg *msg;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
804
  	int err;
1e7869373   Kirill Korotaev   [PATCH] IPC names...
805
  	struct ipc_namespace *ns;
194a6b5b9   Waiman Long   sched/wake_q: Ren...
806
  	DEFINE_WAKE_Q(wake_q);
1e7869373   Kirill Korotaev   [PATCH] IPC names...
807
808
  
  	ns = current->nsproxy->ipc_ns;
5a06a363e   Ingo Molnar   [PATCH] ipc/msg.c...
809

1e7869373   Kirill Korotaev   [PATCH] IPC names...
810
  	if (msgsz > ns->msg_ctlmax || (long) msgsz < 0 || msqid < 0)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
811
  		return -EINVAL;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
812
813
  	if (mtype < 1)
  		return -EINVAL;
651971cb7   suzuki   [PATCH] Fix the s...
814
  	msg = load_msg(mtext, msgsz);
5a06a363e   Ingo Molnar   [PATCH] ipc/msg.c...
815
  	if (IS_ERR(msg))
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
816
817
818
819
  		return PTR_ERR(msg);
  
  	msg->m_type = mtype;
  	msg->m_ts = msgsz;
3dd1f784e   Davidlohr Bueso   ipc,msg: shorten ...
820
821
  	rcu_read_lock();
  	msq = msq_obtain_object_check(ns, msqid);
023a53557   Nadia Derbey   ipc: integrate ip...
822
823
  	if (IS_ERR(msq)) {
  		err = PTR_ERR(msq);
3dd1f784e   Davidlohr Bueso   ipc,msg: shorten ...
824
  		goto out_unlock1;
023a53557   Nadia Derbey   ipc: integrate ip...
825
  	}
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
826

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

4271b05a2   Davidlohr Bueso   ipc,msg: prevent ...
834
  		/* raced with RMID? */
0f3d2b013   Rafael Aquini   ipc: introduce ip...
835
  		if (!ipc_valid_object(&msq->q_perm)) {
4271b05a2   Davidlohr Bueso   ipc,msg: prevent ...
836
837
838
  			err = -EIDRM;
  			goto out_unlock0;
  		}
d8c6e8543   Eric W. Biederman   msg/security: Pas...
839
  		err = security_msg_queue_msgsnd(&msq->q_perm, msg, msgflg);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
840
  		if (err)
bebcb928c   Manfred Spraul   ipc/msg.c: Fix lo...
841
  			goto out_unlock0;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
842

ed27f9122   Davidlohr Bueso   ipc/msg: avoid wa...
843
  		if (msg_fits_inqueue(msq, msgsz))
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
844
  			break;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
845
846
  
  		/* queue full, wait: */
5a06a363e   Ingo Molnar   [PATCH] ipc/msg.c...
847
848
  		if (msgflg & IPC_NOWAIT) {
  			err = -EAGAIN;
bebcb928c   Manfred Spraul   ipc/msg.c: Fix lo...
849
  			goto out_unlock0;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
850
  		}
3dd1f784e   Davidlohr Bueso   ipc,msg: shorten ...
851

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

dba4cdd39   Manfred Spraul   ipc: merge ipc_rc...
855
  		if (!ipc_rcu_getref(&msq->q_perm)) {
6062a8dc0   Rik van Riel   ipc,sem: fine gra...
856
  			err = -EIDRM;
3dd1f784e   Davidlohr Bueso   ipc,msg: shorten ...
857
  			goto out_unlock0;
6062a8dc0   Rik van Riel   ipc,sem: fine gra...
858
  		}
3dd1f784e   Davidlohr Bueso   ipc,msg: shorten ...
859
860
  		ipc_unlock_object(&msq->q_perm);
  		rcu_read_unlock();
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
861
  		schedule();
3dd1f784e   Davidlohr Bueso   ipc,msg: shorten ...
862
863
  		rcu_read_lock();
  		ipc_lock_object(&msq->q_perm);
dba4cdd39   Manfred Spraul   ipc: merge ipc_rc...
864
  		ipc_rcu_putref(&msq->q_perm, msg_rcu_free);
0f3d2b013   Rafael Aquini   ipc: introduce ip...
865
866
  		/* raced with RMID? */
  		if (!ipc_valid_object(&msq->q_perm)) {
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
867
  			err = -EIDRM;
3dd1f784e   Davidlohr Bueso   ipc,msg: shorten ...
868
  			goto out_unlock0;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
869
870
  		}
  		ss_del(&s);
5a06a363e   Ingo Molnar   [PATCH] ipc/msg.c...
871

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

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

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

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

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

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

4a674f34b   Stanislav Kinsbursky   ipc: introduce me...
1040
  	if (msgflg & MSG_COPY) {
4f87dac38   Michael Kerrisk   ipc: Fix 2 bugs i...
1041
1042
  		if ((msgflg & MSG_EXCEPT) || !(msgflg & IPC_NOWAIT))
  			return -EINVAL;
8ac6ed585   Peter Hurley   ipc: implement MS...
1043
  		copy = prepare_copy(buf, min_t(size_t, bufsz, ns->msg_ctlmax));
4a674f34b   Stanislav Kinsbursky   ipc: introduce me...
1044
1045
1046
  		if (IS_ERR(copy))
  			return PTR_ERR(copy);
  	}
5a06a363e   Ingo Molnar   [PATCH] ipc/msg.c...
1047
  	mode = convert_mode(&msgtyp, msgflg);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1048

41a0d523d   Davidlohr Bueso   ipc,msg: shorten ...
1049
1050
  	rcu_read_lock();
  	msq = msq_obtain_object_check(ns, msqid);
4a674f34b   Stanislav Kinsbursky   ipc: introduce me...
1051
  	if (IS_ERR(msq)) {
41a0d523d   Davidlohr Bueso   ipc,msg: shorten ...
1052
  		rcu_read_unlock();
85398aa8d   Stanislav Kinsbursky   ipc: simplify fre...
1053
  		free_copy(copy);
023a53557   Nadia Derbey   ipc: integrate ip...
1054
  		return PTR_ERR(msq);
4a674f34b   Stanislav Kinsbursky   ipc: introduce me...
1055
  	}
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1056
1057
1058
  
  	for (;;) {
  		struct msg_receiver msr_d;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1059
1060
  
  		msg = ERR_PTR(-EACCES);
b0e77598f   Serge E. Hallyn   userns: user name...
1061
  		if (ipcperms(ns, &msq->q_perm, S_IRUGO))
41a0d523d   Davidlohr Bueso   ipc,msg: shorten ...
1062
  			goto out_unlock1;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1063

41a0d523d   Davidlohr Bueso   ipc,msg: shorten ...
1064
  		ipc_lock_object(&msq->q_perm);
4271b05a2   Davidlohr Bueso   ipc,msg: prevent ...
1065
1066
  
  		/* raced with RMID? */
0f3d2b013   Rafael Aquini   ipc: introduce ip...
1067
  		if (!ipc_valid_object(&msq->q_perm)) {
4271b05a2   Davidlohr Bueso   ipc,msg: prevent ...
1068
1069
1070
  			msg = ERR_PTR(-EIDRM);
  			goto out_unlock0;
  		}
daaf74cf0   Peter Hurley   ipc: refactor msg...
1071
  		msg = find_msg(msq, &msgtyp, mode);
5a06a363e   Ingo Molnar   [PATCH] ipc/msg.c...
1072
1073
1074
1075
1076
  		if (!IS_ERR(msg)) {
  			/*
  			 * Found a suitable message.
  			 * Unlink it from the queue.
  			 */
f9dd87f47   Stanislav Kinsbursky   ipc: message queu...
1077
  			if ((bufsz < msg->m_ts) && !(msgflg & MSG_NOERROR)) {
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1078
  				msg = ERR_PTR(-E2BIG);
41a0d523d   Davidlohr Bueso   ipc,msg: shorten ...
1079
  				goto out_unlock0;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1080
  			}
3fcfe7865   Stanislav Kinsbursky   ipc: add more com...
1081
1082
1083
1084
  			/*
  			 * If we are copying, then do not unlink message and do
  			 * not update queue parameters.
  			 */
852028af8   Peter Hurley   ipc: remove msg h...
1085
1086
  			if (msgflg & MSG_COPY) {
  				msg = copy_msg(msg, copy);
41a0d523d   Davidlohr Bueso   ipc,msg: shorten ...
1087
  				goto out_unlock0;
852028af8   Peter Hurley   ipc: remove msg h...
1088
  			}
41a0d523d   Davidlohr Bueso   ipc,msg: shorten ...
1089

1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1090
1091
  			list_del(&msg->m_list);
  			msq->q_qnum--;
2a70b7879   Arnd Bergmann   y2038: ipc: Use k...
1092
  			msq->q_rtime = ktime_get_real_seconds();
39a4940ea   Eric W. Biederman   ipc/msg: Fix msgc...
1093
  			ipc_update_pid(&msq->q_lrpid, task_tgid(current));
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1094
  			msq->q_cbytes -= msg->m_ts;
3ac88a41f   Kirill Korotaev   virtualization of...
1095
1096
  			atomic_sub(msg->m_ts, &ns->msg_bytes);
  			atomic_dec(&ns->msg_hdrs);
ed27f9122   Davidlohr Bueso   ipc/msg: avoid wa...
1097
  			ss_wakeup(msq, &wake_q, false);
41a0d523d   Davidlohr Bueso   ipc,msg: shorten ...
1098
1099
  
  			goto out_unlock0;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1100
  		}
41a0d523d   Davidlohr Bueso   ipc,msg: shorten ...
1101

1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1102
1103
1104
  		/* No message waiting. Wait for a message */
  		if (msgflg & IPC_NOWAIT) {
  			msg = ERR_PTR(-ENOMSG);
41a0d523d   Davidlohr Bueso   ipc,msg: shorten ...
1105
  			goto out_unlock0;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1106
  		}
41a0d523d   Davidlohr Bueso   ipc,msg: shorten ...
1107

5a06a363e   Ingo Molnar   [PATCH] ipc/msg.c...
1108
  		list_add_tail(&msr_d.r_list, &msq->q_receivers);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1109
1110
1111
  		msr_d.r_tsk = current;
  		msr_d.r_msgtype = msgtyp;
  		msr_d.r_mode = mode;
5a06a363e   Ingo Molnar   [PATCH] ipc/msg.c...
1112
  		if (msgflg & MSG_NOERROR)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1113
  			msr_d.r_maxsize = INT_MAX;
5a06a363e   Ingo Molnar   [PATCH] ipc/msg.c...
1114
  		else
f9dd87f47   Stanislav Kinsbursky   ipc: message queu...
1115
  			msr_d.r_maxsize = bufsz;
0d97a82ba   Manfred Spraul   ipc/msg.c: update...
1116
1117
1118
1119
1120
  
  		/* memory barrier not require due to ipc_lock_object() */
  		WRITE_ONCE(msr_d.r_msg, ERR_PTR(-EAGAIN));
  
  		/* memory barrier not required, we own ipc_lock_object() */
f75a2f358   Davidlohr Bueso   ipc,msg: use curr...
1121
  		__set_current_state(TASK_INTERRUPTIBLE);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1122

41a0d523d   Davidlohr Bueso   ipc,msg: shorten ...
1123
1124
  		ipc_unlock_object(&msq->q_perm);
  		rcu_read_unlock();
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1125
  		schedule();
ee51636ca   Sebastian Andrzej Siewior   ipc/msg: implemen...
1126
1127
1128
1129
1130
1131
  		/*
  		 * 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
1132
1133
  		 * 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
1134
1135
  		 */
  		rcu_read_lock();
ee51636ca   Sebastian Andrzej Siewior   ipc/msg: implemen...
1136
1137
1138
1139
1140
1141
  		/*
  		 * 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 ...
1142
  		 *
ee51636ca   Sebastian Andrzej Siewior   ipc/msg: implemen...
1143
1144
  		 * 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
1145
  		 */
ee51636ca   Sebastian Andrzej Siewior   ipc/msg: implemen...
1146
  		msg = READ_ONCE(msr_d.r_msg);
0d97a82ba   Manfred Spraul   ipc/msg.c: update...
1147
1148
1149
  		if (msg != ERR_PTR(-EAGAIN)) {
  			/* see MSG_BARRIER for purpose/pairing */
  			smp_acquire__after_ctrl_dep();
41a0d523d   Davidlohr Bueso   ipc,msg: shorten ...
1150
  			goto out_unlock1;
0d97a82ba   Manfred Spraul   ipc/msg.c: update...
1151
  		}
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1152

ee51636ca   Sebastian Andrzej Siewior   ipc/msg: implemen...
1153
1154
1155
1156
  		 /*
  		  * ... or see -EAGAIN, acquire the lock to check the message
  		  * again.
  		  */
41a0d523d   Davidlohr Bueso   ipc,msg: shorten ...
1157
  		ipc_lock_object(&msq->q_perm);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1158

0d97a82ba   Manfred Spraul   ipc/msg.c: update...
1159
  		msg = READ_ONCE(msr_d.r_msg);
5a06a363e   Ingo Molnar   [PATCH] ipc/msg.c...
1160
  		if (msg != ERR_PTR(-EAGAIN))
41a0d523d   Davidlohr Bueso   ipc,msg: shorten ...
1161
  			goto out_unlock0;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1162
1163
1164
1165
  
  		list_del(&msr_d.r_list);
  		if (signal_pending(current)) {
  			msg = ERR_PTR(-ERESTARTNOHAND);
41a0d523d   Davidlohr Bueso   ipc,msg: shorten ...
1166
  			goto out_unlock0;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1167
  		}
41a0d523d   Davidlohr Bueso   ipc,msg: shorten ...
1168
1169
  
  		ipc_unlock_object(&msq->q_perm);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1170
  	}
41a0d523d   Davidlohr Bueso   ipc,msg: shorten ...
1171
1172
1173
  
  out_unlock0:
  	ipc_unlock_object(&msq->q_perm);
e3658538b   Davidlohr Bueso   ipc/msg: batch qu...
1174
  	wake_up_q(&wake_q);
41a0d523d   Davidlohr Bueso   ipc,msg: shorten ...
1175
1176
  out_unlock1:
  	rcu_read_unlock();
4a674f34b   Stanislav Kinsbursky   ipc: introduce me...
1177
  	if (IS_ERR(msg)) {
85398aa8d   Stanislav Kinsbursky   ipc: simplify fre...
1178
  		free_copy(copy);
5a06a363e   Ingo Molnar   [PATCH] ipc/msg.c...
1179
  		return PTR_ERR(msg);
4a674f34b   Stanislav Kinsbursky   ipc: introduce me...
1180
  	}
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1181

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

f9dd87f47   Stanislav Kinsbursky   ipc: message queu...
1185
  	return bufsz;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1186
  }
078faac9e   Dominik Brodowski   ipc: add msgrcv s...
1187
1188
1189
1190
1191
  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...
1192
1193
  SYSCALL_DEFINE5(msgrcv, int, msqid, struct msgbuf __user *, msgp, size_t, msgsz,
  		long, msgtyp, int, msgflg)
651971cb7   suzuki   [PATCH] Fix the s...
1194
  {
078faac9e   Dominik Brodowski   ipc: add msgrcv s...
1195
  	return ksys_msgrcv(msqid, msgp, msgsz, msgtyp, msgflg);
651971cb7   suzuki   [PATCH] Fix the s...
1196
  }
9b1404c24   Al Viro   msgrcv(2), msgsnd...
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
  #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...
1211
1212
  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...
1213
1214
1215
1216
  {
  	return do_msgrcv(msqid, compat_ptr(msgp), (ssize_t)msgsz, (long)msgtyp,
  			 msgflg, compat_do_msg_fill);
  }
078faac9e   Dominik Brodowski   ipc: add msgrcv s...
1217
1218
1219
1220
1221
1222
1223
  
  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...
1224
  #endif
3440a6bd1   Davidlohr Bueso   ipc,msg: move som...
1225

eae04d25a   Davidlohr Bueso   ipc: simplify ipc...
1226
  void msg_init_ns(struct ipc_namespace *ns)
3440a6bd1   Davidlohr Bueso   ipc,msg: move som...
1227
1228
1229
  {
  	ns->msg_ctlmax = MSGMAX;
  	ns->msg_ctlmnb = MSGMNB;
0050ee059   Manfred Spraul   ipc/msg: increase...
1230
  	ns->msg_ctlmni = MSGMNI;
3440a6bd1   Davidlohr Bueso   ipc,msg: move som...
1231
1232
1233
  
  	atomic_set(&ns->msg_bytes, 0);
  	atomic_set(&ns->msg_hdrs, 0);
eae04d25a   Davidlohr Bueso   ipc: simplify ipc...
1234
  	ipc_init_ids(&ns->ids[IPC_MSG_IDS]);
3440a6bd1   Davidlohr Bueso   ipc,msg: move som...
1235
1236
1237
1238
1239
1240
1241
  }
  
  #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...
1242
  	rhashtable_destroy(&ns->ids[IPC_MSG_IDS].key_ht);
3440a6bd1   Davidlohr Bueso   ipc,msg: move som...
1243
1244
  }
  #endif
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1245
  #ifdef CONFIG_PROC_FS
19b4946ca   Mike Waychison   [PATCH] ipc: conv...
1246
  static int sysvipc_msg_proc_show(struct seq_file *s, void *it)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1247
  {
39a4940ea   Eric W. Biederman   ipc/msg: Fix msgc...
1248
  	struct pid_namespace *pid_ns = ipc_seq_pid_ns(s);
1efdb69b0   Eric W. Biederman   userns: Convert i...
1249
  	struct user_namespace *user_ns = seq_user_ns(s);
ade9f91b3   Kees Cook   ipc: add missing ...
1250
1251
  	struct kern_ipc_perm *ipcp = it;
  	struct msg_queue *msq = container_of(ipcp, struct msg_queue, q_perm);
19b4946ca   Mike Waychison   [PATCH] ipc: conv...
1252

7f032d6ef   Joe Perches   ipc: remove use o...
1253
  	seq_printf(s,
50578ea97   Deepa Dinamani   ipc: msg: Make ms...
1254
1255
  		   "%10d %10d  %4o  %10lu %10lu %5u %5u %5u %5u %5u %5u %10llu %10llu %10llu
  ",
7f032d6ef   Joe Perches   ipc: remove use o...
1256
1257
1258
1259
1260
  		   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...
1261
1262
  		   pid_nr_ns(msq->q_lspid, pid_ns),
  		   pid_nr_ns(msq->q_lrpid, pid_ns),
7f032d6ef   Joe Perches   ipc: remove use o...
1263
1264
1265
1266
1267
1268
1269
1270
1271
  		   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
1272
1273
  }
  #endif
3440a6bd1   Davidlohr Bueso   ipc,msg: move som...
1274

eae04d25a   Davidlohr Bueso   ipc: simplify ipc...
1275
  void __init msg_init(void)
3440a6bd1   Davidlohr Bueso   ipc,msg: move som...
1276
  {
eae04d25a   Davidlohr Bueso   ipc: simplify ipc...
1277
  	msg_init_ns(&init_ipc_ns);
3440a6bd1   Davidlohr Bueso   ipc,msg: move som...
1278

3440a6bd1   Davidlohr Bueso   ipc,msg: move som...
1279
1280
1281
1282
1283
  	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);
  }