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