Whamcloud - gitweb
LU-16974 utils: make bandwidth options consistent
[fs/lustre-release.git] / lustre / ptlrpc / gss / gss_keyring.c
1 // SPDX-License-Identifier: GPL-2.0
2
3 /*
4  * Copyright (c) 2007, 2010, Oracle and/or its affiliates. All rights reserved.
5  * Use is subject to license terms.
6  *
7  * Copyright (c) 2012, 2014, Intel Corporation.
8  */
9
10 /*
11  * This file is part of Lustre, http://www.lustre.org/
12  *
13  * Author: Eric Mei <ericm@clusterfs.com>
14  */
15
16 #define DEBUG_SUBSYSTEM S_SEC
17 #include <linux/init.h>
18 #include <linux/module.h>
19 #include <linux/slab.h>
20 #include <linux/dcache.h>
21 #include <linux/fs.h>
22 #include <linux/crypto.h>
23 #include <linux/key.h>
24 #include <linux/keyctl.h>
25 #include <linux/key-type.h>
26 #include <linux/mutex.h>
27 #include <linux/list.h>
28 #include <asm/atomic.h>
29
30 #include <obd.h>
31 #include <obd_class.h>
32 #include <obd_support.h>
33 #include <uapi/linux/lustre/lustre_idl.h>
34 #include <lustre_sec.h>
35 #include <lustre_net.h>
36 #include <lustre_import.h>
37
38 #include "gss_err.h"
39 #include "gss_internal.h"
40 #include "gss_api.h"
41
42 #ifdef HAVE_GET_REQUEST_KEY_AUTH
43 #include <keys/request_key_auth-type.h>
44 #endif
45
46 static struct ptlrpc_sec_policy gss_policy_keyring;
47 static struct ptlrpc_ctx_ops gss_keyring_ctxops;
48 static struct key_type gss_key_type;
49
50 static int sec_install_rctx_kr(struct ptlrpc_sec *sec,
51                                struct ptlrpc_svc_ctx *svc_ctx);
52 static void request_key_unlink(struct key *key, bool fullsearch);
53
54 /*
55  * the timeout is only for the case that upcall child process die abnormally.
56  * in any other cases it should finally update kernel key.
57  *
58  * FIXME we'd better to incorporate the client & server side upcall timeouts
59  * into the framework of Adaptive Timeouts, but we need to figure out how to
60  * make sure that kernel knows the upcall processes is in-progress or died
61  * unexpectedly.
62  */
63 #define KEYRING_UPCALL_TIMEOUT  (obd_timeout + obd_timeout)
64
65 /* Check caller's namespace in gss_keyring upcall */
66 unsigned int gss_check_upcall_ns = 1;
67
68 /****************************************
69  * internal helpers                     *
70  ****************************************/
71
72 static inline void keyring_upcall_lock(struct gss_sec_keyring *gsec_kr)
73 {
74 #ifdef HAVE_KEYRING_UPCALL_SERIALIZED
75         mutex_lock(&gsec_kr->gsk_uc_lock);
76 #endif
77 }
78
79 static inline void keyring_upcall_unlock(struct gss_sec_keyring *gsec_kr)
80 {
81 #ifdef HAVE_KEYRING_UPCALL_SERIALIZED
82         mutex_unlock(&gsec_kr->gsk_uc_lock);
83 #endif
84 }
85
86 static inline void key_invalidate_locked(struct key *key)
87 {
88         set_bit(KEY_FLAG_INVALIDATED, &key->flags);
89 }
90
91 static void ctx_upcall_timeout_kr(cfs_timer_cb_arg_t data)
92 {
93         struct gss_cli_ctx_keyring *gctx_kr = cfs_from_timer(gctx_kr,
94                                                              data, gck_timer);
95         struct ptlrpc_cli_ctx *ctx = &(gctx_kr->gck_base.gc_base);
96         struct obd_import *imp = ctx->cc_sec->ps_import;
97         struct key *key = gctx_kr->gck_key;
98
99         if (key)
100                 CDEBUG(D_SEC,
101                        "%s: GSS context (%p) negotiation timeout, invalidating key (%p)\n",
102                        imp->imp_obd->obd_name, ctx, key);
103         else
104                 CDEBUG(D_SEC,
105                        "%s: GSS context (%p) negotiation timeout, ignoring already unlinked key\n",
106                        imp->imp_obd->obd_name, ctx);
107
108         cli_ctx_expire(ctx);
109         if (key)
110                 key_invalidate_locked(key);
111 }
112
113 static void ctx_start_timer_kr(struct ptlrpc_cli_ctx *ctx, time64_t timeout)
114 {
115         struct gss_cli_ctx_keyring *gctx_kr = ctx2gctx_keyring(ctx);
116         struct timer_list *timer = &gctx_kr->gck_timer;
117
118         LASSERT(timer);
119
120         CDEBUG(D_SEC, "ctx %p: start timer %llds\n", ctx, timeout);
121
122         cfs_timer_setup(timer, ctx_upcall_timeout_kr,
123                         (unsigned long)gctx_kr, 0);
124         timer->expires = cfs_time_seconds(timeout) + jiffies;
125         add_timer(timer);
126 }
127
128 /*
129  * caller should make sure no race with other threads
130  */
131 static
132 void ctx_clear_timer_kr(struct ptlrpc_cli_ctx *ctx)
133 {
134         struct gss_cli_ctx_keyring *gctx_kr = ctx2gctx_keyring(ctx);
135         struct timer_list          *timer = &gctx_kr->gck_timer;
136
137         CDEBUG(D_SEC, "ctx %p, key %p\n", ctx, gctx_kr->gck_key);
138
139         timer_delete_sync(timer);
140 }
141
142 static
143 struct ptlrpc_cli_ctx *ctx_create_kr(struct ptlrpc_sec *sec,
144                                      struct vfs_cred *vcred)
145 {
146         struct ptlrpc_cli_ctx      *ctx;
147         struct gss_cli_ctx_keyring *gctx_kr;
148
149         OBD_ALLOC_PTR(gctx_kr);
150         if (gctx_kr == NULL)
151                 return NULL;
152
153         cfs_timer_setup(&gctx_kr->gck_timer, NULL, 0, 0);
154
155         ctx = &gctx_kr->gck_base.gc_base;
156
157         if (gss_cli_ctx_init_common(sec, ctx, &gss_keyring_ctxops, vcred)) {
158                 OBD_FREE_PTR(gctx_kr);
159                 return NULL;
160         }
161
162         ctx->cc_expire = ktime_get_real_seconds() + KEYRING_UPCALL_TIMEOUT;
163         clear_bit(PTLRPC_CTX_NEW_BIT, &ctx->cc_flags);
164         atomic_inc(&ctx->cc_refcount); /* for the caller */
165
166         return ctx;
167 }
168
169 static void ctx_destroy_kr(struct ptlrpc_cli_ctx *ctx)
170 {
171         struct ptlrpc_sec               *sec = ctx->cc_sec;
172         struct gss_cli_ctx_keyring      *gctx_kr = ctx2gctx_keyring(ctx);
173
174         CDEBUG(D_SEC, "destroying ctx %p\n", ctx);
175
176         /* at this time the association with key has been broken. */
177         LASSERT(sec);
178         LASSERT(atomic_read(&sec->ps_refcount) > 0);
179         LASSERT(atomic_read(&sec->ps_nctx) > 0);
180         LASSERT(test_bit(PTLRPC_CTX_CACHED_BIT, &ctx->cc_flags) == 0);
181         LASSERT(gctx_kr->gck_key == NULL);
182
183         ctx_clear_timer_kr(ctx);
184
185         if (gss_cli_ctx_fini_common(sec, ctx))
186                 return;
187
188         OBD_FREE_PTR(gctx_kr);
189
190         atomic_dec(&sec->ps_nctx);
191         sptlrpc_sec_put(sec);
192 }
193
194 static void ctx_release_kr(struct ptlrpc_cli_ctx *ctx, int sync)
195 {
196         if (sync) {
197                 ctx_destroy_kr(ctx);
198         } else {
199                 atomic_inc(&ctx->cc_refcount);
200                 sptlrpc_gc_add_ctx(ctx);
201         }
202 }
203
204 static void ctx_put_kr(struct ptlrpc_cli_ctx *ctx, int sync)
205 {
206         LASSERT(atomic_read(&ctx->cc_refcount) > 0);
207
208         if (atomic_dec_and_test(&ctx->cc_refcount))
209                 ctx_release_kr(ctx, sync);
210 }
211
212 /*
213  * key <-> ctx association and rules:
214  * - ctx might not bind with any key
215  * - key/ctx binding is protected by key semaphore (if the key present)
216  * - key and ctx each take a reference of the other
217  * - ctx enlist/unlist is protected by ctx spinlock
218  * - never enlist a ctx after it's been unlisted
219  * - whoever do enlist should also do bind, lock key before enlist:
220  *   - lock key -> lock ctx -> enlist -> unlock ctx -> bind -> unlock key
221  * - whoever do unlist should also do unbind:
222  *   - lock key -> lock ctx -> unlist -> unlock ctx -> unbind -> unlock key
223  *   - lock ctx -> unlist -> unlock ctx -> lock key -> unbind -> unlock key
224  */
225
226 static inline void spin_lock_if(spinlock_t *lock, int condition)
227 {
228         if (condition)
229                 spin_lock(lock);
230 }
231
232 static inline void spin_unlock_if(spinlock_t *lock, int condition)
233 {
234         if (condition)
235                 spin_unlock(lock);
236 }
237
238 static void ctx_enlist_kr(struct ptlrpc_cli_ctx *ctx, int is_root, int locked)
239 {
240         struct ptlrpc_sec       *sec = ctx->cc_sec;
241         struct gss_sec_keyring  *gsec_kr = sec2gsec_keyring(sec);
242
243         LASSERT(!test_bit(PTLRPC_CTX_CACHED_BIT, &ctx->cc_flags));
244         LASSERT(atomic_read(&ctx->cc_refcount) > 0);
245
246         spin_lock_if(&sec->ps_lock, !locked);
247
248         atomic_inc(&ctx->cc_refcount);
249         set_bit(PTLRPC_CTX_CACHED_BIT, &ctx->cc_flags);
250         hlist_add_head(&ctx->cc_cache, &gsec_kr->gsk_clist);
251         if (is_root)
252                 gsec_kr->gsk_root_ctx = ctx;
253
254         spin_unlock_if(&sec->ps_lock, !locked);
255 }
256
257 /*
258  * Note after this get called, caller should not access ctx again because
259  * it might have been freed, unless caller hold at least one refcount of
260  * the ctx.
261  *
262  * return non-zero if we indeed unlist this ctx.
263  */
264 static int ctx_unlist_kr(struct ptlrpc_cli_ctx *ctx, int locked)
265 {
266         struct ptlrpc_sec       *sec = ctx->cc_sec;
267         struct gss_sec_keyring  *gsec_kr = sec2gsec_keyring(sec);
268
269         /* if hashed bit has gone, leave the job to somebody who is doing it */
270         if (test_and_clear_bit(PTLRPC_CTX_CACHED_BIT, &ctx->cc_flags) == 0)
271                 return 0;
272
273         /* drop ref inside spin lock to prevent race with other operations */
274         spin_lock_if(&sec->ps_lock, !locked);
275
276         if (gsec_kr->gsk_root_ctx == ctx)
277                 gsec_kr->gsk_root_ctx = NULL;
278         hlist_del_init(&ctx->cc_cache);
279         atomic_dec(&ctx->cc_refcount);
280
281         spin_unlock_if(&sec->ps_lock, !locked);
282
283         return 1;
284 }
285
286 /*
287  * Get specific payload. Newer kernels support 4 slots.
288  */
289 static void *
290 key_get_payload(struct key *key, unsigned int index)
291 {
292         void *key_ptr = NULL;
293
294 #ifdef HAVE_KEY_PAYLOAD_DATA_ARRAY
295         key_ptr = key->payload.data[index];
296 #else
297         if (!index)
298                 key_ptr = key->payload.data;
299 #endif
300         return key_ptr;
301 }
302
303 /*
304  * Set specific payload. Newer kernels support 4 slots.
305  */
306 static int key_set_payload(struct key *key, unsigned int index,
307                            struct ptlrpc_cli_ctx *ctx)
308 {
309         int rc = -EINVAL;
310
311 #ifdef HAVE_KEY_PAYLOAD_DATA_ARRAY
312         if (index < 4) {
313                 key->payload.data[index] = ctx;
314 #else
315         if (!index) {
316                 key->payload.data = ctx;
317 #endif
318                 rc = 0;
319         }
320         return rc;
321 }
322
323 /*
324  * bind a key with a ctx together.
325  * caller must hold write lock of the key, as well as ref on key & ctx.
326  */
327 static void bind_key_ctx(struct key *key, struct ptlrpc_cli_ctx *ctx)
328 {
329         LASSERT(atomic_read(&ctx->cc_refcount) > 0);
330         LASSERT(ll_read_key_usage(key) > 0);
331         LASSERT(ctx2gctx_keyring(ctx)->gck_key == NULL);
332         LASSERT(!key_get_payload(key, 0));
333
334         /* at this time context may or may not in list. */
335         key_get(key);
336         atomic_inc(&ctx->cc_refcount);
337         ctx2gctx_keyring(ctx)->gck_key = key;
338         LASSERT(!key_set_payload(key, 0, ctx));
339 }
340
341 /*
342  * unbind a key and a ctx.
343  * caller must hold write lock, as well as a ref of the key.
344  */
345 static void unbind_key_ctx(struct key *key, struct ptlrpc_cli_ctx *ctx)
346 {
347         /* give up on invalidated or empty key,
348          * someone else already took care of it
349          */
350         if (test_bit(KEY_FLAG_INVALIDATED, &key->flags) ||
351             key_get_payload(key, 0) != ctx) {
352                 CDEBUG(D_SEC, "key %08x already handled\n", key->serial);
353                 return;
354         }
355
356         /* must invalidate the key, or others may find it during lookup */
357         key_invalidate_locked(key);
358         request_key_unlink(key, false);
359
360         key_set_payload(key, 0, NULL);
361         ctx2gctx_keyring(ctx)->gck_key = NULL;
362
363         /* once ctx get split from key, the timer is meaningless */
364         ctx_clear_timer_kr(ctx);
365
366         ctx_put_kr(ctx, 1);
367         key_put(key);
368 }
369
370 /*
371  * given a ctx, unbind with its coupled key, if any.
372  * unbind could only be called once, so we don't worry the key be released
373  * by someone else.
374  */
375 static void unbind_ctx_kr(struct ptlrpc_cli_ctx *ctx)
376 {
377         struct key      *key = ctx2gctx_keyring(ctx)->gck_key;
378
379         if (key) {
380                 key_get(key);
381                 down_write(&key->sem);
382                 unbind_key_ctx(key, ctx);
383                 up_write(&key->sem);
384                 key_put(key);
385         }
386 }
387
388 /*
389  * given a key, unbind with its coupled ctx, if any.
390  * caller must hold write lock, as well as a ref of the key.
391  */
392 static void unbind_key_locked(struct key *key)
393 {
394         struct ptlrpc_cli_ctx *ctx = key_get_payload(key, 0);
395
396         if (ctx)
397                 unbind_key_ctx(key, ctx);
398 }
399
400 /*
401  * unlist a ctx, and unbind from coupled key
402  */
403 static void kill_ctx_kr(struct ptlrpc_cli_ctx *ctx)
404 {
405         if (ctx_unlist_kr(ctx, 0))
406                 unbind_ctx_kr(ctx);
407 }
408
409 /*
410  * given a key, unlist and unbind with the coupled ctx (if any).
411  * caller must hold write lock, as well as a ref of the key.
412  */
413 static void kill_key_locked(struct key *key)
414 {
415         struct ptlrpc_cli_ctx *ctx = key_get_payload(key, 0);
416
417         if (ctx && ctx_unlist_kr(ctx, 0))
418                 unbind_key_locked(key);
419 }
420
421 /*
422  * caller should hold one ref on contexts in freelist.
423  */
424 static void dispose_ctx_list_kr(struct hlist_head *freelist)
425 {
426         struct hlist_node *next;
427         struct ptlrpc_cli_ctx   *ctx;
428         struct gss_cli_ctx      *gctx;
429
430         hlist_for_each_entry_safe(ctx, next, freelist, cc_cache) {
431                 hlist_del_init(&ctx->cc_cache);
432
433                 /* reverse ctx: update current seq to buddy svcctx if exist.
434                  * ideally this should be done at gss_cli_ctx_finalize(), but
435                  * the ctx destroy could be delayed by:
436                  *  1) ctx still has reference;
437                  *  2) ctx destroy is asynchronous;
438                  * and reverse import call inval_all_ctx() require this be done
439                  * _immediately_ otherwise newly created reverse ctx might copy
440                  * the very old sequence number from svcctx. */
441                 gctx = ctx2gctx(ctx);
442                 if (!rawobj_empty(&gctx->gc_svc_handle) &&
443                     sec_is_reverse(gctx->gc_base.cc_sec)) {
444                         gss_svc_upcall_update_sequence(&gctx->gc_svc_handle,
445                                         (__u32) atomic_read(&gctx->gc_seq));
446                 }
447
448                 /* we need to wakeup waiting reqs here. the context might
449                  * be forced released before upcall finished, then the
450                  * late-arrived downcall can't find the ctx even. */
451                 sptlrpc_cli_ctx_wakeup(ctx);
452
453                 unbind_ctx_kr(ctx);
454                 ctx_put_kr(ctx, 0);
455         }
456 }
457
458 /*
459  * lookup a root context directly in a sec, return root ctx with a
460  * reference taken or NULL.
461  */
462 static
463 struct ptlrpc_cli_ctx * sec_lookup_root_ctx_kr(struct ptlrpc_sec *sec)
464 {
465         struct gss_sec_keyring  *gsec_kr = sec2gsec_keyring(sec);
466         struct ptlrpc_cli_ctx   *ctx = NULL;
467         time64_t now = ktime_get_real_seconds();
468
469         spin_lock(&sec->ps_lock);
470
471         ctx = gsec_kr->gsk_root_ctx;
472
473         /* Need to find valid rev ctx if we do not have one yet,
474          * or if it is expired.
475          */
476         if (unlikely(sec_is_reverse(sec)) &&
477             (ctx == NULL || ctx->cc_expire < now)) {
478                 struct ptlrpc_cli_ctx   *tmp;
479
480                 /* For reverse context, browse list and pick the one with
481                  * shortest expire time and that has not expired yet.
482                  * This one is most likely to have an established peer context
483                  * on client side.
484                  */
485                 hlist_for_each_entry(tmp, &gsec_kr->gsk_clist, cc_cache) {
486                         if (ctx == NULL || ctx->cc_expire == 0 ||
487                             (tmp->cc_expire > now &&
488                              tmp->cc_expire < ctx->cc_expire) ||
489                             (ctx->cc_expire < now &&
490                              tmp->cc_expire > ctx->cc_expire)) {
491                                 ctx = tmp;
492                                 /* promote to be root_ctx */
493                                 gsec_kr->gsk_root_ctx = ctx;
494                         }
495                 }
496         }
497
498         if (ctx) {
499                 LASSERT(atomic_read(&ctx->cc_refcount) > 0);
500                 LASSERT(!hlist_empty(&gsec_kr->gsk_clist));
501                 atomic_inc(&ctx->cc_refcount);
502         }
503
504         spin_unlock(&sec->ps_lock);
505
506         return ctx;
507 }
508
509 #define RVS_CTX_EXPIRE_NICE    (10)
510
511 static
512 void rvs_sec_install_root_ctx_kr(struct ptlrpc_sec *sec,
513                                  struct ptlrpc_cli_ctx *new_ctx,
514                                  struct key *key)
515 {
516         struct gss_sec_keyring *gsec_kr = sec2gsec_keyring(sec);
517         struct ptlrpc_cli_ctx *ctx;
518         struct hlist_node *next;
519         time64_t now;
520
521         ENTRY;
522         LASSERT(sec_is_reverse(sec));
523
524         spin_lock(&sec->ps_lock);
525
526         now = ktime_get_real_seconds();
527
528         /* set all existing ctxs short expiry */
529         hlist_for_each_entry_safe(ctx, next, &gsec_kr->gsk_clist, cc_cache) {
530                 if (ctx->cc_expire > now + RVS_CTX_EXPIRE_NICE) {
531                         ctx->cc_early_expire = 1;
532                         ctx->cc_expire = now + RVS_CTX_EXPIRE_NICE;
533                 } else if (ctx != gsec_kr->gsk_root_ctx &&
534                            ctx->cc_expire < now) {
535                         /* unlist expired context to remove it from gsk_clist */
536                         if (ctx_unlist_kr(ctx, 1)) {
537                                 /* release unlisted ctx to destroy it */
538                                 set_bit(PTLRPC_CTX_DEAD_BIT, &ctx->cc_flags);
539                                 ctx_release_kr(ctx, 1);
540                         }
541                 }
542         }
543
544         /* If there's root_ctx there, instead obsolete the current
545          * immediately, we leave it continue operating for a little while.
546          * hopefully when the first backward rpc with newest ctx send out,
547          * the client side already have the peer ctx well established.
548          */
549         ctx_enlist_kr(new_ctx, gsec_kr->gsk_root_ctx ? 0 : 1, 1);
550
551         if (key)
552                 bind_key_ctx(key, new_ctx);
553
554         spin_unlock(&sec->ps_lock);
555 }
556
557 static void construct_key_desc(void *buf, int bufsize,
558                                struct ptlrpc_sec *sec, uid_t uid)
559 {
560         snprintf(buf, bufsize, "%d@%x", uid, sec->ps_id);
561         ((char *)buf)[bufsize - 1] = '\0';
562 }
563
564 /****************************************
565  * sec apis                             *
566  ****************************************/
567
568 static
569 struct ptlrpc_sec * gss_sec_create_kr(struct obd_import *imp,
570                                       struct ptlrpc_svc_ctx *svcctx,
571                                       struct sptlrpc_flavor *sf)
572 {
573         struct gss_sec_keyring  *gsec_kr;
574         ENTRY;
575
576         OBD_ALLOC(gsec_kr, sizeof(*gsec_kr));
577         if (gsec_kr == NULL)
578                 RETURN(NULL);
579
580         INIT_HLIST_HEAD(&gsec_kr->gsk_clist);
581         gsec_kr->gsk_root_ctx = NULL;
582         mutex_init(&gsec_kr->gsk_root_uc_lock);
583 #ifdef HAVE_KEYRING_UPCALL_SERIALIZED
584         mutex_init(&gsec_kr->gsk_uc_lock);
585 #endif
586
587         if (gss_sec_create_common(&gsec_kr->gsk_base, &gss_policy_keyring,
588                                   imp, svcctx, sf))
589                 goto err_free;
590
591         if (svcctx != NULL &&
592             sec_install_rctx_kr(&gsec_kr->gsk_base.gs_base, svcctx)) {
593                 gss_sec_destroy_common(&gsec_kr->gsk_base);
594                 goto err_free;
595         }
596
597         RETURN(&gsec_kr->gsk_base.gs_base);
598
599 err_free:
600         OBD_FREE(gsec_kr, sizeof(*gsec_kr));
601         RETURN(NULL);
602 }
603
604 static
605 void gss_sec_destroy_kr(struct ptlrpc_sec *sec)
606 {
607         struct gss_sec          *gsec = sec2gsec(sec);
608         struct gss_sec_keyring  *gsec_kr = sec2gsec_keyring(sec);
609
610         CDEBUG(D_SEC, "destroy %s@%p\n", sec->ps_policy->sp_name, sec);
611
612         LASSERT(atomic_read(&sec->ps_nctx) == 0);
613         LASSERT(hlist_empty(&gsec_kr->gsk_clist));
614         LASSERT(gsec_kr->gsk_root_ctx == NULL);
615
616         gss_sec_destroy_common(gsec);
617
618         OBD_FREE(gsec_kr, sizeof(*gsec_kr));
619 }
620
621 static inline int user_is_root(struct ptlrpc_sec *sec, struct vfs_cred *vcred)
622 {
623         /* except the ROOTONLY flag, treat it as root user only if real uid
624          * is 0, euid/fsuid being 0 are handled as setuid scenarios */
625         if (sec_is_rootonly(sec) || (vcred->vc_uid == 0))
626                 return 1;
627         else
628                 return 0;
629 }
630
631 /*
632  * When lookup_user_key is available use the kernel API rather than directly
633  * accessing the uid_keyring and session_keyring via the current process
634  * credentials.
635  */
636 #ifdef HAVE_LOOKUP_USER_KEY
637
638 #ifdef HAVE_KEY_NEED_UNLINK
639 /* from Linux security/keys/internal.h: */
640 #  ifndef KEY_LOOKUP_PARTIAL
641 #    define KEY_LOOKUP_PARTIAL 0x2
642 #  endif
643 #else
644 #  define KEY_NEED_UNLINK 0
645 #  ifndef KEY_LOOKUP_FOR_UNLINK
646 #    define KEY_LOOKUP_FOR_UNLINK 0x4
647 #  endif
648 #  define KEY_LOOKUP_PARTIAL KEY_LOOKUP_FOR_UNLINK
649 #endif /* HAVE_KEY_NEED_UNLINK */
650
651 static struct key *_user_key(key_serial_t id)
652 {
653         key_ref_t ref;
654
655         might_sleep();
656         ref = lookup_user_key(id, KEY_LOOKUP_PARTIAL, KEY_NEED_UNLINK);
657         if (IS_ERR(ref))
658                 return NULL;
659         return key_ref_to_ptr(ref);
660 }
661
662 static inline struct key *get_user_session_keyring(const struct cred *cred)
663 {
664         return _user_key(KEY_SPEC_USER_SESSION_KEYRING);
665 }
666
667 static inline struct key *get_user_keyring(const struct cred *cred)
668 {
669         return _user_key(KEY_SPEC_USER_KEYRING);
670 }
671
672 static inline struct key *get_session_keyring(const struct cred *cred)
673 {
674         return _user_key(KEY_SPEC_SESSION_KEYRING);
675 }
676 #else
677 static inline struct key *get_user_session_keyring(const struct cred *cred)
678 {
679         return key_get(cred->user->session_keyring);
680 }
681
682 static inline struct key *get_user_keyring(const struct cred *cred)
683 {
684         return key_get(cred->user->uid_keyring);
685 }
686
687 static inline struct key *get_session_keyring(const struct cred *cred)
688 {
689         return key_get(cred->session_keyring);
690 }
691 #endif
692
693 /*
694  * Get the appropriate destination keyring for the request.
695  *
696  * The keyring selected is returned with an extra reference upon it which the
697  * caller must release.
698  */
699 /*
700  * Function inspired from the kernel's one, unfortunately not exported.
701  */
702 static int construct_get_dest_keyring(struct key **_dest_keyring)
703 {
704         struct key *dest_keyring = *_dest_keyring;
705         const struct cred *cred = current_cred();
706
707         if (dest_keyring) {
708                 /* the caller supplied one */
709                 key_get(dest_keyring);
710                 return 0;
711         }
712
713         switch (cred->jit_keyring) {
714         case KEY_REQKEY_DEFL_DEFAULT:
715         case KEY_REQKEY_DEFL_REQUESTOR_KEYRING:
716 #ifdef HAVE_GET_REQUEST_KEY_AUTH
717                 if (cred->request_key_auth) {
718                         struct request_key_auth *rka;
719                         struct key *authkey = cred->request_key_auth;
720
721                         down_read(&authkey->sem);
722                         rka = get_request_key_auth(authkey);
723                         if (!test_bit(KEY_FLAG_REVOKED, &authkey->flags))
724                                 dest_keyring = key_get(rka->dest_keyring);
725                         up_read(&authkey->sem);
726                         if (dest_keyring)
727                                 break;
728                 }
729 #endif
730                 fallthrough;
731         case KEY_REQKEY_DEFL_THREAD_KEYRING:
732                 dest_keyring = key_get(cred->thread_keyring);
733                 if (dest_keyring)
734                         break;
735                 fallthrough;
736         case KEY_REQKEY_DEFL_PROCESS_KEYRING:
737                 dest_keyring = key_get(cred->process_keyring);
738                 if (dest_keyring)
739                         break;
740                 fallthrough;
741         case KEY_REQKEY_DEFL_SESSION_KEYRING:
742                 dest_keyring = get_session_keyring(cred);
743                 if (dest_keyring) {
744                         if (!test_bit(KEY_FLAG_REVOKED, &dest_keyring->flags))
745                                 break;
746                         key_put(dest_keyring);
747                 }
748                 fallthrough;
749         case KEY_REQKEY_DEFL_USER_SESSION_KEYRING:
750                 dest_keyring = get_user_session_keyring(cred);
751                 break;
752         case KEY_REQKEY_DEFL_USER_KEYRING:
753                 dest_keyring = get_user_keyring(cred);
754                 break;
755         case KEY_REQKEY_DEFL_GROUP_KEYRING:
756         default:
757                 LBUG();
758         }
759
760         *_dest_keyring = dest_keyring;
761         return 0;
762 }
763
764 /*
765  * Unlink key from its keyring, which was linked during request_key().
766  */
767 static void request_key_unlink(struct key *key, bool fullsearch)
768 {
769         kuid_t kuid_orig = current_cred()->user->uid;
770 #ifdef HAVE_USER_UID_KEYRING
771         struct key *root_uid_keyring = NULL;
772 #endif
773         const struct cred *old_cred = NULL;
774         struct cred *new_cred = NULL;
775         struct key *ring = NULL;
776         uid_t uid, key_uid;
777         int res;
778
779         uid = from_kuid(current_user_ns(), kuid_orig);
780         key_uid = from_kuid(&init_user_ns, key->uid);
781         /* unlink key with user's creds if it's a user key */
782         if (key_uid != uid) {
783                 new_cred = prepare_creds();
784                 if (new_cred == NULL)
785                         goto search;
786
787                 new_cred->uid = key->uid;
788                 new_cred->user->uid = key->uid;
789                 if (new_cred->user_ns != &init_user_ns) {
790                         put_user_ns(new_cred->user_ns);
791                         new_cred->user_ns = get_user_ns(&init_user_ns);
792                 }
793 #ifdef HAVE_USER_UID_KEYRING
794                 root_uid_keyring = current_cred()->user->uid_keyring;
795                 new_cred->user->uid_keyring = NULL;
796 #endif
797                 old_cred = override_creds(new_cred);
798         }
799
800         /* User keys are linked to the user keyring. So get it now. */
801         if (key_uid && !fullsearch) {
802                 /* Getting a key(ring) normally increases its refcount by 1.
803                  * But if we overrode creds above, calling get_user_keyring()
804                  * will add one more ref, because of the user switch.
805                  */
806                 ring = get_user_keyring(current_cred());
807         } else {
808 search:
809                 if (construct_get_dest_keyring(&ring))
810                         ring = NULL;
811         }
812
813         if (ring) {
814                 res = key_unlink(ring, key);
815                 CDEBUG(D_SEC,
816                        "Unlink key %08x (%p) from keyring %08x: %d\n",
817                        key->serial, key, ring->serial, res);
818                 /* matches key_get()/get_user_keyring() above */
819                 key_put(ring);
820         } else {
821                 CDEBUG(D_SEC,
822                        "Missing keyring, key %08x (%p) could not be unlinked, ignored\n",
823                        key->serial, key);
824         }
825
826         if (old_cred) {
827                 revert_creds(old_cred);
828                 put_cred(new_cred);
829                 current_cred()->user->uid = kuid_orig;
830 #ifdef HAVE_USER_UID_KEYRING
831                 /* We are switching creds back, so need to drop ref on keyring
832                  * for kernel implementation based on user keyring pinned from
833                  * the user_struct struct.
834                  */
835                 if (key_uid && !fullsearch)
836                         key_put(ring);
837                 if (root_uid_keyring)
838                         current_cred()->user->uid_keyring = root_uid_keyring;
839 #endif
840         }
841 }
842
843 /**
844  * \retval a valid context on success
845  * \retval -ev error number or NULL on error
846  */
847 static
848 struct ptlrpc_cli_ctx * gss_sec_lookup_ctx_kr(struct ptlrpc_sec *sec,
849                                               struct vfs_cred *vcred,
850                                               int create, int remove_dead)
851 {
852         struct obd_import *imp = sec->ps_import;
853         struct gss_sec_keyring *gsec_kr = sec2gsec_keyring(sec);
854         struct ptlrpc_cli_ctx *ctx = NULL;
855         unsigned int is_root = 0, create_new = 0;
856         const struct cred *old_cred = NULL;
857         struct cred *new_cred = NULL;
858         struct key *key;
859         char desc[24];
860         char *coinfo;
861         int coinfo_size;
862         const char *sec_part_flags = "";
863         char svc_flag = '-';
864         pid_t caller_pid;
865         struct lnet_nid primary;
866         ENTRY;
867
868         LASSERT(imp != NULL);
869
870         is_root = user_is_root(sec, vcred);
871
872         /* a little bit optimization for root context */
873         if (is_root) {
874                 ctx = sec_lookup_root_ctx_kr(sec);
875                 /*
876                  * Only lookup directly for REVERSE sec, which should
877                  * always succeed.
878                  */
879                 if (ctx || sec_is_reverse(sec))
880                         RETURN(ctx);
881         }
882
883         if (!create)
884                 RETURN(ERR_PTR(-ENODATA));
885
886         /* for root context, obtain lock and check again, this time hold
887          * the root upcall lock, make sure nobody else populated new root
888          * context after last check.
889          */
890         if (is_root) {
891                 mutex_lock(&gsec_kr->gsk_root_uc_lock);
892
893                 ctx = sec_lookup_root_ctx_kr(sec);
894                 if (ctx)
895                         goto out;
896
897                 /* update reverse handle for root user */
898                 sec2gsec(sec)->gs_rvs_hdl = gss_get_next_ctx_index();
899
900                 switch (sec->ps_part) {
901                 case LUSTRE_SP_MDT:
902                         sec_part_flags = "m";
903                         break;
904                 case LUSTRE_SP_OST:
905                         sec_part_flags = "o";
906                         break;
907                 case LUSTRE_SP_MGC:
908                         sec_part_flags = "rmo";
909                         break;
910                 case LUSTRE_SP_CLI:
911                         sec_part_flags = "r";
912                         break;
913                 case LUSTRE_SP_MGS:
914                 default:
915                         LBUG();
916                 }
917
918                 switch (SPTLRPC_FLVR_SVC(sec->ps_flvr.sf_rpc)) {
919                 case SPTLRPC_SVC_NULL:
920                         svc_flag = 'n';
921                         break;
922                 case SPTLRPC_SVC_AUTH:
923                         svc_flag = 'a';
924                         break;
925                 case SPTLRPC_SVC_INTG:
926                         svc_flag = 'i';
927                         break;
928                 case SPTLRPC_SVC_PRIV:
929                         svc_flag = 'p';
930                         break;
931                 default:
932                         LBUG();
933                 }
934         }
935
936         /* in case of setuid, key will be constructed as owner of fsuid/fsgid,
937          * but we do authentication based on real uid/gid. the key permission
938          * bits will be exactly as POS_ALL, so only processes who subscribed
939          * this key could have the access, although the quota might be counted
940          * on others (fsuid/fsgid).
941          *
942          * keyring will use fsuid/fsgid as upcall parameters, so we have to
943          * encode real uid/gid into callout info.
944          */
945
946         /* But first we need to make sure the obd type is supported */
947         if (strcmp(imp->imp_obd->obd_type->typ_name, LUSTRE_MDC_NAME) &&
948             strcmp(imp->imp_obd->obd_type->typ_name, LUSTRE_OSC_NAME) &&
949             strcmp(imp->imp_obd->obd_type->typ_name, LUSTRE_MGC_NAME) &&
950             strcmp(imp->imp_obd->obd_type->typ_name, LUSTRE_LWP_NAME) &&
951             strcmp(imp->imp_obd->obd_type->typ_name, LUSTRE_OSP_NAME)) {
952                 CERROR("obd %s is not a supported device\n",
953                        imp->imp_obd->obd_name);
954                 GOTO(out, ctx = NULL);
955         }
956
957         construct_key_desc(desc, sizeof(desc), sec, vcred->vc_uid);
958
959         /* callout info format:
960          * secid:mech:uid:gid:sec_flags:svc_flag:svc_type:peer_nid:target_uuid:
961          * self_nid:pid
962          */
963         coinfo_size = sizeof(struct obd_uuid) + MAX_OBD_NAME + 64;
964         OBD_ALLOC(coinfo, coinfo_size);
965         if (coinfo == NULL)
966                 goto out;
967
968         /* Last callout parameter is pid of process whose namespace will be used
969          * for credentials' retrieval.
970          */
971         if (gss_check_upcall_ns) {
972                 /* For user's credentials (in which case sec_part_flags is
973                  * empty), use current PID instead of import's reference
974                  * PID to get reference namespace.
975                  */
976                 if (sec_part_flags[0] == '\0')
977                         caller_pid = current->pid;
978                 else
979                         caller_pid = imp->imp_sec_refpid;
980         } else {
981                 /* Do not switch namespace in gss keyring upcall. */
982                 caller_pid = 0;
983         }
984         primary = imp->imp_connection->c_self;
985         LNetPrimaryNID(&primary);
986
987         /* FIXME !! Needs to support larger NIDs */
988         snprintf(coinfo, coinfo_size, "%d:%s:%u:%u:%s:%c:%d:%#llx:%s:%#llx:%d",
989                  sec->ps_id, sec2gsec(sec)->gs_mech->gm_name,
990                  vcred->vc_uid, vcred->vc_gid,
991                  sec_part_flags, svc_flag, import_to_gss_svc(imp),
992                  lnet_nid_to_nid4(&imp->imp_connection->c_peer.nid),
993                  imp->imp_obd->obd_name,
994                  lnet_nid_to_nid4(&primary),
995                  caller_pid);
996
997         CDEBUG(D_SEC, "requesting key for %s\n", desc);
998
999         if (vcred->vc_uid) {
1000                 /* If the session keyring is revoked, it must not be used by
1001                  * request_key(), otherwise we would get -EKEYREVOKED and
1002                  * the user keyring would not even be searched.
1003                  * So prepare new creds with no session keyring.
1004                  */
1005                 if (current_cred()->session_keyring &&
1006                     test_bit(KEY_FLAG_REVOKED,
1007                              &current_cred()->session_keyring->flags)) {
1008                         new_cred = prepare_creds();
1009                         if (new_cred) {
1010                                 new_cred->session_keyring = NULL;
1011                                 old_cred = override_creds(new_cred);
1012                         }
1013                 }
1014         }
1015
1016         keyring_upcall_lock(gsec_kr);
1017         key = request_key(&gss_key_type, desc, coinfo);
1018         keyring_upcall_unlock(gsec_kr);
1019         if (old_cred) {
1020                 revert_creds(old_cred);
1021                 put_cred(new_cred);
1022         }
1023
1024         OBD_FREE(coinfo, coinfo_size);
1025
1026         if (IS_ERR(key)) {
1027                 CERROR("%s: request key failed for uid %d: rc = %ld\n",
1028                        imp->imp_obd->obd_name, vcred->vc_uid,
1029                        PTR_ERR(key));
1030                 ctx = ERR_CAST(key);
1031                 goto out;
1032         }
1033         CDEBUG(D_SEC, "obtained key %08x for %s\n", key->serial, desc);
1034
1035         /* once payload.data was pointed to a ctx, it never changes until
1036          * we de-associate them; but parallel request_key() may return
1037          * a key with payload.data == NULL at the same time. so we still
1038          * need wirtelock of key->sem to serialize them.
1039          */
1040         down_write(&key->sem);
1041
1042         ctx = key_get_payload(key, 0);
1043         if (likely(ctx)) {
1044                 LASSERT(atomic_read(&ctx->cc_refcount) >= 1);
1045                 LASSERT(ctx2gctx_keyring(ctx)->gck_key == key);
1046                 LASSERT(ll_read_key_usage(key) >= 2);
1047
1048                 /* simply take a ref and return. it's upper layer's
1049                  * responsibility to detect & replace dead ctx.
1050                  */
1051                 atomic_inc(&ctx->cc_refcount);
1052         } else {
1053                 /* pre initialization with a cli_ctx. this can't be done in
1054                  * key_instantiate() because we'v no enough information
1055                  * there.
1056                  */
1057                 ctx = ctx_create_kr(sec, vcred);
1058                 if (ctx != NULL) {
1059                         ctx_enlist_kr(ctx, is_root, 0);
1060                         bind_key_ctx(key, ctx);
1061
1062                         ctx_start_timer_kr(ctx, KEYRING_UPCALL_TIMEOUT);
1063
1064                         CDEBUG(D_SEC, "installed key %p <-> ctx %p (sec %p)\n",
1065                                key, ctx, sec);
1066                 } else {
1067                         CDEBUG(D_SEC, "invalidating key %08x (%p)\n",
1068                                key->serial, key);
1069                         key_invalidate_locked(key);
1070                 }
1071
1072                 create_new = 1;
1073         }
1074
1075         up_write(&key->sem);
1076
1077         /* We want user keys to be linked to the user keyring (see call to
1078          * keyctl_instantiate() from prepare_and_instantiate() in userspace).
1079          * But internally request_key() links the key to the session or
1080          * user session keyring, depending on jit_keyring value. Avoid that by
1081          * unlinking the key from this keyring. It will spare
1082          * us pain when we need to remove the key later on.
1083          */
1084         if (!is_root || create_new)
1085                 request_key_unlink(key, true);
1086
1087         key_put(key);
1088 out:
1089         if (is_root)
1090                 mutex_unlock(&gsec_kr->gsk_root_uc_lock);
1091         RETURN(ctx);
1092 }
1093
1094 static
1095 void gss_sec_release_ctx_kr(struct ptlrpc_sec *sec,
1096                             struct ptlrpc_cli_ctx *ctx,
1097                             int sync)
1098 {
1099         LASSERT(atomic_read(&sec->ps_refcount) > 0);
1100         LASSERT(atomic_read(&ctx->cc_refcount) == 0);
1101         ctx_release_kr(ctx, sync);
1102 }
1103
1104 /*
1105  * flush context of normal user, we must resort to keyring itself to find out
1106  * contexts which belong to me.
1107  *
1108  * Note here we suppose only to flush _my_ context, the "uid" will
1109  * be ignored in the search.
1110  */
1111 static void flush_user_ctx_cache_kr(struct ptlrpc_sec *sec, uid_t uid,
1112                                     int grace, int force)
1113 {
1114         const struct cred *old_cred = NULL;
1115         struct cred *new_cred = NULL;
1116         struct key *key;
1117         char desc[24];
1118
1119         /* nothing to do for reverse or rootonly sec */
1120         if (sec_is_reverse(sec) || sec_is_rootonly(sec))
1121                 return;
1122
1123         construct_key_desc(desc, sizeof(desc), sec, uid);
1124
1125         if (uid) {
1126                 /* If the session keyring is revoked, it must not be used by
1127                  * request_key(), otherwise we would get -EKEYREVOKED and
1128                  * the user keyring would not even be searched.
1129                  * So prepare new creds with no session keyring.
1130                  */
1131                 if (current_cred()->session_keyring &&
1132                     test_bit(KEY_FLAG_REVOKED,
1133                              &current_cred()->session_keyring->flags)) {
1134                         new_cred = prepare_creds();
1135                         if (new_cred) {
1136                                 new_cred->session_keyring = NULL;
1137                                 old_cred = override_creds(new_cred);
1138                         }
1139                 }
1140         }
1141
1142         /* there should be only one valid key, but we put it in the
1143          * loop in case of any weird cases */
1144         for (;;) {
1145                 key = request_key(&gss_key_type, desc, NULL);
1146                 if (IS_ERR(key)) {
1147                         CDEBUG(D_SEC,
1148                                "No more key found for current user: rc=%ld\n",
1149                                 PTR_ERR(key));
1150                         break;
1151                 }
1152
1153                 down_write(&key->sem);
1154
1155                 kill_key_locked(key);
1156
1157                 /* kill_key_locked() should usually revoke the key, but we
1158                  * invalidate it as well to completely get rid of it.
1159                  */
1160                 key_invalidate_locked(key);
1161
1162                 up_write(&key->sem);
1163                 key_put(key);
1164         }
1165
1166         if (old_cred) {
1167                 revert_creds(old_cred);
1168                 put_cred(new_cred);
1169         }
1170 }
1171
1172 /*
1173  * flush context of root or all, we iterate through the list.
1174  */
1175 static
1176 void flush_spec_ctx_cache_kr(struct ptlrpc_sec *sec, uid_t uid, int grace,
1177                              int force)
1178 {
1179         struct gss_sec_keyring  *gsec_kr;
1180         struct hlist_head        freelist = HLIST_HEAD_INIT;
1181         struct hlist_node *next;
1182         struct ptlrpc_cli_ctx   *ctx;
1183         ENTRY;
1184
1185         gsec_kr = sec2gsec_keyring(sec);
1186
1187         spin_lock(&sec->ps_lock);
1188         hlist_for_each_entry_safe(ctx, next, &gsec_kr->gsk_clist,
1189                                   cc_cache) {
1190                 LASSERT(atomic_read(&ctx->cc_refcount) > 0);
1191
1192                 if (uid != -1 && uid != ctx->cc_vcred.vc_uid)
1193                         continue;
1194
1195                 /* at this moment there's at least 2 base reference:
1196                  * key association and in-list. */
1197                 if (atomic_read(&ctx->cc_refcount) > 2) {
1198                         if (!force)
1199                                 continue;
1200                         CWARN("flush busy ctx %p(%u->%s, extra ref %d)\n",
1201                               ctx, ctx->cc_vcred.vc_uid,
1202                               sec2target_str(ctx->cc_sec),
1203                               atomic_read(&ctx->cc_refcount) - 2);
1204                 }
1205
1206                 set_bit(PTLRPC_CTX_DEAD_BIT, &ctx->cc_flags);
1207                 if (!grace)
1208                         clear_bit(PTLRPC_CTX_UPTODATE_BIT, &ctx->cc_flags);
1209
1210                 atomic_inc(&ctx->cc_refcount);
1211
1212                 if (ctx_unlist_kr(ctx, 1)) {
1213                         hlist_add_head(&ctx->cc_cache, &freelist);
1214                 } else {
1215                         LASSERT(atomic_read(&ctx->cc_refcount) >= 2);
1216                         atomic_dec(&ctx->cc_refcount);
1217                 }
1218         }
1219         spin_unlock(&sec->ps_lock);
1220
1221         dispose_ctx_list_kr(&freelist);
1222         EXIT;
1223 }
1224
1225 static
1226 int gss_sec_flush_ctx_cache_kr(struct ptlrpc_sec *sec,
1227                                uid_t uid, int grace, int force)
1228 {
1229         ENTRY;
1230
1231         CDEBUG(D_SEC, "sec %p(%d, nctx %d), uid %d, grace %d, force %d\n",
1232                sec, atomic_read(&sec->ps_refcount),
1233                atomic_read(&sec->ps_nctx),
1234                uid, grace, force);
1235
1236         if (uid != -1 && uid != 0)
1237                 flush_user_ctx_cache_kr(sec, uid, grace, force);
1238         else
1239                 flush_spec_ctx_cache_kr(sec, uid, grace, force);
1240
1241         RETURN(0);
1242 }
1243
1244 static
1245 void gss_sec_gc_ctx_kr(struct ptlrpc_sec *sec)
1246 {
1247         struct gss_sec_keyring *gsec_kr = sec2gsec_keyring(sec);
1248         struct hlist_head freelist = HLIST_HEAD_INIT;
1249         struct ptlrpc_cli_ctx *ctx;
1250         struct gss_cli_ctx *gctx;
1251         struct hlist_node *next;
1252         ENTRY;
1253
1254         CDEBUG(D_SEC, "running gc\n");
1255
1256         spin_lock(&sec->ps_lock);
1257         hlist_for_each_entry_safe(ctx, next, &gsec_kr->gsk_clist,
1258                                   cc_cache) {
1259                 LASSERT(atomic_read(&ctx->cc_refcount) > 0);
1260
1261                 atomic_inc(&ctx->cc_refcount);
1262
1263                 if (cli_ctx_check_death(ctx) && ctx_unlist_kr(ctx, 1)) {
1264                         gctx = ctx2gctx(ctx);
1265
1266                         hlist_add_head(&ctx->cc_cache, &freelist);
1267                         CWARN("%s: cleaning gss ctx hdl %#llx:%#llx\n",
1268                               ctx->cc_sec->ps_import->imp_obd->obd_name,
1269                               gss_handle_to_u64(&gctx->gc_handle),
1270                               gss_handle_to_u64(&gctx->gc_svc_handle));
1271                 } else {
1272                         LASSERT(atomic_read(&ctx->cc_refcount) >= 2);
1273                         atomic_dec(&ctx->cc_refcount);
1274                 }
1275         }
1276         spin_unlock(&sec->ps_lock);
1277
1278         dispose_ctx_list_kr(&freelist);
1279         EXIT;
1280 }
1281
1282 static
1283 int gss_sec_display_kr(struct ptlrpc_sec *sec, struct seq_file *seq)
1284 {
1285         struct gss_sec_keyring *gsec_kr = sec2gsec_keyring(sec);
1286         struct hlist_node *next;
1287         struct ptlrpc_cli_ctx *ctx;
1288         struct gss_cli_ctx *gctx;
1289         struct ptlrpc_connection *conn;
1290         time64_t now = ktime_get_real_seconds();
1291
1292         ENTRY;
1293         spin_lock(&sec->ps_lock);
1294         hlist_for_each_entry_safe(ctx, next, &gsec_kr->gsk_clist,
1295                                   cc_cache) {
1296                 struct key *key;
1297                 char flags_str[40];
1298                 char mech[40];
1299
1300                 gctx = ctx2gctx(ctx);
1301                 key = ctx2gctx_keyring(ctx)->gck_key;
1302                 if (sec_is_reverse(sec) &&
1303                     ctx->cc_sec && ctx->cc_sec->ps_import &&
1304                     ctx->cc_sec->ps_import->imp_connection)
1305                         conn = ctx->cc_sec->ps_import->imp_connection;
1306                 else
1307                         conn = NULL;
1308
1309                 gss_cli_ctx_flags2str(ctx->cc_flags,
1310                                       flags_str, sizeof(flags_str));
1311
1312                 if (gctx->gc_mechctx)
1313                         lgss_display(gctx->gc_mechctx, mech, sizeof(mech));
1314                 else
1315                         snprintf(mech, sizeof(mech), "N/A");
1316                 mech[sizeof(mech) - 1] = '\0';
1317
1318                 seq_printf(seq,
1319                            "- { %s%s%suid: %u, ctxref: %d, expire: %lld, delta: %lld, flags: [%s], seq: %d, win: %u, key: %08x, keyref: %d, hdl: \"%#llx:%#llx\", mech: \"%s\" }\n",
1320                            conn ? "peer_nid: " : "",
1321                            conn ? libcfs_nidstr(&conn->c_peer.nid) : "",
1322                            conn ? ", " : "",
1323                            ctx->cc_vcred.vc_uid, atomic_read(&ctx->cc_refcount),
1324                            ctx->cc_expire,
1325                            ctx->cc_expire ?  ctx->cc_expire - now : 0,
1326                            flags_str, atomic_read(&gctx->gc_seq),
1327                            gctx->gc_win, key ? key->serial : 0,
1328                            key ? ll_read_key_usage(key) : 0,
1329                            gss_handle_to_u64(&gctx->gc_handle),
1330                            gss_handle_to_u64(&gctx->gc_svc_handle),
1331                            mech);
1332         }
1333         spin_unlock(&sec->ps_lock);
1334
1335         RETURN(0);
1336 }
1337
1338 /****************************************
1339  * cli_ctx apis                         *
1340  ****************************************/
1341
1342 static
1343 int gss_cli_ctx_refresh_kr(struct ptlrpc_cli_ctx *ctx)
1344 {
1345         /* upcall is already on the way */
1346         struct gss_cli_ctx *gctx = ctx ? ctx2gctx(ctx) : NULL;
1347
1348         /* record latest sequence number in buddy svcctx */
1349         if (gctx && !rawobj_empty(&gctx->gc_svc_handle) &&
1350             sec_is_reverse(gctx->gc_base.cc_sec)) {
1351                 return gss_svc_upcall_update_sequence(&gctx->gc_svc_handle,
1352                                              (__u32)atomic_read(&gctx->gc_seq));
1353         }
1354         return 0;
1355 }
1356
1357 static
1358 int gss_cli_ctx_validate_kr(struct ptlrpc_cli_ctx *ctx)
1359 {
1360         LASSERT(atomic_read(&ctx->cc_refcount) > 0);
1361         LASSERT(ctx->cc_sec);
1362
1363         if (cli_ctx_check_death(ctx)) {
1364                 kill_ctx_kr(ctx);
1365                 return 1;
1366         }
1367
1368         if (cli_ctx_is_ready(ctx))
1369                 return 0;
1370         return 1;
1371 }
1372
1373 static
1374 void gss_cli_ctx_die_kr(struct ptlrpc_cli_ctx *ctx, int grace)
1375 {
1376         LASSERT(atomic_read(&ctx->cc_refcount) > 0);
1377         LASSERT(ctx->cc_sec);
1378
1379         cli_ctx_expire(ctx);
1380         kill_ctx_kr(ctx);
1381 }
1382
1383 /****************************************
1384  * (reverse) service                    *
1385  ****************************************/
1386
1387 /*
1388  * reverse context could have nothing to do with keyrings. here we still keep
1389  * the version which bind to a key, for future reference.
1390  */
1391 #define HAVE_REVERSE_CTX_NOKEY
1392
1393 #ifdef HAVE_REVERSE_CTX_NOKEY
1394
1395 static
1396 int sec_install_rctx_kr(struct ptlrpc_sec *sec,
1397                         struct ptlrpc_svc_ctx *svc_ctx)
1398 {
1399         struct ptlrpc_cli_ctx *cli_ctx;
1400         struct vfs_cred vcred = { .vc_uid = 0 };
1401         int rc;
1402
1403         LASSERT(sec);
1404         LASSERT(svc_ctx);
1405
1406         cli_ctx = ctx_create_kr(sec, &vcred);
1407         if (cli_ctx == NULL)
1408                 return -ENOMEM;
1409
1410         rc = gss_copy_rvc_cli_ctx(cli_ctx, svc_ctx);
1411         if (rc) {
1412                 CERROR("failed copy reverse cli ctx: %d\n", rc);
1413
1414                 ctx_put_kr(cli_ctx, 1);
1415                 return rc;
1416         }
1417
1418         rvs_sec_install_root_ctx_kr(sec, cli_ctx, NULL);
1419
1420         ctx_put_kr(cli_ctx, 1);
1421
1422         return 0;
1423 }
1424
1425 #else /* ! HAVE_REVERSE_CTX_NOKEY */
1426
1427 static
1428 int sec_install_rctx_kr(struct ptlrpc_sec *sec,
1429                         struct ptlrpc_svc_ctx *svc_ctx)
1430 {
1431         struct ptlrpc_cli_ctx *cli_ctx = NULL;
1432         struct key *key;
1433         struct vfs_cred vcred = { .vc_uid = 0 };
1434         char desc[64];
1435         int rc;
1436
1437         LASSERT(sec);
1438         LASSERT(svc_ctx);
1439         CWARN("called\n");
1440
1441         construct_key_desc(desc, sizeof(desc), sec, 0);
1442
1443         key = key_alloc(&gss_key_type, desc, 0, 0,
1444                         KEY_POS_ALL | KEY_USR_ALL, 1);
1445         if (IS_ERR(key)) {
1446                 CERROR("failed to alloc key: %ld\n", PTR_ERR(key));
1447                 return PTR_ERR(key);
1448         }
1449
1450         rc = key_instantiate_and_link(key, NULL, 0, NULL, NULL);
1451         if (rc) {
1452                 CERROR("failed to instantiate key: %d\n", rc);
1453                 goto err_revoke;
1454         }
1455
1456         down_write(&key->sem);
1457
1458         LASSERT(!key_get_payload(key, 0));
1459
1460         cli_ctx = ctx_create_kr(sec, &vcred);
1461         if (cli_ctx == NULL) {
1462                 rc = -ENOMEM;
1463                 goto err_up;
1464         }
1465
1466         rc = gss_copy_rvc_cli_ctx(cli_ctx, svc_ctx);
1467         if (rc) {
1468                 CERROR("failed copy reverse cli ctx: %d\n", rc);
1469                 goto err_put;
1470         }
1471
1472         rvs_sec_install_root_ctx_kr(sec, cli_ctx, key);
1473
1474         ctx_put_kr(cli_ctx, 1);
1475         up_write(&key->sem);
1476
1477         rc = 0;
1478         CWARN("ok!\n");
1479 out:
1480         key_put(key);
1481         return rc;
1482
1483 err_put:
1484         ctx_put_kr(cli_ctx, 1);
1485 err_up:
1486         up_write(&key->sem);
1487 err_revoke:
1488         key_revoke(key);
1489         goto out;
1490 }
1491
1492 #endif /* HAVE_REVERSE_CTX_NOKEY */
1493
1494 /****************************************
1495  * service apis                         *
1496  ****************************************/
1497
1498 static
1499 int gss_svc_accept_kr(struct ptlrpc_request *req)
1500 {
1501         return gss_svc_accept(&gss_policy_keyring, req);
1502 }
1503
1504 static
1505 int gss_svc_install_rctx_kr(struct obd_import *imp,
1506                             struct ptlrpc_svc_ctx *svc_ctx)
1507 {
1508         struct ptlrpc_sec *sec;
1509         int                rc;
1510
1511         sec = sptlrpc_import_sec_ref(imp);
1512         LASSERT(sec);
1513
1514         rc = sec_install_rctx_kr(sec, svc_ctx);
1515         sptlrpc_sec_put(sec);
1516
1517         return rc;
1518 }
1519
1520 /****************************************
1521  * key apis                             *
1522  ****************************************/
1523
1524 static
1525 #ifdef HAVE_KEY_TYPE_INSTANTIATE_2ARGS
1526 int gss_kt_instantiate(struct key *key, struct key_preparsed_payload *prep)
1527 {
1528         const void *data = prep->data;
1529         size_t datalen = prep->datalen;
1530 #else
1531 int gss_kt_instantiate(struct key *key, const void *data, size_t datalen)
1532 {
1533 #endif
1534         struct key *keyring;
1535         int uid, rc;
1536
1537         ENTRY;
1538
1539         CDEBUG(D_SEC, "instantiating key %08x (%p)\n", key->serial, key);
1540
1541         if (data != NULL || datalen != 0) {
1542                 CERROR("invalid: data %p, len %lu\n", data, (long)datalen);
1543                 RETURN(-EINVAL);
1544         }
1545
1546         if (key_get_payload(key, 0)) {
1547                 CERROR("key already have payload\n");
1548                 RETURN(-EINVAL);
1549         }
1550
1551         /* link the key to session keyring, so following context negotiation
1552          * rpc fired from user space could find this key. This will be unlinked
1553          * automatically when upcall processes die.
1554          *
1555          * we can't do this through keyctl from userspace, because the upcall
1556          * might be neither possessor nor owner of the key (setuid).
1557          *
1558          * the session keyring is created upon upcall, and don't change all
1559          * the way until upcall finished, so rcu lock is not needed here.
1560          *
1561          * But for end users, link to the user keyring. This simplifies key
1562          * management, makes them shared accross all user sessions, and avoids
1563          * unfortunate key leak if lfs flushctx is not called at user logout.
1564          */
1565         uid = from_kuid(&init_user_ns, current_uid());
1566         if (uid == 0)
1567                 keyring = get_session_keyring(current_cred());
1568         else
1569                 keyring = get_user_keyring(current_cred());
1570
1571         lockdep_off();
1572         rc = key_link(keyring, key);
1573         lockdep_on();
1574         if (unlikely(rc)) {
1575                 CERROR("failed to link key %08x to keyring %08x: %d\n",
1576                        key->serial, keyring->serial, rc);
1577                 GOTO(out, rc);
1578         }
1579
1580         CDEBUG(D_SEC,
1581               "key %08x (%p) linked to keyring %08x and instantiated, ctx %p\n",
1582                key->serial, key, keyring->serial, key_get_payload(key, 0));
1583 out:
1584         key_put(keyring);
1585         RETURN(rc);
1586 }
1587
1588 /*
1589  * called with key semaphore write locked. it means we can operate
1590  * on the context without fear of loosing refcount.
1591  */
1592 static
1593 #ifdef HAVE_KEY_TYPE_INSTANTIATE_2ARGS
1594 int gss_kt_update(struct key *key, struct key_preparsed_payload *prep)
1595 {
1596         const void *data = prep->data;
1597         __u32 datalen32 = (__u32) prep->datalen;
1598 #else
1599 int gss_kt_update(struct key *key, const void *data, size_t datalen)
1600 {
1601         __u32 datalen32 = (__u32) datalen;
1602 #endif
1603         struct ptlrpc_cli_ctx *ctx = key_get_payload(key, 0);
1604         struct gss_cli_ctx *gctx;
1605         rawobj_t tmpobj = RAWOBJ_EMPTY;
1606         int rc;
1607         ENTRY;
1608
1609         CDEBUG(D_SEC, "updating key %08x (%p)\n", key->serial, key);
1610
1611         if (data == NULL || datalen32 == 0) {
1612                 CWARN("invalid: data %p, len %lu\n", data, (long)datalen32);
1613                 RETURN(-EINVAL);
1614         }
1615
1616         /* if upcall finished negotiation too fast (mostly likely because
1617          * of local error happened) and call kt_update(), the ctx
1618          * might be still NULL. but the key will finally be associate
1619          * with a context, or be revoked. if key status is fine, return
1620          * -EAGAIN to allow userspace sleep a while and call again. */
1621         if (ctx == NULL) {
1622                 CDEBUG(D_SEC, "update too soon: key %08x (%p) flags %lx\n",
1623                        key->serial, key, key->flags);
1624
1625                 rc = key_validate(key);
1626                 if (rc == 0)
1627                         RETURN(-EAGAIN);
1628                 else
1629                         RETURN(rc);
1630         }
1631
1632         LASSERT(atomic_read(&ctx->cc_refcount) > 0);
1633         LASSERT(ctx->cc_sec);
1634
1635         ctx_clear_timer_kr(ctx);
1636
1637         /* don't proceed if already refreshed */
1638         if (cli_ctx_is_refreshed(ctx)) {
1639                 CWARN("ctx already done refresh\n");
1640                 RETURN(0);
1641         }
1642
1643         sptlrpc_cli_ctx_get(ctx);
1644         gctx = ctx2gctx(ctx);
1645
1646         rc = buffer_extract_bytes(&data, &datalen32, &gctx->gc_win,
1647                                   sizeof(gctx->gc_win));
1648         if (rc) {
1649                 CERROR("failed extract seq_win\n");
1650                 goto out;
1651         }
1652
1653         if (gctx->gc_win == 0) {
1654                 __u32   nego_rpc_err, nego_gss_err;
1655
1656                 rc = buffer_extract_bytes(&data, &datalen32, &nego_rpc_err,
1657                                           sizeof(nego_rpc_err));
1658                 if (rc) {
1659                         CERROR("cannot extract RPC: rc = %d\n", rc);
1660                         goto out;
1661                 }
1662
1663                 rc = buffer_extract_bytes(&data, &datalen32, &nego_gss_err,
1664                                           sizeof(nego_gss_err));
1665                 if (rc) {
1666                         CERROR("failed to extract gss rc = %d\n", rc);
1667                         goto out;
1668                 }
1669
1670                 CERROR("negotiation: rpc err %d, gss err %x\n",
1671                        nego_rpc_err, nego_gss_err);
1672
1673                 rc = nego_rpc_err ? nego_rpc_err : -EACCES;
1674         } else {
1675                 rc = rawobj_extract_local_alloc(&gctx->gc_handle,
1676                                                 (__u32 **) &data, &datalen32);
1677                 if (rc) {
1678                         CERROR("failed extract handle\n");
1679                         goto out;
1680                 }
1681
1682                 rc = rawobj_extract_local(&tmpobj,
1683                                           (__u32 **) &data, &datalen32);
1684                 if (rc) {
1685                         CERROR("failed extract mech\n");
1686                         goto out;
1687                 }
1688
1689                 rc = lgss_import_sec_context(&tmpobj,
1690                                              sec2gsec(ctx->cc_sec)->gs_mech,
1691                                              &gctx->gc_mechctx);
1692                 if (rc != GSS_S_COMPLETE)
1693                         CERROR("failed import context\n");
1694                 else
1695                         rc = 0;
1696         }
1697 out:
1698         CDEBUG(D_SEC, "update of key %08x (%p): %d\n", key->serial, key, rc);
1699         /* we don't care what current status of this ctx, even someone else
1700          * is operating on the ctx at the same time. we just add up our own
1701          * opinions here. */
1702         if (rc == 0) {
1703                 gss_cli_ctx_uptodate(gctx);
1704                 /* In case of success, only the companion key for root ctx can
1705                  * be unbound. User keys are required to be able to retrieve
1706                  * the associated gss context.
1707                  */
1708                 if (ctx->cc_vcred.vc_uid == 0)
1709                         unbind_key_ctx(key, ctx);
1710         } else {
1711                 /* In case of failure, unbind the companion key for all contexts
1712                  * i.e root and regular users. It will also invalidate the key.
1713                  */
1714                 unbind_key_ctx(key, ctx);
1715                 if (rc != -ERESTART)
1716                         set_bit(PTLRPC_CTX_ERROR_BIT, &ctx->cc_flags);
1717                 cli_ctx_expire(ctx);
1718         }
1719
1720         /* let user space think it's a success */
1721         sptlrpc_cli_ctx_put(ctx, 1);
1722         RETURN(0);
1723 }
1724
1725 #ifndef HAVE_KEY_MATCH_DATA
1726 static int
1727 gss_kt_match(const struct key *key, const void *desc)
1728 {
1729         return strcmp(key->description, (const char *) desc) == 0 &&
1730                 !test_bit(KEY_FLAG_REVOKED, &key->flags);
1731 }
1732 #else /* ! HAVE_KEY_MATCH_DATA */
1733 static bool
1734 gss_kt_match(const struct key *key, const struct key_match_data *match_data)
1735 {
1736         const char *desc = match_data->raw_data;
1737
1738         return strcmp(key->description, desc) == 0 &&
1739                 !test_bit(KEY_FLAG_REVOKED, &key->flags);
1740 }
1741
1742 /*
1743  * Preparse the match criterion.
1744  */
1745 static int gss_kt_match_preparse(struct key_match_data *match_data)
1746 {
1747         match_data->lookup_type = KEYRING_SEARCH_LOOKUP_DIRECT;
1748         match_data->cmp = gss_kt_match;
1749         return 0;
1750 }
1751 #endif /* HAVE_KEY_MATCH_DATA */
1752
1753 static
1754 void gss_kt_destroy(struct key *key)
1755 {
1756         ENTRY;
1757         LASSERT(!key_get_payload(key, 0));
1758         CDEBUG(D_SEC, "destroy key %08x %p\n", key->serial, key);
1759         EXIT;
1760 }
1761
1762 static
1763 void gss_kt_describe(const struct key *key, struct seq_file *s)
1764 {
1765         if (key->description == NULL)
1766                 seq_puts(s, "[null]");
1767         else
1768                 seq_puts(s, key->description);
1769 }
1770
1771 static void gss_kt_revoke(struct key *key)
1772 {
1773         CDEBUG(D_SEC, "revoking key %08x (%p) ref %d\n",
1774                key->serial, key, ll_read_key_usage(key));
1775         kill_key_locked(key);
1776         CDEBUG(D_SEC, "key %08x (%p) revoked ref %d\n",
1777                key->serial, key, ll_read_key_usage(key));
1778 }
1779
1780 static struct key_type gss_key_type =
1781 {
1782         .name           = "lgssc",
1783         .def_datalen    = 0,
1784         .instantiate    = gss_kt_instantiate,
1785         .update         = gss_kt_update,
1786 #ifdef HAVE_KEY_MATCH_DATA
1787         .match_preparse = gss_kt_match_preparse,
1788 #else
1789         .match          = gss_kt_match,
1790 #endif
1791         .destroy        = gss_kt_destroy,
1792         .describe       = gss_kt_describe,
1793         .revoke         = gss_kt_revoke,
1794 };
1795
1796 /****************************************
1797  * lustre gss keyring policy            *
1798  ****************************************/
1799
1800 static struct ptlrpc_ctx_ops gss_keyring_ctxops = {
1801         .match                  = gss_cli_ctx_match,
1802         .refresh                = gss_cli_ctx_refresh_kr,
1803         .validate               = gss_cli_ctx_validate_kr,
1804         .die                    = gss_cli_ctx_die_kr,
1805         .sign                   = gss_cli_ctx_sign,
1806         .verify                 = gss_cli_ctx_verify,
1807         .seal                   = gss_cli_ctx_seal,
1808         .unseal                 = gss_cli_ctx_unseal,
1809         .wrap_bulk              = gss_cli_ctx_wrap_bulk,
1810         .unwrap_bulk            = gss_cli_ctx_unwrap_bulk,
1811 };
1812
1813 static struct ptlrpc_sec_cops gss_sec_keyring_cops = {
1814         .create_sec             = gss_sec_create_kr,
1815         .destroy_sec            = gss_sec_destroy_kr,
1816         .kill_sec               = gss_sec_kill,
1817         .lookup_ctx             = gss_sec_lookup_ctx_kr,
1818         .release_ctx            = gss_sec_release_ctx_kr,
1819         .flush_ctx_cache        = gss_sec_flush_ctx_cache_kr,
1820         .gc_ctx                 = gss_sec_gc_ctx_kr,
1821         .install_rctx           = gss_sec_install_rctx,
1822         .alloc_reqbuf           = gss_alloc_reqbuf,
1823         .free_reqbuf            = gss_free_reqbuf,
1824         .alloc_repbuf           = gss_alloc_repbuf,
1825         .free_repbuf            = gss_free_repbuf,
1826         .enlarge_reqbuf         = gss_enlarge_reqbuf,
1827         .display                = gss_sec_display_kr,
1828 };
1829
1830 static struct ptlrpc_sec_sops gss_sec_keyring_sops = {
1831         .accept                 = gss_svc_accept_kr,
1832         .invalidate_ctx         = gss_svc_invalidate_ctx,
1833         .alloc_rs               = gss_svc_alloc_rs,
1834         .authorize              = gss_svc_authorize,
1835         .free_rs                = gss_svc_free_rs,
1836         .free_ctx               = gss_svc_free_ctx,
1837         .prep_bulk              = gss_svc_prep_bulk,
1838         .unwrap_bulk            = gss_svc_unwrap_bulk,
1839         .wrap_bulk              = gss_svc_wrap_bulk,
1840         .install_rctx           = gss_svc_install_rctx_kr,
1841 };
1842
1843 static struct ptlrpc_sec_policy gss_policy_keyring = {
1844         .sp_owner               = THIS_MODULE,
1845         .sp_name                = "gss.keyring",
1846         .sp_policy              = SPTLRPC_POLICY_GSS,
1847         .sp_cops                = &gss_sec_keyring_cops,
1848         .sp_sops                = &gss_sec_keyring_sops,
1849 };
1850
1851
1852 int __init gss_init_keyring(void)
1853 {
1854         int rc;
1855
1856         rc = register_key_type(&gss_key_type);
1857         if (rc) {
1858                 CERROR("failed to register keyring type: %d\n", rc);
1859                 return rc;
1860         }
1861
1862         rc = sptlrpc_register_policy(&gss_policy_keyring);
1863         if (rc) {
1864                 unregister_key_type(&gss_key_type);
1865                 return rc;
1866         }
1867
1868         return 0;
1869 }
1870
1871 void __exit gss_exit_keyring(void)
1872 {
1873         unregister_key_type(&gss_key_type);
1874         sptlrpc_unregister_policy(&gss_policy_keyring);
1875 }