libafs: Drop xvcache for AllocCBR
[openafs.git] / src / afs / LINUX / osi_groups.c
1 /*
2  * Copyright 2000, International Business Machines Corporation and others.
3  * All Rights Reserved.
4  * 
5  * This software has been released under the terms of the IBM Public
6  * License.  For details, see the LICENSE file in the top-level source
7  * directory or online at http://www.openafs.org/dl/license10.html
8  */
9
10 /*
11  * Implements:
12  * setgroups (syscall)
13  * setpag
14  *
15  */
16 #include <afsconfig.h>
17 #include "afs/param.h"
18 #ifdef LINUX_KEYRING_SUPPORT
19 #include <linux/seq_file.h>
20 #endif
21
22
23 #include "afs/sysincludes.h"
24 #include "afsincludes.h"
25 #include "afs/afs_stats.h"      /* statistics */
26 #include "afs/nfsclient.h"
27 #include "osi_compat.h"
28
29 #ifdef AFS_LINUX26_ONEGROUP_ENV
30 # define NUMPAGGROUPS 1
31
32 static afs_uint32
33 afs_linux_pag_from_groups(struct group_info *group_info) {
34     afs_uint32 g0 = 0;
35     afs_uint32 i;
36
37     if (group_info->ngroups < NUMPAGGROUPS)
38         return NOPAG;
39
40     for (i = 0; (i < group_info->ngroups &&
41                  (g0 = GROUP_AT(group_info, i)) != (gid_t) NOGROUP); i++) {
42         if (((g0 >> 24) & 0xff) == 'A')
43             return g0;
44     }
45     return NOPAG;
46 }
47
48 static inline void
49 afs_linux_pag_to_groups(afs_uint32 newpag,
50                         struct group_info *old, struct group_info **new) {
51     int need_space = 0;
52     int i;
53     int j;
54
55     if (afs_linux_pag_from_groups(old) == NOPAG)
56         need_space = NUMPAGGROUPS;
57
58     *new = groups_alloc(old->ngroups + need_space);
59
60     for (i = 0, j = 0; i < old->ngroups; ++i) {
61         int ths = GROUP_AT(old, i);
62         int last = i > 0 ? GROUP_AT(old, i-1) : 0;
63         if ((ths >> 24) == 'A')
64             continue;
65         if (last <= newpag && ths > newpag) {
66            GROUP_AT(*new, j) = newpag;
67            j++;
68         }
69         GROUP_AT(*new, j) = ths;
70         j++;
71     }
72     if (j != i + need_space)
73         GROUP_AT(*new, j) = newpag;
74 }
75
76 #else
77 # define NUMPAGGROUPS 2
78
79 static inline afs_uint32
80 afs_linux_pag_from_groups(struct group_info *group_info) {
81
82     if (group_info->ngroups < NUMPAGGROUPS)
83         return NOPAG;
84
85     return afs_get_pag_from_groups(GROUP_AT(group_info, 0), GROUP_AT(group_info, 1));
86 }
87
88 static inline void
89 afs_linux_pag_to_groups(afs_uint32 newpag,
90                         struct group_info *old, struct group_info *new) {
91     int need_space = 0;
92     int i;
93     gid_t g0;
94     gid_t g1;
95
96     if (afs_linux_pag_from_groups(old) == NOPAG)
97         need_space = NUMPAGGGROUPS;
98
99     *new = groups_alloc(old->ngroups + need_space);
100
101     for (i = 0; i < old->ngroups; ++i)
102           GROUP_AT(new, i + need_space) = GROUP_AT(old, i);
103
104     afs_get_groups_from_pag(newpag, &g0, g1);
105     GROUP_AT(new, 0) = g0;
106     GROUP_AT(new, 1) = g1;
107 }
108 #endif
109
110 afs_int32
111 osi_get_group_pag(afs_ucred_t *cred) {
112     return afs_linux_pag_from_groups(afs_cr_group_info(cred));
113 }
114
115
116 static int
117 afs_setgroups(cred_t **cr, struct group_info *group_info, int change_parent)
118 {
119     struct group_info *old_info;
120
121     AFS_STATCNT(afs_setgroups);
122
123     old_info = afs_cr_group_info(*cr);
124     get_group_info(group_info);
125     afs_set_cr_group_info(*cr, group_info);
126     put_group_info(old_info);
127
128     crset(*cr);
129
130 #if defined(STRUCT_TASK_STRUCT_HAS_PARENT) && !defined(STRUCT_TASK_STRUCT_HAS_CRED)
131     if (change_parent) {
132         old_info = current->parent->group_info;
133         get_group_info(group_info);
134         current->parent->group_info = group_info;
135         put_group_info(old_info);
136     }
137 #endif
138
139     return (0);
140 }
141
142 int
143 __setpag(cred_t **cr, afs_uint32 pagvalue, afs_uint32 *newpag,
144          int change_parent)
145 {
146     struct group_info *group_info;
147     struct group_info *tmp;
148
149     get_group_info(afs_cr_group_info(*cr));
150     group_info = afs_cr_group_info(*cr);
151
152     *newpag = (pagvalue == -1 ? genpag() : pagvalue);
153     afs_linux_pag_to_groups(*newpag, group_info, &tmp);
154
155     put_group_info(group_info);
156     group_info = tmp;
157
158     afs_setgroups(cr, group_info, change_parent);
159
160     put_group_info(group_info);
161
162     return 0;
163 }
164
165 #ifdef LINUX_KEYRING_SUPPORT
166 extern struct key_type key_type_keyring __attribute__((weak));
167 static struct key_type *__key_type_keyring = &key_type_keyring;
168
169 /* install_session_keyring returns negative error values */
170 static int
171 install_session_keyring(struct key *keyring)
172 {
173     struct key *old;
174     char desc[20];
175     int code = -EINVAL;
176     unsigned long flags;
177
178     if (!__key_type_keyring)
179         return code;
180
181     if (!keyring) {
182
183         /* create an empty session keyring */
184         sprintf(desc, "_ses.%u", current->tgid);
185
186         /* if we're root, don't count the keyring against our quota. This
187          * avoids starvation issues when dealing with PAM modules that always
188          * setpag() as root */
189         if (current_uid() == 0)
190             flags = KEY_ALLOC_NOT_IN_QUOTA;
191         else
192             flags = KEY_ALLOC_IN_QUOTA;
193
194         keyring = afs_linux_key_alloc(
195                             __key_type_keyring, desc,
196                             current_uid(), current_gid(),
197                             (KEY_POS_ALL & ~KEY_POS_SETATTR) | KEY_USR_ALL,
198                             flags);
199
200         if (IS_ERR(keyring)) {
201             code = PTR_ERR(keyring);
202             goto out;
203         }
204     }
205
206     code = key_instantiate_and_link(keyring, NULL, 0, NULL, NULL);
207     if (code < 0) {
208         key_put(keyring);
209         goto out;
210     }
211
212     /* install the keyring */
213     spin_lock_irq(&current->sighand->siglock);
214     old = task_session_keyring(current);
215     smp_wmb();
216     task_session_keyring(current) = keyring;
217     spin_unlock_irq(&current->sighand->siglock);
218
219     if (old)
220             key_put(old);
221
222 out:
223     return code;
224 }
225 #endif /* LINUX_KEYRING_SUPPORT */
226
227 /* Error codes from setpag must be positive, otherwise they don't
228  * make it back into userspace properly. Error codes from the
229  * Linux keyring utilities, and from install_session_keyring()
230  * are negative. So we need to be careful to convert them correctly
231  * here
232  */
233 int
234 setpag(cred_t **cr, afs_uint32 pagvalue, afs_uint32 *newpag,
235        int change_parent)
236 {
237     int code;
238
239     AFS_STATCNT(setpag);
240
241     code = __setpag(cr, pagvalue, newpag, change_parent);
242
243 #ifdef LINUX_KEYRING_SUPPORT
244     if (code == 0 && afs_cr_rgid(*cr) != NFSXLATOR_CRED) {
245         code = install_session_keyring(NULL);
246         if (code == 0 && current_session_keyring()) {
247             struct key *key;
248             key_perm_t perm;
249
250             perm = KEY_POS_VIEW | KEY_POS_SEARCH;
251             perm |= KEY_USR_VIEW | KEY_USR_SEARCH;
252
253             key = afs_linux_key_alloc(&key_type_afs_pag, "_pag", 0, 0, perm, KEY_ALLOC_NOT_IN_QUOTA);
254
255             if (!IS_ERR(key)) {
256                 key_instantiate_and_link(key, (void *) newpag, sizeof(afs_uint32),
257                                          current_session_keyring(), NULL);
258                 key_put(key);
259             } else {
260                 code = PTR_ERR(key);
261             }
262         }
263         if (code)
264             code = -code;
265     }
266 #endif /* LINUX_KEYRING_SUPPORT */
267
268     return code;
269 }
270
271
272 /* Intercept the standard system call. */
273 extern asmlinkage long (*sys_setgroupsp) (int gidsetsize, gid_t * grouplist);
274 asmlinkage long
275 afs_xsetgroups(int gidsetsize, gid_t * grouplist)
276 {
277     long code;
278     cred_t *cr = crref();
279     afs_uint32 junk;
280     int old_pag;
281
282     old_pag = PagInCred(cr);
283     crfree(cr);
284
285     code = (*sys_setgroupsp) (gidsetsize, grouplist);
286     if (code) {
287         return code;
288     }
289
290     cr = crref();
291     if (old_pag != NOPAG && PagInCred(cr) == NOPAG) {
292         /* re-install old pag if there's room. */
293         code = __setpag(&cr, old_pag, &junk, 0);
294     }
295     crfree(cr);
296
297     /* Linux syscall ABI returns errno as negative */
298     return (-code);
299 }
300
301 /* Intercept the standard uid32 system call. */
302 extern asmlinkage int (*sys_setgroups32p) (int gidsetsize,
303                                            __kernel_gid32_t * grouplist);
304 asmlinkage long
305 afs_xsetgroups32(int gidsetsize, gid_t * grouplist)
306 {
307     long code;
308     cred_t *cr = crref();
309     afs_uint32 junk;
310     int old_pag;
311
312     old_pag = PagInCred(cr);
313     crfree(cr);
314
315     code = (*sys_setgroups32p) (gidsetsize, grouplist);
316
317     if (code) {
318         return code;
319     }
320
321     cr = crref();
322     if (old_pag != NOPAG && PagInCred(cr) == NOPAG) {
323         /* re-install old pag if there's room. */
324         code = __setpag(&cr, old_pag, &junk, 0);
325     }
326     crfree(cr);
327
328     /* Linux syscall ABI returns errno as negative */
329     return (-code);
330 }
331
332 #if defined(AFS_PPC64_LINUX20_ENV)
333 /* Intercept the uid16 system call as used by 32bit programs. */
334 extern asmlinkage long (*sys32_setgroupsp)(int gidsetsize, gid_t *grouplist);
335 asmlinkage long afs32_xsetgroups(int gidsetsize, gid_t *grouplist)
336 {
337     long code;
338     cred_t *cr = crref();
339     afs_uint32 junk;
340     int old_pag;
341     
342     old_pag = PagInCred(cr);
343     crfree(cr);
344     
345     code = (*sys32_setgroupsp)(gidsetsize, grouplist);
346     if (code) {
347         return code;
348     }
349     
350     cr = crref();
351     if (old_pag != NOPAG && PagInCred(cr) == NOPAG) {
352         /* re-install old pag if there's room. */
353         code = __setpag(&cr, old_pag, &junk, 0);
354     }
355     crfree(cr);
356     
357     /* Linux syscall ABI returns errno as negative */
358     return (-code);
359 }
360 #endif
361
362 #if defined(AFS_SPARC64_LINUX20_ENV) || defined(AFS_AMD64_LINUX20_ENV)
363 /* Intercept the uid16 system call as used by 32bit programs. */
364 #ifdef AFS_AMD64_LINUX20_ENV
365 extern asmlinkage long (*sys32_setgroupsp) (int gidsetsize, u16 * grouplist);
366 #endif /* AFS_AMD64_LINUX20_ENV */
367 #ifdef AFS_SPARC64_LINUX26_ENV
368 extern asmlinkage int (*sys32_setgroupsp) (int gidsetsize,
369                                            __kernel_gid32_t * grouplist);
370 #endif /* AFS_SPARC64_LINUX26_ENV */
371 asmlinkage long
372 afs32_xsetgroups(int gidsetsize, u16 * grouplist)
373 {
374     long code;
375     cred_t *cr = crref();
376     afs_uint32 junk;
377     int old_pag;
378     
379     old_pag = PagInCred(cr);
380     crfree(cr);
381     
382     code = (*sys32_setgroupsp) (gidsetsize, grouplist);
383     if (code) {
384         return code;
385     }
386     
387     cr = crref();
388     if (old_pag != NOPAG && PagInCred(cr) == NOPAG) {
389         /* re-install old pag if there's room. */
390         code = __setpag(&cr, old_pag, &junk, 0);
391     }
392     crfree(cr);
393     
394     /* Linux syscall ABI returns errno as negative */
395     return (-code);
396 }
397
398 /* Intercept the uid32 system call as used by 32bit programs. */
399 #ifdef AFS_AMD64_LINUX20_ENV
400 extern asmlinkage long (*sys32_setgroups32p) (int gidsetsize, gid_t * grouplist);
401 #endif /* AFS_AMD64_LINUX20_ENV */
402 #ifdef AFS_SPARC64_LINUX26_ENV
403 extern asmlinkage int (*sys32_setgroups32p) (int gidsetsize,
404                                              __kernel_gid32_t * grouplist);
405 #endif /* AFS_SPARC64_LINUX26_ENV */
406 asmlinkage long
407 afs32_xsetgroups32(int gidsetsize, gid_t * grouplist)
408 {
409     long code;
410     cred_t *cr = crref();
411     afs_uint32 junk;
412     int old_pag;
413
414     old_pag = PagInCred(cr);
415     crfree(cr);
416
417     code = (*sys32_setgroups32p) (gidsetsize, grouplist);
418     if (code) {
419         return code;
420     }
421
422     cr = crref();
423     if (old_pag != NOPAG && PagInCred(cr) == NOPAG) {
424         /* re-install old pag if there's room. */
425         code = __setpag(&cr, old_pag, &junk, 0);
426     }
427     crfree(cr);
428
429     /* Linux syscall ABI returns errno as negative */
430     return (-code);
431 }
432 #endif
433
434
435 #ifdef LINUX_KEYRING_SUPPORT
436 static void afs_pag_describe(const struct key *key, struct seq_file *m)
437 {
438     seq_puts(m, key->description);
439
440     seq_printf(m, ": %u", key->datalen);
441 }
442
443 static int afs_pag_instantiate(struct key *key, const void *data, size_t datalen)
444 {
445     int code;
446     afs_uint32 *userpag, pag = NOPAG;
447
448     if (key->uid != 0 || key->gid != 0)
449         return -EPERM;
450
451     code = -EINVAL;
452     get_group_info(current_group_info());
453
454     if (datalen != sizeof(afs_uint32) || !data)
455         goto error;
456
457     /* ensure key being set matches current pag */
458     pag = afs_linux_pag_from_groups(current_group_info());
459
460     if (pag == NOPAG)
461         goto error;
462
463     userpag = (afs_uint32 *) data;
464     if (*userpag != pag)
465         goto error;
466
467     key->payload.value = (unsigned long) *userpag;
468     key->datalen = sizeof(afs_uint32);
469     code = 0;
470
471 error:
472     put_group_info(current_group_info());
473     return code;
474 }
475
476 static int afs_pag_match(const struct key *key, const void *description)
477 {
478         return strcmp(key->description, description) == 0;
479 }
480
481 static void afs_pag_destroy(struct key *key)
482 {
483     afs_uint32 pag = key->payload.value;
484     int locked = ISAFS_GLOCK();
485
486     if (!locked)
487         AFS_GLOCK();
488
489     afs_MarkUserExpired(pag);
490
491     if (!locked)
492         AFS_GUNLOCK();
493 }
494
495 struct key_type key_type_afs_pag =
496 {
497     .name        = "afs_pag",
498     .describe    = afs_pag_describe,
499     .instantiate = afs_pag_instantiate,
500     .match       = afs_pag_match,
501     .destroy     = afs_pag_destroy,
502 };
503
504 #ifdef EXPORTED_TASKLIST_LOCK
505 extern rwlock_t tasklist_lock __attribute__((weak));
506 #endif
507
508 void osi_keyring_init(void)
509 {
510 #if !defined(EXPORTED_KEY_TYPE_KEYRING)
511     struct task_struct *p;
512
513     /* If we can't lock the tasklist, either with its explicit lock,
514      * or by using the RCU lock, then we can't safely work out the 
515      * type of a keyring. So, we have to rely on the weak reference. 
516      * If that's not available, then keyring based PAGs won't work.
517      */
518     
519 #if defined(EXPORTED_TASKLIST_LOCK) || (LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,16) && defined(HAVE_LINUX_RCU_READ_LOCK))
520     if (__key_type_keyring == NULL) {
521 # ifdef EXPORTED_TASKLIST_LOCK
522         if (&tasklist_lock)
523             read_lock(&tasklist_lock);
524 # endif
525 # if (LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,16) && defined(HAVE_LINUX_RCU_READ_LOCK))
526 #  ifdef EXPORTED_TASKLIST_LOCK
527         else
528 #  endif
529             rcu_read_lock();
530 # endif
531 #if defined(HAVE_LINUX_FIND_TASK_BY_PID)
532         p = find_task_by_pid(1);
533 #else
534         p = find_task_by_vpid(1);
535 #endif
536         if (p && task_user(p)->session_keyring)
537             __key_type_keyring = task_user(p)->session_keyring->type;
538 # ifdef EXPORTED_TASKLIST_LOCK
539         if (&tasklist_lock)
540             read_unlock(&tasklist_lock);
541 # endif
542 # if (LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,16) && defined(HAVE_LINUX_RCU_READ_LOCK))
543 #  ifdef EXPORTED_TASKLIST_LOCK
544         else
545 #  endif
546             rcu_read_unlock();
547 # endif
548     }
549 #endif
550 #endif
551
552     register_key_type(&key_type_afs_pag);
553 }
554
555 void osi_keyring_shutdown(void)
556 {
557     unregister_key_type(&key_type_afs_pag);
558 }
559
560 afs_int32
561 osi_get_keyring_pag(afs_ucred_t *cred)
562 {
563     struct key *key;
564     afs_uint32 newpag;
565     afs_int32 keyring_pag = NOPAG;
566
567     if (afs_cr_rgid(cred) != NFSXLATOR_CRED) {
568         key = afs_linux_search_keyring(cred, &key_type_afs_pag);
569
570         if (!IS_ERR(key)) {
571             if (key_validate(key) == 0 && key->uid == 0) {      /* also verify in the session keyring? */
572                 keyring_pag = key->payload.value;
573                 /* Only set PAG in groups if needed,
574                  * and the creds are from the current process */
575                 if (afs_linux_cred_is_current(cred) &&
576                     ((keyring_pag >> 24) & 0xff) == 'A' &&
577                     keyring_pag != afs_linux_pag_from_groups(current_group_info())) {
578                         __setpag(&cred, keyring_pag, &newpag, 0);
579                 }
580             }
581             key_put(key);
582         }
583     }
584     return keyring_pag;
585 }
586
587 #else
588 void osi_keyring_init(void)
589 {
590         return;
591 }
592
593 void osi_keyring_shutdown(void)
594 {
595         return;
596 }
597 #endif