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 /* Before these flags were added in Linux commit v2.6.18-rc1~816,
39 * key_alloc just took a boolean not_in_quota */
40 # define KEY_ALLOC_IN_QUOTA 0
41 # define KEY_ALLOC_NOT_IN_QUOTA 1
45 #ifdef AFS_LINUX26_ONEGROUP_ENV
46 # define NUMPAGGROUPS 1
49 afs_linux_pag_from_groups(struct group_info *group_info) {
53 if (group_info->ngroups < NUMPAGGROUPS)
56 for (i = 0; (i < group_info->ngroups &&
57 (g0 = GROUP_AT(group_info, i)) != (gid_t) NOGROUP); i++) {
58 if (((g0 >> 24) & 0xff) == 'A')
65 afs_linux_pag_to_groups(afs_uint32 newpag,
66 struct group_info *old, struct group_info **new) {
71 if (afs_linux_pag_from_groups(old) == NOPAG)
72 need_space = NUMPAGGROUPS;
74 *new = groups_alloc(old->ngroups + need_space);
76 for (i = 0, j = 0; i < old->ngroups; ++i) {
77 int ths = GROUP_AT(old, i);
78 int last = i > 0 ? GROUP_AT(old, i-1) : 0;
79 if ((ths >> 24) == 'A')
81 if (last <= newpag && ths > newpag) {
82 GROUP_AT(*new, j) = newpag;
85 GROUP_AT(*new, j) = ths;
88 if (j != i + need_space)
89 GROUP_AT(*new, j) = newpag;
93 # define NUMPAGGROUPS 2
95 static inline afs_uint32
96 afs_linux_pag_from_groups(struct group_info *group_info) {
98 if (group_info->ngroups < NUMPAGGROUPS)
101 return afs_get_pag_from_groups(GROUP_AT(group_info, 0), GROUP_AT(group_info, 1));
105 afs_linux_pag_to_groups(afs_uint32 newpag,
106 struct group_info *old, struct group_info *new) {
112 if (afs_linux_pag_from_groups(old) == NOPAG)
113 need_space = NUMPAGGGROUPS;
115 *new = groups_alloc(old->ngroups + need_space);
117 for (i = 0; i < old->ngroups; ++i)
118 GROUP_AT(new, i + need_space) = GROUP_AT(old, i);
120 afs_get_groups_from_pag(newpag, &g0, g1);
121 GROUP_AT(new, 0) = g0;
122 GROUP_AT(new, 1) = g1;
127 osi_get_group_pag(afs_ucred_t *cred) {
128 return afs_linux_pag_from_groups(afs_cr_group_info(cred));
133 afs_setgroups(cred_t **cr, struct group_info *group_info, int change_parent)
135 struct group_info *old_info;
137 AFS_STATCNT(afs_setgroups);
139 old_info = afs_cr_group_info(*cr);
140 get_group_info(group_info);
141 afs_set_cr_group_info(*cr, group_info);
142 put_group_info(old_info);
146 #if defined(STRUCT_TASK_STRUCT_HAS_PARENT) && !defined(STRUCT_TASK_STRUCT_HAS_CRED)
148 old_info = current->parent->group_info;
149 get_group_info(group_info);
150 current->parent->group_info = group_info;
151 put_group_info(old_info);
159 __setpag(cred_t **cr, afs_uint32 pagvalue, afs_uint32 *newpag,
162 struct group_info *group_info;
163 struct group_info *tmp;
165 get_group_info(afs_cr_group_info(*cr));
166 group_info = afs_cr_group_info(*cr);
168 *newpag = (pagvalue == -1 ? genpag() : pagvalue);
169 afs_linux_pag_to_groups(*newpag, group_info, &tmp);
171 put_group_info(group_info);
174 afs_setgroups(cr, group_info, change_parent);
176 put_group_info(group_info);
181 #ifdef LINUX_KEYRING_SUPPORT
182 extern struct key_type key_type_keyring __attribute__((weak));
183 static struct key_type *__key_type_keyring = &key_type_keyring;
186 install_session_keyring(struct key *keyring)
193 if (!__key_type_keyring)
198 /* create an empty session keyring */
199 sprintf(desc, "_ses.%u", current->tgid);
201 /* if we're root, don't count the keyring against our quota. This
202 * avoids starvation issues when dealing with PAM modules that always
203 * setpag() as root */
204 if (current_uid() == 0)
205 flags = KEY_ALLOC_NOT_IN_QUOTA;
207 flags = KEY_ALLOC_IN_QUOTA;
209 keyring = afs_linux_key_alloc(
210 __key_type_keyring, desc,
211 current_uid(), current_gid(),
212 (KEY_POS_ALL & ~KEY_POS_SETATTR) | KEY_USR_ALL,
215 if (IS_ERR(keyring)) {
216 code = PTR_ERR(keyring);
221 code = key_instantiate_and_link(keyring, NULL, 0, NULL, NULL);
227 /* install the keyring */
228 spin_lock_irq(¤t->sighand->siglock);
229 old = task_session_keyring(current);
231 task_session_keyring(current) = keyring;
232 spin_unlock_irq(¤t->sighand->siglock);
240 #endif /* LINUX_KEYRING_SUPPORT */
243 setpag(cred_t **cr, afs_uint32 pagvalue, afs_uint32 *newpag,
250 code = __setpag(cr, pagvalue, newpag, change_parent);
252 #ifdef LINUX_KEYRING_SUPPORT
253 if (code == 0 && afs_cr_rgid(*cr) != NFSXLATOR_CRED) {
254 code = install_session_keyring(NULL);
255 if (code == 0 && current_session_keyring()) {
259 perm = KEY_POS_VIEW | KEY_POS_SEARCH;
260 perm |= KEY_USR_VIEW | KEY_USR_SEARCH;
262 key = afs_linux_key_alloc(&key_type_afs_pag, "_pag", 0, 0, perm, KEY_ALLOC_NOT_IN_QUOTA);
265 key_instantiate_and_link(key, (void *) newpag, sizeof(afs_uint32),
266 current_session_keyring(), NULL);
273 #endif /* LINUX_KEYRING_SUPPORT */
279 /* Intercept the standard system call. */
280 extern asmlinkage long (*sys_setgroupsp) (int gidsetsize, gid_t * grouplist);
282 afs_xsetgroups(int gidsetsize, gid_t * grouplist)
285 cred_t *cr = crref();
289 old_pag = PagInCred(cr);
292 code = (*sys_setgroupsp) (gidsetsize, grouplist);
298 if (old_pag != NOPAG && PagInCred(cr) == NOPAG) {
299 /* re-install old pag if there's room. */
300 code = __setpag(&cr, old_pag, &junk, 0);
304 /* Linux syscall ABI returns errno as negative */
308 /* Intercept the standard uid32 system call. */
309 extern asmlinkage int (*sys_setgroups32p) (int gidsetsize,
310 __kernel_gid32_t * grouplist);
312 afs_xsetgroups32(int gidsetsize, gid_t * grouplist)
315 cred_t *cr = crref();
319 old_pag = PagInCred(cr);
322 code = (*sys_setgroups32p) (gidsetsize, grouplist);
329 if (old_pag != NOPAG && PagInCred(cr) == NOPAG) {
330 /* re-install old pag if there's room. */
331 code = __setpag(&cr, old_pag, &junk, 0);
335 /* Linux syscall ABI returns errno as negative */
339 #if defined(AFS_PPC64_LINUX20_ENV)
340 /* Intercept the uid16 system call as used by 32bit programs. */
341 extern asmlinkage long (*sys32_setgroupsp)(int gidsetsize, gid_t *grouplist);
342 asmlinkage long afs32_xsetgroups(int gidsetsize, gid_t *grouplist)
345 cred_t *cr = crref();
349 old_pag = PagInCred(cr);
352 code = (*sys32_setgroupsp)(gidsetsize, grouplist);
358 if (old_pag != NOPAG && PagInCred(cr) == NOPAG) {
359 /* re-install old pag if there's room. */
360 code = __setpag(&cr, old_pag, &junk, 0);
364 /* Linux syscall ABI returns errno as negative */
369 #if defined(AFS_SPARC64_LINUX20_ENV) || defined(AFS_AMD64_LINUX20_ENV)
370 /* Intercept the uid16 system call as used by 32bit programs. */
371 #ifdef AFS_AMD64_LINUX20_ENV
372 extern asmlinkage long (*sys32_setgroupsp) (int gidsetsize, u16 * grouplist);
373 #endif /* AFS_AMD64_LINUX20_ENV */
374 #ifdef AFS_SPARC64_LINUX26_ENV
375 extern asmlinkage int (*sys32_setgroupsp) (int gidsetsize,
376 __kernel_gid32_t * grouplist);
377 #endif /* AFS_SPARC64_LINUX26_ENV */
379 afs32_xsetgroups(int gidsetsize, u16 * grouplist)
382 cred_t *cr = crref();
386 old_pag = PagInCred(cr);
389 code = (*sys32_setgroupsp) (gidsetsize, grouplist);
395 if (old_pag != NOPAG && PagInCred(cr) == NOPAG) {
396 /* re-install old pag if there's room. */
397 code = __setpag(&cr, old_pag, &junk, 0);
401 /* Linux syscall ABI returns errno as negative */
405 /* Intercept the uid32 system call as used by 32bit programs. */
406 #ifdef AFS_AMD64_LINUX20_ENV
407 extern asmlinkage long (*sys32_setgroups32p) (int gidsetsize, gid_t * grouplist);
408 #endif /* AFS_AMD64_LINUX20_ENV */
409 #ifdef AFS_SPARC64_LINUX26_ENV
410 extern asmlinkage int (*sys32_setgroups32p) (int gidsetsize,
411 __kernel_gid32_t * grouplist);
412 #endif /* AFS_SPARC64_LINUX26_ENV */
414 afs32_xsetgroups32(int gidsetsize, gid_t * grouplist)
417 cred_t *cr = crref();
421 old_pag = PagInCred(cr);
424 code = (*sys32_setgroups32p) (gidsetsize, grouplist);
430 if (old_pag != NOPAG && PagInCred(cr) == NOPAG) {
431 /* re-install old pag if there's room. */
432 code = __setpag(&cr, old_pag, &junk, 0);
436 /* Linux syscall ABI returns errno as negative */
442 #ifdef LINUX_KEYRING_SUPPORT
443 static void afs_pag_describe(const struct key *key, struct seq_file *m)
445 seq_puts(m, key->description);
447 seq_printf(m, ": %u", key->datalen);
450 static int afs_pag_instantiate(struct key *key, const void *data, size_t datalen)
453 afs_uint32 *userpag, pag = NOPAG;
455 if (key->uid != 0 || key->gid != 0)
459 get_group_info(current_group_info());
461 if (datalen != sizeof(afs_uint32) || !data)
464 /* ensure key being set matches current pag */
465 pag = afs_linux_pag_from_groups(current_group_info());
470 userpag = (afs_uint32 *) data;
474 key->payload.value = (unsigned long) *userpag;
475 key->datalen = sizeof(afs_uint32);
479 put_group_info(current_group_info());
483 static int afs_pag_match(const struct key *key, const void *description)
485 return strcmp(key->description, description) == 0;
488 static void afs_pag_destroy(struct key *key)
490 afs_uint32 pag = key->payload.value;
491 int locked = ISAFS_GLOCK();
496 afs_MarkUserExpired(pag);
502 struct key_type key_type_afs_pag =
505 .describe = afs_pag_describe,
506 .instantiate = afs_pag_instantiate,
507 .match = afs_pag_match,
508 .destroy = afs_pag_destroy,
511 #ifdef EXPORTED_TASKLIST_LOCK
512 extern rwlock_t tasklist_lock __attribute__((weak));
515 void osi_keyring_init(void)
517 #if !defined(EXPORTED_KEY_TYPE_KEYRING)
518 struct task_struct *p;
520 /* If we can't lock the tasklist, either with its explicit lock,
521 * or by using the RCU lock, then we can't safely work out the
522 * type of a keyring. So, we have to rely on the weak reference.
523 * If that's not available, then keyring based PAGs won't work.
526 #if defined(EXPORTED_TASKLIST_LOCK) || (LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,16) && defined(HAVE_LINUX_RCU_READ_LOCK))
527 if (__key_type_keyring == NULL) {
528 # ifdef EXPORTED_TASKLIST_LOCK
530 read_lock(&tasklist_lock);
532 # if (LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,16) && defined(HAVE_LINUX_RCU_READ_LOCK))
533 # ifdef EXPORTED_TASKLIST_LOCK
538 #if defined(HAVE_LINUX_FIND_TASK_BY_PID)
539 p = find_task_by_pid(1);
541 p = find_task_by_vpid(1);
543 if (p && task_user(p)->session_keyring)
544 __key_type_keyring = task_user(p)->session_keyring->type;
545 # ifdef EXPORTED_TASKLIST_LOCK
547 read_unlock(&tasklist_lock);
549 # if (LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,16) && defined(HAVE_LINUX_RCU_READ_LOCK))
550 # ifdef EXPORTED_TASKLIST_LOCK
559 register_key_type(&key_type_afs_pag);
562 void osi_keyring_shutdown(void)
564 unregister_key_type(&key_type_afs_pag);
568 osi_get_keyring_pag(afs_ucred_t *cred)
572 afs_int32 keyring_pag = NOPAG;
574 if (afs_cr_rgid(cred) != NFSXLATOR_CRED) {
575 key = afs_linux_search_keyring(cred, &key_type_afs_pag);
578 if (key_validate(key) == 0 && key->uid == 0) { /* also verify in the session keyring? */
579 keyring_pag = key->payload.value;
580 /* Only set PAG in groups if needed,
581 * and the creds are from the current process */
582 if (afs_linux_cred_is_current(cred) &&
583 ((keyring_pag >> 24) & 0xff) == 'A' &&
584 keyring_pag != afs_linux_pag_from_groups(current_group_info())) {
585 __setpag(&cred, keyring_pag, &newpag, 0);
595 void osi_keyring_init(void)
600 void osi_keyring_shutdown(void)