1 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
2 * vim:expandtab:shiftwidth=8:tabstop=8:
4 * Modifications for Lustre
5 * Copyright 2004 - 2007, Cluster File Systems, Inc.
7 * Author: Eric Mei <ericm@clusterfs.com>
11 * linux/net/sunrpc/auth_gss.c
13 * RPCSEC_GSS client authentication.
15 * Copyright (c) 2000 The Regents of the University of Michigan.
16 * All rights reserved.
18 * Dug Song <dugsong@monkey.org>
19 * Andy Adamson <andros@umich.edu>
21 * Redistribution and use in source and binary forms, with or without
22 * modification, are permitted provided that the following conditions
25 * 1. Redistributions of source code must retain the above copyright
26 * notice, this list of conditions and the following disclaimer.
27 * 2. Redistributions in binary form must reproduce the above copyright
28 * notice, this list of conditions and the following disclaimer in the
29 * documentation and/or other materials provided with the distribution.
30 * 3. Neither the name of the University nor the names of its
31 * contributors may be used to endorse or promote products derived
32 * from this software without specific prior written permission.
34 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED
35 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
36 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
37 * DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
38 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
39 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
40 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
41 * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
42 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
43 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
44 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
49 # define EXPORT_SYMTAB
51 #define DEBUG_SUBSYSTEM S_SEC
53 #include <linux/init.h>
54 #include <linux/module.h>
55 #include <linux/slab.h>
56 #include <linux/dcache.h>
58 #include <linux/random.h>
59 #include <linux/mutex.h>
60 #include <asm/atomic.h>
62 #include <liblustre.h>
66 #include <obd_class.h>
67 #include <obd_support.h>
68 #include <lustre/lustre_idl.h>
69 #include <lustre_net.h>
70 #include <lustre_import.h>
71 #include <lustre_sec.h>
74 #include "gss_internal.h"
77 #include <linux/crypto.h>
80 static inline int msg_last_segidx(struct lustre_msg *msg)
82 LASSERT(msg->lm_bufcount > 0);
83 return msg->lm_bufcount - 1;
85 static inline int msg_last_seglen(struct lustre_msg *msg)
87 return msg->lm_buflens[msg_last_segidx(msg)];
90 /********************************************
92 ********************************************/
95 void gss_header_swabber(struct gss_header *ghdr)
97 __swab32s(&ghdr->gh_flags);
98 __swab32s(&ghdr->gh_proc);
99 __swab32s(&ghdr->gh_seq);
100 __swab32s(&ghdr->gh_svc);
101 __swab32s(&ghdr->gh_pad1);
102 __swab32s(&ghdr->gh_handle.len);
105 struct gss_header *gss_swab_header(struct lustre_msg *msg, int segment)
107 struct gss_header *ghdr;
109 ghdr = lustre_swab_buf(msg, segment, sizeof(*ghdr),
113 sizeof(*ghdr) + ghdr->gh_handle.len > msg->lm_buflens[segment]) {
114 CERROR("gss header require length %u, now %u received\n",
115 (unsigned int) sizeof(*ghdr) + ghdr->gh_handle.len,
116 msg->lm_buflens[segment]);
124 void gss_netobj_swabber(netobj_t *obj)
126 __swab32s(&obj->len);
129 netobj_t *gss_swab_netobj(struct lustre_msg *msg, int segment)
133 obj = lustre_swab_buf(msg, segment, sizeof(*obj), gss_netobj_swabber);
134 if (obj && sizeof(*obj) + obj->len > msg->lm_buflens[segment]) {
135 CERROR("netobj require length %u but only %u received\n",
136 (unsigned int) sizeof(*obj) + obj->len,
137 msg->lm_buflens[segment]);
145 * payload should be obtained from mechanism. but currently since we
146 * only support kerberos, we could simply use fixed value.
150 #define GSS_KRB5_INTEG_MAX_PAYLOAD (40)
153 int gss_estimate_payload(struct gss_ctx *mechctx, int msgsize, int privacy)
156 /* we suppose max cipher block size is 16 bytes. here we
157 * add 16 for confounder and 16 for padding. */
158 return GSS_KRB5_INTEG_MAX_PAYLOAD + msgsize + 16 + 16 + 16;
160 return GSS_KRB5_INTEG_MAX_PAYLOAD;
165 * return signature size, otherwise < 0 to indicate error
167 static int gss_sign_msg(struct lustre_msg *msg,
168 struct gss_ctx *mechctx,
169 enum lustre_sec_part sp,
170 __u32 flags, __u32 proc, __u32 seq, __u32 svc,
173 struct gss_header *ghdr;
174 rawobj_t text[3], mic;
175 int textcnt, max_textcnt, mic_idx;
178 LASSERT(msg->lm_bufcount >= 2);
181 LASSERT(msg->lm_buflens[0] >=
182 sizeof(*ghdr) + (handle ? handle->len : 0));
183 ghdr = lustre_msg_buf(msg, 0, 0);
185 ghdr->gh_version = PTLRPC_GSS_VERSION;
186 ghdr->gh_sp = (__u8) sp;
187 ghdr->gh_flags = flags;
188 ghdr->gh_proc = proc;
192 /* fill in a fake one */
193 ghdr->gh_handle.len = 0;
195 ghdr->gh_handle.len = handle->len;
196 memcpy(ghdr->gh_handle.data, handle->data, handle->len);
199 /* no actual signature for null mode */
200 if (svc == SPTLRPC_SVC_NULL)
201 return lustre_msg_size_v2(msg->lm_bufcount, msg->lm_buflens);
204 mic_idx = msg_last_segidx(msg);
205 max_textcnt = (svc == SPTLRPC_SVC_AUTH) ? 1 : mic_idx;
207 for (textcnt = 0; textcnt < max_textcnt; textcnt++) {
208 text[textcnt].len = msg->lm_buflens[textcnt];
209 text[textcnt].data = lustre_msg_buf(msg, textcnt, 0);
212 mic.len = msg->lm_buflens[mic_idx];
213 mic.data = lustre_msg_buf(msg, mic_idx, 0);
215 major = lgss_get_mic(mechctx, textcnt, text, &mic);
216 if (major != GSS_S_COMPLETE) {
217 CERROR("fail to generate MIC: %08x\n", major);
220 LASSERT(mic.len <= msg->lm_buflens[mic_idx]);
222 return lustre_shrink_msg(msg, mic_idx, mic.len, 0);
229 __u32 gss_verify_msg(struct lustre_msg *msg,
230 struct gss_ctx *mechctx,
233 rawobj_t text[3], mic;
234 int textcnt, max_textcnt;
238 LASSERT(msg->lm_bufcount >= 2);
240 if (svc == SPTLRPC_SVC_NULL)
241 return GSS_S_COMPLETE;
243 mic_idx = msg_last_segidx(msg);
244 max_textcnt = (svc == SPTLRPC_SVC_AUTH) ? 1 : mic_idx;
246 for (textcnt = 0; textcnt < max_textcnt; textcnt++) {
247 text[textcnt].len = msg->lm_buflens[textcnt];
248 text[textcnt].data = lustre_msg_buf(msg, textcnt, 0);
251 mic.len = msg->lm_buflens[mic_idx];
252 mic.data = lustre_msg_buf(msg, mic_idx, 0);
254 major = lgss_verify_mic(mechctx, textcnt, text, &mic);
255 if (major != GSS_S_COMPLETE)
256 CERROR("mic verify error: %08x\n", major);
262 * return gss error code
265 __u32 gss_unseal_msg(struct gss_ctx *mechctx,
266 struct lustre_msg *msgbuf,
267 int *msg_len, int msgbuf_len)
269 rawobj_t clear_obj, micobj, msgobj, token;
275 if (msgbuf->lm_bufcount != 3) {
276 CERROR("invalid bufcount %d\n", msgbuf->lm_bufcount);
277 RETURN(GSS_S_FAILURE);
280 /* verify gss header */
281 msgobj.len = msgbuf->lm_buflens[0];
282 msgobj.data = lustre_msg_buf(msgbuf, 0, 0);
283 micobj.len = msgbuf->lm_buflens[1];
284 micobj.data = lustre_msg_buf(msgbuf, 1, 0);
286 major = lgss_verify_mic(mechctx, 1, &msgobj, &micobj);
287 if (major != GSS_S_COMPLETE) {
288 CERROR("priv: mic verify error: %08x\n", major);
292 /* temporary clear text buffer */
293 clear_buflen = msgbuf->lm_buflens[2];
294 OBD_ALLOC(clear_buf, clear_buflen);
296 RETURN(GSS_S_FAILURE);
298 token.len = msgbuf->lm_buflens[2];
299 token.data = lustre_msg_buf(msgbuf, 2, 0);
301 clear_obj.len = clear_buflen;
302 clear_obj.data = clear_buf;
304 major = lgss_unwrap(mechctx, &token, &clear_obj);
305 if (major != GSS_S_COMPLETE) {
306 CERROR("priv: unwrap message error: %08x\n", major);
307 GOTO(out_free, major = GSS_S_FAILURE);
309 LASSERT(clear_obj.len <= clear_buflen);
311 /* now the decrypted message */
312 memcpy(msgbuf, clear_obj.data, clear_obj.len);
313 *msg_len = clear_obj.len;
315 major = GSS_S_COMPLETE;
317 OBD_FREE(clear_buf, clear_buflen);
321 /********************************************
322 * gss client context manipulation helpers *
323 ********************************************/
325 int cli_ctx_expire(struct ptlrpc_cli_ctx *ctx)
327 LASSERT(atomic_read(&ctx->cc_refcount));
329 if (!test_and_set_bit(PTLRPC_CTX_DEAD_BIT, &ctx->cc_flags)) {
330 if (!ctx->cc_early_expire)
331 clear_bit(PTLRPC_CTX_UPTODATE_BIT, &ctx->cc_flags);
333 CWARN("ctx %p(%u->%s) get expired: %lu(%+lds)\n",
334 ctx, ctx->cc_vcred.vc_uid, sec2target_str(ctx->cc_sec),
336 ctx->cc_expire == 0 ? 0 :
337 cfs_time_sub(ctx->cc_expire, cfs_time_current_sec()));
346 * return 1 if the context is dead.
348 int cli_ctx_check_death(struct ptlrpc_cli_ctx *ctx)
350 if (unlikely(cli_ctx_is_dead(ctx)))
353 /* expire is 0 means never expire. a newly created gss context
354 * which during upcall may has 0 expiration */
355 if (ctx->cc_expire == 0)
358 /* check real expiration */
359 if (cfs_time_after(ctx->cc_expire, cfs_time_current_sec()))
366 void gss_cli_ctx_uptodate(struct gss_cli_ctx *gctx)
368 struct ptlrpc_cli_ctx *ctx = &gctx->gc_base;
369 unsigned long ctx_expiry;
371 if (lgss_inquire_context(gctx->gc_mechctx, &ctx_expiry)) {
372 CERROR("ctx %p(%u): unable to inquire, expire it now\n",
373 gctx, ctx->cc_vcred.vc_uid);
374 ctx_expiry = 1; /* make it expired now */
377 ctx->cc_expire = gss_round_ctx_expiry(ctx_expiry,
378 ctx->cc_sec->ps_flvr.sf_flags);
380 /* At this point this ctx might have been marked as dead by
381 * someone else, in which case nobody will make further use
382 * of it. we don't care, and mark it UPTODATE will help
383 * destroying server side context when it be destroied. */
384 set_bit(PTLRPC_CTX_UPTODATE_BIT, &ctx->cc_flags);
386 if (sec_is_reverse(ctx->cc_sec)) {
387 CWARN("server installed reverse ctx %p idx "LPX64", "
388 "expiry %lu(%+lds)\n", ctx,
389 gss_handle_to_u64(&gctx->gc_handle),
390 ctx->cc_expire, ctx->cc_expire - cfs_time_current_sec());
392 CWARN("client refreshed ctx %p idx "LPX64" (%u->%s), "
393 "expiry %lu(%+lds)\n", ctx,
394 gss_handle_to_u64(&gctx->gc_handle),
395 ctx->cc_vcred.vc_uid, sec2target_str(ctx->cc_sec),
396 ctx->cc_expire, ctx->cc_expire - cfs_time_current_sec());
398 /* install reverse svc ctx for root context */
399 if (ctx->cc_vcred.vc_uid == 0)
400 gss_sec_install_rctx(ctx->cc_sec->ps_import,
405 static void gss_cli_ctx_finalize(struct gss_cli_ctx *gctx)
407 LASSERT(gctx->gc_base.cc_sec);
409 if (gctx->gc_mechctx) {
410 lgss_delete_sec_context(&gctx->gc_mechctx);
411 gctx->gc_mechctx = NULL;
414 if (!rawobj_empty(&gctx->gc_svc_handle)) {
415 /* forward ctx: mark buddy reverse svcctx soon-expire. */
416 if (!sec_is_reverse(gctx->gc_base.cc_sec) &&
417 !rawobj_empty(&gctx->gc_svc_handle))
418 gss_svc_upcall_expire_rvs_ctx(&gctx->gc_svc_handle);
420 rawobj_free(&gctx->gc_svc_handle);
423 rawobj_free(&gctx->gc_handle);
427 * Based on sequence number algorithm as specified in RFC 2203.
429 * modified for our own problem: arriving request has valid sequence number,
430 * but unwrapping request might cost a long time, after that its sequence
431 * are not valid anymore (fall behind the window). It rarely happen, mostly
432 * under extreme load.
434 * note we should not check sequence before verify the integrity of incoming
435 * request, because just one attacking request with high sequence number might
436 * cause all following request be dropped.
438 * so here we use a multi-phase approach: prepare 2 sequence windows,
439 * "main window" for normal sequence and "back window" for fall behind sequence.
440 * and 3-phase checking mechanism:
441 * 0 - before integrity verification, perform a initial sequence checking in
442 * main window, which only try and don't actually set any bits. if the
443 * sequence is high above the window or fit in the window and the bit
444 * is 0, then accept and proceed to integrity verification. otherwise
445 * reject this sequence.
446 * 1 - after integrity verification, check in main window again. if this
447 * sequence is high above the window or fit in the window and the bit
448 * is 0, then set the bit and accept; if it fit in the window but bit
449 * already set, then reject; if it fall behind the window, then proceed
451 * 2 - check in back window. if it is high above the window or fit in the
452 * window and the bit is 0, then set the bit and accept. otherwise reject.
455 * 1: looks like a replay
459 * note phase 0 is necessary, because otherwise replay attacking request of
460 * sequence which between the 2 windows can't be detected.
462 * this mechanism can't totally solve the problem, but could help much less
463 * number of valid requests be dropped.
466 int gss_do_check_seq(unsigned long *window, __u32 win_size, __u32 *max_seq,
467 __u32 seq_num, int phase)
469 LASSERT(phase >= 0 && phase <= 2);
471 if (seq_num > *max_seq) {
473 * 1. high above the window
478 if (seq_num >= *max_seq + win_size) {
479 memset(window, 0, win_size / 8);
482 while(*max_seq < seq_num) {
484 __clear_bit((*max_seq) % win_size, window);
487 __set_bit(seq_num % win_size, window);
488 } else if (seq_num + win_size <= *max_seq) {
490 * 2. low behind the window
492 if (phase == 0 || phase == 2)
495 CWARN("seq %u is %u behind (size %d), check backup window\n",
496 seq_num, *max_seq - win_size - seq_num, win_size);
500 * 3. fit into the window
504 if (test_bit(seq_num % win_size, window))
509 if (__test_and_set_bit(seq_num % win_size, window))
518 CERROR("seq %u (%s %s window) is a replay: max %u, winsize %d\n",
520 seq_num + win_size > *max_seq ? "in" : "behind",
521 phase == 2 ? "backup " : "main",
527 * Based on sequence number algorithm as specified in RFC 2203.
529 * if @set == 0: initial check, don't set any bit in window
530 * if @sec == 1: final check, set bit in window
532 int gss_check_seq_num(struct gss_svc_seq_data *ssd, __u32 seq_num, int set)
536 spin_lock(&ssd->ssd_lock);
542 rc = gss_do_check_seq(ssd->ssd_win_main, GSS_SEQ_WIN_MAIN,
543 &ssd->ssd_max_main, seq_num, 0);
545 gss_stat_oos_record_svc(0, 1);
548 * phase 1 checking main window
550 rc = gss_do_check_seq(ssd->ssd_win_main, GSS_SEQ_WIN_MAIN,
551 &ssd->ssd_max_main, seq_num, 1);
554 gss_stat_oos_record_svc(1, 1);
560 * phase 2 checking back window
562 rc = gss_do_check_seq(ssd->ssd_win_back, GSS_SEQ_WIN_BACK,
563 &ssd->ssd_max_back, seq_num, 2);
565 gss_stat_oos_record_svc(2, 1);
567 gss_stat_oos_record_svc(2, 0);
570 spin_unlock(&ssd->ssd_lock);
574 /***************************************
576 ***************************************/
579 int gss_cli_payload(struct ptlrpc_cli_ctx *ctx,
580 int msgsize, int privacy)
582 return gss_estimate_payload(NULL, msgsize, privacy);
585 int gss_cli_ctx_match(struct ptlrpc_cli_ctx *ctx, struct vfs_cred *vcred)
587 return (ctx->cc_vcred.vc_uid == vcred->vc_uid);
590 void gss_cli_ctx_flags2str(unsigned long flags, char *buf, int bufsize)
594 if (flags & PTLRPC_CTX_NEW)
595 strncat(buf, "new,", bufsize);
596 if (flags & PTLRPC_CTX_UPTODATE)
597 strncat(buf, "uptodate,", bufsize);
598 if (flags & PTLRPC_CTX_DEAD)
599 strncat(buf, "dead,", bufsize);
600 if (flags & PTLRPC_CTX_ERROR)
601 strncat(buf, "error,", bufsize);
602 if (flags & PTLRPC_CTX_CACHED)
603 strncat(buf, "cached,", bufsize);
604 if (flags & PTLRPC_CTX_ETERNAL)
605 strncat(buf, "eternal,", bufsize);
607 strncat(buf, "-,", bufsize);
609 buf[strlen(buf) - 1] = '\0';
612 int gss_cli_ctx_sign(struct ptlrpc_cli_ctx *ctx,
613 struct ptlrpc_request *req)
615 struct gss_cli_ctx *gctx = ctx2gctx(ctx);
616 __u32 flags = 0, seq, svc;
620 LASSERT(req->rq_reqbuf);
621 LASSERT(req->rq_reqbuf->lm_bufcount >= 2);
622 LASSERT(req->rq_cli_ctx == ctx);
624 /* nothing to do for context negotiation RPCs */
625 if (req->rq_ctx_init)
628 svc = RPC_FLVR_SVC(req->rq_flvr.sf_rpc);
629 if (req->rq_pack_bulk)
630 flags |= LUSTRE_GSS_PACK_BULK;
631 if (req->rq_pack_udesc)
632 flags |= LUSTRE_GSS_PACK_USER;
635 seq = atomic_inc_return(&gctx->gc_seq);
637 rc = gss_sign_msg(req->rq_reqbuf, gctx->gc_mechctx,
638 ctx->cc_sec->ps_part,
639 flags, gctx->gc_proc, seq, svc,
644 /* gss_sign_msg() msg might take long time to finish, in which period
645 * more rpcs could be wrapped up and sent out. if we found too many
646 * of them we should repack this rpc, because sent it too late might
647 * lead to the sequence number fall behind the window on server and
648 * be dropped. also applies to gss_cli_ctx_seal().
650 * Note: null mode dosen't check sequence number. */
651 if (svc != SPTLRPC_SVC_NULL &&
652 atomic_read(&gctx->gc_seq) - seq > GSS_SEQ_REPACK_THRESHOLD) {
653 int behind = atomic_read(&gctx->gc_seq) - seq;
655 gss_stat_oos_record_cli(behind);
656 CWARN("req %p: %u behind, retry signing\n", req, behind);
660 req->rq_reqdata_len = rc;
665 int gss_cli_ctx_handle_err_notify(struct ptlrpc_cli_ctx *ctx,
666 struct ptlrpc_request *req,
667 struct gss_header *ghdr)
669 struct gss_err_header *errhdr;
672 LASSERT(ghdr->gh_proc == PTLRPC_GSS_PROC_ERR);
674 errhdr = (struct gss_err_header *) ghdr;
676 CWARN("req x"LPU64"/t"LPU64", ctx %p idx "LPX64"(%u->%s): "
677 "%sserver respond (%08x/%08x)\n",
678 req->rq_xid, req->rq_transno, ctx,
679 gss_handle_to_u64(&ctx2gctx(ctx)->gc_handle),
680 ctx->cc_vcred.vc_uid, sec2target_str(ctx->cc_sec),
681 sec_is_reverse(ctx->cc_sec) ? "reverse" : "",
682 errhdr->gh_major, errhdr->gh_minor);
684 /* context fini rpc, let it failed */
685 if (req->rq_ctx_fini) {
686 CWARN("context fini rpc failed\n");
690 /* reverse sec, just return error, don't expire this ctx because it's
691 * crucial to callback rpcs. note if the callback rpc failed because
692 * of bit flip during network transfer, the client will be evicted
693 * directly. so more gracefully we probably want let it retry for
694 * number of times. */
695 if (sec_is_reverse(ctx->cc_sec))
698 if (errhdr->gh_major != GSS_S_NO_CONTEXT &&
699 errhdr->gh_major != GSS_S_BAD_SIG)
702 /* server return NO_CONTEXT might be caused by context expire
703 * or server reboot/failover. we try to refresh a new ctx which
704 * be transparent to upper layer.
706 * In some cases, our gss handle is possible to be incidentally
707 * identical to another handle since the handle itself is not
708 * fully random. In krb5 case, the GSS_S_BAD_SIG will be
709 * returned, maybe other gss error for other mechanism.
711 * if we add new mechanism, make sure the correct error are
712 * returned in this case. */
713 CWARN("%s: server might lost the context, retrying\n",
714 errhdr->gh_major == GSS_S_NO_CONTEXT ? "NO_CONTEXT" : "BAD_SIG");
716 sptlrpc_cli_ctx_expire(ctx);
718 /* we need replace the ctx right here, otherwise during
719 * resent we'll hit the logic in sptlrpc_req_refresh_ctx()
720 * which keep the ctx with RESEND flag, thus we'll never
721 * get rid of this ctx. */
722 rc = sptlrpc_req_replace_dead_ctx(req);
729 int gss_cli_ctx_verify(struct ptlrpc_cli_ctx *ctx,
730 struct ptlrpc_request *req)
732 struct gss_cli_ctx *gctx;
733 struct gss_header *ghdr, *reqhdr;
734 struct lustre_msg *msg = req->rq_repbuf;
739 LASSERT(req->rq_cli_ctx == ctx);
742 req->rq_repdata_len = req->rq_nob_received;
743 gctx = container_of(ctx, struct gss_cli_ctx, gc_base);
745 /* special case for context negotiation, rq_repmsg/rq_replen actually
746 * are not used currently. */
747 if (req->rq_ctx_init) {
748 req->rq_repmsg = lustre_msg_buf(msg, 1, 0);
749 req->rq_replen = msg->lm_buflens[1];
753 if (msg->lm_bufcount < 2 || msg->lm_bufcount > 4) {
754 CERROR("unexpected bufcount %u\n", msg->lm_bufcount);
758 ghdr = gss_swab_header(msg, 0);
760 CERROR("can't decode gss header\n");
765 reqhdr = lustre_msg_buf(msg, 0, sizeof(*reqhdr));
768 if (ghdr->gh_version != reqhdr->gh_version) {
769 CERROR("gss version %u mismatch, expect %u\n",
770 ghdr->gh_version, reqhdr->gh_version);
774 switch (ghdr->gh_proc) {
775 case PTLRPC_GSS_PROC_DATA:
776 if (!equi(req->rq_pack_bulk == 1,
777 ghdr->gh_flags & LUSTRE_GSS_PACK_BULK)) {
778 CERROR("%s bulk flag in reply\n",
779 req->rq_pack_bulk ? "missing" : "unexpected");
783 if (ghdr->gh_seq != reqhdr->gh_seq) {
784 CERROR("seqnum %u mismatch, expect %u\n",
785 ghdr->gh_seq, reqhdr->gh_seq);
789 if (ghdr->gh_svc != reqhdr->gh_svc) {
790 CERROR("svc %u mismatch, expect %u\n",
791 ghdr->gh_svc, reqhdr->gh_svc);
795 if (lustre_msg_swabbed(msg))
796 gss_header_swabber(ghdr);
798 major = gss_verify_msg(msg, gctx->gc_mechctx, reqhdr->gh_svc);
799 if (major != GSS_S_COMPLETE)
802 req->rq_repmsg = lustre_msg_buf(msg, 1, 0);
803 req->rq_replen = msg->lm_buflens[1];
805 if (req->rq_pack_bulk) {
807 /* bulk checksum is right after the lustre msg */
808 if (msg->lm_bufcount < 3) {
809 CERROR("Invalid reply bufcount %u\n",
814 rc = bulk_sec_desc_unpack(msg, 2);
817 case PTLRPC_GSS_PROC_ERR:
818 rc = gss_cli_ctx_handle_err_notify(ctx, req, ghdr);
821 CERROR("unknown gss proc %d\n", ghdr->gh_proc);
828 int gss_cli_ctx_seal(struct ptlrpc_cli_ctx *ctx,
829 struct ptlrpc_request *req)
831 struct gss_cli_ctx *gctx;
832 rawobj_t msgobj, cipher_obj, micobj;
833 struct gss_header *ghdr;
834 int buflens[3], wiresize, rc;
838 LASSERT(req->rq_clrbuf);
839 LASSERT(req->rq_cli_ctx == ctx);
840 LASSERT(req->rq_reqlen);
842 gctx = container_of(ctx, struct gss_cli_ctx, gc_base);
844 /* close clear data length */
845 req->rq_clrdata_len = lustre_msg_size_v2(req->rq_clrbuf->lm_bufcount,
846 req->rq_clrbuf->lm_buflens);
848 /* calculate wire data length */
849 buflens[0] = PTLRPC_GSS_HEADER_SIZE;
850 buflens[1] = gss_cli_payload(&gctx->gc_base, buflens[0], 0);
851 buflens[2] = gss_cli_payload(&gctx->gc_base, req->rq_clrdata_len, 1);
852 wiresize = lustre_msg_size_v2(3, buflens);
854 /* allocate wire buffer */
857 LASSERT(req->rq_reqbuf);
858 LASSERT(req->rq_reqbuf != req->rq_clrbuf);
859 LASSERT(req->rq_reqbuf_len >= wiresize);
861 OBD_ALLOC(req->rq_reqbuf, wiresize);
864 req->rq_reqbuf_len = wiresize;
867 lustre_init_msg_v2(req->rq_reqbuf, 3, buflens, NULL);
868 req->rq_reqbuf->lm_secflvr = req->rq_flvr.sf_rpc;
871 ghdr = lustre_msg_buf(req->rq_reqbuf, 0, 0);
872 ghdr->gh_version = PTLRPC_GSS_VERSION;
873 ghdr->gh_sp = (__u8) ctx->cc_sec->ps_part;
875 ghdr->gh_proc = gctx->gc_proc;
876 ghdr->gh_seq = atomic_inc_return(&gctx->gc_seq);
877 ghdr->gh_svc = SPTLRPC_SVC_PRIV;
878 ghdr->gh_handle.len = gctx->gc_handle.len;
879 memcpy(ghdr->gh_handle.data, gctx->gc_handle.data, gctx->gc_handle.len);
880 if (req->rq_pack_bulk)
881 ghdr->gh_flags |= LUSTRE_GSS_PACK_BULK;
882 if (req->rq_pack_udesc)
883 ghdr->gh_flags |= LUSTRE_GSS_PACK_USER;
886 /* header signature */
887 msgobj.len = req->rq_reqbuf->lm_buflens[0];
888 msgobj.data = lustre_msg_buf(req->rq_reqbuf, 0, 0);
889 micobj.len = req->rq_reqbuf->lm_buflens[1];
890 micobj.data = lustre_msg_buf(req->rq_reqbuf, 1, 0);
892 major = lgss_get_mic(gctx->gc_mechctx, 1, &msgobj, &micobj);
893 if (major != GSS_S_COMPLETE) {
894 CERROR("priv: sign message error: %08x\n", major);
895 GOTO(err_free, rc = -EPERM);
897 /* perhaps shrink msg has potential problem in re-packing???
898 * ship a little bit more data is fine.
899 lustre_shrink_msg(req->rq_reqbuf, 1, micobj.len, 0);
903 msgobj.len = req->rq_clrdata_len;
904 msgobj.data = (__u8 *) req->rq_clrbuf;
907 cipher_obj.len = req->rq_reqbuf->lm_buflens[2];
908 cipher_obj.data = lustre_msg_buf(req->rq_reqbuf, 2, 0);
910 major = lgss_wrap(gctx->gc_mechctx, &msgobj, req->rq_clrbuf_len,
912 if (major != GSS_S_COMPLETE) {
913 CERROR("priv: wrap message error: %08x\n", major);
914 GOTO(err_free, rc = -EPERM);
916 LASSERT(cipher_obj.len <= buflens[2]);
918 /* see explain in gss_cli_ctx_sign() */
919 if (atomic_read(&gctx->gc_seq) - ghdr->gh_seq >
920 GSS_SEQ_REPACK_THRESHOLD) {
921 int behind = atomic_read(&gctx->gc_seq) - ghdr->gh_seq;
923 gss_stat_oos_record_cli(behind);
924 CWARN("req %p: %u behind, retry sealing\n", req, behind);
926 ghdr->gh_seq = atomic_inc_return(&gctx->gc_seq);
930 /* now set the final wire data length */
931 req->rq_reqdata_len = lustre_shrink_msg(req->rq_reqbuf, 2,
938 OBD_FREE(req->rq_reqbuf, req->rq_reqbuf_len);
939 req->rq_reqbuf = NULL;
940 req->rq_reqbuf_len = 0;
945 int gss_cli_ctx_unseal(struct ptlrpc_cli_ctx *ctx,
946 struct ptlrpc_request *req)
948 struct gss_cli_ctx *gctx;
949 struct gss_header *ghdr;
954 LASSERT(req->rq_repbuf);
955 LASSERT(req->rq_cli_ctx == ctx);
957 gctx = container_of(ctx, struct gss_cli_ctx, gc_base);
959 ghdr = gss_swab_header(req->rq_repbuf, 0);
961 CERROR("can't decode gss header\n");
966 if (ghdr->gh_version != PTLRPC_GSS_VERSION) {
967 CERROR("gss version %u mismatch, expect %u\n",
968 ghdr->gh_version, PTLRPC_GSS_VERSION);
972 switch (ghdr->gh_proc) {
973 case PTLRPC_GSS_PROC_DATA:
974 if (!equi(req->rq_pack_bulk == 1,
975 ghdr->gh_flags & LUSTRE_GSS_PACK_BULK)) {
976 CERROR("%s bulk flag in reply\n",
977 req->rq_pack_bulk ? "missing" : "unexpected");
981 if (lustre_msg_swabbed(req->rq_repbuf))
982 gss_header_swabber(ghdr);
984 major = gss_unseal_msg(gctx->gc_mechctx, req->rq_repbuf,
985 &msglen, req->rq_repbuf_len);
986 if (major != GSS_S_COMPLETE) {
991 if (lustre_unpack_msg(req->rq_repbuf, msglen)) {
992 CERROR("Failed to unpack after decryption\n");
995 req->rq_repdata_len = msglen;
997 if (req->rq_repbuf->lm_bufcount < 1) {
998 CERROR("Invalid reply buffer: empty\n");
1002 if (req->rq_pack_bulk) {
1003 if (req->rq_repbuf->lm_bufcount < 2) {
1004 CERROR("Too few request buffer segments %d\n",
1005 req->rq_repbuf->lm_bufcount);
1009 /* bulk checksum is the last segment */
1010 if (bulk_sec_desc_unpack(req->rq_repbuf,
1011 req->rq_repbuf->lm_bufcount-1))
1015 req->rq_repmsg = lustre_msg_buf(req->rq_repbuf, 0, 0);
1016 req->rq_replen = req->rq_repbuf->lm_buflens[0];
1020 case PTLRPC_GSS_PROC_ERR:
1021 rc = gss_cli_ctx_handle_err_notify(ctx, req, ghdr);
1024 CERROR("unexpected proc %d\n", ghdr->gh_proc);
1031 /*********************************************
1032 * reverse context installation *
1033 *********************************************/
1036 int gss_install_rvs_svc_ctx(struct obd_import *imp,
1037 struct gss_sec *gsec,
1038 struct gss_cli_ctx *gctx)
1040 return gss_svc_upcall_install_rvs_ctx(imp, gsec, gctx);
1043 /*********************************************
1044 * GSS security APIs *
1045 *********************************************/
1046 int gss_sec_create_common(struct gss_sec *gsec,
1047 struct ptlrpc_sec_policy *policy,
1048 struct obd_import *imp,
1049 struct ptlrpc_svc_ctx *svcctx,
1050 struct sptlrpc_flavor *sf)
1052 struct ptlrpc_sec *sec;
1055 LASSERT(RPC_FLVR_POLICY(sf->sf_rpc) == SPTLRPC_POLICY_GSS);
1057 gsec->gs_mech = lgss_subflavor_to_mech(RPC_FLVR_SUB(sf->sf_rpc));
1058 if (!gsec->gs_mech) {
1059 CERROR("gss backend 0x%x not found\n",
1060 RPC_FLVR_SUB(sf->sf_rpc));
1064 spin_lock_init(&gsec->gs_lock);
1065 gsec->gs_rvs_hdl = 0ULL;
1067 /* initialize upper ptlrpc_sec */
1068 sec = &gsec->gs_base;
1069 sec->ps_policy = policy;
1070 atomic_set(&sec->ps_refcount, 0);
1071 atomic_set(&sec->ps_nctx, 0);
1072 sec->ps_id = sptlrpc_get_next_secid();
1074 sec->ps_import = class_import_get(imp);
1075 sec->ps_lock = SPIN_LOCK_UNLOCKED;
1076 CFS_INIT_LIST_HEAD(&sec->ps_gc_list);
1079 sec->ps_gc_interval = GSS_GC_INTERVAL;
1081 LASSERT(sec_is_reverse(sec));
1083 /* never do gc on reverse sec */
1084 sec->ps_gc_interval = 0;
1087 if (sec->ps_flvr.sf_bulk_ciph != BULK_CIPH_ALG_NULL &&
1088 sec->ps_flvr.sf_flags & PTLRPC_SEC_FL_BULK)
1089 sptlrpc_enc_pool_add_user();
1091 CDEBUG(D_SEC, "create %s%s@%p\n", (svcctx ? "reverse " : ""),
1092 policy->sp_name, gsec);
1096 void gss_sec_destroy_common(struct gss_sec *gsec)
1098 struct ptlrpc_sec *sec = &gsec->gs_base;
1101 LASSERT(sec->ps_import);
1102 LASSERT(atomic_read(&sec->ps_refcount) == 0);
1103 LASSERT(atomic_read(&sec->ps_nctx) == 0);
1105 if (gsec->gs_mech) {
1106 lgss_mech_put(gsec->gs_mech);
1107 gsec->gs_mech = NULL;
1110 class_import_put(sec->ps_import);
1112 if (sec->ps_flvr.sf_bulk_ciph != BULK_CIPH_ALG_NULL &&
1113 sec->ps_flvr.sf_flags & PTLRPC_SEC_FL_BULK)
1114 sptlrpc_enc_pool_del_user();
1119 void gss_sec_kill(struct ptlrpc_sec *sec)
1124 int gss_cli_ctx_init_common(struct ptlrpc_sec *sec,
1125 struct ptlrpc_cli_ctx *ctx,
1126 struct ptlrpc_ctx_ops *ctxops,
1127 struct vfs_cred *vcred)
1129 struct gss_cli_ctx *gctx = ctx2gctx(ctx);
1132 atomic_set(&gctx->gc_seq, 0);
1134 CFS_INIT_HLIST_NODE(&ctx->cc_cache);
1135 atomic_set(&ctx->cc_refcount, 0);
1137 ctx->cc_ops = ctxops;
1139 ctx->cc_flags = PTLRPC_CTX_NEW;
1140 ctx->cc_vcred = *vcred;
1141 spin_lock_init(&ctx->cc_lock);
1142 CFS_INIT_LIST_HEAD(&ctx->cc_req_list);
1143 CFS_INIT_LIST_HEAD(&ctx->cc_gc_chain);
1145 /* take a ref on belonging sec, balanced in ctx destroying */
1146 atomic_inc(&sec->ps_refcount);
1147 /* statistic only */
1148 atomic_inc(&sec->ps_nctx);
1150 CDEBUG(D_SEC, "%s@%p: create ctx %p(%u->%s)\n",
1151 sec->ps_policy->sp_name, ctx->cc_sec,
1152 ctx, ctx->cc_vcred.vc_uid, sec2target_str(ctx->cc_sec));
1158 * 1: the context has been taken care of by someone else
1159 * 0: proceed to really destroy the context locally
1161 int gss_cli_ctx_fini_common(struct ptlrpc_sec *sec,
1162 struct ptlrpc_cli_ctx *ctx)
1164 struct gss_cli_ctx *gctx = ctx2gctx(ctx);
1166 LASSERT(atomic_read(&sec->ps_nctx) > 0);
1167 LASSERT(atomic_read(&ctx->cc_refcount) == 0);
1168 LASSERT(ctx->cc_sec == sec);
1170 if (gctx->gc_mechctx) {
1171 /* the final context fini rpc will use this ctx too, and it's
1172 * asynchronous which finished by request_out_callback(). so
1173 * we add refcount, whoever drop finally drop the refcount to
1174 * 0 should responsible for the rest of destroy. */
1175 atomic_inc(&ctx->cc_refcount);
1177 gss_do_ctx_fini_rpc(gctx);
1178 gss_cli_ctx_finalize(gctx);
1180 if (!atomic_dec_and_test(&ctx->cc_refcount))
1184 if (sec_is_reverse(sec))
1185 CWARN("reverse sec %p: destroy ctx %p\n",
1188 CWARN("%s@%p: destroy ctx %p(%u->%s)\n",
1189 sec->ps_policy->sp_name, ctx->cc_sec,
1190 ctx, ctx->cc_vcred.vc_uid, sec2target_str(ctx->cc_sec));
1196 int gss_alloc_reqbuf_intg(struct ptlrpc_sec *sec,
1197 struct ptlrpc_request *req,
1198 int svc, int msgsize)
1200 int bufsize, txtsize;
1201 int buflens[5], bufcnt = 2;
1205 * on-wire data layout:
1208 * - user descriptor (optional)
1209 * - bulk sec descriptor (optional)
1210 * - signature (optional)
1211 * - svc == NULL: NULL
1212 * - svc == AUTH: signature of gss header
1213 * - svc == INTG: signature of all above
1215 * if this is context negotiation, reserver fixed space
1216 * at the last (signature) segment regardless of svc mode.
1219 buflens[0] = PTLRPC_GSS_HEADER_SIZE;
1220 txtsize = buflens[0];
1222 buflens[1] = msgsize;
1223 if (svc == SPTLRPC_SVC_INTG)
1224 txtsize += buflens[1];
1226 if (req->rq_pack_udesc) {
1227 buflens[bufcnt] = sptlrpc_current_user_desc_size();
1228 if (svc == SPTLRPC_SVC_INTG)
1229 txtsize += buflens[bufcnt];
1233 if (req->rq_pack_bulk) {
1234 buflens[bufcnt] = bulk_sec_desc_size(
1235 req->rq_flvr.sf_bulk_hash, 1,
1237 if (svc == SPTLRPC_SVC_INTG)
1238 txtsize += buflens[bufcnt];
1242 if (req->rq_ctx_init)
1243 buflens[bufcnt++] = GSS_CTX_INIT_MAX_LEN;
1244 else if (svc != SPTLRPC_SVC_NULL)
1245 buflens[bufcnt++] = gss_cli_payload(req->rq_cli_ctx, txtsize,0);
1247 bufsize = lustre_msg_size_v2(bufcnt, buflens);
1249 if (!req->rq_reqbuf) {
1250 bufsize = size_roundup_power2(bufsize);
1252 OBD_ALLOC(req->rq_reqbuf, bufsize);
1253 if (!req->rq_reqbuf)
1256 req->rq_reqbuf_len = bufsize;
1258 LASSERT(req->rq_pool);
1259 LASSERT(req->rq_reqbuf_len >= bufsize);
1260 memset(req->rq_reqbuf, 0, bufsize);
1263 lustre_init_msg_v2(req->rq_reqbuf, bufcnt, buflens, NULL);
1264 req->rq_reqbuf->lm_secflvr = req->rq_flvr.sf_rpc;
1266 req->rq_reqmsg = lustre_msg_buf(req->rq_reqbuf, 1, msgsize);
1267 LASSERT(req->rq_reqmsg);
1269 /* pack user desc here, later we might leave current user's process */
1270 if (req->rq_pack_udesc)
1271 sptlrpc_pack_user_desc(req->rq_reqbuf, 2);
1277 int gss_alloc_reqbuf_priv(struct ptlrpc_sec *sec,
1278 struct ptlrpc_request *req,
1281 int ibuflens[3], ibufcnt;
1283 int clearsize, wiresize;
1286 LASSERT(req->rq_clrbuf == NULL);
1287 LASSERT(req->rq_clrbuf_len == 0);
1289 /* Inner (clear) buffers
1291 * - user descriptor (optional)
1292 * - bulk checksum (optional)
1296 ibuflens[0] = msgsize;
1298 if (req->rq_pack_udesc)
1299 ibuflens[ibufcnt++] = sptlrpc_current_user_desc_size();
1300 if (req->rq_pack_bulk)
1301 ibuflens[ibufcnt++] = bulk_sec_desc_size(
1302 req->rq_flvr.sf_bulk_hash, 1,
1305 clearsize = lustre_msg_size_v2(ibufcnt, ibuflens);
1306 /* to allow append padding during encryption */
1307 clearsize += GSS_MAX_CIPHER_BLOCK;
1309 /* Wrapper (wire) buffers
1311 * - signature of gss header
1315 buflens[0] = PTLRPC_GSS_HEADER_SIZE;
1316 buflens[1] = gss_cli_payload(req->rq_cli_ctx, buflens[0], 0);
1317 buflens[2] = gss_cli_payload(req->rq_cli_ctx, clearsize, 1);
1318 wiresize = lustre_msg_size_v2(3, buflens);
1321 /* rq_reqbuf is preallocated */
1322 LASSERT(req->rq_reqbuf);
1323 LASSERT(req->rq_reqbuf_len >= wiresize);
1325 memset(req->rq_reqbuf, 0, req->rq_reqbuf_len);
1327 /* if the pre-allocated buffer is big enough, we just pack
1328 * both clear buf & request buf in it, to avoid more alloc. */
1329 if (clearsize + wiresize <= req->rq_reqbuf_len) {
1331 (void *) (((char *) req->rq_reqbuf) + wiresize);
1333 CWARN("pre-allocated buf size %d is not enough for "
1334 "both clear (%d) and cipher (%d) text, proceed "
1335 "with extra allocation\n", req->rq_reqbuf_len,
1336 clearsize, wiresize);
1340 if (!req->rq_clrbuf) {
1341 clearsize = size_roundup_power2(clearsize);
1343 OBD_ALLOC(req->rq_clrbuf, clearsize);
1344 if (!req->rq_clrbuf)
1347 req->rq_clrbuf_len = clearsize;
1349 lustre_init_msg_v2(req->rq_clrbuf, ibufcnt, ibuflens, NULL);
1350 req->rq_reqmsg = lustre_msg_buf(req->rq_clrbuf, 0, msgsize);
1352 if (req->rq_pack_udesc)
1353 sptlrpc_pack_user_desc(req->rq_clrbuf, 1);
1359 * NOTE: any change of request buffer allocation should also consider
1360 * changing enlarge_reqbuf() series functions.
1362 int gss_alloc_reqbuf(struct ptlrpc_sec *sec,
1363 struct ptlrpc_request *req,
1366 int svc = RPC_FLVR_SVC(req->rq_flvr.sf_rpc);
1368 LASSERT(!req->rq_pack_bulk ||
1369 (req->rq_bulk_read || req->rq_bulk_write));
1372 case SPTLRPC_SVC_NULL:
1373 case SPTLRPC_SVC_AUTH:
1374 case SPTLRPC_SVC_INTG:
1375 return gss_alloc_reqbuf_intg(sec, req, svc, msgsize);
1376 case SPTLRPC_SVC_PRIV:
1377 return gss_alloc_reqbuf_priv(sec, req, msgsize);
1379 LASSERTF(0, "bad rpc flavor %x\n", req->rq_flvr.sf_rpc);
1384 void gss_free_reqbuf(struct ptlrpc_sec *sec,
1385 struct ptlrpc_request *req)
1390 LASSERT(!req->rq_pool || req->rq_reqbuf);
1391 privacy = RPC_FLVR_SVC(req->rq_flvr.sf_rpc) == SPTLRPC_SVC_PRIV;
1393 if (!req->rq_clrbuf)
1394 goto release_reqbuf;
1396 /* release clear buffer */
1398 LASSERT(req->rq_clrbuf_len);
1401 req->rq_clrbuf >= req->rq_reqbuf &&
1402 (char *) req->rq_clrbuf <
1403 (char *) req->rq_reqbuf + req->rq_reqbuf_len)
1404 goto release_reqbuf;
1406 OBD_FREE(req->rq_clrbuf, req->rq_clrbuf_len);
1407 req->rq_clrbuf = NULL;
1408 req->rq_clrbuf_len = 0;
1411 if (!req->rq_pool && req->rq_reqbuf) {
1412 LASSERT(req->rq_reqbuf_len);
1414 OBD_FREE(req->rq_reqbuf, req->rq_reqbuf_len);
1415 req->rq_reqbuf = NULL;
1416 req->rq_reqbuf_len = 0;
1419 req->rq_reqmsg = NULL;
1424 static int do_alloc_repbuf(struct ptlrpc_request *req, int bufsize)
1426 bufsize = size_roundup_power2(bufsize);
1428 OBD_ALLOC(req->rq_repbuf, bufsize);
1429 if (!req->rq_repbuf)
1432 req->rq_repbuf_len = bufsize;
1437 int gss_alloc_repbuf_intg(struct ptlrpc_sec *sec,
1438 struct ptlrpc_request *req,
1439 int svc, int msgsize)
1442 int buflens[4], bufcnt = 2;
1445 * on-wire data layout:
1448 * - bulk sec descriptor (optional)
1449 * - signature (optional)
1450 * - svc == NULL: NULL
1451 * - svc == AUTH: signature of gss header
1452 * - svc == INTG: signature of all above
1454 * if this is context negotiation, reserver fixed space
1455 * at the last (signature) segment regardless of svc mode.
1458 buflens[0] = PTLRPC_GSS_HEADER_SIZE;
1459 txtsize = buflens[0];
1461 buflens[1] = msgsize;
1462 if (svc == SPTLRPC_SVC_INTG)
1463 txtsize += buflens[1];
1465 if (req->rq_pack_bulk) {
1466 buflens[bufcnt] = bulk_sec_desc_size(
1467 req->rq_flvr.sf_bulk_hash, 0,
1469 if (svc == SPTLRPC_SVC_INTG)
1470 txtsize += buflens[bufcnt];
1474 if (req->rq_ctx_init)
1475 buflens[bufcnt++] = GSS_CTX_INIT_MAX_LEN;
1476 else if (svc != SPTLRPC_SVC_NULL)
1477 buflens[bufcnt++] = gss_cli_payload(req->rq_cli_ctx, txtsize,0);
1479 return do_alloc_repbuf(req, lustre_msg_size_v2(bufcnt, buflens));
1483 int gss_alloc_repbuf_priv(struct ptlrpc_sec *sec,
1484 struct ptlrpc_request *req,
1488 int buflens[3], bufcnt;
1490 /* Inner (clear) buffers
1492 * - bulk checksum (optional)
1496 buflens[0] = msgsize;
1498 if (req->rq_pack_bulk) {
1499 buflens[bufcnt++] = bulk_sec_desc_size(
1500 req->rq_flvr.sf_bulk_hash, 0,
1503 txtsize = lustre_msg_size_v2(bufcnt, buflens);
1504 txtsize += GSS_MAX_CIPHER_BLOCK;
1506 /* Wrapper (wire) buffers
1508 * - signature of gss header
1513 buflens[0] = PTLRPC_GSS_HEADER_SIZE;
1514 buflens[1] = gss_cli_payload(req->rq_cli_ctx, buflens[0], 0);
1515 buflens[2] = gss_cli_payload(req->rq_cli_ctx, txtsize, 1);
1517 return do_alloc_repbuf(req, lustre_msg_size_v2(bufcnt, buflens));
1520 int gss_alloc_repbuf(struct ptlrpc_sec *sec,
1521 struct ptlrpc_request *req,
1524 int svc = RPC_FLVR_SVC(req->rq_flvr.sf_rpc);
1527 LASSERT(!req->rq_pack_bulk ||
1528 (req->rq_bulk_read || req->rq_bulk_write));
1531 case SPTLRPC_SVC_NULL:
1532 case SPTLRPC_SVC_AUTH:
1533 case SPTLRPC_SVC_INTG:
1534 return gss_alloc_repbuf_intg(sec, req, svc, msgsize);
1535 case SPTLRPC_SVC_PRIV:
1536 return gss_alloc_repbuf_priv(sec, req, msgsize);
1538 LASSERTF(0, "bad rpc flavor %x\n", req->rq_flvr.sf_rpc);
1543 void gss_free_repbuf(struct ptlrpc_sec *sec,
1544 struct ptlrpc_request *req)
1546 OBD_FREE(req->rq_repbuf, req->rq_repbuf_len);
1547 req->rq_repbuf = NULL;
1548 req->rq_repbuf_len = 0;
1550 req->rq_repmsg = NULL;
1553 static int get_enlarged_msgsize(struct lustre_msg *msg,
1554 int segment, int newsize)
1556 int save, newmsg_size;
1558 LASSERT(newsize >= msg->lm_buflens[segment]);
1560 save = msg->lm_buflens[segment];
1561 msg->lm_buflens[segment] = newsize;
1562 newmsg_size = lustre_msg_size_v2(msg->lm_bufcount, msg->lm_buflens);
1563 msg->lm_buflens[segment] = save;
1568 static int get_enlarged_msgsize2(struct lustre_msg *msg,
1569 int segment1, int newsize1,
1570 int segment2, int newsize2)
1572 int save1, save2, newmsg_size;
1574 LASSERT(newsize1 >= msg->lm_buflens[segment1]);
1575 LASSERT(newsize2 >= msg->lm_buflens[segment2]);
1577 save1 = msg->lm_buflens[segment1];
1578 save2 = msg->lm_buflens[segment2];
1579 msg->lm_buflens[segment1] = newsize1;
1580 msg->lm_buflens[segment2] = newsize2;
1581 newmsg_size = lustre_msg_size_v2(msg->lm_bufcount, msg->lm_buflens);
1582 msg->lm_buflens[segment1] = save1;
1583 msg->lm_buflens[segment2] = save2;
1589 int gss_enlarge_reqbuf_intg(struct ptlrpc_sec *sec,
1590 struct ptlrpc_request *req,
1592 int segment, int newsize)
1594 struct lustre_msg *newbuf;
1595 int txtsize, sigsize = 0, i;
1596 int newmsg_size, newbuf_size;
1599 * gss header is at seg 0;
1600 * embedded msg is at seg 1;
1601 * signature (if any) is at the last seg
1603 LASSERT(req->rq_reqbuf);
1604 LASSERT(req->rq_reqbuf_len > req->rq_reqlen);
1605 LASSERT(req->rq_reqbuf->lm_bufcount >= 2);
1606 LASSERT(lustre_msg_buf(req->rq_reqbuf, 1, 0) == req->rq_reqmsg);
1608 /* 1. compute new embedded msg size */
1609 newmsg_size = get_enlarged_msgsize(req->rq_reqmsg, segment, newsize);
1610 LASSERT(newmsg_size >= req->rq_reqbuf->lm_buflens[1]);
1612 /* 2. compute new wrapper msg size */
1613 if (svc == SPTLRPC_SVC_NULL) {
1614 /* no signature, get size directly */
1615 newbuf_size = get_enlarged_msgsize(req->rq_reqbuf,
1618 txtsize = req->rq_reqbuf->lm_buflens[0];
1620 if (svc == SPTLRPC_SVC_INTG) {
1621 for (i = 1; i < req->rq_reqbuf->lm_bufcount; i++)
1622 txtsize += req->rq_reqbuf->lm_buflens[i];
1623 txtsize += newmsg_size - req->rq_reqbuf->lm_buflens[1];
1626 sigsize = gss_cli_payload(req->rq_cli_ctx, txtsize, 0);
1627 LASSERT(sigsize >= msg_last_seglen(req->rq_reqbuf));
1629 newbuf_size = get_enlarged_msgsize2(
1632 msg_last_segidx(req->rq_reqbuf),
1636 /* request from pool should always have enough buffer */
1637 LASSERT(!req->rq_pool || req->rq_reqbuf_len >= newbuf_size);
1639 if (req->rq_reqbuf_len < newbuf_size) {
1640 newbuf_size = size_roundup_power2(newbuf_size);
1642 OBD_ALLOC(newbuf, newbuf_size);
1646 memcpy(newbuf, req->rq_reqbuf, req->rq_reqbuf_len);
1648 OBD_FREE(req->rq_reqbuf, req->rq_reqbuf_len);
1649 req->rq_reqbuf = newbuf;
1650 req->rq_reqbuf_len = newbuf_size;
1651 req->rq_reqmsg = lustre_msg_buf(req->rq_reqbuf, 1, 0);
1654 /* do enlargement, from wrapper to embedded, from end to begin */
1655 if (svc != SPTLRPC_SVC_NULL)
1656 _sptlrpc_enlarge_msg_inplace(req->rq_reqbuf,
1657 msg_last_segidx(req->rq_reqbuf),
1660 _sptlrpc_enlarge_msg_inplace(req->rq_reqbuf, 1, newmsg_size);
1661 _sptlrpc_enlarge_msg_inplace(req->rq_reqmsg, segment, newsize);
1663 req->rq_reqlen = newmsg_size;
1668 int gss_enlarge_reqbuf_priv(struct ptlrpc_sec *sec,
1669 struct ptlrpc_request *req,
1670 int segment, int newsize)
1672 struct lustre_msg *newclrbuf;
1673 int newmsg_size, newclrbuf_size, newcipbuf_size;
1677 * embedded msg is at seg 0 of clear buffer;
1678 * cipher text is at seg 2 of cipher buffer;
1680 LASSERT(req->rq_pool ||
1681 (req->rq_reqbuf == NULL && req->rq_reqbuf_len == 0));
1682 LASSERT(req->rq_reqbuf == NULL ||
1683 (req->rq_pool && req->rq_reqbuf->lm_bufcount == 3));
1684 LASSERT(req->rq_clrbuf);
1685 LASSERT(req->rq_clrbuf_len > req->rq_reqlen);
1686 LASSERT(lustre_msg_buf(req->rq_clrbuf, 0, 0) == req->rq_reqmsg);
1688 /* compute new embedded msg size */
1689 newmsg_size = get_enlarged_msgsize(req->rq_reqmsg, segment, newsize);
1691 /* compute new clear buffer size */
1692 newclrbuf_size = get_enlarged_msgsize(req->rq_clrbuf, 0, newmsg_size);
1693 newclrbuf_size += GSS_MAX_CIPHER_BLOCK;
1695 /* compute new cipher buffer size */
1696 buflens[0] = PTLRPC_GSS_HEADER_SIZE;
1697 buflens[1] = gss_cli_payload(req->rq_cli_ctx, buflens[0], 0);
1698 buflens[2] = gss_cli_payload(req->rq_cli_ctx, newclrbuf_size, 1);
1699 newcipbuf_size = lustre_msg_size_v2(3, buflens);
1701 /* handle the case that we put both clear buf and cipher buf into
1702 * pre-allocated single buffer. */
1703 if (unlikely(req->rq_pool) &&
1704 req->rq_clrbuf >= req->rq_reqbuf &&
1705 (char *) req->rq_clrbuf <
1706 (char *) req->rq_reqbuf + req->rq_reqbuf_len) {
1707 /* it couldn't be better we still fit into the
1708 * pre-allocated buffer. */
1709 if (newclrbuf_size + newcipbuf_size <= req->rq_reqbuf_len) {
1712 /* move clear text backward. */
1713 src = req->rq_clrbuf;
1714 dst = (char *) req->rq_reqbuf + newcipbuf_size;
1716 memmove(dst, src, req->rq_clrbuf_len);
1718 req->rq_clrbuf = (struct lustre_msg *) dst;
1719 req->rq_clrbuf_len = newclrbuf_size;
1720 req->rq_reqmsg = lustre_msg_buf(req->rq_clrbuf, 0, 0);
1722 /* sadly we have to split out the clear buffer */
1723 LASSERT(req->rq_reqbuf_len >= newcipbuf_size);
1724 LASSERT(req->rq_clrbuf_len < newclrbuf_size);
1728 if (req->rq_clrbuf_len < newclrbuf_size) {
1729 newclrbuf_size = size_roundup_power2(newclrbuf_size);
1731 OBD_ALLOC(newclrbuf, newclrbuf_size);
1732 if (newclrbuf == NULL)
1735 memcpy(newclrbuf, req->rq_clrbuf, req->rq_clrbuf_len);
1737 if (req->rq_reqbuf == NULL ||
1738 req->rq_clrbuf < req->rq_reqbuf ||
1739 (char *) req->rq_clrbuf >=
1740 (char *) req->rq_reqbuf + req->rq_reqbuf_len) {
1741 OBD_FREE(req->rq_clrbuf, req->rq_clrbuf_len);
1744 req->rq_clrbuf = newclrbuf;
1745 req->rq_clrbuf_len = newclrbuf_size;
1746 req->rq_reqmsg = lustre_msg_buf(req->rq_clrbuf, 0, 0);
1749 _sptlrpc_enlarge_msg_inplace(req->rq_clrbuf, 0, newmsg_size);
1750 _sptlrpc_enlarge_msg_inplace(req->rq_reqmsg, segment, newsize);
1751 req->rq_reqlen = newmsg_size;
1756 int gss_enlarge_reqbuf(struct ptlrpc_sec *sec,
1757 struct ptlrpc_request *req,
1758 int segment, int newsize)
1760 int svc = RPC_FLVR_SVC(req->rq_flvr.sf_rpc);
1762 LASSERT(!req->rq_ctx_init && !req->rq_ctx_fini);
1765 case SPTLRPC_SVC_NULL:
1766 case SPTLRPC_SVC_AUTH:
1767 case SPTLRPC_SVC_INTG:
1768 return gss_enlarge_reqbuf_intg(sec, req, svc, segment, newsize);
1769 case SPTLRPC_SVC_PRIV:
1770 return gss_enlarge_reqbuf_priv(sec, req, segment, newsize);
1772 LASSERTF(0, "bad rpc flavor %x\n", req->rq_flvr.sf_rpc);
1777 int gss_sec_install_rctx(struct obd_import *imp,
1778 struct ptlrpc_sec *sec,
1779 struct ptlrpc_cli_ctx *ctx)
1781 struct gss_sec *gsec;
1782 struct gss_cli_ctx *gctx;
1785 gsec = container_of(sec, struct gss_sec, gs_base);
1786 gctx = container_of(ctx, struct gss_cli_ctx, gc_base);
1788 rc = gss_install_rvs_svc_ctx(imp, gsec, gctx);
1792 /********************************************
1794 ********************************************/
1797 int gss_svc_reqctx_is_special(struct gss_svc_reqctx *grctx)
1800 return (grctx->src_init || grctx->src_init_continue ||
1801 grctx->src_err_notify);
1805 void gss_svc_reqctx_free(struct gss_svc_reqctx *grctx)
1808 gss_svc_upcall_put_ctx(grctx->src_ctx);
1810 sptlrpc_policy_put(grctx->src_base.sc_policy);
1811 OBD_FREE_PTR(grctx);
1815 void gss_svc_reqctx_addref(struct gss_svc_reqctx *grctx)
1817 LASSERT(atomic_read(&grctx->src_base.sc_refcount) > 0);
1818 atomic_inc(&grctx->src_base.sc_refcount);
1822 void gss_svc_reqctx_decref(struct gss_svc_reqctx *grctx)
1824 LASSERT(atomic_read(&grctx->src_base.sc_refcount) > 0);
1826 if (atomic_dec_and_test(&grctx->src_base.sc_refcount))
1827 gss_svc_reqctx_free(grctx);
1831 int gss_svc_sign(struct ptlrpc_request *req,
1832 struct ptlrpc_reply_state *rs,
1833 struct gss_svc_reqctx *grctx,
1840 LASSERT(rs->rs_msg == lustre_msg_buf(rs->rs_repbuf, 1, 0));
1842 /* embedded lustre_msg might have been shrinked */
1843 if (req->rq_replen != rs->rs_repbuf->lm_buflens[1])
1844 lustre_shrink_msg(rs->rs_repbuf, 1, req->rq_replen, 1);
1846 if (req->rq_pack_bulk)
1847 flags |= LUSTRE_GSS_PACK_BULK;
1849 rc = gss_sign_msg(rs->rs_repbuf, grctx->src_ctx->gsc_mechctx,
1850 LUSTRE_SP_ANY, flags, PTLRPC_GSS_PROC_DATA,
1851 grctx->src_wirectx.gw_seq, svc, NULL);
1855 rs->rs_repdata_len = rc;
1859 int gss_pack_err_notify(struct ptlrpc_request *req, __u32 major, __u32 minor)
1861 struct gss_svc_reqctx *grctx = gss_svc_ctx2reqctx(req->rq_svc_ctx);
1862 struct ptlrpc_reply_state *rs;
1863 struct gss_err_header *ghdr;
1864 int replen = sizeof(struct ptlrpc_body);
1868 //if (OBD_FAIL_CHECK_ORSET(OBD_FAIL_SVCGSS_ERR_NOTIFY, OBD_FAIL_ONCE))
1871 grctx->src_err_notify = 1;
1872 grctx->src_reserve_len = 0;
1874 rc = lustre_pack_reply_v2(req, 1, &replen, NULL);
1876 CERROR("could not pack reply, err %d\n", rc);
1881 rs = req->rq_reply_state;
1882 LASSERT(rs->rs_repbuf->lm_buflens[1] >= sizeof(*ghdr));
1883 ghdr = lustre_msg_buf(rs->rs_repbuf, 0, 0);
1884 ghdr->gh_version = PTLRPC_GSS_VERSION;
1886 ghdr->gh_proc = PTLRPC_GSS_PROC_ERR;
1887 ghdr->gh_major = major;
1888 ghdr->gh_minor = minor;
1889 ghdr->gh_handle.len = 0; /* fake context handle */
1891 rs->rs_repdata_len = lustre_msg_size_v2(rs->rs_repbuf->lm_bufcount,
1892 rs->rs_repbuf->lm_buflens);
1894 CDEBUG(D_SEC, "prepare gss error notify(0x%x/0x%x) to %s\n",
1895 major, minor, libcfs_nid2str(req->rq_peer.nid));
1900 int gss_svc_handle_init(struct ptlrpc_request *req,
1901 struct gss_wire_ctx *gw)
1903 struct gss_svc_reqctx *grctx = gss_svc_ctx2reqctx(req->rq_svc_ctx);
1904 struct lustre_msg *reqbuf = req->rq_reqbuf;
1905 struct obd_uuid *uuid;
1906 struct obd_device *target;
1907 rawobj_t uuid_obj, rvs_hdl, in_token;
1909 __u32 *secdata, seclen;
1913 CDEBUG(D_SEC, "processing gss init(%d) request from %s\n", gw->gw_proc,
1914 libcfs_nid2str(req->rq_peer.nid));
1916 req->rq_ctx_init = 1;
1918 if (gw->gw_flags & LUSTRE_GSS_PACK_BULK) {
1919 CERROR("unexpected bulk flag\n");
1920 RETURN(SECSVC_DROP);
1923 if (gw->gw_proc == PTLRPC_GSS_PROC_INIT && gw->gw_handle.len != 0) {
1924 CERROR("proc %u: invalid handle length %u\n",
1925 gw->gw_proc, gw->gw_handle.len);
1926 RETURN(SECSVC_DROP);
1929 if (reqbuf->lm_bufcount < 3 || reqbuf->lm_bufcount > 4){
1930 CERROR("Invalid bufcount %d\n", reqbuf->lm_bufcount);
1931 RETURN(SECSVC_DROP);
1934 /* ctx initiate payload is in last segment */
1935 secdata = lustre_msg_buf(reqbuf, reqbuf->lm_bufcount - 1, 0);
1936 seclen = reqbuf->lm_buflens[reqbuf->lm_bufcount - 1];
1938 if (seclen < 4 + 4) {
1939 CERROR("sec size %d too small\n", seclen);
1940 RETURN(SECSVC_DROP);
1943 /* lustre svc type */
1944 lustre_svc = le32_to_cpu(*secdata++);
1947 /* extract target uuid, note this code is somewhat fragile
1948 * because touched internal structure of obd_uuid */
1949 if (rawobj_extract(&uuid_obj, &secdata, &seclen)) {
1950 CERROR("failed to extract target uuid\n");
1951 RETURN(SECSVC_DROP);
1953 uuid_obj.data[uuid_obj.len - 1] = '\0';
1955 uuid = (struct obd_uuid *) uuid_obj.data;
1956 target = class_uuid2obd(uuid);
1957 if (!target || target->obd_stopping || !target->obd_set_up) {
1958 CERROR("target '%s' is not available for context init (%s)\n",
1959 uuid->uuid, target == NULL ? "no target" :
1960 (target->obd_stopping ? "stopping" : "not set up"));
1961 RETURN(SECSVC_DROP);
1964 /* extract reverse handle */
1965 if (rawobj_extract(&rvs_hdl, &secdata, &seclen)) {
1966 CERROR("failed extract reverse handle\n");
1967 RETURN(SECSVC_DROP);
1971 if (rawobj_extract(&in_token, &secdata, &seclen)) {
1972 CERROR("can't extract token\n");
1973 RETURN(SECSVC_DROP);
1976 rc = gss_svc_upcall_handle_init(req, grctx, gw, target, lustre_svc,
1977 &rvs_hdl, &in_token);
1978 if (rc != SECSVC_OK)
1981 if (grctx->src_ctx->gsc_usr_mds || grctx->src_ctx->gsc_usr_root)
1982 CWARN("create svc ctx %p: user from %s authenticated as %s\n",
1983 grctx->src_ctx, libcfs_nid2str(req->rq_peer.nid),
1984 grctx->src_ctx->gsc_usr_mds ? "mds" : "root");
1986 CWARN("create svc ctx %p: accept user %u from %s\n",
1987 grctx->src_ctx, grctx->src_ctx->gsc_uid,
1988 libcfs_nid2str(req->rq_peer.nid));
1990 if (gw->gw_flags & LUSTRE_GSS_PACK_USER) {
1991 if (reqbuf->lm_bufcount < 4) {
1992 CERROR("missing user descriptor\n");
1993 RETURN(SECSVC_DROP);
1995 if (sptlrpc_unpack_user_desc(reqbuf, 2)) {
1996 CERROR("Mal-formed user descriptor\n");
1997 RETURN(SECSVC_DROP);
2000 req->rq_pack_udesc = 1;
2001 req->rq_user_desc = lustre_msg_buf(reqbuf, 2, 0);
2004 req->rq_reqmsg = lustre_msg_buf(reqbuf, 1, 0);
2005 req->rq_reqlen = lustre_msg_buflen(reqbuf, 1);
2011 * last segment must be the gss signature.
2014 int gss_svc_verify_request(struct ptlrpc_request *req,
2015 struct gss_svc_reqctx *grctx,
2016 struct gss_wire_ctx *gw,
2019 struct gss_svc_ctx *gctx = grctx->src_ctx;
2020 struct lustre_msg *msg = req->rq_reqbuf;
2024 *major = GSS_S_COMPLETE;
2026 if (msg->lm_bufcount < 2) {
2027 CERROR("Too few segments (%u) in request\n", msg->lm_bufcount);
2031 if (gw->gw_svc == SPTLRPC_SVC_NULL)
2034 if (gss_check_seq_num(&gctx->gsc_seqdata, gw->gw_seq, 0)) {
2035 CERROR("phase 0: discard replayed req: seq %u\n", gw->gw_seq);
2036 *major = GSS_S_DUPLICATE_TOKEN;
2040 *major = gss_verify_msg(msg, gctx->gsc_mechctx, gw->gw_svc);
2041 if (*major != GSS_S_COMPLETE)
2044 if (gctx->gsc_reverse == 0 &&
2045 gss_check_seq_num(&gctx->gsc_seqdata, gw->gw_seq, 1)) {
2046 CERROR("phase 1+: discard replayed req: seq %u\n", gw->gw_seq);
2047 *major = GSS_S_DUPLICATE_TOKEN;
2052 /* user descriptor */
2053 if (gw->gw_flags & LUSTRE_GSS_PACK_USER) {
2054 if (msg->lm_bufcount < (offset + 1)) {
2055 CERROR("no user desc included\n");
2059 if (sptlrpc_unpack_user_desc(msg, offset)) {
2060 CERROR("Mal-formed user descriptor\n");
2064 req->rq_pack_udesc = 1;
2065 req->rq_user_desc = lustre_msg_buf(msg, offset, 0);
2069 /* check bulk cksum data */
2070 if (gw->gw_flags & LUSTRE_GSS_PACK_BULK) {
2071 if (msg->lm_bufcount < (offset + 1)) {
2072 CERROR("no bulk checksum included\n");
2076 if (bulk_sec_desc_unpack(msg, offset))
2079 req->rq_pack_bulk = 1;
2080 grctx->src_reqbsd = lustre_msg_buf(msg, offset, 0);
2081 grctx->src_reqbsd_size = lustre_msg_buflen(msg, offset);
2084 req->rq_reqmsg = lustre_msg_buf(msg, 1, 0);
2085 req->rq_reqlen = msg->lm_buflens[1];
2090 int gss_svc_unseal_request(struct ptlrpc_request *req,
2091 struct gss_svc_reqctx *grctx,
2092 struct gss_wire_ctx *gw,
2095 struct gss_svc_ctx *gctx = grctx->src_ctx;
2096 struct lustre_msg *msg = req->rq_reqbuf;
2097 int msglen, offset = 1;
2100 if (gss_check_seq_num(&gctx->gsc_seqdata, gw->gw_seq, 0)) {
2101 CERROR("phase 0: discard replayed req: seq %u\n", gw->gw_seq);
2102 *major = GSS_S_DUPLICATE_TOKEN;
2106 *major = gss_unseal_msg(gctx->gsc_mechctx, msg,
2107 &msglen, req->rq_reqdata_len);
2108 if (*major != GSS_S_COMPLETE)
2111 if (gss_check_seq_num(&gctx->gsc_seqdata, gw->gw_seq, 1)) {
2112 CERROR("phase 1+: discard replayed req: seq %u\n", gw->gw_seq);
2113 *major = GSS_S_DUPLICATE_TOKEN;
2117 if (lustre_unpack_msg(msg, msglen)) {
2118 CERROR("Failed to unpack after decryption\n");
2121 req->rq_reqdata_len = msglen;
2123 if (msg->lm_bufcount < 1) {
2124 CERROR("Invalid buffer: is empty\n");
2128 if (gw->gw_flags & LUSTRE_GSS_PACK_USER) {
2129 if (msg->lm_bufcount < offset + 1) {
2130 CERROR("no user descriptor included\n");
2134 if (sptlrpc_unpack_user_desc(msg, offset)) {
2135 CERROR("Mal-formed user descriptor\n");
2139 req->rq_pack_udesc = 1;
2140 req->rq_user_desc = lustre_msg_buf(msg, offset, 0);
2144 if (gw->gw_flags & LUSTRE_GSS_PACK_BULK) {
2145 if (msg->lm_bufcount < offset + 1) {
2146 CERROR("no bulk checksum included\n");
2150 if (bulk_sec_desc_unpack(msg, offset))
2153 req->rq_pack_bulk = 1;
2154 grctx->src_reqbsd = lustre_msg_buf(msg, offset, 0);
2155 grctx->src_reqbsd_size = lustre_msg_buflen(msg, offset);
2158 req->rq_reqmsg = lustre_msg_buf(req->rq_reqbuf, 0, 0);
2159 req->rq_reqlen = req->rq_reqbuf->lm_buflens[0];
2164 int gss_svc_handle_data(struct ptlrpc_request *req,
2165 struct gss_wire_ctx *gw)
2167 struct gss_svc_reqctx *grctx = gss_svc_ctx2reqctx(req->rq_svc_ctx);
2172 grctx->src_ctx = gss_svc_upcall_get_ctx(req, gw);
2173 if (!grctx->src_ctx) {
2174 major = GSS_S_NO_CONTEXT;
2178 switch (gw->gw_svc) {
2179 case SPTLRPC_SVC_NULL:
2180 case SPTLRPC_SVC_AUTH:
2181 case SPTLRPC_SVC_INTG:
2182 rc = gss_svc_verify_request(req, grctx, gw, &major);
2184 case SPTLRPC_SVC_PRIV:
2185 rc = gss_svc_unseal_request(req, grctx, gw, &major);
2188 CERROR("unsupported gss service %d\n", gw->gw_svc);
2195 CERROR("svc %u failed: major 0x%08x: req xid "LPU64" ctx %p idx "
2196 LPX64"(%u->%s)\n", gw->gw_svc, major, req->rq_xid,
2197 grctx->src_ctx, gss_handle_to_u64(&gw->gw_handle),
2198 grctx->src_ctx->gsc_uid, libcfs_nid2str(req->rq_peer.nid));
2200 /* we only notify client in case of NO_CONTEXT/BAD_SIG, which
2201 * might happen after server reboot, to allow recovery. */
2202 if ((major == GSS_S_NO_CONTEXT || major == GSS_S_BAD_SIG) &&
2203 gss_pack_err_notify(req, major, 0) == 0)
2204 RETURN(SECSVC_COMPLETE);
2206 RETURN(SECSVC_DROP);
2210 int gss_svc_handle_destroy(struct ptlrpc_request *req,
2211 struct gss_wire_ctx *gw)
2213 struct gss_svc_reqctx *grctx = gss_svc_ctx2reqctx(req->rq_svc_ctx);
2217 req->rq_ctx_fini = 1;
2218 req->rq_no_reply = 1;
2220 grctx->src_ctx = gss_svc_upcall_get_ctx(req, gw);
2221 if (!grctx->src_ctx) {
2222 CDEBUG(D_SEC, "invalid gss context handle for destroy.\n");
2223 RETURN(SECSVC_DROP);
2226 if (gw->gw_svc != SPTLRPC_SVC_INTG) {
2227 CERROR("svc %u is not supported in destroy.\n", gw->gw_svc);
2228 RETURN(SECSVC_DROP);
2231 if (gss_svc_verify_request(req, grctx, gw, &major))
2232 RETURN(SECSVC_DROP);
2234 CWARN("destroy svc ctx %p idx "LPX64" (%u->%s)\n",
2235 grctx->src_ctx, gss_handle_to_u64(&gw->gw_handle),
2236 grctx->src_ctx->gsc_uid, libcfs_nid2str(req->rq_peer.nid));
2238 gss_svc_upcall_destroy_ctx(grctx->src_ctx);
2240 if (gw->gw_flags & LUSTRE_GSS_PACK_USER) {
2241 if (req->rq_reqbuf->lm_bufcount < 4) {
2242 CERROR("missing user descriptor, ignore it\n");
2245 if (sptlrpc_unpack_user_desc(req->rq_reqbuf, 2)) {
2246 CERROR("Mal-formed user descriptor, ignore it\n");
2250 req->rq_pack_udesc = 1;
2251 req->rq_user_desc = lustre_msg_buf(req->rq_reqbuf, 2, 0);
2257 int gss_svc_accept(struct ptlrpc_sec_policy *policy, struct ptlrpc_request *req)
2259 struct gss_header *ghdr;
2260 struct gss_svc_reqctx *grctx;
2261 struct gss_wire_ctx *gw;
2265 LASSERT(req->rq_reqbuf);
2266 LASSERT(req->rq_svc_ctx == NULL);
2268 if (req->rq_reqbuf->lm_bufcount < 2) {
2269 CERROR("buf count only %d\n", req->rq_reqbuf->lm_bufcount);
2270 RETURN(SECSVC_DROP);
2273 ghdr = gss_swab_header(req->rq_reqbuf, 0);
2275 CERROR("can't decode gss header\n");
2276 RETURN(SECSVC_DROP);
2280 if (ghdr->gh_version != PTLRPC_GSS_VERSION) {
2281 CERROR("gss version %u, expect %u\n", ghdr->gh_version,
2282 PTLRPC_GSS_VERSION);
2283 RETURN(SECSVC_DROP);
2286 req->rq_sp_from = ghdr->gh_sp;
2288 /* alloc grctx data */
2289 OBD_ALLOC_PTR(grctx);
2291 CERROR("fail to alloc svc reqctx\n");
2292 RETURN(SECSVC_DROP);
2294 grctx->src_base.sc_policy = sptlrpc_policy_get(policy);
2295 atomic_set(&grctx->src_base.sc_refcount, 1);
2296 req->rq_svc_ctx = &grctx->src_base;
2297 gw = &grctx->src_wirectx;
2299 /* save wire context */
2300 gw->gw_flags = ghdr->gh_flags;
2301 gw->gw_proc = ghdr->gh_proc;
2302 gw->gw_seq = ghdr->gh_seq;
2303 gw->gw_svc = ghdr->gh_svc;
2304 rawobj_from_netobj(&gw->gw_handle, &ghdr->gh_handle);
2306 /* keep original wire header which subject to checksum verification */
2307 if (lustre_msg_swabbed(req->rq_reqbuf))
2308 gss_header_swabber(ghdr);
2310 switch(ghdr->gh_proc) {
2311 case PTLRPC_GSS_PROC_INIT:
2312 case PTLRPC_GSS_PROC_CONTINUE_INIT:
2313 rc = gss_svc_handle_init(req, gw);
2315 case PTLRPC_GSS_PROC_DATA:
2316 rc = gss_svc_handle_data(req, gw);
2318 case PTLRPC_GSS_PROC_DESTROY:
2319 rc = gss_svc_handle_destroy(req, gw);
2322 CERROR("unknown proc %u\n", gw->gw_proc);
2329 LASSERT (grctx->src_ctx);
2331 req->rq_auth_gss = 1;
2332 req->rq_auth_remote = grctx->src_ctx->gsc_remote;
2333 req->rq_auth_usr_mdt = grctx->src_ctx->gsc_usr_mds;
2334 req->rq_auth_usr_root = grctx->src_ctx->gsc_usr_root;
2335 req->rq_auth_uid = grctx->src_ctx->gsc_uid;
2336 req->rq_auth_mapped_uid = grctx->src_ctx->gsc_mapped_uid;
2338 case SECSVC_COMPLETE:
2341 gss_svc_reqctx_free(grctx);
2342 req->rq_svc_ctx = NULL;
2349 void gss_svc_invalidate_ctx(struct ptlrpc_svc_ctx *svc_ctx)
2351 struct gss_svc_reqctx *grctx;
2354 if (svc_ctx == NULL) {
2359 grctx = gss_svc_ctx2reqctx(svc_ctx);
2361 CWARN("gss svc invalidate ctx %p(%u)\n",
2362 grctx->src_ctx, grctx->src_ctx->gsc_uid);
2363 gss_svc_upcall_destroy_ctx(grctx->src_ctx);
2369 int gss_svc_payload(struct gss_svc_reqctx *grctx, int msgsize, int privacy)
2371 if (gss_svc_reqctx_is_special(grctx))
2372 return grctx->src_reserve_len;
2374 return gss_estimate_payload(NULL, msgsize, privacy);
2377 int gss_svc_alloc_rs(struct ptlrpc_request *req, int msglen)
2379 struct gss_svc_reqctx *grctx;
2380 struct ptlrpc_reply_state *rs;
2381 int privacy, svc, bsd_off = 0;
2382 int ibuflens[2], ibufcnt = 0;
2383 int buflens[4], bufcnt;
2384 int txtsize, wmsg_size, rs_size;
2387 LASSERT(msglen % 8 == 0);
2389 if (req->rq_pack_bulk && !req->rq_bulk_read && !req->rq_bulk_write) {
2390 CERROR("client request bulk sec on non-bulk rpc\n");
2394 svc = RPC_FLVR_SVC(req->rq_flvr.sf_rpc);
2396 grctx = gss_svc_ctx2reqctx(req->rq_svc_ctx);
2397 if (gss_svc_reqctx_is_special(grctx))
2400 privacy = (svc == SPTLRPC_SVC_PRIV);
2405 ibuflens[0] = msglen;
2407 if (req->rq_pack_bulk) {
2408 LASSERT(grctx->src_reqbsd);
2411 ibuflens[ibufcnt++] = bulk_sec_desc_size(
2412 grctx->src_reqbsd->bsd_hash_alg,
2413 0, req->rq_bulk_read);
2416 txtsize = lustre_msg_size_v2(ibufcnt, ibuflens);
2417 txtsize += GSS_MAX_CIPHER_BLOCK;
2419 /* wrapper buffer */
2421 buflens[0] = PTLRPC_GSS_HEADER_SIZE;
2422 buflens[1] = gss_svc_payload(grctx, buflens[0], 0);
2423 buflens[2] = gss_svc_payload(grctx, txtsize, 1);
2426 buflens[0] = PTLRPC_GSS_HEADER_SIZE;
2427 buflens[1] = msglen;
2429 txtsize = buflens[0];
2430 if (svc == SPTLRPC_SVC_INTG)
2431 txtsize += buflens[1];
2433 if (req->rq_pack_bulk) {
2434 LASSERT(grctx->src_reqbsd);
2437 buflens[bufcnt] = bulk_sec_desc_size(
2438 grctx->src_reqbsd->bsd_hash_alg,
2439 0, req->rq_bulk_read);
2440 if (svc == SPTLRPC_SVC_INTG)
2441 txtsize += buflens[bufcnt];
2445 if (gss_svc_reqctx_is_special(grctx) ||
2446 svc != SPTLRPC_SVC_NULL)
2447 buflens[bufcnt++] = gss_svc_payload(grctx, txtsize, 0);
2450 wmsg_size = lustre_msg_size_v2(bufcnt, buflens);
2452 rs_size = sizeof(*rs) + wmsg_size;
2453 rs = req->rq_reply_state;
2457 LASSERT(rs->rs_size >= rs_size);
2459 OBD_ALLOC(rs, rs_size);
2463 rs->rs_size = rs_size;
2466 rs->rs_repbuf = (struct lustre_msg *) (rs + 1);
2467 rs->rs_repbuf_len = wmsg_size;
2469 /* initialize the buffer */
2471 lustre_init_msg_v2(rs->rs_repbuf, ibufcnt, ibuflens, NULL);
2472 rs->rs_msg = lustre_msg_buf(rs->rs_repbuf, 0, msglen);
2474 lustre_init_msg_v2(rs->rs_repbuf, bufcnt, buflens, NULL);
2475 rs->rs_repbuf->lm_secflvr = req->rq_flvr.sf_rpc;
2477 rs->rs_msg = lustre_msg_buf(rs->rs_repbuf, 1, 0);
2481 grctx->src_repbsd = lustre_msg_buf(rs->rs_repbuf, bsd_off, 0);
2482 grctx->src_repbsd_size = lustre_msg_buflen(rs->rs_repbuf,
2486 gss_svc_reqctx_addref(grctx);
2487 rs->rs_svc_ctx = req->rq_svc_ctx;
2489 LASSERT(rs->rs_msg);
2490 req->rq_reply_state = rs;
2495 int gss_svc_seal(struct ptlrpc_request *req,
2496 struct ptlrpc_reply_state *rs,
2497 struct gss_svc_reqctx *grctx)
2499 struct gss_svc_ctx *gctx = grctx->src_ctx;
2500 rawobj_t msgobj, cipher_obj, micobj;
2501 struct gss_header *ghdr;
2503 int cipher_buflen, buflens[3];
2508 /* embedded lustre_msg might have been shrinked */
2509 if (req->rq_replen != rs->rs_repbuf->lm_buflens[0])
2510 lustre_shrink_msg(rs->rs_repbuf, 0, req->rq_replen, 1);
2512 /* clear data length */
2513 msglen = lustre_msg_size_v2(rs->rs_repbuf->lm_bufcount,
2514 rs->rs_repbuf->lm_buflens);
2517 msgobj.len = msglen;
2518 msgobj.data = (__u8 *) rs->rs_repbuf;
2520 /* allocate temporary cipher buffer */
2521 cipher_buflen = gss_estimate_payload(gctx->gsc_mechctx, msglen, 1);
2522 OBD_ALLOC(cipher_buf, cipher_buflen);
2526 cipher_obj.len = cipher_buflen;
2527 cipher_obj.data = cipher_buf;
2529 major = lgss_wrap(gctx->gsc_mechctx, &msgobj, rs->rs_repbuf_len,
2531 if (major != GSS_S_COMPLETE) {
2532 CERROR("priv: wrap message error: %08x\n", major);
2533 GOTO(out_free, rc = -EPERM);
2535 LASSERT(cipher_obj.len <= cipher_buflen);
2537 /* we are about to override data at rs->rs_repbuf, nullify pointers
2538 * to which to catch further illegal usage. */
2539 grctx->src_repbsd = NULL;
2540 grctx->src_repbsd_size = 0;
2542 /* now the real wire data */
2543 buflens[0] = PTLRPC_GSS_HEADER_SIZE;
2544 buflens[1] = gss_estimate_payload(gctx->gsc_mechctx, buflens[0], 0);
2545 buflens[2] = cipher_obj.len;
2547 LASSERT(lustre_msg_size_v2(3, buflens) <= rs->rs_repbuf_len);
2548 lustre_init_msg_v2(rs->rs_repbuf, 3, buflens, NULL);
2549 rs->rs_repbuf->lm_secflvr = req->rq_flvr.sf_rpc;
2552 ghdr = lustre_msg_buf(rs->rs_repbuf, 0, 0);
2553 ghdr->gh_version = PTLRPC_GSS_VERSION;
2555 ghdr->gh_proc = PTLRPC_GSS_PROC_DATA;
2556 ghdr->gh_seq = grctx->src_wirectx.gw_seq;
2557 ghdr->gh_svc = SPTLRPC_SVC_PRIV;
2558 ghdr->gh_handle.len = 0;
2559 if (req->rq_pack_bulk)
2560 ghdr->gh_flags |= LUSTRE_GSS_PACK_BULK;
2562 /* header signature */
2563 msgobj.len = rs->rs_repbuf->lm_buflens[0];
2564 msgobj.data = lustre_msg_buf(rs->rs_repbuf, 0, 0);
2565 micobj.len = rs->rs_repbuf->lm_buflens[1];
2566 micobj.data = lustre_msg_buf(rs->rs_repbuf, 1, 0);
2568 major = lgss_get_mic(gctx->gsc_mechctx, 1, &msgobj, &micobj);
2569 if (major != GSS_S_COMPLETE) {
2570 CERROR("priv: sign message error: %08x\n", major);
2571 GOTO(out_free, rc = -EPERM);
2573 lustre_shrink_msg(rs->rs_repbuf, 1, micobj.len, 0);
2576 memcpy(lustre_msg_buf(rs->rs_repbuf, 2, 0),
2577 cipher_obj.data, cipher_obj.len);
2579 rs->rs_repdata_len = lustre_shrink_msg(rs->rs_repbuf, 2,
2582 /* to catch upper layer's further access */
2584 req->rq_repmsg = NULL;
2589 OBD_FREE(cipher_buf, cipher_buflen);
2593 int gss_svc_authorize(struct ptlrpc_request *req)
2595 struct ptlrpc_reply_state *rs = req->rq_reply_state;
2596 struct gss_svc_reqctx *grctx = gss_svc_ctx2reqctx(req->rq_svc_ctx);
2597 struct gss_wire_ctx *gw;
2601 if (gss_svc_reqctx_is_special(grctx))
2604 gw = &grctx->src_wirectx;
2605 if (gw->gw_proc != PTLRPC_GSS_PROC_DATA &&
2606 gw->gw_proc != PTLRPC_GSS_PROC_DESTROY) {
2607 CERROR("proc %d not support\n", gw->gw_proc);
2611 LASSERT(grctx->src_ctx);
2613 switch (gw->gw_svc) {
2614 case SPTLRPC_SVC_NULL:
2615 case SPTLRPC_SVC_AUTH:
2616 case SPTLRPC_SVC_INTG:
2617 rc = gss_svc_sign(req, rs, grctx, gw->gw_svc);
2619 case SPTLRPC_SVC_PRIV:
2620 rc = gss_svc_seal(req, rs, grctx);
2623 CERROR("Unknown service %d\n", gw->gw_svc);
2624 GOTO(out, rc = -EINVAL);
2632 void gss_svc_free_rs(struct ptlrpc_reply_state *rs)
2634 struct gss_svc_reqctx *grctx;
2636 LASSERT(rs->rs_svc_ctx);
2637 grctx = container_of(rs->rs_svc_ctx, struct gss_svc_reqctx, src_base);
2639 /* paranoid, maybe not necessary */
2640 grctx->src_reqbsd = NULL;
2641 grctx->src_repbsd = NULL;
2643 gss_svc_reqctx_decref(grctx);
2644 rs->rs_svc_ctx = NULL;
2646 if (!rs->rs_prealloc)
2647 OBD_FREE(rs, rs->rs_size);
2650 void gss_svc_free_ctx(struct ptlrpc_svc_ctx *ctx)
2652 LASSERT(atomic_read(&ctx->sc_refcount) == 0);
2653 gss_svc_reqctx_free(gss_svc_ctx2reqctx(ctx));
2656 int gss_copy_rvc_cli_ctx(struct ptlrpc_cli_ctx *cli_ctx,
2657 struct ptlrpc_svc_ctx *svc_ctx)
2659 struct gss_cli_ctx *cli_gctx = ctx2gctx(cli_ctx);
2660 struct gss_svc_ctx *svc_gctx = gss_svc_ctx2gssctx(svc_ctx);
2661 struct gss_ctx *mechctx = NULL;
2664 LASSERT(svc_gctx && svc_gctx->gsc_mechctx);
2666 cli_gctx->gc_proc = PTLRPC_GSS_PROC_DATA;
2667 cli_gctx->gc_win = GSS_SEQ_WIN;
2669 /* The problem is the reverse ctx might get lost in some recovery
2670 * situations, and the same svc_ctx will be used to re-create it.
2671 * if there's callback be sentout before that, new reverse ctx start
2672 * with sequence 0 will lead to future callback rpc be treated as
2675 * each reverse root ctx will record its latest sequence number on its
2676 * buddy svcctx before be destroied, so here we continue use it.
2678 atomic_set(&cli_gctx->gc_seq, svc_gctx->gsc_rvs_seq);
2680 if (gss_svc_upcall_dup_handle(&cli_gctx->gc_svc_handle, svc_gctx)) {
2681 CERROR("failed to dup svc handle\n");
2685 if (lgss_copy_reverse_context(svc_gctx->gsc_mechctx, &mechctx) !=
2687 CERROR("failed to copy mech context\n");
2688 goto err_svc_handle;
2691 if (rawobj_dup(&cli_gctx->gc_handle, &svc_gctx->gsc_rvs_hdl)) {
2692 CERROR("failed to dup reverse handle\n");
2696 cli_gctx->gc_mechctx = mechctx;
2697 gss_cli_ctx_uptodate(cli_gctx);
2702 lgss_delete_sec_context(&mechctx);
2704 rawobj_free(&cli_gctx->gc_svc_handle);
2709 int __init sptlrpc_gss_init(void)
2713 rc = gss_init_lproc();
2717 rc = gss_init_cli_upcall();
2721 rc = gss_init_svc_upcall();
2723 goto out_cli_upcall;
2725 rc = init_kerberos_module();
2727 goto out_svc_upcall;
2729 /* register policy after all other stuff be intialized, because it
2730 * might be in used immediately after the registration. */
2732 rc = gss_init_keyring();
2736 #ifdef HAVE_GSS_PIPEFS
2737 rc = gss_init_pipefs();
2744 #ifdef HAVE_GSS_PIPEFS
2750 cleanup_kerberos_module();
2752 gss_exit_svc_upcall();
2754 gss_exit_cli_upcall();
2760 static void __exit sptlrpc_gss_exit(void)
2763 #ifdef HAVE_GSS_PIPEFS
2766 cleanup_kerberos_module();
2767 gss_exit_svc_upcall();
2768 gss_exit_cli_upcall();
2772 MODULE_AUTHOR("Cluster File Systems, Inc. <info@clusterfs.com>");
2773 MODULE_DESCRIPTION("GSS security policy for Lustre");
2774 MODULE_LICENSE("GPL");
2776 module_init(sptlrpc_gss_init);
2777 module_exit(sptlrpc_gss_exit);