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 #include "osi_compat.h"
29 #include <linux/smp_lock.h>
31 #ifdef AFS_LINUX26_ONEGROUP_ENV
32 #define NUMPAGGROUPS 1
34 #define NUMPAGGROUPS 2
38 afs_setgroups(cred_t **cr, struct group_info *group_info, int change_parent)
40 struct group_info *old_info;
42 AFS_STATCNT(afs_setgroups);
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);
51 #if defined(STRUCT_TASK_STRUCT_HAS_PARENT) && !defined(STRUCT_TASK_HAS_CRED)
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);
62 /* Returns number of groups. And we trust groups to be large enough to
63 * hold all the groups.
65 static struct group_info *
66 afs_getgroups(cred_t * cr)
68 AFS_STATCNT(afs_getgroups);
70 get_group_info(afs_cr_group_info(cr));
71 return afs_cr_group_info(cr);
75 __setpag(cred_t **cr, afs_uint32 pagvalue, afs_uint32 *newpag,
78 struct group_info *group_info;
79 #ifndef AFS_LINUX26_ONEGROUP_ENV
82 struct group_info *tmp;
84 #ifdef AFS_LINUX26_ONEGROUP_ENV
89 group_info = afs_getgroups(*cr);
90 if (group_info->ngroups < NUMPAGGROUPS
91 || afs_get_pag_from_groups(
92 #ifdef AFS_LINUX26_ONEGROUP_ENV
95 GROUP_AT(group_info, 0) ,GROUP_AT(group_info, 1)
98 /* We will have to make sure group_info is big enough for pag */
99 need_space = NUMPAGGROUPS;
101 tmp = groups_alloc(group_info->ngroups + need_space);
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')
110 if (last <= *newpag && ths > *newpag) {
111 GROUP_AT(tmp, j) = *newpag;
114 GROUP_AT(tmp, j) = ths;
117 if (j != i + need_space)
118 GROUP_AT(tmp, j) = *newpag;
120 for (i = 0; i < group_info->ngroups; ++i)
121 GROUP_AT(tmp, i + need_space) = GROUP_AT(group_info, i);
123 put_group_info(group_info);
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;
132 afs_setgroups(cr, group_info, change_parent);
134 put_group_info(group_info);
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;
144 install_session_keyring(struct key *keyring)
150 if (!__key_type_keyring)
155 /* create an empty session keyring */
156 sprintf(desc, "_ses.%u", current->tgid);
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,
164 if (IS_ERR(keyring)) {
165 code = PTR_ERR(keyring);
170 code = key_instantiate_and_link(keyring, NULL, 0, NULL, NULL);
176 /* install the keyring */
177 spin_lock_irq(¤t->sighand->siglock);
178 old = task_session_keyring(current);
180 task_session_keyring(current) = keyring;
181 spin_unlock_irq(¤t->sighand->siglock);
189 #endif /* LINUX_KEYRING_SUPPORT */
192 setpag(cred_t **cr, afs_uint32 pagvalue, afs_uint32 *newpag,
199 code = __setpag(cr, pagvalue, newpag, change_parent);
201 #ifdef LINUX_KEYRING_SUPPORT
202 if (code == 0 && afs_cr_rgid(*cr) != NFSXLATOR_CRED) {
203 (void) install_session_keyring(NULL);
205 if (current_session_keyring()) {
209 perm = KEY_POS_VIEW | KEY_POS_SEARCH;
210 perm |= KEY_USR_VIEW | KEY_USR_SEARCH;
212 key = afs_linux_key_alloc(&key_type_afs_pag, "_pag", 0, 0, perm, 1);
215 key_instantiate_and_link(key, (void *) newpag, sizeof(afs_uint32),
216 current_session_keyring(), NULL);
221 #endif /* LINUX_KEYRING_SUPPORT */
227 /* Intercept the standard system call. */
228 extern asmlinkage long (*sys_setgroupsp) (int gidsetsize, gid_t * grouplist);
230 afs_xsetgroups(int gidsetsize, gid_t * grouplist)
233 cred_t *cr = crref();
238 old_pag = PagInCred(cr);
242 code = (*sys_setgroupsp) (gidsetsize, grouplist);
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);
256 /* Linux syscall ABI returns errno as negative */
260 /* Intercept the standard uid32 system call. */
261 extern asmlinkage long (*sys_setgroups32p) (int gidsetsize, gid_t * grouplist);
263 afs_xsetgroups32(int gidsetsize, gid_t * grouplist)
266 cred_t *cr = crref();
271 old_pag = PagInCred(cr);
275 code = (*sys_setgroups32p) (gidsetsize, grouplist);
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);
290 /* Linux syscall ABI returns errno as negative */
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)
300 cred_t *cr = crref();
305 old_pag = PagInCred(cr);
309 code = (*sys32_setgroupsp)(gidsetsize, grouplist);
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);
323 /* Linux syscall ABI returns errno as negative */
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);
332 afs32_xsetgroups(int gidsetsize, u16 * grouplist)
335 cred_t *cr = crref();
340 old_pag = PagInCred(cr);
344 code = (*sys32_setgroupsp) (gidsetsize, grouplist);
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);
358 /* Linux syscall ABI returns errno as negative */
362 /* Intercept the uid32 system call as used by 32bit programs. */
363 extern long (*sys32_setgroups32p) (int gidsetsize, gid_t * grouplist);
365 afs32_xsetgroups32(int gidsetsize, gid_t * grouplist)
368 cred_t *cr = crref();
373 old_pag = PagInCred(cr);
377 code = (*sys32_setgroups32p) (gidsetsize, grouplist);
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);
391 /* Linux syscall ABI returns errno as negative */
397 #ifdef LINUX_KEYRING_SUPPORT
398 static void afs_pag_describe(const struct key *key, struct seq_file *m)
400 seq_puts(m, key->description);
402 seq_printf(m, ": %u", key->datalen);
405 static int afs_pag_instantiate(struct key *key, const void *data, size_t datalen)
408 afs_uint32 *userpag, pag = NOPAG;
409 #ifndef AFS_LINUX26_ONEGROUP_ENV
413 if (key->uid != 0 || key->gid != 0)
417 get_group_info(current_group_info());
419 if (datalen != sizeof(afs_uint32) || !data)
422 if (current_group_info()->ngroups < NUMPAGGROUPS)
425 /* ensure key being set matches current pag */
426 #ifdef AFS_LINUX26_ONEGROUP_ENV
427 pag = afs_get_pag_from_groups(current_group_info());
429 g0 = GROUP_AT(current_group_info(), 0);
430 g1 = GROUP_AT(current_group_info(), 1);
432 pag = afs_get_pag_from_groups(g0, g1);
437 userpag = (afs_uint32 *) data;
441 key->payload.value = (unsigned long) *userpag;
442 key->datalen = sizeof(afs_uint32);
446 put_group_info(current_group_info());
450 static int afs_pag_match(const struct key *key, const void *description)
452 return strcmp(key->description, description) == 0;
455 static void afs_pag_destroy(struct key *key)
457 afs_uint32 pag = key->payload.value;
459 int locked = ISAFS_GLOCK();
463 pu = afs_FindUser(pag, -1, READ_LOCK);
465 pu->ct.EndTimestamp = 0;
467 afs_PutUser(pu, READ_LOCK);
473 struct key_type key_type_afs_pag =
476 .describe = afs_pag_describe,
477 .instantiate = afs_pag_instantiate,
478 .match = afs_pag_match,
479 .destroy = afs_pag_destroy,
482 #ifdef EXPORTED_TASKLIST_LOCK
483 extern rwlock_t tasklist_lock __attribute__((weak));
486 void osi_keyring_init(void)
488 #if !defined(EXPORTED_KEY_TYPE_KEYRING)
489 struct task_struct *p;
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.
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
501 read_lock(&tasklist_lock);
503 # if (LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,16) && defined(EXPORTED_RCU_READ_LOCK))
504 # ifdef EXPORTED_TASKLIST_LOCK
509 #if defined(EXPORTED_FIND_TASK_BY_PID)
510 p = find_task_by_pid(1);
512 p = find_task_by_vpid(1);
514 if (p && task_user(p)->session_keyring)
515 __key_type_keyring = task_user(p)->session_keyring->type;
516 # ifdef EXPORTED_TASKLIST_LOCK
518 read_unlock(&tasklist_lock);
520 # if (LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,16) && defined(EXPORTED_RCU_READ_LOCK))
521 # ifdef EXPORTED_TASKLIST_LOCK
530 register_key_type(&key_type_afs_pag);
533 void osi_keyring_shutdown(void)
535 unregister_key_type(&key_type_afs_pag);
539 osi_get_keyring_pag(afs_ucred_t *cred)
543 afs_int32 keyring_pag = NOPAG;
545 if (afs_cr_rgid(cred) != NFSXLATOR_CRED) {
546 key = afs_linux_search_keyring(cred, &key_type_afs_pag);
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);
566 void osi_keyring_init(void)
571 void osi_keyring_shutdown(void)