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 #ifdef AFS_LINUX22_ENV
29 #include "h/smp_lock.h"
32 #ifdef AFS_LINUX26_ONEGROUP_ENV
33 #define NUMPAGGROUPS 1
35 #define NUMPAGGROUPS 2
38 #if defined(AFS_LINUX26_ENV)
40 afs_setgroups(cred_t **cr, struct group_info *group_info, int change_parent)
42 struct group_info *old_info;
44 AFS_STATCNT(afs_setgroups);
46 old_info = (*cr)->cr_group_info;
47 get_group_info(group_info);
48 (*cr)->cr_group_info = group_info;
49 put_group_info(old_info);
54 old_info = current->parent->group_info;
55 get_group_info(group_info);
56 current->parent->group_info = group_info;
57 put_group_info(old_info);
64 afs_setgroups(cred_t **cr, int ngroups, gid_t * gidset, int change_parent)
70 AFS_STATCNT(afs_setgroups);
72 if (ngroups > NGROUPS)
75 gp = (*cr)->cr_groups;
76 if (ngroups < NGROUPS)
77 gp[ngroups] = (gid_t) NOGROUP;
79 for (i = ngroups; i > 0; i--) {
83 (*cr)->cr_ngroups = ngroups;
89 #if defined(AFS_LINUX26_ENV)
90 static struct group_info *
91 afs_getgroups(cred_t * cr)
93 AFS_STATCNT(afs_getgroups);
95 get_group_info(cr->cr_group_info);
96 return cr->cr_group_info;
99 /* Returns number of groups. And we trust groups to be large enough to
100 * hold all the groups.
103 afs_getgroups(cred_t *cr, gid_t *groups)
109 AFS_STATCNT(afs_getgroups);
114 for (i = 0; (i < n) && (*gp != (gid_t) NOGROUP); i++)
120 #if !defined(AFS_LINUX26_ENV)
121 /* Only propogate the PAG to the parent process. Unix's propogate to
122 * all processes sharing the cred.
125 set_pag_in_parent(int pag, int g0, int g1)
128 #ifdef STRUCT_TASK_STRUCT_HAS_PARENT
129 gid_t *gp = current->parent->groups;
130 int ngroups = current->parent->ngroups;
132 gid_t *gp = current->p_pptr->groups;
133 int ngroups = current->p_pptr->ngroups;
136 if ((ngroups < 2) || (afs_get_pag_from_groups(gp[0], gp[1]) == NOPAG)) {
137 /* We will have to shift grouplist to make room for pag */
138 if (ngroups + 2 > NGROUPS) {
141 for (i = ngroups - 1; i >= 0; i--) {
148 if (ngroups < NGROUPS)
149 gp[ngroups] = NOGROUP;
151 #ifdef STRUCT_TASK_STRUCT_HAS_PARENT
152 current->parent->ngroups = ngroups;
154 current->p_pptr->ngroups = ngroups;
160 #if defined(AFS_LINUX26_ENV)
162 __setpag(cred_t **cr, afs_uint32 pagvalue, afs_uint32 *newpag,
165 struct group_info *group_info;
167 struct group_info *tmp;
169 #ifdef AFS_LINUX26_ONEGROUP_ENV
174 group_info = afs_getgroups(*cr);
175 if (group_info->ngroups < NUMPAGGROUPS
176 || afs_get_pag_from_groups(
177 #ifdef AFS_LINUX26_ONEGROUP_ENV
180 GROUP_AT(group_info, 0) ,GROUP_AT(group_info, 1)
183 /* We will have to make sure group_info is big enough for pag */
184 need_space = NUMPAGGROUPS;
186 tmp = groups_alloc(group_info->ngroups + need_space);
188 *newpag = (pagvalue == -1 ? genpag() : pagvalue);
189 #ifdef AFS_LINUX26_ONEGROUP_ENV
190 for (i = 0, j = 0; i < group_info->ngroups; ++i) {
191 int ths = GROUP_AT(group_info, i);
192 int last = i > 0 ? GROUP_AT(group_info, i-1) : 0;
193 if ((ths >> 24) == 'A')
195 if (last <= *newpag && ths > *newpag) {
196 GROUP_AT(tmp, j) = *newpag;
199 GROUP_AT(tmp, j) = ths;
202 if (j != i + need_space)
203 GROUP_AT(tmp, j) = *newpag;
205 for (i = 0; i < group_info->ngroups; ++i)
206 GROUP_AT(tmp, i + need_space) = GROUP_AT(group_info, i);
208 put_group_info(group_info);
211 #ifndef AFS_LINUX26_ONEGROUP_ENV
212 afs_get_groups_from_pag(*newpag, &g0, &g1);
213 GROUP_AT(group_info, 0) = g0;
214 GROUP_AT(group_info, 1) = g1;
217 afs_setgroups(cr, group_info, change_parent);
219 put_group_info(group_info);
224 #ifdef LINUX_KEYRING_SUPPORT
225 static struct key_type *__key_type_keyring;
228 install_session_keyring(struct task_struct *task, struct key *keyring)
232 unsigned long not_in_quota;
235 if (!__key_type_keyring)
240 /* create an empty session keyring */
241 not_in_quota = KEY_ALLOC_IN_QUOTA;
242 sprintf(desc, "_ses.%u", task->tgid);
244 #ifdef KEY_ALLOC_NEEDS_STRUCT_TASK
245 keyring = key_alloc(__key_type_keyring, desc,
246 task->uid, task->gid, task,
247 (KEY_POS_ALL & ~KEY_POS_SETATTR) | KEY_USR_ALL,
250 keyring = key_alloc(__key_type_keyring, desc,
251 task->uid, task->gid,
252 (KEY_POS_ALL & ~KEY_POS_SETATTR) | KEY_USR_ALL,
255 if (IS_ERR(keyring)) {
256 code = PTR_ERR(keyring);
261 code = key_instantiate_and_link(keyring, NULL, 0, NULL, NULL);
267 /* install the keyring */
268 spin_lock_irq(&task->sighand->siglock);
269 old = task->signal->session_keyring;
271 task->signal->session_keyring = keyring;
272 spin_unlock_irq(&task->sighand->siglock);
280 #endif /* LINUX_KEYRING_SUPPORT */
284 __setpag(cred_t **cr, afs_uint32 pagvalue, afs_uint32 *newpag,
288 afs_int32 ngroups, code = 0;
291 gidset = (gid_t *) osi_Alloc(NGROUPS * sizeof(gidset[0]));
292 ngroups = afs_getgroups(*cr, gidset);
294 if (afs_get_pag_from_groups(gidset[0], gidset[1]) == NOPAG) {
295 /* We will have to shift grouplist to make room for pag */
296 if (ngroups + 2 > NGROUPS) {
297 osi_Free((char *)gidset, NGROUPS * sizeof(int));
300 for (j = ngroups - 1; j >= 0; j--) {
301 gidset[j + 2] = gidset[j];
305 *newpag = (pagvalue == -1 ? genpag() : pagvalue);
306 afs_get_groups_from_pag(*newpag, &gidset[0], &gidset[1]);
307 code = afs_setgroups(cr, ngroups, gidset, change_parent);
309 /* If change_parent is set, then we should set the pag in the parent as
312 if (change_parent && !code) {
313 code = set_pag_in_parent(*newpag, gidset[0], gidset[1]);
316 osi_Free((char *)gidset, NGROUPS * sizeof(int));
323 setpag(cred_t **cr, afs_uint32 pagvalue, afs_uint32 *newpag,
330 code = __setpag(cr, pagvalue, newpag, change_parent);
332 #ifdef LINUX_KEYRING_SUPPORT
335 (void) install_session_keyring(current, NULL);
337 if (current->signal->session_keyring) {
341 perm = KEY_POS_VIEW | KEY_POS_SEARCH;
342 perm |= KEY_USR_VIEW | KEY_USR_SEARCH;
344 #ifdef KEY_ALLOC_NEEDS_STRUCT_TASK
345 key = key_alloc(&key_type_afs_pag, "_pag", 0, 0, current, perm, 1);
347 key = key_alloc(&key_type_afs_pag, "_pag", 0, 0, perm, 1);
351 key_instantiate_and_link(key, (void *) newpag, sizeof(afs_uint32),
352 current->signal->session_keyring, NULL);
357 #endif /* LINUX_KEYRING_SUPPORT */
363 /* Intercept the standard system call. */
364 extern asmlinkage long (*sys_setgroupsp) (int gidsetsize, gid_t * grouplist);
366 afs_xsetgroups(int gidsetsize, gid_t * grouplist)
369 cred_t *cr = crref();
374 old_pag = PagInCred(cr);
378 code = (*sys_setgroupsp) (gidsetsize, grouplist);
385 if (old_pag != NOPAG && PagInCred(cr) == NOPAG) {
386 /* re-install old pag if there's room. */
387 code = __setpag(&cr, old_pag, &junk, 0);
392 /* Linux syscall ABI returns errno as negative */
396 #if defined(AFS_LINUX24_ENV)
397 /* Intercept the standard uid32 system call. */
398 extern asmlinkage long (*sys_setgroups32p) (int gidsetsize, gid_t * grouplist);
400 afs_xsetgroups32(int gidsetsize, gid_t * grouplist)
403 cred_t *cr = crref();
408 old_pag = PagInCred(cr);
412 code = (*sys_setgroups32p) (gidsetsize, grouplist);
420 if (old_pag != NOPAG && PagInCred(cr) == NOPAG) {
421 /* re-install old pag if there's room. */
422 code = __setpag(&cr, old_pag, &junk, 0);
427 /* Linux syscall ABI returns errno as negative */
432 #if defined(AFS_PPC64_LINUX20_ENV)
433 /* Intercept the uid16 system call as used by 32bit programs. */
434 extern long (*sys32_setgroupsp)(int gidsetsize, gid_t *grouplist);
435 asmlinkage long afs32_xsetgroups(int gidsetsize, gid_t *grouplist)
438 cred_t *cr = crref();
443 old_pag = PagInCred(cr);
447 code = (*sys32_setgroupsp)(gidsetsize, grouplist);
454 if (old_pag != NOPAG && PagInCred(cr) == NOPAG) {
455 /* re-install old pag if there's room. */
456 code = __setpag(&cr, old_pag, &junk, 0);
461 /* Linux syscall ABI returns errno as negative */
466 #if defined(AFS_SPARC64_LINUX20_ENV) || defined(AFS_AMD64_LINUX20_ENV)
467 /* Intercept the uid16 system call as used by 32bit programs. */
468 extern long (*sys32_setgroupsp) (int gidsetsize, u16 * grouplist);
470 afs32_xsetgroups(int gidsetsize, u16 * grouplist)
473 cred_t *cr = crref();
478 old_pag = PagInCred(cr);
482 code = (*sys32_setgroupsp) (gidsetsize, grouplist);
489 if (old_pag != NOPAG && PagInCred(cr) == NOPAG) {
490 /* re-install old pag if there's room. */
491 code = __setpag(&cr, old_pag, &junk, 0);
496 /* Linux syscall ABI returns errno as negative */
500 #ifdef AFS_LINUX24_ENV
501 /* Intercept the uid32 system call as used by 32bit programs. */
502 extern long (*sys32_setgroups32p) (int gidsetsize, gid_t * grouplist);
504 afs32_xsetgroups32(int gidsetsize, gid_t * grouplist)
507 cred_t *cr = crref();
512 old_pag = PagInCred(cr);
516 code = (*sys32_setgroups32p) (gidsetsize, grouplist);
523 if (old_pag != NOPAG && PagInCred(cr) == NOPAG) {
524 /* re-install old pag if there's room. */
525 code = __setpag(&cr, old_pag, &junk, 0);
530 /* Linux syscall ABI returns errno as negative */
537 #ifdef LINUX_KEYRING_SUPPORT
538 static void afs_pag_describe(const struct key *key, struct seq_file *m)
540 seq_puts(m, key->description);
542 seq_printf(m, ": %u", key->datalen);
545 static int afs_pag_instantiate(struct key *key, const void *data, size_t datalen)
548 afs_uint32 *userpag, pag = NOPAG;
551 if (key->uid != 0 || key->gid != 0)
555 get_group_info(current->group_info);
557 if (datalen != sizeof(afs_uint32) || !data)
560 if (current->group_info->ngroups < NUMPAGGROUPS)
563 /* ensure key being set matches current pag */
564 #ifdef AFS_LINUX26_ONEGROUP_ENV
565 pag = afs_get_pag_from_groups(current->group_info);
567 g0 = GROUP_AT(current->group_info, 0);
568 g1 = GROUP_AT(current->group_info, 1);
570 pag = afs_get_pag_from_groups(g0, g1);
575 userpag = (afs_uint32 *) data;
579 key->payload.value = (unsigned long) *userpag;
580 key->datalen = sizeof(afs_uint32);
584 put_group_info(current->group_info);
588 static int afs_pag_match(const struct key *key, const void *description)
590 return strcmp(key->description, description) == 0;
593 static void afs_pag_destroy(struct key *key)
595 afs_uint32 pag = key->payload.value;
597 int locked = ISAFS_GLOCK();
601 pu = afs_FindUser(pag, -1, READ_LOCK);
603 pu->ct.EndTimestamp = 0;
605 afs_PutUser(pu, READ_LOCK);
611 struct key_type key_type_afs_pag =
614 .describe = afs_pag_describe,
615 .instantiate = afs_pag_instantiate,
616 .match = afs_pag_match,
617 .destroy = afs_pag_destroy,
620 #ifdef EXPORTED_TASKLIST_LOCK
621 extern rwlock_t tasklist_lock __attribute__((weak));
624 void osi_keyring_init(void)
626 struct task_struct *p;
627 #ifdef EXPORTED_TASKLIST_LOCK
629 read_lock(&tasklist_lock);
631 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,16)
632 #ifdef EXPORTED_TASKLIST_LOCK
637 p = find_task_by_pid(1);
638 if (p && p->user->session_keyring)
639 __key_type_keyring = p->user->session_keyring->type;
640 #ifdef EXPORTED_TASKLIST_LOCK
642 read_unlock(&tasklist_lock);
644 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,16)
645 #ifdef EXPORTED_TASKLIST_LOCK
651 register_key_type(&key_type_afs_pag);
654 void osi_keyring_shutdown(void)
656 unregister_key_type(&key_type_afs_pag);
660 void osi_keyring_init(void)
665 void osi_keyring_shutdown(void)