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