Whamcloud - gitweb
LU-17744 ldiskfs: mballoc stats fixes
[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  *
31  * lustre/ptlrpc/gss/gss_cli_upcall.c
32  *
33  * Author: Eric Mei <ericm@clusterfs.com>
34  */
35
36 #define DEBUG_SUBSYSTEM S_SEC
37 #include <linux/init.h>
38 #include <linux/module.h>
39 #include <linux/slab.h>
40 #include <linux/dcache.h>
41 #include <linux/fs.h>
42 #include <linux/mutex.h>
43
44 #include <obd.h>
45 #include <obd_class.h>
46 #include <obd_support.h>
47 #include <lustre_net.h>
48 #include <lustre_import.h>
49 #include <lustre_sec.h>
50 #include <uapi/linux/lustre/lgss.h>
51
52 #include "gss_err.h"
53 #include "gss_internal.h"
54 #include "gss_api.h"
55
56 /**********************************************
57  * gss context init/fini helper               *
58  **********************************************/
59
60 static
61 int ctx_init_pack_request(struct obd_import *imp,
62                           struct ptlrpc_request *req,
63                           int lustre_srv,
64                           uid_t uid, gid_t gid,
65                           long token_size,
66                           char __user *token)
67 {
68         struct lustre_msg *msg = req->rq_reqbuf;
69         struct gss_sec *gsec;
70         struct gss_header *ghdr;
71         struct ptlrpc_user_desc *pud;
72         __u32 total_size;
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         /* new clients are expected to set KCSUM flag */
104         ghdr->gh_flags |= LUSTRE_GSS_PACK_KCSUM;
105
106         /* security payload */
107         p = lustre_msg_buf(msg, offset, 0);
108         size = msg->lm_buflens[offset];
109         LASSERT(p);
110
111         /* 1. lustre svc type */
112         LASSERT(size > 4);
113         *p++ = cpu_to_le32(lustre_srv);
114         size -= 4;
115
116         /* 2. target uuid */
117         obj.len = strlen(imp->imp_obd->u.cli.cl_target_uuid.uuid) + 1;
118         obj.data = imp->imp_obd->u.cli.cl_target_uuid.uuid;
119         if (rawobj_serialize(&obj, &p, &size))
120                 LBUG();
121
122         /* 3. reverse context handle. actually only needed by root user,
123          *    but we send it anyway. */
124         gsec = sec2gsec(req->rq_cli_ctx->cc_sec);
125         obj.len = sizeof(gsec->gs_rvs_hdl);
126         obj.data = (__u8 *) &gsec->gs_rvs_hdl;
127         if (rawobj_serialize(&obj, &p, &size))
128                 LBUG();
129
130         /* 4. now the token */
131         total_size = sizeof(__u32) + token_size;
132         if (size < total_size) {
133                 CERROR("%s: security token is too large (%d > %d): rc = %d\n",
134                        imp->imp_obd->obd_name, total_size, size, -E2BIG);
135                 return -E2BIG;
136         }
137         *p++ = cpu_to_le32(((__u32) token_size));
138         if (copy_from_user(p, token, token_size)) {
139                 CERROR("can't copy token\n");
140                 return -EFAULT;
141         }
142
143         if (size > sizeof(__u32) + round_up(token_size, 4)) {
144                 size -= sizeof(__u32) + round_up(token_size, 4);
145                 req->rq_reqdata_len = lustre_shrink_msg(req->rq_reqbuf, offset,
146                                              msg->lm_buflens[offset] - size, 0);
147         }
148         return 0;
149 }
150
151 static
152 int ctx_init_parse_reply(struct lustre_msg *msg, int swabbed,
153                          char __user *outbuf, long outlen)
154 {
155         struct gss_rep_header   *ghdr;
156         __u32                    obj_len, round_len;
157         __u32                    status, effective = 0;
158
159         if (msg->lm_bufcount != 3) {
160                 CERROR("unexpected bufcount %u\n", msg->lm_bufcount);
161                 return -EPROTO;
162         }
163
164         ghdr = (struct gss_rep_header *) gss_swab_header(msg, 0, swabbed);
165         if (ghdr == NULL) {
166                 CERROR("unable to extract gss reply header\n");
167                 return -EPROTO;
168         }
169
170         if (ghdr->gh_version != PTLRPC_GSS_VERSION) {
171                 CERROR("invalid gss version %u\n", ghdr->gh_version);
172                 return -EPROTO;
173         }
174
175         if (outlen < (4 + 2) * 4 + round_up(ghdr->gh_handle.len, 4) +
176                      round_up(msg->lm_buflens[2], 4)) {
177                 CERROR("output buffer size %ld too small\n", outlen);
178                 return -EFAULT;
179         }
180
181         status = 0;
182         effective = 0;
183
184         if (copy_to_user(outbuf, &status, 4))
185                 return -EFAULT;
186         outbuf += 4;
187         if (copy_to_user(outbuf, &ghdr->gh_major, 4))
188                 return -EFAULT;
189         outbuf += 4;
190         if (copy_to_user(outbuf, &ghdr->gh_minor, 4))
191                 return -EFAULT;
192         outbuf += 4;
193         if (copy_to_user(outbuf, &ghdr->gh_seqwin, 4))
194                 return -EFAULT;
195         outbuf += 4;
196         effective += 4 * 4;
197
198         /* handle */
199         obj_len = ghdr->gh_handle.len;
200         round_len = (obj_len + 3) & ~3;
201         if (copy_to_user(outbuf, &obj_len, 4))
202                 return -EFAULT;
203         outbuf += 4;
204         if (copy_to_user(outbuf, (char *) ghdr->gh_handle.data, round_len))
205                 return -EFAULT;
206         outbuf += round_len;
207         effective += 4 + round_len;
208
209         /* out token */
210         obj_len = msg->lm_buflens[2];
211         round_len = (obj_len + 3) & ~3;
212         if (copy_to_user(outbuf, &obj_len, 4))
213                 return -EFAULT;
214         outbuf += 4;
215         if (copy_to_user(outbuf, lustre_msg_buf(msg, 2, 0), round_len))
216                 return -EFAULT;
217         outbuf += round_len;
218         effective += 4 + round_len;
219
220         return effective;
221 }
222
223 int gss_do_ctx_init_rpc(char __user *buffer, unsigned long count)
224 {
225         struct obd_import *imp = NULL, *imp0;
226         struct ptlrpc_request *req;
227         struct lgssd_ioctl_param param;
228         struct obd_device *obd;
229         char obdname[64];
230         long lsize;
231         int rc;
232
233         if (count != sizeof(param)) {
234                 CERROR("ioctl size %lu, expect %lu, please check lgss_keyring version\n",
235                        count, (unsigned long) sizeof(param));
236                 RETURN(-EINVAL);
237         }
238         if (copy_from_user(&param, buffer, sizeof(param))) {
239                 CERROR("failed copy data from lgssd\n");
240                 RETURN(-EFAULT);
241         }
242
243         if (param.version != GSSD_INTERFACE_VERSION) {
244                 CERROR("gssd interface version %d (expect %d)\n",
245                        param.version, GSSD_INTERFACE_VERSION);
246                 RETURN(-EINVAL);
247         }
248
249         /* take name */
250         if (strncpy_from_user(obdname, (const char __user *)param.uuid,
251                               sizeof(obdname)) <= 0) {
252                 CERROR("Invalid obdname pointer\n");
253                 RETURN(-EFAULT);
254         }
255
256         obd = class_name2obd(obdname);
257         if (!obd) {
258                 rc = -EINVAL;
259                 CERROR("%s: no such obd: rc = %d\n", obdname, rc);
260                 RETURN(rc);
261         }
262
263         if (unlikely(!obd->obd_set_up)) {
264                 rc = -EINVAL;
265                 CERROR("%s: obd not setup: rc = %d\n", obdname, rc);
266                 RETURN(rc);
267         }
268
269         spin_lock(&obd->obd_dev_lock);
270         if (obd->obd_stopping) {
271                 rc = -EINVAL;
272                 CERROR("%s: obd has stopped: rc = %d\n", obdname, rc);
273                 spin_unlock(&obd->obd_dev_lock);
274                 RETURN(rc);
275         }
276
277         if (!obd->obd_type || obd->obd_magic != OBD_DEVICE_MAGIC) {
278                 rc = -EINVAL;
279                 CERROR("%s: obd not valid: rc = %d\n", obdname, rc);
280                 spin_unlock(&obd->obd_dev_lock);
281                 RETURN(rc);
282         }
283
284         if (strcmp(obd->obd_type->typ_name, LUSTRE_MDC_NAME) &&
285             strcmp(obd->obd_type->typ_name, LUSTRE_OSC_NAME) &&
286             strcmp(obd->obd_type->typ_name, LUSTRE_MGC_NAME) &&
287             strcmp(obd->obd_type->typ_name, LUSTRE_LWP_NAME) &&
288             strcmp(obd->obd_type->typ_name, LUSTRE_OSP_NAME)) {
289                 rc = -EINVAL;
290                 CERROR("%s: obd is not a client device: rc = %d\n",
291                        obdname, rc);
292                 spin_unlock(&obd->obd_dev_lock);
293                 RETURN(rc);
294         }
295         spin_unlock(&obd->obd_dev_lock);
296
297         with_imp_locked(obd, imp0, rc) {
298                 if (!imp0->imp_obd || !imp0->imp_sec)
299                         rc = -ENODEV;
300                 else
301                         imp = class_import_get(imp0);
302         }
303         if (rc) {
304                 rc = -EINVAL;
305                 CERROR("%s: import has gone: rc = %d\n", obd->obd_name, rc);
306                 RETURN(rc);
307         }
308
309         if (imp->imp_deactive) {
310                 rc = -EINVAL;
311                 CERROR("%s: import has been deactivated: rc = %d\n",
312                        obd->obd_name, rc);
313                 class_import_put(imp);
314                 RETURN(rc);
315         }
316
317         req = ptlrpc_request_alloc_pack(imp, &RQF_SEC_CTX, LUSTRE_OBD_VERSION,
318                                         SEC_CTX_INIT);
319         if (!req || !req->rq_cli_ctx || !req->rq_cli_ctx->cc_sec) {
320                 param.status = -ENOMEM;
321                 goto out_copy;
322         }
323
324         if (req->rq_cli_ctx->cc_sec->ps_id != param.secid) {
325                 rc = -EINVAL;
326                 CWARN("%s: original secid %d, now has changed to %d, cancel this negotiation: rc = %d\n",
327                       obd->obd_name, param.secid,
328                       req->rq_cli_ctx->cc_sec->ps_id, rc);
329                 param.status = rc;
330                 goto out_copy;
331         }
332
333         /* get token */
334         rc = ctx_init_pack_request(imp, req,
335                                    param.lustre_svc,
336                                    param.uid, param.gid,
337                                    param.send_token_size,
338                                    (char __user *)param.send_token);
339         if (rc) {
340                 param.status = rc;
341                 goto out_copy;
342         }
343
344         ptlrpc_request_set_replen(req);
345
346         rc = ptlrpc_queue_wait(req);
347         if (rc) {
348                 /* If any _real_ denial be made, we expect server return
349                  * -EACCES reply or return success but indicate gss error
350                  * inside reply messsage. All other errors are treated as
351                  * timeout, caller might try the negotiation repeatedly,
352                  * leave recovery decisions to general ptlrpc layer.
353                  *
354                  * FIXME maybe some other error code shouldn't be treated
355                  * as timeout.
356                  */
357                 param.status = rc;
358                 if (rc != -EACCES)
359                         param.status = -ETIMEDOUT;
360                 CDEBUG(D_SEC,
361                        "%s: ctx init req got %d, returning to userspace status %llu\n",
362                        obd->obd_name, rc, param.status);
363                 goto out_copy;
364         }
365
366         LASSERT(req->rq_repdata);
367         lsize = ctx_init_parse_reply(req->rq_repdata,
368                                      req_capsule_rep_need_swab(&req->rq_pill),
369                                      (char __user *)param.reply_buf,
370                                      param.reply_buf_size);
371         if (lsize < 0) {
372                 param.status = (int) lsize;
373                 goto out_copy;
374         }
375
376         param.status = 0;
377         param.reply_length = lsize;
378
379 out_copy:
380         if (copy_to_user(buffer, &param, sizeof(param)))
381                 rc = -EFAULT;
382         else
383                 rc = 0;
384
385         class_import_put(imp);
386         ptlrpc_req_finished(req);
387         RETURN(rc);
388 }
389
390 int gss_do_ctx_fini_rpc(struct gss_cli_ctx *gctx)
391 {
392         struct ptlrpc_cli_ctx   *ctx = &gctx->gc_base;
393         struct obd_import       *imp = ctx->cc_sec->ps_import;
394         struct ptlrpc_request   *req;
395         struct ptlrpc_user_desc *pud;
396         int                      rc;
397         ENTRY;
398
399         LASSERT(atomic_read(&ctx->cc_refcount) > 0);
400
401         if (cli_ctx_is_error(ctx) || !cli_ctx_is_uptodate(ctx)) {
402                 CDEBUG(D_SEC, "ctx %p(%u->%s) not uptodate, "
403                        "don't send destroy rpc\n", ctx,
404                        ctx->cc_vcred.vc_uid, sec2target_str(ctx->cc_sec));
405                 RETURN(0);
406         }
407
408         might_sleep();
409
410         CDEBUG(D_SEC, "%s ctx %p idx %#llx (%u->%s)\n",
411                sec_is_reverse(ctx->cc_sec) ?
412                "server finishing reverse" : "client finishing forward",
413                ctx, gss_handle_to_u64(&gctx->gc_handle),
414                ctx->cc_vcred.vc_uid, sec2target_str(ctx->cc_sec));
415
416         gctx->gc_proc = PTLRPC_GSS_PROC_DESTROY;
417
418         req = ptlrpc_request_alloc(imp, &RQF_SEC_CTX);
419         if (req == NULL) {
420                 CWARN("ctx %p(%u): fail to prepare rpc, destroy locally\n",
421                       ctx, ctx->cc_vcred.vc_uid);
422                 GOTO(out, rc = -ENOMEM);
423         }
424
425         rc = ptlrpc_request_bufs_pack(req, LUSTRE_OBD_VERSION, SEC_CTX_FINI,
426                                       NULL, ctx);
427         if (rc)
428                 GOTO(out_ref, rc);
429
430         /* fix the user desc */
431         if (req->rq_pack_udesc) {
432                 /* we rely the fact that this request is in AUTH mode,
433                  * and user_desc at offset 2. */
434                 pud = lustre_msg_buf(req->rq_reqbuf, 2, sizeof(*pud));
435                 LASSERT(pud);
436                 pud->pud_uid = pud->pud_fsuid = ctx->cc_vcred.vc_uid;
437                 pud->pud_gid = pud->pud_fsgid = ctx->cc_vcred.vc_gid;
438                 pud->pud_cap = 0;
439                 pud->pud_ngroups = 0;
440         }
441
442         req->rq_phase = RQ_PHASE_RPC;
443         rc = ptl_send_rpc(req, 1);
444         if (rc)
445                 CWARN("ctx %p(%u->%s): rpc error %d, destroy locally\n", ctx,
446                       ctx->cc_vcred.vc_uid, sec2target_str(ctx->cc_sec), rc);
447
448 out_ref:
449         ptlrpc_req_finished(req);
450 out:
451         RETURN(rc);
452 }
453
454 int __init gss_init_cli_upcall(void)
455 {
456         return 0;
457 }
458
459 void gss_exit_cli_upcall(void)
460 {
461 }