Whamcloud - gitweb
LU-17400 uapi: Fix incorrect snamelen return value
[fs/lustre-release.git] / lustre / ptlrpc / sec.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.gnu.org/licenses/gpl-2.0.html
19  *
20  * GPL HEADER END
21  */
22 /*
23  * Copyright (c) 2007, 2010, Oracle and/or its affiliates. All rights reserved.
24  * Use is subject to license terms.
25  *
26  * Copyright (c) 2011, 2017, Intel Corporation.
27  */
28 /*
29  * This file is part of Lustre, http://www.lustre.org/
30  *
31  * lustre/ptlrpc/sec.c
32  *
33  * Author: Eric Mei <ericm@clusterfs.com>
34  */
35
36 #define DEBUG_SUBSYSTEM S_SEC
37
38 #include <linux/user_namespace.h>
39 #include <linux/uidgid.h>
40 #include <linux/crypto.h>
41 #include <linux/key.h>
42
43 #include <libcfs/libcfs.h>
44 #include <obd.h>
45 #include <obd_class.h>
46 #include <obd_support.h>
47 #include <lustre_net.h>
48 #include <lustre_import.h>
49 #include <lustre_dlm.h>
50 #include <lustre_sec.h>
51
52 #include "ptlrpc_internal.h"
53
54 static int send_sepol;
55 module_param(send_sepol, int, 0644);
56 MODULE_PARM_DESC(send_sepol, "Client sends SELinux policy status");
57
58 /*
59  * policy registers
60  */
61
62 static rwlock_t policy_lock;
63 static struct ptlrpc_sec_policy *policies[SPTLRPC_POLICY_MAX] = {
64         NULL,
65 };
66
67 int sptlrpc_register_policy(struct ptlrpc_sec_policy *policy)
68 {
69         __u16 number = policy->sp_policy;
70
71         LASSERT(policy->sp_name);
72         LASSERT(policy->sp_cops);
73         LASSERT(policy->sp_sops);
74
75         if (number >= SPTLRPC_POLICY_MAX)
76                 return -EINVAL;
77
78         write_lock(&policy_lock);
79         if (unlikely(policies[number])) {
80                 write_unlock(&policy_lock);
81                 return -EALREADY;
82         }
83         policies[number] = policy;
84         write_unlock(&policy_lock);
85
86         CDEBUG(D_SEC, "%s: registered\n", policy->sp_name);
87         return 0;
88 }
89 EXPORT_SYMBOL(sptlrpc_register_policy);
90
91 int sptlrpc_unregister_policy(struct ptlrpc_sec_policy *policy)
92 {
93         __u16 number = policy->sp_policy;
94
95         LASSERT(number < SPTLRPC_POLICY_MAX);
96
97         write_lock(&policy_lock);
98         if (unlikely(policies[number] == NULL)) {
99                 write_unlock(&policy_lock);
100                 CERROR("%s: already unregistered\n", policy->sp_name);
101                 return -EINVAL;
102         }
103
104         LASSERT(policies[number] == policy);
105         policies[number] = NULL;
106         write_unlock(&policy_lock);
107
108         CDEBUG(D_SEC, "%s: unregistered\n", policy->sp_name);
109         return 0;
110 }
111 EXPORT_SYMBOL(sptlrpc_unregister_policy);
112
113 static
114 struct ptlrpc_sec_policy *sptlrpc_wireflavor2policy(__u32 flavor)
115 {
116         static DEFINE_MUTEX(load_mutex);
117         struct ptlrpc_sec_policy *policy;
118         __u16 number = SPTLRPC_FLVR_POLICY(flavor);
119         int rc;
120
121         if (number >= SPTLRPC_POLICY_MAX)
122                 return NULL;
123
124         while (1) {
125                 read_lock(&policy_lock);
126                 policy = policies[number];
127                 if (policy && !try_module_get(policy->sp_owner))
128                         policy = NULL;
129                 read_unlock(&policy_lock);
130
131                 if (policy != NULL || number != SPTLRPC_POLICY_GSS)
132                         break;
133
134                 /* try to load gss module, happens only if policy at index
135                  * SPTLRPC_POLICY_GSS is not already referenced in
136                  * global array policies[]
137                  */
138                 mutex_lock(&load_mutex);
139                 /* The fact that request_module() returns 0 does not guarantee
140                  * the module has done its job. So we must check that the
141                  * requested policy is now available. This is done by checking
142                  * again for policies[number] in the loop.
143                  */
144                 rc = request_module("ptlrpc_gss");
145                 if (rc == 0)
146                         CDEBUG(D_SEC, "module ptlrpc_gss loaded on demand\n");
147                 else
148                         CERROR("Unable to load module ptlrpc_gss: rc %d\n", rc);
149                 mutex_unlock(&load_mutex);
150         }
151
152         return policy;
153 }
154
155 __u32 sptlrpc_name2flavor_base(const char *name)
156 {
157         if (!strcmp(name, "null"))
158                 return SPTLRPC_FLVR_NULL;
159         if (!strcmp(name, "plain"))
160                 return SPTLRPC_FLVR_PLAIN;
161         if (!strcmp(name, "gssnull"))
162                 return SPTLRPC_FLVR_GSSNULL;
163         if (!strcmp(name, "krb5n"))
164                 return SPTLRPC_FLVR_KRB5N;
165         if (!strcmp(name, "krb5a"))
166                 return SPTLRPC_FLVR_KRB5A;
167         if (!strcmp(name, "krb5i"))
168                 return SPTLRPC_FLVR_KRB5I;
169         if (!strcmp(name, "krb5p"))
170                 return SPTLRPC_FLVR_KRB5P;
171         if (!strcmp(name, "skn"))
172                 return SPTLRPC_FLVR_SKN;
173         if (!strcmp(name, "ska"))
174                 return SPTLRPC_FLVR_SKA;
175         if (!strcmp(name, "ski"))
176                 return SPTLRPC_FLVR_SKI;
177         if (!strcmp(name, "skpi"))
178                 return SPTLRPC_FLVR_SKPI;
179
180         return SPTLRPC_FLVR_INVALID;
181 }
182 EXPORT_SYMBOL(sptlrpc_name2flavor_base);
183
184 const char *sptlrpc_flavor2name_base(__u32 flvr)
185 {
186         __u32   base = SPTLRPC_FLVR_BASE(flvr);
187
188         if (base == SPTLRPC_FLVR_BASE(SPTLRPC_FLVR_NULL))
189                 return "null";
190         else if (base == SPTLRPC_FLVR_BASE(SPTLRPC_FLVR_PLAIN))
191                 return "plain";
192         else if (base == SPTLRPC_FLVR_BASE(SPTLRPC_FLVR_GSSNULL))
193                 return "gssnull";
194         else if (base == SPTLRPC_FLVR_BASE(SPTLRPC_FLVR_KRB5N))
195                 return "krb5n";
196         else if (base == SPTLRPC_FLVR_BASE(SPTLRPC_FLVR_KRB5A))
197                 return "krb5a";
198         else if (base == SPTLRPC_FLVR_BASE(SPTLRPC_FLVR_KRB5I))
199                 return "krb5i";
200         else if (base == SPTLRPC_FLVR_BASE(SPTLRPC_FLVR_KRB5P))
201                 return "krb5p";
202         else if (base == SPTLRPC_FLVR_BASE(SPTLRPC_FLVR_SKN))
203                 return "skn";
204         else if (base == SPTLRPC_FLVR_BASE(SPTLRPC_FLVR_SKA))
205                 return "ska";
206         else if (base == SPTLRPC_FLVR_BASE(SPTLRPC_FLVR_SKI))
207                 return "ski";
208         else if (base == SPTLRPC_FLVR_BASE(SPTLRPC_FLVR_SKPI))
209                 return "skpi";
210
211         CERROR("invalid wire flavor 0x%x\n", flvr);
212         return "invalid";
213 }
214 EXPORT_SYMBOL(sptlrpc_flavor2name_base);
215
216 char *sptlrpc_flavor2name_bulk(struct sptlrpc_flavor *sf,
217                                char *buf, int bufsize)
218 {
219         if (SPTLRPC_FLVR_POLICY(sf->sf_rpc) == SPTLRPC_POLICY_PLAIN)
220                 snprintf(buf, bufsize, "hash:%s",
221                         sptlrpc_get_hash_name(sf->u_bulk.hash.hash_alg));
222         else
223                 snprintf(buf, bufsize, "%s",
224                         sptlrpc_flavor2name_base(sf->sf_rpc));
225
226         buf[bufsize - 1] = '\0';
227         return buf;
228 }
229 EXPORT_SYMBOL(sptlrpc_flavor2name_bulk);
230
231 char *sptlrpc_flavor2name(struct sptlrpc_flavor *sf, char *buf, int bufsize)
232 {
233         size_t ln;
234
235         ln = snprintf(buf, bufsize, "%s", sptlrpc_flavor2name_base(sf->sf_rpc));
236
237         /*
238          * currently we don't support customized bulk specification for
239          * flavors other than plain
240          */
241         if (SPTLRPC_FLVR_POLICY(sf->sf_rpc) == SPTLRPC_POLICY_PLAIN) {
242                 char bspec[16];
243
244                 bspec[0] = '-';
245                 sptlrpc_flavor2name_bulk(sf, bspec + 1, sizeof(bspec) - 1);
246                 strncat(buf, bspec, bufsize - ln);
247         }
248
249         buf[bufsize - 1] = '\0';
250         return buf;
251 }
252 EXPORT_SYMBOL(sptlrpc_flavor2name);
253
254 char *sptlrpc_secflags2str(__u32 flags, char *buf, int bufsize)
255 {
256         buf[0] = '\0';
257
258         if (flags & PTLRPC_SEC_FL_REVERSE)
259                 strlcat(buf, "reverse,", bufsize);
260         if (flags & PTLRPC_SEC_FL_ROOTONLY)
261                 strlcat(buf, "rootonly,", bufsize);
262         if (flags & PTLRPC_SEC_FL_UDESC)
263                 strlcat(buf, "udesc,", bufsize);
264         if (flags & PTLRPC_SEC_FL_BULK)
265                 strlcat(buf, "bulk,", bufsize);
266         if (buf[0] == '\0')
267                 strlcat(buf, "-,", bufsize);
268
269         return buf;
270 }
271 EXPORT_SYMBOL(sptlrpc_secflags2str);
272
273 /*
274  * client context APIs
275  */
276
277 static
278 struct ptlrpc_cli_ctx *get_my_ctx(struct ptlrpc_sec *sec)
279 {
280         struct vfs_cred vcred;
281         int create = 1, remove_dead = 1;
282
283         LASSERT(sec);
284         LASSERT(sec->ps_policy->sp_cops->lookup_ctx);
285
286         if (sec->ps_flvr.sf_flags & (PTLRPC_SEC_FL_REVERSE |
287                                      PTLRPC_SEC_FL_ROOTONLY)) {
288                 vcred.vc_uid = 0;
289                 vcred.vc_gid = 0;
290                 if (sec->ps_flvr.sf_flags & PTLRPC_SEC_FL_REVERSE) {
291                         create = 0;
292                         remove_dead = 0;
293                 }
294         } else {
295                 vcred.vc_uid = from_kuid(&init_user_ns, current_uid());
296                 vcred.vc_gid = from_kgid(&init_user_ns, current_gid());
297         }
298
299         return sec->ps_policy->sp_cops->lookup_ctx(sec, &vcred, create,
300                                                    remove_dead);
301 }
302
303 struct ptlrpc_cli_ctx *sptlrpc_cli_ctx_get(struct ptlrpc_cli_ctx *ctx)
304 {
305         atomic_inc(&ctx->cc_refcount);
306         return ctx;
307 }
308 EXPORT_SYMBOL(sptlrpc_cli_ctx_get);
309
310 void sptlrpc_cli_ctx_put(struct ptlrpc_cli_ctx *ctx, int sync)
311 {
312         struct ptlrpc_sec *sec = ctx->cc_sec;
313
314         LASSERT(sec);
315         LASSERT(atomic_read(&(ctx)->cc_refcount) > 0);
316
317         if (!atomic_dec_and_test(&ctx->cc_refcount))
318                 return;
319
320         sec->ps_policy->sp_cops->release_ctx(sec, ctx, sync);
321 }
322 EXPORT_SYMBOL(sptlrpc_cli_ctx_put);
323
324 /**
325  * Expire the client context immediately.
326  *
327  * \pre Caller must hold at least 1 reference on the \a ctx.
328  */
329 void sptlrpc_cli_ctx_expire(struct ptlrpc_cli_ctx *ctx)
330 {
331         LASSERT(ctx->cc_ops->die);
332         ctx->cc_ops->die(ctx, 0);
333 }
334 EXPORT_SYMBOL(sptlrpc_cli_ctx_expire);
335
336 /**
337  * To wake up the threads who are waiting for this client context. Called
338  * after some status change happened on \a ctx.
339  */
340 void sptlrpc_cli_ctx_wakeup(struct ptlrpc_cli_ctx *ctx)
341 {
342         struct ptlrpc_request *req, *next;
343
344         spin_lock(&ctx->cc_lock);
345         list_for_each_entry_safe(req, next, &ctx->cc_req_list,
346                                      rq_ctx_chain) {
347                 list_del_init(&req->rq_ctx_chain);
348                 ptlrpc_client_wake_req(req);
349         }
350         spin_unlock(&ctx->cc_lock);
351 }
352 EXPORT_SYMBOL(sptlrpc_cli_ctx_wakeup);
353
354 int sptlrpc_cli_ctx_display(struct ptlrpc_cli_ctx *ctx, char *buf, int bufsize)
355 {
356         LASSERT(ctx->cc_ops);
357
358         if (ctx->cc_ops->display == NULL)
359                 return 0;
360
361         return ctx->cc_ops->display(ctx, buf, bufsize);
362 }
363
364 static int import_sec_check_expire(struct obd_import *imp)
365 {
366         int adapt = 0;
367
368         write_lock(&imp->imp_sec_lock);
369         if (imp->imp_sec_expire &&
370             imp->imp_sec_expire < ktime_get_real_seconds()) {
371                 adapt = 1;
372                 imp->imp_sec_expire = 0;
373         }
374         write_unlock(&imp->imp_sec_lock);
375
376         if (!adapt)
377                 return 0;
378
379         CDEBUG(D_SEC, "found delayed sec adapt expired, do it now\n");
380         return sptlrpc_import_sec_adapt(imp, NULL, NULL);
381 }
382
383 /**
384  * Get and validate the client side ptlrpc security facilities from
385  * \a imp. There is a race condition on client reconnect when the import is
386  * being destroyed while there are outstanding client bound requests. In
387  * this case do not output any error messages if import secuity is not
388  * found.
389  *
390  * \param[in] imp obd import associated with client
391  * \param[out] sec client side ptlrpc security
392  *
393  * \retval 0 if security retrieved successfully
394  * \retval -ve errno if there was a problem
395  */
396 static int import_sec_validate_get(struct obd_import *imp,
397                                    struct ptlrpc_sec **sec)
398 {
399         int rc;
400
401         if (unlikely(imp->imp_sec_expire)) {
402                 rc = import_sec_check_expire(imp);
403                 if (rc)
404                         return rc;
405         }
406
407         *sec = sptlrpc_import_sec_ref(imp);
408         if (*sec == NULL) {
409                 /* Only output an error when the import is still active */
410                 if (!test_bit(WORK_STRUCT_PENDING_BIT,
411                               work_data_bits(&imp->imp_zombie_work)))
412                         CERROR("import %p (%s) with no sec\n",
413                                imp, ptlrpc_import_state_name(imp->imp_state));
414                 return -EACCES;
415         }
416
417         if (unlikely((*sec)->ps_dying)) {
418                 CERROR("attempt to use dying sec %p\n", sec);
419                 sptlrpc_sec_put(*sec);
420                 return -EACCES;
421         }
422
423         return 0;
424 }
425
426 /**
427  * Given a \a req, find or allocate an appropriate context for it.
428  * \pre req->rq_cli_ctx == NULL.
429  *
430  * \retval 0 succeed, and req->rq_cli_ctx is set.
431  * \retval -ev error number, and req->rq_cli_ctx == NULL.
432  */
433 int sptlrpc_req_get_ctx(struct ptlrpc_request *req)
434 {
435         struct obd_import *imp = req->rq_import;
436         struct ptlrpc_sec *sec;
437         int rc;
438
439         ENTRY;
440
441         LASSERT(!req->rq_cli_ctx);
442         LASSERT(imp);
443
444         rc = import_sec_validate_get(imp, &sec);
445         if (rc)
446                 RETURN(rc);
447
448         req->rq_cli_ctx = get_my_ctx(sec);
449
450         sptlrpc_sec_put(sec);
451
452         if (!req->rq_cli_ctx) {
453                 CERROR("req %p: fail to get context\n", req);
454                 RETURN(-ECONNREFUSED);
455         }
456
457         RETURN(0);
458 }
459
460 /**
461  * Drop the context for \a req.
462  * \pre req->rq_cli_ctx != NULL.
463  * \post req->rq_cli_ctx == NULL.
464  *
465  * If \a sync == 0, this function should return quickly without sleep;
466  * otherwise it might trigger and wait for the whole process of sending
467  * an context-destroying rpc to server.
468  */
469 void sptlrpc_req_put_ctx(struct ptlrpc_request *req, int sync)
470 {
471         ENTRY;
472
473         LASSERT(req);
474         LASSERT(req->rq_cli_ctx);
475
476         /*
477          * request might be asked to release earlier while still
478          * in the context waiting list.
479          */
480         if (!list_empty(&req->rq_ctx_chain)) {
481                 spin_lock(&req->rq_cli_ctx->cc_lock);
482                 list_del_init(&req->rq_ctx_chain);
483                 spin_unlock(&req->rq_cli_ctx->cc_lock);
484         }
485
486         sptlrpc_cli_ctx_put(req->rq_cli_ctx, sync);
487         req->rq_cli_ctx = NULL;
488         EXIT;
489 }
490
491 static
492 int sptlrpc_req_ctx_switch(struct ptlrpc_request *req,
493                            struct ptlrpc_cli_ctx *oldctx,
494                            struct ptlrpc_cli_ctx *newctx)
495 {
496         struct sptlrpc_flavor old_flvr;
497         char *reqmsg = NULL; /* to workaround old gcc */
498         int reqmsg_size;
499         int rc = 0;
500
501         CDEBUG(D_SEC,
502                "req %p: switch ctx %p(%u->%s) -> %p(%u->%s), switch sec %p(%s) -> %p(%s)\n",
503                req, oldctx, oldctx->cc_vcred.vc_uid,
504                sec2target_str(oldctx->cc_sec), newctx, newctx->cc_vcred.vc_uid,
505                sec2target_str(newctx->cc_sec), oldctx->cc_sec,
506                oldctx->cc_sec->ps_policy->sp_name, newctx->cc_sec,
507                newctx->cc_sec->ps_policy->sp_name);
508
509         /* save flavor */
510         old_flvr = req->rq_flvr;
511
512         /* save request message */
513         reqmsg_size = req->rq_reqlen;
514         if (reqmsg_size != 0) {
515                 LASSERT(req->rq_reqmsg);
516                 OBD_ALLOC_LARGE(reqmsg, reqmsg_size);
517                 if (reqmsg == NULL)
518                         return -ENOMEM;
519                 memcpy(reqmsg, req->rq_reqmsg, reqmsg_size);
520         }
521
522         /* release old req/rep buf */
523         req->rq_cli_ctx = oldctx;
524         sptlrpc_cli_free_reqbuf(req);
525         sptlrpc_cli_free_repbuf(req);
526         req->rq_cli_ctx = newctx;
527
528         /* recalculate the flavor */
529         sptlrpc_req_set_flavor(req, 0);
530
531         /*
532          * alloc new request buffer
533          * we don't need to alloc reply buffer here, leave it to the
534          * rest procedure of ptlrpc
535          */
536         if (reqmsg_size != 0) {
537                 rc = sptlrpc_cli_alloc_reqbuf(req, reqmsg_size);
538                 if (!rc) {
539                         LASSERT(req->rq_reqmsg);
540                         memcpy(req->rq_reqmsg, reqmsg, reqmsg_size);
541                 } else {
542                         CWARN("failed to alloc reqbuf: %d\n", rc);
543                         req->rq_flvr = old_flvr;
544                 }
545
546                 OBD_FREE_LARGE(reqmsg, reqmsg_size);
547         }
548         return rc;
549 }
550
551 /**
552  * If current context of \a req is dead somehow, e.g. we just switched flavor
553  * thus marked original contexts dead, we'll find a new context for it. if
554  * no switch is needed, \a req will end up with the same context.
555  *
556  * \note a request must have a context, to keep other parts of code happy.
557  * In any case of failure during the switching, we must restore the old one.
558  */
559 int sptlrpc_req_replace_dead_ctx(struct ptlrpc_request *req)
560 {
561         struct ptlrpc_cli_ctx *oldctx = req->rq_cli_ctx;
562         struct ptlrpc_cli_ctx *newctx;
563         int rc;
564
565         ENTRY;
566
567         LASSERT(oldctx);
568
569         sptlrpc_cli_ctx_get(oldctx);
570         sptlrpc_req_put_ctx(req, 0);
571
572         rc = sptlrpc_req_get_ctx(req);
573         if (unlikely(rc)) {
574                 LASSERT(!req->rq_cli_ctx);
575
576                 /* restore old ctx */
577                 req->rq_cli_ctx = oldctx;
578                 RETURN(rc);
579         }
580
581         newctx = req->rq_cli_ctx;
582         LASSERT(newctx);
583
584         if (unlikely(newctx == oldctx &&
585                      test_bit(PTLRPC_CTX_DEAD_BIT, &oldctx->cc_flags))) {
586                 /*
587                  * still get the old dead ctx, usually means system too busy
588                  */
589                 CDEBUG(D_SEC,
590                        "ctx (%p, fl %lx) doesn't switch, relax a little bit\n",
591                        newctx, newctx->cc_flags);
592
593                 schedule_timeout_interruptible(cfs_time_seconds(1));
594         } else if (unlikely(test_bit(PTLRPC_CTX_UPTODATE_BIT, &newctx->cc_flags)
595                             == 0)) {
596                 /*
597                  * new ctx not up to date yet
598                  */
599                 CDEBUG(D_SEC,
600                        "ctx (%p, fl %lx) doesn't switch, not up to date yet\n",
601                        newctx, newctx->cc_flags);
602         } else {
603                 /*
604                  * it's possible newctx == oldctx if we're switching
605                  * subflavor with the same sec.
606                  */
607                 rc = sptlrpc_req_ctx_switch(req, oldctx, newctx);
608                 if (rc) {
609                         /* restore old ctx */
610                         sptlrpc_req_put_ctx(req, 0);
611                         req->rq_cli_ctx = oldctx;
612                         RETURN(rc);
613                 }
614
615                 LASSERT(req->rq_cli_ctx == newctx);
616         }
617
618         sptlrpc_cli_ctx_put(oldctx, 1);
619         RETURN(0);
620 }
621 EXPORT_SYMBOL(sptlrpc_req_replace_dead_ctx);
622
623 static
624 int ctx_check_refresh(struct ptlrpc_cli_ctx *ctx)
625 {
626         if (cli_ctx_is_refreshed(ctx))
627                 return 1;
628         return 0;
629 }
630
631 static
632 void ctx_refresh_interrupt(struct ptlrpc_request *req)
633 {
634
635         spin_lock(&req->rq_lock);
636         req->rq_intr = 1;
637         spin_unlock(&req->rq_lock);
638 }
639
640 static
641 void req_off_ctx_list(struct ptlrpc_request *req, struct ptlrpc_cli_ctx *ctx)
642 {
643         spin_lock(&ctx->cc_lock);
644         if (!list_empty(&req->rq_ctx_chain))
645                 list_del_init(&req->rq_ctx_chain);
646         spin_unlock(&ctx->cc_lock);
647 }
648
649 /**
650  * To refresh the context of \req, if it's not up-to-date.
651  * \param timeout
652  * - == 0: do not wait
653  * - == MAX_SCHEDULE_TIMEOUT: wait indefinitely
654  * - > 0: not supported
655  *
656  * The status of the context could be subject to be changed by other threads
657  * at any time. We allow this race, but once we return with 0, the caller will
658  * suppose it's uptodated and keep using it until the owning rpc is done.
659  *
660  * \retval 0 only if the context is uptodated.
661  * \retval -ev error number.
662  */
663 int sptlrpc_req_refresh_ctx(struct ptlrpc_request *req, long timeout)
664 {
665         struct ptlrpc_cli_ctx *ctx = req->rq_cli_ctx;
666         struct ptlrpc_sec *sec;
667         int rc;
668
669         ENTRY;
670
671         LASSERT(ctx);
672
673         if (req->rq_ctx_init || req->rq_ctx_fini)
674                 RETURN(0);
675
676         if (timeout != 0 && timeout != MAX_SCHEDULE_TIMEOUT) {
677                 CERROR("req %p: invalid timeout %lu\n", req, timeout);
678                 RETURN(-EINVAL);
679         }
680
681         /*
682          * during the process a request's context might change type even
683          * (e.g. from gss ctx to null ctx), so each loop we need to re-check
684          * everything
685          */
686 again:
687         rc = import_sec_validate_get(req->rq_import, &sec);
688         if (rc)
689                 RETURN(rc);
690
691         if (sec->ps_flvr.sf_rpc != req->rq_flvr.sf_rpc) {
692                 CDEBUG(D_SEC, "req %p: flavor has changed %x -> %x\n",
693                        req, req->rq_flvr.sf_rpc, sec->ps_flvr.sf_rpc);
694                 req_off_ctx_list(req, ctx);
695                 sptlrpc_req_replace_dead_ctx(req);
696                 ctx = req->rq_cli_ctx;
697         }
698         sptlrpc_sec_put(sec);
699
700         if (cli_ctx_is_eternal(ctx))
701                 RETURN(0);
702
703         if (unlikely(test_bit(PTLRPC_CTX_NEW_BIT, &ctx->cc_flags))) {
704                 if (ctx->cc_ops->refresh)
705                         ctx->cc_ops->refresh(ctx);
706         }
707         LASSERT(test_bit(PTLRPC_CTX_NEW_BIT, &ctx->cc_flags) == 0);
708
709         LASSERT(ctx->cc_ops->validate);
710         if (ctx->cc_ops->validate(ctx) == 0) {
711                 req_off_ctx_list(req, ctx);
712                 RETURN(0);
713         }
714
715         if (unlikely(test_bit(PTLRPC_CTX_ERROR_BIT, &ctx->cc_flags))) {
716                 spin_lock(&req->rq_lock);
717                 req->rq_err = 1;
718                 spin_unlock(&req->rq_lock);
719                 req_off_ctx_list(req, ctx);
720                 RETURN(-EPERM);
721         }
722
723         /*
724          * There's a subtle issue for resending RPCs, suppose following
725          * situation:
726          *  1. the request was sent to server.
727          *  2. recovery was kicked start, after finished the request was
728          *     marked as resent.
729          *  3. resend the request.
730          *  4. old reply from server received, we accept and verify the reply.
731          *     this has to be success, otherwise the error will be aware
732          *     by application.
733          *  5. new reply from server received, dropped by LNet.
734          *
735          * Note the xid of old & new request is the same. We can't simply
736          * change xid for the resent request because the server replies on
737          * it for reply reconstruction.
738          *
739          * Commonly the original context should be uptodate because we
740          * have an expiry nice time; server will keep its context because
741          * we at least hold a ref of old context which prevent context
742          * from destroying RPC being sent. So server still can accept the
743          * request and finish the RPC. But if that's not the case:
744          *  1. If server side context has been trimmed, a NO_CONTEXT will
745          *     be returned, gss_cli_ctx_verify/unseal will switch to new
746          *     context by force.
747          *  2. Current context never be refreshed, then we are fine: we
748          *     never really send request with old context before.
749          */
750         if (test_bit(PTLRPC_CTX_UPTODATE_BIT, &ctx->cc_flags) &&
751             unlikely(req->rq_reqmsg) &&
752             lustre_msg_get_flags(req->rq_reqmsg) & MSG_RESENT) {
753                 req_off_ctx_list(req, ctx);
754                 RETURN(0);
755         }
756
757         if (unlikely(test_bit(PTLRPC_CTX_DEAD_BIT, &ctx->cc_flags))) {
758                 req_off_ctx_list(req, ctx);
759                 /*
760                  * don't switch ctx if import was deactivated
761                  */
762                 if (req->rq_import->imp_deactive) {
763                         spin_lock(&req->rq_lock);
764                         req->rq_err = 1;
765                         spin_unlock(&req->rq_lock);
766                         RETURN(-EINTR);
767                 }
768
769                 rc = sptlrpc_req_replace_dead_ctx(req);
770                 if (rc) {
771                         LASSERT(ctx == req->rq_cli_ctx);
772                         CERROR("req %p: failed to replace dead ctx %p: %d\n",
773                                req, ctx, rc);
774                         spin_lock(&req->rq_lock);
775                         req->rq_err = 1;
776                         spin_unlock(&req->rq_lock);
777                         RETURN(rc);
778                 }
779
780                 ctx = req->rq_cli_ctx;
781                 goto again;
782         }
783
784         /*
785          * Now we're sure this context is during upcall, add myself into
786          * waiting list
787          */
788         spin_lock(&ctx->cc_lock);
789         if (list_empty(&req->rq_ctx_chain))
790                 list_add(&req->rq_ctx_chain, &ctx->cc_req_list);
791         spin_unlock(&ctx->cc_lock);
792
793         if (timeout == 0)
794                 RETURN(-EAGAIN);
795
796         /* Clear any flags that may be present from previous sends */
797         LASSERT(req->rq_receiving_reply == 0);
798         spin_lock(&req->rq_lock);
799         req->rq_err = 0;
800         req->rq_timedout = 0;
801         req->rq_resend = 0;
802         req->rq_restart = 0;
803         spin_unlock(&req->rq_lock);
804
805         /* by now we know that timeout value is MAX_SCHEDULE_TIMEOUT,
806          * so wait indefinitely with non-fatal signals blocked
807          */
808         if (l_wait_event_abortable(req->rq_reply_waitq,
809                                    ctx_check_refresh(ctx)) == -ERESTARTSYS) {
810                 rc = -EINTR;
811                 ctx_refresh_interrupt(req);
812         }
813
814         /*
815          * following cases could lead us here:
816          * - successfully refreshed;
817          * - interrupted;
818          * - timedout, and we don't want recover from the failure;
819          * - timedout, and waked up upon recovery finished;
820          * - someone else mark this ctx dead by force;
821          * - someone invalidate the req and call ptlrpc_client_wake_req(),
822          *   e.g. ptlrpc_abort_inflight();
823          */
824         if (!cli_ctx_is_refreshed(ctx)) {
825                 /* timed out or interruptted */
826                 req_off_ctx_list(req, ctx);
827
828                 LASSERT(rc != 0);
829                 RETURN(rc);
830         }
831
832         goto again;
833 }
834
835 /* Bring ptlrpc_sec context up-to-date */
836 int sptlrpc_export_update_ctx(struct obd_export *exp)
837 {
838         struct obd_import *imp = exp ? exp->exp_imp_reverse : NULL;
839         struct ptlrpc_sec *sec = NULL;
840         struct ptlrpc_cli_ctx *ctx = NULL;
841         int rc = 0;
842
843         if (imp)
844                 sec = sptlrpc_import_sec_ref(imp);
845         if (sec) {
846                 ctx = get_my_ctx(sec);
847                 sptlrpc_sec_put(sec);
848         }
849
850         if (ctx) {
851                 if (ctx->cc_ops->refresh)
852                         rc = ctx->cc_ops->refresh(ctx);
853                 sptlrpc_cli_ctx_put(ctx, 1);
854         }
855         return rc;
856 }
857
858 /**
859  * Initialize flavor settings for \a req, according to \a opcode.
860  *
861  * \note this could be called in two situations:
862  * - new request from ptlrpc_pre_req(), with proper @opcode
863  * - old request which changed ctx in the middle, with @opcode == 0
864  */
865 void sptlrpc_req_set_flavor(struct ptlrpc_request *req, int opcode)
866 {
867         struct ptlrpc_sec *sec;
868
869         LASSERT(req->rq_import);
870         LASSERT(req->rq_cli_ctx);
871         LASSERT(req->rq_cli_ctx->cc_sec);
872         LASSERT(req->rq_bulk_read == 0 || req->rq_bulk_write == 0);
873
874         /* special security flags according to opcode */
875         switch (opcode) {
876         case OST_READ:
877         case MDS_READPAGE:
878         case MGS_CONFIG_READ:
879         case OBD_IDX_READ:
880                 req->rq_bulk_read = 1;
881                 break;
882         case OST_WRITE:
883         case MDS_WRITEPAGE:
884                 req->rq_bulk_write = 1;
885                 break;
886         case SEC_CTX_INIT:
887                 req->rq_ctx_init = 1;
888                 break;
889         case SEC_CTX_FINI:
890                 req->rq_ctx_fini = 1;
891                 break;
892         case 0:
893                 /* init/fini rpc won't be resend, so can't be here */
894                 LASSERT(req->rq_ctx_init == 0);
895                 LASSERT(req->rq_ctx_fini == 0);
896
897                 /* cleanup flags, which should be recalculated */
898                 req->rq_pack_udesc = 0;
899                 req->rq_pack_bulk = 0;
900                 break;
901         }
902
903         sec = req->rq_cli_ctx->cc_sec;
904
905         spin_lock(&sec->ps_lock);
906         req->rq_flvr = sec->ps_flvr;
907         spin_unlock(&sec->ps_lock);
908
909         /*
910          * force SVC_NULL for context initiation rpc, SVC_INTG for context
911          * destruction rpc
912          */
913         if (unlikely(req->rq_ctx_init))
914                 flvr_set_svc(&req->rq_flvr.sf_rpc, SPTLRPC_SVC_NULL);
915         else if (unlikely(req->rq_ctx_fini))
916                 flvr_set_svc(&req->rq_flvr.sf_rpc, SPTLRPC_SVC_INTG);
917
918         /* user descriptor flag, null security can't do it anyway */
919         if ((sec->ps_flvr.sf_flags & PTLRPC_SEC_FL_UDESC) &&
920             (req->rq_flvr.sf_rpc != SPTLRPC_FLVR_NULL))
921                 req->rq_pack_udesc = 1;
922
923         /* bulk security flag */
924         if ((req->rq_bulk_read || req->rq_bulk_write) &&
925             sptlrpc_flavor_has_bulk(&req->rq_flvr))
926                 req->rq_pack_bulk = 1;
927 }
928
929 void sptlrpc_request_out_callback(struct ptlrpc_request *req)
930 {
931         if (SPTLRPC_FLVR_SVC(req->rq_flvr.sf_rpc) != SPTLRPC_SVC_PRIV)
932                 return;
933
934         LASSERT(req->rq_clrbuf);
935         if (req->rq_pool || !req->rq_reqbuf)
936                 return;
937
938         OBD_FREE(req->rq_reqbuf, req->rq_reqbuf_len);
939         req->rq_reqbuf = NULL;
940         req->rq_reqbuf_len = 0;
941 }
942
943 /**
944  * Given an import \a imp, check whether current user has a valid context
945  * or not. We may create a new context and try to refresh it, and try
946  * repeatedly try in case of non-fatal errors. Return 0 means success.
947  */
948 int sptlrpc_import_check_ctx(struct obd_import *imp)
949 {
950         struct ptlrpc_sec     *sec;
951         struct ptlrpc_cli_ctx *ctx;
952         struct ptlrpc_request *req = NULL;
953         int rc;
954
955         ENTRY;
956
957         might_sleep();
958
959         sec = sptlrpc_import_sec_ref(imp);
960         ctx = get_my_ctx(sec);
961         sptlrpc_sec_put(sec);
962
963         if (!ctx)
964                 RETURN(-ENOMEM);
965
966         if (cli_ctx_is_eternal(ctx) ||
967             ctx->cc_ops->validate(ctx) == 0) {
968                 sptlrpc_cli_ctx_put(ctx, 1);
969                 RETURN(0);
970         }
971
972         if (cli_ctx_is_error(ctx)) {
973                 sptlrpc_cli_ctx_put(ctx, 1);
974                 RETURN(-EACCES);
975         }
976
977         req = ptlrpc_request_cache_alloc(GFP_NOFS);
978         if (!req)
979                 RETURN(-ENOMEM);
980
981         ptlrpc_cli_req_init(req);
982         atomic_set(&req->rq_refcount, 10000);
983
984         req->rq_import = imp;
985         req->rq_flvr = sec->ps_flvr;
986         req->rq_cli_ctx = ctx;
987
988         rc = sptlrpc_req_refresh_ctx(req, MAX_SCHEDULE_TIMEOUT);
989         LASSERT(list_empty(&req->rq_ctx_chain));
990         sptlrpc_cli_ctx_put(req->rq_cli_ctx, 1);
991         ptlrpc_request_cache_free(req);
992
993         RETURN(rc);
994 }
995
996 /**
997  * Used by ptlrpc client, to perform the pre-defined security transformation
998  * upon the request message of \a req. After this function called,
999  * req->rq_reqmsg is still accessible as clear text.
1000  */
1001 int sptlrpc_cli_wrap_request(struct ptlrpc_request *req)
1002 {
1003         struct ptlrpc_cli_ctx *ctx = req->rq_cli_ctx;
1004         int rc = 0;
1005
1006         ENTRY;
1007
1008         LASSERT(ctx);
1009         LASSERT(ctx->cc_sec);
1010         LASSERT(req->rq_reqbuf || req->rq_clrbuf);
1011
1012         /*
1013          * we wrap bulk request here because now we can be sure
1014          * the context is uptodate.
1015          */
1016         if (req->rq_bulk) {
1017                 rc = sptlrpc_cli_wrap_bulk(req, req->rq_bulk);
1018                 if (rc)
1019                         RETURN(rc);
1020         }
1021
1022         switch (SPTLRPC_FLVR_SVC(req->rq_flvr.sf_rpc)) {
1023         case SPTLRPC_SVC_NULL:
1024         case SPTLRPC_SVC_AUTH:
1025         case SPTLRPC_SVC_INTG:
1026                 LASSERT(ctx->cc_ops->sign);
1027                 rc = ctx->cc_ops->sign(ctx, req);
1028                 break;
1029         case SPTLRPC_SVC_PRIV:
1030                 LASSERT(ctx->cc_ops->seal);
1031                 rc = ctx->cc_ops->seal(ctx, req);
1032                 break;
1033         default:
1034                 LBUG();
1035         }
1036
1037         if (rc == 0) {
1038                 LASSERT(req->rq_reqdata_len);
1039                 LASSERT(req->rq_reqdata_len % 8 == 0);
1040                 LASSERT(req->rq_reqdata_len <= req->rq_reqbuf_len);
1041         }
1042
1043         RETURN(rc);
1044 }
1045
1046 static int do_cli_unwrap_reply(struct ptlrpc_request *req)
1047 {
1048         struct ptlrpc_cli_ctx *ctx = req->rq_cli_ctx;
1049         int rc;
1050
1051         ENTRY;
1052
1053         LASSERT(ctx);
1054         LASSERT(ctx->cc_sec);
1055         LASSERT(req->rq_repbuf);
1056         LASSERT(req->rq_repdata);
1057         LASSERT(req->rq_repmsg == NULL);
1058
1059         req->rq_rep_swab_mask = 0;
1060
1061         rc = __lustre_unpack_msg(req->rq_repdata, req->rq_repdata_len);
1062         switch (rc) {
1063         case 1:
1064                 req_capsule_set_rep_swabbed(&req->rq_pill,
1065                                             MSG_PTLRPC_HEADER_OFF);
1066         case 0:
1067                 break;
1068         default:
1069                 CERROR("failed unpack reply: x%llu\n", req->rq_xid);
1070                 RETURN(-EPROTO);
1071         }
1072
1073         if (req->rq_repdata_len < sizeof(struct lustre_msg)) {
1074                 CERROR("replied data length %d too small\n",
1075                        req->rq_repdata_len);
1076                 RETURN(-EPROTO);
1077         }
1078
1079         if (SPTLRPC_FLVR_POLICY(req->rq_repdata->lm_secflvr) !=
1080             SPTLRPC_FLVR_POLICY(req->rq_flvr.sf_rpc)) {
1081                 CERROR("reply policy %u doesn't match request policy %u\n",
1082                        SPTLRPC_FLVR_POLICY(req->rq_repdata->lm_secflvr),
1083                        SPTLRPC_FLVR_POLICY(req->rq_flvr.sf_rpc));
1084                 RETURN(-EPROTO);
1085         }
1086
1087         switch (SPTLRPC_FLVR_SVC(req->rq_flvr.sf_rpc)) {
1088         case SPTLRPC_SVC_NULL:
1089         case SPTLRPC_SVC_AUTH:
1090         case SPTLRPC_SVC_INTG:
1091                 LASSERT(ctx->cc_ops->verify);
1092                 rc = ctx->cc_ops->verify(ctx, req);
1093                 break;
1094         case SPTLRPC_SVC_PRIV:
1095                 LASSERT(ctx->cc_ops->unseal);
1096                 rc = ctx->cc_ops->unseal(ctx, req);
1097                 break;
1098         default:
1099                 LBUG();
1100         }
1101         LASSERT(rc || req->rq_repmsg || req->rq_resend);
1102
1103         if (SPTLRPC_FLVR_POLICY(req->rq_flvr.sf_rpc) != SPTLRPC_POLICY_NULL &&
1104             !req->rq_ctx_init)
1105                 req->rq_rep_swab_mask = 0;
1106         RETURN(rc);
1107 }
1108
1109 /**
1110  * Used by ptlrpc client, to perform security transformation upon the reply
1111  * message of \a req. After return successfully, req->rq_repmsg points to
1112  * the reply message in clear text.
1113  *
1114  * \pre the reply buffer should have been un-posted from LNet, so nothing is
1115  * going to change.
1116  */
1117 int sptlrpc_cli_unwrap_reply(struct ptlrpc_request *req)
1118 {
1119         LASSERT(req->rq_repbuf);
1120         LASSERT(req->rq_repdata == NULL);
1121         LASSERT(req->rq_repmsg == NULL);
1122         LASSERT(req->rq_reply_off + req->rq_nob_received <= req->rq_repbuf_len);
1123
1124         if (req->rq_reply_off == 0 &&
1125             (lustre_msghdr_get_flags(req->rq_reqmsg) & MSGHDR_AT_SUPPORT)) {
1126                 CERROR("real reply with offset 0\n");
1127                 return -EPROTO;
1128         }
1129
1130         if (req->rq_reply_off % 8 != 0) {
1131                 CERROR("reply at odd offset %u\n", req->rq_reply_off);
1132                 return -EPROTO;
1133         }
1134
1135         req->rq_repdata = (struct lustre_msg *)
1136                                 (req->rq_repbuf + req->rq_reply_off);
1137         req->rq_repdata_len = req->rq_nob_received;
1138
1139         return do_cli_unwrap_reply(req);
1140 }
1141
1142 /**
1143  * Used by ptlrpc client, to perform security transformation upon the early
1144  * reply message of \a req. We expect the rq_reply_off is 0, and
1145  * rq_nob_received is the early reply size.
1146  *
1147  * Because the receive buffer might be still posted, the reply data might be
1148  * changed at any time, no matter we're holding rq_lock or not. For this reason
1149  * we allocate a separate ptlrpc_request and reply buffer for early reply
1150  * processing.
1151  *
1152  * \retval 0 success, \a req_ret is filled with a duplicated ptlrpc_request.
1153  * Later the caller must call sptlrpc_cli_finish_early_reply() on the returned
1154  * \a *req_ret to release it.
1155  * \retval -ev error number, and \a req_ret will not be set.
1156  */
1157 int sptlrpc_cli_unwrap_early_reply(struct ptlrpc_request *req,
1158                                    struct ptlrpc_request **req_ret)
1159 {
1160         struct ptlrpc_request *early_req;
1161         char *early_buf;
1162         int early_bufsz, early_size;
1163         int rc;
1164
1165         ENTRY;
1166
1167         early_req = ptlrpc_request_cache_alloc(GFP_NOFS);
1168         if (early_req == NULL)
1169                 RETURN(-ENOMEM);
1170
1171         ptlrpc_cli_req_init(early_req);
1172
1173         early_size = req->rq_nob_received;
1174         early_bufsz = size_roundup_power2(early_size);
1175         OBD_ALLOC_LARGE(early_buf, early_bufsz);
1176         if (early_buf == NULL)
1177                 GOTO(err_req, rc = -ENOMEM);
1178
1179         /* sanity checkings and copy data out, do it inside spinlock */
1180         spin_lock(&req->rq_lock);
1181
1182         if (req->rq_replied) {
1183                 spin_unlock(&req->rq_lock);
1184                 GOTO(err_buf, rc = -EALREADY);
1185         }
1186
1187         LASSERT(req->rq_repbuf);
1188         LASSERT(req->rq_repdata == NULL);
1189         LASSERT(req->rq_repmsg == NULL);
1190
1191         if (req->rq_reply_off != 0) {
1192                 CERROR("early reply with offset %u\n", req->rq_reply_off);
1193                 spin_unlock(&req->rq_lock);
1194                 GOTO(err_buf, rc = -EPROTO);
1195         }
1196
1197         if (req->rq_nob_received != early_size) {
1198                 /* even another early arrived the size should be the same */
1199                 CERROR("data size has changed from %u to %u\n",
1200                        early_size, req->rq_nob_received);
1201                 spin_unlock(&req->rq_lock);
1202                 GOTO(err_buf, rc = -EINVAL);
1203         }
1204
1205         if (req->rq_nob_received < sizeof(struct lustre_msg)) {
1206                 CERROR("early reply length %d too small\n",
1207                        req->rq_nob_received);
1208                 spin_unlock(&req->rq_lock);
1209                 GOTO(err_buf, rc = -EALREADY);
1210         }
1211
1212         memcpy(early_buf, req->rq_repbuf, early_size);
1213         spin_unlock(&req->rq_lock);
1214
1215         early_req->rq_cli_ctx = sptlrpc_cli_ctx_get(req->rq_cli_ctx);
1216         early_req->rq_flvr = req->rq_flvr;
1217         early_req->rq_repbuf = early_buf;
1218         early_req->rq_repbuf_len = early_bufsz;
1219         early_req->rq_repdata = (struct lustre_msg *) early_buf;
1220         early_req->rq_repdata_len = early_size;
1221         early_req->rq_early = 1;
1222         early_req->rq_reqmsg = req->rq_reqmsg;
1223
1224         rc = do_cli_unwrap_reply(early_req);
1225         if (rc) {
1226                 DEBUG_REQ(D_ADAPTTO, early_req,
1227                           "unwrap early reply: rc = %d", rc);
1228                 GOTO(err_ctx, rc);
1229         }
1230
1231         LASSERT(early_req->rq_repmsg);
1232         *req_ret = early_req;
1233         RETURN(0);
1234
1235 err_ctx:
1236         sptlrpc_cli_ctx_put(early_req->rq_cli_ctx, 1);
1237 err_buf:
1238         OBD_FREE_LARGE(early_buf, early_bufsz);
1239 err_req:
1240         ptlrpc_request_cache_free(early_req);
1241         RETURN(rc);
1242 }
1243
1244 /**
1245  * Used by ptlrpc client, to release a processed early reply \a early_req.
1246  *
1247  * \pre \a early_req was obtained from calling sptlrpc_cli_unwrap_early_reply().
1248  */
1249 void sptlrpc_cli_finish_early_reply(struct ptlrpc_request *early_req)
1250 {
1251         LASSERT(early_req->rq_repbuf);
1252         LASSERT(early_req->rq_repdata);
1253         LASSERT(early_req->rq_repmsg);
1254
1255         sptlrpc_cli_ctx_put(early_req->rq_cli_ctx, 1);
1256         OBD_FREE_LARGE(early_req->rq_repbuf, early_req->rq_repbuf_len);
1257         ptlrpc_request_cache_free(early_req);
1258 }
1259
1260 /**************************************************
1261  * sec ID                                         *
1262  **************************************************/
1263
1264 /*
1265  * "fixed" sec (e.g. null) use sec_id < 0
1266  */
1267 static atomic_t sptlrpc_sec_id = ATOMIC_INIT(1);
1268
1269 int sptlrpc_get_next_secid(void)
1270 {
1271         return atomic_inc_return(&sptlrpc_sec_id);
1272 }
1273 EXPORT_SYMBOL(sptlrpc_get_next_secid);
1274
1275 /*
1276  * client side high-level security APIs
1277  */
1278
1279 static int sec_cop_flush_ctx_cache(struct ptlrpc_sec *sec, uid_t uid,
1280                                    int grace, int force)
1281 {
1282         struct ptlrpc_sec_policy *policy = sec->ps_policy;
1283
1284         LASSERT(policy->sp_cops);
1285         LASSERT(policy->sp_cops->flush_ctx_cache);
1286
1287         return policy->sp_cops->flush_ctx_cache(sec, uid, grace, force);
1288 }
1289
1290 static void sec_cop_destroy_sec(struct ptlrpc_sec *sec)
1291 {
1292         struct ptlrpc_sec_policy *policy = sec->ps_policy;
1293         struct sptlrpc_sepol *sepol;
1294
1295         LASSERT(atomic_read(&sec->ps_refcount) == 0);
1296         LASSERT(policy->sp_cops->destroy_sec);
1297
1298         CDEBUG(D_SEC, "%s@%p: being destroyed\n", sec->ps_policy->sp_name, sec);
1299
1300         spin_lock(&sec->ps_lock);
1301         sec->ps_sepol_checknext = ktime_set(0, 0);
1302         sepol = rcu_dereference_protected(sec->ps_sepol, 1);
1303         rcu_assign_pointer(sec->ps_sepol, NULL);
1304         spin_unlock(&sec->ps_lock);
1305
1306         sptlrpc_sepol_put(sepol);
1307
1308         policy->sp_cops->destroy_sec(sec);
1309         sptlrpc_policy_put(policy);
1310 }
1311
1312 void sptlrpc_sec_destroy(struct ptlrpc_sec *sec)
1313 {
1314         sec_cop_destroy_sec(sec);
1315 }
1316 EXPORT_SYMBOL(sptlrpc_sec_destroy);
1317
1318 static void sptlrpc_sec_kill(struct ptlrpc_sec *sec)
1319 {
1320         LASSERT(atomic_read(&(sec)->ps_refcount) > 0);
1321
1322         if (sec->ps_policy->sp_cops->kill_sec) {
1323                 sec->ps_policy->sp_cops->kill_sec(sec);
1324
1325                 sec_cop_flush_ctx_cache(sec, -1, 1, 1);
1326         }
1327 }
1328
1329 struct ptlrpc_sec *sptlrpc_sec_get(struct ptlrpc_sec *sec)
1330 {
1331         if (sec)
1332                 atomic_inc(&sec->ps_refcount);
1333
1334         return sec;
1335 }
1336 EXPORT_SYMBOL(sptlrpc_sec_get);
1337
1338 void sptlrpc_sec_put(struct ptlrpc_sec *sec)
1339 {
1340         if (sec) {
1341                 LASSERT(atomic_read(&(sec)->ps_refcount) > 0);
1342
1343                 if (atomic_dec_and_test(&sec->ps_refcount)) {
1344                         sptlrpc_gc_del_sec(sec);
1345                         sec_cop_destroy_sec(sec);
1346                 }
1347         }
1348 }
1349 EXPORT_SYMBOL(sptlrpc_sec_put);
1350
1351 /*
1352  * policy module is responsible for taking refrence of import
1353  */
1354 static
1355 struct ptlrpc_sec * sptlrpc_sec_create(struct obd_import *imp,
1356                                        struct ptlrpc_svc_ctx *svc_ctx,
1357                                        struct sptlrpc_flavor *sf,
1358                                        enum lustre_sec_part sp)
1359 {
1360         struct ptlrpc_sec_policy *policy;
1361         struct ptlrpc_sec *sec;
1362         char str[32];
1363
1364         ENTRY;
1365
1366         if (svc_ctx) {
1367                 LASSERT(imp->imp_dlm_fake == 1);
1368
1369                 CDEBUG(D_SEC, "%s %s: reverse sec using flavor %s\n",
1370                        imp->imp_obd->obd_type->typ_name,
1371                        imp->imp_obd->obd_name,
1372                        sptlrpc_flavor2name(sf, str, sizeof(str)));
1373
1374                 policy = sptlrpc_policy_get(svc_ctx->sc_policy);
1375                 sf->sf_flags |= PTLRPC_SEC_FL_REVERSE | PTLRPC_SEC_FL_ROOTONLY;
1376         } else {
1377                 LASSERT(imp->imp_dlm_fake == 0);
1378
1379                 CDEBUG(D_SEC, "%s %s: select security flavor %s\n",
1380                        imp->imp_obd->obd_type->typ_name,
1381                        imp->imp_obd->obd_name,
1382                        sptlrpc_flavor2name(sf, str, sizeof(str)));
1383
1384                 policy = sptlrpc_wireflavor2policy(sf->sf_rpc);
1385                 if (!policy) {
1386                         CERROR("invalid flavor 0x%x\n", sf->sf_rpc);
1387                         RETURN(NULL);
1388                 }
1389         }
1390
1391         sec = policy->sp_cops->create_sec(imp, svc_ctx, sf);
1392         if (sec) {
1393                 atomic_inc(&sec->ps_refcount);
1394
1395                 sec->ps_part = sp;
1396
1397                 if (sec->ps_gc_interval && policy->sp_cops->gc_ctx)
1398                         sptlrpc_gc_add_sec(sec);
1399         } else {
1400                 sptlrpc_policy_put(policy);
1401         }
1402
1403         RETURN(sec);
1404 }
1405
1406 struct ptlrpc_sec *sptlrpc_import_sec_ref(struct obd_import *imp)
1407 {
1408         struct ptlrpc_sec *sec;
1409
1410         read_lock(&imp->imp_sec_lock);
1411         sec = sptlrpc_sec_get(imp->imp_sec);
1412         read_unlock(&imp->imp_sec_lock);
1413
1414         return sec;
1415 }
1416 EXPORT_SYMBOL(sptlrpc_import_sec_ref);
1417
1418 static void sptlrpc_import_sec_install(struct obd_import *imp,
1419                                        struct ptlrpc_sec *sec)
1420 {
1421         struct ptlrpc_sec *old_sec;
1422
1423         LASSERT(atomic_read(&(sec)->ps_refcount) > 0);
1424
1425         write_lock(&imp->imp_sec_lock);
1426         old_sec = imp->imp_sec;
1427         imp->imp_sec = sec;
1428         write_unlock(&imp->imp_sec_lock);
1429
1430         if (old_sec) {
1431                 sptlrpc_sec_kill(old_sec);
1432
1433                 /* balance the ref taken by this import */
1434                 sptlrpc_sec_put(old_sec);
1435         }
1436 }
1437
1438 static inline
1439 int flavor_equal(struct sptlrpc_flavor *sf1, struct sptlrpc_flavor *sf2)
1440 {
1441         return (memcmp(sf1, sf2, sizeof(*sf1)) == 0);
1442 }
1443
1444 static inline
1445 void flavor_copy(struct sptlrpc_flavor *dst, struct sptlrpc_flavor *src)
1446 {
1447         *dst = *src;
1448 }
1449
1450 /**
1451  * To get an appropriate ptlrpc_sec for the \a imp, according to the current
1452  * configuration. Upon called, imp->imp_sec may or may not be NULL.
1453  *
1454  *  - regular import: \a svc_ctx should be NULL and \a flvr is ignored;
1455  *  - reverse import: \a svc_ctx and \a flvr are obtained from incoming request.
1456  */
1457 int sptlrpc_import_sec_adapt(struct obd_import *imp,
1458                              struct ptlrpc_svc_ctx *svc_ctx,
1459                              struct sptlrpc_flavor *flvr)
1460 {
1461         struct ptlrpc_connection *conn;
1462         struct sptlrpc_flavor sf;
1463         struct ptlrpc_sec *sec, *newsec;
1464         enum lustre_sec_part sp;
1465         char str[24];
1466         int rc = 0;
1467
1468         ENTRY;
1469
1470         might_sleep();
1471
1472         if (imp == NULL)
1473                 RETURN(0);
1474
1475         conn = imp->imp_connection;
1476
1477         if (svc_ctx == NULL) {
1478                 struct client_obd *cliobd = &imp->imp_obd->u.cli;
1479                 /*
1480                  * normal import, determine flavor from rule set, except
1481                  * for mgc the flavor is predetermined.
1482                  */
1483                 if (cliobd->cl_sp_me == LUSTRE_SP_MGC)
1484                         sf = cliobd->cl_flvr_mgc;
1485                 else
1486                         sptlrpc_conf_choose_flavor(cliobd->cl_sp_me,
1487                                                    cliobd->cl_sp_to,
1488                                                    &cliobd->cl_target_uuid,
1489                                                    &conn->c_self, &sf);
1490
1491                 sp = imp->imp_obd->u.cli.cl_sp_me;
1492         } else {
1493                 /* reverse import, determine flavor from incoming reqeust */
1494                 sf = *flvr;
1495
1496                 if (sf.sf_rpc != SPTLRPC_FLVR_NULL)
1497                         sf.sf_flags = PTLRPC_SEC_FL_REVERSE |
1498                                       PTLRPC_SEC_FL_ROOTONLY;
1499
1500                 sp = sptlrpc_target_sec_part(imp->imp_obd);
1501         }
1502
1503         sec = sptlrpc_import_sec_ref(imp);
1504         if (sec) {
1505                 char str2[24];
1506
1507                 if (flavor_equal(&sf, &sec->ps_flvr))
1508                         GOTO(out, rc);
1509
1510                 CDEBUG(D_SEC, "import %s->%s: changing flavor %s -> %s\n",
1511                        imp->imp_obd->obd_name,
1512                        obd_uuid2str(&conn->c_remote_uuid),
1513                        sptlrpc_flavor2name(&sec->ps_flvr, str, sizeof(str)),
1514                        sptlrpc_flavor2name(&sf, str2, sizeof(str2)));
1515         } else if (SPTLRPC_FLVR_BASE(sf.sf_rpc) !=
1516                    SPTLRPC_FLVR_BASE(SPTLRPC_FLVR_NULL)) {
1517                 CDEBUG(D_SEC, "import %s->%s netid %x: select flavor %s\n",
1518                        imp->imp_obd->obd_name,
1519                        obd_uuid2str(&conn->c_remote_uuid),
1520                        LNET_NID_NET(&conn->c_self),
1521                        sptlrpc_flavor2name(&sf, str, sizeof(str)));
1522         }
1523
1524         newsec = sptlrpc_sec_create(imp, svc_ctx, &sf, sp);
1525         if (newsec) {
1526                 sptlrpc_import_sec_install(imp, newsec);
1527         } else {
1528                 CERROR("import %s->%s: failed to create new sec\n",
1529                        imp->imp_obd->obd_name,
1530                        obd_uuid2str(&conn->c_remote_uuid));
1531                 rc = -EPERM;
1532         }
1533
1534 out:
1535         sptlrpc_sec_put(sec);
1536         RETURN(rc);
1537 }
1538
1539 void sptlrpc_import_sec_put(struct obd_import *imp)
1540 {
1541         if (imp->imp_sec) {
1542                 sptlrpc_sec_kill(imp->imp_sec);
1543
1544                 sptlrpc_sec_put(imp->imp_sec);
1545                 imp->imp_sec = NULL;
1546         }
1547 }
1548
1549 static void import_flush_ctx_common(struct obd_import *imp,
1550                                     uid_t uid, int grace, int force)
1551 {
1552         struct ptlrpc_sec *sec;
1553
1554         if (imp == NULL)
1555                 return;
1556
1557         sec = sptlrpc_import_sec_ref(imp);
1558         if (sec == NULL)
1559                 return;
1560
1561         sec_cop_flush_ctx_cache(sec, uid, grace, force);
1562         sptlrpc_sec_put(sec);
1563 }
1564
1565 void sptlrpc_import_flush_root_ctx(struct obd_import *imp)
1566 {
1567         /*
1568          * it's important to use grace mode, see explain in
1569          * sptlrpc_req_refresh_ctx()
1570          */
1571         import_flush_ctx_common(imp, 0, 1, 1);
1572 }
1573
1574 void sptlrpc_import_flush_my_ctx(struct obd_import *imp)
1575 {
1576         import_flush_ctx_common(imp, from_kuid(&init_user_ns, current_uid()),
1577                                 1, 1);
1578 }
1579 EXPORT_SYMBOL(sptlrpc_import_flush_my_ctx);
1580
1581 void sptlrpc_import_flush_all_ctx(struct obd_import *imp)
1582 {
1583         import_flush_ctx_common(imp, -1, 1, 1);
1584 }
1585 EXPORT_SYMBOL(sptlrpc_import_flush_all_ctx);
1586
1587 /**
1588  * Used by ptlrpc client to allocate request buffer of \a req. Upon return
1589  * successfully, req->rq_reqmsg points to a buffer with size \a msgsize.
1590  */
1591 int sptlrpc_cli_alloc_reqbuf(struct ptlrpc_request *req, int msgsize)
1592 {
1593         struct ptlrpc_cli_ctx *ctx = req->rq_cli_ctx;
1594         struct ptlrpc_sec_policy *policy;
1595         int rc;
1596
1597         LASSERT(ctx);
1598         LASSERT(ctx->cc_sec);
1599         LASSERT(ctx->cc_sec->ps_policy);
1600         LASSERT(req->rq_reqmsg == NULL);
1601         LASSERT(atomic_read(&(ctx)->cc_refcount) > 0);
1602
1603         policy = ctx->cc_sec->ps_policy;
1604         rc = policy->sp_cops->alloc_reqbuf(ctx->cc_sec, req, msgsize);
1605         if (!rc) {
1606                 LASSERT(req->rq_reqmsg);
1607                 LASSERT(req->rq_reqbuf || req->rq_clrbuf);
1608
1609                 /* zeroing preallocated buffer */
1610                 if (req->rq_pool)
1611                         memset(req->rq_reqmsg, 0, msgsize);
1612         }
1613
1614         return rc;
1615 }
1616
1617 /**
1618  * Used by ptlrpc client to free request buffer of \a req. After this
1619  * req->rq_reqmsg is set to NULL and should not be accessed anymore.
1620  */
1621 void sptlrpc_cli_free_reqbuf(struct ptlrpc_request *req)
1622 {
1623         struct ptlrpc_cli_ctx *ctx = req->rq_cli_ctx;
1624         struct ptlrpc_sec_policy *policy;
1625
1626         LASSERT(ctx);
1627         LASSERT(ctx->cc_sec);
1628         LASSERT(ctx->cc_sec->ps_policy);
1629         LASSERT(atomic_read(&(ctx)->cc_refcount) > 0);
1630
1631         if (req->rq_reqbuf == NULL && req->rq_clrbuf == NULL)
1632                 return;
1633
1634         policy = ctx->cc_sec->ps_policy;
1635         policy->sp_cops->free_reqbuf(ctx->cc_sec, req);
1636         req->rq_reqmsg = NULL;
1637 }
1638
1639 /*
1640  * NOTE caller must guarantee the buffer size is enough for the enlargement
1641  */
1642 void _sptlrpc_enlarge_msg_inplace(struct lustre_msg *msg,
1643                                   int segment, int newsize)
1644 {
1645         void *src, *dst;
1646         int oldsize, oldmsg_size, movesize;
1647
1648         LASSERT(segment < msg->lm_bufcount);
1649         LASSERT(msg->lm_buflens[segment] <= newsize);
1650
1651         if (msg->lm_buflens[segment] == newsize)
1652                 return;
1653
1654         /* nothing to do if we are enlarging the last segment */
1655         if (segment == msg->lm_bufcount - 1) {
1656                 msg->lm_buflens[segment] = newsize;
1657                 return;
1658         }
1659
1660         oldsize = msg->lm_buflens[segment];
1661
1662         src = lustre_msg_buf(msg, segment + 1, 0);
1663         msg->lm_buflens[segment] = newsize;
1664         dst = lustre_msg_buf(msg, segment + 1, 0);
1665         msg->lm_buflens[segment] = oldsize;
1666
1667         /* move from segment + 1 to end segment */
1668         LASSERT(msg->lm_magic == LUSTRE_MSG_MAGIC_V2);
1669         oldmsg_size = lustre_msg_size_v2(msg->lm_bufcount, msg->lm_buflens);
1670         movesize = oldmsg_size - ((unsigned long) src - (unsigned long) msg);
1671         LASSERT(movesize >= 0);
1672
1673         if (movesize)
1674                 memmove(dst, src, movesize);
1675
1676         /* note we don't clear the ares where old data live, not secret */
1677
1678         /* finally set new segment size */
1679         msg->lm_buflens[segment] = newsize;
1680 }
1681 EXPORT_SYMBOL(_sptlrpc_enlarge_msg_inplace);
1682
1683 /**
1684  * Used by ptlrpc client to enlarge the \a segment of request message pointed
1685  * by req->rq_reqmsg to size \a newsize, all previously filled-in data will be
1686  * preserved after the enlargement. this must be called after original request
1687  * buffer being allocated.
1688  *
1689  * \note after this be called, rq_reqmsg and rq_reqlen might have been changed,
1690  * so caller should refresh its local pointers if needed.
1691  */
1692 int sptlrpc_cli_enlarge_reqbuf(struct ptlrpc_request *req,
1693                                const struct req_msg_field *field,
1694                                int newsize)
1695 {
1696         struct req_capsule *pill = &req->rq_pill;
1697         struct ptlrpc_cli_ctx *ctx = req->rq_cli_ctx;
1698         struct ptlrpc_sec_cops *cops;
1699         struct lustre_msg *msg = req->rq_reqmsg;
1700         int segment = __req_capsule_offset(pill, field, RCL_CLIENT);
1701
1702         LASSERT(ctx);
1703         LASSERT(msg);
1704         LASSERT(msg->lm_bufcount > segment);
1705         LASSERT(msg->lm_buflens[segment] <= newsize);
1706
1707         if (msg->lm_buflens[segment] == newsize)
1708                 return 0;
1709
1710         cops = ctx->cc_sec->ps_policy->sp_cops;
1711         LASSERT(cops->enlarge_reqbuf);
1712         return cops->enlarge_reqbuf(ctx->cc_sec, req, segment, newsize);
1713 }
1714 EXPORT_SYMBOL(sptlrpc_cli_enlarge_reqbuf);
1715
1716 /**
1717  * Used by ptlrpc client to allocate reply buffer of \a req.
1718  *
1719  * \note After this, req->rq_repmsg is still not accessible.
1720  */
1721 int sptlrpc_cli_alloc_repbuf(struct ptlrpc_request *req, int msgsize)
1722 {
1723         struct ptlrpc_cli_ctx *ctx = req->rq_cli_ctx;
1724         struct ptlrpc_sec_policy *policy;
1725
1726         ENTRY;
1727
1728         LASSERT(ctx);
1729         LASSERT(ctx->cc_sec);
1730         LASSERT(ctx->cc_sec->ps_policy);
1731
1732         if (req->rq_repbuf)
1733                 RETURN(0);
1734
1735         policy = ctx->cc_sec->ps_policy;
1736         RETURN(policy->sp_cops->alloc_repbuf(ctx->cc_sec, req, msgsize));
1737 }
1738
1739 /**
1740  * Used by ptlrpc client to free reply buffer of \a req. After this
1741  * req->rq_repmsg is set to NULL and should not be accessed anymore.
1742  */
1743 void sptlrpc_cli_free_repbuf(struct ptlrpc_request *req)
1744 {
1745         struct ptlrpc_cli_ctx *ctx = req->rq_cli_ctx;
1746         struct ptlrpc_sec_policy *policy;
1747
1748         ENTRY;
1749
1750         LASSERT(ctx);
1751         LASSERT(ctx->cc_sec);
1752         LASSERT(ctx->cc_sec->ps_policy);
1753         LASSERT(atomic_read(&(ctx)->cc_refcount) > 0);
1754
1755         if (req->rq_repbuf == NULL)
1756                 return;
1757         LASSERT(req->rq_repbuf_len);
1758
1759         policy = ctx->cc_sec->ps_policy;
1760         policy->sp_cops->free_repbuf(ctx->cc_sec, req);
1761         req->rq_repmsg = NULL;
1762         EXIT;
1763 }
1764 EXPORT_SYMBOL(sptlrpc_cli_free_repbuf);
1765
1766 int sptlrpc_cli_install_rvs_ctx(struct obd_import *imp,
1767                                 struct ptlrpc_cli_ctx *ctx)
1768 {
1769         struct ptlrpc_sec_policy *policy = ctx->cc_sec->ps_policy;
1770
1771         if (!policy->sp_cops->install_rctx)
1772                 return 0;
1773         return policy->sp_cops->install_rctx(imp, ctx->cc_sec, ctx);
1774 }
1775
1776 int sptlrpc_svc_install_rvs_ctx(struct obd_import *imp,
1777                                 struct ptlrpc_svc_ctx *ctx)
1778 {
1779         struct ptlrpc_sec_policy *policy = ctx->sc_policy;
1780
1781         if (!policy->sp_sops->install_rctx)
1782                 return 0;
1783         return policy->sp_sops->install_rctx(imp, ctx);
1784 }
1785
1786
1787 /* Get SELinux policy info from userspace */
1788 static int sepol_helper(struct obd_import *imp)
1789 {
1790         char mtime_str[21] = { 0 }, mode_str[2] = { 0 };
1791         char *argv[] = {
1792                 [0] = "/usr/sbin/l_getsepol",
1793                 [1] = "-o",
1794                 [2] = NULL,         /* obd type */
1795                 [3] = "-n",
1796                 [4] = NULL,         /* obd name */
1797                 [5] = "-t",
1798                 [6] = mtime_str,    /* policy mtime */
1799                 [7] = "-m",
1800                 [8] = mode_str,     /* enforcing mode */
1801                 [9] = NULL
1802         };
1803         struct sptlrpc_sepol *sepol;
1804         char *envp[] = {
1805                 [0] = "HOME=/",
1806                 [1] = "PATH=/sbin:/usr/sbin",
1807                 [2] = NULL
1808         };
1809         signed short ret;
1810         int rc = 0;
1811
1812         if (imp == NULL || imp->imp_obd == NULL ||
1813             imp->imp_obd->obd_type == NULL)
1814                 RETURN(-EINVAL);
1815
1816         argv[2] = (char *)imp->imp_obd->obd_type->typ_name;
1817         argv[4] = imp->imp_obd->obd_name;
1818
1819         rcu_read_lock();
1820         sepol = rcu_dereference(imp->imp_sec->ps_sepol);
1821         if (!sepol) {
1822                 /* ps_sepol has not been initialized */
1823                 argv[5] = NULL;
1824                 argv[7] = NULL;
1825         } else {
1826                 time64_t mtime_ms;
1827
1828                 mtime_ms = ktime_to_ms(sepol->ssp_mtime);
1829                 snprintf(mtime_str, sizeof(mtime_str), "%lld",
1830                          mtime_ms / MSEC_PER_SEC);
1831                 if (sepol->ssp_sepol_size > 1)
1832                         mode_str[0] = sepol->ssp_sepol[0];
1833         }
1834         rcu_read_unlock();
1835
1836         ret = call_usermodehelper(argv[0], argv, envp, UMH_WAIT_PROC);
1837         rc = ret>>8;
1838
1839         return rc;
1840 }
1841
1842 static inline int sptlrpc_sepol_needs_check(struct ptlrpc_sec *imp_sec)
1843 {
1844         ktime_t checknext;
1845
1846         if (send_sepol == 0)
1847                 return 0;
1848
1849         if (send_sepol == -1)
1850                 /* send_sepol == -1 means fetch sepol status every time */
1851                 return 1;
1852
1853         spin_lock(&imp_sec->ps_lock);
1854         checknext = imp_sec->ps_sepol_checknext;
1855         spin_unlock(&imp_sec->ps_lock);
1856
1857         /* next check is too far in time, please update */
1858         if (ktime_after(checknext,
1859                         ktime_add(ktime_get(), ktime_set(send_sepol, 0))))
1860                 goto setnext;
1861
1862         if (ktime_before(ktime_get(), checknext))
1863                 /* too early to fetch sepol status */
1864                 return 0;
1865
1866 setnext:
1867         /* define new sepol_checknext time */
1868         spin_lock(&imp_sec->ps_lock);
1869         imp_sec->ps_sepol_checknext = ktime_add(ktime_get(),
1870                                                 ktime_set(send_sepol, 0));
1871         spin_unlock(&imp_sec->ps_lock);
1872
1873         return 1;
1874 }
1875
1876 static void sptlrpc_sepol_release(struct kref *ref)
1877 {
1878         struct sptlrpc_sepol *p = container_of(ref, struct sptlrpc_sepol,
1879                                               ssp_ref);
1880         kfree_rcu(p, ssp_rcu);
1881 }
1882
1883 void sptlrpc_sepol_put(struct sptlrpc_sepol *pol)
1884 {
1885         if (!pol)
1886                 return;
1887         kref_put(&pol->ssp_ref, sptlrpc_sepol_release);
1888 }
1889 EXPORT_SYMBOL(sptlrpc_sepol_put);
1890
1891 struct sptlrpc_sepol *sptlrpc_sepol_get_cached(struct ptlrpc_sec *imp_sec)
1892 {
1893         struct sptlrpc_sepol *p;
1894
1895 retry:
1896         rcu_read_lock();
1897         p = rcu_dereference(imp_sec->ps_sepol);
1898         if (p && !kref_get_unless_zero(&p->ssp_ref)) {
1899                 rcu_read_unlock();
1900                 goto retry;
1901         }
1902         rcu_read_unlock();
1903
1904         return p;
1905 }
1906 EXPORT_SYMBOL(sptlrpc_sepol_get_cached);
1907
1908 struct sptlrpc_sepol *sptlrpc_sepol_get(struct ptlrpc_request *req)
1909 {
1910         struct ptlrpc_sec *imp_sec = req->rq_import->imp_sec;
1911         struct sptlrpc_sepol *out;
1912         int rc = 0;
1913
1914         ENTRY;
1915
1916 #ifndef HAVE_SELINUX
1917         if (unlikely(send_sepol != 0))
1918                 CDEBUG(D_SEC,
1919                        "Client cannot report SELinux status, it was not built against libselinux.\n");
1920         RETURN(NULL);
1921 #endif
1922
1923         if (send_sepol == 0)
1924                 RETURN(NULL);
1925
1926         if (imp_sec == NULL)
1927                 RETURN(ERR_PTR(-EINVAL));
1928
1929         /* Retrieve SELinux status info */
1930         if (sptlrpc_sepol_needs_check(imp_sec))
1931                 rc = sepol_helper(req->rq_import);
1932
1933         if (unlikely(rc == -ENODEV)) {
1934                 CDEBUG(D_SEC,
1935                        "Client cannot report SELinux status, SELinux is disabled.\n");
1936                 RETURN(NULL);
1937         }
1938         if (unlikely(rc))
1939                 RETURN(ERR_PTR(rc));
1940
1941         out = sptlrpc_sepol_get_cached(imp_sec);
1942         if (!out)
1943                 RETURN(ERR_PTR(-ENODATA));
1944
1945         RETURN(out);
1946 }
1947 EXPORT_SYMBOL(sptlrpc_sepol_get);
1948
1949 /*
1950  * server side security
1951  */
1952
1953 static int flavor_allowed(struct sptlrpc_flavor *exp,
1954                           struct ptlrpc_request *req)
1955 {
1956         struct sptlrpc_flavor *flvr = &req->rq_flvr;
1957
1958         if (exp->sf_rpc == SPTLRPC_FLVR_ANY || exp->sf_rpc == flvr->sf_rpc)
1959                 return 1;
1960
1961         if ((req->rq_ctx_init || req->rq_ctx_fini) &&
1962             SPTLRPC_FLVR_POLICY(exp->sf_rpc) ==
1963             SPTLRPC_FLVR_POLICY(flvr->sf_rpc) &&
1964             SPTLRPC_FLVR_MECH(exp->sf_rpc) == SPTLRPC_FLVR_MECH(flvr->sf_rpc))
1965                 return 1;
1966
1967         return 0;
1968 }
1969
1970 #define EXP_FLVR_UPDATE_EXPIRE      (OBD_TIMEOUT_DEFAULT + 10)
1971
1972 /**
1973  * Given an export \a exp, check whether the flavor of incoming \a req
1974  * is allowed by the export \a exp. Main logic is about taking care of
1975  * changing configurations. Return 0 means success.
1976  */
1977 int sptlrpc_target_export_check(struct obd_export *exp,
1978                                 struct ptlrpc_request *req)
1979 {
1980         struct sptlrpc_flavor   flavor;
1981
1982         if (exp == NULL)
1983                 return 0;
1984
1985         /*
1986          * client side export has no imp_reverse, skip
1987          * FIXME maybe we should check flavor this as well???
1988          */
1989         if (exp->exp_imp_reverse == NULL)
1990                 return 0;
1991
1992         /* don't care about ctx fini rpc */
1993         if (req->rq_ctx_fini)
1994                 return 0;
1995
1996         spin_lock(&exp->exp_lock);
1997
1998         /*
1999          * if flavor just changed (exp->exp_flvr_changed != 0), we wait for
2000          * the first req with the new flavor, then treat it as current flavor,
2001          * adapt reverse sec according to it.
2002          * note the first rpc with new flavor might not be with root ctx, in
2003          * which case delay the sec_adapt by leaving exp_flvr_adapt == 1.
2004          */
2005         if (unlikely(exp->exp_flvr_changed) &&
2006             flavor_allowed(&exp->exp_flvr_old[1], req)) {
2007                 /*
2008                  * make the new flavor as "current", and old ones as
2009                  * about-to-expire
2010                  */
2011                 CDEBUG(D_SEC, "exp %p: just changed: %x->%x\n", exp,
2012                        exp->exp_flvr.sf_rpc, exp->exp_flvr_old[1].sf_rpc);
2013                 flavor = exp->exp_flvr_old[1];
2014                 exp->exp_flvr_old[1] = exp->exp_flvr_old[0];
2015                 exp->exp_flvr_expire[1] = exp->exp_flvr_expire[0];
2016                 exp->exp_flvr_old[0] = exp->exp_flvr;
2017                 exp->exp_flvr_expire[0] = ktime_get_real_seconds() +
2018                                           EXP_FLVR_UPDATE_EXPIRE;
2019                 exp->exp_flvr = flavor;
2020
2021                 /* flavor change finished */
2022                 exp->exp_flvr_changed = 0;
2023                 LASSERT(exp->exp_flvr_adapt == 1);
2024
2025                 /* if it's gss, we only interested in root ctx init */
2026                 if (req->rq_auth_gss &&
2027                     !(req->rq_ctx_init &&
2028                     (req->rq_auth_usr_root || req->rq_auth_usr_mdt ||
2029                     req->rq_auth_usr_ost))) {
2030                         spin_unlock(&exp->exp_lock);
2031                         CDEBUG(D_SEC, "is good but not root(%d:%d:%d:%d:%d)\n",
2032                                req->rq_auth_gss, req->rq_ctx_init,
2033                                req->rq_auth_usr_root, req->rq_auth_usr_mdt,
2034                                req->rq_auth_usr_ost);
2035                         return 0;
2036                 }
2037
2038                 exp->exp_flvr_adapt = 0;
2039                 spin_unlock(&exp->exp_lock);
2040
2041                 return sptlrpc_import_sec_adapt(exp->exp_imp_reverse,
2042                                                 req->rq_svc_ctx, &flavor);
2043         }
2044
2045         /*
2046          * if it equals to the current flavor, we accept it, but need to
2047          * dealing with reverse sec/ctx
2048          */
2049         if (likely(flavor_allowed(&exp->exp_flvr, req))) {
2050                 /*
2051                  * most cases should return here, we only interested in
2052                  * gss root ctx init
2053                  */
2054                 if (!req->rq_auth_gss || !req->rq_ctx_init ||
2055                     (!req->rq_auth_usr_root && !req->rq_auth_usr_mdt &&
2056                      !req->rq_auth_usr_ost)) {
2057                         spin_unlock(&exp->exp_lock);
2058                         return 0;
2059                 }
2060
2061                 /*
2062                  * if flavor just changed, we should not proceed, just leave
2063                  * it and current flavor will be discovered and replaced
2064                  * shortly, and let _this_ rpc pass through
2065                  */
2066                 if (exp->exp_flvr_changed) {
2067                         LASSERT(exp->exp_flvr_adapt);
2068                         spin_unlock(&exp->exp_lock);
2069                         return 0;
2070                 }
2071
2072                 if (exp->exp_flvr_adapt) {
2073                         exp->exp_flvr_adapt = 0;
2074                         CDEBUG(D_SEC, "exp %p (%x|%x|%x): do delayed adapt\n",
2075                                exp, exp->exp_flvr.sf_rpc,
2076                                exp->exp_flvr_old[0].sf_rpc,
2077                                exp->exp_flvr_old[1].sf_rpc);
2078                         flavor = exp->exp_flvr;
2079                         spin_unlock(&exp->exp_lock);
2080
2081                         return sptlrpc_import_sec_adapt(exp->exp_imp_reverse,
2082                                                         req->rq_svc_ctx,
2083                                                         &flavor);
2084                 } else {
2085                         CDEBUG(D_SEC,
2086                                "exp %p (%x|%x|%x): is current flavor, install rvs ctx\n",
2087                                exp, exp->exp_flvr.sf_rpc,
2088                                exp->exp_flvr_old[0].sf_rpc,
2089                                exp->exp_flvr_old[1].sf_rpc);
2090                         spin_unlock(&exp->exp_lock);
2091
2092                         return sptlrpc_svc_install_rvs_ctx(exp->exp_imp_reverse,
2093                                                            req->rq_svc_ctx);
2094                 }
2095         }
2096
2097         if (exp->exp_flvr_expire[0]) {
2098                 if (exp->exp_flvr_expire[0] >= ktime_get_real_seconds()) {
2099                         if (flavor_allowed(&exp->exp_flvr_old[0], req)) {
2100                                 CDEBUG(D_SEC,
2101                                        "exp %p (%x|%x|%x): match the middle one (%lld)\n",
2102                                        exp, exp->exp_flvr.sf_rpc,
2103                                        exp->exp_flvr_old[0].sf_rpc,
2104                                        exp->exp_flvr_old[1].sf_rpc,
2105                                        (s64)(exp->exp_flvr_expire[0] -
2106                                              ktime_get_real_seconds()));
2107                                 spin_unlock(&exp->exp_lock);
2108                                 return 0;
2109                         }
2110                 } else {
2111                         CDEBUG(D_SEC, "mark middle expired\n");
2112                         exp->exp_flvr_expire[0] = 0;
2113                 }
2114                 CDEBUG(D_SEC, "exp %p (%x|%x|%x): %x not match middle\n", exp,
2115                        exp->exp_flvr.sf_rpc,
2116                        exp->exp_flvr_old[0].sf_rpc, exp->exp_flvr_old[1].sf_rpc,
2117                        req->rq_flvr.sf_rpc);
2118         }
2119
2120         /*
2121          * now it doesn't match the current flavor, the only chance we can
2122          * accept it is match the old flavors which is not expired.
2123          */
2124         if (exp->exp_flvr_changed == 0 && exp->exp_flvr_expire[1]) {
2125                 if (exp->exp_flvr_expire[1] >= ktime_get_real_seconds()) {
2126                         if (flavor_allowed(&exp->exp_flvr_old[1], req)) {
2127                                 CDEBUG(D_SEC, "exp %p (%x|%x|%x): match the oldest one (%lld)\n",
2128                                        exp,
2129                                        exp->exp_flvr.sf_rpc,
2130                                        exp->exp_flvr_old[0].sf_rpc,
2131                                        exp->exp_flvr_old[1].sf_rpc,
2132                                        (s64)(exp->exp_flvr_expire[1] -
2133                                        ktime_get_real_seconds()));
2134                                 spin_unlock(&exp->exp_lock);
2135                                 return 0;
2136                         }
2137                 } else {
2138                         CDEBUG(D_SEC, "mark oldest expired\n");
2139                         exp->exp_flvr_expire[1] = 0;
2140                 }
2141                 CDEBUG(D_SEC, "exp %p (%x|%x|%x): %x not match found\n",
2142                        exp, exp->exp_flvr.sf_rpc,
2143                        exp->exp_flvr_old[0].sf_rpc, exp->exp_flvr_old[1].sf_rpc,
2144                        req->rq_flvr.sf_rpc);
2145         } else {
2146                 CDEBUG(D_SEC, "exp %p (%x|%x|%x): skip the last one\n",
2147                        exp, exp->exp_flvr.sf_rpc, exp->exp_flvr_old[0].sf_rpc,
2148                        exp->exp_flvr_old[1].sf_rpc);
2149         }
2150
2151         spin_unlock(&exp->exp_lock);
2152
2153         CWARN("exp %p(%s): req %p (%u|%u|%u|%u|%u|%u) with unauthorized flavor %x, expect %x|%x(%+lld)|%x(%+lld)\n",
2154               exp, exp->exp_obd->obd_name,
2155               req, req->rq_auth_gss, req->rq_ctx_init, req->rq_ctx_fini,
2156               req->rq_auth_usr_root, req->rq_auth_usr_mdt, req->rq_auth_usr_ost,
2157               req->rq_flvr.sf_rpc,
2158               exp->exp_flvr.sf_rpc,
2159               exp->exp_flvr_old[0].sf_rpc,
2160               exp->exp_flvr_expire[0] ?
2161               (s64)(exp->exp_flvr_expire[0] - ktime_get_real_seconds()) : 0,
2162               exp->exp_flvr_old[1].sf_rpc,
2163               exp->exp_flvr_expire[1] ?
2164               (s64)(exp->exp_flvr_expire[1] - ktime_get_real_seconds()) : 0);
2165         return -EACCES;
2166 }
2167 EXPORT_SYMBOL(sptlrpc_target_export_check);
2168
2169 void sptlrpc_target_update_exp_flavor(struct obd_device *obd,
2170                                       struct sptlrpc_rule_set *rset)
2171 {
2172         struct obd_export *exp;
2173         struct sptlrpc_flavor new_flvr;
2174
2175         LASSERT(obd);
2176
2177         spin_lock(&obd->obd_dev_lock);
2178
2179         list_for_each_entry(exp, &obd->obd_exports, exp_obd_chain) {
2180                 if (exp->exp_connection == NULL)
2181                         continue;
2182
2183                 /*
2184                  * note if this export had just been updated flavor
2185                  * (exp_flvr_changed == 1), this will override the
2186                  * previous one.
2187                  */
2188                 spin_lock(&exp->exp_lock);
2189                 sptlrpc_target_choose_flavor(rset, exp->exp_sp_peer,
2190                                              &exp->exp_connection->c_peer.nid,
2191                                              &new_flvr);
2192                 if (exp->exp_flvr_changed ||
2193                     !flavor_equal(&new_flvr, &exp->exp_flvr)) {
2194                         exp->exp_flvr_old[1] = new_flvr;
2195                         exp->exp_flvr_expire[1] = 0;
2196                         exp->exp_flvr_changed = 1;
2197                         exp->exp_flvr_adapt = 1;
2198
2199                         CDEBUG(D_SEC, "exp %p (%s): updated flavor %x->%x\n",
2200                                exp, sptlrpc_part2name(exp->exp_sp_peer),
2201                                exp->exp_flvr.sf_rpc,
2202                                exp->exp_flvr_old[1].sf_rpc);
2203                 }
2204                 spin_unlock(&exp->exp_lock);
2205         }
2206
2207         spin_unlock(&obd->obd_dev_lock);
2208 }
2209 EXPORT_SYMBOL(sptlrpc_target_update_exp_flavor);
2210
2211 static int sptlrpc_svc_check_from(struct ptlrpc_request *req, int svc_rc)
2212 {
2213         /* peer's claim is unreliable unless gss is being used */
2214         if (!req->rq_auth_gss || svc_rc == SECSVC_DROP)
2215                 return svc_rc;
2216
2217         switch (req->rq_sp_from) {
2218         case LUSTRE_SP_CLI:
2219                 if (req->rq_auth_usr_mdt || req->rq_auth_usr_ost) {
2220                         /* The below message is checked in sanity-sec test_33 */
2221                         DEBUG_REQ(D_ERROR, req, "faked source CLI");
2222                         svc_rc = SECSVC_DROP;
2223                 }
2224                 break;
2225         case LUSTRE_SP_MDT:
2226                 if (!req->rq_auth_usr_mdt) {
2227                         /* The below message is checked in sanity-sec test_33 */
2228                         DEBUG_REQ(D_ERROR, req, "faked source MDT");
2229                         svc_rc = SECSVC_DROP;
2230                 }
2231                 break;
2232         case LUSTRE_SP_OST:
2233                 if (!req->rq_auth_usr_ost) {
2234                         /* The below message is checked in sanity-sec test_33 */
2235                         DEBUG_REQ(D_ERROR, req, "faked source OST");
2236                         svc_rc = SECSVC_DROP;
2237                 }
2238                 break;
2239         case LUSTRE_SP_MGS:
2240         case LUSTRE_SP_MGC:
2241                 if (!req->rq_auth_usr_root && !req->rq_auth_usr_mdt &&
2242                     !req->rq_auth_usr_ost) {
2243                         /* The below message is checked in sanity-sec test_33 */
2244                         DEBUG_REQ(D_ERROR, req, "faked source MGC/MGS");
2245                         svc_rc = SECSVC_DROP;
2246                 }
2247                 break;
2248         case LUSTRE_SP_ANY:
2249         default:
2250                 DEBUG_REQ(D_ERROR, req, "invalid source %u", req->rq_sp_from);
2251                 svc_rc = SECSVC_DROP;
2252         }
2253
2254         return svc_rc;
2255 }
2256
2257 /**
2258  * Used by ptlrpc server, to perform transformation upon request message of
2259  * incoming \a req. This must be the first thing to do with an incoming
2260  * request in ptlrpc layer.
2261  *
2262  * \retval SECSVC_OK success, and req->rq_reqmsg point to request message in
2263  * clear text, size is req->rq_reqlen; also req->rq_svc_ctx is set.
2264  * \retval SECSVC_COMPLETE success, the request has been fully processed, and
2265  * reply message has been prepared.
2266  * \retval SECSVC_DROP failed, this request should be dropped.
2267  */
2268 int sptlrpc_svc_unwrap_request(struct ptlrpc_request *req)
2269 {
2270         struct ptlrpc_sec_policy *policy;
2271         struct lustre_msg *msg = req->rq_reqbuf;
2272         int rc;
2273
2274         ENTRY;
2275
2276         LASSERT(msg);
2277         LASSERT(req->rq_reqmsg == NULL);
2278         LASSERT(req->rq_repmsg == NULL);
2279         LASSERT(req->rq_svc_ctx == NULL);
2280
2281         req->rq_req_swab_mask = 0;
2282
2283         rc = __lustre_unpack_msg(msg, req->rq_reqdata_len);
2284         switch (rc) {
2285         case 1:
2286                 req_capsule_set_req_swabbed(&req->rq_pill,
2287                                             MSG_PTLRPC_HEADER_OFF);
2288         case 0:
2289                 break;
2290         default:
2291                 CERROR("error unpacking request from %s x%llu\n",
2292                        libcfs_idstr(&req->rq_peer), req->rq_xid);
2293                 RETURN(SECSVC_DROP);
2294         }
2295
2296         req->rq_flvr.sf_rpc = WIRE_FLVR(msg->lm_secflvr);
2297         req->rq_sp_from = LUSTRE_SP_ANY;
2298         req->rq_auth_uid = -1; /* set to INVALID_UID */
2299         req->rq_auth_mapped_uid = -1;
2300
2301         policy = sptlrpc_wireflavor2policy(req->rq_flvr.sf_rpc);
2302         if (!policy) {
2303                 CERROR("unsupported rpc flavor %x\n", req->rq_flvr.sf_rpc);
2304                 RETURN(SECSVC_DROP);
2305         }
2306
2307         LASSERT(policy->sp_sops->accept);
2308         rc = policy->sp_sops->accept(req);
2309         sptlrpc_policy_put(policy);
2310         LASSERT(req->rq_reqmsg || rc != SECSVC_OK);
2311         LASSERT(req->rq_svc_ctx || rc == SECSVC_DROP);
2312
2313         /*
2314          * if it's not null flavor (which means embedded packing msg),
2315          * reset the swab mask for the comming inner msg unpacking.
2316          */
2317         if (SPTLRPC_FLVR_POLICY(req->rq_flvr.sf_rpc) != SPTLRPC_POLICY_NULL)
2318                 req->rq_req_swab_mask = 0;
2319
2320         /* sanity check for the request source */
2321         rc = sptlrpc_svc_check_from(req, rc);
2322         RETURN(rc);
2323 }
2324
2325 /**
2326  * Used by ptlrpc server, to allocate reply buffer for \a req. If succeed,
2327  * req->rq_reply_state is set, and req->rq_reply_state->rs_msg point to
2328  * a buffer of \a msglen size.
2329  */
2330 int sptlrpc_svc_alloc_rs(struct ptlrpc_request *req, int msglen)
2331 {
2332         struct ptlrpc_sec_policy *policy;
2333         struct ptlrpc_reply_state *rs;
2334         int rc;
2335
2336         ENTRY;
2337
2338         LASSERT(req->rq_svc_ctx);
2339         LASSERT(req->rq_svc_ctx->sc_policy);
2340
2341         policy = req->rq_svc_ctx->sc_policy;
2342         LASSERT(policy->sp_sops->alloc_rs);
2343
2344         rc = policy->sp_sops->alloc_rs(req, msglen);
2345         if (unlikely(rc == -ENOMEM)) {
2346                 struct ptlrpc_service_part *svcpt = req->rq_rqbd->rqbd_svcpt;
2347
2348                 if (svcpt->scp_service->srv_max_reply_size <
2349                    msglen + sizeof(struct ptlrpc_reply_state)) {
2350                         /* Just return failure if the size is too big */
2351                         CERROR("size of message is too big (%zd), %d allowed\n",
2352                                 msglen + sizeof(struct ptlrpc_reply_state),
2353                                 svcpt->scp_service->srv_max_reply_size);
2354                         RETURN(-ENOMEM);
2355                 }
2356
2357                 /* failed alloc, try emergency pool */
2358                 rs = lustre_get_emerg_rs(svcpt);
2359                 if (rs == NULL)
2360                         RETURN(-ENOMEM);
2361
2362                 req->rq_reply_state = rs;
2363                 rc = policy->sp_sops->alloc_rs(req, msglen);
2364                 if (rc) {
2365                         lustre_put_emerg_rs(rs);
2366                         req->rq_reply_state = NULL;
2367                 }
2368         }
2369
2370         LASSERT(rc != 0 ||
2371                 (req->rq_reply_state && req->rq_reply_state->rs_msg));
2372
2373         RETURN(rc);
2374 }
2375
2376 /**
2377  * Used by ptlrpc server, to perform transformation upon reply message.
2378  *
2379  * \post req->rq_reply_off is set to approriate server-controlled reply offset.
2380  * \post req->rq_repmsg and req->rq_reply_state->rs_msg becomes inaccessible.
2381  */
2382 int sptlrpc_svc_wrap_reply(struct ptlrpc_request *req)
2383 {
2384         struct ptlrpc_sec_policy *policy;
2385         int rc;
2386
2387         ENTRY;
2388
2389         LASSERT(req->rq_svc_ctx);
2390         LASSERT(req->rq_svc_ctx->sc_policy);
2391
2392         policy = req->rq_svc_ctx->sc_policy;
2393         LASSERT(policy->sp_sops->authorize);
2394
2395         rc = policy->sp_sops->authorize(req);
2396         LASSERT(rc || req->rq_reply_state->rs_repdata_len);
2397
2398         RETURN(rc);
2399 }
2400
2401 /**
2402  * Used by ptlrpc server, to free reply_state.
2403  */
2404 void sptlrpc_svc_free_rs(struct ptlrpc_reply_state *rs)
2405 {
2406         struct ptlrpc_sec_policy *policy;
2407         unsigned int prealloc;
2408
2409         ENTRY;
2410
2411         LASSERT(rs->rs_svc_ctx);
2412         LASSERT(rs->rs_svc_ctx->sc_policy);
2413
2414         policy = rs->rs_svc_ctx->sc_policy;
2415         LASSERT(policy->sp_sops->free_rs);
2416
2417         prealloc = rs->rs_prealloc;
2418         policy->sp_sops->free_rs(rs);
2419
2420         if (prealloc)
2421                 lustre_put_emerg_rs(rs);
2422         EXIT;
2423 }
2424
2425 void sptlrpc_svc_ctx_addref(struct ptlrpc_request *req)
2426 {
2427         struct ptlrpc_svc_ctx *ctx = req->rq_svc_ctx;
2428
2429         if (ctx != NULL)
2430                 atomic_inc(&ctx->sc_refcount);
2431 }
2432
2433 void sptlrpc_svc_ctx_decref(struct ptlrpc_request *req)
2434 {
2435         struct ptlrpc_svc_ctx *ctx = req->rq_svc_ctx;
2436
2437         if (ctx == NULL)
2438                 return;
2439
2440         LASSERT(atomic_read(&(ctx)->sc_refcount) > 0);
2441         if (atomic_dec_and_test(&ctx->sc_refcount)) {
2442                 if (ctx->sc_policy->sp_sops->free_ctx)
2443                         ctx->sc_policy->sp_sops->free_ctx(ctx);
2444         }
2445         req->rq_svc_ctx = NULL;
2446 }
2447
2448 void sptlrpc_svc_ctx_invalidate(struct ptlrpc_request *req)
2449 {
2450         struct ptlrpc_svc_ctx *ctx = req->rq_svc_ctx;
2451
2452         if (ctx == NULL)
2453                 return;
2454
2455         LASSERT(atomic_read(&(ctx)->sc_refcount) > 0);
2456         if (ctx->sc_policy->sp_sops->invalidate_ctx)
2457                 ctx->sc_policy->sp_sops->invalidate_ctx(ctx);
2458 }
2459 EXPORT_SYMBOL(sptlrpc_svc_ctx_invalidate);
2460
2461 /*
2462  * bulk security
2463  */
2464
2465 /**
2466  * Perform transformation upon bulk data pointed by \a desc. This is called
2467  * before transforming the request message.
2468  */
2469 int sptlrpc_cli_wrap_bulk(struct ptlrpc_request *req,
2470                           struct ptlrpc_bulk_desc *desc)
2471 {
2472         struct ptlrpc_cli_ctx *ctx;
2473
2474         LASSERT(req->rq_bulk_read || req->rq_bulk_write);
2475
2476         if (!req->rq_pack_bulk)
2477                 return 0;
2478
2479         ctx = req->rq_cli_ctx;
2480         if (ctx->cc_ops->wrap_bulk)
2481                 return ctx->cc_ops->wrap_bulk(ctx, req, desc);
2482         return 0;
2483 }
2484 EXPORT_SYMBOL(sptlrpc_cli_wrap_bulk);
2485
2486 /**
2487  * This is called after unwrap the reply message.
2488  * return nob of actual plain text size received, or error code.
2489  */
2490 int sptlrpc_cli_unwrap_bulk_read(struct ptlrpc_request *req,
2491                                  struct ptlrpc_bulk_desc *desc,
2492                                  int nob)
2493 {
2494         struct ptlrpc_cli_ctx *ctx;
2495         int rc;
2496
2497         LASSERT(req->rq_bulk_read && !req->rq_bulk_write);
2498
2499         if (!req->rq_pack_bulk)
2500                 return desc->bd_nob_transferred;
2501
2502         ctx = req->rq_cli_ctx;
2503         if (ctx->cc_ops->unwrap_bulk) {
2504                 rc = ctx->cc_ops->unwrap_bulk(ctx, req, desc);
2505                 if (rc < 0)
2506                         return rc;
2507         }
2508         return desc->bd_nob_transferred;
2509 }
2510 EXPORT_SYMBOL(sptlrpc_cli_unwrap_bulk_read);
2511
2512 /**
2513  * This is called after unwrap the reply message.
2514  * return 0 for success or error code.
2515  */
2516 int sptlrpc_cli_unwrap_bulk_write(struct ptlrpc_request *req,
2517                                   struct ptlrpc_bulk_desc *desc)
2518 {
2519         struct ptlrpc_cli_ctx *ctx;
2520         int rc;
2521
2522         LASSERT(!req->rq_bulk_read && req->rq_bulk_write);
2523
2524         if (!req->rq_pack_bulk)
2525                 return 0;
2526
2527         ctx = req->rq_cli_ctx;
2528         if (ctx->cc_ops->unwrap_bulk) {
2529                 rc = ctx->cc_ops->unwrap_bulk(ctx, req, desc);
2530                 if (rc < 0)
2531                         return rc;
2532         }
2533
2534         /*
2535          * if everything is going right, nob should equals to nob_transferred.
2536          * in case of privacy mode, nob_transferred needs to be adjusted.
2537          */
2538         if (desc->bd_nob != desc->bd_nob_transferred) {
2539                 CERROR("nob %d doesn't match transferred nob %d\n",
2540                        desc->bd_nob, desc->bd_nob_transferred);
2541                 return -EPROTO;
2542         }
2543
2544         return 0;
2545 }
2546 EXPORT_SYMBOL(sptlrpc_cli_unwrap_bulk_write);
2547
2548 #ifdef HAVE_SERVER_SUPPORT
2549 /**
2550  * Performe transformation upon outgoing bulk read.
2551  */
2552 int sptlrpc_svc_wrap_bulk(struct ptlrpc_request *req,
2553                           struct ptlrpc_bulk_desc *desc)
2554 {
2555         struct ptlrpc_svc_ctx *ctx;
2556
2557         LASSERT(req->rq_bulk_read);
2558
2559         if (!req->rq_pack_bulk)
2560                 return 0;
2561
2562         ctx = req->rq_svc_ctx;
2563         if (ctx->sc_policy->sp_sops->wrap_bulk)
2564                 return ctx->sc_policy->sp_sops->wrap_bulk(req, desc);
2565
2566         return 0;
2567 }
2568 EXPORT_SYMBOL(sptlrpc_svc_wrap_bulk);
2569
2570 /**
2571  * Performe transformation upon incoming bulk write.
2572  */
2573 int sptlrpc_svc_unwrap_bulk(struct ptlrpc_request *req,
2574                             struct ptlrpc_bulk_desc *desc)
2575 {
2576         struct ptlrpc_svc_ctx *ctx;
2577         int rc;
2578
2579         LASSERT(req->rq_bulk_write);
2580
2581         /*
2582          * if it's in privacy mode, transferred should >= expected; otherwise
2583          * transferred should == expected.
2584          */
2585         if (desc->bd_nob_transferred < desc->bd_nob ||
2586             (desc->bd_nob_transferred > desc->bd_nob &&
2587              SPTLRPC_FLVR_BULK_SVC(req->rq_flvr.sf_rpc) !=
2588              SPTLRPC_BULK_SVC_PRIV)) {
2589                 DEBUG_REQ(D_ERROR, req, "truncated bulk GET %d(%d)",
2590                           desc->bd_nob_transferred, desc->bd_nob);
2591                 return -ETIMEDOUT;
2592         }
2593
2594         if (!req->rq_pack_bulk)
2595                 return 0;
2596
2597         ctx = req->rq_svc_ctx;
2598         if (ctx->sc_policy->sp_sops->unwrap_bulk) {
2599                 rc = ctx->sc_policy->sp_sops->unwrap_bulk(req, desc);
2600                 if (rc)
2601                         CERROR("error unwrap bulk: %d\n", rc);
2602         }
2603
2604         /* return 0 to allow reply be sent */
2605         return 0;
2606 }
2607 EXPORT_SYMBOL(sptlrpc_svc_unwrap_bulk);
2608
2609 /**
2610  * Prepare buffers for incoming bulk write.
2611  */
2612 int sptlrpc_svc_prep_bulk(struct ptlrpc_request *req,
2613                           struct ptlrpc_bulk_desc *desc)
2614 {
2615         struct ptlrpc_svc_ctx *ctx;
2616
2617         LASSERT(req->rq_bulk_write);
2618
2619         if (!req->rq_pack_bulk)
2620                 return 0;
2621
2622         ctx = req->rq_svc_ctx;
2623         if (ctx->sc_policy->sp_sops->prep_bulk)
2624                 return ctx->sc_policy->sp_sops->prep_bulk(req, desc);
2625
2626         return 0;
2627 }
2628 EXPORT_SYMBOL(sptlrpc_svc_prep_bulk);
2629
2630 #endif /* HAVE_SERVER_SUPPORT */
2631
2632 /*
2633  * user descriptor helpers
2634  */
2635
2636 int sptlrpc_current_user_desc_size(void)
2637 {
2638         int ngroups;
2639
2640         ngroups = current_cred()->group_info->ngroups;
2641
2642         if (ngroups > LUSTRE_MAX_GROUPS)
2643                 ngroups = LUSTRE_MAX_GROUPS;
2644         return sptlrpc_user_desc_size(ngroups);
2645 }
2646 EXPORT_SYMBOL(sptlrpc_current_user_desc_size);
2647
2648 int sptlrpc_pack_user_desc(struct lustre_msg *msg, int offset)
2649 {
2650         struct ptlrpc_user_desc *pud;
2651         int ngroups;
2652
2653         pud = lustre_msg_buf(msg, offset, 0);
2654
2655         pud->pud_uid = from_kuid(&init_user_ns, current_uid());
2656         pud->pud_gid = from_kgid(&init_user_ns, current_gid());
2657         pud->pud_fsuid = from_kuid(&init_user_ns, current_fsuid());
2658         pud->pud_fsgid = from_kgid(&init_user_ns, current_fsgid());
2659         pud->pud_cap = ll_capability_u32(current_cap());
2660         pud->pud_ngroups = (msg->lm_buflens[offset] - sizeof(*pud)) / 4;
2661
2662         task_lock(current);
2663         ngroups = current_cred()->group_info->ngroups;
2664         if (pud->pud_ngroups > ngroups)
2665                 pud->pud_ngroups = ngroups;
2666 #ifdef HAVE_GROUP_INFO_GID
2667         memcpy(pud->pud_groups, current_cred()->group_info->gid,
2668                pud->pud_ngroups * sizeof(__u32));
2669 #else /* !HAVE_GROUP_INFO_GID */
2670         memcpy(pud->pud_groups, current_cred()->group_info->blocks[0],
2671                pud->pud_ngroups * sizeof(__u32));
2672 #endif /* HAVE_GROUP_INFO_GID */
2673         task_unlock(current);
2674
2675         return 0;
2676 }
2677 EXPORT_SYMBOL(sptlrpc_pack_user_desc);
2678
2679 int sptlrpc_unpack_user_desc(struct lustre_msg *msg, int offset, int swabbed)
2680 {
2681         struct ptlrpc_user_desc *pud;
2682         int i;
2683
2684         pud = lustre_msg_buf(msg, offset, sizeof(*pud));
2685         if (!pud)
2686                 return -EINVAL;
2687
2688         if (swabbed) {
2689                 __swab32s(&pud->pud_uid);
2690                 __swab32s(&pud->pud_gid);
2691                 __swab32s(&pud->pud_fsuid);
2692                 __swab32s(&pud->pud_fsgid);
2693                 __swab32s(&pud->pud_cap);
2694                 __swab32s(&pud->pud_ngroups);
2695         }
2696
2697         if (pud->pud_ngroups > LUSTRE_MAX_GROUPS) {
2698                 CERROR("%u groups is too large\n", pud->pud_ngroups);
2699                 return -EINVAL;
2700         }
2701
2702         if (sizeof(*pud) + pud->pud_ngroups * sizeof(__u32) >
2703             msg->lm_buflens[offset]) {
2704                 CERROR("%u groups are claimed but bufsize only %u\n",
2705                        pud->pud_ngroups, msg->lm_buflens[offset]);
2706                 return -EINVAL;
2707         }
2708
2709         if (swabbed) {
2710                 for (i = 0; i < pud->pud_ngroups; i++)
2711                         __swab32s(&pud->pud_groups[i]);
2712         }
2713
2714         return 0;
2715 }
2716 EXPORT_SYMBOL(sptlrpc_unpack_user_desc);
2717
2718 /*
2719  * misc helpers
2720  */
2721
2722 const char *sec2target_str(struct ptlrpc_sec *sec)
2723 {
2724         if (!sec || !sec->ps_import || !sec->ps_import->imp_obd)
2725                 return "*";
2726         if (sec_is_reverse(sec))
2727                 return "c";
2728         return obd_uuid2str(&sec->ps_import->imp_obd->u.cli.cl_target_uuid);
2729 }
2730 EXPORT_SYMBOL(sec2target_str);
2731
2732 /*
2733  * return true if the bulk data is protected
2734  */
2735 int sptlrpc_flavor_has_bulk(struct sptlrpc_flavor *flvr)
2736 {
2737         switch (SPTLRPC_FLVR_BULK_SVC(flvr->sf_rpc)) {
2738         case SPTLRPC_BULK_SVC_INTG:
2739         case SPTLRPC_BULK_SVC_PRIV:
2740                 return 1;
2741         default:
2742                 return 0;
2743         }
2744 }
2745 EXPORT_SYMBOL(sptlrpc_flavor_has_bulk);
2746
2747 /*
2748  * crypto API helper/alloc blkciper
2749  */
2750
2751 /*
2752  * initialize/finalize
2753  */
2754
2755 int sptlrpc_init(void)
2756 {
2757         int rc;
2758
2759         rwlock_init(&policy_lock);
2760
2761         rc = sptlrpc_gc_init();
2762         if (rc)
2763                 goto out;
2764
2765         rc = sptlrpc_conf_init();
2766         if (rc)
2767                 goto out_gc;
2768
2769         rc = sptlrpc_enc_pool_init();
2770         if (rc)
2771                 goto out_conf;
2772
2773         rc = sptlrpc_null_init();
2774         if (rc)
2775                 goto out_pool;
2776
2777         rc = sptlrpc_plain_init();
2778         if (rc)
2779                 goto out_null;
2780
2781         rc = sptlrpc_lproc_init();
2782         if (rc)
2783                 goto out_plain;
2784
2785         return 0;
2786
2787 out_plain:
2788         sptlrpc_plain_fini();
2789 out_null:
2790         sptlrpc_null_fini();
2791 out_pool:
2792         sptlrpc_enc_pool_fini();
2793 out_conf:
2794         sptlrpc_conf_fini();
2795 out_gc:
2796         sptlrpc_gc_fini();
2797 out:
2798         return rc;
2799 }
2800
2801 void sptlrpc_fini(void)
2802 {
2803         sptlrpc_lproc_fini();
2804         sptlrpc_plain_fini();
2805         sptlrpc_null_fini();
2806         sptlrpc_enc_pool_fini();
2807         sptlrpc_conf_fini();
2808         sptlrpc_gc_fini();
2809 }