Whamcloud - gitweb
1d81b547418bbfb92ae29dcf437e9f7c75d5cc56
[fs/lustre-release.git] / lustre / ptlrpc / gss / sec_gss.c
1 /*
2  * Modifications for Lustre
3  *
4  * Copyright (c) 2007, 2010, Oracle and/or its affiliates. All rights reserved.
5  *
6  * Copyright (c) 2011, 2015, Intel Corporation.
7  *
8  * Author: Eric Mei <ericm@clusterfs.com>
9  */
10
11 /*
12  * linux/net/sunrpc/auth_gss.c
13  *
14  * RPCSEC_GSS client authentication.
15  *
16  *  Copyright (c) 2000 The Regents of the University of Michigan.
17  *  All rights reserved.
18  *
19  *  Dug Song       <dugsong@monkey.org>
20  *  Andy Adamson   <andros@umich.edu>
21  *
22  *  Redistribution and use in source and binary forms, with or without
23  *  modification, are permitted provided that the following conditions
24  *  are met:
25  *
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.
34  *
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.
46  *
47  */
48
49 #define DEBUG_SUBSYSTEM S_SEC
50 #include <linux/init.h>
51 #include <linux/module.h>
52 #include <linux/slab.h>
53 #include <linux/dcache.h>
54 #include <linux/fs.h>
55 #include <linux/mutex.h>
56 #include <asm/atomic.h>
57
58 #include <obd.h>
59 #include <obd_class.h>
60 #include <obd_support.h>
61 #include <obd_cksum.h>
62 #include <lustre_net.h>
63 #include <lustre_import.h>
64 #include <lustre_sec.h>
65
66 #include "gss_err.h"
67 #include "gss_internal.h"
68 #include "gss_api.h"
69
70 #include <linux/crypto.h>
71 #include <linux/crc32.h>
72
73 /*
74  * early reply have fixed size, respectively in privacy and integrity mode.
75  * so we calculate them only once.
76  */
77 static int gss_at_reply_off_integ;
78 static int gss_at_reply_off_priv;
79
80
81 static inline int msg_last_segidx(struct lustre_msg *msg)
82 {
83         LASSERT(msg->lm_bufcount > 0);
84         return msg->lm_bufcount - 1;
85 }
86 static inline int msg_last_seglen(struct lustre_msg *msg)
87 {
88         return msg->lm_buflens[msg_last_segidx(msg)];
89 }
90
91  /* wire data swabber */
92 static
93 void gss_header_swabber(struct gss_header *ghdr)
94 {
95         __swab32s(&ghdr->gh_flags);
96         __swab32s(&ghdr->gh_proc);
97         __swab32s(&ghdr->gh_seq);
98         __swab32s(&ghdr->gh_svc);
99         __swab32s(&ghdr->gh_pad1);
100         __swab32s(&ghdr->gh_handle.len);
101 }
102
103 struct gss_header *gss_swab_header(struct lustre_msg *msg, int segment,
104                                    int swabbed)
105 {
106         struct gss_header *ghdr;
107
108         ghdr = lustre_msg_buf(msg, segment, sizeof(*ghdr));
109         if (ghdr == NULL)
110                 return NULL;
111
112         if (swabbed)
113                 gss_header_swabber(ghdr);
114
115         if (sizeof(*ghdr) + ghdr->gh_handle.len > msg->lm_buflens[segment]) {
116                 CERROR("gss header has length %d, now %u received\n",
117                        (int) sizeof(*ghdr) + ghdr->gh_handle.len,
118                        msg->lm_buflens[segment]);
119                 return NULL;
120         }
121
122         return ghdr;
123 }
124
125 /*
126  * payload should be obtained from mechanism. but currently since we
127  * only support kerberos, we could simply use fixed value.
128  * krb5 "meta" data:
129  *  - krb5 header:      16
130  *  - krb5 checksum:    20
131  *
132  * for privacy mode, payload also include the cipher text which has the same
133  * size as plain text, plus possible confounder, padding both at maximum cipher
134  * block size.
135  */
136 #define GSS_KRB5_INTEG_MAX_PAYLOAD      (40)
137
138 static inline
139 int gss_mech_payload(struct gss_ctx *mechctx, int msgsize, int privacy)
140 {
141         if (privacy)
142                 return GSS_KRB5_INTEG_MAX_PAYLOAD + 16 + 16 + 16 + msgsize;
143         else
144                 return GSS_KRB5_INTEG_MAX_PAYLOAD;
145 }
146
147 /*
148  * return signature size, otherwise < 0 to indicate error
149  */
150 static int gss_sign_msg(struct lustre_msg *msg, struct gss_ctx *mechctx,
151                         enum lustre_sec_part sp, __u32 flags, __u32 proc,
152                         __u32 seq, __u32 svc, rawobj_t *handle)
153 {
154         struct gss_header      *ghdr;
155         rawobj_t                text[4], mic;
156         int                     textcnt, max_textcnt, mic_idx;
157         __u32                   major;
158
159         LASSERT(msg->lm_bufcount >= 2);
160
161         /* gss hdr */
162         LASSERT(msg->lm_buflens[0] >=
163                 sizeof(*ghdr) + (handle ? handle->len : 0));
164         ghdr = lustre_msg_buf(msg, 0, 0);
165
166         ghdr->gh_version = PTLRPC_GSS_VERSION;
167         ghdr->gh_sp = (__u8) sp;
168         ghdr->gh_flags = flags;
169         ghdr->gh_proc = proc;
170         ghdr->gh_seq = seq;
171         ghdr->gh_svc = svc;
172         if (!handle) {
173                 /* fill in a fake one */
174                 ghdr->gh_handle.len = 0;
175         } else {
176                 ghdr->gh_handle.len = handle->len;
177                 memcpy(ghdr->gh_handle.data, handle->data, handle->len);
178         }
179
180         /* no actual signature for null mode */
181         if (svc == SPTLRPC_SVC_NULL)
182                 return lustre_msg_size_v2(msg->lm_bufcount, msg->lm_buflens);
183
184         /* MIC */
185         mic_idx = msg_last_segidx(msg);
186         max_textcnt = (svc == SPTLRPC_SVC_AUTH) ? 1 : mic_idx;
187
188         for (textcnt = 0; textcnt < max_textcnt; textcnt++) {
189                 text[textcnt].len = msg->lm_buflens[textcnt];
190                 text[textcnt].data = lustre_msg_buf(msg, textcnt, 0);
191         }
192
193         mic.len = msg->lm_buflens[mic_idx];
194         mic.data = lustre_msg_buf(msg, mic_idx, 0);
195
196         major = lgss_get_mic(mechctx, textcnt, text, 0, NULL, &mic);
197         if (major != GSS_S_COMPLETE) {
198                 CERROR("fail to generate MIC: %08x\n", major);
199                 return -EPERM;
200         }
201         LASSERT(mic.len <= msg->lm_buflens[mic_idx]);
202
203         return lustre_shrink_msg(msg, mic_idx, mic.len, 0);
204 }
205
206 /*
207  * return gss error
208  */
209 static
210 __u32 gss_verify_msg(struct lustre_msg *msg, struct gss_ctx *mechctx,
211                      __u32 svc)
212 {
213         rawobj_t        text[4], mic;
214         int             textcnt, max_textcnt;
215         int             mic_idx;
216         __u32           major;
217
218         LASSERT(msg->lm_bufcount >= 2);
219
220         if (svc == SPTLRPC_SVC_NULL)
221                 return GSS_S_COMPLETE;
222
223         mic_idx = msg_last_segidx(msg);
224         max_textcnt = (svc == SPTLRPC_SVC_AUTH) ? 1 : mic_idx;
225
226         for (textcnt = 0; textcnt < max_textcnt; textcnt++) {
227                 text[textcnt].len = msg->lm_buflens[textcnt];
228                 text[textcnt].data = lustre_msg_buf(msg, textcnt, 0);
229         }
230
231         mic.len = msg->lm_buflens[mic_idx];
232         mic.data = lustre_msg_buf(msg, mic_idx, 0);
233
234         major = lgss_verify_mic(mechctx, textcnt, text, 0, NULL, &mic);
235         if (major != GSS_S_COMPLETE)
236                 CERROR("mic verify error: %08x\n", major);
237
238         return major;
239 }
240
241 /*
242  * return gss error code
243  */
244 static
245 __u32 gss_unseal_msg(struct gss_ctx *mechctx, struct lustre_msg *msgbuf,
246                      int *msg_len, int msgbuf_len)
247 {
248         rawobj_t                 clear_obj, hdrobj, token;
249         __u8                    *clear_buf;
250         int                      clear_buflen;
251         __u32                    major;
252         ENTRY;
253
254         if (msgbuf->lm_bufcount != 2) {
255                 CERROR("invalid bufcount %d\n", msgbuf->lm_bufcount);
256                 RETURN(GSS_S_FAILURE);
257         }
258
259         /* allocate a temporary clear text buffer, same sized as token,
260          * we assume the final clear text size <= token size
261          */
262         clear_buflen = lustre_msg_buflen(msgbuf, 1);
263         OBD_ALLOC_LARGE(clear_buf, clear_buflen);
264         if (!clear_buf)
265                 RETURN(GSS_S_FAILURE);
266
267         /* buffer objects */
268         hdrobj.len = lustre_msg_buflen(msgbuf, 0);
269         hdrobj.data = lustre_msg_buf(msgbuf, 0, 0);
270         token.len = lustre_msg_buflen(msgbuf, 1);
271         token.data = lustre_msg_buf(msgbuf, 1, 0);
272         clear_obj.len = clear_buflen;
273         clear_obj.data = clear_buf;
274
275         major = lgss_unwrap(mechctx, &hdrobj, &token, &clear_obj);
276         if (major != GSS_S_COMPLETE) {
277                 CERROR("unwrap message error: %08x\n", major);
278                 GOTO(out_free, major = GSS_S_FAILURE);
279         }
280         LASSERT(clear_obj.len <= clear_buflen);
281         LASSERT(clear_obj.len <= msgbuf_len);
282
283         /* now the decrypted message */
284         memcpy(msgbuf, clear_obj.data, clear_obj.len);
285         *msg_len = clear_obj.len;
286
287         major = GSS_S_COMPLETE;
288 out_free:
289         OBD_FREE_LARGE(clear_buf, clear_buflen);
290         RETURN(major);
291 }
292
293 /* gss client context manipulation helpers  */
294 int cli_ctx_expire(struct ptlrpc_cli_ctx *ctx)
295 {
296         LASSERT(atomic_read(&ctx->cc_refcount));
297
298         if (!test_and_set_bit(PTLRPC_CTX_DEAD_BIT, &ctx->cc_flags)) {
299                 if (!ctx->cc_early_expire)
300                         clear_bit(PTLRPC_CTX_UPTODATE_BIT, &ctx->cc_flags);
301
302                 CDEBUG(D_SEC, "ctx %p(%u->%s) get expired: %lld(%+llds)\n",
303                        ctx, ctx->cc_vcred.vc_uid, sec2target_str(ctx->cc_sec),
304                        ctx->cc_expire,
305                        ctx->cc_expire == 0 ? 0 :
306                        ctx->cc_expire - ktime_get_real_seconds());
307
308                 sptlrpc_cli_ctx_wakeup(ctx);
309                 return 1;
310         }
311
312         return 0;
313 }
314
315 /*
316  * return 1 if the context is dead.
317  */
318 int cli_ctx_check_death(struct ptlrpc_cli_ctx *ctx)
319 {
320         if (unlikely(cli_ctx_is_dead(ctx)))
321                 return 1;
322
323         /* expire is 0 means never expire. a newly created gss context
324          * which during upcall may has 0 expiration
325          */
326         if (ctx->cc_expire == 0)
327                 return 0;
328
329         /* check real expiration */
330         if (ctx->cc_expire > ktime_get_real_seconds())
331                 return 0;
332
333         cli_ctx_expire(ctx);
334         return 1;
335 }
336
337 void gss_cli_ctx_uptodate(struct gss_cli_ctx *gctx)
338 {
339         struct ptlrpc_cli_ctx *ctx = &gctx->gc_base;
340         time64_t ctx_expiry;
341
342         if (lgss_inquire_context(gctx->gc_mechctx, &ctx_expiry)) {
343                 CERROR("ctx %p(%u): unable to inquire, expire it now\n",
344                        gctx, ctx->cc_vcred.vc_uid);
345                 ctx_expiry = 1; /* make it expired now */
346         }
347
348         ctx->cc_expire = gss_round_ctx_expiry(ctx_expiry,
349                                               ctx->cc_sec->ps_flvr.sf_flags);
350
351         /* At this point this ctx might have been marked as dead by
352          * someone else, in which case nobody will make further use
353          * of it. we don't care, and mark it UPTODATE will help
354          * destroying server side context when it be destroyed.
355          */
356         set_bit(PTLRPC_CTX_UPTODATE_BIT, &ctx->cc_flags);
357
358         if (sec_is_reverse(ctx->cc_sec)) {
359                 CDEBUG(D_SEC, "server installed reverse ctx %p idx %#llx, expiry %lld(%+llds)\n",
360                        ctx, gss_handle_to_u64(&gctx->gc_handle),
361                        ctx->cc_expire,
362                        ctx->cc_expire - ktime_get_real_seconds());
363         } else {
364                 CDEBUG(D_SEC, "client refreshed ctx %p idx %#llx (%u->%s), expiry %lld(%+llds)\n",
365                        ctx, gss_handle_to_u64(&gctx->gc_handle),
366                        ctx->cc_vcred.vc_uid, sec2target_str(ctx->cc_sec),
367                        ctx->cc_expire,
368                        ctx->cc_expire - ktime_get_real_seconds());
369
370                 /* install reverse svc ctx for root context */
371                 if (ctx->cc_vcred.vc_uid == 0)
372                         gss_sec_install_rctx(ctx->cc_sec->ps_import,
373                                              ctx->cc_sec, ctx);
374         }
375
376         sptlrpc_cli_ctx_wakeup(ctx);
377 }
378
379 static void gss_cli_ctx_finalize(struct gss_cli_ctx *gctx)
380 {
381         LASSERT(gctx->gc_base.cc_sec);
382
383         if (gctx->gc_mechctx) {
384                 lgss_delete_sec_context(&gctx->gc_mechctx);
385                 gctx->gc_mechctx = NULL;
386         }
387
388         if (!rawobj_empty(&gctx->gc_svc_handle)) {
389                 /* forward ctx: mark buddy reverse svcctx soon-expire. */
390                 if (!sec_is_reverse(gctx->gc_base.cc_sec) &&
391                     !rawobj_empty(&gctx->gc_svc_handle))
392                         gss_svc_upcall_expire_rvs_ctx(&gctx->gc_svc_handle);
393
394                 rawobj_free(&gctx->gc_svc_handle);
395         }
396
397         rawobj_free(&gctx->gc_handle);
398 }
399
400 /**
401  * Based on sequence number algorithm as specified in RFC 2203.
402  *
403  * Modified for our own problem: arriving request has valid sequence number,
404  * but unwrapping request might cost a long time, after that its sequence
405  * are not valid anymore (fall behind the window). It rarely happen, mostly
406  * under extreme load.
407  *
408  * Note we should not check sequence before verifying the integrity of incoming
409  * request, because just one attacking request with high sequence number might
410  * cause all following requests be dropped.
411  *
412  * So here we use a multi-phase approach: prepare 2 sequence windows,
413  * "main window" for normal sequence and "back window" for fall behind sequence.
414  * and 3-phase checking mechanism:
415  *  0 - before integrity verification, perform an initial sequence checking in
416  *      main window, which only tries and doesn't actually set any bits. if the
417  *      sequence is high above the window or fits in the window and the bit
418  *      is 0, then accept and proceed to integrity verification. otherwise
419  *      reject this sequence.
420  *  1 - after integrity verification, check in main window again. if this
421  *      sequence is high above the window or fits in the window and the bit
422  *      is 0, then set the bit and accept; if it fits in the window but bit
423  *      already set, then reject; if it falls behind the window, then proceed
424  *      to phase 2.
425  *  2 - check in back window. if it is high above the window or fits in the
426  *      window and the bit is 0, then set the bit and accept. otherwise reject.
427  *
428  * \return       1:     looks like a replay
429  * \return       0:     is ok
430  * \return      -1:     is a replay
431  *
432  * Note phase 0 is necessary, because otherwise replay attacking request of
433  * sequence which between the 2 windows can't be detected.
434  *
435  * This mechanism can't totally solve the problem, but could help reduce the
436  * number of valid requests be dropped.
437  */
438 static
439 int gss_do_check_seq(unsigned long *window, __u32 win_size, __u32 *max_seq,
440                      __u32 seq_num, int phase)
441 {
442         LASSERT(phase >= 0 && phase <= 2);
443
444         if (seq_num > *max_seq) {
445                 /*
446                  * 1. high above the window
447                  */
448                 if (phase == 0)
449                         return 0;
450
451                 if (seq_num >= *max_seq + win_size) {
452                         memset(window, 0, win_size / 8);
453                         *max_seq = seq_num;
454                 } else {
455                         while (*max_seq < seq_num) {
456                                 (*max_seq)++;
457                                 __clear_bit((*max_seq) % win_size, window);
458                         }
459                 }
460                 __set_bit(seq_num % win_size, window);
461         } else if (seq_num + win_size <= *max_seq) {
462                 /*
463                  * 2. low behind the window
464                  */
465                 if (phase == 0 || phase == 2)
466                         goto replay;
467
468                 CWARN("seq %u is %u behind (size %d), check backup window\n",
469                       seq_num, *max_seq - win_size - seq_num, win_size);
470                 return 1;
471         } else {
472                 /*
473                  * 3. fit into the window
474                  */
475                 switch (phase) {
476                 case 0:
477                         if (test_bit(seq_num % win_size, window))
478                                 goto replay;
479                         break;
480                 case 1:
481                 case 2:
482                         if (__test_and_set_bit(seq_num % win_size, window))
483                                 goto replay;
484                         break;
485                 }
486         }
487
488         return 0;
489
490 replay:
491         CERROR("seq %u (%s %s window) is a replay: max %u, winsize %d\n",
492                seq_num,
493                seq_num + win_size > *max_seq ? "in" : "behind",
494                phase == 2 ? "backup " : "main",
495                *max_seq, win_size);
496         return -1;
497 }
498
499 /*
500  * Based on sequence number algorithm as specified in RFC 2203.
501  *
502  * if @set == 0: initial check, don't set any bit in window
503  * if @sec == 1: final check, set bit in window
504  */
505 int gss_check_seq_num(struct gss_svc_seq_data *ssd, __u32 seq_num, int set)
506 {
507         int rc = 0;
508
509         spin_lock(&ssd->ssd_lock);
510
511         if (set == 0) {
512                 /*
513                  * phase 0 testing
514                  */
515                 rc = gss_do_check_seq(ssd->ssd_win_main, GSS_SEQ_WIN_MAIN,
516                                       &ssd->ssd_max_main, seq_num, 0);
517                 if (unlikely(rc))
518                         gss_stat_oos_record_svc(0, 1);
519         } else {
520                 /*
521                  * phase 1 checking main window
522                  */
523                 rc = gss_do_check_seq(ssd->ssd_win_main, GSS_SEQ_WIN_MAIN,
524                                       &ssd->ssd_max_main, seq_num, 1);
525                 switch (rc) {
526                 case -1:
527                         gss_stat_oos_record_svc(1, 1);
528                         fallthrough;
529                 case 0:
530                         goto exit;
531                 }
532                 /*
533                  * phase 2 checking back window
534                  */
535                 rc = gss_do_check_seq(ssd->ssd_win_back, GSS_SEQ_WIN_BACK,
536                                       &ssd->ssd_max_back, seq_num, 2);
537                 if (rc)
538                         gss_stat_oos_record_svc(2, 1);
539                 else
540                         gss_stat_oos_record_svc(2, 0);
541         }
542 exit:
543         spin_unlock(&ssd->ssd_lock);
544         return rc;
545 }
546
547 /* cred APIs */
548 static inline int gss_cli_payload(struct ptlrpc_cli_ctx *ctx, int msgsize,
549                                   int privacy)
550 {
551         return gss_mech_payload(NULL, msgsize, privacy);
552 }
553
554 static int gss_cli_bulk_payload(struct ptlrpc_cli_ctx *ctx,
555                                 struct sptlrpc_flavor *flvr,
556                                 int reply, int read)
557 {
558         int payload = sizeof(struct ptlrpc_bulk_sec_desc);
559
560         LASSERT(SPTLRPC_FLVR_BULK_TYPE(flvr->sf_rpc) == SPTLRPC_BULK_DEFAULT);
561
562         if ((!reply && !read) || (reply && read)) {
563                 switch (SPTLRPC_FLVR_BULK_SVC(flvr->sf_rpc)) {
564                 case SPTLRPC_BULK_SVC_NULL:
565                         break;
566                 case SPTLRPC_BULK_SVC_INTG:
567                         payload += gss_cli_payload(ctx, 0, 0);
568                         break;
569                 case SPTLRPC_BULK_SVC_PRIV:
570                         payload += gss_cli_payload(ctx, 0, 1);
571                         break;
572                 case SPTLRPC_BULK_SVC_AUTH:
573                 default:
574                         LBUG();
575                 }
576         }
577
578         return payload;
579 }
580
581 int gss_cli_ctx_match(struct ptlrpc_cli_ctx *ctx, struct vfs_cred *vcred)
582 {
583         return (ctx->cc_vcred.vc_uid == vcred->vc_uid);
584 }
585
586 void gss_cli_ctx_flags2str(unsigned long flags, char *buf, int bufsize)
587 {
588         buf[0] = '\0';
589
590         if (flags & PTLRPC_CTX_NEW)
591                 strlcat(buf, "new, ", bufsize);
592         if (flags & PTLRPC_CTX_UPTODATE)
593                 strlcat(buf, "uptodate, ", bufsize);
594         if (flags & PTLRPC_CTX_DEAD)
595                 strlcat(buf, "dead, ", bufsize);
596         if (flags & PTLRPC_CTX_ERROR)
597                 strlcat(buf, "error, ", bufsize);
598         if (flags & PTLRPC_CTX_CACHED)
599                 strlcat(buf, "cached, ", bufsize);
600         if (flags & PTLRPC_CTX_ETERNAL)
601                 strlcat(buf, "eternal, ", bufsize);
602         if (buf[strlen(buf) - 2] == ',')
603                 buf[strlen(buf) - 2] = '\0';
604 }
605
606 int gss_cli_ctx_sign(struct ptlrpc_cli_ctx *ctx, struct ptlrpc_request *req)
607 {
608         struct gss_cli_ctx      *gctx = ctx2gctx(ctx);
609         __u32                    flags = 0, seq, svc;
610         int                      rc;
611         ENTRY;
612
613         LASSERT(req->rq_reqbuf);
614         LASSERT(req->rq_reqbuf->lm_bufcount >= 2);
615         LASSERT(req->rq_cli_ctx == ctx);
616
617         /* nothing to do for context negotiation RPCs */
618         if (req->rq_ctx_init)
619                 RETURN(0);
620
621         svc = SPTLRPC_FLVR_SVC(req->rq_flvr.sf_rpc);
622         if (req->rq_pack_bulk)
623                 flags |= LUSTRE_GSS_PACK_BULK;
624         if (req->rq_pack_udesc)
625                 flags |= LUSTRE_GSS_PACK_USER;
626
627 redo:
628         seq = atomic_inc_return(&gctx->gc_seq);
629
630         rc = gss_sign_msg(req->rq_reqbuf, gctx->gc_mechctx,
631                           ctx->cc_sec->ps_part,
632                           flags, gctx->gc_proc, seq, svc,
633                           &gctx->gc_handle);
634         if (rc < 0)
635                 RETURN(rc);
636
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().
642          *
643          * Note: null mode doesn't check sequence number.
644          */
645         if (svc != SPTLRPC_SVC_NULL &&
646             atomic_read(&gctx->gc_seq) - seq > GSS_SEQ_REPACK_THRESHOLD) {
647                 int behind = atomic_read(&gctx->gc_seq) - seq;
648
649                 gss_stat_oos_record_cli(behind);
650                 CWARN("req %p: %u behind, retry signing\n", req, behind);
651                 goto redo;
652         }
653
654         req->rq_reqdata_len = rc;
655         RETURN(0);
656 }
657
658 static
659 int gss_cli_ctx_handle_err_notify(struct ptlrpc_cli_ctx *ctx,
660                                   struct ptlrpc_request *req,
661                                   struct gss_header *ghdr)
662 {
663         struct gss_err_header *errhdr;
664         int rc;
665
666         LASSERT(ghdr->gh_proc == PTLRPC_GSS_PROC_ERR);
667
668         errhdr = (struct gss_err_header *) ghdr;
669
670         CWARN("%s: req x%llu/t%llu, ctx %p idx %#llx(%u->%s): %sserver respond (%08x/%08x)\n",
671               ctx->cc_sec->ps_import->imp_obd->obd_name,
672               req->rq_xid, req->rq_transno, ctx,
673               gss_handle_to_u64(&ctx2gctx(ctx)->gc_handle),
674               ctx->cc_vcred.vc_uid, sec2target_str(ctx->cc_sec),
675               sec_is_reverse(ctx->cc_sec) ? "reverse " : "",
676               errhdr->gh_major, errhdr->gh_minor);
677
678         /* context fini rpc, let it failed */
679         if (req->rq_ctx_fini) {
680                 CWARN("%s: context fini rpc failed: rc = %d\n",
681                       ctx->cc_sec->ps_import->imp_obd->obd_name, -EINVAL);
682                 return -EINVAL;
683         }
684
685         /* reverse sec, just return error, don't expire this ctx because it's
686          * crucial to callback rpcs. note if the callback rpc failed because
687          * of bit flip during network transfer, the client will be evicted
688          * directly. so more gracefully we probably want let it retry for
689          * number of times.
690          */
691         if (sec_is_reverse(ctx->cc_sec) &&
692             errhdr->gh_major != GSS_S_NO_CONTEXT)
693                 return -EINVAL;
694
695         if (errhdr->gh_major != GSS_S_NO_CONTEXT &&
696             errhdr->gh_major != GSS_S_BAD_SIG)
697                 return -EACCES;
698
699         /* server return NO_CONTEXT might be caused by context expire
700          * or server reboot/failover. we try to refresh a new ctx which
701          * be transparent to upper layer.
702          *
703          * In some cases, our gss handle is possible to be incidentally
704          * identical to another handle since the handle itself is not
705          * fully random. In krb5 case, the GSS_S_BAD_SIG will be
706          * returned, maybe other gss error for other mechanism.
707          *
708          * if we add new mechanism, make sure the correct error are
709          * returned in this case.
710          */
711         CWARN("%s: %s might have lost the context (%s), retrying\n",
712               ctx->cc_sec->ps_import->imp_obd->obd_name,
713               sec_is_reverse(ctx->cc_sec) ? "client" : "server",
714               errhdr->gh_major == GSS_S_NO_CONTEXT ?  "NO_CONTEXT" : "BAD_SIG");
715
716         sptlrpc_cli_ctx_expire(ctx);
717
718         /* we need replace the ctx right here, otherwise during
719          * resent we'll hit the logic in sptlrpc_req_refresh_ctx()
720          * which keep the ctx with RESEND flag, thus we'll never
721          * get rid of this ctx.
722          */
723         rc = sptlrpc_req_replace_dead_ctx(req);
724         if (rc == 0)
725                 req->rq_resend = 1;
726
727         return rc;
728 }
729
730 int gss_cli_ctx_verify(struct ptlrpc_cli_ctx *ctx, struct ptlrpc_request *req)
731 {
732         struct gss_cli_ctx     *gctx;
733         struct gss_header      *ghdr, *reqhdr;
734         struct lustre_msg      *msg = req->rq_repdata;
735         __u32                   major;
736         int                     pack_bulk, swabbed, rc = 0;
737         ENTRY;
738
739         LASSERT(req->rq_cli_ctx == ctx);
740         LASSERT(msg);
741
742         gctx = container_of(ctx, struct gss_cli_ctx, gc_base);
743
744         /* special case for context negotiation, rq_repmsg/rq_replen actually
745          * are not used currently. but early reply always be treated normally
746          */
747         if (req->rq_ctx_init && !req->rq_early) {
748                 req->rq_repmsg = lustre_msg_buf(msg, 1, 0);
749                 req->rq_replen = msg->lm_buflens[1];
750                 RETURN(0);
751         }
752
753         if (msg->lm_bufcount < 2 || msg->lm_bufcount > 4) {
754                 CERROR("unexpected bufcount %u\n", msg->lm_bufcount);
755                 RETURN(-EPROTO);
756         }
757
758         swabbed = req_capsule_rep_need_swab(&req->rq_pill);
759
760         ghdr = gss_swab_header(msg, 0, swabbed);
761         if (ghdr == NULL) {
762                 CERROR("can't decode gss header\n");
763                 RETURN(-EPROTO);
764         }
765
766         /* sanity checks */
767         reqhdr = lustre_msg_buf(msg, 0, sizeof(*reqhdr));
768         LASSERT(reqhdr);
769
770         if (ghdr->gh_version != reqhdr->gh_version) {
771                 CERROR("gss version %u mismatch, expect %u\n",
772                        ghdr->gh_version, reqhdr->gh_version);
773                 RETURN(-EPROTO);
774         }
775
776         switch (ghdr->gh_proc) {
777         case PTLRPC_GSS_PROC_DATA:
778                 pack_bulk = ghdr->gh_flags & LUSTRE_GSS_PACK_BULK;
779
780                 if (!req->rq_early &&
781                     !equi(req->rq_pack_bulk == 1, pack_bulk)) {
782                         CERROR("%s bulk flag in reply\n",
783                                req->rq_pack_bulk ? "missing" : "unexpected");
784                         RETURN(-EPROTO);
785                 }
786
787                 if (ghdr->gh_seq != reqhdr->gh_seq) {
788                         CERROR("seqnum %u mismatch, expect %u\n",
789                                ghdr->gh_seq, reqhdr->gh_seq);
790                         RETURN(-EPROTO);
791                 }
792
793                 if (ghdr->gh_svc != reqhdr->gh_svc) {
794                         CERROR("svc %u mismatch, expect %u\n",
795                                ghdr->gh_svc, reqhdr->gh_svc);
796                         RETURN(-EPROTO);
797                 }
798
799                 if (swabbed)
800                         gss_header_swabber(ghdr);
801
802                 major = gss_verify_msg(msg, gctx->gc_mechctx, reqhdr->gh_svc);
803                 if (major != GSS_S_COMPLETE) {
804                         CERROR("failed to verify reply: %x\n", major);
805                         RETURN(-EPERM);
806                 }
807
808                 if (req->rq_early && reqhdr->gh_svc == SPTLRPC_SVC_NULL) {
809                         __u32 cksum;
810
811                         cksum = crc32_le(!(__u32) 0,
812                                          lustre_msg_buf(msg, 1, 0),
813                                          lustre_msg_buflen(msg, 1));
814                         if (cksum != msg->lm_cksum) {
815                                 CWARN("early reply checksum mismatch: %08x != %08x\n",
816                                       cksum, msg->lm_cksum);
817                                 RETURN(-EPROTO);
818                         }
819                 }
820
821                 if (pack_bulk) {
822                         /* bulk checksum is right after the lustre msg */
823                         if (msg->lm_bufcount < 3) {
824                                 CERROR("Invalid reply bufcount %u\n",
825                                        msg->lm_bufcount);
826                                 RETURN(-EPROTO);
827                         }
828
829                         rc = bulk_sec_desc_unpack(msg, 2, swabbed);
830                         if (rc) {
831                                 CERROR("unpack bulk desc: %d\n", rc);
832                                 RETURN(rc);
833                         }
834                 }
835
836                 req->rq_repmsg = lustre_msg_buf(msg, 1, 0);
837                 req->rq_replen = msg->lm_buflens[1];
838                 break;
839         case PTLRPC_GSS_PROC_ERR:
840                 if (req->rq_early) {
841                         CERROR("server return error with early reply\n");
842                         rc = -EPROTO;
843                 } else {
844                         rc = gss_cli_ctx_handle_err_notify(ctx, req, ghdr);
845                 }
846                 break;
847         default:
848                 CERROR("unknown gss proc %d\n", ghdr->gh_proc);
849                 rc = -EPROTO;
850         }
851
852         RETURN(rc);
853 }
854
855 int gss_cli_ctx_seal(struct ptlrpc_cli_ctx *ctx, struct ptlrpc_request *req)
856 {
857         struct gss_cli_ctx      *gctx;
858         rawobj_t                 hdrobj, msgobj, token;
859         struct gss_header       *ghdr;
860         __u32                    buflens[2], major;
861         int                      wiresize, rc;
862         ENTRY;
863
864         LASSERT(req->rq_clrbuf);
865         LASSERT(req->rq_cli_ctx == ctx);
866         LASSERT(req->rq_reqlen);
867
868         gctx = container_of(ctx, struct gss_cli_ctx, gc_base);
869
870         /* final clear data length */
871         req->rq_clrdata_len = lustre_msg_size_v2(req->rq_clrbuf->lm_bufcount,
872                                                  req->rq_clrbuf->lm_buflens);
873
874         /* calculate wire data length */
875         buflens[0] = PTLRPC_GSS_HEADER_SIZE;
876         buflens[1] = gss_cli_payload(&gctx->gc_base, req->rq_clrdata_len, 1);
877         wiresize = lustre_msg_size_v2(2, buflens);
878
879         /* allocate wire buffer */
880         if (req->rq_pool) {
881                 /* pre-allocated */
882                 LASSERT(req->rq_reqbuf);
883                 LASSERT(req->rq_reqbuf != req->rq_clrbuf);
884                 LASSERT(req->rq_reqbuf_len >= wiresize);
885         } else {
886                 OBD_ALLOC_LARGE(req->rq_reqbuf, wiresize);
887                 if (!req->rq_reqbuf)
888                         RETURN(-ENOMEM);
889                 req->rq_reqbuf_len = wiresize;
890         }
891
892         lustre_init_msg_v2(req->rq_reqbuf, 2, buflens, NULL);
893         req->rq_reqbuf->lm_secflvr = req->rq_flvr.sf_rpc;
894
895         /* gss header */
896         ghdr = lustre_msg_buf(req->rq_reqbuf, 0, 0);
897         ghdr->gh_version = PTLRPC_GSS_VERSION;
898         ghdr->gh_sp = (__u8) ctx->cc_sec->ps_part;
899         ghdr->gh_flags = 0;
900         ghdr->gh_proc = gctx->gc_proc;
901         ghdr->gh_svc = SPTLRPC_SVC_PRIV;
902         ghdr->gh_handle.len = gctx->gc_handle.len;
903         memcpy(ghdr->gh_handle.data, gctx->gc_handle.data, gctx->gc_handle.len);
904         if (req->rq_pack_bulk)
905                 ghdr->gh_flags |= LUSTRE_GSS_PACK_BULK;
906         if (req->rq_pack_udesc)
907                 ghdr->gh_flags |= LUSTRE_GSS_PACK_USER;
908
909 redo:
910         ghdr->gh_seq = atomic_inc_return(&gctx->gc_seq);
911
912         /* buffer objects */
913         hdrobj.len = PTLRPC_GSS_HEADER_SIZE;
914         hdrobj.data = (__u8 *) ghdr;
915         msgobj.len = req->rq_clrdata_len;
916         msgobj.data = (__u8 *) req->rq_clrbuf;
917         token.len = lustre_msg_buflen(req->rq_reqbuf, 1);
918         token.data = lustre_msg_buf(req->rq_reqbuf, 1, 0);
919
920         major = lgss_wrap(gctx->gc_mechctx, &hdrobj, &msgobj,
921                           req->rq_clrbuf_len, &token);
922         if (major != GSS_S_COMPLETE) {
923                 CERROR("priv: wrap message error: %08x\n", major);
924                 GOTO(err_free, rc = -EPERM);
925         }
926         LASSERT(token.len <= buflens[1]);
927
928         /* see explain in gss_cli_ctx_sign() */
929         if (unlikely(atomic_read(&gctx->gc_seq) - ghdr->gh_seq >
930                      GSS_SEQ_REPACK_THRESHOLD)) {
931                 int behind = atomic_read(&gctx->gc_seq) - ghdr->gh_seq;
932
933                 gss_stat_oos_record_cli(behind);
934                 CWARN("req %p: %u behind, retry sealing\n", req, behind);
935
936                 ghdr->gh_seq = atomic_inc_return(&gctx->gc_seq);
937                 goto redo;
938         }
939
940         /* now set the final wire data length */
941         req->rq_reqdata_len = lustre_shrink_msg(req->rq_reqbuf, 1, token.len,
942                                                 0);
943         RETURN(0);
944
945 err_free:
946         if (!req->rq_pool) {
947                 OBD_FREE_LARGE(req->rq_reqbuf, req->rq_reqbuf_len);
948                 req->rq_reqbuf = NULL;
949                 req->rq_reqbuf_len = 0;
950         }
951         RETURN(rc);
952 }
953
954 int gss_cli_ctx_unseal(struct ptlrpc_cli_ctx *ctx, struct ptlrpc_request *req)
955 {
956         struct gss_cli_ctx      *gctx;
957         struct gss_header       *ghdr;
958         struct lustre_msg       *msg = req->rq_repdata;
959         int                      msglen, pack_bulk, swabbed, rc;
960         __u32                    major;
961         ENTRY;
962
963         LASSERT(req->rq_cli_ctx == ctx);
964         LASSERT(req->rq_ctx_init == 0);
965         LASSERT(msg);
966
967         gctx = container_of(ctx, struct gss_cli_ctx, gc_base);
968         swabbed = req_capsule_rep_need_swab(&req->rq_pill);
969
970         ghdr = gss_swab_header(msg, 0, swabbed);
971         if (ghdr == NULL) {
972                 CERROR("can't decode gss header\n");
973                 RETURN(-EPROTO);
974         }
975
976         /* sanity checks */
977         if (ghdr->gh_version != PTLRPC_GSS_VERSION) {
978                 CERROR("gss version %u mismatch, expect %u\n",
979                        ghdr->gh_version, PTLRPC_GSS_VERSION);
980                 RETURN(-EPROTO);
981         }
982
983         switch (ghdr->gh_proc) {
984         case PTLRPC_GSS_PROC_DATA:
985                 pack_bulk = ghdr->gh_flags & LUSTRE_GSS_PACK_BULK;
986
987                 if (!req->rq_early && !equi(req->rq_pack_bulk == 1,
988                                             pack_bulk)) {
989                         CERROR("%s bulk flag in reply\n",
990                                req->rq_pack_bulk ? "missing" : "unexpected");
991                         RETURN(-EPROTO);
992                 }
993
994                 if (swabbed)
995                         gss_header_swabber(ghdr);
996
997                 /* use rq_repdata_len as buffer size, which assume unseal
998                  * doesn't need extra memory space. for precise control, we'd
999                  * better calculate out actual buffer size as
1000                  * (repbuf_len - offset - repdata_len)
1001                  */
1002                 major = gss_unseal_msg(gctx->gc_mechctx, msg, &msglen,
1003                                        req->rq_repdata_len);
1004                 if (major != GSS_S_COMPLETE) {
1005                         CERROR("failed to unwrap reply: %x\n", major);
1006                         rc = -EPERM;
1007                         break;
1008                 }
1009
1010                 swabbed = __lustre_unpack_msg(msg, msglen);
1011                 if (swabbed < 0) {
1012                         CERROR("Failed to unpack after decryption\n");
1013                         RETURN(-EPROTO);
1014                 }
1015
1016                 if (msg->lm_bufcount < 1) {
1017                         CERROR("Invalid reply buffer: empty\n");
1018                         RETURN(-EPROTO);
1019                 }
1020
1021                 if (pack_bulk) {
1022                         if (msg->lm_bufcount < 2) {
1023                                 CERROR("bufcount %u: missing bulk sec desc\n",
1024                                        msg->lm_bufcount);
1025                                 RETURN(-EPROTO);
1026                         }
1027
1028                         /* bulk checksum is the last segment */
1029                         if (bulk_sec_desc_unpack(msg, msg->lm_bufcount - 1,
1030                                                  swabbed))
1031                                 RETURN(-EPROTO);
1032                 }
1033
1034                 req->rq_repmsg = lustre_msg_buf(msg, 0, 0);
1035                 req->rq_replen = msg->lm_buflens[0];
1036
1037                 rc = 0;
1038                 break;
1039         case PTLRPC_GSS_PROC_ERR:
1040                 if (req->rq_early) {
1041                         CERROR("server return error with early reply\n");
1042                         rc = -EPROTO;
1043                 } else {
1044                         rc = gss_cli_ctx_handle_err_notify(ctx, req, ghdr);
1045                 }
1046                 break;
1047         default:
1048                 CERROR("unexpected proc %d\n", ghdr->gh_proc);
1049                 rc = -EPERM;
1050         }
1051
1052         RETURN(rc);
1053 }
1054
1055 /* reverse context installation */
1056 static inline
1057 int gss_install_rvs_svc_ctx(struct obd_import *imp, struct gss_sec *gsec,
1058                             struct gss_cli_ctx *gctx)
1059 {
1060         return gss_svc_upcall_install_rvs_ctx(imp, gsec, gctx);
1061 }
1062
1063 /* GSS security APIs */
1064 int gss_sec_create_common(struct gss_sec *gsec,
1065                           struct ptlrpc_sec_policy *policy,
1066                           struct obd_import *imp,
1067                           struct ptlrpc_svc_ctx *svcctx,
1068                           struct sptlrpc_flavor *sf)
1069 {
1070         struct ptlrpc_sec   *sec;
1071
1072         LASSERT(imp);
1073         LASSERT(SPTLRPC_FLVR_POLICY(sf->sf_rpc) == SPTLRPC_POLICY_GSS);
1074
1075         gsec->gs_mech =
1076                 lgss_subflavor_to_mech(SPTLRPC_FLVR_BASE_SUB(sf->sf_rpc));
1077         if (!gsec->gs_mech) {
1078                 CERROR("gss backend 0x%x not found\n",
1079                        SPTLRPC_FLVR_BASE_SUB(sf->sf_rpc));
1080                 return -EOPNOTSUPP;
1081         }
1082
1083         spin_lock_init(&gsec->gs_lock);
1084         gsec->gs_rvs_hdl = 0ULL;
1085
1086         /* initialize upper ptlrpc_sec */
1087         sec = &gsec->gs_base;
1088         sec->ps_policy = policy;
1089         atomic_set(&sec->ps_refcount, 0);
1090         atomic_set(&sec->ps_nctx, 0);
1091         sec->ps_id = sptlrpc_get_next_secid();
1092         sec->ps_flvr = *sf;
1093         sec->ps_import = class_import_get(imp);
1094         spin_lock_init(&sec->ps_lock);
1095         INIT_LIST_HEAD(&sec->ps_gc_list);
1096
1097         if (!svcctx) {
1098                 sec->ps_gc_interval = GSS_GC_INTERVAL;
1099         } else {
1100                 LASSERT(sec_is_reverse(sec));
1101
1102                 /* never do gc on reverse sec */
1103                 sec->ps_gc_interval = 0;
1104         }
1105
1106         if (SPTLRPC_FLVR_BULK_SVC(sec->ps_flvr.sf_rpc) == SPTLRPC_BULK_SVC_PRIV)
1107                 sptlrpc_pool_add_user();
1108
1109         CDEBUG(D_SEC, "create %s%s@%p\n", (svcctx ? "reverse " : ""),
1110                policy->sp_name, gsec);
1111         return 0;
1112 }
1113
1114 void gss_sec_destroy_common(struct gss_sec *gsec)
1115 {
1116         struct ptlrpc_sec       *sec = &gsec->gs_base;
1117         ENTRY;
1118
1119         LASSERT(sec->ps_import);
1120         LASSERT(atomic_read(&sec->ps_refcount) == 0);
1121         LASSERT(atomic_read(&sec->ps_nctx) == 0);
1122
1123         if (gsec->gs_mech) {
1124                 lgss_mech_put(gsec->gs_mech);
1125                 gsec->gs_mech = NULL;
1126         }
1127
1128         class_import_put(sec->ps_import);
1129         EXIT;
1130 }
1131
1132 void gss_sec_kill(struct ptlrpc_sec *sec)
1133 {
1134         sec->ps_dying = 1;
1135 }
1136
1137 int gss_cli_ctx_init_common(struct ptlrpc_sec *sec, struct ptlrpc_cli_ctx *ctx,
1138                             struct ptlrpc_ctx_ops *ctxops,
1139                             struct vfs_cred *vcred)
1140 {
1141         struct gss_cli_ctx      *gctx = ctx2gctx(ctx);
1142
1143         gctx->gc_win = 0;
1144         atomic_set(&gctx->gc_seq, 0);
1145
1146         INIT_HLIST_NODE(&ctx->cc_cache);
1147         atomic_set(&ctx->cc_refcount, 0);
1148         ctx->cc_sec = sec;
1149         ctx->cc_ops = ctxops;
1150         ctx->cc_expire = 0;
1151         ctx->cc_flags = PTLRPC_CTX_NEW;
1152         ctx->cc_vcred = *vcred;
1153         spin_lock_init(&ctx->cc_lock);
1154         INIT_LIST_HEAD(&ctx->cc_req_list);
1155         INIT_LIST_HEAD(&ctx->cc_gc_chain);
1156
1157         /* take a ref on belonging sec, balanced in ctx destroying */
1158         atomic_inc(&sec->ps_refcount);
1159         /* statistic only */
1160         atomic_inc(&sec->ps_nctx);
1161
1162         CDEBUG(D_SEC, "%s@%p: create ctx %p(%u->%s)\n",
1163                sec->ps_policy->sp_name, ctx->cc_sec,
1164                ctx, ctx->cc_vcred.vc_uid, sec2target_str(ctx->cc_sec));
1165         return 0;
1166 }
1167
1168 /*
1169  * return value:
1170  *   1: the context has been taken care of by someone else
1171  *   0: proceed to really destroy the context locally
1172  */
1173 int gss_cli_ctx_fini_common(struct ptlrpc_sec *sec, struct ptlrpc_cli_ctx *ctx)
1174 {
1175         struct gss_cli_ctx *gctx = ctx2gctx(ctx);
1176
1177         LASSERT(atomic_read(&sec->ps_nctx) > 0);
1178         LASSERT(atomic_read(&ctx->cc_refcount) == 0);
1179         LASSERT(ctx->cc_sec == sec);
1180
1181         /*
1182          * remove UPTODATE flag of reverse ctx thus we won't send fini rpc,
1183          * this is to avoid potential problems of client side reverse svc ctx
1184          * be mis-destroyed in various recovery senarios. anyway client can
1185          * manage its reverse ctx well by associating it with its buddy ctx.
1186          */
1187         if (sec_is_reverse(sec))
1188                 ctx->cc_flags &= ~PTLRPC_CTX_UPTODATE;
1189
1190         if (gctx->gc_mechctx) {
1191                 /* the final context fini rpc will use this ctx too, and it's
1192                  * asynchronous which finished by request_out_callback(). so
1193                  * we add refcount, whoever drop finally drop the refcount to
1194                  * 0 should responsible for the rest of destroy.
1195                  */
1196                 atomic_inc(&ctx->cc_refcount);
1197
1198                 gss_do_ctx_fini_rpc(gctx);
1199                 gss_cli_ctx_finalize(gctx);
1200
1201                 if (!atomic_dec_and_test(&ctx->cc_refcount))
1202                         return 1;
1203         }
1204
1205         if (sec_is_reverse(sec))
1206                 CDEBUG(D_SEC, "reverse sec %p: destroy ctx %p\n",
1207                        ctx->cc_sec, ctx);
1208         else
1209                 CDEBUG(D_SEC, "%s@%p: destroy ctx %p(%u->%s)\n",
1210                        sec->ps_policy->sp_name, ctx->cc_sec,
1211                        ctx, ctx->cc_vcred.vc_uid, sec2target_str(ctx->cc_sec));
1212
1213         return 0;
1214 }
1215
1216 static
1217 int gss_alloc_reqbuf_intg(struct ptlrpc_sec *sec, struct ptlrpc_request *req,
1218                           int svc, int msgsize)
1219 {
1220         int                       bufsize, txtsize;
1221         int                       bufcnt = 2;
1222         __u32                     buflens[5];
1223         ENTRY;
1224
1225         /*
1226          * on-wire data layout:
1227          * - gss header
1228          * - lustre message
1229          * - user descriptor (optional)
1230          * - bulk sec descriptor (optional)
1231          * - signature (optional)
1232          *   - svc == NULL: NULL
1233          *   - svc == AUTH: signature of gss header
1234          *   - svc == INTG: signature of all above
1235          *
1236          * if this is context negotiation, reserver fixed space
1237          * at the last (signature) segment regardless of svc mode.
1238          */
1239
1240         buflens[0] = PTLRPC_GSS_HEADER_SIZE;
1241         txtsize = buflens[0];
1242
1243         buflens[1] = msgsize;
1244         if (svc == SPTLRPC_SVC_INTG)
1245                 txtsize += buflens[1];
1246
1247         if (req->rq_pack_udesc) {
1248                 buflens[bufcnt] = sptlrpc_current_user_desc_size();
1249                 if (svc == SPTLRPC_SVC_INTG)
1250                         txtsize += buflens[bufcnt];
1251                 bufcnt++;
1252         }
1253
1254         if (req->rq_pack_bulk) {
1255                 buflens[bufcnt] = gss_cli_bulk_payload(req->rq_cli_ctx,
1256                                                        &req->rq_flvr,
1257                                                        0, req->rq_bulk_read);
1258                 if (svc == SPTLRPC_SVC_INTG)
1259                         txtsize += buflens[bufcnt];
1260                 bufcnt++;
1261         }
1262
1263         if (req->rq_ctx_init)
1264                 buflens[bufcnt++] = GSS_CTX_INIT_MAX_LEN;
1265         else if (svc != SPTLRPC_SVC_NULL)
1266                 buflens[bufcnt++] = gss_cli_payload(req->rq_cli_ctx, txtsize,
1267                                                     0);
1268
1269         bufsize = lustre_msg_size_v2(bufcnt, buflens);
1270
1271         if (!req->rq_reqbuf) {
1272                 bufsize = size_roundup_power2(bufsize);
1273
1274                 OBD_ALLOC_LARGE(req->rq_reqbuf, bufsize);
1275                 if (!req->rq_reqbuf)
1276                         RETURN(-ENOMEM);
1277
1278                 req->rq_reqbuf_len = bufsize;
1279         } else {
1280                 LASSERT(req->rq_pool);
1281                 LASSERT(req->rq_reqbuf_len >= bufsize);
1282                 memset(req->rq_reqbuf, 0, bufsize);
1283         }
1284
1285         lustre_init_msg_v2(req->rq_reqbuf, bufcnt, buflens, NULL);
1286         req->rq_reqbuf->lm_secflvr = req->rq_flvr.sf_rpc;
1287
1288         req->rq_reqmsg = lustre_msg_buf(req->rq_reqbuf, 1, msgsize);
1289         LASSERT(req->rq_reqmsg);
1290
1291         /* pack user desc here, later we might leave current user's process */
1292         if (req->rq_pack_udesc)
1293                 sptlrpc_pack_user_desc(req->rq_reqbuf, 2);
1294
1295         RETURN(0);
1296 }
1297
1298 static
1299 int gss_alloc_reqbuf_priv(struct ptlrpc_sec *sec, struct ptlrpc_request *req,
1300                           int msgsize)
1301 {
1302         __u32                     ibuflens[3], wbuflens[2];
1303         int                       ibufcnt;
1304         int                       clearsize, wiresize;
1305         ENTRY;
1306
1307         LASSERT(req->rq_clrbuf == NULL);
1308         LASSERT(req->rq_clrbuf_len == 0);
1309
1310         /* Inner (clear) buffers
1311          *  - lustre message
1312          *  - user descriptor (optional)
1313          *  - bulk checksum (optional)
1314          */
1315         ibufcnt = 1;
1316         ibuflens[0] = msgsize;
1317
1318         if (req->rq_pack_udesc)
1319                 ibuflens[ibufcnt++] = sptlrpc_current_user_desc_size();
1320         if (req->rq_pack_bulk)
1321                 ibuflens[ibufcnt++] = gss_cli_bulk_payload(req->rq_cli_ctx,
1322                                                            &req->rq_flvr, 0,
1323                                                            req->rq_bulk_read);
1324
1325         clearsize = lustre_msg_size_v2(ibufcnt, ibuflens);
1326         /* to allow append padding during encryption */
1327         clearsize += GSS_MAX_CIPHER_BLOCK;
1328
1329         /* Wrapper (wire) buffers
1330          *  - gss header
1331          *  - cipher text
1332          */
1333         wbuflens[0] = PTLRPC_GSS_HEADER_SIZE;
1334         wbuflens[1] = gss_cli_payload(req->rq_cli_ctx, clearsize, 1);
1335         wiresize = lustre_msg_size_v2(2, wbuflens);
1336
1337         if (req->rq_pool) {
1338                 /* rq_reqbuf is preallocated */
1339                 LASSERT(req->rq_reqbuf);
1340                 LASSERT(req->rq_reqbuf_len >= wiresize);
1341
1342                 memset(req->rq_reqbuf, 0, req->rq_reqbuf_len);
1343
1344                 /* if the pre-allocated buffer is big enough, we just pack
1345                  * both clear buf & request buf in it, to avoid more alloc.
1346                  */
1347                 if (clearsize + wiresize <= req->rq_reqbuf_len) {
1348                         req->rq_clrbuf =
1349                                 (void *) (((char *) req->rq_reqbuf) + wiresize);
1350                 } else {
1351                         CWARN("pre-allocated buf size %d is not enough for both clear (%d) and cipher (%d) text, proceed with extra allocation\n",
1352                               req->rq_reqbuf_len, clearsize, wiresize);
1353                 }
1354         }
1355
1356         if (!req->rq_clrbuf) {
1357                 clearsize = size_roundup_power2(clearsize);
1358
1359                 OBD_ALLOC_LARGE(req->rq_clrbuf, clearsize);
1360                 if (!req->rq_clrbuf)
1361                         RETURN(-ENOMEM);
1362         }
1363         req->rq_clrbuf_len = clearsize;
1364
1365         lustre_init_msg_v2(req->rq_clrbuf, ibufcnt, ibuflens, NULL);
1366         req->rq_reqmsg = lustre_msg_buf(req->rq_clrbuf, 0, msgsize);
1367
1368         if (req->rq_pack_udesc)
1369                 sptlrpc_pack_user_desc(req->rq_clrbuf, 1);
1370
1371         RETURN(0);
1372 }
1373
1374 /*
1375  * NOTE: any change of request buffer allocation should also consider
1376  * changing enlarge_reqbuf() series functions.
1377  */
1378 int gss_alloc_reqbuf(struct ptlrpc_sec *sec, struct ptlrpc_request *req,
1379                      int msgsize)
1380 {
1381         int     svc = SPTLRPC_FLVR_SVC(req->rq_flvr.sf_rpc);
1382
1383         LASSERT(!req->rq_pack_bulk ||
1384                 (req->rq_bulk_read || req->rq_bulk_write));
1385
1386         switch (svc) {
1387         case SPTLRPC_SVC_NULL:
1388         case SPTLRPC_SVC_AUTH:
1389         case SPTLRPC_SVC_INTG:
1390                 return gss_alloc_reqbuf_intg(sec, req, svc, msgsize);
1391         case SPTLRPC_SVC_PRIV:
1392                 return gss_alloc_reqbuf_priv(sec, req, msgsize);
1393         default:
1394                 LASSERTF(0, "bad rpc flavor %x\n", req->rq_flvr.sf_rpc);
1395                 return 0;
1396         }
1397 }
1398
1399 void gss_free_reqbuf(struct ptlrpc_sec *sec, struct ptlrpc_request *req)
1400 {
1401         int     privacy;
1402         ENTRY;
1403
1404         LASSERT(!req->rq_pool || req->rq_reqbuf);
1405         privacy = SPTLRPC_FLVR_SVC(req->rq_flvr.sf_rpc) == SPTLRPC_SVC_PRIV;
1406
1407         if (!req->rq_clrbuf)
1408                 goto release_reqbuf;
1409
1410         /* release clear buffer */
1411         LASSERT(privacy);
1412         LASSERT(req->rq_clrbuf_len);
1413
1414         if (req->rq_pool == NULL || req->rq_clrbuf < req->rq_reqbuf ||
1415             (char *) req->rq_clrbuf >= (char *) req->rq_reqbuf +
1416             req->rq_reqbuf_len)
1417                 OBD_FREE_LARGE(req->rq_clrbuf, req->rq_clrbuf_len);
1418
1419         req->rq_clrbuf = NULL;
1420         req->rq_clrbuf_len = 0;
1421
1422 release_reqbuf:
1423         if (!req->rq_pool && req->rq_reqbuf) {
1424                 LASSERT(req->rq_reqbuf_len);
1425
1426                 OBD_FREE_LARGE(req->rq_reqbuf, req->rq_reqbuf_len);
1427                 req->rq_reqbuf = NULL;
1428                 req->rq_reqbuf_len = 0;
1429         }
1430
1431         EXIT;
1432 }
1433
1434 static int do_alloc_repbuf(struct ptlrpc_request *req, int bufsize)
1435 {
1436         bufsize = size_roundup_power2(bufsize);
1437
1438         OBD_ALLOC_LARGE(req->rq_repbuf, bufsize);
1439         if (!req->rq_repbuf)
1440                 return -ENOMEM;
1441
1442         req->rq_repbuf_len = bufsize;
1443         return 0;
1444 }
1445
1446 static
1447 int gss_alloc_repbuf_intg(struct ptlrpc_sec *sec, struct ptlrpc_request *req,
1448                           int svc, int msgsize)
1449 {
1450         int             txtsize;
1451         __u32           buflens[4];
1452         int             bufcnt = 2;
1453         int             alloc_size;
1454
1455         /*
1456          * on-wire data layout:
1457          * - gss header
1458          * - lustre message
1459          * - bulk sec descriptor (optional)
1460          * - signature (optional)
1461          *   - svc == NULL: NULL
1462          *   - svc == AUTH: signature of gss header
1463          *   - svc == INTG: signature of all above
1464          *
1465          * if this is context negotiation, reserver fixed space
1466          * at the last (signature) segment regardless of svc mode.
1467          */
1468
1469         buflens[0] = PTLRPC_GSS_HEADER_SIZE;
1470         txtsize = buflens[0];
1471
1472         buflens[1] = msgsize;
1473         if (svc == SPTLRPC_SVC_INTG)
1474                 txtsize += buflens[1];
1475
1476         if (req->rq_pack_bulk) {
1477                 buflens[bufcnt] = gss_cli_bulk_payload(req->rq_cli_ctx,
1478                                                        &req->rq_flvr, 1,
1479                                                        req->rq_bulk_read);
1480                 if (svc == SPTLRPC_SVC_INTG)
1481                         txtsize += buflens[bufcnt];
1482                 bufcnt++;
1483         }
1484
1485         if (req->rq_ctx_init)
1486                 buflens[bufcnt++] = GSS_CTX_INIT_MAX_LEN;
1487         else if (svc != SPTLRPC_SVC_NULL)
1488                 buflens[bufcnt++] = gss_cli_payload(req->rq_cli_ctx, txtsize,
1489                                                     0);
1490
1491         alloc_size = lustre_msg_size_v2(bufcnt, buflens);
1492
1493         /* add space for early reply */
1494         alloc_size += gss_at_reply_off_integ;
1495
1496         return do_alloc_repbuf(req, alloc_size);
1497 }
1498
1499 static
1500 int gss_alloc_repbuf_priv(struct ptlrpc_sec *sec, struct ptlrpc_request *req,
1501                           int msgsize)
1502 {
1503         int             txtsize;
1504         __u32           buflens[2];
1505         int             bufcnt;
1506         int             alloc_size;
1507
1508         /* inner buffers */
1509         bufcnt = 1;
1510         buflens[0] = msgsize;
1511
1512         if (req->rq_pack_bulk)
1513                 buflens[bufcnt++] = gss_cli_bulk_payload(req->rq_cli_ctx,
1514                                                          &req->rq_flvr, 1,
1515                                                          req->rq_bulk_read);
1516         txtsize = lustre_msg_size_v2(bufcnt, buflens);
1517         txtsize += GSS_MAX_CIPHER_BLOCK;
1518
1519         /* wrapper buffers */
1520         bufcnt = 2;
1521         buflens[0] = PTLRPC_GSS_HEADER_SIZE;
1522         buflens[1] = gss_cli_payload(req->rq_cli_ctx, txtsize, 1);
1523
1524         alloc_size = lustre_msg_size_v2(bufcnt, buflens);
1525         /* add space for early reply */
1526         alloc_size += gss_at_reply_off_priv;
1527
1528         return do_alloc_repbuf(req, alloc_size);
1529 }
1530
1531 int gss_alloc_repbuf(struct ptlrpc_sec *sec, struct ptlrpc_request *req,
1532                      int msgsize)
1533 {
1534         int     svc = SPTLRPC_FLVR_SVC(req->rq_flvr.sf_rpc);
1535         ENTRY;
1536
1537         LASSERT(!req->rq_pack_bulk ||
1538                 (req->rq_bulk_read || req->rq_bulk_write));
1539
1540         switch (svc) {
1541         case SPTLRPC_SVC_NULL:
1542         case SPTLRPC_SVC_AUTH:
1543         case SPTLRPC_SVC_INTG:
1544                 return gss_alloc_repbuf_intg(sec, req, svc, msgsize);
1545         case SPTLRPC_SVC_PRIV:
1546                 return gss_alloc_repbuf_priv(sec, req, msgsize);
1547         default:
1548                 LASSERTF(0, "bad rpc flavor %x\n", req->rq_flvr.sf_rpc);
1549                 return 0;
1550         }
1551 }
1552
1553 void gss_free_repbuf(struct ptlrpc_sec *sec, struct ptlrpc_request *req)
1554 {
1555         OBD_FREE_LARGE(req->rq_repbuf, req->rq_repbuf_len);
1556         req->rq_repbuf = NULL;
1557         req->rq_repbuf_len = 0;
1558         req->rq_repdata = NULL;
1559         req->rq_repdata_len = 0;
1560 }
1561
1562 static int get_enlarged_msgsize(struct lustre_msg *msg, int segment,
1563                                 int newsize)
1564 {
1565         int save, newmsg_size;
1566
1567         LASSERT(newsize >= msg->lm_buflens[segment]);
1568
1569         save = msg->lm_buflens[segment];
1570         msg->lm_buflens[segment] = newsize;
1571         newmsg_size = lustre_msg_size_v2(msg->lm_bufcount, msg->lm_buflens);
1572         msg->lm_buflens[segment] = save;
1573
1574         return newmsg_size;
1575 }
1576
1577 static int get_enlarged_msgsize2(struct lustre_msg *msg, int segment1,
1578                                  int newsize1, int segment2, int newsize2)
1579 {
1580         int save1, save2, newmsg_size;
1581
1582         LASSERT(newsize1 >= msg->lm_buflens[segment1]);
1583         LASSERT(newsize2 >= msg->lm_buflens[segment2]);
1584
1585         save1 = msg->lm_buflens[segment1];
1586         save2 = msg->lm_buflens[segment2];
1587         msg->lm_buflens[segment1] = newsize1;
1588         msg->lm_buflens[segment2] = newsize2;
1589         newmsg_size = lustre_msg_size_v2(msg->lm_bufcount, msg->lm_buflens);
1590         msg->lm_buflens[segment1] = save1;
1591         msg->lm_buflens[segment2] = save2;
1592
1593         return newmsg_size;
1594 }
1595
1596 static
1597 int gss_enlarge_reqbuf_intg(struct ptlrpc_sec *sec, struct ptlrpc_request *req,
1598                             int svc, int segment, int newsize)
1599 {
1600         struct lustre_msg      *newbuf;
1601         int                     txtsize, sigsize = 0, i;
1602         int                     newmsg_size, newbuf_size;
1603
1604         /*
1605          * gss header is at seg 0;
1606          * embedded msg is at seg 1;
1607          * signature (if any) is at the last seg
1608          */
1609         LASSERT(req->rq_reqbuf);
1610         LASSERT(req->rq_reqbuf_len > req->rq_reqlen);
1611         LASSERT(req->rq_reqbuf->lm_bufcount >= 2);
1612         LASSERT(lustre_msg_buf(req->rq_reqbuf, 1, 0) == req->rq_reqmsg);
1613
1614         /* 1. compute new embedded msg size */
1615         newmsg_size = get_enlarged_msgsize(req->rq_reqmsg, segment, newsize);
1616         LASSERT(newmsg_size >= req->rq_reqbuf->lm_buflens[1]);
1617
1618         /* 2. compute new wrapper msg size */
1619         if (svc == SPTLRPC_SVC_NULL) {
1620                 /* no signature, get size directly */
1621                 newbuf_size = get_enlarged_msgsize(req->rq_reqbuf, 1,
1622                                                    newmsg_size);
1623         } else {
1624                 txtsize = req->rq_reqbuf->lm_buflens[0];
1625
1626                 if (svc == SPTLRPC_SVC_INTG) {
1627                         for (i = 1; i < req->rq_reqbuf->lm_bufcount; i++)
1628                                 txtsize += req->rq_reqbuf->lm_buflens[i];
1629                         txtsize += newmsg_size - req->rq_reqbuf->lm_buflens[1];
1630                 }
1631
1632                 sigsize = gss_cli_payload(req->rq_cli_ctx, txtsize, 0);
1633                 LASSERT(sigsize >= msg_last_seglen(req->rq_reqbuf));
1634
1635                 newbuf_size = get_enlarged_msgsize2(req->rq_reqbuf, 1,
1636                                                     newmsg_size,
1637                                                     msg_last_segidx(req->rq_reqbuf),
1638                                                     sigsize);
1639         }
1640
1641         /* request from pool should always have enough buffer */
1642         LASSERT(!req->rq_pool || req->rq_reqbuf_len >= newbuf_size);
1643
1644         if (req->rq_reqbuf_len < newbuf_size) {
1645                 newbuf_size = size_roundup_power2(newbuf_size);
1646
1647                 OBD_ALLOC_LARGE(newbuf, newbuf_size);
1648                 if (newbuf == NULL)
1649                         RETURN(-ENOMEM);
1650
1651                 /* Must lock this, so that otherwise unprotected change of
1652                  * rq_reqmsg is not racing with parallel processing of
1653                  * imp_replay_list traversing threads. See LU-3333
1654                  * This is a bandaid at best, we really need to deal with this
1655                  * in request enlarging code before unpacking that's already
1656                  * there
1657                  */
1658                 if (req->rq_import)
1659                         spin_lock(&req->rq_import->imp_lock);
1660
1661                 memcpy(newbuf, req->rq_reqbuf, req->rq_reqbuf_len);
1662
1663                 OBD_FREE_LARGE(req->rq_reqbuf, req->rq_reqbuf_len);
1664                 req->rq_reqbuf = newbuf;
1665                 req->rq_reqbuf_len = newbuf_size;
1666                 req->rq_reqmsg = lustre_msg_buf(req->rq_reqbuf, 1, 0);
1667
1668                 if (req->rq_import)
1669                         spin_unlock(&req->rq_import->imp_lock);
1670         }
1671
1672         /* do enlargement, from wrapper to embedded, from end to begin */
1673         if (svc != SPTLRPC_SVC_NULL)
1674                 _sptlrpc_enlarge_msg_inplace(req->rq_reqbuf,
1675                                              msg_last_segidx(req->rq_reqbuf),
1676                                              sigsize);
1677
1678         _sptlrpc_enlarge_msg_inplace(req->rq_reqbuf, 1, newmsg_size);
1679         _sptlrpc_enlarge_msg_inplace(req->rq_reqmsg, segment, newsize);
1680
1681         req->rq_reqlen = newmsg_size;
1682         RETURN(0);
1683 }
1684
1685 static
1686 int gss_enlarge_reqbuf_priv(struct ptlrpc_sec *sec, struct ptlrpc_request *req,
1687                             int segment, int newsize)
1688 {
1689         struct lustre_msg      *newclrbuf;
1690         int                     newmsg_size, newclrbuf_size, newcipbuf_size;
1691         __u32                   buflens[3];
1692
1693         /*
1694          * embedded msg is at seg 0 of clear buffer;
1695          * cipher text is at seg 2 of cipher buffer;
1696          */
1697         LASSERT(req->rq_pool ||
1698                 (req->rq_reqbuf == NULL && req->rq_reqbuf_len == 0));
1699         LASSERT(req->rq_reqbuf == NULL ||
1700                 (req->rq_pool && req->rq_reqbuf->lm_bufcount == 3));
1701         LASSERT(req->rq_clrbuf);
1702         LASSERT(req->rq_clrbuf_len > req->rq_reqlen);
1703         LASSERT(lustre_msg_buf(req->rq_clrbuf, 0, 0) == req->rq_reqmsg);
1704
1705         /* compute new embedded msg size */
1706         newmsg_size = get_enlarged_msgsize(req->rq_reqmsg, segment, newsize);
1707
1708         /* compute new clear buffer size */
1709         newclrbuf_size = get_enlarged_msgsize(req->rq_clrbuf, 0, newmsg_size);
1710         newclrbuf_size += GSS_MAX_CIPHER_BLOCK;
1711
1712         /* compute new cipher buffer size */
1713         buflens[0] = PTLRPC_GSS_HEADER_SIZE;
1714         buflens[1] = gss_cli_payload(req->rq_cli_ctx, buflens[0], 0);
1715         buflens[2] = gss_cli_payload(req->rq_cli_ctx, newclrbuf_size, 1);
1716         newcipbuf_size = lustre_msg_size_v2(3, buflens);
1717
1718         /* handle the case that we put both clear buf and cipher buf into
1719          * pre-allocated single buffer.
1720          */
1721         if (unlikely(req->rq_pool) && req->rq_clrbuf >= req->rq_reqbuf &&
1722             (char *) req->rq_clrbuf < (char *) req->rq_reqbuf +
1723             req->rq_reqbuf_len) {
1724                 /* it couldn't be better we still fit into the
1725                  * pre-allocated buffer.
1726                  */
1727                 if (newclrbuf_size + newcipbuf_size <= req->rq_reqbuf_len) {
1728                         void *src, *dst;
1729
1730                         if (req->rq_import)
1731                                 spin_lock(&req->rq_import->imp_lock);
1732                         /* move clear text backward. */
1733                         src = req->rq_clrbuf;
1734                         dst = (char *) req->rq_reqbuf + newcipbuf_size;
1735
1736                         memmove(dst, src, req->rq_clrbuf_len);
1737
1738                         req->rq_clrbuf = (struct lustre_msg *) dst;
1739                         req->rq_clrbuf_len = newclrbuf_size;
1740                         req->rq_reqmsg = lustre_msg_buf(req->rq_clrbuf, 0, 0);
1741
1742                         if (req->rq_import)
1743                                 spin_unlock(&req->rq_import->imp_lock);
1744                 } else {
1745                         /* sadly we have to split out the clear buffer */
1746                         LASSERT(req->rq_reqbuf_len >= newcipbuf_size);
1747                         LASSERT(req->rq_clrbuf_len < newclrbuf_size);
1748                 }
1749         }
1750
1751         if (req->rq_clrbuf_len < newclrbuf_size) {
1752                 newclrbuf_size = size_roundup_power2(newclrbuf_size);
1753
1754                 OBD_ALLOC_LARGE(newclrbuf, newclrbuf_size);
1755                 if (newclrbuf == NULL)
1756                         RETURN(-ENOMEM);
1757
1758                 /* Must lock this, so that otherwise unprotected change of
1759                  * rq_reqmsg is not racing with parallel processing of
1760                  * imp_replay_list traversing threads. See LU-3333
1761                  * This is a bandaid at best, we really need to deal with this
1762                  * in request enlarging code before unpacking that's already
1763                  * there
1764                  */
1765                 if (req->rq_import)
1766                         spin_lock(&req->rq_import->imp_lock);
1767
1768                 memcpy(newclrbuf, req->rq_clrbuf, req->rq_clrbuf_len);
1769
1770                 if (req->rq_reqbuf == NULL || req->rq_clrbuf < req->rq_reqbuf ||
1771                     (char *) req->rq_clrbuf >= (char *) req->rq_reqbuf +
1772                     req->rq_reqbuf_len) {
1773                         OBD_FREE_LARGE(req->rq_clrbuf, req->rq_clrbuf_len);
1774                 }
1775
1776                 req->rq_clrbuf = newclrbuf;
1777                 req->rq_clrbuf_len = newclrbuf_size;
1778                 req->rq_reqmsg = lustre_msg_buf(req->rq_clrbuf, 0, 0);
1779
1780                 if (req->rq_import)
1781                         spin_unlock(&req->rq_import->imp_lock);
1782         }
1783
1784         _sptlrpc_enlarge_msg_inplace(req->rq_clrbuf, 0, newmsg_size);
1785         _sptlrpc_enlarge_msg_inplace(req->rq_reqmsg, segment, newsize);
1786         req->rq_reqlen = newmsg_size;
1787
1788         RETURN(0);
1789 }
1790
1791 int gss_enlarge_reqbuf(struct ptlrpc_sec *sec, struct ptlrpc_request *req,
1792                        int segment, int newsize)
1793 {
1794         int     svc = SPTLRPC_FLVR_SVC(req->rq_flvr.sf_rpc);
1795
1796         LASSERT(!req->rq_ctx_init && !req->rq_ctx_fini);
1797
1798         switch (svc) {
1799         case SPTLRPC_SVC_NULL:
1800         case SPTLRPC_SVC_AUTH:
1801         case SPTLRPC_SVC_INTG:
1802                 return gss_enlarge_reqbuf_intg(sec, req, svc, segment, newsize);
1803         case SPTLRPC_SVC_PRIV:
1804                 return gss_enlarge_reqbuf_priv(sec, req, segment, newsize);
1805         default:
1806                 LASSERTF(0, "bad rpc flavor %x\n", req->rq_flvr.sf_rpc);
1807                 return 0;
1808         }
1809 }
1810
1811 int gss_sec_install_rctx(struct obd_import *imp, struct ptlrpc_sec *sec,
1812                          struct ptlrpc_cli_ctx *ctx)
1813 {
1814         struct gss_sec     *gsec;
1815         struct gss_cli_ctx *gctx;
1816         int                 rc;
1817
1818         gsec = container_of(sec, struct gss_sec, gs_base);
1819         gctx = container_of(ctx, struct gss_cli_ctx, gc_base);
1820
1821         rc = gss_install_rvs_svc_ctx(imp, gsec, gctx);
1822         return rc;
1823 }
1824
1825 /* server side API */
1826 static inline
1827 int gss_svc_reqctx_is_special(struct gss_svc_reqctx *grctx)
1828 {
1829         LASSERT(grctx);
1830         return (grctx->src_init || grctx->src_init_continue ||
1831                 grctx->src_err_notify);
1832 }
1833
1834 static
1835 void gss_svc_reqctx_free(struct gss_svc_reqctx *grctx)
1836 {
1837         if (grctx->src_ctx)
1838                 gss_svc_upcall_put_ctx(grctx->src_ctx);
1839
1840         sptlrpc_policy_put(grctx->src_base.sc_policy);
1841         OBD_FREE_PTR(grctx);
1842 }
1843
1844 static inline
1845 void gss_svc_reqctx_addref(struct gss_svc_reqctx *grctx)
1846 {
1847         LASSERT(atomic_read(&grctx->src_base.sc_refcount) > 0);
1848         atomic_inc(&grctx->src_base.sc_refcount);
1849 }
1850
1851 static inline
1852 void gss_svc_reqctx_decref(struct gss_svc_reqctx *grctx)
1853 {
1854         LASSERT(atomic_read(&grctx->src_base.sc_refcount) > 0);
1855
1856         if (atomic_dec_and_test(&grctx->src_base.sc_refcount))
1857                 gss_svc_reqctx_free(grctx);
1858 }
1859
1860 static
1861 int gss_svc_sign(struct ptlrpc_request *req, struct ptlrpc_reply_state *rs,
1862                  struct gss_svc_reqctx *grctx, __u32 svc)
1863 {
1864         __u32   flags = 0;
1865         int     rc;
1866         ENTRY;
1867
1868         LASSERT(rs->rs_msg == lustre_msg_buf(rs->rs_repbuf, 1, 0));
1869
1870         /* embedded lustre_msg might have been shrunk */
1871         if (req->rq_replen != rs->rs_repbuf->lm_buflens[1])
1872                 lustre_shrink_msg(rs->rs_repbuf, 1, req->rq_replen, 1);
1873
1874         if (req->rq_pack_bulk)
1875                 flags |= LUSTRE_GSS_PACK_BULK;
1876
1877         rc = gss_sign_msg(rs->rs_repbuf, grctx->src_ctx->gsc_mechctx,
1878                           LUSTRE_SP_ANY, flags, PTLRPC_GSS_PROC_DATA,
1879                           grctx->src_wirectx.gw_seq, svc, NULL);
1880         if (rc < 0)
1881                 RETURN(rc);
1882
1883         rs->rs_repdata_len = rc;
1884
1885         if (likely(req->rq_packed_final)) {
1886                 if (lustre_msghdr_get_flags(req->rq_reqmsg) & MSGHDR_AT_SUPPORT)
1887                         req->rq_reply_off = gss_at_reply_off_integ;
1888                 else
1889                         req->rq_reply_off = 0;
1890         } else {
1891                 if (svc == SPTLRPC_SVC_NULL)
1892                         rs->rs_repbuf->lm_cksum = crc32_le(!(__u32) 0,
1893                                         lustre_msg_buf(rs->rs_repbuf, 1, 0),
1894                                         lustre_msg_buflen(rs->rs_repbuf, 1));
1895                 req->rq_reply_off = 0;
1896         }
1897
1898         RETURN(0);
1899 }
1900
1901 int gss_pack_err_notify(struct ptlrpc_request *req, __u32 major, __u32 minor)
1902 {
1903         struct gss_svc_reqctx     *grctx = gss_svc_ctx2reqctx(req->rq_svc_ctx);
1904         struct ptlrpc_reply_state *rs;
1905         struct gss_err_header     *ghdr;
1906         int                        replen = sizeof(struct ptlrpc_body);
1907         int                        rc;
1908         ENTRY;
1909
1910         grctx->src_err_notify = 1;
1911         grctx->src_reserve_len = 0;
1912
1913         rc = lustre_pack_reply_v2(req, 1, &replen, NULL, 0);
1914         if (rc) {
1915                 CERROR("could not pack reply, err %d\n", rc);
1916                 RETURN(rc);
1917         }
1918
1919         /* gss hdr */
1920         rs = req->rq_reply_state;
1921         LASSERT(rs->rs_repbuf->lm_buflens[1] >= sizeof(*ghdr));
1922         ghdr = lustre_msg_buf(rs->rs_repbuf, 0, 0);
1923         ghdr->gh_version = PTLRPC_GSS_VERSION;
1924         ghdr->gh_flags = 0;
1925         ghdr->gh_proc = PTLRPC_GSS_PROC_ERR;
1926         ghdr->gh_major = major;
1927         ghdr->gh_minor = minor;
1928         ghdr->gh_handle.len = 0; /* fake context handle */
1929
1930         rs->rs_repdata_len = lustre_msg_size_v2(rs->rs_repbuf->lm_bufcount,
1931                                                 rs->rs_repbuf->lm_buflens);
1932
1933         CDEBUG(D_SEC, "prepare gss error notify(0x%x/0x%x) to %s\n", major,
1934                minor, libcfs_nidstr(&req->rq_peer.nid));
1935         RETURN(0);
1936 }
1937
1938 static
1939 int gss_svc_handle_init(struct ptlrpc_request *req, struct gss_wire_ctx *gw)
1940 {
1941         struct gss_svc_reqctx *grctx = gss_svc_ctx2reqctx(req->rq_svc_ctx);
1942         struct lustre_msg *reqbuf = req->rq_reqbuf;
1943         struct obd_uuid *uuid;
1944         struct obd_device *target;
1945         rawobj_t uuid_obj, rvs_hdl, in_token;
1946         __u32 lustre_svc;
1947         __u32 *secdata, seclen;
1948         int swabbed, rc;
1949
1950         ENTRY;
1951         CDEBUG(D_SEC, "processing gss init(%d) request from %s\n", gw->gw_proc,
1952                libcfs_nidstr(&req->rq_peer.nid));
1953
1954         req->rq_ctx_init = 1;
1955
1956         if (gw->gw_flags & LUSTRE_GSS_PACK_BULK) {
1957                 rc = SECSVC_DROP;
1958                 CDEBUG(D_SEC, "unexpected bulk flag: rc = %d\n", rc);
1959                 RETURN(rc);
1960         }
1961
1962         if (gw->gw_proc == PTLRPC_GSS_PROC_INIT && gw->gw_handle.len != 0) {
1963                 rc = SECSVC_DROP;
1964                 CDEBUG(D_SEC, "proc %u: invalid handle length %u: rc = %d\n",
1965                        gw->gw_proc, gw->gw_handle.len, rc);
1966                 RETURN(rc);
1967         }
1968
1969         if (reqbuf->lm_bufcount < 3 || reqbuf->lm_bufcount > 4) {
1970                 rc = SECSVC_DROP;
1971                 CDEBUG(D_SEC, "Invalid bufcount %d: rc = %d\n",
1972                        reqbuf->lm_bufcount, rc);
1973                 RETURN(rc);
1974         }
1975
1976         swabbed = req_capsule_req_need_swab(&req->rq_pill);
1977
1978         /* ctx initiate payload is in last segment */
1979         secdata = lustre_msg_buf(reqbuf, reqbuf->lm_bufcount - 1, 0);
1980         seclen = reqbuf->lm_buflens[reqbuf->lm_bufcount - 1];
1981
1982         if (seclen < 4 + 4) {
1983                 rc = SECSVC_DROP;
1984                 CDEBUG(D_SEC, "sec size %d too small: rc = %d\n", seclen, rc);
1985                 RETURN(rc);
1986         }
1987
1988         /* lustre svc type */
1989         lustre_svc = le32_to_cpu(*secdata++);
1990         seclen -= 4;
1991
1992         /* extract target uuid, note this code is somewhat fragile
1993          * because touched internal structure of obd_uuid
1994          */
1995         if (rawobj_extract(&uuid_obj, &secdata, &seclen)) {
1996                 rc = SECSVC_DROP;
1997                 CDEBUG(D_SEC, "failed to extract target uuid: rc = %d\n", rc);
1998                 RETURN(rc);
1999         }
2000         uuid_obj.data[uuid_obj.len - 1] = '\0';
2001
2002         uuid = (struct obd_uuid *) uuid_obj.data;
2003         target = class_uuid2obd(uuid);
2004         if (!target || target->obd_stopping || !target->obd_set_up) {
2005                 char *target_start;
2006                 int target_len;
2007
2008                 if (gss_pack_err_notify(req, GSS_S_NO_CONTEXT, 0) == 0)
2009                         rc = SECSVC_COMPLETE;
2010                 else
2011                         rc = SECSVC_DROP;
2012
2013                 deuuidify(uuid->uuid, NULL, &target_start, &target_len);
2014                 LCONSOLE_ERROR("%.*s: not available for GSS context init from %s (%s).\n",
2015                                target_len, target_start,
2016                                libcfs_nidstr(&req->rq_peer.nid),
2017                                target ?
2018                                (target->obd_stopping ?
2019                                 "stopping" : "not set up") :
2020                                "no target");
2021                 RETURN(rc);
2022         }
2023
2024         /* extract reverse handle */
2025         if (rawobj_extract(&rvs_hdl, &secdata, &seclen)) {
2026                 rc = SECSVC_DROP;
2027                 CDEBUG(D_SEC, "%s: failed extract reverse handle: rc = %d\n",
2028                        target->obd_name, rc);
2029                 RETURN(rc);
2030         }
2031
2032         /* extract token */
2033         if (rawobj_extract(&in_token, &secdata, &seclen)) {
2034                 rc = SECSVC_DROP;
2035                 CDEBUG(D_SEC, "%s: can't extract token: rc = %d\n",
2036                        target->obd_name, rc);
2037                 RETURN(rc);
2038         }
2039
2040         rc = gss_svc_upcall_handle_init(req, grctx, gw, target, lustre_svc,
2041                                         &rvs_hdl, &in_token);
2042         if (rc != SECSVC_OK)
2043                 RETURN(rc);
2044
2045         if (grctx->src_ctx->gsc_usr_mds || grctx->src_ctx->gsc_usr_oss ||
2046             grctx->src_ctx->gsc_usr_root)
2047                 CDEBUG(D_SEC,
2048                        "%s: create svc ctx %p: user from %s authenticated as %s\n",
2049                        target->obd_name,
2050                        grctx->src_ctx, libcfs_nidstr(&req->rq_peer.nid),
2051                        grctx->src_ctx->gsc_usr_root ? "root" :
2052                        (grctx->src_ctx->gsc_usr_mds ? "mds" :
2053                         (grctx->src_ctx->gsc_usr_oss ? "oss" : "null")));
2054         else
2055                 CDEBUG(D_SEC, "%s: create svc ctx %p: accept user %u from %s\n",
2056                        target->obd_name,
2057                        grctx->src_ctx, grctx->src_ctx->gsc_uid,
2058                        libcfs_nidstr(&req->rq_peer.nid));
2059
2060         if (gw->gw_flags & LUSTRE_GSS_PACK_USER) {
2061                 if (reqbuf->lm_bufcount < 4) {
2062                         rc = SECSVC_DROP;
2063                         CDEBUG(D_SEC, "%s: missing user descriptor: rc = %d\n",
2064                                target->obd_name, rc);
2065                         RETURN(rc);
2066                 }
2067                 if (sptlrpc_unpack_user_desc(reqbuf, 2, swabbed)) {
2068                         rc = SECSVC_DROP;
2069                         CDEBUG(D_SEC,
2070                                "%s: Mal-formed user descriptor: rc = %d\n",
2071                                target->obd_name, rc);
2072                         RETURN(rc);
2073                 }
2074
2075                 req->rq_pack_udesc = 1;
2076                 req->rq_user_desc = lustre_msg_buf(reqbuf, 2, 0);
2077         }
2078
2079         req->rq_reqmsg = lustre_msg_buf(reqbuf, 1, 0);
2080         req->rq_reqlen = lustre_msg_buflen(reqbuf, 1);
2081
2082         RETURN(rc);
2083 }
2084
2085 /*
2086  * last segment must be the gss signature.
2087  */
2088 static
2089 int gss_svc_verify_request(struct ptlrpc_request *req,
2090                            struct gss_svc_reqctx *grctx,
2091                            struct gss_wire_ctx *gw, __u32 *major)
2092 {
2093         struct gss_svc_ctx *gctx = grctx->src_ctx;
2094         struct lustre_msg  *msg = req->rq_reqbuf;
2095         int                 offset = 2;
2096         int                 swabbed;
2097         ENTRY;
2098
2099         *major = GSS_S_COMPLETE;
2100
2101         if (msg->lm_bufcount < 2) {
2102                 CERROR("Too few segments (%u) in request\n", msg->lm_bufcount);
2103                 RETURN(-EINVAL);
2104         }
2105
2106         if (gw->gw_svc == SPTLRPC_SVC_NULL)
2107                 goto verified;
2108
2109         if (gss_check_seq_num(&gctx->gsc_seqdata, gw->gw_seq, 0)) {
2110                 CERROR("phase 0: discard replayed req: seq %u\n", gw->gw_seq);
2111                 *major = GSS_S_DUPLICATE_TOKEN;
2112                 RETURN(-EACCES);
2113         }
2114
2115         *major = gss_verify_msg(msg, gctx->gsc_mechctx, gw->gw_svc);
2116         if (*major != GSS_S_COMPLETE) {
2117                 CERROR("failed to verify request: %x\n", *major);
2118                 RETURN(-EACCES);
2119         }
2120
2121         if (gctx->gsc_reverse == 0 &&
2122             gss_check_seq_num(&gctx->gsc_seqdata, gw->gw_seq, 1)) {
2123                 CERROR("phase 1+: discard replayed req: seq %u\n", gw->gw_seq);
2124                 *major = GSS_S_DUPLICATE_TOKEN;
2125                 RETURN(-EACCES);
2126         }
2127
2128 verified:
2129         swabbed = req_capsule_req_need_swab(&req->rq_pill);
2130
2131         /* user descriptor */
2132         if (gw->gw_flags & LUSTRE_GSS_PACK_USER) {
2133                 if (msg->lm_bufcount < (offset + 1)) {
2134                         CERROR("no user desc included\n");
2135                         RETURN(-EINVAL);
2136                 }
2137
2138                 if (sptlrpc_unpack_user_desc(msg, offset, swabbed)) {
2139                         CERROR("Mal-formed user descriptor\n");
2140                         RETURN(-EINVAL);
2141                 }
2142
2143                 req->rq_pack_udesc = 1;
2144                 req->rq_user_desc = lustre_msg_buf(msg, offset, 0);
2145                 offset++;
2146         }
2147
2148         /* check bulk_sec_desc data */
2149         if (gw->gw_flags & LUSTRE_GSS_PACK_BULK) {
2150                 if (msg->lm_bufcount < (offset + 1)) {
2151                         CERROR("missing bulk sec descriptor\n");
2152                         RETURN(-EINVAL);
2153                 }
2154
2155                 if (bulk_sec_desc_unpack(msg, offset, swabbed))
2156                         RETURN(-EINVAL);
2157
2158                 req->rq_pack_bulk = 1;
2159                 grctx->src_reqbsd = lustre_msg_buf(msg, offset, 0);
2160                 grctx->src_reqbsd_size = lustre_msg_buflen(msg, offset);
2161         }
2162
2163         req->rq_reqmsg = lustre_msg_buf(msg, 1, 0);
2164         req->rq_reqlen = msg->lm_buflens[1];
2165         RETURN(0);
2166 }
2167
2168 static
2169 int gss_svc_unseal_request(struct ptlrpc_request *req,
2170                            struct gss_svc_reqctx *grctx,
2171                            struct gss_wire_ctx *gw, __u32 *major)
2172 {
2173         struct gss_svc_ctx *gctx = grctx->src_ctx;
2174         struct lustre_msg  *msg = req->rq_reqbuf;
2175         int                 swabbed, msglen, offset = 1;
2176         ENTRY;
2177
2178         if (gss_check_seq_num(&gctx->gsc_seqdata, gw->gw_seq, 0)) {
2179                 CERROR("phase 0: discard replayed req: seq %u\n", gw->gw_seq);
2180                 *major = GSS_S_DUPLICATE_TOKEN;
2181                 RETURN(-EACCES);
2182         }
2183
2184         *major = gss_unseal_msg(gctx->gsc_mechctx, msg, &msglen,
2185                                 req->rq_reqdata_len);
2186         if (*major != GSS_S_COMPLETE) {
2187                 CERROR("failed to unwrap request: %x\n", *major);
2188                 RETURN(-EACCES);
2189         }
2190
2191         if (gss_check_seq_num(&gctx->gsc_seqdata, gw->gw_seq, 1)) {
2192                 CERROR("phase 1+: discard replayed req: seq %u\n", gw->gw_seq);
2193                 *major = GSS_S_DUPLICATE_TOKEN;
2194                 RETURN(-EACCES);
2195         }
2196
2197         swabbed = __lustre_unpack_msg(msg, msglen);
2198         if (swabbed < 0) {
2199                 CERROR("Failed to unpack after decryption\n");
2200                 RETURN(-EINVAL);
2201         }
2202         req->rq_reqdata_len = msglen;
2203
2204         if (msg->lm_bufcount < 1) {
2205                 CERROR("Invalid buffer: is empty\n");
2206                 RETURN(-EINVAL);
2207         }
2208
2209         if (gw->gw_flags & LUSTRE_GSS_PACK_USER) {
2210                 if (msg->lm_bufcount < offset + 1) {
2211                         CERROR("no user descriptor included\n");
2212                         RETURN(-EINVAL);
2213                 }
2214
2215                 if (sptlrpc_unpack_user_desc(msg, offset, swabbed)) {
2216                         CERROR("Mal-formed user descriptor\n");
2217                         RETURN(-EINVAL);
2218                 }
2219
2220                 req->rq_pack_udesc = 1;
2221                 req->rq_user_desc = lustre_msg_buf(msg, offset, 0);
2222                 offset++;
2223         }
2224
2225         if (gw->gw_flags & LUSTRE_GSS_PACK_BULK) {
2226                 if (msg->lm_bufcount < offset + 1) {
2227                         CERROR("no bulk checksum included\n");
2228                         RETURN(-EINVAL);
2229                 }
2230
2231                 if (bulk_sec_desc_unpack(msg, offset, swabbed))
2232                         RETURN(-EINVAL);
2233
2234                 req->rq_pack_bulk = 1;
2235                 grctx->src_reqbsd = lustre_msg_buf(msg, offset, 0);
2236                 grctx->src_reqbsd_size = lustre_msg_buflen(msg, offset);
2237         }
2238
2239         req->rq_reqmsg = lustre_msg_buf(req->rq_reqbuf, 0, 0);
2240         req->rq_reqlen = req->rq_reqbuf->lm_buflens[0];
2241         RETURN(0);
2242 }
2243
2244 static
2245 int gss_svc_handle_data(struct ptlrpc_request *req, struct gss_wire_ctx *gw)
2246 {
2247         struct gss_svc_reqctx *grctx = gss_svc_ctx2reqctx(req->rq_svc_ctx);
2248         __u32                  major = 0;
2249         int                    rc = 0;
2250         ENTRY;
2251
2252         grctx->src_ctx = gss_svc_upcall_get_ctx(req, gw);
2253         if (!grctx->src_ctx) {
2254                 major = GSS_S_NO_CONTEXT;
2255                 goto error;
2256         }
2257
2258         switch (gw->gw_svc) {
2259         case SPTLRPC_SVC_NULL:
2260         case SPTLRPC_SVC_AUTH:
2261         case SPTLRPC_SVC_INTG:
2262                 rc = gss_svc_verify_request(req, grctx, gw, &major);
2263                 break;
2264         case SPTLRPC_SVC_PRIV:
2265                 rc = gss_svc_unseal_request(req, grctx, gw, &major);
2266                 break;
2267         default:
2268                 CERROR("unsupported gss service %d\n", gw->gw_svc);
2269                 rc = -EINVAL;
2270         }
2271
2272         if (rc == 0)
2273                 RETURN(SECSVC_OK);
2274
2275         CERROR("svc %u failed: major 0x%08x: req xid %llu ctx %p idx %#llx(%u->%s)\n",
2276                gw->gw_svc, major, req->rq_xid, grctx->src_ctx,
2277                gss_handle_to_u64(&gw->gw_handle), grctx->src_ctx->gsc_uid,
2278                libcfs_nidstr(&req->rq_peer.nid));
2279 error:
2280         /* we only notify client in case of NO_CONTEXT/BAD_SIG, which
2281          * might happen after server reboot, to allow recovery.
2282          */
2283         if ((major == GSS_S_NO_CONTEXT || major == GSS_S_BAD_SIG) &&
2284             gss_pack_err_notify(req, major, 0) == 0)
2285                 RETURN(SECSVC_COMPLETE);
2286
2287         RETURN(SECSVC_DROP);
2288 }
2289
2290 static
2291 int gss_svc_handle_destroy(struct ptlrpc_request *req, struct gss_wire_ctx *gw)
2292 {
2293         struct gss_svc_reqctx  *grctx = gss_svc_ctx2reqctx(req->rq_svc_ctx);
2294         __u32                   major;
2295         ENTRY;
2296
2297         req->rq_ctx_fini = 1;
2298         req->rq_no_reply = 1;
2299
2300         grctx->src_ctx = gss_svc_upcall_get_ctx(req, gw);
2301         if (!grctx->src_ctx) {
2302                 CDEBUG(D_SEC, "invalid gss context handle for destroy.\n");
2303                 RETURN(SECSVC_DROP);
2304         }
2305
2306         if (gw->gw_svc != SPTLRPC_SVC_INTG) {
2307                 CERROR("svc %u is not supported in destroy.\n", gw->gw_svc);
2308                 RETURN(SECSVC_DROP);
2309         }
2310
2311         if (gss_svc_verify_request(req, grctx, gw, &major))
2312                 RETURN(SECSVC_DROP);
2313
2314         CDEBUG(D_SEC, "destroy svc ctx %p idx %#llx (%u->%s)\n",
2315                grctx->src_ctx, gss_handle_to_u64(&gw->gw_handle),
2316                grctx->src_ctx->gsc_uid, libcfs_nidstr(&req->rq_peer.nid));
2317
2318         gss_svc_upcall_destroy_ctx(grctx->src_ctx);
2319
2320         if (gw->gw_flags & LUSTRE_GSS_PACK_USER) {
2321                 if (req->rq_reqbuf->lm_bufcount < 4) {
2322                         CERROR("missing user descriptor, ignore it\n");
2323                         RETURN(SECSVC_OK);
2324                 }
2325                 if (sptlrpc_unpack_user_desc(req->rq_reqbuf, 2,
2326                                 req_capsule_req_need_swab(&req->rq_pill))) {
2327                         CERROR("Mal-formed user descriptor, ignore it\n");
2328                         RETURN(SECSVC_OK);
2329                 }
2330
2331                 req->rq_pack_udesc = 1;
2332                 req->rq_user_desc = lustre_msg_buf(req->rq_reqbuf, 2, 0);
2333         }
2334
2335         RETURN(SECSVC_OK);
2336 }
2337
2338 int gss_svc_accept(struct ptlrpc_sec_policy *policy, struct ptlrpc_request *req)
2339 {
2340         struct gss_header      *ghdr;
2341         struct gss_svc_reqctx  *grctx;
2342         struct gss_wire_ctx    *gw;
2343         int                     swabbed, rc;
2344         ENTRY;
2345
2346         LASSERT(req->rq_reqbuf);
2347         LASSERT(req->rq_svc_ctx == NULL);
2348
2349         if (req->rq_reqbuf->lm_bufcount < 2) {
2350                 CERROR("buf count only %d\n", req->rq_reqbuf->lm_bufcount);
2351                 RETURN(SECSVC_DROP);
2352         }
2353
2354         swabbed = req_capsule_req_need_swab(&req->rq_pill);
2355
2356         ghdr = gss_swab_header(req->rq_reqbuf, 0, swabbed);
2357         if (ghdr == NULL) {
2358                 CERROR("can't decode gss header\n");
2359                 RETURN(SECSVC_DROP);
2360         }
2361
2362         /* sanity checks */
2363         if (ghdr->gh_version != PTLRPC_GSS_VERSION) {
2364                 CERROR("gss version %u, expect %u\n", ghdr->gh_version,
2365                        PTLRPC_GSS_VERSION);
2366                 RETURN(SECSVC_DROP);
2367         }
2368
2369         req->rq_sp_from = ghdr->gh_sp;
2370
2371         /* alloc grctx data */
2372         OBD_ALLOC_PTR(grctx);
2373         if (!grctx)
2374                 RETURN(SECSVC_DROP);
2375
2376         grctx->src_base.sc_policy = sptlrpc_policy_get(policy);
2377         atomic_set(&grctx->src_base.sc_refcount, 1);
2378         req->rq_svc_ctx = &grctx->src_base;
2379         gw = &grctx->src_wirectx;
2380
2381         /* save wire context */
2382         gw->gw_flags = ghdr->gh_flags;
2383         gw->gw_proc = ghdr->gh_proc;
2384         gw->gw_seq = ghdr->gh_seq;
2385         gw->gw_svc = ghdr->gh_svc;
2386         rawobj_from_netobj(&gw->gw_handle, &ghdr->gh_handle);
2387
2388         /* keep original wire header which subject to checksum verification */
2389         if (swabbed)
2390                 gss_header_swabber(ghdr);
2391
2392         switch (ghdr->gh_proc) {
2393         case PTLRPC_GSS_PROC_INIT:
2394         case PTLRPC_GSS_PROC_CONTINUE_INIT:
2395                 rc = gss_svc_handle_init(req, gw);
2396                 break;
2397         case PTLRPC_GSS_PROC_DATA:
2398                 rc = gss_svc_handle_data(req, gw);
2399                 break;
2400         case PTLRPC_GSS_PROC_DESTROY:
2401                 rc = gss_svc_handle_destroy(req, gw);
2402                 break;
2403         default:
2404                 CERROR("unknown proc %u\n", gw->gw_proc);
2405                 rc = SECSVC_DROP;
2406                 break;
2407         }
2408
2409         switch (rc) {
2410         case SECSVC_OK:
2411                 LASSERT (grctx->src_ctx);
2412
2413                 req->rq_auth_gss = 1;
2414                 req->rq_auth_usr_mdt = grctx->src_ctx->gsc_usr_mds;
2415                 req->rq_auth_usr_ost = grctx->src_ctx->gsc_usr_oss;
2416                 req->rq_auth_usr_root = grctx->src_ctx->gsc_usr_root;
2417                 req->rq_auth_uid = grctx->src_ctx->gsc_uid;
2418                 req->rq_auth_mapped_uid = grctx->src_ctx->gsc_mapped_uid;
2419                 break;
2420         case SECSVC_COMPLETE:
2421                 break;
2422         case SECSVC_DROP:
2423                 gss_svc_reqctx_free(grctx);
2424                 req->rq_svc_ctx = NULL;
2425                 break;
2426         }
2427
2428         RETURN(rc);
2429 }
2430
2431 void gss_svc_invalidate_ctx(struct ptlrpc_svc_ctx *svc_ctx)
2432 {
2433         struct gss_svc_reqctx  *grctx;
2434         ENTRY;
2435
2436         if (svc_ctx == NULL) {
2437                 EXIT;
2438                 return;
2439         }
2440
2441         grctx = gss_svc_ctx2reqctx(svc_ctx);
2442
2443         CWARN("gss svc invalidate ctx %p(%u)\n", grctx->src_ctx,
2444               grctx->src_ctx->gsc_uid);
2445         gss_svc_upcall_destroy_ctx(grctx->src_ctx);
2446
2447         EXIT;
2448 }
2449
2450 static inline
2451 int gss_svc_payload(struct gss_svc_reqctx *grctx, int early,
2452                     int msgsize, int privacy)
2453 {
2454         /* we should treat early reply normally, but which is actually sharing
2455          * the same ctx with original request, so in this case we should
2456          * ignore the special ctx's special flags
2457          */
2458         if (early == 0 && gss_svc_reqctx_is_special(grctx))
2459                 return grctx->src_reserve_len;
2460
2461         return gss_mech_payload(NULL, msgsize, privacy);
2462 }
2463
2464 static int gss_svc_bulk_payload(struct gss_svc_ctx *gctx,
2465                                 struct sptlrpc_flavor *flvr, int read)
2466 {
2467         int     payload = sizeof(struct ptlrpc_bulk_sec_desc);
2468
2469         if (read) {
2470                 switch (SPTLRPC_FLVR_BULK_SVC(flvr->sf_rpc)) {
2471                 case SPTLRPC_BULK_SVC_NULL:
2472                         break;
2473                 case SPTLRPC_BULK_SVC_INTG:
2474                         payload += gss_mech_payload(NULL, 0, 0);
2475                         break;
2476                 case SPTLRPC_BULK_SVC_PRIV:
2477                         payload += gss_mech_payload(NULL, 0, 1);
2478                         break;
2479                 case SPTLRPC_BULK_SVC_AUTH:
2480                 default:
2481                         LBUG();
2482                 }
2483         }
2484
2485         return payload;
2486 }
2487
2488 int gss_svc_alloc_rs(struct ptlrpc_request *req, int msglen)
2489 {
2490         struct gss_svc_reqctx       *grctx;
2491         struct ptlrpc_reply_state   *rs;
2492         int                          early, privacy, svc, bsd_off = 0;
2493         __u32                        ibuflens[2], buflens[4];
2494         int                          ibufcnt = 0, bufcnt;
2495         int                          txtsize, wmsg_size, rs_size;
2496         ENTRY;
2497
2498         LASSERT(msglen % 8 == 0);
2499
2500         if (req->rq_pack_bulk && !req->rq_bulk_read && !req->rq_bulk_write) {
2501                 CERROR("client request bulk sec on non-bulk rpc\n");
2502                 RETURN(-EPROTO);
2503         }
2504
2505         svc = SPTLRPC_FLVR_SVC(req->rq_flvr.sf_rpc);
2506         early = (req->rq_packed_final == 0);
2507
2508         grctx = gss_svc_ctx2reqctx(req->rq_svc_ctx);
2509         if (!early && gss_svc_reqctx_is_special(grctx))
2510                 privacy = 0;
2511         else
2512                 privacy = (svc == SPTLRPC_SVC_PRIV);
2513
2514         if (privacy) {
2515                 /* inner clear buffers */
2516                 ibufcnt = 1;
2517                 ibuflens[0] = msglen;
2518
2519                 if (req->rq_pack_bulk) {
2520                         LASSERT(grctx->src_reqbsd);
2521
2522                         bsd_off = ibufcnt;
2523                         ibuflens[ibufcnt++] = gss_svc_bulk_payload(
2524                                                         grctx->src_ctx,
2525                                                         &req->rq_flvr,
2526                                                         req->rq_bulk_read);
2527                 }
2528
2529                 txtsize = lustre_msg_size_v2(ibufcnt, ibuflens);
2530                 txtsize += GSS_MAX_CIPHER_BLOCK;
2531
2532                 /* wrapper buffer */
2533                 bufcnt = 2;
2534                 buflens[0] = PTLRPC_GSS_HEADER_SIZE;
2535                 buflens[1] = gss_svc_payload(grctx, early, txtsize, 1);
2536         } else {
2537                 bufcnt = 2;
2538                 buflens[0] = PTLRPC_GSS_HEADER_SIZE;
2539                 buflens[1] = msglen;
2540
2541                 txtsize = buflens[0];
2542                 if (svc == SPTLRPC_SVC_INTG)
2543                         txtsize += buflens[1];
2544
2545                 if (req->rq_pack_bulk) {
2546                         LASSERT(grctx->src_reqbsd);
2547
2548                         bsd_off = bufcnt;
2549                         buflens[bufcnt] = gss_svc_bulk_payload(
2550                                                         grctx->src_ctx,
2551                                                         &req->rq_flvr,
2552                                                         req->rq_bulk_read);
2553                         if (svc == SPTLRPC_SVC_INTG)
2554                                 txtsize += buflens[bufcnt];
2555                         bufcnt++;
2556                 }
2557
2558                 if ((!early && gss_svc_reqctx_is_special(grctx)) ||
2559                      svc != SPTLRPC_SVC_NULL)
2560                         buflens[bufcnt++] = gss_svc_payload(grctx, early,
2561                                                             txtsize, 0);
2562         }
2563
2564         wmsg_size = lustre_msg_size_v2(bufcnt, buflens);
2565
2566         rs_size = sizeof(*rs) + wmsg_size;
2567         rs = req->rq_reply_state;
2568
2569         if (rs) {
2570                 /* pre-allocated */
2571                 LASSERT(rs->rs_size >= rs_size);
2572         } else {
2573                 OBD_ALLOC_LARGE(rs, rs_size);
2574                 if (rs == NULL)
2575                         RETURN(-ENOMEM);
2576
2577                 rs->rs_size = rs_size;
2578         }
2579
2580         rs->rs_repbuf = (struct lustre_msg *) (rs + 1);
2581         rs->rs_repbuf_len = wmsg_size;
2582
2583         /* initialize the buffer */
2584         if (privacy) {
2585                 lustre_init_msg_v2(rs->rs_repbuf, ibufcnt, ibuflens, NULL);
2586                 rs->rs_msg = lustre_msg_buf(rs->rs_repbuf, 0, msglen);
2587         } else {
2588                 lustre_init_msg_v2(rs->rs_repbuf, bufcnt, buflens, NULL);
2589                 rs->rs_repbuf->lm_secflvr = req->rq_flvr.sf_rpc;
2590
2591                 rs->rs_msg = lustre_msg_buf(rs->rs_repbuf, 1, 0);
2592         }
2593
2594         if (bsd_off) {
2595                 grctx->src_repbsd = lustre_msg_buf(rs->rs_repbuf, bsd_off, 0);
2596                 grctx->src_repbsd_size = lustre_msg_buflen(rs->rs_repbuf,
2597                                                            bsd_off);
2598         }
2599
2600         gss_svc_reqctx_addref(grctx);
2601         rs->rs_svc_ctx = req->rq_svc_ctx;
2602
2603         LASSERT(rs->rs_msg);
2604         req->rq_reply_state = rs;
2605         RETURN(0);
2606 }
2607
2608 static int gss_svc_seal(struct ptlrpc_request *req,
2609                         struct ptlrpc_reply_state *rs,
2610                         struct gss_svc_reqctx *grctx)
2611 {
2612         struct gss_svc_ctx      *gctx = grctx->src_ctx;
2613         rawobj_t                 hdrobj, msgobj, token;
2614         struct gss_header       *ghdr;
2615         __u8                    *token_buf;
2616         int                      token_buflen;
2617         __u32                    buflens[2], major;
2618         int                      msglen, rc;
2619         ENTRY;
2620
2621         /* get clear data length. note embedded lustre_msg might
2622          * have been shrunk
2623          */
2624         if (req->rq_replen != lustre_msg_buflen(rs->rs_repbuf, 0))
2625                 msglen = lustre_shrink_msg(rs->rs_repbuf, 0, req->rq_replen, 1);
2626         else
2627                 msglen = lustre_msg_size_v2(rs->rs_repbuf->lm_bufcount,
2628                                             rs->rs_repbuf->lm_buflens);
2629
2630         /* temporarily use tail of buffer to hold gss header data */
2631         LASSERT(msglen + PTLRPC_GSS_HEADER_SIZE <= rs->rs_repbuf_len);
2632         ghdr = (struct gss_header *) ((char *) rs->rs_repbuf +
2633                         rs->rs_repbuf_len - PTLRPC_GSS_HEADER_SIZE);
2634         ghdr->gh_version = PTLRPC_GSS_VERSION;
2635         ghdr->gh_sp = LUSTRE_SP_ANY;
2636         ghdr->gh_flags = 0;
2637         ghdr->gh_proc = PTLRPC_GSS_PROC_DATA;
2638         ghdr->gh_seq = grctx->src_wirectx.gw_seq;
2639         ghdr->gh_svc = SPTLRPC_SVC_PRIV;
2640         ghdr->gh_handle.len = 0;
2641         if (req->rq_pack_bulk)
2642                 ghdr->gh_flags |= LUSTRE_GSS_PACK_BULK;
2643
2644         /* allocate temporary cipher buffer */
2645         token_buflen = gss_mech_payload(gctx->gsc_mechctx, msglen, 1);
2646         OBD_ALLOC_LARGE(token_buf, token_buflen);
2647         if (token_buf == NULL)
2648                 RETURN(-ENOMEM);
2649
2650         hdrobj.len = PTLRPC_GSS_HEADER_SIZE;
2651         hdrobj.data = (__u8 *) ghdr;
2652         msgobj.len = msglen;
2653         msgobj.data = (__u8 *) rs->rs_repbuf;
2654         token.len = token_buflen;
2655         token.data = token_buf;
2656
2657         major = lgss_wrap(gctx->gsc_mechctx, &hdrobj, &msgobj,
2658                           rs->rs_repbuf_len - PTLRPC_GSS_HEADER_SIZE, &token);
2659         if (major != GSS_S_COMPLETE) {
2660                 CERROR("wrap message error: %08x\n", major);
2661                 GOTO(out_free, rc = -EPERM);
2662         }
2663         LASSERT(token.len <= token_buflen);
2664
2665         /* we are about to override data at rs->rs_repbuf, nullify pointers
2666          * to which to catch further illegal usage.
2667          */
2668         if (req->rq_pack_bulk) {
2669                 grctx->src_repbsd = NULL;
2670                 grctx->src_repbsd_size = 0;
2671         }
2672
2673         /* now fill the actual wire data
2674          * - gss header
2675          * - gss token
2676          */
2677         buflens[0] = PTLRPC_GSS_HEADER_SIZE;
2678         buflens[1] = token.len;
2679
2680         rs->rs_repdata_len = lustre_msg_size_v2(2, buflens);
2681         LASSERT(rs->rs_repdata_len <= rs->rs_repbuf_len);
2682
2683         lustre_init_msg_v2(rs->rs_repbuf, 2, buflens, NULL);
2684         rs->rs_repbuf->lm_secflvr = req->rq_flvr.sf_rpc;
2685
2686         memcpy(lustre_msg_buf(rs->rs_repbuf, 0, 0), ghdr,
2687                PTLRPC_GSS_HEADER_SIZE);
2688         memcpy(lustre_msg_buf(rs->rs_repbuf, 1, 0), token.data, token.len);
2689
2690         /* reply offset */
2691         if (req->rq_packed_final &&
2692             (lustre_msghdr_get_flags(req->rq_reqmsg) & MSGHDR_AT_SUPPORT))
2693                 req->rq_reply_off = gss_at_reply_off_priv;
2694         else
2695                 req->rq_reply_off = 0;
2696
2697         /* to catch upper layer's further access */
2698         rs->rs_msg = NULL;
2699         req->rq_repmsg = NULL;
2700         req->rq_replen = 0;
2701
2702         rc = 0;
2703 out_free:
2704         OBD_FREE_LARGE(token_buf, token_buflen);
2705         RETURN(rc);
2706 }
2707
2708 int gss_svc_authorize(struct ptlrpc_request *req)
2709 {
2710         struct ptlrpc_reply_state *rs = req->rq_reply_state;
2711         struct gss_svc_reqctx     *grctx = gss_svc_ctx2reqctx(req->rq_svc_ctx);
2712         struct gss_wire_ctx       *gw = &grctx->src_wirectx;
2713         int                        early, rc;
2714         ENTRY;
2715
2716         early = (req->rq_packed_final == 0);
2717
2718         if (!early && gss_svc_reqctx_is_special(grctx)) {
2719                 LASSERT(rs->rs_repdata_len != 0);
2720
2721                 req->rq_reply_off = gss_at_reply_off_integ;
2722                 RETURN(0);
2723         }
2724
2725         /* early reply could happen in many cases */
2726         if (!early && gw->gw_proc != PTLRPC_GSS_PROC_DATA &&
2727             gw->gw_proc != PTLRPC_GSS_PROC_DESTROY) {
2728                 CERROR("proc %d not support\n", gw->gw_proc);
2729                 RETURN(-EINVAL);
2730         }
2731
2732         LASSERT(grctx->src_ctx);
2733
2734         switch (gw->gw_svc) {
2735         case SPTLRPC_SVC_NULL:
2736         case SPTLRPC_SVC_AUTH:
2737         case SPTLRPC_SVC_INTG:
2738                 rc = gss_svc_sign(req, rs, grctx, gw->gw_svc);
2739                 break;
2740         case SPTLRPC_SVC_PRIV:
2741                 rc = gss_svc_seal(req, rs, grctx);
2742                 break;
2743         default:
2744                 CERROR("Unknown service %d\n", gw->gw_svc);
2745                 GOTO(out, rc = -EINVAL);
2746         }
2747         rc = 0;
2748
2749 out:
2750         RETURN(rc);
2751 }
2752
2753 void gss_svc_free_rs(struct ptlrpc_reply_state *rs)
2754 {
2755         struct gss_svc_reqctx *grctx;
2756
2757         LASSERT(rs->rs_svc_ctx);
2758         grctx = container_of(rs->rs_svc_ctx, struct gss_svc_reqctx, src_base);
2759
2760         gss_svc_reqctx_decref(grctx);
2761         rs->rs_svc_ctx = NULL;
2762
2763         if (!rs->rs_prealloc)
2764                 OBD_FREE_LARGE(rs, rs->rs_size);
2765 }
2766
2767 void gss_svc_free_ctx(struct ptlrpc_svc_ctx *ctx)
2768 {
2769         LASSERT(atomic_read(&ctx->sc_refcount) == 0);
2770         gss_svc_reqctx_free(gss_svc_ctx2reqctx(ctx));
2771 }
2772
2773 int gss_copy_rvc_cli_ctx(struct ptlrpc_cli_ctx *cli_ctx,
2774                          struct ptlrpc_svc_ctx *svc_ctx)
2775 {
2776         struct gss_cli_ctx     *cli_gctx = ctx2gctx(cli_ctx);
2777         struct gss_svc_ctx     *svc_gctx = gss_svc_ctx2gssctx(svc_ctx);
2778         struct gss_ctx         *mechctx = NULL;
2779
2780         LASSERT(cli_gctx);
2781         LASSERT(svc_gctx && svc_gctx->gsc_mechctx);
2782
2783         cli_gctx->gc_proc = PTLRPC_GSS_PROC_DATA;
2784         cli_gctx->gc_win = GSS_SEQ_WIN;
2785
2786         /* The problem is the reverse ctx might get lost in some recovery
2787          * situations, and the same svc_ctx will be used to re-create it.
2788          * if there's callback be sentout before that, new reverse ctx start
2789          * with sequence 0 will lead to future callback rpc be treated as
2790          * replay.
2791          *
2792          * each reverse root ctx will record its latest sequence number on its
2793          * buddy svcctx before be destroyed, so here we continue use it.
2794          */
2795         atomic_set(&cli_gctx->gc_seq, svc_gctx->gsc_rvs_seq);
2796
2797         if (gss_svc_upcall_dup_handle(&cli_gctx->gc_svc_handle, svc_gctx)) {
2798                 CERROR("failed to dup svc handle\n");
2799                 goto err_out;
2800         }
2801
2802         if (lgss_copy_reverse_context(svc_gctx->gsc_mechctx, &mechctx) !=
2803             GSS_S_COMPLETE) {
2804                 CERROR("failed to copy mech context\n");
2805                 goto err_svc_handle;
2806         }
2807
2808         if (rawobj_dup(&cli_gctx->gc_handle, &svc_gctx->gsc_rvs_hdl)) {
2809                 CERROR("failed to dup reverse handle\n");
2810                 goto err_ctx;
2811         }
2812
2813         cli_gctx->gc_mechctx = mechctx;
2814         gss_cli_ctx_uptodate(cli_gctx);
2815
2816         return 0;
2817
2818 err_ctx:
2819         lgss_delete_sec_context(&mechctx);
2820 err_svc_handle:
2821         rawobj_free(&cli_gctx->gc_svc_handle);
2822 err_out:
2823         return -ENOMEM;
2824 }
2825
2826 static void gss_init_at_reply_offset(void)
2827 {
2828         __u32 buflens[3];
2829         int clearsize;
2830
2831         buflens[0] = PTLRPC_GSS_HEADER_SIZE;
2832         buflens[1] = lustre_msg_early_size;
2833         buflens[2] = gss_cli_payload(NULL, buflens[1], 0);
2834         gss_at_reply_off_integ = lustre_msg_size_v2(3, buflens);
2835
2836         buflens[0] = lustre_msg_early_size;
2837         clearsize = lustre_msg_size_v2(1, buflens);
2838         buflens[0] = PTLRPC_GSS_HEADER_SIZE;
2839         buflens[1] = gss_cli_payload(NULL, clearsize, 0);
2840         buflens[2] = gss_cli_payload(NULL, clearsize, 1);
2841         gss_at_reply_off_priv = lustre_msg_size_v2(3, buflens);
2842 }
2843
2844 static int __init sptlrpc_gss_init(void)
2845 {
2846         int rc;
2847
2848         rc = gss_init_tunables();
2849         if (rc)
2850                 return rc;
2851
2852         rc = gss_init_cli_upcall();
2853         if (rc)
2854                 goto out_tunables;
2855
2856         rc = gss_init_svc_upcall();
2857         if (rc)
2858                 goto out_cli_upcall;
2859
2860         rc = init_null_module();
2861         if (rc)
2862                 goto out_svc_upcall;
2863
2864         rc = init_kerberos_module();
2865         if (rc)
2866                 goto out_null;
2867
2868         rc = init_sk_module();
2869         if (rc)
2870                 goto out_kerberos;
2871
2872         /* register policy after all other stuff be initialized, because it
2873          * might be in used immediately after the registration.
2874          */
2875         rc = gss_init_keyring();
2876         if (rc)
2877                 goto out_sk;
2878
2879         gss_init_at_reply_offset();
2880
2881         return 0;
2882
2883 out_sk:
2884         cleanup_sk_module();
2885 out_kerberos:
2886         cleanup_kerberos_module();
2887 out_null:
2888         cleanup_null_module();
2889 out_svc_upcall:
2890         gss_exit_svc_upcall();
2891 out_cli_upcall:
2892         gss_exit_cli_upcall();
2893 out_tunables:
2894         gss_exit_tunables();
2895         return rc;
2896 }
2897
2898 static void __exit sptlrpc_gss_exit(void)
2899 {
2900         gss_exit_keyring();
2901         cleanup_kerberos_module();
2902         gss_exit_svc_upcall();
2903         gss_exit_cli_upcall();
2904         gss_exit_tunables();
2905 }
2906
2907 MODULE_AUTHOR("OpenSFS, Inc. <http://www.lustre.org/>");
2908 MODULE_DESCRIPTION("Lustre GSS security policy");
2909 MODULE_VERSION(LUSTRE_VERSION_STRING);
2910 MODULE_LICENSE("GPL");
2911
2912 module_init(sptlrpc_gss_init);
2913 module_exit(sptlrpc_gss_exit);