DEVEL15-windows-afsd-badsharename-20090317
[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     gid_t g0, g1;
170     struct group_info *tmp;
171     int i;
172 #ifdef AFS_LINUX26_ONEGROUP_ENV
173     int j;
174 #endif
175     int need_space = 0;
176
177     group_info = afs_getgroups(*cr);
178     if (group_info->ngroups < NUMPAGGROUPS
179         ||  afs_get_pag_from_groups(
180 #ifdef AFS_LINUX26_ONEGROUP_ENV
181             group_info
182 #else
183             GROUP_AT(group_info, 0) ,GROUP_AT(group_info, 1)
184 #endif
185                                     ) == NOPAG) 
186         /* We will have to make sure group_info is big enough for pag */
187         need_space = NUMPAGGROUPS;
188
189     tmp = groups_alloc(group_info->ngroups + need_space);
190     
191     *newpag = (pagvalue == -1 ? genpag() : pagvalue);
192 #ifdef AFS_LINUX26_ONEGROUP_ENV
193     for (i = 0, j = 0; i < group_info->ngroups; ++i) {
194         int ths = GROUP_AT(group_info, i);
195         int last = i > 0 ? GROUP_AT(group_info, i-1) : 0;
196         if ((ths >> 24) == 'A')
197             continue;
198         if (last <= *newpag && ths > *newpag) {
199            GROUP_AT(tmp, j) = *newpag;
200            j++;
201         }
202         GROUP_AT(tmp, j) = ths;
203         j++;
204     }
205     if (j != i + need_space)
206         GROUP_AT(tmp, j) = *newpag;
207 #else
208     for (i = 0; i < group_info->ngroups; ++i)
209       GROUP_AT(tmp, i + need_space) = GROUP_AT(group_info, i);
210 #endif
211     put_group_info(group_info);
212     group_info = tmp;
213
214 #ifndef AFS_LINUX26_ONEGROUP_ENV
215     afs_get_groups_from_pag(*newpag, &g0, &g1);
216     GROUP_AT(group_info, 0) = g0;
217     GROUP_AT(group_info, 1) = g1;
218 #endif
219
220     afs_setgroups(cr, group_info, change_parent);
221
222     put_group_info(group_info);
223
224     return 0;
225 }
226
227 #ifdef LINUX_KEYRING_SUPPORT
228 extern struct key_type key_type_keyring __attribute__((weak));
229 static struct key_type *__key_type_keyring = &key_type_keyring;
230
231 static int
232 install_session_keyring(struct task_struct *task, struct key *keyring)
233 {
234     struct key *old;
235     char desc[20];
236     unsigned long not_in_quota;
237     int code = -EINVAL;
238
239     if (!__key_type_keyring)
240         return code;
241
242     if (!keyring) {
243
244         /* create an empty session keyring */
245         not_in_quota = KEY_ALLOC_IN_QUOTA;
246         sprintf(desc, "_ses.%u", task->tgid);
247
248 #ifdef KEY_ALLOC_NEEDS_STRUCT_TASK
249         keyring = key_alloc(__key_type_keyring, desc,
250                             task_uid(task), task_gid(task), task,
251                             (KEY_POS_ALL & ~KEY_POS_SETATTR) | KEY_USR_ALL,
252                             not_in_quota);
253 #else
254         keyring = key_alloc(__key_type_keyring, desc,
255                             task_uid(task), task_gid(task),
256                             (KEY_POS_ALL & ~KEY_POS_SETATTR) | KEY_USR_ALL,
257                             not_in_quota);
258 #endif
259         if (IS_ERR(keyring)) {
260             code = PTR_ERR(keyring);
261             goto out;
262         }
263     }
264
265     code = key_instantiate_and_link(keyring, NULL, 0, NULL, NULL);
266     if (code < 0) {
267         key_put(keyring);
268         goto out;
269     }
270
271     /* install the keyring */
272     spin_lock_irq(&task->sighand->siglock);
273     old = task_session_keyring(task);
274     smp_wmb();
275     task_session_keyring(task) = keyring;
276     spin_unlock_irq(&task->sighand->siglock);
277
278     if (old)
279             key_put(old);
280
281 out:
282     return code;
283 }
284 #endif /* LINUX_KEYRING_SUPPORT */
285
286 #else
287 int
288 __setpag(cred_t **cr, afs_uint32 pagvalue, afs_uint32 *newpag,
289          int change_parent)
290 {
291     gid_t *gidset;
292     afs_int32 ngroups, code = 0;
293     int j;
294
295     gidset = (gid_t *) osi_Alloc(NGROUPS * sizeof(gidset[0]));
296     ngroups = afs_getgroups(*cr, gidset);
297
298     if (afs_get_pag_from_groups(gidset[0], gidset[1]) == NOPAG) {
299         /* We will have to shift grouplist to make room for pag */
300         if (ngroups + 2 > NGROUPS) {
301             osi_Free((char *)gidset, NGROUPS * sizeof(int));
302             return EINVAL;
303         }
304         for (j = ngroups - 1; j >= 0; j--) {
305             gidset[j + 2] = gidset[j];
306         }
307         ngroups += 2;
308     }
309     *newpag = (pagvalue == -1 ? genpag() : pagvalue);
310     afs_get_groups_from_pag(*newpag, &gidset[0], &gidset[1]);
311     code = afs_setgroups(cr, ngroups, gidset, change_parent);
312
313     /* If change_parent is set, then we should set the pag in the parent as
314      * well.
315      */
316     if (change_parent && !code) {
317         code = set_pag_in_parent(*newpag, gidset[0], gidset[1]);
318     }
319
320     osi_Free((char *)gidset, NGROUPS * sizeof(int));
321     return code;
322 }
323 #endif
324
325
326 int
327 setpag(cred_t **cr, afs_uint32 pagvalue, afs_uint32 *newpag,
328        int change_parent)
329 {
330     int code;
331
332     AFS_STATCNT(setpag);
333
334     code = __setpag(cr, pagvalue, newpag, change_parent);
335
336 #ifdef LINUX_KEYRING_SUPPORT
337     if (code == 0 && (*cr)->cr_rgid != NFSXLATOR_CRED) {
338         (void) install_session_keyring(current, NULL);
339
340         if (current_session_keyring()) {
341             struct key *key;
342             key_perm_t perm;
343
344             perm = KEY_POS_VIEW | KEY_POS_SEARCH;
345             perm |= KEY_USR_VIEW | KEY_USR_SEARCH;
346
347 #ifdef KEY_ALLOC_NEEDS_STRUCT_TASK
348             key = key_alloc(&key_type_afs_pag, "_pag", 0, 0, current, perm, 1);
349 #else
350             key = key_alloc(&key_type_afs_pag, "_pag", 0, 0, perm, 1);
351 #endif
352
353             if (!IS_ERR(key)) {
354                 key_instantiate_and_link(key, (void *) newpag, sizeof(afs_uint32),
355                                          current_session_keyring(), NULL);
356                 key_put(key);
357             }
358         }
359     }
360 #endif /* LINUX_KEYRING_SUPPORT */
361
362     return code;
363 }
364
365
366 /* Intercept the standard system call. */
367 extern asmlinkage long (*sys_setgroupsp) (int gidsetsize, gid_t * grouplist);
368 asmlinkage long
369 afs_xsetgroups(int gidsetsize, gid_t * grouplist)
370 {
371     long code;
372     cred_t *cr = crref();
373     afs_uint32 junk;
374     int old_pag;
375
376     lock_kernel();
377     old_pag = PagInCred(cr);
378     crfree(cr);
379     unlock_kernel();
380
381     code = (*sys_setgroupsp) (gidsetsize, grouplist);
382     if (code) {
383         return code;
384     }
385
386     lock_kernel();
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     unlock_kernel();
394
395     /* Linux syscall ABI returns errno as negative */
396     return (-code);
397 }
398
399 #if defined(AFS_LINUX24_ENV)
400 /* Intercept the standard uid32 system call. */
401 extern asmlinkage long (*sys_setgroups32p) (int gidsetsize, gid_t * grouplist);
402 asmlinkage long
403 afs_xsetgroups32(int gidsetsize, gid_t * grouplist)
404 {
405     long code;
406     cred_t *cr = crref();
407     afs_uint32 junk;
408     int old_pag;
409
410     lock_kernel();
411     old_pag = PagInCred(cr);
412     crfree(cr);
413     unlock_kernel();
414
415     code = (*sys_setgroups32p) (gidsetsize, grouplist);
416
417     if (code) {
418         return code;
419     }
420
421     lock_kernel();
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     unlock_kernel();
429
430     /* Linux syscall ABI returns errno as negative */
431     return (-code);
432 }
433 #endif
434
435 #if defined(AFS_PPC64_LINUX20_ENV)
436 /* Intercept the uid16 system call as used by 32bit programs. */
437 extern long (*sys32_setgroupsp)(int gidsetsize, gid_t *grouplist);
438 asmlinkage long afs32_xsetgroups(int gidsetsize, gid_t *grouplist)
439 {
440     long code;
441     cred_t *cr = crref();
442     afs_uint32 junk;
443     int old_pag;
444     
445     lock_kernel();
446     old_pag = PagInCred(cr);
447     crfree(cr);
448     unlock_kernel();
449     
450     code = (*sys32_setgroupsp)(gidsetsize, grouplist);
451     if (code) {
452         return code;
453     }
454     
455     lock_kernel();
456     cr = crref();
457     if (old_pag != NOPAG && PagInCred(cr) == NOPAG) {
458         /* re-install old pag if there's room. */
459         code = __setpag(&cr, old_pag, &junk, 0);
460     }
461     crfree(cr);
462     unlock_kernel();
463     
464     /* Linux syscall ABI returns errno as negative */
465     return (-code);
466 }
467 #endif
468
469 #if defined(AFS_SPARC64_LINUX20_ENV) || defined(AFS_AMD64_LINUX20_ENV)
470 /* Intercept the uid16 system call as used by 32bit programs. */
471 extern long (*sys32_setgroupsp) (int gidsetsize, u16 * grouplist);
472 asmlinkage long
473 afs32_xsetgroups(int gidsetsize, u16 * grouplist)
474 {
475     long code;
476     cred_t *cr = crref();
477     afs_uint32 junk;
478     int old_pag;
479     
480     lock_kernel();
481     old_pag = PagInCred(cr);
482     crfree(cr);
483     unlock_kernel();
484     
485     code = (*sys32_setgroupsp) (gidsetsize, grouplist);
486     if (code) {
487         return code;
488     }
489     
490     lock_kernel();
491     cr = crref();
492     if (old_pag != NOPAG && PagInCred(cr) == NOPAG) {
493         /* re-install old pag if there's room. */
494         code = __setpag(&cr, old_pag, &junk, 0);
495     }
496     crfree(cr);
497     unlock_kernel();
498     
499     /* Linux syscall ABI returns errno as negative */
500     return (-code);
501 }
502
503 #ifdef AFS_LINUX24_ENV
504 /* Intercept the uid32 system call as used by 32bit programs. */
505 extern long (*sys32_setgroups32p) (int gidsetsize, gid_t * grouplist);
506 asmlinkage long
507 afs32_xsetgroups32(int gidsetsize, gid_t * grouplist)
508 {
509     long code;
510     cred_t *cr = crref();
511     afs_uint32 junk;
512     int old_pag;
513
514     lock_kernel();
515     old_pag = PagInCred(cr);
516     crfree(cr);
517     unlock_kernel();
518
519     code = (*sys32_setgroups32p) (gidsetsize, grouplist);
520     if (code) {
521         return code;
522     }
523
524     lock_kernel();
525     cr = crref();
526     if (old_pag != NOPAG && PagInCred(cr) == NOPAG) {
527         /* re-install old pag if there's room. */
528         code = __setpag(&cr, old_pag, &junk, 0);
529     }
530     crfree(cr);
531     unlock_kernel();
532
533     /* Linux syscall ABI returns errno as negative */
534     return (-code);
535 }
536 #endif
537 #endif
538
539
540 #ifdef LINUX_KEYRING_SUPPORT
541 static void afs_pag_describe(const struct key *key, struct seq_file *m)
542 {
543     seq_puts(m, key->description);
544
545     seq_printf(m, ": %u", key->datalen);
546 }
547
548 static int afs_pag_instantiate(struct key *key, const void *data, size_t datalen)
549 {
550     int code;
551     afs_uint32 *userpag, pag = NOPAG;
552 #ifndef AFS_LINUX26_ONEGROUP_ENV
553     int g0, g1;
554 #endif
555
556     if (key->uid != 0 || key->gid != 0)
557         return -EPERM;
558
559     code = -EINVAL;
560     get_group_info(current_group_info());
561
562     if (datalen != sizeof(afs_uint32) || !data)
563         goto error;
564
565     if (current_group_info()->ngroups < NUMPAGGROUPS)
566         goto error;
567
568     /* ensure key being set matches current pag */
569 #ifdef AFS_LINUX26_ONEGROUP_ENV
570     pag = afs_get_pag_from_groups(current_group_info());
571 #else
572     g0 = GROUP_AT(current_group_info(), 0);
573     g1 = GROUP_AT(current_group_info(), 1);
574
575     pag = afs_get_pag_from_groups(g0, g1);
576 #endif
577     if (pag == NOPAG)
578         goto error;
579
580     userpag = (afs_uint32 *) data;
581     if (*userpag != pag)
582         goto error;
583
584     key->payload.value = (unsigned long) *userpag;
585     key->datalen = sizeof(afs_uint32);
586     code = 0;
587
588 error:
589     put_group_info(current_group_info());
590     return code;
591 }
592
593 static int afs_pag_match(const struct key *key, const void *description)
594 {
595         return strcmp(key->description, description) == 0;
596 }
597
598 static void afs_pag_destroy(struct key *key)
599 {
600     afs_uint32 pag = key->payload.value;
601     struct unixuser *pu;
602     int locked = ISAFS_GLOCK();
603
604     if (!locked)
605         AFS_GLOCK();
606     pu = afs_FindUser(pag, -1, READ_LOCK);
607     if (pu) {
608         pu->ct.EndTimestamp = 0;
609         pu->tokenTime = 0;
610         afs_PutUser(pu, READ_LOCK);
611     }
612     if (!locked)
613         AFS_GUNLOCK();
614 }
615
616 struct key_type key_type_afs_pag =
617 {
618     .name        = "afs_pag",
619     .describe    = afs_pag_describe,
620     .instantiate = afs_pag_instantiate,
621     .match       = afs_pag_match,
622     .destroy     = afs_pag_destroy,
623 };
624
625 #ifdef EXPORTED_TASKLIST_LOCK
626 extern rwlock_t tasklist_lock __attribute__((weak));
627 #endif
628
629 void osi_keyring_init(void)
630 {
631     struct task_struct *p;
632
633     /* If we can't lock the tasklist, either with its explicit lock,
634      * or by using the RCU lock, then we can't safely work out the 
635      * type of a keyring. So, we have to rely on the weak reference. 
636      * If that's not available, then keyring based PAGs won't work.
637      */
638     
639 #if defined(EXPORTED_TASKLIST_LOCK) || (LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,16) && defined(EXPORTED_RCU_READ_LOCK))
640     if (__key_type_keyring == NULL) {
641 # ifdef EXPORTED_TASKLIST_LOCK
642         if (&tasklist_lock)
643             read_lock(&tasklist_lock);
644 # endif
645 # if (LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,16) && defined(EXPORTED_RCU_READ_LOCK))
646 #  ifdef EXPORTED_TASKLIST_LOCK
647         else
648 #  endif
649             rcu_read_lock();
650 # endif
651 #if defined(EXPORTED_FIND_TASK_BY_PID)
652         p = find_task_by_pid(1);
653 #else
654         p = find_task_by_vpid(1);
655 #endif
656         if (p && task_user(p)->session_keyring)
657             __key_type_keyring = task_user(p)->session_keyring->type;
658 # ifdef EXPORTED_TASKLIST_LOCK
659         if (&tasklist_lock)
660             read_unlock(&tasklist_lock);
661 # endif
662 # if (LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,16) && defined(EXPORTED_RCU_READ_LOCK))
663 #  ifdef EXPORTED_TASKLIST_LOCK
664         else
665 #  endif
666             rcu_read_unlock();
667 # endif
668     }
669 #endif
670
671     register_key_type(&key_type_afs_pag);
672 }
673
674 void osi_keyring_shutdown(void)
675 {
676     unregister_key_type(&key_type_afs_pag);
677 }
678
679 #else
680 void osi_keyring_init(void)
681 {
682         return;
683 }
684
685 void osi_keyring_shutdown(void)
686 {
687         return;
688 }
689 #endif