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