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.sun.com/software/products/lustre/docs/GPLv2.pdf
20 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
21 * CA 95054 USA or visit www.sun.com if you need additional information or
27 * Copyright (c) 2007, 2010, Oracle and/or its affiliates. All rights reserved.
28 * Use is subject to license terms.
30 * Copyright (c) 2011, 2014, Intel Corporation.
33 * This file is part of Lustre, http://www.lustre.org/
34 * Lustre is a trademark of Sun Microsystems, Inc.
36 * lustre/ptlrpc/gss/gss_cli_upcall.c
38 * Author: Eric Mei <ericm@clusterfs.com>
41 #define DEBUG_SUBSYSTEM S_SEC
42 #include <linux/init.h>
43 #include <linux/module.h>
44 #include <linux/slab.h>
45 #include <linux/dcache.h>
47 #include <linux/mutex.h>
50 #include <obd_class.h>
51 #include <obd_support.h>
52 #include <lustre/lustre_idl.h>
53 #include <lustre_net.h>
54 #include <lustre_import.h>
55 #include <lustre_sec.h>
58 #include "gss_internal.h"
61 /**********************************************
62 * gss context init/fini helper *
63 **********************************************/
66 int ctx_init_pack_request(struct obd_import *imp,
67 struct ptlrpc_request *req,
73 struct lustre_msg *msg = req->rq_reqbuf;
75 struct gss_header *ghdr;
76 struct ptlrpc_user_desc *pud;
77 __u32 *p, size, offset = 2;
80 LASSERT(msg->lm_bufcount <= 4);
81 LASSERT(req->rq_cli_ctx);
82 LASSERT(req->rq_cli_ctx->cc_sec);
85 ghdr = lustre_msg_buf(msg, 0, sizeof(*ghdr));
86 ghdr->gh_version = PTLRPC_GSS_VERSION;
87 ghdr->gh_sp = (__u8) imp->imp_sec->ps_part;
89 ghdr->gh_proc = PTLRPC_GSS_PROC_INIT;
91 ghdr->gh_svc = SPTLRPC_SVC_NULL;
92 ghdr->gh_handle.len = 0;
94 /* fix the user desc */
95 if (req->rq_pack_udesc) {
96 ghdr->gh_flags |= LUSTRE_GSS_PACK_USER;
98 pud = lustre_msg_buf(msg, offset, sizeof(*pud));
100 pud->pud_uid = pud->pud_fsuid = uid;
101 pud->pud_gid = pud->pud_fsgid = gid;
103 pud->pud_ngroups = 0;
107 /* security payload */
108 p = lustre_msg_buf(msg, offset, 0);
109 size = msg->lm_buflens[offset];
112 /* 1. lustre svc type */
114 *p++ = cpu_to_le32(lustre_srv);
118 obj.len = strlen(imp->imp_obd->u.cli.cl_target_uuid.uuid) + 1;
119 obj.data = imp->imp_obd->u.cli.cl_target_uuid.uuid;
120 if (rawobj_serialize(&obj, &p, &size))
123 /* 3. reverse context handle. actually only needed by root user,
124 * but we send it anyway. */
125 gsec = sec2gsec(req->rq_cli_ctx->cc_sec);
126 obj.len = sizeof(gsec->gs_rvs_hdl);
127 obj.data = (__u8 *) &gsec->gs_rvs_hdl;
128 if (rawobj_serialize(&obj, &p, &size))
131 /* 4. now the token */
132 LASSERT(size >= (sizeof(__u32) + token_size));
133 *p++ = cpu_to_le32(((__u32) token_size));
134 if (copy_from_user(p, token, token_size)) {
135 CERROR("can't copy token\n");
138 size -= sizeof(__u32) + cfs_size_round4(token_size);
140 req->rq_reqdata_len = lustre_shrink_msg(req->rq_reqbuf, offset,
141 msg->lm_buflens[offset] - size, 0);
146 int ctx_init_parse_reply(struct lustre_msg *msg, int swabbed,
147 char __user *outbuf, long outlen)
149 struct gss_rep_header *ghdr;
150 __u32 obj_len, round_len;
151 __u32 status, effective = 0;
153 if (msg->lm_bufcount != 3) {
154 CERROR("unexpected bufcount %u\n", msg->lm_bufcount);
158 ghdr = (struct gss_rep_header *) gss_swab_header(msg, 0, swabbed);
160 CERROR("unable to extract gss reply header\n");
164 if (ghdr->gh_version != PTLRPC_GSS_VERSION) {
165 CERROR("invalid gss version %u\n", ghdr->gh_version);
169 if (outlen < (4 + 2) * 4 + cfs_size_round4(ghdr->gh_handle.len) +
170 cfs_size_round4(msg->lm_buflens[2])) {
171 CERROR("output buffer size %ld too small\n", outlen);
178 if (copy_to_user(outbuf, &status, 4))
181 if (copy_to_user(outbuf, &ghdr->gh_major, 4))
184 if (copy_to_user(outbuf, &ghdr->gh_minor, 4))
187 if (copy_to_user(outbuf, &ghdr->gh_seqwin, 4))
193 obj_len = ghdr->gh_handle.len;
194 round_len = (obj_len + 3) & ~3;
195 if (copy_to_user(outbuf, &obj_len, 4))
198 if (copy_to_user(outbuf, (char *) ghdr->gh_handle.data, round_len))
201 effective += 4 + round_len;
204 obj_len = msg->lm_buflens[2];
205 round_len = (obj_len + 3) & ~3;
206 if (copy_to_user(outbuf, &obj_len, 4))
209 if (copy_to_user(outbuf, lustre_msg_buf(msg, 2, 0), round_len))
212 effective += 4 + round_len;
217 /* XXX move to where lgssd could see */
218 struct lgssd_ioctl_param {
219 int version; /* in */
221 char __user *uuid; /* in */
222 int lustre_svc; /* in */
225 long send_token_size;/* in */
226 char __user *send_token; /* in */
227 long reply_buf_size; /* in */
228 char __user *reply_buf; /* in */
229 long status; /* out */
230 long reply_length; /* out */
233 int gss_do_ctx_init_rpc(char __user *buffer, unsigned long count)
235 struct obd_import *imp;
236 struct ptlrpc_request *req;
237 struct lgssd_ioctl_param param;
238 struct obd_device *obd;
243 if (count != sizeof(param)) {
244 CERROR("ioctl size %lu, expect %lu, please check lgss_keyring "
245 "version\n", count, (unsigned long) sizeof(param));
248 if (copy_from_user(¶m, buffer, sizeof(param))) {
249 CERROR("failed copy data from lgssd\n");
253 if (param.version != GSSD_INTERFACE_VERSION) {
254 CERROR("gssd interface version %d (expect %d)\n",
255 param.version, GSSD_INTERFACE_VERSION);
260 if (strncpy_from_user(obdname, param.uuid, sizeof(obdname)) <= 0) {
261 CERROR("Invalid obdname pointer\n");
265 obd = class_name2obd(obdname);
267 CERROR("no such obd %s\n", obdname);
271 if (unlikely(!obd->obd_set_up)) {
272 CERROR("obd %s not setup\n", obdname);
276 spin_lock(&obd->obd_dev_lock);
277 if (obd->obd_stopping) {
278 CERROR("obd %s has stopped\n", obdname);
279 spin_unlock(&obd->obd_dev_lock);
283 if (strcmp(obd->obd_type->typ_name, LUSTRE_MDC_NAME) &&
284 strcmp(obd->obd_type->typ_name, LUSTRE_OSC_NAME) &&
285 strcmp(obd->obd_type->typ_name, LUSTRE_MGC_NAME) &&
286 strcmp(obd->obd_type->typ_name, LUSTRE_LWP_NAME) &&
287 strcmp(obd->obd_type->typ_name, LUSTRE_OSP_NAME)) {
288 CERROR("obd %s is not a client device\n", obdname);
289 spin_unlock(&obd->obd_dev_lock);
292 spin_unlock(&obd->obd_dev_lock);
294 down_read(&obd->u.cli.cl_sem);
295 if (obd->u.cli.cl_import == NULL) {
296 CERROR("obd %s: import has gone\n", obd->obd_name);
297 up_read(&obd->u.cli.cl_sem);
300 imp = class_import_get(obd->u.cli.cl_import);
301 up_read(&obd->u.cli.cl_sem);
303 if (imp->imp_deactive) {
304 CERROR("import has been deactivated\n");
305 class_import_put(imp);
309 req = ptlrpc_request_alloc_pack(imp, &RQF_SEC_CTX, LUSTRE_OBD_VERSION,
312 param.status = -ENOMEM;
316 if (req->rq_cli_ctx->cc_sec->ps_id != param.secid) {
317 CWARN("original secid %d, now has changed to %d, "
318 "cancel this negotiation\n", param.secid,
319 req->rq_cli_ctx->cc_sec->ps_id);
320 param.status = -EINVAL;
325 rc = ctx_init_pack_request(imp, req,
327 param.uid, param.gid,
328 param.send_token_size,
335 ptlrpc_request_set_replen(req);
337 rc = ptlrpc_queue_wait(req);
339 /* If any _real_ denial be made, we expect server return
340 * -EACCES reply or return success but indicate gss error
341 * inside reply messsage. All other errors are treated as
342 * timeout, caller might try the negotiation repeatedly,
343 * leave recovery decisions to general ptlrpc layer.
345 * FIXME maybe some other error code shouldn't be treated
349 param.status = -ETIMEDOUT;
353 LASSERT(req->rq_repdata);
354 lsize = ctx_init_parse_reply(req->rq_repdata,
355 ptlrpc_rep_need_swab(req),
356 param.reply_buf, param.reply_buf_size);
358 param.status = (int) lsize;
363 param.reply_length = lsize;
366 if (copy_to_user(buffer, ¶m, sizeof(param)))
371 class_import_put(imp);
372 ptlrpc_req_finished(req);
376 int gss_do_ctx_fini_rpc(struct gss_cli_ctx *gctx)
378 struct ptlrpc_cli_ctx *ctx = &gctx->gc_base;
379 struct obd_import *imp = ctx->cc_sec->ps_import;
380 struct ptlrpc_request *req;
381 struct ptlrpc_user_desc *pud;
385 LASSERT(atomic_read(&ctx->cc_refcount) > 0);
387 if (cli_ctx_is_error(ctx) || !cli_ctx_is_uptodate(ctx)) {
388 CDEBUG(D_SEC, "ctx %p(%u->%s) not uptodate, "
389 "don't send destroy rpc\n", ctx,
390 ctx->cc_vcred.vc_uid, sec2target_str(ctx->cc_sec));
396 CWARN("%s ctx %p idx %#llx (%u->%s)\n",
397 sec_is_reverse(ctx->cc_sec) ?
398 "server finishing reverse" : "client finishing forward",
399 ctx, gss_handle_to_u64(&gctx->gc_handle),
400 ctx->cc_vcred.vc_uid, sec2target_str(ctx->cc_sec));
402 gctx->gc_proc = PTLRPC_GSS_PROC_DESTROY;
404 req = ptlrpc_request_alloc(imp, &RQF_SEC_CTX);
406 CWARN("ctx %p(%u): fail to prepare rpc, destroy locally\n",
407 ctx, ctx->cc_vcred.vc_uid);
408 GOTO(out, rc = -ENOMEM);
411 rc = ptlrpc_request_bufs_pack(req, LUSTRE_OBD_VERSION, SEC_CTX_FINI,
416 /* fix the user desc */
417 if (req->rq_pack_udesc) {
418 /* we rely the fact that this request is in AUTH mode,
419 * and user_desc at offset 2. */
420 pud = lustre_msg_buf(req->rq_reqbuf, 2, sizeof(*pud));
422 pud->pud_uid = pud->pud_fsuid = ctx->cc_vcred.vc_uid;
423 pud->pud_gid = pud->pud_fsgid = ctx->cc_vcred.vc_gid;
425 pud->pud_ngroups = 0;
428 req->rq_phase = RQ_PHASE_RPC;
429 rc = ptl_send_rpc(req, 1);
431 CWARN("ctx %p(%u->%s): rpc error %d, destroy locally\n", ctx,
432 ctx->cc_vcred.vc_uid, sec2target_str(ctx->cc_sec), rc);
435 ptlrpc_req_finished(req);
440 int __init gss_init_cli_upcall(void)
445 void gss_exit_cli_upcall(void)