4 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License version 2 only,
8 * as published by the Free Software Foundation.
10 * This program is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * General Public License version 2 for more details (a copy is included
14 * in the LICENSE file that accompanied this code).
16 * You should have received a copy of the GNU General Public License
17 * version 2 along with this program; If not, see
18 * http://www.gnu.org/licenses/gpl-2.0.html
23 * Copyright (c) 2007, 2010, Oracle and/or its affiliates. All rights reserved.
24 * Use is subject to license terms.
26 * Copyright (c) 2012, 2014, Intel Corporation.
29 * This file is part of Lustre, http://www.lustre.org/
30 * Lustre is a trademark of Sun Microsystems, Inc.
32 * lustre/ptlrpc/gss/gss_keyring.c
34 * Author: Eric Mei <ericm@clusterfs.com>
37 #define DEBUG_SUBSYSTEM S_SEC
38 #include <linux/init.h>
39 #include <linux/module.h>
40 #include <linux/slab.h>
41 #include <linux/dcache.h>
43 #include <linux/crypto.h>
44 #include <linux/key.h>
45 #include <linux/keyctl.h>
46 #include <linux/key-type.h>
47 #include <linux/mutex.h>
48 #include <asm/atomic.h>
50 #include <libcfs/linux/linux-list.h>
52 #include <obd_class.h>
53 #include <obd_support.h>
54 #include <uapi/linux/lustre/lustre_idl.h>
55 #include <lustre_sec.h>
56 #include <lustre_net.h>
57 #include <lustre_import.h>
60 #include "gss_internal.h"
63 #ifdef HAVE_GET_REQUEST_KEY_AUTH
64 #include <keys/request_key_auth-type.h>
67 static struct ptlrpc_sec_policy gss_policy_keyring;
68 static struct ptlrpc_ctx_ops gss_keyring_ctxops;
69 static struct key_type gss_key_type;
71 static int sec_install_rctx_kr(struct ptlrpc_sec *sec,
72 struct ptlrpc_svc_ctx *svc_ctx);
75 * the timeout is only for the case that upcall child process die abnormally.
76 * in any other cases it should finally update kernel key.
78 * FIXME we'd better to incorporate the client & server side upcall timeouts
79 * into the framework of Adaptive Timeouts, but we need to figure out how to
80 * make sure that kernel knows the upcall processes is in-progress or died
83 #define KEYRING_UPCALL_TIMEOUT (obd_timeout + obd_timeout)
85 /****************************************
87 ****************************************/
89 static inline void keyring_upcall_lock(struct gss_sec_keyring *gsec_kr)
91 #ifdef HAVE_KEYRING_UPCALL_SERIALIZED
92 mutex_lock(&gsec_kr->gsk_uc_lock);
96 static inline void keyring_upcall_unlock(struct gss_sec_keyring *gsec_kr)
98 #ifdef HAVE_KEYRING_UPCALL_SERIALIZED
99 mutex_unlock(&gsec_kr->gsk_uc_lock);
103 static inline void key_revoke_locked(struct key *key)
105 set_bit(KEY_FLAG_REVOKED, &key->flags);
108 static void ctx_upcall_timeout_kr(cfs_timer_cb_arg_t data)
110 struct gss_cli_ctx_keyring *gctx_kr = cfs_from_timer(gctx_kr,
112 struct ptlrpc_cli_ctx *ctx = &(gctx_kr->gck_base.gc_base);
113 struct key *key = gctx_kr->gck_key;
115 CWARN("ctx %p, key %p\n", ctx, key);
120 key_revoke_locked(key);
123 static void ctx_start_timer_kr(struct ptlrpc_cli_ctx *ctx, time64_t timeout)
125 struct gss_cli_ctx_keyring *gctx_kr = ctx2gctx_keyring(ctx);
126 struct timer_list *timer = &gctx_kr->gck_timer;
130 CDEBUG(D_SEC, "ctx %p: start timer %llds\n", ctx, timeout);
132 cfs_timer_setup(timer, ctx_upcall_timeout_kr,
133 (unsigned long)gctx_kr, 0);
134 timer->expires = cfs_time_seconds(timeout) + jiffies;
139 * caller should make sure no race with other threads
142 void ctx_clear_timer_kr(struct ptlrpc_cli_ctx *ctx)
144 struct gss_cli_ctx_keyring *gctx_kr = ctx2gctx_keyring(ctx);
145 struct timer_list *timer = &gctx_kr->gck_timer;
147 CDEBUG(D_SEC, "ctx %p, key %p\n", ctx, gctx_kr->gck_key);
149 del_singleshot_timer_sync(timer);
153 struct ptlrpc_cli_ctx *ctx_create_kr(struct ptlrpc_sec *sec,
154 struct vfs_cred *vcred)
156 struct ptlrpc_cli_ctx *ctx;
157 struct gss_cli_ctx_keyring *gctx_kr;
159 OBD_ALLOC_PTR(gctx_kr);
163 cfs_timer_setup(&gctx_kr->gck_timer, NULL, 0, 0);
165 ctx = &gctx_kr->gck_base.gc_base;
167 if (gss_cli_ctx_init_common(sec, ctx, &gss_keyring_ctxops, vcred)) {
168 OBD_FREE_PTR(gctx_kr);
172 ctx->cc_expire = ktime_get_real_seconds() + KEYRING_UPCALL_TIMEOUT;
173 clear_bit(PTLRPC_CTX_NEW_BIT, &ctx->cc_flags);
174 atomic_inc(&ctx->cc_refcount); /* for the caller */
179 static void ctx_destroy_kr(struct ptlrpc_cli_ctx *ctx)
181 struct ptlrpc_sec *sec = ctx->cc_sec;
182 struct gss_cli_ctx_keyring *gctx_kr = ctx2gctx_keyring(ctx);
184 CDEBUG(D_SEC, "destroying ctx %p\n", ctx);
186 /* at this time the association with key has been broken. */
188 LASSERT(atomic_read(&sec->ps_refcount) > 0);
189 LASSERT(atomic_read(&sec->ps_nctx) > 0);
190 LASSERT(test_bit(PTLRPC_CTX_CACHED_BIT, &ctx->cc_flags) == 0);
191 LASSERT(gctx_kr->gck_key == NULL);
193 ctx_clear_timer_kr(ctx);
195 if (gss_cli_ctx_fini_common(sec, ctx))
198 OBD_FREE_PTR(gctx_kr);
200 atomic_dec(&sec->ps_nctx);
201 sptlrpc_sec_put(sec);
204 static void ctx_release_kr(struct ptlrpc_cli_ctx *ctx, int sync)
209 atomic_inc(&ctx->cc_refcount);
210 sptlrpc_gc_add_ctx(ctx);
214 static void ctx_put_kr(struct ptlrpc_cli_ctx *ctx, int sync)
216 LASSERT(atomic_read(&ctx->cc_refcount) > 0);
218 if (atomic_dec_and_test(&ctx->cc_refcount))
219 ctx_release_kr(ctx, sync);
223 * key <-> ctx association and rules:
224 * - ctx might not bind with any key
225 * - key/ctx binding is protected by key semaphore (if the key present)
226 * - key and ctx each take a reference of the other
227 * - ctx enlist/unlist is protected by ctx spinlock
228 * - never enlist a ctx after it's been unlisted
229 * - whoever do enlist should also do bind, lock key before enlist:
230 * - lock key -> lock ctx -> enlist -> unlock ctx -> bind -> unlock key
231 * - whoever do unlist should also do unbind:
232 * - lock key -> lock ctx -> unlist -> unlock ctx -> unbind -> unlock key
233 * - lock ctx -> unlist -> unlock ctx -> lock key -> unbind -> unlock key
236 static inline void spin_lock_if(spinlock_t *lock, int condition)
242 static inline void spin_unlock_if(spinlock_t *lock, int condition)
248 static void ctx_enlist_kr(struct ptlrpc_cli_ctx *ctx, int is_root, int locked)
250 struct ptlrpc_sec *sec = ctx->cc_sec;
251 struct gss_sec_keyring *gsec_kr = sec2gsec_keyring(sec);
253 LASSERT(!test_bit(PTLRPC_CTX_CACHED_BIT, &ctx->cc_flags));
254 LASSERT(atomic_read(&ctx->cc_refcount) > 0);
256 spin_lock_if(&sec->ps_lock, !locked);
258 atomic_inc(&ctx->cc_refcount);
259 set_bit(PTLRPC_CTX_CACHED_BIT, &ctx->cc_flags);
260 hlist_add_head(&ctx->cc_cache, &gsec_kr->gsk_clist);
262 gsec_kr->gsk_root_ctx = ctx;
264 spin_unlock_if(&sec->ps_lock, !locked);
268 * Note after this get called, caller should not access ctx again because
269 * it might have been freed, unless caller hold at least one refcount of
272 * return non-zero if we indeed unlist this ctx.
274 static int ctx_unlist_kr(struct ptlrpc_cli_ctx *ctx, int locked)
276 struct ptlrpc_sec *sec = ctx->cc_sec;
277 struct gss_sec_keyring *gsec_kr = sec2gsec_keyring(sec);
279 /* if hashed bit has gone, leave the job to somebody who is doing it */
280 if (test_and_clear_bit(PTLRPC_CTX_CACHED_BIT, &ctx->cc_flags) == 0)
283 /* drop ref inside spin lock to prevent race with other operations */
284 spin_lock_if(&sec->ps_lock, !locked);
286 if (gsec_kr->gsk_root_ctx == ctx)
287 gsec_kr->gsk_root_ctx = NULL;
288 hlist_del_init(&ctx->cc_cache);
289 atomic_dec(&ctx->cc_refcount);
291 spin_unlock_if(&sec->ps_lock, !locked);
297 * Get specific payload. Newer kernels support 4 slots.
300 key_get_payload(struct key *key, unsigned int index)
302 void *key_ptr = NULL;
304 #ifdef HAVE_KEY_PAYLOAD_DATA_ARRAY
305 key_ptr = key->payload.data[index];
308 key_ptr = key->payload.data;
314 * Set specific payload. Newer kernels support 4 slots.
316 static int key_set_payload(struct key *key, unsigned int index,
317 struct ptlrpc_cli_ctx *ctx)
321 #ifdef HAVE_KEY_PAYLOAD_DATA_ARRAY
323 key->payload.data[index] = ctx;
326 key->payload.data = ctx;
334 * bind a key with a ctx together.
335 * caller must hold write lock of the key, as well as ref on key & ctx.
337 static void bind_key_ctx(struct key *key, struct ptlrpc_cli_ctx *ctx)
339 LASSERT(atomic_read(&ctx->cc_refcount) > 0);
340 LASSERT(ll_read_key_usage(key) > 0);
341 LASSERT(ctx2gctx_keyring(ctx)->gck_key == NULL);
342 LASSERT(!key_get_payload(key, 0));
344 /* at this time context may or may not in list. */
346 atomic_inc(&ctx->cc_refcount);
347 ctx2gctx_keyring(ctx)->gck_key = key;
348 LASSERT(!key_set_payload(key, 0, ctx));
352 * unbind a key and a ctx.
353 * caller must hold write lock, as well as a ref of the key.
355 static void unbind_key_ctx(struct key *key, struct ptlrpc_cli_ctx *ctx)
357 LASSERT(key_get_payload(key, 0) == ctx);
358 LASSERT(test_bit(PTLRPC_CTX_CACHED_BIT, &ctx->cc_flags) == 0);
360 /* must revoke the key, or others may treat it as newly created */
361 key_revoke_locked(key);
363 key_set_payload(key, 0, NULL);
364 ctx2gctx_keyring(ctx)->gck_key = NULL;
366 /* once ctx get split from key, the timer is meaningless */
367 ctx_clear_timer_kr(ctx);
374 * given a ctx, unbind with its coupled key, if any.
375 * unbind could only be called once, so we don't worry the key be released
378 static void unbind_ctx_kr(struct ptlrpc_cli_ctx *ctx)
380 struct key *key = ctx2gctx_keyring(ctx)->gck_key;
383 LASSERT(key_get_payload(key, 0) == ctx);
386 down_write(&key->sem);
387 unbind_key_ctx(key, ctx);
394 * given a key, unbind with its coupled ctx, if any.
395 * caller must hold write lock, as well as a ref of the key.
397 static void unbind_key_locked(struct key *key)
399 struct ptlrpc_cli_ctx *ctx = key_get_payload(key, 0);
402 unbind_key_ctx(key, ctx);
406 * unlist a ctx, and unbind from coupled key
408 static void kill_ctx_kr(struct ptlrpc_cli_ctx *ctx)
410 if (ctx_unlist_kr(ctx, 0))
415 * given a key, unlist and unbind with the coupled ctx (if any).
416 * caller must hold write lock, as well as a ref of the key.
418 static void kill_key_locked(struct key *key)
420 struct ptlrpc_cli_ctx *ctx = key_get_payload(key, 0);
422 if (ctx && ctx_unlist_kr(ctx, 0))
423 unbind_key_locked(key);
427 * caller should hold one ref on contexts in freelist.
429 static void dispose_ctx_list_kr(struct hlist_head *freelist)
431 struct hlist_node *next;
432 struct ptlrpc_cli_ctx *ctx;
433 struct gss_cli_ctx *gctx;
435 hlist_for_each_entry_safe(ctx, next, freelist, cc_cache) {
436 hlist_del_init(&ctx->cc_cache);
438 /* reverse ctx: update current seq to buddy svcctx if exist.
439 * ideally this should be done at gss_cli_ctx_finalize(), but
440 * the ctx destroy could be delayed by:
441 * 1) ctx still has reference;
442 * 2) ctx destroy is asynchronous;
443 * and reverse import call inval_all_ctx() require this be done
444 * _immediately_ otherwise newly created reverse ctx might copy
445 * the very old sequence number from svcctx. */
446 gctx = ctx2gctx(ctx);
447 if (!rawobj_empty(&gctx->gc_svc_handle) &&
448 sec_is_reverse(gctx->gc_base.cc_sec)) {
449 gss_svc_upcall_update_sequence(&gctx->gc_svc_handle,
450 (__u32) atomic_read(&gctx->gc_seq));
453 /* we need to wakeup waiting reqs here. the context might
454 * be forced released before upcall finished, then the
455 * late-arrived downcall can't find the ctx even. */
456 sptlrpc_cli_ctx_wakeup(ctx);
464 * lookup a root context directly in a sec, return root ctx with a
465 * reference taken or NULL.
468 struct ptlrpc_cli_ctx * sec_lookup_root_ctx_kr(struct ptlrpc_sec *sec)
470 struct gss_sec_keyring *gsec_kr = sec2gsec_keyring(sec);
471 struct ptlrpc_cli_ctx *ctx = NULL;
473 spin_lock(&sec->ps_lock);
475 ctx = gsec_kr->gsk_root_ctx;
477 if (ctx == NULL && unlikely(sec_is_reverse(sec))) {
478 struct ptlrpc_cli_ctx *tmp;
480 /* reverse ctx, search root ctx in list, choose the one
481 * with shortest expire time, which is most possibly have
482 * an established peer ctx at client side. */
483 hlist_for_each_entry(tmp, &gsec_kr->gsk_clist, cc_cache) {
484 if (ctx == NULL || ctx->cc_expire == 0 ||
485 ctx->cc_expire > tmp->cc_expire) {
487 /* promote to be root_ctx */
488 gsec_kr->gsk_root_ctx = ctx;
494 LASSERT(atomic_read(&ctx->cc_refcount) > 0);
495 LASSERT(!hlist_empty(&gsec_kr->gsk_clist));
496 atomic_inc(&ctx->cc_refcount);
499 spin_unlock(&sec->ps_lock);
504 #define RVS_CTX_EXPIRE_NICE (10)
507 void rvs_sec_install_root_ctx_kr(struct ptlrpc_sec *sec,
508 struct ptlrpc_cli_ctx *new_ctx,
511 struct gss_sec_keyring *gsec_kr = sec2gsec_keyring(sec);
512 struct ptlrpc_cli_ctx *ctx;
516 LASSERT(sec_is_reverse(sec));
518 spin_lock(&sec->ps_lock);
520 now = ktime_get_real_seconds();
522 /* set all existing ctxs short expiry */
523 hlist_for_each_entry(ctx, &gsec_kr->gsk_clist, cc_cache) {
524 if (ctx->cc_expire > now + RVS_CTX_EXPIRE_NICE) {
525 ctx->cc_early_expire = 1;
526 ctx->cc_expire = now + RVS_CTX_EXPIRE_NICE;
530 /* if there's root_ctx there, instead obsolete the current
531 * immediately, we leave it continue operating for a little while.
532 * hopefully when the first backward rpc with newest ctx send out,
533 * the client side already have the peer ctx well established. */
534 ctx_enlist_kr(new_ctx, gsec_kr->gsk_root_ctx ? 0 : 1, 1);
537 bind_key_ctx(key, new_ctx);
539 spin_unlock(&sec->ps_lock);
542 static void construct_key_desc(void *buf, int bufsize,
543 struct ptlrpc_sec *sec, uid_t uid)
545 snprintf(buf, bufsize, "%d@%x", uid, sec->ps_id);
546 ((char *)buf)[bufsize - 1] = '\0';
549 /****************************************
551 ****************************************/
554 struct ptlrpc_sec * gss_sec_create_kr(struct obd_import *imp,
555 struct ptlrpc_svc_ctx *svcctx,
556 struct sptlrpc_flavor *sf)
558 struct gss_sec_keyring *gsec_kr;
561 OBD_ALLOC(gsec_kr, sizeof(*gsec_kr));
565 INIT_HLIST_HEAD(&gsec_kr->gsk_clist);
566 gsec_kr->gsk_root_ctx = NULL;
567 mutex_init(&gsec_kr->gsk_root_uc_lock);
568 #ifdef HAVE_KEYRING_UPCALL_SERIALIZED
569 mutex_init(&gsec_kr->gsk_uc_lock);
572 if (gss_sec_create_common(&gsec_kr->gsk_base, &gss_policy_keyring,
576 if (svcctx != NULL &&
577 sec_install_rctx_kr(&gsec_kr->gsk_base.gs_base, svcctx)) {
578 gss_sec_destroy_common(&gsec_kr->gsk_base);
582 RETURN(&gsec_kr->gsk_base.gs_base);
585 OBD_FREE(gsec_kr, sizeof(*gsec_kr));
590 void gss_sec_destroy_kr(struct ptlrpc_sec *sec)
592 struct gss_sec *gsec = sec2gsec(sec);
593 struct gss_sec_keyring *gsec_kr = sec2gsec_keyring(sec);
595 CDEBUG(D_SEC, "destroy %s@%p\n", sec->ps_policy->sp_name, sec);
597 LASSERT(hlist_empty(&gsec_kr->gsk_clist));
598 LASSERT(gsec_kr->gsk_root_ctx == NULL);
600 gss_sec_destroy_common(gsec);
602 OBD_FREE(gsec_kr, sizeof(*gsec_kr));
605 static inline int user_is_root(struct ptlrpc_sec *sec, struct vfs_cred *vcred)
607 /* except the ROOTONLY flag, treat it as root user only if real uid
608 * is 0, euid/fsuid being 0 are handled as setuid scenarios */
609 if (sec_is_rootonly(sec) || (vcred->vc_uid == 0))
616 * kernel 5.3: commit 0f44e4d976f96c6439da0d6717238efa4b91196e
617 * keys: Move the user and user-session keyrings to the user_namespace
619 * When lookup_user_key is available use the kernel API rather than directly
620 * accessing the uid_keyring and session_keyring via the current process
623 #ifdef HAVE_LOOKUP_USER_KEY
625 /* from Linux security/keys/internal.h: */
626 #ifndef KEY_LOOKUP_FOR_UNLINK
627 #define KEY_LOOKUP_FOR_UNLINK 0x04
630 static struct key *_user_key(key_serial_t id)
635 ref = lookup_user_key(id, KEY_LOOKUP_FOR_UNLINK, 0);
638 return key_ref_to_ptr(ref);
641 static inline struct key *get_user_session_keyring(const struct cred *cred)
643 return _user_key(KEY_SPEC_USER_SESSION_KEYRING);
646 static inline struct key *get_user_keyring(const struct cred *cred)
648 return _user_key(KEY_SPEC_USER_KEYRING);
651 static inline struct key *get_user_session_keyring(const struct cred *cred)
653 return key_get(cred->user->session_keyring);
656 static inline struct key *get_user_keyring(const struct cred *cred)
658 return key_get(cred->user->uid_keyring);
663 * unlink request key from it's ring, which is linked during request_key().
664 * sadly, we have to 'guess' which keyring it's linked to.
666 * FIXME this code is fragile, it depends on how request_key() is implemented.
668 static void request_key_unlink(struct key *key)
670 const struct cred *cred = current_cred();
671 struct key *ring = NULL;
673 switch (cred->jit_keyring) {
674 case KEY_REQKEY_DEFL_DEFAULT:
675 case KEY_REQKEY_DEFL_REQUESTOR_KEYRING:
676 #ifdef HAVE_GET_REQUEST_KEY_AUTH
677 if (cred->request_key_auth) {
678 struct request_key_auth *rka;
679 struct key *authkey = cred->request_key_auth;
681 down_read(&authkey->sem);
682 rka = get_request_key_auth(authkey);
683 if (!test_bit(KEY_FLAG_REVOKED, &authkey->flags))
684 ring = key_get(rka->dest_keyring);
685 up_read(&authkey->sem);
691 case KEY_REQKEY_DEFL_THREAD_KEYRING:
692 ring = key_get(cred->thread_keyring);
696 case KEY_REQKEY_DEFL_PROCESS_KEYRING:
697 ring = key_get(cred->process_keyring);
701 case KEY_REQKEY_DEFL_SESSION_KEYRING:
703 ring = key_get(rcu_dereference(cred->session_keyring));
708 case KEY_REQKEY_DEFL_USER_SESSION_KEYRING:
709 ring = get_user_session_keyring(cred);
711 case KEY_REQKEY_DEFL_USER_KEYRING:
712 ring = get_user_keyring(cred);
714 case KEY_REQKEY_DEFL_GROUP_KEYRING:
720 key_unlink(ring, key);
725 struct ptlrpc_cli_ctx * gss_sec_lookup_ctx_kr(struct ptlrpc_sec *sec,
726 struct vfs_cred *vcred,
727 int create, int remove_dead)
729 struct obd_import *imp = sec->ps_import;
730 struct gss_sec_keyring *gsec_kr = sec2gsec_keyring(sec);
731 struct ptlrpc_cli_ctx *ctx = NULL;
732 unsigned int is_root = 0, create_new = 0;
737 const char *sec_part_flags = "";
741 LASSERT(imp != NULL);
743 is_root = user_is_root(sec, vcred);
745 /* a little bit optimization for root context */
747 ctx = sec_lookup_root_ctx_kr(sec);
749 * Only lookup directly for REVERSE sec, which should
752 if (ctx || sec_is_reverse(sec))
756 LASSERT(create != 0);
758 /* for root context, obtain lock and check again, this time hold
759 * the root upcall lock, make sure nobody else populated new root
760 * context after last check. */
762 mutex_lock(&gsec_kr->gsk_root_uc_lock);
764 ctx = sec_lookup_root_ctx_kr(sec);
768 /* update reverse handle for root user */
769 sec2gsec(sec)->gs_rvs_hdl = gss_get_next_ctx_index();
771 switch (sec->ps_part) {
773 sec_part_flags = "m";
776 sec_part_flags = "o";
779 sec_part_flags = "rmo";
782 sec_part_flags = "r";
789 switch (SPTLRPC_FLVR_SVC(sec->ps_flvr.sf_rpc)) {
790 case SPTLRPC_SVC_NULL:
793 case SPTLRPC_SVC_AUTH:
796 case SPTLRPC_SVC_INTG:
799 case SPTLRPC_SVC_PRIV:
807 /* in case of setuid, key will be constructed as owner of fsuid/fsgid,
808 * but we do authentication based on real uid/gid. the key permission
809 * bits will be exactly as POS_ALL, so only processes who subscribed
810 * this key could have the access, although the quota might be counted
811 * on others (fsuid/fsgid).
813 * keyring will use fsuid/fsgid as upcall parameters, so we have to
814 * encode real uid/gid into callout info.
817 /* But first we need to make sure the obd type is supported */
818 if (strcmp(imp->imp_obd->obd_type->typ_name, LUSTRE_MDC_NAME) &&
819 strcmp(imp->imp_obd->obd_type->typ_name, LUSTRE_OSC_NAME) &&
820 strcmp(imp->imp_obd->obd_type->typ_name, LUSTRE_MGC_NAME) &&
821 strcmp(imp->imp_obd->obd_type->typ_name, LUSTRE_LWP_NAME) &&
822 strcmp(imp->imp_obd->obd_type->typ_name, LUSTRE_OSP_NAME)) {
823 CERROR("obd %s is not a supported device\n",
824 imp->imp_obd->obd_name);
825 GOTO(out, ctx = NULL);
828 construct_key_desc(desc, sizeof(desc), sec, vcred->vc_uid);
830 /* callout info format:
831 * secid:mech:uid:gid:sec_flags:svc_flag:svc_type:peer_nid:target_uuid:
834 coinfo_size = sizeof(struct obd_uuid) + MAX_OBD_NAME + 64;
835 OBD_ALLOC(coinfo, coinfo_size);
839 /* Last callout parameter is pid of process whose namespace will be used
840 * for credentials' retrieval.
841 * For user's credentials (in which case sec_part_flags is empty), use
842 * current PID instead of import's reference PID to get reference
844 snprintf(coinfo, coinfo_size, "%d:%s:%u:%u:%s:%c:%d:%#llx:%s:%#llx:%d",
845 sec->ps_id, sec2gsec(sec)->gs_mech->gm_name,
846 vcred->vc_uid, vcred->vc_gid,
847 sec_part_flags, svc_flag, import_to_gss_svc(imp),
848 imp->imp_connection->c_peer.nid, imp->imp_obd->obd_name,
849 imp->imp_connection->c_self,
850 sec_part_flags[0] == '\0' ?
851 current->pid : imp->imp_sec_refpid);
853 CDEBUG(D_SEC, "requesting key for %s\n", desc);
855 keyring_upcall_lock(gsec_kr);
856 key = request_key(&gss_key_type, desc, coinfo);
857 keyring_upcall_unlock(gsec_kr);
859 OBD_FREE(coinfo, coinfo_size);
862 CERROR("failed request key: %ld\n", PTR_ERR(key));
865 CDEBUG(D_SEC, "obtained key %08x for %s\n", key->serial, desc);
867 /* once payload.data was pointed to a ctx, it never changes until
868 * we de-associate them; but parallel request_key() may return
869 * a key with payload.data == NULL at the same time. so we still
870 * need wirtelock of key->sem to serialize them. */
871 down_write(&key->sem);
873 ctx = key_get_payload(key, 0);
875 LASSERT(atomic_read(&ctx->cc_refcount) >= 1);
876 LASSERT(ctx2gctx_keyring(ctx)->gck_key == key);
877 LASSERT(ll_read_key_usage(key) >= 2);
879 /* simply take a ref and return. it's upper layer's
880 * responsibility to detect & replace dead ctx. */
881 atomic_inc(&ctx->cc_refcount);
883 /* pre initialization with a cli_ctx. this can't be done in
884 * key_instantiate() because we'v no enough information
886 ctx = ctx_create_kr(sec, vcred);
888 ctx_enlist_kr(ctx, is_root, 0);
889 bind_key_ctx(key, ctx);
891 ctx_start_timer_kr(ctx, KEYRING_UPCALL_TIMEOUT);
893 CDEBUG(D_SEC, "installed key %p <-> ctx %p (sec %p)\n",
896 /* we'd prefer to call key_revoke(), but we more like
897 * to revoke it within this key->sem locked period. */
898 key_revoke_locked(key);
906 if (is_root && create_new)
907 request_key_unlink(key);
912 mutex_unlock(&gsec_kr->gsk_root_uc_lock);
917 void gss_sec_release_ctx_kr(struct ptlrpc_sec *sec,
918 struct ptlrpc_cli_ctx *ctx,
921 LASSERT(atomic_read(&sec->ps_refcount) > 0);
922 LASSERT(atomic_read(&ctx->cc_refcount) == 0);
923 ctx_release_kr(ctx, sync);
927 * flush context of normal user, we must resort to keyring itself to find out
928 * contexts which belong to me.
930 * Note here we suppose only to flush _my_ context, the "uid" will
931 * be ignored in the search.
934 void flush_user_ctx_cache_kr(struct ptlrpc_sec *sec,
936 int grace, int force)
941 /* nothing to do for reverse or rootonly sec */
942 if (sec_is_reverse(sec) || sec_is_rootonly(sec))
945 construct_key_desc(desc, sizeof(desc), sec, uid);
947 /* there should be only one valid key, but we put it in the
948 * loop in case of any weird cases */
950 key = request_key(&gss_key_type, desc, NULL);
952 CDEBUG(D_SEC, "No more key found for current user\n");
956 down_write(&key->sem);
958 kill_key_locked(key);
960 /* kill_key_locked() should usually revoke the key, but we
961 * revoke it again to make sure, e.g. some case the key may
962 * not well coupled with a context. */
963 key_revoke_locked(key);
967 request_key_unlink(key);
974 * flush context of root or all, we iterate through the list.
977 void flush_spec_ctx_cache_kr(struct ptlrpc_sec *sec, uid_t uid, int grace,
980 struct gss_sec_keyring *gsec_kr;
981 struct hlist_head freelist = HLIST_HEAD_INIT;
982 struct hlist_node *next;
983 struct ptlrpc_cli_ctx *ctx;
986 gsec_kr = sec2gsec_keyring(sec);
988 spin_lock(&sec->ps_lock);
989 hlist_for_each_entry_safe(ctx, next, &gsec_kr->gsk_clist,
991 LASSERT(atomic_read(&ctx->cc_refcount) > 0);
993 if (uid != -1 && uid != ctx->cc_vcred.vc_uid)
996 /* at this moment there's at least 2 base reference:
997 * key association and in-list. */
998 if (atomic_read(&ctx->cc_refcount) > 2) {
1001 CWARN("flush busy ctx %p(%u->%s, extra ref %d)\n",
1002 ctx, ctx->cc_vcred.vc_uid,
1003 sec2target_str(ctx->cc_sec),
1004 atomic_read(&ctx->cc_refcount) - 2);
1007 set_bit(PTLRPC_CTX_DEAD_BIT, &ctx->cc_flags);
1009 clear_bit(PTLRPC_CTX_UPTODATE_BIT, &ctx->cc_flags);
1011 atomic_inc(&ctx->cc_refcount);
1013 if (ctx_unlist_kr(ctx, 1)) {
1014 hlist_add_head(&ctx->cc_cache, &freelist);
1016 LASSERT(atomic_read(&ctx->cc_refcount) >= 2);
1017 atomic_dec(&ctx->cc_refcount);
1020 spin_unlock(&sec->ps_lock);
1022 dispose_ctx_list_kr(&freelist);
1027 int gss_sec_flush_ctx_cache_kr(struct ptlrpc_sec *sec,
1028 uid_t uid, int grace, int force)
1032 CDEBUG(D_SEC, "sec %p(%d, nctx %d), uid %d, grace %d, force %d\n",
1033 sec, atomic_read(&sec->ps_refcount),
1034 atomic_read(&sec->ps_nctx),
1037 if (uid != -1 && uid != 0)
1038 flush_user_ctx_cache_kr(sec, uid, grace, force);
1040 flush_spec_ctx_cache_kr(sec, uid, grace, force);
1046 void gss_sec_gc_ctx_kr(struct ptlrpc_sec *sec)
1048 struct gss_sec_keyring *gsec_kr = sec2gsec_keyring(sec);
1049 struct hlist_head freelist = HLIST_HEAD_INIT;
1050 struct hlist_node *next;
1051 struct ptlrpc_cli_ctx *ctx;
1054 CWARN("running gc\n");
1056 spin_lock(&sec->ps_lock);
1057 hlist_for_each_entry_safe(ctx, next, &gsec_kr->gsk_clist,
1059 LASSERT(atomic_read(&ctx->cc_refcount) > 0);
1061 atomic_inc(&ctx->cc_refcount);
1063 if (cli_ctx_check_death(ctx) && ctx_unlist_kr(ctx, 1)) {
1064 hlist_add_head(&ctx->cc_cache, &freelist);
1065 CWARN("unhashed ctx %p\n", ctx);
1067 LASSERT(atomic_read(&ctx->cc_refcount) >= 2);
1068 atomic_dec(&ctx->cc_refcount);
1071 spin_unlock(&sec->ps_lock);
1073 dispose_ctx_list_kr(&freelist);
1078 int gss_sec_display_kr(struct ptlrpc_sec *sec, struct seq_file *seq)
1080 struct gss_sec_keyring *gsec_kr = sec2gsec_keyring(sec);
1081 struct hlist_node *next;
1082 struct ptlrpc_cli_ctx *ctx;
1083 struct gss_cli_ctx *gctx;
1084 time64_t now = ktime_get_real_seconds();
1087 spin_lock(&sec->ps_lock);
1088 hlist_for_each_entry_safe(ctx, next, &gsec_kr->gsk_clist,
1094 gctx = ctx2gctx(ctx);
1095 key = ctx2gctx_keyring(ctx)->gck_key;
1097 gss_cli_ctx_flags2str(ctx->cc_flags,
1098 flags_str, sizeof(flags_str));
1100 if (gctx->gc_mechctx)
1101 lgss_display(gctx->gc_mechctx, mech, sizeof(mech));
1103 snprintf(mech, sizeof(mech), "N/A");
1104 mech[sizeof(mech) - 1] = '\0';
1107 "%p: uid %u, ref %d, expire %lld(%+lld), fl %s, seq %d, win %u, key %08x(ref %d), hdl %#llx:%#llx, mech: %s\n",
1108 ctx, ctx->cc_vcred.vc_uid,
1109 atomic_read(&ctx->cc_refcount),
1111 ctx->cc_expire ? ctx->cc_expire - now : 0,
1113 atomic_read(&gctx->gc_seq),
1115 key ? key->serial : 0,
1116 key ? ll_read_key_usage(key) : 0,
1117 gss_handle_to_u64(&gctx->gc_handle),
1118 gss_handle_to_u64(&gctx->gc_svc_handle),
1121 spin_unlock(&sec->ps_lock);
1126 /****************************************
1128 ****************************************/
1131 int gss_cli_ctx_refresh_kr(struct ptlrpc_cli_ctx *ctx)
1133 /* upcall is already on the way */
1138 int gss_cli_ctx_validate_kr(struct ptlrpc_cli_ctx *ctx)
1140 LASSERT(atomic_read(&ctx->cc_refcount) > 0);
1141 LASSERT(ctx->cc_sec);
1143 if (cli_ctx_check_death(ctx)) {
1148 if (cli_ctx_is_ready(ctx))
1154 void gss_cli_ctx_die_kr(struct ptlrpc_cli_ctx *ctx, int grace)
1156 LASSERT(atomic_read(&ctx->cc_refcount) > 0);
1157 LASSERT(ctx->cc_sec);
1159 cli_ctx_expire(ctx);
1163 /****************************************
1164 * (reverse) service *
1165 ****************************************/
1168 * reverse context could have nothing to do with keyrings. here we still keep
1169 * the version which bind to a key, for future reference.
1171 #define HAVE_REVERSE_CTX_NOKEY
1173 #ifdef HAVE_REVERSE_CTX_NOKEY
1176 int sec_install_rctx_kr(struct ptlrpc_sec *sec,
1177 struct ptlrpc_svc_ctx *svc_ctx)
1179 struct ptlrpc_cli_ctx *cli_ctx;
1180 struct vfs_cred vcred = { .vc_uid = 0 };
1186 cli_ctx = ctx_create_kr(sec, &vcred);
1187 if (cli_ctx == NULL)
1190 rc = gss_copy_rvc_cli_ctx(cli_ctx, svc_ctx);
1192 CERROR("failed copy reverse cli ctx: %d\n", rc);
1194 ctx_put_kr(cli_ctx, 1);
1198 rvs_sec_install_root_ctx_kr(sec, cli_ctx, NULL);
1200 ctx_put_kr(cli_ctx, 1);
1205 #else /* ! HAVE_REVERSE_CTX_NOKEY */
1208 int sec_install_rctx_kr(struct ptlrpc_sec *sec,
1209 struct ptlrpc_svc_ctx *svc_ctx)
1211 struct ptlrpc_cli_ctx *cli_ctx = NULL;
1213 struct vfs_cred vcred = { .vc_uid = 0 };
1221 construct_key_desc(desc, sizeof(desc), sec, 0);
1223 key = key_alloc(&gss_key_type, desc, 0, 0,
1224 KEY_POS_ALL | KEY_USR_ALL, 1);
1226 CERROR("failed to alloc key: %ld\n", PTR_ERR(key));
1227 return PTR_ERR(key);
1230 rc = key_instantiate_and_link(key, NULL, 0, NULL, NULL);
1232 CERROR("failed to instantiate key: %d\n", rc);
1236 down_write(&key->sem);
1238 LASSERT(!key_get_payload(key, 0));
1240 cli_ctx = ctx_create_kr(sec, &vcred);
1241 if (cli_ctx == NULL) {
1246 rc = gss_copy_rvc_cli_ctx(cli_ctx, svc_ctx);
1248 CERROR("failed copy reverse cli ctx: %d\n", rc);
1252 rvs_sec_install_root_ctx_kr(sec, cli_ctx, key);
1254 ctx_put_kr(cli_ctx, 1);
1255 up_write(&key->sem);
1264 ctx_put_kr(cli_ctx, 1);
1266 up_write(&key->sem);
1272 #endif /* HAVE_REVERSE_CTX_NOKEY */
1274 /****************************************
1276 ****************************************/
1279 int gss_svc_accept_kr(struct ptlrpc_request *req)
1281 return gss_svc_accept(&gss_policy_keyring, req);
1285 int gss_svc_install_rctx_kr(struct obd_import *imp,
1286 struct ptlrpc_svc_ctx *svc_ctx)
1288 struct ptlrpc_sec *sec;
1291 sec = sptlrpc_import_sec_ref(imp);
1294 rc = sec_install_rctx_kr(sec, svc_ctx);
1295 sptlrpc_sec_put(sec);
1300 /****************************************
1302 ****************************************/
1305 #ifdef HAVE_KEY_TYPE_INSTANTIATE_2ARGS
1306 int gss_kt_instantiate(struct key *key, struct key_preparsed_payload *prep)
1308 const void *data = prep->data;
1309 size_t datalen = prep->datalen;
1311 int gss_kt_instantiate(struct key *key, const void *data, size_t datalen)
1317 if (data != NULL || datalen != 0) {
1318 CERROR("invalid: data %p, len %lu\n", data, (long)datalen);
1322 if (key_get_payload(key, 0)) {
1323 CERROR("key already have payload\n");
1327 /* link the key to session keyring, so following context negotiation
1328 * rpc fired from user space could find this key. This will be unlinked
1329 * automatically when upcall processes die.
1331 * we can't do this through keyctl from userspace, because the upcall
1332 * might be neither possessor nor owner of the key (setuid).
1334 * the session keyring is created upon upcall, and don't change all
1335 * the way until upcall finished, so rcu lock is not needed here.
1337 LASSERT(current_cred()->session_keyring);
1340 rc = key_link(current_cred()->session_keyring, key);
1343 CERROR("failed to link key %08x to keyring %08x: %d\n",
1345 current_cred()->session_keyring->serial, rc);
1349 CDEBUG(D_SEC, "key %p instantiated, ctx %p\n", key,
1350 key_get_payload(key, 0));
1355 * called with key semaphore write locked. it means we can operate
1356 * on the context without fear of loosing refcount.
1359 #ifdef HAVE_KEY_TYPE_INSTANTIATE_2ARGS
1360 int gss_kt_update(struct key *key, struct key_preparsed_payload *prep)
1362 const void *data = prep->data;
1363 __u32 datalen32 = (__u32) prep->datalen;
1365 int gss_kt_update(struct key *key, const void *data, size_t datalen)
1367 __u32 datalen32 = (__u32) datalen;
1369 struct ptlrpc_cli_ctx *ctx = key_get_payload(key, 0);
1370 struct gss_cli_ctx *gctx;
1371 rawobj_t tmpobj = RAWOBJ_EMPTY;
1375 if (data == NULL || datalen32 == 0) {
1376 CWARN("invalid: data %p, len %lu\n", data, (long)datalen32);
1380 /* if upcall finished negotiation too fast (mostly likely because
1381 * of local error happened) and call kt_update(), the ctx
1382 * might be still NULL. but the key will finally be associate
1383 * with a context, or be revoked. if key status is fine, return
1384 * -EAGAIN to allow userspace sleep a while and call again. */
1386 CDEBUG(D_SEC, "update too soon: key %p(%x) flags %lx\n",
1387 key, key->serial, key->flags);
1389 rc = key_validate(key);
1396 LASSERT(atomic_read(&ctx->cc_refcount) > 0);
1397 LASSERT(ctx->cc_sec);
1399 ctx_clear_timer_kr(ctx);
1401 /* don't proceed if already refreshed */
1402 if (cli_ctx_is_refreshed(ctx)) {
1403 CWARN("ctx already done refresh\n");
1407 sptlrpc_cli_ctx_get(ctx);
1408 gctx = ctx2gctx(ctx);
1410 rc = buffer_extract_bytes(&data, &datalen32, &gctx->gc_win,
1411 sizeof(gctx->gc_win));
1413 CERROR("failed extract seq_win\n");
1417 if (gctx->gc_win == 0) {
1418 __u32 nego_rpc_err, nego_gss_err;
1420 rc = buffer_extract_bytes(&data, &datalen32, &nego_rpc_err,
1421 sizeof(nego_rpc_err));
1423 CERROR("cannot extract RPC: rc = %d\n", rc);
1427 rc = buffer_extract_bytes(&data, &datalen32, &nego_gss_err,
1428 sizeof(nego_gss_err));
1430 CERROR("failed to extract gss rc = %d\n", rc);
1434 CERROR("negotiation: rpc err %d, gss err %x\n",
1435 nego_rpc_err, nego_gss_err);
1437 rc = nego_rpc_err ? nego_rpc_err : -EACCES;
1439 rc = rawobj_extract_local_alloc(&gctx->gc_handle,
1440 (__u32 **) &data, &datalen32);
1442 CERROR("failed extract handle\n");
1446 rc = rawobj_extract_local(&tmpobj,
1447 (__u32 **) &data, &datalen32);
1449 CERROR("failed extract mech\n");
1453 rc = lgss_import_sec_context(&tmpobj,
1454 sec2gsec(ctx->cc_sec)->gs_mech,
1456 if (rc != GSS_S_COMPLETE)
1457 CERROR("failed import context\n");
1462 /* we don't care what current status of this ctx, even someone else
1463 * is operating on the ctx at the same time. we just add up our own
1466 gss_cli_ctx_uptodate(gctx);
1468 /* this will also revoke the key. has to be done before
1469 * wakeup waiters otherwise they can find the stale key */
1470 kill_key_locked(key);
1472 cli_ctx_expire(ctx);
1474 if (rc != -ERESTART)
1475 set_bit(PTLRPC_CTX_ERROR_BIT, &ctx->cc_flags);
1478 /* let user space think it's a success */
1479 sptlrpc_cli_ctx_put(ctx, 1);
1483 #ifndef HAVE_KEY_MATCH_DATA
1485 gss_kt_match(const struct key *key, const void *desc)
1487 return strcmp(key->description, (const char *) desc) == 0 &&
1488 !test_bit(KEY_FLAG_REVOKED, &key->flags);
1490 #else /* ! HAVE_KEY_MATCH_DATA */
1492 gss_kt_match(const struct key *key, const struct key_match_data *match_data)
1494 const char *desc = match_data->raw_data;
1496 return strcmp(key->description, desc) == 0 &&
1497 !test_bit(KEY_FLAG_REVOKED, &key->flags);
1501 * Preparse the match criterion.
1503 static int gss_kt_match_preparse(struct key_match_data *match_data)
1505 match_data->lookup_type = KEYRING_SEARCH_LOOKUP_DIRECT;
1506 match_data->cmp = gss_kt_match;
1509 #endif /* HAVE_KEY_MATCH_DATA */
1512 void gss_kt_destroy(struct key *key)
1515 LASSERT(!key_get_payload(key, 0));
1516 CDEBUG(D_SEC, "destroy key %p\n", key);
1521 void gss_kt_describe(const struct key *key, struct seq_file *s)
1523 if (key->description == NULL)
1524 seq_puts(s, "[null]");
1526 seq_puts(s, key->description);
1529 static struct key_type gss_key_type =
1533 .instantiate = gss_kt_instantiate,
1534 .update = gss_kt_update,
1535 #ifdef HAVE_KEY_MATCH_DATA
1536 .match_preparse = gss_kt_match_preparse,
1538 .match = gss_kt_match,
1540 .destroy = gss_kt_destroy,
1541 .describe = gss_kt_describe,
1544 /****************************************
1545 * lustre gss keyring policy *
1546 ****************************************/
1548 static struct ptlrpc_ctx_ops gss_keyring_ctxops = {
1549 .match = gss_cli_ctx_match,
1550 .refresh = gss_cli_ctx_refresh_kr,
1551 .validate = gss_cli_ctx_validate_kr,
1552 .die = gss_cli_ctx_die_kr,
1553 .sign = gss_cli_ctx_sign,
1554 .verify = gss_cli_ctx_verify,
1555 .seal = gss_cli_ctx_seal,
1556 .unseal = gss_cli_ctx_unseal,
1557 .wrap_bulk = gss_cli_ctx_wrap_bulk,
1558 .unwrap_bulk = gss_cli_ctx_unwrap_bulk,
1561 static struct ptlrpc_sec_cops gss_sec_keyring_cops = {
1562 .create_sec = gss_sec_create_kr,
1563 .destroy_sec = gss_sec_destroy_kr,
1564 .kill_sec = gss_sec_kill,
1565 .lookup_ctx = gss_sec_lookup_ctx_kr,
1566 .release_ctx = gss_sec_release_ctx_kr,
1567 .flush_ctx_cache = gss_sec_flush_ctx_cache_kr,
1568 .gc_ctx = gss_sec_gc_ctx_kr,
1569 .install_rctx = gss_sec_install_rctx,
1570 .alloc_reqbuf = gss_alloc_reqbuf,
1571 .free_reqbuf = gss_free_reqbuf,
1572 .alloc_repbuf = gss_alloc_repbuf,
1573 .free_repbuf = gss_free_repbuf,
1574 .enlarge_reqbuf = gss_enlarge_reqbuf,
1575 .display = gss_sec_display_kr,
1578 static struct ptlrpc_sec_sops gss_sec_keyring_sops = {
1579 .accept = gss_svc_accept_kr,
1580 .invalidate_ctx = gss_svc_invalidate_ctx,
1581 .alloc_rs = gss_svc_alloc_rs,
1582 .authorize = gss_svc_authorize,
1583 .free_rs = gss_svc_free_rs,
1584 .free_ctx = gss_svc_free_ctx,
1585 .prep_bulk = gss_svc_prep_bulk,
1586 .unwrap_bulk = gss_svc_unwrap_bulk,
1587 .wrap_bulk = gss_svc_wrap_bulk,
1588 .install_rctx = gss_svc_install_rctx_kr,
1591 static struct ptlrpc_sec_policy gss_policy_keyring = {
1592 .sp_owner = THIS_MODULE,
1593 .sp_name = "gss.keyring",
1594 .sp_policy = SPTLRPC_POLICY_GSS,
1595 .sp_cops = &gss_sec_keyring_cops,
1596 .sp_sops = &gss_sec_keyring_sops,
1600 int __init gss_init_keyring(void)
1604 rc = register_key_type(&gss_key_type);
1606 CERROR("failed to register keyring type: %d\n", rc);
1610 rc = sptlrpc_register_policy(&gss_policy_keyring);
1612 unregister_key_type(&gss_key_type);
1619 void __exit gss_exit_keyring(void)
1621 unregister_key_type(&gss_key_type);
1622 sptlrpc_unregister_policy(&gss_policy_keyring);