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