Whamcloud - gitweb
LU-17015 gss: support large kerberos token on client
[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, *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                 CERROR("no such obd %s\n", obdname);
259                 RETURN(-EINVAL);
260         }
261
262         if (unlikely(!obd->obd_set_up)) {
263                 CERROR("obd %s not setup\n", obdname);
264                 RETURN(-EINVAL);
265         }
266
267         spin_lock(&obd->obd_dev_lock);
268         if (obd->obd_stopping) {
269                 CERROR("obd %s has stopped\n", obdname);
270                 spin_unlock(&obd->obd_dev_lock);
271                 RETURN(-EINVAL);
272         }
273
274         if (strcmp(obd->obd_type->typ_name, LUSTRE_MDC_NAME) &&
275             strcmp(obd->obd_type->typ_name, LUSTRE_OSC_NAME) &&
276             strcmp(obd->obd_type->typ_name, LUSTRE_MGC_NAME) &&
277             strcmp(obd->obd_type->typ_name, LUSTRE_LWP_NAME) &&
278             strcmp(obd->obd_type->typ_name, LUSTRE_OSP_NAME)) {
279                 CERROR("obd %s is not a client device\n", obdname);
280                 spin_unlock(&obd->obd_dev_lock);
281                 RETURN(-EINVAL);
282         }
283         spin_unlock(&obd->obd_dev_lock);
284
285         with_imp_locked(obd, imp0, rc)
286                 imp = class_import_get(imp0);
287         if (rc) {
288                 CERROR("obd %s: import has gone\n", obd->obd_name);
289                 RETURN(-EINVAL);
290         }
291
292         if (imp->imp_deactive) {
293                 CERROR("import has been deactivated\n");
294                 class_import_put(imp);
295                 RETURN(-EINVAL);
296         }
297
298         req = ptlrpc_request_alloc_pack(imp, &RQF_SEC_CTX, LUSTRE_OBD_VERSION,
299                                         SEC_CTX_INIT);
300         if (req == NULL) {
301                 param.status = -ENOMEM;
302                 goto out_copy;
303         }
304
305         if (req->rq_cli_ctx->cc_sec->ps_id != param.secid) {
306                 CWARN("original secid %d, now has changed to %d, cancel this negotiation\n",
307                       param.secid, req->rq_cli_ctx->cc_sec->ps_id);
308                 param.status = -EINVAL;
309                 goto out_copy;
310         }
311
312         /* get token */
313         rc = ctx_init_pack_request(imp, req,
314                                    param.lustre_svc,
315                                    param.uid, param.gid,
316                                    param.send_token_size,
317                                    (char __user *)param.send_token);
318         if (rc) {
319                 param.status = rc;
320                 goto out_copy;
321         }
322
323         ptlrpc_request_set_replen(req);
324
325         rc = ptlrpc_queue_wait(req);
326         if (rc) {
327                 /* If any _real_ denial be made, we expect server return
328                  * -EACCES reply or return success but indicate gss error
329                  * inside reply messsage. All other errors are treated as
330                  * timeout, caller might try the negotiation repeatedly,
331                  * leave recovery decisions to general ptlrpc layer.
332                  *
333                  * FIXME maybe some other error code shouldn't be treated
334                  * as timeout.
335                  */
336                 param.status = rc;
337                 if (rc != -EACCES)
338                         param.status = -ETIMEDOUT;
339                 goto out_copy;
340         }
341
342         LASSERT(req->rq_repdata);
343         lsize = ctx_init_parse_reply(req->rq_repdata,
344                                      req_capsule_rep_need_swab(&req->rq_pill),
345                                      (char __user *)param.reply_buf,
346                                      param.reply_buf_size);
347         if (lsize < 0) {
348                 param.status = (int) lsize;
349                 goto out_copy;
350         }
351
352         param.status = 0;
353         param.reply_length = lsize;
354
355 out_copy:
356         if (copy_to_user(buffer, &param, sizeof(param)))
357                 rc = -EFAULT;
358         else
359                 rc = 0;
360
361         class_import_put(imp);
362         ptlrpc_req_finished(req);
363         RETURN(rc);
364 }
365
366 int gss_do_ctx_fini_rpc(struct gss_cli_ctx *gctx)
367 {
368         struct ptlrpc_cli_ctx   *ctx = &gctx->gc_base;
369         struct obd_import       *imp = ctx->cc_sec->ps_import;
370         struct ptlrpc_request   *req;
371         struct ptlrpc_user_desc *pud;
372         int                      rc;
373         ENTRY;
374
375         LASSERT(atomic_read(&ctx->cc_refcount) > 0);
376
377         if (cli_ctx_is_error(ctx) || !cli_ctx_is_uptodate(ctx)) {
378                 CDEBUG(D_SEC, "ctx %p(%u->%s) not uptodate, "
379                        "don't send destroy rpc\n", ctx,
380                        ctx->cc_vcred.vc_uid, sec2target_str(ctx->cc_sec));
381                 RETURN(0);
382         }
383
384         might_sleep();
385
386         CDEBUG(D_SEC, "%s ctx %p idx %#llx (%u->%s)\n",
387                sec_is_reverse(ctx->cc_sec) ?
388                "server finishing reverse" : "client finishing forward",
389                ctx, gss_handle_to_u64(&gctx->gc_handle),
390                ctx->cc_vcred.vc_uid, sec2target_str(ctx->cc_sec));
391
392         gctx->gc_proc = PTLRPC_GSS_PROC_DESTROY;
393
394         req = ptlrpc_request_alloc(imp, &RQF_SEC_CTX);
395         if (req == NULL) {
396                 CWARN("ctx %p(%u): fail to prepare rpc, destroy locally\n",
397                       ctx, ctx->cc_vcred.vc_uid);
398                 GOTO(out, rc = -ENOMEM);
399         }
400
401         rc = ptlrpc_request_bufs_pack(req, LUSTRE_OBD_VERSION, SEC_CTX_FINI,
402                                       NULL, ctx);
403         if (rc)
404                 GOTO(out_ref, rc);
405
406         /* fix the user desc */
407         if (req->rq_pack_udesc) {
408                 /* we rely the fact that this request is in AUTH mode,
409                  * and user_desc at offset 2. */
410                 pud = lustre_msg_buf(req->rq_reqbuf, 2, sizeof(*pud));
411                 LASSERT(pud);
412                 pud->pud_uid = pud->pud_fsuid = ctx->cc_vcred.vc_uid;
413                 pud->pud_gid = pud->pud_fsgid = ctx->cc_vcred.vc_gid;
414                 pud->pud_cap = 0;
415                 pud->pud_ngroups = 0;
416         }
417
418         req->rq_phase = RQ_PHASE_RPC;
419         rc = ptl_send_rpc(req, 1);
420         if (rc)
421                 CWARN("ctx %p(%u->%s): rpc error %d, destroy locally\n", ctx,
422                       ctx->cc_vcred.vc_uid, sec2target_str(ctx->cc_sec), rc);
423
424 out_ref:
425         ptlrpc_req_finished(req);
426 out:
427         RETURN(rc);
428 }
429
430 int __init gss_init_cli_upcall(void)
431 {
432         return 0;
433 }
434
435 void gss_exit_cli_upcall(void)
436 {
437 }