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, 2016, Intel Corporation.
29 * This file is part of Lustre, http://www.lustre.org/
30 * Lustre is a trademark of Sun Microsystems, Inc.
32 * lustre/ptlrpc/gss/gss_cli_upcall.c
34 * Author: Eric Mei <ericm@clusterfs.com>
37 #define DEBUG_SUBSYSTEM S_SEC
38 #include <linux/init.h>
39 #include <linux/module.h>
40 #include <linux/slab.h>
41 #include <linux/dcache.h>
43 #include <linux/mutex.h>
46 #include <obd_class.h>
47 #include <obd_support.h>
48 #include <lustre_net.h>
49 #include <lustre_import.h>
50 #include <lustre_sec.h>
53 #include "gss_internal.h"
56 /**********************************************
57 * gss context init/fini helper *
58 **********************************************/
61 int ctx_init_pack_request(struct obd_import *imp,
62 struct ptlrpc_request *req,
68 struct lustre_msg *msg = req->rq_reqbuf;
70 struct gss_header *ghdr;
71 struct ptlrpc_user_desc *pud;
72 __u32 *p, size, offset = 2;
75 LASSERT(msg->lm_bufcount <= 4);
76 LASSERT(req->rq_cli_ctx);
77 LASSERT(req->rq_cli_ctx->cc_sec);
80 ghdr = lustre_msg_buf(msg, 0, sizeof(*ghdr));
81 ghdr->gh_version = PTLRPC_GSS_VERSION;
82 ghdr->gh_sp = (__u8) imp->imp_sec->ps_part;
84 ghdr->gh_proc = PTLRPC_GSS_PROC_INIT;
86 ghdr->gh_svc = SPTLRPC_SVC_NULL;
87 ghdr->gh_handle.len = 0;
89 /* fix the user desc */
90 if (req->rq_pack_udesc) {
91 ghdr->gh_flags |= LUSTRE_GSS_PACK_USER;
93 pud = lustre_msg_buf(msg, offset, sizeof(*pud));
95 pud->pud_uid = pud->pud_fsuid = uid;
96 pud->pud_gid = pud->pud_fsgid = gid;
102 /* new clients are expected to set KCSUM flag */
103 ghdr->gh_flags |= LUSTRE_GSS_PACK_KCSUM;
105 /* security payload */
106 p = lustre_msg_buf(msg, offset, 0);
107 size = msg->lm_buflens[offset];
110 /* 1. lustre svc type */
112 *p++ = cpu_to_le32(lustre_srv);
116 obj.len = strlen(imp->imp_obd->u.cli.cl_target_uuid.uuid) + 1;
117 obj.data = imp->imp_obd->u.cli.cl_target_uuid.uuid;
118 if (rawobj_serialize(&obj, &p, &size))
121 /* 3. reverse context handle. actually only needed by root user,
122 * but we send it anyway. */
123 gsec = sec2gsec(req->rq_cli_ctx->cc_sec);
124 obj.len = sizeof(gsec->gs_rvs_hdl);
125 obj.data = (__u8 *) &gsec->gs_rvs_hdl;
126 if (rawobj_serialize(&obj, &p, &size))
129 /* 4. now the token */
130 LASSERT(size >= (sizeof(__u32) + token_size));
131 *p++ = cpu_to_le32(((__u32) token_size));
132 if (copy_from_user(p, token, token_size)) {
133 CERROR("can't copy token\n");
136 size -= sizeof(__u32) + cfs_size_round4(token_size);
138 req->rq_reqdata_len = lustre_shrink_msg(req->rq_reqbuf, offset,
139 msg->lm_buflens[offset] - size, 0);
144 int ctx_init_parse_reply(struct lustre_msg *msg, int swabbed,
145 char __user *outbuf, long outlen)
147 struct gss_rep_header *ghdr;
148 __u32 obj_len, round_len;
149 __u32 status, effective = 0;
151 if (msg->lm_bufcount != 3) {
152 CERROR("unexpected bufcount %u\n", msg->lm_bufcount);
156 ghdr = (struct gss_rep_header *) gss_swab_header(msg, 0, swabbed);
158 CERROR("unable to extract gss reply header\n");
162 if (ghdr->gh_version != PTLRPC_GSS_VERSION) {
163 CERROR("invalid gss version %u\n", ghdr->gh_version);
167 if (outlen < (4 + 2) * 4 + cfs_size_round4(ghdr->gh_handle.len) +
168 cfs_size_round4(msg->lm_buflens[2])) {
169 CERROR("output buffer size %ld too small\n", outlen);
176 if (copy_to_user(outbuf, &status, 4))
179 if (copy_to_user(outbuf, &ghdr->gh_major, 4))
182 if (copy_to_user(outbuf, &ghdr->gh_minor, 4))
185 if (copy_to_user(outbuf, &ghdr->gh_seqwin, 4))
191 obj_len = ghdr->gh_handle.len;
192 round_len = (obj_len + 3) & ~3;
193 if (copy_to_user(outbuf, &obj_len, 4))
196 if (copy_to_user(outbuf, (char *) ghdr->gh_handle.data, round_len))
199 effective += 4 + round_len;
202 obj_len = msg->lm_buflens[2];
203 round_len = (obj_len + 3) & ~3;
204 if (copy_to_user(outbuf, &obj_len, 4))
207 if (copy_to_user(outbuf, lustre_msg_buf(msg, 2, 0), round_len))
210 effective += 4 + round_len;
215 /* XXX move to where lgssd could see */
216 struct lgssd_ioctl_param {
217 int version; /* in */
219 char __user *uuid; /* in */
220 int lustre_svc; /* in */
223 long send_token_size;/* in */
224 char __user *send_token; /* in */
225 long reply_buf_size; /* in */
226 char __user *reply_buf; /* in */
227 long status; /* out */
228 long reply_length; /* out */
231 int gss_do_ctx_init_rpc(char __user *buffer, unsigned long count)
233 struct obd_import *imp;
234 struct ptlrpc_request *req;
235 struct lgssd_ioctl_param param;
236 struct obd_device *obd;
241 if (count != sizeof(param)) {
242 CERROR("ioctl size %lu, expect %lu, please check lgss_keyring "
243 "version\n", count, (unsigned long) sizeof(param));
246 if (copy_from_user(¶m, buffer, sizeof(param))) {
247 CERROR("failed copy data from lgssd\n");
251 if (param.version != GSSD_INTERFACE_VERSION) {
252 CERROR("gssd interface version %d (expect %d)\n",
253 param.version, GSSD_INTERFACE_VERSION);
258 if (strncpy_from_user(obdname, param.uuid, sizeof(obdname)) <= 0) {
259 CERROR("Invalid obdname pointer\n");
263 obd = class_name2obd(obdname);
265 CERROR("no such obd %s\n", obdname);
269 if (unlikely(!obd->obd_set_up)) {
270 CERROR("obd %s not setup\n", obdname);
274 spin_lock(&obd->obd_dev_lock);
275 if (obd->obd_stopping) {
276 CERROR("obd %s has stopped\n", obdname);
277 spin_unlock(&obd->obd_dev_lock);
281 if (strcmp(obd->obd_type->typ_name, LUSTRE_MDC_NAME) &&
282 strcmp(obd->obd_type->typ_name, LUSTRE_OSC_NAME) &&
283 strcmp(obd->obd_type->typ_name, LUSTRE_MGC_NAME) &&
284 strcmp(obd->obd_type->typ_name, LUSTRE_LWP_NAME) &&
285 strcmp(obd->obd_type->typ_name, LUSTRE_OSP_NAME)) {
286 CERROR("obd %s is not a client device\n", obdname);
287 spin_unlock(&obd->obd_dev_lock);
290 spin_unlock(&obd->obd_dev_lock);
292 down_read(&obd->u.cli.cl_sem);
293 if (obd->u.cli.cl_import == NULL) {
294 CERROR("obd %s: import has gone\n", obd->obd_name);
295 up_read(&obd->u.cli.cl_sem);
298 imp = class_import_get(obd->u.cli.cl_import);
299 up_read(&obd->u.cli.cl_sem);
301 if (imp->imp_deactive) {
302 CERROR("import has been deactivated\n");
303 class_import_put(imp);
307 req = ptlrpc_request_alloc_pack(imp, &RQF_SEC_CTX, LUSTRE_OBD_VERSION,
310 param.status = -ENOMEM;
314 if (req->rq_cli_ctx->cc_sec->ps_id != param.secid) {
315 CWARN("original secid %d, now has changed to %d, "
316 "cancel this negotiation\n", param.secid,
317 req->rq_cli_ctx->cc_sec->ps_id);
318 param.status = -EINVAL;
323 rc = ctx_init_pack_request(imp, req,
325 param.uid, param.gid,
326 param.send_token_size,
333 ptlrpc_request_set_replen(req);
335 rc = ptlrpc_queue_wait(req);
337 /* If any _real_ denial be made, we expect server return
338 * -EACCES reply or return success but indicate gss error
339 * inside reply messsage. All other errors are treated as
340 * timeout, caller might try the negotiation repeatedly,
341 * leave recovery decisions to general ptlrpc layer.
343 * FIXME maybe some other error code shouldn't be treated
347 param.status = -ETIMEDOUT;
351 LASSERT(req->rq_repdata);
352 lsize = ctx_init_parse_reply(req->rq_repdata,
353 ptlrpc_rep_need_swab(req),
354 param.reply_buf, param.reply_buf_size);
356 param.status = (int) lsize;
361 param.reply_length = lsize;
364 if (copy_to_user(buffer, ¶m, sizeof(param)))
369 class_import_put(imp);
370 ptlrpc_req_finished(req);
374 int gss_do_ctx_fini_rpc(struct gss_cli_ctx *gctx)
376 struct ptlrpc_cli_ctx *ctx = &gctx->gc_base;
377 struct obd_import *imp = ctx->cc_sec->ps_import;
378 struct ptlrpc_request *req;
379 struct ptlrpc_user_desc *pud;
383 LASSERT(atomic_read(&ctx->cc_refcount) > 0);
385 if (cli_ctx_is_error(ctx) || !cli_ctx_is_uptodate(ctx)) {
386 CDEBUG(D_SEC, "ctx %p(%u->%s) not uptodate, "
387 "don't send destroy rpc\n", ctx,
388 ctx->cc_vcred.vc_uid, sec2target_str(ctx->cc_sec));
394 CWARN("%s ctx %p idx %#llx (%u->%s)\n",
395 sec_is_reverse(ctx->cc_sec) ?
396 "server finishing reverse" : "client finishing forward",
397 ctx, gss_handle_to_u64(&gctx->gc_handle),
398 ctx->cc_vcred.vc_uid, sec2target_str(ctx->cc_sec));
400 gctx->gc_proc = PTLRPC_GSS_PROC_DESTROY;
402 req = ptlrpc_request_alloc(imp, &RQF_SEC_CTX);
404 CWARN("ctx %p(%u): fail to prepare rpc, destroy locally\n",
405 ctx, ctx->cc_vcred.vc_uid);
406 GOTO(out, rc = -ENOMEM);
409 rc = ptlrpc_request_bufs_pack(req, LUSTRE_OBD_VERSION, SEC_CTX_FINI,
414 /* fix the user desc */
415 if (req->rq_pack_udesc) {
416 /* we rely the fact that this request is in AUTH mode,
417 * and user_desc at offset 2. */
418 pud = lustre_msg_buf(req->rq_reqbuf, 2, sizeof(*pud));
420 pud->pud_uid = pud->pud_fsuid = ctx->cc_vcred.vc_uid;
421 pud->pud_gid = pud->pud_fsgid = ctx->cc_vcred.vc_gid;
423 pud->pud_ngroups = 0;
426 req->rq_phase = RQ_PHASE_RPC;
427 rc = ptl_send_rpc(req, 1);
429 CWARN("ctx %p(%u->%s): rpc error %d, destroy locally\n", ctx,
430 ctx->cc_vcred.vc_uid, sec2target_str(ctx->cc_sec), rc);
433 ptlrpc_req_finished(req);
438 int __init gss_init_cli_upcall(void)
443 void gss_exit_cli_upcall(void)