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 <linux/smp_lock.h>
29 #ifdef AFS_LINUX26_ONEGROUP_ENV
30 #define NUMPAGGROUPS 1
32 #define NUMPAGGROUPS 2
36 afs_setgroups(cred_t **cr, struct group_info *group_info, int change_parent)
38 struct group_info *old_info;
40 AFS_STATCNT(afs_setgroups);
42 old_info = cr_group_info(*cr);
43 get_group_info(group_info);
44 set_cr_group_info(*cr, group_info);
45 put_group_info(old_info);
49 #if defined(STRUCT_TASK_STRUCT_HAS_PARENT) && !defined(STRUCT_TASK_HAS_CRED)
51 old_info = current->parent->group_info;
52 get_group_info(group_info);
53 current->parent->group_info = group_info;
54 put_group_info(old_info);
60 /* Returns number of groups. And we trust groups to be large enough to
61 * hold all the groups.
63 static struct group_info *
64 afs_getgroups(cred_t * cr)
66 AFS_STATCNT(afs_getgroups);
68 get_group_info(cr_group_info(cr));
69 return cr_group_info(cr);
73 __setpag(cred_t **cr, afs_uint32 pagvalue, afs_uint32 *newpag,
76 struct group_info *group_info;
77 #ifndef AFS_LINUX26_ONEGROUP_ENV
80 struct group_info *tmp;
82 #ifdef AFS_LINUX26_ONEGROUP_ENV
87 group_info = afs_getgroups(*cr);
88 if (group_info->ngroups < NUMPAGGROUPS
89 || afs_get_pag_from_groups(
90 #ifdef AFS_LINUX26_ONEGROUP_ENV
93 GROUP_AT(group_info, 0) ,GROUP_AT(group_info, 1)
96 /* We will have to make sure group_info is big enough for pag */
97 need_space = NUMPAGGROUPS;
99 tmp = groups_alloc(group_info->ngroups + need_space);
101 *newpag = (pagvalue == -1 ? genpag() : pagvalue);
102 #ifdef AFS_LINUX26_ONEGROUP_ENV
103 for (i = 0, j = 0; i < group_info->ngroups; ++i) {
104 int ths = GROUP_AT(group_info, i);
105 int last = i > 0 ? GROUP_AT(group_info, i-1) : 0;
106 if ((ths >> 24) == 'A')
108 if (last <= *newpag && ths > *newpag) {
109 GROUP_AT(tmp, j) = *newpag;
112 GROUP_AT(tmp, j) = ths;
115 if (j != i + need_space)
116 GROUP_AT(tmp, j) = *newpag;
118 for (i = 0; i < group_info->ngroups; ++i)
119 GROUP_AT(tmp, i + need_space) = GROUP_AT(group_info, i);
121 put_group_info(group_info);
124 #ifndef AFS_LINUX26_ONEGROUP_ENV
125 afs_get_groups_from_pag(*newpag, &g0, &g1);
126 GROUP_AT(group_info, 0) = g0;
127 GROUP_AT(group_info, 1) = g1;
130 afs_setgroups(cr, group_info, change_parent);
132 put_group_info(group_info);
137 #ifdef LINUX_KEYRING_SUPPORT
138 extern struct key_type key_type_keyring __attribute__((weak));
139 static struct key_type *__key_type_keyring = &key_type_keyring;
142 install_session_keyring(struct key *keyring)
146 unsigned long not_in_quota;
149 if (!__key_type_keyring)
154 /* create an empty session keyring */
155 not_in_quota = KEY_ALLOC_IN_QUOTA;
156 sprintf(desc, "_ses.%u", current->tgid);
158 #if defined(KEY_ALLOC_NEEDS_STRUCT_TASK)
159 keyring = key_alloc(__key_type_keyring, desc,
160 current_uid(), current_gid(), current,
161 (KEY_POS_ALL & ~KEY_POS_SETATTR) | KEY_USR_ALL,
163 #elif defined(KEY_ALLOC_NEEDS_CRED)
164 keyring = key_alloc(__key_type_keyring, desc,
165 current_uid(), current_gid(), current_cred(),
166 (KEY_POS_ALL & ~KEY_POS_SETATTR) | KEY_USR_ALL,
169 keyring = key_alloc(__key_type_keyring, desc,
170 current_uid(), current_gid(),
171 (KEY_POS_ALL & ~KEY_POS_SETATTR) | KEY_USR_ALL,
174 if (IS_ERR(keyring)) {
175 code = PTR_ERR(keyring);
180 code = key_instantiate_and_link(keyring, NULL, 0, NULL, NULL);
186 /* install the keyring */
187 spin_lock_irq(¤t->sighand->siglock);
188 old = task_session_keyring(current);
190 task_session_keyring(current) = keyring;
191 spin_unlock_irq(¤t->sighand->siglock);
199 #endif /* LINUX_KEYRING_SUPPORT */
202 setpag(cred_t **cr, afs_uint32 pagvalue, afs_uint32 *newpag,
209 code = __setpag(cr, pagvalue, newpag, change_parent);
211 #ifdef LINUX_KEYRING_SUPPORT
212 if (code == 0 && cr_rgid(*cr) != NFSXLATOR_CRED) {
213 (void) install_session_keyring(NULL);
215 if (current_session_keyring()) {
219 perm = KEY_POS_VIEW | KEY_POS_SEARCH;
220 perm |= KEY_USR_VIEW | KEY_USR_SEARCH;
222 #if defined(KEY_ALLOC_NEEDS_STRUCT_TASK)
223 key = key_alloc(&key_type_afs_pag, "_pag", 0, 0, current, perm, 1);
224 #elif defined(KEY_ALLOC_NEEDS_CRED)
225 key = key_alloc(&key_type_afs_pag, "_pag", 0, 0, current_cred(), perm, 1);
227 key = key_alloc(&key_type_afs_pag, "_pag", 0, 0, perm, 1);
231 key_instantiate_and_link(key, (void *) newpag, sizeof(afs_uint32),
232 current_session_keyring(), NULL);
237 #endif /* LINUX_KEYRING_SUPPORT */
243 /* Intercept the standard system call. */
244 extern asmlinkage long (*sys_setgroupsp) (int gidsetsize, gid_t * grouplist);
246 afs_xsetgroups(int gidsetsize, gid_t * grouplist)
249 cred_t *cr = crref();
254 old_pag = PagInCred(cr);
258 code = (*sys_setgroupsp) (gidsetsize, grouplist);
265 if (old_pag != NOPAG && PagInCred(cr) == NOPAG) {
266 /* re-install old pag if there's room. */
267 code = __setpag(&cr, old_pag, &junk, 0);
272 /* Linux syscall ABI returns errno as negative */
276 /* Intercept the standard uid32 system call. */
277 extern asmlinkage long (*sys_setgroups32p) (int gidsetsize, gid_t * grouplist);
279 afs_xsetgroups32(int gidsetsize, gid_t * grouplist)
282 cred_t *cr = crref();
287 old_pag = PagInCred(cr);
291 code = (*sys_setgroups32p) (gidsetsize, grouplist);
299 if (old_pag != NOPAG && PagInCred(cr) == NOPAG) {
300 /* re-install old pag if there's room. */
301 code = __setpag(&cr, old_pag, &junk, 0);
306 /* Linux syscall ABI returns errno as negative */
310 #if defined(AFS_PPC64_LINUX20_ENV)
311 /* Intercept the uid16 system call as used by 32bit programs. */
312 extern long (*sys32_setgroupsp)(int gidsetsize, gid_t *grouplist);
313 asmlinkage long afs32_xsetgroups(int gidsetsize, gid_t *grouplist)
316 cred_t *cr = crref();
321 old_pag = PagInCred(cr);
325 code = (*sys32_setgroupsp)(gidsetsize, grouplist);
332 if (old_pag != NOPAG && PagInCred(cr) == NOPAG) {
333 /* re-install old pag if there's room. */
334 code = __setpag(&cr, old_pag, &junk, 0);
339 /* Linux syscall ABI returns errno as negative */
344 #if defined(AFS_SPARC64_LINUX20_ENV) || defined(AFS_AMD64_LINUX20_ENV)
345 /* Intercept the uid16 system call as used by 32bit programs. */
346 extern long (*sys32_setgroupsp) (int gidsetsize, u16 * grouplist);
348 afs32_xsetgroups(int gidsetsize, u16 * grouplist)
351 cred_t *cr = crref();
356 old_pag = PagInCred(cr);
360 code = (*sys32_setgroupsp) (gidsetsize, grouplist);
367 if (old_pag != NOPAG && PagInCred(cr) == NOPAG) {
368 /* re-install old pag if there's room. */
369 code = __setpag(&cr, old_pag, &junk, 0);
374 /* Linux syscall ABI returns errno as negative */
378 /* Intercept the uid32 system call as used by 32bit programs. */
379 extern long (*sys32_setgroups32p) (int gidsetsize, gid_t * grouplist);
381 afs32_xsetgroups32(int gidsetsize, gid_t * grouplist)
384 cred_t *cr = crref();
389 old_pag = PagInCred(cr);
393 code = (*sys32_setgroups32p) (gidsetsize, grouplist);
400 if (old_pag != NOPAG && PagInCred(cr) == NOPAG) {
401 /* re-install old pag if there's room. */
402 code = __setpag(&cr, old_pag, &junk, 0);
407 /* Linux syscall ABI returns errno as negative */
413 #ifdef LINUX_KEYRING_SUPPORT
414 static void afs_pag_describe(const struct key *key, struct seq_file *m)
416 seq_puts(m, key->description);
418 seq_printf(m, ": %u", key->datalen);
421 static int afs_pag_instantiate(struct key *key, const void *data, size_t datalen)
424 afs_uint32 *userpag, pag = NOPAG;
425 #ifndef AFS_LINUX26_ONEGROUP_ENV
429 if (key->uid != 0 || key->gid != 0)
433 get_group_info(current_group_info());
435 if (datalen != sizeof(afs_uint32) || !data)
438 if (current_group_info()->ngroups < NUMPAGGROUPS)
441 /* ensure key being set matches current pag */
442 #ifdef AFS_LINUX26_ONEGROUP_ENV
443 pag = afs_get_pag_from_groups(current_group_info());
445 g0 = GROUP_AT(current_group_info(), 0);
446 g1 = GROUP_AT(current_group_info(), 1);
448 pag = afs_get_pag_from_groups(g0, g1);
453 userpag = (afs_uint32 *) data;
457 key->payload.value = (unsigned long) *userpag;
458 key->datalen = sizeof(afs_uint32);
462 put_group_info(current_group_info());
466 static int afs_pag_match(const struct key *key, const void *description)
468 return strcmp(key->description, description) == 0;
471 static void afs_pag_destroy(struct key *key)
473 afs_uint32 pag = key->payload.value;
475 int locked = ISAFS_GLOCK();
479 pu = afs_FindUser(pag, -1, READ_LOCK);
481 pu->ct.EndTimestamp = 0;
483 afs_PutUser(pu, READ_LOCK);
489 struct key_type key_type_afs_pag =
492 .describe = afs_pag_describe,
493 .instantiate = afs_pag_instantiate,
494 .match = afs_pag_match,
495 .destroy = afs_pag_destroy,
498 #ifdef EXPORTED_TASKLIST_LOCK
499 extern rwlock_t tasklist_lock __attribute__((weak));
502 void osi_keyring_init(void)
504 #if !defined(EXPORTED_KEY_TYPE_KEYRING)
505 struct task_struct *p;
507 /* If we can't lock the tasklist, either with its explicit lock,
508 * or by using the RCU lock, then we can't safely work out the
509 * type of a keyring. So, we have to rely on the weak reference.
510 * If that's not available, then keyring based PAGs won't work.
513 #if defined(EXPORTED_TASKLIST_LOCK) || (LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,16) && defined(EXPORTED_RCU_READ_LOCK))
514 if (__key_type_keyring == NULL) {
515 # ifdef EXPORTED_TASKLIST_LOCK
517 read_lock(&tasklist_lock);
519 # if (LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,16) && defined(EXPORTED_RCU_READ_LOCK))
520 # ifdef EXPORTED_TASKLIST_LOCK
525 #if defined(EXPORTED_FIND_TASK_BY_PID)
526 p = find_task_by_pid(1);
528 p = find_task_by_vpid(1);
530 if (p && task_user(p)->session_keyring)
531 __key_type_keyring = task_user(p)->session_keyring->type;
532 # ifdef EXPORTED_TASKLIST_LOCK
534 read_unlock(&tasklist_lock);
536 # if (LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,16) && defined(EXPORTED_RCU_READ_LOCK))
537 # ifdef EXPORTED_TASKLIST_LOCK
546 register_key_type(&key_type_afs_pag);
549 void osi_keyring_shutdown(void)
551 unregister_key_type(&key_type_afs_pag);
555 void osi_keyring_init(void)
560 void osi_keyring_shutdown(void)