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 #ifdef AFS_LINUX26_ONEGROUP_ENV
30 # define NUMPAGGROUPS 1
33 afs_linux_pag_from_groups(struct group_info *group_info) {
37 if (group_info->ngroups < NUMPAGGROUPS)
40 for (i = 0; i < group_info->ngroups; i++) {
41 g0 = afs_from_kgid(GROUP_AT(group_info, i));
42 if (((g0 >> 24) & 0xff) == 'A')
49 afs_linux_pag_to_groups(afs_uint32 newpag,
50 struct group_info *old, struct group_info **new) {
54 afs_kgid_t newkgid = afs_make_kgid(newpag);
56 if (afs_linux_pag_from_groups(old) == NOPAG)
57 need_space = NUMPAGGROUPS;
59 *new = groups_alloc(old->ngroups + need_space);
61 for (i = 0, j = 0; i < old->ngroups; ++i) {
62 afs_kgid_t ths = GROUP_AT(old, i);
63 if ((afs_from_kgid(ths) >> 24) == 'A')
65 if ((i == 0 || !gid_lt(newkgid, GROUP_AT(old, i-1))) &&
66 gid_lt(newkgid, ths)) {
67 GROUP_AT(*new, j) = newkgid;
70 GROUP_AT(*new, j) = ths;
73 if (j != i + need_space)
74 GROUP_AT(*new, j) = newkgid;
78 # define NUMPAGGROUPS 2
80 static inline afs_uint32
81 afs_linux_pag_from_groups(struct group_info *group_info) {
83 if (group_info->ngroups < NUMPAGGROUPS)
86 return afs_get_pag_from_groups(GROUP_AT(group_info, 0), GROUP_AT(group_info, 1));
90 afs_linux_pag_to_groups(afs_uint32 newpag,
91 struct group_info *old, struct group_info **new) {
97 if (afs_linux_pag_from_groups(old) == NOPAG)
98 need_space = NUMPAGGGROUPS;
100 *new = groups_alloc(old->ngroups + need_space);
102 for (i = 0; i < old->ngroups; ++i)
103 GROUP_AT(new, i + need_space) = GROUP_AT(old, i);
105 afs_get_groups_from_pag(newpag, &g0, g1);
106 GROUP_AT(new, 0) = g0;
107 GROUP_AT(new, 1) = g1;
112 osi_get_group_pag(afs_ucred_t *cred) {
113 return afs_linux_pag_from_groups(afs_cr_group_info(cred));
118 afs_setgroups(cred_t **cr, struct group_info *group_info, int change_parent)
120 struct group_info *old_info;
122 AFS_STATCNT(afs_setgroups);
124 old_info = afs_cr_group_info(*cr);
125 get_group_info(group_info);
126 afs_set_cr_group_info(*cr, group_info);
127 put_group_info(old_info);
131 #if defined(STRUCT_TASK_STRUCT_HAS_PARENT) && !defined(STRUCT_TASK_STRUCT_HAS_CRED)
133 old_info = current->parent->group_info;
134 get_group_info(group_info);
135 current->parent->group_info = group_info;
136 put_group_info(old_info);
144 __setpag(cred_t **cr, afs_uint32 pagvalue, afs_uint32 *newpag,
145 int change_parent, struct group_info **old_groups)
147 struct group_info *group_info;
148 struct group_info *tmp;
150 get_group_info(afs_cr_group_info(*cr));
151 group_info = afs_cr_group_info(*cr);
153 *newpag = (pagvalue == -1 ? genpag() : pagvalue);
154 afs_linux_pag_to_groups(*newpag, group_info, &tmp);
157 *old_groups = group_info;
159 put_group_info(group_info);
163 afs_setgroups(cr, tmp, change_parent);
170 #ifdef LINUX_KEYRING_SUPPORT
171 extern struct key_type key_type_keyring __attribute__((weak));
172 static struct key_type *__key_type_keyring = &key_type_keyring;
174 /* install_session_keyring returns negative error values */
176 install_session_keyring(struct key *keyring)
183 if (!__key_type_keyring)
188 /* create an empty session keyring */
189 sprintf(desc, "_ses.%u", current->tgid);
191 /* if we're root, don't count the keyring against our quota. This
192 * avoids starvation issues when dealing with PAM modules that always
193 * setpag() as root */
194 if (capable(CAP_SYS_ADMIN))
195 flags = KEY_ALLOC_NOT_IN_QUOTA;
197 flags = KEY_ALLOC_IN_QUOTA;
199 keyring = afs_linux_key_alloc(
200 __key_type_keyring, desc,
201 current_uid(), current_gid(),
202 (KEY_POS_ALL & ~KEY_POS_SETATTR) | KEY_USR_ALL,
205 if (IS_ERR(keyring)) {
206 code = PTR_ERR(keyring);
211 code = key_instantiate_and_link(keyring, NULL, 0, NULL, NULL);
217 /* install the keyring */
218 old = afs_set_session_keyring(keyring);
225 #endif /* LINUX_KEYRING_SUPPORT */
227 /* Error codes from setpag must be positive, otherwise they don't
228 * make it back into userspace properly. Error codes from the
229 * Linux keyring utilities, and from install_session_keyring()
230 * are negative. So we need to be careful to convert them correctly
234 setpag(cred_t **cr, afs_uint32 pagvalue, afs_uint32 *newpag,
238 struct group_info *old_groups = NULL;
242 code = __setpag(cr, pagvalue, newpag, change_parent, &old_groups);
244 #ifdef LINUX_KEYRING_SUPPORT
245 if (code == 0 && afs_cr_rgid(*cr) != NFSXLATOR_CRED) {
246 code = install_session_keyring(NULL);
247 if (code == 0 && current_session_keyring()) {
251 perm = KEY_POS_VIEW | KEY_POS_SEARCH;
252 perm |= KEY_USR_VIEW | KEY_USR_SEARCH;
254 key = afs_linux_key_alloc(&key_type_afs_pag, "_pag", GLOBAL_ROOT_UID, GLOBAL_ROOT_GID, perm, KEY_ALLOC_NOT_IN_QUOTA);
257 code = key_instantiate_and_link(key, (void *) newpag, sizeof(afs_uint32),
258 current_session_keyring(), NULL);
267 #endif /* LINUX_KEYRING_SUPPORT */
271 afs_setgroups(cr, old_groups, change_parent);
272 put_group_info(old_groups);
276 afs_MarkUserExpired(*newpag);
285 /* Intercept the standard system call. */
286 extern asmlinkage long (*sys_setgroupsp) (int gidsetsize, gid_t * grouplist);
288 afs_xsetgroups(int gidsetsize, gid_t * grouplist)
291 cred_t *cr = crref();
295 old_pag = PagInCred(cr);
298 code = (*sys_setgroupsp) (gidsetsize, grouplist);
304 if (old_pag != NOPAG && PagInCred(cr) == NOPAG) {
305 /* re-install old pag if there's room. */
306 code = __setpag(&cr, old_pag, &junk, 0, NULL);
310 /* Linux syscall ABI returns errno as negative */
314 /* Intercept the standard uid32 system call. */
315 extern asmlinkage int (*sys_setgroups32p) (int gidsetsize,
316 __kernel_gid32_t * grouplist);
318 afs_xsetgroups32(int gidsetsize, gid_t * grouplist)
321 cred_t *cr = crref();
325 old_pag = PagInCred(cr);
328 code = (*sys_setgroups32p) (gidsetsize, grouplist);
335 if (old_pag != NOPAG && PagInCred(cr) == NOPAG) {
336 /* re-install old pag if there's room. */
337 code = __setpag(&cr, old_pag, &junk, 0, NULL);
341 /* Linux syscall ABI returns errno as negative */
345 #if defined(AFS_PPC64_LINUX20_ENV)
346 /* Intercept the uid16 system call as used by 32bit programs. */
347 extern asmlinkage long (*sys32_setgroupsp)(int gidsetsize, gid_t *grouplist);
348 asmlinkage long afs32_xsetgroups(int gidsetsize, gid_t *grouplist)
351 cred_t *cr = crref();
355 old_pag = PagInCred(cr);
358 code = (*sys32_setgroupsp)(gidsetsize, grouplist);
364 if (old_pag != NOPAG && PagInCred(cr) == NOPAG) {
365 /* re-install old pag if there's room. */
366 code = __setpag(&cr, old_pag, &junk, 0, NULL);
370 /* Linux syscall ABI returns errno as negative */
375 #if defined(AFS_SPARC64_LINUX20_ENV) || defined(AFS_AMD64_LINUX20_ENV)
376 /* Intercept the uid16 system call as used by 32bit programs. */
377 #ifdef AFS_AMD64_LINUX20_ENV
378 extern asmlinkage long (*sys32_setgroupsp) (int gidsetsize, u16 * grouplist);
379 #endif /* AFS_AMD64_LINUX20_ENV */
380 #ifdef AFS_SPARC64_LINUX26_ENV
381 extern asmlinkage int (*sys32_setgroupsp) (int gidsetsize,
382 __kernel_gid32_t * grouplist);
383 #endif /* AFS_SPARC64_LINUX26_ENV */
385 afs32_xsetgroups(int gidsetsize, u16 * grouplist)
388 cred_t *cr = crref();
392 old_pag = PagInCred(cr);
395 code = (*sys32_setgroupsp) (gidsetsize, grouplist);
401 if (old_pag != NOPAG && PagInCred(cr) == NOPAG) {
402 /* re-install old pag if there's room. */
403 code = __setpag(&cr, old_pag, &junk, 0, NULL);
407 /* Linux syscall ABI returns errno as negative */
411 /* Intercept the uid32 system call as used by 32bit programs. */
412 #ifdef AFS_AMD64_LINUX20_ENV
413 extern asmlinkage long (*sys32_setgroups32p) (int gidsetsize, gid_t * grouplist);
414 #endif /* AFS_AMD64_LINUX20_ENV */
415 #ifdef AFS_SPARC64_LINUX26_ENV
416 extern asmlinkage int (*sys32_setgroups32p) (int gidsetsize,
417 __kernel_gid32_t * grouplist);
418 #endif /* AFS_SPARC64_LINUX26_ENV */
420 afs32_xsetgroups32(int gidsetsize, gid_t * grouplist)
423 cred_t *cr = crref();
427 old_pag = PagInCred(cr);
430 code = (*sys32_setgroups32p) (gidsetsize, grouplist);
436 if (old_pag != NOPAG && PagInCred(cr) == NOPAG) {
437 /* re-install old pag if there's room. */
438 code = __setpag(&cr, old_pag, &junk, 0, NULL);
442 /* Linux syscall ABI returns errno as negative */
448 #ifdef LINUX_KEYRING_SUPPORT
449 static void afs_pag_describe(const struct key *key, struct seq_file *m)
451 seq_puts(m, key->description);
453 seq_printf(m, ": %u", key->datalen);
456 #if defined(STRUCT_KEY_TYPE_HAS_PREPARSE)
457 static int afs_pag_instantiate(struct key *key, struct key_preparsed_payload *prep)
459 static int afs_pag_instantiate(struct key *key, const void *data, size_t datalen)
463 afs_uint32 *userpag, pag = NOPAG;
465 if (!uid_eq(key->uid, GLOBAL_ROOT_UID) || !gid_eq(key->gid, GLOBAL_ROOT_GID))
469 get_group_info(current_group_info());
471 #if defined(STRUCT_KEY_TYPE_HAS_PREPARSE)
472 if (prep->datalen != sizeof(afs_uint32) || !prep->data)
474 if (datalen != sizeof(afs_uint32) || !data)
478 /* ensure key being set matches current pag */
479 pag = afs_linux_pag_from_groups(current_group_info());
484 #if defined(STRUCT_KEY_TYPE_HAS_PREPARSE)
485 userpag = (afs_uint32 *)prep->data;
487 userpag = (afs_uint32 *)data;
492 key->payload.value = (unsigned long) *userpag;
493 key->datalen = sizeof(afs_uint32);
497 put_group_info(current_group_info());
501 #if defined(STRUCT_KEY_TYPE_HAS_MATCH)
502 static int afs_pag_match(const struct key *key, const void *description)
504 return strcmp(key->description, description) == 0;
508 static void afs_pag_destroy(struct key *key)
510 afs_uint32 pag = key->payload.value;
511 int locked = ISAFS_GLOCK();
516 afs_MarkUserExpired(pag);
522 struct key_type key_type_afs_pag =
525 .describe = afs_pag_describe,
526 #if defined(STRUCT_KEY_TYPE_HAS_INSTANTIATE_PREP)
527 .instantiate_prep = afs_pag_instantiate,
530 .instantiate = afs_pag_instantiate,
532 #if defined(STRUCT_KEY_TYPE_HAS_MATCH)
533 .match = afs_pag_match,
535 .destroy = afs_pag_destroy,
538 #ifdef EXPORTED_TASKLIST_LOCK
539 extern rwlock_t tasklist_lock __attribute__((weak));
542 void osi_keyring_init(void)
544 #if !defined(EXPORTED_KEY_TYPE_KEYRING)
545 struct task_struct *p;
547 /* If we can't lock the tasklist, either with its explicit lock,
548 * or by using the RCU lock, then we can't safely work out the
549 * type of a keyring. So, we have to rely on the weak reference.
550 * If that's not available, then keyring based PAGs won't work.
553 #if defined(EXPORTED_TASKLIST_LOCK) || (LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,16) && defined(HAVE_LINUX_RCU_READ_LOCK))
554 if (__key_type_keyring == NULL) {
555 # ifdef EXPORTED_TASKLIST_LOCK
557 read_lock(&tasklist_lock);
559 # if (LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,16) && defined(HAVE_LINUX_RCU_READ_LOCK))
560 # ifdef EXPORTED_TASKLIST_LOCK
565 #if defined(HAVE_LINUX_FIND_TASK_BY_PID)
566 p = find_task_by_pid(1);
568 p = find_task_by_vpid(1);
570 if (p && task_user(p)->session_keyring)
571 __key_type_keyring = task_user(p)->session_keyring->type;
572 # ifdef EXPORTED_TASKLIST_LOCK
574 read_unlock(&tasklist_lock);
576 # if (LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,16) && defined(HAVE_LINUX_RCU_READ_LOCK))
577 # ifdef EXPORTED_TASKLIST_LOCK
586 register_key_type(&key_type_afs_pag);
589 void osi_keyring_shutdown(void)
591 unregister_key_type(&key_type_afs_pag);
595 osi_get_keyring_pag(afs_ucred_t *cred)
599 afs_int32 keyring_pag = NOPAG;
601 if (afs_cr_rgid(cred) != NFSXLATOR_CRED) {
602 key = afs_linux_search_keyring(cred, &key_type_afs_pag);
605 if (key_validate(key) == 0 && uid_eq(key->uid, GLOBAL_ROOT_UID)) { /* also verify in the session keyring? */
606 keyring_pag = key->payload.value;
607 /* Only set PAG in groups if needed,
608 * and the creds are from the current process */
609 if (afs_linux_cred_is_current(cred) &&
610 ((keyring_pag >> 24) & 0xff) == 'A' &&
611 keyring_pag != afs_linux_pag_from_groups(current_group_info())) {
612 __setpag(&cred, keyring_pag, &newpag, 0, NULL);
622 void osi_keyring_init(void)
627 void osi_keyring_shutdown(void)