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);
306 /* if hashed bit has gone, leave the job to somebody who is doing it */
307 if (test_and_clear_bit(PTLRPC_CTX_CACHED_BIT, &ctx->cc_flags) == 0)
310 /* drop ref inside spin lock to prevent race with other operations */
311 spin_lock_if(&sec->ps_lock, !locked);
313 if (gsec_kr->gsk_root_ctx == ctx)
314 gsec_kr->gsk_root_ctx = NULL;
315 hlist_del_init(&ctx->cc_cache);
316 atomic_dec(&ctx->cc_refcount);
318 spin_unlock_if(&sec->ps_lock, !locked);
324 * bind a key with a ctx together.
325 * caller must hold write lock of the key, as well as ref on key & ctx.
328 void bind_key_ctx(struct key *key, struct ptlrpc_cli_ctx *ctx)
330 LASSERT(atomic_read(&ctx->cc_refcount) > 0);
331 LASSERT(atomic_read(&key->usage) > 0);
332 LASSERT(ctx2gctx_keyring(ctx)->gck_key == NULL);
333 LASSERT(key->payload.data == NULL);
335 /* at this time context may or may not in list. */
337 atomic_inc(&ctx->cc_refcount);
338 ctx2gctx_keyring(ctx)->gck_key = key;
339 key->payload.data = ctx;
343 * unbind a key and a ctx.
344 * caller must hold write lock, as well as a ref of the key.
347 void unbind_key_ctx(struct key *key, struct ptlrpc_cli_ctx *ctx)
349 LASSERT(key->payload.data == ctx);
350 LASSERT(test_bit(PTLRPC_CTX_CACHED_BIT, &ctx->cc_flags) == 0);
352 /* must revoke the key, or others may treat it as newly created */
353 key_revoke_locked(key);
355 key->payload.data = NULL;
356 ctx2gctx_keyring(ctx)->gck_key = NULL;
358 /* once ctx get split from key, the timer is meaningless */
359 ctx_clear_timer_kr(ctx);
366 * given a ctx, unbind with its coupled key, if any.
367 * unbind could only be called once, so we don't worry the key be released
370 static void unbind_ctx_kr(struct ptlrpc_cli_ctx *ctx)
372 struct key *key = ctx2gctx_keyring(ctx)->gck_key;
375 LASSERT(key->payload.data == ctx);
378 down_write(&key->sem);
379 unbind_key_ctx(key, ctx);
386 * given a key, unbind with its coupled ctx, if any.
387 * caller must hold write lock, as well as a ref of the key.
389 static void unbind_key_locked(struct key *key)
391 struct ptlrpc_cli_ctx *ctx = key->payload.data;
394 unbind_key_ctx(key, ctx);
398 * unlist a ctx, and unbind from coupled key
400 static void kill_ctx_kr(struct ptlrpc_cli_ctx *ctx)
402 if (ctx_unlist_kr(ctx, 0))
407 * given a key, unlist and unbind with the coupled ctx (if any).
408 * caller must hold write lock, as well as a ref of the key.
410 static void kill_key_locked(struct key *key)
412 struct ptlrpc_cli_ctx *ctx = key->payload.data;
414 if (ctx && ctx_unlist_kr(ctx, 0))
415 unbind_key_locked(key);
419 * caller should hold one ref on contexts in freelist.
421 static void dispose_ctx_list_kr(struct hlist_head *freelist)
423 struct hlist_node *pos, *next;
424 struct ptlrpc_cli_ctx *ctx;
426 hlist_for_each_entry_safe(ctx, pos, next, freelist, cc_cache) {
427 hlist_del_init(&ctx->cc_cache);
429 /* we need to wakeup waiting reqs here. the context might
430 * be forced released before upcall finished, then the
431 * late-arrived downcall can't find the ctx even. */
432 sptlrpc_cli_ctx_wakeup(ctx);
440 * lookup a root context directly in a sec, return root ctx with a
441 * reference taken or NULL.
444 struct ptlrpc_cli_ctx * sec_lookup_root_ctx_kr(struct ptlrpc_sec *sec)
446 struct gss_sec_keyring *gsec_kr = sec2gsec_keyring(sec);
447 struct ptlrpc_cli_ctx *ctx = NULL;
449 spin_lock(&sec->ps_lock);
451 ctx = gsec_kr->gsk_root_ctx;
453 if (ctx == NULL && unlikely(sec_is_reverse(sec))) {
454 struct hlist_node *node;
455 struct ptlrpc_cli_ctx *tmp;
457 /* reverse ctx, search root ctx in list, choose the one
458 * with shortest expire time, which is most possibly have
459 * an established peer ctx at client side. */
460 hlist_for_each_entry(tmp, node, &gsec_kr->gsk_clist, cc_cache) {
461 if (ctx == NULL || ctx->cc_expire == 0 ||
462 ctx->cc_expire > tmp->cc_expire) {
464 /* promote to be root_ctx */
465 gsec_kr->gsk_root_ctx = ctx;
471 LASSERT(atomic_read(&ctx->cc_refcount) > 0);
472 LASSERT(!hlist_empty(&gsec_kr->gsk_clist));
473 atomic_inc(&ctx->cc_refcount);
476 spin_unlock(&sec->ps_lock);
481 #define RVS_CTX_EXPIRE_NICE (10)
484 void rvs_sec_install_root_ctx_kr(struct ptlrpc_sec *sec,
485 struct ptlrpc_cli_ctx *new_ctx,
488 struct gss_sec_keyring *gsec_kr = sec2gsec_keyring(sec);
489 struct hlist_node *hnode;
490 struct ptlrpc_cli_ctx *ctx;
494 LASSERT(sec_is_reverse(sec));
496 spin_lock(&sec->ps_lock);
498 now = cfs_time_current_sec();
500 /* set all existing ctxs short expiry */
501 hlist_for_each_entry(ctx, hnode, &gsec_kr->gsk_clist, cc_cache) {
502 if (ctx->cc_expire > now + RVS_CTX_EXPIRE_NICE) {
503 ctx->cc_early_expire = 1;
504 ctx->cc_expire = now + RVS_CTX_EXPIRE_NICE;
508 /* if there's root_ctx there, instead obsolete the current
509 * immediately, we leave it continue operating for a little while.
510 * hopefully when the first backward rpc with newest ctx send out,
511 * the client side already have the peer ctx well established. */
512 ctx_enlist_kr(new_ctx, gsec_kr->gsk_root_ctx ? 0 : 1, 1);
515 bind_key_ctx(key, new_ctx);
517 spin_unlock(&sec->ps_lock);
520 static void construct_key_desc(void *buf, int bufsize,
521 struct ptlrpc_sec *sec, uid_t uid)
523 snprintf(buf, bufsize, "%d@%x", uid, sec2gsec_keyring(sec)->gsk_id);
524 ((char *)buf)[bufsize - 1] = '\0';
527 /****************************************
529 ****************************************/
531 static atomic_t gss_sec_id_kr = ATOMIC_INIT(0);
534 struct ptlrpc_sec * gss_sec_create_kr(struct obd_import *imp,
535 struct ptlrpc_svc_ctx *ctx,
539 struct gss_sec_keyring *gsec_kr;
542 OBD_ALLOC(gsec_kr, sizeof(*gsec_kr));
546 gsec_kr->gsk_id = atomic_inc_return(&gss_sec_id_kr);
547 CFS_INIT_HLIST_HEAD(&gsec_kr->gsk_clist);
548 gsec_kr->gsk_root_ctx = NULL;
549 mutex_init(&gsec_kr->gsk_root_uc_lock);
550 #ifdef HAVE_KEYRING_UPCALL_SERIALIZED
551 mutex_init(&gsec_kr->gsk_uc_lock);
554 if (gss_sec_create_common(&gsec_kr->gsk_base, &gss_policy_keyring,
555 imp, ctx, flavor, flags))
559 if (sec_install_rctx_kr(&gsec_kr->gsk_base.gs_base, ctx)) {
560 gss_sec_destroy_common(&gsec_kr->gsk_base);
565 RETURN(&gsec_kr->gsk_base.gs_base);
568 OBD_FREE(gsec_kr, sizeof(*gsec_kr));
573 void gss_sec_destroy_kr(struct ptlrpc_sec *sec)
575 struct gss_sec *gsec = sec2gsec(sec);
576 struct gss_sec_keyring *gsec_kr = sec2gsec_keyring(sec);
578 CDEBUG(D_SEC, "destroy %s@%p\n", sec->ps_policy->sp_name, sec);
580 LASSERT(hlist_empty(&gsec_kr->gsk_clist));
581 LASSERT(gsec_kr->gsk_root_ctx == NULL);
583 gss_sec_destroy_common(gsec);
585 OBD_FREE(gsec_kr, sizeof(*gsec_kr));
589 int user_is_root(struct ptlrpc_sec *sec, struct vfs_cred *vcred)
591 if (sec->ps_flags & PTLRPC_SEC_FL_ROOTONLY)
595 * more precisely deal with setuid. maybe add more infomation
596 * into vfs_cred ?? */
597 return (vcred->vc_uid == 0);
601 * unlink request key from it's ring, which is linked during request_key().
602 * sadly, we have to 'guess' which keyring it's linked to.
604 * FIXME this code is fragile, depend on how request_key_link() is implemented.
606 static void request_key_unlink(struct key *key)
608 struct task_struct *tsk = current;
611 switch (task_aux(tsk)->jit_keyring) {
612 case KEY_REQKEY_DEFL_DEFAULT:
613 case KEY_REQKEY_DEFL_THREAD_KEYRING:
614 ring = key_get(task_aux(tsk)->thread_keyring);
617 case KEY_REQKEY_DEFL_PROCESS_KEYRING:
618 ring = key_get(tsk->signal->process_keyring);
621 case KEY_REQKEY_DEFL_SESSION_KEYRING:
623 ring = key_get(rcu_dereference(tsk->signal->session_keyring));
627 case KEY_REQKEY_DEFL_USER_SESSION_KEYRING:
628 ring = key_get(tsk->user->session_keyring);
630 case KEY_REQKEY_DEFL_USER_KEYRING:
631 ring = key_get(tsk->user->uid_keyring);
633 case KEY_REQKEY_DEFL_GROUP_KEYRING:
639 key_unlink(ring, key);
644 struct ptlrpc_cli_ctx * gss_sec_lookup_ctx_kr(struct ptlrpc_sec *sec,
645 struct vfs_cred *vcred,
646 int create, int remove_dead)
648 struct obd_import *imp = sec->ps_import;
649 struct gss_sec_keyring *gsec_kr = sec2gsec_keyring(sec);
650 struct ptlrpc_cli_ctx *ctx = NULL;
651 unsigned int is_root = 0, create_new = 0;
655 const int coinfo_size = sizeof(struct obd_uuid) + 64;
659 LASSERT(imp != NULL);
661 is_root = user_is_root(sec, vcred);
663 /* a little bit optimization for root context */
665 ctx = sec_lookup_root_ctx_kr(sec);
667 * Only lookup directly for REVERSE sec, which should
670 if (ctx || sec_is_reverse(sec))
674 LASSERT(create != 0);
676 /* for root context, obtain lock and check again, this time hold
677 * the root upcall lock, make sure nobody else populated new root
678 * context after last check. */
680 mutex_lock(&gsec_kr->gsk_root_uc_lock);
682 ctx = sec_lookup_root_ctx_kr(sec);
686 /* update reverse handle for root user */
687 sec2gsec(sec)->gs_rvs_hdl = gss_get_next_ctx_index();
692 construct_key_desc(desc, sizeof(desc), sec, vcred->vc_uid);
694 /* callout info: mech:flags:svc_type:peer_nid:target_uuid */
695 OBD_ALLOC(coinfo, coinfo_size);
699 snprintf(coinfo, coinfo_size, "%s:%s:%d:"LPX64":%s",
700 sec2gsec(sec)->gs_mech->gm_name,
701 co_flags, import_to_gss_svc(imp),
702 imp->imp_connection->c_peer.nid, imp->imp_obd->obd_name);
704 keyring_upcall_lock(gsec_kr);
705 key = request_key(&gss_key_type, desc, coinfo);
706 keyring_upcall_unlock(gsec_kr);
708 OBD_FREE(coinfo, coinfo_size);
711 CERROR("failed request key: %ld\n", PTR_ERR(key));
715 /* once payload.data was pointed to a ctx, it never changes until
716 * we de-associate them; but parallel request_key() may return
717 * a key with payload.data == NULL at the same time. so we still
718 * need wirtelock of key->sem to serialize them. */
719 down_write(&key->sem);
721 if (likely(key->payload.data != NULL)) {
722 ctx = key->payload.data;
724 LASSERT(atomic_read(&ctx->cc_refcount) >= 1);
725 LASSERT(ctx2gctx_keyring(ctx)->gck_key == key);
726 LASSERT(atomic_read(&key->usage) >= 2);
728 /* simply take a ref and return. it's upper layer's
729 * responsibility to detect & replace dead ctx. */
730 atomic_inc(&ctx->cc_refcount);
732 /* pre initialization with a cli_ctx. this can't be done in
733 * key_instantiate() because we'v no enough information
735 ctx = ctx_create_kr(sec, vcred);
737 ctx_enlist_kr(ctx, is_root, 0);
738 bind_key_ctx(key, ctx);
740 ctx_start_timer_kr(ctx, KEYRING_UPCALL_TIMEOUT);
742 CDEBUG(D_SEC, "installed key %p <-> ctx %p (sec %p)\n",
745 /* we'd prefer to call key_revoke(), but we more like
746 * to revoke it within this key->sem locked period. */
747 key_revoke_locked(key);
755 if (is_root && create_new)
756 request_key_unlink(key);
761 mutex_unlock(&gsec_kr->gsk_root_uc_lock);
766 void gss_sec_release_ctx_kr(struct ptlrpc_sec *sec,
767 struct ptlrpc_cli_ctx *ctx,
770 LASSERT(atomic_read(&ctx->cc_refcount) == 0);
775 atomic_inc(&ctx->cc_refcount);
776 sptlrpc_gc_add_ctx(ctx);
781 * flush context of normal user, we must resort to keyring itself to find out
782 * contexts which belong to me.
784 * Note here we suppose only to flush _my_ context, the "uid" will
785 * be ignored in the search.
788 void flush_user_ctx_cache_kr(struct ptlrpc_sec *sec,
790 int grace, int force)
795 /* nothing to do for reverse or rootonly sec */
796 if (sec_is_reverse(sec) || sec_is_rootonly(sec))
799 construct_key_desc(desc, sizeof(desc), sec, uid);
801 /* there should be only one valid key, but we put it in the
802 * loop in case of any weird cases */
804 key = request_key(&gss_key_type, desc, NULL);
806 CWARN("No more key found for current user\n");
810 down_write(&key->sem);
812 kill_key_locked(key);
814 /* kill_key_locked() should usually revoke the key, but we
815 * revoke it again to make sure, e.g. some case the key may
816 * not well coupled with a context. */
817 key_revoke_locked(key);
826 * flush context of root or all, we iterate through the list.
829 void flush_spec_ctx_cache_kr(struct ptlrpc_sec *sec,
831 int grace, int force)
833 struct gss_sec_keyring *gsec_kr;
834 struct hlist_head freelist = CFS_HLIST_HEAD_INIT;
835 struct hlist_node *pos, *next;
836 struct ptlrpc_cli_ctx *ctx;
839 gsec_kr = sec2gsec_keyring(sec);
841 spin_lock(&sec->ps_lock);
842 hlist_for_each_entry_safe(ctx, pos, next,
843 &gsec_kr->gsk_clist, cc_cache) {
844 LASSERT(atomic_read(&ctx->cc_refcount) > 0);
846 if (uid != -1 && uid != ctx->cc_vcred.vc_uid)
849 /* at this moment there's at least 2 base reference:
850 * key association and in-list. */
851 if (atomic_read(&ctx->cc_refcount) > 2) {
854 CWARN("flush busy ctx %p(%u->%s, extra ref %d)\n",
855 ctx, ctx->cc_vcred.vc_uid,
856 sec2target_str(ctx->cc_sec),
857 atomic_read(&ctx->cc_refcount) - 2);
860 set_bit(PTLRPC_CTX_DEAD_BIT, &ctx->cc_flags);
862 clear_bit(PTLRPC_CTX_UPTODATE_BIT, &ctx->cc_flags);
864 atomic_inc(&ctx->cc_refcount);
866 if (ctx_unlist_kr(ctx, 1))
867 hlist_add_head(&ctx->cc_cache, &freelist);
869 LASSERT(atomic_read(&ctx->cc_refcount) >= 2);
870 atomic_dec(&ctx->cc_refcount);
874 spin_unlock(&sec->ps_lock);
876 dispose_ctx_list_kr(&freelist);
881 int gss_sec_flush_ctx_cache_kr(struct ptlrpc_sec *sec,
883 int grace, int force)
887 CDEBUG(D_SEC, "sec %p(%d, busy %d), uid %d, grace %d, force %d\n",
888 sec, atomic_read(&sec->ps_refcount), atomic_read(&sec->ps_busy),
891 if (uid != -1 && uid != 0)
892 flush_user_ctx_cache_kr(sec, uid, grace, force);
894 flush_spec_ctx_cache_kr(sec, uid, grace, force);
900 void gss_sec_gc_ctx_kr(struct ptlrpc_sec *sec)
902 struct gss_sec_keyring *gsec_kr = sec2gsec_keyring(sec);
903 struct hlist_head freelist = CFS_HLIST_HEAD_INIT;
904 struct hlist_node *pos, *next;
905 struct ptlrpc_cli_ctx *ctx;
908 CWARN("running gc\n");
910 spin_lock(&sec->ps_lock);
911 hlist_for_each_entry_safe(ctx, pos, next,
912 &gsec_kr->gsk_clist, cc_cache) {
913 LASSERT(atomic_read(&ctx->cc_refcount) > 0);
915 atomic_inc(&ctx->cc_refcount);
917 if (cli_ctx_check_death(ctx) && ctx_unlist_kr(ctx, 1)) {
918 hlist_add_head(&ctx->cc_cache, &freelist);
919 CWARN("unhashed ctx %p\n", ctx);
921 LASSERT(atomic_read(&ctx->cc_refcount) >= 2);
922 atomic_dec(&ctx->cc_refcount);
925 spin_unlock(&sec->ps_lock);
927 dispose_ctx_list_kr(&freelist);
933 int gss_sec_display_kr(struct ptlrpc_sec *sec, char *buf, int bufsize)
935 struct gss_sec_keyring *gsec_kr = sec2gsec_keyring(sec);
936 struct hlist_node *pos, *next;
937 struct ptlrpc_cli_ctx *ctx;
941 written = snprintf(buf, bufsize, "context list ===>\n");
945 spin_lock(&sec->ps_lock);
946 hlist_for_each_entry_safe(ctx, pos, next,
947 &gsec_kr->gsk_clist, cc_cache) {
948 struct gss_cli_ctx *gctx;
953 gctx = ctx2gctx(ctx);
954 key = ctx2gctx_keyring(ctx)->gck_key;
956 gss_cli_ctx_flags2str(ctx->cc_flags,
957 flags_str, sizeof(flags_str));
959 len = snprintf(buf, bufsize, "%p(%d): uid %u, exp %ld(%ld)s, "
960 "fl %s, seq %d, win %u, key %08x(%d), ",
961 ctx, atomic_read(&ctx->cc_refcount),
962 ctx->cc_vcred.vc_uid,
964 ctx->cc_expire - cfs_time_current_sec(),
966 atomic_read(&gctx->gc_seq),
968 key ? key->serial : 0,
969 key ? atomic_read(&key->usage) : 0);
978 if (gctx->gc_mechctx)
979 len = lgss_display(gctx->gc_mechctx, buf, bufsize);
981 len = snprintf(buf, bufsize, "mech N/A\n");
990 spin_unlock(&sec->ps_lock);
995 /****************************************
997 ****************************************/
1000 int gss_cli_ctx_refresh_kr(struct ptlrpc_cli_ctx *ctx)
1002 /* upcall is already on the way */
1007 int gss_cli_ctx_validate_kr(struct ptlrpc_cli_ctx *ctx)
1009 LASSERT(atomic_read(&ctx->cc_refcount) > 0);
1010 LASSERT(ctx->cc_sec);
1012 if (cli_ctx_check_death(ctx)) {
1017 if (cli_ctx_is_ready(ctx))
1023 void gss_cli_ctx_die_kr(struct ptlrpc_cli_ctx *ctx, int grace)
1025 LASSERT(atomic_read(&ctx->cc_refcount) > 0);
1026 LASSERT(ctx->cc_sec);
1028 CWARN("ctx %p(%d)\n", ctx, atomic_read(&ctx->cc_refcount));
1029 cli_ctx_expire(ctx);
1033 /****************************************
1034 * (reverse) service *
1035 ****************************************/
1038 * reverse context could have nothing to do with keyrings. here we still keep
1039 * the version which bind to a key, for future reference.
1041 #define HAVE_REVERSE_CTX_NOKEY
1043 #ifdef HAVE_REVERSE_CTX_NOKEY
1046 int sec_install_rctx_kr(struct ptlrpc_sec *sec,
1047 struct ptlrpc_svc_ctx *svc_ctx)
1049 struct ptlrpc_cli_ctx *cli_ctx;
1050 struct vfs_cred vcred = { 0, 0 };
1056 cli_ctx = ctx_create_kr(sec, &vcred);
1057 if (cli_ctx == NULL)
1060 rc = gss_copy_rvc_cli_ctx(cli_ctx, svc_ctx);
1062 CERROR("failed copy reverse cli ctx: %d\n", rc);
1064 ctx_put_kr(cli_ctx);
1068 rvs_sec_install_root_ctx_kr(sec, cli_ctx, NULL);
1070 ctx_put_kr(cli_ctx);
1075 #else /* ! 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 = NULL;
1083 struct vfs_cred vcred = { 0, 0 };
1091 construct_key_desc(desc, sizeof(desc), sec, 0);
1093 key = key_alloc(&gss_key_type, desc, 0, 0,
1094 KEY_POS_ALL | KEY_USR_ALL, 1);
1096 CERROR("failed to alloc key: %ld\n", PTR_ERR(key));
1097 return PTR_ERR(key);
1100 rc = key_instantiate_and_link(key, NULL, 0, NULL, NULL);
1102 CERROR("failed to instantiate key: %d\n", rc);
1106 down_write(&key->sem);
1108 LASSERT(key->payload.data == NULL);
1110 cli_ctx = ctx_create_kr(sec, &vcred);
1111 if (cli_ctx == NULL) {
1116 rc = gss_copy_rvc_cli_ctx(cli_ctx, svc_ctx);
1118 CERROR("failed copy reverse cli ctx: %d\n", rc);
1122 rvs_sec_install_root_ctx_kr(sec, cli_ctx, key);
1124 ctx_put_kr(cli_ctx);
1125 up_write(&key->sem);
1134 ctx_put_kr(cli_ctx);
1136 up_write(&key->sem);
1142 #endif /* HAVE_REVERSE_CTX_NOKEY */
1144 /****************************************
1146 ****************************************/
1149 int gss_svc_accept_kr(struct ptlrpc_request *req)
1151 return gss_svc_accept(&gss_policy_keyring, req);
1155 int gss_svc_install_rctx_kr(struct obd_import *imp,
1156 struct ptlrpc_svc_ctx *svc_ctx)
1158 LASSERT(imp->imp_sec);
1160 return sec_install_rctx_kr(imp->imp_sec, svc_ctx);
1163 /****************************************
1165 ****************************************/
1168 int gss_kt_instantiate(struct key *key, const void *data, size_t datalen)
1172 if (data != NULL || datalen != 0) {
1173 CERROR("invalid: data %p, len %d\n", data, datalen);
1177 if (key->payload.data != 0) {
1178 CERROR("key already have payload\n");
1183 key->perm |= KEY_POS_ALL | KEY_USR_ALL;
1184 CDEBUG(D_SEC, "key %p instantiated, ctx %p\n", key, key->payload.data);
1189 * called with key semaphore write locked. it means we can operate
1190 * on the context without fear of loosing refcount.
1193 int gss_kt_update(struct key *key, const void *data, size_t datalen)
1195 struct ptlrpc_cli_ctx *ctx = key->payload.data;
1196 struct gss_cli_ctx *gctx;
1197 rawobj_t tmpobj = RAWOBJ_EMPTY;
1201 if (data == NULL || datalen == 0) {
1202 CWARN("invalid: data %p, len %d\n", data, datalen);
1206 /* there's a race between userspace parent - child processes. if
1207 * child finish negotiation too fast and call kt_update(), the ctx
1208 * might be still NULL. but the key will finally be associate
1209 * with a context, or be revoked. if key status is fine, return
1210 * -EAGAIN to allow userspace sleep a while and call again. */
1212 CWARN("race in userspace. key %p(%x) flags %lx\n",
1213 key, key->serial, key->flags);
1215 rc = key_validate(key);
1222 LASSERT(atomic_read(&ctx->cc_refcount) > 0);
1223 LASSERT(ctx->cc_sec);
1225 ctx_clear_timer_kr(ctx);
1227 /* don't proceed if already refreshed */
1228 if (cli_ctx_is_refreshed(ctx)) {
1229 CWARN("ctx already done refresh\n");
1230 sptlrpc_cli_ctx_wakeup(ctx);
1234 sptlrpc_cli_ctx_get(ctx);
1235 gctx = ctx2gctx(ctx);
1237 rc = buffer_extract_bytes(&data, &datalen, &gctx->gc_win,
1238 sizeof(gctx->gc_win));
1240 CERROR("failed extract seq_win\n");
1244 if (gctx->gc_win == 0) {
1245 __u32 nego_rpc_err, nego_gss_err;
1247 rc = buffer_extract_bytes(&data, &datalen, &nego_rpc_err,
1248 sizeof(nego_rpc_err));
1250 CERROR("failed to extrace rpc rc\n");
1254 rc = buffer_extract_bytes(&data, &datalen, &nego_gss_err,
1255 sizeof(nego_gss_err));
1257 CERROR("failed to extrace gss rc\n");
1261 CERROR("negotiation: rpc err %d, gss err %x\n",
1262 nego_rpc_err, nego_gss_err);
1264 rc = nego_rpc_err ? nego_rpc_err : -EACCES;
1266 rc = rawobj_extract_local_alloc(&gctx->gc_handle,
1267 (__u32 **) &data, &datalen);
1269 CERROR("failed extract handle\n");
1273 rc = rawobj_extract_local(&tmpobj, (__u32 **) &data, &datalen);
1275 CERROR("failed extract mech\n");
1279 rc = lgss_import_sec_context(&tmpobj,
1280 sec2gsec(ctx->cc_sec)->gs_mech,
1282 if (rc != GSS_S_COMPLETE)
1283 CERROR("failed import context\n");
1288 /* we don't care what current status of this ctx, even someone else
1289 * is operating on the ctx at the same time. we just add up our own
1292 gss_cli_ctx_uptodate(gctx);
1294 /* this will also revoke the key. has to be done before
1295 * wakeup waiters otherwise they can find the stale key */
1296 kill_key_locked(key);
1298 cli_ctx_expire(ctx);
1300 if (rc != -ERESTART)
1301 set_bit(PTLRPC_CTX_ERROR_BIT, &ctx->cc_flags);
1304 sptlrpc_cli_ctx_wakeup(ctx);
1306 /* let user space think it's a success */
1307 sptlrpc_cli_ctx_put(ctx, 1);
1312 int gss_kt_match(const struct key *key, const void *desc)
1314 return (strcmp(key->description, (const char *) desc) == 0);
1318 void gss_kt_destroy(struct key *key)
1321 LASSERT(key->payload.data == NULL);
1322 CDEBUG(D_SEC, "destroy key %p\n", key);
1327 void gss_kt_describe(const struct key *key, struct seq_file *s)
1329 if (key->description == NULL)
1330 seq_puts(s, "[null]");
1332 seq_puts(s, key->description);
1335 static struct key_type gss_key_type =
1339 .instantiate = gss_kt_instantiate,
1340 .update = gss_kt_update,
1341 .match = gss_kt_match,
1342 .destroy = gss_kt_destroy,
1343 .describe = gss_kt_describe,
1346 /****************************************
1347 * lustre gss keyring policy *
1348 ****************************************/
1350 static struct ptlrpc_ctx_ops gss_keyring_ctxops = {
1351 .match = gss_cli_ctx_match,
1352 .refresh = gss_cli_ctx_refresh_kr,
1353 .validate = gss_cli_ctx_validate_kr,
1354 .die = gss_cli_ctx_die_kr,
1355 .sign = gss_cli_ctx_sign,
1356 .verify = gss_cli_ctx_verify,
1357 .seal = gss_cli_ctx_seal,
1358 .unseal = gss_cli_ctx_unseal,
1359 .wrap_bulk = gss_cli_ctx_wrap_bulk,
1360 .unwrap_bulk = gss_cli_ctx_unwrap_bulk,
1363 static struct ptlrpc_sec_cops gss_sec_keyring_cops = {
1364 .create_sec = gss_sec_create_kr,
1365 .destroy_sec = gss_sec_destroy_kr,
1366 .lookup_ctx = gss_sec_lookup_ctx_kr,
1367 .release_ctx = gss_sec_release_ctx_kr,
1368 .flush_ctx_cache = gss_sec_flush_ctx_cache_kr,
1369 .gc_ctx = gss_sec_gc_ctx_kr,
1370 .install_rctx = gss_sec_install_rctx,
1371 .alloc_reqbuf = gss_alloc_reqbuf,
1372 .free_reqbuf = gss_free_reqbuf,
1373 .alloc_repbuf = gss_alloc_repbuf,
1374 .free_repbuf = gss_free_repbuf,
1375 .enlarge_reqbuf = gss_enlarge_reqbuf,
1376 .display = gss_sec_display_kr,
1379 static struct ptlrpc_sec_sops gss_sec_keyring_sops = {
1380 .accept = gss_svc_accept_kr,
1381 .invalidate_ctx = gss_svc_invalidate_ctx,
1382 .alloc_rs = gss_svc_alloc_rs,
1383 .authorize = gss_svc_authorize,
1384 .free_rs = gss_svc_free_rs,
1385 .free_ctx = gss_svc_free_ctx,
1386 .unwrap_bulk = gss_svc_unwrap_bulk,
1387 .wrap_bulk = gss_svc_wrap_bulk,
1388 .install_rctx = gss_svc_install_rctx_kr,
1391 static struct ptlrpc_sec_policy gss_policy_keyring = {
1392 .sp_owner = THIS_MODULE,
1393 .sp_name = "gss.keyring",
1394 .sp_policy = SPTLRPC_POLICY_GSS,
1395 .sp_cops = &gss_sec_keyring_cops,
1396 .sp_sops = &gss_sec_keyring_sops,
1400 int __init gss_init_keyring(void)
1404 rc = register_key_type(&gss_key_type);
1406 CERROR("failed to register keyring type: %d\n", rc);
1410 rc = sptlrpc_register_policy(&gss_policy_keyring);
1412 unregister_key_type(&gss_key_type);
1419 void __exit gss_exit_keyring(void)
1421 unregister_key_type(&gss_key_type);
1422 sptlrpc_unregister_policy(&gss_policy_keyring);