Whamcloud - gitweb
LU-12401 gss: fix checksum for Kerberos and SSK
[fs/lustre-release.git] / lustre / ptlrpc / gss / gss_internal.h
1 /*
2  * Modified from NFSv4 project for Lustre
3  *
4  * Copyright (c) 2007, 2010, Oracle and/or its affiliates. All rights reserved.
5  *
6  * Copyright (c) 2012, 2016, Intel Corporation.
7  *
8  * Author: Eric Mei <ericm@clusterfs.com>
9  */
10
11 #ifndef __PTLRPC_GSS_GSS_INTERNAL_H_
12 #define __PTLRPC_GSS_GSS_INTERNAL_H_
13
14 #include <crypto/hash.h>
15 #include <libcfs/libcfs_crypto.h>
16 #include <lustre_sec.h>
17
18 /*
19  * rawobj stuff
20  */
21 typedef struct netobj_s {
22         __u32           len;
23         __u8            data[0];
24 } netobj_t;
25
26 #define NETOBJ_EMPTY    ((netobj_t) { 0 })
27
28 typedef struct rawobj_s {
29         __u32           len;
30         __u8           *data;
31 } rawobj_t;
32
33 #define RAWOBJ_EMPTY    ((rawobj_t) { 0, NULL })
34
35 typedef struct rawobj_buf_s {
36         __u32           dataoff;
37         __u32           datalen;
38         __u32           buflen;
39         __u8           *buf;
40 } rawobj_buf_t;
41
42 int rawobj_empty(rawobj_t *obj);
43 int rawobj_alloc(rawobj_t *obj, char *buf, int len);
44 void rawobj_free(rawobj_t *obj);
45 int rawobj_equal(rawobj_t *a, rawobj_t *b);
46 int rawobj_dup(rawobj_t *dest, rawobj_t *src);
47 int rawobj_serialize(rawobj_t *obj, __u32 **buf, __u32 *buflen);
48 int rawobj_extract(rawobj_t *obj, __u32 **buf, __u32 *buflen);
49 int rawobj_extract_alloc(rawobj_t *obj, __u32 **buf, __u32 *buflen);
50 int rawobj_extract_local(rawobj_t *obj, __u32 **buf, __u32 *buflen);
51 int rawobj_extract_local_alloc(rawobj_t *obj, __u32 **buf, __u32 *buflen);
52 int rawobj_from_netobj(rawobj_t *rawobj, netobj_t *netobj);
53 int rawobj_from_netobj_alloc(rawobj_t *obj, netobj_t *netobj);
54
55 int buffer_extract_bytes(const void **buf, __u32 *buflen,
56                          void *res, __u32 reslen);
57
58 /*
59  * several timeout values. client refresh upcall timeout we using
60  * default in pipefs implemnetation.
61  */
62 #define __TIMEOUT_DELTA                 (10)
63
64 #define GSS_SECINIT_RPC_TIMEOUT                                         \
65         (obd_timeout < __TIMEOUT_DELTA ?                                \
66          __TIMEOUT_DELTA : obd_timeout - __TIMEOUT_DELTA)
67
68 #define GSS_SECFINI_RPC_TIMEOUT         (__TIMEOUT_DELTA)
69 #define GSS_SECSVC_UPCALL_TIMEOUT       (GSS_SECINIT_RPC_TIMEOUT)
70
71 /*
72  * default gc interval
73  */
74 #define GSS_GC_INTERVAL                 (60 * 60) /* 60 minutes */
75
76 static inline time64_t gss_round_ctx_expiry(time64_t expiry,
77                                             unsigned long sec_flags)
78 {
79         if (sec_flags & PTLRPC_SEC_FL_REVERSE)
80                 return expiry;
81
82         if (ktime_get_real_seconds() + __TIMEOUT_DELTA <= expiry)
83                 return expiry - __TIMEOUT_DELTA;
84
85         return expiry;
86 }
87
88 /*
89  * Max encryption element in block cipher algorithms.
90  */
91 #define GSS_MAX_CIPHER_BLOCK               (16)
92
93 /*
94  * XXX make it visible of kernel and lgssd/lsvcgssd
95  */
96 enum {
97         GSSD_INTERFACE_VERSION_V1 = 1,
98         GSSD_INTERFACE_VERSION_V2 = 2,
99         GSSD_INTERFACE_VERSION = GSSD_INTERFACE_VERSION_V2,
100 };
101
102 #define PTLRPC_GSS_VERSION              (1)
103
104
105 enum ptlrpc_gss_proc {
106         PTLRPC_GSS_PROC_DATA            = 0,
107         PTLRPC_GSS_PROC_INIT            = 1,
108         PTLRPC_GSS_PROC_CONTINUE_INIT   = 2,
109         PTLRPC_GSS_PROC_DESTROY         = 3,
110         PTLRPC_GSS_PROC_ERR             = 4,
111 };
112
113 enum ptlrpc_gss_tgt {
114         LUSTRE_GSS_TGT_MGS              = 0,
115         LUSTRE_GSS_TGT_MDS              = 1,
116         LUSTRE_GSS_TGT_OSS              = 2,
117 };
118
119 enum ptlrpc_gss_header_flags {
120         LUSTRE_GSS_PACK_BULK            = 1,
121         LUSTRE_GSS_PACK_USER            = 2,
122         LUSTRE_GSS_PACK_KCSUM           = 4,
123 };
124
125 static inline
126 __u32 import_to_gss_svc(struct obd_import *imp)
127 {
128         int cl_sp_to = LUSTRE_SP_ANY;
129
130         if (imp->imp_obd)
131                 cl_sp_to = imp->imp_obd->u.cli.cl_sp_to;
132
133         switch (cl_sp_to) {
134         case LUSTRE_SP_MDT:
135                 return LUSTRE_GSS_TGT_MDS;
136         case LUSTRE_SP_OST:
137                 return LUSTRE_GSS_TGT_OSS;
138         case LUSTRE_SP_MGC:
139         case LUSTRE_SP_MGS:
140                 return LUSTRE_GSS_TGT_MGS;
141         case LUSTRE_SP_CLI:
142         case LUSTRE_SP_ANY:
143         default:
144                 return 0;
145         }
146 }
147
148 /*
149  * following 3 header must have the same size and offset
150  */
151 struct gss_header {
152         __u8                    gh_version;     /* gss version */
153         __u8                    gh_sp;          /* sec part */
154         __u16                   gh_pad0;
155         __u32                   gh_flags;       /* wrap flags */
156         __u32                   gh_proc;        /* proc */
157         __u32                   gh_seq;         /* sequence */
158         __u32                   gh_svc;         /* service */
159         __u32                   gh_pad1;
160         __u32                   gh_pad2;
161         __u32                   gh_pad3;
162         netobj_t                gh_handle;      /* context handle */
163 };
164
165 struct gss_rep_header {
166         __u8                    gh_version;
167         __u8                    gh_sp;
168         __u16                   gh_pad0;
169         __u32                   gh_flags;
170         __u32                   gh_proc;
171         __u32                   gh_major;
172         __u32                   gh_minor;
173         __u32                   gh_seqwin;
174         __u32                   gh_pad2;
175         __u32                   gh_pad3;
176         netobj_t                gh_handle;
177 };
178
179 struct gss_err_header {
180         __u8                    gh_version;
181         __u8                    gh_sp;
182         __u16                   gh_pad0;
183         __u32                   gh_flags;
184         __u32                   gh_proc;
185         __u32                   gh_major;
186         __u32                   gh_minor;
187         __u32                   gh_pad1;
188         __u32                   gh_pad2;
189         __u32                   gh_pad3;
190         netobj_t                gh_handle;
191 };
192
193 /*
194  * part of wire context information send from client which be saved and
195  * used later by server.
196  */
197 struct gss_wire_ctx {
198         __u32                   gw_flags;
199         __u32                   gw_proc;
200         __u32                   gw_seq;
201         __u32                   gw_svc;
202         rawobj_t                gw_handle;
203 };
204
205 #define PTLRPC_GSS_MAX_HANDLE_SIZE      (8)
206 #define PTLRPC_GSS_HEADER_SIZE          (sizeof(struct gss_header) + \
207                                          PTLRPC_GSS_MAX_HANDLE_SIZE)
208
209
210 static inline __u64 gss_handle_to_u64(rawobj_t *handle)
211 {
212         if (handle->len != PTLRPC_GSS_MAX_HANDLE_SIZE)
213                 return -1;
214         return *((__u64 *) handle->data);
215 }
216
217 #define GSS_SEQ_WIN                     (2048)
218 #define GSS_SEQ_WIN_MAIN                GSS_SEQ_WIN
219 #define GSS_SEQ_WIN_BACK                (128)
220 #define GSS_SEQ_REPACK_THRESHOLD        (GSS_SEQ_WIN_MAIN / 2 + \
221                                          GSS_SEQ_WIN_MAIN / 4)
222
223 struct gss_svc_seq_data {
224         spinlock_t              ssd_lock;
225         /*
226          * highest sequence number seen so far, for main and back window
227          */
228         __u32                   ssd_max_main;
229         __u32                   ssd_max_back;
230         /*
231          * main and back window
232          * for i such that ssd_max - GSS_SEQ_WIN < i <= ssd_max, the i-th bit
233          * of ssd_win is nonzero iff sequence number i has been seen already.
234          */
235         unsigned long           ssd_win_main[GSS_SEQ_WIN_MAIN/BITS_PER_LONG];
236         unsigned long           ssd_win_back[GSS_SEQ_WIN_BACK/BITS_PER_LONG];
237 };
238
239 struct gss_svc_ctx {
240         struct gss_ctx         *gsc_mechctx;
241         struct gss_svc_seq_data gsc_seqdata;
242         rawobj_t                gsc_rvs_hdl;
243         __u32                   gsc_rvs_seq;
244         uid_t                   gsc_uid;
245         gid_t                   gsc_gid;
246         uid_t                   gsc_mapped_uid;
247         unsigned int            gsc_usr_root:1,
248                                 gsc_usr_mds:1,
249                                 gsc_usr_oss:1,
250                                 gsc_remote:1,
251                                 gsc_reverse:1;
252 };
253
254 struct gss_svc_reqctx {
255         struct ptlrpc_svc_ctx           src_base;
256         /*
257          * context
258          */
259         struct gss_wire_ctx             src_wirectx;
260         struct gss_svc_ctx             *src_ctx;
261         /*
262          * record place of bulk_sec_desc in request/reply buffer
263          */
264         struct ptlrpc_bulk_sec_desc    *src_reqbsd;
265         int                             src_reqbsd_size;
266         struct ptlrpc_bulk_sec_desc    *src_repbsd;
267         int                             src_repbsd_size;
268         /*
269          * flags
270          */
271         unsigned int                    src_init:1,
272                                         src_init_continue:1,
273                                         src_err_notify:1;
274         int                             src_reserve_len;
275 };
276
277 struct gss_cli_ctx {
278         struct ptlrpc_cli_ctx   gc_base;
279         __u32                   gc_flavor;
280         __u32                   gc_proc;
281         __u32                   gc_win;
282         atomic_t                gc_seq;
283         rawobj_t                gc_handle;
284         struct gss_ctx          *gc_mechctx;
285         /* handle for the buddy svc ctx */
286         rawobj_t                gc_svc_handle;
287 };
288
289 struct gss_cli_ctx_keyring {
290         struct gss_cli_ctx      gck_base;
291         struct key             *gck_key;
292         struct timer_list      *gck_timer;
293 };
294
295 struct gss_sec {
296         struct ptlrpc_sec       gs_base;
297         struct gss_api_mech     *gs_mech;
298         spinlock_t              gs_lock;
299         __u64                   gs_rvs_hdl;
300 };
301
302 struct gss_sec_pipefs {
303         struct gss_sec          gsp_base;
304         int                     gsp_chash_size; /* must be 2^n */
305         struct hlist_head       gsp_chash[0];
306 };
307
308 /*
309  * FIXME cleanup the keyring upcall mutexes
310  */
311 #define HAVE_KEYRING_UPCALL_SERIALIZED  1
312
313 struct gss_sec_keyring {
314         struct gss_sec          gsk_base;
315         /*
316          * all contexts listed here. access is protected by sec spinlock.
317          */
318         struct hlist_head       gsk_clist;
319         /*
320          * specially point to root ctx (only one at a time). access is
321          * protected by sec spinlock.
322          */
323         struct ptlrpc_cli_ctx  *gsk_root_ctx;
324         /*
325          * specially serialize upcalls for root context.
326          */
327         struct mutex                    gsk_root_uc_lock;
328
329 #ifdef HAVE_KEYRING_UPCALL_SERIALIZED
330         struct mutex            gsk_uc_lock;    /* serialize upcalls */
331 #endif
332 };
333
334 static inline struct gss_cli_ctx *ctx2gctx(struct ptlrpc_cli_ctx *ctx)
335 {
336         return container_of(ctx, struct gss_cli_ctx, gc_base);
337 }
338
339 static inline
340 struct gss_cli_ctx_keyring *ctx2gctx_keyring(struct ptlrpc_cli_ctx *ctx)
341 {
342         return container_of(ctx2gctx(ctx),
343                             struct gss_cli_ctx_keyring, gck_base);
344 }
345
346 static inline struct gss_sec *sec2gsec(struct ptlrpc_sec *sec)
347 {
348         return container_of(sec, struct gss_sec, gs_base);
349 }
350
351 static inline struct gss_sec_pipefs *sec2gsec_pipefs(struct ptlrpc_sec *sec)
352 {
353         return container_of(sec2gsec(sec), struct gss_sec_pipefs, gsp_base);
354 }
355
356 static inline struct gss_sec_keyring *sec2gsec_keyring(struct ptlrpc_sec *sec)
357 {
358         return container_of(sec2gsec(sec), struct gss_sec_keyring, gsk_base);
359 }
360
361
362 #define GSS_CTX_INIT_MAX_LEN            (1024)
363
364 /*
365  * This only guaranteed be enough for current krb5 des-cbc-crc . We might
366  * adjust this when new enc type or mech added in.
367  */
368 #define GSS_PRIVBUF_PREFIX_LEN         (32)
369 #define GSS_PRIVBUF_SUFFIX_LEN         (32)
370
371 static inline
372 struct gss_svc_reqctx *gss_svc_ctx2reqctx(struct ptlrpc_svc_ctx *ctx)
373 {
374         LASSERT(ctx);
375         return container_of(ctx, struct gss_svc_reqctx, src_base);
376 }
377
378 static inline
379 struct gss_svc_ctx *gss_svc_ctx2gssctx(struct ptlrpc_svc_ctx *ctx)
380 {
381         LASSERT(ctx);
382         return gss_svc_ctx2reqctx(ctx)->src_ctx;
383 }
384
385 /* sec_gss.c */
386 int gss_cli_ctx_match(struct ptlrpc_cli_ctx *ctx, struct vfs_cred *vcred);
387 int gss_cli_ctx_display(struct ptlrpc_cli_ctx *ctx, char *buf, int bufsize);
388 int gss_cli_ctx_sign(struct ptlrpc_cli_ctx *ctx, struct ptlrpc_request *req);
389 int gss_cli_ctx_verify(struct ptlrpc_cli_ctx *ctx, struct ptlrpc_request *req);
390 int gss_cli_ctx_seal(struct ptlrpc_cli_ctx *ctx, struct ptlrpc_request *req);
391 int gss_cli_ctx_unseal(struct ptlrpc_cli_ctx *ctx, struct ptlrpc_request *req);
392
393 int  gss_sec_install_rctx(struct obd_import *imp, struct ptlrpc_sec *sec,
394                           struct ptlrpc_cli_ctx *ctx);
395 int  gss_alloc_reqbuf(struct ptlrpc_sec *sec, struct ptlrpc_request *req,
396                       int msgsize);
397 void gss_free_reqbuf(struct ptlrpc_sec *sec, struct ptlrpc_request *req);
398 int  gss_alloc_repbuf(struct ptlrpc_sec *sec, struct ptlrpc_request *req,
399                       int msgsize);
400 void gss_free_repbuf(struct ptlrpc_sec *sec, struct ptlrpc_request *req);
401 int  gss_enlarge_reqbuf(struct ptlrpc_sec *sec, struct ptlrpc_request *req,
402                         int segment, int newsize);
403
404 int  gss_svc_accept(struct ptlrpc_sec_policy *policy,
405                     struct ptlrpc_request *req);
406 void gss_svc_invalidate_ctx(struct ptlrpc_svc_ctx *svc_ctx);
407 int  gss_svc_alloc_rs(struct ptlrpc_request *req, int msglen);
408 int  gss_svc_authorize(struct ptlrpc_request *req);
409 void gss_svc_free_rs(struct ptlrpc_reply_state *rs);
410 void gss_svc_free_ctx(struct ptlrpc_svc_ctx *ctx);
411
412 int cli_ctx_expire(struct ptlrpc_cli_ctx *ctx);
413 int cli_ctx_check_death(struct ptlrpc_cli_ctx *ctx);
414
415 int gss_copy_rvc_cli_ctx(struct ptlrpc_cli_ctx *cli_ctx,
416                          struct ptlrpc_svc_ctx *svc_ctx);
417
418 struct gss_header *gss_swab_header(struct lustre_msg *msg, int segment,
419                                    int swabbed);
420 netobj_t *gss_swab_netobj(struct lustre_msg *msg, int segment);
421
422 void gss_cli_ctx_uptodate(struct gss_cli_ctx *gctx);
423 int gss_pack_err_notify(struct ptlrpc_request *req, __u32 major, __u32 minor);
424 int gss_check_seq_num(struct gss_svc_seq_data *sd, __u32 seq_num, int set);
425
426 int gss_sec_create_common(struct gss_sec *gsec,
427                           struct ptlrpc_sec_policy *policy,
428                           struct obd_import *imp,
429                           struct ptlrpc_svc_ctx *ctx,
430                           struct sptlrpc_flavor *sf);
431 void gss_sec_destroy_common(struct gss_sec *gsec);
432 void gss_sec_kill(struct ptlrpc_sec *sec);
433
434 int gss_cli_ctx_init_common(struct ptlrpc_sec *sec,
435                             struct ptlrpc_cli_ctx *ctx,
436                             struct ptlrpc_ctx_ops *ctxops,
437                             struct vfs_cred *vcred);
438 int gss_cli_ctx_fini_common(struct ptlrpc_sec *sec,
439                             struct ptlrpc_cli_ctx *ctx);
440
441 void gss_cli_ctx_flags2str(unsigned long flags, char *buf, int bufsize);
442
443 /* gss_keyring.c */
444 #ifndef HAVE_GSS_KEYRING
445 static inline int  __init gss_init_keyring(void) { return 0; }
446 static inline void __exit gss_exit_keyring(void) { return; }
447 #else
448 int  __init gss_init_keyring(void);
449 void __exit gss_exit_keyring(void);
450 #endif
451
452 /* gss_pipefs.c */
453 #ifndef HAVE_GSS_PIPEFS
454 static inline int  __init gss_init_pipefs(void) { return 0; }
455 static inline void __exit gss_exit_pipefs(void) { return; }
456 #else
457 int  __init gss_init_pipefs(void);
458 void __exit gss_exit_pipefs(void);
459 #endif
460
461 /* gss_bulk.c */
462 int gss_cli_prep_bulk(struct ptlrpc_request *req,
463                       struct ptlrpc_bulk_desc *desc);
464 int gss_cli_ctx_wrap_bulk(struct ptlrpc_cli_ctx *ctx,
465                           struct ptlrpc_request *req,
466                           struct ptlrpc_bulk_desc *desc);
467 int gss_cli_ctx_unwrap_bulk(struct ptlrpc_cli_ctx *ctx,
468                             struct ptlrpc_request *req,
469                             struct ptlrpc_bulk_desc *desc);
470 int gss_svc_prep_bulk(struct ptlrpc_request *req,
471                       struct ptlrpc_bulk_desc *desc);
472 int gss_svc_unwrap_bulk(struct ptlrpc_request *req,
473                         struct ptlrpc_bulk_desc *desc);
474 int gss_svc_wrap_bulk(struct ptlrpc_request *req,
475                       struct ptlrpc_bulk_desc *desc);
476
477 /* gss_generic_token.c */
478 int g_token_size(rawobj_t *mech, unsigned int body_size);
479 void g_make_token_header(rawobj_t *mech, int body_size, unsigned char **buf);
480 __u32 g_verify_token_header(rawobj_t *mech, int *body_size,
481                             unsigned char **buf_in, int toksize);
482
483
484 /* gss_cli_upcall.c */
485 int gss_do_ctx_init_rpc(char __user *buffer, unsigned long count);
486 int gss_do_ctx_fini_rpc(struct gss_cli_ctx *gctx);
487
488 int  __init gss_init_cli_upcall(void);
489 void gss_exit_cli_upcall(void);
490
491 /* gss_svc_upcall.c */
492 __u64 gss_get_next_ctx_index(void);
493 int gss_svc_upcall_install_rvs_ctx(struct obd_import *imp,
494                                    struct gss_sec *gsec,
495                                    struct gss_cli_ctx *gctx);
496 int gss_svc_upcall_expire_rvs_ctx(rawobj_t *handle);
497 int gss_svc_upcall_dup_handle(rawobj_t *handle, struct gss_svc_ctx *ctx);
498 int gss_svc_upcall_update_sequence(rawobj_t *handle, __u32 seq);
499 int gss_svc_upcall_handle_init(struct ptlrpc_request *req,
500                                struct gss_svc_reqctx *grctx,
501                                struct gss_wire_ctx *gw,
502                                struct obd_device *target,
503                                __u32 lustre_svc,
504                                rawobj_t *rvs_hdl,
505                                rawobj_t *in_token);
506 struct gss_svc_ctx *gss_svc_upcall_get_ctx(struct ptlrpc_request *req,
507                                            struct gss_wire_ctx *gw);
508 void gss_svc_upcall_put_ctx(struct gss_svc_ctx *ctx);
509 void gss_svc_upcall_destroy_ctx(struct gss_svc_ctx *ctx);
510
511 int  __init gss_init_svc_upcall(void);
512 void gss_exit_svc_upcall(void);
513 extern unsigned int krb5_allow_old_client_csum;
514
515 /* lproc_gss.c */
516 void gss_stat_oos_record_cli(int behind);
517 void gss_stat_oos_record_svc(int phase, int replay);
518
519 int  __init gss_init_tunables(void);
520 void gss_exit_tunables(void);
521
522 /* gss_null_mech.c */
523 int __init init_null_module(void);
524 void cleanup_null_module(void);
525
526 /* gss_krb5_mech.c */
527 int __init init_kerberos_module(void);
528 void cleanup_kerberos_module(void);
529
530 /* gss_sk_mech.c */
531 #ifdef HAVE_OPENSSL_SSK
532 int __init init_sk_module(void);
533 void cleanup_sk_module(void);
534 #else
535 static inline int init_sk_module(void) { return 0; }
536 static inline void cleanup_sk_module(void) { return; }
537 #endif /* HAVE_OPENSSL_SSK */
538
539 /* debug */
540 static inline
541 void __dbg_memdump(char *name, void *ptr, int size)
542 {
543         char *buf, *p = (char *) ptr;
544         int bufsize = size * 2 + 1, i;
545
546         OBD_ALLOC(buf, bufsize);
547         if (!buf) {
548                 CDEBUG(D_ERROR, "DUMP ERROR: can't alloc %d bytes\n", bufsize);
549                 return;
550         }
551
552         for (i = 0; i < size; i++)
553                 sprintf(&buf[i+i], "%02x", (__u8) p[i]);
554         buf[size + size] = '\0';
555         LCONSOLE_INFO("DUMP %s@%p(%d): %s\n", name, ptr, size, buf);
556         OBD_FREE(buf, bufsize);
557 }
558
559 static inline unsigned int ll_read_key_usage(struct key *key)
560 {
561 #ifdef HAVE_KEY_USAGE_REFCOUNT
562         return refcount_read(&key->usage);
563 #else
564         return atomic_read(&key->usage);
565 #endif
566 }
567
568 #endif /* __PTLRPC_GSS_GSS_INTERNAL_H_ */