4 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
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.
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).
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
23 * Copyright (c) 2007, 2010, Oracle and/or its affiliates. All rights reserved.
24 * Use is subject to license terms.
26 * Copyright (c) 2011, 2017, Intel Corporation.
29 * This file is part of Lustre, http://www.lustre.org/
33 * Author: Eric Mei <ericm@clusterfs.com>
36 #define DEBUG_SUBSYSTEM S_SEC
38 #include <linux/user_namespace.h>
39 #include <linux/uidgid.h>
40 #include <linux/crypto.h>
41 #include <linux/key.h>
43 #include <libcfs/libcfs.h>
45 #include <obd_class.h>
46 #include <obd_support.h>
47 #include <lustre_net.h>
48 #include <lustre_import.h>
49 #include <lustre_dlm.h>
50 #include <lustre_sec.h>
51 #include <libcfs/libcfs_crypto.h>
53 #include "ptlrpc_internal.h"
55 static int send_sepol;
56 module_param(send_sepol, int, 0644);
57 MODULE_PARM_DESC(send_sepol, "Client sends SELinux policy status");
63 static rwlock_t policy_lock;
64 static struct ptlrpc_sec_policy *policies[SPTLRPC_POLICY_MAX] = {
68 int sptlrpc_register_policy(struct ptlrpc_sec_policy *policy)
70 __u16 number = policy->sp_policy;
72 LASSERT(policy->sp_name);
73 LASSERT(policy->sp_cops);
74 LASSERT(policy->sp_sops);
76 if (number >= SPTLRPC_POLICY_MAX)
79 write_lock(&policy_lock);
80 if (unlikely(policies[number])) {
81 write_unlock(&policy_lock);
84 policies[number] = policy;
85 write_unlock(&policy_lock);
87 CDEBUG(D_SEC, "%s: registered\n", policy->sp_name);
90 EXPORT_SYMBOL(sptlrpc_register_policy);
92 int sptlrpc_unregister_policy(struct ptlrpc_sec_policy *policy)
94 __u16 number = policy->sp_policy;
96 LASSERT(number < SPTLRPC_POLICY_MAX);
98 write_lock(&policy_lock);
99 if (unlikely(policies[number] == NULL)) {
100 write_unlock(&policy_lock);
101 CERROR("%s: already unregistered\n", policy->sp_name);
105 LASSERT(policies[number] == policy);
106 policies[number] = NULL;
107 write_unlock(&policy_lock);
109 CDEBUG(D_SEC, "%s: unregistered\n", policy->sp_name);
112 EXPORT_SYMBOL(sptlrpc_unregister_policy);
115 struct ptlrpc_sec_policy *sptlrpc_wireflavor2policy(__u32 flavor)
117 static DEFINE_MUTEX(load_mutex);
118 struct ptlrpc_sec_policy *policy;
119 __u16 number = SPTLRPC_FLVR_POLICY(flavor);
122 if (number >= SPTLRPC_POLICY_MAX)
126 read_lock(&policy_lock);
127 policy = policies[number];
128 if (policy && !try_module_get(policy->sp_owner))
130 read_unlock(&policy_lock);
132 if (policy != NULL || number != SPTLRPC_POLICY_GSS)
135 /* try to load gss module, happens only if policy at index
136 * SPTLRPC_POLICY_GSS is not already referenced in
137 * global array policies[]
139 mutex_lock(&load_mutex);
140 /* The fact that request_module() returns 0 does not guarantee
141 * the module has done its job. So we must check that the
142 * requested policy is now available. This is done by checking
143 * again for policies[number] in the loop.
145 rc = request_module("ptlrpc_gss");
147 CDEBUG(D_SEC, "module ptlrpc_gss loaded on demand\n");
149 CERROR("Unable to load module ptlrpc_gss: rc %d\n", rc);
150 mutex_unlock(&load_mutex);
156 __u32 sptlrpc_name2flavor_base(const char *name)
158 if (!strcmp(name, "null"))
159 return SPTLRPC_FLVR_NULL;
160 if (!strcmp(name, "plain"))
161 return SPTLRPC_FLVR_PLAIN;
162 if (!strcmp(name, "gssnull"))
163 return SPTLRPC_FLVR_GSSNULL;
164 if (!strcmp(name, "krb5n"))
165 return SPTLRPC_FLVR_KRB5N;
166 if (!strcmp(name, "krb5a"))
167 return SPTLRPC_FLVR_KRB5A;
168 if (!strcmp(name, "krb5i"))
169 return SPTLRPC_FLVR_KRB5I;
170 if (!strcmp(name, "krb5p"))
171 return SPTLRPC_FLVR_KRB5P;
172 if (!strcmp(name, "skn"))
173 return SPTLRPC_FLVR_SKN;
174 if (!strcmp(name, "ska"))
175 return SPTLRPC_FLVR_SKA;
176 if (!strcmp(name, "ski"))
177 return SPTLRPC_FLVR_SKI;
178 if (!strcmp(name, "skpi"))
179 return SPTLRPC_FLVR_SKPI;
181 return SPTLRPC_FLVR_INVALID;
183 EXPORT_SYMBOL(sptlrpc_name2flavor_base);
185 const char *sptlrpc_flavor2name_base(__u32 flvr)
187 __u32 base = SPTLRPC_FLVR_BASE(flvr);
189 if (base == SPTLRPC_FLVR_BASE(SPTLRPC_FLVR_NULL))
191 else if (base == SPTLRPC_FLVR_BASE(SPTLRPC_FLVR_PLAIN))
193 else if (base == SPTLRPC_FLVR_BASE(SPTLRPC_FLVR_GSSNULL))
195 else if (base == SPTLRPC_FLVR_BASE(SPTLRPC_FLVR_KRB5N))
197 else if (base == SPTLRPC_FLVR_BASE(SPTLRPC_FLVR_KRB5A))
199 else if (base == SPTLRPC_FLVR_BASE(SPTLRPC_FLVR_KRB5I))
201 else if (base == SPTLRPC_FLVR_BASE(SPTLRPC_FLVR_KRB5P))
203 else if (base == SPTLRPC_FLVR_BASE(SPTLRPC_FLVR_SKN))
205 else if (base == SPTLRPC_FLVR_BASE(SPTLRPC_FLVR_SKA))
207 else if (base == SPTLRPC_FLVR_BASE(SPTLRPC_FLVR_SKI))
209 else if (base == SPTLRPC_FLVR_BASE(SPTLRPC_FLVR_SKPI))
212 CERROR("invalid wire flavor 0x%x\n", flvr);
215 EXPORT_SYMBOL(sptlrpc_flavor2name_base);
217 char *sptlrpc_flavor2name_bulk(struct sptlrpc_flavor *sf,
218 char *buf, int bufsize)
220 if (SPTLRPC_FLVR_POLICY(sf->sf_rpc) == SPTLRPC_POLICY_PLAIN)
221 snprintf(buf, bufsize, "hash:%s",
222 sptlrpc_get_hash_name(sf->u_bulk.hash.hash_alg));
224 snprintf(buf, bufsize, "%s",
225 sptlrpc_flavor2name_base(sf->sf_rpc));
227 buf[bufsize - 1] = '\0';
230 EXPORT_SYMBOL(sptlrpc_flavor2name_bulk);
232 char *sptlrpc_flavor2name(struct sptlrpc_flavor *sf, char *buf, int bufsize)
236 ln = snprintf(buf, bufsize, "%s", sptlrpc_flavor2name_base(sf->sf_rpc));
239 * currently we don't support customized bulk specification for
240 * flavors other than plain
242 if (SPTLRPC_FLVR_POLICY(sf->sf_rpc) == SPTLRPC_POLICY_PLAIN) {
246 sptlrpc_flavor2name_bulk(sf, bspec + 1, sizeof(bspec) - 1);
247 strncat(buf, bspec, bufsize - ln);
250 buf[bufsize - 1] = '\0';
253 EXPORT_SYMBOL(sptlrpc_flavor2name);
255 char *sptlrpc_secflags2str(__u32 flags, char *buf, int bufsize)
259 if (flags & PTLRPC_SEC_FL_REVERSE)
260 strlcat(buf, "reverse,", bufsize);
261 if (flags & PTLRPC_SEC_FL_ROOTONLY)
262 strlcat(buf, "rootonly,", bufsize);
263 if (flags & PTLRPC_SEC_FL_UDESC)
264 strlcat(buf, "udesc,", bufsize);
265 if (flags & PTLRPC_SEC_FL_BULK)
266 strlcat(buf, "bulk,", bufsize);
268 strlcat(buf, "-,", bufsize);
272 EXPORT_SYMBOL(sptlrpc_secflags2str);
275 * client context APIs
279 struct ptlrpc_cli_ctx *get_my_ctx(struct ptlrpc_sec *sec)
281 struct vfs_cred vcred;
282 int create = 1, remove_dead = 1;
285 LASSERT(sec->ps_policy->sp_cops->lookup_ctx);
287 if (sec->ps_flvr.sf_flags & (PTLRPC_SEC_FL_REVERSE |
288 PTLRPC_SEC_FL_ROOTONLY)) {
291 if (sec->ps_flvr.sf_flags & PTLRPC_SEC_FL_REVERSE) {
296 vcred.vc_uid = from_kuid(&init_user_ns, current_uid());
297 vcred.vc_gid = from_kgid(&init_user_ns, current_gid());
300 return sec->ps_policy->sp_cops->lookup_ctx(sec, &vcred, create,
304 struct ptlrpc_cli_ctx *sptlrpc_cli_ctx_get(struct ptlrpc_cli_ctx *ctx)
306 atomic_inc(&ctx->cc_refcount);
309 EXPORT_SYMBOL(sptlrpc_cli_ctx_get);
311 void sptlrpc_cli_ctx_put(struct ptlrpc_cli_ctx *ctx, int sync)
313 struct ptlrpc_sec *sec = ctx->cc_sec;
316 LASSERT(atomic_read(&(ctx)->cc_refcount) > 0);
318 if (!atomic_dec_and_test(&ctx->cc_refcount))
321 sec->ps_policy->sp_cops->release_ctx(sec, ctx, sync);
323 EXPORT_SYMBOL(sptlrpc_cli_ctx_put);
326 * Expire the client context immediately.
328 * \pre Caller must hold at least 1 reference on the \a ctx.
330 void sptlrpc_cli_ctx_expire(struct ptlrpc_cli_ctx *ctx)
332 LASSERT(ctx->cc_ops->die);
333 ctx->cc_ops->die(ctx, 0);
335 EXPORT_SYMBOL(sptlrpc_cli_ctx_expire);
338 * To wake up the threads who are waiting for this client context. Called
339 * after some status change happened on \a ctx.
341 void sptlrpc_cli_ctx_wakeup(struct ptlrpc_cli_ctx *ctx)
343 struct ptlrpc_request *req, *next;
345 spin_lock(&ctx->cc_lock);
346 list_for_each_entry_safe(req, next, &ctx->cc_req_list,
348 list_del_init(&req->rq_ctx_chain);
349 ptlrpc_client_wake_req(req);
351 spin_unlock(&ctx->cc_lock);
353 EXPORT_SYMBOL(sptlrpc_cli_ctx_wakeup);
355 int sptlrpc_cli_ctx_display(struct ptlrpc_cli_ctx *ctx, char *buf, int bufsize)
357 LASSERT(ctx->cc_ops);
359 if (ctx->cc_ops->display == NULL)
362 return ctx->cc_ops->display(ctx, buf, bufsize);
365 static int import_sec_check_expire(struct obd_import *imp)
369 write_lock(&imp->imp_sec_lock);
370 if (imp->imp_sec_expire &&
371 imp->imp_sec_expire < ktime_get_real_seconds()) {
373 imp->imp_sec_expire = 0;
375 write_unlock(&imp->imp_sec_lock);
380 CDEBUG(D_SEC, "found delayed sec adapt expired, do it now\n");
381 return sptlrpc_import_sec_adapt(imp, NULL, NULL);
385 * Get and validate the client side ptlrpc security facilities from
386 * \a imp. There is a race condition on client reconnect when the import is
387 * being destroyed while there are outstanding client bound requests. In
388 * this case do not output any error messages if import secuity is not
391 * \param[in] imp obd import associated with client
392 * \param[out] sec client side ptlrpc security
394 * \retval 0 if security retrieved successfully
395 * \retval -ve errno if there was a problem
397 static int import_sec_validate_get(struct obd_import *imp,
398 struct ptlrpc_sec **sec)
402 if (unlikely(imp->imp_sec_expire)) {
403 rc = import_sec_check_expire(imp);
408 *sec = sptlrpc_import_sec_ref(imp);
410 /* Only output an error when the import is still active */
411 if (!test_bit(WORK_STRUCT_PENDING_BIT,
412 work_data_bits(&imp->imp_zombie_work)))
413 CERROR("import %p (%s) with no sec\n",
414 imp, ptlrpc_import_state_name(imp->imp_state));
418 if (unlikely((*sec)->ps_dying)) {
419 CERROR("attempt to use dying sec %p\n", sec);
420 sptlrpc_sec_put(*sec);
428 * Given a \a req, find or allocate an appropriate context for it.
429 * \pre req->rq_cli_ctx == NULL.
431 * \retval 0 succeed, and req->rq_cli_ctx is set.
432 * \retval -ev error number, and req->rq_cli_ctx == NULL.
434 int sptlrpc_req_get_ctx(struct ptlrpc_request *req)
436 struct obd_import *imp = req->rq_import;
437 struct ptlrpc_sec *sec;
442 LASSERT(!req->rq_cli_ctx);
445 rc = import_sec_validate_get(imp, &sec);
449 req->rq_cli_ctx = get_my_ctx(sec);
451 sptlrpc_sec_put(sec);
453 if (!req->rq_cli_ctx) {
455 } else if (IS_ERR(req->rq_cli_ctx)) {
456 rc = PTR_ERR(req->rq_cli_ctx);
457 req->rq_cli_ctx = NULL;
461 CERROR("%s: fail to get context for req %p: rc = %d\n",
462 imp->imp_obd->obd_name, req, rc);
468 * Drop the context for \a req.
469 * \pre req->rq_cli_ctx != NULL.
470 * \post req->rq_cli_ctx == NULL.
472 * If \a sync == 0, this function should return quickly without sleep;
473 * otherwise it might trigger and wait for the whole process of sending
474 * an context-destroying rpc to server.
476 void sptlrpc_req_put_ctx(struct ptlrpc_request *req, int sync)
481 LASSERT(req->rq_cli_ctx);
484 * request might be asked to release earlier while still
485 * in the context waiting list.
487 if (!list_empty(&req->rq_ctx_chain)) {
488 spin_lock(&req->rq_cli_ctx->cc_lock);
489 list_del_init(&req->rq_ctx_chain);
490 spin_unlock(&req->rq_cli_ctx->cc_lock);
493 sptlrpc_cli_ctx_put(req->rq_cli_ctx, sync);
494 req->rq_cli_ctx = NULL;
499 int sptlrpc_req_ctx_switch(struct ptlrpc_request *req,
500 struct ptlrpc_cli_ctx *oldctx,
501 struct ptlrpc_cli_ctx *newctx)
503 struct sptlrpc_flavor old_flvr;
504 char *reqmsg = NULL; /* to workaround old gcc */
509 "req %p: switch ctx %p(%u->%s) -> %p(%u->%s), switch sec %p(%s) -> %p(%s)\n",
510 req, oldctx, oldctx->cc_vcred.vc_uid,
511 sec2target_str(oldctx->cc_sec), newctx, newctx->cc_vcred.vc_uid,
512 sec2target_str(newctx->cc_sec), oldctx->cc_sec,
513 oldctx->cc_sec->ps_policy->sp_name, newctx->cc_sec,
514 newctx->cc_sec->ps_policy->sp_name);
517 old_flvr = req->rq_flvr;
519 /* save request message */
520 reqmsg_size = req->rq_reqlen;
521 if (reqmsg_size != 0) {
522 LASSERT(req->rq_reqmsg);
523 OBD_ALLOC_LARGE(reqmsg, reqmsg_size);
526 memcpy(reqmsg, req->rq_reqmsg, reqmsg_size);
529 /* release old req/rep buf */
530 req->rq_cli_ctx = oldctx;
531 sptlrpc_cli_free_reqbuf(req);
532 sptlrpc_cli_free_repbuf(req);
533 req->rq_cli_ctx = newctx;
535 /* recalculate the flavor */
536 sptlrpc_req_set_flavor(req, 0);
539 * alloc new request buffer
540 * we don't need to alloc reply buffer here, leave it to the
541 * rest procedure of ptlrpc
543 if (reqmsg_size != 0) {
544 rc = sptlrpc_cli_alloc_reqbuf(req, reqmsg_size);
546 LASSERT(req->rq_reqmsg);
547 memcpy(req->rq_reqmsg, reqmsg, reqmsg_size);
549 CWARN("failed to alloc reqbuf: %d\n", rc);
550 req->rq_flvr = old_flvr;
553 OBD_FREE_LARGE(reqmsg, reqmsg_size);
559 * If current context of \a req is dead somehow, e.g. we just switched flavor
560 * thus marked original contexts dead, we'll find a new context for it. if
561 * no switch is needed, \a req will end up with the same context.
563 * \note a request must have a context, to keep other parts of code happy.
564 * In any case of failure during the switching, we must restore the old one.
566 int sptlrpc_req_replace_dead_ctx(struct ptlrpc_request *req)
568 struct ptlrpc_cli_ctx *oldctx = req->rq_cli_ctx;
569 struct ptlrpc_cli_ctx *newctx;
576 sptlrpc_cli_ctx_get(oldctx);
577 sptlrpc_req_put_ctx(req, 0);
579 rc = sptlrpc_req_get_ctx(req);
581 LASSERT(!req->rq_cli_ctx);
583 /* restore old ctx */
584 req->rq_cli_ctx = oldctx;
588 newctx = req->rq_cli_ctx;
591 if (unlikely(newctx == oldctx &&
592 test_bit(PTLRPC_CTX_DEAD_BIT, &oldctx->cc_flags))) {
594 * still get the old dead ctx, usually means system too busy
597 "ctx (%p, fl %lx) doesn't switch, relax a little bit\n",
598 newctx, newctx->cc_flags);
600 schedule_timeout_interruptible(cfs_time_seconds(1));
601 } else if (unlikely(test_bit(PTLRPC_CTX_UPTODATE_BIT, &newctx->cc_flags)
604 * new ctx not up to date yet
607 "ctx (%p, fl %lx) doesn't switch, not up to date yet\n",
608 newctx, newctx->cc_flags);
611 * it's possible newctx == oldctx if we're switching
612 * subflavor with the same sec.
614 rc = sptlrpc_req_ctx_switch(req, oldctx, newctx);
616 /* restore old ctx */
617 sptlrpc_req_put_ctx(req, 0);
618 req->rq_cli_ctx = oldctx;
622 LASSERT(req->rq_cli_ctx == newctx);
625 sptlrpc_cli_ctx_put(oldctx, 1);
628 EXPORT_SYMBOL(sptlrpc_req_replace_dead_ctx);
631 int ctx_check_refresh(struct ptlrpc_cli_ctx *ctx)
633 if (cli_ctx_is_refreshed(ctx))
639 void ctx_refresh_interrupt(struct ptlrpc_request *req)
642 spin_lock(&req->rq_lock);
644 spin_unlock(&req->rq_lock);
648 void req_off_ctx_list(struct ptlrpc_request *req, struct ptlrpc_cli_ctx *ctx)
650 spin_lock(&ctx->cc_lock);
651 if (!list_empty(&req->rq_ctx_chain))
652 list_del_init(&req->rq_ctx_chain);
653 spin_unlock(&ctx->cc_lock);
657 * To refresh the context of \req, if it's not up-to-date.
659 * - == 0: do not wait
660 * - == MAX_SCHEDULE_TIMEOUT: wait indefinitely
661 * - > 0: not supported
663 * The status of the context could be subject to be changed by other threads
664 * at any time. We allow this race, but once we return with 0, the caller will
665 * suppose it's uptodated and keep using it until the owning rpc is done.
667 * \retval 0 only if the context is uptodated.
668 * \retval -ev error number.
670 int sptlrpc_req_refresh_ctx(struct ptlrpc_request *req, long timeout)
672 struct ptlrpc_cli_ctx *ctx = req->rq_cli_ctx;
673 struct ptlrpc_sec *sec;
680 if (req->rq_ctx_init || req->rq_ctx_fini)
683 if (timeout != 0 && timeout != MAX_SCHEDULE_TIMEOUT) {
684 CERROR("req %p: invalid timeout %lu\n", req, timeout);
689 * during the process a request's context might change type even
690 * (e.g. from gss ctx to null ctx), so each loop we need to re-check
694 rc = import_sec_validate_get(req->rq_import, &sec);
698 if (sec->ps_flvr.sf_rpc != req->rq_flvr.sf_rpc) {
699 CDEBUG(D_SEC, "req %p: flavor has changed %x -> %x\n",
700 req, req->rq_flvr.sf_rpc, sec->ps_flvr.sf_rpc);
701 req_off_ctx_list(req, ctx);
702 sptlrpc_req_replace_dead_ctx(req);
703 ctx = req->rq_cli_ctx;
705 sptlrpc_sec_put(sec);
707 if (cli_ctx_is_eternal(ctx))
710 if (unlikely(test_bit(PTLRPC_CTX_NEW_BIT, &ctx->cc_flags))) {
711 if (ctx->cc_ops->refresh)
712 ctx->cc_ops->refresh(ctx);
714 LASSERT(test_bit(PTLRPC_CTX_NEW_BIT, &ctx->cc_flags) == 0);
716 LASSERT(ctx->cc_ops->validate);
717 if (ctx->cc_ops->validate(ctx) == 0) {
718 req_off_ctx_list(req, ctx);
722 if (unlikely(test_bit(PTLRPC_CTX_ERROR_BIT, &ctx->cc_flags))) {
723 spin_lock(&req->rq_lock);
725 spin_unlock(&req->rq_lock);
726 req_off_ctx_list(req, ctx);
731 * There's a subtle issue for resending RPCs, suppose following
733 * 1. the request was sent to server.
734 * 2. recovery was kicked start, after finished the request was
736 * 3. resend the request.
737 * 4. old reply from server received, we accept and verify the reply.
738 * this has to be success, otherwise the error will be aware
740 * 5. new reply from server received, dropped by LNet.
742 * Note the xid of old & new request is the same. We can't simply
743 * change xid for the resent request because the server replies on
744 * it for reply reconstruction.
746 * Commonly the original context should be uptodate because we
747 * have an expiry nice time; server will keep its context because
748 * we at least hold a ref of old context which prevent context
749 * from destroying RPC being sent. So server still can accept the
750 * request and finish the RPC. But if that's not the case:
751 * 1. If server side context has been trimmed, a NO_CONTEXT will
752 * be returned, gss_cli_ctx_verify/unseal will switch to new
754 * 2. Current context never be refreshed, then we are fine: we
755 * never really send request with old context before.
757 if (test_bit(PTLRPC_CTX_UPTODATE_BIT, &ctx->cc_flags) &&
758 unlikely(req->rq_reqmsg) &&
759 lustre_msg_get_flags(req->rq_reqmsg) & MSG_RESENT) {
760 req_off_ctx_list(req, ctx);
764 if (unlikely(test_bit(PTLRPC_CTX_DEAD_BIT, &ctx->cc_flags))) {
765 req_off_ctx_list(req, ctx);
767 * don't switch ctx if import was deactivated
769 if (req->rq_import->imp_deactive) {
770 spin_lock(&req->rq_lock);
772 spin_unlock(&req->rq_lock);
776 rc = sptlrpc_req_replace_dead_ctx(req);
778 LASSERT(ctx == req->rq_cli_ctx);
779 CERROR("req %p: failed to replace dead ctx %p: %d\n",
781 spin_lock(&req->rq_lock);
783 spin_unlock(&req->rq_lock);
787 ctx = req->rq_cli_ctx;
792 * Now we're sure this context is during upcall, add myself into
795 spin_lock(&ctx->cc_lock);
796 if (list_empty(&req->rq_ctx_chain))
797 list_add(&req->rq_ctx_chain, &ctx->cc_req_list);
798 spin_unlock(&ctx->cc_lock);
803 /* Clear any flags that may be present from previous sends */
804 LASSERT(req->rq_receiving_reply == 0);
805 spin_lock(&req->rq_lock);
807 req->rq_timedout = 0;
810 spin_unlock(&req->rq_lock);
812 /* by now we know that timeout value is MAX_SCHEDULE_TIMEOUT,
813 * so wait indefinitely with non-fatal signals blocked
815 if (l_wait_event_abortable(req->rq_reply_waitq,
816 ctx_check_refresh(ctx)) == -ERESTARTSYS) {
818 ctx_refresh_interrupt(req);
822 * following cases could lead us here:
823 * - successfully refreshed;
825 * - timedout, and we don't want recover from the failure;
826 * - timedout, and waked up upon recovery finished;
827 * - someone else mark this ctx dead by force;
828 * - someone invalidate the req and call ptlrpc_client_wake_req(),
829 * e.g. ptlrpc_abort_inflight();
831 if (!cli_ctx_is_refreshed(ctx)) {
832 /* timed out or interruptted */
833 req_off_ctx_list(req, ctx);
842 /* Bring ptlrpc_sec context up-to-date */
843 int sptlrpc_export_update_ctx(struct obd_export *exp)
845 struct obd_import *imp = exp ? exp->exp_imp_reverse : NULL;
846 struct ptlrpc_sec *sec = NULL;
847 struct ptlrpc_cli_ctx *ctx = NULL;
851 sec = sptlrpc_import_sec_ref(imp);
853 ctx = get_my_ctx(sec);
856 sptlrpc_sec_put(sec);
860 if (ctx->cc_ops->refresh)
861 rc = ctx->cc_ops->refresh(ctx);
862 sptlrpc_cli_ctx_put(ctx, 1);
868 * Initialize flavor settings for \a req, according to \a opcode.
870 * \note this could be called in two situations:
871 * - new request from ptlrpc_pre_req(), with proper @opcode
872 * - old request which changed ctx in the middle, with @opcode == 0
874 void sptlrpc_req_set_flavor(struct ptlrpc_request *req, int opcode)
876 struct ptlrpc_sec *sec;
878 LASSERT(req->rq_import);
879 LASSERT(req->rq_cli_ctx);
880 LASSERT(req->rq_cli_ctx->cc_sec);
881 LASSERT(req->rq_bulk_read == 0 || req->rq_bulk_write == 0);
883 /* special security flags according to opcode */
887 case MGS_CONFIG_READ:
889 req->rq_bulk_read = 1;
893 req->rq_bulk_write = 1;
896 req->rq_ctx_init = 1;
899 req->rq_ctx_fini = 1;
902 /* init/fini rpc won't be resend, so can't be here */
903 LASSERT(req->rq_ctx_init == 0);
904 LASSERT(req->rq_ctx_fini == 0);
906 /* cleanup flags, which should be recalculated */
907 req->rq_pack_udesc = 0;
908 req->rq_pack_bulk = 0;
912 sec = req->rq_cli_ctx->cc_sec;
914 spin_lock(&sec->ps_lock);
915 req->rq_flvr = sec->ps_flvr;
916 spin_unlock(&sec->ps_lock);
919 * force SVC_NULL for context initiation rpc, SVC_INTG for context
922 if (unlikely(req->rq_ctx_init))
923 flvr_set_svc(&req->rq_flvr.sf_rpc, SPTLRPC_SVC_NULL);
924 else if (unlikely(req->rq_ctx_fini))
925 flvr_set_svc(&req->rq_flvr.sf_rpc, SPTLRPC_SVC_INTG);
927 /* user descriptor flag, null security can't do it anyway */
928 if ((sec->ps_flvr.sf_flags & PTLRPC_SEC_FL_UDESC) &&
929 (req->rq_flvr.sf_rpc != SPTLRPC_FLVR_NULL))
930 req->rq_pack_udesc = 1;
932 /* bulk security flag */
933 if ((req->rq_bulk_read || req->rq_bulk_write) &&
934 sptlrpc_flavor_has_bulk(&req->rq_flvr))
935 req->rq_pack_bulk = 1;
938 void sptlrpc_request_out_callback(struct ptlrpc_request *req)
940 if (SPTLRPC_FLVR_SVC(req->rq_flvr.sf_rpc) != SPTLRPC_SVC_PRIV)
943 LASSERT(req->rq_clrbuf);
944 if (req->rq_pool || !req->rq_reqbuf)
947 OBD_FREE(req->rq_reqbuf, req->rq_reqbuf_len);
948 req->rq_reqbuf = NULL;
949 req->rq_reqbuf_len = 0;
953 * Given an import \a imp, check whether current user has a valid context
954 * or not. We may create a new context and try to refresh it, and try
955 * repeatedly try in case of non-fatal errors. Return 0 means success.
957 int sptlrpc_import_check_ctx(struct obd_import *imp)
959 struct ptlrpc_sec *sec;
960 struct ptlrpc_cli_ctx *ctx;
961 struct ptlrpc_request *req = NULL;
968 sec = sptlrpc_import_sec_ref(imp);
969 ctx = get_my_ctx(sec);
970 sptlrpc_sec_put(sec);
973 RETURN(PTR_ERR(ctx));
977 if (cli_ctx_is_eternal(ctx) ||
978 ctx->cc_ops->validate(ctx) == 0) {
979 sptlrpc_cli_ctx_put(ctx, 1);
983 if (cli_ctx_is_error(ctx)) {
984 sptlrpc_cli_ctx_put(ctx, 1);
988 req = ptlrpc_request_cache_alloc(GFP_NOFS);
992 ptlrpc_cli_req_init(req);
993 atomic_set(&req->rq_refcount, 10000);
995 req->rq_import = imp;
996 req->rq_flvr = sec->ps_flvr;
997 req->rq_cli_ctx = ctx;
999 rc = sptlrpc_req_refresh_ctx(req, MAX_SCHEDULE_TIMEOUT);
1000 LASSERT(list_empty(&req->rq_ctx_chain));
1001 sptlrpc_cli_ctx_put(req->rq_cli_ctx, 1);
1002 ptlrpc_request_cache_free(req);
1008 * Used by ptlrpc client, to perform the pre-defined security transformation
1009 * upon the request message of \a req. After this function called,
1010 * req->rq_reqmsg is still accessible as clear text.
1012 int sptlrpc_cli_wrap_request(struct ptlrpc_request *req)
1014 struct ptlrpc_cli_ctx *ctx = req->rq_cli_ctx;
1020 LASSERT(ctx->cc_sec);
1021 LASSERT(req->rq_reqbuf || req->rq_clrbuf);
1024 * we wrap bulk request here because now we can be sure
1025 * the context is uptodate.
1028 rc = sptlrpc_cli_wrap_bulk(req, req->rq_bulk);
1033 switch (SPTLRPC_FLVR_SVC(req->rq_flvr.sf_rpc)) {
1034 case SPTLRPC_SVC_NULL:
1035 case SPTLRPC_SVC_AUTH:
1036 case SPTLRPC_SVC_INTG:
1037 LASSERT(ctx->cc_ops->sign);
1038 rc = ctx->cc_ops->sign(ctx, req);
1040 case SPTLRPC_SVC_PRIV:
1041 LASSERT(ctx->cc_ops->seal);
1042 rc = ctx->cc_ops->seal(ctx, req);
1049 LASSERT(req->rq_reqdata_len);
1050 LASSERT(req->rq_reqdata_len % 8 == 0);
1051 LASSERT(req->rq_reqdata_len <= req->rq_reqbuf_len);
1057 static int do_cli_unwrap_reply(struct ptlrpc_request *req)
1059 struct ptlrpc_cli_ctx *ctx = req->rq_cli_ctx;
1065 LASSERT(ctx->cc_sec);
1066 LASSERT(req->rq_repbuf);
1067 LASSERT(req->rq_repdata);
1068 LASSERT(req->rq_repmsg == NULL);
1070 req->rq_rep_swab_mask = 0;
1072 rc = __lustre_unpack_msg(req->rq_repdata, req->rq_repdata_len);
1075 req_capsule_set_rep_swabbed(&req->rq_pill,
1076 MSG_PTLRPC_HEADER_OFF);
1080 CERROR("failed unpack reply: x%llu\n", req->rq_xid);
1084 if (req->rq_repdata_len < sizeof(struct lustre_msg)) {
1085 CERROR("replied data length %d too small\n",
1086 req->rq_repdata_len);
1090 if (SPTLRPC_FLVR_POLICY(req->rq_repdata->lm_secflvr) !=
1091 SPTLRPC_FLVR_POLICY(req->rq_flvr.sf_rpc)) {
1092 CERROR("reply policy %u doesn't match request policy %u\n",
1093 SPTLRPC_FLVR_POLICY(req->rq_repdata->lm_secflvr),
1094 SPTLRPC_FLVR_POLICY(req->rq_flvr.sf_rpc));
1098 switch (SPTLRPC_FLVR_SVC(req->rq_flvr.sf_rpc)) {
1099 case SPTLRPC_SVC_NULL:
1100 case SPTLRPC_SVC_AUTH:
1101 case SPTLRPC_SVC_INTG:
1102 LASSERT(ctx->cc_ops->verify);
1103 rc = ctx->cc_ops->verify(ctx, req);
1105 case SPTLRPC_SVC_PRIV:
1106 LASSERT(ctx->cc_ops->unseal);
1107 rc = ctx->cc_ops->unseal(ctx, req);
1112 LASSERT(rc || req->rq_repmsg || req->rq_resend);
1114 if (SPTLRPC_FLVR_POLICY(req->rq_flvr.sf_rpc) != SPTLRPC_POLICY_NULL &&
1116 req->rq_rep_swab_mask = 0;
1121 * Used by ptlrpc client, to perform security transformation upon the reply
1122 * message of \a req. After return successfully, req->rq_repmsg points to
1123 * the reply message in clear text.
1125 * \pre the reply buffer should have been un-posted from LNet, so nothing is
1128 int sptlrpc_cli_unwrap_reply(struct ptlrpc_request *req)
1130 LASSERT(req->rq_repbuf);
1131 LASSERT(req->rq_repdata == NULL);
1132 LASSERT(req->rq_repmsg == NULL);
1133 LASSERT(req->rq_reply_off + req->rq_nob_received <= req->rq_repbuf_len);
1135 if (req->rq_reply_off == 0 &&
1136 (lustre_msghdr_get_flags(req->rq_reqmsg) & MSGHDR_AT_SUPPORT)) {
1137 CERROR("real reply with offset 0\n");
1141 if (req->rq_reply_off % 8 != 0) {
1142 CERROR("reply at odd offset %u\n", req->rq_reply_off);
1146 req->rq_repdata = (struct lustre_msg *)
1147 (req->rq_repbuf + req->rq_reply_off);
1148 req->rq_repdata_len = req->rq_nob_received;
1150 return do_cli_unwrap_reply(req);
1154 * Used by ptlrpc client, to perform security transformation upon the early
1155 * reply message of \a req. We expect the rq_reply_off is 0, and
1156 * rq_nob_received is the early reply size.
1158 * Because the receive buffer might be still posted, the reply data might be
1159 * changed at any time, no matter we're holding rq_lock or not. For this reason
1160 * we allocate a separate ptlrpc_request and reply buffer for early reply
1163 * \retval 0 success, \a req_ret is filled with a duplicated ptlrpc_request.
1164 * Later the caller must call sptlrpc_cli_finish_early_reply() on the returned
1165 * \a *req_ret to release it.
1166 * \retval -ev error number, and \a req_ret will not be set.
1168 int sptlrpc_cli_unwrap_early_reply(struct ptlrpc_request *req,
1169 struct ptlrpc_request **req_ret)
1171 struct ptlrpc_request *early_req;
1173 int early_bufsz, early_size;
1178 early_req = ptlrpc_request_cache_alloc(GFP_NOFS);
1179 if (early_req == NULL)
1182 ptlrpc_cli_req_init(early_req);
1184 early_size = req->rq_nob_received;
1185 early_bufsz = size_roundup_power2(early_size);
1186 OBD_ALLOC_LARGE(early_buf, early_bufsz);
1187 if (early_buf == NULL)
1188 GOTO(err_req, rc = -ENOMEM);
1190 /* sanity checkings and copy data out, do it inside spinlock */
1191 spin_lock(&req->rq_lock);
1193 if (req->rq_replied) {
1194 spin_unlock(&req->rq_lock);
1195 GOTO(err_buf, rc = -EALREADY);
1198 LASSERT(req->rq_repbuf);
1199 LASSERT(req->rq_repdata == NULL);
1200 LASSERT(req->rq_repmsg == NULL);
1202 if (req->rq_reply_off != 0) {
1203 CERROR("early reply with offset %u\n", req->rq_reply_off);
1204 spin_unlock(&req->rq_lock);
1205 GOTO(err_buf, rc = -EPROTO);
1208 if (req->rq_nob_received != early_size) {
1209 /* even another early arrived the size should be the same */
1210 CERROR("data size has changed from %u to %u\n",
1211 early_size, req->rq_nob_received);
1212 spin_unlock(&req->rq_lock);
1213 GOTO(err_buf, rc = -EINVAL);
1216 if (req->rq_nob_received < sizeof(struct lustre_msg)) {
1217 CERROR("early reply length %d too small\n",
1218 req->rq_nob_received);
1219 spin_unlock(&req->rq_lock);
1220 GOTO(err_buf, rc = -EALREADY);
1223 memcpy(early_buf, req->rq_repbuf, early_size);
1224 spin_unlock(&req->rq_lock);
1226 early_req->rq_cli_ctx = sptlrpc_cli_ctx_get(req->rq_cli_ctx);
1227 early_req->rq_flvr = req->rq_flvr;
1228 early_req->rq_repbuf = early_buf;
1229 early_req->rq_repbuf_len = early_bufsz;
1230 early_req->rq_repdata = (struct lustre_msg *) early_buf;
1231 early_req->rq_repdata_len = early_size;
1232 early_req->rq_early = 1;
1233 early_req->rq_reqmsg = req->rq_reqmsg;
1235 rc = do_cli_unwrap_reply(early_req);
1237 DEBUG_REQ(D_ADAPTTO, early_req,
1238 "unwrap early reply: rc = %d", rc);
1242 LASSERT(early_req->rq_repmsg);
1243 *req_ret = early_req;
1247 sptlrpc_cli_ctx_put(early_req->rq_cli_ctx, 1);
1249 OBD_FREE_LARGE(early_buf, early_bufsz);
1251 ptlrpc_request_cache_free(early_req);
1256 * Used by ptlrpc client, to release a processed early reply \a early_req.
1258 * \pre \a early_req was obtained from calling sptlrpc_cli_unwrap_early_reply().
1260 void sptlrpc_cli_finish_early_reply(struct ptlrpc_request *early_req)
1262 LASSERT(early_req->rq_repbuf);
1263 LASSERT(early_req->rq_repdata);
1264 LASSERT(early_req->rq_repmsg);
1266 sptlrpc_cli_ctx_put(early_req->rq_cli_ctx, 1);
1267 OBD_FREE_LARGE(early_req->rq_repbuf, early_req->rq_repbuf_len);
1268 ptlrpc_request_cache_free(early_req);
1271 /**************************************************
1273 **************************************************/
1276 * "fixed" sec (e.g. null) use sec_id < 0
1278 static atomic_t sptlrpc_sec_id = ATOMIC_INIT(1);
1280 int sptlrpc_get_next_secid(void)
1282 return atomic_inc_return(&sptlrpc_sec_id);
1284 EXPORT_SYMBOL(sptlrpc_get_next_secid);
1287 * client side high-level security APIs
1290 static int sec_cop_flush_ctx_cache(struct ptlrpc_sec *sec, uid_t uid,
1291 int grace, int force)
1293 struct ptlrpc_sec_policy *policy = sec->ps_policy;
1295 LASSERT(policy->sp_cops);
1296 LASSERT(policy->sp_cops->flush_ctx_cache);
1298 return policy->sp_cops->flush_ctx_cache(sec, uid, grace, force);
1301 static void sec_cop_destroy_sec(struct ptlrpc_sec *sec)
1303 struct ptlrpc_sec_policy *policy = sec->ps_policy;
1304 struct sptlrpc_sepol *sepol;
1306 LASSERT(atomic_read(&sec->ps_refcount) == 0);
1307 LASSERT(policy->sp_cops->destroy_sec);
1309 CDEBUG(D_SEC, "%s@%p: being destroyed\n", sec->ps_policy->sp_name, sec);
1311 spin_lock(&sec->ps_lock);
1312 sec->ps_sepol_checknext = ktime_set(0, 0);
1313 sepol = rcu_dereference_protected(sec->ps_sepol, 1);
1314 rcu_assign_pointer(sec->ps_sepol, NULL);
1315 spin_unlock(&sec->ps_lock);
1317 sptlrpc_sepol_put(sepol);
1319 policy->sp_cops->destroy_sec(sec);
1320 sptlrpc_policy_put(policy);
1323 void sptlrpc_sec_destroy(struct ptlrpc_sec *sec)
1325 sec_cop_destroy_sec(sec);
1327 EXPORT_SYMBOL(sptlrpc_sec_destroy);
1329 static void sptlrpc_sec_kill(struct ptlrpc_sec *sec)
1331 LASSERT(atomic_read(&(sec)->ps_refcount) > 0);
1333 if (sec->ps_policy->sp_cops->kill_sec) {
1334 sec->ps_policy->sp_cops->kill_sec(sec);
1336 sec_cop_flush_ctx_cache(sec, -1, 1, 1);
1340 struct ptlrpc_sec *sptlrpc_sec_get(struct ptlrpc_sec *sec)
1343 atomic_inc(&sec->ps_refcount);
1347 EXPORT_SYMBOL(sptlrpc_sec_get);
1349 void sptlrpc_sec_put(struct ptlrpc_sec *sec)
1352 LASSERT(atomic_read(&(sec)->ps_refcount) > 0);
1354 if (atomic_dec_and_test(&sec->ps_refcount)) {
1355 sptlrpc_gc_del_sec(sec);
1356 sec_cop_destroy_sec(sec);
1360 EXPORT_SYMBOL(sptlrpc_sec_put);
1363 * policy module is responsible for taking refrence of import
1366 struct ptlrpc_sec * sptlrpc_sec_create(struct obd_import *imp,
1367 struct ptlrpc_svc_ctx *svc_ctx,
1368 struct sptlrpc_flavor *sf,
1369 enum lustre_sec_part sp)
1371 struct ptlrpc_sec_policy *policy;
1372 struct ptlrpc_sec *sec;
1378 LASSERT(imp->imp_dlm_fake == 1);
1380 CDEBUG(D_SEC, "%s %s: reverse sec using flavor %s\n",
1381 imp->imp_obd->obd_type->typ_name,
1382 imp->imp_obd->obd_name,
1383 sptlrpc_flavor2name(sf, str, sizeof(str)));
1385 policy = sptlrpc_policy_get(svc_ctx->sc_policy);
1386 sf->sf_flags |= PTLRPC_SEC_FL_REVERSE | PTLRPC_SEC_FL_ROOTONLY;
1388 LASSERT(imp->imp_dlm_fake == 0);
1390 CDEBUG(D_SEC, "%s %s: select security flavor %s\n",
1391 imp->imp_obd->obd_type->typ_name,
1392 imp->imp_obd->obd_name,
1393 sptlrpc_flavor2name(sf, str, sizeof(str)));
1395 policy = sptlrpc_wireflavor2policy(sf->sf_rpc);
1397 CERROR("invalid flavor 0x%x\n", sf->sf_rpc);
1402 sec = policy->sp_cops->create_sec(imp, svc_ctx, sf);
1404 atomic_inc(&sec->ps_refcount);
1408 if (sec->ps_gc_interval && policy->sp_cops->gc_ctx)
1409 sptlrpc_gc_add_sec(sec);
1411 sptlrpc_policy_put(policy);
1417 static int print_srpc_serverctx_seq(struct obd_export *exp, void *cb_data)
1419 struct seq_file *m = cb_data;
1420 struct obd_import *imp = exp->exp_imp_reverse;
1421 struct ptlrpc_sec *sec = NULL;
1424 sec = sptlrpc_import_sec_ref(imp);
1428 if (sec->ps_policy->sp_cops->display)
1429 sec->ps_policy->sp_cops->display(sec, m);
1431 sptlrpc_sec_put(sec);
1436 int lprocfs_srpc_serverctx_seq_show(struct seq_file *m, void *data)
1438 struct obd_device *obd = m->private;
1439 struct obd_export *exp, *n;
1441 spin_lock(&obd->obd_dev_lock);
1442 list_for_each_entry_safe(exp, n, &obd->obd_exports, exp_obd_chain) {
1443 print_srpc_serverctx_seq(exp, m);
1445 spin_unlock(&obd->obd_dev_lock);
1449 EXPORT_SYMBOL(lprocfs_srpc_serverctx_seq_show);
1451 struct ptlrpc_sec *sptlrpc_import_sec_ref(struct obd_import *imp)
1453 struct ptlrpc_sec *sec;
1455 read_lock(&imp->imp_sec_lock);
1456 sec = sptlrpc_sec_get(imp->imp_sec);
1457 read_unlock(&imp->imp_sec_lock);
1461 EXPORT_SYMBOL(sptlrpc_import_sec_ref);
1463 static void sptlrpc_import_sec_install(struct obd_import *imp,
1464 struct ptlrpc_sec *sec)
1466 struct ptlrpc_sec *old_sec;
1468 LASSERT(atomic_read(&(sec)->ps_refcount) > 0);
1470 write_lock(&imp->imp_sec_lock);
1471 old_sec = imp->imp_sec;
1473 write_unlock(&imp->imp_sec_lock);
1476 sptlrpc_sec_kill(old_sec);
1478 /* balance the ref taken by this import */
1479 sptlrpc_sec_put(old_sec);
1484 int flavor_equal(struct sptlrpc_flavor *sf1, struct sptlrpc_flavor *sf2)
1486 return (memcmp(sf1, sf2, sizeof(*sf1)) == 0);
1490 void flavor_copy(struct sptlrpc_flavor *dst, struct sptlrpc_flavor *src)
1496 * To get an appropriate ptlrpc_sec for the \a imp, according to the current
1497 * configuration. Upon called, imp->imp_sec may or may not be NULL.
1499 * - regular import: \a svc_ctx should be NULL and \a flvr is ignored;
1500 * - reverse import: \a svc_ctx and \a flvr are obtained from incoming request.
1502 int sptlrpc_import_sec_adapt(struct obd_import *imp,
1503 struct ptlrpc_svc_ctx *svc_ctx,
1504 struct sptlrpc_flavor *flvr)
1506 struct ptlrpc_connection *conn;
1507 struct sptlrpc_flavor sf;
1508 struct ptlrpc_sec *sec, *newsec;
1509 enum lustre_sec_part sp;
1520 conn = imp->imp_connection;
1522 if (svc_ctx == NULL) {
1523 struct client_obd *cliobd = &imp->imp_obd->u.cli;
1525 * normal import, determine flavor from rule set, except
1526 * for mgc the flavor is predetermined.
1528 if (cliobd->cl_sp_me == LUSTRE_SP_MGC)
1529 sf = cliobd->cl_flvr_mgc;
1531 sptlrpc_conf_choose_flavor(cliobd->cl_sp_me,
1533 &cliobd->cl_target_uuid,
1534 &conn->c_self, &sf);
1536 sp = imp->imp_obd->u.cli.cl_sp_me;
1538 /* reverse import, determine flavor from incoming reqeust */
1541 if (sf.sf_rpc != SPTLRPC_FLVR_NULL)
1542 sf.sf_flags = PTLRPC_SEC_FL_REVERSE |
1543 PTLRPC_SEC_FL_ROOTONLY;
1545 sp = sptlrpc_target_sec_part(imp->imp_obd);
1548 sec = sptlrpc_import_sec_ref(imp);
1552 if (flavor_equal(&sf, &sec->ps_flvr))
1555 CDEBUG(D_SEC, "import %s->%s: changing flavor %s -> %s\n",
1556 imp->imp_obd->obd_name,
1557 obd_uuid2str(&conn->c_remote_uuid),
1558 sptlrpc_flavor2name(&sec->ps_flvr, str, sizeof(str)),
1559 sptlrpc_flavor2name(&sf, str2, sizeof(str2)));
1560 } else if (SPTLRPC_FLVR_BASE(sf.sf_rpc) !=
1561 SPTLRPC_FLVR_BASE(SPTLRPC_FLVR_NULL)) {
1562 CDEBUG(D_SEC, "import %s->%s netid %x: select flavor %s\n",
1563 imp->imp_obd->obd_name,
1564 obd_uuid2str(&conn->c_remote_uuid),
1565 LNET_NID_NET(&conn->c_self),
1566 sptlrpc_flavor2name(&sf, str, sizeof(str)));
1569 newsec = sptlrpc_sec_create(imp, svc_ctx, &sf, sp);
1571 sptlrpc_import_sec_install(imp, newsec);
1573 CERROR("import %s->%s: failed to create new sec\n",
1574 imp->imp_obd->obd_name,
1575 obd_uuid2str(&conn->c_remote_uuid));
1580 sptlrpc_sec_put(sec);
1584 void sptlrpc_import_sec_put(struct obd_import *imp)
1587 sptlrpc_sec_kill(imp->imp_sec);
1589 sptlrpc_sec_put(imp->imp_sec);
1590 imp->imp_sec = NULL;
1594 static void import_flush_ctx_common(struct obd_import *imp,
1595 uid_t uid, int grace, int force)
1597 struct ptlrpc_sec *sec;
1602 sec = sptlrpc_import_sec_ref(imp);
1606 sec_cop_flush_ctx_cache(sec, uid, grace, force);
1607 sptlrpc_sec_put(sec);
1610 void sptlrpc_import_flush_root_ctx(struct obd_import *imp)
1613 * it's important to use grace mode, see explain in
1614 * sptlrpc_req_refresh_ctx()
1616 import_flush_ctx_common(imp, 0, 1, 1);
1619 void sptlrpc_import_flush_my_ctx(struct obd_import *imp)
1621 import_flush_ctx_common(imp, from_kuid(&init_user_ns, current_uid()),
1624 EXPORT_SYMBOL(sptlrpc_import_flush_my_ctx);
1626 void sptlrpc_import_flush_all_ctx(struct obd_import *imp)
1628 import_flush_ctx_common(imp, -1, 1, 1);
1630 EXPORT_SYMBOL(sptlrpc_import_flush_all_ctx);
1633 * Used by ptlrpc client to allocate request buffer of \a req. Upon return
1634 * successfully, req->rq_reqmsg points to a buffer with size \a msgsize.
1636 int sptlrpc_cli_alloc_reqbuf(struct ptlrpc_request *req, int msgsize)
1638 struct ptlrpc_cli_ctx *ctx = req->rq_cli_ctx;
1639 struct ptlrpc_sec_policy *policy;
1643 LASSERT(ctx->cc_sec);
1644 LASSERT(ctx->cc_sec->ps_policy);
1645 LASSERT(req->rq_reqmsg == NULL);
1646 LASSERT(atomic_read(&(ctx)->cc_refcount) > 0);
1648 policy = ctx->cc_sec->ps_policy;
1649 rc = policy->sp_cops->alloc_reqbuf(ctx->cc_sec, req, msgsize);
1651 LASSERT(req->rq_reqmsg);
1652 LASSERT(req->rq_reqbuf || req->rq_clrbuf);
1654 /* zeroing preallocated buffer */
1656 memset(req->rq_reqmsg, 0, msgsize);
1663 * Used by ptlrpc client to free request buffer of \a req. After this
1664 * req->rq_reqmsg is set to NULL and should not be accessed anymore.
1666 void sptlrpc_cli_free_reqbuf(struct ptlrpc_request *req)
1668 struct ptlrpc_cli_ctx *ctx = req->rq_cli_ctx;
1669 struct ptlrpc_sec_policy *policy;
1672 LASSERT(ctx->cc_sec);
1673 LASSERT(ctx->cc_sec->ps_policy);
1674 LASSERT(atomic_read(&(ctx)->cc_refcount) > 0);
1676 if (req->rq_reqbuf == NULL && req->rq_clrbuf == NULL)
1679 policy = ctx->cc_sec->ps_policy;
1680 policy->sp_cops->free_reqbuf(ctx->cc_sec, req);
1681 req->rq_reqmsg = NULL;
1685 * NOTE caller must guarantee the buffer size is enough for the enlargement
1687 void _sptlrpc_enlarge_msg_inplace(struct lustre_msg *msg,
1688 int segment, int newsize)
1691 int oldsize, oldmsg_size, movesize;
1693 LASSERT(segment < msg->lm_bufcount);
1694 LASSERT(msg->lm_buflens[segment] <= newsize);
1696 if (msg->lm_buflens[segment] == newsize)
1699 /* nothing to do if we are enlarging the last segment */
1700 if (segment == msg->lm_bufcount - 1) {
1701 msg->lm_buflens[segment] = newsize;
1705 oldsize = msg->lm_buflens[segment];
1707 src = lustre_msg_buf(msg, segment + 1, 0);
1708 msg->lm_buflens[segment] = newsize;
1709 dst = lustre_msg_buf(msg, segment + 1, 0);
1710 msg->lm_buflens[segment] = oldsize;
1712 /* move from segment + 1 to end segment */
1713 LASSERT(msg->lm_magic == LUSTRE_MSG_MAGIC_V2);
1714 oldmsg_size = lustre_msg_size_v2(msg->lm_bufcount, msg->lm_buflens);
1715 movesize = oldmsg_size - ((unsigned long) src - (unsigned long) msg);
1716 LASSERT(movesize >= 0);
1719 memmove(dst, src, movesize);
1721 /* note we don't clear the ares where old data live, not secret */
1723 /* finally set new segment size */
1724 msg->lm_buflens[segment] = newsize;
1726 EXPORT_SYMBOL(_sptlrpc_enlarge_msg_inplace);
1729 * Used by ptlrpc client to enlarge the \a segment of request message pointed
1730 * by req->rq_reqmsg to size \a newsize, all previously filled-in data will be
1731 * preserved after the enlargement. this must be called after original request
1732 * buffer being allocated.
1734 * \note after this be called, rq_reqmsg and rq_reqlen might have been changed,
1735 * so caller should refresh its local pointers if needed.
1737 int sptlrpc_cli_enlarge_reqbuf(struct ptlrpc_request *req,
1738 const struct req_msg_field *field,
1741 struct req_capsule *pill = &req->rq_pill;
1742 struct ptlrpc_cli_ctx *ctx = req->rq_cli_ctx;
1743 struct ptlrpc_sec_cops *cops;
1744 struct lustre_msg *msg = req->rq_reqmsg;
1745 int segment = __req_capsule_offset(pill, field, RCL_CLIENT);
1749 LASSERT(msg->lm_bufcount > segment);
1750 LASSERT(msg->lm_buflens[segment] <= newsize);
1752 if (msg->lm_buflens[segment] == newsize)
1755 cops = ctx->cc_sec->ps_policy->sp_cops;
1756 LASSERT(cops->enlarge_reqbuf);
1757 return cops->enlarge_reqbuf(ctx->cc_sec, req, segment, newsize);
1759 EXPORT_SYMBOL(sptlrpc_cli_enlarge_reqbuf);
1762 * Used by ptlrpc client to allocate reply buffer of \a req.
1764 * \note After this, req->rq_repmsg is still not accessible.
1766 int sptlrpc_cli_alloc_repbuf(struct ptlrpc_request *req, int msgsize)
1768 struct ptlrpc_cli_ctx *ctx = req->rq_cli_ctx;
1769 struct ptlrpc_sec_policy *policy;
1774 LASSERT(ctx->cc_sec);
1775 LASSERT(ctx->cc_sec->ps_policy);
1780 policy = ctx->cc_sec->ps_policy;
1781 RETURN(policy->sp_cops->alloc_repbuf(ctx->cc_sec, req, msgsize));
1785 * Used by ptlrpc client to free reply buffer of \a req. After this
1786 * req->rq_repmsg is set to NULL and should not be accessed anymore.
1788 void sptlrpc_cli_free_repbuf(struct ptlrpc_request *req)
1790 struct ptlrpc_cli_ctx *ctx = req->rq_cli_ctx;
1791 struct ptlrpc_sec_policy *policy;
1796 LASSERT(ctx->cc_sec);
1797 LASSERT(ctx->cc_sec->ps_policy);
1798 LASSERT(atomic_read(&(ctx)->cc_refcount) > 0);
1800 if (req->rq_repbuf == NULL)
1802 LASSERT(req->rq_repbuf_len);
1804 policy = ctx->cc_sec->ps_policy;
1805 policy->sp_cops->free_repbuf(ctx->cc_sec, req);
1806 req->rq_repmsg = NULL;
1809 EXPORT_SYMBOL(sptlrpc_cli_free_repbuf);
1811 int sptlrpc_cli_install_rvs_ctx(struct obd_import *imp,
1812 struct ptlrpc_cli_ctx *ctx)
1814 struct ptlrpc_sec_policy *policy = ctx->cc_sec->ps_policy;
1816 if (!policy->sp_cops->install_rctx)
1818 return policy->sp_cops->install_rctx(imp, ctx->cc_sec, ctx);
1821 int sptlrpc_svc_install_rvs_ctx(struct obd_import *imp,
1822 struct ptlrpc_svc_ctx *ctx)
1824 struct ptlrpc_sec_policy *policy = ctx->sc_policy;
1826 if (!policy->sp_sops->install_rctx)
1828 return policy->sp_sops->install_rctx(imp, ctx);
1832 /* Get SELinux policy info from userspace */
1833 static int sepol_helper(struct obd_import *imp)
1835 char mtime_str[21] = { 0 }, mode_str[2] = { 0 };
1837 [0] = "/usr/sbin/l_getsepol",
1839 [2] = NULL, /* obd type */
1841 [4] = NULL, /* obd name */
1843 [6] = mtime_str, /* policy mtime */
1845 [8] = mode_str, /* enforcing mode */
1848 struct sptlrpc_sepol *sepol;
1851 [1] = "PATH=/sbin:/usr/sbin",
1857 if (imp == NULL || imp->imp_obd == NULL ||
1858 imp->imp_obd->obd_type == NULL)
1861 argv[2] = (char *)imp->imp_obd->obd_type->typ_name;
1862 argv[4] = imp->imp_obd->obd_name;
1865 sepol = rcu_dereference(imp->imp_sec->ps_sepol);
1867 /* ps_sepol has not been initialized */
1873 mtime_ms = ktime_to_ms(sepol->ssp_mtime);
1874 snprintf(mtime_str, sizeof(mtime_str), "%lld",
1875 mtime_ms / MSEC_PER_SEC);
1876 if (sepol->ssp_sepol_size > 1)
1877 mode_str[0] = sepol->ssp_sepol[0];
1881 ret = call_usermodehelper(argv[0], argv, envp, UMH_WAIT_PROC);
1887 static inline int sptlrpc_sepol_needs_check(struct ptlrpc_sec *imp_sec)
1891 if (send_sepol == 0)
1894 if (send_sepol == -1)
1895 /* send_sepol == -1 means fetch sepol status every time */
1898 spin_lock(&imp_sec->ps_lock);
1899 checknext = imp_sec->ps_sepol_checknext;
1900 spin_unlock(&imp_sec->ps_lock);
1902 /* next check is too far in time, please update */
1903 if (ktime_after(checknext,
1904 ktime_add(ktime_get(), ktime_set(send_sepol, 0))))
1907 if (ktime_before(ktime_get(), checknext))
1908 /* too early to fetch sepol status */
1912 /* define new sepol_checknext time */
1913 spin_lock(&imp_sec->ps_lock);
1914 imp_sec->ps_sepol_checknext = ktime_add(ktime_get(),
1915 ktime_set(send_sepol, 0));
1916 spin_unlock(&imp_sec->ps_lock);
1921 static void sptlrpc_sepol_release(struct kref *ref)
1923 struct sptlrpc_sepol *p = container_of(ref, struct sptlrpc_sepol,
1925 kfree_rcu(p, ssp_rcu);
1928 void sptlrpc_sepol_put(struct sptlrpc_sepol *pol)
1932 kref_put(&pol->ssp_ref, sptlrpc_sepol_release);
1934 EXPORT_SYMBOL(sptlrpc_sepol_put);
1936 struct sptlrpc_sepol *sptlrpc_sepol_get_cached(struct ptlrpc_sec *imp_sec)
1938 struct sptlrpc_sepol *p;
1942 p = rcu_dereference(imp_sec->ps_sepol);
1943 if (p && !kref_get_unless_zero(&p->ssp_ref)) {
1951 EXPORT_SYMBOL(sptlrpc_sepol_get_cached);
1953 struct sptlrpc_sepol *sptlrpc_sepol_get(struct ptlrpc_request *req)
1955 struct ptlrpc_sec *imp_sec = req->rq_import->imp_sec;
1956 struct sptlrpc_sepol *out;
1961 #ifndef HAVE_SELINUX
1962 if (unlikely(send_sepol != 0))
1964 "Client cannot report SELinux status, it was not built against libselinux.\n");
1968 if (send_sepol == 0)
1971 if (imp_sec == NULL)
1972 RETURN(ERR_PTR(-EINVAL));
1974 /* Retrieve SELinux status info */
1975 if (sptlrpc_sepol_needs_check(imp_sec))
1976 rc = sepol_helper(req->rq_import);
1978 if (unlikely(rc == -ENODEV)) {
1980 "Client cannot report SELinux status, SELinux is disabled.\n");
1984 RETURN(ERR_PTR(rc));
1986 out = sptlrpc_sepol_get_cached(imp_sec);
1988 RETURN(ERR_PTR(-ENODATA));
1992 EXPORT_SYMBOL(sptlrpc_sepol_get);
1995 * server side security
1998 static int flavor_allowed(struct sptlrpc_flavor *exp,
1999 struct ptlrpc_request *req)
2001 struct sptlrpc_flavor *flvr = &req->rq_flvr;
2003 if (exp->sf_rpc == SPTLRPC_FLVR_ANY || exp->sf_rpc == flvr->sf_rpc)
2006 if ((req->rq_ctx_init || req->rq_ctx_fini) &&
2007 SPTLRPC_FLVR_POLICY(exp->sf_rpc) ==
2008 SPTLRPC_FLVR_POLICY(flvr->sf_rpc) &&
2009 SPTLRPC_FLVR_MECH(exp->sf_rpc) == SPTLRPC_FLVR_MECH(flvr->sf_rpc))
2015 #define EXP_FLVR_UPDATE_EXPIRE (OBD_TIMEOUT_DEFAULT + 10)
2018 * Given an export \a exp, check whether the flavor of incoming \a req
2019 * is allowed by the export \a exp. Main logic is about taking care of
2020 * changing configurations. Return 0 means success.
2022 int sptlrpc_target_export_check(struct obd_export *exp,
2023 struct ptlrpc_request *req)
2025 struct sptlrpc_flavor flavor;
2031 * client side export has no imp_reverse, skip
2032 * FIXME maybe we should check flavor this as well???
2034 if (exp->exp_imp_reverse == NULL)
2037 /* don't care about ctx fini rpc */
2038 if (req->rq_ctx_fini)
2041 spin_lock(&exp->exp_lock);
2044 * if flavor just changed (exp->exp_flvr_changed != 0), we wait for
2045 * the first req with the new flavor, then treat it as current flavor,
2046 * adapt reverse sec according to it.
2047 * note the first rpc with new flavor might not be with root ctx, in
2048 * which case delay the sec_adapt by leaving exp_flvr_adapt == 1.
2050 if (unlikely(exp->exp_flvr_changed) &&
2051 flavor_allowed(&exp->exp_flvr_old[1], req)) {
2053 * make the new flavor as "current", and old ones as
2056 CDEBUG(D_SEC, "exp %p: just changed: %x->%x\n", exp,
2057 exp->exp_flvr.sf_rpc, exp->exp_flvr_old[1].sf_rpc);
2058 flavor = exp->exp_flvr_old[1];
2059 exp->exp_flvr_old[1] = exp->exp_flvr_old[0];
2060 exp->exp_flvr_expire[1] = exp->exp_flvr_expire[0];
2061 exp->exp_flvr_old[0] = exp->exp_flvr;
2062 exp->exp_flvr_expire[0] = ktime_get_real_seconds() +
2063 EXP_FLVR_UPDATE_EXPIRE;
2064 exp->exp_flvr = flavor;
2066 /* flavor change finished */
2067 exp->exp_flvr_changed = 0;
2068 LASSERT(exp->exp_flvr_adapt == 1);
2070 /* if it's gss, we only interested in root ctx init */
2071 if (req->rq_auth_gss &&
2072 !(req->rq_ctx_init &&
2073 (req->rq_auth_usr_root || req->rq_auth_usr_mdt ||
2074 req->rq_auth_usr_ost))) {
2075 spin_unlock(&exp->exp_lock);
2076 CDEBUG(D_SEC, "is good but not root(%d:%d:%d:%d:%d)\n",
2077 req->rq_auth_gss, req->rq_ctx_init,
2078 req->rq_auth_usr_root, req->rq_auth_usr_mdt,
2079 req->rq_auth_usr_ost);
2083 exp->exp_flvr_adapt = 0;
2084 spin_unlock(&exp->exp_lock);
2086 return sptlrpc_import_sec_adapt(exp->exp_imp_reverse,
2087 req->rq_svc_ctx, &flavor);
2091 * if it equals to the current flavor, we accept it, but need to
2092 * dealing with reverse sec/ctx
2094 if (likely(flavor_allowed(&exp->exp_flvr, req))) {
2096 * most cases should return here, we only interested in
2099 if (!req->rq_auth_gss || !req->rq_ctx_init ||
2100 (!req->rq_auth_usr_root && !req->rq_auth_usr_mdt &&
2101 !req->rq_auth_usr_ost)) {
2102 spin_unlock(&exp->exp_lock);
2107 * if flavor just changed, we should not proceed, just leave
2108 * it and current flavor will be discovered and replaced
2109 * shortly, and let _this_ rpc pass through
2111 if (exp->exp_flvr_changed) {
2112 LASSERT(exp->exp_flvr_adapt);
2113 spin_unlock(&exp->exp_lock);
2117 if (exp->exp_flvr_adapt) {
2118 exp->exp_flvr_adapt = 0;
2119 CDEBUG(D_SEC, "exp %p (%x|%x|%x): do delayed adapt\n",
2120 exp, exp->exp_flvr.sf_rpc,
2121 exp->exp_flvr_old[0].sf_rpc,
2122 exp->exp_flvr_old[1].sf_rpc);
2123 flavor = exp->exp_flvr;
2124 spin_unlock(&exp->exp_lock);
2126 return sptlrpc_import_sec_adapt(exp->exp_imp_reverse,
2131 "exp %p (%x|%x|%x): is current flavor, install rvs ctx\n",
2132 exp, exp->exp_flvr.sf_rpc,
2133 exp->exp_flvr_old[0].sf_rpc,
2134 exp->exp_flvr_old[1].sf_rpc);
2135 spin_unlock(&exp->exp_lock);
2137 return sptlrpc_svc_install_rvs_ctx(exp->exp_imp_reverse,
2142 if (exp->exp_flvr_expire[0]) {
2143 if (exp->exp_flvr_expire[0] >= ktime_get_real_seconds()) {
2144 if (flavor_allowed(&exp->exp_flvr_old[0], req)) {
2146 "exp %p (%x|%x|%x): match the middle one (%lld)\n",
2147 exp, exp->exp_flvr.sf_rpc,
2148 exp->exp_flvr_old[0].sf_rpc,
2149 exp->exp_flvr_old[1].sf_rpc,
2150 (s64)(exp->exp_flvr_expire[0] -
2151 ktime_get_real_seconds()));
2152 spin_unlock(&exp->exp_lock);
2156 CDEBUG(D_SEC, "mark middle expired\n");
2157 exp->exp_flvr_expire[0] = 0;
2159 CDEBUG(D_SEC, "exp %p (%x|%x|%x): %x not match middle\n", exp,
2160 exp->exp_flvr.sf_rpc,
2161 exp->exp_flvr_old[0].sf_rpc, exp->exp_flvr_old[1].sf_rpc,
2162 req->rq_flvr.sf_rpc);
2166 * now it doesn't match the current flavor, the only chance we can
2167 * accept it is match the old flavors which is not expired.
2169 if (exp->exp_flvr_changed == 0 && exp->exp_flvr_expire[1]) {
2170 if (exp->exp_flvr_expire[1] >= ktime_get_real_seconds()) {
2171 if (flavor_allowed(&exp->exp_flvr_old[1], req)) {
2172 CDEBUG(D_SEC, "exp %p (%x|%x|%x): match the oldest one (%lld)\n",
2174 exp->exp_flvr.sf_rpc,
2175 exp->exp_flvr_old[0].sf_rpc,
2176 exp->exp_flvr_old[1].sf_rpc,
2177 (s64)(exp->exp_flvr_expire[1] -
2178 ktime_get_real_seconds()));
2179 spin_unlock(&exp->exp_lock);
2183 CDEBUG(D_SEC, "mark oldest expired\n");
2184 exp->exp_flvr_expire[1] = 0;
2186 CDEBUG(D_SEC, "exp %p (%x|%x|%x): %x not match found\n",
2187 exp, exp->exp_flvr.sf_rpc,
2188 exp->exp_flvr_old[0].sf_rpc, exp->exp_flvr_old[1].sf_rpc,
2189 req->rq_flvr.sf_rpc);
2191 CDEBUG(D_SEC, "exp %p (%x|%x|%x): skip the last one\n",
2192 exp, exp->exp_flvr.sf_rpc, exp->exp_flvr_old[0].sf_rpc,
2193 exp->exp_flvr_old[1].sf_rpc);
2196 spin_unlock(&exp->exp_lock);
2198 CWARN("exp %p(%s): req %p (%u|%u|%u|%u|%u|%u) with unauthorized flavor %x, expect %x|%x(%+lld)|%x(%+lld)\n",
2199 exp, exp->exp_obd->obd_name,
2200 req, req->rq_auth_gss, req->rq_ctx_init, req->rq_ctx_fini,
2201 req->rq_auth_usr_root, req->rq_auth_usr_mdt, req->rq_auth_usr_ost,
2202 req->rq_flvr.sf_rpc,
2203 exp->exp_flvr.sf_rpc,
2204 exp->exp_flvr_old[0].sf_rpc,
2205 exp->exp_flvr_expire[0] ?
2206 (s64)(exp->exp_flvr_expire[0] - ktime_get_real_seconds()) : 0,
2207 exp->exp_flvr_old[1].sf_rpc,
2208 exp->exp_flvr_expire[1] ?
2209 (s64)(exp->exp_flvr_expire[1] - ktime_get_real_seconds()) : 0);
2212 EXPORT_SYMBOL(sptlrpc_target_export_check);
2214 void sptlrpc_target_update_exp_flavor(struct obd_device *obd,
2215 struct sptlrpc_rule_set *rset)
2217 struct obd_export *exp;
2218 struct sptlrpc_flavor new_flvr;
2222 spin_lock(&obd->obd_dev_lock);
2224 list_for_each_entry(exp, &obd->obd_exports, exp_obd_chain) {
2225 if (exp->exp_connection == NULL)
2229 * note if this export had just been updated flavor
2230 * (exp_flvr_changed == 1), this will override the
2233 spin_lock(&exp->exp_lock);
2234 sptlrpc_target_choose_flavor(rset, exp->exp_sp_peer,
2235 &exp->exp_connection->c_peer.nid,
2237 if (exp->exp_flvr_changed ||
2238 !flavor_equal(&new_flvr, &exp->exp_flvr)) {
2239 exp->exp_flvr_old[1] = new_flvr;
2240 exp->exp_flvr_expire[1] = 0;
2241 exp->exp_flvr_changed = 1;
2242 exp->exp_flvr_adapt = 1;
2244 CDEBUG(D_SEC, "exp %p (%s): updated flavor %x->%x\n",
2245 exp, sptlrpc_part2name(exp->exp_sp_peer),
2246 exp->exp_flvr.sf_rpc,
2247 exp->exp_flvr_old[1].sf_rpc);
2249 spin_unlock(&exp->exp_lock);
2252 spin_unlock(&obd->obd_dev_lock);
2254 EXPORT_SYMBOL(sptlrpc_target_update_exp_flavor);
2256 static int sptlrpc_svc_check_from(struct ptlrpc_request *req, int svc_rc)
2258 /* peer's claim is unreliable unless gss is being used */
2259 if (!req->rq_auth_gss || svc_rc == SECSVC_DROP)
2262 switch (req->rq_sp_from) {
2264 if (req->rq_auth_usr_mdt || req->rq_auth_usr_ost) {
2265 /* The below message is checked in sanity-sec test_33 */
2266 DEBUG_REQ(D_ERROR, req, "faked source CLI");
2267 svc_rc = SECSVC_DROP;
2271 if (!req->rq_auth_usr_mdt) {
2272 /* The below message is checked in sanity-sec test_33 */
2273 DEBUG_REQ(D_ERROR, req, "faked source MDT");
2274 svc_rc = SECSVC_DROP;
2278 if (!req->rq_auth_usr_ost) {
2279 /* The below message is checked in sanity-sec test_33 */
2280 DEBUG_REQ(D_ERROR, req, "faked source OST");
2281 svc_rc = SECSVC_DROP;
2286 if (!req->rq_auth_usr_root && !req->rq_auth_usr_mdt &&
2287 !req->rq_auth_usr_ost) {
2288 /* The below message is checked in sanity-sec test_33 */
2289 DEBUG_REQ(D_ERROR, req, "faked source MGC/MGS");
2290 svc_rc = SECSVC_DROP;
2295 DEBUG_REQ(D_ERROR, req, "invalid source %u", req->rq_sp_from);
2296 svc_rc = SECSVC_DROP;
2303 * Used by ptlrpc server, to perform transformation upon request message of
2304 * incoming \a req. This must be the first thing to do with an incoming
2305 * request in ptlrpc layer.
2307 * \retval SECSVC_OK success, and req->rq_reqmsg point to request message in
2308 * clear text, size is req->rq_reqlen; also req->rq_svc_ctx is set.
2309 * \retval SECSVC_COMPLETE success, the request has been fully processed, and
2310 * reply message has been prepared.
2311 * \retval SECSVC_DROP failed, this request should be dropped.
2313 int sptlrpc_svc_unwrap_request(struct ptlrpc_request *req)
2315 struct ptlrpc_sec_policy *policy;
2316 struct lustre_msg *msg = req->rq_reqbuf;
2322 LASSERT(req->rq_reqmsg == NULL);
2323 LASSERT(req->rq_repmsg == NULL);
2324 LASSERT(req->rq_svc_ctx == NULL);
2326 req->rq_req_swab_mask = 0;
2328 rc = __lustre_unpack_msg(msg, req->rq_reqdata_len);
2331 req_capsule_set_req_swabbed(&req->rq_pill,
2332 MSG_PTLRPC_HEADER_OFF);
2336 CERROR("error unpacking request from %s x%llu\n",
2337 libcfs_idstr(&req->rq_peer), req->rq_xid);
2338 RETURN(SECSVC_DROP);
2341 req->rq_flvr.sf_rpc = WIRE_FLVR(msg->lm_secflvr);
2342 req->rq_sp_from = LUSTRE_SP_ANY;
2343 req->rq_auth_uid = -1; /* set to INVALID_UID */
2344 req->rq_auth_mapped_uid = -1;
2346 policy = sptlrpc_wireflavor2policy(req->rq_flvr.sf_rpc);
2348 CERROR("unsupported rpc flavor %x\n", req->rq_flvr.sf_rpc);
2349 RETURN(SECSVC_DROP);
2352 LASSERT(policy->sp_sops->accept);
2353 rc = policy->sp_sops->accept(req);
2354 sptlrpc_policy_put(policy);
2355 LASSERT(req->rq_reqmsg || rc != SECSVC_OK);
2356 LASSERT(req->rq_svc_ctx || rc == SECSVC_DROP);
2359 * if it's not null flavor (which means embedded packing msg),
2360 * reset the swab mask for the comming inner msg unpacking.
2362 if (SPTLRPC_FLVR_POLICY(req->rq_flvr.sf_rpc) != SPTLRPC_POLICY_NULL)
2363 req->rq_req_swab_mask = 0;
2365 /* sanity check for the request source */
2366 rc = sptlrpc_svc_check_from(req, rc);
2371 * Used by ptlrpc server, to allocate reply buffer for \a req. If succeed,
2372 * req->rq_reply_state is set, and req->rq_reply_state->rs_msg point to
2373 * a buffer of \a msglen size.
2375 int sptlrpc_svc_alloc_rs(struct ptlrpc_request *req, int msglen)
2377 struct ptlrpc_sec_policy *policy;
2378 struct ptlrpc_reply_state *rs;
2383 LASSERT(req->rq_svc_ctx);
2384 LASSERT(req->rq_svc_ctx->sc_policy);
2386 policy = req->rq_svc_ctx->sc_policy;
2387 LASSERT(policy->sp_sops->alloc_rs);
2389 rc = policy->sp_sops->alloc_rs(req, msglen);
2390 if (unlikely(rc == -ENOMEM)) {
2391 struct ptlrpc_service_part *svcpt = req->rq_rqbd->rqbd_svcpt;
2393 if (svcpt->scp_service->srv_max_reply_size <
2394 msglen + sizeof(struct ptlrpc_reply_state)) {
2395 /* Just return failure if the size is too big */
2396 CERROR("size of message is too big (%zd), %d allowed\n",
2397 msglen + sizeof(struct ptlrpc_reply_state),
2398 svcpt->scp_service->srv_max_reply_size);
2402 /* failed alloc, try emergency pool */
2403 rs = lustre_get_emerg_rs(svcpt);
2407 req->rq_reply_state = rs;
2408 rc = policy->sp_sops->alloc_rs(req, msglen);
2410 lustre_put_emerg_rs(rs);
2411 req->rq_reply_state = NULL;
2416 (req->rq_reply_state && req->rq_reply_state->rs_msg));
2422 * Used by ptlrpc server, to perform transformation upon reply message.
2424 * \post req->rq_reply_off is set to approriate server-controlled reply offset.
2425 * \post req->rq_repmsg and req->rq_reply_state->rs_msg becomes inaccessible.
2427 int sptlrpc_svc_wrap_reply(struct ptlrpc_request *req)
2429 struct ptlrpc_sec_policy *policy;
2434 LASSERT(req->rq_svc_ctx);
2435 LASSERT(req->rq_svc_ctx->sc_policy);
2437 policy = req->rq_svc_ctx->sc_policy;
2438 LASSERT(policy->sp_sops->authorize);
2440 rc = policy->sp_sops->authorize(req);
2441 LASSERT(rc || req->rq_reply_state->rs_repdata_len);
2447 * Used by ptlrpc server, to free reply_state.
2449 void sptlrpc_svc_free_rs(struct ptlrpc_reply_state *rs)
2451 struct ptlrpc_sec_policy *policy;
2452 unsigned int prealloc;
2456 LASSERT(rs->rs_svc_ctx);
2457 LASSERT(rs->rs_svc_ctx->sc_policy);
2459 policy = rs->rs_svc_ctx->sc_policy;
2460 LASSERT(policy->sp_sops->free_rs);
2462 prealloc = rs->rs_prealloc;
2463 policy->sp_sops->free_rs(rs);
2466 lustre_put_emerg_rs(rs);
2470 void sptlrpc_svc_ctx_addref(struct ptlrpc_request *req)
2472 struct ptlrpc_svc_ctx *ctx = req->rq_svc_ctx;
2475 atomic_inc(&ctx->sc_refcount);
2478 void sptlrpc_svc_ctx_decref(struct ptlrpc_request *req)
2480 struct ptlrpc_svc_ctx *ctx = req->rq_svc_ctx;
2485 LASSERT(atomic_read(&(ctx)->sc_refcount) > 0);
2486 if (atomic_dec_and_test(&ctx->sc_refcount)) {
2487 if (ctx->sc_policy->sp_sops->free_ctx)
2488 ctx->sc_policy->sp_sops->free_ctx(ctx);
2490 req->rq_svc_ctx = NULL;
2493 void sptlrpc_svc_ctx_invalidate(struct ptlrpc_request *req)
2495 struct ptlrpc_svc_ctx *ctx = req->rq_svc_ctx;
2500 LASSERT(atomic_read(&(ctx)->sc_refcount) > 0);
2501 if (ctx->sc_policy->sp_sops->invalidate_ctx)
2502 ctx->sc_policy->sp_sops->invalidate_ctx(ctx);
2504 EXPORT_SYMBOL(sptlrpc_svc_ctx_invalidate);
2511 * Perform transformation upon bulk data pointed by \a desc. This is called
2512 * before transforming the request message.
2514 int sptlrpc_cli_wrap_bulk(struct ptlrpc_request *req,
2515 struct ptlrpc_bulk_desc *desc)
2517 struct ptlrpc_cli_ctx *ctx;
2519 LASSERT(req->rq_bulk_read || req->rq_bulk_write);
2521 if (!req->rq_pack_bulk)
2524 ctx = req->rq_cli_ctx;
2525 if (ctx->cc_ops->wrap_bulk)
2526 return ctx->cc_ops->wrap_bulk(ctx, req, desc);
2529 EXPORT_SYMBOL(sptlrpc_cli_wrap_bulk);
2532 * This is called after unwrap the reply message.
2533 * return nob of actual plain text size received, or error code.
2535 int sptlrpc_cli_unwrap_bulk_read(struct ptlrpc_request *req,
2536 struct ptlrpc_bulk_desc *desc,
2539 struct ptlrpc_cli_ctx *ctx;
2542 LASSERT(req->rq_bulk_read && !req->rq_bulk_write);
2544 if (!req->rq_pack_bulk)
2545 return desc->bd_nob_transferred;
2547 ctx = req->rq_cli_ctx;
2548 if (ctx->cc_ops->unwrap_bulk) {
2549 rc = ctx->cc_ops->unwrap_bulk(ctx, req, desc);
2553 return desc->bd_nob_transferred;
2555 EXPORT_SYMBOL(sptlrpc_cli_unwrap_bulk_read);
2558 * This is called after unwrap the reply message.
2559 * return 0 for success or error code.
2561 int sptlrpc_cli_unwrap_bulk_write(struct ptlrpc_request *req,
2562 struct ptlrpc_bulk_desc *desc)
2564 struct ptlrpc_cli_ctx *ctx;
2567 LASSERT(!req->rq_bulk_read && req->rq_bulk_write);
2569 if (!req->rq_pack_bulk)
2572 ctx = req->rq_cli_ctx;
2573 if (ctx->cc_ops->unwrap_bulk) {
2574 rc = ctx->cc_ops->unwrap_bulk(ctx, req, desc);
2580 * if everything is going right, nob should equals to nob_transferred.
2581 * in case of privacy mode, nob_transferred needs to be adjusted.
2583 if (desc->bd_nob != desc->bd_nob_transferred) {
2584 CERROR("nob %d doesn't match transferred nob %d\n",
2585 desc->bd_nob, desc->bd_nob_transferred);
2591 EXPORT_SYMBOL(sptlrpc_cli_unwrap_bulk_write);
2593 #ifdef HAVE_SERVER_SUPPORT
2595 * Performe transformation upon outgoing bulk read.
2597 int sptlrpc_svc_wrap_bulk(struct ptlrpc_request *req,
2598 struct ptlrpc_bulk_desc *desc)
2600 struct ptlrpc_svc_ctx *ctx;
2602 LASSERT(req->rq_bulk_read);
2604 if (!req->rq_pack_bulk)
2607 ctx = req->rq_svc_ctx;
2608 if (ctx->sc_policy->sp_sops->wrap_bulk)
2609 return ctx->sc_policy->sp_sops->wrap_bulk(req, desc);
2613 EXPORT_SYMBOL(sptlrpc_svc_wrap_bulk);
2616 * Performe transformation upon incoming bulk write.
2618 int sptlrpc_svc_unwrap_bulk(struct ptlrpc_request *req,
2619 struct ptlrpc_bulk_desc *desc)
2621 struct ptlrpc_svc_ctx *ctx;
2624 LASSERT(req->rq_bulk_write);
2627 * if it's in privacy mode, transferred should >= expected; otherwise
2628 * transferred should == expected.
2630 if (desc->bd_nob_transferred < desc->bd_nob ||
2631 (desc->bd_nob_transferred > desc->bd_nob &&
2632 SPTLRPC_FLVR_BULK_SVC(req->rq_flvr.sf_rpc) !=
2633 SPTLRPC_BULK_SVC_PRIV)) {
2634 DEBUG_REQ(D_ERROR, req, "truncated bulk GET %d(%d)",
2635 desc->bd_nob_transferred, desc->bd_nob);
2639 if (!req->rq_pack_bulk)
2642 ctx = req->rq_svc_ctx;
2643 if (ctx->sc_policy->sp_sops->unwrap_bulk) {
2644 rc = ctx->sc_policy->sp_sops->unwrap_bulk(req, desc);
2646 CERROR("error unwrap bulk: %d\n", rc);
2649 /* return 0 to allow reply be sent */
2652 EXPORT_SYMBOL(sptlrpc_svc_unwrap_bulk);
2655 * Prepare buffers for incoming bulk write.
2657 int sptlrpc_svc_prep_bulk(struct ptlrpc_request *req,
2658 struct ptlrpc_bulk_desc *desc)
2660 struct ptlrpc_svc_ctx *ctx;
2662 LASSERT(req->rq_bulk_write);
2664 if (!req->rq_pack_bulk)
2667 ctx = req->rq_svc_ctx;
2668 if (ctx->sc_policy->sp_sops->prep_bulk)
2669 return ctx->sc_policy->sp_sops->prep_bulk(req, desc);
2673 EXPORT_SYMBOL(sptlrpc_svc_prep_bulk);
2675 #endif /* HAVE_SERVER_SUPPORT */
2678 * user descriptor helpers
2681 int sptlrpc_current_user_desc_size(void)
2685 ngroups = current_cred()->group_info->ngroups;
2687 if (ngroups > LUSTRE_MAX_GROUPS)
2688 ngroups = LUSTRE_MAX_GROUPS;
2689 return sptlrpc_user_desc_size(ngroups);
2691 EXPORT_SYMBOL(sptlrpc_current_user_desc_size);
2693 int sptlrpc_pack_user_desc(struct lustre_msg *msg, int offset)
2695 struct ptlrpc_user_desc *pud;
2698 pud = lustre_msg_buf(msg, offset, 0);
2700 pud->pud_uid = from_kuid(&init_user_ns, current_uid());
2701 pud->pud_gid = from_kgid(&init_user_ns, current_gid());
2702 pud->pud_fsuid = from_kuid(&init_user_ns, current_fsuid());
2703 pud->pud_fsgid = from_kgid(&init_user_ns, current_fsgid());
2704 pud->pud_cap = ll_capability_u32(current_cap());
2705 pud->pud_ngroups = (msg->lm_buflens[offset] - sizeof(*pud)) / 4;
2708 ngroups = current_cred()->group_info->ngroups;
2709 if (pud->pud_ngroups > ngroups)
2710 pud->pud_ngroups = ngroups;
2711 #ifdef HAVE_GROUP_INFO_GID
2712 memcpy(pud->pud_groups, current_cred()->group_info->gid,
2713 pud->pud_ngroups * sizeof(__u32));
2714 #else /* !HAVE_GROUP_INFO_GID */
2715 memcpy(pud->pud_groups, current_cred()->group_info->blocks[0],
2716 pud->pud_ngroups * sizeof(__u32));
2717 #endif /* HAVE_GROUP_INFO_GID */
2718 task_unlock(current);
2722 EXPORT_SYMBOL(sptlrpc_pack_user_desc);
2724 int sptlrpc_unpack_user_desc(struct lustre_msg *msg, int offset, int swabbed)
2726 struct ptlrpc_user_desc *pud;
2729 pud = lustre_msg_buf(msg, offset, sizeof(*pud));
2734 __swab32s(&pud->pud_uid);
2735 __swab32s(&pud->pud_gid);
2736 __swab32s(&pud->pud_fsuid);
2737 __swab32s(&pud->pud_fsgid);
2738 __swab32s(&pud->pud_cap);
2739 __swab32s(&pud->pud_ngroups);
2742 if (pud->pud_ngroups > LUSTRE_MAX_GROUPS) {
2743 CERROR("%u groups is too large\n", pud->pud_ngroups);
2747 if (sizeof(*pud) + pud->pud_ngroups * sizeof(__u32) >
2748 msg->lm_buflens[offset]) {
2749 CERROR("%u groups are claimed but bufsize only %u\n",
2750 pud->pud_ngroups, msg->lm_buflens[offset]);
2755 for (i = 0; i < pud->pud_ngroups; i++)
2756 __swab32s(&pud->pud_groups[i]);
2761 EXPORT_SYMBOL(sptlrpc_unpack_user_desc);
2767 const char *sec2target_str(struct ptlrpc_sec *sec)
2769 if (!sec || !sec->ps_import || !sec->ps_import->imp_obd)
2771 if (sec_is_reverse(sec))
2773 return obd_uuid2str(&sec->ps_import->imp_obd->u.cli.cl_target_uuid);
2775 EXPORT_SYMBOL(sec2target_str);
2778 * return true if the bulk data is protected
2780 int sptlrpc_flavor_has_bulk(struct sptlrpc_flavor *flvr)
2782 switch (SPTLRPC_FLVR_BULK_SVC(flvr->sf_rpc)) {
2783 case SPTLRPC_BULK_SVC_INTG:
2784 case SPTLRPC_BULK_SVC_PRIV:
2790 EXPORT_SYMBOL(sptlrpc_flavor_has_bulk);
2793 static int cfs_hash_alg_id[] = {
2794 [BULK_HASH_ALG_NULL] = CFS_HASH_ALG_NULL,
2795 [BULK_HASH_ALG_ADLER32] = CFS_HASH_ALG_ADLER32,
2796 [BULK_HASH_ALG_CRC32] = CFS_HASH_ALG_CRC32,
2797 [BULK_HASH_ALG_MD5] = CFS_HASH_ALG_MD5,
2798 [BULK_HASH_ALG_SHA1] = CFS_HASH_ALG_SHA1,
2799 [BULK_HASH_ALG_SHA256] = CFS_HASH_ALG_SHA256,
2800 [BULK_HASH_ALG_SHA384] = CFS_HASH_ALG_SHA384,
2801 [BULK_HASH_ALG_SHA512] = CFS_HASH_ALG_SHA512,
2803 const char *sptlrpc_get_hash_name(__u8 hash_alg)
2805 return cfs_crypto_hash_name(cfs_hash_alg_id[hash_alg]);
2808 __u8 sptlrpc_get_hash_alg(const char *algname)
2810 return cfs_crypto_hash_alg(algname);
2813 int bulk_sec_desc_unpack(struct lustre_msg *msg, int offset, int swabbed)
2815 struct ptlrpc_bulk_sec_desc *bsd;
2816 int size = msg->lm_buflens[offset];
2818 bsd = lustre_msg_buf(msg, offset, sizeof(*bsd));
2820 CERROR("Invalid bulk sec desc: size %d\n", size);
2825 __swab32s(&bsd->bsd_nob);
2827 if (unlikely(bsd->bsd_version != 0)) {
2828 CERROR("Unexpected version %u\n", bsd->bsd_version);
2832 if (unlikely(bsd->bsd_type >= SPTLRPC_BULK_MAX)) {
2833 CERROR("Invalid type %u\n", bsd->bsd_type);
2837 /* FIXME more sanity check here */
2839 if (unlikely(bsd->bsd_svc != SPTLRPC_BULK_SVC_NULL &&
2840 bsd->bsd_svc != SPTLRPC_BULK_SVC_INTG &&
2841 bsd->bsd_svc != SPTLRPC_BULK_SVC_PRIV)) {
2842 CERROR("Invalid svc %u\n", bsd->bsd_svc);
2848 EXPORT_SYMBOL(bulk_sec_desc_unpack);
2851 * Compute the checksum of an RPC buffer payload. If the return \a buflen
2852 * is not large enough, truncate the result to fit so that it is possible
2853 * to use a hash function with a large hash space, but only use a part of
2854 * the resulting hash.
2856 int sptlrpc_get_bulk_checksum(struct ptlrpc_bulk_desc *desc, __u8 alg,
2857 void *buf, int buflen)
2859 struct ahash_request *req;
2861 unsigned int bufsize;
2864 LASSERT(alg > BULK_HASH_ALG_NULL && alg < BULK_HASH_ALG_MAX);
2865 LASSERT(buflen >= 4);
2867 req = cfs_crypto_hash_init(cfs_hash_alg_id[alg], NULL, 0);
2869 CERROR("Unable to initialize checksum hash %s\n",
2870 cfs_crypto_hash_name(cfs_hash_alg_id[alg]));
2871 return PTR_ERR(req);
2874 hashsize = cfs_crypto_hash_digestsize(cfs_hash_alg_id[alg]);
2876 for (i = 0; i < desc->bd_iov_count; i++) {
2877 cfs_crypto_hash_update_page(req,
2878 desc->bd_vec[i].bv_page,
2879 desc->bd_vec[i].bv_offset &
2881 desc->bd_vec[i].bv_len);
2884 if (hashsize > buflen) {
2885 unsigned char hashbuf[CFS_CRYPTO_HASH_DIGESTSIZE_MAX];
2887 bufsize = sizeof(hashbuf);
2888 LASSERTF(bufsize >= hashsize, "bufsize = %u < hashsize %u\n",
2890 err = cfs_crypto_hash_final(req, hashbuf, &bufsize);
2891 memcpy(buf, hashbuf, buflen);
2894 err = cfs_crypto_hash_final(req, buf, &bufsize);
2901 * crypto API helper/alloc blkciper
2905 * initialize/finalize
2908 int sptlrpc_init(void)
2912 rwlock_init(&policy_lock);
2914 rc = sptlrpc_gc_init();
2918 rc = sptlrpc_conf_init();
2922 rc = sptlrpc_null_init();
2926 rc = sptlrpc_plain_init();
2930 rc = sptlrpc_lproc_init();
2937 sptlrpc_plain_fini();
2939 sptlrpc_null_fini();
2941 sptlrpc_conf_fini();
2948 void sptlrpc_fini(void)
2950 sptlrpc_lproc_fini();
2951 sptlrpc_plain_fini();
2952 sptlrpc_null_fini();
2953 sptlrpc_conf_fini();