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