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/lustre_idl.h>
49 #include <lustre_net.h>
50 #include <lustre_import.h>
51 #include <lustre_sec.h>
54 #include "gss_internal.h"
57 /**********************************************
58 * gss context init/fini helper *
59 **********************************************/
62 int ctx_init_pack_request(struct obd_import *imp,
63 struct ptlrpc_request *req,
69 struct lustre_msg *msg = req->rq_reqbuf;
71 struct gss_header *ghdr;
72 struct ptlrpc_user_desc *pud;
73 __u32 *p, size, offset = 2;
76 LASSERT(msg->lm_bufcount <= 4);
77 LASSERT(req->rq_cli_ctx);
78 LASSERT(req->rq_cli_ctx->cc_sec);
81 ghdr = lustre_msg_buf(msg, 0, sizeof(*ghdr));
82 ghdr->gh_version = PTLRPC_GSS_VERSION;
83 ghdr->gh_sp = (__u8) imp->imp_sec->ps_part;
85 ghdr->gh_proc = PTLRPC_GSS_PROC_INIT;
87 ghdr->gh_svc = SPTLRPC_SVC_NULL;
88 ghdr->gh_handle.len = 0;
90 /* fix the user desc */
91 if (req->rq_pack_udesc) {
92 ghdr->gh_flags |= LUSTRE_GSS_PACK_USER;
94 pud = lustre_msg_buf(msg, offset, sizeof(*pud));
96 pud->pud_uid = pud->pud_fsuid = uid;
97 pud->pud_gid = pud->pud_fsgid = gid;
103 /* security payload */
104 p = lustre_msg_buf(msg, offset, 0);
105 size = msg->lm_buflens[offset];
108 /* 1. lustre svc type */
110 *p++ = cpu_to_le32(lustre_srv);
114 obj.len = strlen(imp->imp_obd->u.cli.cl_target_uuid.uuid) + 1;
115 obj.data = imp->imp_obd->u.cli.cl_target_uuid.uuid;
116 if (rawobj_serialize(&obj, &p, &size))
119 /* 3. reverse context handle. actually only needed by root user,
120 * but we send it anyway. */
121 gsec = sec2gsec(req->rq_cli_ctx->cc_sec);
122 obj.len = sizeof(gsec->gs_rvs_hdl);
123 obj.data = (__u8 *) &gsec->gs_rvs_hdl;
124 if (rawobj_serialize(&obj, &p, &size))
127 /* 4. now the token */
128 LASSERT(size >= (sizeof(__u32) + token_size));
129 *p++ = cpu_to_le32(((__u32) token_size));
130 if (copy_from_user(p, token, token_size)) {
131 CERROR("can't copy token\n");
134 size -= sizeof(__u32) + cfs_size_round4(token_size);
136 req->rq_reqdata_len = lustre_shrink_msg(req->rq_reqbuf, offset,
137 msg->lm_buflens[offset] - size, 0);
142 int ctx_init_parse_reply(struct lustre_msg *msg, int swabbed,
143 char __user *outbuf, long outlen)
145 struct gss_rep_header *ghdr;
146 __u32 obj_len, round_len;
147 __u32 status, effective = 0;
149 if (msg->lm_bufcount != 3) {
150 CERROR("unexpected bufcount %u\n", msg->lm_bufcount);
154 ghdr = (struct gss_rep_header *) gss_swab_header(msg, 0, swabbed);
156 CERROR("unable to extract gss reply header\n");
160 if (ghdr->gh_version != PTLRPC_GSS_VERSION) {
161 CERROR("invalid gss version %u\n", ghdr->gh_version);
165 if (outlen < (4 + 2) * 4 + cfs_size_round4(ghdr->gh_handle.len) +
166 cfs_size_round4(msg->lm_buflens[2])) {
167 CERROR("output buffer size %ld too small\n", outlen);
174 if (copy_to_user(outbuf, &status, 4))
177 if (copy_to_user(outbuf, &ghdr->gh_major, 4))
180 if (copy_to_user(outbuf, &ghdr->gh_minor, 4))
183 if (copy_to_user(outbuf, &ghdr->gh_seqwin, 4))
189 obj_len = ghdr->gh_handle.len;
190 round_len = (obj_len + 3) & ~3;
191 if (copy_to_user(outbuf, &obj_len, 4))
194 if (copy_to_user(outbuf, (char *) ghdr->gh_handle.data, round_len))
197 effective += 4 + round_len;
200 obj_len = msg->lm_buflens[2];
201 round_len = (obj_len + 3) & ~3;
202 if (copy_to_user(outbuf, &obj_len, 4))
205 if (copy_to_user(outbuf, lustre_msg_buf(msg, 2, 0), round_len))
208 effective += 4 + round_len;
213 /* XXX move to where lgssd could see */
214 struct lgssd_ioctl_param {
215 int version; /* in */
217 char __user *uuid; /* in */
218 int lustre_svc; /* in */
221 long send_token_size;/* in */
222 char __user *send_token; /* in */
223 long reply_buf_size; /* in */
224 char __user *reply_buf; /* in */
225 long status; /* out */
226 long reply_length; /* out */
229 int gss_do_ctx_init_rpc(char __user *buffer, unsigned long count)
231 struct obd_import *imp;
232 struct ptlrpc_request *req;
233 struct lgssd_ioctl_param param;
234 struct obd_device *obd;
239 if (count != sizeof(param)) {
240 CERROR("ioctl size %lu, expect %lu, please check lgss_keyring "
241 "version\n", count, (unsigned long) sizeof(param));
244 if (copy_from_user(¶m, buffer, sizeof(param))) {
245 CERROR("failed copy data from lgssd\n");
249 if (param.version != GSSD_INTERFACE_VERSION) {
250 CERROR("gssd interface version %d (expect %d)\n",
251 param.version, GSSD_INTERFACE_VERSION);
256 if (strncpy_from_user(obdname, param.uuid, sizeof(obdname)) <= 0) {
257 CERROR("Invalid obdname pointer\n");
261 obd = class_name2obd(obdname);
263 CERROR("no such obd %s\n", obdname);
267 if (unlikely(!obd->obd_set_up)) {
268 CERROR("obd %s not setup\n", obdname);
272 spin_lock(&obd->obd_dev_lock);
273 if (obd->obd_stopping) {
274 CERROR("obd %s has stopped\n", obdname);
275 spin_unlock(&obd->obd_dev_lock);
279 if (strcmp(obd->obd_type->typ_name, LUSTRE_MDC_NAME) &&
280 strcmp(obd->obd_type->typ_name, LUSTRE_OSC_NAME) &&
281 strcmp(obd->obd_type->typ_name, LUSTRE_MGC_NAME) &&
282 strcmp(obd->obd_type->typ_name, LUSTRE_LWP_NAME) &&
283 strcmp(obd->obd_type->typ_name, LUSTRE_OSP_NAME)) {
284 CERROR("obd %s is not a client device\n", obdname);
285 spin_unlock(&obd->obd_dev_lock);
288 spin_unlock(&obd->obd_dev_lock);
290 down_read(&obd->u.cli.cl_sem);
291 if (obd->u.cli.cl_import == NULL) {
292 CERROR("obd %s: import has gone\n", obd->obd_name);
293 up_read(&obd->u.cli.cl_sem);
296 imp = class_import_get(obd->u.cli.cl_import);
297 up_read(&obd->u.cli.cl_sem);
299 if (imp->imp_deactive) {
300 CERROR("import has been deactivated\n");
301 class_import_put(imp);
305 req = ptlrpc_request_alloc_pack(imp, &RQF_SEC_CTX, LUSTRE_OBD_VERSION,
308 param.status = -ENOMEM;
312 if (req->rq_cli_ctx->cc_sec->ps_id != param.secid) {
313 CWARN("original secid %d, now has changed to %d, "
314 "cancel this negotiation\n", param.secid,
315 req->rq_cli_ctx->cc_sec->ps_id);
316 param.status = -EINVAL;
321 rc = ctx_init_pack_request(imp, req,
323 param.uid, param.gid,
324 param.send_token_size,
331 ptlrpc_request_set_replen(req);
333 rc = ptlrpc_queue_wait(req);
335 /* If any _real_ denial be made, we expect server return
336 * -EACCES reply or return success but indicate gss error
337 * inside reply messsage. All other errors are treated as
338 * timeout, caller might try the negotiation repeatedly,
339 * leave recovery decisions to general ptlrpc layer.
341 * FIXME maybe some other error code shouldn't be treated
345 param.status = -ETIMEDOUT;
349 LASSERT(req->rq_repdata);
350 lsize = ctx_init_parse_reply(req->rq_repdata,
351 ptlrpc_rep_need_swab(req),
352 param.reply_buf, param.reply_buf_size);
354 param.status = (int) lsize;
359 param.reply_length = lsize;
362 if (copy_to_user(buffer, ¶m, sizeof(param)))
367 class_import_put(imp);
368 ptlrpc_req_finished(req);
372 int gss_do_ctx_fini_rpc(struct gss_cli_ctx *gctx)
374 struct ptlrpc_cli_ctx *ctx = &gctx->gc_base;
375 struct obd_import *imp = ctx->cc_sec->ps_import;
376 struct ptlrpc_request *req;
377 struct ptlrpc_user_desc *pud;
381 LASSERT(atomic_read(&ctx->cc_refcount) > 0);
383 if (cli_ctx_is_error(ctx) || !cli_ctx_is_uptodate(ctx)) {
384 CDEBUG(D_SEC, "ctx %p(%u->%s) not uptodate, "
385 "don't send destroy rpc\n", ctx,
386 ctx->cc_vcred.vc_uid, sec2target_str(ctx->cc_sec));
392 CWARN("%s ctx %p idx %#llx (%u->%s)\n",
393 sec_is_reverse(ctx->cc_sec) ?
394 "server finishing reverse" : "client finishing forward",
395 ctx, gss_handle_to_u64(&gctx->gc_handle),
396 ctx->cc_vcred.vc_uid, sec2target_str(ctx->cc_sec));
398 gctx->gc_proc = PTLRPC_GSS_PROC_DESTROY;
400 req = ptlrpc_request_alloc(imp, &RQF_SEC_CTX);
402 CWARN("ctx %p(%u): fail to prepare rpc, destroy locally\n",
403 ctx, ctx->cc_vcred.vc_uid);
404 GOTO(out, rc = -ENOMEM);
407 rc = ptlrpc_request_bufs_pack(req, LUSTRE_OBD_VERSION, SEC_CTX_FINI,
412 /* fix the user desc */
413 if (req->rq_pack_udesc) {
414 /* we rely the fact that this request is in AUTH mode,
415 * and user_desc at offset 2. */
416 pud = lustre_msg_buf(req->rq_reqbuf, 2, sizeof(*pud));
418 pud->pud_uid = pud->pud_fsuid = ctx->cc_vcred.vc_uid;
419 pud->pud_gid = pud->pud_fsgid = ctx->cc_vcred.vc_gid;
421 pud->pud_ngroups = 0;
424 req->rq_phase = RQ_PHASE_RPC;
425 rc = ptl_send_rpc(req, 1);
427 CWARN("ctx %p(%u->%s): rpc error %d, destroy locally\n", ctx,
428 ctx->cc_vcred.vc_uid, sec2target_str(ctx->cc_sec), rc);
431 ptlrpc_req_finished(req);
436 int __init gss_init_cli_upcall(void)
441 void gss_exit_cli_upcall(void)