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_version);
98 __swab32s(&ghdr->gh_flags);
99 __swab32s(&ghdr->gh_proc);
100 __swab32s(&ghdr->gh_seq);
101 __swab32s(&ghdr->gh_svc);
102 __swab32s(&ghdr->gh_pad1);
103 __swab32s(&ghdr->gh_pad2);
104 __swab32s(&ghdr->gh_pad3);
105 __swab32s(&ghdr->gh_handle.len);
108 struct gss_header *gss_swab_header(struct lustre_msg *msg, int segment)
110 struct gss_header *ghdr;
112 ghdr = lustre_swab_buf(msg, segment, sizeof(*ghdr),
116 sizeof(*ghdr) + ghdr->gh_handle.len > msg->lm_buflens[segment]) {
117 CERROR("gss header require length %u, now %u received\n",
118 (unsigned int) sizeof(*ghdr) + ghdr->gh_handle.len,
119 msg->lm_buflens[segment]);
127 void gss_netobj_swabber(netobj_t *obj)
129 __swab32s(&obj->len);
132 netobj_t *gss_swab_netobj(struct lustre_msg *msg, int segment)
136 obj = lustre_swab_buf(msg, segment, sizeof(*obj), gss_netobj_swabber);
137 if (obj && sizeof(*obj) + obj->len > msg->lm_buflens[segment]) {
138 CERROR("netobj require length %u but only %u received\n",
139 (unsigned int) sizeof(*obj) + obj->len,
140 msg->lm_buflens[segment]);
148 * payload should be obtained from mechanism. but currently since we
149 * only support kerberos, we could simply use fixed value.
153 #define GSS_KRB5_INTEG_MAX_PAYLOAD (40)
156 int gss_estimate_payload(struct gss_ctx *mechctx, int msgsize, int privacy)
159 /* we suppose max cipher block size is 16 bytes. here we
160 * add 16 for confounder and 16 for padding. */
161 return GSS_KRB5_INTEG_MAX_PAYLOAD + msgsize + 16 + 16 + 16;
163 return GSS_KRB5_INTEG_MAX_PAYLOAD;
168 * return signature size, otherwise < 0 to indicate error
171 int gss_sign_msg(struct lustre_msg *msg,
172 struct gss_ctx *mechctx,
173 __u32 proc, __u32 seq, __u32 svc,
176 struct gss_header *ghdr;
177 rawobj_t text[3], mic;
178 int textcnt, max_textcnt, mic_idx;
181 LASSERT(msg->lm_bufcount >= 2);
184 LASSERT(msg->lm_buflens[0] >=
185 sizeof(*ghdr) + (handle ? handle->len : 0));
186 ghdr = lustre_msg_buf(msg, 0, 0);
188 ghdr->gh_version = PTLRPC_GSS_VERSION;
190 ghdr->gh_proc = proc;
194 /* fill in a fake one */
195 ghdr->gh_handle.len = 0;
197 ghdr->gh_handle.len = handle->len;
198 memcpy(ghdr->gh_handle.data, handle->data, handle->len);
201 /* no actual signature for null mode */
202 if (svc == SPTLRPC_SVC_NULL)
203 return lustre_msg_size_v2(msg->lm_bufcount, msg->lm_buflens);
206 mic_idx = msg_last_segidx(msg);
207 max_textcnt = (svc == SPTLRPC_SVC_AUTH) ? 1 : mic_idx;
209 for (textcnt = 0; textcnt < max_textcnt; textcnt++) {
210 text[textcnt].len = msg->lm_buflens[textcnt];
211 text[textcnt].data = lustre_msg_buf(msg, textcnt, 0);
214 mic.len = msg->lm_buflens[mic_idx];
215 mic.data = lustre_msg_buf(msg, mic_idx, 0);
217 major = lgss_get_mic(mechctx, textcnt, text, &mic);
218 if (major != GSS_S_COMPLETE) {
219 CERROR("fail to generate MIC: %08x\n", major);
222 LASSERT(mic.len <= msg->lm_buflens[mic_idx]);
224 return lustre_shrink_msg(msg, mic_idx, mic.len, 0);
231 __u32 gss_verify_msg(struct lustre_msg *msg,
232 struct gss_ctx *mechctx,
235 rawobj_t text[3], mic;
236 int textcnt, max_textcnt;
240 LASSERT(msg->lm_bufcount >= 2);
242 if (svc == SPTLRPC_SVC_NULL)
243 return GSS_S_COMPLETE;
245 mic_idx = msg_last_segidx(msg);
246 max_textcnt = (svc == SPTLRPC_SVC_AUTH) ? 1 : mic_idx;
248 for (textcnt = 0; textcnt < max_textcnt; textcnt++) {
249 text[textcnt].len = msg->lm_buflens[textcnt];
250 text[textcnt].data = lustre_msg_buf(msg, textcnt, 0);
253 mic.len = msg->lm_buflens[mic_idx];
254 mic.data = lustre_msg_buf(msg, mic_idx, 0);
256 major = lgss_verify_mic(mechctx, textcnt, text, &mic);
257 if (major != GSS_S_COMPLETE)
258 CERROR("mic verify error: %08x\n", major);
264 * return gss error code
267 __u32 gss_unseal_msg(struct gss_ctx *mechctx,
268 struct lustre_msg *msgbuf,
269 int *msg_len, int msgbuf_len)
271 rawobj_t clear_obj, micobj, msgobj, token;
277 if (msgbuf->lm_bufcount != 3) {
278 CERROR("invalid bufcount %d\n", msgbuf->lm_bufcount);
279 RETURN(GSS_S_FAILURE);
282 /* verify gss header */
283 msgobj.len = msgbuf->lm_buflens[0];
284 msgobj.data = lustre_msg_buf(msgbuf, 0, 0);
285 micobj.len = msgbuf->lm_buflens[1];
286 micobj.data = lustre_msg_buf(msgbuf, 1, 0);
288 major = lgss_verify_mic(mechctx, 1, &msgobj, &micobj);
289 if (major != GSS_S_COMPLETE) {
290 CERROR("priv: mic verify error: %08x\n", major);
294 /* temporary clear text buffer */
295 clear_buflen = msgbuf->lm_buflens[2];
296 OBD_ALLOC(clear_buf, clear_buflen);
298 RETURN(GSS_S_FAILURE);
300 token.len = msgbuf->lm_buflens[2];
301 token.data = lustre_msg_buf(msgbuf, 2, 0);
303 clear_obj.len = clear_buflen;
304 clear_obj.data = clear_buf;
306 major = lgss_unwrap(mechctx, &token, &clear_obj);
307 if (major != GSS_S_COMPLETE) {
308 CERROR("priv: unwrap message error: %08x\n", major);
309 GOTO(out_free, major = GSS_S_FAILURE);
311 LASSERT(clear_obj.len <= clear_buflen);
313 /* now the decrypted message */
314 memcpy(msgbuf, clear_obj.data, clear_obj.len);
315 *msg_len = clear_obj.len;
317 major = GSS_S_COMPLETE;
319 OBD_FREE(clear_buf, clear_buflen);
323 /********************************************
324 * gss client context manipulation helpers *
325 ********************************************/
327 int cli_ctx_expire(struct ptlrpc_cli_ctx *ctx)
329 LASSERT(atomic_read(&ctx->cc_refcount));
331 if (!test_and_set_bit(PTLRPC_CTX_DEAD_BIT, &ctx->cc_flags)) {
334 if (!ctx->cc_early_expire)
335 clear_bit(PTLRPC_CTX_UPTODATE_BIT, &ctx->cc_flags);
337 now = cfs_time_current_sec();
338 if (ctx->cc_expire && cfs_time_aftereq(now, ctx->cc_expire))
339 CWARN("ctx %p(%u->%s): get expired (%lds exceeds)\n",
340 ctx, ctx->cc_vcred.vc_uid,
341 sec2target_str(ctx->cc_sec),
342 cfs_time_sub(now, ctx->cc_expire));
344 CWARN("ctx %p(%u->%s): force to die (%lds remains)\n",
345 ctx, ctx->cc_vcred.vc_uid,
346 sec2target_str(ctx->cc_sec),
347 ctx->cc_expire == 0 ? 0 :
348 cfs_time_sub(ctx->cc_expire, now));
356 * return 1 if the context is dead.
358 int cli_ctx_check_death(struct ptlrpc_cli_ctx *ctx)
360 if (unlikely(cli_ctx_is_dead(ctx)))
363 /* expire is 0 means never expire. a newly created gss context
364 * which during upcall may has 0 expiration */
365 if (ctx->cc_expire == 0)
368 /* check real expiration */
369 if (cfs_time_after(ctx->cc_expire, cfs_time_current_sec()))
376 void gss_cli_ctx_uptodate(struct gss_cli_ctx *gctx)
378 struct ptlrpc_cli_ctx *ctx = &gctx->gc_base;
379 unsigned long ctx_expiry;
381 if (lgss_inquire_context(gctx->gc_mechctx, &ctx_expiry)) {
382 CERROR("ctx %p(%u): unable to inquire, expire it now\n",
383 gctx, ctx->cc_vcred.vc_uid);
384 ctx_expiry = 1; /* make it expired now */
387 ctx->cc_expire = gss_round_ctx_expiry(ctx_expiry,
388 ctx->cc_sec->ps_flags);
390 /* At this point this ctx might have been marked as dead by
391 * someone else, in which case nobody will make further use
392 * of it. we don't care, and mark it UPTODATE will help
393 * destroying server side context when it be destroied. */
394 set_bit(PTLRPC_CTX_UPTODATE_BIT, &ctx->cc_flags);
396 if (sec_is_reverse(ctx->cc_sec))
397 CDEBUG(D_SEC, "server installed reverse ctx %p, "
398 "will expire at %lu(%lds lifetime)\n",
400 ctx->cc_expire - cfs_time_current_sec());
402 CWARN("client refreshed ctx %p(%u->%s), will expire at "
403 "%lu(%lds lifetime)\n", ctx, ctx->cc_vcred.vc_uid,
404 sec2target_str(ctx->cc_sec), ctx->cc_expire,
405 ctx->cc_expire - cfs_time_current_sec());
407 /* install reverse svc ctx, but only for forward connection
408 * and root context */
409 if (!sec_is_reverse(ctx->cc_sec) && ctx->cc_vcred.vc_uid == 0) {
410 gss_sec_install_rctx(ctx->cc_sec->ps_import,
416 void gss_cli_ctx_finalize(struct gss_cli_ctx *gctx)
418 if (gctx->gc_mechctx)
419 lgss_delete_sec_context(&gctx->gc_mechctx);
421 rawobj_free(&gctx->gc_handle);
425 * Based on sequence number algorithm as specified in RFC 2203.
427 * modified for our own problem: arriving request has valid sequence number,
428 * but unwrapping request might cost a long time, after that its sequence
429 * are not valid anymore (fall behind the window). It rarely happen, mostly
430 * under extreme load.
432 * note we should not check sequence before verify the integrity of incoming
433 * request, because just one attacking request with high sequence number might
434 * cause all following request be dropped.
436 * so here we use a multi-phase approach: prepare 2 sequence windows,
437 * "main window" for normal sequence and "back window" for fall behind sequence.
438 * and 3-phase checking mechanism:
439 * 0 - before integrity verification, perform a initial sequence checking in
440 * main window, which only try and don't actually set any bits. if the
441 * sequence is high above the window or fit in the window and the bit
442 * is 0, then accept and proceed to integrity verification. otherwise
443 * reject this sequence.
444 * 1 - after integrity verification, check in main window again. if this
445 * sequence is high above the window or fit in the window and the bit
446 * is 0, then set the bit and accept; if it fit in the window but bit
447 * already set, then reject; if it fall behind the window, then proceed
449 * 2 - check in back window. if it is high above the window or fit in the
450 * window and the bit is 0, then set the bit and accept. otherwise reject.
453 * 1: looks like a replay
457 * note phase 0 is necessary, because otherwise replay attacking request of
458 * sequence which between the 2 windows can't be detected.
460 * this mechanism can't totally solve the problem, but could help much less
461 * number of valid requests be dropped.
464 int gss_do_check_seq(unsigned long *window, __u32 win_size, __u32 *max_seq,
465 __u32 seq_num, int phase)
467 LASSERT(phase >= 0 && phase <= 2);
469 if (seq_num > *max_seq) {
471 * 1. high above the window
476 if (seq_num >= *max_seq + win_size) {
477 memset(window, 0, win_size / 8);
480 while(*max_seq < seq_num) {
482 __clear_bit((*max_seq) % win_size, window);
485 __set_bit(seq_num % win_size, window);
486 } else if (seq_num + win_size <= *max_seq) {
488 * 2. low behind the window
490 if (phase == 0 || phase == 2)
493 CWARN("seq %u is %u behind (size %d), check backup window\n",
494 seq_num, *max_seq - win_size - seq_num, win_size);
498 * 3. fit into the window
502 if (test_bit(seq_num % win_size, window))
507 if (__test_and_set_bit(seq_num % win_size, window))
516 CERROR("seq %u (%s %s window) is a replay: max %u, winsize %d\n",
518 seq_num + win_size > *max_seq ? "in" : "behind",
519 phase == 2 ? "backup " : "main",
525 * Based on sequence number algorithm as specified in RFC 2203.
527 * if @set == 0: initial check, don't set any bit in window
528 * if @sec == 1: final check, set bit in window
530 int gss_check_seq_num(struct gss_svc_seq_data *ssd, __u32 seq_num, int set)
534 spin_lock(&ssd->ssd_lock);
540 rc = gss_do_check_seq(ssd->ssd_win_main, GSS_SEQ_WIN_MAIN,
541 &ssd->ssd_max_main, seq_num, 0);
543 gss_stat_oos_record_svc(0, 1);
546 * phase 1 checking main window
548 rc = gss_do_check_seq(ssd->ssd_win_main, GSS_SEQ_WIN_MAIN,
549 &ssd->ssd_max_main, seq_num, 1);
552 gss_stat_oos_record_svc(1, 1);
558 * phase 2 checking back window
560 rc = gss_do_check_seq(ssd->ssd_win_back, GSS_SEQ_WIN_BACK,
561 &ssd->ssd_max_back, seq_num, 2);
563 gss_stat_oos_record_svc(2, 1);
565 gss_stat_oos_record_svc(2, 0);
568 spin_unlock(&ssd->ssd_lock);
572 /***************************************
574 ***************************************/
577 int gss_cli_payload(struct ptlrpc_cli_ctx *ctx,
578 int msgsize, int privacy)
580 return gss_estimate_payload(NULL, msgsize, privacy);
583 int gss_cli_ctx_match(struct ptlrpc_cli_ctx *ctx, struct vfs_cred *vcred)
585 return (ctx->cc_vcred.vc_uid == vcred->vc_uid);
588 void gss_cli_ctx_flags2str(unsigned long flags, char *buf, int bufsize)
592 if (flags & PTLRPC_CTX_NEW)
593 strncat(buf, "new,", bufsize);
594 if (flags & PTLRPC_CTX_UPTODATE)
595 strncat(buf, "uptodate,", bufsize);
596 if (flags & PTLRPC_CTX_DEAD)
597 strncat(buf, "dead,", bufsize);
598 if (flags & PTLRPC_CTX_ERROR)
599 strncat(buf, "error,", bufsize);
600 if (flags & PTLRPC_CTX_CACHED)
601 strncat(buf, "cached,", bufsize);
602 if (flags & PTLRPC_CTX_ETERNAL)
603 strncat(buf, "eternal,", bufsize);
605 strncat(buf, "-,", bufsize);
607 buf[strlen(buf) - 1] = '\0';
610 int gss_cli_ctx_sign(struct ptlrpc_cli_ctx *ctx,
611 struct ptlrpc_request *req)
613 struct gss_cli_ctx *gctx;
618 LASSERT(req->rq_reqbuf);
619 LASSERT(req->rq_reqbuf->lm_bufcount >= 2);
620 LASSERT(req->rq_cli_ctx == ctx);
622 /* nothing to do for context negotiation RPCs */
623 if (req->rq_ctx_init)
626 gctx = container_of(ctx, struct gss_cli_ctx, gc_base);
627 svc = SEC_FLAVOR_SVC(req->rq_sec_flavor);
629 seq = atomic_inc_return(&gctx->gc_seq);
631 rc = gss_sign_msg(req->rq_reqbuf, gctx->gc_mechctx,
632 gctx->gc_proc, seq, svc,
637 /* gss_sign_msg() msg might take long time to finish, in which period
638 * more rpcs could be wrapped up and sent out. if we found too many
639 * of them we should repack this rpc, because sent it too late might
640 * lead to the sequence number fall behind the window on server and
641 * be dropped. also applies to gss_cli_ctx_seal().
643 * Note: null mode dosen't check sequence number. */
644 if (svc != SPTLRPC_SVC_NULL &&
645 atomic_read(&gctx->gc_seq) - seq > GSS_SEQ_REPACK_THRESHOLD) {
646 int behind = atomic_read(&gctx->gc_seq) - seq;
648 gss_stat_oos_record_cli(behind);
649 CWARN("req %p: %u behind, retry signing\n", req, behind);
653 req->rq_reqdata_len = rc;
658 int gss_cli_ctx_handle_err_notify(struct ptlrpc_cli_ctx *ctx,
659 struct ptlrpc_request *req,
660 struct gss_header *ghdr)
662 struct gss_err_header *errhdr;
665 LASSERT(ghdr->gh_proc == PTLRPC_GSS_PROC_ERR);
667 errhdr = (struct gss_err_header *) ghdr;
669 /* server return NO_CONTEXT might be caused by context expire
670 * or server reboot/failover. we refresh the cred transparently
672 * In some cases, our gss handle is possible to be incidentally
673 * identical to another handle since the handle itself is not
674 * fully random. In krb5 case, the GSS_S_BAD_SIG will be
675 * returned, maybe other gss error for other mechanism.
677 * if we add new mechanism, make sure the correct error are
678 * returned in this case.
680 * but in any cases, don't resend ctx destroying rpc, don't resend
682 if (req->rq_ctx_fini) {
683 CWARN("server respond error (%08x/%08x) for ctx fini\n",
684 errhdr->gh_major, errhdr->gh_minor);
686 } else if (sec_is_reverse(ctx->cc_sec)) {
687 CWARN("reverse server respond error (%08x/%08x)\n",
688 errhdr->gh_major, errhdr->gh_minor);
690 } else if (errhdr->gh_major == GSS_S_NO_CONTEXT ||
691 errhdr->gh_major == GSS_S_BAD_SIG) {
692 CWARN("req x"LPU64"/t"LPU64": server respond ctx %p(%u->%s) "
693 "%s, server might lost the context.\n",
694 req->rq_xid, req->rq_transno, ctx, ctx->cc_vcred.vc_uid,
695 sec2target_str(ctx->cc_sec),
696 errhdr->gh_major == GSS_S_NO_CONTEXT ?
697 "NO_CONTEXT" : "BAD_SIG");
699 sptlrpc_cli_ctx_expire(ctx);
701 /* we need replace the ctx right here, otherwise during
702 * resent we'll hit the logic in sptlrpc_req_refresh_ctx()
703 * which keep the ctx with RESEND flag, thus we'll never
704 * get rid of this ctx. */
705 rc = sptlrpc_req_replace_dead_ctx(req);
709 CERROR("req %p: server report gss error (%x/%x)\n",
710 req, errhdr->gh_major, errhdr->gh_minor);
717 int gss_cli_ctx_verify(struct ptlrpc_cli_ctx *ctx,
718 struct ptlrpc_request *req)
720 struct gss_cli_ctx *gctx;
721 struct gss_header *ghdr, *reqhdr;
722 struct lustre_msg *msg = req->rq_repbuf;
727 LASSERT(req->rq_cli_ctx == ctx);
730 req->rq_repdata_len = req->rq_nob_received;
731 gctx = container_of(ctx, struct gss_cli_ctx, gc_base);
733 /* special case for context negotiation, rq_repmsg/rq_replen actually
734 * are not used currently. */
735 if (req->rq_ctx_init) {
736 req->rq_repmsg = lustre_msg_buf(msg, 1, 0);
737 req->rq_replen = msg->lm_buflens[1];
741 if (msg->lm_bufcount < 2 || msg->lm_bufcount > 4) {
742 CERROR("unexpected bufcount %u\n", msg->lm_bufcount);
746 ghdr = gss_swab_header(msg, 0);
748 CERROR("can't decode gss header\n");
753 reqhdr = lustre_msg_buf(msg, 0, sizeof(*reqhdr));
756 if (ghdr->gh_version != reqhdr->gh_version) {
757 CERROR("gss version %u mismatch, expect %u\n",
758 ghdr->gh_version, reqhdr->gh_version);
762 switch (ghdr->gh_proc) {
763 case PTLRPC_GSS_PROC_DATA:
764 if (ghdr->gh_seq != reqhdr->gh_seq) {
765 CERROR("seqnum %u mismatch, expect %u\n",
766 ghdr->gh_seq, reqhdr->gh_seq);
770 if (ghdr->gh_svc != reqhdr->gh_svc) {
771 CERROR("svc %u mismatch, expect %u\n",
772 ghdr->gh_svc, reqhdr->gh_svc);
776 if (lustre_msg_swabbed(msg))
777 gss_header_swabber(ghdr);
779 major = gss_verify_msg(msg, gctx->gc_mechctx, reqhdr->gh_svc);
780 if (major != GSS_S_COMPLETE)
783 req->rq_repmsg = lustre_msg_buf(msg, 1, 0);
784 req->rq_replen = msg->lm_buflens[1];
786 if (SEC_FLAVOR_HAS_BULK(req->rq_sec_flavor)) {
787 if (msg->lm_bufcount < 4) {
788 CERROR("Invalid reply bufcount %u\n",
793 /* bulk checksum is the second last segment */
794 rc = bulk_sec_desc_unpack(msg, msg->lm_bufcount - 2);
797 case PTLRPC_GSS_PROC_ERR:
798 rc = gss_cli_ctx_handle_err_notify(ctx, req, ghdr);
801 CERROR("unknown gss proc %d\n", ghdr->gh_proc);
808 int gss_cli_ctx_seal(struct ptlrpc_cli_ctx *ctx,
809 struct ptlrpc_request *req)
811 struct gss_cli_ctx *gctx;
812 rawobj_t msgobj, cipher_obj, micobj;
813 struct gss_header *ghdr;
814 int buflens[3], wiresize, rc;
818 LASSERT(req->rq_clrbuf);
819 LASSERT(req->rq_cli_ctx == ctx);
820 LASSERT(req->rq_reqlen);
822 gctx = container_of(ctx, struct gss_cli_ctx, gc_base);
824 /* close clear data length */
825 req->rq_clrdata_len = lustre_msg_size_v2(req->rq_clrbuf->lm_bufcount,
826 req->rq_clrbuf->lm_buflens);
828 /* calculate wire data length */
829 buflens[0] = PTLRPC_GSS_HEADER_SIZE;
830 buflens[1] = gss_cli_payload(&gctx->gc_base, buflens[0], 0);
831 buflens[2] = gss_cli_payload(&gctx->gc_base, req->rq_clrdata_len, 1);
832 wiresize = lustre_msg_size_v2(3, buflens);
834 /* allocate wire buffer */
837 LASSERT(req->rq_reqbuf);
838 LASSERT(req->rq_reqbuf != req->rq_clrbuf);
839 LASSERT(req->rq_reqbuf_len >= wiresize);
841 OBD_ALLOC(req->rq_reqbuf, wiresize);
844 req->rq_reqbuf_len = wiresize;
847 lustre_init_msg_v2(req->rq_reqbuf, 3, buflens, NULL);
848 req->rq_reqbuf->lm_secflvr = req->rq_sec_flavor;
851 ghdr = lustre_msg_buf(req->rq_reqbuf, 0, 0);
852 ghdr->gh_version = PTLRPC_GSS_VERSION;
854 ghdr->gh_proc = gctx->gc_proc;
855 ghdr->gh_seq = atomic_inc_return(&gctx->gc_seq);
856 ghdr->gh_svc = SPTLRPC_SVC_PRIV;
857 ghdr->gh_handle.len = gctx->gc_handle.len;
858 memcpy(ghdr->gh_handle.data, gctx->gc_handle.data, gctx->gc_handle.len);
861 /* header signature */
862 msgobj.len = req->rq_reqbuf->lm_buflens[0];
863 msgobj.data = lustre_msg_buf(req->rq_reqbuf, 0, 0);
864 micobj.len = req->rq_reqbuf->lm_buflens[1];
865 micobj.data = lustre_msg_buf(req->rq_reqbuf, 1, 0);
867 major = lgss_get_mic(gctx->gc_mechctx, 1, &msgobj, &micobj);
868 if (major != GSS_S_COMPLETE) {
869 CERROR("priv: sign message error: %08x\n", major);
870 GOTO(err_free, rc = -EPERM);
872 /* perhaps shrink msg has potential problem in re-packing???
873 * ship a little bit more data is fine.
874 lustre_shrink_msg(req->rq_reqbuf, 1, micobj.len, 0);
878 msgobj.len = req->rq_clrdata_len;
879 msgobj.data = (__u8 *) req->rq_clrbuf;
882 cipher_obj.len = req->rq_reqbuf->lm_buflens[2];
883 cipher_obj.data = lustre_msg_buf(req->rq_reqbuf, 2, 0);
885 major = lgss_wrap(gctx->gc_mechctx, &msgobj, req->rq_clrbuf_len,
887 if (major != GSS_S_COMPLETE) {
888 CERROR("priv: wrap message error: %08x\n", major);
889 GOTO(err_free, rc = -EPERM);
891 LASSERT(cipher_obj.len <= buflens[2]);
893 /* see explain in gss_cli_ctx_sign() */
894 if (atomic_read(&gctx->gc_seq) - ghdr->gh_seq >
895 GSS_SEQ_REPACK_THRESHOLD) {
896 int behind = atomic_read(&gctx->gc_seq) - ghdr->gh_seq;
898 gss_stat_oos_record_cli(behind);
899 CWARN("req %p: %u behind, retry sealing\n", req, behind);
901 ghdr->gh_seq = atomic_inc_return(&gctx->gc_seq);
905 /* now set the final wire data length */
906 req->rq_reqdata_len = lustre_shrink_msg(req->rq_reqbuf, 2,
913 OBD_FREE(req->rq_reqbuf, req->rq_reqbuf_len);
914 req->rq_reqbuf = NULL;
915 req->rq_reqbuf_len = 0;
920 int gss_cli_ctx_unseal(struct ptlrpc_cli_ctx *ctx,
921 struct ptlrpc_request *req)
923 struct gss_cli_ctx *gctx;
924 struct gss_header *ghdr;
929 LASSERT(req->rq_repbuf);
930 LASSERT(req->rq_cli_ctx == ctx);
932 gctx = container_of(ctx, struct gss_cli_ctx, gc_base);
934 ghdr = gss_swab_header(req->rq_repbuf, 0);
936 CERROR("can't decode gss header\n");
941 if (ghdr->gh_version != PTLRPC_GSS_VERSION) {
942 CERROR("gss version %u mismatch, expect %u\n",
943 ghdr->gh_version, PTLRPC_GSS_VERSION);
947 switch (ghdr->gh_proc) {
948 case PTLRPC_GSS_PROC_DATA:
949 if (lustre_msg_swabbed(req->rq_repbuf))
950 gss_header_swabber(ghdr);
952 major = gss_unseal_msg(gctx->gc_mechctx, req->rq_repbuf,
953 &msglen, req->rq_repbuf_len);
954 if (major != GSS_S_COMPLETE) {
959 if (lustre_unpack_msg(req->rq_repbuf, msglen)) {
960 CERROR("Failed to unpack after decryption\n");
963 req->rq_repdata_len = msglen;
965 if (req->rq_repbuf->lm_bufcount < 1) {
966 CERROR("Invalid reply buffer: empty\n");
970 if (SEC_FLAVOR_HAS_BULK(req->rq_sec_flavor)) {
971 if (req->rq_repbuf->lm_bufcount < 2) {
972 CERROR("Too few request buffer segments %d\n",
973 req->rq_repbuf->lm_bufcount);
977 /* bulk checksum is the last segment */
978 if (bulk_sec_desc_unpack(req->rq_repbuf,
979 req->rq_repbuf->lm_bufcount-1))
983 req->rq_repmsg = lustre_msg_buf(req->rq_repbuf, 0, 0);
984 req->rq_replen = req->rq_repbuf->lm_buflens[0];
988 case PTLRPC_GSS_PROC_ERR:
989 rc = gss_cli_ctx_handle_err_notify(ctx, req, ghdr);
992 CERROR("unexpected proc %d\n", ghdr->gh_proc);
999 /*********************************************
1000 * reverse context installation *
1001 *********************************************/
1004 int gss_install_rvs_svc_ctx(struct obd_import *imp,
1005 struct gss_sec *gsec,
1006 struct gss_cli_ctx *gctx)
1008 return gss_svc_upcall_install_rvs_ctx(imp, gsec, gctx);
1011 /*********************************************
1012 * GSS security APIs *
1013 *********************************************/
1014 int gss_sec_create_common(struct gss_sec *gsec,
1015 struct ptlrpc_sec_policy *policy,
1016 struct obd_import *imp,
1017 struct ptlrpc_svc_ctx *ctx,
1019 unsigned long flags)
1021 struct ptlrpc_sec *sec;
1024 LASSERT(SEC_FLAVOR_POLICY(flavor) == SPTLRPC_POLICY_GSS);
1026 gsec->gs_mech = lgss_subflavor_to_mech(SEC_FLAVOR_SUB(flavor));
1027 if (!gsec->gs_mech) {
1028 CERROR("gss backend 0x%x not found\n", SEC_FLAVOR_SUB(flavor));
1032 spin_lock_init(&gsec->gs_lock);
1033 gsec->gs_rvs_hdl = 0ULL;
1035 /* initialize upper ptlrpc_sec */
1036 sec = &gsec->gs_base;
1037 sec->ps_policy = policy;
1038 sec->ps_flavor = flavor;
1039 sec->ps_flags = flags;
1040 sec->ps_import = class_import_get(imp);
1041 sec->ps_lock = SPIN_LOCK_UNLOCKED;
1042 atomic_set(&sec->ps_busy, 0);
1043 CFS_INIT_LIST_HEAD(&sec->ps_gc_list);
1046 sec->ps_gc_interval = GSS_GC_INTERVAL;
1047 sec->ps_gc_next = cfs_time_current_sec() + sec->ps_gc_interval;
1049 LASSERT(sec_is_reverse(sec));
1051 /* never do gc on reverse sec */
1052 sec->ps_gc_interval = 0;
1053 sec->ps_gc_next = 0;
1056 if (SEC_FLAVOR_SVC(flavor) == SPTLRPC_SVC_PRIV &&
1057 flags & PTLRPC_SEC_FL_BULK)
1058 sptlrpc_enc_pool_add_user();
1060 CDEBUG(D_SEC, "create %s%s@%p\n", (ctx ? "reverse " : ""),
1061 policy->sp_name, gsec);
1065 void gss_sec_destroy_common(struct gss_sec *gsec)
1067 struct ptlrpc_sec *sec = &gsec->gs_base;
1070 LASSERT(sec->ps_import);
1071 LASSERT(atomic_read(&sec->ps_refcount) == 0);
1072 LASSERT(atomic_read(&sec->ps_busy) == 0);
1074 if (gsec->gs_mech) {
1075 lgss_mech_put(gsec->gs_mech);
1076 gsec->gs_mech = NULL;
1079 class_import_put(sec->ps_import);
1081 if (SEC_FLAVOR_SVC(sec->ps_flavor) == SPTLRPC_SVC_PRIV &&
1082 sec->ps_flags & PTLRPC_SEC_FL_BULK)
1083 sptlrpc_enc_pool_del_user();
1088 int gss_cli_ctx_init_common(struct ptlrpc_sec *sec,
1089 struct ptlrpc_cli_ctx *ctx,
1090 struct ptlrpc_ctx_ops *ctxops,
1091 struct vfs_cred *vcred)
1093 struct gss_cli_ctx *gctx = ctx2gctx(ctx);
1096 atomic_set(&gctx->gc_seq, 0);
1098 CFS_INIT_HLIST_NODE(&ctx->cc_cache);
1099 atomic_set(&ctx->cc_refcount, 0);
1101 ctx->cc_ops = ctxops;
1103 ctx->cc_flags = PTLRPC_CTX_NEW;
1104 ctx->cc_vcred = *vcred;
1105 spin_lock_init(&ctx->cc_lock);
1106 CFS_INIT_LIST_HEAD(&ctx->cc_req_list);
1107 CFS_INIT_LIST_HEAD(&ctx->cc_gc_chain);
1109 /* take a ref on belonging sec */
1110 atomic_inc(&sec->ps_busy);
1112 CDEBUG(D_SEC, "%s@%p: create ctx %p(%u->%s)\n",
1113 sec->ps_policy->sp_name, ctx->cc_sec,
1114 ctx, ctx->cc_vcred.vc_uid, sec2target_str(ctx->cc_sec));
1119 * return 1 if the busy count of the sec dropped to zero, then usually caller
1120 * should destroy the sec too; otherwise return 0.
1122 int gss_cli_ctx_fini_common(struct ptlrpc_sec *sec,
1123 struct ptlrpc_cli_ctx *ctx)
1125 struct gss_cli_ctx *gctx = ctx2gctx(ctx);
1127 LASSERT(ctx->cc_sec == sec);
1128 LASSERT(atomic_read(&ctx->cc_refcount) == 0);
1129 LASSERT(atomic_read(&sec->ps_busy) > 0);
1131 if (gctx->gc_mechctx) {
1132 gss_do_ctx_fini_rpc(gctx);
1133 gss_cli_ctx_finalize(gctx);
1136 if (sec_is_reverse(sec))
1137 CDEBUG(D_SEC, "reverse sec %p: destroy ctx %p\n",
1140 CWARN("%s@%p: destroy ctx %p(%u->%s)\n",
1141 sec->ps_policy->sp_name, ctx->cc_sec,
1142 ctx, ctx->cc_vcred.vc_uid, sec2target_str(ctx->cc_sec));
1144 if (atomic_dec_and_test(&sec->ps_busy)) {
1145 LASSERT(atomic_read(&sec->ps_refcount) == 0);
1153 int gss_alloc_reqbuf_intg(struct ptlrpc_sec *sec,
1154 struct ptlrpc_request *req,
1155 int svc, int msgsize)
1157 struct sec_flavor_config *conf;
1158 int bufsize, txtsize;
1159 int buflens[5], bufcnt = 2;
1163 * on-wire data layout:
1166 * - user descriptor (optional)
1167 * - bulk sec descriptor (optional)
1168 * - signature (optional)
1169 * - svc == NULL: NULL
1170 * - svc == AUTH: signature of gss header
1171 * - svc == INTG: signature of all above
1173 * if this is context negotiation, reserver fixed space
1174 * at the last (signature) segment regardless of svc mode.
1177 buflens[0] = PTLRPC_GSS_HEADER_SIZE;
1178 txtsize = buflens[0];
1180 buflens[1] = msgsize;
1181 if (svc == SPTLRPC_SVC_INTG)
1182 txtsize += buflens[1];
1184 if (SEC_FLAVOR_HAS_USER(req->rq_sec_flavor)) {
1185 buflens[bufcnt] = sptlrpc_current_user_desc_size();
1186 if (svc == SPTLRPC_SVC_INTG)
1187 txtsize += buflens[bufcnt];
1191 if (SEC_FLAVOR_HAS_BULK(req->rq_sec_flavor)) {
1192 conf = &req->rq_import->imp_obd->u.cli.cl_sec_conf;
1193 buflens[bufcnt] = bulk_sec_desc_size(conf->sfc_bulk_csum, 1,
1195 if (svc == SPTLRPC_SVC_INTG)
1196 txtsize += buflens[bufcnt];
1200 if (req->rq_ctx_init)
1201 buflens[bufcnt++] = GSS_CTX_INIT_MAX_LEN;
1202 else if (svc != SPTLRPC_SVC_NULL)
1203 buflens[bufcnt++] = gss_cli_payload(req->rq_cli_ctx, txtsize,0);
1205 bufsize = lustre_msg_size_v2(bufcnt, buflens);
1207 if (!req->rq_reqbuf) {
1208 bufsize = size_roundup_power2(bufsize);
1210 OBD_ALLOC(req->rq_reqbuf, bufsize);
1211 if (!req->rq_reqbuf)
1214 req->rq_reqbuf_len = bufsize;
1216 LASSERT(req->rq_pool);
1217 LASSERT(req->rq_reqbuf_len >= bufsize);
1218 memset(req->rq_reqbuf, 0, bufsize);
1221 lustre_init_msg_v2(req->rq_reqbuf, bufcnt, buflens, NULL);
1222 req->rq_reqbuf->lm_secflvr = req->rq_sec_flavor;
1224 req->rq_reqmsg = lustre_msg_buf(req->rq_reqbuf, 1, msgsize);
1225 LASSERT(req->rq_reqmsg);
1227 /* pack user desc here, later we might leave current user's process */
1228 if (SEC_FLAVOR_HAS_USER(req->rq_sec_flavor))
1229 sptlrpc_pack_user_desc(req->rq_reqbuf, 2);
1235 int gss_alloc_reqbuf_priv(struct ptlrpc_sec *sec,
1236 struct ptlrpc_request *req,
1239 struct sec_flavor_config *conf;
1240 int ibuflens[3], ibufcnt;
1242 int clearsize, wiresize;
1245 LASSERT(req->rq_clrbuf == NULL);
1246 LASSERT(req->rq_clrbuf_len == 0);
1248 /* Inner (clear) buffers
1250 * - user descriptor (optional)
1251 * - bulk checksum (optional)
1255 ibuflens[0] = msgsize;
1257 if (SEC_FLAVOR_HAS_USER(req->rq_sec_flavor))
1258 ibuflens[ibufcnt++] = sptlrpc_current_user_desc_size();
1259 if (SEC_FLAVOR_HAS_BULK(req->rq_sec_flavor)) {
1260 conf = &req->rq_import->imp_obd->u.cli.cl_sec_conf;
1261 ibuflens[ibufcnt++] = bulk_sec_desc_size(conf->sfc_bulk_csum, 1,
1264 clearsize = lustre_msg_size_v2(ibufcnt, ibuflens);
1265 /* to allow append padding during encryption */
1266 clearsize += GSS_MAX_CIPHER_BLOCK;
1268 /* Wrapper (wire) buffers
1270 * - signature of gss header
1274 buflens[0] = PTLRPC_GSS_HEADER_SIZE;
1275 buflens[1] = gss_cli_payload(req->rq_cli_ctx, buflens[0], 0);
1276 buflens[2] = gss_cli_payload(req->rq_cli_ctx, clearsize, 1);
1277 wiresize = lustre_msg_size_v2(3, buflens);
1280 /* rq_reqbuf is preallocated */
1281 LASSERT(req->rq_reqbuf);
1282 LASSERT(req->rq_reqbuf_len >= wiresize);
1284 memset(req->rq_reqbuf, 0, req->rq_reqbuf_len);
1286 /* if the pre-allocated buffer is big enough, we just pack
1287 * both clear buf & request buf in it, to avoid more alloc. */
1288 if (clearsize + wiresize <= req->rq_reqbuf_len) {
1290 (void *) (((char *) req->rq_reqbuf) + wiresize);
1292 CWARN("pre-allocated buf size %d is not enough for "
1293 "both clear (%d) and cipher (%d) text, proceed "
1294 "with extra allocation\n", req->rq_reqbuf_len,
1295 clearsize, wiresize);
1299 if (!req->rq_clrbuf) {
1300 clearsize = size_roundup_power2(clearsize);
1302 OBD_ALLOC(req->rq_clrbuf, clearsize);
1303 if (!req->rq_clrbuf)
1306 req->rq_clrbuf_len = clearsize;
1308 lustre_init_msg_v2(req->rq_clrbuf, ibufcnt, ibuflens, NULL);
1309 req->rq_reqmsg = lustre_msg_buf(req->rq_clrbuf, 0, msgsize);
1311 if (SEC_FLAVOR_HAS_USER(req->rq_sec_flavor))
1312 sptlrpc_pack_user_desc(req->rq_clrbuf, 1);
1318 * NOTE: any change of request buffer allocation should also consider
1319 * changing enlarge_reqbuf() series functions.
1321 int gss_alloc_reqbuf(struct ptlrpc_sec *sec,
1322 struct ptlrpc_request *req,
1325 int svc = SEC_FLAVOR_SVC(req->rq_sec_flavor);
1327 LASSERT(!SEC_FLAVOR_HAS_BULK(req->rq_sec_flavor) ||
1328 (req->rq_bulk_read || req->rq_bulk_write));
1331 case SPTLRPC_SVC_NULL:
1332 case SPTLRPC_SVC_AUTH:
1333 case SPTLRPC_SVC_INTG:
1334 return gss_alloc_reqbuf_intg(sec, req, svc, msgsize);
1335 case SPTLRPC_SVC_PRIV:
1336 return gss_alloc_reqbuf_priv(sec, req, msgsize);
1338 LASSERTF(0, "bad flavor %x\n", req->rq_sec_flavor);
1343 void gss_free_reqbuf(struct ptlrpc_sec *sec,
1344 struct ptlrpc_request *req)
1349 LASSERT(!req->rq_pool || req->rq_reqbuf);
1350 privacy = SEC_FLAVOR_SVC(req->rq_sec_flavor) == SPTLRPC_SVC_PRIV;
1352 if (!req->rq_clrbuf)
1353 goto release_reqbuf;
1355 /* release clear buffer */
1357 LASSERT(req->rq_clrbuf_len);
1360 req->rq_clrbuf >= req->rq_reqbuf &&
1361 (char *) req->rq_clrbuf <
1362 (char *) req->rq_reqbuf + req->rq_reqbuf_len)
1363 goto release_reqbuf;
1365 OBD_FREE(req->rq_clrbuf, req->rq_clrbuf_len);
1366 req->rq_clrbuf = NULL;
1367 req->rq_clrbuf_len = 0;
1370 if (!req->rq_pool && req->rq_reqbuf) {
1371 OBD_FREE(req->rq_reqbuf, req->rq_reqbuf_len);
1372 req->rq_reqbuf = NULL;
1373 req->rq_reqbuf_len = 0;
1379 static int do_alloc_repbuf(struct ptlrpc_request *req, int bufsize)
1381 bufsize = size_roundup_power2(bufsize);
1383 OBD_ALLOC(req->rq_repbuf, bufsize);
1384 if (!req->rq_repbuf)
1387 req->rq_repbuf_len = bufsize;
1392 int gss_alloc_repbuf_intg(struct ptlrpc_sec *sec,
1393 struct ptlrpc_request *req,
1394 int svc, int msgsize)
1396 struct sec_flavor_config *conf;
1398 int buflens[4], bufcnt = 2;
1401 * on-wire data layout:
1404 * - bulk sec descriptor (optional)
1405 * - signature (optional)
1406 * - svc == NULL: NULL
1407 * - svc == AUTH: signature of gss header
1408 * - svc == INTG: signature of all above
1410 * if this is context negotiation, reserver fixed space
1411 * at the last (signature) segment regardless of svc mode.
1414 buflens[0] = PTLRPC_GSS_HEADER_SIZE;
1415 txtsize = buflens[0];
1417 buflens[1] = msgsize;
1418 if (svc == SPTLRPC_SVC_INTG)
1419 txtsize += buflens[1];
1421 if (SEC_FLAVOR_HAS_BULK(req->rq_sec_flavor)) {
1422 conf = &req->rq_import->imp_obd->u.cli.cl_sec_conf;
1423 buflens[bufcnt] = bulk_sec_desc_size(conf->sfc_bulk_csum, 0,
1425 if (svc == SPTLRPC_SVC_INTG)
1426 txtsize += buflens[bufcnt];
1430 if (req->rq_ctx_init)
1431 buflens[bufcnt++] = GSS_CTX_INIT_MAX_LEN;
1432 else if (svc != SPTLRPC_SVC_NULL)
1433 buflens[bufcnt++] = gss_cli_payload(req->rq_cli_ctx, txtsize,0);
1435 return do_alloc_repbuf(req, lustre_msg_size_v2(bufcnt, buflens));
1439 int gss_alloc_repbuf_priv(struct ptlrpc_sec *sec,
1440 struct ptlrpc_request *req,
1443 struct sec_flavor_config *conf;
1445 int buflens[3], bufcnt;
1447 /* Inner (clear) buffers
1449 * - bulk checksum (optional)
1453 buflens[0] = msgsize;
1455 if (SEC_FLAVOR_HAS_BULK(req->rq_sec_flavor)) {
1456 conf = &req->rq_import->imp_obd->u.cli.cl_sec_conf;
1457 buflens[bufcnt++] = bulk_sec_desc_size(
1458 conf->sfc_bulk_csum, 0,
1461 txtsize = lustre_msg_size_v2(bufcnt, buflens);
1462 txtsize += GSS_MAX_CIPHER_BLOCK;
1464 /* Wrapper (wire) buffers
1466 * - signature of gss header
1471 buflens[0] = PTLRPC_GSS_HEADER_SIZE;
1472 buflens[1] = gss_cli_payload(req->rq_cli_ctx, buflens[0], 0);
1473 buflens[2] = gss_cli_payload(req->rq_cli_ctx, txtsize, 1);
1475 return do_alloc_repbuf(req, lustre_msg_size_v2(bufcnt, buflens));
1478 int gss_alloc_repbuf(struct ptlrpc_sec *sec,
1479 struct ptlrpc_request *req,
1482 int svc = SEC_FLAVOR_SVC(req->rq_sec_flavor);
1485 LASSERT(!SEC_FLAVOR_HAS_BULK(req->rq_sec_flavor) ||
1486 (req->rq_bulk_read || req->rq_bulk_write));
1489 case SPTLRPC_SVC_NULL:
1490 case SPTLRPC_SVC_AUTH:
1491 case SPTLRPC_SVC_INTG:
1492 return gss_alloc_repbuf_intg(sec, req, svc, msgsize);
1493 case SPTLRPC_SVC_PRIV:
1494 return gss_alloc_repbuf_priv(sec, req, msgsize);
1496 LASSERTF(0, "bad flavor %x\n", req->rq_sec_flavor);
1501 void gss_free_repbuf(struct ptlrpc_sec *sec,
1502 struct ptlrpc_request *req)
1504 OBD_FREE(req->rq_repbuf, req->rq_repbuf_len);
1505 req->rq_repbuf = NULL;
1506 req->rq_repbuf_len = 0;
1509 static int get_enlarged_msgsize(struct lustre_msg *msg,
1510 int segment, int newsize)
1512 int save, newmsg_size;
1514 LASSERT(newsize >= msg->lm_buflens[segment]);
1516 save = msg->lm_buflens[segment];
1517 msg->lm_buflens[segment] = newsize;
1518 newmsg_size = lustre_msg_size_v2(msg->lm_bufcount, msg->lm_buflens);
1519 msg->lm_buflens[segment] = save;
1524 static int get_enlarged_msgsize2(struct lustre_msg *msg,
1525 int segment1, int newsize1,
1526 int segment2, int newsize2)
1528 int save1, save2, newmsg_size;
1530 LASSERT(newsize1 >= msg->lm_buflens[segment1]);
1531 LASSERT(newsize2 >= msg->lm_buflens[segment2]);
1533 save1 = msg->lm_buflens[segment1];
1534 save2 = msg->lm_buflens[segment2];
1535 msg->lm_buflens[segment1] = newsize1;
1536 msg->lm_buflens[segment2] = newsize2;
1537 newmsg_size = lustre_msg_size_v2(msg->lm_bufcount, msg->lm_buflens);
1538 msg->lm_buflens[segment1] = save1;
1539 msg->lm_buflens[segment2] = save2;
1545 int gss_enlarge_reqbuf_intg(struct ptlrpc_sec *sec,
1546 struct ptlrpc_request *req,
1548 int segment, int newsize)
1550 struct lustre_msg *newbuf;
1551 int txtsize, sigsize = 0, i;
1552 int newmsg_size, newbuf_size;
1555 * gss header is at seg 0;
1556 * embedded msg is at seg 1;
1557 * signature (if any) is at the last seg
1559 LASSERT(req->rq_reqbuf);
1560 LASSERT(req->rq_reqbuf_len > req->rq_reqlen);
1561 LASSERT(req->rq_reqbuf->lm_bufcount >= 2);
1562 LASSERT(lustre_msg_buf(req->rq_reqbuf, 1, 0) == req->rq_reqmsg);
1564 /* 1. compute new embedded msg size */
1565 newmsg_size = get_enlarged_msgsize(req->rq_reqmsg, segment, newsize);
1566 LASSERT(newmsg_size >= req->rq_reqbuf->lm_buflens[1]);
1568 /* 2. compute new wrapper msg size */
1569 if (svc == SPTLRPC_SVC_NULL) {
1570 /* no signature, get size directly */
1571 newbuf_size = get_enlarged_msgsize(req->rq_reqbuf,
1574 txtsize = req->rq_reqbuf->lm_buflens[0];
1576 if (svc == SPTLRPC_SVC_INTG) {
1577 for (i = 1; i < req->rq_reqbuf->lm_bufcount; i++)
1578 txtsize += req->rq_reqbuf->lm_buflens[i];
1579 txtsize += newmsg_size - req->rq_reqbuf->lm_buflens[1];
1582 sigsize = gss_cli_payload(req->rq_cli_ctx, txtsize, 0);
1583 LASSERT(sigsize >= msg_last_seglen(req->rq_reqbuf));
1585 newbuf_size = get_enlarged_msgsize2(
1588 msg_last_segidx(req->rq_reqbuf),
1592 /* request from pool should always have enough buffer */
1593 LASSERT(!req->rq_pool || req->rq_reqbuf_len >= newbuf_size);
1595 if (req->rq_reqbuf_len < newbuf_size) {
1596 newbuf_size = size_roundup_power2(newbuf_size);
1598 OBD_ALLOC(newbuf, newbuf_size);
1602 memcpy(newbuf, req->rq_reqbuf, req->rq_reqbuf_len);
1604 OBD_FREE(req->rq_reqbuf, req->rq_reqbuf_len);
1605 req->rq_reqbuf = newbuf;
1606 req->rq_reqbuf_len = newbuf_size;
1607 req->rq_reqmsg = lustre_msg_buf(req->rq_reqbuf, 1, 0);
1610 /* do enlargement, from wrapper to embedded, from end to begin */
1611 if (svc != SPTLRPC_SVC_NULL)
1612 _sptlrpc_enlarge_msg_inplace(req->rq_reqbuf,
1613 msg_last_segidx(req->rq_reqbuf),
1616 _sptlrpc_enlarge_msg_inplace(req->rq_reqbuf, 1, newmsg_size);
1617 _sptlrpc_enlarge_msg_inplace(req->rq_reqmsg, segment, newsize);
1619 req->rq_reqlen = newmsg_size;
1624 int gss_enlarge_reqbuf_priv(struct ptlrpc_sec *sec,
1625 struct ptlrpc_request *req,
1626 int segment, int newsize)
1628 struct lustre_msg *newclrbuf;
1629 int newmsg_size, newclrbuf_size, newcipbuf_size;
1633 * embedded msg is at seg 0 of clear buffer;
1634 * cipher text is at seg 2 of cipher buffer;
1636 LASSERT(req->rq_pool ||
1637 (req->rq_reqbuf == NULL && req->rq_reqbuf_len == 0));
1638 LASSERT(req->rq_reqbuf == NULL ||
1639 (req->rq_pool && req->rq_reqbuf->lm_bufcount == 3));
1640 LASSERT(req->rq_clrbuf);
1641 LASSERT(req->rq_clrbuf_len > req->rq_reqlen);
1642 LASSERT(lustre_msg_buf(req->rq_clrbuf, 0, 0) == req->rq_reqmsg);
1644 /* compute new embedded msg size */
1645 newmsg_size = get_enlarged_msgsize(req->rq_reqmsg, segment, newsize);
1647 /* compute new clear buffer size */
1648 newclrbuf_size = get_enlarged_msgsize(req->rq_clrbuf, 0, newmsg_size);
1649 newclrbuf_size += GSS_MAX_CIPHER_BLOCK;
1651 /* compute new cipher buffer size */
1652 buflens[0] = PTLRPC_GSS_HEADER_SIZE;
1653 buflens[1] = gss_cli_payload(req->rq_cli_ctx, buflens[0], 0);
1654 buflens[2] = gss_cli_payload(req->rq_cli_ctx, newclrbuf_size, 1);
1655 newcipbuf_size = lustre_msg_size_v2(3, buflens);
1657 /* handle the case that we put both clear buf and cipher buf into
1658 * pre-allocated single buffer. */
1659 if (unlikely(req->rq_pool) &&
1660 req->rq_clrbuf >= req->rq_reqbuf &&
1661 (char *) req->rq_clrbuf <
1662 (char *) req->rq_reqbuf + req->rq_reqbuf_len) {
1663 /* it couldn't be better we still fit into the
1664 * pre-allocated buffer. */
1665 if (newclrbuf_size + newcipbuf_size <= req->rq_reqbuf_len) {
1668 /* move clear text backward. */
1669 src = req->rq_clrbuf;
1670 dst = (char *) req->rq_reqbuf + newcipbuf_size;
1672 memmove(dst, src, req->rq_clrbuf_len);
1674 req->rq_clrbuf = (struct lustre_msg *) dst;
1675 req->rq_clrbuf_len = newclrbuf_size;
1676 req->rq_reqmsg = lustre_msg_buf(req->rq_clrbuf, 0, 0);
1678 /* sadly we have to split out the clear buffer */
1679 LASSERT(req->rq_reqbuf_len >= newcipbuf_size);
1680 LASSERT(req->rq_clrbuf_len < newclrbuf_size);
1684 if (req->rq_clrbuf_len < newclrbuf_size) {
1685 newclrbuf_size = size_roundup_power2(newclrbuf_size);
1687 OBD_ALLOC(newclrbuf, newclrbuf_size);
1688 if (newclrbuf == NULL)
1691 memcpy(newclrbuf, req->rq_clrbuf, req->rq_clrbuf_len);
1693 if (req->rq_reqbuf == NULL ||
1694 req->rq_clrbuf < req->rq_reqbuf ||
1695 (char *) req->rq_clrbuf >=
1696 (char *) req->rq_reqbuf + req->rq_reqbuf_len) {
1697 OBD_FREE(req->rq_clrbuf, req->rq_clrbuf_len);
1700 req->rq_clrbuf = newclrbuf;
1701 req->rq_clrbuf_len = newclrbuf_size;
1702 req->rq_reqmsg = lustre_msg_buf(req->rq_clrbuf, 0, 0);
1705 _sptlrpc_enlarge_msg_inplace(req->rq_clrbuf, 0, newmsg_size);
1706 _sptlrpc_enlarge_msg_inplace(req->rq_reqmsg, segment, newsize);
1707 req->rq_reqlen = newmsg_size;
1712 int gss_enlarge_reqbuf(struct ptlrpc_sec *sec,
1713 struct ptlrpc_request *req,
1714 int segment, int newsize)
1716 int svc = SEC_FLAVOR_SVC(req->rq_sec_flavor);
1718 LASSERT(!req->rq_ctx_init && !req->rq_ctx_fini);
1721 case SPTLRPC_SVC_NULL:
1722 case SPTLRPC_SVC_AUTH:
1723 case SPTLRPC_SVC_INTG:
1724 return gss_enlarge_reqbuf_intg(sec, req, svc, segment, newsize);
1725 case SPTLRPC_SVC_PRIV:
1726 return gss_enlarge_reqbuf_priv(sec, req, segment, newsize);
1728 LASSERTF(0, "bad flavor %x\n", req->rq_sec_flavor);
1733 int gss_sec_install_rctx(struct obd_import *imp,
1734 struct ptlrpc_sec *sec,
1735 struct ptlrpc_cli_ctx *ctx)
1737 struct gss_sec *gsec;
1738 struct gss_cli_ctx *gctx;
1741 gsec = container_of(sec, struct gss_sec, gs_base);
1742 gctx = container_of(ctx, struct gss_cli_ctx, gc_base);
1744 rc = gss_install_rvs_svc_ctx(imp, gsec, gctx);
1748 /********************************************
1750 ********************************************/
1753 int gss_svc_reqctx_is_special(struct gss_svc_reqctx *grctx)
1756 return (grctx->src_init || grctx->src_init_continue ||
1757 grctx->src_err_notify);
1761 void gss_svc_reqctx_free(struct gss_svc_reqctx *grctx)
1764 gss_svc_upcall_put_ctx(grctx->src_ctx);
1766 sptlrpc_policy_put(grctx->src_base.sc_policy);
1767 OBD_FREE_PTR(grctx);
1771 void gss_svc_reqctx_addref(struct gss_svc_reqctx *grctx)
1773 LASSERT(atomic_read(&grctx->src_base.sc_refcount) > 0);
1774 atomic_inc(&grctx->src_base.sc_refcount);
1778 void gss_svc_reqctx_decref(struct gss_svc_reqctx *grctx)
1780 LASSERT(atomic_read(&grctx->src_base.sc_refcount) > 0);
1782 if (atomic_dec_and_test(&grctx->src_base.sc_refcount))
1783 gss_svc_reqctx_free(grctx);
1787 int gss_svc_sign(struct ptlrpc_request *req,
1788 struct ptlrpc_reply_state *rs,
1789 struct gss_svc_reqctx *grctx,
1795 LASSERT(rs->rs_msg == lustre_msg_buf(rs->rs_repbuf, 1, 0));
1797 /* embedded lustre_msg might have been shrinked */
1798 if (req->rq_replen != rs->rs_repbuf->lm_buflens[1])
1799 lustre_shrink_msg(rs->rs_repbuf, 1, req->rq_replen, 1);
1801 rc = gss_sign_msg(rs->rs_repbuf, grctx->src_ctx->gsc_mechctx,
1802 PTLRPC_GSS_PROC_DATA, grctx->src_wirectx.gw_seq,
1807 rs->rs_repdata_len = rc;
1811 int gss_pack_err_notify(struct ptlrpc_request *req, __u32 major, __u32 minor)
1813 struct gss_svc_reqctx *grctx = gss_svc_ctx2reqctx(req->rq_svc_ctx);
1814 struct ptlrpc_reply_state *rs;
1815 struct gss_err_header *ghdr;
1816 int replen = sizeof(struct ptlrpc_body);
1820 //OBD_FAIL_RETURN(OBD_FAIL_SVCGSS_ERR_NOTIFY|OBD_FAIL_ONCE, -EINVAL);
1822 grctx->src_err_notify = 1;
1823 grctx->src_reserve_len = 0;
1825 rc = lustre_pack_reply_v2(req, 1, &replen, NULL);
1827 CERROR("could not pack reply, err %d\n", rc);
1832 rs = req->rq_reply_state;
1833 LASSERT(rs->rs_repbuf->lm_buflens[1] >= sizeof(*ghdr));
1834 ghdr = lustre_msg_buf(rs->rs_repbuf, 0, 0);
1835 ghdr->gh_version = PTLRPC_GSS_VERSION;
1837 ghdr->gh_proc = PTLRPC_GSS_PROC_ERR;
1838 ghdr->gh_major = major;
1839 ghdr->gh_minor = minor;
1840 ghdr->gh_handle.len = 0; /* fake context handle */
1842 rs->rs_repdata_len = lustre_msg_size_v2(rs->rs_repbuf->lm_bufcount,
1843 rs->rs_repbuf->lm_buflens);
1845 CDEBUG(D_SEC, "prepare gss error notify(0x%x/0x%x) to %s\n",
1846 major, minor, libcfs_nid2str(req->rq_peer.nid));
1851 int gss_svc_handle_init(struct ptlrpc_request *req,
1852 struct gss_wire_ctx *gw)
1854 struct gss_svc_reqctx *grctx = gss_svc_ctx2reqctx(req->rq_svc_ctx);
1855 struct lustre_msg *reqbuf = req->rq_reqbuf;
1856 struct obd_uuid *uuid;
1857 struct obd_device *target;
1858 rawobj_t uuid_obj, rvs_hdl, in_token;
1860 __u32 *secdata, seclen;
1864 CDEBUG(D_SEC, "processing gss init(%d) request from %s\n", gw->gw_proc,
1865 libcfs_nid2str(req->rq_peer.nid));
1867 req->rq_ctx_init = 1;
1869 if (gw->gw_proc == PTLRPC_GSS_PROC_INIT && gw->gw_handle.len != 0) {
1870 CERROR("proc %u: invalid handle length %u\n",
1871 gw->gw_proc, gw->gw_handle.len);
1872 RETURN(SECSVC_DROP);
1875 if (reqbuf->lm_bufcount < 3 || reqbuf->lm_bufcount > 4){
1876 CERROR("Invalid bufcount %d\n", reqbuf->lm_bufcount);
1877 RETURN(SECSVC_DROP);
1880 /* ctx initiate payload is in last segment */
1881 secdata = lustre_msg_buf(reqbuf, reqbuf->lm_bufcount - 1, 0);
1882 seclen = reqbuf->lm_buflens[reqbuf->lm_bufcount - 1];
1884 if (seclen < 4 + 4) {
1885 CERROR("sec size %d too small\n", seclen);
1886 RETURN(SECSVC_DROP);
1889 /* lustre svc type */
1890 lustre_svc = le32_to_cpu(*secdata++);
1893 /* extract target uuid, note this code is somewhat fragile
1894 * because touched internal structure of obd_uuid */
1895 if (rawobj_extract(&uuid_obj, &secdata, &seclen)) {
1896 CERROR("failed to extract target uuid\n");
1897 RETURN(SECSVC_DROP);
1899 uuid_obj.data[uuid_obj.len - 1] = '\0';
1901 uuid = (struct obd_uuid *) uuid_obj.data;
1902 target = class_uuid2obd(uuid);
1903 if (!target || target->obd_stopping || !target->obd_set_up) {
1904 CERROR("target '%s' is not available for context init (%s)",
1905 uuid->uuid, target == NULL ? "no target" :
1906 (target->obd_stopping ? "stopping" : "not set up"));
1907 RETURN(SECSVC_DROP);
1910 /* extract reverse handle */
1911 if (rawobj_extract(&rvs_hdl, &secdata, &seclen)) {
1912 CERROR("failed extract reverse handle\n");
1913 RETURN(SECSVC_DROP);
1917 if (rawobj_extract(&in_token, &secdata, &seclen)) {
1918 CERROR("can't extract token\n");
1919 RETURN(SECSVC_DROP);
1922 rc = gss_svc_upcall_handle_init(req, grctx, gw, target, lustre_svc,
1923 &rvs_hdl, &in_token);
1924 if (rc != SECSVC_OK)
1927 if (grctx->src_ctx->gsc_usr_mds || grctx->src_ctx->gsc_usr_root)
1928 CWARN("user from %s authenticated as %s\n",
1929 libcfs_nid2str(req->rq_peer.nid),
1930 grctx->src_ctx->gsc_usr_mds ? "mds" : "root");
1932 CWARN("accept user %u from %s\n", grctx->src_ctx->gsc_uid,
1933 libcfs_nid2str(req->rq_peer.nid));
1935 if (SEC_FLAVOR_HAS_USER(req->rq_sec_flavor)) {
1936 if (reqbuf->lm_bufcount < 4) {
1937 CERROR("missing user descriptor\n");
1938 RETURN(SECSVC_DROP);
1940 if (sptlrpc_unpack_user_desc(reqbuf, 2)) {
1941 CERROR("Mal-formed user descriptor\n");
1942 RETURN(SECSVC_DROP);
1944 req->rq_user_desc = lustre_msg_buf(reqbuf, 2, 0);
1947 req->rq_reqmsg = lustre_msg_buf(reqbuf, 1, 0);
1948 req->rq_reqlen = lustre_msg_buflen(reqbuf, 1);
1954 * last segment must be the gss signature.
1957 int gss_svc_verify_request(struct ptlrpc_request *req,
1958 struct gss_svc_reqctx *grctx,
1959 struct gss_wire_ctx *gw,
1962 struct gss_svc_ctx *gctx = grctx->src_ctx;
1963 struct lustre_msg *msg = req->rq_reqbuf;
1967 *major = GSS_S_COMPLETE;
1969 if (msg->lm_bufcount < 2) {
1970 CERROR("Too few segments (%u) in request\n", msg->lm_bufcount);
1974 if (gw->gw_svc == SPTLRPC_SVC_NULL)
1977 if (gss_check_seq_num(&gctx->gsc_seqdata, gw->gw_seq, 0)) {
1978 CERROR("phase 0: discard replayed req: seq %u\n", gw->gw_seq);
1979 *major = GSS_S_DUPLICATE_TOKEN;
1983 *major = gss_verify_msg(msg, gctx->gsc_mechctx, gw->gw_svc);
1984 if (*major != GSS_S_COMPLETE)
1987 if (gss_check_seq_num(&gctx->gsc_seqdata, gw->gw_seq, 1)) {
1988 CERROR("phase 1+: discard replayed req: seq %u\n", gw->gw_seq);
1989 *major = GSS_S_DUPLICATE_TOKEN;
1994 /* user descriptor */
1995 if (SEC_FLAVOR_HAS_USER(req->rq_sec_flavor)) {
1996 if (msg->lm_bufcount < (offset + 1)) {
1997 CERROR("no user desc included\n");
2001 if (sptlrpc_unpack_user_desc(msg, offset)) {
2002 CERROR("Mal-formed user descriptor\n");
2006 req->rq_user_desc = lustre_msg_buf(msg, offset, 0);
2010 /* check bulk cksum data */
2011 if (SEC_FLAVOR_HAS_BULK(req->rq_sec_flavor)) {
2012 if (msg->lm_bufcount < (offset + 1)) {
2013 CERROR("no bulk checksum included\n");
2017 if (bulk_sec_desc_unpack(msg, offset))
2020 grctx->src_reqbsd = lustre_msg_buf(msg, offset, 0);
2021 grctx->src_reqbsd_size = lustre_msg_buflen(msg, offset);
2024 req->rq_reqmsg = lustre_msg_buf(msg, 1, 0);
2025 req->rq_reqlen = msg->lm_buflens[1];
2030 int gss_svc_unseal_request(struct ptlrpc_request *req,
2031 struct gss_svc_reqctx *grctx,
2032 struct gss_wire_ctx *gw,
2035 struct gss_svc_ctx *gctx = grctx->src_ctx;
2036 struct lustre_msg *msg = req->rq_reqbuf;
2037 int msglen, offset = 1;
2040 if (gss_check_seq_num(&gctx->gsc_seqdata, gw->gw_seq, 0)) {
2041 CERROR("phase 0: discard replayed req: seq %u\n", gw->gw_seq);
2042 *major = GSS_S_DUPLICATE_TOKEN;
2046 *major = gss_unseal_msg(gctx->gsc_mechctx, msg,
2047 &msglen, req->rq_reqdata_len);
2048 if (*major != GSS_S_COMPLETE)
2051 if (gss_check_seq_num(&gctx->gsc_seqdata, gw->gw_seq, 1)) {
2052 CERROR("phase 1+: discard replayed req: seq %u\n", gw->gw_seq);
2053 *major = GSS_S_DUPLICATE_TOKEN;
2057 if (lustre_unpack_msg(msg, msglen)) {
2058 CERROR("Failed to unpack after decryption\n");
2061 req->rq_reqdata_len = msglen;
2063 if (msg->lm_bufcount < 1) {
2064 CERROR("Invalid buffer: is empty\n");
2068 if (SEC_FLAVOR_HAS_USER(req->rq_sec_flavor)) {
2069 if (msg->lm_bufcount < offset + 1) {
2070 CERROR("no user descriptor included\n");
2074 if (sptlrpc_unpack_user_desc(msg, offset)) {
2075 CERROR("Mal-formed user descriptor\n");
2079 req->rq_user_desc = lustre_msg_buf(msg, offset, 0);
2083 if (SEC_FLAVOR_HAS_BULK(req->rq_sec_flavor)) {
2084 if (msg->lm_bufcount < offset + 1) {
2085 CERROR("no bulk checksum included\n");
2089 if (bulk_sec_desc_unpack(msg, offset))
2092 grctx->src_reqbsd = lustre_msg_buf(msg, offset, 0);
2093 grctx->src_reqbsd_size = lustre_msg_buflen(msg, offset);
2096 req->rq_reqmsg = lustre_msg_buf(req->rq_reqbuf, 0, 0);
2097 req->rq_reqlen = req->rq_reqbuf->lm_buflens[0];
2102 int gss_svc_handle_data(struct ptlrpc_request *req,
2103 struct gss_wire_ctx *gw)
2105 struct gss_svc_reqctx *grctx = gss_svc_ctx2reqctx(req->rq_svc_ctx);
2110 grctx->src_ctx = gss_svc_upcall_get_ctx(req, gw);
2111 if (!grctx->src_ctx) {
2112 major = GSS_S_NO_CONTEXT;
2116 switch (gw->gw_svc) {
2117 case SPTLRPC_SVC_NULL:
2118 case SPTLRPC_SVC_AUTH:
2119 case SPTLRPC_SVC_INTG:
2120 rc = gss_svc_verify_request(req, grctx, gw, &major);
2122 case SPTLRPC_SVC_PRIV:
2123 rc = gss_svc_unseal_request(req, grctx, gw, &major);
2126 CERROR("unsupported gss service %d\n", gw->gw_svc);
2133 CERROR("svc %u failed: major 0x%08x: ctx %p(%u->%s)\n",
2134 gw->gw_svc, major, grctx->src_ctx, grctx->src_ctx->gsc_uid,
2135 libcfs_nid2str(req->rq_peer.nid));
2137 /* we only notify client in case of NO_CONTEXT/BAD_SIG, which
2138 * might happen after server reboot, to allow recovery. */
2139 if ((major == GSS_S_NO_CONTEXT || major == GSS_S_BAD_SIG) &&
2140 gss_pack_err_notify(req, major, 0) == 0)
2141 RETURN(SECSVC_COMPLETE);
2143 RETURN(SECSVC_DROP);
2147 int gss_svc_handle_destroy(struct ptlrpc_request *req,
2148 struct gss_wire_ctx *gw)
2150 struct gss_svc_reqctx *grctx = gss_svc_ctx2reqctx(req->rq_svc_ctx);
2154 req->rq_ctx_fini = 1;
2155 req->rq_no_reply = 1;
2157 grctx->src_ctx = gss_svc_upcall_get_ctx(req, gw);
2158 if (!grctx->src_ctx) {
2159 CWARN("invalid gss context handle for destroy.\n");
2160 RETURN(SECSVC_DROP);
2163 if (gw->gw_svc != SPTLRPC_SVC_INTG) {
2164 CERROR("svc %u is not supported in destroy.\n", gw->gw_svc);
2165 RETURN(SECSVC_DROP);
2168 if (gss_svc_verify_request(req, grctx, gw, &major))
2169 RETURN(SECSVC_DROP);
2171 CWARN("destroy svc ctx %p(%u->%s)\n", grctx->src_ctx,
2172 grctx->src_ctx->gsc_uid, libcfs_nid2str(req->rq_peer.nid));
2174 gss_svc_upcall_destroy_ctx(grctx->src_ctx);
2176 if (SEC_FLAVOR_HAS_USER(req->rq_sec_flavor)) {
2177 if (req->rq_reqbuf->lm_bufcount < 4) {
2178 CERROR("missing user descriptor, ignore it\n");
2181 if (sptlrpc_unpack_user_desc(req->rq_reqbuf, 2)) {
2182 CERROR("Mal-formed user descriptor, ignore it\n");
2185 req->rq_user_desc = lustre_msg_buf(req->rq_reqbuf, 2, 0);
2191 int gss_svc_accept(struct ptlrpc_sec_policy *policy, struct ptlrpc_request *req)
2193 struct gss_header *ghdr;
2194 struct gss_svc_reqctx *grctx;
2195 struct gss_wire_ctx *gw;
2199 LASSERT(req->rq_reqbuf);
2200 LASSERT(req->rq_svc_ctx == NULL);
2202 if (req->rq_reqbuf->lm_bufcount < 2) {
2203 CERROR("buf count only %d\n", req->rq_reqbuf->lm_bufcount);
2204 RETURN(SECSVC_DROP);
2207 ghdr = gss_swab_header(req->rq_reqbuf, 0);
2209 CERROR("can't decode gss header\n");
2210 RETURN(SECSVC_DROP);
2214 if (ghdr->gh_version != PTLRPC_GSS_VERSION) {
2215 CERROR("gss version %u, expect %u\n", ghdr->gh_version,
2216 PTLRPC_GSS_VERSION);
2217 RETURN(SECSVC_DROP);
2220 /* alloc grctx data */
2221 OBD_ALLOC_PTR(grctx);
2223 CERROR("fail to alloc svc reqctx\n");
2224 RETURN(SECSVC_DROP);
2226 grctx->src_base.sc_policy = sptlrpc_policy_get(policy);
2227 atomic_set(&grctx->src_base.sc_refcount, 1);
2228 req->rq_svc_ctx = &grctx->src_base;
2229 gw = &grctx->src_wirectx;
2231 /* save wire context */
2232 gw->gw_proc = ghdr->gh_proc;
2233 gw->gw_seq = ghdr->gh_seq;
2234 gw->gw_svc = ghdr->gh_svc;
2235 rawobj_from_netobj(&gw->gw_handle, &ghdr->gh_handle);
2237 /* keep original wire header which subject to checksum verification */
2238 if (lustre_msg_swabbed(req->rq_reqbuf))
2239 gss_header_swabber(ghdr);
2241 switch(ghdr->gh_proc) {
2242 case PTLRPC_GSS_PROC_INIT:
2243 case PTLRPC_GSS_PROC_CONTINUE_INIT:
2244 rc = gss_svc_handle_init(req, gw);
2246 case PTLRPC_GSS_PROC_DATA:
2247 rc = gss_svc_handle_data(req, gw);
2249 case PTLRPC_GSS_PROC_DESTROY:
2250 rc = gss_svc_handle_destroy(req, gw);
2253 CERROR("unknown proc %u\n", gw->gw_proc);
2260 LASSERT (grctx->src_ctx);
2262 req->rq_auth_gss = 1;
2263 req->rq_auth_remote = grctx->src_ctx->gsc_remote;
2264 req->rq_auth_usr_mdt = grctx->src_ctx->gsc_usr_mds;
2265 req->rq_auth_usr_root = grctx->src_ctx->gsc_usr_root;
2266 req->rq_auth_uid = grctx->src_ctx->gsc_uid;
2267 req->rq_auth_mapped_uid = grctx->src_ctx->gsc_mapped_uid;
2269 case SECSVC_COMPLETE:
2272 gss_svc_reqctx_free(grctx);
2273 req->rq_svc_ctx = NULL;
2280 void gss_svc_invalidate_ctx(struct ptlrpc_svc_ctx *svc_ctx)
2282 struct gss_svc_reqctx *grctx;
2285 if (svc_ctx == NULL) {
2290 grctx = gss_svc_ctx2reqctx(svc_ctx);
2292 CWARN("gss svc invalidate ctx %p(%u)\n",
2293 grctx->src_ctx, grctx->src_ctx->gsc_uid);
2294 gss_svc_upcall_destroy_ctx(grctx->src_ctx);
2300 int gss_svc_payload(struct gss_svc_reqctx *grctx, int msgsize, int privacy)
2302 if (gss_svc_reqctx_is_special(grctx))
2303 return grctx->src_reserve_len;
2305 return gss_estimate_payload(NULL, msgsize, privacy);
2308 int gss_svc_alloc_rs(struct ptlrpc_request *req, int msglen)
2310 struct gss_svc_reqctx *grctx;
2311 struct ptlrpc_reply_state *rs;
2312 int privacy, svc, bsd_off = 0;
2313 int ibuflens[2], ibufcnt = 0;
2314 int buflens[4], bufcnt;
2315 int txtsize, wmsg_size, rs_size;
2318 LASSERT(msglen % 8 == 0);
2320 if (SEC_FLAVOR_HAS_BULK(req->rq_sec_flavor) &&
2321 !req->rq_bulk_read && !req->rq_bulk_write) {
2322 CERROR("client request bulk sec on non-bulk rpc\n");
2326 svc = SEC_FLAVOR_SVC(req->rq_sec_flavor);
2328 grctx = gss_svc_ctx2reqctx(req->rq_svc_ctx);
2329 if (gss_svc_reqctx_is_special(grctx))
2332 privacy = (svc == SPTLRPC_SVC_PRIV);
2337 ibuflens[0] = msglen;
2339 if (SEC_FLAVOR_HAS_BULK(req->rq_sec_flavor)) {
2340 LASSERT(grctx->src_reqbsd);
2343 ibuflens[ibufcnt++] = bulk_sec_desc_size(
2344 grctx->src_reqbsd->bsd_csum_alg,
2345 0, req->rq_bulk_read);
2348 txtsize = lustre_msg_size_v2(ibufcnt, ibuflens);
2349 txtsize += GSS_MAX_CIPHER_BLOCK;
2351 /* wrapper buffer */
2353 buflens[0] = PTLRPC_GSS_HEADER_SIZE;
2354 buflens[1] = gss_svc_payload(grctx, buflens[0], 0);
2355 buflens[2] = gss_svc_payload(grctx, txtsize, 1);
2358 buflens[0] = PTLRPC_GSS_HEADER_SIZE;
2359 buflens[1] = msglen;
2361 txtsize = buflens[0];
2362 if (svc == SPTLRPC_SVC_INTG)
2363 txtsize += buflens[1];
2365 if (SEC_FLAVOR_HAS_BULK(req->rq_sec_flavor)) {
2366 LASSERT(grctx->src_reqbsd);
2369 buflens[bufcnt] = bulk_sec_desc_size(
2370 grctx->src_reqbsd->bsd_csum_alg,
2371 0, req->rq_bulk_read);
2372 if (svc == SPTLRPC_SVC_INTG)
2373 txtsize += buflens[bufcnt];
2377 if (gss_svc_reqctx_is_special(grctx) ||
2378 svc != SPTLRPC_SVC_NULL)
2379 buflens[bufcnt++] = gss_svc_payload(grctx, txtsize, 0);
2382 wmsg_size = lustre_msg_size_v2(bufcnt, buflens);
2384 rs_size = sizeof(*rs) + wmsg_size;
2385 rs = req->rq_reply_state;
2389 LASSERT(rs->rs_size >= rs_size);
2391 OBD_ALLOC(rs, rs_size);
2395 rs->rs_size = rs_size;
2398 rs->rs_repbuf = (struct lustre_msg *) (rs + 1);
2399 rs->rs_repbuf_len = wmsg_size;
2401 /* initialize the buffer */
2403 lustre_init_msg_v2(rs->rs_repbuf, ibufcnt, ibuflens, NULL);
2404 rs->rs_msg = lustre_msg_buf(rs->rs_repbuf, 0, msglen);
2406 lustre_init_msg_v2(rs->rs_repbuf, bufcnt, buflens, NULL);
2407 rs->rs_repbuf->lm_secflvr = req->rq_sec_flavor;
2409 rs->rs_msg = lustre_msg_buf(rs->rs_repbuf, 1, 0);
2413 grctx->src_repbsd = lustre_msg_buf(rs->rs_repbuf, bsd_off, 0);
2414 grctx->src_repbsd_size = lustre_msg_buflen(rs->rs_repbuf,
2418 gss_svc_reqctx_addref(grctx);
2419 rs->rs_svc_ctx = req->rq_svc_ctx;
2421 LASSERT(rs->rs_msg);
2422 req->rq_reply_state = rs;
2427 int gss_svc_seal(struct ptlrpc_request *req,
2428 struct ptlrpc_reply_state *rs,
2429 struct gss_svc_reqctx *grctx)
2431 struct gss_svc_ctx *gctx = grctx->src_ctx;
2432 rawobj_t msgobj, cipher_obj, micobj;
2433 struct gss_header *ghdr;
2435 int cipher_buflen, buflens[3];
2440 /* embedded lustre_msg might have been shrinked */
2441 if (req->rq_replen != rs->rs_repbuf->lm_buflens[0])
2442 lustre_shrink_msg(rs->rs_repbuf, 0, req->rq_replen, 1);
2444 /* clear data length */
2445 msglen = lustre_msg_size_v2(rs->rs_repbuf->lm_bufcount,
2446 rs->rs_repbuf->lm_buflens);
2449 msgobj.len = msglen;
2450 msgobj.data = (__u8 *) rs->rs_repbuf;
2452 /* allocate temporary cipher buffer */
2453 cipher_buflen = gss_estimate_payload(gctx->gsc_mechctx, msglen, 1);
2454 OBD_ALLOC(cipher_buf, cipher_buflen);
2458 cipher_obj.len = cipher_buflen;
2459 cipher_obj.data = cipher_buf;
2461 major = lgss_wrap(gctx->gsc_mechctx, &msgobj, rs->rs_repbuf_len,
2463 if (major != GSS_S_COMPLETE) {
2464 CERROR("priv: wrap message error: %08x\n", major);
2465 GOTO(out_free, rc = -EPERM);
2467 LASSERT(cipher_obj.len <= cipher_buflen);
2469 /* we are about to override data at rs->rs_repbuf, nullify pointers
2470 * to which to catch further illegal usage. */
2471 grctx->src_repbsd = NULL;
2472 grctx->src_repbsd_size = 0;
2474 /* now the real wire data */
2475 buflens[0] = PTLRPC_GSS_HEADER_SIZE;
2476 buflens[1] = gss_estimate_payload(gctx->gsc_mechctx, buflens[0], 0);
2477 buflens[2] = cipher_obj.len;
2479 LASSERT(lustre_msg_size_v2(3, buflens) <= rs->rs_repbuf_len);
2480 lustre_init_msg_v2(rs->rs_repbuf, 3, buflens, NULL);
2481 rs->rs_repbuf->lm_secflvr = req->rq_sec_flavor;
2484 ghdr = lustre_msg_buf(rs->rs_repbuf, 0, 0);
2485 ghdr->gh_version = PTLRPC_GSS_VERSION;
2487 ghdr->gh_proc = PTLRPC_GSS_PROC_DATA;
2488 ghdr->gh_seq = grctx->src_wirectx.gw_seq;
2489 ghdr->gh_svc = SPTLRPC_SVC_PRIV;
2490 ghdr->gh_handle.len = 0;
2492 /* header signature */
2493 msgobj.len = rs->rs_repbuf->lm_buflens[0];
2494 msgobj.data = lustre_msg_buf(rs->rs_repbuf, 0, 0);
2495 micobj.len = rs->rs_repbuf->lm_buflens[1];
2496 micobj.data = lustre_msg_buf(rs->rs_repbuf, 1, 0);
2498 major = lgss_get_mic(gctx->gsc_mechctx, 1, &msgobj, &micobj);
2499 if (major != GSS_S_COMPLETE) {
2500 CERROR("priv: sign message error: %08x\n", major);
2501 GOTO(out_free, rc = -EPERM);
2503 lustre_shrink_msg(rs->rs_repbuf, 1, micobj.len, 0);
2506 memcpy(lustre_msg_buf(rs->rs_repbuf, 2, 0),
2507 cipher_obj.data, cipher_obj.len);
2509 rs->rs_repdata_len = lustre_shrink_msg(rs->rs_repbuf, 2,
2512 /* to catch upper layer's further access */
2514 req->rq_repmsg = NULL;
2519 OBD_FREE(cipher_buf, cipher_buflen);
2523 int gss_svc_authorize(struct ptlrpc_request *req)
2525 struct ptlrpc_reply_state *rs = req->rq_reply_state;
2526 struct gss_svc_reqctx *grctx = gss_svc_ctx2reqctx(req->rq_svc_ctx);
2527 struct gss_wire_ctx *gw;
2531 if (gss_svc_reqctx_is_special(grctx))
2534 gw = &grctx->src_wirectx;
2535 if (gw->gw_proc != PTLRPC_GSS_PROC_DATA &&
2536 gw->gw_proc != PTLRPC_GSS_PROC_DESTROY) {
2537 CERROR("proc %d not support\n", gw->gw_proc);
2541 LASSERT(grctx->src_ctx);
2543 switch (gw->gw_svc) {
2544 case SPTLRPC_SVC_NULL:
2545 case SPTLRPC_SVC_AUTH:
2546 case SPTLRPC_SVC_INTG:
2547 rc = gss_svc_sign(req, rs, grctx, gw->gw_svc);
2549 case SPTLRPC_SVC_PRIV:
2550 rc = gss_svc_seal(req, rs, grctx);
2553 CERROR("Unknown service %d\n", gw->gw_svc);
2554 GOTO(out, rc = -EINVAL);
2562 void gss_svc_free_rs(struct ptlrpc_reply_state *rs)
2564 struct gss_svc_reqctx *grctx;
2566 LASSERT(rs->rs_svc_ctx);
2567 grctx = container_of(rs->rs_svc_ctx, struct gss_svc_reqctx, src_base);
2569 /* paranoid, maybe not necessary */
2570 grctx->src_reqbsd = NULL;
2571 grctx->src_repbsd = NULL;
2573 gss_svc_reqctx_decref(grctx);
2574 rs->rs_svc_ctx = NULL;
2576 if (!rs->rs_prealloc)
2577 OBD_FREE(rs, rs->rs_size);
2580 void gss_svc_free_ctx(struct ptlrpc_svc_ctx *ctx)
2582 LASSERT(atomic_read(&ctx->sc_refcount) == 0);
2583 gss_svc_reqctx_free(gss_svc_ctx2reqctx(ctx));
2586 int gss_copy_rvc_cli_ctx(struct ptlrpc_cli_ctx *cli_ctx,
2587 struct ptlrpc_svc_ctx *svc_ctx)
2589 struct gss_cli_ctx *cli_gctx = ctx2gctx(cli_ctx);
2590 struct gss_svc_reqctx *grctx;
2591 struct gss_ctx *mechctx = NULL;
2593 cli_gctx->gc_proc = PTLRPC_GSS_PROC_DATA;
2594 cli_gctx->gc_win = GSS_SEQ_WIN;
2595 atomic_set(&cli_gctx->gc_seq, 0);
2597 grctx = container_of(svc_ctx, struct gss_svc_reqctx, src_base);
2598 LASSERT(grctx->src_ctx);
2599 LASSERT(grctx->src_ctx->gsc_mechctx);
2601 if (lgss_copy_reverse_context(grctx->src_ctx->gsc_mechctx, &mechctx) !=
2603 CERROR("failed to copy mech context\n");
2607 if (rawobj_dup(&cli_gctx->gc_handle, &grctx->src_ctx->gsc_rvs_hdl)) {
2608 CERROR("failed to dup reverse handle\n");
2609 lgss_delete_sec_context(&mechctx);
2613 cli_gctx->gc_mechctx = mechctx;
2614 gss_cli_ctx_uptodate(cli_gctx);
2619 int __init sptlrpc_gss_init(void)
2623 rc = gss_init_lproc();
2627 rc = gss_init_cli_upcall();
2631 rc = gss_init_svc_upcall();
2633 goto out_cli_upcall;
2635 rc = init_kerberos_module();
2637 goto out_svc_upcall;
2639 /* register policy after all other stuff be intialized, because it
2640 * might be in used immediately after the registration. */
2642 rc = gss_init_keyring();
2646 #ifdef HAVE_GSS_PIPEFS
2647 rc = gss_init_pipefs();
2654 #ifdef HAVE_GSS_PIPEFS
2660 cleanup_kerberos_module();
2662 gss_exit_svc_upcall();
2664 gss_exit_cli_upcall();
2670 static void __exit sptlrpc_gss_exit(void)
2673 #ifdef HAVE_GSS_PIPEFS
2676 cleanup_kerberos_module();
2677 gss_exit_svc_upcall();
2678 gss_exit_cli_upcall();
2682 MODULE_AUTHOR("Cluster File Systems, Inc. <info@clusterfs.com>");
2683 MODULE_DESCRIPTION("GSS security policy for Lustre");
2684 MODULE_LICENSE("GPL");
2686 module_init(sptlrpc_gss_init);
2687 module_exit(sptlrpc_gss_exit);