LINUX: Include key-related headers in osi_compat.h
[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_STRUCT_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     unsigned long flags;
178
179     if (!__key_type_keyring)
180         return code;
181
182     if (!keyring) {
183
184         /* create an empty session keyring */
185         sprintf(desc, "_ses.%u", current->tgid);
186
187         /* if we're root, don't count the keyring against our quota. This
188          * avoids starvation issues when dealing with PAM modules that always
189          * setpag() as root */
190         if (current_uid() == 0)
191             flags = KEY_ALLOC_NOT_IN_QUOTA;
192         else
193             flags = KEY_ALLOC_IN_QUOTA;
194
195         keyring = afs_linux_key_alloc(
196                             __key_type_keyring, desc,
197                             current_uid(), current_gid(),
198                             (KEY_POS_ALL & ~KEY_POS_SETATTR) | KEY_USR_ALL,
199                             flags);
200
201         if (IS_ERR(keyring)) {
202             code = PTR_ERR(keyring);
203             goto out;
204         }
205     }
206
207     code = key_instantiate_and_link(keyring, NULL, 0, NULL, NULL);
208     if (code < 0) {
209         key_put(keyring);
210         goto out;
211     }
212
213     /* install the keyring */
214     spin_lock_irq(&current->sighand->siglock);
215     old = task_session_keyring(current);
216     smp_wmb();
217     task_session_keyring(current) = keyring;
218     spin_unlock_irq(&current->sighand->siglock);
219
220     if (old)
221             key_put(old);
222
223 out:
224     return code;
225 }
226 #endif /* LINUX_KEYRING_SUPPORT */
227
228 int
229 setpag(cred_t **cr, afs_uint32 pagvalue, afs_uint32 *newpag,
230        int change_parent)
231 {
232     int code;
233
234     AFS_STATCNT(setpag);
235
236     code = __setpag(cr, pagvalue, newpag, change_parent);
237
238 #ifdef LINUX_KEYRING_SUPPORT
239     if (code == 0 && afs_cr_rgid(*cr) != NFSXLATOR_CRED) {
240         code = install_session_keyring(NULL);
241         if (code == 0 && current_session_keyring()) {
242             struct key *key;
243             key_perm_t perm;
244
245             perm = KEY_POS_VIEW | KEY_POS_SEARCH;
246             perm |= KEY_USR_VIEW | KEY_USR_SEARCH;
247
248             key = afs_linux_key_alloc(&key_type_afs_pag, "_pag", 0, 0, perm, KEY_ALLOC_NOT_IN_QUOTA);
249
250             if (!IS_ERR(key)) {
251                 key_instantiate_and_link(key, (void *) newpag, sizeof(afs_uint32),
252                                          current_session_keyring(), NULL);
253                 key_put(key);
254             } else {
255                 code = PTR_ERR(key);
256             }
257         }
258     }
259 #endif /* LINUX_KEYRING_SUPPORT */
260
261     return code;
262 }
263
264
265 /* Intercept the standard system call. */
266 extern asmlinkage long (*sys_setgroupsp) (int gidsetsize, gid_t * grouplist);
267 asmlinkage long
268 afs_xsetgroups(int gidsetsize, gid_t * grouplist)
269 {
270     long code;
271     cred_t *cr = crref();
272     afs_uint32 junk;
273     int old_pag;
274
275     old_pag = PagInCred(cr);
276     crfree(cr);
277
278     code = (*sys_setgroupsp) (gidsetsize, grouplist);
279     if (code) {
280         return code;
281     }
282
283     cr = crref();
284     if (old_pag != NOPAG && PagInCred(cr) == NOPAG) {
285         /* re-install old pag if there's room. */
286         code = __setpag(&cr, old_pag, &junk, 0);
287     }
288     crfree(cr);
289
290     /* Linux syscall ABI returns errno as negative */
291     return (-code);
292 }
293
294 /* Intercept the standard uid32 system call. */
295 extern asmlinkage int (*sys_setgroups32p) (int gidsetsize,
296                                            __kernel_gid32_t * grouplist);
297 asmlinkage long
298 afs_xsetgroups32(int gidsetsize, gid_t * grouplist)
299 {
300     long code;
301     cred_t *cr = crref();
302     afs_uint32 junk;
303     int old_pag;
304
305     old_pag = PagInCred(cr);
306     crfree(cr);
307
308     code = (*sys_setgroups32p) (gidsetsize, grouplist);
309
310     if (code) {
311         return code;
312     }
313
314     cr = crref();
315     if (old_pag != NOPAG && PagInCred(cr) == NOPAG) {
316         /* re-install old pag if there's room. */
317         code = __setpag(&cr, old_pag, &junk, 0);
318     }
319     crfree(cr);
320
321     /* Linux syscall ABI returns errno as negative */
322     return (-code);
323 }
324
325 #if defined(AFS_PPC64_LINUX20_ENV)
326 /* Intercept the uid16 system call as used by 32bit programs. */
327 extern asmlinkage long (*sys32_setgroupsp)(int gidsetsize, gid_t *grouplist);
328 asmlinkage long afs32_xsetgroups(int gidsetsize, gid_t *grouplist)
329 {
330     long code;
331     cred_t *cr = crref();
332     afs_uint32 junk;
333     int old_pag;
334     
335     old_pag = PagInCred(cr);
336     crfree(cr);
337     
338     code = (*sys32_setgroupsp)(gidsetsize, grouplist);
339     if (code) {
340         return code;
341     }
342     
343     cr = crref();
344     if (old_pag != NOPAG && PagInCred(cr) == NOPAG) {
345         /* re-install old pag if there's room. */
346         code = __setpag(&cr, old_pag, &junk, 0);
347     }
348     crfree(cr);
349     
350     /* Linux syscall ABI returns errno as negative */
351     return (-code);
352 }
353 #endif
354
355 #if defined(AFS_SPARC64_LINUX20_ENV) || defined(AFS_AMD64_LINUX20_ENV)
356 /* Intercept the uid16 system call as used by 32bit programs. */
357 #ifdef AFS_AMD64_LINUX20_ENV
358 extern asmlinkage long (*sys32_setgroupsp) (int gidsetsize, u16 * grouplist);
359 #endif /* AFS_AMD64_LINUX20_ENV */
360 #ifdef AFS_SPARC64_LINUX26_ENV
361 extern asmlinkage int (*sys32_setgroupsp) (int gidsetsize,
362                                            __kernel_gid32_t * grouplist);
363 #endif /* AFS_SPARC64_LINUX26_ENV */
364 asmlinkage long
365 afs32_xsetgroups(int gidsetsize, u16 * grouplist)
366 {
367     long code;
368     cred_t *cr = crref();
369     afs_uint32 junk;
370     int old_pag;
371     
372     old_pag = PagInCred(cr);
373     crfree(cr);
374     
375     code = (*sys32_setgroupsp) (gidsetsize, grouplist);
376     if (code) {
377         return code;
378     }
379     
380     cr = crref();
381     if (old_pag != NOPAG && PagInCred(cr) == NOPAG) {
382         /* re-install old pag if there's room. */
383         code = __setpag(&cr, old_pag, &junk, 0);
384     }
385     crfree(cr);
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 #ifdef AFS_AMD64_LINUX20_ENV
393 extern asmlinkage long (*sys32_setgroups32p) (int gidsetsize, gid_t * grouplist);
394 #endif /* AFS_AMD64_LINUX20_ENV */
395 #ifdef AFS_SPARC64_LINUX26_ENV
396 extern asmlinkage int (*sys32_setgroups32p) (int gidsetsize,
397                                              __kernel_gid32_t * grouplist);
398 #endif /* AFS_SPARC64_LINUX26_ENV */
399 asmlinkage long
400 afs32_xsetgroups32(int gidsetsize, gid_t * grouplist)
401 {
402     long code;
403     cred_t *cr = crref();
404     afs_uint32 junk;
405     int old_pag;
406
407     old_pag = PagInCred(cr);
408     crfree(cr);
409
410     code = (*sys32_setgroups32p) (gidsetsize, grouplist);
411     if (code) {
412         return code;
413     }
414
415     cr = crref();
416     if (old_pag != NOPAG && PagInCred(cr) == NOPAG) {
417         /* re-install old pag if there's room. */
418         code = __setpag(&cr, old_pag, &junk, 0);
419     }
420     crfree(cr);
421
422     /* Linux syscall ABI returns errno as negative */
423     return (-code);
424 }
425 #endif
426
427
428 #ifdef LINUX_KEYRING_SUPPORT
429 static void afs_pag_describe(const struct key *key, struct seq_file *m)
430 {
431     seq_puts(m, key->description);
432
433     seq_printf(m, ": %u", key->datalen);
434 }
435
436 static int afs_pag_instantiate(struct key *key, const void *data, size_t datalen)
437 {
438     int code;
439     afs_uint32 *userpag, pag = NOPAG;
440
441     if (key->uid != 0 || key->gid != 0)
442         return -EPERM;
443
444     code = -EINVAL;
445     get_group_info(current_group_info());
446
447     if (datalen != sizeof(afs_uint32) || !data)
448         goto error;
449
450     /* ensure key being set matches current pag */
451     pag = afs_linux_pag_from_groups(current_group_info());
452
453     if (pag == NOPAG)
454         goto error;
455
456     userpag = (afs_uint32 *) data;
457     if (*userpag != pag)
458         goto error;
459
460     key->payload.value = (unsigned long) *userpag;
461     key->datalen = sizeof(afs_uint32);
462     code = 0;
463
464 error:
465     put_group_info(current_group_info());
466     return code;
467 }
468
469 static int afs_pag_match(const struct key *key, const void *description)
470 {
471         return strcmp(key->description, description) == 0;
472 }
473
474 static void afs_pag_destroy(struct key *key)
475 {
476     afs_uint32 pag = key->payload.value;
477     int locked = ISAFS_GLOCK();
478
479     if (!locked)
480         AFS_GLOCK();
481
482     afs_MarkUserExpired(pag);
483
484     if (!locked)
485         AFS_GUNLOCK();
486 }
487
488 struct key_type key_type_afs_pag =
489 {
490     .name        = "afs_pag",
491     .describe    = afs_pag_describe,
492     .instantiate = afs_pag_instantiate,
493     .match       = afs_pag_match,
494     .destroy     = afs_pag_destroy,
495 };
496
497 #ifdef EXPORTED_TASKLIST_LOCK
498 extern rwlock_t tasklist_lock __attribute__((weak));
499 #endif
500
501 void osi_keyring_init(void)
502 {
503 #if !defined(EXPORTED_KEY_TYPE_KEYRING)
504     struct task_struct *p;
505
506     /* If we can't lock the tasklist, either with its explicit lock,
507      * or by using the RCU lock, then we can't safely work out the 
508      * type of a keyring. So, we have to rely on the weak reference. 
509      * If that's not available, then keyring based PAGs won't work.
510      */
511     
512 #if defined(EXPORTED_TASKLIST_LOCK) || (LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,16) && defined(HAVE_LINUX_RCU_READ_LOCK))
513     if (__key_type_keyring == NULL) {
514 # ifdef EXPORTED_TASKLIST_LOCK
515         if (&tasklist_lock)
516             read_lock(&tasklist_lock);
517 # endif
518 # if (LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,16) && defined(HAVE_LINUX_RCU_READ_LOCK))
519 #  ifdef EXPORTED_TASKLIST_LOCK
520         else
521 #  endif
522             rcu_read_lock();
523 # endif
524 #if defined(HAVE_LINUX_FIND_TASK_BY_PID)
525         p = find_task_by_pid(1);
526 #else
527         p = find_task_by_vpid(1);
528 #endif
529         if (p && task_user(p)->session_keyring)
530             __key_type_keyring = task_user(p)->session_keyring->type;
531 # ifdef EXPORTED_TASKLIST_LOCK
532         if (&tasklist_lock)
533             read_unlock(&tasklist_lock);
534 # endif
535 # if (LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,16) && defined(HAVE_LINUX_RCU_READ_LOCK))
536 #  ifdef EXPORTED_TASKLIST_LOCK
537         else
538 #  endif
539             rcu_read_unlock();
540 # endif
541     }
542 #endif
543 #endif
544
545     register_key_type(&key_type_afs_pag);
546 }
547
548 void osi_keyring_shutdown(void)
549 {
550     unregister_key_type(&key_type_afs_pag);
551 }
552
553 afs_int32
554 osi_get_keyring_pag(afs_ucred_t *cred)
555 {
556     struct key *key;
557     afs_uint32 newpag;
558     afs_int32 keyring_pag = NOPAG;
559
560     if (afs_cr_rgid(cred) != NFSXLATOR_CRED) {
561         key = afs_linux_search_keyring(cred, &key_type_afs_pag);
562
563         if (!IS_ERR(key)) {
564             if (key_validate(key) == 0 && key->uid == 0) {      /* also verify in the session keyring? */
565                 keyring_pag = key->payload.value;
566                 /* Only set PAG in groups if needed,
567                  * and the creds are from the current process */
568                 if (afs_linux_cred_is_current(cred) &&
569                     ((keyring_pag >> 24) & 0xff) == 'A' &&
570                     keyring_pag != afs_linux_pag_from_groups(current_group_info())) {
571                         __setpag(&cred, keyring_pag, &newpag, 0);
572                 }
573             }
574             key_put(key);
575         }
576     }
577     return keyring_pag;
578 }
579
580 #else
581 void osi_keyring_init(void)
582 {
583         return;
584 }
585
586 void osi_keyring_shutdown(void)
587 {
588         return;
589 }
590 #endif