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>
21 #include <linux/key-type.h>
28 #include "afs/sysincludes.h"
29 #include "afsincludes.h"
30 #include "afs/afs_stats.h" /* statistics */
31 #include "afs/nfsclient.h"
32 #ifdef AFS_LINUX22_ENV
33 #include "h/smp_lock.h"
36 #ifdef AFS_LINUX26_ONEGROUP_ENV
37 #define NUMPAGGROUPS 1
39 #define NUMPAGGROUPS 2
42 #if defined(AFS_LINUX26_ENV)
44 afs_setgroups(cred_t **cr, struct group_info *group_info, int change_parent)
46 struct group_info *old_info;
48 AFS_STATCNT(afs_setgroups);
50 old_info = (*cr)->cr_group_info;
51 get_group_info(group_info);
52 (*cr)->cr_group_info = group_info;
53 put_group_info(old_info);
58 old_info = current->parent->group_info;
59 get_group_info(group_info);
60 current->parent->group_info = group_info;
61 put_group_info(old_info);
68 afs_setgroups(cred_t **cr, int ngroups, gid_t * gidset, int change_parent)
74 AFS_STATCNT(afs_setgroups);
76 if (ngroups > NGROUPS)
79 gp = (*cr)->cr_groups;
80 if (ngroups < NGROUPS)
81 gp[ngroups] = (gid_t) NOGROUP;
83 for (i = ngroups; i > 0; i--) {
87 (*cr)->cr_ngroups = ngroups;
93 #if defined(AFS_LINUX26_ENV)
94 static struct group_info *
95 afs_getgroups(cred_t * cr)
97 AFS_STATCNT(afs_getgroups);
99 get_group_info(cr->cr_group_info);
100 return cr->cr_group_info;
103 /* Returns number of groups. And we trust groups to be large enough to
104 * hold all the groups.
107 afs_getgroups(cred_t *cr, gid_t *groups)
113 AFS_STATCNT(afs_getgroups);
118 for (i = 0; (i < n) && (*gp != (gid_t) NOGROUP); i++)
124 #if !defined(AFS_LINUX26_ENV)
125 /* Only propogate the PAG to the parent process. Unix's propogate to
126 * all processes sharing the cred.
129 set_pag_in_parent(int pag, int g0, int g1)
132 #ifdef STRUCT_TASK_STRUCT_HAS_PARENT
133 gid_t *gp = current->parent->groups;
134 int ngroups = current->parent->ngroups;
136 gid_t *gp = current->p_pptr->groups;
137 int ngroups = current->p_pptr->ngroups;
140 if ((ngroups < 2) || (afs_get_pag_from_groups(gp[0], gp[1]) == NOPAG)) {
141 /* We will have to shift grouplist to make room for pag */
142 if (ngroups + 2 > NGROUPS) {
145 for (i = ngroups - 1; i >= 0; i--) {
152 if (ngroups < NGROUPS)
153 gp[ngroups] = NOGROUP;
155 #ifdef STRUCT_TASK_STRUCT_HAS_PARENT
156 current->parent->ngroups = ngroups;
158 current->p_pptr->ngroups = ngroups;
164 #if defined(AFS_LINUX26_ENV)
166 __setpag(cred_t **cr, afs_uint32 pagvalue, afs_uint32 *newpag,
169 struct group_info *group_info;
171 struct group_info *tmp;
173 #ifdef AFS_LINUX26_ONEGROUP_ENV
178 group_info = afs_getgroups(*cr);
179 if (group_info->ngroups < NUMPAGGROUPS
180 || afs_get_pag_from_groups(
181 #ifdef AFS_LINUX26_ONEGROUP_ENV
184 GROUP_AT(group_info, 0) ,GROUP_AT(group_info, 1)
187 /* We will have to make sure group_info is big enough for pag */
188 need_space = NUMPAGGROUPS;
190 tmp = groups_alloc(group_info->ngroups + need_space);
192 *newpag = (pagvalue == -1 ? genpag() : pagvalue);
193 #ifdef AFS_LINUX26_ONEGROUP_ENV
194 for (i = 0, j = 0; i < group_info->ngroups; ++i) {
195 int ths = GROUP_AT(group_info, i);
196 int last = i > 0 ? GROUP_AT(group_info, i-1) : 0;
197 if ((ths >> 24) == 'A')
199 if (last <= *newpag && ths > *newpag) {
200 GROUP_AT(tmp, j) = *newpag;
203 GROUP_AT(tmp, j) = ths;
206 if (j != i + need_space)
207 GROUP_AT(tmp, j) = *newpag;
209 for (i = 0; i < group_info->ngroups; ++i)
210 GROUP_AT(tmp, i + need_space) = GROUP_AT(group_info, i);
212 put_group_info(group_info);
215 #ifndef AFS_LINUX26_ONEGROUP_ENV
216 afs_get_groups_from_pag(*newpag, &g0, &g1);
217 GROUP_AT(group_info, 0) = g0;
218 GROUP_AT(group_info, 1) = g1;
221 afs_setgroups(cr, group_info, change_parent);
223 put_group_info(group_info);
228 #ifdef LINUX_KEYRING_SUPPORT
229 static struct key_type *__key_type_keyring;
232 install_session_keyring(struct task_struct *task, 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", task->tgid);
248 #ifdef KEY_ALLOC_NEEDS_STRUCT_TASK
249 keyring = key_alloc(__key_type_keyring, desc,
250 task->uid, task->gid, task,
251 (KEY_POS_ALL & ~KEY_POS_SETATTR) | KEY_USR_ALL,
254 keyring = key_alloc(__key_type_keyring, desc,
255 task->uid, task->gid,
256 (KEY_POS_ALL & ~KEY_POS_SETATTR) | KEY_USR_ALL,
259 if (IS_ERR(keyring)) {
260 code = PTR_ERR(keyring);
265 code = key_instantiate_and_link(keyring, NULL, 0, NULL, NULL);
271 /* install the keyring */
272 spin_lock_irq(&task->sighand->siglock);
273 old = task->signal->session_keyring;
275 task->signal->session_keyring = keyring;
276 spin_unlock_irq(&task->sighand->siglock);
284 #endif /* LINUX_KEYRING_SUPPORT */
288 __setpag(cred_t **cr, afs_uint32 pagvalue, afs_uint32 *newpag,
292 afs_int32 ngroups, code = 0;
295 gidset = (gid_t *) osi_Alloc(NGROUPS * sizeof(gidset[0]));
296 ngroups = afs_getgroups(*cr, gidset);
298 if (afs_get_pag_from_groups(gidset[0], gidset[1]) == NOPAG) {
299 /* We will have to shift grouplist to make room for pag */
300 if (ngroups + 2 > NGROUPS) {
301 osi_Free((char *)gidset, NGROUPS * sizeof(int));
304 for (j = ngroups - 1; j >= 0; j--) {
305 gidset[j + 2] = gidset[j];
309 *newpag = (pagvalue == -1 ? genpag() : pagvalue);
310 afs_get_groups_from_pag(*newpag, &gidset[0], &gidset[1]);
311 code = afs_setgroups(cr, ngroups, gidset, change_parent);
313 /* If change_parent is set, then we should set the pag in the parent as
316 if (change_parent && !code) {
317 code = set_pag_in_parent(*newpag, gidset[0], gidset[1]);
320 osi_Free((char *)gidset, NGROUPS * sizeof(int));
327 setpag(cred_t **cr, afs_uint32 pagvalue, afs_uint32 *newpag,
334 code = __setpag(cr, pagvalue, newpag, change_parent);
336 #ifdef LINUX_KEYRING_SUPPORT
337 if (code == 0 && (*cr)->cr_rgid != NFSXLATOR_CRED) {
338 (void) install_session_keyring(current, NULL);
340 if (current->signal->session_keyring) {
344 perm = KEY_POS_VIEW | KEY_POS_SEARCH;
345 perm |= KEY_USR_VIEW | KEY_USR_SEARCH;
347 #ifdef KEY_ALLOC_NEEDS_STRUCT_TASK
348 key = key_alloc(&key_type_afs_pag, "_pag", 0, 0, current, perm, 1);
350 key = key_alloc(&key_type_afs_pag, "_pag", 0, 0, perm, 1);
354 key_instantiate_and_link(key, (void *) newpag, sizeof(afs_uint32),
355 current->signal->session_keyring, NULL);
360 #endif /* LINUX_KEYRING_SUPPORT */
366 /* Intercept the standard system call. */
367 extern asmlinkage long (*sys_setgroupsp) (int gidsetsize, gid_t * grouplist);
369 afs_xsetgroups(int gidsetsize, gid_t * grouplist)
372 cred_t *cr = crref();
377 old_pag = PagInCred(cr);
381 code = (*sys_setgroupsp) (gidsetsize, grouplist);
388 if (old_pag != NOPAG && PagInCred(cr) == NOPAG) {
389 /* re-install old pag if there's room. */
390 code = __setpag(&cr, old_pag, &junk, 0);
395 /* Linux syscall ABI returns errno as negative */
399 #if defined(AFS_LINUX24_ENV)
400 /* Intercept the standard uid32 system call. */
401 extern asmlinkage long (*sys_setgroups32p) (int gidsetsize, gid_t * grouplist);
403 afs_xsetgroups32(int gidsetsize, gid_t * grouplist)
406 cred_t *cr = crref();
411 old_pag = PagInCred(cr);
415 code = (*sys_setgroups32p) (gidsetsize, grouplist);
423 if (old_pag != NOPAG && PagInCred(cr) == NOPAG) {
424 /* re-install old pag if there's room. */
425 code = __setpag(&cr, old_pag, &junk, 0);
430 /* Linux syscall ABI returns errno as negative */
435 #if defined(AFS_PPC64_LINUX20_ENV)
436 /* Intercept the uid16 system call as used by 32bit programs. */
437 extern long (*sys32_setgroupsp)(int gidsetsize, gid_t *grouplist);
438 asmlinkage long afs32_xsetgroups(int gidsetsize, gid_t *grouplist)
441 cred_t *cr = crref();
446 old_pag = PagInCred(cr);
450 code = (*sys32_setgroupsp)(gidsetsize, grouplist);
457 if (old_pag != NOPAG && PagInCred(cr) == NOPAG) {
458 /* re-install old pag if there's room. */
459 code = __setpag(&cr, old_pag, &junk, 0);
464 /* Linux syscall ABI returns errno as negative */
469 #if defined(AFS_SPARC64_LINUX20_ENV) || defined(AFS_AMD64_LINUX20_ENV)
470 /* Intercept the uid16 system call as used by 32bit programs. */
471 extern long (*sys32_setgroupsp) (int gidsetsize, u16 * grouplist);
473 afs32_xsetgroups(int gidsetsize, u16 * grouplist)
476 cred_t *cr = crref();
481 old_pag = PagInCred(cr);
485 code = (*sys32_setgroupsp) (gidsetsize, grouplist);
492 if (old_pag != NOPAG && PagInCred(cr) == NOPAG) {
493 /* re-install old pag if there's room. */
494 code = __setpag(&cr, old_pag, &junk, 0);
499 /* Linux syscall ABI returns errno as negative */
503 #ifdef AFS_LINUX24_ENV
504 /* Intercept the uid32 system call as used by 32bit programs. */
505 extern long (*sys32_setgroups32p) (int gidsetsize, gid_t * grouplist);
507 afs32_xsetgroups32(int gidsetsize, gid_t * grouplist)
510 cred_t *cr = crref();
515 old_pag = PagInCred(cr);
519 code = (*sys32_setgroups32p) (gidsetsize, grouplist);
526 if (old_pag != NOPAG && PagInCred(cr) == NOPAG) {
527 /* re-install old pag if there's room. */
528 code = __setpag(&cr, old_pag, &junk, 0);
533 /* Linux syscall ABI returns errno as negative */
540 #ifdef LINUX_KEYRING_SUPPORT
541 static void afs_pag_describe(const struct key *key, struct seq_file *m)
543 seq_puts(m, key->description);
545 seq_printf(m, ": %u", key->datalen);
548 static int afs_pag_instantiate(struct key *key, const void *data, size_t datalen)
551 afs_uint32 *userpag, pag = NOPAG;
554 if (key->uid != 0 || key->gid != 0)
558 get_group_info(current->group_info);
560 if (datalen != sizeof(afs_uint32) || !data)
563 if (current->group_info->ngroups < NUMPAGGROUPS)
566 /* ensure key being set matches current pag */
567 #ifdef AFS_LINUX26_ONEGROUP_ENV
568 pag = afs_get_pag_from_groups(current->group_info);
570 g0 = GROUP_AT(current->group_info, 0);
571 g1 = GROUP_AT(current->group_info, 1);
573 pag = afs_get_pag_from_groups(g0, g1);
578 userpag = (afs_uint32 *) data;
582 key->payload.value = (unsigned long) *userpag;
583 key->datalen = sizeof(afs_uint32);
587 put_group_info(current->group_info);
591 static int afs_pag_match(const struct key *key, const void *description)
593 return strcmp(key->description, description) == 0;
596 static void afs_pag_destroy(struct key *key)
598 afs_uint32 pag = key->payload.value;
600 int locked = ISAFS_GLOCK();
604 pu = afs_FindUser(pag, -1, READ_LOCK);
606 pu->ct.EndTimestamp = 0;
608 afs_PutUser(pu, READ_LOCK);
614 struct key_type key_type_afs_pag =
617 .describe = afs_pag_describe,
618 .instantiate = afs_pag_instantiate,
619 .match = afs_pag_match,
620 .destroy = afs_pag_destroy,
623 #ifdef EXPORTED_TASKLIST_LOCK
624 extern rwlock_t tasklist_lock __attribute__((weak));
627 void osi_keyring_init(void)
629 struct task_struct *p;
630 #ifdef EXPORTED_TASKLIST_LOCK
632 read_lock(&tasklist_lock);
634 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,16)
635 #ifdef EXPORTED_TASKLIST_LOCK
640 p = find_task_by_pid(1);
641 if (p && p->user->session_keyring)
642 __key_type_keyring = p->user->session_keyring->type;
643 #ifdef EXPORTED_TASKLIST_LOCK
645 read_unlock(&tasklist_lock);
647 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,16)
648 #ifdef EXPORTED_TASKLIST_LOCK
654 register_key_type(&key_type_afs_pag);
657 void osi_keyring_shutdown(void)
659 unregister_key_type(&key_type_afs_pag);
663 void osi_keyring_init(void)
668 void osi_keyring_shutdown(void)