Whamcloud - gitweb
LU-5443 lustre: replace direct HZ access with kernel APIs
[fs/lustre-release.git] / lustre / ptlrpc / gss / gss_keyring.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.sun.com/software/products/lustre/docs/GPLv2.pdf
19  *
20  * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
21  * CA 95054 USA or visit www.sun.com if you need additional information or
22  * have any questions.
23  *
24  * GPL HEADER END
25  */
26 /*
27  * Copyright (c) 2007, 2010, Oracle and/or its affiliates. All rights reserved.
28  * Use is subject to license terms.
29  *
30  * Copyright (c) 2012, Intel Corporation.
31  */
32 /*
33  * This file is part of Lustre, http://www.lustre.org/
34  * Lustre is a trademark of Sun Microsystems, Inc.
35  *
36  * lustre/ptlrpc/gss/gss_keyring.c
37  *
38  * Author: Eric Mei <ericm@clusterfs.com>
39  */
40
41 #define DEBUG_SUBSYSTEM S_SEC
42 #include <linux/init.h>
43 #include <linux/module.h>
44 #include <linux/slab.h>
45 #include <linux/dcache.h>
46 #include <linux/fs.h>
47 #include <linux/crypto.h>
48 #include <linux/key.h>
49 #include <linux/keyctl.h>
50 #include <linux/key-type.h>
51 #include <linux/mutex.h>
52 #include <asm/atomic.h>
53
54 #include <obd.h>
55 #include <obd_class.h>
56 #include <obd_support.h>
57 #include <lustre/lustre_idl.h>
58 #include <lustre_sec.h>
59 #include <lustre_net.h>
60 #include <lustre_import.h>
61
62 #include "gss_err.h"
63 #include "gss_internal.h"
64 #include "gss_api.h"
65
66 static struct ptlrpc_sec_policy gss_policy_keyring;
67 static struct ptlrpc_ctx_ops gss_keyring_ctxops;
68 static struct key_type gss_key_type;
69
70 static int sec_install_rctx_kr(struct ptlrpc_sec *sec,
71                                struct ptlrpc_svc_ctx *svc_ctx);
72
73 /*
74  * the timeout is only for the case that upcall child process die abnormally.
75  * in any other cases it should finally update kernel key.
76  * 
77  * FIXME we'd better to incorporate the client & server side upcall timeouts
78  * into the framework of Adaptive Timeouts, but we need to figure out how to
79  * make sure that kernel knows the upcall processes is in-progress or died
80  * unexpectedly.
81  */
82 #define KEYRING_UPCALL_TIMEOUT  (obd_timeout + obd_timeout)
83
84 /****************************************
85  * internal helpers                     *
86  ****************************************/
87
88 #define DUMP_PROCESS_KEYRINGS(tsk)                                      \
89 {                                                                       \
90         CWARN("DUMP PK: %s[%u,%u/%u](<-%s[%u,%u/%u]): "                 \
91               "a %d, t %d, p %d, s %d, u %d, us %d, df %d\n",           \
92               tsk->comm, tsk->pid, tsk->uid, tsk->fsuid,                \
93               tsk->parent->comm, tsk->parent->pid,                      \
94               tsk->parent->uid, tsk->parent->fsuid,                     \
95               tsk->request_key_auth ?                                   \
96               tsk->request_key_auth->serial : 0,                        \
97               key_cred(tsk)->thread_keyring ?                           \
98               key_cred(tsk)->thread_keyring->serial : 0,                \
99               key_tgcred(tsk)->process_keyring ?                        \
100               key_tgcred(tsk)->process_keyring->serial : 0,             \
101               key_tgcred(tsk)->session_keyring ?                        \
102               key_tgcred(tsk)->session_keyring->serial : 0,             \
103               key_cred(tsk)->user->uid_keyring ?                        \
104               key_cred(tsk)->user->uid_keyring->serial : 0,             \
105               key_cred(tsk)->user->session_keyring ?                    \
106               key_cred(tsk)->user->session_keyring->serial : 0,         \
107               key_cred(tsk)->jit_keyring                                \
108              );                                                         \
109 }
110
111 #define DUMP_KEY(key)                                                   \
112 {                                                                       \
113         CWARN("DUMP KEY: %p(%d) ref %d u%u/g%u desc %s\n",              \
114               key, key->serial, atomic_read(&key->usage),               \
115               key->uid, key->gid,                                       \
116               key->description ? key->description : "n/a"               \
117              );                                                         \
118 }
119
120 #define key_cred(tsk)   ((tsk)->cred)
121 #define key_tgcred(tsk) ((tsk)->cred->tgcred)
122
123 static inline void keyring_upcall_lock(struct gss_sec_keyring *gsec_kr)
124 {
125 #ifdef HAVE_KEYRING_UPCALL_SERIALIZED
126         mutex_lock(&gsec_kr->gsk_uc_lock);
127 #endif
128 }
129
130 static inline void keyring_upcall_unlock(struct gss_sec_keyring *gsec_kr)
131 {
132 #ifdef HAVE_KEYRING_UPCALL_SERIALIZED
133         mutex_unlock(&gsec_kr->gsk_uc_lock);
134 #endif
135 }
136
137 static inline void key_revoke_locked(struct key *key)
138 {
139         set_bit(KEY_FLAG_REVOKED, &key->flags);
140 }
141
142 static void ctx_upcall_timeout_kr(unsigned long data)
143 {
144         struct ptlrpc_cli_ctx *ctx = (struct ptlrpc_cli_ctx *) data;
145         struct key            *key = ctx2gctx_keyring(ctx)->gck_key;
146
147         CWARN("ctx %p, key %p\n", ctx, key);
148
149         LASSERT(key);
150
151         cli_ctx_expire(ctx);
152         key_revoke_locked(key);
153 }
154
155 static void ctx_start_timer_kr(struct ptlrpc_cli_ctx *ctx, long timeout)
156 {
157         struct gss_cli_ctx_keyring *gctx_kr = ctx2gctx_keyring(ctx);
158         struct timer_list          *timer = gctx_kr->gck_timer;
159
160         LASSERT(timer);
161
162         CDEBUG(D_SEC, "ctx %p: start timer %lds\n", ctx, timeout);
163         timeout = msecs_to_jiffies(timeout * MSEC_PER_SEC) +
164                   cfs_time_current();
165
166         init_timer(timer);
167         timer->expires = timeout;
168         timer->data = (unsigned long ) ctx;
169         timer->function = ctx_upcall_timeout_kr;
170
171         add_timer(timer);
172 }
173
174 /*
175  * caller should make sure no race with other threads
176  */
177 static
178 void ctx_clear_timer_kr(struct ptlrpc_cli_ctx *ctx)
179 {
180         struct gss_cli_ctx_keyring *gctx_kr = ctx2gctx_keyring(ctx);
181         struct timer_list          *timer = gctx_kr->gck_timer;
182
183         if (timer == NULL)
184                 return;
185
186         CDEBUG(D_SEC, "ctx %p, key %p\n", ctx, gctx_kr->gck_key);
187
188         gctx_kr->gck_timer = NULL;
189
190         del_singleshot_timer_sync(timer);
191
192         OBD_FREE_PTR(timer);
193 }
194
195 static
196 struct ptlrpc_cli_ctx *ctx_create_kr(struct ptlrpc_sec *sec,
197                                      struct vfs_cred *vcred)
198 {
199         struct ptlrpc_cli_ctx      *ctx;
200         struct gss_cli_ctx_keyring *gctx_kr;
201
202         OBD_ALLOC_PTR(gctx_kr);
203         if (gctx_kr == NULL)
204                 return NULL;
205
206         OBD_ALLOC_PTR(gctx_kr->gck_timer);
207         if (gctx_kr->gck_timer == NULL) {
208                 OBD_FREE_PTR(gctx_kr);
209                 return NULL;
210         }
211         init_timer(gctx_kr->gck_timer);
212
213         ctx = &gctx_kr->gck_base.gc_base;
214
215         if (gss_cli_ctx_init_common(sec, ctx, &gss_keyring_ctxops, vcred)) {
216                 OBD_FREE_PTR(gctx_kr->gck_timer);
217                 OBD_FREE_PTR(gctx_kr);
218                 return NULL;
219         }
220
221         ctx->cc_expire = cfs_time_current_sec() + KEYRING_UPCALL_TIMEOUT;
222         clear_bit(PTLRPC_CTX_NEW_BIT, &ctx->cc_flags);
223         atomic_inc(&ctx->cc_refcount); /* for the caller */
224
225         return ctx;
226 }
227
228 static void ctx_destroy_kr(struct ptlrpc_cli_ctx *ctx)
229 {
230         struct ptlrpc_sec               *sec = ctx->cc_sec;
231         struct gss_cli_ctx_keyring      *gctx_kr = ctx2gctx_keyring(ctx);
232
233         CDEBUG(D_SEC, "destroying ctx %p\n", ctx);
234
235         /* at this time the association with key has been broken. */
236         LASSERT(sec);
237         LASSERT(atomic_read(&sec->ps_refcount) > 0);
238         LASSERT(atomic_read(&sec->ps_nctx) > 0);
239         LASSERT(test_bit(PTLRPC_CTX_CACHED_BIT, &ctx->cc_flags) == 0);
240         LASSERT(gctx_kr->gck_key == NULL);
241
242         ctx_clear_timer_kr(ctx);
243         LASSERT(gctx_kr->gck_timer == NULL);
244
245         if (gss_cli_ctx_fini_common(sec, ctx))
246                 return;
247
248         OBD_FREE_PTR(gctx_kr);
249
250         atomic_dec(&sec->ps_nctx);
251         sptlrpc_sec_put(sec);
252 }
253
254 static void ctx_release_kr(struct ptlrpc_cli_ctx *ctx, int sync)
255 {
256         if (sync) {
257                 ctx_destroy_kr(ctx);
258         } else {
259                 atomic_inc(&ctx->cc_refcount);
260                 sptlrpc_gc_add_ctx(ctx);
261         }
262 }
263
264 static void ctx_put_kr(struct ptlrpc_cli_ctx *ctx, int sync)
265 {
266         LASSERT(atomic_read(&ctx->cc_refcount) > 0);
267
268         if (atomic_dec_and_test(&ctx->cc_refcount))
269                 ctx_release_kr(ctx, sync);
270 }
271
272 /*
273  * key <-> ctx association and rules:
274  * - ctx might not bind with any key
275  * - key/ctx binding is protected by key semaphore (if the key present)
276  * - key and ctx each take a reference of the other
277  * - ctx enlist/unlist is protected by ctx spinlock
278  * - never enlist a ctx after it's been unlisted
279  * - whoever do enlist should also do bind, lock key before enlist:
280  *   - lock key -> lock ctx -> enlist -> unlock ctx -> bind -> unlock key
281  * - whoever do unlist should also do unbind:
282  *   - lock key -> lock ctx -> unlist -> unlock ctx -> unbind -> unlock key
283  *   - lock ctx -> unlist -> unlock ctx -> lock key -> unbind -> unlock key
284  */
285
286 static inline void spin_lock_if(spinlock_t *lock, int condition)
287 {
288         if (condition)
289                 spin_lock(lock);
290 }
291
292 static inline void spin_unlock_if(spinlock_t *lock, int condition)
293 {
294         if (condition)
295                 spin_unlock(lock);
296 }
297
298 static void ctx_enlist_kr(struct ptlrpc_cli_ctx *ctx, int is_root, int locked)
299 {
300         struct ptlrpc_sec       *sec = ctx->cc_sec;
301         struct gss_sec_keyring  *gsec_kr = sec2gsec_keyring(sec);
302
303         LASSERT(!test_bit(PTLRPC_CTX_CACHED_BIT, &ctx->cc_flags));
304         LASSERT(atomic_read(&ctx->cc_refcount) > 0);
305
306         spin_lock_if(&sec->ps_lock, !locked);
307
308         atomic_inc(&ctx->cc_refcount);
309         set_bit(PTLRPC_CTX_CACHED_BIT, &ctx->cc_flags);
310         hlist_add_head(&ctx->cc_cache, &gsec_kr->gsk_clist);
311         if (is_root)
312                 gsec_kr->gsk_root_ctx = ctx;
313
314         spin_unlock_if(&sec->ps_lock, !locked);
315 }
316
317 /*
318  * Note after this get called, caller should not access ctx again because
319  * it might have been freed, unless caller hold at least one refcount of
320  * the ctx.
321  *
322  * return non-zero if we indeed unlist this ctx.
323  */
324 static int ctx_unlist_kr(struct ptlrpc_cli_ctx *ctx, int locked)
325 {
326         struct ptlrpc_sec       *sec = ctx->cc_sec;
327         struct gss_sec_keyring  *gsec_kr = sec2gsec_keyring(sec);
328
329         /* if hashed bit has gone, leave the job to somebody who is doing it */
330         if (test_and_clear_bit(PTLRPC_CTX_CACHED_BIT, &ctx->cc_flags) == 0)
331                 return 0;
332
333         /* drop ref inside spin lock to prevent race with other operations */
334         spin_lock_if(&sec->ps_lock, !locked);
335
336         if (gsec_kr->gsk_root_ctx == ctx)
337                 gsec_kr->gsk_root_ctx = NULL;
338         hlist_del_init(&ctx->cc_cache);
339         atomic_dec(&ctx->cc_refcount);
340
341         spin_unlock_if(&sec->ps_lock, !locked);
342
343         return 1;
344 }
345
346 /*
347  * bind a key with a ctx together.
348  * caller must hold write lock of the key, as well as ref on key & ctx.
349  */
350 static void bind_key_ctx(struct key *key, struct ptlrpc_cli_ctx *ctx)
351 {
352         LASSERT(atomic_read(&ctx->cc_refcount) > 0);
353         LASSERT(atomic_read(&key->usage) > 0);
354         LASSERT(ctx2gctx_keyring(ctx)->gck_key == NULL);
355         LASSERT(key->payload.data == NULL);
356
357         /* at this time context may or may not in list. */
358         key_get(key);
359         atomic_inc(&ctx->cc_refcount);
360         ctx2gctx_keyring(ctx)->gck_key = key;
361         key->payload.data = ctx;
362 }
363
364 /*
365  * unbind a key and a ctx.
366  * caller must hold write lock, as well as a ref of the key.
367  */
368 static void unbind_key_ctx(struct key *key, struct ptlrpc_cli_ctx *ctx)
369 {
370         LASSERT(key->payload.data == ctx);
371         LASSERT(test_bit(PTLRPC_CTX_CACHED_BIT, &ctx->cc_flags) == 0);
372
373         /* must revoke the key, or others may treat it as newly created */
374         key_revoke_locked(key);
375
376         key->payload.data = NULL;
377         ctx2gctx_keyring(ctx)->gck_key = NULL;
378
379         /* once ctx get split from key, the timer is meaningless */
380         ctx_clear_timer_kr(ctx);
381
382         ctx_put_kr(ctx, 1);
383         key_put(key);
384 }
385
386 /*
387  * given a ctx, unbind with its coupled key, if any.
388  * unbind could only be called once, so we don't worry the key be released
389  * by someone else.
390  */
391 static void unbind_ctx_kr(struct ptlrpc_cli_ctx *ctx)
392 {
393         struct key      *key = ctx2gctx_keyring(ctx)->gck_key;
394
395         if (key) {
396                 LASSERT(key->payload.data == ctx);
397
398                 key_get(key);
399                 down_write(&key->sem);
400                 unbind_key_ctx(key, ctx);
401                 up_write(&key->sem);
402                 key_put(key);
403         }
404 }
405
406 /*
407  * given a key, unbind with its coupled ctx, if any.
408  * caller must hold write lock, as well as a ref of the key.
409  */
410 static void unbind_key_locked(struct key *key)
411 {
412         struct ptlrpc_cli_ctx   *ctx = key->payload.data;
413
414         if (ctx)
415                 unbind_key_ctx(key, ctx);
416 }
417
418 /*
419  * unlist a ctx, and unbind from coupled key
420  */
421 static void kill_ctx_kr(struct ptlrpc_cli_ctx *ctx)
422 {
423         if (ctx_unlist_kr(ctx, 0))
424                 unbind_ctx_kr(ctx);
425 }
426
427 /*
428  * given a key, unlist and unbind with the coupled ctx (if any).
429  * caller must hold write lock, as well as a ref of the key.
430  */
431 static void kill_key_locked(struct key *key)
432 {
433         struct ptlrpc_cli_ctx *ctx = key->payload.data;
434
435         if (ctx && ctx_unlist_kr(ctx, 0))
436                 unbind_key_locked(key);
437 }
438
439 /*
440  * caller should hold one ref on contexts in freelist.
441  */
442 static void dispose_ctx_list_kr(struct hlist_head *freelist)
443 {
444         struct hlist_node       __maybe_unused *pos, *next;
445         struct ptlrpc_cli_ctx   *ctx;
446         struct gss_cli_ctx      *gctx;
447
448         cfs_hlist_for_each_entry_safe(ctx, pos, next, freelist, cc_cache) {
449                 hlist_del_init(&ctx->cc_cache);
450
451                 /* reverse ctx: update current seq to buddy svcctx if exist.
452                  * ideally this should be done at gss_cli_ctx_finalize(), but
453                  * the ctx destroy could be delayed by:
454                  *  1) ctx still has reference;
455                  *  2) ctx destroy is asynchronous;
456                  * and reverse import call inval_all_ctx() require this be done
457                  * _immediately_ otherwise newly created reverse ctx might copy
458                  * the very old sequence number from svcctx. */
459                 gctx = ctx2gctx(ctx);
460                 if (!rawobj_empty(&gctx->gc_svc_handle) &&
461                     sec_is_reverse(gctx->gc_base.cc_sec)) {
462                         gss_svc_upcall_update_sequence(&gctx->gc_svc_handle,
463                                         (__u32) atomic_read(&gctx->gc_seq));
464                 }
465
466                 /* we need to wakeup waiting reqs here. the context might
467                  * be forced released before upcall finished, then the
468                  * late-arrived downcall can't find the ctx even. */
469                 sptlrpc_cli_ctx_wakeup(ctx);
470
471                 unbind_ctx_kr(ctx);
472                 ctx_put_kr(ctx, 0);
473         }
474 }
475
476 /*
477  * lookup a root context directly in a sec, return root ctx with a
478  * reference taken or NULL.
479  */
480 static
481 struct ptlrpc_cli_ctx * sec_lookup_root_ctx_kr(struct ptlrpc_sec *sec)
482 {
483         struct gss_sec_keyring  *gsec_kr = sec2gsec_keyring(sec);
484         struct ptlrpc_cli_ctx   *ctx = NULL;
485
486         spin_lock(&sec->ps_lock);
487
488         ctx = gsec_kr->gsk_root_ctx;
489
490         if (ctx == NULL && unlikely(sec_is_reverse(sec))) {
491                 struct hlist_node       __maybe_unused *node;
492                 struct ptlrpc_cli_ctx   *tmp;
493
494                 /* reverse ctx, search root ctx in list, choose the one
495                  * with shortest expire time, which is most possibly have
496                  * an established peer ctx at client side. */
497                 cfs_hlist_for_each_entry(tmp, node, &gsec_kr->gsk_clist,
498                                          cc_cache) {
499                         if (ctx == NULL || ctx->cc_expire == 0 ||
500                             ctx->cc_expire > tmp->cc_expire) {
501                                 ctx = tmp;
502                                 /* promote to be root_ctx */
503                                 gsec_kr->gsk_root_ctx = ctx;
504                         }
505                 }
506         }
507
508         if (ctx) {
509                 LASSERT(atomic_read(&ctx->cc_refcount) > 0);
510                 LASSERT(!hlist_empty(&gsec_kr->gsk_clist));
511                 atomic_inc(&ctx->cc_refcount);
512         }
513
514         spin_unlock(&sec->ps_lock);
515
516         return ctx;
517 }
518
519 #define RVS_CTX_EXPIRE_NICE    (10)
520
521 static
522 void rvs_sec_install_root_ctx_kr(struct ptlrpc_sec *sec,
523                                  struct ptlrpc_cli_ctx *new_ctx,
524                                  struct key *key)
525 {
526         struct gss_sec_keyring  *gsec_kr = sec2gsec_keyring(sec);
527         struct hlist_node       __maybe_unused *hnode;
528         struct ptlrpc_cli_ctx   *ctx;
529         cfs_time_t              now;
530         ENTRY;
531
532         LASSERT(sec_is_reverse(sec));
533
534         spin_lock(&sec->ps_lock);
535
536         now = cfs_time_current_sec();
537
538         /* set all existing ctxs short expiry */
539         cfs_hlist_for_each_entry(ctx, hnode, &gsec_kr->gsk_clist, cc_cache) {
540                 if (ctx->cc_expire > now + RVS_CTX_EXPIRE_NICE) {
541                         ctx->cc_early_expire = 1;
542                         ctx->cc_expire = now + RVS_CTX_EXPIRE_NICE;
543                 }
544         }
545
546         /* if there's root_ctx there, instead obsolete the current
547          * immediately, we leave it continue operating for a little while.
548          * hopefully when the first backward rpc with newest ctx send out,
549          * the client side already have the peer ctx well established. */
550         ctx_enlist_kr(new_ctx, gsec_kr->gsk_root_ctx ? 0 : 1, 1);
551
552         if (key)
553                 bind_key_ctx(key, new_ctx);
554
555         spin_unlock(&sec->ps_lock);
556 }
557
558 static void construct_key_desc(void *buf, int bufsize,
559                                struct ptlrpc_sec *sec, uid_t uid)
560 {
561         snprintf(buf, bufsize, "%d@%x", uid, sec->ps_id);
562         ((char *)buf)[bufsize - 1] = '\0';
563 }
564
565 /****************************************
566  * sec apis                             *
567  ****************************************/
568
569 static
570 struct ptlrpc_sec * gss_sec_create_kr(struct obd_import *imp,
571                                       struct ptlrpc_svc_ctx *svcctx,
572                                       struct sptlrpc_flavor *sf)
573 {
574         struct gss_sec_keyring  *gsec_kr;
575         ENTRY;
576
577         OBD_ALLOC(gsec_kr, sizeof(*gsec_kr));
578         if (gsec_kr == NULL)
579                 RETURN(NULL);
580
581         INIT_HLIST_HEAD(&gsec_kr->gsk_clist);
582         gsec_kr->gsk_root_ctx = NULL;
583         mutex_init(&gsec_kr->gsk_root_uc_lock);
584 #ifdef HAVE_KEYRING_UPCALL_SERIALIZED
585         mutex_init(&gsec_kr->gsk_uc_lock);
586 #endif
587
588         if (gss_sec_create_common(&gsec_kr->gsk_base, &gss_policy_keyring,
589                                   imp, svcctx, sf))
590                 goto err_free;
591
592         if (svcctx != NULL &&
593             sec_install_rctx_kr(&gsec_kr->gsk_base.gs_base, svcctx)) {
594                 gss_sec_destroy_common(&gsec_kr->gsk_base);
595                 goto err_free;
596         }
597
598         RETURN(&gsec_kr->gsk_base.gs_base);
599
600 err_free:
601         OBD_FREE(gsec_kr, sizeof(*gsec_kr));
602         RETURN(NULL);
603 }
604
605 static
606 void gss_sec_destroy_kr(struct ptlrpc_sec *sec)
607 {
608         struct gss_sec          *gsec = sec2gsec(sec);
609         struct gss_sec_keyring  *gsec_kr = sec2gsec_keyring(sec);
610
611         CDEBUG(D_SEC, "destroy %s@%p\n", sec->ps_policy->sp_name, sec);
612
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  * unlink request key from it's ring, which is linked during request_key().
633  * sadly, we have to 'guess' which keyring it's linked to.
634  *
635  * FIXME this code is fragile, depend on how request_key_link() is implemented.
636  */
637 static void request_key_unlink(struct key *key)
638 {
639         struct task_struct *tsk = current;
640         struct key *ring;
641
642         switch (key_cred(tsk)->jit_keyring) {
643         case KEY_REQKEY_DEFL_DEFAULT:
644         case KEY_REQKEY_DEFL_THREAD_KEYRING:
645                 ring = key_get(key_cred(tsk)->thread_keyring);
646                 if (ring)
647                         break;
648         case KEY_REQKEY_DEFL_PROCESS_KEYRING:
649                 ring = key_get(key_tgcred(tsk)->process_keyring);
650                 if (ring)
651                         break;
652         case KEY_REQKEY_DEFL_SESSION_KEYRING:
653                 rcu_read_lock();
654                 ring = key_get(rcu_dereference(key_tgcred(tsk)
655                                                ->session_keyring));
656                 rcu_read_unlock();
657                 if (ring)
658                         break;
659         case KEY_REQKEY_DEFL_USER_SESSION_KEYRING:
660                 ring = key_get(key_cred(tsk)->user->session_keyring);
661                 break;
662         case KEY_REQKEY_DEFL_USER_KEYRING:
663                 ring = key_get(key_cred(tsk)->user->uid_keyring);
664                 break;
665         case KEY_REQKEY_DEFL_GROUP_KEYRING:
666         default:
667                 LBUG();
668         }
669
670         LASSERT(ring);
671         key_unlink(ring, key);
672         key_put(ring);
673 }
674
675 static
676 struct ptlrpc_cli_ctx * gss_sec_lookup_ctx_kr(struct ptlrpc_sec *sec,
677                                               struct vfs_cred *vcred,
678                                               int create, int remove_dead)
679 {
680         struct obd_import       *imp = sec->ps_import;
681         struct gss_sec_keyring  *gsec_kr = sec2gsec_keyring(sec);
682         struct ptlrpc_cli_ctx   *ctx = NULL;
683         unsigned int             is_root = 0, create_new = 0;
684         struct key              *key;
685         char                     desc[24];
686         char                    *coinfo;
687         int                      coinfo_size;
688         char                    *co_flags = "";
689         ENTRY;
690
691         LASSERT(imp != NULL);
692
693         is_root = user_is_root(sec, vcred);
694
695         /* a little bit optimization for root context */
696         if (is_root) {
697                 ctx = sec_lookup_root_ctx_kr(sec);
698                 /*
699                  * Only lookup directly for REVERSE sec, which should
700                  * always succeed.
701                  */
702                 if (ctx || sec_is_reverse(sec))
703                         RETURN(ctx);
704         }
705
706         LASSERT(create != 0);
707
708         /* for root context, obtain lock and check again, this time hold
709          * the root upcall lock, make sure nobody else populated new root
710          * context after last check. */
711         if (is_root) {
712                 mutex_lock(&gsec_kr->gsk_root_uc_lock);
713
714                 ctx = sec_lookup_root_ctx_kr(sec);
715                 if (ctx)
716                         goto out;
717
718                 /* update reverse handle for root user */
719                 sec2gsec(sec)->gs_rvs_hdl = gss_get_next_ctx_index();
720
721                 switch (sec->ps_part) {
722                 case LUSTRE_SP_MDT:
723                         co_flags = "m";
724                         break;
725                 case LUSTRE_SP_OST:
726                         co_flags = "o";
727                         break;
728                 case LUSTRE_SP_MGC:
729                         co_flags = "rmo";
730                         break;
731                 case LUSTRE_SP_CLI:
732                         co_flags = "r";
733                         break;
734                 case LUSTRE_SP_MGS:
735                 default:
736                         LBUG();
737                 }
738         }
739
740         /* in case of setuid, key will be constructed as owner of fsuid/fsgid,
741          * but we do authentication based on real uid/gid. the key permission
742          * bits will be exactly as POS_ALL, so only processes who subscribed
743          * this key could have the access, although the quota might be counted
744          * on others (fsuid/fsgid).
745          *
746          * keyring will use fsuid/fsgid as upcall parameters, so we have to
747          * encode real uid/gid into callout info.
748          */
749
750         construct_key_desc(desc, sizeof(desc), sec, vcred->vc_uid);
751
752         /* callout info format:
753          * secid:mech:uid:gid:flags:svc_type:peer_nid:target_uuid
754          */
755         coinfo_size = sizeof(struct obd_uuid) + MAX_OBD_NAME + 64;
756         OBD_ALLOC(coinfo, coinfo_size);
757         if (coinfo == NULL)
758                 goto out;
759
760         snprintf(coinfo, coinfo_size, "%d:%s:%u:%u:%s:%d:"LPX64":%s",
761                  sec->ps_id, sec2gsec(sec)->gs_mech->gm_name,
762                  vcred->vc_uid, vcred->vc_gid,
763                  co_flags, import_to_gss_svc(imp),
764                  imp->imp_connection->c_peer.nid, imp->imp_obd->obd_name);
765
766         CDEBUG(D_SEC, "requesting key for %s\n", desc);
767
768         keyring_upcall_lock(gsec_kr);
769         key = request_key(&gss_key_type, desc, coinfo);
770         keyring_upcall_unlock(gsec_kr);
771
772         OBD_FREE(coinfo, coinfo_size);
773
774         if (IS_ERR(key)) {
775                 CERROR("failed request key: %ld\n", PTR_ERR(key));
776                 goto out;
777         }
778         CDEBUG(D_SEC, "obtained key %08x for %s\n", key->serial, desc);
779
780         /* once payload.data was pointed to a ctx, it never changes until
781          * we de-associate them; but parallel request_key() may return
782          * a key with payload.data == NULL at the same time. so we still
783          * need wirtelock of key->sem to serialize them. */
784         down_write(&key->sem);
785
786         if (likely(key->payload.data != NULL)) {
787                 ctx = key->payload.data;
788
789                 LASSERT(atomic_read(&ctx->cc_refcount) >= 1);
790                 LASSERT(ctx2gctx_keyring(ctx)->gck_key == key);
791                 LASSERT(atomic_read(&key->usage) >= 2);
792
793                 /* simply take a ref and return. it's upper layer's
794                  * responsibility to detect & replace dead ctx. */
795                 atomic_inc(&ctx->cc_refcount);
796         } else {
797                 /* pre initialization with a cli_ctx. this can't be done in
798                  * key_instantiate() because we'v no enough information
799                  * there. */
800                 ctx = ctx_create_kr(sec, vcred);
801                 if (ctx != NULL) {
802                         ctx_enlist_kr(ctx, is_root, 0);
803                         bind_key_ctx(key, ctx);
804
805                         ctx_start_timer_kr(ctx, KEYRING_UPCALL_TIMEOUT);
806
807                         CDEBUG(D_SEC, "installed key %p <-> ctx %p (sec %p)\n",
808                                key, ctx, sec);
809                 } else {
810                         /* we'd prefer to call key_revoke(), but we more like
811                          * to revoke it within this key->sem locked period. */
812                         key_revoke_locked(key);
813                 }
814
815                 create_new = 1;
816         }
817
818         up_write(&key->sem);
819
820         if (is_root && create_new)
821                 request_key_unlink(key);
822
823         key_put(key);
824 out:
825         if (is_root)
826                 mutex_unlock(&gsec_kr->gsk_root_uc_lock);
827         RETURN(ctx);
828 }
829
830 static
831 void gss_sec_release_ctx_kr(struct ptlrpc_sec *sec,
832                             struct ptlrpc_cli_ctx *ctx,
833                             int sync)
834 {
835         LASSERT(atomic_read(&sec->ps_refcount) > 0);
836         LASSERT(atomic_read(&ctx->cc_refcount) == 0);
837         ctx_release_kr(ctx, sync);
838 }
839
840 /*
841  * flush context of normal user, we must resort to keyring itself to find out
842  * contexts which belong to me.
843  *
844  * Note here we suppose only to flush _my_ context, the "uid" will
845  * be ignored in the search.
846  */
847 static
848 void flush_user_ctx_cache_kr(struct ptlrpc_sec *sec,
849                              uid_t uid,
850                              int grace, int force)
851 {
852         struct key              *key;
853         char                     desc[24];
854
855         /* nothing to do for reverse or rootonly sec */
856         if (sec_is_reverse(sec) || sec_is_rootonly(sec))
857                 return;
858
859         construct_key_desc(desc, sizeof(desc), sec, uid);
860
861         /* there should be only one valid key, but we put it in the
862          * loop in case of any weird cases */
863         for (;;) {
864                 key = request_key(&gss_key_type, desc, NULL);
865                 if (IS_ERR(key)) {
866                         CDEBUG(D_SEC, "No more key found for current user\n");
867                         break;
868                 }
869
870                 down_write(&key->sem);
871
872                 kill_key_locked(key);
873
874                 /* kill_key_locked() should usually revoke the key, but we
875                  * revoke it again to make sure, e.g. some case the key may
876                  * not well coupled with a context. */
877                 key_revoke_locked(key);
878
879                 up_write(&key->sem);
880
881                 key_put(key);
882         }
883 }
884
885 /*
886  * flush context of root or all, we iterate through the list.
887  */
888 static
889 void flush_spec_ctx_cache_kr(struct ptlrpc_sec *sec, uid_t uid, int grace,
890                              int force)
891 {
892         struct gss_sec_keyring  *gsec_kr;
893         struct hlist_head        freelist = HLIST_HEAD_INIT;
894         struct hlist_node       __maybe_unused *pos, *next;
895         struct ptlrpc_cli_ctx   *ctx;
896         ENTRY;
897
898         gsec_kr = sec2gsec_keyring(sec);
899
900         spin_lock(&sec->ps_lock);
901         cfs_hlist_for_each_entry_safe(ctx, pos, next,
902                                       &gsec_kr->gsk_clist, cc_cache) {
903                 LASSERT(atomic_read(&ctx->cc_refcount) > 0);
904
905                 if (uid != -1 && uid != ctx->cc_vcred.vc_uid)
906                         continue;
907
908                 /* at this moment there's at least 2 base reference:
909                  * key association and in-list. */
910                 if (atomic_read(&ctx->cc_refcount) > 2) {
911                         if (!force)
912                                 continue;
913                         CWARN("flush busy ctx %p(%u->%s, extra ref %d)\n",
914                               ctx, ctx->cc_vcred.vc_uid,
915                               sec2target_str(ctx->cc_sec),
916                               atomic_read(&ctx->cc_refcount) - 2);
917                 }
918
919                 set_bit(PTLRPC_CTX_DEAD_BIT, &ctx->cc_flags);
920                 if (!grace)
921                         clear_bit(PTLRPC_CTX_UPTODATE_BIT, &ctx->cc_flags);
922
923                 atomic_inc(&ctx->cc_refcount);
924
925                 if (ctx_unlist_kr(ctx, 1)) {
926                         hlist_add_head(&ctx->cc_cache, &freelist);
927                 } else {
928                         LASSERT(atomic_read(&ctx->cc_refcount) >= 2);
929                         atomic_dec(&ctx->cc_refcount);
930                 }
931         }
932         spin_unlock(&sec->ps_lock);
933
934         dispose_ctx_list_kr(&freelist);
935         EXIT;
936 }
937
938 static
939 int gss_sec_flush_ctx_cache_kr(struct ptlrpc_sec *sec,
940                                uid_t uid, int grace, int force)
941 {
942         ENTRY;
943
944         CDEBUG(D_SEC, "sec %p(%d, nctx %d), uid %d, grace %d, force %d\n",
945                sec, atomic_read(&sec->ps_refcount),
946                atomic_read(&sec->ps_nctx),
947                uid, grace, force);
948
949         if (uid != -1 && uid != 0)
950                 flush_user_ctx_cache_kr(sec, uid, grace, force);
951         else
952                 flush_spec_ctx_cache_kr(sec, uid, grace, force);
953
954         RETURN(0);
955 }
956
957 static
958 void gss_sec_gc_ctx_kr(struct ptlrpc_sec *sec)
959 {
960         struct gss_sec_keyring  *gsec_kr = sec2gsec_keyring(sec);
961         struct hlist_head       freelist = HLIST_HEAD_INIT;
962         struct hlist_node       __maybe_unused *pos, *next;
963         struct ptlrpc_cli_ctx   *ctx;
964         ENTRY;
965
966         CWARN("running gc\n");
967
968         spin_lock(&sec->ps_lock);
969         cfs_hlist_for_each_entry_safe(ctx, pos, next,
970                                       &gsec_kr->gsk_clist, cc_cache) {
971                 LASSERT(atomic_read(&ctx->cc_refcount) > 0);
972
973                 atomic_inc(&ctx->cc_refcount);
974
975                 if (cli_ctx_check_death(ctx) && ctx_unlist_kr(ctx, 1)) {
976                         hlist_add_head(&ctx->cc_cache, &freelist);
977                         CWARN("unhashed ctx %p\n", ctx);
978                 } else {
979                         LASSERT(atomic_read(&ctx->cc_refcount) >= 2);
980                         atomic_dec(&ctx->cc_refcount);
981                 }
982         }
983         spin_unlock(&sec->ps_lock);
984
985         dispose_ctx_list_kr(&freelist);
986         EXIT;
987         return;
988 }
989
990 static
991 int gss_sec_display_kr(struct ptlrpc_sec *sec, struct seq_file *seq)
992 {
993         struct gss_sec_keyring  *gsec_kr = sec2gsec_keyring(sec);
994         struct hlist_node       __maybe_unused *pos, *next;
995         struct ptlrpc_cli_ctx   *ctx;
996         struct gss_cli_ctx      *gctx;
997         time_t                   now = cfs_time_current_sec();
998         ENTRY;
999
1000         spin_lock(&sec->ps_lock);
1001         cfs_hlist_for_each_entry_safe(ctx, pos, next,
1002                                       &gsec_kr->gsk_clist, cc_cache) {
1003                 struct key             *key;
1004                 char                    flags_str[40];
1005                 char                    mech[40];
1006
1007                 gctx = ctx2gctx(ctx);
1008                 key = ctx2gctx_keyring(ctx)->gck_key;
1009
1010                 gss_cli_ctx_flags2str(ctx->cc_flags,
1011                                       flags_str, sizeof(flags_str));
1012
1013                 if (gctx->gc_mechctx)
1014                         lgss_display(gctx->gc_mechctx, mech, sizeof(mech));
1015                 else
1016                         snprintf(mech, sizeof(mech), "N/A");
1017                 mech[sizeof(mech) - 1] = '\0';
1018
1019                 seq_printf(seq, "%p: uid %u, ref %d, expire %ld(%+ld), fl %s, "
1020                            "seq %d, win %u, key %08x(ref %d), "
1021                            "hdl "LPX64":"LPX64", mech: %s\n",
1022                            ctx, ctx->cc_vcred.vc_uid,
1023                            atomic_read(&ctx->cc_refcount),
1024                            ctx->cc_expire,
1025                            ctx->cc_expire ?  ctx->cc_expire - now : 0,
1026                            flags_str,
1027                            atomic_read(&gctx->gc_seq),
1028                            gctx->gc_win,
1029                            key ? key->serial : 0,
1030                            key ? atomic_read(&key->usage) : 0,
1031                            gss_handle_to_u64(&gctx->gc_handle),
1032                            gss_handle_to_u64(&gctx->gc_svc_handle),
1033                            mech);
1034         }
1035         spin_unlock(&sec->ps_lock);
1036
1037         RETURN(0);
1038 }
1039
1040 /****************************************
1041  * cli_ctx apis                         *
1042  ****************************************/
1043
1044 static
1045 int gss_cli_ctx_refresh_kr(struct ptlrpc_cli_ctx *ctx)
1046 {
1047         /* upcall is already on the way */
1048         return 0;
1049 }
1050
1051 static
1052 int gss_cli_ctx_validate_kr(struct ptlrpc_cli_ctx *ctx)
1053 {
1054         LASSERT(atomic_read(&ctx->cc_refcount) > 0);
1055         LASSERT(ctx->cc_sec);
1056
1057         if (cli_ctx_check_death(ctx)) {
1058                 kill_ctx_kr(ctx);
1059                 return 1;
1060         }
1061
1062         if (cli_ctx_is_ready(ctx))
1063                 return 0;
1064         return 1;
1065 }
1066
1067 static
1068 void gss_cli_ctx_die_kr(struct ptlrpc_cli_ctx *ctx, int grace)
1069 {
1070         LASSERT(atomic_read(&ctx->cc_refcount) > 0);
1071         LASSERT(ctx->cc_sec);
1072
1073         cli_ctx_expire(ctx);
1074         kill_ctx_kr(ctx);
1075 }
1076
1077 /****************************************
1078  * (reverse) service                    *
1079  ****************************************/
1080
1081 /*
1082  * reverse context could have nothing to do with keyrings. here we still keep
1083  * the version which bind to a key, for future reference.
1084  */
1085 #define HAVE_REVERSE_CTX_NOKEY
1086
1087 #ifdef HAVE_REVERSE_CTX_NOKEY
1088
1089 static
1090 int sec_install_rctx_kr(struct ptlrpc_sec *sec,
1091                         struct ptlrpc_svc_ctx *svc_ctx)
1092 {
1093         struct ptlrpc_cli_ctx   *cli_ctx;
1094         struct vfs_cred          vcred = { 0, 0 };
1095         int                      rc;
1096
1097         LASSERT(sec);
1098         LASSERT(svc_ctx);
1099
1100         cli_ctx = ctx_create_kr(sec, &vcred);
1101         if (cli_ctx == NULL)
1102                 return -ENOMEM;
1103
1104         rc = gss_copy_rvc_cli_ctx(cli_ctx, svc_ctx);
1105         if (rc) {
1106                 CERROR("failed copy reverse cli ctx: %d\n", rc);
1107
1108                 ctx_put_kr(cli_ctx, 1);
1109                 return rc;
1110         }
1111
1112         rvs_sec_install_root_ctx_kr(sec, cli_ctx, NULL);
1113
1114         ctx_put_kr(cli_ctx, 1);
1115
1116         return 0;
1117 }
1118
1119 #else /* ! HAVE_REVERSE_CTX_NOKEY */
1120
1121 static
1122 int sec_install_rctx_kr(struct ptlrpc_sec *sec,
1123                         struct ptlrpc_svc_ctx *svc_ctx)
1124 {
1125         struct ptlrpc_cli_ctx   *cli_ctx = NULL;
1126         struct key              *key;
1127         struct vfs_cred          vcred = { 0, 0 };
1128         char                     desc[64];
1129         int                      rc;
1130
1131         LASSERT(sec);
1132         LASSERT(svc_ctx);
1133         CWARN("called\n");
1134
1135         construct_key_desc(desc, sizeof(desc), sec, 0);
1136
1137         key = key_alloc(&gss_key_type, desc, 0, 0,
1138                         KEY_POS_ALL | KEY_USR_ALL, 1);
1139         if (IS_ERR(key)) {
1140                 CERROR("failed to alloc key: %ld\n", PTR_ERR(key));
1141                 return PTR_ERR(key);
1142         }
1143
1144         rc = key_instantiate_and_link(key, NULL, 0, NULL, NULL);
1145         if (rc) {
1146                 CERROR("failed to instantiate key: %d\n", rc);
1147                 goto err_revoke;
1148         }
1149
1150         down_write(&key->sem);
1151
1152         LASSERT(key->payload.data == NULL);
1153
1154         cli_ctx = ctx_create_kr(sec, &vcred);
1155         if (cli_ctx == NULL) {
1156                 rc = -ENOMEM;
1157                 goto err_up;
1158         }
1159
1160         rc = gss_copy_rvc_cli_ctx(cli_ctx, svc_ctx);
1161         if (rc) {
1162                 CERROR("failed copy reverse cli ctx: %d\n", rc);
1163                 goto err_put;
1164         }
1165
1166         rvs_sec_install_root_ctx_kr(sec, cli_ctx, key);
1167
1168         ctx_put_kr(cli_ctx, 1);
1169         up_write(&key->sem);
1170
1171         rc = 0;
1172         CWARN("ok!\n");
1173 out:
1174         key_put(key);
1175         return rc;
1176
1177 err_put:
1178         ctx_put_kr(cli_ctx, 1);
1179 err_up:
1180         up_write(&key->sem);
1181 err_revoke:
1182         key_revoke(key);
1183         goto out;
1184 }
1185
1186 #endif /* HAVE_REVERSE_CTX_NOKEY */
1187
1188 /****************************************
1189  * service apis                         *
1190  ****************************************/
1191
1192 static
1193 int gss_svc_accept_kr(struct ptlrpc_request *req)
1194 {
1195         return gss_svc_accept(&gss_policy_keyring, req);
1196 }
1197
1198 static
1199 int gss_svc_install_rctx_kr(struct obd_import *imp,
1200                             struct ptlrpc_svc_ctx *svc_ctx)
1201 {
1202         struct ptlrpc_sec *sec;
1203         int                rc;
1204
1205         sec = sptlrpc_import_sec_ref(imp);
1206         LASSERT(sec);
1207
1208         rc = sec_install_rctx_kr(sec, svc_ctx);
1209         sptlrpc_sec_put(sec);
1210
1211         return rc;
1212 }
1213
1214 /****************************************
1215  * key apis                             *
1216  ****************************************/
1217
1218 static
1219 int gss_kt_instantiate(struct key *key, const void *data, size_t datalen)
1220 {
1221         int             rc;
1222         ENTRY;
1223
1224         if (data != NULL || datalen != 0) {
1225                 CERROR("invalid: data %p, len %lu\n", data, (long)datalen);
1226                 RETURN(-EINVAL);
1227         }
1228
1229         if (key->payload.data != 0) {
1230                 CERROR("key already have payload\n");
1231                 RETURN(-EINVAL);
1232         }
1233
1234         /* link the key to session keyring, so following context negotiation
1235          * rpc fired from user space could find this key. This will be unlinked
1236          * automatically when upcall processes die.
1237          *
1238          * we can't do this through keyctl from userspace, because the upcall
1239          * might be neither possessor nor owner of the key (setuid).
1240          *
1241          * the session keyring is created upon upcall, and don't change all
1242          * the way until upcall finished, so rcu lock is not needed here.
1243          */
1244         LASSERT(key_tgcred(current)->session_keyring);
1245
1246         lockdep_off();
1247         rc = key_link(key_tgcred(current)->session_keyring, key);
1248         lockdep_on();
1249         if (unlikely(rc)) {
1250                 CERROR("failed to link key %08x to keyring %08x: %d\n",
1251                        key->serial,
1252                        key_tgcred(current)->session_keyring->serial, rc);
1253                 RETURN(rc);
1254         }
1255
1256         CDEBUG(D_SEC, "key %p instantiated, ctx %p\n", key, key->payload.data);
1257         RETURN(0);
1258 }
1259
1260 /*
1261  * called with key semaphore write locked. it means we can operate
1262  * on the context without fear of loosing refcount.
1263  */
1264 static
1265 int gss_kt_update(struct key *key, const void *data, size_t datalen)
1266 {
1267         struct ptlrpc_cli_ctx   *ctx = key->payload.data;
1268         struct gss_cli_ctx      *gctx;
1269         rawobj_t                 tmpobj = RAWOBJ_EMPTY;
1270         __u32                    datalen32 = (__u32) datalen;
1271         int                      rc;
1272         ENTRY;
1273
1274         if (data == NULL || datalen == 0) {
1275                 CWARN("invalid: data %p, len %lu\n", data, (long)datalen);
1276                 RETURN(-EINVAL);
1277         }
1278
1279         /* if upcall finished negotiation too fast (mostly likely because
1280          * of local error happened) and call kt_update(), the ctx
1281          * might be still NULL. but the key will finally be associate
1282          * with a context, or be revoked. if key status is fine, return
1283          * -EAGAIN to allow userspace sleep a while and call again. */
1284         if (ctx == NULL) {
1285                 CDEBUG(D_SEC, "update too soon: key %p(%x) flags %lx\n",
1286                       key, key->serial, key->flags);
1287
1288                 rc = key_validate(key);
1289                 if (rc == 0)
1290                         RETURN(-EAGAIN);
1291                 else
1292                         RETURN(rc);
1293         }
1294
1295         LASSERT(atomic_read(&ctx->cc_refcount) > 0);
1296         LASSERT(ctx->cc_sec);
1297
1298         ctx_clear_timer_kr(ctx);
1299
1300         /* don't proceed if already refreshed */
1301         if (cli_ctx_is_refreshed(ctx)) {
1302                 CWARN("ctx already done refresh\n");
1303                 RETURN(0);
1304         }
1305
1306         sptlrpc_cli_ctx_get(ctx);
1307         gctx = ctx2gctx(ctx);
1308
1309         rc = buffer_extract_bytes(&data, &datalen32, &gctx->gc_win,
1310                                   sizeof(gctx->gc_win));
1311         if (rc) {
1312                 CERROR("failed extract seq_win\n");
1313                 goto out;
1314         }
1315
1316         if (gctx->gc_win == 0) {
1317                 __u32   nego_rpc_err, nego_gss_err;
1318
1319                 rc = buffer_extract_bytes(&data, &datalen32, &nego_rpc_err,
1320                                           sizeof(nego_rpc_err));
1321                 if (rc) {
1322                         CERROR("failed to extrace rpc rc\n");
1323                         goto out;
1324                 }
1325
1326                 rc = buffer_extract_bytes(&data, &datalen32, &nego_gss_err,
1327                                           sizeof(nego_gss_err));
1328                 if (rc) {
1329                         CERROR("failed to extrace gss rc\n");
1330                         goto out;
1331                 }
1332
1333                 CERROR("negotiation: rpc err %d, gss err %x\n",
1334                        nego_rpc_err, nego_gss_err);
1335
1336                 rc = nego_rpc_err ? nego_rpc_err : -EACCES;
1337         } else {
1338                 rc = rawobj_extract_local_alloc(&gctx->gc_handle,
1339                                                 (__u32 **) &data, &datalen32);
1340                 if (rc) {
1341                         CERROR("failed extract handle\n");
1342                         goto out;
1343                 }
1344
1345                 rc = rawobj_extract_local(&tmpobj, (__u32 **) &data,&datalen32);
1346                 if (rc) {
1347                         CERROR("failed extract mech\n");
1348                         goto out;
1349                 }
1350
1351                 rc = lgss_import_sec_context(&tmpobj,
1352                                              sec2gsec(ctx->cc_sec)->gs_mech,
1353                                              &gctx->gc_mechctx);
1354                 if (rc != GSS_S_COMPLETE)
1355                         CERROR("failed import context\n");
1356                 else
1357                         rc = 0;
1358         }
1359 out:
1360         /* we don't care what current status of this ctx, even someone else
1361          * is operating on the ctx at the same time. we just add up our own
1362          * opinions here. */
1363         if (rc == 0) {
1364                 gss_cli_ctx_uptodate(gctx);
1365         } else {
1366                 /* this will also revoke the key. has to be done before
1367                  * wakeup waiters otherwise they can find the stale key */
1368                 kill_key_locked(key);
1369
1370                 cli_ctx_expire(ctx);
1371
1372                 if (rc != -ERESTART)
1373                         set_bit(PTLRPC_CTX_ERROR_BIT, &ctx->cc_flags);
1374         }
1375
1376         /* let user space think it's a success */
1377         sptlrpc_cli_ctx_put(ctx, 1);
1378         RETURN(0);
1379 }
1380
1381 static
1382 int gss_kt_match(const struct key *key, const void *desc)
1383 {
1384         return (strcmp(key->description, (const char *) desc) == 0);
1385 }
1386
1387 static
1388 void gss_kt_destroy(struct key *key)
1389 {
1390         ENTRY;
1391         LASSERT(key->payload.data == NULL);
1392         CDEBUG(D_SEC, "destroy key %p\n", key);
1393         EXIT;
1394 }
1395
1396 static
1397 void gss_kt_describe(const struct key *key, struct seq_file *s)
1398 {
1399         if (key->description == NULL)
1400                 seq_puts(s, "[null]");
1401         else
1402                 seq_puts(s, key->description);
1403 }
1404
1405 static struct key_type gss_key_type =
1406 {
1407         .name           = "lgssc",
1408         .def_datalen    = 0,
1409         .instantiate    = gss_kt_instantiate,
1410         .update         = gss_kt_update,
1411         .match          = gss_kt_match,
1412         .destroy        = gss_kt_destroy,
1413         .describe       = gss_kt_describe,
1414 };
1415
1416 /****************************************
1417  * lustre gss keyring policy            *
1418  ****************************************/
1419
1420 static struct ptlrpc_ctx_ops gss_keyring_ctxops = {
1421         .match                  = gss_cli_ctx_match,
1422         .refresh                = gss_cli_ctx_refresh_kr,
1423         .validate               = gss_cli_ctx_validate_kr,
1424         .die                    = gss_cli_ctx_die_kr,
1425         .sign                   = gss_cli_ctx_sign,
1426         .verify                 = gss_cli_ctx_verify,
1427         .seal                   = gss_cli_ctx_seal,
1428         .unseal                 = gss_cli_ctx_unseal,
1429         .wrap_bulk              = gss_cli_ctx_wrap_bulk,
1430         .unwrap_bulk            = gss_cli_ctx_unwrap_bulk,
1431 };
1432
1433 static struct ptlrpc_sec_cops gss_sec_keyring_cops = {
1434         .create_sec             = gss_sec_create_kr,
1435         .destroy_sec            = gss_sec_destroy_kr,
1436         .kill_sec               = gss_sec_kill,
1437         .lookup_ctx             = gss_sec_lookup_ctx_kr,
1438         .release_ctx            = gss_sec_release_ctx_kr,
1439         .flush_ctx_cache        = gss_sec_flush_ctx_cache_kr,
1440         .gc_ctx                 = gss_sec_gc_ctx_kr,
1441         .install_rctx           = gss_sec_install_rctx,
1442         .alloc_reqbuf           = gss_alloc_reqbuf,
1443         .free_reqbuf            = gss_free_reqbuf,
1444         .alloc_repbuf           = gss_alloc_repbuf,
1445         .free_repbuf            = gss_free_repbuf,
1446         .enlarge_reqbuf         = gss_enlarge_reqbuf,
1447         .display                = gss_sec_display_kr,
1448 };
1449
1450 static struct ptlrpc_sec_sops gss_sec_keyring_sops = {
1451         .accept                 = gss_svc_accept_kr,
1452         .invalidate_ctx         = gss_svc_invalidate_ctx,
1453         .alloc_rs               = gss_svc_alloc_rs,
1454         .authorize              = gss_svc_authorize,
1455         .free_rs                = gss_svc_free_rs,
1456         .free_ctx               = gss_svc_free_ctx,
1457         .prep_bulk              = gss_svc_prep_bulk,
1458         .unwrap_bulk            = gss_svc_unwrap_bulk,
1459         .wrap_bulk              = gss_svc_wrap_bulk,
1460         .install_rctx           = gss_svc_install_rctx_kr,
1461 };
1462
1463 static struct ptlrpc_sec_policy gss_policy_keyring = {
1464         .sp_owner               = THIS_MODULE,
1465         .sp_name                = "gss.keyring",
1466         .sp_policy              = SPTLRPC_POLICY_GSS,
1467         .sp_cops                = &gss_sec_keyring_cops,
1468         .sp_sops                = &gss_sec_keyring_sops,
1469 };
1470
1471
1472 int __init gss_init_keyring(void)
1473 {
1474         int rc;
1475
1476         rc = register_key_type(&gss_key_type);
1477         if (rc) {
1478                 CERROR("failed to register keyring type: %d\n", rc);
1479                 return rc;
1480         }
1481
1482         rc = sptlrpc_register_policy(&gss_policy_keyring);
1483         if (rc) {
1484                 unregister_key_type(&gss_key_type);
1485                 return rc;
1486         }
1487
1488         return 0;
1489 }
1490
1491 void __exit gss_exit_keyring(void)
1492 {
1493         unregister_key_type(&gss_key_type);
1494         sptlrpc_unregister_policy(&gss_policy_keyring);
1495 }