Blame view

ipc/util.c 21.8 KB
b24413180   Greg Kroah-Hartman   License cleanup: ...
1
  // SPDX-License-Identifier: GPL-2.0
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2
3
4
5
6
7
8
9
10
  /*
   * 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...
11
   *	      Manfred Spraul <manfred@colorfullife.com>
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
12
13
   * 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...
14
15
   * Mar 2006 - support for audit of ipc object properties
   *            Dustin Kirkland <dustin.kirkland@us.ibm.com>
73ea41302   Kirill Korotaev   [PATCH] IPC names...
16
17
18
   * Jun 2006 - namespaces ssupport
   *            OpenVZ, SWsoft Inc.
   *            Pavel Emelianov <xemul@openvz.org>
05603c44a   Davidlohr Bueso   ipc: document gen...
19
20
   *
   * General sysv ipc locking scheme:
18ccee263   Davidlohr Bueso   ipc: update locki...
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
   *	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
42
   */
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
43
44
45
46
  #include <linux/mm.h>
  #include <linux/shm.h>
  #include <linux/init.h>
  #include <linux/msg.h>
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
47
48
  #include <linux/vmalloc.h>
  #include <linux/slab.h>
8f68fa2d1   Andrew Morton   ipc/util.c: use r...
49
  #include <linux/notifier.h>
c59ede7b7   Randy.Dunlap   [PATCH] move capa...
50
  #include <linux/capability.h>
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
51
52
53
54
  #include <linux/highuid.h>
  #include <linux/security.h>
  #include <linux/rcupdate.h>
  #include <linux/workqueue.h>
ae7817745   Mike Waychison   [PATCH] ipc: add ...
55
56
  #include <linux/seq_file.h>
  #include <linux/proc_fs.h>
073115d6b   Steve Grubb   [PATCH] Rework of...
57
  #include <linux/audit.h>
73ea41302   Kirill Korotaev   [PATCH] IPC names...
58
  #include <linux/nsproxy.h>
3e148c799   Nadia Derbey   fix idr_find() lo...
59
  #include <linux/rwsem.h>
b6b337ad1   Nadia Derbey   ipc: recompute ms...
60
  #include <linux/memory.h>
ae5e1b22f   Pavel Emelyanov   namespaces: move ...
61
  #include <linux/ipc_namespace.h>
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
62
63
64
65
  
  #include <asm/unistd.h>
  
  #include "util.h"
ae7817745   Mike Waychison   [PATCH] ipc: add ...
66
67
68
  struct ipc_proc_iface {
  	const char *path;
  	const char *header;
73ea41302   Kirill Korotaev   [PATCH] IPC names...
69
  	int ids;
ae7817745   Mike Waychison   [PATCH] ipc: add ...
70
71
  	int (*show)(struct seq_file *, void *);
  };
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
72
  /**
8001c8581   Davidlohr Bueso   ipc: standardize ...
73
   * ipc_init - initialise ipc subsystem
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
74
   *
8001c8581   Davidlohr Bueso   ipc: standardize ...
75
76
77
78
79
80
   * 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
81
   */
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
82
83
  static int __init ipc_init(void)
  {
0cfb6aee7   Guillaume Knispel   ipc: optimize sem...
84
85
86
87
88
89
90
91
  	int err_sem, err_msg;
  
  	err_sem = sem_init();
  	WARN(err_sem, "ipc: sysv sem_init failed: %d
  ", err_sem);
  	err_msg = msg_init();
  	WARN(err_msg, "ipc: sysv msg_init failed: %d
  ", err_msg);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
92
  	shm_init();
0cfb6aee7   Guillaume Knispel   ipc: optimize sem...
93
94
  
  	return err_msg ? err_msg : err_sem;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
95
  }
6d08a2567   Davidlohr Bueso   ipc: use device_i...
96
  device_initcall(ipc_init);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
97

0cfb6aee7   Guillaume Knispel   ipc: optimize sem...
98
99
100
101
102
103
104
  static const struct rhashtable_params ipc_kht_params = {
  	.head_offset		= offsetof(struct kern_ipc_perm, khtnode),
  	.key_offset		= offsetof(struct kern_ipc_perm, key),
  	.key_len		= FIELD_SIZEOF(struct kern_ipc_perm, key),
  	.locks_mul		= 1,
  	.automatic_shrinking	= true,
  };
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
105
  /**
8001c8581   Davidlohr Bueso   ipc: standardize ...
106
107
   * ipc_init_ids	- initialise ipc identifiers
   * @ids: ipc identifier set
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
108
   *
8001c8581   Davidlohr Bueso   ipc: standardize ...
109
   * Set up the sequence range to use for the ipc identifier range (limited
0cfb6aee7   Guillaume Knispel   ipc: optimize sem...
110
   * below IPCMNI) then initialise the keys hashtable and ids idr.
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
111
   */
0cfb6aee7   Guillaume Knispel   ipc: optimize sem...
112
  int ipc_init_ids(struct ipc_ids *ids)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
113
  {
0cfb6aee7   Guillaume Knispel   ipc: optimize sem...
114
  	int err;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
115
  	ids->in_use = 0;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
116
  	ids->seq = 0;
03f595668   Stanislav Kinsbursky   ipc: add sysctl t...
117
  	ids->next_id = -1;
daf948c7d   Davidlohr Bueso   ipc: delete seq_m...
118
  	init_rwsem(&ids->rwsem);
0cfb6aee7   Guillaume Knispel   ipc: optimize sem...
119
120
121
  	err = rhashtable_init(&ids->key_ht, &ipc_kht_params);
  	if (err)
  		return err;
7ca7e564e   Nadia Derbey   ipc: store ipcs i...
122
  	idr_init(&ids->ipcs_idr);
0cfb6aee7   Guillaume Knispel   ipc: optimize sem...
123
124
  	ids->tables_initialized = true;
  	return 0;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
125
  }
ae7817745   Mike Waychison   [PATCH] ipc: add ...
126
  #ifdef CONFIG_PROC_FS
9a32144e9   Arjan van de Ven   [PATCH] mark stru...
127
  static const struct file_operations sysvipc_proc_fops;
ae7817745   Mike Waychison   [PATCH] ipc: add ...
128
  /**
8001c8581   Davidlohr Bueso   ipc: standardize ...
129
130
131
132
133
   * 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 ...
134
135
   */
  void __init ipc_init_proc_interface(const char *path, const char *header,
73ea41302   Kirill Korotaev   [PATCH] IPC names...
136
  		int ids, int (*show)(struct seq_file *, void *))
ae7817745   Mike Waychison   [PATCH] ipc: add ...
137
138
139
140
141
142
143
144
145
146
147
  {
  	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-...
148
149
150
151
152
  	pde = proc_create_data(path,
  			       S_IRUGO,        /* world readable */
  			       NULL,           /* parent dir */
  			       &sysvipc_proc_fops,
  			       iface);
3ab08fe20   Davidlohr Bueso   ipc: remove brace...
153
  	if (!pde)
ae7817745   Mike Waychison   [PATCH] ipc: add ...
154
  		kfree(iface);
ae7817745   Mike Waychison   [PATCH] ipc: add ...
155
156
  }
  #endif
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
157
  /**
8001c8581   Davidlohr Bueso   ipc: standardize ...
158
159
160
   * ipc_findkey	- find a key in an ipc identifier set
   * @ids: ipc identifier set
   * @key: key to find
46c0a8ca3   Paul McQuade   ipc, kernel: clea...
161
   *
8001c8581   Davidlohr Bueso   ipc: standardize ...
162
163
164
   * Returns the locked pointer to the ipc structure if found or NULL
   * otherwise. If key is found ipc points to the owning ipc structure
   *
0cfb6aee7   Guillaume Knispel   ipc: optimize sem...
165
   * Called with writer ipc_ids.rwsem held.
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
166
   */
7748dbfaa   Nadia Derbey   ipc: unify the sy...
167
  static struct kern_ipc_perm *ipc_findkey(struct ipc_ids *ids, key_t key)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
168
  {
0cfb6aee7   Guillaume Knispel   ipc: optimize sem...
169
  	struct kern_ipc_perm *ipcp = NULL;
7ca7e564e   Nadia Derbey   ipc: store ipcs i...
170

0cfb6aee7   Guillaume Knispel   ipc: optimize sem...
171
172
173
  	if (likely(ids->tables_initialized))
  		ipcp = rhashtable_lookup_fast(&ids->key_ht, &key,
  					      ipc_kht_params);
7ca7e564e   Nadia Derbey   ipc: store ipcs i...
174

0cfb6aee7   Guillaume Knispel   ipc: optimize sem...
175
  	if (ipcp) {
32a275001   Davidlohr Bueso   ipc: drop ipc_loc...
176
  		rcu_read_lock();
0cfb6aee7   Guillaume Knispel   ipc: optimize sem...
177
178
  		ipc_lock_object(ipcp);
  		return ipcp;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
179
  	}
7ca7e564e   Nadia Derbey   ipc: store ipcs i...
180
181
  
  	return NULL;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
182
  }
7ca7e564e   Nadia Derbey   ipc: store ipcs i...
183
  /**
8001c8581   Davidlohr Bueso   ipc: standardize ...
184
185
   * ipc_get_maxid - get the last assigned id
   * @ids: ipc identifier set
7ca7e564e   Nadia Derbey   ipc: store ipcs i...
186
   *
8001c8581   Davidlohr Bueso   ipc: standardize ...
187
   * Called with ipc_ids.rwsem held.
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
188
   */
7ca7e564e   Nadia Derbey   ipc: store ipcs i...
189
190
191
192
193
194
195
196
  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
197

7ca7e564e   Nadia Derbey   ipc: store ipcs i...
198
199
200
201
202
203
204
205
206
207
208
209
210
  	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
211
212
213
  }
  
  /**
8001c8581   Davidlohr Bueso   ipc: standardize ...
214
215
216
217
   * 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
218
   *
8001c8581   Davidlohr Bueso   ipc: standardize ...
219
220
221
222
   * 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
223
   *
8001c8581   Davidlohr Bueso   ipc: standardize ...
224
   * Called with writer ipc_ids.rwsem held.
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
225
   */
239521f31   Manfred Spraul   ipc: whitespace c...
226
  int ipc_addid(struct ipc_ids *ids, struct kern_ipc_perm *new, int size)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
227
  {
1efdb69b0   Eric W. Biederman   userns: Convert i...
228
229
  	kuid_t euid;
  	kgid_t egid;
0cfb6aee7   Guillaume Knispel   ipc: optimize sem...
230
  	int id, err;
03f595668   Stanislav Kinsbursky   ipc: add sysctl t...
231
  	int next_id = ids->next_id;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
232

7ca7e564e   Nadia Derbey   ipc: store ipcs i...
233
234
  	if (size > IPCMNI)
  		size = IPCMNI;
0cfb6aee7   Guillaume Knispel   ipc: optimize sem...
235
  	if (!ids->tables_initialized || ids->in_use >= size)
283bb7fad   Pierre Peiffer   IPC: fix error ca...
236
  		return -ENOSPC;
7ca7e564e   Nadia Derbey   ipc: store ipcs i...
237

54924ea33   Tejun Heo   ipc: convert to i...
238
  	idr_preload(GFP_KERNEL);
9405c03ee   Elena Reshetova   ipc: convert kern...
239
  	refcount_set(&new->refcount, 1);
e00b4ff7e   Nadia Derbey   sysvipc: fix the ...
240
  	spin_lock_init(&new->lock);
72a8ff2f9   Rafael Aquini   ipc: change kern_...
241
  	new->deleted = false;
e00b4ff7e   Nadia Derbey   sysvipc: fix the ...
242
243
  	rcu_read_lock();
  	spin_lock(&new->lock);
b9a532277   Linus Torvalds   Initialize msg/sh...
244
245
246
  	current_euid_egid(&euid, &egid);
  	new->cuid = new->uid = euid;
  	new->gid = new->cgid = egid;
54924ea33   Tejun Heo   ipc: convert to i...
247
248
249
250
  	id = idr_alloc(&ids->ipcs_idr, new,
  		       (next_id < 0) ? 0 : ipcid_to_idx(next_id), 0,
  		       GFP_NOWAIT);
  	idr_preload_end();
0cfb6aee7   Guillaume Knispel   ipc: optimize sem...
251
252
253
254
255
256
257
258
259
  
  	if (id >= 0 && new->key != IPC_PRIVATE) {
  		err = rhashtable_insert_fast(&ids->key_ht, &new->khtnode,
  					     ipc_kht_params);
  		if (err < 0) {
  			idr_remove(&ids->ipcs_idr, id);
  			id = err;
  		}
  	}
54924ea33   Tejun Heo   ipc: convert to i...
260
  	if (id < 0) {
e00b4ff7e   Nadia Derbey   sysvipc: fix the ...
261
262
  		spin_unlock(&new->lock);
  		rcu_read_unlock();
54924ea33   Tejun Heo   ipc: convert to i...
263
  		return id;
e00b4ff7e   Nadia Derbey   sysvipc: fix the ...
264
  	}
7ca7e564e   Nadia Derbey   ipc: store ipcs i...
265

1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
266
  	ids->in_use++;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
267

03f595668   Stanislav Kinsbursky   ipc: add sysctl t...
268
269
  	if (next_id < 0) {
  		new->seq = ids->seq++;
daf948c7d   Davidlohr Bueso   ipc: delete seq_m...
270
  		if (ids->seq > IPCID_SEQ_MAX)
03f595668   Stanislav Kinsbursky   ipc: add sysctl t...
271
272
273
274
275
  			ids->seq = 0;
  	} else {
  		new->seq = ipcid_to_seqx(next_id);
  		ids->next_id = -1;
  	}
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
276

48dea404e   Pierre Peiffer   IPC: use ipc_buil...
277
  	new->id = ipc_buildid(id, new->seq);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
278
279
280
281
  	return id;
  }
  
  /**
8001c8581   Davidlohr Bueso   ipc: standardize ...
282
283
   * ipcget_new -	create a new ipc object
   * @ns: ipc namespace
da3dae54e   Masanari Iida   Documentation: Do...
284
   * @ids: ipc identifier set
8001c8581   Davidlohr Bueso   ipc: standardize ...
285
286
287
288
289
   * @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...
290
   */
b2d75cddc   Pavel Emelyanov   ipc: uninline som...
291
  static int ipcget_new(struct ipc_namespace *ns, struct ipc_ids *ids,
eb66ec44f   Mathias Krause   ipc: constify ipc...
292
  		const struct ipc_ops *ops, struct ipc_params *params)
7748dbfaa   Nadia Derbey   ipc: unify the sy...
293
294
  {
  	int err;
7748dbfaa   Nadia Derbey   ipc: unify the sy...
295

d9a605e40   Davidlohr Bueso   ipc: rename ids->...
296
  	down_write(&ids->rwsem);
7748dbfaa   Nadia Derbey   ipc: unify the sy...
297
  	err = ops->getnew(ns, params);
d9a605e40   Davidlohr Bueso   ipc: rename ids->...
298
  	up_write(&ids->rwsem);
7748dbfaa   Nadia Derbey   ipc: unify the sy...
299
300
301
302
  	return err;
  }
  
  /**
8001c8581   Davidlohr Bueso   ipc: standardize ...
303
304
305
306
307
   * 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...
308
   *
8001c8581   Davidlohr Bueso   ipc: standardize ...
309
310
311
   * 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...
312
   *
8001c8581   Davidlohr Bueso   ipc: standardize ...
313
   * On success, the ipc id is returned.
f4566f048   Nadia Derbey   ipc: fix wrong co...
314
   *
8001c8581   Davidlohr Bueso   ipc: standardize ...
315
   * It is called with ipc_ids.rwsem and ipcp->lock held.
7748dbfaa   Nadia Derbey   ipc: unify the sy...
316
   */
b0e77598f   Serge E. Hallyn   userns: user name...
317
318
  static int ipc_check_perms(struct ipc_namespace *ns,
  			   struct kern_ipc_perm *ipcp,
eb66ec44f   Mathias Krause   ipc: constify ipc...
319
  			   const struct ipc_ops *ops,
b0e77598f   Serge E. Hallyn   userns: user name...
320
  			   struct ipc_params *params)
7748dbfaa   Nadia Derbey   ipc: unify the sy...
321
322
  {
  	int err;
b0e77598f   Serge E. Hallyn   userns: user name...
323
  	if (ipcperms(ns, ipcp, params->flg))
7748dbfaa   Nadia Derbey   ipc: unify the sy...
324
325
326
327
328
329
330
331
332
333
334
  		err = -EACCES;
  	else {
  		err = ops->associate(ipcp, params->flg);
  		if (!err)
  			err = ipcp->id;
  	}
  
  	return err;
  }
  
  /**
8001c8581   Davidlohr Bueso   ipc: standardize ...
335
336
   * ipcget_public - get an ipc object or create a new one
   * @ns: ipc namespace
da3dae54e   Masanari Iida   Documentation: Do...
337
   * @ids: ipc identifier set
8001c8581   Davidlohr Bueso   ipc: standardize ...
338
339
340
341
342
343
344
345
346
   * @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...
347
   */
b2d75cddc   Pavel Emelyanov   ipc: uninline som...
348
  static int ipcget_public(struct ipc_namespace *ns, struct ipc_ids *ids,
eb66ec44f   Mathias Krause   ipc: constify ipc...
349
  		const struct ipc_ops *ops, struct ipc_params *params)
7748dbfaa   Nadia Derbey   ipc: unify the sy...
350
351
352
353
  {
  	struct kern_ipc_perm *ipcp;
  	int flg = params->flg;
  	int err;
7748dbfaa   Nadia Derbey   ipc: unify the sy...
354

3e148c799   Nadia Derbey   fix idr_find() lo...
355
356
357
358
  	/*
  	 * 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->...
359
  	down_write(&ids->rwsem);
7748dbfaa   Nadia Derbey   ipc: unify the sy...
360
361
362
363
364
  	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...
365
366
367
368
369
370
371
372
373
374
375
376
  		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...
377
378
379
380
  				/*
  				 * ipc_check_perms returns the IPC id on
  				 * success
  				 */
b0e77598f   Serge E. Hallyn   userns: user name...
381
  				err = ipc_check_perms(ns, ipcp, ops, params);
7748dbfaa   Nadia Derbey   ipc: unify the sy...
382
383
384
  		}
  		ipc_unlock(ipcp);
  	}
d9a605e40   Davidlohr Bueso   ipc: rename ids->...
385
  	up_write(&ids->rwsem);
7748dbfaa   Nadia Derbey   ipc: unify the sy...
386
387
388
  
  	return err;
  }
0cfb6aee7   Guillaume Knispel   ipc: optimize sem...
389
390
391
392
393
394
395
396
397
398
399
400
401
402
  /**
   * ipc_kht_remove - remove an ipc from the key hashtable
   * @ids: ipc identifier set
   * @ipcp: ipc perm structure containing the key to remove
   *
   * 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.
   */
  static void ipc_kht_remove(struct ipc_ids *ids, struct kern_ipc_perm *ipcp)
  {
  	if (ipcp->key != IPC_PRIVATE)
  		rhashtable_remove_fast(&ids->key_ht, &ipcp->khtnode,
  				       ipc_kht_params);
  }
7748dbfaa   Nadia Derbey   ipc: unify the sy...
403
404
  
  /**
8001c8581   Davidlohr Bueso   ipc: standardize ...
405
406
407
   * 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
408
   *
8001c8581   Davidlohr Bueso   ipc: standardize ...
409
410
   * 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
411
   */
7ca7e564e   Nadia Derbey   ipc: store ipcs i...
412
  void ipc_rmid(struct ipc_ids *ids, struct kern_ipc_perm *ipcp)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
413
  {
ce621f5ba   Nadia Derbey   ipc: introduce th...
414
  	int lid = ipcid_to_idx(ipcp->id);
7ca7e564e   Nadia Derbey   ipc: store ipcs i...
415
416
  
  	idr_remove(&ids->ipcs_idr, lid);
0cfb6aee7   Guillaume Knispel   ipc: optimize sem...
417
  	ipc_kht_remove(ids, ipcp);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
418
  	ids->in_use--;
72a8ff2f9   Rafael Aquini   ipc: change kern_...
419
  	ipcp->deleted = true;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
420
  }
0cfb6aee7   Guillaume Knispel   ipc: optimize sem...
421
422
423
424
425
426
427
428
429
430
431
432
433
  /**
   * ipc_set_key_private - switch the key of an existing ipc to IPC_PRIVATE
   * @ids: ipc identifier set
   * @ipcp: ipc perm structure containing the key to modify
   *
   * 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.
   */
  void ipc_set_key_private(struct ipc_ids *ids, struct kern_ipc_perm *ipcp)
  {
  	ipc_kht_remove(ids, ipcp);
  	ipcp->key = IPC_PRIVATE;
  }
dba4cdd39   Manfred Spraul   ipc: merge ipc_rc...
434
  int ipc_rcu_getref(struct kern_ipc_perm *ptr)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
435
  {
9405c03ee   Elena Reshetova   ipc: convert kern...
436
  	return refcount_inc_not_zero(&ptr->refcount);
65f27f384   David Howells   WorkStruct: Pass ...
437
  }
dba4cdd39   Manfred Spraul   ipc: merge ipc_rc...
438
439
  void ipc_rcu_putref(struct kern_ipc_perm *ptr,
  			void (*func)(struct rcu_head *head))
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
440
  {
9405c03ee   Elena Reshetova   ipc: convert kern...
441
  	if (!refcount_dec_and_test(&ptr->refcount))
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
442
  		return;
dba4cdd39   Manfred Spraul   ipc: merge ipc_rc...
443
  	call_rcu(&ptr->rcu, func);
53dad6d3a   Davidlohr Bueso   ipc: fix race wit...
444
  }
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
445
  /**
8001c8581   Davidlohr Bueso   ipc: standardize ...
446
447
448
449
   * ipcperms - check ipc permissions
   * @ns: ipc namespace
   * @ipcp: ipc permission set
   * @flag: desired permission set
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
450
   *
8001c8581   Davidlohr Bueso   ipc: standardize ...
451
452
   * Check user, group, other permissions for access
   * to ipc resources. return 0 if allowed
b0e77598f   Serge E. Hallyn   userns: user name...
453
   *
0e056eb55   mchehab@s-opensource.com   kernel-api.rst: f...
454
   * @flag will most probably be 0 or ``S_...UGO`` from <linux/stat.h>
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
455
   */
b0e77598f   Serge E. Hallyn   userns: user name...
456
457
  int ipcperms(struct ipc_namespace *ns, struct kern_ipc_perm *ipcp, short flag)
  {
1efdb69b0   Eric W. Biederman   userns: Convert i...
458
  	kuid_t euid = current_euid();
a33e67510   Al Viro   sanitize audit_ip...
459
  	int requested_mode, granted_mode;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
460

a33e67510   Al Viro   sanitize audit_ip...
461
  	audit_ipc_obj(ipcp);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
462
463
  	requested_mode = (flag >> 6) | (flag >> 3) | flag;
  	granted_mode = ipcp->mode;
1efdb69b0   Eric W. Biederman   userns: Convert i...
464
465
  	if (uid_eq(euid, ipcp->cuid) ||
  	    uid_eq(euid, ipcp->uid))
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
466
467
468
469
  		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? */
46c0a8ca3   Paul McQuade   ipc, kernel: clea...
470
  	if ((requested_mode & ~granted_mode & 0007) &&
b0e77598f   Serge E. Hallyn   userns: user name...
471
  	    !ns_capable(ns->user_ns, CAP_IPC_OWNER))
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
472
473
474
475
476
477
478
479
480
481
482
  		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 ...
483
484
485
   * 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
486
   *
8001c8581   Davidlohr Bueso   ipc: standardize ...
487
488
   * Turn the kernel object @in into a set of permissions descriptions
   * for returning to userspace (@out).
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
489
   */
239521f31   Manfred Spraul   ipc: whitespace c...
490
  void kernel_to_ipc64_perm(struct kern_ipc_perm *in, struct ipc64_perm *out)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
491
492
  {
  	out->key	= in->key;
1efdb69b0   Eric W. Biederman   userns: Convert i...
493
494
495
496
  	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
497
498
499
500
501
  	out->mode	= in->mode;
  	out->seq	= in->seq;
  }
  
  /**
8001c8581   Davidlohr Bueso   ipc: standardize ...
502
503
504
   * 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
505
   *
8001c8581   Davidlohr Bueso   ipc: standardize ...
506
507
   * 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
508
   */
239521f31   Manfred Spraul   ipc: whitespace c...
509
  void ipc64_perm_to_ipc_perm(struct ipc64_perm *in, struct ipc_perm *out)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
510
511
512
513
514
515
516
517
518
  {
  	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...
519
  /**
0cfb6aee7   Guillaume Knispel   ipc: optimize sem...
520
   * ipc_obtain_object_idr
4d2bff5eb   Davidlohr Bueso   ipc: introduce ob...
521
522
523
524
525
526
527
528
   * @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.
   */
55b7ae501   Davidlohr Bueso   ipc: rename ipc_o...
529
  struct kern_ipc_perm *ipc_obtain_object_idr(struct ipc_ids *ids, int id)
4d2bff5eb   Davidlohr Bueso   ipc: introduce ob...
530
531
532
  {
  	struct kern_ipc_perm *out;
  	int lid = ipcid_to_idx(id);
0cfb6aee7   Guillaume Knispel   ipc: optimize sem...
533
534
  	if (unlikely(!ids->tables_initialized))
  		return ERR_PTR(-EINVAL);
4d2bff5eb   Davidlohr Bueso   ipc: introduce ob...
535
536
537
538
539
540
541
542
  	out = idr_find(&ids->ipcs_idr, lid);
  	if (!out)
  		return ERR_PTR(-EINVAL);
  
  	return out;
  }
  
  /**
8001c8581   Davidlohr Bueso   ipc: standardize ...
543
544
   * ipc_lock - lock an ipc structure without rwsem held
   * @ids: ipc identifier set
f4566f048   Nadia Derbey   ipc: fix wrong co...
545
546
547
548
   * @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...
549
   * The ipc object is locked on successful exit.
f4566f048   Nadia Derbey   ipc: fix wrong co...
550
   */
7ca7e564e   Nadia Derbey   ipc: store ipcs i...
551
  struct kern_ipc_perm *ipc_lock(struct ipc_ids *ids, int id)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
552
  {
7ca7e564e   Nadia Derbey   ipc: store ipcs i...
553
  	struct kern_ipc_perm *out;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
554
555
  
  	rcu_read_lock();
55b7ae501   Davidlohr Bueso   ipc: rename ipc_o...
556
  	out = ipc_obtain_object_idr(ids, id);
4d2bff5eb   Davidlohr Bueso   ipc: introduce ob...
557
  	if (IS_ERR(out))
f8b591849   Davidlohr Bueso   ipc,sysv: make re...
558
  		goto err;
7ca7e564e   Nadia Derbey   ipc: store ipcs i...
559

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

f8b591849   Davidlohr Bueso   ipc,sysv: make re...
562
563
564
565
566
  	/*
  	 * ipc_rmid() may have already freed the ID while ipc_lock()
  	 * was spinning: here verify that the structure is still valid.
  	 * Upon races with RMID, return -EIDRM, thus indicating that
  	 * the ID points to a removed identifier.
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
567
  	 */
72a8ff2f9   Rafael Aquini   ipc: change kern_...
568
  	if (ipc_valid_object(out))
4d2bff5eb   Davidlohr Bueso   ipc: introduce ob...
569
570
571
  		return out;
  
  	spin_unlock(&out->lock);
f8b591849   Davidlohr Bueso   ipc,sysv: make re...
572
573
  	out = ERR_PTR(-EIDRM);
  err:
4d2bff5eb   Davidlohr Bueso   ipc: introduce ob...
574
575
576
  	rcu_read_unlock();
  	return out;
  }
7ca7e564e   Nadia Derbey   ipc: store ipcs i...
577

4d2bff5eb   Davidlohr Bueso   ipc: introduce ob...
578
579
580
581
582
  /**
   * ipc_obtain_object_check
   * @ids: ipc identifier set
   * @id: ipc id to look for
   *
55b7ae501   Davidlohr Bueso   ipc: rename ipc_o...
583
   * Similar to ipc_obtain_object_idr() but also checks
4d2bff5eb   Davidlohr Bueso   ipc: introduce ob...
584
585
586
587
588
589
590
   * 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)
  {
55b7ae501   Davidlohr Bueso   ipc: rename ipc_o...
591
  	struct kern_ipc_perm *out = ipc_obtain_object_idr(ids, id);
4d2bff5eb   Davidlohr Bueso   ipc: introduce ob...
592
593
594
595
596
  
  	if (IS_ERR(out))
  		goto out;
  
  	if (ipc_checkid(out, id))
6157dbbfb   Davidlohr Bueso   ipc,sysv: return ...
597
  		return ERR_PTR(-EINVAL);
4d2bff5eb   Davidlohr Bueso   ipc: introduce ob...
598
  out:
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
599
600
  	return out;
  }
b2d75cddc   Pavel Emelyanov   ipc: uninline som...
601
602
  /**
   * ipcget - Common sys_*get() code
da3dae54e   Masanari Iida   Documentation: Do...
603
   * @ns: namespace
8001c8581   Davidlohr Bueso   ipc: standardize ...
604
605
606
607
   * @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...
608
609
610
611
   *
   * Common routine called by sys_msgget(), sys_semget() and sys_shmget().
   */
  int ipcget(struct ipc_namespace *ns, struct ipc_ids *ids,
eb66ec44f   Mathias Krause   ipc: constify ipc...
612
  			const struct ipc_ops *ops, struct ipc_params *params)
b2d75cddc   Pavel Emelyanov   ipc: uninline som...
613
614
615
616
617
618
  {
  	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...
619
  /**
8001c8581   Davidlohr Bueso   ipc: standardize ...
620
   * ipc_update_perm - update the permissions of an ipc object
8f4a3809c   Pierre Peiffer   IPC: introduce ip...
621
622
623
   * @in:  the permission given as input.
   * @out: the permission of the ipc to set.
   */
1efdb69b0   Eric W. Biederman   userns: Convert i...
624
  int ipc_update_perm(struct ipc64_perm *in, struct kern_ipc_perm *out)
8f4a3809c   Pierre Peiffer   IPC: introduce ip...
625
  {
1efdb69b0   Eric W. Biederman   userns: Convert i...
626
627
628
629
630
631
632
  	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...
633
634
  	out->mode = (out->mode & ~S_IRWXUGO)
  		| (in->mode & S_IRWXUGO);
1efdb69b0   Eric W. Biederman   userns: Convert i...
635
636
  
  	return 0;
8f4a3809c   Pierre Peiffer   IPC: introduce ip...
637
  }
a5f75e7f2   Pierre Peiffer   IPC: consolidate ...
638
  /**
3b1c4ad37   Davidlohr Bueso   ipc: drop ipcctl_...
639
   * ipcctl_pre_down_nolock - retrieve an ipc and check permissions for some IPC_XXX cmd
8001c8581   Davidlohr Bueso   ipc: standardize ...
640
   * @ns:  ipc namespace
a5f75e7f2   Pierre Peiffer   IPC: consolidate ...
641
642
643
644
645
646
647
648
   * @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.
0e056eb55   mchehab@s-opensource.com   kernel-api.rst: f...
649
650
651
652
653
654
   * 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
   *   - returns a pointer to the ipc object or otherwise, the corresponding
   *     error.
7b4cc5d84   Davidlohr Bueso   ipc: move locking...
655
   *
d9a605e40   Davidlohr Bueso   ipc: rename ids->...
656
   * Call holding the both the rwsem and the rcu read lock.
a5f75e7f2   Pierre Peiffer   IPC: consolidate ...
657
   */
444d0f621   Davidlohr Bueso   ipc: introduce lo...
658
  struct kern_ipc_perm *ipcctl_pre_down_nolock(struct ipc_namespace *ns,
3b1c4ad37   Davidlohr Bueso   ipc: drop ipcctl_...
659
660
  					struct ipc_ids *ids, int id, int cmd,
  					struct ipc64_perm *perm, int extra_perm)
444d0f621   Davidlohr Bueso   ipc: introduce lo...
661
  {
1efdb69b0   Eric W. Biederman   userns: Convert i...
662
  	kuid_t euid;
444d0f621   Davidlohr Bueso   ipc: introduce lo...
663
664
  	int err = -EPERM;
  	struct kern_ipc_perm *ipcp;
a5f75e7f2   Pierre Peiffer   IPC: consolidate ...
665

444d0f621   Davidlohr Bueso   ipc: introduce lo...
666
  	ipcp = ipc_obtain_object_check(ids, id);
a5f75e7f2   Pierre Peiffer   IPC: consolidate ...
667
668
  	if (IS_ERR(ipcp)) {
  		err = PTR_ERR(ipcp);
7b4cc5d84   Davidlohr Bueso   ipc: move locking...
669
  		goto err;
a5f75e7f2   Pierre Peiffer   IPC: consolidate ...
670
  	}
a33e67510   Al Viro   sanitize audit_ip...
671
  	audit_ipc_obj(ipcp);
e816f370c   Al Viro   sanitize audit_ip...
672
673
  	if (cmd == IPC_SET)
  		audit_ipc_set_perm(extra_perm, perm->uid,
444d0f621   Davidlohr Bueso   ipc: introduce lo...
674
  				   perm->gid, perm->mode);
414c0708d   David Howells   CRED: Wrap task c...
675
676
  
  	euid = current_euid();
1efdb69b0   Eric W. Biederman   userns: Convert i...
677
  	if (uid_eq(euid, ipcp->cuid) || uid_eq(euid, ipcp->uid)  ||
b0e77598f   Serge E. Hallyn   userns: user name...
678
  	    ns_capable(ns->user_ns, CAP_SYS_ADMIN))
7b4cc5d84   Davidlohr Bueso   ipc: move locking...
679
680
  		return ipcp; /* successful lookup */
  err:
a5f75e7f2   Pierre Peiffer   IPC: consolidate ...
681
682
  	return ERR_PTR(err);
  }
c1d7e01d7   Will Deacon   ipc: use Kconfig ...
683
  #ifdef CONFIG_ARCH_WANT_IPC_PARSE_VERSION
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
684
685
686
  
  
  /**
8001c8581   Davidlohr Bueso   ipc: standardize ...
687
688
   * ipc_parse_version - ipc call version
   * @cmd: pointer to command
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
689
   *
8001c8581   Davidlohr Bueso   ipc: standardize ...
690
691
692
   * 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
693
   */
239521f31   Manfred Spraul   ipc: whitespace c...
694
  int ipc_parse_version(int *cmd)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
695
696
697
698
699
700
701
702
  {
  	if (*cmd & IPC_64) {
  		*cmd ^= IPC_64;
  		return IPC_64;
  	} else {
  		return IPC_OLD;
  	}
  }
c1d7e01d7   Will Deacon   ipc: use Kconfig ...
703
  #endif /* CONFIG_ARCH_WANT_IPC_PARSE_VERSION */
ae7817745   Mike Waychison   [PATCH] ipc: add ...
704
705
  
  #ifdef CONFIG_PROC_FS
bc1fc6d88   Eric W. Biederman   [PATCH] ipc: save...
706
707
708
709
  struct ipc_proc_iter {
  	struct ipc_namespace *ns;
  	struct ipc_proc_iface *iface;
  };
7ca7e564e   Nadia Derbey   ipc: store ipcs i...
710
711
712
  /*
   * This routine locks the ipc structure found at least at position pos.
   */
b524b9adb   Adrian Bunk   make ipc/util.c:s...
713
714
  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 ...
715
  {
7ca7e564e   Nadia Derbey   ipc: store ipcs i...
716
717
  	struct kern_ipc_perm *ipc;
  	int total, id;
73ea41302   Kirill Korotaev   [PATCH] IPC names...
718

7ca7e564e   Nadia Derbey   ipc: store ipcs i...
719
720
721
722
723
724
  	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 ...
725

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

239521f31   Manfred Spraul   ipc: whitespace c...
729
  	for (; pos < IPCMNI; pos++) {
7ca7e564e   Nadia Derbey   ipc: store ipcs i...
730
731
732
  		ipc = idr_find(&ids->ipcs_idr, pos);
  		if (ipc != NULL) {
  			*new_pos = pos + 1;
32a275001   Davidlohr Bueso   ipc: drop ipc_loc...
733
734
  			rcu_read_lock();
  			ipc_lock_object(ipc);
ae7817745   Mike Waychison   [PATCH] ipc: add ...
735
736
737
738
739
740
741
  			return ipc;
  		}
  	}
  
  	/* Out of range - return NULL to terminate iteration */
  	return NULL;
  }
7ca7e564e   Nadia Derbey   ipc: store ipcs i...
742
743
744
745
746
747
748
749
750
  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 ...
751
  	return sysvipc_find_ipc(&iter->ns->ids[iface->ids], *pos, pos);
7ca7e564e   Nadia Derbey   ipc: store ipcs i...
752
  }
ae7817745   Mike Waychison   [PATCH] ipc: add ...
753
  /*
f4566f048   Nadia Derbey   ipc: fix wrong co...
754
755
   * 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 ...
756
757
758
   */
  static void *sysvipc_proc_start(struct seq_file *s, loff_t *pos)
  {
bc1fc6d88   Eric W. Biederman   [PATCH] ipc: save...
759
760
  	struct ipc_proc_iter *iter = s->private;
  	struct ipc_proc_iface *iface = iter->iface;
73ea41302   Kirill Korotaev   [PATCH] IPC names...
761
  	struct ipc_ids *ids;
ed2ddbf88   Pierre Peiffer   IPC: make struct ...
762
  	ids = &iter->ns->ids[iface->ids];
ae7817745   Mike Waychison   [PATCH] ipc: add ...
763
764
765
766
767
  
  	/*
  	 * Take the lock - this will be released by the corresponding
  	 * call to stop().
  	 */
d9a605e40   Davidlohr Bueso   ipc: rename ids->...
768
  	down_read(&ids->rwsem);
ae7817745   Mike Waychison   [PATCH] ipc: add ...
769
770
771
772
773
774
775
776
777
778
  
  	/* 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...
779
  	return sysvipc_find_ipc(ids, *pos - 1, pos);
ae7817745   Mike Waychison   [PATCH] ipc: add ...
780
781
782
783
784
  }
  
  static void sysvipc_proc_stop(struct seq_file *s, void *it)
  {
  	struct kern_ipc_perm *ipc = it;
bc1fc6d88   Eric W. Biederman   [PATCH] ipc: save...
785
786
  	struct ipc_proc_iter *iter = s->private;
  	struct ipc_proc_iface *iface = iter->iface;
73ea41302   Kirill Korotaev   [PATCH] IPC names...
787
  	struct ipc_ids *ids;
ae7817745   Mike Waychison   [PATCH] ipc: add ...
788

f4566f048   Nadia Derbey   ipc: fix wrong co...
789
  	/* If we had a locked structure, release it */
ae7817745   Mike Waychison   [PATCH] ipc: add ...
790
791
  	if (ipc && ipc != SEQ_START_TOKEN)
  		ipc_unlock(ipc);
ed2ddbf88   Pierre Peiffer   IPC: make struct ...
792
  	ids = &iter->ns->ids[iface->ids];
ae7817745   Mike Waychison   [PATCH] ipc: add ...
793
  	/* Release the lock we took in start() */
d9a605e40   Davidlohr Bueso   ipc: rename ids->...
794
  	up_read(&ids->rwsem);
ae7817745   Mike Waychison   [PATCH] ipc: add ...
795
796
797
798
  }
  
  static int sysvipc_proc_show(struct seq_file *s, void *it)
  {
bc1fc6d88   Eric W. Biederman   [PATCH] ipc: save...
799
800
  	struct ipc_proc_iter *iter = s->private;
  	struct ipc_proc_iface *iface = iter->iface;
ae7817745   Mike Waychison   [PATCH] ipc: add ...
801

7f032d6ef   Joe Perches   ipc: remove use o...
802
803
804
805
  	if (it == SEQ_START_TOKEN) {
  		seq_puts(s, iface->header);
  		return 0;
  	}
ae7817745   Mike Waychison   [PATCH] ipc: add ...
806
807
808
  
  	return iface->show(s, it);
  }
88e9d34c7   James Morris   seq_file: constif...
809
  static const struct seq_operations sysvipc_proc_seqops = {
ae7817745   Mike Waychison   [PATCH] ipc: add ...
810
811
812
813
814
  	.start = sysvipc_proc_start,
  	.stop  = sysvipc_proc_stop,
  	.next  = sysvipc_proc_next,
  	.show  = sysvipc_proc_show,
  };
bc1fc6d88   Eric W. Biederman   [PATCH] ipc: save...
815
816
  static int sysvipc_proc_open(struct inode *inode, struct file *file)
  {
bc1fc6d88   Eric W. Biederman   [PATCH] ipc: save...
817
  	struct ipc_proc_iter *iter;
d66a0520c   Rob Jones   ipc/util.c: use _...
818
  	iter = __seq_open_private(file, &sysvipc_proc_seqops, sizeof(*iter));
bc1fc6d88   Eric W. Biederman   [PATCH] ipc: save...
819
  	if (!iter)
d66a0520c   Rob Jones   ipc/util.c: use _...
820
  		return -ENOMEM;
bc1fc6d88   Eric W. Biederman   [PATCH] ipc: save...
821

d9dda78ba   Al Viro   procfs: new helpe...
822
  	iter->iface = PDE_DATA(inode);
bc1fc6d88   Eric W. Biederman   [PATCH] ipc: save...
823
  	iter->ns    = get_ipc_ns(current->nsproxy->ipc_ns);
d66a0520c   Rob Jones   ipc/util.c: use _...
824
825
  
  	return 0;
bc1fc6d88   Eric W. Biederman   [PATCH] ipc: save...
826
827
828
829
830
831
832
833
  }
  
  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 ...
834
  }
9a32144e9   Arjan van de Ven   [PATCH] mark stru...
835
  static const struct file_operations sysvipc_proc_fops = {
ae7817745   Mike Waychison   [PATCH] ipc: add ...
836
837
838
  	.open    = sysvipc_proc_open,
  	.read    = seq_read,
  	.llseek  = seq_lseek,
bc1fc6d88   Eric W. Biederman   [PATCH] ipc: save...
839
  	.release = sysvipc_proc_release,
ae7817745   Mike Waychison   [PATCH] ipc: add ...
840
841
  };
  #endif /* CONFIG_PROC_FS */