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