1 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
2 * vim:expandtab:shiftwidth=8:tabstop=8:
6 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License version 2 only,
10 * as published by the Free Software Foundation.
12 * This program is distributed in the hope that it will be useful, but
13 * WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * General Public License version 2 for more details (a copy is included
16 * in the LICENSE file that accompanied this code).
18 * You should have received a copy of the GNU General Public License
19 * version 2 along with this program; If not, see
20 * http://www.sun.com/software/products/lustre/docs/GPLv2.pdf
22 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
23 * CA 95054 USA or visit www.sun.com if you need additional information or
29 * Copyright (c) 2007, 2010, Oracle and/or its affiliates. All rights reserved.
30 * Use is subject to license terms.
33 * This file is part of Lustre, http://www.lustre.org/
34 * Lustre is a trademark of Sun Microsystems, Inc.
36 * lustre/ptlrpc/gss/gss_keyring.c
38 * Author: Eric Mei <ericm@clusterfs.com>
42 # define EXPORT_SYMTAB
44 #define DEBUG_SUBSYSTEM S_SEC
46 #include <linux/init.h>
47 #include <linux/module.h>
48 #include <linux/slab.h>
49 #include <linux/dcache.h>
51 #include <linux/crypto.h>
52 #include <linux/key.h>
53 #include <linux/keyctl.h>
54 #include <linux/mutex.h>
55 #include <asm/atomic.h>
57 #include <liblustre.h>
61 #include <obd_class.h>
62 #include <obd_support.h>
63 #include <lustre/lustre_idl.h>
64 #include <lustre_sec.h>
65 #include <lustre_net.h>
66 #include <lustre_import.h>
69 #include "gss_internal.h"
72 static struct ptlrpc_sec_policy gss_policy_keyring;
73 static struct ptlrpc_ctx_ops gss_keyring_ctxops;
74 static struct key_type gss_key_type;
76 static int sec_install_rctx_kr(struct ptlrpc_sec *sec,
77 struct ptlrpc_svc_ctx *svc_ctx);
80 * the timeout is only for the case that upcall child process die abnormally.
81 * in any other cases it should finally update kernel key.
83 * FIXME we'd better to incorporate the client & server side upcall timeouts
84 * into the framework of Adaptive Timeouts, but we need to figure out how to
85 * make sure that kernel knows the upcall processes is in-progress or died
88 #define KEYRING_UPCALL_TIMEOUT (obd_timeout + obd_timeout)
90 /****************************************
92 ****************************************/
94 #define DUMP_PROCESS_KEYRINGS(tsk) \
96 CWARN("DUMP PK: %s[%u,%u/%u](<-%s[%u,%u/%u]): " \
97 "a %d, t %d, p %d, s %d, u %d, us %d, df %d\n", \
98 tsk->comm, tsk->pid, tsk->uid, tsk->fsuid, \
99 tsk->parent->comm, tsk->parent->pid, \
100 tsk->parent->uid, tsk->parent->fsuid, \
101 tsk->request_key_auth ? \
102 tsk->request_key_auth->serial : 0, \
103 tsk->thread_keyring ? \
104 tsk->thread_keyring->serial : 0, \
105 tsk->signal->process_keyring ? \
106 tsk->signal->process_keyring->serial : 0, \
107 tsk->signal->session_keyring ? \
108 tsk->signal->session_keyring->serial : 0, \
109 tsk->user->uid_keyring ? \
110 tsk->user->uid_keyring->serial : 0, \
111 tsk->user->session_keyring ? \
112 tsk->user->session_keyring->serial : 0, \
117 #define DUMP_KEY(key) \
119 CWARN("DUMP KEY: %p(%d) ref %d u%u/g%u desc %s\n", \
120 key, key->serial, atomic_read(&key->usage), \
121 key->uid, key->gid, \
122 key->description ? key->description : "n/a" \
127 static inline void keyring_upcall_lock(struct gss_sec_keyring *gsec_kr)
129 #ifdef HAVE_KEYRING_UPCALL_SERIALIZED
130 cfs_mutex_lock(&gsec_kr->gsk_uc_lock);
134 static inline void keyring_upcall_unlock(struct gss_sec_keyring *gsec_kr)
136 #ifdef HAVE_KEYRING_UPCALL_SERIALIZED
137 cfs_mutex_unlock(&gsec_kr->gsk_uc_lock);
141 static inline void key_revoke_locked(struct key *key)
143 set_bit(KEY_FLAG_REVOKED, &key->flags);
146 static void ctx_upcall_timeout_kr(unsigned long data)
148 struct ptlrpc_cli_ctx *ctx = (struct ptlrpc_cli_ctx *) data;
149 struct key *key = ctx2gctx_keyring(ctx)->gck_key;
151 CWARN("ctx %p, key %p\n", ctx, key);
156 key_revoke_locked(key);
160 void ctx_start_timer_kr(struct ptlrpc_cli_ctx *ctx, long timeout)
162 struct gss_cli_ctx_keyring *gctx_kr = ctx2gctx_keyring(ctx);
163 struct timer_list *timer = gctx_kr->gck_timer;
167 CDEBUG(D_SEC, "ctx %p: start timer %lds\n", ctx, timeout);
168 timeout = timeout * CFS_HZ + cfs_time_current();
171 timer->expires = timeout;
172 timer->data = (unsigned long ) ctx;
173 timer->function = ctx_upcall_timeout_kr;
179 * caller should make sure no race with other threads
182 void ctx_clear_timer_kr(struct ptlrpc_cli_ctx *ctx)
184 struct gss_cli_ctx_keyring *gctx_kr = ctx2gctx_keyring(ctx);
185 struct timer_list *timer = gctx_kr->gck_timer;
190 CDEBUG(D_SEC, "ctx %p, key %p\n", ctx, gctx_kr->gck_key);
192 gctx_kr->gck_timer = NULL;
194 del_singleshot_timer_sync(timer);
200 struct ptlrpc_cli_ctx *ctx_create_kr(struct ptlrpc_sec *sec,
201 struct vfs_cred *vcred)
203 struct ptlrpc_cli_ctx *ctx;
204 struct gss_cli_ctx_keyring *gctx_kr;
206 OBD_ALLOC_PTR(gctx_kr);
210 OBD_ALLOC_PTR(gctx_kr->gck_timer);
211 if (gctx_kr->gck_timer == NULL) {
212 OBD_FREE_PTR(gctx_kr);
215 init_timer(gctx_kr->gck_timer);
217 ctx = &gctx_kr->gck_base.gc_base;
219 if (gss_cli_ctx_init_common(sec, ctx, &gss_keyring_ctxops, vcred)) {
220 OBD_FREE_PTR(gctx_kr->gck_timer);
221 OBD_FREE_PTR(gctx_kr);
225 ctx->cc_expire = cfs_time_current_sec() + KEYRING_UPCALL_TIMEOUT;
226 cfs_clear_bit(PTLRPC_CTX_NEW_BIT, &ctx->cc_flags);
227 cfs_atomic_inc(&ctx->cc_refcount); /* for the caller */
232 static void ctx_destroy_kr(struct ptlrpc_cli_ctx *ctx)
234 struct ptlrpc_sec *sec = ctx->cc_sec;
235 struct gss_cli_ctx_keyring *gctx_kr = ctx2gctx_keyring(ctx);
237 CDEBUG(D_SEC, "destroying ctx %p\n", ctx);
239 /* at this time the association with key has been broken. */
241 LASSERT(cfs_atomic_read(&sec->ps_refcount) > 0);
242 LASSERT(cfs_atomic_read(&sec->ps_nctx) > 0);
243 LASSERT(cfs_test_bit(PTLRPC_CTX_CACHED_BIT, &ctx->cc_flags) == 0);
244 LASSERT(gctx_kr->gck_key == NULL);
246 ctx_clear_timer_kr(ctx);
247 LASSERT(gctx_kr->gck_timer == NULL);
249 if (gss_cli_ctx_fini_common(sec, ctx))
252 OBD_FREE_PTR(gctx_kr);
254 cfs_atomic_dec(&sec->ps_nctx);
255 sptlrpc_sec_put(sec);
258 static void ctx_release_kr(struct ptlrpc_cli_ctx *ctx, int sync)
263 cfs_atomic_inc(&ctx->cc_refcount);
264 sptlrpc_gc_add_ctx(ctx);
268 static void ctx_put_kr(struct ptlrpc_cli_ctx *ctx, int sync)
270 LASSERT(cfs_atomic_read(&ctx->cc_refcount) > 0);
272 if (cfs_atomic_dec_and_test(&ctx->cc_refcount))
273 ctx_release_kr(ctx, sync);
277 * key <-> ctx association and rules:
278 * - ctx might not bind with any key
279 * - key/ctx binding is protected by key semaphore (if the key present)
280 * - key and ctx each take a reference of the other
281 * - ctx enlist/unlist is protected by ctx spinlock
282 * - never enlist a ctx after it's been unlisted
283 * - whoever do enlist should also do bind, lock key before enlist:
284 * - lock key -> lock ctx -> enlist -> unlock ctx -> bind -> unlock key
285 * - whoever do unlist should also do unbind:
286 * - lock key -> lock ctx -> unlist -> unlock ctx -> unbind -> unlock key
287 * - lock ctx -> unlist -> unlock ctx -> lock key -> unbind -> unlock key
290 static inline void spin_lock_if(cfs_spinlock_t *lock, int condition)
296 static inline void spin_unlock_if(cfs_spinlock_t *lock, int condition)
299 cfs_spin_unlock(lock);
302 static void ctx_enlist_kr(struct ptlrpc_cli_ctx *ctx, int is_root, int locked)
304 struct ptlrpc_sec *sec = ctx->cc_sec;
305 struct gss_sec_keyring *gsec_kr = sec2gsec_keyring(sec);
307 LASSERT(!cfs_test_bit(PTLRPC_CTX_CACHED_BIT, &ctx->cc_flags));
308 LASSERT(cfs_atomic_read(&ctx->cc_refcount) > 0);
310 spin_lock_if(&sec->ps_lock, !locked);
312 cfs_atomic_inc(&ctx->cc_refcount);
313 cfs_set_bit(PTLRPC_CTX_CACHED_BIT, &ctx->cc_flags);
314 cfs_hlist_add_head(&ctx->cc_cache, &gsec_kr->gsk_clist);
316 gsec_kr->gsk_root_ctx = ctx;
318 spin_unlock_if(&sec->ps_lock, !locked);
322 * Note after this get called, caller should not access ctx again because
323 * it might have been freed, unless caller hold at least one refcount of
326 * return non-zero if we indeed unlist this ctx.
328 static int ctx_unlist_kr(struct ptlrpc_cli_ctx *ctx, int locked)
330 struct ptlrpc_sec *sec = ctx->cc_sec;
331 struct gss_sec_keyring *gsec_kr = sec2gsec_keyring(sec);
333 /* if hashed bit has gone, leave the job to somebody who is doing it */
334 if (cfs_test_and_clear_bit(PTLRPC_CTX_CACHED_BIT, &ctx->cc_flags) == 0)
337 /* drop ref inside spin lock to prevent race with other operations */
338 spin_lock_if(&sec->ps_lock, !locked);
340 if (gsec_kr->gsk_root_ctx == ctx)
341 gsec_kr->gsk_root_ctx = NULL;
342 cfs_hlist_del_init(&ctx->cc_cache);
343 cfs_atomic_dec(&ctx->cc_refcount);
345 spin_unlock_if(&sec->ps_lock, !locked);
351 * bind a key with a ctx together.
352 * caller must hold write lock of the key, as well as ref on key & ctx.
354 static void bind_key_ctx(struct key *key, struct ptlrpc_cli_ctx *ctx)
356 LASSERT(cfs_atomic_read(&ctx->cc_refcount) > 0);
357 LASSERT(atomic_read(&key->usage) > 0);
358 LASSERT(ctx2gctx_keyring(ctx)->gck_key == NULL);
359 LASSERT(key->payload.data == NULL);
361 /* at this time context may or may not in list. */
363 cfs_atomic_inc(&ctx->cc_refcount);
364 ctx2gctx_keyring(ctx)->gck_key = key;
365 key->payload.data = ctx;
369 * unbind a key and a ctx.
370 * caller must hold write lock, as well as a ref of the key.
372 static void unbind_key_ctx(struct key *key, struct ptlrpc_cli_ctx *ctx)
374 LASSERT(key->payload.data == ctx);
375 LASSERT(cfs_test_bit(PTLRPC_CTX_CACHED_BIT, &ctx->cc_flags) == 0);
377 /* must revoke the key, or others may treat it as newly created */
378 key_revoke_locked(key);
380 key->payload.data = NULL;
381 ctx2gctx_keyring(ctx)->gck_key = NULL;
383 /* once ctx get split from key, the timer is meaningless */
384 ctx_clear_timer_kr(ctx);
391 * given a ctx, unbind with its coupled key, if any.
392 * unbind could only be called once, so we don't worry the key be released
395 static void unbind_ctx_kr(struct ptlrpc_cli_ctx *ctx)
397 struct key *key = ctx2gctx_keyring(ctx)->gck_key;
400 LASSERT(key->payload.data == ctx);
403 down_write(&key->sem);
404 unbind_key_ctx(key, ctx);
411 * given a key, unbind with its coupled ctx, if any.
412 * caller must hold write lock, as well as a ref of the key.
414 static void unbind_key_locked(struct key *key)
416 struct ptlrpc_cli_ctx *ctx = key->payload.data;
419 unbind_key_ctx(key, ctx);
423 * unlist a ctx, and unbind from coupled key
425 static void kill_ctx_kr(struct ptlrpc_cli_ctx *ctx)
427 if (ctx_unlist_kr(ctx, 0))
432 * given a key, unlist and unbind with the coupled ctx (if any).
433 * caller must hold write lock, as well as a ref of the key.
435 static void kill_key_locked(struct key *key)
437 struct ptlrpc_cli_ctx *ctx = key->payload.data;
439 if (ctx && ctx_unlist_kr(ctx, 0))
440 unbind_key_locked(key);
444 * caller should hold one ref on contexts in freelist.
446 static void dispose_ctx_list_kr(cfs_hlist_head_t *freelist)
448 cfs_hlist_node_t *pos, *next;
449 struct ptlrpc_cli_ctx *ctx;
450 struct gss_cli_ctx *gctx;
452 cfs_hlist_for_each_entry_safe(ctx, pos, next, freelist, cc_cache) {
453 cfs_hlist_del_init(&ctx->cc_cache);
455 /* reverse ctx: update current seq to buddy svcctx if exist.
456 * ideally this should be done at gss_cli_ctx_finalize(), but
457 * the ctx destroy could be delayed by:
458 * 1) ctx still has reference;
459 * 2) ctx destroy is asynchronous;
460 * and reverse import call inval_all_ctx() require this be done
461 *_immediately_ otherwise newly created reverse ctx might copy
462 * the very old sequence number from svcctx. */
463 gctx = ctx2gctx(ctx);
464 if (!rawobj_empty(&gctx->gc_svc_handle) &&
465 sec_is_reverse(gctx->gc_base.cc_sec)) {
466 gss_svc_upcall_update_sequence(&gctx->gc_svc_handle,
467 (__u32) cfs_atomic_read(&gctx->gc_seq));
470 /* we need to wakeup waiting reqs here. the context might
471 * be forced released before upcall finished, then the
472 * late-arrived downcall can't find the ctx even. */
473 sptlrpc_cli_ctx_wakeup(ctx);
481 * lookup a root context directly in a sec, return root ctx with a
482 * reference taken or NULL.
485 struct ptlrpc_cli_ctx * sec_lookup_root_ctx_kr(struct ptlrpc_sec *sec)
487 struct gss_sec_keyring *gsec_kr = sec2gsec_keyring(sec);
488 struct ptlrpc_cli_ctx *ctx = NULL;
490 cfs_spin_lock(&sec->ps_lock);
492 ctx = gsec_kr->gsk_root_ctx;
494 if (ctx == NULL && unlikely(sec_is_reverse(sec))) {
495 cfs_hlist_node_t *node;
496 struct ptlrpc_cli_ctx *tmp;
498 /* reverse ctx, search root ctx in list, choose the one
499 * with shortest expire time, which is most possibly have
500 * an established peer ctx at client side. */
501 cfs_hlist_for_each_entry(tmp, node, &gsec_kr->gsk_clist,
503 if (ctx == NULL || ctx->cc_expire == 0 ||
504 ctx->cc_expire > tmp->cc_expire) {
506 /* promote to be root_ctx */
507 gsec_kr->gsk_root_ctx = ctx;
513 LASSERT(cfs_atomic_read(&ctx->cc_refcount) > 0);
514 LASSERT(!cfs_hlist_empty(&gsec_kr->gsk_clist));
515 cfs_atomic_inc(&ctx->cc_refcount);
518 cfs_spin_unlock(&sec->ps_lock);
523 #define RVS_CTX_EXPIRE_NICE (10)
526 void rvs_sec_install_root_ctx_kr(struct ptlrpc_sec *sec,
527 struct ptlrpc_cli_ctx *new_ctx,
530 struct gss_sec_keyring *gsec_kr = sec2gsec_keyring(sec);
531 cfs_hlist_node_t *hnode;
532 struct ptlrpc_cli_ctx *ctx;
536 LASSERT(sec_is_reverse(sec));
538 cfs_spin_lock(&sec->ps_lock);
540 now = cfs_time_current_sec();
542 /* set all existing ctxs short expiry */
543 cfs_hlist_for_each_entry(ctx, hnode, &gsec_kr->gsk_clist, cc_cache) {
544 if (ctx->cc_expire > now + RVS_CTX_EXPIRE_NICE) {
545 ctx->cc_early_expire = 1;
546 ctx->cc_expire = now + RVS_CTX_EXPIRE_NICE;
550 /* if there's root_ctx there, instead obsolete the current
551 * immediately, we leave it continue operating for a little while.
552 * hopefully when the first backward rpc with newest ctx send out,
553 * the client side already have the peer ctx well established. */
554 ctx_enlist_kr(new_ctx, gsec_kr->gsk_root_ctx ? 0 : 1, 1);
557 bind_key_ctx(key, new_ctx);
559 cfs_spin_unlock(&sec->ps_lock);
562 static void construct_key_desc(void *buf, int bufsize,
563 struct ptlrpc_sec *sec, uid_t uid)
565 snprintf(buf, bufsize, "%d@%x", uid, sec->ps_id);
566 ((char *)buf)[bufsize - 1] = '\0';
569 /****************************************
571 ****************************************/
574 struct ptlrpc_sec * gss_sec_create_kr(struct obd_import *imp,
575 struct ptlrpc_svc_ctx *svcctx,
576 struct sptlrpc_flavor *sf)
578 struct gss_sec_keyring *gsec_kr;
581 OBD_ALLOC(gsec_kr, sizeof(*gsec_kr));
585 CFS_INIT_HLIST_HEAD(&gsec_kr->gsk_clist);
586 gsec_kr->gsk_root_ctx = NULL;
587 cfs_mutex_init(&gsec_kr->gsk_root_uc_lock);
588 #ifdef HAVE_KEYRING_UPCALL_SERIALIZED
589 cfs_mutex_init(&gsec_kr->gsk_uc_lock);
592 if (gss_sec_create_common(&gsec_kr->gsk_base, &gss_policy_keyring,
596 if (svcctx != NULL &&
597 sec_install_rctx_kr(&gsec_kr->gsk_base.gs_base, svcctx)) {
598 gss_sec_destroy_common(&gsec_kr->gsk_base);
602 RETURN(&gsec_kr->gsk_base.gs_base);
605 OBD_FREE(gsec_kr, sizeof(*gsec_kr));
610 void gss_sec_destroy_kr(struct ptlrpc_sec *sec)
612 struct gss_sec *gsec = sec2gsec(sec);
613 struct gss_sec_keyring *gsec_kr = sec2gsec_keyring(sec);
615 CDEBUG(D_SEC, "destroy %s@%p\n", sec->ps_policy->sp_name, sec);
617 LASSERT(cfs_hlist_empty(&gsec_kr->gsk_clist));
618 LASSERT(gsec_kr->gsk_root_ctx == NULL);
620 gss_sec_destroy_common(gsec);
622 OBD_FREE(gsec_kr, sizeof(*gsec_kr));
625 static inline int user_is_root(struct ptlrpc_sec *sec, struct vfs_cred *vcred)
627 /* except the ROOTONLY flag, treat it as root user only if real uid
628 * is 0, euid/fsuid being 0 are handled as setuid scenarios */
629 if (sec_is_rootonly(sec) || (vcred->vc_uid == 0))
636 * unlink request key from it's ring, which is linked during request_key().
637 * sadly, we have to 'guess' which keyring it's linked to.
639 * FIXME this code is fragile, depend on how request_key_link() is implemented.
641 static void request_key_unlink(struct key *key)
643 struct task_struct *tsk = current;
646 switch (tsk->jit_keyring) {
647 case KEY_REQKEY_DEFL_DEFAULT:
648 case KEY_REQKEY_DEFL_THREAD_KEYRING:
649 ring = key_get(tsk->thread_keyring);
652 case KEY_REQKEY_DEFL_PROCESS_KEYRING:
653 ring = key_get(tsk->signal->process_keyring);
656 case KEY_REQKEY_DEFL_SESSION_KEYRING:
658 ring = key_get(rcu_dereference(tsk->signal->session_keyring));
662 case KEY_REQKEY_DEFL_USER_SESSION_KEYRING:
663 ring = key_get(tsk->user->session_keyring);
665 case KEY_REQKEY_DEFL_USER_KEYRING:
666 ring = key_get(tsk->user->uid_keyring);
668 case KEY_REQKEY_DEFL_GROUP_KEYRING:
674 key_unlink(ring, key);
679 struct ptlrpc_cli_ctx * gss_sec_lookup_ctx_kr(struct ptlrpc_sec *sec,
680 struct vfs_cred *vcred,
681 int create, int remove_dead)
683 struct obd_import *imp = sec->ps_import;
684 struct gss_sec_keyring *gsec_kr = sec2gsec_keyring(sec);
685 struct ptlrpc_cli_ctx *ctx = NULL;
686 unsigned int is_root = 0, create_new = 0;
694 LASSERT(imp != NULL);
696 is_root = user_is_root(sec, vcred);
698 /* a little bit optimization for root context */
700 ctx = sec_lookup_root_ctx_kr(sec);
702 * Only lookup directly for REVERSE sec, which should
705 if (ctx || sec_is_reverse(sec))
709 LASSERT(create != 0);
711 /* for root context, obtain lock and check again, this time hold
712 * the root upcall lock, make sure nobody else populated new root
713 * context after last check. */
715 cfs_mutex_lock(&gsec_kr->gsk_root_uc_lock);
717 ctx = sec_lookup_root_ctx_kr(sec);
721 /* update reverse handle for root user */
722 sec2gsec(sec)->gs_rvs_hdl = gss_get_next_ctx_index();
727 /* in case of setuid, key will be constructed as owner of fsuid/fsgid,
728 * but we do authentication based on real uid/gid. the key permission
729 * bits will be exactly as POS_ALL, so only processes who subscribed
730 * this key could have the access, although the quota might be counted
731 * on others (fsuid/fsgid).
733 * keyring will use fsuid/fsgid as upcall parameters, so we have to
734 * encode real uid/gid into callout info.
737 construct_key_desc(desc, sizeof(desc), sec, vcred->vc_uid);
739 /* callout info format:
740 * secid:mech:uid:gid:flags:svc_type:peer_nid:target_uuid
742 coinfo_size = sizeof(struct obd_uuid) + MAX_OBD_NAME + 64;
743 OBD_ALLOC(coinfo, coinfo_size);
747 snprintf(coinfo, coinfo_size, "%d:%s:%u:%u:%s:%d:"LPX64":%s",
748 sec->ps_id, sec2gsec(sec)->gs_mech->gm_name,
749 vcred->vc_uid, vcred->vc_gid,
750 co_flags, import_to_gss_svc(imp),
751 imp->imp_connection->c_peer.nid, imp->imp_obd->obd_name);
753 CDEBUG(D_SEC, "requesting key for %s\n", desc);
755 keyring_upcall_lock(gsec_kr);
756 key = request_key(&gss_key_type, desc, coinfo);
757 keyring_upcall_unlock(gsec_kr);
759 OBD_FREE(coinfo, coinfo_size);
762 CERROR("failed request key: %ld\n", PTR_ERR(key));
765 CDEBUG(D_SEC, "obtained key %08x for %s\n", key->serial, desc);
767 /* once payload.data was pointed to a ctx, it never changes until
768 * we de-associate them; but parallel request_key() may return
769 * a key with payload.data == NULL at the same time. so we still
770 * need wirtelock of key->sem to serialize them. */
771 down_write(&key->sem);
773 if (likely(key->payload.data != NULL)) {
774 ctx = key->payload.data;
776 LASSERT(cfs_atomic_read(&ctx->cc_refcount) >= 1);
777 LASSERT(ctx2gctx_keyring(ctx)->gck_key == key);
778 LASSERT(atomic_read(&key->usage) >= 2);
780 /* simply take a ref and return. it's upper layer's
781 * responsibility to detect & replace dead ctx. */
782 cfs_atomic_inc(&ctx->cc_refcount);
784 /* pre initialization with a cli_ctx. this can't be done in
785 * key_instantiate() because we'v no enough information
787 ctx = ctx_create_kr(sec, vcred);
789 ctx_enlist_kr(ctx, is_root, 0);
790 bind_key_ctx(key, ctx);
792 ctx_start_timer_kr(ctx, KEYRING_UPCALL_TIMEOUT);
794 CDEBUG(D_SEC, "installed key %p <-> ctx %p (sec %p)\n",
797 /* we'd prefer to call key_revoke(), but we more like
798 * to revoke it within this key->sem locked period. */
799 key_revoke_locked(key);
807 if (is_root && create_new)
808 request_key_unlink(key);
813 cfs_mutex_unlock(&gsec_kr->gsk_root_uc_lock);
818 void gss_sec_release_ctx_kr(struct ptlrpc_sec *sec,
819 struct ptlrpc_cli_ctx *ctx,
822 LASSERT(cfs_atomic_read(&sec->ps_refcount) > 0);
823 LASSERT(cfs_atomic_read(&ctx->cc_refcount) == 0);
824 ctx_release_kr(ctx, sync);
828 * flush context of normal user, we must resort to keyring itself to find out
829 * contexts which belong to me.
831 * Note here we suppose only to flush _my_ context, the "uid" will
832 * be ignored in the search.
835 void flush_user_ctx_cache_kr(struct ptlrpc_sec *sec,
837 int grace, int force)
842 /* nothing to do for reverse or rootonly sec */
843 if (sec_is_reverse(sec) || sec_is_rootonly(sec))
846 construct_key_desc(desc, sizeof(desc), sec, uid);
848 /* there should be only one valid key, but we put it in the
849 * loop in case of any weird cases */
851 key = request_key(&gss_key_type, desc, NULL);
853 CDEBUG(D_SEC, "No more key found for current user\n");
857 down_write(&key->sem);
859 kill_key_locked(key);
861 /* kill_key_locked() should usually revoke the key, but we
862 * revoke it again to make sure, e.g. some case the key may
863 * not well coupled with a context. */
864 key_revoke_locked(key);
873 * flush context of root or all, we iterate through the list.
876 void flush_spec_ctx_cache_kr(struct ptlrpc_sec *sec,
878 int grace, int force)
880 struct gss_sec_keyring *gsec_kr;
881 cfs_hlist_head_t freelist = CFS_HLIST_HEAD_INIT;
882 cfs_hlist_node_t *pos, *next;
883 struct ptlrpc_cli_ctx *ctx;
886 gsec_kr = sec2gsec_keyring(sec);
888 cfs_spin_lock(&sec->ps_lock);
889 cfs_hlist_for_each_entry_safe(ctx, pos, next,
890 &gsec_kr->gsk_clist, cc_cache) {
891 LASSERT(cfs_atomic_read(&ctx->cc_refcount) > 0);
893 if (uid != -1 && uid != ctx->cc_vcred.vc_uid)
896 /* at this moment there's at least 2 base reference:
897 * key association and in-list. */
898 if (cfs_atomic_read(&ctx->cc_refcount) > 2) {
901 CWARN("flush busy ctx %p(%u->%s, extra ref %d)\n",
902 ctx, ctx->cc_vcred.vc_uid,
903 sec2target_str(ctx->cc_sec),
904 cfs_atomic_read(&ctx->cc_refcount) - 2);
907 cfs_set_bit(PTLRPC_CTX_DEAD_BIT, &ctx->cc_flags);
909 cfs_clear_bit(PTLRPC_CTX_UPTODATE_BIT, &ctx->cc_flags);
911 cfs_atomic_inc(&ctx->cc_refcount);
913 if (ctx_unlist_kr(ctx, 1)) {
914 cfs_hlist_add_head(&ctx->cc_cache, &freelist);
916 LASSERT(cfs_atomic_read(&ctx->cc_refcount) >= 2);
917 cfs_atomic_dec(&ctx->cc_refcount);
920 cfs_spin_unlock(&sec->ps_lock);
922 dispose_ctx_list_kr(&freelist);
927 int gss_sec_flush_ctx_cache_kr(struct ptlrpc_sec *sec,
928 uid_t uid, int grace, int force)
932 CDEBUG(D_SEC, "sec %p(%d, nctx %d), uid %d, grace %d, force %d\n",
933 sec, cfs_atomic_read(&sec->ps_refcount),
934 cfs_atomic_read(&sec->ps_nctx),
937 if (uid != -1 && uid != 0)
938 flush_user_ctx_cache_kr(sec, uid, grace, force);
940 flush_spec_ctx_cache_kr(sec, uid, grace, force);
946 void gss_sec_gc_ctx_kr(struct ptlrpc_sec *sec)
948 struct gss_sec_keyring *gsec_kr = sec2gsec_keyring(sec);
949 cfs_hlist_head_t freelist = CFS_HLIST_HEAD_INIT;
950 cfs_hlist_node_t *pos, *next;
951 struct ptlrpc_cli_ctx *ctx;
954 CWARN("running gc\n");
956 cfs_spin_lock(&sec->ps_lock);
957 cfs_hlist_for_each_entry_safe(ctx, pos, next,
958 &gsec_kr->gsk_clist, cc_cache) {
959 LASSERT(cfs_atomic_read(&ctx->cc_refcount) > 0);
961 cfs_atomic_inc(&ctx->cc_refcount);
963 if (cli_ctx_check_death(ctx) && ctx_unlist_kr(ctx, 1)) {
964 cfs_hlist_add_head(&ctx->cc_cache, &freelist);
965 CWARN("unhashed ctx %p\n", ctx);
967 LASSERT(cfs_atomic_read(&ctx->cc_refcount) >= 2);
968 cfs_atomic_dec(&ctx->cc_refcount);
971 cfs_spin_unlock(&sec->ps_lock);
973 dispose_ctx_list_kr(&freelist);
979 int gss_sec_display_kr(struct ptlrpc_sec *sec, struct seq_file *seq)
981 struct gss_sec_keyring *gsec_kr = sec2gsec_keyring(sec);
982 cfs_hlist_node_t *pos, *next;
983 struct ptlrpc_cli_ctx *ctx;
984 struct gss_cli_ctx *gctx;
985 time_t now = cfs_time_current_sec();
988 cfs_spin_lock(&sec->ps_lock);
989 cfs_hlist_for_each_entry_safe(ctx, pos, next,
990 &gsec_kr->gsk_clist, cc_cache) {
995 gctx = ctx2gctx(ctx);
996 key = ctx2gctx_keyring(ctx)->gck_key;
998 gss_cli_ctx_flags2str(ctx->cc_flags,
999 flags_str, sizeof(flags_str));
1001 if (gctx->gc_mechctx)
1002 lgss_display(gctx->gc_mechctx, mech, sizeof(mech));
1004 snprintf(mech, sizeof(mech), "N/A");
1005 mech[sizeof(mech) - 1] = '\0';
1007 seq_printf(seq, "%p: uid %u, ref %d, expire %ld(%+ld), fl %s, "
1008 "seq %d, win %u, key %08x(ref %d), "
1009 "hdl "LPX64":"LPX64", mech: %s\n",
1010 ctx, ctx->cc_vcred.vc_uid,
1011 cfs_atomic_read(&ctx->cc_refcount),
1013 ctx->cc_expire ? ctx->cc_expire - now : 0,
1015 cfs_atomic_read(&gctx->gc_seq),
1017 key ? key->serial : 0,
1018 key ? atomic_read(&key->usage) : 0,
1019 gss_handle_to_u64(&gctx->gc_handle),
1020 gss_handle_to_u64(&gctx->gc_svc_handle),
1023 cfs_spin_unlock(&sec->ps_lock);
1028 /****************************************
1030 ****************************************/
1033 int gss_cli_ctx_refresh_kr(struct ptlrpc_cli_ctx *ctx)
1035 /* upcall is already on the way */
1040 int gss_cli_ctx_validate_kr(struct ptlrpc_cli_ctx *ctx)
1042 LASSERT(cfs_atomic_read(&ctx->cc_refcount) > 0);
1043 LASSERT(ctx->cc_sec);
1045 if (cli_ctx_check_death(ctx)) {
1050 if (cli_ctx_is_ready(ctx))
1056 void gss_cli_ctx_die_kr(struct ptlrpc_cli_ctx *ctx, int grace)
1058 LASSERT(cfs_atomic_read(&ctx->cc_refcount) > 0);
1059 LASSERT(ctx->cc_sec);
1061 cli_ctx_expire(ctx);
1065 /****************************************
1066 * (reverse) service *
1067 ****************************************/
1070 * reverse context could have nothing to do with keyrings. here we still keep
1071 * the version which bind to a key, for future reference.
1073 #define HAVE_REVERSE_CTX_NOKEY
1075 #ifdef HAVE_REVERSE_CTX_NOKEY
1078 int sec_install_rctx_kr(struct ptlrpc_sec *sec,
1079 struct ptlrpc_svc_ctx *svc_ctx)
1081 struct ptlrpc_cli_ctx *cli_ctx;
1082 struct vfs_cred vcred = { 0, 0 };
1088 cli_ctx = ctx_create_kr(sec, &vcred);
1089 if (cli_ctx == NULL)
1092 rc = gss_copy_rvc_cli_ctx(cli_ctx, svc_ctx);
1094 CERROR("failed copy reverse cli ctx: %d\n", rc);
1096 ctx_put_kr(cli_ctx, 1);
1100 rvs_sec_install_root_ctx_kr(sec, cli_ctx, NULL);
1102 ctx_put_kr(cli_ctx, 1);
1107 #else /* ! HAVE_REVERSE_CTX_NOKEY */
1110 int sec_install_rctx_kr(struct ptlrpc_sec *sec,
1111 struct ptlrpc_svc_ctx *svc_ctx)
1113 struct ptlrpc_cli_ctx *cli_ctx = NULL;
1115 struct vfs_cred vcred = { 0, 0 };
1123 construct_key_desc(desc, sizeof(desc), sec, 0);
1125 key = key_alloc(&gss_key_type, desc, 0, 0,
1126 KEY_POS_ALL | KEY_USR_ALL, 1);
1128 CERROR("failed to alloc key: %ld\n", PTR_ERR(key));
1129 return PTR_ERR(key);
1132 rc = key_instantiate_and_link(key, NULL, 0, NULL, NULL);
1134 CERROR("failed to instantiate key: %d\n", rc);
1138 down_write(&key->sem);
1140 LASSERT(key->payload.data == NULL);
1142 cli_ctx = ctx_create_kr(sec, &vcred);
1143 if (cli_ctx == NULL) {
1148 rc = gss_copy_rvc_cli_ctx(cli_ctx, svc_ctx);
1150 CERROR("failed copy reverse cli ctx: %d\n", rc);
1154 rvs_sec_install_root_ctx_kr(sec, cli_ctx, key);
1156 ctx_put_kr(cli_ctx, 1);
1157 up_write(&key->sem);
1166 ctx_put_kr(cli_ctx, 1);
1168 up_write(&key->sem);
1174 #endif /* HAVE_REVERSE_CTX_NOKEY */
1176 /****************************************
1178 ****************************************/
1181 int gss_svc_accept_kr(struct ptlrpc_request *req)
1183 return gss_svc_accept(&gss_policy_keyring, req);
1187 int gss_svc_install_rctx_kr(struct obd_import *imp,
1188 struct ptlrpc_svc_ctx *svc_ctx)
1190 struct ptlrpc_sec *sec;
1193 sec = sptlrpc_import_sec_ref(imp);
1196 rc = sec_install_rctx_kr(sec, svc_ctx);
1197 sptlrpc_sec_put(sec);
1202 /****************************************
1204 ****************************************/
1207 int gss_kt_instantiate(struct key *key, const void *data, size_t datalen)
1212 if (data != NULL || datalen != 0) {
1213 CERROR("invalid: data %p, len %lu\n", data, (long)datalen);
1217 if (key->payload.data != 0) {
1218 CERROR("key already have payload\n");
1222 /* link the key to session keyring, so following context negotiation
1223 * rpc fired from user space could find this key. This will be unlinked
1224 * automatically when upcall processes die.
1226 * we can't do this through keyctl from userspace, because the upcall
1227 * might be neither possessor nor owner of the key (setuid).
1229 * the session keyring is created upon upcall, and don't change all
1230 * the way until upcall finished, so rcu lock is not needed here.
1232 LASSERT(cfs_current()->signal->session_keyring);
1235 rc = key_link(cfs_current()->signal->session_keyring, key);
1238 CERROR("failed to link key %08x to keyring %08x: %d\n",
1240 cfs_current()->signal->session_keyring->serial, rc);
1244 CDEBUG(D_SEC, "key %p instantiated, ctx %p\n", key, key->payload.data);
1249 * called with key semaphore write locked. it means we can operate
1250 * on the context without fear of loosing refcount.
1253 int gss_kt_update(struct key *key, const void *data, size_t datalen)
1255 struct ptlrpc_cli_ctx *ctx = key->payload.data;
1256 struct gss_cli_ctx *gctx;
1257 rawobj_t tmpobj = RAWOBJ_EMPTY;
1258 __u32 datalen32 = (__u32) datalen;
1262 if (data == NULL || datalen == 0) {
1263 CWARN("invalid: data %p, len %lu\n", data, (long)datalen);
1267 /* if upcall finished negotiation too fast (mostly likely because
1268 * of local error happened) and call kt_update(), the ctx
1269 * might be still NULL. but the key will finally be associate
1270 * with a context, or be revoked. if key status is fine, return
1271 * -EAGAIN to allow userspace sleep a while and call again. */
1273 CDEBUG(D_SEC, "update too soon: key %p(%x) flags %lx\n",
1274 key, key->serial, key->flags);
1276 rc = key_validate(key);
1283 LASSERT(cfs_atomic_read(&ctx->cc_refcount) > 0);
1284 LASSERT(ctx->cc_sec);
1286 ctx_clear_timer_kr(ctx);
1288 /* don't proceed if already refreshed */
1289 if (cli_ctx_is_refreshed(ctx)) {
1290 CWARN("ctx already done refresh\n");
1294 sptlrpc_cli_ctx_get(ctx);
1295 gctx = ctx2gctx(ctx);
1297 rc = buffer_extract_bytes(&data, &datalen32, &gctx->gc_win,
1298 sizeof(gctx->gc_win));
1300 CERROR("failed extract seq_win\n");
1304 if (gctx->gc_win == 0) {
1305 __u32 nego_rpc_err, nego_gss_err;
1307 rc = buffer_extract_bytes(&data, &datalen32, &nego_rpc_err,
1308 sizeof(nego_rpc_err));
1310 CERROR("failed to extrace rpc rc\n");
1314 rc = buffer_extract_bytes(&data, &datalen32, &nego_gss_err,
1315 sizeof(nego_gss_err));
1317 CERROR("failed to extrace gss rc\n");
1321 CERROR("negotiation: rpc err %d, gss err %x\n",
1322 nego_rpc_err, nego_gss_err);
1324 rc = nego_rpc_err ? nego_rpc_err : -EACCES;
1326 rc = rawobj_extract_local_alloc(&gctx->gc_handle,
1327 (__u32 **) &data, &datalen32);
1329 CERROR("failed extract handle\n");
1333 rc = rawobj_extract_local(&tmpobj, (__u32 **) &data,&datalen32);
1335 CERROR("failed extract mech\n");
1339 rc = lgss_import_sec_context(&tmpobj,
1340 sec2gsec(ctx->cc_sec)->gs_mech,
1342 if (rc != GSS_S_COMPLETE)
1343 CERROR("failed import context\n");
1348 /* we don't care what current status of this ctx, even someone else
1349 * is operating on the ctx at the same time. we just add up our own
1352 gss_cli_ctx_uptodate(gctx);
1354 /* this will also revoke the key. has to be done before
1355 * wakeup waiters otherwise they can find the stale key */
1356 kill_key_locked(key);
1358 cli_ctx_expire(ctx);
1360 if (rc != -ERESTART)
1361 cfs_set_bit(PTLRPC_CTX_ERROR_BIT, &ctx->cc_flags);
1364 /* let user space think it's a success */
1365 sptlrpc_cli_ctx_put(ctx, 1);
1370 int gss_kt_match(const struct key *key, const void *desc)
1372 return (strcmp(key->description, (const char *) desc) == 0);
1376 void gss_kt_destroy(struct key *key)
1379 LASSERT(key->payload.data == NULL);
1380 CDEBUG(D_SEC, "destroy key %p\n", key);
1385 void gss_kt_describe(const struct key *key, struct seq_file *s)
1387 if (key->description == NULL)
1388 seq_puts(s, "[null]");
1390 seq_puts(s, key->description);
1393 static struct key_type gss_key_type =
1397 .instantiate = gss_kt_instantiate,
1398 .update = gss_kt_update,
1399 .match = gss_kt_match,
1400 .destroy = gss_kt_destroy,
1401 .describe = gss_kt_describe,
1404 /****************************************
1405 * lustre gss keyring policy *
1406 ****************************************/
1408 static struct ptlrpc_ctx_ops gss_keyring_ctxops = {
1409 .match = gss_cli_ctx_match,
1410 .refresh = gss_cli_ctx_refresh_kr,
1411 .validate = gss_cli_ctx_validate_kr,
1412 .die = gss_cli_ctx_die_kr,
1413 .sign = gss_cli_ctx_sign,
1414 .verify = gss_cli_ctx_verify,
1415 .seal = gss_cli_ctx_seal,
1416 .unseal = gss_cli_ctx_unseal,
1417 .wrap_bulk = gss_cli_ctx_wrap_bulk,
1418 .unwrap_bulk = gss_cli_ctx_unwrap_bulk,
1421 static struct ptlrpc_sec_cops gss_sec_keyring_cops = {
1422 .create_sec = gss_sec_create_kr,
1423 .destroy_sec = gss_sec_destroy_kr,
1424 .kill_sec = gss_sec_kill,
1425 .lookup_ctx = gss_sec_lookup_ctx_kr,
1426 .release_ctx = gss_sec_release_ctx_kr,
1427 .flush_ctx_cache = gss_sec_flush_ctx_cache_kr,
1428 .gc_ctx = gss_sec_gc_ctx_kr,
1429 .install_rctx = gss_sec_install_rctx,
1430 .alloc_reqbuf = gss_alloc_reqbuf,
1431 .free_reqbuf = gss_free_reqbuf,
1432 .alloc_repbuf = gss_alloc_repbuf,
1433 .free_repbuf = gss_free_repbuf,
1434 .enlarge_reqbuf = gss_enlarge_reqbuf,
1435 .display = gss_sec_display_kr,
1438 static struct ptlrpc_sec_sops gss_sec_keyring_sops = {
1439 .accept = gss_svc_accept_kr,
1440 .invalidate_ctx = gss_svc_invalidate_ctx,
1441 .alloc_rs = gss_svc_alloc_rs,
1442 .authorize = gss_svc_authorize,
1443 .free_rs = gss_svc_free_rs,
1444 .free_ctx = gss_svc_free_ctx,
1445 .prep_bulk = gss_svc_prep_bulk,
1446 .unwrap_bulk = gss_svc_unwrap_bulk,
1447 .wrap_bulk = gss_svc_wrap_bulk,
1448 .install_rctx = gss_svc_install_rctx_kr,
1451 static struct ptlrpc_sec_policy gss_policy_keyring = {
1452 .sp_owner = THIS_MODULE,
1453 .sp_name = "gss.keyring",
1454 .sp_policy = SPTLRPC_POLICY_GSS,
1455 .sp_cops = &gss_sec_keyring_cops,
1456 .sp_sops = &gss_sec_keyring_sops,
1460 int __init gss_init_keyring(void)
1464 rc = register_key_type(&gss_key_type);
1466 CERROR("failed to register keyring type: %d\n", rc);
1470 rc = sptlrpc_register_policy(&gss_policy_keyring);
1472 unregister_key_type(&gss_key_type);
1479 void __exit gss_exit_keyring(void)
1481 unregister_key_type(&gss_key_type);
1482 sptlrpc_unregister_policy(&gss_policy_keyring);