2 * Copyright 2000, International Business Machines Corporation and others.
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
16 #include <afsconfig.h>
17 #include "afs/param.h"
18 #ifdef LINUX_KEYRING_SUPPORT
19 #include <linux/seq_file.h>
25 #include "afs/sysincludes.h"
26 #include "afsincludes.h"
27 #include "afs/afs_stats.h" /* statistics */
28 #include "afs/nfsclient.h"
29 #ifdef AFS_LINUX22_ENV
30 #include "h/smp_lock.h"
33 #ifdef AFS_LINUX26_ONEGROUP_ENV
34 #define NUMPAGGROUPS 1
36 #define NUMPAGGROUPS 2
39 #if defined(AFS_LINUX26_ENV)
41 afs_setgroups(cred_t **cr, struct group_info *group_info, int change_parent)
43 struct group_info *old_info;
45 AFS_STATCNT(afs_setgroups);
47 old_info = (*cr)->cr_group_info;
48 get_group_info(group_info);
49 (*cr)->cr_group_info = group_info;
50 put_group_info(old_info);
54 #if defined(STRUCT_TASK_STRUCT_HAS_PARENT) && !defined(STRUCT_TASK_HAS_CRED)
56 old_info = current->parent->group_info;
57 get_group_info(group_info);
58 current->parent->group_info = group_info;
59 put_group_info(old_info);
67 afs_setgroups(cred_t **cr, int ngroups, gid_t * gidset, int change_parent)
73 AFS_STATCNT(afs_setgroups);
75 if (ngroups > NGROUPS)
78 gp = (*cr)->cr_groups;
79 if (ngroups < NGROUPS)
80 gp[ngroups] = (gid_t) NOGROUP;
82 for (i = ngroups; i > 0; i--) {
86 (*cr)->cr_ngroups = ngroups;
92 #if defined(AFS_LINUX26_ENV)
93 static struct group_info *
94 afs_getgroups(cred_t * cr)
96 AFS_STATCNT(afs_getgroups);
98 get_group_info(cr->cr_group_info);
99 return cr->cr_group_info;
102 /* Returns number of groups. And we trust groups to be large enough to
103 * hold all the groups.
106 afs_getgroups(cred_t *cr, gid_t *groups)
112 AFS_STATCNT(afs_getgroups);
117 for (i = 0; (i < n) && (*gp != (gid_t) NOGROUP); i++)
123 #if !defined(AFS_LINUX26_ENV)
124 /* Only propogate the PAG to the parent process. Unix's propogate to
125 * all processes sharing the cred.
128 set_pag_in_parent(int pag, int g0, int g1)
131 #ifdef STRUCT_TASK_STRUCT_HAS_PARENT
132 gid_t *gp = current->parent->groups;
133 int ngroups = current->parent->ngroups;
135 gid_t *gp = current->p_pptr->groups;
136 int ngroups = current->p_pptr->ngroups;
139 if ((ngroups < 2) || (afs_get_pag_from_groups(gp[0], gp[1]) == NOPAG)) {
140 /* We will have to shift grouplist to make room for pag */
141 if (ngroups + 2 > NGROUPS) {
144 for (i = ngroups - 1; i >= 0; i--) {
151 if (ngroups < NGROUPS)
152 gp[ngroups] = NOGROUP;
154 #ifdef STRUCT_TASK_STRUCT_HAS_PARENT
155 current->parent->ngroups = ngroups;
157 current->p_pptr->ngroups = ngroups;
163 #if defined(AFS_LINUX26_ENV)
165 __setpag(cred_t **cr, afs_uint32 pagvalue, afs_uint32 *newpag,
168 struct group_info *group_info;
169 #ifndef AFS_LINUX26_ONEGROUP_ENV
172 struct group_info *tmp;
174 #ifdef AFS_LINUX26_ONEGROUP_ENV
179 group_info = afs_getgroups(*cr);
180 if (group_info->ngroups < NUMPAGGROUPS
181 || afs_get_pag_from_groups(
182 #ifdef AFS_LINUX26_ONEGROUP_ENV
185 GROUP_AT(group_info, 0) ,GROUP_AT(group_info, 1)
188 /* We will have to make sure group_info is big enough for pag */
189 need_space = NUMPAGGROUPS;
191 tmp = groups_alloc(group_info->ngroups + need_space);
193 *newpag = (pagvalue == -1 ? genpag() : pagvalue);
194 #ifdef AFS_LINUX26_ONEGROUP_ENV
195 for (i = 0, j = 0; i < group_info->ngroups; ++i) {
196 int ths = GROUP_AT(group_info, i);
197 int last = i > 0 ? GROUP_AT(group_info, i-1) : 0;
198 if ((ths >> 24) == 'A')
200 if (last <= *newpag && ths > *newpag) {
201 GROUP_AT(tmp, j) = *newpag;
204 GROUP_AT(tmp, j) = ths;
207 if (j != i + need_space)
208 GROUP_AT(tmp, j) = *newpag;
210 for (i = 0; i < group_info->ngroups; ++i)
211 GROUP_AT(tmp, i + need_space) = GROUP_AT(group_info, i);
213 put_group_info(group_info);
216 #ifndef AFS_LINUX26_ONEGROUP_ENV
217 afs_get_groups_from_pag(*newpag, &g0, &g1);
218 GROUP_AT(group_info, 0) = g0;
219 GROUP_AT(group_info, 1) = g1;
222 afs_setgroups(cr, group_info, change_parent);
224 put_group_info(group_info);
229 #ifdef LINUX_KEYRING_SUPPORT
230 extern struct key_type key_type_keyring __attribute__((weak));
231 static struct key_type *__key_type_keyring = &key_type_keyring;
234 install_session_keyring(struct task_struct *task, struct key *keyring)
238 unsigned long not_in_quota;
241 if (!__key_type_keyring)
246 /* create an empty session keyring */
247 not_in_quota = KEY_ALLOC_IN_QUOTA;
248 sprintf(desc, "_ses.%u", task->tgid);
250 #ifdef KEY_ALLOC_NEEDS_STRUCT_TASK
251 keyring = key_alloc(__key_type_keyring, desc,
252 task_uid(task), task_gid(task), task,
253 (KEY_POS_ALL & ~KEY_POS_SETATTR) | KEY_USR_ALL,
256 keyring = key_alloc(__key_type_keyring, desc,
257 task_uid(task), task_gid(task),
258 (KEY_POS_ALL & ~KEY_POS_SETATTR) | KEY_USR_ALL,
261 if (IS_ERR(keyring)) {
262 code = PTR_ERR(keyring);
267 code = key_instantiate_and_link(keyring, NULL, 0, NULL, NULL);
273 /* install the keyring */
274 spin_lock_irq(&task->sighand->siglock);
275 old = task_session_keyring(task);
277 task_session_keyring(task) = keyring;
278 spin_unlock_irq(&task->sighand->siglock);
286 #endif /* LINUX_KEYRING_SUPPORT */
290 __setpag(cred_t **cr, afs_uint32 pagvalue, afs_uint32 *newpag,
294 afs_int32 ngroups, code = 0;
297 gidset = (gid_t *) osi_Alloc(NGROUPS * sizeof(gidset[0]));
298 ngroups = afs_getgroups(*cr, gidset);
300 if (afs_get_pag_from_groups(gidset[0], gidset[1]) == NOPAG) {
301 /* We will have to shift grouplist to make room for pag */
302 if (ngroups + 2 > NGROUPS) {
303 osi_Free((char *)gidset, NGROUPS * sizeof(int));
306 for (j = ngroups - 1; j >= 0; j--) {
307 gidset[j + 2] = gidset[j];
311 *newpag = (pagvalue == -1 ? genpag() : pagvalue);
312 afs_get_groups_from_pag(*newpag, &gidset[0], &gidset[1]);
313 code = afs_setgroups(cr, ngroups, gidset, change_parent);
315 /* If change_parent is set, then we should set the pag in the parent as
318 if (change_parent && !code) {
319 code = set_pag_in_parent(*newpag, gidset[0], gidset[1]);
322 osi_Free((char *)gidset, NGROUPS * sizeof(int));
329 setpag(cred_t **cr, afs_uint32 pagvalue, afs_uint32 *newpag,
336 code = __setpag(cr, pagvalue, newpag, change_parent);
338 #ifdef LINUX_KEYRING_SUPPORT
339 if (code == 0 && (*cr)->cr_rgid != NFSXLATOR_CRED) {
340 (void) install_session_keyring(current, NULL);
342 if (current_session_keyring()) {
346 perm = KEY_POS_VIEW | KEY_POS_SEARCH;
347 perm |= KEY_USR_VIEW | KEY_USR_SEARCH;
349 #ifdef KEY_ALLOC_NEEDS_STRUCT_TASK
350 key = key_alloc(&key_type_afs_pag, "_pag", 0, 0, current, perm, 1);
352 key = key_alloc(&key_type_afs_pag, "_pag", 0, 0, perm, 1);
356 key_instantiate_and_link(key, (void *) newpag, sizeof(afs_uint32),
357 current_session_keyring(), NULL);
362 #endif /* LINUX_KEYRING_SUPPORT */
368 /* Intercept the standard system call. */
369 extern asmlinkage long (*sys_setgroupsp) (int gidsetsize, gid_t * grouplist);
371 afs_xsetgroups(int gidsetsize, gid_t * grouplist)
374 cred_t *cr = crref();
379 old_pag = PagInCred(cr);
383 code = (*sys_setgroupsp) (gidsetsize, grouplist);
390 if (old_pag != NOPAG && PagInCred(cr) == NOPAG) {
391 /* re-install old pag if there's room. */
392 code = __setpag(&cr, old_pag, &junk, 0);
397 /* Linux syscall ABI returns errno as negative */
401 #if defined(AFS_LINUX24_ENV)
402 /* Intercept the standard uid32 system call. */
403 extern asmlinkage long (*sys_setgroups32p) (int gidsetsize, gid_t * grouplist);
405 afs_xsetgroups32(int gidsetsize, gid_t * grouplist)
408 cred_t *cr = crref();
413 old_pag = PagInCred(cr);
417 code = (*sys_setgroups32p) (gidsetsize, grouplist);
425 if (old_pag != NOPAG && PagInCred(cr) == NOPAG) {
426 /* re-install old pag if there's room. */
427 code = __setpag(&cr, old_pag, &junk, 0);
432 /* Linux syscall ABI returns errno as negative */
437 #if defined(AFS_PPC64_LINUX20_ENV)
438 /* Intercept the uid16 system call as used by 32bit programs. */
439 extern long (*sys32_setgroupsp)(int gidsetsize, gid_t *grouplist);
440 asmlinkage long afs32_xsetgroups(int gidsetsize, gid_t *grouplist)
443 cred_t *cr = crref();
448 old_pag = PagInCred(cr);
452 code = (*sys32_setgroupsp)(gidsetsize, grouplist);
459 if (old_pag != NOPAG && PagInCred(cr) == NOPAG) {
460 /* re-install old pag if there's room. */
461 code = __setpag(&cr, old_pag, &junk, 0);
466 /* Linux syscall ABI returns errno as negative */
471 #if defined(AFS_SPARC64_LINUX20_ENV) || defined(AFS_AMD64_LINUX20_ENV)
472 /* Intercept the uid16 system call as used by 32bit programs. */
473 extern long (*sys32_setgroupsp) (int gidsetsize, u16 * grouplist);
475 afs32_xsetgroups(int gidsetsize, u16 * grouplist)
478 cred_t *cr = crref();
483 old_pag = PagInCred(cr);
487 code = (*sys32_setgroupsp) (gidsetsize, grouplist);
494 if (old_pag != NOPAG && PagInCred(cr) == NOPAG) {
495 /* re-install old pag if there's room. */
496 code = __setpag(&cr, old_pag, &junk, 0);
501 /* Linux syscall ABI returns errno as negative */
505 #ifdef AFS_LINUX24_ENV
506 /* Intercept the uid32 system call as used by 32bit programs. */
507 extern long (*sys32_setgroups32p) (int gidsetsize, gid_t * grouplist);
509 afs32_xsetgroups32(int gidsetsize, gid_t * grouplist)
512 cred_t *cr = crref();
517 old_pag = PagInCred(cr);
521 code = (*sys32_setgroups32p) (gidsetsize, grouplist);
528 if (old_pag != NOPAG && PagInCred(cr) == NOPAG) {
529 /* re-install old pag if there's room. */
530 code = __setpag(&cr, old_pag, &junk, 0);
535 /* Linux syscall ABI returns errno as negative */
542 #ifdef LINUX_KEYRING_SUPPORT
543 static void afs_pag_describe(const struct key *key, struct seq_file *m)
545 seq_puts(m, key->description);
547 seq_printf(m, ": %u", key->datalen);
550 static int afs_pag_instantiate(struct key *key, const void *data, size_t datalen)
553 afs_uint32 *userpag, pag = NOPAG;
554 #ifndef AFS_LINUX26_ONEGROUP_ENV
558 if (key->uid != 0 || key->gid != 0)
562 get_group_info(current_group_info());
564 if (datalen != sizeof(afs_uint32) || !data)
567 if (current_group_info()->ngroups < NUMPAGGROUPS)
570 /* ensure key being set matches current pag */
571 #ifdef AFS_LINUX26_ONEGROUP_ENV
572 pag = afs_get_pag_from_groups(current_group_info());
574 g0 = GROUP_AT(current_group_info(), 0);
575 g1 = GROUP_AT(current_group_info(), 1);
577 pag = afs_get_pag_from_groups(g0, g1);
582 userpag = (afs_uint32 *) data;
586 key->payload.value = (unsigned long) *userpag;
587 key->datalen = sizeof(afs_uint32);
591 put_group_info(current_group_info());
595 static int afs_pag_match(const struct key *key, const void *description)
597 return strcmp(key->description, description) == 0;
600 static void afs_pag_destroy(struct key *key)
602 afs_uint32 pag = key->payload.value;
604 int locked = ISAFS_GLOCK();
608 pu = afs_FindUser(pag, -1, READ_LOCK);
610 pu->ct.EndTimestamp = 0;
612 afs_PutUser(pu, READ_LOCK);
618 struct key_type key_type_afs_pag =
621 .describe = afs_pag_describe,
622 .instantiate = afs_pag_instantiate,
623 .match = afs_pag_match,
624 .destroy = afs_pag_destroy,
627 #ifdef EXPORTED_TASKLIST_LOCK
628 extern rwlock_t tasklist_lock __attribute__((weak));
631 void osi_keyring_init(void)
633 struct task_struct *p;
635 /* If we can't lock the tasklist, either with its explicit lock,
636 * or by using the RCU lock, then we can't safely work out the
637 * type of a keyring. So, we have to rely on the weak reference.
638 * If that's not available, then keyring based PAGs won't work.
641 #if defined(EXPORTED_TASKLIST_LOCK) || (LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,16) && defined(EXPORTED_RCU_READ_LOCK))
642 if (__key_type_keyring == NULL) {
643 # ifdef EXPORTED_TASKLIST_LOCK
645 read_lock(&tasklist_lock);
647 # if (LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,16) && defined(EXPORTED_RCU_READ_LOCK))
648 # ifdef EXPORTED_TASKLIST_LOCK
653 #if defined(EXPORTED_FIND_TASK_BY_PID)
654 p = find_task_by_pid(1);
656 p = find_task_by_vpid(1);
658 if (p && task_user(p)->session_keyring)
659 __key_type_keyring = task_user(p)->session_keyring->type;
660 # ifdef EXPORTED_TASKLIST_LOCK
662 read_unlock(&tasklist_lock);
664 # if (LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,16) && defined(EXPORTED_RCU_READ_LOCK))
665 # ifdef EXPORTED_TASKLIST_LOCK
673 register_key_type(&key_type_afs_pag);
676 void osi_keyring_shutdown(void)
678 unregister_key_type(&key_type_afs_pag);
682 void osi_keyring_init(void)
687 void osi_keyring_shutdown(void)