Whamcloud - gitweb
41c6fe5fbacbf3b6bb9893550a77613a6edfbd4b
[fs/lustre-release.git] / lustre / ptlrpc / gss / gss_bulk.c
1 /*
2  * GPL HEADER START
3  *
4  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License version 2 only,
8  * as published by the Free Software Foundation.
9  *
10  * This program is distributed in the hope that it will be useful, but
11  * WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  * General Public License version 2 for more details (a copy is included
14  * in the LICENSE file that accompanied this code).
15  *
16  * You should have received a copy of the GNU General Public License
17  * version 2 along with this program; If not, see
18  * http://www.sun.com/software/products/lustre/docs/GPLv2.pdf
19  *
20  * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
21  * CA 95054 USA or visit www.sun.com if you need additional information or
22  * have any questions.
23  *
24  * GPL HEADER END
25  */
26 /*
27  * Copyright (c) 2007, 2010, Oracle and/or its affiliates. All rights reserved.
28  * Use is subject to license terms.
29  *
30  * Copyright (c) 2014, Intel Corporation.
31  */
32 /*
33  * This file is part of Lustre, http://www.lustre.org/
34  * Lustre is a trademark of Sun Microsystems, Inc.
35  *
36  * lustre/ptlrpc/gss/gss_bulk.c
37  *
38  * Author: Eric Mei <eric.mei@sun.com>
39  */
40
41 #define DEBUG_SUBSYSTEM S_SEC
42 #include <linux/init.h>
43 #include <linux/module.h>
44 #include <linux/slab.h>
45 #include <linux/dcache.h>
46 #include <linux/fs.h>
47 #include <linux/mutex.h>
48 #include <linux/crypto.h>
49
50 #include <obd.h>
51 #include <obd_class.h>
52 #include <obd_support.h>
53 #include <lustre/lustre_idl.h>
54 #include <lustre_net.h>
55 #include <lustre_import.h>
56 #include <lustre_sec.h>
57
58 #include "gss_err.h"
59 #include "gss_internal.h"
60 #include "gss_api.h"
61
62 int gss_cli_ctx_wrap_bulk(struct ptlrpc_cli_ctx *ctx,
63                           struct ptlrpc_request *req,
64                           struct ptlrpc_bulk_desc *desc)
65 {
66         struct gss_cli_ctx              *gctx;
67         struct lustre_msg               *msg;
68         struct ptlrpc_bulk_sec_desc     *bsd;
69         rawobj_t                         token;
70         __u32                            maj;
71         int                              offset;
72         int                              rc;
73         ENTRY;
74
75         LASSERT(req->rq_pack_bulk);
76         LASSERT(req->rq_bulk_read || req->rq_bulk_write);
77
78         gctx = container_of(ctx, struct gss_cli_ctx, gc_base);
79         LASSERT(gctx->gc_mechctx);
80
81         switch (SPTLRPC_FLVR_SVC(req->rq_flvr.sf_rpc)) {
82         case SPTLRPC_SVC_NULL:
83                 LASSERT(req->rq_reqbuf->lm_bufcount >= 3);
84                 msg = req->rq_reqbuf;
85                 offset = msg->lm_bufcount - 1;
86                 break;
87         case SPTLRPC_SVC_AUTH:
88         case SPTLRPC_SVC_INTG:
89                 LASSERT(req->rq_reqbuf->lm_bufcount >= 4);
90                 msg = req->rq_reqbuf;
91                 offset = msg->lm_bufcount - 2;
92                 break;
93         case SPTLRPC_SVC_PRIV:
94                 LASSERT(req->rq_clrbuf->lm_bufcount >= 2);
95                 msg = req->rq_clrbuf;
96                 offset = msg->lm_bufcount - 1;
97                 break;
98         default:
99                 LBUG();
100         }
101
102         bsd = lustre_msg_buf(msg, offset, sizeof(*bsd));
103         bsd->bsd_version = 0;
104         bsd->bsd_flags = 0;
105         bsd->bsd_type = SPTLRPC_BULK_DEFAULT;
106         bsd->bsd_svc = SPTLRPC_FLVR_BULK_SVC(req->rq_flvr.sf_rpc);
107
108         if (bsd->bsd_svc == SPTLRPC_BULK_SVC_NULL)
109                 RETURN(0);
110
111         LASSERT(bsd->bsd_svc == SPTLRPC_BULK_SVC_INTG ||
112                 bsd->bsd_svc == SPTLRPC_BULK_SVC_PRIV);
113
114         if (req->rq_bulk_read) {
115                 /*
116                  * bulk read: prepare receiving pages only for privacy mode.
117                  */
118                 if (bsd->bsd_svc == SPTLRPC_BULK_SVC_PRIV)
119                         return gss_cli_prep_bulk(req, desc);
120         } else {
121                 /*
122                  * bulk write: sign or encrypt bulk pages.
123                  */
124                 bsd->bsd_nob = desc->bd_nob;
125
126                 if (bsd->bsd_svc == SPTLRPC_BULK_SVC_INTG) {
127                         /* integrity mode */
128                         token.data = bsd->bsd_data;
129                         token.len = lustre_msg_buflen(msg, offset) -
130                                     sizeof(*bsd);
131
132                         maj = lgss_get_mic(gctx->gc_mechctx, 0, NULL,
133                                            desc->bd_iov_count, desc->bd_iov,
134                                            &token);
135                         if (maj != GSS_S_COMPLETE) {
136                                 CWARN("failed to sign bulk data: %x\n", maj);
137                                 RETURN(-EACCES);
138                         }
139                 } else {
140                         /* privacy mode */
141                         if (desc->bd_iov_count == 0)
142                                 RETURN(0);
143
144                         rc = sptlrpc_enc_pool_get_pages(desc);
145                         if (rc) {
146                                 CERROR("bulk write: failed to allocate "
147                                        "encryption pages: %d\n", rc);
148                                 RETURN(rc);
149                         }
150
151                         token.data = bsd->bsd_data;
152                         token.len = lustre_msg_buflen(msg, offset) -
153                                     sizeof(*bsd);
154
155                         maj = lgss_wrap_bulk(gctx->gc_mechctx, desc, &token, 0);
156                         if (maj != GSS_S_COMPLETE) {
157                                 CWARN("fail to encrypt bulk data: %x\n", maj);
158                                 RETURN(-EACCES);
159                         }
160                 }
161         }
162
163         RETURN(0);
164 }
165
166 int gss_cli_ctx_unwrap_bulk(struct ptlrpc_cli_ctx *ctx,
167                             struct ptlrpc_request *req,
168                             struct ptlrpc_bulk_desc *desc)
169 {
170         struct gss_cli_ctx              *gctx;
171         struct lustre_msg               *rmsg, *vmsg;
172         struct ptlrpc_bulk_sec_desc     *bsdr, *bsdv;
173         rawobj_t                         token;
174         __u32                            maj;
175         int                              roff, voff;
176         ENTRY;
177
178         LASSERT(req->rq_pack_bulk);
179         LASSERT(req->rq_bulk_read || req->rq_bulk_write);
180
181         switch (SPTLRPC_FLVR_SVC(req->rq_flvr.sf_rpc)) {
182         case SPTLRPC_SVC_NULL:
183                 vmsg = req->rq_repdata;
184                 LASSERT(vmsg != NULL && vmsg->lm_bufcount >= 3);
185                 voff = vmsg->lm_bufcount - 1;
186
187                 rmsg = req->rq_reqbuf;
188                 LASSERT(rmsg != NULL && rmsg->lm_bufcount >= 3);
189                 roff = rmsg->lm_bufcount - 1; /* last segment */
190                 break;
191         case SPTLRPC_SVC_AUTH:
192         case SPTLRPC_SVC_INTG:
193                 vmsg = req->rq_repdata;
194                 LASSERT(vmsg != NULL && vmsg->lm_bufcount >= 4);
195                 voff = vmsg->lm_bufcount - 2;
196
197                 rmsg = req->rq_reqbuf;
198                 LASSERT(rmsg != NULL && rmsg->lm_bufcount >= 4);
199                 roff = rmsg->lm_bufcount - 2; /* second last segment */
200                 break;
201         case SPTLRPC_SVC_PRIV:
202                 vmsg = req->rq_repdata;
203                 LASSERT(vmsg != NULL && vmsg->lm_bufcount >= 2);
204                 voff = vmsg->lm_bufcount - 1;
205
206                 rmsg = req->rq_clrbuf;
207                 LASSERT(rmsg != NULL && rmsg->lm_bufcount >= 2);
208                 roff = rmsg->lm_bufcount - 1; /* last segment */
209                 break;
210         default:
211                 LBUG();
212         }
213
214         bsdr = lustre_msg_buf(rmsg, roff, sizeof(*bsdr));
215         bsdv = lustre_msg_buf(vmsg, voff, sizeof(*bsdv));
216         LASSERT(bsdr && bsdv);
217
218         if (bsdr->bsd_version != bsdv->bsd_version ||
219             bsdr->bsd_type != bsdv->bsd_type ||
220             bsdr->bsd_svc != bsdv->bsd_svc) {
221                 CERROR("bulk security descriptor mismatch: "
222                        "(%u,%u,%u) != (%u,%u,%u)\n",
223                        bsdr->bsd_version, bsdr->bsd_type, bsdr->bsd_svc,
224                        bsdv->bsd_version, bsdv->bsd_type, bsdv->bsd_svc);
225                 RETURN(-EPROTO);
226         }
227
228         LASSERT(bsdv->bsd_svc == SPTLRPC_BULK_SVC_NULL ||
229                 bsdv->bsd_svc == SPTLRPC_BULK_SVC_INTG ||
230                 bsdv->bsd_svc == SPTLRPC_BULK_SVC_PRIV);
231
232         /*
233          * in privacy mode if return success, make sure bd_nob_transferred
234          * is the actual size of the clear text, otherwise upper layer
235          * may be surprised.
236          */
237         if (req->rq_bulk_write) {
238                 if (bsdv->bsd_flags & BSD_FL_ERR) {
239                         CERROR("server reported bulk i/o failure\n");
240                         RETURN(-EIO);
241                 }
242
243                 if (bsdv->bsd_svc == SPTLRPC_BULK_SVC_PRIV)
244                         desc->bd_nob_transferred = desc->bd_nob;
245         } else {
246                 /*
247                  * bulk read, upon return success, bd_nob_transferred is
248                  * the size of plain text actually received.
249                  */
250                 gctx = container_of(ctx, struct gss_cli_ctx, gc_base);
251                 LASSERT(gctx->gc_mechctx);
252
253                 if (bsdv->bsd_svc == SPTLRPC_BULK_SVC_INTG) {
254                         int i, nob;
255
256                         /* fix the actual data size */
257                         for (i = 0, nob = 0; i < desc->bd_iov_count; i++) {
258                                 if (desc->bd_iov[i].kiov_len + nob >
259                                     desc->bd_nob_transferred) {
260                                         desc->bd_iov[i].kiov_len =
261                                                 desc->bd_nob_transferred - nob;
262                                 }
263                                 nob += desc->bd_iov[i].kiov_len;
264                         }
265
266                         token.data = bsdv->bsd_data;
267                         token.len = lustre_msg_buflen(vmsg, voff) -
268                                     sizeof(*bsdv);
269
270                         maj = lgss_verify_mic(gctx->gc_mechctx, 0, NULL,
271                                               desc->bd_iov_count, desc->bd_iov,
272                                               &token);
273                         if (maj != GSS_S_COMPLETE) {
274                                 CERROR("failed to verify bulk read: %x\n", maj);
275                                 RETURN(-EACCES);
276                         }
277                 } else if (bsdv->bsd_svc == SPTLRPC_BULK_SVC_PRIV) {
278                         desc->bd_nob = bsdv->bsd_nob;
279                         if (desc->bd_nob == 0)
280                                 RETURN(0);
281
282                         token.data = bsdv->bsd_data;
283                         token.len = lustre_msg_buflen(vmsg, voff) -
284                                     sizeof(*bsdr);
285
286                         maj = lgss_unwrap_bulk(gctx->gc_mechctx, desc,
287                                                &token, 1);
288                         if (maj != GSS_S_COMPLETE) {
289                                 CERROR("failed to decrypt bulk read: %x\n",
290                                        maj);
291                                 RETURN(-EACCES);
292                         }
293
294                         desc->bd_nob_transferred = desc->bd_nob;
295                 }
296         }
297
298         RETURN(0);
299 }
300
301 static int gss_prep_bulk(struct ptlrpc_bulk_desc *desc,
302                          struct gss_ctx *mechctx)
303 {
304         int     rc;
305
306         if (desc->bd_iov_count == 0)
307                 return 0;
308
309         rc = sptlrpc_enc_pool_get_pages(desc);
310         if (rc)
311                 return rc;
312
313         if (lgss_prep_bulk(mechctx, desc) != GSS_S_COMPLETE)
314                 return -EACCES;
315
316         return 0;
317 }
318
319 int gss_cli_prep_bulk(struct ptlrpc_request *req,
320                       struct ptlrpc_bulk_desc *desc)
321 {
322         int             rc;
323         ENTRY;
324
325         LASSERT(req->rq_cli_ctx);
326         LASSERT(req->rq_pack_bulk);
327         LASSERT(req->rq_bulk_read);
328
329         if (SPTLRPC_FLVR_BULK_SVC(req->rq_flvr.sf_rpc) != SPTLRPC_BULK_SVC_PRIV)
330                 RETURN(0);
331
332         rc = gss_prep_bulk(desc, ctx2gctx(req->rq_cli_ctx)->gc_mechctx);
333         if (rc)
334                 CERROR("bulk read: failed to prepare encryption "
335                        "pages: %d\n", rc);
336
337         RETURN(rc);
338 }
339
340 int gss_svc_prep_bulk(struct ptlrpc_request *req,
341                       struct ptlrpc_bulk_desc *desc)
342 {
343         struct gss_svc_reqctx        *grctx;
344         struct ptlrpc_bulk_sec_desc  *bsd;
345         int                           rc;
346         ENTRY;
347
348         LASSERT(req->rq_svc_ctx);
349         LASSERT(req->rq_pack_bulk);
350         LASSERT(req->rq_bulk_write);
351
352         grctx = gss_svc_ctx2reqctx(req->rq_svc_ctx);
353         LASSERT(grctx->src_reqbsd);
354         LASSERT(grctx->src_repbsd);
355         LASSERT(grctx->src_ctx);
356         LASSERT(grctx->src_ctx->gsc_mechctx);
357
358         bsd = grctx->src_reqbsd;
359         if (bsd->bsd_svc != SPTLRPC_BULK_SVC_PRIV)
360                 RETURN(0);
361
362         rc = gss_prep_bulk(desc, grctx->src_ctx->gsc_mechctx);
363         if (rc)
364                 CERROR("bulk write: failed to prepare encryption "
365                        "pages: %d\n", rc);
366
367         RETURN(rc);
368 }
369
370 int gss_svc_unwrap_bulk(struct ptlrpc_request *req,
371                         struct ptlrpc_bulk_desc *desc)
372 {
373         struct gss_svc_reqctx        *grctx;
374         struct ptlrpc_bulk_sec_desc  *bsdr, *bsdv;
375         rawobj_t                      token;
376         __u32                         maj;
377         ENTRY;
378
379         LASSERT(req->rq_svc_ctx);
380         LASSERT(req->rq_pack_bulk);
381         LASSERT(req->rq_bulk_write);
382
383         grctx = gss_svc_ctx2reqctx(req->rq_svc_ctx);
384
385         LASSERT(grctx->src_reqbsd);
386         LASSERT(grctx->src_repbsd);
387         LASSERT(grctx->src_ctx);
388         LASSERT(grctx->src_ctx->gsc_mechctx);
389
390         bsdr = grctx->src_reqbsd;
391         bsdv = grctx->src_repbsd;
392
393         /* bsdr has been sanity checked during unpacking */
394         bsdv->bsd_version = 0;
395         bsdv->bsd_type = SPTLRPC_BULK_DEFAULT;
396         bsdv->bsd_svc = bsdr->bsd_svc;
397         bsdv->bsd_flags = 0;
398
399         switch (bsdv->bsd_svc) {
400         case SPTLRPC_BULK_SVC_INTG:
401                 token.data = bsdr->bsd_data;
402                 token.len = grctx->src_reqbsd_size - sizeof(*bsdr);
403
404                 maj = lgss_verify_mic(grctx->src_ctx->gsc_mechctx, 0, NULL,
405                                       desc->bd_iov_count, desc->bd_iov, &token);
406                 if (maj != GSS_S_COMPLETE) {
407                         bsdv->bsd_flags |= BSD_FL_ERR;
408                         CERROR("failed to verify bulk signature: %x\n", maj);
409                         RETURN(-EACCES);
410                 }
411                 break;
412         case SPTLRPC_BULK_SVC_PRIV:
413                 if (bsdr->bsd_nob != desc->bd_nob) {
414                         bsdv->bsd_flags |= BSD_FL_ERR;
415                         CERROR("prepared nob %d doesn't match the actual "
416                                "nob %d\n", desc->bd_nob, bsdr->bsd_nob);
417                         RETURN(-EPROTO);
418                 }
419
420                 if (desc->bd_iov_count == 0) {
421                         LASSERT(desc->bd_nob == 0);
422                         break;
423                 }
424
425                 token.data = bsdr->bsd_data;
426                 token.len = grctx->src_reqbsd_size - sizeof(*bsdr);
427
428                 maj = lgss_unwrap_bulk(grctx->src_ctx->gsc_mechctx,
429                                        desc, &token, 0);
430                 if (maj != GSS_S_COMPLETE) {
431                         bsdv->bsd_flags |= BSD_FL_ERR;
432                         CERROR("failed decrypt bulk data: %x\n", maj);
433                         RETURN(-EACCES);
434                 }
435                 break;
436         }
437
438         RETURN(0);
439 }
440
441 int gss_svc_wrap_bulk(struct ptlrpc_request *req,
442                       struct ptlrpc_bulk_desc *desc)
443 {
444         struct gss_svc_reqctx        *grctx;
445         struct ptlrpc_bulk_sec_desc  *bsdr, *bsdv;
446         rawobj_t                      token;
447         __u32                         maj;
448         int                           rc;
449         ENTRY;
450
451         LASSERT(req->rq_svc_ctx);
452         LASSERT(req->rq_pack_bulk);
453         LASSERT(req->rq_bulk_read);
454
455         grctx = gss_svc_ctx2reqctx(req->rq_svc_ctx);
456
457         LASSERT(grctx->src_reqbsd);
458         LASSERT(grctx->src_repbsd);
459         LASSERT(grctx->src_ctx);
460         LASSERT(grctx->src_ctx->gsc_mechctx);
461
462         bsdr = grctx->src_reqbsd;
463         bsdv = grctx->src_repbsd;
464
465         /* bsdr has been sanity checked during unpacking */
466         bsdv->bsd_version = 0;
467         bsdv->bsd_type = SPTLRPC_BULK_DEFAULT;
468         bsdv->bsd_svc = bsdr->bsd_svc;
469         bsdv->bsd_flags = 0;
470
471         switch (bsdv->bsd_svc) {
472         case SPTLRPC_BULK_SVC_INTG:
473                 token.data = bsdv->bsd_data;
474                 token.len = grctx->src_repbsd_size - sizeof(*bsdv);
475
476                 maj = lgss_get_mic(grctx->src_ctx->gsc_mechctx, 0, NULL,
477                                    desc->bd_iov_count, desc->bd_iov, &token);
478                 if (maj != GSS_S_COMPLETE) {
479                         bsdv->bsd_flags |= BSD_FL_ERR;
480                         CERROR("failed to sign bulk data: %x\n", maj);
481                         RETURN(-EACCES);
482                 }
483                 break;
484         case SPTLRPC_BULK_SVC_PRIV:
485                 bsdv->bsd_nob = desc->bd_nob;
486
487                 if (desc->bd_iov_count == 0) {
488                         LASSERT(desc->bd_nob == 0);
489                         break;
490                 }
491
492                 rc = sptlrpc_enc_pool_get_pages(desc);
493                 if (rc) {
494                         bsdv->bsd_flags |= BSD_FL_ERR;
495                         CERROR("bulk read: failed to allocate encryption "
496                                "pages: %d\n", rc);
497                         RETURN(rc);
498                 }
499
500                 token.data = bsdv->bsd_data;
501                 token.len = grctx->src_repbsd_size - sizeof(*bsdv);
502
503                 maj = lgss_wrap_bulk(grctx->src_ctx->gsc_mechctx,
504                                      desc, &token, 1);
505                 if (maj != GSS_S_COMPLETE) {
506                         bsdv->bsd_flags |= BSD_FL_ERR;
507                         CERROR("failed to encrypt bulk data: %x\n", maj);
508                         RETURN(-EACCES);
509                 }
510                 break;
511         }
512
513         RETURN(0);
514 }