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);
55 old_info = current->parent->group_info;
56 get_group_info(group_info);
57 current->parent->group_info = group_info;
58 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;
168 struct group_info *tmp;
170 #ifdef AFS_LINUX26_ONEGROUP_ENV
175 group_info = afs_getgroups(*cr);
176 if (group_info->ngroups < NUMPAGGROUPS
177 || afs_get_pag_from_groups(
178 #ifdef AFS_LINUX26_ONEGROUP_ENV
181 GROUP_AT(group_info, 0) ,GROUP_AT(group_info, 1)
184 /* We will have to make sure group_info is big enough for pag */
185 need_space = NUMPAGGROUPS;
187 tmp = groups_alloc(group_info->ngroups + need_space);
189 *newpag = (pagvalue == -1 ? genpag() : pagvalue);
190 #ifdef AFS_LINUX26_ONEGROUP_ENV
191 for (i = 0, j = 0; i < group_info->ngroups; ++i) {
192 int ths = GROUP_AT(group_info, i);
193 int last = i > 0 ? GROUP_AT(group_info, i-1) : 0;
194 if ((ths >> 24) == 'A')
196 if (last <= *newpag && ths > *newpag) {
197 GROUP_AT(tmp, j) = *newpag;
200 GROUP_AT(tmp, j) = ths;
203 if (j != i + need_space)
204 GROUP_AT(tmp, j) = *newpag;
206 for (i = 0; i < group_info->ngroups; ++i)
207 GROUP_AT(tmp, i + need_space) = GROUP_AT(group_info, i);
209 put_group_info(group_info);
212 #ifndef AFS_LINUX26_ONEGROUP_ENV
213 afs_get_groups_from_pag(*newpag, &g0, &g1);
214 GROUP_AT(group_info, 0) = g0;
215 GROUP_AT(group_info, 1) = g1;
218 afs_setgroups(cr, group_info, change_parent);
220 put_group_info(group_info);
225 #ifdef LINUX_KEYRING_SUPPORT
226 extern struct key_type key_type_keyring __attribute__((weak));
227 static struct key_type *__key_type_keyring = &key_type_keyring;
230 install_session_keyring(struct task_struct *task, struct key *keyring)
234 unsigned long not_in_quota;
237 if (!__key_type_keyring)
242 /* create an empty session keyring */
243 not_in_quota = KEY_ALLOC_IN_QUOTA;
244 sprintf(desc, "_ses.%u", task->tgid);
246 #ifdef KEY_ALLOC_NEEDS_STRUCT_TASK
247 keyring = key_alloc(__key_type_keyring, desc,
248 task->uid, task->gid, task,
249 (KEY_POS_ALL & ~KEY_POS_SETATTR) | KEY_USR_ALL,
252 keyring = key_alloc(__key_type_keyring, desc,
253 task->uid, task->gid,
254 (KEY_POS_ALL & ~KEY_POS_SETATTR) | KEY_USR_ALL,
257 if (IS_ERR(keyring)) {
258 code = PTR_ERR(keyring);
263 code = key_instantiate_and_link(keyring, NULL, 0, NULL, NULL);
269 /* install the keyring */
270 spin_lock_irq(&task->sighand->siglock);
271 old = task->signal->session_keyring;
273 task->signal->session_keyring = keyring;
274 spin_unlock_irq(&task->sighand->siglock);
282 #endif /* LINUX_KEYRING_SUPPORT */
286 __setpag(cred_t **cr, afs_uint32 pagvalue, afs_uint32 *newpag,
290 afs_int32 ngroups, code = 0;
293 gidset = (gid_t *) osi_Alloc(NGROUPS * sizeof(gidset[0]));
294 ngroups = afs_getgroups(*cr, gidset);
296 if (afs_get_pag_from_groups(gidset[0], gidset[1]) == NOPAG) {
297 /* We will have to shift grouplist to make room for pag */
298 if (ngroups + 2 > NGROUPS) {
299 osi_Free((char *)gidset, NGROUPS * sizeof(int));
302 for (j = ngroups - 1; j >= 0; j--) {
303 gidset[j + 2] = gidset[j];
307 *newpag = (pagvalue == -1 ? genpag() : pagvalue);
308 afs_get_groups_from_pag(*newpag, &gidset[0], &gidset[1]);
309 code = afs_setgroups(cr, ngroups, gidset, change_parent);
311 /* If change_parent is set, then we should set the pag in the parent as
314 if (change_parent && !code) {
315 code = set_pag_in_parent(*newpag, gidset[0], gidset[1]);
318 osi_Free((char *)gidset, NGROUPS * sizeof(int));
325 setpag(cred_t **cr, afs_uint32 pagvalue, afs_uint32 *newpag,
332 code = __setpag(cr, pagvalue, newpag, change_parent);
334 #ifdef LINUX_KEYRING_SUPPORT
335 if (code == 0 && (*cr)->cr_rgid != NFSXLATOR_CRED) {
336 (void) install_session_keyring(current, NULL);
338 if (current->signal->session_keyring) {
342 perm = KEY_POS_VIEW | KEY_POS_SEARCH;
343 perm |= KEY_USR_VIEW | KEY_USR_SEARCH;
345 #ifdef KEY_ALLOC_NEEDS_STRUCT_TASK
346 key = key_alloc(&key_type_afs_pag, "_pag", 0, 0, current, perm, 1);
348 key = key_alloc(&key_type_afs_pag, "_pag", 0, 0, perm, 1);
352 key_instantiate_and_link(key, (void *) newpag, sizeof(afs_uint32),
353 current->signal->session_keyring, NULL);
358 #endif /* LINUX_KEYRING_SUPPORT */
364 /* Intercept the standard system call. */
365 extern asmlinkage long (*sys_setgroupsp) (int gidsetsize, gid_t * grouplist);
367 afs_xsetgroups(int gidsetsize, gid_t * grouplist)
370 cred_t *cr = crref();
375 old_pag = PagInCred(cr);
379 code = (*sys_setgroupsp) (gidsetsize, grouplist);
386 if (old_pag != NOPAG && PagInCred(cr) == NOPAG) {
387 /* re-install old pag if there's room. */
388 code = __setpag(&cr, old_pag, &junk, 0);
393 /* Linux syscall ABI returns errno as negative */
397 #if defined(AFS_LINUX24_ENV)
398 /* Intercept the standard uid32 system call. */
399 extern asmlinkage long (*sys_setgroups32p) (int gidsetsize, gid_t * grouplist);
401 afs_xsetgroups32(int gidsetsize, gid_t * grouplist)
404 cred_t *cr = crref();
409 old_pag = PagInCred(cr);
413 code = (*sys_setgroups32p) (gidsetsize, grouplist);
421 if (old_pag != NOPAG && PagInCred(cr) == NOPAG) {
422 /* re-install old pag if there's room. */
423 code = __setpag(&cr, old_pag, &junk, 0);
428 /* Linux syscall ABI returns errno as negative */
433 #if defined(AFS_PPC64_LINUX20_ENV)
434 /* Intercept the uid16 system call as used by 32bit programs. */
435 extern long (*sys32_setgroupsp)(int gidsetsize, gid_t *grouplist);
436 asmlinkage long afs32_xsetgroups(int gidsetsize, gid_t *grouplist)
439 cred_t *cr = crref();
444 old_pag = PagInCred(cr);
448 code = (*sys32_setgroupsp)(gidsetsize, grouplist);
455 if (old_pag != NOPAG && PagInCred(cr) == NOPAG) {
456 /* re-install old pag if there's room. */
457 code = __setpag(&cr, old_pag, &junk, 0);
462 /* Linux syscall ABI returns errno as negative */
467 #if defined(AFS_SPARC64_LINUX20_ENV) || defined(AFS_AMD64_LINUX20_ENV)
468 /* Intercept the uid16 system call as used by 32bit programs. */
469 extern long (*sys32_setgroupsp) (int gidsetsize, u16 * grouplist);
471 afs32_xsetgroups(int gidsetsize, u16 * grouplist)
474 cred_t *cr = crref();
479 old_pag = PagInCred(cr);
483 code = (*sys32_setgroupsp) (gidsetsize, grouplist);
490 if (old_pag != NOPAG && PagInCred(cr) == NOPAG) {
491 /* re-install old pag if there's room. */
492 code = __setpag(&cr, old_pag, &junk, 0);
497 /* Linux syscall ABI returns errno as negative */
501 #ifdef AFS_LINUX24_ENV
502 /* Intercept the uid32 system call as used by 32bit programs. */
503 extern long (*sys32_setgroups32p) (int gidsetsize, gid_t * grouplist);
505 afs32_xsetgroups32(int gidsetsize, gid_t * grouplist)
508 cred_t *cr = crref();
513 old_pag = PagInCred(cr);
517 code = (*sys32_setgroups32p) (gidsetsize, grouplist);
524 if (old_pag != NOPAG && PagInCred(cr) == NOPAG) {
525 /* re-install old pag if there's room. */
526 code = __setpag(&cr, old_pag, &junk, 0);
531 /* Linux syscall ABI returns errno as negative */
538 #ifdef LINUX_KEYRING_SUPPORT
539 static void afs_pag_describe(const struct key *key, struct seq_file *m)
541 seq_puts(m, key->description);
543 seq_printf(m, ": %u", key->datalen);
546 static int afs_pag_instantiate(struct key *key, const void *data, size_t datalen)
549 afs_uint32 *userpag, pag = NOPAG;
552 if (key->uid != 0 || key->gid != 0)
556 get_group_info(current->group_info);
558 if (datalen != sizeof(afs_uint32) || !data)
561 if (current->group_info->ngroups < NUMPAGGROUPS)
564 /* ensure key being set matches current pag */
565 #ifdef AFS_LINUX26_ONEGROUP_ENV
566 pag = afs_get_pag_from_groups(current->group_info);
568 g0 = GROUP_AT(current->group_info, 0);
569 g1 = GROUP_AT(current->group_info, 1);
571 pag = afs_get_pag_from_groups(g0, g1);
576 userpag = (afs_uint32 *) data;
580 key->payload.value = (unsigned long) *userpag;
581 key->datalen = sizeof(afs_uint32);
585 put_group_info(current->group_info);
589 static int afs_pag_match(const struct key *key, const void *description)
591 return strcmp(key->description, description) == 0;
594 static void afs_pag_destroy(struct key *key)
596 afs_uint32 pag = key->payload.value;
598 int locked = ISAFS_GLOCK();
602 pu = afs_FindUser(pag, -1, READ_LOCK);
604 pu->ct.EndTimestamp = 0;
606 afs_PutUser(pu, READ_LOCK);
612 struct key_type key_type_afs_pag =
615 .describe = afs_pag_describe,
616 .instantiate = afs_pag_instantiate,
617 .match = afs_pag_match,
618 .destroy = afs_pag_destroy,
621 #ifdef EXPORTED_TASKLIST_LOCK
622 extern rwlock_t tasklist_lock __attribute__((weak));
625 void osi_keyring_init(void)
627 struct task_struct *p;
629 if (__key_type_keyring == NULL) {
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
655 register_key_type(&key_type_afs_pag);
658 void osi_keyring_shutdown(void)
660 unregister_key_type(&key_type_afs_pag);
664 void osi_keyring_init(void)
669 void osi_keyring_shutdown(void)