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 #define task_aux(tsk) (tsk)
67 * the timeout is only for the case that upcall child process die abnormally.
68 * in any other cases it should finally update kernel key. so we set this
69 * timeout value excessive long.
71 #define KEYRING_UPCALL_TIMEOUT (obd_timeout + obd_timeout)
73 /****************************************
75 ****************************************/
77 #define DUMP_PROCESS_KEYRINGS(tsk) \
79 CWARN("DUMP PK: %s[%u,%u/%u](<-%s[%u,%u/%u]): " \
80 "a %d, t %d, p %d, s %d, u %d, us %d, df %d\n", \
81 tsk->comm, tsk->pid, tsk->uid, tsk->fsuid, \
82 tsk->parent->comm, tsk->parent->pid, \
83 tsk->parent->uid, tsk->parent->fsuid, \
84 task_aux(tsk)->request_key_auth ? \
85 task_aux(tsk)->request_key_auth->serial : 0, \
86 task_aux(tsk)->thread_keyring ? \
87 task_aux(tsk)->thread_keyring->serial : 0, \
88 tsk->signal->process_keyring ? \
89 tsk->signal->process_keyring->serial : 0, \
90 tsk->signal->session_keyring ? \
91 tsk->signal->session_keyring->serial : 0, \
92 tsk->user->uid_keyring ? \
93 tsk->user->uid_keyring->serial : 0, \
94 tsk->user->session_keyring ? \
95 tsk->user->session_keyring->serial : 0, \
96 task_aux(tsk)->jit_keyring \
100 #define DUMP_KEY(key) \
102 CWARN("DUMP KEY: %p(%d) ref %d u%u/g%u desc %s\n", \
103 key, key->serial, atomic_read(&key->usage), \
104 key->uid, key->gid, \
105 key->description ? key->description : "n/a" \
110 static inline void keyring_upcall_lock(struct gss_sec_keyring *gsec_kr)
112 #ifdef HAVE_KEYRING_UPCALL_SERIALIZED
113 mutex_lock(&gsec_kr->gsk_uc_lock);
117 static inline void keyring_upcall_unlock(struct gss_sec_keyring *gsec_kr)
119 #ifdef HAVE_KEYRING_UPCALL_SERIALIZED
120 mutex_unlock(&gsec_kr->gsk_uc_lock);
124 static inline void key_revoke_locked(struct key *key)
126 set_bit(KEY_FLAG_REVOKED, &key->flags);
129 static void ctx_upcall_timeout_kr(unsigned long data)
131 struct ptlrpc_cli_ctx *ctx = (struct ptlrpc_cli_ctx *) data;
132 struct key *key = ctx2gctx_keyring(ctx)->gck_key;
134 CWARN("ctx %p, key %p\n", ctx, key);
139 key_revoke_locked(key);
140 sptlrpc_cli_ctx_wakeup(ctx);
144 void ctx_start_timer_kr(struct ptlrpc_cli_ctx *ctx, long timeout)
146 struct gss_cli_ctx_keyring *gctx_kr = ctx2gctx_keyring(ctx);
147 struct timer_list *timer = gctx_kr->gck_timer;
151 CDEBUG(D_SEC, "ctx %p: start timer %lds\n", ctx, timeout);
152 timeout = timeout * HZ + cfs_time_current();
155 timer->expires = timeout;
156 timer->data = (unsigned long ) ctx;
157 timer->function = ctx_upcall_timeout_kr;
163 * caller should make sure no race with other threads
166 void ctx_clear_timer_kr(struct ptlrpc_cli_ctx *ctx)
168 struct gss_cli_ctx_keyring *gctx_kr = ctx2gctx_keyring(ctx);
169 struct timer_list *timer = gctx_kr->gck_timer;
174 CDEBUG(D_SEC, "ctx %p, key %p\n", ctx, gctx_kr->gck_key);
176 gctx_kr->gck_timer = NULL;
178 del_singleshot_timer_sync(timer);
184 struct ptlrpc_cli_ctx *ctx_create_kr(struct ptlrpc_sec *sec,
185 struct vfs_cred *vcred)
187 struct ptlrpc_cli_ctx *ctx;
188 struct gss_cli_ctx_keyring *gctx_kr;
190 OBD_ALLOC_PTR(gctx_kr);
194 OBD_ALLOC_PTR(gctx_kr->gck_timer);
195 if (gctx_kr->gck_timer == NULL) {
196 OBD_FREE_PTR(gctx_kr);
199 init_timer(gctx_kr->gck_timer);
201 ctx = &gctx_kr->gck_base.gc_base;
203 if (gss_cli_ctx_init_common(sec, ctx, &gss_keyring_ctxops, vcred)) {
204 OBD_FREE_PTR(gctx_kr->gck_timer);
205 OBD_FREE_PTR(gctx_kr);
209 ctx->cc_expire = cfs_time_current_sec() + KEYRING_UPCALL_TIMEOUT;
210 clear_bit(PTLRPC_CTX_NEW_BIT, &ctx->cc_flags);
211 atomic_inc(&ctx->cc_refcount); /* for the caller */
216 static void ctx_destroy_kr(struct ptlrpc_cli_ctx *ctx)
218 struct ptlrpc_sec *sec = ctx->cc_sec;
219 struct gss_cli_ctx_keyring *gctx_kr = ctx2gctx_keyring(ctx);
221 CDEBUG(D_SEC, "destroying ctx %p\n", ctx);
223 /* at this time the association with key has been broken. */
225 LASSERT(atomic_read(&sec->ps_refcount) > 0);
226 LASSERT(atomic_read(&sec->ps_nctx) > 0);
227 LASSERT(test_bit(PTLRPC_CTX_CACHED_BIT, &ctx->cc_flags) == 0);
228 LASSERT(gctx_kr->gck_key == NULL);
230 ctx_clear_timer_kr(ctx);
231 LASSERT(gctx_kr->gck_timer == NULL);
233 if (gss_cli_ctx_fini_common(sec, ctx))
236 OBD_FREE_PTR(gctx_kr);
238 atomic_dec(&sec->ps_nctx);
239 sptlrpc_sec_put(sec);
242 static void ctx_release_kr(struct ptlrpc_cli_ctx *ctx, int sync)
247 atomic_inc(&ctx->cc_refcount);
248 sptlrpc_gc_add_ctx(ctx);
252 static void ctx_put_kr(struct ptlrpc_cli_ctx *ctx, int sync)
254 LASSERT(atomic_read(&ctx->cc_refcount) > 0);
256 if (atomic_dec_and_test(&ctx->cc_refcount))
257 ctx_release_kr(ctx, sync);
261 * key <-> ctx association and rules:
262 * - ctx might not bind with any key
263 * - key/ctx binding is protected by key semaphore (if the key present)
264 * - key and ctx each take a reference of the other
265 * - ctx enlist/unlist is protected by ctx spinlock
266 * - never enlist a ctx after it's been unlisted
267 * - whoever do enlist should also do bind, lock key before enlist:
268 * - lock key -> lock ctx -> enlist -> unlock ctx -> bind -> unlock key
269 * - whoever do unlist should also do unbind:
270 * - lock key -> lock ctx -> unlist -> unlock ctx -> unbind -> unlock key
271 * - lock ctx -> unlist -> unlock ctx -> lock key -> unbind -> unlock key
274 static inline void spin_lock_if(spinlock_t *lock, int condition)
280 static inline void spin_unlock_if(spinlock_t *lock, int condition)
286 static void ctx_enlist_kr(struct ptlrpc_cli_ctx *ctx, int is_root, int locked)
288 struct ptlrpc_sec *sec = ctx->cc_sec;
289 struct gss_sec_keyring *gsec_kr = sec2gsec_keyring(sec);
291 LASSERT(!test_bit(PTLRPC_CTX_CACHED_BIT, &ctx->cc_flags));
292 LASSERT(atomic_read(&ctx->cc_refcount) > 0);
294 spin_lock_if(&sec->ps_lock, !locked);
296 atomic_inc(&ctx->cc_refcount);
297 set_bit(PTLRPC_CTX_CACHED_BIT, &ctx->cc_flags);
298 hlist_add_head(&ctx->cc_cache, &gsec_kr->gsk_clist);
300 gsec_kr->gsk_root_ctx = ctx;
302 spin_unlock_if(&sec->ps_lock, !locked);
306 * Note after this get called, caller should not access ctx again because
307 * it might have been freed, unless caller hold at least one refcount of
310 * return non-zero if we indeed unlist this ctx.
312 static int ctx_unlist_kr(struct ptlrpc_cli_ctx *ctx, int locked)
314 struct ptlrpc_sec *sec = ctx->cc_sec;
315 struct gss_sec_keyring *gsec_kr = sec2gsec_keyring(sec);
317 /* if hashed bit has gone, leave the job to somebody who is doing it */
318 if (test_and_clear_bit(PTLRPC_CTX_CACHED_BIT, &ctx->cc_flags) == 0)
321 /* drop ref inside spin lock to prevent race with other operations */
322 spin_lock_if(&sec->ps_lock, !locked);
324 if (gsec_kr->gsk_root_ctx == ctx)
325 gsec_kr->gsk_root_ctx = NULL;
326 hlist_del_init(&ctx->cc_cache);
327 atomic_dec(&ctx->cc_refcount);
329 spin_unlock_if(&sec->ps_lock, !locked);
335 * bind a key with a ctx together.
336 * caller must hold write lock of the key, as well as ref on key & ctx.
338 static void bind_key_ctx(struct key *key, struct ptlrpc_cli_ctx *ctx)
340 LASSERT(atomic_read(&ctx->cc_refcount) > 0);
341 LASSERT(atomic_read(&key->usage) > 0);
342 LASSERT(ctx2gctx_keyring(ctx)->gck_key == NULL);
343 LASSERT(key->payload.data == NULL);
345 /* at this time context may or may not in list. */
347 atomic_inc(&ctx->cc_refcount);
348 ctx2gctx_keyring(ctx)->gck_key = key;
349 key->payload.data = ctx;
353 * unbind a key and a ctx.
354 * caller must hold write lock, as well as a ref of the key.
356 static void unbind_key_ctx(struct key *key, struct ptlrpc_cli_ctx *ctx)
358 LASSERT(key->payload.data == ctx);
359 LASSERT(test_bit(PTLRPC_CTX_CACHED_BIT, &ctx->cc_flags) == 0);
361 /* must revoke the key, or others may treat it as newly created */
362 key_revoke_locked(key);
364 key->payload.data = NULL;
365 ctx2gctx_keyring(ctx)->gck_key = NULL;
367 /* once ctx get split from key, the timer is meaningless */
368 ctx_clear_timer_kr(ctx);
375 * given a ctx, unbind with its coupled key, if any.
376 * unbind could only be called once, so we don't worry the key be released
379 static void unbind_ctx_kr(struct ptlrpc_cli_ctx *ctx)
381 struct key *key = ctx2gctx_keyring(ctx)->gck_key;
384 LASSERT(key->payload.data == ctx);
387 down_write(&key->sem);
388 unbind_key_ctx(key, ctx);
395 * given a key, unbind with its coupled ctx, if any.
396 * caller must hold write lock, as well as a ref of the key.
398 static void unbind_key_locked(struct key *key)
400 struct ptlrpc_cli_ctx *ctx = key->payload.data;
403 unbind_key_ctx(key, ctx);
407 * unlist a ctx, and unbind from coupled key
409 static void kill_ctx_kr(struct ptlrpc_cli_ctx *ctx)
411 if (ctx_unlist_kr(ctx, 0))
416 * given a key, unlist and unbind with the coupled ctx (if any).
417 * caller must hold write lock, as well as a ref of the key.
419 static void kill_key_locked(struct key *key)
421 struct ptlrpc_cli_ctx *ctx = key->payload.data;
423 if (ctx && ctx_unlist_kr(ctx, 0))
424 unbind_key_locked(key);
428 * caller should hold one ref on contexts in freelist.
430 static void dispose_ctx_list_kr(struct hlist_head *freelist)
432 struct hlist_node *pos, *next;
433 struct ptlrpc_cli_ctx *ctx;
434 struct gss_cli_ctx *gctx;
436 hlist_for_each_entry_safe(ctx, pos, next, freelist, cc_cache) {
437 hlist_del_init(&ctx->cc_cache);
439 /* reverse ctx: update current seq to buddy svcctx if exist.
440 * ideally this should be done at gss_cli_ctx_finalize(), but
441 * the ctx destroy could be delayed by:
442 * 1) ctx still has reference;
443 * 2) ctx destroy is asynchronous;
444 * and reverse import call inval_all_ctx() require this be done
445 *_immediately_ otherwise newly created reverse ctx might copy
446 * the very old sequence number from svcctx. */
447 gctx = ctx2gctx(ctx);
448 if (!rawobj_empty(&gctx->gc_svc_handle) &&
449 sec_is_reverse(gctx->gc_base.cc_sec)) {
450 gss_svc_upcall_update_sequence(&gctx->gc_svc_handle,
451 (__u32) atomic_read(&gctx->gc_seq));
454 /* we need to wakeup waiting reqs here. the context might
455 * be forced released before upcall finished, then the
456 * late-arrived downcall can't find the ctx even. */
457 sptlrpc_cli_ctx_wakeup(ctx);
465 * lookup a root context directly in a sec, return root ctx with a
466 * reference taken or NULL.
469 struct ptlrpc_cli_ctx * sec_lookup_root_ctx_kr(struct ptlrpc_sec *sec)
471 struct gss_sec_keyring *gsec_kr = sec2gsec_keyring(sec);
472 struct ptlrpc_cli_ctx *ctx = NULL;
474 spin_lock(&sec->ps_lock);
476 ctx = gsec_kr->gsk_root_ctx;
478 if (ctx == NULL && unlikely(sec_is_reverse(sec))) {
479 struct hlist_node *node;
480 struct ptlrpc_cli_ctx *tmp;
482 /* reverse ctx, search root ctx in list, choose the one
483 * with shortest expire time, which is most possibly have
484 * an established peer ctx at client side. */
485 hlist_for_each_entry(tmp, node, &gsec_kr->gsk_clist, cc_cache) {
486 if (ctx == NULL || ctx->cc_expire == 0 ||
487 ctx->cc_expire > tmp->cc_expire) {
489 /* promote to be root_ctx */
490 gsec_kr->gsk_root_ctx = ctx;
496 LASSERT(atomic_read(&ctx->cc_refcount) > 0);
497 LASSERT(!hlist_empty(&gsec_kr->gsk_clist));
498 atomic_inc(&ctx->cc_refcount);
501 spin_unlock(&sec->ps_lock);
506 #define RVS_CTX_EXPIRE_NICE (10)
509 void rvs_sec_install_root_ctx_kr(struct ptlrpc_sec *sec,
510 struct ptlrpc_cli_ctx *new_ctx,
513 struct gss_sec_keyring *gsec_kr = sec2gsec_keyring(sec);
514 struct hlist_node *hnode;
515 struct ptlrpc_cli_ctx *ctx;
519 LASSERT(sec_is_reverse(sec));
521 spin_lock(&sec->ps_lock);
523 now = cfs_time_current_sec();
525 /* set all existing ctxs short expiry */
526 hlist_for_each_entry(ctx, hnode, &gsec_kr->gsk_clist, cc_cache) {
527 if (ctx->cc_expire > now + RVS_CTX_EXPIRE_NICE) {
528 ctx->cc_early_expire = 1;
529 ctx->cc_expire = now + RVS_CTX_EXPIRE_NICE;
533 /* if there's root_ctx there, instead obsolete the current
534 * immediately, we leave it continue operating for a little while.
535 * hopefully when the first backward rpc with newest ctx send out,
536 * the client side already have the peer ctx well established. */
537 ctx_enlist_kr(new_ctx, gsec_kr->gsk_root_ctx ? 0 : 1, 1);
540 bind_key_ctx(key, new_ctx);
542 spin_unlock(&sec->ps_lock);
545 static void construct_key_desc(void *buf, int bufsize,
546 struct ptlrpc_sec *sec, uid_t uid)
548 snprintf(buf, bufsize, "%d@%x", uid, sec->ps_id);
549 ((char *)buf)[bufsize - 1] = '\0';
552 /****************************************
554 ****************************************/
557 struct ptlrpc_sec * gss_sec_create_kr(struct obd_import *imp,
558 struct ptlrpc_svc_ctx *svcctx,
559 struct sptlrpc_flavor *sf)
561 struct gss_sec_keyring *gsec_kr;
564 OBD_ALLOC(gsec_kr, sizeof(*gsec_kr));
568 CFS_INIT_HLIST_HEAD(&gsec_kr->gsk_clist);
569 gsec_kr->gsk_root_ctx = NULL;
570 mutex_init(&gsec_kr->gsk_root_uc_lock);
571 #ifdef HAVE_KEYRING_UPCALL_SERIALIZED
572 mutex_init(&gsec_kr->gsk_uc_lock);
575 if (gss_sec_create_common(&gsec_kr->gsk_base, &gss_policy_keyring,
579 if (svcctx != NULL &&
580 sec_install_rctx_kr(&gsec_kr->gsk_base.gs_base, svcctx)) {
581 gss_sec_destroy_common(&gsec_kr->gsk_base);
585 RETURN(&gsec_kr->gsk_base.gs_base);
588 OBD_FREE(gsec_kr, sizeof(*gsec_kr));
593 void gss_sec_destroy_kr(struct ptlrpc_sec *sec)
595 struct gss_sec *gsec = sec2gsec(sec);
596 struct gss_sec_keyring *gsec_kr = sec2gsec_keyring(sec);
598 CDEBUG(D_SEC, "destroy %s@%p\n", sec->ps_policy->sp_name, sec);
600 LASSERT(hlist_empty(&gsec_kr->gsk_clist));
601 LASSERT(gsec_kr->gsk_root_ctx == NULL);
603 gss_sec_destroy_common(gsec);
605 OBD_FREE(gsec_kr, sizeof(*gsec_kr));
608 static inline int user_is_root(struct ptlrpc_sec *sec, struct vfs_cred *vcred)
610 /* except the ROOTONLY flag, treat it as root user only if real uid
611 * is 0, euid/fsuid being 0 are handled as setuid scenarios */
612 if (sec_is_rootonly(sec) || (vcred->vc_uid == 0))
619 * unlink request key from it's ring, which is linked during request_key().
620 * sadly, we have to 'guess' which keyring it's linked to.
622 * FIXME this code is fragile, depend on how request_key_link() is implemented.
624 static void request_key_unlink(struct key *key)
626 struct task_struct *tsk = current;
629 switch (task_aux(tsk)->jit_keyring) {
630 case KEY_REQKEY_DEFL_DEFAULT:
631 case KEY_REQKEY_DEFL_THREAD_KEYRING:
632 ring = key_get(task_aux(tsk)->thread_keyring);
635 case KEY_REQKEY_DEFL_PROCESS_KEYRING:
636 ring = key_get(tsk->signal->process_keyring);
639 case KEY_REQKEY_DEFL_SESSION_KEYRING:
641 ring = key_get(rcu_dereference(tsk->signal->session_keyring));
645 case KEY_REQKEY_DEFL_USER_SESSION_KEYRING:
646 ring = key_get(tsk->user->session_keyring);
648 case KEY_REQKEY_DEFL_USER_KEYRING:
649 ring = key_get(tsk->user->uid_keyring);
651 case KEY_REQKEY_DEFL_GROUP_KEYRING:
657 key_unlink(ring, key);
662 struct ptlrpc_cli_ctx * gss_sec_lookup_ctx_kr(struct ptlrpc_sec *sec,
663 struct vfs_cred *vcred,
664 int create, int remove_dead)
666 struct obd_import *imp = sec->ps_import;
667 struct gss_sec_keyring *gsec_kr = sec2gsec_keyring(sec);
668 struct ptlrpc_cli_ctx *ctx = NULL;
669 unsigned int is_root = 0, create_new = 0;
677 LASSERT(imp != NULL);
679 is_root = user_is_root(sec, vcred);
681 /* a little bit optimization for root context */
683 ctx = sec_lookup_root_ctx_kr(sec);
685 * Only lookup directly for REVERSE sec, which should
688 if (ctx || sec_is_reverse(sec))
692 LASSERT(create != 0);
694 /* for root context, obtain lock and check again, this time hold
695 * the root upcall lock, make sure nobody else populated new root
696 * context after last check. */
698 mutex_lock(&gsec_kr->gsk_root_uc_lock);
700 ctx = sec_lookup_root_ctx_kr(sec);
704 /* update reverse handle for root user */
705 sec2gsec(sec)->gs_rvs_hdl = gss_get_next_ctx_index();
710 /* in case of setuid, key will be constructed as owner of fsuid/fsgid,
711 * but we do authentication based on real uid/gid. the key permission
712 * bits will be exactly as POS_ALL, so only processes who subscribed
713 * this key could have the access, although the quota might be counted
714 * on others (fsuid/fsgid).
716 * keyring will use fsuid/fsgid as upcall parameters, so we have to
717 * encode real uid/gid into callout info.
720 construct_key_desc(desc, sizeof(desc), sec, vcred->vc_uid);
722 /* callout info format:
723 * secid:mech:uid:gid:flags:svc_type:peer_nid:target_uuid
725 coinfo_size = sizeof(struct obd_uuid) + MAX_OBD_NAME + 64;
726 OBD_ALLOC(coinfo, coinfo_size);
730 snprintf(coinfo, coinfo_size, "%d:%s:%u:%u:%s:%d:"LPX64":%s",
731 sec->ps_id, sec2gsec(sec)->gs_mech->gm_name,
732 vcred->vc_uid, vcred->vc_gid,
733 co_flags, import_to_gss_svc(imp),
734 imp->imp_connection->c_peer.nid, imp->imp_obd->obd_name);
736 keyring_upcall_lock(gsec_kr);
737 key = request_key(&gss_key_type, desc, coinfo);
738 keyring_upcall_unlock(gsec_kr);
740 OBD_FREE(coinfo, coinfo_size);
743 CERROR("failed request key: %ld\n", PTR_ERR(key));
747 /* once payload.data was pointed to a ctx, it never changes until
748 * we de-associate them; but parallel request_key() may return
749 * a key with payload.data == NULL at the same time. so we still
750 * need wirtelock of key->sem to serialize them. */
751 down_write(&key->sem);
753 if (likely(key->payload.data != NULL)) {
754 ctx = key->payload.data;
756 LASSERT(atomic_read(&ctx->cc_refcount) >= 1);
757 LASSERT(ctx2gctx_keyring(ctx)->gck_key == key);
758 LASSERT(atomic_read(&key->usage) >= 2);
760 /* simply take a ref and return. it's upper layer's
761 * responsibility to detect & replace dead ctx. */
762 atomic_inc(&ctx->cc_refcount);
764 /* pre initialization with a cli_ctx. this can't be done in
765 * key_instantiate() because we'v no enough information
767 ctx = ctx_create_kr(sec, vcred);
769 ctx_enlist_kr(ctx, is_root, 0);
770 bind_key_ctx(key, ctx);
772 ctx_start_timer_kr(ctx, KEYRING_UPCALL_TIMEOUT);
774 CDEBUG(D_SEC, "installed key %p <-> ctx %p (sec %p)\n",
777 /* we'd prefer to call key_revoke(), but we more like
778 * to revoke it within this key->sem locked period. */
779 key_revoke_locked(key);
787 if (is_root && create_new)
788 request_key_unlink(key);
793 mutex_unlock(&gsec_kr->gsk_root_uc_lock);
798 void gss_sec_release_ctx_kr(struct ptlrpc_sec *sec,
799 struct ptlrpc_cli_ctx *ctx,
802 LASSERT(atomic_read(&sec->ps_refcount) > 0);
803 LASSERT(atomic_read(&ctx->cc_refcount) == 0);
804 ctx_release_kr(ctx, sync);
808 * flush context of normal user, we must resort to keyring itself to find out
809 * contexts which belong to me.
811 * Note here we suppose only to flush _my_ context, the "uid" will
812 * be ignored in the search.
815 void flush_user_ctx_cache_kr(struct ptlrpc_sec *sec,
817 int grace, int force)
822 /* nothing to do for reverse or rootonly sec */
823 if (sec_is_reverse(sec) || sec_is_rootonly(sec))
826 construct_key_desc(desc, sizeof(desc), sec, uid);
828 /* there should be only one valid key, but we put it in the
829 * loop in case of any weird cases */
831 key = request_key(&gss_key_type, desc, NULL);
833 CWARN("No more key found for current user\n");
837 down_write(&key->sem);
839 kill_key_locked(key);
841 /* kill_key_locked() should usually revoke the key, but we
842 * revoke it again to make sure, e.g. some case the key may
843 * not well coupled with a context. */
844 key_revoke_locked(key);
853 * flush context of root or all, we iterate through the list.
856 void flush_spec_ctx_cache_kr(struct ptlrpc_sec *sec,
858 int grace, int force)
860 struct gss_sec_keyring *gsec_kr;
861 struct hlist_head freelist = CFS_HLIST_HEAD_INIT;
862 struct hlist_node *pos, *next;
863 struct ptlrpc_cli_ctx *ctx;
866 gsec_kr = sec2gsec_keyring(sec);
868 spin_lock(&sec->ps_lock);
869 hlist_for_each_entry_safe(ctx, pos, next,
870 &gsec_kr->gsk_clist, cc_cache) {
871 LASSERT(atomic_read(&ctx->cc_refcount) > 0);
873 if (uid != -1 && uid != ctx->cc_vcred.vc_uid)
876 /* at this moment there's at least 2 base reference:
877 * key association and in-list. */
878 if (atomic_read(&ctx->cc_refcount) > 2) {
881 CWARN("flush busy ctx %p(%u->%s, extra ref %d)\n",
882 ctx, ctx->cc_vcred.vc_uid,
883 sec2target_str(ctx->cc_sec),
884 atomic_read(&ctx->cc_refcount) - 2);
887 set_bit(PTLRPC_CTX_DEAD_BIT, &ctx->cc_flags);
889 clear_bit(PTLRPC_CTX_UPTODATE_BIT, &ctx->cc_flags);
891 atomic_inc(&ctx->cc_refcount);
893 if (ctx_unlist_kr(ctx, 1)) {
894 hlist_add_head(&ctx->cc_cache, &freelist);
896 LASSERT(atomic_read(&ctx->cc_refcount) >= 2);
897 atomic_dec(&ctx->cc_refcount);
900 spin_unlock(&sec->ps_lock);
902 dispose_ctx_list_kr(&freelist);
907 int gss_sec_flush_ctx_cache_kr(struct ptlrpc_sec *sec,
909 int grace, int force)
913 CDEBUG(D_SEC, "sec %p(%d, nctx %d), uid %d, grace %d, force %d\n",
914 sec, atomic_read(&sec->ps_refcount), atomic_read(&sec->ps_nctx),
917 if (uid != -1 && uid != 0)
918 flush_user_ctx_cache_kr(sec, uid, grace, force);
920 flush_spec_ctx_cache_kr(sec, uid, grace, force);
926 void gss_sec_gc_ctx_kr(struct ptlrpc_sec *sec)
928 struct gss_sec_keyring *gsec_kr = sec2gsec_keyring(sec);
929 struct hlist_head freelist = CFS_HLIST_HEAD_INIT;
930 struct hlist_node *pos, *next;
931 struct ptlrpc_cli_ctx *ctx;
934 CWARN("running gc\n");
936 spin_lock(&sec->ps_lock);
937 hlist_for_each_entry_safe(ctx, pos, next,
938 &gsec_kr->gsk_clist, cc_cache) {
939 LASSERT(atomic_read(&ctx->cc_refcount) > 0);
941 atomic_inc(&ctx->cc_refcount);
943 if (cli_ctx_check_death(ctx) && ctx_unlist_kr(ctx, 1)) {
944 hlist_add_head(&ctx->cc_cache, &freelist);
945 CWARN("unhashed ctx %p\n", ctx);
947 LASSERT(atomic_read(&ctx->cc_refcount) >= 2);
948 atomic_dec(&ctx->cc_refcount);
951 spin_unlock(&sec->ps_lock);
953 dispose_ctx_list_kr(&freelist);
959 int gss_sec_display_kr(struct ptlrpc_sec *sec, struct seq_file *seq)
961 struct gss_sec_keyring *gsec_kr = sec2gsec_keyring(sec);
962 struct hlist_node *pos, *next;
963 struct ptlrpc_cli_ctx *ctx;
964 struct gss_cli_ctx *gctx;
965 time_t now = cfs_time_current_sec();
968 spin_lock(&sec->ps_lock);
969 hlist_for_each_entry_safe(ctx, pos, next,
970 &gsec_kr->gsk_clist, cc_cache) {
975 gctx = ctx2gctx(ctx);
976 key = ctx2gctx_keyring(ctx)->gck_key;
978 gss_cli_ctx_flags2str(ctx->cc_flags,
979 flags_str, sizeof(flags_str));
981 if (gctx->gc_mechctx)
982 lgss_display(gctx->gc_mechctx, mech, sizeof(mech));
984 snprintf(mech, sizeof(mech), "N/A");
985 mech[sizeof(mech) - 1] = '\0';
987 seq_printf(seq, "%p: uid %u, ref %d, expire %ld(%+ld), fl %s, "
988 "seq %d, win %u, key %08x(ref %d), "
989 "hdl "LPX64":"LPX64", mech: %s\n",
990 ctx, ctx->cc_vcred.vc_uid,
991 atomic_read(&ctx->cc_refcount),
993 ctx->cc_expire ? ctx->cc_expire - now : 0,
995 atomic_read(&gctx->gc_seq),
997 key ? key->serial : 0,
998 key ? atomic_read(&key->usage) : 0,
999 gss_handle_to_u64(&gctx->gc_handle),
1000 gss_handle_to_u64(&gctx->gc_svc_handle),
1003 spin_unlock(&sec->ps_lock);
1008 /****************************************
1010 ****************************************/
1013 int gss_cli_ctx_refresh_kr(struct ptlrpc_cli_ctx *ctx)
1015 /* upcall is already on the way */
1020 int gss_cli_ctx_validate_kr(struct ptlrpc_cli_ctx *ctx)
1022 LASSERT(atomic_read(&ctx->cc_refcount) > 0);
1023 LASSERT(ctx->cc_sec);
1025 if (cli_ctx_check_death(ctx)) {
1030 if (cli_ctx_is_ready(ctx))
1036 void gss_cli_ctx_die_kr(struct ptlrpc_cli_ctx *ctx, int grace)
1038 LASSERT(atomic_read(&ctx->cc_refcount) > 0);
1039 LASSERT(ctx->cc_sec);
1041 cli_ctx_expire(ctx);
1045 /****************************************
1046 * (reverse) service *
1047 ****************************************/
1050 * reverse context could have nothing to do with keyrings. here we still keep
1051 * the version which bind to a key, for future reference.
1053 #define HAVE_REVERSE_CTX_NOKEY
1055 #ifdef HAVE_REVERSE_CTX_NOKEY
1058 int sec_install_rctx_kr(struct ptlrpc_sec *sec,
1059 struct ptlrpc_svc_ctx *svc_ctx)
1061 struct ptlrpc_cli_ctx *cli_ctx;
1062 struct vfs_cred vcred = { 0, 0 };
1068 cli_ctx = ctx_create_kr(sec, &vcred);
1069 if (cli_ctx == NULL)
1072 rc = gss_copy_rvc_cli_ctx(cli_ctx, svc_ctx);
1074 CERROR("failed copy reverse cli ctx: %d\n", rc);
1076 ctx_put_kr(cli_ctx, 1);
1080 rvs_sec_install_root_ctx_kr(sec, cli_ctx, NULL);
1082 ctx_put_kr(cli_ctx, 1);
1087 #else /* ! HAVE_REVERSE_CTX_NOKEY */
1090 int sec_install_rctx_kr(struct ptlrpc_sec *sec,
1091 struct ptlrpc_svc_ctx *svc_ctx)
1093 struct ptlrpc_cli_ctx *cli_ctx = NULL;
1095 struct vfs_cred vcred = { 0, 0 };
1103 construct_key_desc(desc, sizeof(desc), sec, 0);
1105 key = key_alloc(&gss_key_type, desc, 0, 0,
1106 KEY_POS_ALL | KEY_USR_ALL, 1);
1108 CERROR("failed to alloc key: %ld\n", PTR_ERR(key));
1109 return PTR_ERR(key);
1112 rc = key_instantiate_and_link(key, NULL, 0, NULL, NULL);
1114 CERROR("failed to instantiate key: %d\n", rc);
1118 down_write(&key->sem);
1120 LASSERT(key->payload.data == NULL);
1122 cli_ctx = ctx_create_kr(sec, &vcred);
1123 if (cli_ctx == NULL) {
1128 rc = gss_copy_rvc_cli_ctx(cli_ctx, svc_ctx);
1130 CERROR("failed copy reverse cli ctx: %d\n", rc);
1134 rvs_sec_install_root_ctx_kr(sec, cli_ctx, key);
1136 ctx_put_kr(cli_ctx, 1);
1137 up_write(&key->sem);
1146 ctx_put_kr(cli_ctx, 1);
1148 up_write(&key->sem);
1154 #endif /* HAVE_REVERSE_CTX_NOKEY */
1156 /****************************************
1158 ****************************************/
1161 int gss_svc_accept_kr(struct ptlrpc_request *req)
1163 return gss_svc_accept(&gss_policy_keyring, req);
1167 int gss_svc_install_rctx_kr(struct obd_import *imp,
1168 struct ptlrpc_svc_ctx *svc_ctx)
1170 struct ptlrpc_sec *sec;
1173 sec = sptlrpc_import_sec_ref(imp);
1176 rc = sec_install_rctx_kr(sec, svc_ctx);
1177 sptlrpc_sec_put(sec);
1182 /****************************************
1184 ****************************************/
1187 int gss_kt_instantiate(struct key *key, const void *data, size_t datalen)
1192 if (data != NULL || datalen != 0) {
1193 CERROR("invalid: data %p, len %d\n", data, datalen);
1197 if (key->payload.data != 0) {
1198 CERROR("key already have payload\n");
1202 /* link the key to session keyring, so following context negotiation
1203 * rpc fired from user space could find this key. This will be unlinked
1204 * automatically when upcall processes die.
1206 * we can't do this through keyctl from userspace, because the upcall
1207 * might be neither possessor nor owner of the key (setuid).
1209 * the session keyring is created upon upcall, and don't change all
1210 * the way until upcall finished, so rcu lock is not needed here.
1212 LASSERT(cfs_current()->signal->session_keyring);
1214 rc = key_link(cfs_current()->signal->session_keyring, key);
1216 CERROR("failed to link key %08x to keyring %08x: %d\n",
1218 cfs_current()->signal->session_keyring->serial, rc);
1222 CDEBUG(D_SEC, "key %p instantiated, ctx %p\n", key, key->payload.data);
1227 * called with key semaphore write locked. it means we can operate
1228 * on the context without fear of loosing refcount.
1231 int gss_kt_update(struct key *key, const void *data, size_t datalen)
1233 struct ptlrpc_cli_ctx *ctx = key->payload.data;
1234 struct gss_cli_ctx *gctx;
1235 rawobj_t tmpobj = RAWOBJ_EMPTY;
1239 if (data == NULL || datalen == 0) {
1240 CWARN("invalid: data %p, len %d\n", data, datalen);
1244 /* there's a race between userspace parent - child processes. if
1245 * child finish negotiation too fast and call kt_update(), the ctx
1246 * might be still NULL. but the key will finally be associate
1247 * with a context, or be revoked. if key status is fine, return
1248 * -EAGAIN to allow userspace sleep a while and call again. */
1250 CWARN("race in userspace. key %p(%x) flags %lx\n",
1251 key, key->serial, key->flags);
1253 rc = key_validate(key);
1260 LASSERT(atomic_read(&ctx->cc_refcount) > 0);
1261 LASSERT(ctx->cc_sec);
1263 ctx_clear_timer_kr(ctx);
1265 /* don't proceed if already refreshed */
1266 if (cli_ctx_is_refreshed(ctx)) {
1267 CWARN("ctx already done refresh\n");
1268 sptlrpc_cli_ctx_wakeup(ctx);
1272 sptlrpc_cli_ctx_get(ctx);
1273 gctx = ctx2gctx(ctx);
1275 rc = buffer_extract_bytes(&data, &datalen, &gctx->gc_win,
1276 sizeof(gctx->gc_win));
1278 CERROR("failed extract seq_win\n");
1282 if (gctx->gc_win == 0) {
1283 __u32 nego_rpc_err, nego_gss_err;
1285 rc = buffer_extract_bytes(&data, &datalen, &nego_rpc_err,
1286 sizeof(nego_rpc_err));
1288 CERROR("failed to extrace rpc rc\n");
1292 rc = buffer_extract_bytes(&data, &datalen, &nego_gss_err,
1293 sizeof(nego_gss_err));
1295 CERROR("failed to extrace gss rc\n");
1299 CERROR("negotiation: rpc err %d, gss err %x\n",
1300 nego_rpc_err, nego_gss_err);
1302 rc = nego_rpc_err ? nego_rpc_err : -EACCES;
1304 rc = rawobj_extract_local_alloc(&gctx->gc_handle,
1305 (__u32 **) &data, &datalen);
1307 CERROR("failed extract handle\n");
1311 rc = rawobj_extract_local(&tmpobj, (__u32 **) &data, &datalen);
1313 CERROR("failed extract mech\n");
1317 rc = lgss_import_sec_context(&tmpobj,
1318 sec2gsec(ctx->cc_sec)->gs_mech,
1320 if (rc != GSS_S_COMPLETE)
1321 CERROR("failed import context\n");
1326 /* we don't care what current status of this ctx, even someone else
1327 * is operating on the ctx at the same time. we just add up our own
1330 gss_cli_ctx_uptodate(gctx);
1332 /* this will also revoke the key. has to be done before
1333 * wakeup waiters otherwise they can find the stale key */
1334 kill_key_locked(key);
1336 cli_ctx_expire(ctx);
1338 if (rc != -ERESTART)
1339 set_bit(PTLRPC_CTX_ERROR_BIT, &ctx->cc_flags);
1342 sptlrpc_cli_ctx_wakeup(ctx);
1344 /* let user space think it's a success */
1345 sptlrpc_cli_ctx_put(ctx, 1);
1350 int gss_kt_match(const struct key *key, const void *desc)
1352 return (strcmp(key->description, (const char *) desc) == 0);
1356 void gss_kt_destroy(struct key *key)
1359 LASSERT(key->payload.data == NULL);
1360 CDEBUG(D_SEC, "destroy key %p\n", key);
1365 void gss_kt_describe(const struct key *key, struct seq_file *s)
1367 if (key->description == NULL)
1368 seq_puts(s, "[null]");
1370 seq_puts(s, key->description);
1373 static struct key_type gss_key_type =
1377 .instantiate = gss_kt_instantiate,
1378 .update = gss_kt_update,
1379 .match = gss_kt_match,
1380 .destroy = gss_kt_destroy,
1381 .describe = gss_kt_describe,
1384 /****************************************
1385 * lustre gss keyring policy *
1386 ****************************************/
1388 static struct ptlrpc_ctx_ops gss_keyring_ctxops = {
1389 .match = gss_cli_ctx_match,
1390 .refresh = gss_cli_ctx_refresh_kr,
1391 .validate = gss_cli_ctx_validate_kr,
1392 .die = gss_cli_ctx_die_kr,
1393 .sign = gss_cli_ctx_sign,
1394 .verify = gss_cli_ctx_verify,
1395 .seal = gss_cli_ctx_seal,
1396 .unseal = gss_cli_ctx_unseal,
1397 .wrap_bulk = gss_cli_ctx_wrap_bulk,
1398 .unwrap_bulk = gss_cli_ctx_unwrap_bulk,
1401 static struct ptlrpc_sec_cops gss_sec_keyring_cops = {
1402 .create_sec = gss_sec_create_kr,
1403 .destroy_sec = gss_sec_destroy_kr,
1404 .kill_sec = gss_sec_kill,
1405 .lookup_ctx = gss_sec_lookup_ctx_kr,
1406 .release_ctx = gss_sec_release_ctx_kr,
1407 .flush_ctx_cache = gss_sec_flush_ctx_cache_kr,
1408 .gc_ctx = gss_sec_gc_ctx_kr,
1409 .install_rctx = gss_sec_install_rctx,
1410 .alloc_reqbuf = gss_alloc_reqbuf,
1411 .free_reqbuf = gss_free_reqbuf,
1412 .alloc_repbuf = gss_alloc_repbuf,
1413 .free_repbuf = gss_free_repbuf,
1414 .enlarge_reqbuf = gss_enlarge_reqbuf,
1415 .display = gss_sec_display_kr,
1418 static struct ptlrpc_sec_sops gss_sec_keyring_sops = {
1419 .accept = gss_svc_accept_kr,
1420 .invalidate_ctx = gss_svc_invalidate_ctx,
1421 .alloc_rs = gss_svc_alloc_rs,
1422 .authorize = gss_svc_authorize,
1423 .free_rs = gss_svc_free_rs,
1424 .free_ctx = gss_svc_free_ctx,
1425 .unwrap_bulk = gss_svc_unwrap_bulk,
1426 .wrap_bulk = gss_svc_wrap_bulk,
1427 .install_rctx = gss_svc_install_rctx_kr,
1430 static struct ptlrpc_sec_policy gss_policy_keyring = {
1431 .sp_owner = THIS_MODULE,
1432 .sp_name = "gss.keyring",
1433 .sp_policy = SPTLRPC_POLICY_GSS,
1434 .sp_cops = &gss_sec_keyring_cops,
1435 .sp_sops = &gss_sec_keyring_sops,
1439 int __init gss_init_keyring(void)
1443 rc = register_key_type(&gss_key_type);
1445 CERROR("failed to register keyring type: %d\n", rc);
1449 rc = sptlrpc_register_policy(&gss_policy_keyring);
1451 unregister_key_type(&gss_key_type);
1458 void __exit gss_exit_keyring(void)
1460 unregister_key_type(&gss_key_type);
1461 sptlrpc_unregister_policy(&gss_policy_keyring);