Blame view

ipc/util.c 22.3 KB
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1
2
3
4
5
6
7
8
9
  /*
   * linux/ipc/util.c
   * Copyright (C) 1992 Krishna Balasubramanian
   *
   * Sep 1997 - Call suser() last after "normal" permission checks so we
   *            get BSD style process accounting right.
   *            Occurs in several places in the IPC code.
   *            Chris Evans, <chris@ferret.lmh.ox.ac.uk>
   * Nov 1999 - ipc helper functions, unified SMP locking
624dffcbc   Christian Kujau   correct email add...
10
   *	      Manfred Spraul <manfred@colorfullife.com>
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
11
12
   * Oct 2002 - One lock per IPC id. RCU ipc_free for lock-free grow_ary().
   *            Mingming Cao <cmm@us.ibm.com>
073115d6b   Steve Grubb   [PATCH] Rework of...
13
14
   * Mar 2006 - support for audit of ipc object properties
   *            Dustin Kirkland <dustin.kirkland@us.ibm.com>
73ea41302   Kirill Korotaev   [PATCH] IPC names...
15
16
17
   * Jun 2006 - namespaces ssupport
   *            OpenVZ, SWsoft Inc.
   *            Pavel Emelianov <xemul@openvz.org>
05603c44a   Davidlohr Bueso   ipc: document gen...
18
19
   *
   * General sysv ipc locking scheme:
18ccee263   Davidlohr Bueso   ipc: update locki...
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
   *	rcu_read_lock()
   *          obtain the ipc object (kern_ipc_perm) by looking up the id in an idr
   *	    tree.
   *	    - perform initial checks (capabilities, auditing and permission,
   *	      etc).
   *	    - perform read-only operations, such as STAT, INFO commands.
   *	      acquire the ipc lock (kern_ipc_perm.lock) through
   *	      ipc_lock_object()
   *		- perform data updates, such as SET, RMID commands and
   *		  mechanism-specific operations (semop/semtimedop,
   *		  msgsnd/msgrcv, shmat/shmdt).
   *	    drop the ipc lock, through ipc_unlock_object().
   *	rcu_read_unlock()
   *
   *  The ids->rwsem must be taken when:
   *	- creating, removing and iterating the existing entries in ipc
   *	  identifier sets.
   *	- iterating through files under /proc/sysvipc/
   *
   *  Note that sems have a special fast path that avoids kern_ipc_perm.lock -
   *  see sem_lock().
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
41
   */
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
42
43
44
45
  #include <linux/mm.h>
  #include <linux/shm.h>
  #include <linux/init.h>
  #include <linux/msg.h>
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
46
47
  #include <linux/vmalloc.h>
  #include <linux/slab.h>
8f68fa2d1   Andrew Morton   ipc/util.c: use r...
48
  #include <linux/notifier.h>
c59ede7b7   Randy.Dunlap   [PATCH] move capa...
49
  #include <linux/capability.h>
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
50
51
52
53
  #include <linux/highuid.h>
  #include <linux/security.h>
  #include <linux/rcupdate.h>
  #include <linux/workqueue.h>
ae7817745   Mike Waychison   [PATCH] ipc: add ...
54
55
  #include <linux/seq_file.h>
  #include <linux/proc_fs.h>
073115d6b   Steve Grubb   [PATCH] Rework of...
56
  #include <linux/audit.h>
73ea41302   Kirill Korotaev   [PATCH] IPC names...
57
  #include <linux/nsproxy.h>
3e148c799   Nadia Derbey   fix idr_find() lo...
58
  #include <linux/rwsem.h>
b6b337ad1   Nadia Derbey   ipc: recompute ms...
59
  #include <linux/memory.h>
ae5e1b22f   Pavel Emelyanov   namespaces: move ...
60
  #include <linux/ipc_namespace.h>
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
61
62
63
64
  
  #include <asm/unistd.h>
  
  #include "util.h"
ae7817745   Mike Waychison   [PATCH] ipc: add ...
65
66
67
  struct ipc_proc_iface {
  	const char *path;
  	const char *header;
73ea41302   Kirill Korotaev   [PATCH] IPC names...
68
  	int ids;
ae7817745   Mike Waychison   [PATCH] ipc: add ...
69
70
  	int (*show)(struct seq_file *, void *);
  };
424450c1d   Nadia Derbey   ipc: invoke the i...
71
72
73
74
  static void ipc_memory_notifier(struct work_struct *work)
  {
  	ipcns_notify(IPCNS_MEMCHANGED);
  }
b6b337ad1   Nadia Derbey   ipc: recompute ms...
75
76
77
  static int ipc_memory_callback(struct notifier_block *self,
  				unsigned long action, void *arg)
  {
8f68fa2d1   Andrew Morton   ipc/util.c: use r...
78
  	static DECLARE_WORK(ipc_memory_wq, ipc_memory_notifier);
b6b337ad1   Nadia Derbey   ipc: recompute ms...
79
80
81
82
83
84
  	switch (action) {
  	case MEM_ONLINE:    /* memory successfully brought online */
  	case MEM_OFFLINE:   /* or offline: it's time to recompute msgmni */
  		/*
  		 * This is done by invoking the ipcns notifier chain with the
  		 * IPC_MEMCHANGED event.
424450c1d   Nadia Derbey   ipc: invoke the i...
85
86
87
  		 * In order not to keep the lock on the hotplug memory chain
  		 * for too long, queue a work item that will, when waken up,
  		 * activate the ipcns notification chain.
b6b337ad1   Nadia Derbey   ipc: recompute ms...
88
  		 */
206fa9409   Xie XiuQi   ipc/util.c: remov...
89
  		schedule_work(&ipc_memory_wq);
b6b337ad1   Nadia Derbey   ipc: recompute ms...
90
91
92
93
94
95
96
97
98
99
100
  		break;
  	case MEM_GOING_ONLINE:
  	case MEM_GOING_OFFLINE:
  	case MEM_CANCEL_ONLINE:
  	case MEM_CANCEL_OFFLINE:
  	default:
  		break;
  	}
  
  	return NOTIFY_OK;
  }
8f68fa2d1   Andrew Morton   ipc/util.c: use r...
101
102
103
104
  static struct notifier_block ipc_memory_nb = {
  	.notifier_call = ipc_memory_callback,
  	.priority = IPC_CALLBACK_PRI,
  };
b6b337ad1   Nadia Derbey   ipc: recompute ms...
105

1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
106
  /**
8001c8581   Davidlohr Bueso   ipc: standardize ...
107
   * ipc_init - initialise ipc subsystem
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
108
   *
8001c8581   Davidlohr Bueso   ipc: standardize ...
109
110
111
112
113
114
   * The various sysv ipc resources (semaphores, messages and shared
   * memory) are initialised.
   *
   * A callback routine is registered into the memory hotplug notifier
   * chain: since msgmni scales to lowmem this callback routine will be
   * called upon successful memory add / remove to recompute msmgni.
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
115
   */
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
116
117
118
119
120
  static int __init ipc_init(void)
  {
  	sem_init();
  	msg_init();
  	shm_init();
8f68fa2d1   Andrew Morton   ipc/util.c: use r...
121
  	register_hotmemory_notifier(&ipc_memory_nb);
b6b337ad1   Nadia Derbey   ipc: recompute ms...
122
  	register_ipcns_notifier(&init_ipc_ns);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
123
124
  	return 0;
  }
6d08a2567   Davidlohr Bueso   ipc: use device_i...
125
  device_initcall(ipc_init);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
126
127
  
  /**
8001c8581   Davidlohr Bueso   ipc: standardize ...
128
129
   * ipc_init_ids	- initialise ipc identifiers
   * @ids: ipc identifier set
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
130
   *
8001c8581   Davidlohr Bueso   ipc: standardize ...
131
132
   * Set up the sequence range to use for the ipc identifier range (limited
   * below IPCMNI) then initialise the ids idr.
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
133
   */
7ca7e564e   Nadia Derbey   ipc: store ipcs i...
134
  void ipc_init_ids(struct ipc_ids *ids)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
135
  {
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
136
  	ids->in_use = 0;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
137
  	ids->seq = 0;
03f595668   Stanislav Kinsbursky   ipc: add sysctl t...
138
  	ids->next_id = -1;
daf948c7d   Davidlohr Bueso   ipc: delete seq_m...
139
  	init_rwsem(&ids->rwsem);
7ca7e564e   Nadia Derbey   ipc: store ipcs i...
140
  	idr_init(&ids->ipcs_idr);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
141
  }
ae7817745   Mike Waychison   [PATCH] ipc: add ...
142
  #ifdef CONFIG_PROC_FS
9a32144e9   Arjan van de Ven   [PATCH] mark stru...
143
  static const struct file_operations sysvipc_proc_fops;
ae7817745   Mike Waychison   [PATCH] ipc: add ...
144
  /**
8001c8581   Davidlohr Bueso   ipc: standardize ...
145
146
147
148
149
   * ipc_init_proc_interface -  create a proc interface for sysipc types using a seq_file interface.
   * @path: Path in procfs
   * @header: Banner to be printed at the beginning of the file.
   * @ids: ipc id table to iterate.
   * @show: show routine.
ae7817745   Mike Waychison   [PATCH] ipc: add ...
150
151
   */
  void __init ipc_init_proc_interface(const char *path, const char *header,
73ea41302   Kirill Korotaev   [PATCH] IPC names...
152
  		int ids, int (*show)(struct seq_file *, void *))
ae7817745   Mike Waychison   [PATCH] ipc: add ...
153
154
155
156
157
158
159
160
161
162
163
  {
  	struct proc_dir_entry *pde;
  	struct ipc_proc_iface *iface;
  
  	iface = kmalloc(sizeof(*iface), GFP_KERNEL);
  	if (!iface)
  		return;
  	iface->path	= path;
  	iface->header	= header;
  	iface->ids	= ids;
  	iface->show	= show;
6a6375db1   Denis V. Lunev   sysvipc: use non-...
164
165
166
167
168
  	pde = proc_create_data(path,
  			       S_IRUGO,        /* world readable */
  			       NULL,           /* parent dir */
  			       &sysvipc_proc_fops,
  			       iface);
3ab08fe20   Davidlohr Bueso   ipc: remove brace...
169
  	if (!pde)
ae7817745   Mike Waychison   [PATCH] ipc: add ...
170
  		kfree(iface);
ae7817745   Mike Waychison   [PATCH] ipc: add ...
171
172
  }
  #endif
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
173
  /**
8001c8581   Davidlohr Bueso   ipc: standardize ...
174
175
176
   * ipc_findkey	- find a key in an ipc identifier set
   * @ids: ipc identifier set
   * @key: key to find
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
177
   *	
8001c8581   Davidlohr Bueso   ipc: standardize ...
178
179
180
181
   * Returns the locked pointer to the ipc structure if found or NULL
   * otherwise. If key is found ipc points to the owning ipc structure
   *
   * Called with ipc_ids.rwsem held.
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
182
   */
7748dbfaa   Nadia Derbey   ipc: unify the sy...
183
  static struct kern_ipc_perm *ipc_findkey(struct ipc_ids *ids, key_t key)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
184
  {
7ca7e564e   Nadia Derbey   ipc: store ipcs i...
185
186
187
  	struct kern_ipc_perm *ipc;
  	int next_id;
  	int total;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
188

7ca7e564e   Nadia Derbey   ipc: store ipcs i...
189
190
191
192
  	for (total = 0, next_id = 0; total < ids->in_use; next_id++) {
  		ipc = idr_find(&ids->ipcs_idr, next_id);
  
  		if (ipc == NULL)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
193
  			continue;
7ca7e564e   Nadia Derbey   ipc: store ipcs i...
194
195
196
197
198
  
  		if (ipc->key != key) {
  			total++;
  			continue;
  		}
32a275001   Davidlohr Bueso   ipc: drop ipc_loc...
199
200
  		rcu_read_lock();
  		ipc_lock_object(ipc);
7ca7e564e   Nadia Derbey   ipc: store ipcs i...
201
  		return ipc;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
202
  	}
7ca7e564e   Nadia Derbey   ipc: store ipcs i...
203
204
  
  	return NULL;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
205
  }
7ca7e564e   Nadia Derbey   ipc: store ipcs i...
206
  /**
8001c8581   Davidlohr Bueso   ipc: standardize ...
207
208
   * ipc_get_maxid - get the last assigned id
   * @ids: ipc identifier set
7ca7e564e   Nadia Derbey   ipc: store ipcs i...
209
   *
8001c8581   Davidlohr Bueso   ipc: standardize ...
210
   * Called with ipc_ids.rwsem held.
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
211
   */
7ca7e564e   Nadia Derbey   ipc: store ipcs i...
212
213
214
215
216
217
218
219
  int ipc_get_maxid(struct ipc_ids *ids)
  {
  	struct kern_ipc_perm *ipc;
  	int max_id = -1;
  	int total, id;
  
  	if (ids->in_use == 0)
  		return -1;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
220

7ca7e564e   Nadia Derbey   ipc: store ipcs i...
221
222
223
224
225
226
227
228
229
230
231
232
233
  	if (ids->in_use == IPCMNI)
  		return IPCMNI - 1;
  
  	/* Look for the last assigned id */
  	total = 0;
  	for (id = 0; id < IPCMNI && total < ids->in_use; id++) {
  		ipc = idr_find(&ids->ipcs_idr, id);
  		if (ipc != NULL) {
  			max_id = id;
  			total++;
  		}
  	}
  	return max_id;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
234
235
236
  }
  
  /**
8001c8581   Davidlohr Bueso   ipc: standardize ...
237
238
239
240
   * ipc_addid - add an ipc identifier
   * @ids: ipc identifier set
   * @new: new ipc permission set
   * @size: limit for the number of used ids
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
241
   *
8001c8581   Davidlohr Bueso   ipc: standardize ...
242
243
244
245
   * Add an entry 'new' to the ipc ids idr. The permissions object is
   * initialised and the first free entry is set up and the id assigned
   * is returned. The 'new' entry is returned in a locked state on success.
   * On failure the entry is not locked and a negative err-code is returned.
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
246
   *
8001c8581   Davidlohr Bueso   ipc: standardize ...
247
   * Called with writer ipc_ids.rwsem held.
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
248
   */
239521f31   Manfred Spraul   ipc: whitespace c...
249
  int ipc_addid(struct ipc_ids *ids, struct kern_ipc_perm *new, int size)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
250
  {
1efdb69b0   Eric W. Biederman   userns: Convert i...
251
252
  	kuid_t euid;
  	kgid_t egid;
54924ea33   Tejun Heo   ipc: convert to i...
253
  	int id;
03f595668   Stanislav Kinsbursky   ipc: add sysctl t...
254
  	int next_id = ids->next_id;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
255

7ca7e564e   Nadia Derbey   ipc: store ipcs i...
256
257
258
259
  	if (size > IPCMNI)
  		size = IPCMNI;
  
  	if (ids->in_use >= size)
283bb7fad   Pierre Peiffer   IPC: fix error ca...
260
  		return -ENOSPC;
7ca7e564e   Nadia Derbey   ipc: store ipcs i...
261

54924ea33   Tejun Heo   ipc: convert to i...
262
  	idr_preload(GFP_KERNEL);
e00b4ff7e   Nadia Derbey   sysvipc: fix the ...
263
  	spin_lock_init(&new->lock);
72a8ff2f9   Rafael Aquini   ipc: change kern_...
264
  	new->deleted = false;
e00b4ff7e   Nadia Derbey   sysvipc: fix the ...
265
266
  	rcu_read_lock();
  	spin_lock(&new->lock);
54924ea33   Tejun Heo   ipc: convert to i...
267
268
269
270
271
  	id = idr_alloc(&ids->ipcs_idr, new,
  		       (next_id < 0) ? 0 : ipcid_to_idx(next_id), 0,
  		       GFP_NOWAIT);
  	idr_preload_end();
  	if (id < 0) {
e00b4ff7e   Nadia Derbey   sysvipc: fix the ...
272
273
  		spin_unlock(&new->lock);
  		rcu_read_unlock();
54924ea33   Tejun Heo   ipc: convert to i...
274
  		return id;
e00b4ff7e   Nadia Derbey   sysvipc: fix the ...
275
  	}
7ca7e564e   Nadia Derbey   ipc: store ipcs i...
276

1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
277
  	ids->in_use++;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
278

414c0708d   David Howells   CRED: Wrap task c...
279
280
281
  	current_euid_egid(&euid, &egid);
  	new->cuid = new->uid = euid;
  	new->gid = new->cgid = egid;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
282

03f595668   Stanislav Kinsbursky   ipc: add sysctl t...
283
284
  	if (next_id < 0) {
  		new->seq = ids->seq++;
daf948c7d   Davidlohr Bueso   ipc: delete seq_m...
285
  		if (ids->seq > IPCID_SEQ_MAX)
03f595668   Stanislav Kinsbursky   ipc: add sysctl t...
286
287
288
289
290
  			ids->seq = 0;
  	} else {
  		new->seq = ipcid_to_seqx(next_id);
  		ids->next_id = -1;
  	}
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
291

48dea404e   Pierre Peiffer   IPC: use ipc_buil...
292
  	new->id = ipc_buildid(id, new->seq);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
293
294
295
296
  	return id;
  }
  
  /**
8001c8581   Davidlohr Bueso   ipc: standardize ...
297
298
299
300
301
302
303
304
   * ipcget_new -	create a new ipc object
   * @ns: ipc namespace
   * @ids: ipc identifer set
   * @ops: the actual creation routine to call
   * @params: its parameters
   *
   * This routine is called by sys_msgget, sys_semget() and sys_shmget()
   * when the key is IPC_PRIVATE.
7748dbfaa   Nadia Derbey   ipc: unify the sy...
305
   */
b2d75cddc   Pavel Emelyanov   ipc: uninline som...
306
  static int ipcget_new(struct ipc_namespace *ns, struct ipc_ids *ids,
7748dbfaa   Nadia Derbey   ipc: unify the sy...
307
308
309
  		struct ipc_ops *ops, struct ipc_params *params)
  {
  	int err;
7748dbfaa   Nadia Derbey   ipc: unify the sy...
310

d9a605e40   Davidlohr Bueso   ipc: rename ids->...
311
  	down_write(&ids->rwsem);
7748dbfaa   Nadia Derbey   ipc: unify the sy...
312
  	err = ops->getnew(ns, params);
d9a605e40   Davidlohr Bueso   ipc: rename ids->...
313
  	up_write(&ids->rwsem);
7748dbfaa   Nadia Derbey   ipc: unify the sy...
314
315
316
317
  	return err;
  }
  
  /**
8001c8581   Davidlohr Bueso   ipc: standardize ...
318
319
320
321
322
   * ipc_check_perms - check security and permissions for an ipc object
   * @ns: ipc namespace
   * @ipcp: ipc permission set
   * @ops: the actual security routine to call
   * @params: its parameters
f4566f048   Nadia Derbey   ipc: fix wrong co...
323
   *
8001c8581   Davidlohr Bueso   ipc: standardize ...
324
325
326
   * This routine is called by sys_msgget(), sys_semget() and sys_shmget()
   * when the key is not IPC_PRIVATE and that key already exists in the
   * ds IDR.
f4566f048   Nadia Derbey   ipc: fix wrong co...
327
   *
8001c8581   Davidlohr Bueso   ipc: standardize ...
328
   * On success, the ipc id is returned.
f4566f048   Nadia Derbey   ipc: fix wrong co...
329
   *
8001c8581   Davidlohr Bueso   ipc: standardize ...
330
   * It is called with ipc_ids.rwsem and ipcp->lock held.
7748dbfaa   Nadia Derbey   ipc: unify the sy...
331
   */
b0e77598f   Serge E. Hallyn   userns: user name...
332
333
334
335
  static int ipc_check_perms(struct ipc_namespace *ns,
  			   struct kern_ipc_perm *ipcp,
  			   struct ipc_ops *ops,
  			   struct ipc_params *params)
7748dbfaa   Nadia Derbey   ipc: unify the sy...
336
337
  {
  	int err;
b0e77598f   Serge E. Hallyn   userns: user name...
338
  	if (ipcperms(ns, ipcp, params->flg))
7748dbfaa   Nadia Derbey   ipc: unify the sy...
339
340
341
342
343
344
345
346
347
348
349
  		err = -EACCES;
  	else {
  		err = ops->associate(ipcp, params->flg);
  		if (!err)
  			err = ipcp->id;
  	}
  
  	return err;
  }
  
  /**
8001c8581   Davidlohr Bueso   ipc: standardize ...
350
351
352
353
354
355
356
357
358
359
360
361
   * ipcget_public - get an ipc object or create a new one
   * @ns: ipc namespace
   * @ids: ipc identifer set
   * @ops: the actual creation routine to call
   * @params: its parameters
   *
   * This routine is called by sys_msgget, sys_semget() and sys_shmget()
   * when the key is not IPC_PRIVATE.
   * It adds a new entry if the key is not found and does some permission
   * / security checkings if the key is found.
   *
   * On success, the ipc id is returned.
7748dbfaa   Nadia Derbey   ipc: unify the sy...
362
   */
b2d75cddc   Pavel Emelyanov   ipc: uninline som...
363
  static int ipcget_public(struct ipc_namespace *ns, struct ipc_ids *ids,
7748dbfaa   Nadia Derbey   ipc: unify the sy...
364
365
366
367
368
  		struct ipc_ops *ops, struct ipc_params *params)
  {
  	struct kern_ipc_perm *ipcp;
  	int flg = params->flg;
  	int err;
7748dbfaa   Nadia Derbey   ipc: unify the sy...
369

3e148c799   Nadia Derbey   fix idr_find() lo...
370
371
372
373
  	/*
  	 * Take the lock as a writer since we are potentially going to add
  	 * a new entry + read locks are not "upgradable"
  	 */
d9a605e40   Davidlohr Bueso   ipc: rename ids->...
374
  	down_write(&ids->rwsem);
7748dbfaa   Nadia Derbey   ipc: unify the sy...
375
376
377
378
379
  	ipcp = ipc_findkey(ids, params->key);
  	if (ipcp == NULL) {
  		/* key not used */
  		if (!(flg & IPC_CREAT))
  			err = -ENOENT;
7748dbfaa   Nadia Derbey   ipc: unify the sy...
380
381
382
383
384
385
386
387
388
389
390
391
  		else
  			err = ops->getnew(ns, params);
  	} else {
  		/* ipc object has been locked by ipc_findkey() */
  
  		if (flg & IPC_CREAT && flg & IPC_EXCL)
  			err = -EEXIST;
  		else {
  			err = 0;
  			if (ops->more_checks)
  				err = ops->more_checks(ipcp, params);
  			if (!err)
f4566f048   Nadia Derbey   ipc: fix wrong co...
392
393
394
395
  				/*
  				 * ipc_check_perms returns the IPC id on
  				 * success
  				 */
b0e77598f   Serge E. Hallyn   userns: user name...
396
  				err = ipc_check_perms(ns, ipcp, ops, params);
7748dbfaa   Nadia Derbey   ipc: unify the sy...
397
398
399
  		}
  		ipc_unlock(ipcp);
  	}
d9a605e40   Davidlohr Bueso   ipc: rename ids->...
400
  	up_write(&ids->rwsem);
7748dbfaa   Nadia Derbey   ipc: unify the sy...
401
402
403
404
405
406
  
  	return err;
  }
  
  
  /**
8001c8581   Davidlohr Bueso   ipc: standardize ...
407
408
409
   * ipc_rmid - remove an ipc identifier
   * @ids: ipc identifier set
   * @ipcp: ipc perm structure containing the identifier to remove
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
410
   *
8001c8581   Davidlohr Bueso   ipc: standardize ...
411
412
   * ipc_ids.rwsem (as a writer) and the spinlock for this ID are held
   * before this function is called, and remain locked on the exit.
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
413
   */
7ca7e564e   Nadia Derbey   ipc: store ipcs i...
414
  void ipc_rmid(struct ipc_ids *ids, struct kern_ipc_perm *ipcp)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
415
  {
ce621f5ba   Nadia Derbey   ipc: introduce th...
416
  	int lid = ipcid_to_idx(ipcp->id);
7ca7e564e   Nadia Derbey   ipc: store ipcs i...
417
418
  
  	idr_remove(&ids->ipcs_idr, lid);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
419
  	ids->in_use--;
72a8ff2f9   Rafael Aquini   ipc: change kern_...
420
  	ipcp->deleted = true;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
421
422
423
  }
  
  /**
8001c8581   Davidlohr Bueso   ipc: standardize ...
424
425
   * ipc_alloc -	allocate ipc space
   * @size: size desired
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
426
   *
8001c8581   Davidlohr Bueso   ipc: standardize ...
427
428
   * Allocate memory from the appropriate pools and return a pointer to it.
   * NULL is returned if the allocation fails
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
429
   */
6062a8dc0   Rik van Riel   ipc,sem: fine gra...
430
  void *ipc_alloc(int size)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
431
  {
6062a8dc0   Rik van Riel   ipc,sem: fine gra...
432
  	void *out;
239521f31   Manfred Spraul   ipc: whitespace c...
433
  	if (size > PAGE_SIZE)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
434
435
436
437
438
439
440
  		out = vmalloc(size);
  	else
  		out = kmalloc(size, GFP_KERNEL);
  	return out;
  }
  
  /**
8001c8581   Davidlohr Bueso   ipc: standardize ...
441
442
443
   * ipc_free - free ipc space
   * @ptr: pointer returned by ipc_alloc
   * @size: size of block
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
444
   *
8001c8581   Davidlohr Bueso   ipc: standardize ...
445
446
   * Free a block created with ipc_alloc(). The caller must know the size
   * used in the allocation call.
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
447
   */
239521f31   Manfred Spraul   ipc: whitespace c...
448
  void ipc_free(void *ptr, int size)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
449
  {
239521f31   Manfred Spraul   ipc: whitespace c...
450
  	if (size > PAGE_SIZE)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
451
452
453
454
  		vfree(ptr);
  	else
  		kfree(ptr);
  }
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
455
  /**
8001c8581   Davidlohr Bueso   ipc: standardize ...
456
457
   * ipc_rcu_alloc - allocate ipc and rcu space
   * @size: size desired
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
458
   *
8001c8581   Davidlohr Bueso   ipc: standardize ...
459
460
   * Allocate memory for the rcu header structure +  the object.
   * Returns the pointer to the object or NULL upon failure.
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
461
   */
6062a8dc0   Rik van Riel   ipc,sem: fine gra...
462
  void *ipc_rcu_alloc(int size)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
463
  {
6062a8dc0   Rik van Riel   ipc,sem: fine gra...
464
  	/*
600fe9751   Al Viro   ipc_schedule_free...
465
  	 * We prepend the allocation with the rcu struct
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
466
  	 */
600fe9751   Al Viro   ipc_schedule_free...
467
468
469
470
  	struct ipc_rcu *out = ipc_alloc(sizeof(struct ipc_rcu) + size);
  	if (unlikely(!out))
  		return NULL;
  	atomic_set(&out->refcount, 1);
196aa0132   Manfred Spraul   ipc/util.c, ipc_r...
471
  	return out + 1;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
472
  }
6062a8dc0   Rik van Riel   ipc,sem: fine gra...
473
  int ipc_rcu_getref(void *ptr)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
474
  {
196aa0132   Manfred Spraul   ipc/util.c, ipc_r...
475
476
477
  	struct ipc_rcu *p = ((struct ipc_rcu *)ptr) - 1;
  
  	return atomic_inc_not_zero(&p->refcount);
65f27f384   David Howells   WorkStruct: Pass ...
478
  }
53dad6d3a   Davidlohr Bueso   ipc: fix race wit...
479
  void ipc_rcu_putref(void *ptr, void (*func)(struct rcu_head *head))
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
480
  {
196aa0132   Manfred Spraul   ipc/util.c, ipc_r...
481
  	struct ipc_rcu *p = ((struct ipc_rcu *)ptr) - 1;
600fe9751   Al Viro   ipc_schedule_free...
482
483
  
  	if (!atomic_dec_and_test(&p->refcount))
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
484
  		return;
53dad6d3a   Davidlohr Bueso   ipc: fix race wit...
485
486
487
488
489
490
491
492
493
494
495
  	call_rcu(&p->rcu, func);
  }
  
  void ipc_rcu_free(struct rcu_head *head)
  {
  	struct ipc_rcu *p = container_of(head, struct ipc_rcu, rcu);
  
  	if (is_vmalloc_addr(p))
  		vfree(p);
  	else
  		kfree(p);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
496
497
498
  }
  
  /**
8001c8581   Davidlohr Bueso   ipc: standardize ...
499
500
501
502
   * ipcperms - check ipc permissions
   * @ns: ipc namespace
   * @ipcp: ipc permission set
   * @flag: desired permission set
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
503
   *
8001c8581   Davidlohr Bueso   ipc: standardize ...
504
505
   * Check user, group, other permissions for access
   * to ipc resources. return 0 if allowed
b0e77598f   Serge E. Hallyn   userns: user name...
506
   *
8001c8581   Davidlohr Bueso   ipc: standardize ...
507
   * @flag will most probably be 0 or S_...UGO from <linux/stat.h>
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
508
   */
b0e77598f   Serge E. Hallyn   userns: user name...
509
510
  int ipcperms(struct ipc_namespace *ns, struct kern_ipc_perm *ipcp, short flag)
  {
1efdb69b0   Eric W. Biederman   userns: Convert i...
511
  	kuid_t euid = current_euid();
a33e67510   Al Viro   sanitize audit_ip...
512
  	int requested_mode, granted_mode;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
513

a33e67510   Al Viro   sanitize audit_ip...
514
  	audit_ipc_obj(ipcp);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
515
516
  	requested_mode = (flag >> 6) | (flag >> 3) | flag;
  	granted_mode = ipcp->mode;
1efdb69b0   Eric W. Biederman   userns: Convert i...
517
518
  	if (uid_eq(euid, ipcp->cuid) ||
  	    uid_eq(euid, ipcp->uid))
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
519
520
521
522
523
  		granted_mode >>= 6;
  	else if (in_group_p(ipcp->cgid) || in_group_p(ipcp->gid))
  		granted_mode >>= 3;
  	/* is there some bit set in requested_mode but not in granted_mode? */
  	if ((requested_mode & ~granted_mode & 0007) && 
b0e77598f   Serge E. Hallyn   userns: user name...
524
  	    !ns_capable(ns->user_ns, CAP_IPC_OWNER))
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
525
526
527
528
529
530
531
532
533
534
535
  		return -1;
  
  	return security_ipc_permission(ipcp, flag);
  }
  
  /*
   * Functions to convert between the kern_ipc_perm structure and the
   * old/new ipc_perm structures
   */
  
  /**
8001c8581   Davidlohr Bueso   ipc: standardize ...
536
537
538
   * kernel_to_ipc64_perm	- convert kernel ipc permissions to user
   * @in: kernel permissions
   * @out: new style ipc permissions
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
539
   *
8001c8581   Davidlohr Bueso   ipc: standardize ...
540
541
   * Turn the kernel object @in into a set of permissions descriptions
   * for returning to userspace (@out).
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
542
   */
239521f31   Manfred Spraul   ipc: whitespace c...
543
  void kernel_to_ipc64_perm(struct kern_ipc_perm *in, struct ipc64_perm *out)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
544
545
  {
  	out->key	= in->key;
1efdb69b0   Eric W. Biederman   userns: Convert i...
546
547
548
549
  	out->uid	= from_kuid_munged(current_user_ns(), in->uid);
  	out->gid	= from_kgid_munged(current_user_ns(), in->gid);
  	out->cuid	= from_kuid_munged(current_user_ns(), in->cuid);
  	out->cgid	= from_kgid_munged(current_user_ns(), in->cgid);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
550
551
552
553
554
  	out->mode	= in->mode;
  	out->seq	= in->seq;
  }
  
  /**
8001c8581   Davidlohr Bueso   ipc: standardize ...
555
556
557
   * ipc64_perm_to_ipc_perm - convert new ipc permissions to old
   * @in: new style ipc permissions
   * @out: old style ipc permissions
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
558
   *
8001c8581   Davidlohr Bueso   ipc: standardize ...
559
560
   * Turn the new style permissions object @in into a compatibility
   * object and store it into the @out pointer.
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
561
   */
239521f31   Manfred Spraul   ipc: whitespace c...
562
  void ipc64_perm_to_ipc_perm(struct ipc64_perm *in, struct ipc_perm *out)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
563
564
565
566
567
568
569
570
571
  {
  	out->key	= in->key;
  	SET_UID(out->uid, in->uid);
  	SET_GID(out->gid, in->gid);
  	SET_UID(out->cuid, in->cuid);
  	SET_GID(out->cgid, in->cgid);
  	out->mode	= in->mode;
  	out->seq	= in->seq;
  }
f4566f048   Nadia Derbey   ipc: fix wrong co...
572
  /**
4d2bff5eb   Davidlohr Bueso   ipc: introduce ob...
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
   * ipc_obtain_object
   * @ids: ipc identifier set
   * @id: ipc id to look for
   *
   * Look for an id in the ipc ids idr and return associated ipc object.
   *
   * Call inside the RCU critical section.
   * The ipc object is *not* locked on exit.
   */
  struct kern_ipc_perm *ipc_obtain_object(struct ipc_ids *ids, int id)
  {
  	struct kern_ipc_perm *out;
  	int lid = ipcid_to_idx(id);
  
  	out = idr_find(&ids->ipcs_idr, lid);
  	if (!out)
  		return ERR_PTR(-EINVAL);
  
  	return out;
  }
  
  /**
8001c8581   Davidlohr Bueso   ipc: standardize ...
595
596
   * ipc_lock - lock an ipc structure without rwsem held
   * @ids: ipc identifier set
f4566f048   Nadia Derbey   ipc: fix wrong co...
597
598
599
600
   * @id: ipc id to look for
   *
   * Look for an id in the ipc ids idr and lock the associated ipc object.
   *
4d2bff5eb   Davidlohr Bueso   ipc: introduce ob...
601
   * The ipc object is locked on successful exit.
f4566f048   Nadia Derbey   ipc: fix wrong co...
602
   */
7ca7e564e   Nadia Derbey   ipc: store ipcs i...
603
  struct kern_ipc_perm *ipc_lock(struct ipc_ids *ids, int id)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
604
  {
7ca7e564e   Nadia Derbey   ipc: store ipcs i...
605
  	struct kern_ipc_perm *out;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
606
607
  
  	rcu_read_lock();
4d2bff5eb   Davidlohr Bueso   ipc: introduce ob...
608
609
610
  	out = ipc_obtain_object(ids, id);
  	if (IS_ERR(out))
  		goto err1;
7ca7e564e   Nadia Derbey   ipc: store ipcs i...
611

1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
612
  	spin_lock(&out->lock);
4d2bff5eb   Davidlohr Bueso   ipc: introduce ob...
613

1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
614
615
616
  	/* ipc_rmid() may have already freed the ID while ipc_lock
  	 * was spinning: here verify that the structure is still valid
  	 */
72a8ff2f9   Rafael Aquini   ipc: change kern_...
617
  	if (ipc_valid_object(out))
4d2bff5eb   Davidlohr Bueso   ipc: introduce ob...
618
619
620
621
622
623
624
625
  		return out;
  
  	spin_unlock(&out->lock);
  	out = ERR_PTR(-EINVAL);
  err1:
  	rcu_read_unlock();
  	return out;
  }
7ca7e564e   Nadia Derbey   ipc: store ipcs i...
626

4d2bff5eb   Davidlohr Bueso   ipc: introduce ob...
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
  /**
   * ipc_obtain_object_check
   * @ids: ipc identifier set
   * @id: ipc id to look for
   *
   * Similar to ipc_obtain_object() but also checks
   * the ipc object reference counter.
   *
   * Call inside the RCU critical section.
   * The ipc object is *not* locked on exit.
   */
  struct kern_ipc_perm *ipc_obtain_object_check(struct ipc_ids *ids, int id)
  {
  	struct kern_ipc_perm *out = ipc_obtain_object(ids, id);
  
  	if (IS_ERR(out))
  		goto out;
  
  	if (ipc_checkid(out, id))
  		return ERR_PTR(-EIDRM);
  out:
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
648
649
  	return out;
  }
b2d75cddc   Pavel Emelyanov   ipc: uninline som...
650
651
  /**
   * ipcget - Common sys_*get() code
8001c8581   Davidlohr Bueso   ipc: standardize ...
652
653
654
655
656
   * @ns: namsepace
   * @ids: ipc identifier set
   * @ops: operations to be called on ipc object creation, permission checks
   *       and further checks
   * @params: the parameters needed by the previous operations.
b2d75cddc   Pavel Emelyanov   ipc: uninline som...
657
658
659
660
661
662
663
664
665
666
667
   *
   * Common routine called by sys_msgget(), sys_semget() and sys_shmget().
   */
  int ipcget(struct ipc_namespace *ns, struct ipc_ids *ids,
  			struct ipc_ops *ops, struct ipc_params *params)
  {
  	if (params->key == IPC_PRIVATE)
  		return ipcget_new(ns, ids, ops, params);
  	else
  		return ipcget_public(ns, ids, ops, params);
  }
8f4a3809c   Pierre Peiffer   IPC: introduce ip...
668
  /**
8001c8581   Davidlohr Bueso   ipc: standardize ...
669
   * ipc_update_perm - update the permissions of an ipc object
8f4a3809c   Pierre Peiffer   IPC: introduce ip...
670
671
672
   * @in:  the permission given as input.
   * @out: the permission of the ipc to set.
   */
1efdb69b0   Eric W. Biederman   userns: Convert i...
673
  int ipc_update_perm(struct ipc64_perm *in, struct kern_ipc_perm *out)
8f4a3809c   Pierre Peiffer   IPC: introduce ip...
674
  {
1efdb69b0   Eric W. Biederman   userns: Convert i...
675
676
677
678
679
680
681
  	kuid_t uid = make_kuid(current_user_ns(), in->uid);
  	kgid_t gid = make_kgid(current_user_ns(), in->gid);
  	if (!uid_valid(uid) || !gid_valid(gid))
  		return -EINVAL;
  
  	out->uid = uid;
  	out->gid = gid;
8f4a3809c   Pierre Peiffer   IPC: introduce ip...
682
683
  	out->mode = (out->mode & ~S_IRWXUGO)
  		| (in->mode & S_IRWXUGO);
1efdb69b0   Eric W. Biederman   userns: Convert i...
684
685
  
  	return 0;
8f4a3809c   Pierre Peiffer   IPC: introduce ip...
686
  }
a5f75e7f2   Pierre Peiffer   IPC: consolidate ...
687
  /**
3b1c4ad37   Davidlohr Bueso   ipc: drop ipcctl_...
688
   * ipcctl_pre_down_nolock - retrieve an ipc and check permissions for some IPC_XXX cmd
8001c8581   Davidlohr Bueso   ipc: standardize ...
689
   * @ns:  ipc namespace
a5f75e7f2   Pierre Peiffer   IPC: consolidate ...
690
691
692
693
694
695
696
697
698
699
700
   * @ids:  the table of ids where to look for the ipc
   * @id:   the id of the ipc to retrieve
   * @cmd:  the cmd to check
   * @perm: the permission to set
   * @extra_perm: one extra permission parameter used by msq
   *
   * This function does some common audit and permissions check for some IPC_XXX
   * cmd and is called from semctl_down, shmctl_down and msgctl_down.
   * It must be called without any lock held and
   *  - retrieves the ipc with the given id in the given table.
   *  - performs some audit and permission check, depending on the given cmd
3b1c4ad37   Davidlohr Bueso   ipc: drop ipcctl_...
701
   *  - returns a pointer to the ipc object or otherwise, the corresponding error.
7b4cc5d84   Davidlohr Bueso   ipc: move locking...
702
   *
d9a605e40   Davidlohr Bueso   ipc: rename ids->...
703
   * Call holding the both the rwsem and the rcu read lock.
a5f75e7f2   Pierre Peiffer   IPC: consolidate ...
704
   */
444d0f621   Davidlohr Bueso   ipc: introduce lo...
705
  struct kern_ipc_perm *ipcctl_pre_down_nolock(struct ipc_namespace *ns,
3b1c4ad37   Davidlohr Bueso   ipc: drop ipcctl_...
706
707
  					struct ipc_ids *ids, int id, int cmd,
  					struct ipc64_perm *perm, int extra_perm)
444d0f621   Davidlohr Bueso   ipc: introduce lo...
708
  {
1efdb69b0   Eric W. Biederman   userns: Convert i...
709
  	kuid_t euid;
444d0f621   Davidlohr Bueso   ipc: introduce lo...
710
711
  	int err = -EPERM;
  	struct kern_ipc_perm *ipcp;
a5f75e7f2   Pierre Peiffer   IPC: consolidate ...
712

444d0f621   Davidlohr Bueso   ipc: introduce lo...
713
  	ipcp = ipc_obtain_object_check(ids, id);
a5f75e7f2   Pierre Peiffer   IPC: consolidate ...
714
715
  	if (IS_ERR(ipcp)) {
  		err = PTR_ERR(ipcp);
7b4cc5d84   Davidlohr Bueso   ipc: move locking...
716
  		goto err;
a5f75e7f2   Pierre Peiffer   IPC: consolidate ...
717
  	}
a33e67510   Al Viro   sanitize audit_ip...
718
  	audit_ipc_obj(ipcp);
e816f370c   Al Viro   sanitize audit_ip...
719
720
  	if (cmd == IPC_SET)
  		audit_ipc_set_perm(extra_perm, perm->uid,
444d0f621   Davidlohr Bueso   ipc: introduce lo...
721
  				   perm->gid, perm->mode);
414c0708d   David Howells   CRED: Wrap task c...
722
723
  
  	euid = current_euid();
1efdb69b0   Eric W. Biederman   userns: Convert i...
724
  	if (uid_eq(euid, ipcp->cuid) || uid_eq(euid, ipcp->uid)  ||
b0e77598f   Serge E. Hallyn   userns: user name...
725
  	    ns_capable(ns->user_ns, CAP_SYS_ADMIN))
7b4cc5d84   Davidlohr Bueso   ipc: move locking...
726
727
  		return ipcp; /* successful lookup */
  err:
a5f75e7f2   Pierre Peiffer   IPC: consolidate ...
728
729
  	return ERR_PTR(err);
  }
c1d7e01d7   Will Deacon   ipc: use Kconfig ...
730
  #ifdef CONFIG_ARCH_WANT_IPC_PARSE_VERSION
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
731
732
733
  
  
  /**
8001c8581   Davidlohr Bueso   ipc: standardize ...
734
735
   * ipc_parse_version - ipc call version
   * @cmd: pointer to command
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
736
   *
8001c8581   Davidlohr Bueso   ipc: standardize ...
737
738
739
   * Return IPC_64 for new style IPC and IPC_OLD for old style IPC.
   * The @cmd value is turned from an encoding command and version into
   * just the command code.
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
740
   */
239521f31   Manfred Spraul   ipc: whitespace c...
741
  int ipc_parse_version(int *cmd)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
742
743
744
745
746
747
748
749
  {
  	if (*cmd & IPC_64) {
  		*cmd ^= IPC_64;
  		return IPC_64;
  	} else {
  		return IPC_OLD;
  	}
  }
c1d7e01d7   Will Deacon   ipc: use Kconfig ...
750
  #endif /* CONFIG_ARCH_WANT_IPC_PARSE_VERSION */
ae7817745   Mike Waychison   [PATCH] ipc: add ...
751
752
  
  #ifdef CONFIG_PROC_FS
bc1fc6d88   Eric W. Biederman   [PATCH] ipc: save...
753
754
755
756
  struct ipc_proc_iter {
  	struct ipc_namespace *ns;
  	struct ipc_proc_iface *iface;
  };
7ca7e564e   Nadia Derbey   ipc: store ipcs i...
757
758
759
  /*
   * This routine locks the ipc structure found at least at position pos.
   */
b524b9adb   Adrian Bunk   make ipc/util.c:s...
760
761
  static struct kern_ipc_perm *sysvipc_find_ipc(struct ipc_ids *ids, loff_t pos,
  					      loff_t *new_pos)
ae7817745   Mike Waychison   [PATCH] ipc: add ...
762
  {
7ca7e564e   Nadia Derbey   ipc: store ipcs i...
763
764
  	struct kern_ipc_perm *ipc;
  	int total, id;
73ea41302   Kirill Korotaev   [PATCH] IPC names...
765

7ca7e564e   Nadia Derbey   ipc: store ipcs i...
766
767
768
769
770
771
  	total = 0;
  	for (id = 0; id < pos && total < ids->in_use; id++) {
  		ipc = idr_find(&ids->ipcs_idr, id);
  		if (ipc != NULL)
  			total++;
  	}
ae7817745   Mike Waychison   [PATCH] ipc: add ...
772

7ca7e564e   Nadia Derbey   ipc: store ipcs i...
773
774
  	if (total >= ids->in_use)
  		return NULL;
ae7817745   Mike Waychison   [PATCH] ipc: add ...
775

239521f31   Manfred Spraul   ipc: whitespace c...
776
  	for (; pos < IPCMNI; pos++) {
7ca7e564e   Nadia Derbey   ipc: store ipcs i...
777
778
779
  		ipc = idr_find(&ids->ipcs_idr, pos);
  		if (ipc != NULL) {
  			*new_pos = pos + 1;
32a275001   Davidlohr Bueso   ipc: drop ipc_loc...
780
781
  			rcu_read_lock();
  			ipc_lock_object(ipc);
ae7817745   Mike Waychison   [PATCH] ipc: add ...
782
783
784
785
786
787
788
  			return ipc;
  		}
  	}
  
  	/* Out of range - return NULL to terminate iteration */
  	return NULL;
  }
7ca7e564e   Nadia Derbey   ipc: store ipcs i...
789
790
791
792
793
794
795
796
797
  static void *sysvipc_proc_next(struct seq_file *s, void *it, loff_t *pos)
  {
  	struct ipc_proc_iter *iter = s->private;
  	struct ipc_proc_iface *iface = iter->iface;
  	struct kern_ipc_perm *ipc = it;
  
  	/* If we had an ipc id locked before, unlock it */
  	if (ipc && ipc != SEQ_START_TOKEN)
  		ipc_unlock(ipc);
ed2ddbf88   Pierre Peiffer   IPC: make struct ...
798
  	return sysvipc_find_ipc(&iter->ns->ids[iface->ids], *pos, pos);
7ca7e564e   Nadia Derbey   ipc: store ipcs i...
799
  }
ae7817745   Mike Waychison   [PATCH] ipc: add ...
800
  /*
f4566f048   Nadia Derbey   ipc: fix wrong co...
801
802
   * File positions: pos 0 -> header, pos n -> ipc id = n - 1.
   * SeqFile iterator: iterator value locked ipc pointer or SEQ_TOKEN_START.
ae7817745   Mike Waychison   [PATCH] ipc: add ...
803
804
805
   */
  static void *sysvipc_proc_start(struct seq_file *s, loff_t *pos)
  {
bc1fc6d88   Eric W. Biederman   [PATCH] ipc: save...
806
807
  	struct ipc_proc_iter *iter = s->private;
  	struct ipc_proc_iface *iface = iter->iface;
73ea41302   Kirill Korotaev   [PATCH] IPC names...
808
  	struct ipc_ids *ids;
ed2ddbf88   Pierre Peiffer   IPC: make struct ...
809
  	ids = &iter->ns->ids[iface->ids];
ae7817745   Mike Waychison   [PATCH] ipc: add ...
810
811
812
813
814
  
  	/*
  	 * Take the lock - this will be released by the corresponding
  	 * call to stop().
  	 */
d9a605e40   Davidlohr Bueso   ipc: rename ids->...
815
  	down_read(&ids->rwsem);
ae7817745   Mike Waychison   [PATCH] ipc: add ...
816
817
818
819
820
821
822
823
824
825
  
  	/* pos < 0 is invalid */
  	if (*pos < 0)
  		return NULL;
  
  	/* pos == 0 means header */
  	if (*pos == 0)
  		return SEQ_START_TOKEN;
  
  	/* Find the (pos-1)th ipc */
7ca7e564e   Nadia Derbey   ipc: store ipcs i...
826
  	return sysvipc_find_ipc(ids, *pos - 1, pos);
ae7817745   Mike Waychison   [PATCH] ipc: add ...
827
828
829
830
831
  }
  
  static void sysvipc_proc_stop(struct seq_file *s, void *it)
  {
  	struct kern_ipc_perm *ipc = it;
bc1fc6d88   Eric W. Biederman   [PATCH] ipc: save...
832
833
  	struct ipc_proc_iter *iter = s->private;
  	struct ipc_proc_iface *iface = iter->iface;
73ea41302   Kirill Korotaev   [PATCH] IPC names...
834
  	struct ipc_ids *ids;
ae7817745   Mike Waychison   [PATCH] ipc: add ...
835

f4566f048   Nadia Derbey   ipc: fix wrong co...
836
  	/* If we had a locked structure, release it */
ae7817745   Mike Waychison   [PATCH] ipc: add ...
837
838
  	if (ipc && ipc != SEQ_START_TOKEN)
  		ipc_unlock(ipc);
ed2ddbf88   Pierre Peiffer   IPC: make struct ...
839
  	ids = &iter->ns->ids[iface->ids];
ae7817745   Mike Waychison   [PATCH] ipc: add ...
840
  	/* Release the lock we took in start() */
d9a605e40   Davidlohr Bueso   ipc: rename ids->...
841
  	up_read(&ids->rwsem);
ae7817745   Mike Waychison   [PATCH] ipc: add ...
842
843
844
845
  }
  
  static int sysvipc_proc_show(struct seq_file *s, void *it)
  {
bc1fc6d88   Eric W. Biederman   [PATCH] ipc: save...
846
847
  	struct ipc_proc_iter *iter = s->private;
  	struct ipc_proc_iface *iface = iter->iface;
ae7817745   Mike Waychison   [PATCH] ipc: add ...
848
849
850
851
852
853
  
  	if (it == SEQ_START_TOKEN)
  		return seq_puts(s, iface->header);
  
  	return iface->show(s, it);
  }
88e9d34c7   James Morris   seq_file: constif...
854
  static const struct seq_operations sysvipc_proc_seqops = {
ae7817745   Mike Waychison   [PATCH] ipc: add ...
855
856
857
858
859
  	.start = sysvipc_proc_start,
  	.stop  = sysvipc_proc_stop,
  	.next  = sysvipc_proc_next,
  	.show  = sysvipc_proc_show,
  };
bc1fc6d88   Eric W. Biederman   [PATCH] ipc: save...
860
861
  static int sysvipc_proc_open(struct inode *inode, struct file *file)
  {
ae7817745   Mike Waychison   [PATCH] ipc: add ...
862
863
  	int ret;
  	struct seq_file *seq;
bc1fc6d88   Eric W. Biederman   [PATCH] ipc: save...
864
865
866
867
868
869
  	struct ipc_proc_iter *iter;
  
  	ret = -ENOMEM;
  	iter = kmalloc(sizeof(*iter), GFP_KERNEL);
  	if (!iter)
  		goto out;
ae7817745   Mike Waychison   [PATCH] ipc: add ...
870
871
  
  	ret = seq_open(file, &sysvipc_proc_seqops);
8dc5cd04f   Davidlohr Bueso   ipc: simplify sys...
872
873
874
875
  	if (ret) {
  		kfree(iter);
  		goto out;
  	}
bc1fc6d88   Eric W. Biederman   [PATCH] ipc: save...
876
877
878
  
  	seq = file->private_data;
  	seq->private = iter;
d9dda78ba   Al Viro   procfs: new helpe...
879
  	iter->iface = PDE_DATA(inode);
bc1fc6d88   Eric W. Biederman   [PATCH] ipc: save...
880
881
  	iter->ns    = get_ipc_ns(current->nsproxy->ipc_ns);
  out:
ae7817745   Mike Waychison   [PATCH] ipc: add ...
882
  	return ret;
bc1fc6d88   Eric W. Biederman   [PATCH] ipc: save...
883
884
885
886
887
888
889
890
  }
  
  static int sysvipc_proc_release(struct inode *inode, struct file *file)
  {
  	struct seq_file *seq = file->private_data;
  	struct ipc_proc_iter *iter = seq->private;
  	put_ipc_ns(iter->ns);
  	return seq_release_private(inode, file);
ae7817745   Mike Waychison   [PATCH] ipc: add ...
891
  }
9a32144e9   Arjan van de Ven   [PATCH] mark stru...
892
  static const struct file_operations sysvipc_proc_fops = {
ae7817745   Mike Waychison   [PATCH] ipc: add ...
893
894
895
  	.open    = sysvipc_proc_open,
  	.read    = seq_read,
  	.llseek  = seq_lseek,
bc1fc6d88   Eric W. Biederman   [PATCH] ipc: save...
896
  	.release = sysvipc_proc_release,
ae7817745   Mike Waychison   [PATCH] ipc: add ...
897
898
  };
  #endif /* CONFIG_PROC_FS */