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