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 2008 Sun Microsystems, Inc. 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/random.h>
52 #include <linux/crypto.h>
53 #include <linux/key.h>
54 #include <linux/keyctl.h>
55 #include <linux/mutex.h>
56 #include <asm/atomic.h>
58 #include <liblustre.h>
62 #include <obd_class.h>
63 #include <obd_support.h>
64 #include <lustre/lustre_idl.h>
65 #include <lustre_sec.h>
66 #include <lustre_net.h>
67 #include <lustre_import.h>
70 #include "gss_internal.h"
73 static struct ptlrpc_sec_policy gss_policy_keyring;
74 static struct ptlrpc_ctx_ops gss_keyring_ctxops;
75 static struct key_type gss_key_type;
77 static int sec_install_rctx_kr(struct ptlrpc_sec *sec,
78 struct ptlrpc_svc_ctx *svc_ctx);
81 * the timeout is only for the case that upcall child process die abnormally.
82 * in any other cases it should finally update kernel key.
84 * FIXME we'd better to incorporate the client & server side upcall timeouts
85 * into the framework of Adaptive Timeouts, but we need to figure out how to
86 * make sure that kernel knows the upcall processes is in-progress or died
89 #define KEYRING_UPCALL_TIMEOUT (obd_timeout + obd_timeout)
91 /****************************************
93 ****************************************/
95 #define DUMP_PROCESS_KEYRINGS(tsk) \
97 CWARN("DUMP PK: %s[%u,%u/%u](<-%s[%u,%u/%u]): " \
98 "a %d, t %d, p %d, s %d, u %d, us %d, df %d\n", \
99 tsk->comm, tsk->pid, tsk->uid, tsk->fsuid, \
100 tsk->parent->comm, tsk->parent->pid, \
101 tsk->parent->uid, tsk->parent->fsuid, \
102 tsk->request_key_auth ? \
103 tsk->request_key_auth->serial : 0, \
104 tsk->thread_keyring ? \
105 tsk->thread_keyring->serial : 0, \
106 tsk->signal->process_keyring ? \
107 tsk->signal->process_keyring->serial : 0, \
108 tsk->signal->session_keyring ? \
109 tsk->signal->session_keyring->serial : 0, \
110 tsk->user->uid_keyring ? \
111 tsk->user->uid_keyring->serial : 0, \
112 tsk->user->session_keyring ? \
113 tsk->user->session_keyring->serial : 0, \
118 #define DUMP_KEY(key) \
120 CWARN("DUMP KEY: %p(%d) ref %d u%u/g%u desc %s\n", \
121 key, key->serial, atomic_read(&key->usage), \
122 key->uid, key->gid, \
123 key->description ? key->description : "n/a" \
128 static inline void keyring_upcall_lock(struct gss_sec_keyring *gsec_kr)
130 #ifdef HAVE_KEYRING_UPCALL_SERIALIZED
131 cfs_mutex_lock(&gsec_kr->gsk_uc_lock);
135 static inline void keyring_upcall_unlock(struct gss_sec_keyring *gsec_kr)
137 #ifdef HAVE_KEYRING_UPCALL_SERIALIZED
138 cfs_mutex_unlock(&gsec_kr->gsk_uc_lock);
142 static inline void key_revoke_locked(struct key *key)
144 set_bit(KEY_FLAG_REVOKED, &key->flags);
147 static void ctx_upcall_timeout_kr(unsigned long data)
149 struct ptlrpc_cli_ctx *ctx = (struct ptlrpc_cli_ctx *) data;
150 struct key *key = ctx2gctx_keyring(ctx)->gck_key;
152 CWARN("ctx %p, key %p\n", ctx, key);
157 key_revoke_locked(key);
161 void ctx_start_timer_kr(struct ptlrpc_cli_ctx *ctx, long timeout)
163 struct gss_cli_ctx_keyring *gctx_kr = ctx2gctx_keyring(ctx);
164 struct timer_list *timer = gctx_kr->gck_timer;
168 CDEBUG(D_SEC, "ctx %p: start timer %lds\n", ctx, timeout);
169 timeout = timeout * CFS_HZ + cfs_time_current();
172 timer->expires = timeout;
173 timer->data = (unsigned long ) ctx;
174 timer->function = ctx_upcall_timeout_kr;
180 * caller should make sure no race with other threads
183 void ctx_clear_timer_kr(struct ptlrpc_cli_ctx *ctx)
185 struct gss_cli_ctx_keyring *gctx_kr = ctx2gctx_keyring(ctx);
186 struct timer_list *timer = gctx_kr->gck_timer;
191 CDEBUG(D_SEC, "ctx %p, key %p\n", ctx, gctx_kr->gck_key);
193 gctx_kr->gck_timer = NULL;
195 del_singleshot_timer_sync(timer);
201 struct ptlrpc_cli_ctx *ctx_create_kr(struct ptlrpc_sec *sec,
202 struct vfs_cred *vcred)
204 struct ptlrpc_cli_ctx *ctx;
205 struct gss_cli_ctx_keyring *gctx_kr;
207 OBD_ALLOC_PTR(gctx_kr);
211 OBD_ALLOC_PTR(gctx_kr->gck_timer);
212 if (gctx_kr->gck_timer == NULL) {
213 OBD_FREE_PTR(gctx_kr);
216 init_timer(gctx_kr->gck_timer);
218 ctx = &gctx_kr->gck_base.gc_base;
220 if (gss_cli_ctx_init_common(sec, ctx, &gss_keyring_ctxops, vcred)) {
221 OBD_FREE_PTR(gctx_kr->gck_timer);
222 OBD_FREE_PTR(gctx_kr);
226 ctx->cc_expire = cfs_time_current_sec() + KEYRING_UPCALL_TIMEOUT;
227 cfs_clear_bit(PTLRPC_CTX_NEW_BIT, &ctx->cc_flags);
228 cfs_atomic_inc(&ctx->cc_refcount); /* for the caller */
233 static void ctx_destroy_kr(struct ptlrpc_cli_ctx *ctx)
235 struct ptlrpc_sec *sec = ctx->cc_sec;
236 struct gss_cli_ctx_keyring *gctx_kr = ctx2gctx_keyring(ctx);
238 CDEBUG(D_SEC, "destroying ctx %p\n", ctx);
240 /* at this time the association with key has been broken. */
242 LASSERT(cfs_atomic_read(&sec->ps_refcount) > 0);
243 LASSERT(cfs_atomic_read(&sec->ps_nctx) > 0);
244 LASSERT(cfs_test_bit(PTLRPC_CTX_CACHED_BIT, &ctx->cc_flags) == 0);
245 LASSERT(gctx_kr->gck_key == NULL);
247 ctx_clear_timer_kr(ctx);
248 LASSERT(gctx_kr->gck_timer == NULL);
250 if (gss_cli_ctx_fini_common(sec, ctx))
253 OBD_FREE_PTR(gctx_kr);
255 cfs_atomic_dec(&sec->ps_nctx);
256 sptlrpc_sec_put(sec);
259 static void ctx_release_kr(struct ptlrpc_cli_ctx *ctx, int sync)
264 cfs_atomic_inc(&ctx->cc_refcount);
265 sptlrpc_gc_add_ctx(ctx);
269 static void ctx_put_kr(struct ptlrpc_cli_ctx *ctx, int sync)
271 LASSERT(cfs_atomic_read(&ctx->cc_refcount) > 0);
273 if (cfs_atomic_dec_and_test(&ctx->cc_refcount))
274 ctx_release_kr(ctx, sync);
278 * key <-> ctx association and rules:
279 * - ctx might not bind with any key
280 * - key/ctx binding is protected by key semaphore (if the key present)
281 * - key and ctx each take a reference of the other
282 * - ctx enlist/unlist is protected by ctx spinlock
283 * - never enlist a ctx after it's been unlisted
284 * - whoever do enlist should also do bind, lock key before enlist:
285 * - lock key -> lock ctx -> enlist -> unlock ctx -> bind -> unlock key
286 * - whoever do unlist should also do unbind:
287 * - lock key -> lock ctx -> unlist -> unlock ctx -> unbind -> unlock key
288 * - lock ctx -> unlist -> unlock ctx -> lock key -> unbind -> unlock key
291 static inline void spin_lock_if(cfs_spinlock_t *lock, int condition)
297 static inline void spin_unlock_if(cfs_spinlock_t *lock, int condition)
300 cfs_spin_unlock(lock);
303 static void ctx_enlist_kr(struct ptlrpc_cli_ctx *ctx, int is_root, int locked)
305 struct ptlrpc_sec *sec = ctx->cc_sec;
306 struct gss_sec_keyring *gsec_kr = sec2gsec_keyring(sec);
308 LASSERT(!cfs_test_bit(PTLRPC_CTX_CACHED_BIT, &ctx->cc_flags));
309 LASSERT(cfs_atomic_read(&ctx->cc_refcount) > 0);
311 spin_lock_if(&sec->ps_lock, !locked);
313 cfs_atomic_inc(&ctx->cc_refcount);
314 cfs_set_bit(PTLRPC_CTX_CACHED_BIT, &ctx->cc_flags);
315 cfs_hlist_add_head(&ctx->cc_cache, &gsec_kr->gsk_clist);
317 gsec_kr->gsk_root_ctx = ctx;
319 spin_unlock_if(&sec->ps_lock, !locked);
323 * Note after this get called, caller should not access ctx again because
324 * it might have been freed, unless caller hold at least one refcount of
327 * return non-zero if we indeed unlist this ctx.
329 static int ctx_unlist_kr(struct ptlrpc_cli_ctx *ctx, int locked)
331 struct ptlrpc_sec *sec = ctx->cc_sec;
332 struct gss_sec_keyring *gsec_kr = sec2gsec_keyring(sec);
334 /* if hashed bit has gone, leave the job to somebody who is doing it */
335 if (cfs_test_and_clear_bit(PTLRPC_CTX_CACHED_BIT, &ctx->cc_flags) == 0)
338 /* drop ref inside spin lock to prevent race with other operations */
339 spin_lock_if(&sec->ps_lock, !locked);
341 if (gsec_kr->gsk_root_ctx == ctx)
342 gsec_kr->gsk_root_ctx = NULL;
343 cfs_hlist_del_init(&ctx->cc_cache);
344 cfs_atomic_dec(&ctx->cc_refcount);
346 spin_unlock_if(&sec->ps_lock, !locked);
352 * bind a key with a ctx together.
353 * caller must hold write lock of the key, as well as ref on key & ctx.
355 static void bind_key_ctx(struct key *key, struct ptlrpc_cli_ctx *ctx)
357 LASSERT(cfs_atomic_read(&ctx->cc_refcount) > 0);
358 LASSERT(atomic_read(&key->usage) > 0);
359 LASSERT(ctx2gctx_keyring(ctx)->gck_key == NULL);
360 LASSERT(key->payload.data == NULL);
362 /* at this time context may or may not in list. */
364 cfs_atomic_inc(&ctx->cc_refcount);
365 ctx2gctx_keyring(ctx)->gck_key = key;
366 key->payload.data = ctx;
370 * unbind a key and a ctx.
371 * caller must hold write lock, as well as a ref of the key.
373 static void unbind_key_ctx(struct key *key, struct ptlrpc_cli_ctx *ctx)
375 LASSERT(key->payload.data == ctx);
376 LASSERT(cfs_test_bit(PTLRPC_CTX_CACHED_BIT, &ctx->cc_flags) == 0);
378 /* must revoke the key, or others may treat it as newly created */
379 key_revoke_locked(key);
381 key->payload.data = NULL;
382 ctx2gctx_keyring(ctx)->gck_key = NULL;
384 /* once ctx get split from key, the timer is meaningless */
385 ctx_clear_timer_kr(ctx);
392 * given a ctx, unbind with its coupled key, if any.
393 * unbind could only be called once, so we don't worry the key be released
396 static void unbind_ctx_kr(struct ptlrpc_cli_ctx *ctx)
398 struct key *key = ctx2gctx_keyring(ctx)->gck_key;
401 LASSERT(key->payload.data == ctx);
404 down_write(&key->sem);
405 unbind_key_ctx(key, ctx);
412 * given a key, unbind with its coupled ctx, if any.
413 * caller must hold write lock, as well as a ref of the key.
415 static void unbind_key_locked(struct key *key)
417 struct ptlrpc_cli_ctx *ctx = key->payload.data;
420 unbind_key_ctx(key, ctx);
424 * unlist a ctx, and unbind from coupled key
426 static void kill_ctx_kr(struct ptlrpc_cli_ctx *ctx)
428 if (ctx_unlist_kr(ctx, 0))
433 * given a key, unlist and unbind with the coupled ctx (if any).
434 * caller must hold write lock, as well as a ref of the key.
436 static void kill_key_locked(struct key *key)
438 struct ptlrpc_cli_ctx *ctx = key->payload.data;
440 if (ctx && ctx_unlist_kr(ctx, 0))
441 unbind_key_locked(key);
445 * caller should hold one ref on contexts in freelist.
447 static void dispose_ctx_list_kr(cfs_hlist_head_t *freelist)
449 cfs_hlist_node_t *pos, *next;
450 struct ptlrpc_cli_ctx *ctx;
451 struct gss_cli_ctx *gctx;
453 cfs_hlist_for_each_entry_safe(ctx, pos, next, freelist, cc_cache) {
454 cfs_hlist_del_init(&ctx->cc_cache);
456 /* reverse ctx: update current seq to buddy svcctx if exist.
457 * ideally this should be done at gss_cli_ctx_finalize(), but
458 * the ctx destroy could be delayed by:
459 * 1) ctx still has reference;
460 * 2) ctx destroy is asynchronous;
461 * and reverse import call inval_all_ctx() require this be done
462 *_immediately_ otherwise newly created reverse ctx might copy
463 * the very old sequence number from svcctx. */
464 gctx = ctx2gctx(ctx);
465 if (!rawobj_empty(&gctx->gc_svc_handle) &&
466 sec_is_reverse(gctx->gc_base.cc_sec)) {
467 gss_svc_upcall_update_sequence(&gctx->gc_svc_handle,
468 (__u32) cfs_atomic_read(&gctx->gc_seq));
471 /* we need to wakeup waiting reqs here. the context might
472 * be forced released before upcall finished, then the
473 * late-arrived downcall can't find the ctx even. */
474 sptlrpc_cli_ctx_wakeup(ctx);
482 * lookup a root context directly in a sec, return root ctx with a
483 * reference taken or NULL.
486 struct ptlrpc_cli_ctx * sec_lookup_root_ctx_kr(struct ptlrpc_sec *sec)
488 struct gss_sec_keyring *gsec_kr = sec2gsec_keyring(sec);
489 struct ptlrpc_cli_ctx *ctx = NULL;
491 cfs_spin_lock(&sec->ps_lock);
493 ctx = gsec_kr->gsk_root_ctx;
495 if (ctx == NULL && unlikely(sec_is_reverse(sec))) {
496 cfs_hlist_node_t *node;
497 struct ptlrpc_cli_ctx *tmp;
499 /* reverse ctx, search root ctx in list, choose the one
500 * with shortest expire time, which is most possibly have
501 * an established peer ctx at client side. */
502 cfs_hlist_for_each_entry(tmp, node, &gsec_kr->gsk_clist,
504 if (ctx == NULL || ctx->cc_expire == 0 ||
505 ctx->cc_expire > tmp->cc_expire) {
507 /* promote to be root_ctx */
508 gsec_kr->gsk_root_ctx = ctx;
514 LASSERT(cfs_atomic_read(&ctx->cc_refcount) > 0);
515 LASSERT(!cfs_hlist_empty(&gsec_kr->gsk_clist));
516 cfs_atomic_inc(&ctx->cc_refcount);
519 cfs_spin_unlock(&sec->ps_lock);
524 #define RVS_CTX_EXPIRE_NICE (10)
527 void rvs_sec_install_root_ctx_kr(struct ptlrpc_sec *sec,
528 struct ptlrpc_cli_ctx *new_ctx,
531 struct gss_sec_keyring *gsec_kr = sec2gsec_keyring(sec);
532 cfs_hlist_node_t *hnode;
533 struct ptlrpc_cli_ctx *ctx;
537 LASSERT(sec_is_reverse(sec));
539 cfs_spin_lock(&sec->ps_lock);
541 now = cfs_time_current_sec();
543 /* set all existing ctxs short expiry */
544 cfs_hlist_for_each_entry(ctx, hnode, &gsec_kr->gsk_clist, cc_cache) {
545 if (ctx->cc_expire > now + RVS_CTX_EXPIRE_NICE) {
546 ctx->cc_early_expire = 1;
547 ctx->cc_expire = now + RVS_CTX_EXPIRE_NICE;
551 /* if there's root_ctx there, instead obsolete the current
552 * immediately, we leave it continue operating for a little while.
553 * hopefully when the first backward rpc with newest ctx send out,
554 * the client side already have the peer ctx well established. */
555 ctx_enlist_kr(new_ctx, gsec_kr->gsk_root_ctx ? 0 : 1, 1);
558 bind_key_ctx(key, new_ctx);
560 cfs_spin_unlock(&sec->ps_lock);
563 static void construct_key_desc(void *buf, int bufsize,
564 struct ptlrpc_sec *sec, uid_t uid)
566 snprintf(buf, bufsize, "%d@%x", uid, sec->ps_id);
567 ((char *)buf)[bufsize - 1] = '\0';
570 /****************************************
572 ****************************************/
575 struct ptlrpc_sec * gss_sec_create_kr(struct obd_import *imp,
576 struct ptlrpc_svc_ctx *svcctx,
577 struct sptlrpc_flavor *sf)
579 struct gss_sec_keyring *gsec_kr;
582 OBD_ALLOC(gsec_kr, sizeof(*gsec_kr));
586 CFS_INIT_HLIST_HEAD(&gsec_kr->gsk_clist);
587 gsec_kr->gsk_root_ctx = NULL;
588 cfs_mutex_init(&gsec_kr->gsk_root_uc_lock);
589 #ifdef HAVE_KEYRING_UPCALL_SERIALIZED
590 cfs_mutex_init(&gsec_kr->gsk_uc_lock);
593 if (gss_sec_create_common(&gsec_kr->gsk_base, &gss_policy_keyring,
597 if (svcctx != NULL &&
598 sec_install_rctx_kr(&gsec_kr->gsk_base.gs_base, svcctx)) {
599 gss_sec_destroy_common(&gsec_kr->gsk_base);
603 RETURN(&gsec_kr->gsk_base.gs_base);
606 OBD_FREE(gsec_kr, sizeof(*gsec_kr));
611 void gss_sec_destroy_kr(struct ptlrpc_sec *sec)
613 struct gss_sec *gsec = sec2gsec(sec);
614 struct gss_sec_keyring *gsec_kr = sec2gsec_keyring(sec);
616 CDEBUG(D_SEC, "destroy %s@%p\n", sec->ps_policy->sp_name, sec);
618 LASSERT(cfs_hlist_empty(&gsec_kr->gsk_clist));
619 LASSERT(gsec_kr->gsk_root_ctx == NULL);
621 gss_sec_destroy_common(gsec);
623 OBD_FREE(gsec_kr, sizeof(*gsec_kr));
626 static inline int user_is_root(struct ptlrpc_sec *sec, struct vfs_cred *vcred)
628 /* except the ROOTONLY flag, treat it as root user only if real uid
629 * is 0, euid/fsuid being 0 are handled as setuid scenarios */
630 if (sec_is_rootonly(sec) || (vcred->vc_uid == 0))
637 * unlink request key from it's ring, which is linked during request_key().
638 * sadly, we have to 'guess' which keyring it's linked to.
640 * FIXME this code is fragile, depend on how request_key_link() is implemented.
642 static void request_key_unlink(struct key *key)
644 struct task_struct *tsk = current;
647 switch (tsk->jit_keyring) {
648 case KEY_REQKEY_DEFL_DEFAULT:
649 case KEY_REQKEY_DEFL_THREAD_KEYRING:
650 ring = key_get(tsk->thread_keyring);
653 case KEY_REQKEY_DEFL_PROCESS_KEYRING:
654 ring = key_get(tsk->signal->process_keyring);
657 case KEY_REQKEY_DEFL_SESSION_KEYRING:
659 ring = key_get(rcu_dereference(tsk->signal->session_keyring));
663 case KEY_REQKEY_DEFL_USER_SESSION_KEYRING:
664 ring = key_get(tsk->user->session_keyring);
666 case KEY_REQKEY_DEFL_USER_KEYRING:
667 ring = key_get(tsk->user->uid_keyring);
669 case KEY_REQKEY_DEFL_GROUP_KEYRING:
675 key_unlink(ring, key);
680 struct ptlrpc_cli_ctx * gss_sec_lookup_ctx_kr(struct ptlrpc_sec *sec,
681 struct vfs_cred *vcred,
682 int create, int remove_dead)
684 struct obd_import *imp = sec->ps_import;
685 struct gss_sec_keyring *gsec_kr = sec2gsec_keyring(sec);
686 struct ptlrpc_cli_ctx *ctx = NULL;
687 unsigned int is_root = 0, create_new = 0;
695 LASSERT(imp != NULL);
697 is_root = user_is_root(sec, vcred);
699 /* a little bit optimization for root context */
701 ctx = sec_lookup_root_ctx_kr(sec);
703 * Only lookup directly for REVERSE sec, which should
706 if (ctx || sec_is_reverse(sec))
710 LASSERT(create != 0);
712 /* for root context, obtain lock and check again, this time hold
713 * the root upcall lock, make sure nobody else populated new root
714 * context after last check. */
716 cfs_mutex_lock(&gsec_kr->gsk_root_uc_lock);
718 ctx = sec_lookup_root_ctx_kr(sec);
722 /* update reverse handle for root user */
723 sec2gsec(sec)->gs_rvs_hdl = gss_get_next_ctx_index();
728 /* in case of setuid, key will be constructed as owner of fsuid/fsgid,
729 * but we do authentication based on real uid/gid. the key permission
730 * bits will be exactly as POS_ALL, so only processes who subscribed
731 * this key could have the access, although the quota might be counted
732 * on others (fsuid/fsgid).
734 * keyring will use fsuid/fsgid as upcall parameters, so we have to
735 * encode real uid/gid into callout info.
738 construct_key_desc(desc, sizeof(desc), sec, vcred->vc_uid);
740 /* callout info format:
741 * secid:mech:uid:gid:flags:svc_type:peer_nid:target_uuid
743 coinfo_size = sizeof(struct obd_uuid) + MAX_OBD_NAME + 64;
744 OBD_ALLOC(coinfo, coinfo_size);
748 snprintf(coinfo, coinfo_size, "%d:%s:%u:%u:%s:%d:"LPX64":%s",
749 sec->ps_id, sec2gsec(sec)->gs_mech->gm_name,
750 vcred->vc_uid, vcred->vc_gid,
751 co_flags, import_to_gss_svc(imp),
752 imp->imp_connection->c_peer.nid, imp->imp_obd->obd_name);
754 CDEBUG(D_SEC, "requesting key for %s\n", desc);
756 keyring_upcall_lock(gsec_kr);
757 key = request_key(&gss_key_type, desc, coinfo);
758 keyring_upcall_unlock(gsec_kr);
760 OBD_FREE(coinfo, coinfo_size);
763 CERROR("failed request key: %ld\n", PTR_ERR(key));
766 CDEBUG(D_SEC, "obtained key %08x for %s\n", key->serial, desc);
768 /* once payload.data was pointed to a ctx, it never changes until
769 * we de-associate them; but parallel request_key() may return
770 * a key with payload.data == NULL at the same time. so we still
771 * need wirtelock of key->sem to serialize them. */
772 down_write(&key->sem);
774 if (likely(key->payload.data != NULL)) {
775 ctx = key->payload.data;
777 LASSERT(cfs_atomic_read(&ctx->cc_refcount) >= 1);
778 LASSERT(ctx2gctx_keyring(ctx)->gck_key == key);
779 LASSERT(atomic_read(&key->usage) >= 2);
781 /* simply take a ref and return. it's upper layer's
782 * responsibility to detect & replace dead ctx. */
783 cfs_atomic_inc(&ctx->cc_refcount);
785 /* pre initialization with a cli_ctx. this can't be done in
786 * key_instantiate() because we'v no enough information
788 ctx = ctx_create_kr(sec, vcred);
790 ctx_enlist_kr(ctx, is_root, 0);
791 bind_key_ctx(key, ctx);
793 ctx_start_timer_kr(ctx, KEYRING_UPCALL_TIMEOUT);
795 CDEBUG(D_SEC, "installed key %p <-> ctx %p (sec %p)\n",
798 /* we'd prefer to call key_revoke(), but we more like
799 * to revoke it within this key->sem locked period. */
800 key_revoke_locked(key);
808 if (is_root && create_new)
809 request_key_unlink(key);
814 cfs_mutex_unlock(&gsec_kr->gsk_root_uc_lock);
819 void gss_sec_release_ctx_kr(struct ptlrpc_sec *sec,
820 struct ptlrpc_cli_ctx *ctx,
823 LASSERT(cfs_atomic_read(&sec->ps_refcount) > 0);
824 LASSERT(cfs_atomic_read(&ctx->cc_refcount) == 0);
825 ctx_release_kr(ctx, sync);
829 * flush context of normal user, we must resort to keyring itself to find out
830 * contexts which belong to me.
832 * Note here we suppose only to flush _my_ context, the "uid" will
833 * be ignored in the search.
836 void flush_user_ctx_cache_kr(struct ptlrpc_sec *sec,
838 int grace, int force)
843 /* nothing to do for reverse or rootonly sec */
844 if (sec_is_reverse(sec) || sec_is_rootonly(sec))
847 construct_key_desc(desc, sizeof(desc), sec, uid);
849 /* there should be only one valid key, but we put it in the
850 * loop in case of any weird cases */
852 key = request_key(&gss_key_type, desc, NULL);
854 CDEBUG(D_SEC, "No more key found for current user\n");
858 down_write(&key->sem);
860 kill_key_locked(key);
862 /* kill_key_locked() should usually revoke the key, but we
863 * revoke it again to make sure, e.g. some case the key may
864 * not well coupled with a context. */
865 key_revoke_locked(key);
874 * flush context of root or all, we iterate through the list.
877 void flush_spec_ctx_cache_kr(struct ptlrpc_sec *sec,
879 int grace, int force)
881 struct gss_sec_keyring *gsec_kr;
882 cfs_hlist_head_t freelist = CFS_HLIST_HEAD_INIT;
883 cfs_hlist_node_t *pos, *next;
884 struct ptlrpc_cli_ctx *ctx;
887 gsec_kr = sec2gsec_keyring(sec);
889 cfs_spin_lock(&sec->ps_lock);
890 cfs_hlist_for_each_entry_safe(ctx, pos, next,
891 &gsec_kr->gsk_clist, cc_cache) {
892 LASSERT(cfs_atomic_read(&ctx->cc_refcount) > 0);
894 if (uid != -1 && uid != ctx->cc_vcred.vc_uid)
897 /* at this moment there's at least 2 base reference:
898 * key association and in-list. */
899 if (cfs_atomic_read(&ctx->cc_refcount) > 2) {
902 CWARN("flush busy ctx %p(%u->%s, extra ref %d)\n",
903 ctx, ctx->cc_vcred.vc_uid,
904 sec2target_str(ctx->cc_sec),
905 cfs_atomic_read(&ctx->cc_refcount) - 2);
908 cfs_set_bit(PTLRPC_CTX_DEAD_BIT, &ctx->cc_flags);
910 cfs_clear_bit(PTLRPC_CTX_UPTODATE_BIT, &ctx->cc_flags);
912 cfs_atomic_inc(&ctx->cc_refcount);
914 if (ctx_unlist_kr(ctx, 1)) {
915 cfs_hlist_add_head(&ctx->cc_cache, &freelist);
917 LASSERT(cfs_atomic_read(&ctx->cc_refcount) >= 2);
918 cfs_atomic_dec(&ctx->cc_refcount);
921 cfs_spin_unlock(&sec->ps_lock);
923 dispose_ctx_list_kr(&freelist);
928 int gss_sec_flush_ctx_cache_kr(struct ptlrpc_sec *sec,
930 int grace, int force)
934 CDEBUG(D_SEC, "sec %p(%d, nctx %d), uid %d, grace %d, force %d\n",
935 sec, cfs_atomic_read(&sec->ps_refcount),
936 cfs_atomic_read(&sec->ps_nctx),
939 if (uid != -1 && uid != 0)
940 flush_user_ctx_cache_kr(sec, uid, grace, force);
942 flush_spec_ctx_cache_kr(sec, uid, grace, force);
948 void gss_sec_gc_ctx_kr(struct ptlrpc_sec *sec)
950 struct gss_sec_keyring *gsec_kr = sec2gsec_keyring(sec);
951 cfs_hlist_head_t freelist = CFS_HLIST_HEAD_INIT;
952 cfs_hlist_node_t *pos, *next;
953 struct ptlrpc_cli_ctx *ctx;
956 CWARN("running gc\n");
958 cfs_spin_lock(&sec->ps_lock);
959 cfs_hlist_for_each_entry_safe(ctx, pos, next,
960 &gsec_kr->gsk_clist, cc_cache) {
961 LASSERT(cfs_atomic_read(&ctx->cc_refcount) > 0);
963 cfs_atomic_inc(&ctx->cc_refcount);
965 if (cli_ctx_check_death(ctx) && ctx_unlist_kr(ctx, 1)) {
966 cfs_hlist_add_head(&ctx->cc_cache, &freelist);
967 CWARN("unhashed ctx %p\n", ctx);
969 LASSERT(cfs_atomic_read(&ctx->cc_refcount) >= 2);
970 cfs_atomic_dec(&ctx->cc_refcount);
973 cfs_spin_unlock(&sec->ps_lock);
975 dispose_ctx_list_kr(&freelist);
981 int gss_sec_display_kr(struct ptlrpc_sec *sec, struct seq_file *seq)
983 struct gss_sec_keyring *gsec_kr = sec2gsec_keyring(sec);
984 cfs_hlist_node_t *pos, *next;
985 struct ptlrpc_cli_ctx *ctx;
986 struct gss_cli_ctx *gctx;
987 time_t now = cfs_time_current_sec();
990 cfs_spin_lock(&sec->ps_lock);
991 cfs_hlist_for_each_entry_safe(ctx, pos, next,
992 &gsec_kr->gsk_clist, cc_cache) {
997 gctx = ctx2gctx(ctx);
998 key = ctx2gctx_keyring(ctx)->gck_key;
1000 gss_cli_ctx_flags2str(ctx->cc_flags,
1001 flags_str, sizeof(flags_str));
1003 if (gctx->gc_mechctx)
1004 lgss_display(gctx->gc_mechctx, mech, sizeof(mech));
1006 snprintf(mech, sizeof(mech), "N/A");
1007 mech[sizeof(mech) - 1] = '\0';
1009 seq_printf(seq, "%p: uid %u, ref %d, expire %ld(%+ld), fl %s, "
1010 "seq %d, win %u, key %08x(ref %d), "
1011 "hdl "LPX64":"LPX64", mech: %s\n",
1012 ctx, ctx->cc_vcred.vc_uid,
1013 cfs_atomic_read(&ctx->cc_refcount),
1015 ctx->cc_expire ? ctx->cc_expire - now : 0,
1017 cfs_atomic_read(&gctx->gc_seq),
1019 key ? key->serial : 0,
1020 key ? atomic_read(&key->usage) : 0,
1021 gss_handle_to_u64(&gctx->gc_handle),
1022 gss_handle_to_u64(&gctx->gc_svc_handle),
1025 cfs_spin_unlock(&sec->ps_lock);
1030 /****************************************
1032 ****************************************/
1035 int gss_cli_ctx_refresh_kr(struct ptlrpc_cli_ctx *ctx)
1037 /* upcall is already on the way */
1042 int gss_cli_ctx_validate_kr(struct ptlrpc_cli_ctx *ctx)
1044 LASSERT(cfs_atomic_read(&ctx->cc_refcount) > 0);
1045 LASSERT(ctx->cc_sec);
1047 if (cli_ctx_check_death(ctx)) {
1052 if (cli_ctx_is_ready(ctx))
1058 void gss_cli_ctx_die_kr(struct ptlrpc_cli_ctx *ctx, int grace)
1060 LASSERT(cfs_atomic_read(&ctx->cc_refcount) > 0);
1061 LASSERT(ctx->cc_sec);
1063 cli_ctx_expire(ctx);
1067 /****************************************
1068 * (reverse) service *
1069 ****************************************/
1072 * reverse context could have nothing to do with keyrings. here we still keep
1073 * the version which bind to a key, for future reference.
1075 #define HAVE_REVERSE_CTX_NOKEY
1077 #ifdef HAVE_REVERSE_CTX_NOKEY
1080 int sec_install_rctx_kr(struct ptlrpc_sec *sec,
1081 struct ptlrpc_svc_ctx *svc_ctx)
1083 struct ptlrpc_cli_ctx *cli_ctx;
1084 struct vfs_cred vcred = { 0, 0 };
1090 cli_ctx = ctx_create_kr(sec, &vcred);
1091 if (cli_ctx == NULL)
1094 rc = gss_copy_rvc_cli_ctx(cli_ctx, svc_ctx);
1096 CERROR("failed copy reverse cli ctx: %d\n", rc);
1098 ctx_put_kr(cli_ctx, 1);
1102 rvs_sec_install_root_ctx_kr(sec, cli_ctx, NULL);
1104 ctx_put_kr(cli_ctx, 1);
1109 #else /* ! HAVE_REVERSE_CTX_NOKEY */
1112 int sec_install_rctx_kr(struct ptlrpc_sec *sec,
1113 struct ptlrpc_svc_ctx *svc_ctx)
1115 struct ptlrpc_cli_ctx *cli_ctx = NULL;
1117 struct vfs_cred vcred = { 0, 0 };
1125 construct_key_desc(desc, sizeof(desc), sec, 0);
1127 key = key_alloc(&gss_key_type, desc, 0, 0,
1128 KEY_POS_ALL | KEY_USR_ALL, 1);
1130 CERROR("failed to alloc key: %ld\n", PTR_ERR(key));
1131 return PTR_ERR(key);
1134 rc = key_instantiate_and_link(key, NULL, 0, NULL, NULL);
1136 CERROR("failed to instantiate key: %d\n", rc);
1140 down_write(&key->sem);
1142 LASSERT(key->payload.data == NULL);
1144 cli_ctx = ctx_create_kr(sec, &vcred);
1145 if (cli_ctx == NULL) {
1150 rc = gss_copy_rvc_cli_ctx(cli_ctx, svc_ctx);
1152 CERROR("failed copy reverse cli ctx: %d\n", rc);
1156 rvs_sec_install_root_ctx_kr(sec, cli_ctx, key);
1158 ctx_put_kr(cli_ctx, 1);
1159 up_write(&key->sem);
1168 ctx_put_kr(cli_ctx, 1);
1170 up_write(&key->sem);
1176 #endif /* HAVE_REVERSE_CTX_NOKEY */
1178 /****************************************
1180 ****************************************/
1183 int gss_svc_accept_kr(struct ptlrpc_request *req)
1185 return gss_svc_accept(&gss_policy_keyring, req);
1189 int gss_svc_install_rctx_kr(struct obd_import *imp,
1190 struct ptlrpc_svc_ctx *svc_ctx)
1192 struct ptlrpc_sec *sec;
1195 sec = sptlrpc_import_sec_ref(imp);
1198 rc = sec_install_rctx_kr(sec, svc_ctx);
1199 sptlrpc_sec_put(sec);
1204 /****************************************
1206 ****************************************/
1209 int gss_kt_instantiate(struct key *key, const void *data, size_t datalen)
1214 if (data != NULL || datalen != 0) {
1215 CERROR("invalid: data %p, len %lu\n", data, (long)datalen);
1219 if (key->payload.data != 0) {
1220 CERROR("key already have payload\n");
1224 /* link the key to session keyring, so following context negotiation
1225 * rpc fired from user space could find this key. This will be unlinked
1226 * automatically when upcall processes die.
1228 * we can't do this through keyctl from userspace, because the upcall
1229 * might be neither possessor nor owner of the key (setuid).
1231 * the session keyring is created upon upcall, and don't change all
1232 * the way until upcall finished, so rcu lock is not needed here.
1234 LASSERT(cfs_current()->signal->session_keyring);
1237 rc = key_link(cfs_current()->signal->session_keyring, key);
1240 CERROR("failed to link key %08x to keyring %08x: %d\n",
1242 cfs_current()->signal->session_keyring->serial, rc);
1246 CDEBUG(D_SEC, "key %p instantiated, ctx %p\n", key, key->payload.data);
1251 * called with key semaphore write locked. it means we can operate
1252 * on the context without fear of loosing refcount.
1255 int gss_kt_update(struct key *key, const void *data, size_t datalen)
1257 struct ptlrpc_cli_ctx *ctx = key->payload.data;
1258 struct gss_cli_ctx *gctx;
1259 rawobj_t tmpobj = RAWOBJ_EMPTY;
1260 __u32 datalen32 = (__u32) datalen;
1264 if (data == NULL || datalen == 0) {
1265 CWARN("invalid: data %p, len %lu\n", data, (long)datalen);
1269 /* if upcall finished negotiation too fast (mostly likely because
1270 * of local error happened) and call kt_update(), the ctx
1271 * might be still NULL. but the key will finally be associate
1272 * with a context, or be revoked. if key status is fine, return
1273 * -EAGAIN to allow userspace sleep a while and call again. */
1275 CDEBUG(D_SEC, "update too soon: key %p(%x) flags %lx\n",
1276 key, key->serial, key->flags);
1278 rc = key_validate(key);
1285 LASSERT(cfs_atomic_read(&ctx->cc_refcount) > 0);
1286 LASSERT(ctx->cc_sec);
1288 ctx_clear_timer_kr(ctx);
1290 /* don't proceed if already refreshed */
1291 if (cli_ctx_is_refreshed(ctx)) {
1292 CWARN("ctx already done refresh\n");
1296 sptlrpc_cli_ctx_get(ctx);
1297 gctx = ctx2gctx(ctx);
1299 rc = buffer_extract_bytes(&data, &datalen32, &gctx->gc_win,
1300 sizeof(gctx->gc_win));
1302 CERROR("failed extract seq_win\n");
1306 if (gctx->gc_win == 0) {
1307 __u32 nego_rpc_err, nego_gss_err;
1309 rc = buffer_extract_bytes(&data, &datalen32, &nego_rpc_err,
1310 sizeof(nego_rpc_err));
1312 CERROR("failed to extrace rpc rc\n");
1316 rc = buffer_extract_bytes(&data, &datalen32, &nego_gss_err,
1317 sizeof(nego_gss_err));
1319 CERROR("failed to extrace gss rc\n");
1323 CERROR("negotiation: rpc err %d, gss err %x\n",
1324 nego_rpc_err, nego_gss_err);
1326 rc = nego_rpc_err ? nego_rpc_err : -EACCES;
1328 rc = rawobj_extract_local_alloc(&gctx->gc_handle,
1329 (__u32 **) &data, &datalen32);
1331 CERROR("failed extract handle\n");
1335 rc = rawobj_extract_local(&tmpobj, (__u32 **) &data,&datalen32);
1337 CERROR("failed extract mech\n");
1341 rc = lgss_import_sec_context(&tmpobj,
1342 sec2gsec(ctx->cc_sec)->gs_mech,
1344 if (rc != GSS_S_COMPLETE)
1345 CERROR("failed import context\n");
1350 /* we don't care what current status of this ctx, even someone else
1351 * is operating on the ctx at the same time. we just add up our own
1354 gss_cli_ctx_uptodate(gctx);
1356 /* this will also revoke the key. has to be done before
1357 * wakeup waiters otherwise they can find the stale key */
1358 kill_key_locked(key);
1360 cli_ctx_expire(ctx);
1362 if (rc != -ERESTART)
1363 cfs_set_bit(PTLRPC_CTX_ERROR_BIT, &ctx->cc_flags);
1366 /* let user space think it's a success */
1367 sptlrpc_cli_ctx_put(ctx, 1);
1372 int gss_kt_match(const struct key *key, const void *desc)
1374 return (strcmp(key->description, (const char *) desc) == 0);
1378 void gss_kt_destroy(struct key *key)
1381 LASSERT(key->payload.data == NULL);
1382 CDEBUG(D_SEC, "destroy key %p\n", key);
1387 void gss_kt_describe(const struct key *key, struct seq_file *s)
1389 if (key->description == NULL)
1390 seq_puts(s, "[null]");
1392 seq_puts(s, key->description);
1395 static struct key_type gss_key_type =
1399 .instantiate = gss_kt_instantiate,
1400 .update = gss_kt_update,
1401 .match = gss_kt_match,
1402 .destroy = gss_kt_destroy,
1403 .describe = gss_kt_describe,
1406 /****************************************
1407 * lustre gss keyring policy *
1408 ****************************************/
1410 static struct ptlrpc_ctx_ops gss_keyring_ctxops = {
1411 .match = gss_cli_ctx_match,
1412 .refresh = gss_cli_ctx_refresh_kr,
1413 .validate = gss_cli_ctx_validate_kr,
1414 .die = gss_cli_ctx_die_kr,
1415 .sign = gss_cli_ctx_sign,
1416 .verify = gss_cli_ctx_verify,
1417 .seal = gss_cli_ctx_seal,
1418 .unseal = gss_cli_ctx_unseal,
1419 .wrap_bulk = gss_cli_ctx_wrap_bulk,
1420 .unwrap_bulk = gss_cli_ctx_unwrap_bulk,
1423 static struct ptlrpc_sec_cops gss_sec_keyring_cops = {
1424 .create_sec = gss_sec_create_kr,
1425 .destroy_sec = gss_sec_destroy_kr,
1426 .kill_sec = gss_sec_kill,
1427 .lookup_ctx = gss_sec_lookup_ctx_kr,
1428 .release_ctx = gss_sec_release_ctx_kr,
1429 .flush_ctx_cache = gss_sec_flush_ctx_cache_kr,
1430 .gc_ctx = gss_sec_gc_ctx_kr,
1431 .install_rctx = gss_sec_install_rctx,
1432 .alloc_reqbuf = gss_alloc_reqbuf,
1433 .free_reqbuf = gss_free_reqbuf,
1434 .alloc_repbuf = gss_alloc_repbuf,
1435 .free_repbuf = gss_free_repbuf,
1436 .enlarge_reqbuf = gss_enlarge_reqbuf,
1437 .display = gss_sec_display_kr,
1440 static struct ptlrpc_sec_sops gss_sec_keyring_sops = {
1441 .accept = gss_svc_accept_kr,
1442 .invalidate_ctx = gss_svc_invalidate_ctx,
1443 .alloc_rs = gss_svc_alloc_rs,
1444 .authorize = gss_svc_authorize,
1445 .free_rs = gss_svc_free_rs,
1446 .free_ctx = gss_svc_free_ctx,
1447 .prep_bulk = gss_svc_prep_bulk,
1448 .unwrap_bulk = gss_svc_unwrap_bulk,
1449 .wrap_bulk = gss_svc_wrap_bulk,
1450 .install_rctx = gss_svc_install_rctx_kr,
1453 static struct ptlrpc_sec_policy gss_policy_keyring = {
1454 .sp_owner = THIS_MODULE,
1455 .sp_name = "gss.keyring",
1456 .sp_policy = SPTLRPC_POLICY_GSS,
1457 .sp_cops = &gss_sec_keyring_cops,
1458 .sp_sops = &gss_sec_keyring_sops,
1462 int __init gss_init_keyring(void)
1466 rc = register_key_type(&gss_key_type);
1468 CERROR("failed to register keyring type: %d\n", rc);
1472 rc = sptlrpc_register_policy(&gss_policy_keyring);
1474 unregister_key_type(&gss_key_type);
1481 void __exit gss_exit_keyring(void)
1483 unregister_key_type(&gss_key_type);
1484 sptlrpc_unregister_policy(&gss_policy_keyring);