1 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
2 * vim:expandtab:shiftwidth=8:tabstop=8:
4 * Copyright (C) 2007 Cluster File Systems, Inc.
5 * Author: Eric Mei <ericm@clusterfs.com>
7 * This file is part of Lustre, http://www.lustre.org.
9 * Lustre is free software; you can redistribute it and/or
10 * modify it under the terms of version 2 of the GNU General Public
11 * License as published by the Free Software Foundation.
13 * Lustre is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with Lustre; if not, write to the Free Software
20 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
24 # define EXPORT_SYMTAB
26 #define DEBUG_SUBSYSTEM S_SEC
28 #include <linux/init.h>
29 #include <linux/module.h>
30 #include <linux/slab.h>
31 #include <linux/dcache.h>
33 #include <linux/random.h>
34 #include <linux/crypto.h>
35 #include <linux/key.h>
36 #include <linux/keyctl.h>
37 #include <linux/mutex.h>
38 #include <asm/atomic.h>
40 #include <liblustre.h>
44 #include <obd_class.h>
45 #include <obd_support.h>
46 #include <lustre/lustre_idl.h>
47 #include <lustre_sec.h>
48 #include <lustre_net.h>
49 #include <lustre_import.h>
52 #include "gss_internal.h"
55 static struct ptlrpc_sec_policy gss_policy_keyring;
56 static struct ptlrpc_ctx_ops gss_keyring_ctxops;
57 static struct key_type gss_key_type;
59 static int sec_install_rctx_kr(struct ptlrpc_sec *sec,
60 struct ptlrpc_svc_ctx *svc_ctx);
63 * the timeout is only for the case that upcall child process die abnormally.
64 * in any other cases it should finally update kernel key. so we set this
65 * timeout value excessive long.
67 #define KEYRING_UPCALL_TIMEOUT (obd_timeout + obd_timeout)
69 /****************************************
71 ****************************************/
73 #define DUMP_PROCESS_KEYRINGS(tsk) \
75 CWARN("DUMP PK: %s[%u,%u/%u](<-%s[%u,%u/%u]): " \
76 "a %d, t %d, p %d, s %d, u %d, us %d, df %d\n", \
77 tsk->comm, tsk->pid, tsk->uid, tsk->fsuid, \
78 tsk->parent->comm, tsk->parent->pid, \
79 tsk->parent->uid, tsk->parent->fsuid, \
80 task_aux(tsk)->request_key_auth ? \
81 task_aux(tsk)->request_key_auth->serial : 0, \
82 task_aux(tsk)->thread_keyring ? \
83 task_aux(tsk)->thread_keyring->serial : 0, \
84 tsk->signal->process_keyring ? \
85 tsk->signal->process_keyring->serial : 0, \
86 tsk->signal->session_keyring ? \
87 tsk->signal->session_keyring->serial : 0, \
88 tsk->user->uid_keyring ? \
89 tsk->user->uid_keyring->serial : 0, \
90 tsk->user->session_keyring ? \
91 tsk->user->session_keyring->serial : 0, \
92 task_aux(tsk)->jit_keyring \
96 #define DUMP_KEY(key) \
98 CWARN("DUMP KEY: %p(%d) ref %d u%u/g%u desc %s\n", \
99 key, key->serial, atomic_read(&key->usage), \
100 key->uid, key->gid, \
101 key->description ? key->description : "n/a" \
106 static inline void keyring_upcall_lock(struct gss_sec_keyring *gsec_kr)
108 #ifdef HAVE_KEYRING_UPCALL_SERIALIZED
109 mutex_lock(&gsec_kr->gsk_uc_lock);
113 static inline void keyring_upcall_unlock(struct gss_sec_keyring *gsec_kr)
115 #ifdef HAVE_KEYRING_UPCALL_SERIALIZED
116 mutex_unlock(&gsec_kr->gsk_uc_lock);
120 static inline void key_revoke_locked(struct key *key)
122 set_bit(KEY_FLAG_REVOKED, &key->flags);
125 static void ctx_upcall_timeout_kr(unsigned long data)
127 struct ptlrpc_cli_ctx *ctx = (struct ptlrpc_cli_ctx *) data;
128 struct key *key = ctx2gctx_keyring(ctx)->gck_key;
130 CWARN("ctx %p, key %p\n", ctx, key);
135 key_revoke_locked(key);
136 sptlrpc_cli_ctx_wakeup(ctx);
140 void ctx_start_timer_kr(struct ptlrpc_cli_ctx *ctx, long timeout)
142 struct gss_cli_ctx_keyring *gctx_kr = ctx2gctx_keyring(ctx);
143 struct timer_list *timer = gctx_kr->gck_timer;
147 CDEBUG(D_SEC, "ctx %p: start timer %lds\n", ctx, timeout);
148 timeout = timeout * HZ + cfs_time_current();
151 timer->expires = timeout;
152 timer->data = (unsigned long ) ctx;
153 timer->function = ctx_upcall_timeout_kr;
159 * caller should make sure no race with other threads
162 void ctx_clear_timer_kr(struct ptlrpc_cli_ctx *ctx)
164 struct gss_cli_ctx_keyring *gctx_kr = ctx2gctx_keyring(ctx);
165 struct timer_list *timer = gctx_kr->gck_timer;
170 CDEBUG(D_SEC, "ctx %p, key %p\n", ctx, gctx_kr->gck_key);
172 gctx_kr->gck_timer = NULL;
174 del_singleshot_timer_sync(timer);
180 struct ptlrpc_cli_ctx *ctx_create_kr(struct ptlrpc_sec *sec,
181 struct vfs_cred *vcred)
183 struct ptlrpc_cli_ctx *ctx;
184 struct gss_cli_ctx_keyring *gctx_kr;
186 OBD_ALLOC_PTR(gctx_kr);
190 OBD_ALLOC_PTR(gctx_kr->gck_timer);
191 if (gctx_kr->gck_timer == NULL) {
192 OBD_FREE_PTR(gctx_kr);
195 init_timer(gctx_kr->gck_timer);
197 ctx = &gctx_kr->gck_base.gc_base;
199 if (gss_cli_ctx_init_common(sec, ctx, &gss_keyring_ctxops, vcred)) {
200 OBD_FREE_PTR(gctx_kr->gck_timer);
201 OBD_FREE_PTR(gctx_kr);
205 ctx->cc_expire = cfs_time_current_sec() + KEYRING_UPCALL_TIMEOUT;
206 clear_bit(PTLRPC_CTX_NEW_BIT, &ctx->cc_flags);
207 atomic_inc(&ctx->cc_refcount); /* for the caller */
212 static void ctx_destroy_kr(struct ptlrpc_cli_ctx *ctx)
214 struct ptlrpc_sec *sec = ctx->cc_sec;
215 struct gss_cli_ctx_keyring *gctx_kr = ctx2gctx_keyring(ctx);
218 CDEBUG(D_SEC, "destroying ctx %p\n", ctx);
220 /* at this time the association with key has been broken. */
222 LASSERT(test_bit(PTLRPC_CTX_CACHED_BIT, &ctx->cc_flags) == 0);
223 LASSERT(gctx_kr->gck_key == NULL);
225 ctx_clear_timer_kr(ctx);
226 LASSERT(gctx_kr->gck_timer == NULL);
228 rc = gss_cli_ctx_fini_common(sec, ctx);
230 OBD_FREE_PTR(gctx_kr);
233 CWARN("released the last ctx, proceed to destroy sec %s@%p\n",
234 sec->ps_policy->sp_name, sec);
235 sptlrpc_sec_destroy(sec);
239 static void ctx_put_kr(struct ptlrpc_cli_ctx *ctx)
241 LASSERT(atomic_read(&ctx->cc_refcount) > 0);
243 if (atomic_dec_and_test(&ctx->cc_refcount))
248 * key <-> ctx association and rules:
249 * - ctx might not bind with any key
250 * - key/ctx binding is protected by key semaphore (if the key present)
251 * - key and ctx each take a reference of the other
252 * - ctx enlist/unlist is protected by ctx spinlock
253 * - never enlist a ctx after it's been unlisted
254 * - whoever do enlist should also do bind, lock key before enlist:
255 * - lock key -> lock ctx -> enlist -> unlock ctx -> bind -> unlock key
256 * - whoever do unlist should also do unbind:
257 * - lock key -> lock ctx -> unlist -> unlock ctx -> unbind -> unlock key
258 * - lock ctx -> unlist -> unlock ctx -> lock key -> unbind -> unlock key
261 static inline void spin_lock_if(spinlock_t *lock, int condition)
267 static inline void spin_unlock_if(spinlock_t *lock, int condition)
274 void ctx_enlist_kr(struct ptlrpc_cli_ctx *ctx, int is_root, int locked)
276 struct ptlrpc_sec *sec = ctx->cc_sec;
277 struct gss_sec_keyring *gsec_kr = sec2gsec_keyring(sec);
279 LASSERT(!test_bit(PTLRPC_CTX_CACHED_BIT, &ctx->cc_flags));
280 LASSERT(atomic_read(&ctx->cc_refcount) > 0);
282 spin_lock_if(&sec->ps_lock, !locked);
284 atomic_inc(&ctx->cc_refcount);
285 set_bit(PTLRPC_CTX_CACHED_BIT, &ctx->cc_flags);
286 hlist_add_head(&ctx->cc_cache, &gsec_kr->gsk_clist);
288 gsec_kr->gsk_root_ctx = ctx;
290 spin_unlock_if(&sec->ps_lock, !locked);
294 * Note after this get called, caller should not access ctx again because
295 * it might have been freed, unless caller hold at least one refcount of
298 * return non-zero if we indeed unlist this ctx.
301 int ctx_unlist_kr(struct ptlrpc_cli_ctx *ctx, int locked)
303 struct ptlrpc_sec *sec = ctx->cc_sec;
304 struct gss_sec_keyring *gsec_kr = sec2gsec_keyring(sec);
307 * if hashed bit has gone, leave the job to somebody who is doing it
309 if (test_and_clear_bit(PTLRPC_CTX_CACHED_BIT, &ctx->cc_flags) == 0)
313 * drop ref inside spin lock to prevent race with other operations
315 spin_lock_if(&sec->ps_lock, !locked);
317 if (gsec_kr->gsk_root_ctx == ctx)
318 gsec_kr->gsk_root_ctx = NULL;
319 hlist_del_init(&ctx->cc_cache);
320 atomic_dec(&ctx->cc_refcount);
322 spin_unlock_if(&sec->ps_lock, !locked);
328 * bind a key with a ctx together.
329 * caller must hold write lock of the key, as well as ref on key & ctx.
332 void bind_key_ctx(struct key *key, struct ptlrpc_cli_ctx *ctx)
334 LASSERT(atomic_read(&ctx->cc_refcount) > 0);
335 LASSERT(atomic_read(&key->usage) > 0);
336 LASSERT(ctx2gctx_keyring(ctx)->gck_key == NULL);
337 LASSERT(key->payload.data == NULL);
339 * at this time context may or may not in list.
342 atomic_inc(&ctx->cc_refcount);
343 ctx2gctx_keyring(ctx)->gck_key = key;
344 key->payload.data = ctx;
348 * unbind a key and a ctx.
349 * caller must hold write lock, as well as a ref of the key.
352 void unbind_key_ctx(struct key *key, struct ptlrpc_cli_ctx *ctx)
354 LASSERT(key->payload.data == ctx);
355 LASSERT(test_bit(PTLRPC_CTX_CACHED_BIT, &ctx->cc_flags) == 0);
357 /* must revoke the key, or others may treat it as newly created */
358 key_revoke_locked(key);
360 key->payload.data = NULL;
361 ctx2gctx_keyring(ctx)->gck_key = NULL;
363 /* once ctx get split from key, the timer is meaningless */
364 ctx_clear_timer_kr(ctx);
371 * given a ctx, unbind with its coupled key, if any.
372 * unbind could only be called once, so we don't worry the key be released
375 static void unbind_ctx_kr(struct ptlrpc_cli_ctx *ctx)
377 struct key *key = ctx2gctx_keyring(ctx)->gck_key;
380 LASSERT(key->payload.data == ctx);
383 down_write(&key->sem);
384 unbind_key_ctx(key, ctx);
391 * given a key, unbind with its coupled ctx, if any.
392 * caller must hold write lock, as well as a ref of the key.
394 static void unbind_key_locked(struct key *key)
396 struct ptlrpc_cli_ctx *ctx = key->payload.data;
399 unbind_key_ctx(key, ctx);
403 * unlist a ctx, and unbind from coupled key
405 static void kill_ctx_kr(struct ptlrpc_cli_ctx *ctx)
407 if (ctx_unlist_kr(ctx, 0))
412 * given a key, unlist and unbind with the coupled ctx (if any).
413 * caller must hold write lock, as well as a ref of the key.
415 static void kill_key_locked(struct key *key)
417 struct ptlrpc_cli_ctx *ctx = key->payload.data;
419 if (ctx && ctx_unlist_kr(ctx, 0))
420 unbind_key_locked(key);
424 * caller should hold one ref on contexts in freelist.
426 static void dispose_ctx_list_kr(struct hlist_head *freelist)
428 struct hlist_node *pos, *next;
429 struct ptlrpc_cli_ctx *ctx;
431 hlist_for_each_entry_safe(ctx, pos, next, freelist, cc_cache) {
432 hlist_del_init(&ctx->cc_cache);
435 * we need to wakeup waiting reqs here. the context might
436 * be forced released before upcall finished, then the
437 * late-arrived downcall can't find the ctx even.
439 sptlrpc_cli_ctx_wakeup(ctx);
447 * lookup a root context directly in a sec, return root ctx with a
448 * reference taken or NULL.
451 struct ptlrpc_cli_ctx * sec_lookup_root_ctx_kr(struct ptlrpc_sec *sec)
453 struct gss_sec_keyring *gsec_kr = sec2gsec_keyring(sec);
454 struct ptlrpc_cli_ctx *ctx = NULL;
456 spin_lock(&sec->ps_lock);
458 ctx = gsec_kr->gsk_root_ctx;
460 if (ctx == NULL && unlikely(sec_is_reverse(sec))) {
461 struct hlist_node *node;
462 struct ptlrpc_cli_ctx *tmp;
464 * reverse ctx, search root ctx in list, choose the one
465 * with shortest expire time, which is most possibly have
466 * an established peer ctx at client side.
468 hlist_for_each_entry(tmp, node, &gsec_kr->gsk_clist, cc_cache) {
469 if (ctx == NULL || ctx->cc_expire == 0 ||
470 ctx->cc_expire > tmp->cc_expire) {
472 /* promote to be root_ctx */
473 gsec_kr->gsk_root_ctx = ctx;
479 LASSERT(atomic_read(&ctx->cc_refcount) > 0);
480 LASSERT(!hlist_empty(&gsec_kr->gsk_clist));
481 atomic_inc(&ctx->cc_refcount);
484 spin_unlock(&sec->ps_lock);
489 #define RVS_CTX_EXPIRE_NICE (10)
492 void rvs_sec_install_root_ctx_kr(struct ptlrpc_sec *sec,
493 struct ptlrpc_cli_ctx *new_ctx,
496 struct gss_sec_keyring *gsec_kr = sec2gsec_keyring(sec);
497 struct hlist_node *hnode;
498 struct ptlrpc_cli_ctx *ctx;
502 LASSERT(sec_is_reverse(sec));
504 spin_lock(&sec->ps_lock);
506 now = cfs_time_current_sec();
508 /* set all existing ctxs short expiry */
509 hlist_for_each_entry(ctx, hnode, &gsec_kr->gsk_clist, cc_cache) {
510 if (ctx->cc_expire > now + RVS_CTX_EXPIRE_NICE) {
511 ctx->cc_early_expire = 1;
512 ctx->cc_expire = now + RVS_CTX_EXPIRE_NICE;
516 /* if there's root_ctx there, instead obsolete the current
517 * immediately, we leave it continue operating for a little while.
518 * hopefully when the first backward rpc with newest ctx send out,
519 * the client side already have the peer ctx well established.
521 ctx_enlist_kr(new_ctx, gsec_kr->gsk_root_ctx ? 0 : 1, 1);
524 bind_key_ctx(key, new_ctx);
526 spin_unlock(&sec->ps_lock);
529 static void construct_key_desc(void *buf, int bufsize,
530 struct ptlrpc_sec *sec, uid_t uid)
532 snprintf(buf, bufsize, "%d@%x", uid, sec2gsec_keyring(sec)->gsk_id);
533 ((char *)buf)[bufsize - 1] = '\0';
536 /****************************************
538 ****************************************/
540 static atomic_t gss_sec_id_kr = ATOMIC_INIT(0);
543 struct ptlrpc_sec * gss_sec_create_kr(struct obd_import *imp,
544 struct ptlrpc_svc_ctx *ctx,
548 struct gss_sec_keyring *gsec_kr;
551 OBD_ALLOC(gsec_kr, sizeof(*gsec_kr));
555 gsec_kr->gsk_id = atomic_inc_return(&gss_sec_id_kr);
556 CFS_INIT_HLIST_HEAD(&gsec_kr->gsk_clist);
557 gsec_kr->gsk_root_ctx = NULL;
558 mutex_init(&gsec_kr->gsk_root_uc_lock);
559 #ifdef HAVE_KEYRING_UPCALL_SERIALIZED
560 mutex_init(&gsec_kr->gsk_uc_lock);
563 if (gss_sec_create_common(&gsec_kr->gsk_base, &gss_policy_keyring,
564 imp, ctx, flavor, flags))
568 if (sec_install_rctx_kr(&gsec_kr->gsk_base.gs_base, ctx)) {
569 gss_sec_destroy_common(&gsec_kr->gsk_base);
574 RETURN(&gsec_kr->gsk_base.gs_base);
577 OBD_FREE(gsec_kr, sizeof(*gsec_kr));
582 void gss_sec_destroy_kr(struct ptlrpc_sec *sec)
584 struct gss_sec *gsec = sec2gsec(sec);
585 struct gss_sec_keyring *gsec_kr = sec2gsec_keyring(sec);
587 CDEBUG(D_SEC, "destroy %s@%p\n", sec->ps_policy->sp_name, sec);
589 LASSERT(hlist_empty(&gsec_kr->gsk_clist));
590 LASSERT(gsec_kr->gsk_root_ctx == NULL);
592 gss_sec_destroy_common(gsec);
594 OBD_FREE(gsec_kr, sizeof(*gsec_kr));
598 int user_is_root(struct ptlrpc_sec *sec, struct vfs_cred *vcred)
600 if (sec->ps_flags & PTLRPC_SEC_FL_ROOTONLY)
604 * more precisely deal with setuid. maybe add more infomation
607 return (vcred->vc_uid == 0);
611 * unlink request key from it's ring, which is linked during request_key().
612 * sadly, we have to 'guess' which keyring it's linked to.
614 * FIXME this code is fragile, depend on how request_key_link() is implemented.
616 static void request_key_unlink(struct key *key)
618 struct task_struct *tsk = current;
621 switch (task_aux(tsk)->jit_keyring) {
622 case KEY_REQKEY_DEFL_DEFAULT:
623 case KEY_REQKEY_DEFL_THREAD_KEYRING:
624 ring = key_get(task_aux(tsk)->thread_keyring);
627 case KEY_REQKEY_DEFL_PROCESS_KEYRING:
628 ring = key_get(tsk->signal->process_keyring);
631 case KEY_REQKEY_DEFL_SESSION_KEYRING:
633 ring = key_get(rcu_dereference(tsk->signal->session_keyring));
637 case KEY_REQKEY_DEFL_USER_SESSION_KEYRING:
638 ring = key_get(tsk->user->session_keyring);
640 case KEY_REQKEY_DEFL_USER_KEYRING:
641 ring = key_get(tsk->user->uid_keyring);
643 case KEY_REQKEY_DEFL_GROUP_KEYRING:
649 key_unlink(ring, key);
654 struct ptlrpc_cli_ctx * gss_sec_lookup_ctx_kr(struct ptlrpc_sec *sec,
655 struct vfs_cred *vcred,
656 int create, int remove_dead)
658 struct obd_import *imp = sec->ps_import;
659 struct gss_sec_keyring *gsec_kr = sec2gsec_keyring(sec);
660 struct ptlrpc_cli_ctx *ctx = NULL;
661 unsigned int is_root = 0, create_new = 0;
665 const int coinfo_size = sizeof(struct obd_uuid) + 64;
669 LASSERT(imp != NULL);
671 is_root = user_is_root(sec, vcred);
674 * a little bit optimization for root context
677 ctx = sec_lookup_root_ctx_kr(sec);
679 * Only lookup directly for REVERSE sec, which should
682 if (ctx || sec_is_reverse(sec))
686 LASSERT(create != 0);
689 * for root context, obtain lock and check again, this time hold
690 * the root upcall lock, make sure nobody else populated new root
691 * context after last check.
694 mutex_lock(&gsec_kr->gsk_root_uc_lock);
696 ctx = sec_lookup_root_ctx_kr(sec);
700 /* update reverse handle for root user */
701 sec2gsec(sec)->gs_rvs_hdl = gss_get_next_ctx_index();
706 construct_key_desc(desc, sizeof(desc), sec, vcred->vc_uid);
709 * callout info: mech:flags:svc_type:peer_nid:target_uuid
711 OBD_ALLOC(coinfo, coinfo_size);
715 snprintf(coinfo, coinfo_size, "%s:%s:%d:"LPX64":%s",
716 sec2gsec(sec)->gs_mech->gm_name,
717 co_flags, import_to_gss_svc(imp),
718 imp->imp_connection->c_peer.nid, imp->imp_obd->obd_name);
720 keyring_upcall_lock(gsec_kr);
721 key = request_key(&gss_key_type, desc, coinfo);
722 keyring_upcall_unlock(gsec_kr);
724 OBD_FREE(coinfo, coinfo_size);
727 CERROR("failed request key: %ld\n", PTR_ERR(key));
732 * once payload.data was pointed to a ctx, it never changes until
733 * we de-associate them; but parallel request_key() may return
734 * a key with payload.data == NULL at the same time. so we still
735 * need wirtelock of key->sem to serialize them.
737 down_write(&key->sem);
739 if (likely(key->payload.data != NULL)) {
740 ctx = key->payload.data;
742 LASSERT(atomic_read(&ctx->cc_refcount) >= 1);
743 LASSERT(ctx2gctx_keyring(ctx)->gck_key == key);
744 LASSERT(atomic_read(&key->usage) >= 2);
746 /* simply take a ref and return. it's upper layer's
747 * responsibility to detect & replace dead ctx.
749 atomic_inc(&ctx->cc_refcount);
751 /* pre initialization with a cli_ctx. this can't be done in
752 * key_instantiate() because we'v no enough information there.
754 ctx = ctx_create_kr(sec, vcred);
756 ctx_enlist_kr(ctx, is_root, 0);
757 bind_key_ctx(key, ctx);
759 ctx_start_timer_kr(ctx, KEYRING_UPCALL_TIMEOUT);
761 CDEBUG(D_SEC, "installed key %p <-> ctx %p (sec %p)\n",
765 * we'd prefer to call key_revoke(), but we more like
766 * to revoke it within this key->sem locked period.
768 key_revoke_locked(key);
776 if (is_root && create_new)
777 request_key_unlink(key);
782 mutex_unlock(&gsec_kr->gsk_root_uc_lock);
787 void gss_sec_release_ctx_kr(struct ptlrpc_sec *sec,
788 struct ptlrpc_cli_ctx *ctx,
791 LASSERT(atomic_read(&ctx->cc_refcount) == 0);
796 atomic_inc(&ctx->cc_refcount);
797 sptlrpc_gc_add_ctx(ctx);
802 * flush context of normal user, we must resort to keyring itself to find out
803 * contexts which belong to me.
805 * Note here we suppose only to flush _my_ context, the "uid" will
806 * be ignored in the search.
809 void flush_user_ctx_cache_kr(struct ptlrpc_sec *sec,
811 int grace, int force)
816 /* nothing to do for reverse or rootonly sec */
817 if (sec_is_reverse(sec) || sec_is_rootonly(sec))
820 construct_key_desc(desc, sizeof(desc), sec, uid);
822 /* there should be only one valid key, but we put it in the
823 * loop in case of any weird cases
826 key = request_key(&gss_key_type, desc, NULL);
828 CWARN("No more key found for current user\n");
832 down_write(&key->sem);
834 kill_key_locked(key);
836 /* kill_key_locked() should usually revoke the key, but we
837 * revoke it again to make sure, e.g. some case the key may
838 * not well coupled with a context.
840 key_revoke_locked(key);
849 * flush context of root or all, we iterate through the list.
852 void flush_spec_ctx_cache_kr(struct ptlrpc_sec *sec,
854 int grace, int force)
856 struct gss_sec_keyring *gsec_kr;
857 struct hlist_head freelist = CFS_HLIST_HEAD_INIT;
858 struct hlist_node *pos, *next;
859 struct ptlrpc_cli_ctx *ctx;
862 gsec_kr = sec2gsec_keyring(sec);
864 spin_lock(&sec->ps_lock);
865 hlist_for_each_entry_safe(ctx, pos, next,
866 &gsec_kr->gsk_clist, cc_cache) {
867 LASSERT(atomic_read(&ctx->cc_refcount) > 0);
869 if (uid != -1 && uid != ctx->cc_vcred.vc_uid)
872 /* at this moment there's at least 2 base reference:
873 * key association and in-list.
875 if (atomic_read(&ctx->cc_refcount) > 2) {
878 CWARN("flush busy ctx %p(%u->%s, extra ref %d)\n",
879 ctx, ctx->cc_vcred.vc_uid,
880 sec2target_str(ctx->cc_sec),
881 atomic_read(&ctx->cc_refcount) - 2);
884 set_bit(PTLRPC_CTX_DEAD_BIT, &ctx->cc_flags);
886 clear_bit(PTLRPC_CTX_UPTODATE_BIT, &ctx->cc_flags);
888 atomic_inc(&ctx->cc_refcount);
890 if (ctx_unlist_kr(ctx, 1))
891 hlist_add_head(&ctx->cc_cache, &freelist);
893 LASSERT(atomic_read(&ctx->cc_refcount) >= 2);
894 atomic_dec(&ctx->cc_refcount);
898 spin_unlock(&sec->ps_lock);
900 dispose_ctx_list_kr(&freelist);
905 int gss_sec_flush_ctx_cache_kr(struct ptlrpc_sec *sec,
907 int grace, int force)
911 CDEBUG(D_SEC, "sec %p(%d, busy %d), uid %d, grace %d, force %d\n",
912 sec, atomic_read(&sec->ps_refcount), atomic_read(&sec->ps_busy),
915 if (uid != -1 && uid != 0)
916 flush_user_ctx_cache_kr(sec, uid, grace, force);
918 flush_spec_ctx_cache_kr(sec, uid, grace, force);
924 void gss_sec_gc_ctx_kr(struct ptlrpc_sec *sec)
926 struct gss_sec_keyring *gsec_kr = sec2gsec_keyring(sec);
927 struct hlist_head freelist = CFS_HLIST_HEAD_INIT;
928 struct hlist_node *pos, *next;
929 struct ptlrpc_cli_ctx *ctx;
932 CWARN("running gc\n");
934 spin_lock(&sec->ps_lock);
935 hlist_for_each_entry_safe(ctx, pos, next,
936 &gsec_kr->gsk_clist, cc_cache) {
937 LASSERT(atomic_read(&ctx->cc_refcount) > 0);
939 atomic_inc(&ctx->cc_refcount);
941 if (cli_ctx_check_death(ctx) && ctx_unlist_kr(ctx, 1)) {
942 hlist_add_head(&ctx->cc_cache, &freelist);
943 CWARN("unhashed ctx %p\n", ctx);
945 LASSERT(atomic_read(&ctx->cc_refcount) >= 2);
946 atomic_dec(&ctx->cc_refcount);
949 spin_unlock(&sec->ps_lock);
951 dispose_ctx_list_kr(&freelist);
957 int gss_sec_display_kr(struct ptlrpc_sec *sec, char *buf, int bufsize)
959 struct gss_sec_keyring *gsec_kr = sec2gsec_keyring(sec);
960 struct hlist_node *pos, *next;
961 struct ptlrpc_cli_ctx *ctx;
965 written = snprintf(buf, bufsize, "context list ===>\n");
969 spin_lock(&sec->ps_lock);
970 hlist_for_each_entry_safe(ctx, pos, next,
971 &gsec_kr->gsk_clist, cc_cache) {
972 struct gss_cli_ctx *gctx;
977 gctx = ctx2gctx(ctx);
978 key = ctx2gctx_keyring(ctx)->gck_key;
980 gss_cli_ctx_flags2str(ctx->cc_flags,
981 flags_str, sizeof(flags_str));
983 len = snprintf(buf, bufsize, "%p(%d): uid %u, exp %ld(%ld)s, "
984 "fl %s, seq %d, win %u, key %08x(%d), ",
985 ctx, atomic_read(&ctx->cc_refcount),
986 ctx->cc_vcred.vc_uid,
988 ctx->cc_expire - cfs_time_current_sec(),
990 atomic_read(&gctx->gc_seq),
992 key ? key->serial : 0,
993 key ? atomic_read(&key->usage) : 0);
1002 if (gctx->gc_mechctx)
1003 len = lgss_display(gctx->gc_mechctx, buf, bufsize);
1005 len = snprintf(buf, bufsize, "mech N/A\n");
1014 spin_unlock(&sec->ps_lock);
1019 /****************************************
1021 ****************************************/
1024 int gss_cli_ctx_refresh_kr(struct ptlrpc_cli_ctx *ctx)
1026 /* upcall is already on the way */
1031 int gss_cli_ctx_validate_kr(struct ptlrpc_cli_ctx *ctx)
1033 LASSERT(atomic_read(&ctx->cc_refcount) > 0);
1034 LASSERT(ctx->cc_sec);
1036 if (cli_ctx_check_death(ctx)) {
1041 if (cli_ctx_is_ready(ctx))
1047 void gss_cli_ctx_die_kr(struct ptlrpc_cli_ctx *ctx, int grace)
1049 LASSERT(atomic_read(&ctx->cc_refcount) > 0);
1050 LASSERT(ctx->cc_sec);
1052 CWARN("ctx %p(%d)\n", ctx, atomic_read(&ctx->cc_refcount));
1053 cli_ctx_expire(ctx);
1057 /****************************************
1058 * (reverse) service *
1059 ****************************************/
1062 * reverse context could have nothing to do with keyrings. here we still keep
1063 * the version which bind to a key, for future reference.
1065 #define HAVE_REVERSE_CTX_NOKEY
1067 #ifdef HAVE_REVERSE_CTX_NOKEY
1070 int sec_install_rctx_kr(struct ptlrpc_sec *sec,
1071 struct ptlrpc_svc_ctx *svc_ctx)
1073 struct ptlrpc_cli_ctx *cli_ctx;
1074 struct vfs_cred vcred = { 0, 0 };
1080 cli_ctx = ctx_create_kr(sec, &vcred);
1081 if (cli_ctx == NULL)
1084 rc = gss_copy_rvc_cli_ctx(cli_ctx, svc_ctx);
1086 CERROR("failed copy reverse cli ctx: %d\n", rc);
1088 ctx_put_kr(cli_ctx);
1092 rvs_sec_install_root_ctx_kr(sec, cli_ctx, NULL);
1094 ctx_put_kr(cli_ctx);
1099 #else /* ! HAVE_REVERSE_CTX_NOKEY */
1102 int sec_install_rctx_kr(struct ptlrpc_sec *sec,
1103 struct ptlrpc_svc_ctx *svc_ctx)
1105 struct ptlrpc_cli_ctx *cli_ctx = NULL;
1107 struct vfs_cred vcred = { 0, 0 };
1115 construct_key_desc(desc, sizeof(desc), sec, 0);
1117 key = key_alloc(&gss_key_type, desc, 0, 0,
1118 KEY_POS_ALL | KEY_USR_ALL, 1);
1120 CERROR("failed to alloc key: %ld\n", PTR_ERR(key));
1121 return PTR_ERR(key);
1124 rc = key_instantiate_and_link(key, NULL, 0, NULL, NULL);
1126 CERROR("failed to instantiate key: %d\n", rc);
1130 down_write(&key->sem);
1132 LASSERT(key->payload.data == NULL);
1134 cli_ctx = ctx_create_kr(sec, &vcred);
1135 if (cli_ctx == NULL) {
1140 rc = gss_copy_rvc_cli_ctx(cli_ctx, svc_ctx);
1142 CERROR("failed copy reverse cli ctx: %d\n", rc);
1146 rvs_sec_install_root_ctx_kr(sec, cli_ctx, key);
1148 ctx_put_kr(cli_ctx);
1149 up_write(&key->sem);
1158 ctx_put_kr(cli_ctx);
1160 up_write(&key->sem);
1166 #endif /* HAVE_REVERSE_CTX_NOKEY */
1168 /****************************************
1170 ****************************************/
1173 int gss_svc_accept_kr(struct ptlrpc_request *req)
1175 return gss_svc_accept(&gss_policy_keyring, req);
1179 int gss_svc_install_rctx_kr(struct obd_import *imp,
1180 struct ptlrpc_svc_ctx *svc_ctx)
1182 LASSERT(imp->imp_sec);
1184 return sec_install_rctx_kr(imp->imp_sec, svc_ctx);
1187 /****************************************
1189 ****************************************/
1192 int gss_kt_instantiate(struct key *key, const void *data, size_t datalen)
1196 if (data != NULL || datalen != 0) {
1197 CERROR("invalid: data %p, len %d\n", data, datalen);
1201 if (key->payload.data != 0) {
1202 CERROR("key already have payload\n");
1207 key->perm |= KEY_POS_ALL | KEY_USR_ALL;
1208 CDEBUG(D_SEC, "key %p instantiated, ctx %p\n", key, key->payload.data);
1213 * called with key semaphore write locked. it means we can operate
1214 * on the context without fear of loosing refcount.
1217 int gss_kt_update(struct key *key, const void *data, size_t datalen)
1219 struct ptlrpc_cli_ctx *ctx = key->payload.data;
1220 struct gss_cli_ctx *gctx;
1221 rawobj_t tmpobj = RAWOBJ_EMPTY;
1225 if (data == NULL || datalen == 0) {
1226 CWARN("invalid: data %p, len %d\n", data, datalen);
1231 * there's a race between userspace parent - child processes. if
1232 * child finish negotiation too fast and call kt_update(), the ctx
1233 * might be still NULL. but the key will finally be associate
1234 * with a context, or be revoked. if key status is fine, return
1235 * -EAGAIN to allow userspace sleep a while and call again.
1238 CWARN("race in userspace. key %p(%x) flags %lx\n",
1239 key, key->serial, key->flags);
1241 rc = key_validate(key);
1248 LASSERT(atomic_read(&ctx->cc_refcount) > 0);
1249 LASSERT(ctx->cc_sec);
1251 ctx_clear_timer_kr(ctx);
1253 /* don't proceed if already refreshed */
1254 if (cli_ctx_is_refreshed(ctx)) {
1255 CWARN("ctx already done refresh\n");
1256 sptlrpc_cli_ctx_wakeup(ctx);
1260 sptlrpc_cli_ctx_get(ctx);
1261 gctx = ctx2gctx(ctx);
1264 if (buffer_extract_bytes(&data, &datalen,
1265 &gctx->gc_win, sizeof(gctx->gc_win))) {
1266 CERROR("failed extract seq_win\n");
1270 if (gctx->gc_win == 0) {
1271 __u32 nego_rpc_err, nego_gss_err;
1273 if (buffer_extract_bytes(&data, &datalen,
1274 &nego_rpc_err, sizeof(nego_rpc_err))) {
1275 CERROR("failed to extrace rpc rc\n");
1279 if (buffer_extract_bytes(&data, &datalen,
1280 &nego_gss_err, sizeof(nego_gss_err))) {
1281 CERROR("failed to extrace gss rc\n");
1285 CERROR("negotiation: rpc err %d, gss err %x\n",
1286 nego_rpc_err, nego_gss_err);
1291 if (rawobj_extract_local_alloc(&gctx->gc_handle,
1292 (__u32 **)&data, &datalen)) {
1293 CERROR("failed extract handle\n");
1297 if (rawobj_extract_local(&tmpobj, (__u32 **)&data, &datalen)) {
1298 CERROR("failed extract mech\n");
1302 if (lgss_import_sec_context(&tmpobj,
1303 sec2gsec(ctx->cc_sec)->gs_mech,
1304 &gctx->gc_mechctx) !=
1306 CERROR("failed import context\n");
1313 /* we don't care what current status of this ctx, even someone else
1314 * is operating on the ctx at the same time. we just add up our own
1318 gss_cli_ctx_uptodate(gctx);
1321 * this will also revoke the key. has to be done before
1322 * wakeup waiters otherwise they can find the stale key
1324 kill_key_locked(key);
1326 cli_ctx_expire(ctx);
1328 if (rc != -ERESTART)
1329 set_bit(PTLRPC_CTX_ERROR_BIT, &ctx->cc_flags);
1332 sptlrpc_cli_ctx_wakeup(ctx);
1334 /* let user space think it's a success */
1335 sptlrpc_cli_ctx_put(ctx, 1);
1340 int gss_kt_match(const struct key *key, const void *desc)
1342 return (strcmp(key->description, (const char *) desc) == 0);
1346 void gss_kt_destroy(struct key *key)
1349 LASSERT(key->payload.data == NULL);
1350 CDEBUG(D_SEC, "destroy key %p\n", key);
1355 void gss_kt_describe(const struct key *key, struct seq_file *s)
1357 if (key->description == NULL)
1358 seq_puts(s, "[null]");
1360 seq_puts(s, key->description);
1363 static struct key_type gss_key_type =
1367 .instantiate = gss_kt_instantiate,
1368 .update = gss_kt_update,
1369 .match = gss_kt_match,
1370 .destroy = gss_kt_destroy,
1371 .describe = gss_kt_describe,
1374 /****************************************
1375 * lustre gss keyring policy *
1376 ****************************************/
1378 static struct ptlrpc_ctx_ops gss_keyring_ctxops = {
1379 .match = gss_cli_ctx_match,
1380 .refresh = gss_cli_ctx_refresh_kr,
1381 .validate = gss_cli_ctx_validate_kr,
1382 .die = gss_cli_ctx_die_kr,
1383 .sign = gss_cli_ctx_sign,
1384 .verify = gss_cli_ctx_verify,
1385 .seal = gss_cli_ctx_seal,
1386 .unseal = gss_cli_ctx_unseal,
1387 .wrap_bulk = gss_cli_ctx_wrap_bulk,
1388 .unwrap_bulk = gss_cli_ctx_unwrap_bulk,
1391 static struct ptlrpc_sec_cops gss_sec_keyring_cops = {
1392 .create_sec = gss_sec_create_kr,
1393 .destroy_sec = gss_sec_destroy_kr,
1394 .lookup_ctx = gss_sec_lookup_ctx_kr,
1395 .release_ctx = gss_sec_release_ctx_kr,
1396 .flush_ctx_cache = gss_sec_flush_ctx_cache_kr,
1397 .gc_ctx = gss_sec_gc_ctx_kr,
1398 .install_rctx = gss_sec_install_rctx,
1399 .alloc_reqbuf = gss_alloc_reqbuf,
1400 .free_reqbuf = gss_free_reqbuf,
1401 .alloc_repbuf = gss_alloc_repbuf,
1402 .free_repbuf = gss_free_repbuf,
1403 .enlarge_reqbuf = gss_enlarge_reqbuf,
1404 .display = gss_sec_display_kr,
1407 static struct ptlrpc_sec_sops gss_sec_keyring_sops = {
1408 .accept = gss_svc_accept_kr,
1409 .invalidate_ctx = gss_svc_invalidate_ctx,
1410 .alloc_rs = gss_svc_alloc_rs,
1411 .authorize = gss_svc_authorize,
1412 .free_rs = gss_svc_free_rs,
1413 .free_ctx = gss_svc_free_ctx,
1414 .unwrap_bulk = gss_svc_unwrap_bulk,
1415 .wrap_bulk = gss_svc_wrap_bulk,
1416 .install_rctx = gss_svc_install_rctx_kr,
1419 static struct ptlrpc_sec_policy gss_policy_keyring = {
1420 .sp_owner = THIS_MODULE,
1421 .sp_name = "gss.keyring",
1422 .sp_policy = SPTLRPC_POLICY_GSS,
1423 .sp_cops = &gss_sec_keyring_cops,
1424 .sp_sops = &gss_sec_keyring_sops,
1428 int __init gss_init_keyring(void)
1432 rc = register_key_type(&gss_key_type);
1434 CERROR("failed to register keyring type: %d\n", rc);
1438 rc = sptlrpc_register_policy(&gss_policy_keyring);
1440 unregister_key_type(&gss_key_type);
1447 void __exit gss_exit_keyring(void)
1449 unregister_key_type(&gss_key_type);
1450 sptlrpc_unregister_policy(&gss_policy_keyring);