1 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
2 * vim:expandtab:shiftwidth=8:tabstop=8:
4 * Modifications for Lustre
6 * Copyright 2008 Sun Microsystems, Inc. All rights reserved
8 * Author: Eric Mei <ericm@clusterfs.com>
12 * linux/net/sunrpc/auth_gss.c
14 * RPCSEC_GSS client authentication.
16 * Copyright (c) 2000 The Regents of the University of Michigan.
17 * All rights reserved.
19 * Dug Song <dugsong@monkey.org>
20 * Andy Adamson <andros@umich.edu>
22 * Redistribution and use in source and binary forms, with or without
23 * modification, are permitted provided that the following conditions
26 * 1. Redistributions of source code must retain the above copyright
27 * notice, this list of conditions and the following disclaimer.
28 * 2. Redistributions in binary form must reproduce the above copyright
29 * notice, this list of conditions and the following disclaimer in the
30 * documentation and/or other materials provided with the distribution.
31 * 3. Neither the name of the University nor the names of its
32 * contributors may be used to endorse or promote products derived
33 * from this software without specific prior written permission.
35 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED
36 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
37 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
38 * DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
39 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
40 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
41 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
42 * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
43 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
44 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
45 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
50 # define EXPORT_SYMTAB
52 #define DEBUG_SUBSYSTEM S_SEC
54 #include <linux/init.h>
55 #include <linux/module.h>
56 #include <linux/slab.h>
57 #include <linux/dcache.h>
59 #include <linux/random.h>
60 #include <linux/mutex.h>
61 #include <asm/atomic.h>
63 #include <liblustre.h>
67 #include <obd_class.h>
68 #include <obd_support.h>
69 #include <obd_cksum.h>
70 #include <lustre/lustre_idl.h>
71 #include <lustre_net.h>
72 #include <lustre_import.h>
73 #include <lustre_sec.h>
76 #include "gss_internal.h"
79 #include <linux/crypto.h>
82 * early reply have fixed size, respectively in privacy and integrity mode.
83 * so we calculate them only once.
85 static int gss_at_reply_off_integ;
86 static int gss_at_reply_off_priv;
89 static inline int msg_last_segidx(struct lustre_msg *msg)
91 LASSERT(msg->lm_bufcount > 0);
92 return msg->lm_bufcount - 1;
94 static inline int msg_last_seglen(struct lustre_msg *msg)
96 return msg->lm_buflens[msg_last_segidx(msg)];
99 /********************************************
100 * wire data swabber *
101 ********************************************/
104 void gss_header_swabber(struct gss_header *ghdr)
106 __swab32s(&ghdr->gh_flags);
107 __swab32s(&ghdr->gh_proc);
108 __swab32s(&ghdr->gh_seq);
109 __swab32s(&ghdr->gh_svc);
110 __swab32s(&ghdr->gh_pad1);
111 __swab32s(&ghdr->gh_handle.len);
114 struct gss_header *gss_swab_header(struct lustre_msg *msg, int segment)
116 struct gss_header *ghdr;
118 ghdr = lustre_swab_buf(msg, segment, sizeof(*ghdr),
122 sizeof(*ghdr) + ghdr->gh_handle.len > msg->lm_buflens[segment]) {
123 CERROR("gss header require length %u, now %u received\n",
124 (unsigned int) sizeof(*ghdr) + ghdr->gh_handle.len,
125 msg->lm_buflens[segment]);
133 void gss_netobj_swabber(netobj_t *obj)
135 __swab32s(&obj->len);
138 netobj_t *gss_swab_netobj(struct lustre_msg *msg, int segment)
142 obj = lustre_swab_buf(msg, segment, sizeof(*obj), gss_netobj_swabber);
143 if (obj && sizeof(*obj) + obj->len > msg->lm_buflens[segment]) {
144 CERROR("netobj require length %u but only %u received\n",
145 (unsigned int) sizeof(*obj) + obj->len,
146 msg->lm_buflens[segment]);
154 * payload should be obtained from mechanism. but currently since we
155 * only support kerberos, we could simply use fixed value.
158 * - krb5 checksum: 20
160 * for privacy mode, payload also include the cipher text which has the same
161 * size as plain text, plus possible confounder, padding both at maximum cipher
164 #define GSS_KRB5_INTEG_MAX_PAYLOAD (40)
167 int gss_mech_payload(struct gss_ctx *mechctx, int msgsize, int privacy)
170 return GSS_KRB5_INTEG_MAX_PAYLOAD + 16 + 16 + 16 + msgsize;
172 return GSS_KRB5_INTEG_MAX_PAYLOAD;
176 * return signature size, otherwise < 0 to indicate error
178 static int gss_sign_msg(struct lustre_msg *msg,
179 struct gss_ctx *mechctx,
180 enum lustre_sec_part sp,
181 __u32 flags, __u32 proc, __u32 seq, __u32 svc,
184 struct gss_header *ghdr;
185 rawobj_t text[4], mic;
186 int textcnt, max_textcnt, mic_idx;
189 LASSERT(msg->lm_bufcount >= 2);
192 LASSERT(msg->lm_buflens[0] >=
193 sizeof(*ghdr) + (handle ? handle->len : 0));
194 ghdr = lustre_msg_buf(msg, 0, 0);
196 ghdr->gh_version = PTLRPC_GSS_VERSION;
197 ghdr->gh_sp = (__u8) sp;
198 ghdr->gh_flags = flags;
199 ghdr->gh_proc = proc;
203 /* fill in a fake one */
204 ghdr->gh_handle.len = 0;
206 ghdr->gh_handle.len = handle->len;
207 memcpy(ghdr->gh_handle.data, handle->data, handle->len);
210 /* no actual signature for null mode */
211 if (svc == SPTLRPC_SVC_NULL)
212 return lustre_msg_size_v2(msg->lm_bufcount, msg->lm_buflens);
215 mic_idx = msg_last_segidx(msg);
216 max_textcnt = (svc == SPTLRPC_SVC_AUTH) ? 1 : mic_idx;
218 for (textcnt = 0; textcnt < max_textcnt; textcnt++) {
219 text[textcnt].len = msg->lm_buflens[textcnt];
220 text[textcnt].data = lustre_msg_buf(msg, textcnt, 0);
223 mic.len = msg->lm_buflens[mic_idx];
224 mic.data = lustre_msg_buf(msg, mic_idx, 0);
226 major = lgss_get_mic(mechctx, textcnt, text, 0, NULL, &mic);
227 if (major != GSS_S_COMPLETE) {
228 CERROR("fail to generate MIC: %08x\n", major);
231 LASSERT(mic.len <= msg->lm_buflens[mic_idx]);
233 return lustre_shrink_msg(msg, mic_idx, mic.len, 0);
240 __u32 gss_verify_msg(struct lustre_msg *msg,
241 struct gss_ctx *mechctx,
244 rawobj_t text[4], mic;
245 int textcnt, max_textcnt;
249 LASSERT(msg->lm_bufcount >= 2);
251 if (svc == SPTLRPC_SVC_NULL)
252 return GSS_S_COMPLETE;
254 mic_idx = msg_last_segidx(msg);
255 max_textcnt = (svc == SPTLRPC_SVC_AUTH) ? 1 : mic_idx;
257 for (textcnt = 0; textcnt < max_textcnt; textcnt++) {
258 text[textcnt].len = msg->lm_buflens[textcnt];
259 text[textcnt].data = lustre_msg_buf(msg, textcnt, 0);
262 mic.len = msg->lm_buflens[mic_idx];
263 mic.data = lustre_msg_buf(msg, mic_idx, 0);
265 major = lgss_verify_mic(mechctx, textcnt, text, 0, NULL, &mic);
266 if (major != GSS_S_COMPLETE)
267 CERROR("mic verify error: %08x\n", major);
273 * return gss error code
276 __u32 gss_unseal_msg(struct gss_ctx *mechctx,
277 struct lustre_msg *msgbuf,
278 int *msg_len, int msgbuf_len)
280 rawobj_t clear_obj, hdrobj, token;
286 if (msgbuf->lm_bufcount != 2) {
287 CERROR("invalid bufcount %d\n", msgbuf->lm_bufcount);
288 RETURN(GSS_S_FAILURE);
291 /* allocate a temporary clear text buffer, same sized as token,
292 * we assume the final clear text size <= token size */
293 clear_buflen = lustre_msg_buflen(msgbuf, 1);
294 OBD_ALLOC(clear_buf, clear_buflen);
296 RETURN(GSS_S_FAILURE);
299 hdrobj.len = lustre_msg_buflen(msgbuf, 0);
300 hdrobj.data = lustre_msg_buf(msgbuf, 0, 0);
301 token.len = lustre_msg_buflen(msgbuf, 1);
302 token.data = lustre_msg_buf(msgbuf, 1, 0);
303 clear_obj.len = clear_buflen;
304 clear_obj.data = clear_buf;
306 major = lgss_unwrap(mechctx, &hdrobj, &token, &clear_obj);
307 if (major != GSS_S_COMPLETE) {
308 CERROR("unwrap message error: %08x\n", major);
309 GOTO(out_free, major = GSS_S_FAILURE);
311 LASSERT(clear_obj.len <= clear_buflen);
312 LASSERT(clear_obj.len <= msgbuf_len);
314 /* now the decrypted message */
315 memcpy(msgbuf, clear_obj.data, clear_obj.len);
316 *msg_len = clear_obj.len;
318 major = GSS_S_COMPLETE;
320 OBD_FREE(clear_buf, clear_buflen);
324 /********************************************
325 * gss client context manipulation helpers *
326 ********************************************/
328 int cli_ctx_expire(struct ptlrpc_cli_ctx *ctx)
330 LASSERT(atomic_read(&ctx->cc_refcount));
332 if (!test_and_set_bit(PTLRPC_CTX_DEAD_BIT, &ctx->cc_flags)) {
333 if (!ctx->cc_early_expire)
334 clear_bit(PTLRPC_CTX_UPTODATE_BIT, &ctx->cc_flags);
336 CWARN("ctx %p(%u->%s) get expired: %lu(%+lds)\n",
337 ctx, ctx->cc_vcred.vc_uid, sec2target_str(ctx->cc_sec),
339 ctx->cc_expire == 0 ? 0 :
340 cfs_time_sub(ctx->cc_expire, cfs_time_current_sec()));
349 * return 1 if the context is dead.
351 int cli_ctx_check_death(struct ptlrpc_cli_ctx *ctx)
353 if (unlikely(cli_ctx_is_dead(ctx)))
356 /* expire is 0 means never expire. a newly created gss context
357 * which during upcall may has 0 expiration */
358 if (ctx->cc_expire == 0)
361 /* check real expiration */
362 if (cfs_time_after(ctx->cc_expire, cfs_time_current_sec()))
369 void gss_cli_ctx_uptodate(struct gss_cli_ctx *gctx)
371 struct ptlrpc_cli_ctx *ctx = &gctx->gc_base;
372 unsigned long ctx_expiry;
374 if (lgss_inquire_context(gctx->gc_mechctx, &ctx_expiry)) {
375 CERROR("ctx %p(%u): unable to inquire, expire it now\n",
376 gctx, ctx->cc_vcred.vc_uid);
377 ctx_expiry = 1; /* make it expired now */
380 ctx->cc_expire = gss_round_ctx_expiry(ctx_expiry,
381 ctx->cc_sec->ps_flvr.sf_flags);
383 /* At this point this ctx might have been marked as dead by
384 * someone else, in which case nobody will make further use
385 * of it. we don't care, and mark it UPTODATE will help
386 * destroying server side context when it be destroied. */
387 set_bit(PTLRPC_CTX_UPTODATE_BIT, &ctx->cc_flags);
389 if (sec_is_reverse(ctx->cc_sec)) {
390 CWARN("server installed reverse ctx %p idx "LPX64", "
391 "expiry %lu(%+lds)\n", ctx,
392 gss_handle_to_u64(&gctx->gc_handle),
393 ctx->cc_expire, ctx->cc_expire - cfs_time_current_sec());
395 CWARN("client refreshed ctx %p idx "LPX64" (%u->%s), "
396 "expiry %lu(%+lds)\n", ctx,
397 gss_handle_to_u64(&gctx->gc_handle),
398 ctx->cc_vcred.vc_uid, sec2target_str(ctx->cc_sec),
399 ctx->cc_expire, ctx->cc_expire - cfs_time_current_sec());
401 /* install reverse svc ctx for root context */
402 if (ctx->cc_vcred.vc_uid == 0)
403 gss_sec_install_rctx(ctx->cc_sec->ps_import,
408 static void gss_cli_ctx_finalize(struct gss_cli_ctx *gctx)
410 LASSERT(gctx->gc_base.cc_sec);
412 if (gctx->gc_mechctx) {
413 lgss_delete_sec_context(&gctx->gc_mechctx);
414 gctx->gc_mechctx = NULL;
417 if (!rawobj_empty(&gctx->gc_svc_handle)) {
418 /* forward ctx: mark buddy reverse svcctx soon-expire. */
419 if (!sec_is_reverse(gctx->gc_base.cc_sec) &&
420 !rawobj_empty(&gctx->gc_svc_handle))
421 gss_svc_upcall_expire_rvs_ctx(&gctx->gc_svc_handle);
423 rawobj_free(&gctx->gc_svc_handle);
426 rawobj_free(&gctx->gc_handle);
430 * Based on sequence number algorithm as specified in RFC 2203.
432 * modified for our own problem: arriving request has valid sequence number,
433 * but unwrapping request might cost a long time, after that its sequence
434 * are not valid anymore (fall behind the window). It rarely happen, mostly
435 * under extreme load.
437 * note we should not check sequence before verify the integrity of incoming
438 * request, because just one attacking request with high sequence number might
439 * cause all following request be dropped.
441 * so here we use a multi-phase approach: prepare 2 sequence windows,
442 * "main window" for normal sequence and "back window" for fall behind sequence.
443 * and 3-phase checking mechanism:
444 * 0 - before integrity verification, perform a initial sequence checking in
445 * main window, which only try and don't actually set any bits. if the
446 * sequence is high above the window or fit in the window and the bit
447 * is 0, then accept and proceed to integrity verification. otherwise
448 * reject this sequence.
449 * 1 - after integrity verification, check in main window again. if this
450 * sequence is high above the window or fit in the window and the bit
451 * is 0, then set the bit and accept; if it fit in the window but bit
452 * already set, then reject; if it fall behind the window, then proceed
454 * 2 - check in back window. if it is high above the window or fit in the
455 * window and the bit is 0, then set the bit and accept. otherwise reject.
458 * 1: looks like a replay
462 * note phase 0 is necessary, because otherwise replay attacking request of
463 * sequence which between the 2 windows can't be detected.
465 * this mechanism can't totally solve the problem, but could help much less
466 * number of valid requests be dropped.
469 int gss_do_check_seq(unsigned long *window, __u32 win_size, __u32 *max_seq,
470 __u32 seq_num, int phase)
472 LASSERT(phase >= 0 && phase <= 2);
474 if (seq_num > *max_seq) {
476 * 1. high above the window
481 if (seq_num >= *max_seq + win_size) {
482 memset(window, 0, win_size / 8);
485 while(*max_seq < seq_num) {
487 __clear_bit((*max_seq) % win_size, window);
490 __set_bit(seq_num % win_size, window);
491 } else if (seq_num + win_size <= *max_seq) {
493 * 2. low behind the window
495 if (phase == 0 || phase == 2)
498 CWARN("seq %u is %u behind (size %d), check backup window\n",
499 seq_num, *max_seq - win_size - seq_num, win_size);
503 * 3. fit into the window
507 if (test_bit(seq_num % win_size, window))
512 if (__test_and_set_bit(seq_num % win_size, window))
521 CERROR("seq %u (%s %s window) is a replay: max %u, winsize %d\n",
523 seq_num + win_size > *max_seq ? "in" : "behind",
524 phase == 2 ? "backup " : "main",
530 * Based on sequence number algorithm as specified in RFC 2203.
532 * if @set == 0: initial check, don't set any bit in window
533 * if @sec == 1: final check, set bit in window
535 int gss_check_seq_num(struct gss_svc_seq_data *ssd, __u32 seq_num, int set)
539 spin_lock(&ssd->ssd_lock);
545 rc = gss_do_check_seq(ssd->ssd_win_main, GSS_SEQ_WIN_MAIN,
546 &ssd->ssd_max_main, seq_num, 0);
548 gss_stat_oos_record_svc(0, 1);
551 * phase 1 checking main window
553 rc = gss_do_check_seq(ssd->ssd_win_main, GSS_SEQ_WIN_MAIN,
554 &ssd->ssd_max_main, seq_num, 1);
557 gss_stat_oos_record_svc(1, 1);
563 * phase 2 checking back window
565 rc = gss_do_check_seq(ssd->ssd_win_back, GSS_SEQ_WIN_BACK,
566 &ssd->ssd_max_back, seq_num, 2);
568 gss_stat_oos_record_svc(2, 1);
570 gss_stat_oos_record_svc(2, 0);
573 spin_unlock(&ssd->ssd_lock);
577 /***************************************
579 ***************************************/
581 static inline int gss_cli_payload(struct ptlrpc_cli_ctx *ctx,
582 int msgsize, int privacy)
584 return gss_mech_payload(NULL, msgsize, privacy);
587 static int gss_cli_bulk_payload(struct ptlrpc_cli_ctx *ctx,
588 struct sptlrpc_flavor *flvr,
591 int payload = sizeof(struct ptlrpc_bulk_sec_desc);
593 LASSERT(SPTLRPC_FLVR_BULK_TYPE(flvr->sf_rpc) == SPTLRPC_BULK_DEFAULT);
595 if ((!reply && !read) || (reply && read)) {
596 switch (SPTLRPC_FLVR_BULK_SVC(flvr->sf_rpc)) {
597 case SPTLRPC_BULK_SVC_NULL:
599 case SPTLRPC_BULK_SVC_INTG:
600 payload += gss_cli_payload(ctx, 0, 0);
602 case SPTLRPC_BULK_SVC_PRIV:
603 payload += gss_cli_payload(ctx, 0, 1);
605 case SPTLRPC_BULK_SVC_AUTH:
614 int gss_cli_ctx_match(struct ptlrpc_cli_ctx *ctx, struct vfs_cred *vcred)
616 return (ctx->cc_vcred.vc_uid == vcred->vc_uid);
619 void gss_cli_ctx_flags2str(unsigned long flags, char *buf, int bufsize)
623 if (flags & PTLRPC_CTX_NEW)
624 strncat(buf, "new,", bufsize);
625 if (flags & PTLRPC_CTX_UPTODATE)
626 strncat(buf, "uptodate,", bufsize);
627 if (flags & PTLRPC_CTX_DEAD)
628 strncat(buf, "dead,", bufsize);
629 if (flags & PTLRPC_CTX_ERROR)
630 strncat(buf, "error,", bufsize);
631 if (flags & PTLRPC_CTX_CACHED)
632 strncat(buf, "cached,", bufsize);
633 if (flags & PTLRPC_CTX_ETERNAL)
634 strncat(buf, "eternal,", bufsize);
636 strncat(buf, "-,", bufsize);
638 buf[strlen(buf) - 1] = '\0';
641 int gss_cli_ctx_sign(struct ptlrpc_cli_ctx *ctx,
642 struct ptlrpc_request *req)
644 struct gss_cli_ctx *gctx = ctx2gctx(ctx);
645 __u32 flags = 0, seq, svc;
649 LASSERT(req->rq_reqbuf);
650 LASSERT(req->rq_reqbuf->lm_bufcount >= 2);
651 LASSERT(req->rq_cli_ctx == ctx);
653 /* nothing to do for context negotiation RPCs */
654 if (req->rq_ctx_init)
657 svc = SPTLRPC_FLVR_SVC(req->rq_flvr.sf_rpc);
658 if (req->rq_pack_bulk)
659 flags |= LUSTRE_GSS_PACK_BULK;
660 if (req->rq_pack_udesc)
661 flags |= LUSTRE_GSS_PACK_USER;
664 seq = atomic_inc_return(&gctx->gc_seq);
666 rc = gss_sign_msg(req->rq_reqbuf, gctx->gc_mechctx,
667 ctx->cc_sec->ps_part,
668 flags, gctx->gc_proc, seq, svc,
673 /* gss_sign_msg() msg might take long time to finish, in which period
674 * more rpcs could be wrapped up and sent out. if we found too many
675 * of them we should repack this rpc, because sent it too late might
676 * lead to the sequence number fall behind the window on server and
677 * be dropped. also applies to gss_cli_ctx_seal().
679 * Note: null mode dosen't check sequence number. */
680 if (svc != SPTLRPC_SVC_NULL &&
681 atomic_read(&gctx->gc_seq) - seq > GSS_SEQ_REPACK_THRESHOLD) {
682 int behind = atomic_read(&gctx->gc_seq) - seq;
684 gss_stat_oos_record_cli(behind);
685 CWARN("req %p: %u behind, retry signing\n", req, behind);
689 req->rq_reqdata_len = rc;
694 int gss_cli_ctx_handle_err_notify(struct ptlrpc_cli_ctx *ctx,
695 struct ptlrpc_request *req,
696 struct gss_header *ghdr)
698 struct gss_err_header *errhdr;
701 LASSERT(ghdr->gh_proc == PTLRPC_GSS_PROC_ERR);
703 errhdr = (struct gss_err_header *) ghdr;
705 CWARN("req x"LPU64"/t"LPU64", ctx %p idx "LPX64"(%u->%s): "
706 "%sserver respond (%08x/%08x)\n",
707 req->rq_xid, req->rq_transno, ctx,
708 gss_handle_to_u64(&ctx2gctx(ctx)->gc_handle),
709 ctx->cc_vcred.vc_uid, sec2target_str(ctx->cc_sec),
710 sec_is_reverse(ctx->cc_sec) ? "reverse" : "",
711 errhdr->gh_major, errhdr->gh_minor);
713 /* context fini rpc, let it failed */
714 if (req->rq_ctx_fini) {
715 CWARN("context fini rpc failed\n");
719 /* reverse sec, just return error, don't expire this ctx because it's
720 * crucial to callback rpcs. note if the callback rpc failed because
721 * of bit flip during network transfer, the client will be evicted
722 * directly. so more gracefully we probably want let it retry for
723 * number of times. */
724 if (sec_is_reverse(ctx->cc_sec))
727 if (errhdr->gh_major != GSS_S_NO_CONTEXT &&
728 errhdr->gh_major != GSS_S_BAD_SIG)
731 /* server return NO_CONTEXT might be caused by context expire
732 * or server reboot/failover. we try to refresh a new ctx which
733 * be transparent to upper layer.
735 * In some cases, our gss handle is possible to be incidentally
736 * identical to another handle since the handle itself is not
737 * fully random. In krb5 case, the GSS_S_BAD_SIG will be
738 * returned, maybe other gss error for other mechanism.
740 * if we add new mechanism, make sure the correct error are
741 * returned in this case. */
742 CWARN("%s: server might lost the context, retrying\n",
743 errhdr->gh_major == GSS_S_NO_CONTEXT ? "NO_CONTEXT" : "BAD_SIG");
745 sptlrpc_cli_ctx_expire(ctx);
747 /* we need replace the ctx right here, otherwise during
748 * resent we'll hit the logic in sptlrpc_req_refresh_ctx()
749 * which keep the ctx with RESEND flag, thus we'll never
750 * get rid of this ctx. */
751 rc = sptlrpc_req_replace_dead_ctx(req);
758 int gss_cli_ctx_verify(struct ptlrpc_cli_ctx *ctx,
759 struct ptlrpc_request *req)
761 struct gss_cli_ctx *gctx;
762 struct gss_header *ghdr, *reqhdr;
763 struct lustre_msg *msg = req->rq_repdata;
765 int pack_bulk, rc = 0;
768 LASSERT(req->rq_cli_ctx == ctx);
771 gctx = container_of(ctx, struct gss_cli_ctx, gc_base);
773 /* special case for context negotiation, rq_repmsg/rq_replen actually
774 * are not used currently. but early reply always be treated normally */
775 if (req->rq_ctx_init && !req->rq_early) {
776 req->rq_repmsg = lustre_msg_buf(msg, 1, 0);
777 req->rq_replen = msg->lm_buflens[1];
781 if (msg->lm_bufcount < 2 || msg->lm_bufcount > 4) {
782 CERROR("unexpected bufcount %u\n", msg->lm_bufcount);
786 ghdr = gss_swab_header(msg, 0);
788 CERROR("can't decode gss header\n");
793 reqhdr = lustre_msg_buf(msg, 0, sizeof(*reqhdr));
796 if (ghdr->gh_version != reqhdr->gh_version) {
797 CERROR("gss version %u mismatch, expect %u\n",
798 ghdr->gh_version, reqhdr->gh_version);
802 switch (ghdr->gh_proc) {
803 case PTLRPC_GSS_PROC_DATA:
804 pack_bulk = ghdr->gh_flags & LUSTRE_GSS_PACK_BULK;
806 if (!req->rq_early && !equi(req->rq_pack_bulk == 1, pack_bulk)){
807 CERROR("%s bulk flag in reply\n",
808 req->rq_pack_bulk ? "missing" : "unexpected");
812 if (ghdr->gh_seq != reqhdr->gh_seq) {
813 CERROR("seqnum %u mismatch, expect %u\n",
814 ghdr->gh_seq, reqhdr->gh_seq);
818 if (ghdr->gh_svc != reqhdr->gh_svc) {
819 CERROR("svc %u mismatch, expect %u\n",
820 ghdr->gh_svc, reqhdr->gh_svc);
824 if (lustre_msg_swabbed(msg))
825 gss_header_swabber(ghdr);
827 major = gss_verify_msg(msg, gctx->gc_mechctx, reqhdr->gh_svc);
828 if (major != GSS_S_COMPLETE) {
829 CERROR("failed to verify reply: %x\n", major);
833 if (req->rq_early && reqhdr->gh_svc == SPTLRPC_SVC_NULL) {
836 cksum = crc32_le(!(__u32) 0,
837 lustre_msg_buf(msg, 1, 0),
838 lustre_msg_buflen(msg, 1));
839 if (cksum != msg->lm_cksum) {
840 CWARN("early reply checksum mismatch: "
841 "%08x != %08x\n", cksum, msg->lm_cksum);
847 /* bulk checksum is right after the lustre msg */
848 if (msg->lm_bufcount < 3) {
849 CERROR("Invalid reply bufcount %u\n",
854 rc = bulk_sec_desc_unpack(msg, 2);
856 CERROR("unpack bulk desc: %d\n", rc);
861 req->rq_repmsg = lustre_msg_buf(msg, 1, 0);
862 req->rq_replen = msg->lm_buflens[1];
864 case PTLRPC_GSS_PROC_ERR:
866 CERROR("server return error with early reply\n");
869 rc = gss_cli_ctx_handle_err_notify(ctx, req, ghdr);
873 CERROR("unknown gss proc %d\n", ghdr->gh_proc);
880 int gss_cli_ctx_seal(struct ptlrpc_cli_ctx *ctx,
881 struct ptlrpc_request *req)
883 struct gss_cli_ctx *gctx;
884 rawobj_t hdrobj, msgobj, token;
885 struct gss_header *ghdr;
886 __u32 buflens[2], major;
890 LASSERT(req->rq_clrbuf);
891 LASSERT(req->rq_cli_ctx == ctx);
892 LASSERT(req->rq_reqlen);
894 gctx = container_of(ctx, struct gss_cli_ctx, gc_base);
896 /* final clear data length */
897 req->rq_clrdata_len = lustre_msg_size_v2(req->rq_clrbuf->lm_bufcount,
898 req->rq_clrbuf->lm_buflens);
900 /* calculate wire data length */
901 buflens[0] = PTLRPC_GSS_HEADER_SIZE;
902 buflens[1] = gss_cli_payload(&gctx->gc_base, req->rq_clrdata_len, 1);
903 wiresize = lustre_msg_size_v2(2, buflens);
905 /* allocate wire buffer */
908 LASSERT(req->rq_reqbuf);
909 LASSERT(req->rq_reqbuf != req->rq_clrbuf);
910 LASSERT(req->rq_reqbuf_len >= wiresize);
912 OBD_ALLOC(req->rq_reqbuf, wiresize);
915 req->rq_reqbuf_len = wiresize;
918 lustre_init_msg_v2(req->rq_reqbuf, 2, buflens, NULL);
919 req->rq_reqbuf->lm_secflvr = req->rq_flvr.sf_rpc;
922 ghdr = lustre_msg_buf(req->rq_reqbuf, 0, 0);
923 ghdr->gh_version = PTLRPC_GSS_VERSION;
924 ghdr->gh_sp = (__u8) ctx->cc_sec->ps_part;
926 ghdr->gh_proc = gctx->gc_proc;
927 ghdr->gh_svc = SPTLRPC_SVC_PRIV;
928 ghdr->gh_handle.len = gctx->gc_handle.len;
929 memcpy(ghdr->gh_handle.data, gctx->gc_handle.data, gctx->gc_handle.len);
930 if (req->rq_pack_bulk)
931 ghdr->gh_flags |= LUSTRE_GSS_PACK_BULK;
932 if (req->rq_pack_udesc)
933 ghdr->gh_flags |= LUSTRE_GSS_PACK_USER;
936 ghdr->gh_seq = atomic_inc_return(&gctx->gc_seq);
939 hdrobj.len = PTLRPC_GSS_HEADER_SIZE;
940 hdrobj.data = (__u8 *) ghdr;
941 msgobj.len = req->rq_clrdata_len;
942 msgobj.data = (__u8 *) req->rq_clrbuf;
943 token.len = lustre_msg_buflen(req->rq_reqbuf, 1);
944 token.data = lustre_msg_buf(req->rq_reqbuf, 1, 0);
946 major = lgss_wrap(gctx->gc_mechctx, &hdrobj, &msgobj,
947 req->rq_clrbuf_len, &token);
948 if (major != GSS_S_COMPLETE) {
949 CERROR("priv: wrap message error: %08x\n", major);
950 GOTO(err_free, rc = -EPERM);
952 LASSERT(token.len <= buflens[1]);
954 /* see explain in gss_cli_ctx_sign() */
955 if (unlikely(atomic_read(&gctx->gc_seq) - ghdr->gh_seq >
956 GSS_SEQ_REPACK_THRESHOLD)) {
957 int behind = atomic_read(&gctx->gc_seq) - ghdr->gh_seq;
959 gss_stat_oos_record_cli(behind);
960 CWARN("req %p: %u behind, retry sealing\n", req, behind);
962 ghdr->gh_seq = atomic_inc_return(&gctx->gc_seq);
966 /* now set the final wire data length */
967 req->rq_reqdata_len = lustre_shrink_msg(req->rq_reqbuf, 1, token.len,0);
972 OBD_FREE(req->rq_reqbuf, req->rq_reqbuf_len);
973 req->rq_reqbuf = NULL;
974 req->rq_reqbuf_len = 0;
979 int gss_cli_ctx_unseal(struct ptlrpc_cli_ctx *ctx,
980 struct ptlrpc_request *req)
982 struct gss_cli_ctx *gctx;
983 struct gss_header *ghdr;
984 struct lustre_msg *msg = req->rq_repdata;
985 int msglen, pack_bulk, rc;
989 LASSERT(req->rq_cli_ctx == ctx);
990 LASSERT(req->rq_ctx_init == 0);
993 gctx = container_of(ctx, struct gss_cli_ctx, gc_base);
995 ghdr = gss_swab_header(msg, 0);
997 CERROR("can't decode gss header\n");
1002 if (ghdr->gh_version != PTLRPC_GSS_VERSION) {
1003 CERROR("gss version %u mismatch, expect %u\n",
1004 ghdr->gh_version, PTLRPC_GSS_VERSION);
1008 switch (ghdr->gh_proc) {
1009 case PTLRPC_GSS_PROC_DATA:
1010 pack_bulk = ghdr->gh_flags & LUSTRE_GSS_PACK_BULK;
1012 if (!req->rq_early && !equi(req->rq_pack_bulk == 1, pack_bulk)){
1013 CERROR("%s bulk flag in reply\n",
1014 req->rq_pack_bulk ? "missing" : "unexpected");
1018 if (lustre_msg_swabbed(msg))
1019 gss_header_swabber(ghdr);
1021 /* use rq_repdata_len as buffer size, which assume unseal
1022 * doesn't need extra memory space. for precise control, we'd
1023 * better calculate out actual buffer size as
1024 * (repbuf_len - offset - repdata_len) */
1025 major = gss_unseal_msg(gctx->gc_mechctx, msg,
1026 &msglen, req->rq_repdata_len);
1027 if (major != GSS_S_COMPLETE) {
1028 CERROR("failed to unwrap reply: %x\n", major);
1033 if (lustre_unpack_msg(msg, msglen)) {
1034 CERROR("Failed to unpack after decryption\n");
1038 if (msg->lm_bufcount < 1) {
1039 CERROR("Invalid reply buffer: empty\n");
1044 if (msg->lm_bufcount < 2) {
1045 CERROR("bufcount %u: missing bulk sec desc\n",
1050 /* bulk checksum is the last segment */
1051 if (bulk_sec_desc_unpack(msg, msg->lm_bufcount - 1))
1055 req->rq_repmsg = lustre_msg_buf(msg, 0, 0);
1056 req->rq_replen = msg->lm_buflens[0];
1060 case PTLRPC_GSS_PROC_ERR:
1061 if (req->rq_early) {
1062 CERROR("server return error with early reply\n");
1065 rc = gss_cli_ctx_handle_err_notify(ctx, req, ghdr);
1069 CERROR("unexpected proc %d\n", ghdr->gh_proc);
1076 /*********************************************
1077 * reverse context installation *
1078 *********************************************/
1081 int gss_install_rvs_svc_ctx(struct obd_import *imp,
1082 struct gss_sec *gsec,
1083 struct gss_cli_ctx *gctx)
1085 return gss_svc_upcall_install_rvs_ctx(imp, gsec, gctx);
1088 /*********************************************
1089 * GSS security APIs *
1090 *********************************************/
1091 int gss_sec_create_common(struct gss_sec *gsec,
1092 struct ptlrpc_sec_policy *policy,
1093 struct obd_import *imp,
1094 struct ptlrpc_svc_ctx *svcctx,
1095 struct sptlrpc_flavor *sf)
1097 struct ptlrpc_sec *sec;
1100 LASSERT(SPTLRPC_FLVR_POLICY(sf->sf_rpc) == SPTLRPC_POLICY_GSS);
1102 gsec->gs_mech = lgss_subflavor_to_mech(
1103 SPTLRPC_FLVR_BASE_SUB(sf->sf_rpc));
1104 if (!gsec->gs_mech) {
1105 CERROR("gss backend 0x%x not found\n",
1106 SPTLRPC_FLVR_BASE_SUB(sf->sf_rpc));
1110 spin_lock_init(&gsec->gs_lock);
1111 gsec->gs_rvs_hdl = 0ULL;
1113 /* initialize upper ptlrpc_sec */
1114 sec = &gsec->gs_base;
1115 sec->ps_policy = policy;
1116 atomic_set(&sec->ps_refcount, 0);
1117 atomic_set(&sec->ps_nctx, 0);
1118 sec->ps_id = sptlrpc_get_next_secid();
1120 sec->ps_import = class_import_get(imp);
1121 spin_lock_init(&sec->ps_lock);
1122 CFS_INIT_LIST_HEAD(&sec->ps_gc_list);
1125 sec->ps_gc_interval = GSS_GC_INTERVAL;
1127 LASSERT(sec_is_reverse(sec));
1129 /* never do gc on reverse sec */
1130 sec->ps_gc_interval = 0;
1133 if (SPTLRPC_FLVR_BULK_SVC(sec->ps_flvr.sf_rpc) == SPTLRPC_BULK_SVC_PRIV)
1134 sptlrpc_enc_pool_add_user();
1136 CDEBUG(D_SEC, "create %s%s@%p\n", (svcctx ? "reverse " : ""),
1137 policy->sp_name, gsec);
1141 void gss_sec_destroy_common(struct gss_sec *gsec)
1143 struct ptlrpc_sec *sec = &gsec->gs_base;
1146 LASSERT(sec->ps_import);
1147 LASSERT(atomic_read(&sec->ps_refcount) == 0);
1148 LASSERT(atomic_read(&sec->ps_nctx) == 0);
1150 if (gsec->gs_mech) {
1151 lgss_mech_put(gsec->gs_mech);
1152 gsec->gs_mech = NULL;
1155 class_import_put(sec->ps_import);
1157 if (SPTLRPC_FLVR_BULK_SVC(sec->ps_flvr.sf_rpc) == SPTLRPC_BULK_SVC_PRIV)
1158 sptlrpc_enc_pool_del_user();
1163 void gss_sec_kill(struct ptlrpc_sec *sec)
1168 int gss_cli_ctx_init_common(struct ptlrpc_sec *sec,
1169 struct ptlrpc_cli_ctx *ctx,
1170 struct ptlrpc_ctx_ops *ctxops,
1171 struct vfs_cred *vcred)
1173 struct gss_cli_ctx *gctx = ctx2gctx(ctx);
1176 atomic_set(&gctx->gc_seq, 0);
1178 CFS_INIT_HLIST_NODE(&ctx->cc_cache);
1179 atomic_set(&ctx->cc_refcount, 0);
1181 ctx->cc_ops = ctxops;
1183 ctx->cc_flags = PTLRPC_CTX_NEW;
1184 ctx->cc_vcred = *vcred;
1185 spin_lock_init(&ctx->cc_lock);
1186 CFS_INIT_LIST_HEAD(&ctx->cc_req_list);
1187 CFS_INIT_LIST_HEAD(&ctx->cc_gc_chain);
1189 /* take a ref on belonging sec, balanced in ctx destroying */
1190 atomic_inc(&sec->ps_refcount);
1191 /* statistic only */
1192 atomic_inc(&sec->ps_nctx);
1194 CDEBUG(D_SEC, "%s@%p: create ctx %p(%u->%s)\n",
1195 sec->ps_policy->sp_name, ctx->cc_sec,
1196 ctx, ctx->cc_vcred.vc_uid, sec2target_str(ctx->cc_sec));
1202 * 1: the context has been taken care of by someone else
1203 * 0: proceed to really destroy the context locally
1205 int gss_cli_ctx_fini_common(struct ptlrpc_sec *sec,
1206 struct ptlrpc_cli_ctx *ctx)
1208 struct gss_cli_ctx *gctx = ctx2gctx(ctx);
1210 LASSERT(atomic_read(&sec->ps_nctx) > 0);
1211 LASSERT(atomic_read(&ctx->cc_refcount) == 0);
1212 LASSERT(ctx->cc_sec == sec);
1214 if (gctx->gc_mechctx) {
1215 /* the final context fini rpc will use this ctx too, and it's
1216 * asynchronous which finished by request_out_callback(). so
1217 * we add refcount, whoever drop finally drop the refcount to
1218 * 0 should responsible for the rest of destroy. */
1219 atomic_inc(&ctx->cc_refcount);
1221 gss_do_ctx_fini_rpc(gctx);
1222 gss_cli_ctx_finalize(gctx);
1224 if (!atomic_dec_and_test(&ctx->cc_refcount))
1228 if (sec_is_reverse(sec))
1229 CWARN("reverse sec %p: destroy ctx %p\n",
1232 CWARN("%s@%p: destroy ctx %p(%u->%s)\n",
1233 sec->ps_policy->sp_name, ctx->cc_sec,
1234 ctx, ctx->cc_vcred.vc_uid, sec2target_str(ctx->cc_sec));
1240 int gss_alloc_reqbuf_intg(struct ptlrpc_sec *sec,
1241 struct ptlrpc_request *req,
1242 int svc, int msgsize)
1244 int bufsize, txtsize;
1250 * on-wire data layout:
1253 * - user descriptor (optional)
1254 * - bulk sec descriptor (optional)
1255 * - signature (optional)
1256 * - svc == NULL: NULL
1257 * - svc == AUTH: signature of gss header
1258 * - svc == INTG: signature of all above
1260 * if this is context negotiation, reserver fixed space
1261 * at the last (signature) segment regardless of svc mode.
1264 buflens[0] = PTLRPC_GSS_HEADER_SIZE;
1265 txtsize = buflens[0];
1267 buflens[1] = msgsize;
1268 if (svc == SPTLRPC_SVC_INTG)
1269 txtsize += buflens[1];
1271 if (req->rq_pack_udesc) {
1272 buflens[bufcnt] = sptlrpc_current_user_desc_size();
1273 if (svc == SPTLRPC_SVC_INTG)
1274 txtsize += buflens[bufcnt];
1278 if (req->rq_pack_bulk) {
1279 buflens[bufcnt] = gss_cli_bulk_payload(req->rq_cli_ctx,
1281 0, req->rq_bulk_read);
1282 if (svc == SPTLRPC_SVC_INTG)
1283 txtsize += buflens[bufcnt];
1287 if (req->rq_ctx_init)
1288 buflens[bufcnt++] = GSS_CTX_INIT_MAX_LEN;
1289 else if (svc != SPTLRPC_SVC_NULL)
1290 buflens[bufcnt++] = gss_cli_payload(req->rq_cli_ctx, txtsize,0);
1292 bufsize = lustre_msg_size_v2(bufcnt, buflens);
1294 if (!req->rq_reqbuf) {
1295 bufsize = size_roundup_power2(bufsize);
1297 OBD_ALLOC(req->rq_reqbuf, bufsize);
1298 if (!req->rq_reqbuf)
1301 req->rq_reqbuf_len = bufsize;
1303 LASSERT(req->rq_pool);
1304 LASSERT(req->rq_reqbuf_len >= bufsize);
1305 memset(req->rq_reqbuf, 0, bufsize);
1308 lustre_init_msg_v2(req->rq_reqbuf, bufcnt, buflens, NULL);
1309 req->rq_reqbuf->lm_secflvr = req->rq_flvr.sf_rpc;
1311 req->rq_reqmsg = lustre_msg_buf(req->rq_reqbuf, 1, msgsize);
1312 LASSERT(req->rq_reqmsg);
1314 /* pack user desc here, later we might leave current user's process */
1315 if (req->rq_pack_udesc)
1316 sptlrpc_pack_user_desc(req->rq_reqbuf, 2);
1322 int gss_alloc_reqbuf_priv(struct ptlrpc_sec *sec,
1323 struct ptlrpc_request *req,
1326 __u32 ibuflens[3], wbuflens[2];
1328 int clearsize, wiresize;
1331 LASSERT(req->rq_clrbuf == NULL);
1332 LASSERT(req->rq_clrbuf_len == 0);
1334 /* Inner (clear) buffers
1336 * - user descriptor (optional)
1337 * - bulk checksum (optional)
1340 ibuflens[0] = msgsize;
1342 if (req->rq_pack_udesc)
1343 ibuflens[ibufcnt++] = sptlrpc_current_user_desc_size();
1344 if (req->rq_pack_bulk)
1345 ibuflens[ibufcnt++] = gss_cli_bulk_payload(req->rq_cli_ctx,
1349 clearsize = lustre_msg_size_v2(ibufcnt, ibuflens);
1350 /* to allow append padding during encryption */
1351 clearsize += GSS_MAX_CIPHER_BLOCK;
1353 /* Wrapper (wire) buffers
1357 wbuflens[0] = PTLRPC_GSS_HEADER_SIZE;
1358 wbuflens[1] = gss_cli_payload(req->rq_cli_ctx, clearsize, 1);
1359 wiresize = lustre_msg_size_v2(2, wbuflens);
1362 /* rq_reqbuf is preallocated */
1363 LASSERT(req->rq_reqbuf);
1364 LASSERT(req->rq_reqbuf_len >= wiresize);
1366 memset(req->rq_reqbuf, 0, req->rq_reqbuf_len);
1368 /* if the pre-allocated buffer is big enough, we just pack
1369 * both clear buf & request buf in it, to avoid more alloc. */
1370 if (clearsize + wiresize <= req->rq_reqbuf_len) {
1372 (void *) (((char *) req->rq_reqbuf) + wiresize);
1374 CWARN("pre-allocated buf size %d is not enough for "
1375 "both clear (%d) and cipher (%d) text, proceed "
1376 "with extra allocation\n", req->rq_reqbuf_len,
1377 clearsize, wiresize);
1381 if (!req->rq_clrbuf) {
1382 clearsize = size_roundup_power2(clearsize);
1384 OBD_ALLOC(req->rq_clrbuf, clearsize);
1385 if (!req->rq_clrbuf)
1388 req->rq_clrbuf_len = clearsize;
1390 lustre_init_msg_v2(req->rq_clrbuf, ibufcnt, ibuflens, NULL);
1391 req->rq_reqmsg = lustre_msg_buf(req->rq_clrbuf, 0, msgsize);
1393 if (req->rq_pack_udesc)
1394 sptlrpc_pack_user_desc(req->rq_clrbuf, 1);
1400 * NOTE: any change of request buffer allocation should also consider
1401 * changing enlarge_reqbuf() series functions.
1403 int gss_alloc_reqbuf(struct ptlrpc_sec *sec,
1404 struct ptlrpc_request *req,
1407 int svc = SPTLRPC_FLVR_SVC(req->rq_flvr.sf_rpc);
1409 LASSERT(!req->rq_pack_bulk ||
1410 (req->rq_bulk_read || req->rq_bulk_write));
1413 case SPTLRPC_SVC_NULL:
1414 case SPTLRPC_SVC_AUTH:
1415 case SPTLRPC_SVC_INTG:
1416 return gss_alloc_reqbuf_intg(sec, req, svc, msgsize);
1417 case SPTLRPC_SVC_PRIV:
1418 return gss_alloc_reqbuf_priv(sec, req, msgsize);
1420 LASSERTF(0, "bad rpc flavor %x\n", req->rq_flvr.sf_rpc);
1425 void gss_free_reqbuf(struct ptlrpc_sec *sec,
1426 struct ptlrpc_request *req)
1431 LASSERT(!req->rq_pool || req->rq_reqbuf);
1432 privacy = SPTLRPC_FLVR_SVC(req->rq_flvr.sf_rpc) == SPTLRPC_SVC_PRIV;
1434 if (!req->rq_clrbuf)
1435 goto release_reqbuf;
1437 /* release clear buffer */
1439 LASSERT(req->rq_clrbuf_len);
1442 req->rq_clrbuf >= req->rq_reqbuf &&
1443 (char *) req->rq_clrbuf <
1444 (char *) req->rq_reqbuf + req->rq_reqbuf_len)
1445 goto release_reqbuf;
1447 OBD_FREE(req->rq_clrbuf, req->rq_clrbuf_len);
1448 req->rq_clrbuf = NULL;
1449 req->rq_clrbuf_len = 0;
1452 if (!req->rq_pool && req->rq_reqbuf) {
1453 LASSERT(req->rq_reqbuf_len);
1455 OBD_FREE(req->rq_reqbuf, req->rq_reqbuf_len);
1456 req->rq_reqbuf = NULL;
1457 req->rq_reqbuf_len = 0;
1460 req->rq_reqmsg = NULL;
1465 static int do_alloc_repbuf(struct ptlrpc_request *req, int bufsize)
1467 bufsize = size_roundup_power2(bufsize);
1469 OBD_ALLOC(req->rq_repbuf, bufsize);
1470 if (!req->rq_repbuf)
1473 req->rq_repbuf_len = bufsize;
1478 int gss_alloc_repbuf_intg(struct ptlrpc_sec *sec,
1479 struct ptlrpc_request *req,
1480 int svc, int msgsize)
1488 * on-wire data layout:
1491 * - bulk sec descriptor (optional)
1492 * - signature (optional)
1493 * - svc == NULL: NULL
1494 * - svc == AUTH: signature of gss header
1495 * - svc == INTG: signature of all above
1497 * if this is context negotiation, reserver fixed space
1498 * at the last (signature) segment regardless of svc mode.
1501 buflens[0] = PTLRPC_GSS_HEADER_SIZE;
1502 txtsize = buflens[0];
1504 buflens[1] = msgsize;
1505 if (svc == SPTLRPC_SVC_INTG)
1506 txtsize += buflens[1];
1508 if (req->rq_pack_bulk) {
1509 buflens[bufcnt] = gss_cli_bulk_payload(req->rq_cli_ctx,
1511 1, req->rq_bulk_read);
1512 if (svc == SPTLRPC_SVC_INTG)
1513 txtsize += buflens[bufcnt];
1517 if (req->rq_ctx_init)
1518 buflens[bufcnt++] = GSS_CTX_INIT_MAX_LEN;
1519 else if (svc != SPTLRPC_SVC_NULL)
1520 buflens[bufcnt++] = gss_cli_payload(req->rq_cli_ctx, txtsize,0);
1522 alloc_size = lustre_msg_size_v2(bufcnt, buflens);
1524 /* add space for early reply */
1525 alloc_size += gss_at_reply_off_integ;
1527 return do_alloc_repbuf(req, alloc_size);
1531 int gss_alloc_repbuf_priv(struct ptlrpc_sec *sec,
1532 struct ptlrpc_request *req,
1542 buflens[0] = msgsize;
1544 if (req->rq_pack_bulk)
1545 buflens[bufcnt++] = gss_cli_bulk_payload(req->rq_cli_ctx,
1547 1, req->rq_bulk_read);
1548 txtsize = lustre_msg_size_v2(bufcnt, buflens);
1549 txtsize += GSS_MAX_CIPHER_BLOCK;
1551 /* wrapper buffers */
1553 buflens[0] = PTLRPC_GSS_HEADER_SIZE;
1554 buflens[1] = gss_cli_payload(req->rq_cli_ctx, txtsize, 1);
1556 alloc_size = lustre_msg_size_v2(bufcnt, buflens);
1557 /* add space for early reply */
1558 alloc_size += gss_at_reply_off_priv;
1560 return do_alloc_repbuf(req, alloc_size);
1563 int gss_alloc_repbuf(struct ptlrpc_sec *sec,
1564 struct ptlrpc_request *req,
1567 int svc = SPTLRPC_FLVR_SVC(req->rq_flvr.sf_rpc);
1570 LASSERT(!req->rq_pack_bulk ||
1571 (req->rq_bulk_read || req->rq_bulk_write));
1574 case SPTLRPC_SVC_NULL:
1575 case SPTLRPC_SVC_AUTH:
1576 case SPTLRPC_SVC_INTG:
1577 return gss_alloc_repbuf_intg(sec, req, svc, msgsize);
1578 case SPTLRPC_SVC_PRIV:
1579 return gss_alloc_repbuf_priv(sec, req, msgsize);
1581 LASSERTF(0, "bad rpc flavor %x\n", req->rq_flvr.sf_rpc);
1586 void gss_free_repbuf(struct ptlrpc_sec *sec,
1587 struct ptlrpc_request *req)
1589 OBD_FREE(req->rq_repbuf, req->rq_repbuf_len);
1590 req->rq_repbuf = NULL;
1591 req->rq_repbuf_len = 0;
1593 req->rq_repmsg = NULL;
1596 static int get_enlarged_msgsize(struct lustre_msg *msg,
1597 int segment, int newsize)
1599 int save, newmsg_size;
1601 LASSERT(newsize >= msg->lm_buflens[segment]);
1603 save = msg->lm_buflens[segment];
1604 msg->lm_buflens[segment] = newsize;
1605 newmsg_size = lustre_msg_size_v2(msg->lm_bufcount, msg->lm_buflens);
1606 msg->lm_buflens[segment] = save;
1611 static int get_enlarged_msgsize2(struct lustre_msg *msg,
1612 int segment1, int newsize1,
1613 int segment2, int newsize2)
1615 int save1, save2, newmsg_size;
1617 LASSERT(newsize1 >= msg->lm_buflens[segment1]);
1618 LASSERT(newsize2 >= msg->lm_buflens[segment2]);
1620 save1 = msg->lm_buflens[segment1];
1621 save2 = msg->lm_buflens[segment2];
1622 msg->lm_buflens[segment1] = newsize1;
1623 msg->lm_buflens[segment2] = newsize2;
1624 newmsg_size = lustre_msg_size_v2(msg->lm_bufcount, msg->lm_buflens);
1625 msg->lm_buflens[segment1] = save1;
1626 msg->lm_buflens[segment2] = save2;
1632 int gss_enlarge_reqbuf_intg(struct ptlrpc_sec *sec,
1633 struct ptlrpc_request *req,
1635 int segment, int newsize)
1637 struct lustre_msg *newbuf;
1638 int txtsize, sigsize = 0, i;
1639 int newmsg_size, newbuf_size;
1642 * gss header is at seg 0;
1643 * embedded msg is at seg 1;
1644 * signature (if any) is at the last seg
1646 LASSERT(req->rq_reqbuf);
1647 LASSERT(req->rq_reqbuf_len > req->rq_reqlen);
1648 LASSERT(req->rq_reqbuf->lm_bufcount >= 2);
1649 LASSERT(lustre_msg_buf(req->rq_reqbuf, 1, 0) == req->rq_reqmsg);
1651 /* 1. compute new embedded msg size */
1652 newmsg_size = get_enlarged_msgsize(req->rq_reqmsg, segment, newsize);
1653 LASSERT(newmsg_size >= req->rq_reqbuf->lm_buflens[1]);
1655 /* 2. compute new wrapper msg size */
1656 if (svc == SPTLRPC_SVC_NULL) {
1657 /* no signature, get size directly */
1658 newbuf_size = get_enlarged_msgsize(req->rq_reqbuf,
1661 txtsize = req->rq_reqbuf->lm_buflens[0];
1663 if (svc == SPTLRPC_SVC_INTG) {
1664 for (i = 1; i < req->rq_reqbuf->lm_bufcount; i++)
1665 txtsize += req->rq_reqbuf->lm_buflens[i];
1666 txtsize += newmsg_size - req->rq_reqbuf->lm_buflens[1];
1669 sigsize = gss_cli_payload(req->rq_cli_ctx, txtsize, 0);
1670 LASSERT(sigsize >= msg_last_seglen(req->rq_reqbuf));
1672 newbuf_size = get_enlarged_msgsize2(
1675 msg_last_segidx(req->rq_reqbuf),
1679 /* request from pool should always have enough buffer */
1680 LASSERT(!req->rq_pool || req->rq_reqbuf_len >= newbuf_size);
1682 if (req->rq_reqbuf_len < newbuf_size) {
1683 newbuf_size = size_roundup_power2(newbuf_size);
1685 OBD_ALLOC(newbuf, newbuf_size);
1689 memcpy(newbuf, req->rq_reqbuf, req->rq_reqbuf_len);
1691 OBD_FREE(req->rq_reqbuf, req->rq_reqbuf_len);
1692 req->rq_reqbuf = newbuf;
1693 req->rq_reqbuf_len = newbuf_size;
1694 req->rq_reqmsg = lustre_msg_buf(req->rq_reqbuf, 1, 0);
1697 /* do enlargement, from wrapper to embedded, from end to begin */
1698 if (svc != SPTLRPC_SVC_NULL)
1699 _sptlrpc_enlarge_msg_inplace(req->rq_reqbuf,
1700 msg_last_segidx(req->rq_reqbuf),
1703 _sptlrpc_enlarge_msg_inplace(req->rq_reqbuf, 1, newmsg_size);
1704 _sptlrpc_enlarge_msg_inplace(req->rq_reqmsg, segment, newsize);
1706 req->rq_reqlen = newmsg_size;
1711 int gss_enlarge_reqbuf_priv(struct ptlrpc_sec *sec,
1712 struct ptlrpc_request *req,
1713 int segment, int newsize)
1715 struct lustre_msg *newclrbuf;
1716 int newmsg_size, newclrbuf_size, newcipbuf_size;
1720 * embedded msg is at seg 0 of clear buffer;
1721 * cipher text is at seg 2 of cipher buffer;
1723 LASSERT(req->rq_pool ||
1724 (req->rq_reqbuf == NULL && req->rq_reqbuf_len == 0));
1725 LASSERT(req->rq_reqbuf == NULL ||
1726 (req->rq_pool && req->rq_reqbuf->lm_bufcount == 3));
1727 LASSERT(req->rq_clrbuf);
1728 LASSERT(req->rq_clrbuf_len > req->rq_reqlen);
1729 LASSERT(lustre_msg_buf(req->rq_clrbuf, 0, 0) == req->rq_reqmsg);
1731 /* compute new embedded msg size */
1732 newmsg_size = get_enlarged_msgsize(req->rq_reqmsg, segment, newsize);
1734 /* compute new clear buffer size */
1735 newclrbuf_size = get_enlarged_msgsize(req->rq_clrbuf, 0, newmsg_size);
1736 newclrbuf_size += GSS_MAX_CIPHER_BLOCK;
1738 /* compute new cipher buffer size */
1739 buflens[0] = PTLRPC_GSS_HEADER_SIZE;
1740 buflens[1] = gss_cli_payload(req->rq_cli_ctx, buflens[0], 0);
1741 buflens[2] = gss_cli_payload(req->rq_cli_ctx, newclrbuf_size, 1);
1742 newcipbuf_size = lustre_msg_size_v2(3, buflens);
1744 /* handle the case that we put both clear buf and cipher buf into
1745 * pre-allocated single buffer. */
1746 if (unlikely(req->rq_pool) &&
1747 req->rq_clrbuf >= req->rq_reqbuf &&
1748 (char *) req->rq_clrbuf <
1749 (char *) req->rq_reqbuf + req->rq_reqbuf_len) {
1750 /* it couldn't be better we still fit into the
1751 * pre-allocated buffer. */
1752 if (newclrbuf_size + newcipbuf_size <= req->rq_reqbuf_len) {
1755 /* move clear text backward. */
1756 src = req->rq_clrbuf;
1757 dst = (char *) req->rq_reqbuf + newcipbuf_size;
1759 memmove(dst, src, req->rq_clrbuf_len);
1761 req->rq_clrbuf = (struct lustre_msg *) dst;
1762 req->rq_clrbuf_len = newclrbuf_size;
1763 req->rq_reqmsg = lustre_msg_buf(req->rq_clrbuf, 0, 0);
1765 /* sadly we have to split out the clear buffer */
1766 LASSERT(req->rq_reqbuf_len >= newcipbuf_size);
1767 LASSERT(req->rq_clrbuf_len < newclrbuf_size);
1771 if (req->rq_clrbuf_len < newclrbuf_size) {
1772 newclrbuf_size = size_roundup_power2(newclrbuf_size);
1774 OBD_ALLOC(newclrbuf, newclrbuf_size);
1775 if (newclrbuf == NULL)
1778 memcpy(newclrbuf, req->rq_clrbuf, req->rq_clrbuf_len);
1780 if (req->rq_reqbuf == NULL ||
1781 req->rq_clrbuf < req->rq_reqbuf ||
1782 (char *) req->rq_clrbuf >=
1783 (char *) req->rq_reqbuf + req->rq_reqbuf_len) {
1784 OBD_FREE(req->rq_clrbuf, req->rq_clrbuf_len);
1787 req->rq_clrbuf = newclrbuf;
1788 req->rq_clrbuf_len = newclrbuf_size;
1789 req->rq_reqmsg = lustre_msg_buf(req->rq_clrbuf, 0, 0);
1792 _sptlrpc_enlarge_msg_inplace(req->rq_clrbuf, 0, newmsg_size);
1793 _sptlrpc_enlarge_msg_inplace(req->rq_reqmsg, segment, newsize);
1794 req->rq_reqlen = newmsg_size;
1799 int gss_enlarge_reqbuf(struct ptlrpc_sec *sec,
1800 struct ptlrpc_request *req,
1801 int segment, int newsize)
1803 int svc = SPTLRPC_FLVR_SVC(req->rq_flvr.sf_rpc);
1805 LASSERT(!req->rq_ctx_init && !req->rq_ctx_fini);
1808 case SPTLRPC_SVC_NULL:
1809 case SPTLRPC_SVC_AUTH:
1810 case SPTLRPC_SVC_INTG:
1811 return gss_enlarge_reqbuf_intg(sec, req, svc, segment, newsize);
1812 case SPTLRPC_SVC_PRIV:
1813 return gss_enlarge_reqbuf_priv(sec, req, segment, newsize);
1815 LASSERTF(0, "bad rpc flavor %x\n", req->rq_flvr.sf_rpc);
1820 int gss_sec_install_rctx(struct obd_import *imp,
1821 struct ptlrpc_sec *sec,
1822 struct ptlrpc_cli_ctx *ctx)
1824 struct gss_sec *gsec;
1825 struct gss_cli_ctx *gctx;
1828 gsec = container_of(sec, struct gss_sec, gs_base);
1829 gctx = container_of(ctx, struct gss_cli_ctx, gc_base);
1831 rc = gss_install_rvs_svc_ctx(imp, gsec, gctx);
1835 /********************************************
1837 ********************************************/
1840 int gss_svc_reqctx_is_special(struct gss_svc_reqctx *grctx)
1843 return (grctx->src_init || grctx->src_init_continue ||
1844 grctx->src_err_notify);
1848 void gss_svc_reqctx_free(struct gss_svc_reqctx *grctx)
1851 gss_svc_upcall_put_ctx(grctx->src_ctx);
1853 sptlrpc_policy_put(grctx->src_base.sc_policy);
1854 OBD_FREE_PTR(grctx);
1858 void gss_svc_reqctx_addref(struct gss_svc_reqctx *grctx)
1860 LASSERT(atomic_read(&grctx->src_base.sc_refcount) > 0);
1861 atomic_inc(&grctx->src_base.sc_refcount);
1865 void gss_svc_reqctx_decref(struct gss_svc_reqctx *grctx)
1867 LASSERT(atomic_read(&grctx->src_base.sc_refcount) > 0);
1869 if (atomic_dec_and_test(&grctx->src_base.sc_refcount))
1870 gss_svc_reqctx_free(grctx);
1874 int gss_svc_sign(struct ptlrpc_request *req,
1875 struct ptlrpc_reply_state *rs,
1876 struct gss_svc_reqctx *grctx,
1883 LASSERT(rs->rs_msg == lustre_msg_buf(rs->rs_repbuf, 1, 0));
1885 /* embedded lustre_msg might have been shrinked */
1886 if (req->rq_replen != rs->rs_repbuf->lm_buflens[1])
1887 lustre_shrink_msg(rs->rs_repbuf, 1, req->rq_replen, 1);
1889 if (req->rq_pack_bulk)
1890 flags |= LUSTRE_GSS_PACK_BULK;
1892 rc = gss_sign_msg(rs->rs_repbuf, grctx->src_ctx->gsc_mechctx,
1893 LUSTRE_SP_ANY, flags, PTLRPC_GSS_PROC_DATA,
1894 grctx->src_wirectx.gw_seq, svc, NULL);
1898 rs->rs_repdata_len = rc;
1900 if (likely(req->rq_packed_final)) {
1901 if (lustre_msghdr_get_flags(req->rq_reqmsg) & MSGHDR_AT_SUPPORT)
1902 req->rq_reply_off = gss_at_reply_off_integ;
1904 req->rq_reply_off = 0;
1906 if (svc == SPTLRPC_SVC_NULL)
1907 rs->rs_repbuf->lm_cksum = crc32_le(!(__u32) 0,
1908 lustre_msg_buf(rs->rs_repbuf, 1, 0),
1909 lustre_msg_buflen(rs->rs_repbuf, 1));
1910 req->rq_reply_off = 0;
1916 int gss_pack_err_notify(struct ptlrpc_request *req, __u32 major, __u32 minor)
1918 struct gss_svc_reqctx *grctx = gss_svc_ctx2reqctx(req->rq_svc_ctx);
1919 struct ptlrpc_reply_state *rs;
1920 struct gss_err_header *ghdr;
1921 int replen = sizeof(struct ptlrpc_body);
1925 //if (OBD_FAIL_CHECK_ORSET(OBD_FAIL_SVCGSS_ERR_NOTIFY, OBD_FAIL_ONCE))
1928 grctx->src_err_notify = 1;
1929 grctx->src_reserve_len = 0;
1931 rc = lustre_pack_reply_v2(req, 1, &replen, NULL, 0);
1933 CERROR("could not pack reply, err %d\n", rc);
1938 rs = req->rq_reply_state;
1939 LASSERT(rs->rs_repbuf->lm_buflens[1] >= sizeof(*ghdr));
1940 ghdr = lustre_msg_buf(rs->rs_repbuf, 0, 0);
1941 ghdr->gh_version = PTLRPC_GSS_VERSION;
1943 ghdr->gh_proc = PTLRPC_GSS_PROC_ERR;
1944 ghdr->gh_major = major;
1945 ghdr->gh_minor = minor;
1946 ghdr->gh_handle.len = 0; /* fake context handle */
1948 rs->rs_repdata_len = lustre_msg_size_v2(rs->rs_repbuf->lm_bufcount,
1949 rs->rs_repbuf->lm_buflens);
1951 CDEBUG(D_SEC, "prepare gss error notify(0x%x/0x%x) to %s\n",
1952 major, minor, libcfs_nid2str(req->rq_peer.nid));
1957 int gss_svc_handle_init(struct ptlrpc_request *req,
1958 struct gss_wire_ctx *gw)
1960 struct gss_svc_reqctx *grctx = gss_svc_ctx2reqctx(req->rq_svc_ctx);
1961 struct lustre_msg *reqbuf = req->rq_reqbuf;
1962 struct obd_uuid *uuid;
1963 struct obd_device *target;
1964 rawobj_t uuid_obj, rvs_hdl, in_token;
1966 __u32 *secdata, seclen;
1970 CDEBUG(D_SEC, "processing gss init(%d) request from %s\n", gw->gw_proc,
1971 libcfs_nid2str(req->rq_peer.nid));
1973 req->rq_ctx_init = 1;
1975 if (gw->gw_flags & LUSTRE_GSS_PACK_BULK) {
1976 CERROR("unexpected bulk flag\n");
1977 RETURN(SECSVC_DROP);
1980 if (gw->gw_proc == PTLRPC_GSS_PROC_INIT && gw->gw_handle.len != 0) {
1981 CERROR("proc %u: invalid handle length %u\n",
1982 gw->gw_proc, gw->gw_handle.len);
1983 RETURN(SECSVC_DROP);
1986 if (reqbuf->lm_bufcount < 3 || reqbuf->lm_bufcount > 4){
1987 CERROR("Invalid bufcount %d\n", reqbuf->lm_bufcount);
1988 RETURN(SECSVC_DROP);
1991 /* ctx initiate payload is in last segment */
1992 secdata = lustre_msg_buf(reqbuf, reqbuf->lm_bufcount - 1, 0);
1993 seclen = reqbuf->lm_buflens[reqbuf->lm_bufcount - 1];
1995 if (seclen < 4 + 4) {
1996 CERROR("sec size %d too small\n", seclen);
1997 RETURN(SECSVC_DROP);
2000 /* lustre svc type */
2001 lustre_svc = le32_to_cpu(*secdata++);
2004 /* extract target uuid, note this code is somewhat fragile
2005 * because touched internal structure of obd_uuid */
2006 if (rawobj_extract(&uuid_obj, &secdata, &seclen)) {
2007 CERROR("failed to extract target uuid\n");
2008 RETURN(SECSVC_DROP);
2010 uuid_obj.data[uuid_obj.len - 1] = '\0';
2012 uuid = (struct obd_uuid *) uuid_obj.data;
2013 target = class_uuid2obd(uuid);
2014 if (!target || target->obd_stopping || !target->obd_set_up) {
2015 CERROR("target '%s' is not available for context init (%s)\n",
2016 uuid->uuid, target == NULL ? "no target" :
2017 (target->obd_stopping ? "stopping" : "not set up"));
2018 RETURN(SECSVC_DROP);
2021 /* extract reverse handle */
2022 if (rawobj_extract(&rvs_hdl, &secdata, &seclen)) {
2023 CERROR("failed extract reverse handle\n");
2024 RETURN(SECSVC_DROP);
2028 if (rawobj_extract(&in_token, &secdata, &seclen)) {
2029 CERROR("can't extract token\n");
2030 RETURN(SECSVC_DROP);
2033 rc = gss_svc_upcall_handle_init(req, grctx, gw, target, lustre_svc,
2034 &rvs_hdl, &in_token);
2035 if (rc != SECSVC_OK)
2038 if (grctx->src_ctx->gsc_usr_mds || grctx->src_ctx->gsc_usr_root)
2039 CWARN("create svc ctx %p: user from %s authenticated as %s\n",
2040 grctx->src_ctx, libcfs_nid2str(req->rq_peer.nid),
2041 grctx->src_ctx->gsc_usr_mds ? "mds" : "root");
2043 CWARN("create svc ctx %p: accept user %u from %s\n",
2044 grctx->src_ctx, grctx->src_ctx->gsc_uid,
2045 libcfs_nid2str(req->rq_peer.nid));
2047 if (gw->gw_flags & LUSTRE_GSS_PACK_USER) {
2048 if (reqbuf->lm_bufcount < 4) {
2049 CERROR("missing user descriptor\n");
2050 RETURN(SECSVC_DROP);
2052 if (sptlrpc_unpack_user_desc(reqbuf, 2)) {
2053 CERROR("Mal-formed user descriptor\n");
2054 RETURN(SECSVC_DROP);
2057 req->rq_pack_udesc = 1;
2058 req->rq_user_desc = lustre_msg_buf(reqbuf, 2, 0);
2061 req->rq_reqmsg = lustre_msg_buf(reqbuf, 1, 0);
2062 req->rq_reqlen = lustre_msg_buflen(reqbuf, 1);
2068 * last segment must be the gss signature.
2071 int gss_svc_verify_request(struct ptlrpc_request *req,
2072 struct gss_svc_reqctx *grctx,
2073 struct gss_wire_ctx *gw,
2076 struct gss_svc_ctx *gctx = grctx->src_ctx;
2077 struct lustre_msg *msg = req->rq_reqbuf;
2081 *major = GSS_S_COMPLETE;
2083 if (msg->lm_bufcount < 2) {
2084 CERROR("Too few segments (%u) in request\n", msg->lm_bufcount);
2088 if (gw->gw_svc == SPTLRPC_SVC_NULL)
2091 if (gss_check_seq_num(&gctx->gsc_seqdata, gw->gw_seq, 0)) {
2092 CERROR("phase 0: discard replayed req: seq %u\n", gw->gw_seq);
2093 *major = GSS_S_DUPLICATE_TOKEN;
2097 *major = gss_verify_msg(msg, gctx->gsc_mechctx, gw->gw_svc);
2098 if (*major != GSS_S_COMPLETE) {
2099 CERROR("failed to verify request: %x\n", *major);
2103 if (gctx->gsc_reverse == 0 &&
2104 gss_check_seq_num(&gctx->gsc_seqdata, gw->gw_seq, 1)) {
2105 CERROR("phase 1+: discard replayed req: seq %u\n", gw->gw_seq);
2106 *major = GSS_S_DUPLICATE_TOKEN;
2111 /* user descriptor */
2112 if (gw->gw_flags & LUSTRE_GSS_PACK_USER) {
2113 if (msg->lm_bufcount < (offset + 1)) {
2114 CERROR("no user desc included\n");
2118 if (sptlrpc_unpack_user_desc(msg, offset)) {
2119 CERROR("Mal-formed user descriptor\n");
2123 req->rq_pack_udesc = 1;
2124 req->rq_user_desc = lustre_msg_buf(msg, offset, 0);
2128 /* check bulk_sec_desc data */
2129 if (gw->gw_flags & LUSTRE_GSS_PACK_BULK) {
2130 if (msg->lm_bufcount < (offset + 1)) {
2131 CERROR("missing bulk sec descriptor\n");
2135 if (bulk_sec_desc_unpack(msg, offset))
2138 req->rq_pack_bulk = 1;
2139 grctx->src_reqbsd = lustre_msg_buf(msg, offset, 0);
2140 grctx->src_reqbsd_size = lustre_msg_buflen(msg, offset);
2143 req->rq_reqmsg = lustre_msg_buf(msg, 1, 0);
2144 req->rq_reqlen = msg->lm_buflens[1];
2149 int gss_svc_unseal_request(struct ptlrpc_request *req,
2150 struct gss_svc_reqctx *grctx,
2151 struct gss_wire_ctx *gw,
2154 struct gss_svc_ctx *gctx = grctx->src_ctx;
2155 struct lustre_msg *msg = req->rq_reqbuf;
2156 int msglen, offset = 1;
2159 if (gss_check_seq_num(&gctx->gsc_seqdata, gw->gw_seq, 0)) {
2160 CERROR("phase 0: discard replayed req: seq %u\n", gw->gw_seq);
2161 *major = GSS_S_DUPLICATE_TOKEN;
2165 *major = gss_unseal_msg(gctx->gsc_mechctx, msg,
2166 &msglen, req->rq_reqdata_len);
2167 if (*major != GSS_S_COMPLETE) {
2168 CERROR("failed to unwrap request: %x\n", *major);
2172 if (gss_check_seq_num(&gctx->gsc_seqdata, gw->gw_seq, 1)) {
2173 CERROR("phase 1+: discard replayed req: seq %u\n", gw->gw_seq);
2174 *major = GSS_S_DUPLICATE_TOKEN;
2178 if (lustre_unpack_msg(msg, msglen)) {
2179 CERROR("Failed to unpack after decryption\n");
2182 req->rq_reqdata_len = msglen;
2184 if (msg->lm_bufcount < 1) {
2185 CERROR("Invalid buffer: is empty\n");
2189 if (gw->gw_flags & LUSTRE_GSS_PACK_USER) {
2190 if (msg->lm_bufcount < offset + 1) {
2191 CERROR("no user descriptor included\n");
2195 if (sptlrpc_unpack_user_desc(msg, offset)) {
2196 CERROR("Mal-formed user descriptor\n");
2200 req->rq_pack_udesc = 1;
2201 req->rq_user_desc = lustre_msg_buf(msg, offset, 0);
2205 if (gw->gw_flags & LUSTRE_GSS_PACK_BULK) {
2206 if (msg->lm_bufcount < offset + 1) {
2207 CERROR("no bulk checksum included\n");
2211 if (bulk_sec_desc_unpack(msg, offset))
2214 req->rq_pack_bulk = 1;
2215 grctx->src_reqbsd = lustre_msg_buf(msg, offset, 0);
2216 grctx->src_reqbsd_size = lustre_msg_buflen(msg, offset);
2219 req->rq_reqmsg = lustre_msg_buf(req->rq_reqbuf, 0, 0);
2220 req->rq_reqlen = req->rq_reqbuf->lm_buflens[0];
2225 int gss_svc_handle_data(struct ptlrpc_request *req,
2226 struct gss_wire_ctx *gw)
2228 struct gss_svc_reqctx *grctx = gss_svc_ctx2reqctx(req->rq_svc_ctx);
2233 grctx->src_ctx = gss_svc_upcall_get_ctx(req, gw);
2234 if (!grctx->src_ctx) {
2235 major = GSS_S_NO_CONTEXT;
2239 switch (gw->gw_svc) {
2240 case SPTLRPC_SVC_NULL:
2241 case SPTLRPC_SVC_AUTH:
2242 case SPTLRPC_SVC_INTG:
2243 rc = gss_svc_verify_request(req, grctx, gw, &major);
2245 case SPTLRPC_SVC_PRIV:
2246 rc = gss_svc_unseal_request(req, grctx, gw, &major);
2249 CERROR("unsupported gss service %d\n", gw->gw_svc);
2256 CERROR("svc %u failed: major 0x%08x: req xid "LPU64" ctx %p idx "
2257 LPX64"(%u->%s)\n", gw->gw_svc, major, req->rq_xid,
2258 grctx->src_ctx, gss_handle_to_u64(&gw->gw_handle),
2259 grctx->src_ctx->gsc_uid, libcfs_nid2str(req->rq_peer.nid));
2261 /* we only notify client in case of NO_CONTEXT/BAD_SIG, which
2262 * might happen after server reboot, to allow recovery. */
2263 if ((major == GSS_S_NO_CONTEXT || major == GSS_S_BAD_SIG) &&
2264 gss_pack_err_notify(req, major, 0) == 0)
2265 RETURN(SECSVC_COMPLETE);
2267 RETURN(SECSVC_DROP);
2271 int gss_svc_handle_destroy(struct ptlrpc_request *req,
2272 struct gss_wire_ctx *gw)
2274 struct gss_svc_reqctx *grctx = gss_svc_ctx2reqctx(req->rq_svc_ctx);
2278 req->rq_ctx_fini = 1;
2279 req->rq_no_reply = 1;
2281 grctx->src_ctx = gss_svc_upcall_get_ctx(req, gw);
2282 if (!grctx->src_ctx) {
2283 CDEBUG(D_SEC, "invalid gss context handle for destroy.\n");
2284 RETURN(SECSVC_DROP);
2287 if (gw->gw_svc != SPTLRPC_SVC_INTG) {
2288 CERROR("svc %u is not supported in destroy.\n", gw->gw_svc);
2289 RETURN(SECSVC_DROP);
2292 if (gss_svc_verify_request(req, grctx, gw, &major))
2293 RETURN(SECSVC_DROP);
2295 CWARN("destroy svc ctx %p idx "LPX64" (%u->%s)\n",
2296 grctx->src_ctx, gss_handle_to_u64(&gw->gw_handle),
2297 grctx->src_ctx->gsc_uid, libcfs_nid2str(req->rq_peer.nid));
2299 gss_svc_upcall_destroy_ctx(grctx->src_ctx);
2301 if (gw->gw_flags & LUSTRE_GSS_PACK_USER) {
2302 if (req->rq_reqbuf->lm_bufcount < 4) {
2303 CERROR("missing user descriptor, ignore it\n");
2306 if (sptlrpc_unpack_user_desc(req->rq_reqbuf, 2)) {
2307 CERROR("Mal-formed user descriptor, ignore it\n");
2311 req->rq_pack_udesc = 1;
2312 req->rq_user_desc = lustre_msg_buf(req->rq_reqbuf, 2, 0);
2318 int gss_svc_accept(struct ptlrpc_sec_policy *policy, struct ptlrpc_request *req)
2320 struct gss_header *ghdr;
2321 struct gss_svc_reqctx *grctx;
2322 struct gss_wire_ctx *gw;
2326 LASSERT(req->rq_reqbuf);
2327 LASSERT(req->rq_svc_ctx == NULL);
2329 if (req->rq_reqbuf->lm_bufcount < 2) {
2330 CERROR("buf count only %d\n", req->rq_reqbuf->lm_bufcount);
2331 RETURN(SECSVC_DROP);
2334 ghdr = gss_swab_header(req->rq_reqbuf, 0);
2336 CERROR("can't decode gss header\n");
2337 RETURN(SECSVC_DROP);
2341 if (ghdr->gh_version != PTLRPC_GSS_VERSION) {
2342 CERROR("gss version %u, expect %u\n", ghdr->gh_version,
2343 PTLRPC_GSS_VERSION);
2344 RETURN(SECSVC_DROP);
2347 req->rq_sp_from = ghdr->gh_sp;
2349 /* alloc grctx data */
2350 OBD_ALLOC_PTR(grctx);
2352 RETURN(SECSVC_DROP);
2354 grctx->src_base.sc_policy = sptlrpc_policy_get(policy);
2355 atomic_set(&grctx->src_base.sc_refcount, 1);
2356 req->rq_svc_ctx = &grctx->src_base;
2357 gw = &grctx->src_wirectx;
2359 /* save wire context */
2360 gw->gw_flags = ghdr->gh_flags;
2361 gw->gw_proc = ghdr->gh_proc;
2362 gw->gw_seq = ghdr->gh_seq;
2363 gw->gw_svc = ghdr->gh_svc;
2364 rawobj_from_netobj(&gw->gw_handle, &ghdr->gh_handle);
2366 /* keep original wire header which subject to checksum verification */
2367 if (lustre_msg_swabbed(req->rq_reqbuf))
2368 gss_header_swabber(ghdr);
2370 switch(ghdr->gh_proc) {
2371 case PTLRPC_GSS_PROC_INIT:
2372 case PTLRPC_GSS_PROC_CONTINUE_INIT:
2373 rc = gss_svc_handle_init(req, gw);
2375 case PTLRPC_GSS_PROC_DATA:
2376 rc = gss_svc_handle_data(req, gw);
2378 case PTLRPC_GSS_PROC_DESTROY:
2379 rc = gss_svc_handle_destroy(req, gw);
2382 CERROR("unknown proc %u\n", gw->gw_proc);
2389 LASSERT (grctx->src_ctx);
2391 req->rq_auth_gss = 1;
2392 req->rq_auth_remote = grctx->src_ctx->gsc_remote;
2393 req->rq_auth_usr_mdt = grctx->src_ctx->gsc_usr_mds;
2394 req->rq_auth_usr_root = grctx->src_ctx->gsc_usr_root;
2395 req->rq_auth_uid = grctx->src_ctx->gsc_uid;
2396 req->rq_auth_mapped_uid = grctx->src_ctx->gsc_mapped_uid;
2398 case SECSVC_COMPLETE:
2401 gss_svc_reqctx_free(grctx);
2402 req->rq_svc_ctx = NULL;
2409 void gss_svc_invalidate_ctx(struct ptlrpc_svc_ctx *svc_ctx)
2411 struct gss_svc_reqctx *grctx;
2414 if (svc_ctx == NULL) {
2419 grctx = gss_svc_ctx2reqctx(svc_ctx);
2421 CWARN("gss svc invalidate ctx %p(%u)\n",
2422 grctx->src_ctx, grctx->src_ctx->gsc_uid);
2423 gss_svc_upcall_destroy_ctx(grctx->src_ctx);
2429 int gss_svc_payload(struct gss_svc_reqctx *grctx, int early,
2430 int msgsize, int privacy)
2432 /* we should treat early reply normally, but which is actually sharing
2433 * the same ctx with original request, so in this case we should
2434 * ignore the special ctx's special flags */
2435 if (early == 0 && gss_svc_reqctx_is_special(grctx))
2436 return grctx->src_reserve_len;
2438 return gss_mech_payload(NULL, msgsize, privacy);
2441 static int gss_svc_bulk_payload(struct gss_svc_ctx *gctx,
2442 struct sptlrpc_flavor *flvr,
2445 int payload = sizeof(struct ptlrpc_bulk_sec_desc);
2448 switch (SPTLRPC_FLVR_BULK_SVC(flvr->sf_rpc)) {
2449 case SPTLRPC_BULK_SVC_NULL:
2451 case SPTLRPC_BULK_SVC_INTG:
2452 payload += gss_mech_payload(NULL, 0, 0);
2454 case SPTLRPC_BULK_SVC_PRIV:
2455 payload += gss_mech_payload(NULL, 0, 1);
2457 case SPTLRPC_BULK_SVC_AUTH:
2466 int gss_svc_alloc_rs(struct ptlrpc_request *req, int msglen)
2468 struct gss_svc_reqctx *grctx;
2469 struct ptlrpc_reply_state *rs;
2470 int early, privacy, svc, bsd_off = 0;
2471 __u32 ibuflens[2], buflens[4];
2472 int ibufcnt = 0, bufcnt;
2473 int txtsize, wmsg_size, rs_size;
2476 LASSERT(msglen % 8 == 0);
2478 if (req->rq_pack_bulk && !req->rq_bulk_read && !req->rq_bulk_write) {
2479 CERROR("client request bulk sec on non-bulk rpc\n");
2483 svc = SPTLRPC_FLVR_SVC(req->rq_flvr.sf_rpc);
2484 early = (req->rq_packed_final == 0);
2486 grctx = gss_svc_ctx2reqctx(req->rq_svc_ctx);
2487 if (!early && gss_svc_reqctx_is_special(grctx))
2490 privacy = (svc == SPTLRPC_SVC_PRIV);
2493 /* inner clear buffers */
2495 ibuflens[0] = msglen;
2497 if (req->rq_pack_bulk) {
2498 LASSERT(grctx->src_reqbsd);
2501 ibuflens[ibufcnt++] = gss_svc_bulk_payload(
2507 txtsize = lustre_msg_size_v2(ibufcnt, ibuflens);
2508 txtsize += GSS_MAX_CIPHER_BLOCK;
2510 /* wrapper buffer */
2512 buflens[0] = PTLRPC_GSS_HEADER_SIZE;
2513 buflens[1] = gss_svc_payload(grctx, early, txtsize, 1);
2516 buflens[0] = PTLRPC_GSS_HEADER_SIZE;
2517 buflens[1] = msglen;
2519 txtsize = buflens[0];
2520 if (svc == SPTLRPC_SVC_INTG)
2521 txtsize += buflens[1];
2523 if (req->rq_pack_bulk) {
2524 LASSERT(grctx->src_reqbsd);
2527 buflens[bufcnt] = gss_svc_bulk_payload(
2531 if (svc == SPTLRPC_SVC_INTG)
2532 txtsize += buflens[bufcnt];
2536 if ((!early && gss_svc_reqctx_is_special(grctx)) ||
2537 svc != SPTLRPC_SVC_NULL)
2538 buflens[bufcnt++] = gss_svc_payload(grctx, early,
2542 wmsg_size = lustre_msg_size_v2(bufcnt, buflens);
2544 rs_size = sizeof(*rs) + wmsg_size;
2545 rs = req->rq_reply_state;
2549 LASSERT(rs->rs_size >= rs_size);
2551 OBD_ALLOC(rs, rs_size);
2555 rs->rs_size = rs_size;
2558 rs->rs_repbuf = (struct lustre_msg *) (rs + 1);
2559 rs->rs_repbuf_len = wmsg_size;
2561 /* initialize the buffer */
2563 lustre_init_msg_v2(rs->rs_repbuf, ibufcnt, ibuflens, NULL);
2564 rs->rs_msg = lustre_msg_buf(rs->rs_repbuf, 0, msglen);
2566 lustre_init_msg_v2(rs->rs_repbuf, bufcnt, buflens, NULL);
2567 rs->rs_repbuf->lm_secflvr = req->rq_flvr.sf_rpc;
2569 rs->rs_msg = lustre_msg_buf(rs->rs_repbuf, 1, 0);
2573 grctx->src_repbsd = lustre_msg_buf(rs->rs_repbuf, bsd_off, 0);
2574 grctx->src_repbsd_size = lustre_msg_buflen(rs->rs_repbuf,
2578 gss_svc_reqctx_addref(grctx);
2579 rs->rs_svc_ctx = req->rq_svc_ctx;
2581 LASSERT(rs->rs_msg);
2582 req->rq_reply_state = rs;
2586 static int gss_svc_seal(struct ptlrpc_request *req,
2587 struct ptlrpc_reply_state *rs,
2588 struct gss_svc_reqctx *grctx)
2590 struct gss_svc_ctx *gctx = grctx->src_ctx;
2591 rawobj_t hdrobj, msgobj, token;
2592 struct gss_header *ghdr;
2595 __u32 buflens[2], major;
2599 /* get clear data length. note embedded lustre_msg might
2600 * have been shrinked */
2601 if (req->rq_replen != lustre_msg_buflen(rs->rs_repbuf, 0))
2602 msglen = lustre_shrink_msg(rs->rs_repbuf, 0, req->rq_replen, 1);
2604 msglen = lustre_msg_size_v2(rs->rs_repbuf->lm_bufcount,
2605 rs->rs_repbuf->lm_buflens);
2607 /* temporarily use tail of buffer to hold gss header data */
2608 LASSERT(msglen + PTLRPC_GSS_HEADER_SIZE <= rs->rs_repbuf_len);
2609 ghdr = (struct gss_header *) ((char *) rs->rs_repbuf +
2610 rs->rs_repbuf_len - PTLRPC_GSS_HEADER_SIZE);
2611 ghdr->gh_version = PTLRPC_GSS_VERSION;
2612 ghdr->gh_sp = LUSTRE_SP_ANY;
2614 ghdr->gh_proc = PTLRPC_GSS_PROC_DATA;
2615 ghdr->gh_seq = grctx->src_wirectx.gw_seq;
2616 ghdr->gh_svc = SPTLRPC_SVC_PRIV;
2617 ghdr->gh_handle.len = 0;
2618 if (req->rq_pack_bulk)
2619 ghdr->gh_flags |= LUSTRE_GSS_PACK_BULK;
2621 /* allocate temporary cipher buffer */
2622 token_buflen = gss_mech_payload(gctx->gsc_mechctx, msglen, 1);
2623 OBD_ALLOC(token_buf, token_buflen);
2624 if (token_buf == NULL)
2627 hdrobj.len = PTLRPC_GSS_HEADER_SIZE;
2628 hdrobj.data = (__u8 *) ghdr;
2629 msgobj.len = msglen;
2630 msgobj.data = (__u8 *) rs->rs_repbuf;
2631 token.len = token_buflen;
2632 token.data = token_buf;
2634 major = lgss_wrap(gctx->gsc_mechctx, &hdrobj, &msgobj,
2635 rs->rs_repbuf_len - PTLRPC_GSS_HEADER_SIZE, &token);
2636 if (major != GSS_S_COMPLETE) {
2637 CERROR("wrap message error: %08x\n", major);
2638 GOTO(out_free, rc = -EPERM);
2640 LASSERT(token.len <= token_buflen);
2642 /* we are about to override data at rs->rs_repbuf, nullify pointers
2643 * to which to catch further illegal usage. */
2644 if (req->rq_pack_bulk) {
2645 grctx->src_repbsd = NULL;
2646 grctx->src_repbsd_size = 0;
2649 /* now fill the actual wire data
2653 buflens[0] = PTLRPC_GSS_HEADER_SIZE;
2654 buflens[1] = token.len;
2656 rs->rs_repdata_len = lustre_msg_size_v2(2, buflens);
2657 LASSERT(rs->rs_repdata_len <= rs->rs_repbuf_len);
2659 lustre_init_msg_v2(rs->rs_repbuf, 2, buflens, NULL);
2660 rs->rs_repbuf->lm_secflvr = req->rq_flvr.sf_rpc;
2662 memcpy(lustre_msg_buf(rs->rs_repbuf, 0, 0), ghdr,
2663 PTLRPC_GSS_HEADER_SIZE);
2664 memcpy(lustre_msg_buf(rs->rs_repbuf, 1, 0), token.data, token.len);
2667 if (req->rq_packed_final &&
2668 (lustre_msghdr_get_flags(req->rq_reqmsg) & MSGHDR_AT_SUPPORT))
2669 req->rq_reply_off = gss_at_reply_off_priv;
2671 req->rq_reply_off = 0;
2673 /* to catch upper layer's further access */
2675 req->rq_repmsg = NULL;
2680 OBD_FREE(token_buf, token_buflen);
2684 int gss_svc_authorize(struct ptlrpc_request *req)
2686 struct ptlrpc_reply_state *rs = req->rq_reply_state;
2687 struct gss_svc_reqctx *grctx = gss_svc_ctx2reqctx(req->rq_svc_ctx);
2688 struct gss_wire_ctx *gw = &grctx->src_wirectx;
2692 early = (req->rq_packed_final == 0);
2694 if (!early && gss_svc_reqctx_is_special(grctx)) {
2695 LASSERT(rs->rs_repdata_len != 0);
2697 req->rq_reply_off = gss_at_reply_off_integ;
2701 /* early reply could happen in many cases */
2703 gw->gw_proc != PTLRPC_GSS_PROC_DATA &&
2704 gw->gw_proc != PTLRPC_GSS_PROC_DESTROY) {
2705 CERROR("proc %d not support\n", gw->gw_proc);
2709 LASSERT(grctx->src_ctx);
2711 switch (gw->gw_svc) {
2712 case SPTLRPC_SVC_NULL:
2713 case SPTLRPC_SVC_AUTH:
2714 case SPTLRPC_SVC_INTG:
2715 rc = gss_svc_sign(req, rs, grctx, gw->gw_svc);
2717 case SPTLRPC_SVC_PRIV:
2718 rc = gss_svc_seal(req, rs, grctx);
2721 CERROR("Unknown service %d\n", gw->gw_svc);
2722 GOTO(out, rc = -EINVAL);
2730 void gss_svc_free_rs(struct ptlrpc_reply_state *rs)
2732 struct gss_svc_reqctx *grctx;
2734 LASSERT(rs->rs_svc_ctx);
2735 grctx = container_of(rs->rs_svc_ctx, struct gss_svc_reqctx, src_base);
2737 gss_svc_reqctx_decref(grctx);
2738 rs->rs_svc_ctx = NULL;
2740 if (!rs->rs_prealloc)
2741 OBD_FREE(rs, rs->rs_size);
2744 void gss_svc_free_ctx(struct ptlrpc_svc_ctx *ctx)
2746 LASSERT(atomic_read(&ctx->sc_refcount) == 0);
2747 gss_svc_reqctx_free(gss_svc_ctx2reqctx(ctx));
2750 int gss_copy_rvc_cli_ctx(struct ptlrpc_cli_ctx *cli_ctx,
2751 struct ptlrpc_svc_ctx *svc_ctx)
2753 struct gss_cli_ctx *cli_gctx = ctx2gctx(cli_ctx);
2754 struct gss_svc_ctx *svc_gctx = gss_svc_ctx2gssctx(svc_ctx);
2755 struct gss_ctx *mechctx = NULL;
2758 LASSERT(svc_gctx && svc_gctx->gsc_mechctx);
2760 cli_gctx->gc_proc = PTLRPC_GSS_PROC_DATA;
2761 cli_gctx->gc_win = GSS_SEQ_WIN;
2763 /* The problem is the reverse ctx might get lost in some recovery
2764 * situations, and the same svc_ctx will be used to re-create it.
2765 * if there's callback be sentout before that, new reverse ctx start
2766 * with sequence 0 will lead to future callback rpc be treated as
2769 * each reverse root ctx will record its latest sequence number on its
2770 * buddy svcctx before be destroied, so here we continue use it.
2772 atomic_set(&cli_gctx->gc_seq, svc_gctx->gsc_rvs_seq);
2774 if (gss_svc_upcall_dup_handle(&cli_gctx->gc_svc_handle, svc_gctx)) {
2775 CERROR("failed to dup svc handle\n");
2779 if (lgss_copy_reverse_context(svc_gctx->gsc_mechctx, &mechctx) !=
2781 CERROR("failed to copy mech context\n");
2782 goto err_svc_handle;
2785 if (rawobj_dup(&cli_gctx->gc_handle, &svc_gctx->gsc_rvs_hdl)) {
2786 CERROR("failed to dup reverse handle\n");
2790 cli_gctx->gc_mechctx = mechctx;
2791 gss_cli_ctx_uptodate(cli_gctx);
2796 lgss_delete_sec_context(&mechctx);
2798 rawobj_free(&cli_gctx->gc_svc_handle);
2803 static void gss_init_at_reply_offset(void)
2808 buflens[0] = PTLRPC_GSS_HEADER_SIZE;
2809 buflens[1] = lustre_msg_early_size();
2810 buflens[2] = gss_cli_payload(NULL, buflens[1], 0);
2811 gss_at_reply_off_integ = lustre_msg_size_v2(3, buflens);
2813 buflens[0] = lustre_msg_early_size();
2814 clearsize = lustre_msg_size_v2(1, buflens);
2815 buflens[0] = PTLRPC_GSS_HEADER_SIZE;
2816 buflens[1] = gss_cli_payload(NULL, clearsize, 0);
2817 buflens[2] = gss_cli_payload(NULL, clearsize, 1);
2818 gss_at_reply_off_priv = lustre_msg_size_v2(3, buflens);
2821 int __init sptlrpc_gss_init(void)
2825 rc = gss_init_lproc();
2829 rc = gss_init_cli_upcall();
2833 rc = gss_init_svc_upcall();
2835 goto out_cli_upcall;
2837 rc = init_kerberos_module();
2839 goto out_svc_upcall;
2841 /* register policy after all other stuff be intialized, because it
2842 * might be in used immediately after the registration. */
2844 rc = gss_init_keyring();
2848 #ifdef HAVE_GSS_PIPEFS
2849 rc = gss_init_pipefs();
2854 gss_init_at_reply_offset();
2858 #ifdef HAVE_GSS_PIPEFS
2864 cleanup_kerberos_module();
2866 gss_exit_svc_upcall();
2868 gss_exit_cli_upcall();
2874 static void __exit sptlrpc_gss_exit(void)
2877 #ifdef HAVE_GSS_PIPEFS
2880 cleanup_kerberos_module();
2881 gss_exit_svc_upcall();
2882 gss_exit_cli_upcall();
2886 MODULE_AUTHOR("Sun Microsystems, Inc. <http://www.lustre.org/>");
2887 MODULE_DESCRIPTION("GSS security policy for Lustre");
2888 MODULE_LICENSE("GPL");
2890 module_init(sptlrpc_gss_init);
2891 module_exit(sptlrpc_gss_exit);