linux-key-alloc-changes-20090318
[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 RCSID
23     ("$Header$");
24
25 #include "afs/sysincludes.h"
26 #include "afsincludes.h"
27 #include "afs/afs_stats.h"      /* statistics */
28 #include "afs/nfsclient.h"
29 #ifdef AFS_LINUX22_ENV
30 #include "h/smp_lock.h"
31 #endif
32
33 #ifdef AFS_LINUX26_ONEGROUP_ENV
34 #define NUMPAGGROUPS 1
35 #else
36 #define NUMPAGGROUPS 2
37 #endif
38
39 #if defined(AFS_LINUX26_ENV)
40 static int
41 afs_setgroups(cred_t **cr, struct group_info *group_info, int change_parent)
42 {
43     struct group_info *old_info;
44
45     AFS_STATCNT(afs_setgroups);
46
47     old_info = (*cr)->cr_group_info;
48     get_group_info(group_info);
49     (*cr)->cr_group_info = group_info;
50     put_group_info(old_info);
51
52     crset(*cr);
53
54 #if defined(STRUCT_TASK_STRUCT_HAS_PARENT) && !defined(STRUCT_TASK_HAS_CRED)
55     if (change_parent) {
56         old_info = current->parent->group_info;
57         get_group_info(group_info);
58         current->parent->group_info = group_info;
59         put_group_info(old_info);
60     }
61 #endif
62
63     return (0);
64 }
65 #else
66 static int
67 afs_setgroups(cred_t **cr, int ngroups, gid_t * gidset, int change_parent)
68 {
69     int ngrps;
70     int i;
71     gid_t *gp;
72
73     AFS_STATCNT(afs_setgroups);
74
75     if (ngroups > NGROUPS)
76         return EINVAL;
77
78     gp = (*cr)->cr_groups;
79     if (ngroups < NGROUPS)
80         gp[ngroups] = (gid_t) NOGROUP;
81
82     for (i = ngroups; i > 0; i--) {
83         *gp++ = *gidset++;
84     }
85
86     (*cr)->cr_ngroups = ngroups;
87     crset(*cr);
88     return (0);
89 }
90 #endif
91
92 #if defined(AFS_LINUX26_ENV)
93 static struct group_info *
94 afs_getgroups(cred_t * cr)
95 {
96     AFS_STATCNT(afs_getgroups);
97
98     get_group_info(cr->cr_group_info);
99     return cr->cr_group_info;
100 }
101 #else
102 /* Returns number of groups. And we trust groups to be large enough to
103  * hold all the groups.
104  */
105 static int
106 afs_getgroups(cred_t *cr, gid_t *groups)
107 {
108     int i;
109     int n;
110     gid_t *gp;
111
112     AFS_STATCNT(afs_getgroups);
113
114     gp = cr->cr_groups;
115     n = cr->cr_ngroups;
116
117     for (i = 0; (i < n) && (*gp != (gid_t) NOGROUP); i++)
118         *groups++ = *gp++;
119     return i;
120 }
121 #endif
122
123 #if !defined(AFS_LINUX26_ENV)
124 /* Only propogate the PAG to the parent process. Unix's propogate to 
125  * all processes sharing the cred.
126  */
127 int
128 set_pag_in_parent(int pag, int g0, int g1)
129 {
130     int i;
131 #ifdef STRUCT_TASK_STRUCT_HAS_PARENT
132     gid_t *gp = current->parent->groups;
133     int ngroups = current->parent->ngroups;
134 #else
135     gid_t *gp = current->p_pptr->groups;
136     int ngroups = current->p_pptr->ngroups;
137 #endif
138
139     if ((ngroups < 2) || (afs_get_pag_from_groups(gp[0], gp[1]) == NOPAG)) {
140         /* We will have to shift grouplist to make room for pag */
141         if (ngroups + 2 > NGROUPS) {
142             return EINVAL;
143         }
144         for (i = ngroups - 1; i >= 0; i--) {
145             gp[i + 2] = gp[i];
146         }
147         ngroups += 2;
148     }
149     gp[0] = g0;
150     gp[1] = g1;
151     if (ngroups < NGROUPS)
152         gp[ngroups] = NOGROUP;
153
154 #ifdef STRUCT_TASK_STRUCT_HAS_PARENT
155     current->parent->ngroups = ngroups;
156 #else
157     current->p_pptr->ngroups = ngroups;
158 #endif
159     return 0;
160 }
161 #endif
162
163 #if defined(AFS_LINUX26_ENV)
164 int
165 __setpag(cred_t **cr, afs_uint32 pagvalue, afs_uint32 *newpag,
166          int change_parent)
167 {
168     struct group_info *group_info;
169 #ifndef AFS_LINUX26_ONEGROUP_ENV
170     gid_t g0, g1;
171 #endif
172     struct group_info *tmp;
173     int i;
174 #ifdef AFS_LINUX26_ONEGROUP_ENV
175     int j;
176 #endif
177     int need_space = 0;
178
179     group_info = afs_getgroups(*cr);
180     if (group_info->ngroups < NUMPAGGROUPS
181         ||  afs_get_pag_from_groups(
182 #ifdef AFS_LINUX26_ONEGROUP_ENV
183             group_info
184 #else
185             GROUP_AT(group_info, 0) ,GROUP_AT(group_info, 1)
186 #endif
187                                     ) == NOPAG) 
188         /* We will have to make sure group_info is big enough for pag */
189         need_space = NUMPAGGROUPS;
190
191     tmp = groups_alloc(group_info->ngroups + need_space);
192     
193     *newpag = (pagvalue == -1 ? genpag() : pagvalue);
194 #ifdef AFS_LINUX26_ONEGROUP_ENV
195     for (i = 0, j = 0; i < group_info->ngroups; ++i) {
196         int ths = GROUP_AT(group_info, i);
197         int last = i > 0 ? GROUP_AT(group_info, i-1) : 0;
198         if ((ths >> 24) == 'A')
199             continue;
200         if (last <= *newpag && ths > *newpag) {
201            GROUP_AT(tmp, j) = *newpag;
202            j++;
203         }
204         GROUP_AT(tmp, j) = ths;
205         j++;
206     }
207     if (j != i + need_space)
208         GROUP_AT(tmp, j) = *newpag;
209 #else
210     for (i = 0; i < group_info->ngroups; ++i)
211       GROUP_AT(tmp, i + need_space) = GROUP_AT(group_info, i);
212 #endif
213     put_group_info(group_info);
214     group_info = tmp;
215
216 #ifndef AFS_LINUX26_ONEGROUP_ENV
217     afs_get_groups_from_pag(*newpag, &g0, &g1);
218     GROUP_AT(group_info, 0) = g0;
219     GROUP_AT(group_info, 1) = g1;
220 #endif
221
222     afs_setgroups(cr, group_info, change_parent);
223
224     put_group_info(group_info);
225
226     return 0;
227 }
228
229 #ifdef LINUX_KEYRING_SUPPORT
230 extern struct key_type key_type_keyring __attribute__((weak));
231 static struct key_type *__key_type_keyring = &key_type_keyring;
232
233 static int
234 install_session_keyring(struct task_struct *task, struct key *keyring)
235 {
236     struct key *old;
237     char desc[20];
238     unsigned long not_in_quota;
239     int code = -EINVAL;
240
241     if (!__key_type_keyring)
242         return code;
243
244     if (!keyring) {
245
246         /* create an empty session keyring */
247         not_in_quota = KEY_ALLOC_IN_QUOTA;
248         sprintf(desc, "_ses.%u", task->tgid);
249
250 #if defined(KEY_ALLOC_NEEDS_STRUCT_TASK)
251         keyring = key_alloc(__key_type_keyring, desc,
252                             task_uid(task), task_gid(task), task,
253                             (KEY_POS_ALL & ~KEY_POS_SETATTR) | KEY_USR_ALL,
254                             not_in_quota);
255 #elif defined(KEY_ALLOC_NEEDS_CRED)
256         keyring = key_alloc(__key_type_keyring, desc,
257                             task_uid(task), task_gid(task), current_cred(),
258                             (KEY_POS_ALL & ~KEY_POS_SETATTR) | KEY_USR_ALL,
259                             not_in_quota);
260 #else
261         keyring = key_alloc(__key_type_keyring, desc,
262                             task_uid(task), task_gid(task),
263                             (KEY_POS_ALL & ~KEY_POS_SETATTR) | KEY_USR_ALL,
264                             not_in_quota);
265 #endif
266         if (IS_ERR(keyring)) {
267             code = PTR_ERR(keyring);
268             goto out;
269         }
270     }
271
272     code = key_instantiate_and_link(keyring, NULL, 0, NULL, NULL);
273     if (code < 0) {
274         key_put(keyring);
275         goto out;
276     }
277
278     /* install the keyring */
279     spin_lock_irq(&task->sighand->siglock);
280     old = task_session_keyring(task);
281     smp_wmb();
282     task_session_keyring(task) = keyring;
283     spin_unlock_irq(&task->sighand->siglock);
284
285     if (old)
286             key_put(old);
287
288 out:
289     return code;
290 }
291 #endif /* LINUX_KEYRING_SUPPORT */
292
293 #else
294 int
295 __setpag(cred_t **cr, afs_uint32 pagvalue, afs_uint32 *newpag,
296          int change_parent)
297 {
298     gid_t *gidset;
299     afs_int32 ngroups, code = 0;
300     int j;
301
302     gidset = (gid_t *) osi_Alloc(NGROUPS * sizeof(gidset[0]));
303     ngroups = afs_getgroups(*cr, gidset);
304
305     if (afs_get_pag_from_groups(gidset[0], gidset[1]) == NOPAG) {
306         /* We will have to shift grouplist to make room for pag */
307         if (ngroups + 2 > NGROUPS) {
308             osi_Free((char *)gidset, NGROUPS * sizeof(int));
309             return EINVAL;
310         }
311         for (j = ngroups - 1; j >= 0; j--) {
312             gidset[j + 2] = gidset[j];
313         }
314         ngroups += 2;
315     }
316     *newpag = (pagvalue == -1 ? genpag() : pagvalue);
317     afs_get_groups_from_pag(*newpag, &gidset[0], &gidset[1]);
318     code = afs_setgroups(cr, ngroups, gidset, change_parent);
319
320     /* If change_parent is set, then we should set the pag in the parent as
321      * well.
322      */
323     if (change_parent && !code) {
324         code = set_pag_in_parent(*newpag, gidset[0], gidset[1]);
325     }
326
327     osi_Free((char *)gidset, NGROUPS * sizeof(int));
328     return code;
329 }
330 #endif
331
332
333 int
334 setpag(cred_t **cr, afs_uint32 pagvalue, afs_uint32 *newpag,
335        int change_parent)
336 {
337     int code;
338
339     AFS_STATCNT(setpag);
340
341     code = __setpag(cr, pagvalue, newpag, change_parent);
342
343 #ifdef LINUX_KEYRING_SUPPORT
344     if (code == 0 && (*cr)->cr_rgid != NFSXLATOR_CRED) {
345         (void) install_session_keyring(current, NULL);
346
347         if (current_session_keyring()) {
348             struct key *key;
349             key_perm_t perm;
350
351             perm = KEY_POS_VIEW | KEY_POS_SEARCH;
352             perm |= KEY_USR_VIEW | KEY_USR_SEARCH;
353
354 #if defined(KEY_ALLOC_NEEDS_STRUCT_TASK)
355             key = key_alloc(&key_type_afs_pag, "_pag", 0, 0, current, perm, 1);
356 #elif defined(KEY_ALLOC_NEEDS_CRED)
357             key = key_alloc(&key_type_afs_pag, "_pag", 0, 0, current_cred(), perm, 1);
358 #else
359             key = key_alloc(&key_type_afs_pag, "_pag", 0, 0, perm, 1);
360 #endif
361
362             if (!IS_ERR(key)) {
363                 key_instantiate_and_link(key, (void *) newpag, sizeof(afs_uint32),
364                                          current_session_keyring(), NULL);
365                 key_put(key);
366             }
367         }
368     }
369 #endif /* LINUX_KEYRING_SUPPORT */
370
371     return code;
372 }
373
374
375 /* Intercept the standard system call. */
376 extern asmlinkage long (*sys_setgroupsp) (int gidsetsize, gid_t * grouplist);
377 asmlinkage long
378 afs_xsetgroups(int gidsetsize, gid_t * grouplist)
379 {
380     long code;
381     cred_t *cr = crref();
382     afs_uint32 junk;
383     int old_pag;
384
385     lock_kernel();
386     old_pag = PagInCred(cr);
387     crfree(cr);
388     unlock_kernel();
389
390     code = (*sys_setgroupsp) (gidsetsize, grouplist);
391     if (code) {
392         return code;
393     }
394
395     lock_kernel();
396     cr = crref();
397     if (old_pag != NOPAG && PagInCred(cr) == NOPAG) {
398         /* re-install old pag if there's room. */
399         code = __setpag(&cr, old_pag, &junk, 0);
400     }
401     crfree(cr);
402     unlock_kernel();
403
404     /* Linux syscall ABI returns errno as negative */
405     return (-code);
406 }
407
408 #if defined(AFS_LINUX24_ENV)
409 /* Intercept the standard uid32 system call. */
410 extern asmlinkage long (*sys_setgroups32p) (int gidsetsize, gid_t * grouplist);
411 asmlinkage long
412 afs_xsetgroups32(int gidsetsize, gid_t * grouplist)
413 {
414     long code;
415     cred_t *cr = crref();
416     afs_uint32 junk;
417     int old_pag;
418
419     lock_kernel();
420     old_pag = PagInCred(cr);
421     crfree(cr);
422     unlock_kernel();
423
424     code = (*sys_setgroups32p) (gidsetsize, grouplist);
425
426     if (code) {
427         return code;
428     }
429
430     lock_kernel();
431     cr = crref();
432     if (old_pag != NOPAG && PagInCred(cr) == NOPAG) {
433         /* re-install old pag if there's room. */
434         code = __setpag(&cr, old_pag, &junk, 0);
435     }
436     crfree(cr);
437     unlock_kernel();
438
439     /* Linux syscall ABI returns errno as negative */
440     return (-code);
441 }
442 #endif
443
444 #if defined(AFS_PPC64_LINUX20_ENV)
445 /* Intercept the uid16 system call as used by 32bit programs. */
446 extern long (*sys32_setgroupsp)(int gidsetsize, gid_t *grouplist);
447 asmlinkage long afs32_xsetgroups(int gidsetsize, gid_t *grouplist)
448 {
449     long code;
450     cred_t *cr = crref();
451     afs_uint32 junk;
452     int old_pag;
453     
454     lock_kernel();
455     old_pag = PagInCred(cr);
456     crfree(cr);
457     unlock_kernel();
458     
459     code = (*sys32_setgroupsp)(gidsetsize, grouplist);
460     if (code) {
461         return code;
462     }
463     
464     lock_kernel();
465     cr = crref();
466     if (old_pag != NOPAG && PagInCred(cr) == NOPAG) {
467         /* re-install old pag if there's room. */
468         code = __setpag(&cr, old_pag, &junk, 0);
469     }
470     crfree(cr);
471     unlock_kernel();
472     
473     /* Linux syscall ABI returns errno as negative */
474     return (-code);
475 }
476 #endif
477
478 #if defined(AFS_SPARC64_LINUX20_ENV) || defined(AFS_AMD64_LINUX20_ENV)
479 /* Intercept the uid16 system call as used by 32bit programs. */
480 extern long (*sys32_setgroupsp) (int gidsetsize, u16 * grouplist);
481 asmlinkage long
482 afs32_xsetgroups(int gidsetsize, u16 * grouplist)
483 {
484     long code;
485     cred_t *cr = crref();
486     afs_uint32 junk;
487     int old_pag;
488     
489     lock_kernel();
490     old_pag = PagInCred(cr);
491     crfree(cr);
492     unlock_kernel();
493     
494     code = (*sys32_setgroupsp) (gidsetsize, grouplist);
495     if (code) {
496         return code;
497     }
498     
499     lock_kernel();
500     cr = crref();
501     if (old_pag != NOPAG && PagInCred(cr) == NOPAG) {
502         /* re-install old pag if there's room. */
503         code = __setpag(&cr, old_pag, &junk, 0);
504     }
505     crfree(cr);
506     unlock_kernel();
507     
508     /* Linux syscall ABI returns errno as negative */
509     return (-code);
510 }
511
512 #ifdef AFS_LINUX24_ENV
513 /* Intercept the uid32 system call as used by 32bit programs. */
514 extern long (*sys32_setgroups32p) (int gidsetsize, gid_t * grouplist);
515 asmlinkage long
516 afs32_xsetgroups32(int gidsetsize, gid_t * grouplist)
517 {
518     long code;
519     cred_t *cr = crref();
520     afs_uint32 junk;
521     int old_pag;
522
523     lock_kernel();
524     old_pag = PagInCred(cr);
525     crfree(cr);
526     unlock_kernel();
527
528     code = (*sys32_setgroups32p) (gidsetsize, grouplist);
529     if (code) {
530         return code;
531     }
532
533     lock_kernel();
534     cr = crref();
535     if (old_pag != NOPAG && PagInCred(cr) == NOPAG) {
536         /* re-install old pag if there's room. */
537         code = __setpag(&cr, old_pag, &junk, 0);
538     }
539     crfree(cr);
540     unlock_kernel();
541
542     /* Linux syscall ABI returns errno as negative */
543     return (-code);
544 }
545 #endif
546 #endif
547
548
549 #ifdef LINUX_KEYRING_SUPPORT
550 static void afs_pag_describe(const struct key *key, struct seq_file *m)
551 {
552     seq_puts(m, key->description);
553
554     seq_printf(m, ": %u", key->datalen);
555 }
556
557 static int afs_pag_instantiate(struct key *key, const void *data, size_t datalen)
558 {
559     int code;
560     afs_uint32 *userpag, pag = NOPAG;
561 #ifndef AFS_LINUX26_ONEGROUP_ENV
562     int g0, g1;
563 #endif
564
565     if (key->uid != 0 || key->gid != 0)
566         return -EPERM;
567
568     code = -EINVAL;
569     get_group_info(current_group_info());
570
571     if (datalen != sizeof(afs_uint32) || !data)
572         goto error;
573
574     if (current_group_info()->ngroups < NUMPAGGROUPS)
575         goto error;
576
577     /* ensure key being set matches current pag */
578 #ifdef AFS_LINUX26_ONEGROUP_ENV
579     pag = afs_get_pag_from_groups(current_group_info());
580 #else
581     g0 = GROUP_AT(current_group_info(), 0);
582     g1 = GROUP_AT(current_group_info(), 1);
583
584     pag = afs_get_pag_from_groups(g0, g1);
585 #endif
586     if (pag == NOPAG)
587         goto error;
588
589     userpag = (afs_uint32 *) data;
590     if (*userpag != pag)
591         goto error;
592
593     key->payload.value = (unsigned long) *userpag;
594     key->datalen = sizeof(afs_uint32);
595     code = 0;
596
597 error:
598     put_group_info(current_group_info());
599     return code;
600 }
601
602 static int afs_pag_match(const struct key *key, const void *description)
603 {
604         return strcmp(key->description, description) == 0;
605 }
606
607 static void afs_pag_destroy(struct key *key)
608 {
609     afs_uint32 pag = key->payload.value;
610     struct unixuser *pu;
611     int locked = ISAFS_GLOCK();
612
613     if (!locked)
614         AFS_GLOCK();
615     pu = afs_FindUser(pag, -1, READ_LOCK);
616     if (pu) {
617         pu->ct.EndTimestamp = 0;
618         pu->tokenTime = 0;
619         afs_PutUser(pu, READ_LOCK);
620     }
621     if (!locked)
622         AFS_GUNLOCK();
623 }
624
625 struct key_type key_type_afs_pag =
626 {
627     .name        = "afs_pag",
628     .describe    = afs_pag_describe,
629     .instantiate = afs_pag_instantiate,
630     .match       = afs_pag_match,
631     .destroy     = afs_pag_destroy,
632 };
633
634 #ifdef EXPORTED_TASKLIST_LOCK
635 extern rwlock_t tasklist_lock __attribute__((weak));
636 #endif
637
638 void osi_keyring_init(void)
639 {
640     struct task_struct *p;
641
642     /* If we can't lock the tasklist, either with its explicit lock,
643      * or by using the RCU lock, then we can't safely work out the 
644      * type of a keyring. So, we have to rely on the weak reference. 
645      * If that's not available, then keyring based PAGs won't work.
646      */
647     
648 #if defined(EXPORTED_TASKLIST_LOCK) || (LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,16) && defined(EXPORTED_RCU_READ_LOCK))
649     if (__key_type_keyring == NULL) {
650 # ifdef EXPORTED_TASKLIST_LOCK
651         if (&tasklist_lock)
652             read_lock(&tasklist_lock);
653 # endif
654 # if (LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,16) && defined(EXPORTED_RCU_READ_LOCK))
655 #  ifdef EXPORTED_TASKLIST_LOCK
656         else
657 #  endif
658             rcu_read_lock();
659 # endif
660 #if defined(EXPORTED_FIND_TASK_BY_PID)
661         p = find_task_by_pid(1);
662 #else
663         p = find_task_by_vpid(1);
664 #endif
665         if (p && task_user(p)->session_keyring)
666             __key_type_keyring = task_user(p)->session_keyring->type;
667 # ifdef EXPORTED_TASKLIST_LOCK
668         if (&tasklist_lock)
669             read_unlock(&tasklist_lock);
670 # endif
671 # if (LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,16) && defined(EXPORTED_RCU_READ_LOCK))
672 #  ifdef EXPORTED_TASKLIST_LOCK
673         else
674 #  endif
675             rcu_read_unlock();
676 # endif
677     }
678 #endif
679
680     register_key_type(&key_type_afs_pag);
681 }
682
683 void osi_keyring_shutdown(void)
684 {
685     unregister_key_type(&key_type_afs_pag);
686 }
687
688 #else
689 void osi_keyring_init(void)
690 {
691         return;
692 }
693
694 void osi_keyring_shutdown(void)
695 {
696         return;
697 }
698 #endif