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 #if defined(LINUX_KEYRING_SUPPORT)
32 # include <linux/rwsem.h>
33 # include <linux/key.h>
34 # if defined(HAVE_LINUX_KEY_TYPE_H)
35 # include <linux/key-type.h>
37 # ifndef KEY_ALLOC_IN_QUOTA
38 # define KEY_ALLOC_IN_QUOTA 1
42 #ifdef AFS_LINUX26_ONEGROUP_ENV
43 # define NUMPAGGROUPS 1
46 afs_linux_pag_from_groups(struct group_info *group_info) {
50 if (group_info->ngroups < NUMPAGGROUPS)
53 for (i = 0; (i < group_info->ngroups &&
54 (g0 = GROUP_AT(group_info, i)) != (gid_t) NOGROUP); i++) {
55 if (((g0 >> 24) & 0xff) == 'A')
62 afs_linux_pag_to_groups(afs_uint32 newpag,
63 struct group_info *old, struct group_info **new) {
68 if (afs_linux_pag_from_groups(old) == NOPAG)
69 need_space = NUMPAGGROUPS;
71 *new = groups_alloc(old->ngroups + need_space);
73 for (i = 0, j = 0; i < old->ngroups; ++i) {
74 int ths = GROUP_AT(old, i);
75 int last = i > 0 ? GROUP_AT(old, i-1) : 0;
76 if ((ths >> 24) == 'A')
78 if (last <= newpag && ths > newpag) {
79 GROUP_AT(*new, j) = newpag;
82 GROUP_AT(*new, j) = ths;
85 if (j != i + need_space)
86 GROUP_AT(*new, j) = newpag;
90 # define NUMPAGGROUPS 2
92 static inline afs_uint32
93 afs_linux_pag_from_groups(struct group_info *group_info) {
95 if (group_info->ngroups < NUMPAGGROUPS)
98 return afs_get_pag_from_groups(GROUP_AT(group_info, 0), GROUP_AT(group_info, 1));
102 afs_linux_pag_to_groups(afs_uint32 newpag,
103 struct group_info *old, struct group_info *new) {
109 if (afs_linux_pag_from_groups(old) == NOPAG)
110 need_space = NUMPAGGGROUPS;
112 *new = groups_alloc(old->ngroups + need_space);
114 for (i = 0; i < old->ngroups; ++i)
115 GROUP_AT(new, i + need_space) = GROUP_AT(old, i);
117 afs_get_groups_from_pag(newpag, &g0, g1);
118 GROUP_AT(new, 0) = g0;
119 GROUP_AT(new, 1) = g1;
124 osi_get_group_pag(afs_ucred_t *cred) {
125 return afs_linux_pag_from_groups(afs_cr_group_info(cred));
130 afs_setgroups(cred_t **cr, struct group_info *group_info, int change_parent)
132 struct group_info *old_info;
134 AFS_STATCNT(afs_setgroups);
136 old_info = afs_cr_group_info(*cr);
137 get_group_info(group_info);
138 afs_set_cr_group_info(*cr, group_info);
139 put_group_info(old_info);
143 #if defined(STRUCT_TASK_STRUCT_HAS_PARENT) && !defined(STRUCT_TASK_STRUCT_HAS_CRED)
145 old_info = current->parent->group_info;
146 get_group_info(group_info);
147 current->parent->group_info = group_info;
148 put_group_info(old_info);
156 __setpag(cred_t **cr, afs_uint32 pagvalue, afs_uint32 *newpag,
159 struct group_info *group_info;
160 struct group_info *tmp;
162 get_group_info(afs_cr_group_info(*cr));
163 group_info = afs_cr_group_info(*cr);
165 *newpag = (pagvalue == -1 ? genpag() : pagvalue);
166 afs_linux_pag_to_groups(*newpag, group_info, &tmp);
168 put_group_info(group_info);
171 afs_setgroups(cr, group_info, change_parent);
173 put_group_info(group_info);
178 #ifdef LINUX_KEYRING_SUPPORT
179 extern struct key_type key_type_keyring __attribute__((weak));
180 static struct key_type *__key_type_keyring = &key_type_keyring;
183 install_session_keyring(struct key *keyring)
190 if (!__key_type_keyring)
195 /* create an empty session keyring */
196 sprintf(desc, "_ses.%u", current->tgid);
198 /* if we're root, don't count the keyring against our quota. This
199 * avoids starvation issues when dealing with PAM modules that always
200 * setpag() as root */
201 if (current_uid() == 0)
202 flags = KEY_ALLOC_NOT_IN_QUOTA;
204 flags = KEY_ALLOC_IN_QUOTA;
206 keyring = afs_linux_key_alloc(
207 __key_type_keyring, desc,
208 current_uid(), current_gid(),
209 (KEY_POS_ALL & ~KEY_POS_SETATTR) | KEY_USR_ALL,
212 if (IS_ERR(keyring)) {
213 code = PTR_ERR(keyring);
218 code = key_instantiate_and_link(keyring, NULL, 0, NULL, NULL);
224 /* install the keyring */
225 spin_lock_irq(¤t->sighand->siglock);
226 old = task_session_keyring(current);
228 task_session_keyring(current) = keyring;
229 spin_unlock_irq(¤t->sighand->siglock);
237 #endif /* LINUX_KEYRING_SUPPORT */
240 setpag(cred_t **cr, afs_uint32 pagvalue, afs_uint32 *newpag,
247 code = __setpag(cr, pagvalue, newpag, change_parent);
249 #ifdef LINUX_KEYRING_SUPPORT
250 if (code == 0 && afs_cr_rgid(*cr) != NFSXLATOR_CRED) {
251 code = install_session_keyring(NULL);
252 if (code == 0 && current_session_keyring()) {
256 perm = KEY_POS_VIEW | KEY_POS_SEARCH;
257 perm |= KEY_USR_VIEW | KEY_USR_SEARCH;
259 key = afs_linux_key_alloc(&key_type_afs_pag, "_pag", 0, 0, perm, KEY_ALLOC_NOT_IN_QUOTA);
262 key_instantiate_and_link(key, (void *) newpag, sizeof(afs_uint32),
263 current_session_keyring(), NULL);
270 #endif /* LINUX_KEYRING_SUPPORT */
276 /* Intercept the standard system call. */
277 extern asmlinkage long (*sys_setgroupsp) (int gidsetsize, gid_t * grouplist);
279 afs_xsetgroups(int gidsetsize, gid_t * grouplist)
282 cred_t *cr = crref();
286 old_pag = PagInCred(cr);
289 code = (*sys_setgroupsp) (gidsetsize, grouplist);
295 if (old_pag != NOPAG && PagInCred(cr) == NOPAG) {
296 /* re-install old pag if there's room. */
297 code = __setpag(&cr, old_pag, &junk, 0);
301 /* Linux syscall ABI returns errno as negative */
305 /* Intercept the standard uid32 system call. */
306 extern asmlinkage int (*sys_setgroups32p) (int gidsetsize,
307 __kernel_gid32_t * grouplist);
309 afs_xsetgroups32(int gidsetsize, gid_t * grouplist)
312 cred_t *cr = crref();
316 old_pag = PagInCred(cr);
319 code = (*sys_setgroups32p) (gidsetsize, grouplist);
326 if (old_pag != NOPAG && PagInCred(cr) == NOPAG) {
327 /* re-install old pag if there's room. */
328 code = __setpag(&cr, old_pag, &junk, 0);
332 /* Linux syscall ABI returns errno as negative */
336 #if defined(AFS_PPC64_LINUX20_ENV)
337 /* Intercept the uid16 system call as used by 32bit programs. */
338 extern asmlinkage long (*sys32_setgroupsp)(int gidsetsize, gid_t *grouplist);
339 asmlinkage long afs32_xsetgroups(int gidsetsize, gid_t *grouplist)
342 cred_t *cr = crref();
346 old_pag = PagInCred(cr);
349 code = (*sys32_setgroupsp)(gidsetsize, grouplist);
355 if (old_pag != NOPAG && PagInCred(cr) == NOPAG) {
356 /* re-install old pag if there's room. */
357 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 #ifdef AFS_AMD64_LINUX20_ENV
369 extern asmlinkage long (*sys32_setgroupsp) (int gidsetsize, u16 * grouplist);
370 #endif /* AFS_AMD64_LINUX20_ENV */
371 #ifdef AFS_SPARC64_LINUX26_ENV
372 extern asmlinkage int (*sys32_setgroupsp) (int gidsetsize,
373 __kernel_gid32_t * grouplist);
374 #endif /* AFS_SPARC64_LINUX26_ENV */
376 afs32_xsetgroups(int gidsetsize, u16 * grouplist)
379 cred_t *cr = crref();
383 old_pag = PagInCred(cr);
386 code = (*sys32_setgroupsp) (gidsetsize, grouplist);
392 if (old_pag != NOPAG && PagInCred(cr) == NOPAG) {
393 /* re-install old pag if there's room. */
394 code = __setpag(&cr, old_pag, &junk, 0);
398 /* Linux syscall ABI returns errno as negative */
402 /* Intercept the uid32 system call as used by 32bit programs. */
403 #ifdef AFS_AMD64_LINUX20_ENV
404 extern asmlinkage long (*sys32_setgroups32p) (int gidsetsize, gid_t * grouplist);
405 #endif /* AFS_AMD64_LINUX20_ENV */
406 #ifdef AFS_SPARC64_LINUX26_ENV
407 extern asmlinkage int (*sys32_setgroups32p) (int gidsetsize,
408 __kernel_gid32_t * grouplist);
409 #endif /* AFS_SPARC64_LINUX26_ENV */
411 afs32_xsetgroups32(int gidsetsize, gid_t * grouplist)
414 cred_t *cr = crref();
418 old_pag = PagInCred(cr);
421 code = (*sys32_setgroups32p) (gidsetsize, grouplist);
427 if (old_pag != NOPAG && PagInCred(cr) == NOPAG) {
428 /* re-install old pag if there's room. */
429 code = __setpag(&cr, old_pag, &junk, 0);
433 /* Linux syscall ABI returns errno as negative */
439 #ifdef LINUX_KEYRING_SUPPORT
440 static void afs_pag_describe(const struct key *key, struct seq_file *m)
442 seq_puts(m, key->description);
444 seq_printf(m, ": %u", key->datalen);
447 static int afs_pag_instantiate(struct key *key, const void *data, size_t datalen)
450 afs_uint32 *userpag, pag = NOPAG;
452 if (key->uid != 0 || key->gid != 0)
456 get_group_info(current_group_info());
458 if (datalen != sizeof(afs_uint32) || !data)
461 /* ensure key being set matches current pag */
462 pag = afs_linux_pag_from_groups(current_group_info());
467 userpag = (afs_uint32 *) data;
471 key->payload.value = (unsigned long) *userpag;
472 key->datalen = sizeof(afs_uint32);
476 put_group_info(current_group_info());
480 static int afs_pag_match(const struct key *key, const void *description)
482 return strcmp(key->description, description) == 0;
485 static void afs_pag_destroy(struct key *key)
487 afs_uint32 pag = key->payload.value;
488 int locked = ISAFS_GLOCK();
493 afs_MarkUserExpired(pag);
499 struct key_type key_type_afs_pag =
502 .describe = afs_pag_describe,
503 .instantiate = afs_pag_instantiate,
504 .match = afs_pag_match,
505 .destroy = afs_pag_destroy,
508 #ifdef EXPORTED_TASKLIST_LOCK
509 extern rwlock_t tasklist_lock __attribute__((weak));
512 void osi_keyring_init(void)
514 #if !defined(EXPORTED_KEY_TYPE_KEYRING)
515 struct task_struct *p;
517 /* If we can't lock the tasklist, either with its explicit lock,
518 * or by using the RCU lock, then we can't safely work out the
519 * type of a keyring. So, we have to rely on the weak reference.
520 * If that's not available, then keyring based PAGs won't work.
523 #if defined(EXPORTED_TASKLIST_LOCK) || (LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,16) && defined(HAVE_LINUX_RCU_READ_LOCK))
524 if (__key_type_keyring == NULL) {
525 # ifdef EXPORTED_TASKLIST_LOCK
527 read_lock(&tasklist_lock);
529 # if (LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,16) && defined(HAVE_LINUX_RCU_READ_LOCK))
530 # ifdef EXPORTED_TASKLIST_LOCK
535 #if defined(HAVE_LINUX_FIND_TASK_BY_PID)
536 p = find_task_by_pid(1);
538 p = find_task_by_vpid(1);
540 if (p && task_user(p)->session_keyring)
541 __key_type_keyring = task_user(p)->session_keyring->type;
542 # ifdef EXPORTED_TASKLIST_LOCK
544 read_unlock(&tasklist_lock);
546 # if (LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,16) && defined(HAVE_LINUX_RCU_READ_LOCK))
547 # ifdef EXPORTED_TASKLIST_LOCK
556 register_key_type(&key_type_afs_pag);
559 void osi_keyring_shutdown(void)
561 unregister_key_type(&key_type_afs_pag);
565 osi_get_keyring_pag(afs_ucred_t *cred)
569 afs_int32 keyring_pag = NOPAG;
571 if (afs_cr_rgid(cred) != NFSXLATOR_CRED) {
572 key = afs_linux_search_keyring(cred, &key_type_afs_pag);
575 if (key_validate(key) == 0 && key->uid == 0) { /* also verify in the session keyring? */
576 keyring_pag = key->payload.value;
577 /* Only set PAG in groups if needed,
578 * and the creds are from the current process */
579 if (afs_linux_cred_is_current(cred) &&
580 ((keyring_pag >> 24) & 0xff) == 'A' &&
581 keyring_pag != afs_linux_pag_from_groups(current_group_info())) {
582 __setpag(&cred, keyring_pag, &newpag, 0);
592 void osi_keyring_init(void)
597 void osi_keyring_shutdown(void)