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