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 #ifdef EXPORTS_KEY_TYPE_KEYRING
227 static struct key_type *__key_type_keyring = &key_type_keyring;
229 static struct key_type *__key_type_keyring;
233 install_session_keyring(struct task_struct *task, struct key *keyring)
237 unsigned long not_in_quota;
240 if (!__key_type_keyring)
245 /* create an empty session keyring */
246 not_in_quota = KEY_ALLOC_IN_QUOTA;
247 sprintf(desc, "_ses.%u", task->tgid);
249 #ifdef KEY_ALLOC_NEEDS_STRUCT_TASK
250 keyring = key_alloc(__key_type_keyring, desc,
251 task->uid, task->gid, task,
252 (KEY_POS_ALL & ~KEY_POS_SETATTR) | KEY_USR_ALL,
255 keyring = key_alloc(__key_type_keyring, desc,
256 task->uid, task->gid,
257 (KEY_POS_ALL & ~KEY_POS_SETATTR) | KEY_USR_ALL,
260 if (IS_ERR(keyring)) {
261 code = PTR_ERR(keyring);
266 code = key_instantiate_and_link(keyring, NULL, 0, NULL, NULL);
272 /* install the keyring */
273 spin_lock_irq(&task->sighand->siglock);
274 old = task->signal->session_keyring;
276 task->signal->session_keyring = keyring;
277 spin_unlock_irq(&task->sighand->siglock);
285 #endif /* LINUX_KEYRING_SUPPORT */
289 __setpag(cred_t **cr, afs_uint32 pagvalue, afs_uint32 *newpag,
293 afs_int32 ngroups, code = 0;
296 gidset = (gid_t *) osi_Alloc(NGROUPS * sizeof(gidset[0]));
297 ngroups = afs_getgroups(*cr, gidset);
299 if (afs_get_pag_from_groups(gidset[0], gidset[1]) == NOPAG) {
300 /* We will have to shift grouplist to make room for pag */
301 if (ngroups + 2 > NGROUPS) {
302 osi_Free((char *)gidset, NGROUPS * sizeof(int));
305 for (j = ngroups - 1; j >= 0; j--) {
306 gidset[j + 2] = gidset[j];
310 *newpag = (pagvalue == -1 ? genpag() : pagvalue);
311 afs_get_groups_from_pag(*newpag, &gidset[0], &gidset[1]);
312 code = afs_setgroups(cr, ngroups, gidset, change_parent);
314 /* If change_parent is set, then we should set the pag in the parent as
317 if (change_parent && !code) {
318 code = set_pag_in_parent(*newpag, gidset[0], gidset[1]);
321 osi_Free((char *)gidset, NGROUPS * sizeof(int));
328 setpag(cred_t **cr, afs_uint32 pagvalue, afs_uint32 *newpag,
335 code = __setpag(cr, pagvalue, newpag, change_parent);
337 #ifdef LINUX_KEYRING_SUPPORT
338 if (code == 0 && (*cr)->cr_rgid != NFSXLATOR_CRED) {
339 (void) install_session_keyring(current, NULL);
341 if (current->signal->session_keyring) {
345 perm = KEY_POS_VIEW | KEY_POS_SEARCH;
346 perm |= KEY_USR_VIEW | KEY_USR_SEARCH;
348 #ifdef KEY_ALLOC_NEEDS_STRUCT_TASK
349 key = key_alloc(&key_type_afs_pag, "_pag", 0, 0, current, perm, 1);
351 key = key_alloc(&key_type_afs_pag, "_pag", 0, 0, perm, 1);
355 key_instantiate_and_link(key, (void *) newpag, sizeof(afs_uint32),
356 current->signal->session_keyring, NULL);
361 #endif /* LINUX_KEYRING_SUPPORT */
367 /* Intercept the standard system call. */
368 extern asmlinkage long (*sys_setgroupsp) (int gidsetsize, gid_t * grouplist);
370 afs_xsetgroups(int gidsetsize, gid_t * grouplist)
373 cred_t *cr = crref();
378 old_pag = PagInCred(cr);
382 code = (*sys_setgroupsp) (gidsetsize, grouplist);
389 if (old_pag != NOPAG && PagInCred(cr) == NOPAG) {
390 /* re-install old pag if there's room. */
391 code = __setpag(&cr, old_pag, &junk, 0);
396 /* Linux syscall ABI returns errno as negative */
400 #if defined(AFS_LINUX24_ENV)
401 /* Intercept the standard uid32 system call. */
402 extern asmlinkage long (*sys_setgroups32p) (int gidsetsize, gid_t * grouplist);
404 afs_xsetgroups32(int gidsetsize, gid_t * grouplist)
407 cred_t *cr = crref();
412 old_pag = PagInCred(cr);
416 code = (*sys_setgroups32p) (gidsetsize, grouplist);
424 if (old_pag != NOPAG && PagInCred(cr) == NOPAG) {
425 /* re-install old pag if there's room. */
426 code = __setpag(&cr, old_pag, &junk, 0);
431 /* Linux syscall ABI returns errno as negative */
436 #if defined(AFS_PPC64_LINUX20_ENV)
437 /* Intercept the uid16 system call as used by 32bit programs. */
438 extern long (*sys32_setgroupsp)(int gidsetsize, gid_t *grouplist);
439 asmlinkage long afs32_xsetgroups(int gidsetsize, gid_t *grouplist)
442 cred_t *cr = crref();
447 old_pag = PagInCred(cr);
451 code = (*sys32_setgroupsp)(gidsetsize, grouplist);
458 if (old_pag != NOPAG && PagInCred(cr) == NOPAG) {
459 /* re-install old pag if there's room. */
460 code = __setpag(&cr, old_pag, &junk, 0);
465 /* Linux syscall ABI returns errno as negative */
470 #if defined(AFS_SPARC64_LINUX20_ENV) || defined(AFS_AMD64_LINUX20_ENV)
471 /* Intercept the uid16 system call as used by 32bit programs. */
472 extern long (*sys32_setgroupsp) (int gidsetsize, u16 * grouplist);
474 afs32_xsetgroups(int gidsetsize, u16 * grouplist)
477 cred_t *cr = crref();
482 old_pag = PagInCred(cr);
486 code = (*sys32_setgroupsp) (gidsetsize, grouplist);
493 if (old_pag != NOPAG && PagInCred(cr) == NOPAG) {
494 /* re-install old pag if there's room. */
495 code = __setpag(&cr, old_pag, &junk, 0);
500 /* Linux syscall ABI returns errno as negative */
504 #ifdef AFS_LINUX24_ENV
505 /* Intercept the uid32 system call as used by 32bit programs. */
506 extern long (*sys32_setgroups32p) (int gidsetsize, gid_t * grouplist);
508 afs32_xsetgroups32(int gidsetsize, gid_t * grouplist)
511 cred_t *cr = crref();
516 old_pag = PagInCred(cr);
520 code = (*sys32_setgroups32p) (gidsetsize, grouplist);
527 if (old_pag != NOPAG && PagInCred(cr) == NOPAG) {
528 /* re-install old pag if there's room. */
529 code = __setpag(&cr, old_pag, &junk, 0);
534 /* Linux syscall ABI returns errno as negative */
541 #ifdef LINUX_KEYRING_SUPPORT
542 static void afs_pag_describe(const struct key *key, struct seq_file *m)
544 seq_puts(m, key->description);
546 seq_printf(m, ": %u", key->datalen);
549 static int afs_pag_instantiate(struct key *key, const void *data, size_t datalen)
552 afs_uint32 *userpag, pag = NOPAG;
555 if (key->uid != 0 || key->gid != 0)
559 get_group_info(current->group_info);
561 if (datalen != sizeof(afs_uint32) || !data)
564 if (current->group_info->ngroups < NUMPAGGROUPS)
567 /* ensure key being set matches current pag */
568 #ifdef AFS_LINUX26_ONEGROUP_ENV
569 pag = afs_get_pag_from_groups(current->group_info);
571 g0 = GROUP_AT(current->group_info, 0);
572 g1 = GROUP_AT(current->group_info, 1);
574 pag = afs_get_pag_from_groups(g0, g1);
579 userpag = (afs_uint32 *) data;
583 key->payload.value = (unsigned long) *userpag;
584 key->datalen = sizeof(afs_uint32);
588 put_group_info(current->group_info);
592 static int afs_pag_match(const struct key *key, const void *description)
594 return strcmp(key->description, description) == 0;
597 static void afs_pag_destroy(struct key *key)
599 afs_uint32 pag = key->payload.value;
601 int locked = ISAFS_GLOCK();
605 pu = afs_FindUser(pag, -1, READ_LOCK);
607 pu->ct.EndTimestamp = 0;
609 afs_PutUser(pu, READ_LOCK);
615 struct key_type key_type_afs_pag =
618 .describe = afs_pag_describe,
619 .instantiate = afs_pag_instantiate,
620 .match = afs_pag_match,
621 .destroy = afs_pag_destroy,
624 #ifdef EXPORTED_TASKLIST_LOCK
625 extern rwlock_t tasklist_lock __attribute__((weak));
628 void osi_keyring_init(void)
630 #ifndef EXPORTS_KEY_TYPE_KEYRING
631 struct task_struct *p;
632 #ifdef EXPORTED_TASKLIST_LOCK
634 read_lock(&tasklist_lock);
636 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,16)
637 #ifdef EXPORTED_TASKLIST_LOCK
642 p = find_task_by_pid(1);
643 if (p && p->user->session_keyring)
644 __key_type_keyring = p->user->session_keyring->type;
645 #ifdef EXPORTED_TASKLIST_LOCK
647 read_unlock(&tasklist_lock);
649 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,16)
650 #ifdef EXPORTED_TASKLIST_LOCK
657 register_key_type(&key_type_afs_pag);
660 void osi_keyring_shutdown(void)
662 unregister_key_type(&key_type_afs_pag);
666 void osi_keyring_init(void)
671 void osi_keyring_shutdown(void)