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