Whamcloud - gitweb
d1fa9200452ba5d6b8e63f4c6e474d2caf9a3591
[fs/lustre-release.git] / lustre / ptlrpc / gss / gss_cli_upcall.c
1 /*
2  * GPL HEADER START
3  *
4  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License version 2 only,
8  * as published by the Free Software Foundation.
9  *
10  * This program is distributed in the hope that it will be useful, but
11  * WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  * General Public License version 2 for more details (a copy is included
14  * in the LICENSE file that accompanied this code).
15  *
16  * You should have received a copy of the GNU General Public License
17  * version 2 along with this program; If not, see
18  * http://www.gnu.org/licenses/gpl-2.0.html
19  *
20  * GPL HEADER END
21  */
22 /*
23  * Copyright (c) 2007, 2010, Oracle and/or its affiliates. All rights reserved.
24  * Use is subject to license terms.
25  *
26  * Copyright (c) 2011, 2016, Intel Corporation.
27  */
28 /*
29  * This file is part of Lustre, http://www.lustre.org/
30  * Lustre is a trademark of Sun Microsystems, Inc.
31  *
32  * lustre/ptlrpc/gss/gss_cli_upcall.c
33  *
34  * Author: Eric Mei <ericm@clusterfs.com>
35  */
36
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>
42 #include <linux/fs.h>
43 #include <linux/mutex.h>
44
45 #include <obd.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>
52
53 #include "gss_err.h"
54 #include "gss_internal.h"
55 #include "gss_api.h"
56
57 /**********************************************
58  * gss context init/fini helper               *
59  **********************************************/
60
61 static
62 int ctx_init_pack_request(struct obd_import *imp,
63                           struct ptlrpc_request *req,
64                           int lustre_srv,
65                           uid_t uid, gid_t gid,
66                           long token_size,
67                           char __user *token)
68 {
69         struct lustre_msg       *msg = req->rq_reqbuf;
70         struct gss_sec          *gsec;
71         struct gss_header       *ghdr;
72         struct ptlrpc_user_desc *pud;
73         __u32                   *p, size, offset = 2;
74         rawobj_t                 obj;
75
76         LASSERT(msg->lm_bufcount <= 4);
77         LASSERT(req->rq_cli_ctx);
78         LASSERT(req->rq_cli_ctx->cc_sec);
79
80         /* gss hdr */
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;
84         ghdr->gh_flags = 0;
85         ghdr->gh_proc = PTLRPC_GSS_PROC_INIT;
86         ghdr->gh_seq = 0;
87         ghdr->gh_svc = SPTLRPC_SVC_NULL;
88         ghdr->gh_handle.len = 0;
89
90         /* fix the user desc */
91         if (req->rq_pack_udesc) {
92                 ghdr->gh_flags |= LUSTRE_GSS_PACK_USER;
93
94                 pud = lustre_msg_buf(msg, offset, sizeof(*pud));
95                 LASSERT(pud);
96                 pud->pud_uid = pud->pud_fsuid = uid;
97                 pud->pud_gid = pud->pud_fsgid = gid;
98                 pud->pud_cap = 0;
99                 pud->pud_ngroups = 0;
100                 offset++;
101         }
102
103         /* security payload */
104         p = lustre_msg_buf(msg, offset, 0);
105         size = msg->lm_buflens[offset];
106         LASSERT(p);
107
108         /* 1. lustre svc type */
109         LASSERT(size > 4);
110         *p++ = cpu_to_le32(lustre_srv);
111         size -= 4;
112
113         /* 2. target uuid */
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))
117                 LBUG();
118
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))
125                 LBUG();
126
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");
132                 return -EFAULT;
133         }
134         size -= sizeof(__u32) + cfs_size_round4(token_size);
135
136         req->rq_reqdata_len = lustre_shrink_msg(req->rq_reqbuf, offset,
137                                                 msg->lm_buflens[offset] - size, 0);
138         return 0;
139 }
140
141 static
142 int ctx_init_parse_reply(struct lustre_msg *msg, int swabbed,
143                          char __user *outbuf, long outlen)
144 {
145         struct gss_rep_header   *ghdr;
146         __u32                    obj_len, round_len;
147         __u32                    status, effective = 0;
148
149         if (msg->lm_bufcount != 3) {
150                 CERROR("unexpected bufcount %u\n", msg->lm_bufcount);
151                 return -EPROTO;
152         }
153
154         ghdr = (struct gss_rep_header *) gss_swab_header(msg, 0, swabbed);
155         if (ghdr == NULL) {
156                 CERROR("unable to extract gss reply header\n");
157                 return -EPROTO;
158         }
159
160         if (ghdr->gh_version != PTLRPC_GSS_VERSION) {
161                 CERROR("invalid gss version %u\n", ghdr->gh_version);
162                 return -EPROTO;
163         }
164
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);
168                 return -EFAULT;
169         }
170
171         status = 0;
172         effective = 0;
173
174         if (copy_to_user(outbuf, &status, 4))
175                 return -EFAULT;
176         outbuf += 4;
177         if (copy_to_user(outbuf, &ghdr->gh_major, 4))
178                 return -EFAULT;
179         outbuf += 4;
180         if (copy_to_user(outbuf, &ghdr->gh_minor, 4))
181                 return -EFAULT;
182         outbuf += 4;
183         if (copy_to_user(outbuf, &ghdr->gh_seqwin, 4))
184                 return -EFAULT;
185         outbuf += 4;
186         effective += 4 * 4;
187
188         /* handle */
189         obj_len = ghdr->gh_handle.len;
190         round_len = (obj_len + 3) & ~3;
191         if (copy_to_user(outbuf, &obj_len, 4))
192                 return -EFAULT;
193         outbuf += 4;
194         if (copy_to_user(outbuf, (char *) ghdr->gh_handle.data, round_len))
195                 return -EFAULT;
196         outbuf += round_len;
197         effective += 4 + round_len;
198
199         /* out token */
200         obj_len = msg->lm_buflens[2];
201         round_len = (obj_len + 3) & ~3;
202         if (copy_to_user(outbuf, &obj_len, 4))
203                 return -EFAULT;
204         outbuf += 4;
205         if (copy_to_user(outbuf, lustre_msg_buf(msg, 2, 0), round_len))
206                 return -EFAULT;
207         outbuf += round_len;
208         effective += 4 + round_len;
209
210         return effective;
211 }
212
213 /* XXX move to where lgssd could see */
214 struct lgssd_ioctl_param {
215         int             version;        /* in   */
216         int             secid;          /* in   */
217         char __user    *uuid;           /* in   */
218         int             lustre_svc;     /* in   */
219         uid_t           uid;            /* in   */
220         gid_t           gid;            /* 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  */
227 };
228
229 int gss_do_ctx_init_rpc(char __user *buffer, unsigned long count)
230 {
231         struct obd_import        *imp;
232         struct ptlrpc_request    *req;
233         struct lgssd_ioctl_param  param;
234         struct obd_device        *obd;
235         char                      obdname[64];
236         long                      lsize;
237         int                       rc;
238
239         if (count != sizeof(param)) {
240                 CERROR("ioctl size %lu, expect %lu, please check lgss_keyring "
241                        "version\n", count, (unsigned long) sizeof(param));
242                 RETURN(-EINVAL);
243         }
244         if (copy_from_user(&param, buffer, sizeof(param))) {
245                 CERROR("failed copy data from lgssd\n");
246                 RETURN(-EFAULT);
247         }
248
249         if (param.version != GSSD_INTERFACE_VERSION) {
250                 CERROR("gssd interface version %d (expect %d)\n",
251                         param.version, GSSD_INTERFACE_VERSION);
252                 RETURN(-EINVAL);
253         }
254
255         /* take name */
256         if (strncpy_from_user(obdname, param.uuid, sizeof(obdname)) <= 0) {
257                 CERROR("Invalid obdname pointer\n");
258                 RETURN(-EFAULT);
259         }
260
261         obd = class_name2obd(obdname);
262         if (!obd) {
263                 CERROR("no such obd %s\n", obdname);
264                 RETURN(-EINVAL);
265         }
266
267         if (unlikely(!obd->obd_set_up)) {
268                 CERROR("obd %s not setup\n", obdname);
269                 RETURN(-EINVAL);
270         }
271
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);
276                 RETURN(-EINVAL);
277         }
278
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);
286                 RETURN(-EINVAL);
287         }
288         spin_unlock(&obd->obd_dev_lock);
289
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);
294                 RETURN(-EINVAL);
295         }
296         imp = class_import_get(obd->u.cli.cl_import);
297         up_read(&obd->u.cli.cl_sem);
298
299         if (imp->imp_deactive) {
300                 CERROR("import has been deactivated\n");
301                 class_import_put(imp);
302                 RETURN(-EINVAL);
303         }
304
305         req = ptlrpc_request_alloc_pack(imp, &RQF_SEC_CTX, LUSTRE_OBD_VERSION,
306                                         SEC_CTX_INIT);
307         if (req == NULL) {
308                 param.status = -ENOMEM;
309                 goto out_copy;
310         }
311
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;
317                 goto out_copy;
318         }
319
320         /* get token */
321         rc = ctx_init_pack_request(imp, req,
322                                    param.lustre_svc,
323                                    param.uid, param.gid,
324                                    param.send_token_size,
325                                    param.send_token);
326         if (rc) {
327                 param.status = rc;
328                 goto out_copy;
329         }
330
331         ptlrpc_request_set_replen(req);
332
333         rc = ptlrpc_queue_wait(req);
334         if (rc) {
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.
340                  *
341                  * FIXME maybe some other error code shouldn't be treated
342                  * as timeout. */
343                 param.status = rc;
344                 if (rc != -EACCES)
345                         param.status = -ETIMEDOUT;
346                 goto out_copy;
347         }
348
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);
353         if (lsize < 0) {
354                 param.status = (int) lsize;
355                 goto out_copy;
356         }
357
358         param.status = 0;
359         param.reply_length = lsize;
360
361 out_copy:
362         if (copy_to_user(buffer, &param, sizeof(param)))
363                 rc = -EFAULT;
364         else
365                 rc = 0;
366
367         class_import_put(imp);
368         ptlrpc_req_finished(req);
369         RETURN(rc);
370 }
371
372 int gss_do_ctx_fini_rpc(struct gss_cli_ctx *gctx)
373 {
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;
378         int                      rc;
379         ENTRY;
380
381         LASSERT(atomic_read(&ctx->cc_refcount) > 0);
382
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));
387                 RETURN(0);
388         }
389
390         might_sleep();
391
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));
397
398         gctx->gc_proc = PTLRPC_GSS_PROC_DESTROY;
399
400         req = ptlrpc_request_alloc(imp, &RQF_SEC_CTX);
401         if (req == NULL) {
402                 CWARN("ctx %p(%u): fail to prepare rpc, destroy locally\n",
403                       ctx, ctx->cc_vcred.vc_uid);
404                 GOTO(out, rc = -ENOMEM);
405         }
406
407         rc = ptlrpc_request_bufs_pack(req, LUSTRE_OBD_VERSION, SEC_CTX_FINI,
408                                       NULL, ctx);
409         if (rc)
410                 GOTO(out_ref, rc);
411
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));
417                 LASSERT(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;
420                 pud->pud_cap = 0;
421                 pud->pud_ngroups = 0;
422         }
423
424         req->rq_phase = RQ_PHASE_RPC;
425         rc = ptl_send_rpc(req, 1);
426         if (rc)
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);
429
430 out_ref:
431         ptlrpc_req_finished(req);
432 out:
433         RETURN(rc);
434 }
435
436 int __init gss_init_cli_upcall(void)
437 {
438         return 0;
439 }
440
441 void gss_exit_cli_upcall(void)
442 {
443 }