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>
23 #include "afs/sysincludes.h"
24 #include "afsincludes.h"
25 #include "afs/afs_stats.h" /* statistics */
26 #include "afs/nfsclient.h"
27 #ifdef AFS_LINUX22_ENV
28 #include "h/smp_lock.h"
31 #ifdef AFS_LINUX26_ONEGROUP_ENV
32 #define NUMPAGGROUPS 1
34 #define NUMPAGGROUPS 2
37 #if defined(AFS_LINUX26_ENV)
39 afs_setgroups(cred_t **cr, struct group_info *group_info, int change_parent)
41 struct group_info *old_info;
43 AFS_STATCNT(afs_setgroups);
45 old_info = (*cr)->cr_group_info;
46 get_group_info(group_info);
47 (*cr)->cr_group_info = group_info;
48 put_group_info(old_info);
52 #if defined(STRUCT_TASK_STRUCT_HAS_PARENT) && !defined(STRUCT_TASK_HAS_CRED)
54 old_info = current->parent->group_info;
55 get_group_info(group_info);
56 current->parent->group_info = group_info;
57 put_group_info(old_info);
65 afs_setgroups(cred_t **cr, int ngroups, gid_t * gidset, int change_parent)
71 AFS_STATCNT(afs_setgroups);
73 if (ngroups > NGROUPS)
76 gp = (*cr)->cr_groups;
77 if (ngroups < NGROUPS)
78 gp[ngroups] = (gid_t) NOGROUP;
80 for (i = ngroups; i > 0; i--) {
84 (*cr)->cr_ngroups = ngroups;
90 #if defined(AFS_LINUX26_ENV)
91 static struct group_info *
92 afs_getgroups(cred_t * cr)
94 AFS_STATCNT(afs_getgroups);
96 get_group_info(cr->cr_group_info);
97 return cr->cr_group_info;
100 /* Returns number of groups. And we trust groups to be large enough to
101 * hold all the groups.
104 afs_getgroups(cred_t *cr, gid_t *groups)
110 AFS_STATCNT(afs_getgroups);
115 for (i = 0; (i < n) && (*gp != (gid_t) NOGROUP); i++)
121 #if !defined(AFS_LINUX26_ENV)
122 /* Only propogate the PAG to the parent process. Unix's propogate to
123 * all processes sharing the cred.
126 set_pag_in_parent(int pag, int g0, int g1)
129 #ifdef STRUCT_TASK_STRUCT_HAS_PARENT
130 gid_t *gp = current->parent->groups;
131 int ngroups = current->parent->ngroups;
133 gid_t *gp = current->p_pptr->groups;
134 int ngroups = current->p_pptr->ngroups;
137 if ((ngroups < 2) || (afs_get_pag_from_groups(gp[0], gp[1]) == NOPAG)) {
138 /* We will have to shift grouplist to make room for pag */
139 if (ngroups + 2 > NGROUPS) {
142 for (i = ngroups - 1; i >= 0; i--) {
149 if (ngroups < NGROUPS)
150 gp[ngroups] = NOGROUP;
152 #ifdef STRUCT_TASK_STRUCT_HAS_PARENT
153 current->parent->ngroups = ngroups;
155 current->p_pptr->ngroups = ngroups;
161 #if defined(AFS_LINUX26_ENV)
163 __setpag(cred_t **cr, afs_uint32 pagvalue, afs_uint32 *newpag,
166 struct group_info *group_info;
167 #ifndef AFS_LINUX26_ONEGROUP_ENV
170 struct group_info *tmp;
172 #ifdef AFS_LINUX26_ONEGROUP_ENV
177 group_info = afs_getgroups(*cr);
178 if (group_info->ngroups < NUMPAGGROUPS
179 || afs_get_pag_from_groups(
180 #ifdef AFS_LINUX26_ONEGROUP_ENV
183 GROUP_AT(group_info, 0) ,GROUP_AT(group_info, 1)
186 /* We will have to make sure group_info is big enough for pag */
187 need_space = NUMPAGGROUPS;
189 tmp = groups_alloc(group_info->ngroups + need_space);
191 *newpag = (pagvalue == -1 ? genpag() : pagvalue);
192 #ifdef AFS_LINUX26_ONEGROUP_ENV
193 for (i = 0, j = 0; i < group_info->ngroups; ++i) {
194 int ths = GROUP_AT(group_info, i);
195 int last = i > 0 ? GROUP_AT(group_info, i-1) : 0;
196 if ((ths >> 24) == 'A')
198 if (last <= *newpag && ths > *newpag) {
199 GROUP_AT(tmp, j) = *newpag;
202 GROUP_AT(tmp, j) = ths;
205 if (j != i + need_space)
206 GROUP_AT(tmp, j) = *newpag;
208 for (i = 0; i < group_info->ngroups; ++i)
209 GROUP_AT(tmp, i + need_space) = GROUP_AT(group_info, i);
211 put_group_info(group_info);
214 #ifndef AFS_LINUX26_ONEGROUP_ENV
215 afs_get_groups_from_pag(*newpag, &g0, &g1);
216 GROUP_AT(group_info, 0) = g0;
217 GROUP_AT(group_info, 1) = g1;
220 afs_setgroups(cr, group_info, change_parent);
222 put_group_info(group_info);
227 #ifdef LINUX_KEYRING_SUPPORT
228 extern struct key_type key_type_keyring __attribute__((weak));
229 static struct key_type *__key_type_keyring = &key_type_keyring;
232 install_session_keyring(struct key *keyring)
236 unsigned long not_in_quota;
239 if (!__key_type_keyring)
244 /* create an empty session keyring */
245 not_in_quota = KEY_ALLOC_IN_QUOTA;
246 sprintf(desc, "_ses.%u", current->tgid);
248 #if defined(KEY_ALLOC_NEEDS_STRUCT_TASK)
249 keyring = key_alloc(__key_type_keyring, desc,
250 current_uid(), current_gid(), current,
251 (KEY_POS_ALL & ~KEY_POS_SETATTR) | KEY_USR_ALL,
253 #elif defined(KEY_ALLOC_NEEDS_CRED)
254 keyring = key_alloc(__key_type_keyring, desc,
255 current_uid(), current_gid(), current_cred(),
256 (KEY_POS_ALL & ~KEY_POS_SETATTR) | KEY_USR_ALL,
259 keyring = key_alloc(__key_type_keyring, desc,
260 current_uid(), current_gid(),
261 (KEY_POS_ALL & ~KEY_POS_SETATTR) | KEY_USR_ALL,
264 if (IS_ERR(keyring)) {
265 code = PTR_ERR(keyring);
270 code = key_instantiate_and_link(keyring, NULL, 0, NULL, NULL);
276 /* install the keyring */
277 spin_lock_irq(¤t->sighand->siglock);
278 old = task_session_keyring(current);
280 task_session_keyring(current) = keyring;
281 spin_unlock_irq(¤t->sighand->siglock);
289 #endif /* LINUX_KEYRING_SUPPORT */
293 __setpag(cred_t **cr, afs_uint32 pagvalue, afs_uint32 *newpag,
297 afs_int32 ngroups, code = 0;
300 gidset = (gid_t *) osi_Alloc(NGROUPS * sizeof(gidset[0]));
301 ngroups = afs_getgroups(*cr, gidset);
303 if (afs_get_pag_from_groups(gidset[0], gidset[1]) == NOPAG) {
304 /* We will have to shift grouplist to make room for pag */
305 if (ngroups + 2 > NGROUPS) {
306 osi_Free((char *)gidset, NGROUPS * sizeof(int));
309 for (j = ngroups - 1; j >= 0; j--) {
310 gidset[j + 2] = gidset[j];
314 *newpag = (pagvalue == -1 ? genpag() : pagvalue);
315 afs_get_groups_from_pag(*newpag, &gidset[0], &gidset[1]);
316 code = afs_setgroups(cr, ngroups, gidset, change_parent);
318 /* If change_parent is set, then we should set the pag in the parent as
321 if (change_parent && !code) {
322 code = set_pag_in_parent(*newpag, gidset[0], gidset[1]);
325 osi_Free((char *)gidset, NGROUPS * sizeof(int));
332 setpag(cred_t **cr, afs_uint32 pagvalue, afs_uint32 *newpag,
339 code = __setpag(cr, pagvalue, newpag, change_parent);
341 #ifdef LINUX_KEYRING_SUPPORT
342 if (code == 0 && (*cr)->cr_rgid != NFSXLATOR_CRED) {
343 (void) install_session_keyring(NULL);
345 if (current_session_keyring()) {
349 perm = KEY_POS_VIEW | KEY_POS_SEARCH;
350 perm |= KEY_USR_VIEW | KEY_USR_SEARCH;
352 #if defined(KEY_ALLOC_NEEDS_STRUCT_TASK)
353 key = key_alloc(&key_type_afs_pag, "_pag", 0, 0, current, perm, 1);
354 #elif defined(KEY_ALLOC_NEEDS_CRED)
355 key = key_alloc(&key_type_afs_pag, "_pag", 0, 0, current_cred(), perm, 1);
357 key = key_alloc(&key_type_afs_pag, "_pag", 0, 0, perm, 1);
361 key_instantiate_and_link(key, (void *) newpag, sizeof(afs_uint32),
362 current_session_keyring(), NULL);
367 #endif /* LINUX_KEYRING_SUPPORT */
373 /* Intercept the standard system call. */
374 extern asmlinkage long (*sys_setgroupsp) (int gidsetsize, gid_t * grouplist);
376 afs_xsetgroups(int gidsetsize, gid_t * grouplist)
379 cred_t *cr = crref();
384 old_pag = PagInCred(cr);
388 code = (*sys_setgroupsp) (gidsetsize, grouplist);
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);
402 /* Linux syscall ABI returns errno as negative */
406 #if defined(AFS_LINUX24_ENV)
407 /* Intercept the standard uid32 system call. */
408 extern asmlinkage long (*sys_setgroups32p) (int gidsetsize, gid_t * grouplist);
410 afs_xsetgroups32(int gidsetsize, gid_t * grouplist)
413 cred_t *cr = crref();
418 old_pag = PagInCred(cr);
422 code = (*sys_setgroups32p) (gidsetsize, grouplist);
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);
437 /* Linux syscall ABI returns errno as negative */
442 #if defined(AFS_PPC64_LINUX20_ENV)
443 /* Intercept the uid16 system call as used by 32bit programs. */
444 extern long (*sys32_setgroupsp)(int gidsetsize, gid_t *grouplist);
445 asmlinkage long afs32_xsetgroups(int gidsetsize, gid_t *grouplist)
448 cred_t *cr = crref();
453 old_pag = PagInCred(cr);
457 code = (*sys32_setgroupsp)(gidsetsize, grouplist);
464 if (old_pag != NOPAG && PagInCred(cr) == NOPAG) {
465 /* re-install old pag if there's room. */
466 code = __setpag(&cr, old_pag, &junk, 0);
471 /* Linux syscall ABI returns errno as negative */
476 #if defined(AFS_SPARC64_LINUX20_ENV) || defined(AFS_AMD64_LINUX20_ENV)
477 /* Intercept the uid16 system call as used by 32bit programs. */
478 extern long (*sys32_setgroupsp) (int gidsetsize, u16 * grouplist);
480 afs32_xsetgroups(int gidsetsize, u16 * grouplist)
483 cred_t *cr = crref();
488 old_pag = PagInCred(cr);
492 code = (*sys32_setgroupsp) (gidsetsize, grouplist);
499 if (old_pag != NOPAG && PagInCred(cr) == NOPAG) {
500 /* re-install old pag if there's room. */
501 code = __setpag(&cr, old_pag, &junk, 0);
506 /* Linux syscall ABI returns errno as negative */
510 #ifdef AFS_LINUX24_ENV
511 /* Intercept the uid32 system call as used by 32bit programs. */
512 extern long (*sys32_setgroups32p) (int gidsetsize, gid_t * grouplist);
514 afs32_xsetgroups32(int gidsetsize, gid_t * grouplist)
517 cred_t *cr = crref();
522 old_pag = PagInCred(cr);
526 code = (*sys32_setgroups32p) (gidsetsize, grouplist);
533 if (old_pag != NOPAG && PagInCred(cr) == NOPAG) {
534 /* re-install old pag if there's room. */
535 code = __setpag(&cr, old_pag, &junk, 0);
540 /* Linux syscall ABI returns errno as negative */
547 #ifdef LINUX_KEYRING_SUPPORT
548 static void afs_pag_describe(const struct key *key, struct seq_file *m)
550 seq_puts(m, key->description);
552 seq_printf(m, ": %u", key->datalen);
555 static int afs_pag_instantiate(struct key *key, const void *data, size_t datalen)
558 afs_uint32 *userpag, pag = NOPAG;
559 #ifndef AFS_LINUX26_ONEGROUP_ENV
563 if (key->uid != 0 || key->gid != 0)
567 get_group_info(current_group_info());
569 if (datalen != sizeof(afs_uint32) || !data)
572 if (current_group_info()->ngroups < NUMPAGGROUPS)
575 /* ensure key being set matches current pag */
576 #ifdef AFS_LINUX26_ONEGROUP_ENV
577 pag = afs_get_pag_from_groups(current_group_info());
579 g0 = GROUP_AT(current_group_info(), 0);
580 g1 = GROUP_AT(current_group_info(), 1);
582 pag = afs_get_pag_from_groups(g0, g1);
587 userpag = (afs_uint32 *) data;
591 key->payload.value = (unsigned long) *userpag;
592 key->datalen = sizeof(afs_uint32);
596 put_group_info(current_group_info());
600 static int afs_pag_match(const struct key *key, const void *description)
602 return strcmp(key->description, description) == 0;
605 static void afs_pag_destroy(struct key *key)
607 afs_uint32 pag = key->payload.value;
609 int locked = ISAFS_GLOCK();
613 pu = afs_FindUser(pag, -1, READ_LOCK);
615 pu->ct.EndTimestamp = 0;
617 afs_PutUser(pu, READ_LOCK);
623 struct key_type key_type_afs_pag =
626 .describe = afs_pag_describe,
627 .instantiate = afs_pag_instantiate,
628 .match = afs_pag_match,
629 .destroy = afs_pag_destroy,
632 #ifdef EXPORTED_TASKLIST_LOCK
633 extern rwlock_t tasklist_lock __attribute__((weak));
636 void osi_keyring_init(void)
638 #if !defined(EXPORTED_KEY_TYPE_KEYRING)
639 struct task_struct *p;
641 /* If we can't lock the tasklist, either with its explicit lock,
642 * or by using the RCU lock, then we can't safely work out the
643 * type of a keyring. So, we have to rely on the weak reference.
644 * If that's not available, then keyring based PAGs won't work.
647 #if defined(EXPORTED_TASKLIST_LOCK) || (LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,16) && defined(EXPORTED_RCU_READ_LOCK))
648 if (__key_type_keyring == NULL) {
649 # ifdef EXPORTED_TASKLIST_LOCK
651 read_lock(&tasklist_lock);
653 # if (LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,16) && defined(EXPORTED_RCU_READ_LOCK))
654 # ifdef EXPORTED_TASKLIST_LOCK
659 #if defined(EXPORTED_FIND_TASK_BY_PID)
660 p = find_task_by_pid(1);
662 p = find_task_by_vpid(1);
664 if (p && task_user(p)->session_keyring)
665 __key_type_keyring = task_user(p)->session_keyring->type;
666 # ifdef EXPORTED_TASKLIST_LOCK
668 read_unlock(&tasklist_lock);
670 # if (LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,16) && defined(EXPORTED_RCU_READ_LOCK))
671 # ifdef EXPORTED_TASKLIST_LOCK
680 register_key_type(&key_type_afs_pag);
683 void osi_keyring_shutdown(void)
685 unregister_key_type(&key_type_afs_pag);
689 void osi_keyring_init(void)
694 void osi_keyring_shutdown(void)