vserver 1.9.5.x5
[linux-2.6.git] / ipc / util.c
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
10  *            Manfred Spraul <manfreds@colorfullife.com>
11  * Oct 2002 - One lock per IPC id. RCU ipc_free for lock-free grow_ary().
12  *            Mingming Cao <cmm@us.ibm.com>
13  */
14
15 #include <linux/config.h>
16 #include <linux/mm.h>
17 #include <linux/shm.h>
18 #include <linux/init.h>
19 #include <linux/msg.h>
20 #include <linux/smp_lock.h>
21 #include <linux/vmalloc.h>
22 #include <linux/slab.h>
23 #include <linux/highuid.h>
24 #include <linux/security.h>
25 #include <linux/rcupdate.h>
26 #include <linux/workqueue.h>
27
28 #include <asm/unistd.h>
29
30 #include "util.h"
31
32 /**
33  *      ipc_init        -       initialise IPC subsystem
34  *
35  *      The various system5 IPC resources (semaphores, messages and shared
36  *      memory are initialised
37  */
38  
39 static int __init ipc_init(void)
40 {
41         sem_init();
42         msg_init();
43         shm_init();
44         return 0;
45 }
46 __initcall(ipc_init);
47
48 /**
49  *      ipc_init_ids            -       initialise IPC identifiers
50  *      @ids: Identifier set
51  *      @size: Number of identifiers
52  *
53  *      Given a size for the ipc identifier range (limited below IPCMNI)
54  *      set up the sequence range to use then allocate and initialise the
55  *      array itself. 
56  */
57  
58 void __init ipc_init_ids(struct ipc_ids* ids, int size)
59 {
60         int i;
61         sema_init(&ids->sem,1);
62
63         if(size > IPCMNI)
64                 size = IPCMNI;
65         ids->in_use = 0;
66         ids->max_id = -1;
67         ids->seq = 0;
68         {
69                 int seq_limit = INT_MAX/SEQ_MULTIPLIER;
70                 if(seq_limit > USHRT_MAX)
71                         ids->seq_max = USHRT_MAX;
72                  else
73                         ids->seq_max = seq_limit;
74         }
75
76         ids->entries = ipc_rcu_alloc(sizeof(struct kern_ipc_perm *)*size +
77                                      sizeof(struct ipc_id_ary));
78
79         if(ids->entries == NULL) {
80                 printk(KERN_ERR "ipc_init_ids() failed, ipc service disabled.\n");
81                 size = 0;
82                 ids->entries = &ids->nullentry;
83         }
84         ids->entries->size = size;
85         for(i=0;i<size;i++)
86                 ids->entries->p[i] = NULL;
87 }
88
89 /**
90  *      ipc_findkey     -       find a key in an ipc identifier set     
91  *      @ids: Identifier set
92  *      @key: The key to find
93  *      
94  *      Requires ipc_ids.sem locked.
95  *      Returns the identifier if found or -1 if not.
96  */
97  
98 int ipc_findkey(struct ipc_ids* ids, key_t key)
99 {
100         int id;
101         struct kern_ipc_perm* p;
102         int max_id = ids->max_id;
103
104         /*
105          * rcu_dereference() is not needed here
106          * since ipc_ids.sem is held
107          */
108         for (id = 0; id <= max_id; id++) {
109                 p = ids->entries->p[id];
110                 if (p==NULL)
111                         continue;
112                 if (!vx_check(p->xid, VX_IDENT))
113                         continue;
114                 if (key == p->key)
115                         return id;
116         }
117         return -1;
118 }
119
120 /*
121  * Requires ipc_ids.sem locked
122  */
123 static int grow_ary(struct ipc_ids* ids, int newsize)
124 {
125         struct ipc_id_ary* new;
126         struct ipc_id_ary* old;
127         int i;
128         int size = ids->entries->size;
129
130         if(newsize > IPCMNI)
131                 newsize = IPCMNI;
132         if(newsize <= size)
133                 return newsize;
134
135         new = ipc_rcu_alloc(sizeof(struct kern_ipc_perm *)*newsize +
136                             sizeof(struct ipc_id_ary));
137         if(new == NULL)
138                 return size;
139         new->size = newsize;
140         memcpy(new->p, ids->entries->p, sizeof(struct kern_ipc_perm *)*size +
141                                         sizeof(struct ipc_id_ary));
142         for(i=size;i<newsize;i++) {
143                 new->p[i] = NULL;
144         }
145         old = ids->entries;
146
147         /*
148          * Use rcu_assign_pointer() to make sure the memcpyed contents
149          * of the new array are visible before the new array becomes visible.
150          */
151         rcu_assign_pointer(ids->entries, new);
152
153         ipc_rcu_putref(old);
154         return newsize;
155 }
156
157 /**
158  *      ipc_addid       -       add an IPC identifier
159  *      @ids: IPC identifier set
160  *      @new: new IPC permission set
161  *      @size: new size limit for the id array
162  *
163  *      Add an entry 'new' to the IPC arrays. The permissions object is
164  *      initialised and the first free entry is set up and the id assigned
165  *      is returned. The list is returned in a locked state on success.
166  *      On failure the list is not locked and -1 is returned.
167  *
168  *      Called with ipc_ids.sem held.
169  */
170  
171 int ipc_addid(struct ipc_ids* ids, struct kern_ipc_perm* new, int size)
172 {
173         int id;
174
175         size = grow_ary(ids,size);
176
177         /*
178          * rcu_dereference()() is not needed here since
179          * ipc_ids.sem is held
180          */
181         for (id = 0; id < size; id++) {
182                 if(ids->entries->p[id] == NULL)
183                         goto found;
184         }
185         return -1;
186 found:
187         ids->in_use++;
188         if (id > ids->max_id)
189                 ids->max_id = id;
190
191         new->cuid = new->uid = current->euid;
192         new->gid = new->cgid = current->egid;
193
194         new->seq = ids->seq++;
195         if(ids->seq > ids->seq_max)
196                 ids->seq = 0;
197
198         spin_lock_init(&new->lock);
199         new->deleted = 0;
200         rcu_read_lock();
201         spin_lock(&new->lock);
202         ids->entries->p[id] = new;
203         return id;
204 }
205
206 /**
207  *      ipc_rmid        -       remove an IPC identifier
208  *      @ids: identifier set
209  *      @id: Identifier to remove
210  *
211  *      The identifier must be valid, and in use. The kernel will panic if
212  *      fed an invalid identifier. The entry is removed and internal
213  *      variables recomputed. The object associated with the identifier
214  *      is returned.
215  *      ipc_ids.sem and the spinlock for this ID is hold before this function
216  *      is called, and remain locked on the exit.
217  */
218  
219 struct kern_ipc_perm* ipc_rmid(struct ipc_ids* ids, int id)
220 {
221         struct kern_ipc_perm* p;
222         int lid = id % SEQ_MULTIPLIER;
223         if(lid >= ids->entries->size)
224                 BUG();
225
226         /* 
227          * do not need a rcu_dereference()() here to force ordering
228          * on Alpha, since the ipc_ids.sem is held.
229          */     
230         p = ids->entries->p[lid];
231         ids->entries->p[lid] = NULL;
232         if(p==NULL)
233                 BUG();
234         ids->in_use--;
235
236         if (lid == ids->max_id) {
237                 do {
238                         lid--;
239                         if(lid == -1)
240                                 break;
241                 } while (ids->entries->p[lid] == NULL);
242                 ids->max_id = lid;
243         }
244         p->deleted = 1;
245         return p;
246 }
247
248 /**
249  *      ipc_alloc       -       allocate ipc space
250  *      @size: size desired
251  *
252  *      Allocate memory from the appropriate pools and return a pointer to it.
253  *      NULL is returned if the allocation fails
254  */
255  
256 void* ipc_alloc(int size)
257 {
258         void* out;
259         if(size > PAGE_SIZE)
260                 out = vmalloc(size);
261         else
262                 out = kmalloc(size, GFP_KERNEL);
263         return out;
264 }
265
266 /**
267  *      ipc_free        -       free ipc space
268  *      @ptr: pointer returned by ipc_alloc
269  *      @size: size of block
270  *
271  *      Free a block created with ipc_alloc. The caller must know the size
272  *      used in the allocation call.
273  */
274
275 void ipc_free(void* ptr, int size)
276 {
277         if(size > PAGE_SIZE)
278                 vfree(ptr);
279         else
280                 kfree(ptr);
281 }
282
283 /*
284  * rcu allocations:
285  * There are three headers that are prepended to the actual allocation:
286  * - during use: ipc_rcu_hdr.
287  * - during the rcu grace period: ipc_rcu_grace.
288  * - [only if vmalloc]: ipc_rcu_sched.
289  * Their lifetime doesn't overlap, thus the headers share the same memory.
290  * Unlike a normal union, they are right-aligned, thus some container_of
291  * forward/backward casting is necessary:
292  */
293 struct ipc_rcu_hdr
294 {
295         int refcount;
296         int is_vmalloc;
297         void *data[0];
298 };
299
300
301 struct ipc_rcu_grace
302 {
303         struct rcu_head rcu;
304         /* "void *" makes sure alignment of following data is sane. */
305         void *data[0];
306 };
307
308 struct ipc_rcu_sched
309 {
310         struct work_struct work;
311         /* "void *" makes sure alignment of following data is sane. */
312         void *data[0];
313 };
314
315 #define HDRLEN_KMALLOC          (sizeof(struct ipc_rcu_grace) > sizeof(struct ipc_rcu_hdr) ? \
316                                         sizeof(struct ipc_rcu_grace) : sizeof(struct ipc_rcu_hdr))
317 #define HDRLEN_VMALLOC          (sizeof(struct ipc_rcu_sched) > HDRLEN_KMALLOC ? \
318                                         sizeof(struct ipc_rcu_sched) : HDRLEN_KMALLOC)
319
320 static inline int rcu_use_vmalloc(int size)
321 {
322         /* Too big for a single page? */
323         if (HDRLEN_KMALLOC + size > PAGE_SIZE)
324                 return 1;
325         return 0;
326 }
327
328 /**
329  *      ipc_rcu_alloc   -       allocate ipc and rcu space 
330  *      @size: size desired
331  *
332  *      Allocate memory for the rcu header structure +  the object.
333  *      Returns the pointer to the object.
334  *      NULL is returned if the allocation fails. 
335  */
336  
337 void* ipc_rcu_alloc(int size)
338 {
339         void* out;
340         /* 
341          * We prepend the allocation with the rcu struct, and
342          * workqueue if necessary (for vmalloc). 
343          */
344         if (rcu_use_vmalloc(size)) {
345                 out = vmalloc(HDRLEN_VMALLOC + size);
346                 if (out) {
347                         out += HDRLEN_VMALLOC;
348                         container_of(out, struct ipc_rcu_hdr, data)->is_vmalloc = 1;
349                         container_of(out, struct ipc_rcu_hdr, data)->refcount = 1;
350                 }
351         } else {
352                 out = kmalloc(HDRLEN_KMALLOC + size, GFP_KERNEL);
353                 if (out) {
354                         out += HDRLEN_KMALLOC;
355                         container_of(out, struct ipc_rcu_hdr, data)->is_vmalloc = 0;
356                         container_of(out, struct ipc_rcu_hdr, data)->refcount = 1;
357                 }
358         }
359
360         return out;
361 }
362
363 void ipc_rcu_getref(void *ptr)
364 {
365         container_of(ptr, struct ipc_rcu_hdr, data)->refcount++;
366 }
367
368 /**
369  *      ipc_schedule_free       - free ipc + rcu space
370  * 
371  * Since RCU callback function is called in bh,
372  * we need to defer the vfree to schedule_work
373  */
374 static void ipc_schedule_free(struct rcu_head *head)
375 {
376         struct ipc_rcu_grace *grace =
377                 container_of(head, struct ipc_rcu_grace, rcu);
378         struct ipc_rcu_sched *sched =
379                         container_of(&(grace->data[0]), struct ipc_rcu_sched, data[0]);
380
381         INIT_WORK(&sched->work, vfree, sched);
382         schedule_work(&sched->work);
383 }
384
385 /**
386  *      ipc_immediate_free      - free ipc + rcu space
387  *
388  *      Free from the RCU callback context
389  *
390  */
391 static void ipc_immediate_free(struct rcu_head *head)
392 {
393         struct ipc_rcu_grace *free =
394                 container_of(head, struct ipc_rcu_grace, rcu);
395         kfree(free);
396 }
397
398 void ipc_rcu_putref(void *ptr)
399 {
400         if (--container_of(ptr, struct ipc_rcu_hdr, data)->refcount > 0)
401                 return;
402
403         if (container_of(ptr, struct ipc_rcu_hdr, data)->is_vmalloc) {
404                 call_rcu(&container_of(ptr, struct ipc_rcu_grace, data)->rcu,
405                                 ipc_schedule_free);
406         } else {
407                 call_rcu(&container_of(ptr, struct ipc_rcu_grace, data)->rcu,
408                                 ipc_immediate_free);
409         }
410 }
411
412 /**
413  *      ipcperms        -       check IPC permissions
414  *      @ipcp: IPC permission set
415  *      @flag: desired permission set.
416  *
417  *      Check user, group, other permissions for access
418  *      to ipc resources. return 0 if allowed
419  */
420  
421 int ipcperms (struct kern_ipc_perm *ipcp, short flag)
422 {       /* flag will most probably be 0 or S_...UGO from <linux/stat.h> */
423         int requested_mode, granted_mode;
424
425         if (!vx_check(ipcp->xid, VX_ADMIN|VX_IDENT)) /* maybe just VX_IDENT? */
426                 return -1;
427         requested_mode = (flag >> 6) | (flag >> 3) | flag;
428         granted_mode = ipcp->mode;
429         if (current->euid == ipcp->cuid || current->euid == ipcp->uid)
430                 granted_mode >>= 6;
431         else if (in_group_p(ipcp->cgid) || in_group_p(ipcp->gid))
432                 granted_mode >>= 3;
433         /* is there some bit set in requested_mode but not in granted_mode? */
434         if ((requested_mode & ~granted_mode & 0007) && 
435             !capable(CAP_IPC_OWNER))
436                 return -1;
437
438         return security_ipc_permission(ipcp, flag);
439 }
440
441 /*
442  * Functions to convert between the kern_ipc_perm structure and the
443  * old/new ipc_perm structures
444  */
445
446 /**
447  *      kernel_to_ipc64_perm    -       convert kernel ipc permissions to user
448  *      @in: kernel permissions
449  *      @out: new style IPC permissions
450  *
451  *      Turn the kernel object 'in' into a set of permissions descriptions
452  *      for returning to userspace (out).
453  */
454  
455
456 void kernel_to_ipc64_perm (struct kern_ipc_perm *in, struct ipc64_perm *out)
457 {
458         out->key        = in->key;
459         out->uid        = in->uid;
460         out->gid        = in->gid;
461         out->cuid       = in->cuid;
462         out->cgid       = in->cgid;
463         out->mode       = in->mode;
464         out->seq        = in->seq;
465 }
466
467 /**
468  *      ipc64_perm_to_ipc_perm  -       convert old ipc permissions to new
469  *      @in: new style IPC permissions
470  *      @out: old style IPC permissions
471  *
472  *      Turn the new style permissions object in into a compatibility
473  *      object and store it into the 'out' pointer.
474  */
475  
476 void ipc64_perm_to_ipc_perm (struct ipc64_perm *in, struct ipc_perm *out)
477 {
478         out->key        = in->key;
479         SET_UID(out->uid, in->uid);
480         SET_GID(out->gid, in->gid);
481         SET_UID(out->cuid, in->cuid);
482         SET_GID(out->cgid, in->cgid);
483         out->mode       = in->mode;
484         out->seq        = in->seq;
485 }
486
487 /*
488  * So far only shm_get_stat() calls ipc_get() via shm_get(), so ipc_get()
489  * is called with shm_ids.sem locked.  Since grow_ary() is also called with
490  * shm_ids.sem down(for Shared Memory), there is no need to add read 
491  * barriers here to gurantee the writes in grow_ary() are seen in order 
492  * here (for Alpha).
493  *
494  * However ipc_get() itself does not necessary require ipc_ids.sem down. So
495  * if in the future ipc_get() is used by other places without ipc_ids.sem
496  * down, then ipc_get() needs read memery barriers as ipc_lock() does.
497  */
498 struct kern_ipc_perm* ipc_get(struct ipc_ids* ids, int id)
499 {
500         struct kern_ipc_perm* out;
501         int lid = id % SEQ_MULTIPLIER;
502         if(lid >= ids->entries->size)
503                 return NULL;
504         out = ids->entries->p[lid];
505         return out;
506 }
507
508 struct kern_ipc_perm* ipc_lock(struct ipc_ids* ids, int id)
509 {
510         struct kern_ipc_perm* out;
511         int lid = id % SEQ_MULTIPLIER;
512         struct ipc_id_ary* entries;
513
514         rcu_read_lock();
515         entries = rcu_dereference(ids->entries);
516         if(lid >= entries->size) {
517                 rcu_read_unlock();
518                 return NULL;
519         }
520         out = entries->p[lid];
521         if(out == NULL) {
522                 rcu_read_unlock();
523                 return NULL;
524         }
525         spin_lock(&out->lock);
526         
527         /* ipc_rmid() may have already freed the ID while ipc_lock
528          * was spinning: here verify that the structure is still valid
529          */
530         if (out->deleted) {
531                 spin_unlock(&out->lock);
532                 rcu_read_unlock();
533                 return NULL;
534         }
535         return out;
536 }
537
538 void ipc_lock_by_ptr(struct kern_ipc_perm *perm)
539 {
540         rcu_read_lock();
541         spin_lock(&perm->lock);
542 }
543
544 void ipc_unlock(struct kern_ipc_perm* perm)
545 {
546         spin_unlock(&perm->lock);
547         rcu_read_unlock();
548 }
549
550 int ipc_buildid(struct ipc_ids* ids, int id, int seq)
551 {
552         return SEQ_MULTIPLIER*seq + id;
553 }
554
555 int ipc_checkid(struct ipc_ids* ids, struct kern_ipc_perm* ipcp, int uid)
556 {
557         if(uid/SEQ_MULTIPLIER != ipcp->seq)
558                 return 1;
559         return 0;
560 }
561
562 #ifdef __ARCH_WANT_IPC_PARSE_VERSION
563
564
565 /**
566  *      ipc_parse_version       -       IPC call version
567  *      @cmd: pointer to command
568  *
569  *      Return IPC_64 for new style IPC and IPC_OLD for old style IPC. 
570  *      The cmd value is turned from an encoding command and version into
571  *      just the command code.
572  */
573  
574 int ipc_parse_version (int *cmd)
575 {
576         if (*cmd & IPC_64) {
577                 *cmd ^= IPC_64;
578                 return IPC_64;
579         } else {
580                 return IPC_OLD;
581         }
582 }
583
584 #endif /* __ARCH_WANT_IPC_PARSE_VERSION */