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 "osi_compat.h"
29 #include <linux/smp_lock.h>
31 #ifdef AFS_LINUX26_ONEGROUP_ENV
32 # define NUMPAGGROUPS 1
35 afs_linux_pag_from_groups(struct group_info *group_info) {
39 if (group_info->ngroups < NUMPAGGROUPS)
42 for (i = 0; (i < group_info->ngroups &&
43 (g0 = GROUP_AT(group_info, i)) != (gid_t) NOGROUP); i++) {
44 if (((g0 >> 24) & 0xff) == 'A')
51 afs_linux_pag_to_groups(afs_uint32 newpag,
52 struct group_info *old, struct group_info **new) {
57 if (afs_linux_pag_from_groups(old) == NOPAG)
58 need_space = NUMPAGGROUPS;
60 *new = groups_alloc(old->ngroups + need_space);
62 for (i = 0, j = 0; i < old->ngroups; ++i) {
63 int ths = GROUP_AT(old, i);
64 int last = i > 0 ? GROUP_AT(old, i-1) : 0;
65 if ((ths >> 24) == 'A')
67 if (last <= newpag && ths > newpag) {
68 GROUP_AT(*new, j) = newpag;
71 GROUP_AT(*new, j) = ths;
74 if (j != i + need_space)
75 GROUP_AT(*new, j) = newpag;
79 # define NUMPAGGROUPS 2
81 static inline afs_uint32
82 afs_linux_pag_from_groups(struct group_info *group_info) {
84 if (group_info->ngroups < NUMPAGGROUPS)
87 return afs_get_pag_from_groups(GROUP_AT(group_info, 0), GROUP_AT(group_info, 1));
91 afs_linux_pag_to_groups(afs_uint32 newpag,
92 struct group_info *old, struct group_info *new) {
98 if (afs_linux_pag_from_groups(old) == NOPAG)
99 need_space = NUMPAGGGROUPS;
101 *new = groups_alloc(old->ngroups + need_space);
103 for (i = 0; i < old->ngroups; ++i)
104 GROUP_AT(new, i + need_space) = GROUP_AT(old, i);
106 afs_get_groups_from_pag(newpag, &g0, g1);
107 GROUP_AT(new, 0) = g0;
108 GROUP_AT(new, 1) = g1;
113 osi_get_group_pag(afs_ucred_t *cred) {
114 return afs_linux_pag_from_groups(afs_cr_group_info(cred));
119 afs_setgroups(cred_t **cr, struct group_info *group_info, int change_parent)
121 struct group_info *old_info;
123 AFS_STATCNT(afs_setgroups);
125 old_info = afs_cr_group_info(*cr);
126 get_group_info(group_info);
127 afs_set_cr_group_info(*cr, group_info);
128 put_group_info(old_info);
132 #if defined(STRUCT_TASK_STRUCT_HAS_PARENT) && !defined(STRUCT_TASK_HAS_CRED)
134 old_info = current->parent->group_info;
135 get_group_info(group_info);
136 current->parent->group_info = group_info;
137 put_group_info(old_info);
145 __setpag(cred_t **cr, afs_uint32 pagvalue, afs_uint32 *newpag,
148 struct group_info *group_info;
149 struct group_info *tmp;
151 get_group_info(afs_cr_group_info(*cr));
152 group_info = afs_cr_group_info(*cr);
154 *newpag = (pagvalue == -1 ? genpag() : pagvalue);
155 afs_linux_pag_to_groups(*newpag, group_info, &tmp);
157 put_group_info(group_info);
160 afs_setgroups(cr, group_info, change_parent);
162 put_group_info(group_info);
167 #ifdef LINUX_KEYRING_SUPPORT
168 extern struct key_type key_type_keyring __attribute__((weak));
169 static struct key_type *__key_type_keyring = &key_type_keyring;
172 install_session_keyring(struct key *keyring)
179 if (!__key_type_keyring)
184 /* create an empty session keyring */
185 sprintf(desc, "_ses.%u", current->tgid);
187 /* if we're root, don't count the keyring against our quota. This
188 * avoids starvation issues when dealing with PAM modules that always
189 * setpag() as root */
190 if (current_uid() == 0)
191 flags = KEY_ALLOC_NOT_IN_QUOTA;
193 flags = KEY_ALLOC_IN_QUOTA;
195 keyring = afs_linux_key_alloc(
196 __key_type_keyring, desc,
197 current_uid(), current_gid(),
198 (KEY_POS_ALL & ~KEY_POS_SETATTR) | KEY_USR_ALL,
201 if (IS_ERR(keyring)) {
202 code = PTR_ERR(keyring);
207 code = key_instantiate_and_link(keyring, NULL, 0, NULL, NULL);
213 /* install the keyring */
214 spin_lock_irq(¤t->sighand->siglock);
215 old = task_session_keyring(current);
217 task_session_keyring(current) = keyring;
218 spin_unlock_irq(¤t->sighand->siglock);
226 #endif /* LINUX_KEYRING_SUPPORT */
229 setpag(cred_t **cr, afs_uint32 pagvalue, afs_uint32 *newpag,
236 code = __setpag(cr, pagvalue, newpag, change_parent);
238 #ifdef LINUX_KEYRING_SUPPORT
239 if (code == 0 && afs_cr_rgid(*cr) != NFSXLATOR_CRED) {
240 code = install_session_keyring(NULL);
241 if (code == 0 && current_session_keyring()) {
245 perm = KEY_POS_VIEW | KEY_POS_SEARCH;
246 perm |= KEY_USR_VIEW | KEY_USR_SEARCH;
248 key = afs_linux_key_alloc(&key_type_afs_pag, "_pag", 0, 0, perm, KEY_ALLOC_NOT_IN_QUOTA);
251 key_instantiate_and_link(key, (void *) newpag, sizeof(afs_uint32),
252 current_session_keyring(), NULL);
259 #endif /* LINUX_KEYRING_SUPPORT */
265 /* Intercept the standard system call. */
266 extern asmlinkage long (*sys_setgroupsp) (int gidsetsize, gid_t * grouplist);
268 afs_xsetgroups(int gidsetsize, gid_t * grouplist)
271 cred_t *cr = crref();
276 old_pag = PagInCred(cr);
280 code = (*sys_setgroupsp) (gidsetsize, grouplist);
287 if (old_pag != NOPAG && PagInCred(cr) == NOPAG) {
288 /* re-install old pag if there's room. */
289 code = __setpag(&cr, old_pag, &junk, 0);
294 /* Linux syscall ABI returns errno as negative */
298 /* Intercept the standard uid32 system call. */
299 extern asmlinkage long (*sys_setgroups32p) (int gidsetsize, gid_t * grouplist);
301 afs_xsetgroups32(int gidsetsize, gid_t * grouplist)
304 cred_t *cr = crref();
309 old_pag = PagInCred(cr);
313 code = (*sys_setgroups32p) (gidsetsize, grouplist);
321 if (old_pag != NOPAG && PagInCred(cr) == NOPAG) {
322 /* re-install old pag if there's room. */
323 code = __setpag(&cr, old_pag, &junk, 0);
328 /* Linux syscall ABI returns errno as negative */
332 #if defined(AFS_PPC64_LINUX20_ENV)
333 /* Intercept the uid16 system call as used by 32bit programs. */
334 extern long (*sys32_setgroupsp)(int gidsetsize, gid_t *grouplist);
335 asmlinkage long afs32_xsetgroups(int gidsetsize, gid_t *grouplist)
338 cred_t *cr = crref();
343 old_pag = PagInCred(cr);
347 code = (*sys32_setgroupsp)(gidsetsize, grouplist);
354 if (old_pag != NOPAG && PagInCred(cr) == NOPAG) {
355 /* re-install old pag if there's room. */
356 code = __setpag(&cr, old_pag, &junk, 0);
361 /* Linux syscall ABI returns errno as negative */
366 #if defined(AFS_SPARC64_LINUX20_ENV) || defined(AFS_AMD64_LINUX20_ENV)
367 /* Intercept the uid16 system call as used by 32bit programs. */
368 extern long (*sys32_setgroupsp) (int gidsetsize, u16 * grouplist);
370 afs32_xsetgroups(int gidsetsize, u16 * grouplist)
373 cred_t *cr = crref();
378 old_pag = PagInCred(cr);
382 code = (*sys32_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 /* Intercept the uid32 system call as used by 32bit programs. */
401 extern long (*sys32_setgroups32p) (int gidsetsize, gid_t * grouplist);
403 afs32_xsetgroups32(int gidsetsize, gid_t * grouplist)
406 cred_t *cr = crref();
411 old_pag = PagInCred(cr);
415 code = (*sys32_setgroups32p) (gidsetsize, grouplist);
422 if (old_pag != NOPAG && PagInCred(cr) == NOPAG) {
423 /* re-install old pag if there's room. */
424 code = __setpag(&cr, old_pag, &junk, 0);
429 /* Linux syscall ABI returns errno as negative */
435 #ifdef LINUX_KEYRING_SUPPORT
436 static void afs_pag_describe(const struct key *key, struct seq_file *m)
438 seq_puts(m, key->description);
440 seq_printf(m, ": %u", key->datalen);
443 static int afs_pag_instantiate(struct key *key, const void *data, size_t datalen)
446 afs_uint32 *userpag, pag = NOPAG;
448 if (key->uid != 0 || key->gid != 0)
452 get_group_info(current_group_info());
454 if (datalen != sizeof(afs_uint32) || !data)
457 /* ensure key being set matches current pag */
458 pag = afs_linux_pag_from_groups(current_group_info());
463 userpag = (afs_uint32 *) data;
467 key->payload.value = (unsigned long) *userpag;
468 key->datalen = sizeof(afs_uint32);
472 put_group_info(current_group_info());
476 static int afs_pag_match(const struct key *key, const void *description)
478 return strcmp(key->description, description) == 0;
481 static void afs_pag_destroy(struct key *key)
483 afs_uint32 pag = key->payload.value;
484 int locked = ISAFS_GLOCK();
489 afs_MarkUserExpired(pag);
495 struct key_type key_type_afs_pag =
498 .describe = afs_pag_describe,
499 .instantiate = afs_pag_instantiate,
500 .match = afs_pag_match,
501 .destroy = afs_pag_destroy,
504 #ifdef EXPORTED_TASKLIST_LOCK
505 extern rwlock_t tasklist_lock __attribute__((weak));
508 void osi_keyring_init(void)
510 #if !defined(EXPORTED_KEY_TYPE_KEYRING)
511 struct task_struct *p;
513 /* If we can't lock the tasklist, either with its explicit lock,
514 * or by using the RCU lock, then we can't safely work out the
515 * type of a keyring. So, we have to rely on the weak reference.
516 * If that's not available, then keyring based PAGs won't work.
519 #if defined(EXPORTED_TASKLIST_LOCK) || (LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,16) && defined(HAVE_LINUX_RCU_READ_LOCK))
520 if (__key_type_keyring == NULL) {
521 # ifdef EXPORTED_TASKLIST_LOCK
523 read_lock(&tasklist_lock);
525 # if (LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,16) && defined(HAVE_LINUX_RCU_READ_LOCK))
526 # ifdef EXPORTED_TASKLIST_LOCK
531 #if defined(HAVE_LINUX_FIND_TASK_BY_PID)
532 p = find_task_by_pid(1);
534 p = find_task_by_vpid(1);
536 if (p && task_user(p)->session_keyring)
537 __key_type_keyring = task_user(p)->session_keyring->type;
538 # ifdef EXPORTED_TASKLIST_LOCK
540 read_unlock(&tasklist_lock);
542 # if (LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,16) && defined(HAVE_LINUX_RCU_READ_LOCK))
543 # ifdef EXPORTED_TASKLIST_LOCK
552 register_key_type(&key_type_afs_pag);
555 void osi_keyring_shutdown(void)
557 unregister_key_type(&key_type_afs_pag);
561 osi_get_keyring_pag(afs_ucred_t *cred)
565 afs_int32 keyring_pag = NOPAG;
567 if (afs_cr_rgid(cred) != NFSXLATOR_CRED) {
568 key = afs_linux_search_keyring(cred, &key_type_afs_pag);
571 if (key_validate(key) == 0 && key->uid == 0) { /* also verify in the session keyring? */
572 keyring_pag = key->payload.value;
573 /* Only set PAG in groups if needed,
574 * and the creds are from the current process */
575 if (afs_linux_cred_is_current(cred) &&
576 ((keyring_pag >> 24) & 0xff) == 'A' &&
577 keyring_pag != afs_linux_pag_from_groups(current_group_info())) {
578 __setpag(&cred, keyring_pag, &newpag, 0);
588 void osi_keyring_init(void)
593 void osi_keyring_shutdown(void)