Commit | Line | Data |
---|---|---|
1da177e4 LT |
1 | /* |
2 | * linux/ipc/util.c | |
3 | * Copyright (C) 1992 Krishna Balasubramanian | |
4 | * | |
5 | * Sep 1997 - Call suser() last after "normal" permission checks so we | |
6 | * get BSD style process accounting right. | |
7 | * Occurs in several places in the IPC code. | |
8 | * Chris Evans, <chris@ferret.lmh.ox.ac.uk> | |
9 | * Nov 1999 - ipc helper functions, unified SMP locking | |
624dffcb | 10 | * Manfred Spraul <manfred@colorfullife.com> |
1da177e4 LT |
11 | * Oct 2002 - One lock per IPC id. RCU ipc_free for lock-free grow_ary(). |
12 | * Mingming Cao <cmm@us.ibm.com> | |
073115d6 SG |
13 | * Mar 2006 - support for audit of ipc object properties |
14 | * Dustin Kirkland <dustin.kirkland@us.ibm.com> | |
73ea4130 KK |
15 | * Jun 2006 - namespaces ssupport |
16 | * OpenVZ, SWsoft Inc. | |
17 | * Pavel Emelianov <xemul@openvz.org> | |
1da177e4 LT |
18 | */ |
19 | ||
1da177e4 LT |
20 | #include <linux/mm.h> |
21 | #include <linux/shm.h> | |
22 | #include <linux/init.h> | |
23 | #include <linux/msg.h> | |
24 | #include <linux/smp_lock.h> | |
25 | #include <linux/vmalloc.h> | |
26 | #include <linux/slab.h> | |
c59ede7b | 27 | #include <linux/capability.h> |
1da177e4 LT |
28 | #include <linux/highuid.h> |
29 | #include <linux/security.h> | |
30 | #include <linux/rcupdate.h> | |
31 | #include <linux/workqueue.h> | |
ae781774 MW |
32 | #include <linux/seq_file.h> |
33 | #include <linux/proc_fs.h> | |
073115d6 | 34 | #include <linux/audit.h> |
73ea4130 | 35 | #include <linux/nsproxy.h> |
1da177e4 LT |
36 | |
37 | #include <asm/unistd.h> | |
38 | ||
39 | #include "util.h" | |
40 | ||
ae781774 MW |
41 | struct ipc_proc_iface { |
42 | const char *path; | |
43 | const char *header; | |
73ea4130 | 44 | int ids; |
ae781774 MW |
45 | int (*show)(struct seq_file *, void *); |
46 | }; | |
47 | ||
73ea4130 KK |
48 | struct ipc_namespace init_ipc_ns = { |
49 | .kref = { | |
50 | .refcount = ATOMIC_INIT(2), | |
51 | }, | |
52 | }; | |
53 | ||
54 | #ifdef CONFIG_IPC_NS | |
55 | static struct ipc_namespace *clone_ipc_ns(struct ipc_namespace *old_ns) | |
56 | { | |
57 | int err; | |
58 | struct ipc_namespace *ns; | |
59 | ||
60 | err = -ENOMEM; | |
61 | ns = kmalloc(sizeof(struct ipc_namespace), GFP_KERNEL); | |
62 | if (ns == NULL) | |
63 | goto err_mem; | |
64 | ||
65 | err = sem_init_ns(ns); | |
66 | if (err) | |
67 | goto err_sem; | |
68 | err = msg_init_ns(ns); | |
69 | if (err) | |
70 | goto err_msg; | |
71 | err = shm_init_ns(ns); | |
72 | if (err) | |
73 | goto err_shm; | |
74 | ||
75 | kref_init(&ns->kref); | |
76 | return ns; | |
77 | ||
78 | err_shm: | |
79 | msg_exit_ns(ns); | |
80 | err_msg: | |
81 | sem_exit_ns(ns); | |
82 | err_sem: | |
83 | kfree(ns); | |
84 | err_mem: | |
85 | return ERR_PTR(err); | |
86 | } | |
87 | ||
e3222c4e | 88 | struct ipc_namespace *copy_ipcs(unsigned long flags, struct ipc_namespace *ns) |
73ea4130 | 89 | { |
73ea4130 | 90 | struct ipc_namespace *new_ns; |
73ea4130 | 91 | |
e3222c4e BP |
92 | BUG_ON(!ns); |
93 | get_ipc_ns(ns); | |
73ea4130 KK |
94 | |
95 | if (!(flags & CLONE_NEWIPC)) | |
e3222c4e BP |
96 | return ns; |
97 | ||
98 | new_ns = clone_ipc_ns(ns); | |
99 | ||
100 | put_ipc_ns(ns); | |
101 | return new_ns; | |
73ea4130 KK |
102 | } |
103 | ||
104 | void free_ipc_ns(struct kref *kref) | |
105 | { | |
106 | struct ipc_namespace *ns; | |
107 | ||
108 | ns = container_of(kref, struct ipc_namespace, kref); | |
109 | sem_exit_ns(ns); | |
110 | msg_exit_ns(ns); | |
111 | shm_exit_ns(ns); | |
112 | kfree(ns); | |
113 | } | |
a28d193c | 114 | #else |
e3222c4e | 115 | struct ipc_namespace *copy_ipcs(unsigned long flags, struct ipc_namespace *ns) |
a28d193c SH |
116 | { |
117 | if (flags & CLONE_NEWIPC) | |
e3222c4e BP |
118 | return ERR_PTR(-EINVAL); |
119 | return ns; | |
a28d193c | 120 | } |
73ea4130 KK |
121 | #endif |
122 | ||
1da177e4 LT |
123 | /** |
124 | * ipc_init - initialise IPC subsystem | |
125 | * | |
126 | * The various system5 IPC resources (semaphores, messages and shared | |
72fd4a35 | 127 | * memory) are initialised |
1da177e4 LT |
128 | */ |
129 | ||
130 | static int __init ipc_init(void) | |
131 | { | |
132 | sem_init(); | |
133 | msg_init(); | |
134 | shm_init(); | |
135 | return 0; | |
136 | } | |
137 | __initcall(ipc_init); | |
138 | ||
139 | /** | |
140 | * ipc_init_ids - initialise IPC identifiers | |
141 | * @ids: Identifier set | |
142 | * @size: Number of identifiers | |
143 | * | |
144 | * Given a size for the ipc identifier range (limited below IPCMNI) | |
145 | * set up the sequence range to use then allocate and initialise the | |
146 | * array itself. | |
147 | */ | |
148 | ||
73ea4130 | 149 | void __ipc_init ipc_init_ids(struct ipc_ids* ids, int size) |
1da177e4 LT |
150 | { |
151 | int i; | |
5f921ae9 IM |
152 | |
153 | mutex_init(&ids->mutex); | |
1da177e4 LT |
154 | |
155 | if(size > IPCMNI) | |
156 | size = IPCMNI; | |
157 | ids->in_use = 0; | |
158 | ids->max_id = -1; | |
159 | ids->seq = 0; | |
160 | { | |
161 | int seq_limit = INT_MAX/SEQ_MULTIPLIER; | |
162 | if(seq_limit > USHRT_MAX) | |
163 | ids->seq_max = USHRT_MAX; | |
164 | else | |
165 | ids->seq_max = seq_limit; | |
166 | } | |
167 | ||
168 | ids->entries = ipc_rcu_alloc(sizeof(struct kern_ipc_perm *)*size + | |
169 | sizeof(struct ipc_id_ary)); | |
170 | ||
171 | if(ids->entries == NULL) { | |
172 | printk(KERN_ERR "ipc_init_ids() failed, ipc service disabled.\n"); | |
173 | size = 0; | |
174 | ids->entries = &ids->nullentry; | |
175 | } | |
176 | ids->entries->size = size; | |
177 | for(i=0;i<size;i++) | |
178 | ids->entries->p[i] = NULL; | |
179 | } | |
180 | ||
ae781774 | 181 | #ifdef CONFIG_PROC_FS |
9a32144e | 182 | static const struct file_operations sysvipc_proc_fops; |
ae781774 | 183 | /** |
72fd4a35 | 184 | * ipc_init_proc_interface - Create a proc interface for sysipc types using a seq_file interface. |
ae781774 MW |
185 | * @path: Path in procfs |
186 | * @header: Banner to be printed at the beginning of the file. | |
187 | * @ids: ipc id table to iterate. | |
188 | * @show: show routine. | |
189 | */ | |
190 | void __init ipc_init_proc_interface(const char *path, const char *header, | |
73ea4130 | 191 | int ids, int (*show)(struct seq_file *, void *)) |
ae781774 MW |
192 | { |
193 | struct proc_dir_entry *pde; | |
194 | struct ipc_proc_iface *iface; | |
195 | ||
196 | iface = kmalloc(sizeof(*iface), GFP_KERNEL); | |
197 | if (!iface) | |
198 | return; | |
199 | iface->path = path; | |
200 | iface->header = header; | |
201 | iface->ids = ids; | |
202 | iface->show = show; | |
203 | ||
204 | pde = create_proc_entry(path, | |
205 | S_IRUGO, /* world readable */ | |
206 | NULL /* parent dir */); | |
207 | if (pde) { | |
208 | pde->data = iface; | |
209 | pde->proc_fops = &sysvipc_proc_fops; | |
210 | } else { | |
211 | kfree(iface); | |
212 | } | |
213 | } | |
214 | #endif | |
215 | ||
1da177e4 LT |
216 | /** |
217 | * ipc_findkey - find a key in an ipc identifier set | |
218 | * @ids: Identifier set | |
219 | * @key: The key to find | |
220 | * | |
5f921ae9 | 221 | * Requires ipc_ids.mutex locked. |
1da177e4 LT |
222 | * Returns the identifier if found or -1 if not. |
223 | */ | |
224 | ||
225 | int ipc_findkey(struct ipc_ids* ids, key_t key) | |
226 | { | |
227 | int id; | |
228 | struct kern_ipc_perm* p; | |
229 | int max_id = ids->max_id; | |
230 | ||
231 | /* | |
232 | * rcu_dereference() is not needed here | |
5f921ae9 | 233 | * since ipc_ids.mutex is held |
1da177e4 LT |
234 | */ |
235 | for (id = 0; id <= max_id; id++) { | |
236 | p = ids->entries->p[id]; | |
237 | if(p==NULL) | |
238 | continue; | |
239 | if (key == p->key) | |
240 | return id; | |
241 | } | |
242 | return -1; | |
243 | } | |
244 | ||
245 | /* | |
5f921ae9 | 246 | * Requires ipc_ids.mutex locked |
1da177e4 LT |
247 | */ |
248 | static int grow_ary(struct ipc_ids* ids, int newsize) | |
249 | { | |
250 | struct ipc_id_ary* new; | |
251 | struct ipc_id_ary* old; | |
252 | int i; | |
253 | int size = ids->entries->size; | |
254 | ||
255 | if(newsize > IPCMNI) | |
256 | newsize = IPCMNI; | |
257 | if(newsize <= size) | |
258 | return newsize; | |
259 | ||
260 | new = ipc_rcu_alloc(sizeof(struct kern_ipc_perm *)*newsize + | |
261 | sizeof(struct ipc_id_ary)); | |
262 | if(new == NULL) | |
263 | return size; | |
264 | new->size = newsize; | |
a9a5cd5d | 265 | memcpy(new->p, ids->entries->p, sizeof(struct kern_ipc_perm *)*size); |
1da177e4 LT |
266 | for(i=size;i<newsize;i++) { |
267 | new->p[i] = NULL; | |
268 | } | |
269 | old = ids->entries; | |
270 | ||
271 | /* | |
272 | * Use rcu_assign_pointer() to make sure the memcpyed contents | |
273 | * of the new array are visible before the new array becomes visible. | |
274 | */ | |
275 | rcu_assign_pointer(ids->entries, new); | |
276 | ||
c7e12b83 | 277 | __ipc_fini_ids(ids, old); |
1da177e4 LT |
278 | return newsize; |
279 | } | |
280 | ||
281 | /** | |
282 | * ipc_addid - add an IPC identifier | |
283 | * @ids: IPC identifier set | |
284 | * @new: new IPC permission set | |
285 | * @size: new size limit for the id array | |
286 | * | |
287 | * Add an entry 'new' to the IPC arrays. The permissions object is | |
288 | * initialised and the first free entry is set up and the id assigned | |
289 | * is returned. The list is returned in a locked state on success. | |
290 | * On failure the list is not locked and -1 is returned. | |
291 | * | |
5f921ae9 | 292 | * Called with ipc_ids.mutex held. |
1da177e4 LT |
293 | */ |
294 | ||
295 | int ipc_addid(struct ipc_ids* ids, struct kern_ipc_perm* new, int size) | |
296 | { | |
297 | int id; | |
298 | ||
299 | size = grow_ary(ids,size); | |
300 | ||
301 | /* | |
302 | * rcu_dereference()() is not needed here since | |
5f921ae9 | 303 | * ipc_ids.mutex is held |
1da177e4 LT |
304 | */ |
305 | for (id = 0; id < size; id++) { | |
306 | if(ids->entries->p[id] == NULL) | |
307 | goto found; | |
308 | } | |
309 | return -1; | |
310 | found: | |
311 | ids->in_use++; | |
312 | if (id > ids->max_id) | |
313 | ids->max_id = id; | |
314 | ||
315 | new->cuid = new->uid = current->euid; | |
316 | new->gid = new->cgid = current->egid; | |
317 | ||
318 | new->seq = ids->seq++; | |
319 | if(ids->seq > ids->seq_max) | |
320 | ids->seq = 0; | |
321 | ||
322 | spin_lock_init(&new->lock); | |
323 | new->deleted = 0; | |
324 | rcu_read_lock(); | |
325 | spin_lock(&new->lock); | |
326 | ids->entries->p[id] = new; | |
327 | return id; | |
328 | } | |
329 | ||
330 | /** | |
331 | * ipc_rmid - remove an IPC identifier | |
332 | * @ids: identifier set | |
333 | * @id: Identifier to remove | |
334 | * | |
335 | * The identifier must be valid, and in use. The kernel will panic if | |
336 | * fed an invalid identifier. The entry is removed and internal | |
337 | * variables recomputed. The object associated with the identifier | |
338 | * is returned. | |
5f921ae9 | 339 | * ipc_ids.mutex and the spinlock for this ID is hold before this function |
1da177e4 LT |
340 | * is called, and remain locked on the exit. |
341 | */ | |
342 | ||
343 | struct kern_ipc_perm* ipc_rmid(struct ipc_ids* ids, int id) | |
344 | { | |
345 | struct kern_ipc_perm* p; | |
346 | int lid = id % SEQ_MULTIPLIER; | |
9bc98fc6 | 347 | BUG_ON(lid >= ids->entries->size); |
1da177e4 LT |
348 | |
349 | /* | |
350 | * do not need a rcu_dereference()() here to force ordering | |
5f921ae9 | 351 | * on Alpha, since the ipc_ids.mutex is held. |
1da177e4 LT |
352 | */ |
353 | p = ids->entries->p[lid]; | |
354 | ids->entries->p[lid] = NULL; | |
9bc98fc6 | 355 | BUG_ON(p==NULL); |
1da177e4 LT |
356 | ids->in_use--; |
357 | ||
358 | if (lid == ids->max_id) { | |
359 | do { | |
360 | lid--; | |
361 | if(lid == -1) | |
362 | break; | |
363 | } while (ids->entries->p[lid] == NULL); | |
364 | ids->max_id = lid; | |
365 | } | |
366 | p->deleted = 1; | |
367 | return p; | |
368 | } | |
369 | ||
370 | /** | |
371 | * ipc_alloc - allocate ipc space | |
372 | * @size: size desired | |
373 | * | |
374 | * Allocate memory from the appropriate pools and return a pointer to it. | |
375 | * NULL is returned if the allocation fails | |
376 | */ | |
377 | ||
378 | void* ipc_alloc(int size) | |
379 | { | |
380 | void* out; | |
381 | if(size > PAGE_SIZE) | |
382 | out = vmalloc(size); | |
383 | else | |
384 | out = kmalloc(size, GFP_KERNEL); | |
385 | return out; | |
386 | } | |
387 | ||
388 | /** | |
389 | * ipc_free - free ipc space | |
390 | * @ptr: pointer returned by ipc_alloc | |
391 | * @size: size of block | |
392 | * | |
72fd4a35 | 393 | * Free a block created with ipc_alloc(). The caller must know the size |
1da177e4 LT |
394 | * used in the allocation call. |
395 | */ | |
396 | ||
397 | void ipc_free(void* ptr, int size) | |
398 | { | |
399 | if(size > PAGE_SIZE) | |
400 | vfree(ptr); | |
401 | else | |
402 | kfree(ptr); | |
403 | } | |
404 | ||
405 | /* | |
406 | * rcu allocations: | |
407 | * There are three headers that are prepended to the actual allocation: | |
408 | * - during use: ipc_rcu_hdr. | |
409 | * - during the rcu grace period: ipc_rcu_grace. | |
410 | * - [only if vmalloc]: ipc_rcu_sched. | |
411 | * Their lifetime doesn't overlap, thus the headers share the same memory. | |
412 | * Unlike a normal union, they are right-aligned, thus some container_of | |
413 | * forward/backward casting is necessary: | |
414 | */ | |
415 | struct ipc_rcu_hdr | |
416 | { | |
417 | int refcount; | |
418 | int is_vmalloc; | |
419 | void *data[0]; | |
420 | }; | |
421 | ||
422 | ||
423 | struct ipc_rcu_grace | |
424 | { | |
425 | struct rcu_head rcu; | |
426 | /* "void *" makes sure alignment of following data is sane. */ | |
427 | void *data[0]; | |
428 | }; | |
429 | ||
430 | struct ipc_rcu_sched | |
431 | { | |
432 | struct work_struct work; | |
433 | /* "void *" makes sure alignment of following data is sane. */ | |
434 | void *data[0]; | |
435 | }; | |
436 | ||
437 | #define HDRLEN_KMALLOC (sizeof(struct ipc_rcu_grace) > sizeof(struct ipc_rcu_hdr) ? \ | |
438 | sizeof(struct ipc_rcu_grace) : sizeof(struct ipc_rcu_hdr)) | |
439 | #define HDRLEN_VMALLOC (sizeof(struct ipc_rcu_sched) > HDRLEN_KMALLOC ? \ | |
440 | sizeof(struct ipc_rcu_sched) : HDRLEN_KMALLOC) | |
441 | ||
442 | static inline int rcu_use_vmalloc(int size) | |
443 | { | |
444 | /* Too big for a single page? */ | |
445 | if (HDRLEN_KMALLOC + size > PAGE_SIZE) | |
446 | return 1; | |
447 | return 0; | |
448 | } | |
449 | ||
450 | /** | |
451 | * ipc_rcu_alloc - allocate ipc and rcu space | |
452 | * @size: size desired | |
453 | * | |
454 | * Allocate memory for the rcu header structure + the object. | |
455 | * Returns the pointer to the object. | |
456 | * NULL is returned if the allocation fails. | |
457 | */ | |
458 | ||
459 | void* ipc_rcu_alloc(int size) | |
460 | { | |
461 | void* out; | |
462 | /* | |
463 | * We prepend the allocation with the rcu struct, and | |
464 | * workqueue if necessary (for vmalloc). | |
465 | */ | |
466 | if (rcu_use_vmalloc(size)) { | |
467 | out = vmalloc(HDRLEN_VMALLOC + size); | |
468 | if (out) { | |
469 | out += HDRLEN_VMALLOC; | |
470 | container_of(out, struct ipc_rcu_hdr, data)->is_vmalloc = 1; | |
471 | container_of(out, struct ipc_rcu_hdr, data)->refcount = 1; | |
472 | } | |
473 | } else { | |
474 | out = kmalloc(HDRLEN_KMALLOC + size, GFP_KERNEL); | |
475 | if (out) { | |
476 | out += HDRLEN_KMALLOC; | |
477 | container_of(out, struct ipc_rcu_hdr, data)->is_vmalloc = 0; | |
478 | container_of(out, struct ipc_rcu_hdr, data)->refcount = 1; | |
479 | } | |
480 | } | |
481 | ||
482 | return out; | |
483 | } | |
484 | ||
485 | void ipc_rcu_getref(void *ptr) | |
486 | { | |
487 | container_of(ptr, struct ipc_rcu_hdr, data)->refcount++; | |
488 | } | |
489 | ||
65f27f38 DH |
490 | static void ipc_do_vfree(struct work_struct *work) |
491 | { | |
492 | vfree(container_of(work, struct ipc_rcu_sched, work)); | |
493 | } | |
494 | ||
1da177e4 | 495 | /** |
1e5d5331 RD |
496 | * ipc_schedule_free - free ipc + rcu space |
497 | * @head: RCU callback structure for queued work | |
1da177e4 LT |
498 | * |
499 | * Since RCU callback function is called in bh, | |
72fd4a35 | 500 | * we need to defer the vfree to schedule_work(). |
1da177e4 LT |
501 | */ |
502 | static void ipc_schedule_free(struct rcu_head *head) | |
503 | { | |
504 | struct ipc_rcu_grace *grace = | |
505 | container_of(head, struct ipc_rcu_grace, rcu); | |
506 | struct ipc_rcu_sched *sched = | |
507 | container_of(&(grace->data[0]), struct ipc_rcu_sched, data[0]); | |
508 | ||
65f27f38 | 509 | INIT_WORK(&sched->work, ipc_do_vfree); |
1da177e4 LT |
510 | schedule_work(&sched->work); |
511 | } | |
512 | ||
513 | /** | |
1e5d5331 RD |
514 | * ipc_immediate_free - free ipc + rcu space |
515 | * @head: RCU callback structure that contains pointer to be freed | |
1da177e4 | 516 | * |
72fd4a35 | 517 | * Free from the RCU callback context. |
1da177e4 LT |
518 | */ |
519 | static void ipc_immediate_free(struct rcu_head *head) | |
520 | { | |
521 | struct ipc_rcu_grace *free = | |
522 | container_of(head, struct ipc_rcu_grace, rcu); | |
523 | kfree(free); | |
524 | } | |
525 | ||
526 | void ipc_rcu_putref(void *ptr) | |
527 | { | |
528 | if (--container_of(ptr, struct ipc_rcu_hdr, data)->refcount > 0) | |
529 | return; | |
530 | ||
531 | if (container_of(ptr, struct ipc_rcu_hdr, data)->is_vmalloc) { | |
532 | call_rcu(&container_of(ptr, struct ipc_rcu_grace, data)->rcu, | |
533 | ipc_schedule_free); | |
534 | } else { | |
535 | call_rcu(&container_of(ptr, struct ipc_rcu_grace, data)->rcu, | |
536 | ipc_immediate_free); | |
537 | } | |
538 | } | |
539 | ||
540 | /** | |
541 | * ipcperms - check IPC permissions | |
542 | * @ipcp: IPC permission set | |
543 | * @flag: desired permission set. | |
544 | * | |
545 | * Check user, group, other permissions for access | |
546 | * to ipc resources. return 0 if allowed | |
547 | */ | |
548 | ||
549 | int ipcperms (struct kern_ipc_perm *ipcp, short flag) | |
550 | { /* flag will most probably be 0 or S_...UGO from <linux/stat.h> */ | |
073115d6 | 551 | int requested_mode, granted_mode, err; |
1da177e4 | 552 | |
073115d6 SG |
553 | if (unlikely((err = audit_ipc_obj(ipcp)))) |
554 | return err; | |
1da177e4 LT |
555 | requested_mode = (flag >> 6) | (flag >> 3) | flag; |
556 | granted_mode = ipcp->mode; | |
557 | if (current->euid == ipcp->cuid || current->euid == ipcp->uid) | |
558 | granted_mode >>= 6; | |
559 | else if (in_group_p(ipcp->cgid) || in_group_p(ipcp->gid)) | |
560 | granted_mode >>= 3; | |
561 | /* is there some bit set in requested_mode but not in granted_mode? */ | |
562 | if ((requested_mode & ~granted_mode & 0007) && | |
563 | !capable(CAP_IPC_OWNER)) | |
564 | return -1; | |
565 | ||
566 | return security_ipc_permission(ipcp, flag); | |
567 | } | |
568 | ||
569 | /* | |
570 | * Functions to convert between the kern_ipc_perm structure and the | |
571 | * old/new ipc_perm structures | |
572 | */ | |
573 | ||
574 | /** | |
575 | * kernel_to_ipc64_perm - convert kernel ipc permissions to user | |
576 | * @in: kernel permissions | |
577 | * @out: new style IPC permissions | |
578 | * | |
72fd4a35 RD |
579 | * Turn the kernel object @in into a set of permissions descriptions |
580 | * for returning to userspace (@out). | |
1da177e4 LT |
581 | */ |
582 | ||
583 | ||
584 | void kernel_to_ipc64_perm (struct kern_ipc_perm *in, struct ipc64_perm *out) | |
585 | { | |
586 | out->key = in->key; | |
587 | out->uid = in->uid; | |
588 | out->gid = in->gid; | |
589 | out->cuid = in->cuid; | |
590 | out->cgid = in->cgid; | |
591 | out->mode = in->mode; | |
592 | out->seq = in->seq; | |
593 | } | |
594 | ||
595 | /** | |
596 | * ipc64_perm_to_ipc_perm - convert old ipc permissions to new | |
597 | * @in: new style IPC permissions | |
598 | * @out: old style IPC permissions | |
599 | * | |
72fd4a35 RD |
600 | * Turn the new style permissions object @in into a compatibility |
601 | * object and store it into the @out pointer. | |
1da177e4 LT |
602 | */ |
603 | ||
604 | void ipc64_perm_to_ipc_perm (struct ipc64_perm *in, struct ipc_perm *out) | |
605 | { | |
606 | out->key = in->key; | |
607 | SET_UID(out->uid, in->uid); | |
608 | SET_GID(out->gid, in->gid); | |
609 | SET_UID(out->cuid, in->cuid); | |
610 | SET_GID(out->cgid, in->cgid); | |
611 | out->mode = in->mode; | |
612 | out->seq = in->seq; | |
613 | } | |
614 | ||
615 | /* | |
616 | * So far only shm_get_stat() calls ipc_get() via shm_get(), so ipc_get() | |
5f921ae9 IM |
617 | * is called with shm_ids.mutex locked. Since grow_ary() is also called with |
618 | * shm_ids.mutex down(for Shared Memory), there is no need to add read | |
1da177e4 LT |
619 | * barriers here to gurantee the writes in grow_ary() are seen in order |
620 | * here (for Alpha). | |
621 | * | |
5f921ae9 IM |
622 | * However ipc_get() itself does not necessary require ipc_ids.mutex down. So |
623 | * if in the future ipc_get() is used by other places without ipc_ids.mutex | |
1da177e4 LT |
624 | * down, then ipc_get() needs read memery barriers as ipc_lock() does. |
625 | */ | |
626 | struct kern_ipc_perm* ipc_get(struct ipc_ids* ids, int id) | |
627 | { | |
628 | struct kern_ipc_perm* out; | |
629 | int lid = id % SEQ_MULTIPLIER; | |
630 | if(lid >= ids->entries->size) | |
631 | return NULL; | |
632 | out = ids->entries->p[lid]; | |
633 | return out; | |
634 | } | |
635 | ||
636 | struct kern_ipc_perm* ipc_lock(struct ipc_ids* ids, int id) | |
637 | { | |
638 | struct kern_ipc_perm* out; | |
639 | int lid = id % SEQ_MULTIPLIER; | |
640 | struct ipc_id_ary* entries; | |
641 | ||
642 | rcu_read_lock(); | |
643 | entries = rcu_dereference(ids->entries); | |
644 | if(lid >= entries->size) { | |
645 | rcu_read_unlock(); | |
646 | return NULL; | |
647 | } | |
648 | out = entries->p[lid]; | |
649 | if(out == NULL) { | |
650 | rcu_read_unlock(); | |
651 | return NULL; | |
652 | } | |
653 | spin_lock(&out->lock); | |
654 | ||
655 | /* ipc_rmid() may have already freed the ID while ipc_lock | |
656 | * was spinning: here verify that the structure is still valid | |
657 | */ | |
658 | if (out->deleted) { | |
659 | spin_unlock(&out->lock); | |
660 | rcu_read_unlock(); | |
661 | return NULL; | |
662 | } | |
663 | return out; | |
664 | } | |
665 | ||
666 | void ipc_lock_by_ptr(struct kern_ipc_perm *perm) | |
667 | { | |
668 | rcu_read_lock(); | |
669 | spin_lock(&perm->lock); | |
670 | } | |
671 | ||
672 | void ipc_unlock(struct kern_ipc_perm* perm) | |
673 | { | |
674 | spin_unlock(&perm->lock); | |
675 | rcu_read_unlock(); | |
676 | } | |
677 | ||
678 | int ipc_buildid(struct ipc_ids* ids, int id, int seq) | |
679 | { | |
680 | return SEQ_MULTIPLIER*seq + id; | |
681 | } | |
682 | ||
683 | int ipc_checkid(struct ipc_ids* ids, struct kern_ipc_perm* ipcp, int uid) | |
684 | { | |
685 | if(uid/SEQ_MULTIPLIER != ipcp->seq) | |
686 | return 1; | |
687 | return 0; | |
688 | } | |
689 | ||
690 | #ifdef __ARCH_WANT_IPC_PARSE_VERSION | |
691 | ||
692 | ||
693 | /** | |
694 | * ipc_parse_version - IPC call version | |
695 | * @cmd: pointer to command | |
696 | * | |
697 | * Return IPC_64 for new style IPC and IPC_OLD for old style IPC. | |
72fd4a35 | 698 | * The @cmd value is turned from an encoding command and version into |
1da177e4 LT |
699 | * just the command code. |
700 | */ | |
701 | ||
702 | int ipc_parse_version (int *cmd) | |
703 | { | |
704 | if (*cmd & IPC_64) { | |
705 | *cmd ^= IPC_64; | |
706 | return IPC_64; | |
707 | } else { | |
708 | return IPC_OLD; | |
709 | } | |
710 | } | |
711 | ||
712 | #endif /* __ARCH_WANT_IPC_PARSE_VERSION */ | |
ae781774 MW |
713 | |
714 | #ifdef CONFIG_PROC_FS | |
bc1fc6d8 EB |
715 | struct ipc_proc_iter { |
716 | struct ipc_namespace *ns; | |
717 | struct ipc_proc_iface *iface; | |
718 | }; | |
719 | ||
ae781774 MW |
720 | static void *sysvipc_proc_next(struct seq_file *s, void *it, loff_t *pos) |
721 | { | |
bc1fc6d8 EB |
722 | struct ipc_proc_iter *iter = s->private; |
723 | struct ipc_proc_iface *iface = iter->iface; | |
ae781774 MW |
724 | struct kern_ipc_perm *ipc = it; |
725 | loff_t p; | |
73ea4130 KK |
726 | struct ipc_ids *ids; |
727 | ||
bc1fc6d8 | 728 | ids = iter->ns->ids[iface->ids]; |
ae781774 MW |
729 | |
730 | /* If we had an ipc id locked before, unlock it */ | |
731 | if (ipc && ipc != SEQ_START_TOKEN) | |
732 | ipc_unlock(ipc); | |
733 | ||
734 | /* | |
735 | * p = *pos - 1 (because id 0 starts at position 1) | |
736 | * + 1 (because we increment the position by one) | |
737 | */ | |
73ea4130 KK |
738 | for (p = *pos; p <= ids->max_id; p++) { |
739 | if ((ipc = ipc_lock(ids, p)) != NULL) { | |
ae781774 MW |
740 | *pos = p + 1; |
741 | return ipc; | |
742 | } | |
743 | } | |
744 | ||
745 | /* Out of range - return NULL to terminate iteration */ | |
746 | return NULL; | |
747 | } | |
748 | ||
749 | /* | |
750 | * File positions: pos 0 -> header, pos n -> ipc id + 1. | |
751 | * SeqFile iterator: iterator value locked shp or SEQ_TOKEN_START. | |
752 | */ | |
753 | static void *sysvipc_proc_start(struct seq_file *s, loff_t *pos) | |
754 | { | |
bc1fc6d8 EB |
755 | struct ipc_proc_iter *iter = s->private; |
756 | struct ipc_proc_iface *iface = iter->iface; | |
ae781774 MW |
757 | struct kern_ipc_perm *ipc; |
758 | loff_t p; | |
73ea4130 KK |
759 | struct ipc_ids *ids; |
760 | ||
bc1fc6d8 | 761 | ids = iter->ns->ids[iface->ids]; |
ae781774 MW |
762 | |
763 | /* | |
764 | * Take the lock - this will be released by the corresponding | |
765 | * call to stop(). | |
766 | */ | |
73ea4130 | 767 | mutex_lock(&ids->mutex); |
ae781774 MW |
768 | |
769 | /* pos < 0 is invalid */ | |
770 | if (*pos < 0) | |
771 | return NULL; | |
772 | ||
773 | /* pos == 0 means header */ | |
774 | if (*pos == 0) | |
775 | return SEQ_START_TOKEN; | |
776 | ||
777 | /* Find the (pos-1)th ipc */ | |
73ea4130 KK |
778 | for (p = *pos - 1; p <= ids->max_id; p++) { |
779 | if ((ipc = ipc_lock(ids, p)) != NULL) { | |
ae781774 MW |
780 | *pos = p + 1; |
781 | return ipc; | |
782 | } | |
783 | } | |
784 | return NULL; | |
785 | } | |
786 | ||
787 | static void sysvipc_proc_stop(struct seq_file *s, void *it) | |
788 | { | |
789 | struct kern_ipc_perm *ipc = it; | |
bc1fc6d8 EB |
790 | struct ipc_proc_iter *iter = s->private; |
791 | struct ipc_proc_iface *iface = iter->iface; | |
73ea4130 | 792 | struct ipc_ids *ids; |
ae781774 MW |
793 | |
794 | /* If we had a locked segment, release it */ | |
795 | if (ipc && ipc != SEQ_START_TOKEN) | |
796 | ipc_unlock(ipc); | |
797 | ||
bc1fc6d8 | 798 | ids = iter->ns->ids[iface->ids]; |
ae781774 | 799 | /* Release the lock we took in start() */ |
73ea4130 | 800 | mutex_unlock(&ids->mutex); |
ae781774 MW |
801 | } |
802 | ||
803 | static int sysvipc_proc_show(struct seq_file *s, void *it) | |
804 | { | |
bc1fc6d8 EB |
805 | struct ipc_proc_iter *iter = s->private; |
806 | struct ipc_proc_iface *iface = iter->iface; | |
ae781774 MW |
807 | |
808 | if (it == SEQ_START_TOKEN) | |
809 | return seq_puts(s, iface->header); | |
810 | ||
811 | return iface->show(s, it); | |
812 | } | |
813 | ||
814 | static struct seq_operations sysvipc_proc_seqops = { | |
815 | .start = sysvipc_proc_start, | |
816 | .stop = sysvipc_proc_stop, | |
817 | .next = sysvipc_proc_next, | |
818 | .show = sysvipc_proc_show, | |
819 | }; | |
820 | ||
bc1fc6d8 EB |
821 | static int sysvipc_proc_open(struct inode *inode, struct file *file) |
822 | { | |
ae781774 MW |
823 | int ret; |
824 | struct seq_file *seq; | |
bc1fc6d8 EB |
825 | struct ipc_proc_iter *iter; |
826 | ||
827 | ret = -ENOMEM; | |
828 | iter = kmalloc(sizeof(*iter), GFP_KERNEL); | |
829 | if (!iter) | |
830 | goto out; | |
ae781774 MW |
831 | |
832 | ret = seq_open(file, &sysvipc_proc_seqops); | |
bc1fc6d8 EB |
833 | if (ret) |
834 | goto out_kfree; | |
835 | ||
836 | seq = file->private_data; | |
837 | seq->private = iter; | |
838 | ||
839 | iter->iface = PDE(inode)->data; | |
840 | iter->ns = get_ipc_ns(current->nsproxy->ipc_ns); | |
841 | out: | |
ae781774 | 842 | return ret; |
bc1fc6d8 EB |
843 | out_kfree: |
844 | kfree(iter); | |
845 | goto out; | |
846 | } | |
847 | ||
848 | static int sysvipc_proc_release(struct inode *inode, struct file *file) | |
849 | { | |
850 | struct seq_file *seq = file->private_data; | |
851 | struct ipc_proc_iter *iter = seq->private; | |
852 | put_ipc_ns(iter->ns); | |
853 | return seq_release_private(inode, file); | |
ae781774 MW |
854 | } |
855 | ||
9a32144e | 856 | static const struct file_operations sysvipc_proc_fops = { |
ae781774 MW |
857 | .open = sysvipc_proc_open, |
858 | .read = seq_read, | |
859 | .llseek = seq_lseek, | |
bc1fc6d8 | 860 | .release = sysvipc_proc_release, |
ae781774 MW |
861 | }; |
862 | #endif /* CONFIG_PROC_FS */ |