2 * Modifications for Lustre
4 * Copyright (c) 2007, 2010, Oracle and/or its affiliates. All rights reserved.
6 * Copyright (c) 2012, 2016, Intel Corporation.
8 * Author: Eric Mei <ericm@clusterfs.com>
12 * linux/net/sunrpc/auth_gss.c
14 * RPCSEC_GSS client authentication.
16 * Copyright (c) 2000 The Regents of the University of Michigan.
17 * All rights reserved.
19 * Dug Song <dugsong@monkey.org>
20 * Andy Adamson <andros@umich.edu>
22 * Redistribution and use in source and binary forms, with or without
23 * modification, are permitted provided that the following conditions
26 * 1. Redistributions of source code must retain the above copyright
27 * notice, this list of conditions and the following disclaimer.
28 * 2. Redistributions in binary form must reproduce the above copyright
29 * notice, this list of conditions and the following disclaimer in the
30 * documentation and/or other materials provided with the distribution.
31 * 3. Neither the name of the University nor the names of its
32 * contributors may be used to endorse or promote products derived
33 * from this software without specific prior written permission.
35 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED
36 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
37 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
38 * DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
39 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
40 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
41 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
42 * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
43 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
44 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
45 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
49 #define DEBUG_SUBSYSTEM S_SEC
50 #include <linux/init.h>
51 #include <linux/module.h>
52 #include <linux/slab.h>
53 #include <linux/dcache.h>
55 #include <linux/mutex.h>
56 #include <linux/crypto.h>
57 #include <asm/atomic.h>
58 struct rpc_clnt; /* for rpc_pipefs */
59 #include <linux/sunrpc/rpc_pipe_fs.h>
61 #include <libcfs/linux/linux-list.h>
63 #include <obd_class.h>
64 #include <obd_support.h>
65 #include <uapi/linux/lustre/lustre_idl.h>
66 #include <lustre_sec.h>
67 #include <lustre_net.h>
68 #include <lustre_import.h>
71 #include "gss_internal.h"
74 static struct ptlrpc_sec_policy gss_policy_pipefs;
75 static struct ptlrpc_ctx_ops gss_pipefs_ctxops;
77 static int gss_cli_ctx_refresh_pf(struct ptlrpc_cli_ctx *ctx);
79 static int gss_sec_pipe_upcall_init(struct gss_sec *gsec)
84 static void gss_sec_pipe_upcall_fini(struct gss_sec *gsec)
88 /****************************************
89 * internal context helpers *
90 ****************************************/
93 struct ptlrpc_cli_ctx *ctx_create_pf(struct ptlrpc_sec *sec,
94 struct vfs_cred *vcred)
96 struct gss_cli_ctx *gctx;
103 rc = gss_cli_ctx_init_common(sec, &gctx->gc_base,
104 &gss_pipefs_ctxops, vcred);
110 return &gctx->gc_base;
114 void ctx_destroy_pf(struct ptlrpc_sec *sec, struct ptlrpc_cli_ctx *ctx)
116 struct gss_cli_ctx *gctx = ctx2gctx(ctx);
118 if (gss_cli_ctx_fini_common(sec, ctx))
123 atomic_dec(&sec->ps_nctx);
124 sptlrpc_sec_put(sec);
128 void ctx_enhash_pf(struct ptlrpc_cli_ctx *ctx, struct hlist_head *hash)
130 set_bit(PTLRPC_CTX_CACHED_BIT, &ctx->cc_flags);
131 atomic_inc(&ctx->cc_refcount);
132 hlist_add_head(&ctx->cc_cache, hash);
136 * caller must hold spinlock
139 void ctx_unhash_pf(struct ptlrpc_cli_ctx *ctx, struct hlist_head *freelist)
141 assert_spin_locked(&ctx->cc_sec->ps_lock);
142 LASSERT(atomic_read(&ctx->cc_refcount) > 0);
143 LASSERT(test_bit(PTLRPC_CTX_CACHED_BIT, &ctx->cc_flags));
144 LASSERT(!hlist_unhashed(&ctx->cc_cache));
146 clear_bit(PTLRPC_CTX_CACHED_BIT, &ctx->cc_flags);
148 if (atomic_dec_and_test(&ctx->cc_refcount)) {
149 __hlist_del(&ctx->cc_cache);
150 hlist_add_head(&ctx->cc_cache, freelist);
152 hlist_del_init(&ctx->cc_cache);
157 * return 1 if the context is dead.
160 int ctx_check_death_pf(struct ptlrpc_cli_ctx *ctx,
161 struct hlist_head *freelist)
163 if (cli_ctx_check_death(ctx)) {
165 ctx_unhash_pf(ctx, freelist);
173 int ctx_check_death_locked_pf(struct ptlrpc_cli_ctx *ctx,
174 struct hlist_head *freelist)
176 LASSERT(ctx->cc_sec);
177 LASSERT(atomic_read(&ctx->cc_refcount) > 0);
178 LASSERT(test_bit(PTLRPC_CTX_CACHED_BIT, &ctx->cc_flags));
180 return ctx_check_death_pf(ctx, freelist);
184 int ctx_match_pf(struct ptlrpc_cli_ctx *ctx, struct vfs_cred *vcred)
186 /* a little bit optimization for null policy */
187 if (!ctx->cc_ops->match)
190 return ctx->cc_ops->match(ctx, vcred);
194 void ctx_list_destroy_pf(struct hlist_head *head)
196 struct ptlrpc_cli_ctx *ctx;
198 while (!hlist_empty(head)) {
199 ctx = cfs_hlist_entry(head->first, struct ptlrpc_cli_ctx,
202 LASSERT(atomic_read(&ctx->cc_refcount) == 0);
203 LASSERT(test_bit(PTLRPC_CTX_CACHED_BIT,
204 &ctx->cc_flags) == 0);
206 hlist_del_init(&ctx->cc_cache);
207 ctx_destroy_pf(ctx->cc_sec, ctx);
211 /****************************************
213 ****************************************/
216 int gss_cli_ctx_validate_pf(struct ptlrpc_cli_ctx *ctx)
218 if (ctx_check_death_pf(ctx, NULL))
220 if (cli_ctx_is_ready(ctx))
226 void gss_cli_ctx_die_pf(struct ptlrpc_cli_ctx *ctx, int grace)
228 LASSERT(ctx->cc_sec);
229 LASSERT(atomic_read(&ctx->cc_refcount) > 0);
233 spin_lock(&ctx->cc_sec->ps_lock);
235 if (test_and_clear_bit(PTLRPC_CTX_CACHED_BIT, &ctx->cc_flags)) {
236 LASSERT(!hlist_unhashed(&ctx->cc_cache));
237 LASSERT(atomic_read(&ctx->cc_refcount) > 1);
239 hlist_del_init(&ctx->cc_cache);
240 if (atomic_dec_and_test(&ctx->cc_refcount))
244 spin_unlock(&ctx->cc_sec->ps_lock);
247 /****************************************
248 * reverse context installation *
249 ****************************************/
252 unsigned int ctx_hash_index(int hashsize, __u64 key)
254 return (unsigned int) (key & ((__u64) hashsize - 1));
258 void gss_sec_ctx_replace_pf(struct gss_sec *gsec,
259 struct ptlrpc_cli_ctx *new)
261 struct hlist_node __maybe_unused *pos, *next;
262 struct gss_sec_pipefs *gsec_pf;
263 struct ptlrpc_cli_ctx *ctx;
264 HLIST_HEAD(freelist);
268 gsec_pf = container_of(gsec, struct gss_sec_pipefs, gsp_base);
270 hash = ctx_hash_index(gsec_pf->gsp_chash_size,
271 (__u64) new->cc_vcred.vc_uid);
272 LASSERT(hash < gsec_pf->gsp_chash_size);
274 spin_lock(&gsec->gs_base.ps_lock);
276 cfs_hlist_for_each_entry_safe(ctx, pos, next,
277 &gsec_pf->gsp_chash[hash], cc_cache) {
278 if (!ctx_match_pf(ctx, &new->cc_vcred))
282 ctx_unhash_pf(ctx, &freelist);
286 ctx_enhash_pf(new, &gsec_pf->gsp_chash[hash]);
288 spin_unlock(&gsec->gs_base.ps_lock);
290 ctx_list_destroy_pf(&freelist);
295 int gss_install_rvs_cli_ctx_pf(struct gss_sec *gsec,
296 struct ptlrpc_svc_ctx *svc_ctx)
298 struct vfs_cred vcred;
299 struct ptlrpc_cli_ctx *cli_ctx;
306 cli_ctx = ctx_create_pf(&gsec->gs_base, &vcred);
310 rc = gss_copy_rvc_cli_ctx(cli_ctx, svc_ctx);
312 ctx_destroy_pf(cli_ctx->cc_sec, cli_ctx);
316 gss_sec_ctx_replace_pf(gsec, cli_ctx);
321 void gss_ctx_cache_gc_pf(struct gss_sec_pipefs *gsec_pf,
322 struct hlist_head *freelist)
324 struct ptlrpc_sec *sec;
325 struct ptlrpc_cli_ctx *ctx;
326 struct hlist_node __maybe_unused *pos;
327 struct hlist_node *next;
331 sec = &gsec_pf->gsp_base.gs_base;
333 CDEBUG(D_SEC, "do gc on sec %s@%p\n", sec->ps_policy->sp_name, sec);
335 for (i = 0; i < gsec_pf->gsp_chash_size; i++) {
336 cfs_hlist_for_each_entry_safe(ctx, pos, next,
337 &gsec_pf->gsp_chash[i], cc_cache)
338 ctx_check_death_locked_pf(ctx, freelist);
341 sec->ps_gc_next = ktime_get_real_seconds() + sec->ps_gc_interval;
346 struct ptlrpc_sec* gss_sec_create_pf(struct obd_import *imp,
347 struct ptlrpc_svc_ctx *ctx,
348 struct sptlrpc_flavor *sf)
350 struct gss_sec_pipefs *gsec_pf;
351 int alloc_size, hash_size, i;
354 #define GSS_SEC_PIPEFS_CTX_HASH_SIZE (32)
357 sf->sf_flags & (PTLRPC_SEC_FL_ROOTONLY | PTLRPC_SEC_FL_REVERSE))
360 hash_size = GSS_SEC_PIPEFS_CTX_HASH_SIZE;
362 alloc_size = sizeof(*gsec_pf) +
363 sizeof(struct hlist_head) * hash_size;
365 OBD_ALLOC(gsec_pf, alloc_size);
369 gsec_pf->gsp_chash_size = hash_size;
370 for (i = 0; i < hash_size; i++)
371 INIT_HLIST_HEAD(&gsec_pf->gsp_chash[i]);
373 if (gss_sec_create_common(&gsec_pf->gsp_base, &gss_policy_pipefs,
378 if (gss_sec_pipe_upcall_init(&gsec_pf->gsp_base))
381 if (gss_install_rvs_cli_ctx_pf(&gsec_pf->gsp_base, ctx))
385 RETURN(&gsec_pf->gsp_base.gs_base);
388 gss_sec_destroy_common(&gsec_pf->gsp_base);
390 OBD_FREE(gsec_pf, alloc_size);
395 void gss_sec_destroy_pf(struct ptlrpc_sec *sec)
397 struct gss_sec_pipefs *gsec_pf;
398 struct gss_sec *gsec;
400 CWARN("destroy %s@%p\n", sec->ps_policy->sp_name, sec);
402 gsec = container_of(sec, struct gss_sec, gs_base);
403 gsec_pf = container_of(gsec, struct gss_sec_pipefs, gsp_base);
405 LASSERT(gsec_pf->gsp_chash);
406 LASSERT(gsec_pf->gsp_chash_size);
408 gss_sec_pipe_upcall_fini(gsec);
410 gss_sec_destroy_common(gsec);
412 OBD_FREE(gsec, sizeof(*gsec_pf) +
413 sizeof(struct hlist_head) * gsec_pf->gsp_chash_size);
417 struct ptlrpc_cli_ctx * gss_sec_lookup_ctx_pf(struct ptlrpc_sec *sec,
418 struct vfs_cred *vcred,
419 int create, int remove_dead)
421 struct gss_sec *gsec;
422 struct gss_sec_pipefs *gsec_pf;
423 struct ptlrpc_cli_ctx *ctx = NULL, *new = NULL;
424 struct hlist_head *hash_head;
425 struct hlist_node __maybe_unused *pos, *next;
426 unsigned int hash, gc = 0, found = 0;
427 HLIST_HEAD(freelist);
432 gsec = container_of(sec, struct gss_sec, gs_base);
433 gsec_pf = container_of(gsec, struct gss_sec_pipefs, gsp_base);
435 hash = ctx_hash_index(gsec_pf->gsp_chash_size,
436 (__u64) vcred->vc_uid);
437 hash_head = &gsec_pf->gsp_chash[hash];
438 LASSERT(hash < gsec_pf->gsp_chash_size);
441 spin_lock(&sec->ps_lock);
443 /* gc_next == 0 means never do gc */
444 if (remove_dead && sec->ps_gc_next &&
445 (ktime_get_real_seconds() > sec->ps_gc_next)) {
446 gss_ctx_cache_gc_pf(gsec_pf, &freelist);
450 cfs_hlist_for_each_entry_safe(ctx, pos, next, hash_head, cc_cache) {
452 ctx_check_death_locked_pf(ctx,
453 remove_dead ? &freelist : NULL))
456 if (ctx_match_pf(ctx, vcred)) {
463 if (new && new != ctx) {
464 /* lost the race, just free it */
465 hlist_add_head(&new->cc_cache, &freelist);
469 /* hot node, move to head */
470 if (hash_head->first != &ctx->cc_cache) {
471 __hlist_del(&ctx->cc_cache);
472 hlist_add_head(&ctx->cc_cache, hash_head);
475 /* don't allocate for reverse sec */
476 if (sec_is_reverse(sec)) {
477 spin_unlock(&sec->ps_lock);
482 ctx_enhash_pf(new, hash_head);
485 spin_unlock(&sec->ps_lock);
486 new = ctx_create_pf(sec, vcred);
488 clear_bit(PTLRPC_CTX_NEW_BIT, &new->cc_flags);
498 atomic_inc(&ctx->cc_refcount);
500 spin_unlock(&sec->ps_lock);
502 /* the allocator of the context must give the first push to refresh */
505 gss_cli_ctx_refresh_pf(new);
508 ctx_list_destroy_pf(&freelist);
513 void gss_sec_release_ctx_pf(struct ptlrpc_sec *sec,
514 struct ptlrpc_cli_ctx *ctx,
517 LASSERT(test_bit(PTLRPC_CTX_CACHED_BIT, &ctx->cc_flags) == 0);
518 LASSERT(hlist_unhashed(&ctx->cc_cache));
520 /* if required async, we must clear the UPTODATE bit to prevent extra
521 * rpcs during destroy procedure. */
523 clear_bit(PTLRPC_CTX_UPTODATE_BIT, &ctx->cc_flags);
525 /* destroy this context */
526 ctx_destroy_pf(sec, ctx);
530 * @uid: which user. "-1" means flush all.
531 * @grace: mark context DEAD, allow graceful destroy like notify
533 * @force: also flush busy entries.
535 * return the number of busy context encountered.
537 * In any cases, never touch "eternal" contexts.
540 int gss_sec_flush_ctx_cache_pf(struct ptlrpc_sec *sec,
542 int grace, int force)
544 struct gss_sec *gsec;
545 struct gss_sec_pipefs *gsec_pf;
546 struct ptlrpc_cli_ctx *ctx;
547 struct hlist_node __maybe_unused *pos, *next;
548 HLIST_HEAD(freelist);
552 might_sleep_if(grace);
554 gsec = container_of(sec, struct gss_sec, gs_base);
555 gsec_pf = container_of(gsec, struct gss_sec_pipefs, gsp_base);
557 spin_lock(&sec->ps_lock);
558 for (i = 0; i < gsec_pf->gsp_chash_size; i++) {
559 cfs_hlist_for_each_entry_safe(ctx, pos, next,
560 &gsec_pf->gsp_chash[i],
562 LASSERT(atomic_read(&ctx->cc_refcount) > 0);
564 if (uid != -1 && uid != ctx->cc_vcred.vc_uid)
567 if (atomic_read(&ctx->cc_refcount) > 1) {
572 CWARN("flush busy(%d) ctx %p(%u->%s) by force, "
574 atomic_read(&ctx->cc_refcount),
575 ctx, ctx->cc_vcred.vc_uid,
576 sec2target_str(ctx->cc_sec), grace);
578 ctx_unhash_pf(ctx, &freelist);
580 set_bit(PTLRPC_CTX_DEAD_BIT, &ctx->cc_flags);
582 clear_bit(PTLRPC_CTX_UPTODATE_BIT,
586 spin_unlock(&sec->ps_lock);
588 ctx_list_destroy_pf(&freelist);
592 /****************************************
594 ****************************************/
597 int gss_svc_accept_pf(struct ptlrpc_request *req)
599 return gss_svc_accept(&gss_policy_pipefs, req);
603 int gss_svc_install_rctx_pf(struct obd_import *imp,
604 struct ptlrpc_svc_ctx *ctx)
606 struct ptlrpc_sec *sec;
609 sec = sptlrpc_import_sec_ref(imp);
611 rc = gss_install_rvs_cli_ctx_pf(sec2gsec(sec), ctx);
613 sptlrpc_sec_put(sec);
617 /****************************************
618 * rpc_pipefs definitions *
619 ****************************************/
621 #define LUSTRE_PIPE_ROOT "/lustre"
622 #define LUSTRE_PIPE_KRB5 LUSTRE_PIPE_ROOT"/krb5"
624 struct gss_upcall_msg_data {
628 __u32 gum_svc; /* MDS/OSS... */
629 __u64 gum_nid; /* peer NID */
630 __u8 gum_obd[64]; /* client obd name */
633 struct gss_upcall_msg {
634 struct rpc_pipe_msg gum_base;
635 atomic_t gum_refcount;
636 struct list_head gum_list;
638 struct gss_sec *gum_gsec;
639 struct gss_cli_ctx *gum_gctx;
640 struct gss_upcall_msg_data gum_data;
643 static atomic_t upcall_seq = ATOMIC_INIT(0);
646 __u32 upcall_get_sequence(void)
648 return (__u32) atomic_inc_return(&upcall_seq);
657 __u32 mech_name2idx(const char *name)
659 LASSERT(!strcmp(name, "krb5"));
663 /* pipefs dentries for each mechanisms */
664 static struct dentry *de_pipes[MECH_MAX] = { NULL, };
665 /* all upcall messgaes linked here */
666 static struct list_head upcall_lists[MECH_MAX];
667 /* and protected by this */
668 static spinlock_t upcall_locks[MECH_MAX];
671 void upcall_list_lock(int idx)
673 spin_lock(&upcall_locks[idx]);
677 void upcall_list_unlock(int idx)
679 spin_unlock(&upcall_locks[idx]);
683 void upcall_msg_enlist(struct gss_upcall_msg *msg)
685 __u32 idx = msg->gum_mechidx;
687 upcall_list_lock(idx);
688 list_add(&msg->gum_list, &upcall_lists[idx]);
689 upcall_list_unlock(idx);
693 void upcall_msg_delist(struct gss_upcall_msg *msg)
695 __u32 idx = msg->gum_mechidx;
697 upcall_list_lock(idx);
698 list_del_init(&msg->gum_list);
699 upcall_list_unlock(idx);
702 /****************************************
703 * rpc_pipefs upcall helpers *
704 ****************************************/
707 void gss_release_msg(struct gss_upcall_msg *gmsg)
710 LASSERT(atomic_read(&gmsg->gum_refcount) > 0);
712 if (!atomic_dec_and_test(&gmsg->gum_refcount)) {
717 if (gmsg->gum_gctx) {
718 sptlrpc_cli_ctx_wakeup(&gmsg->gum_gctx->gc_base);
719 sptlrpc_cli_ctx_put(&gmsg->gum_gctx->gc_base, 1);
720 gmsg->gum_gctx = NULL;
723 LASSERT(list_empty(&gmsg->gum_list));
724 LASSERT(list_empty(&gmsg->gum_base.list));
730 void gss_unhash_msg_nolock(struct gss_upcall_msg *gmsg)
732 __u32 idx = gmsg->gum_mechidx;
734 LASSERT(idx < MECH_MAX);
735 assert_spin_locked(&upcall_locks[idx]);
737 if (list_empty(&gmsg->gum_list))
740 list_del_init(&gmsg->gum_list);
741 LASSERT(atomic_read(&gmsg->gum_refcount) > 1);
742 atomic_dec(&gmsg->gum_refcount);
746 void gss_unhash_msg(struct gss_upcall_msg *gmsg)
748 __u32 idx = gmsg->gum_mechidx;
750 LASSERT(idx < MECH_MAX);
751 upcall_list_lock(idx);
752 gss_unhash_msg_nolock(gmsg);
753 upcall_list_unlock(idx);
757 void gss_msg_fail_ctx(struct gss_upcall_msg *gmsg)
759 if (gmsg->gum_gctx) {
760 struct ptlrpc_cli_ctx *ctx = &gmsg->gum_gctx->gc_base;
762 LASSERT(atomic_read(&ctx->cc_refcount) > 0);
763 sptlrpc_cli_ctx_expire(ctx);
764 set_bit(PTLRPC_CTX_ERROR_BIT, &ctx->cc_flags);
769 struct gss_upcall_msg * gss_find_upcall(__u32 mechidx, __u32 seq)
771 struct gss_upcall_msg *gmsg;
773 upcall_list_lock(mechidx);
774 list_for_each_entry(gmsg, &upcall_lists[mechidx], gum_list) {
775 if (gmsg->gum_data.gum_seq != seq)
778 LASSERT(atomic_read(&gmsg->gum_refcount) > 0);
779 LASSERT(gmsg->gum_mechidx == mechidx);
781 atomic_inc(&gmsg->gum_refcount);
782 upcall_list_unlock(mechidx);
785 upcall_list_unlock(mechidx);
790 int simple_get_bytes(char **buf, __u32 *buflen, void *res, __u32 reslen)
792 if (*buflen < reslen) {
793 CERROR("shorter buflen than needed: %u < %u\n",
798 memcpy(res, *buf, reslen);
804 /****************************************
806 ****************************************/
809 ssize_t gss_pipe_upcall(struct file *filp, struct rpc_pipe_msg *msg,
810 char *dst, size_t buflen)
812 char *data = (char *)msg->data + msg->copied;
813 ssize_t mlen = msg->len;
819 left = copy_to_user(dst, data, mlen);
831 ssize_t gss_pipe_downcall(struct file *filp, const char *src, size_t mlen)
833 struct rpc_inode *rpci = RPC_I(file_inode(filp));
834 struct gss_upcall_msg *gss_msg;
835 struct ptlrpc_cli_ctx *ctx;
836 struct gss_cli_ctx *gctx = NULL;
840 __u32 mechidx, seq, gss_err;
843 mechidx = (__u32) (long) rpci->private;
844 LASSERT(mechidx < MECH_MAX);
846 OBD_ALLOC(buf, mlen);
850 if (copy_from_user(buf, src, mlen)) {
851 CERROR("failed copy user space data\n");
852 GOTO(out_free, rc = -EFAULT);
857 /* data passed down format:
861 * - wire_ctx (rawobj)
862 * - mech_ctx (rawobj)
864 if (simple_get_bytes(&data, &datalen, &seq, sizeof(seq))) {
865 CERROR("fail to get seq\n");
866 GOTO(out_free, rc = -EFAULT);
869 gss_msg = gss_find_upcall(mechidx, seq);
871 CERROR("upcall %u has aborted earlier\n", seq);
872 GOTO(out_free, rc = -EINVAL);
875 gss_unhash_msg(gss_msg);
876 gctx = gss_msg->gum_gctx;
878 LASSERT(atomic_read(&gctx->gc_base.cc_refcount) > 0);
880 /* timeout is not in use for now */
881 if (simple_get_bytes(&data, &datalen, &timeout, sizeof(timeout)))
882 GOTO(out_msg, rc = -EFAULT);
884 /* lgssd signal an error by gc_win == 0 */
885 if (simple_get_bytes(&data, &datalen, &gctx->gc_win,
886 sizeof(gctx->gc_win)))
887 GOTO(out_msg, rc = -EFAULT);
889 if (gctx->gc_win == 0) {
894 if (simple_get_bytes(&data, &datalen, &rc, sizeof(rc)))
895 GOTO(out_msg, rc = -EFAULT);
896 if (simple_get_bytes(&data, &datalen, &gss_err,sizeof(gss_err)))
897 GOTO(out_msg, rc = -EFAULT);
899 if (rc == 0 && gss_err == GSS_S_COMPLETE) {
900 CWARN("both rpc & gss error code not set\n");
907 if (rawobj_extract_local(&tmpobj, (__u32 **) &data, &datalen))
908 GOTO(out_msg, rc = -EFAULT);
909 if (rawobj_dup(&gctx->gc_handle, &tmpobj))
910 GOTO(out_msg, rc = -ENOMEM);
913 if (rawobj_extract_local(&tmpobj, (__u32 **) &data, &datalen))
914 GOTO(out_msg, rc = -EFAULT);
915 gss_err = lgss_import_sec_context(&tmpobj,
916 gss_msg->gum_gsec->gs_mech,
921 if (likely(rc == 0 && gss_err == GSS_S_COMPLETE)) {
922 gss_cli_ctx_uptodate(gctx);
924 ctx = &gctx->gc_base;
925 sptlrpc_cli_ctx_expire(ctx);
926 if (rc != -ERESTART || gss_err != GSS_S_COMPLETE)
927 set_bit(PTLRPC_CTX_ERROR_BIT, &ctx->cc_flags);
929 CERROR("refresh ctx %p(uid %d) failed: %d/0x%08x: %s\n",
930 ctx, ctx->cc_vcred.vc_uid, rc, gss_err,
931 test_bit(PTLRPC_CTX_ERROR_BIT, &ctx->cc_flags) ?
932 "fatal error" : "non-fatal");
938 gss_release_msg(gss_msg);
943 * hack pipefs: always return asked length unless all following
944 * downcalls might be messed up. */
950 void gss_pipe_destroy_msg(struct rpc_pipe_msg *msg)
952 struct gss_upcall_msg *gmsg;
953 struct gss_upcall_msg_data *gumd;
954 static time64_t ratelimit;
957 LASSERT(list_empty(&msg->list));
959 /* normally errno is >= 0 */
960 if (msg->errno >= 0) {
965 gmsg = container_of(msg, struct gss_upcall_msg, gum_base);
966 gumd = &gmsg->gum_data;
967 LASSERT(atomic_read(&gmsg->gum_refcount) > 0);
969 CERROR("failed msg %p (seq %u, uid %u, svc %u, nid %#llx, obd %.*s): "
970 "errno %d\n", msg, gumd->gum_seq, gumd->gum_uid, gumd->gum_svc,
971 gumd->gum_nid, (int) sizeof(gumd->gum_obd),
972 gumd->gum_obd, msg->errno);
974 atomic_inc(&gmsg->gum_refcount);
975 gss_unhash_msg(gmsg);
976 if (msg->errno == -ETIMEDOUT || msg->errno == -EPIPE) {
977 time64_t now = ktime_get_real_seconds();
979 if (now > ratelimit) {
980 CWARN("upcall timed out, is lgssd running?\n");
981 ratelimit = now + 15;
984 gss_msg_fail_ctx(gmsg);
985 gss_release_msg(gmsg);
990 void gss_pipe_release(struct inode *inode)
992 struct rpc_inode *rpci = RPC_I(inode);
996 idx = (__u32) (long) rpci->private;
997 LASSERT(idx < MECH_MAX);
999 upcall_list_lock(idx);
1000 while (!list_empty(&upcall_lists[idx])) {
1001 struct gss_upcall_msg *gmsg;
1002 struct gss_upcall_msg_data *gumd;
1004 gmsg = list_entry(upcall_lists[idx].next,
1005 struct gss_upcall_msg, gum_list);
1006 gumd = &gmsg->gum_data;
1007 LASSERT(list_empty(&gmsg->gum_base.list));
1009 CERROR("failing remaining msg %p:seq %u, uid %u, svc %u, "
1010 "nid %#llx, obd %.*s\n", gmsg,
1011 gumd->gum_seq, gumd->gum_uid, gumd->gum_svc,
1012 gumd->gum_nid, (int) sizeof(gumd->gum_obd),
1015 gmsg->gum_base.errno = -EPIPE;
1016 atomic_inc(&gmsg->gum_refcount);
1017 gss_unhash_msg_nolock(gmsg);
1019 gss_msg_fail_ctx(gmsg);
1021 upcall_list_unlock(idx);
1022 gss_release_msg(gmsg);
1023 upcall_list_lock(idx);
1025 upcall_list_unlock(idx);
1029 static struct rpc_pipe_ops gss_upcall_ops = {
1030 .upcall = gss_pipe_upcall,
1031 .downcall = gss_pipe_downcall,
1032 .destroy_msg = gss_pipe_destroy_msg,
1033 .release_pipe = gss_pipe_release,
1036 /****************************************
1037 * upcall helper functions *
1038 ****************************************/
1041 int gss_ctx_refresh_pf(struct ptlrpc_cli_ctx *ctx)
1043 struct obd_import *imp;
1044 struct gss_sec *gsec;
1045 struct gss_upcall_msg *gmsg;
1051 LASSERT(ctx->cc_sec);
1052 LASSERT(ctx->cc_sec->ps_import);
1053 LASSERT(ctx->cc_sec->ps_import->imp_obd);
1055 imp = ctx->cc_sec->ps_import;
1056 if (!imp->imp_connection) {
1057 CERROR("import has no connection set\n");
1061 gsec = container_of(ctx->cc_sec, struct gss_sec, gs_base);
1063 OBD_ALLOC_PTR(gmsg);
1067 /* initialize pipefs base msg */
1068 INIT_LIST_HEAD(&gmsg->gum_base.list);
1069 gmsg->gum_base.data = &gmsg->gum_data;
1070 gmsg->gum_base.len = sizeof(gmsg->gum_data);
1071 gmsg->gum_base.copied = 0;
1072 gmsg->gum_base.errno = 0;
1074 /* init upcall msg */
1075 atomic_set(&gmsg->gum_refcount, 1);
1076 gmsg->gum_mechidx = mech_name2idx(gsec->gs_mech->gm_name);
1077 gmsg->gum_gsec = gsec;
1078 gmsg->gum_gctx = container_of(sptlrpc_cli_ctx_get(ctx),
1079 struct gss_cli_ctx, gc_base);
1080 gmsg->gum_data.gum_seq = upcall_get_sequence();
1081 gmsg->gum_data.gum_uid = ctx->cc_vcred.vc_uid;
1082 gmsg->gum_data.gum_gid = 0; /* not used for now */
1083 gmsg->gum_data.gum_svc = import_to_gss_svc(imp);
1084 gmsg->gum_data.gum_nid = imp->imp_connection->c_peer.nid;
1085 strlcpy(gmsg->gum_data.gum_obd, imp->imp_obd->obd_name,
1086 sizeof(gmsg->gum_data.gum_obd));
1088 /* This only could happen when sysadmin set it dead/expired
1089 * using lctl by force. */
1090 if (ctx->cc_flags & PTLRPC_CTX_STATUS_MASK) {
1091 CWARN("ctx %p(%u->%s) was set flags %lx unexpectedly\n",
1092 ctx, ctx->cc_vcred.vc_uid, sec2target_str(ctx->cc_sec),
1095 LASSERT(!(ctx->cc_flags & PTLRPC_CTX_UPTODATE));
1096 ctx->cc_flags |= PTLRPC_CTX_DEAD | PTLRPC_CTX_ERROR;
1102 upcall_msg_enlist(gmsg);
1104 rc = rpc_queue_upcall(de_pipes[gmsg->gum_mechidx]->d_inode,
1107 CERROR("rpc_queue_upcall failed: %d\n", rc);
1109 upcall_msg_delist(gmsg);
1120 int gss_cli_ctx_refresh_pf(struct ptlrpc_cli_ctx *ctx)
1122 /* if we are refreshing for root, also update the reverse
1123 * handle index, do not confuse reverse contexts. */
1124 if (ctx->cc_vcred.vc_uid == 0) {
1125 struct gss_sec *gsec;
1127 gsec = container_of(ctx->cc_sec, struct gss_sec, gs_base);
1128 gsec->gs_rvs_hdl = gss_get_next_ctx_index();
1131 return gss_ctx_refresh_pf(ctx);
1134 /****************************************
1135 * lustre gss pipefs policy *
1136 ****************************************/
1138 static struct ptlrpc_ctx_ops gss_pipefs_ctxops = {
1139 .match = gss_cli_ctx_match,
1140 .refresh = gss_cli_ctx_refresh_pf,
1141 .validate = gss_cli_ctx_validate_pf,
1142 .die = gss_cli_ctx_die_pf,
1143 .sign = gss_cli_ctx_sign,
1144 .verify = gss_cli_ctx_verify,
1145 .seal = gss_cli_ctx_seal,
1146 .unseal = gss_cli_ctx_unseal,
1147 .wrap_bulk = gss_cli_ctx_wrap_bulk,
1148 .unwrap_bulk = gss_cli_ctx_unwrap_bulk,
1151 static struct ptlrpc_sec_cops gss_sec_pipefs_cops = {
1152 .create_sec = gss_sec_create_pf,
1153 .destroy_sec = gss_sec_destroy_pf,
1154 .kill_sec = gss_sec_kill,
1155 .lookup_ctx = gss_sec_lookup_ctx_pf,
1156 .release_ctx = gss_sec_release_ctx_pf,
1157 .flush_ctx_cache = gss_sec_flush_ctx_cache_pf,
1158 .install_rctx = gss_sec_install_rctx,
1159 .alloc_reqbuf = gss_alloc_reqbuf,
1160 .free_reqbuf = gss_free_reqbuf,
1161 .alloc_repbuf = gss_alloc_repbuf,
1162 .free_repbuf = gss_free_repbuf,
1163 .enlarge_reqbuf = gss_enlarge_reqbuf,
1166 static struct ptlrpc_sec_sops gss_sec_pipefs_sops = {
1167 .accept = gss_svc_accept_pf,
1168 .invalidate_ctx = gss_svc_invalidate_ctx,
1169 .alloc_rs = gss_svc_alloc_rs,
1170 .authorize = gss_svc_authorize,
1171 .free_rs = gss_svc_free_rs,
1172 .free_ctx = gss_svc_free_ctx,
1173 .unwrap_bulk = gss_svc_unwrap_bulk,
1174 .wrap_bulk = gss_svc_wrap_bulk,
1175 .install_rctx = gss_svc_install_rctx_pf,
1178 static struct ptlrpc_sec_policy gss_policy_pipefs = {
1179 .sp_owner = THIS_MODULE,
1180 .sp_name = "gss.pipefs",
1181 .sp_policy = SPTLRPC_POLICY_GSS_PIPEFS,
1182 .sp_cops = &gss_sec_pipefs_cops,
1183 .sp_sops = &gss_sec_pipefs_sops,
1187 int __init gss_init_pipefs_upcall(void)
1192 de = rpc_mkdir(LUSTRE_PIPE_ROOT, NULL);
1193 if (IS_ERR(de) && PTR_ERR(de) != -EEXIST) {
1194 CERROR("Failed to create gss pipe dir: %ld\n", PTR_ERR(de));
1198 /* FIXME hack pipefs: dput will sometimes cause oops during module
1199 * unload and lgssd close the pipe fds. */
1201 /* krb5 mechanism */
1202 de = rpc_mkpipe(LUSTRE_PIPE_KRB5, (void *) MECH_KRB5, &gss_upcall_ops,
1203 RPC_PIPE_WAIT_FOR_OPEN);
1204 if (!de || IS_ERR(de)) {
1205 CERROR("failed to make rpc_pipe %s: %ld\n",
1206 LUSTRE_PIPE_KRB5, PTR_ERR(de));
1207 rpc_rmdir(LUSTRE_PIPE_ROOT);
1211 de_pipes[MECH_KRB5] = de;
1212 INIT_LIST_HEAD(&upcall_lists[MECH_KRB5]);
1213 spin_lock_init(&upcall_locks[MECH_KRB5]);
1219 void __exit gss_exit_pipefs_upcall(void)
1223 for (i = 0; i < MECH_MAX; i++) {
1224 LASSERT(list_empty(&upcall_lists[i]));
1226 /* dput pipe dentry here might cause lgssd oops. */
1230 rpc_unlink(LUSTRE_PIPE_KRB5);
1231 rpc_rmdir(LUSTRE_PIPE_ROOT);
1234 int __init gss_init_pipefs(void)
1238 rc = gss_init_pipefs_upcall();
1242 rc = sptlrpc_register_policy(&gss_policy_pipefs);
1244 gss_exit_pipefs_upcall();
1251 void __exit gss_exit_pipefs(void)
1253 gss_exit_pipefs_upcall();
1254 sptlrpc_unregister_policy(&gss_policy_pipefs);