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