GNU Linux-libre 4.19.314-gnu1
[releases.git] / net / sunrpc / auth_gss / gss_rpc_xdr.c
1 /*
2  * GSS Proxy upcall module
3  *
4  *  Copyright (C) 2012 Simo Sorce <simo@redhat.com>
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 as published by
8  * the Free Software Foundation; either version 2 of the License, or
9  * (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software
18  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
19  */
20
21 #include <linux/sunrpc/svcauth.h>
22 #include "gss_rpc_xdr.h"
23
24 static int gssx_enc_bool(struct xdr_stream *xdr, int v)
25 {
26         __be32 *p;
27
28         p = xdr_reserve_space(xdr, 4);
29         if (unlikely(p == NULL))
30                 return -ENOSPC;
31         *p = v ? xdr_one : xdr_zero;
32         return 0;
33 }
34
35 static int gssx_dec_bool(struct xdr_stream *xdr, u32 *v)
36 {
37         __be32 *p;
38
39         p = xdr_inline_decode(xdr, 4);
40         if (unlikely(p == NULL))
41                 return -ENOSPC;
42         *v = be32_to_cpu(*p);
43         return 0;
44 }
45
46 static int gssx_enc_buffer(struct xdr_stream *xdr,
47                            const gssx_buffer *buf)
48 {
49         __be32 *p;
50
51         p = xdr_reserve_space(xdr, sizeof(u32) + buf->len);
52         if (!p)
53                 return -ENOSPC;
54         xdr_encode_opaque(p, buf->data, buf->len);
55         return 0;
56 }
57
58 static int gssx_enc_in_token(struct xdr_stream *xdr,
59                              const struct gssp_in_token *in)
60 {
61         __be32 *p;
62
63         p = xdr_reserve_space(xdr, 4);
64         if (!p)
65                 return -ENOSPC;
66         *p = cpu_to_be32(in->page_len);
67
68         /* all we need to do is to write pages */
69         xdr_write_pages(xdr, in->pages, in->page_base, in->page_len);
70
71         return 0;
72 }
73
74
75 static int gssx_dec_buffer(struct xdr_stream *xdr,
76                            gssx_buffer *buf)
77 {
78         u32 length;
79         __be32 *p;
80
81         p = xdr_inline_decode(xdr, 4);
82         if (unlikely(p == NULL))
83                 return -ENOSPC;
84
85         length = be32_to_cpup(p);
86         p = xdr_inline_decode(xdr, length);
87         if (unlikely(p == NULL))
88                 return -ENOSPC;
89
90         if (buf->len == 0) {
91                 /* we intentionally are not interested in this buffer */
92                 return 0;
93         }
94         if (length > buf->len)
95                 return -ENOSPC;
96
97         if (!buf->data) {
98                 buf->data = kmemdup(p, length, GFP_KERNEL);
99                 if (!buf->data)
100                         return -ENOMEM;
101         } else {
102                 memcpy(buf->data, p, length);
103         }
104         buf->len = length;
105         return 0;
106 }
107
108 static int gssx_enc_option(struct xdr_stream *xdr,
109                            struct gssx_option *opt)
110 {
111         int err;
112
113         err = gssx_enc_buffer(xdr, &opt->option);
114         if (err)
115                 return err;
116         err = gssx_enc_buffer(xdr, &opt->value);
117         return err;
118 }
119
120 static int gssx_dec_option(struct xdr_stream *xdr,
121                            struct gssx_option *opt)
122 {
123         int err;
124
125         err = gssx_dec_buffer(xdr, &opt->option);
126         if (err)
127                 return err;
128         err = gssx_dec_buffer(xdr, &opt->value);
129         return err;
130 }
131
132 static int dummy_enc_opt_array(struct xdr_stream *xdr,
133                                 const struct gssx_option_array *oa)
134 {
135         __be32 *p;
136
137         if (oa->count != 0)
138                 return -EINVAL;
139
140         p = xdr_reserve_space(xdr, 4);
141         if (!p)
142                 return -ENOSPC;
143         *p = 0;
144
145         return 0;
146 }
147
148 static int dummy_dec_opt_array(struct xdr_stream *xdr,
149                                 struct gssx_option_array *oa)
150 {
151         struct gssx_option dummy;
152         u32 count, i;
153         __be32 *p;
154
155         p = xdr_inline_decode(xdr, 4);
156         if (unlikely(p == NULL))
157                 return -ENOSPC;
158         count = be32_to_cpup(p++);
159         memset(&dummy, 0, sizeof(dummy));
160         for (i = 0; i < count; i++) {
161                 gssx_dec_option(xdr, &dummy);
162         }
163
164         oa->count = 0;
165         oa->data = NULL;
166         return 0;
167 }
168
169 static int get_host_u32(struct xdr_stream *xdr, u32 *res)
170 {
171         __be32 *p;
172
173         p = xdr_inline_decode(xdr, 4);
174         if (!p)
175                 return -EINVAL;
176         /* Contents of linux creds are all host-endian: */
177         memcpy(res, p, sizeof(u32));
178         return 0;
179 }
180
181 static int gssx_dec_linux_creds(struct xdr_stream *xdr,
182                                 struct svc_cred *creds)
183 {
184         u32 length;
185         __be32 *p;
186         u32 tmp;
187         u32 N;
188         int i, err;
189
190         p = xdr_inline_decode(xdr, 4);
191         if (unlikely(p == NULL))
192                 return -ENOSPC;
193
194         length = be32_to_cpup(p);
195
196         if (length > (3 + NGROUPS_MAX) * sizeof(u32))
197                 return -ENOSPC;
198
199         /* uid */
200         err = get_host_u32(xdr, &tmp);
201         if (err)
202                 return err;
203         creds->cr_uid = make_kuid(&init_user_ns, tmp);
204
205         /* gid */
206         err = get_host_u32(xdr, &tmp);
207         if (err)
208                 return err;
209         creds->cr_gid = make_kgid(&init_user_ns, tmp);
210
211         /* number of additional gid's */
212         err = get_host_u32(xdr, &tmp);
213         if (err)
214                 return err;
215         N = tmp;
216         if ((3 + N) * sizeof(u32) != length)
217                 return -EINVAL;
218         creds->cr_group_info = groups_alloc(N);
219         if (creds->cr_group_info == NULL)
220                 return -ENOMEM;
221
222         /* gid's */
223         for (i = 0; i < N; i++) {
224                 kgid_t kgid;
225                 err = get_host_u32(xdr, &tmp);
226                 if (err)
227                         goto out_free_groups;
228                 err = -EINVAL;
229                 kgid = make_kgid(&init_user_ns, tmp);
230                 if (!gid_valid(kgid))
231                         goto out_free_groups;
232                 creds->cr_group_info->gid[i] = kgid;
233         }
234         groups_sort(creds->cr_group_info);
235
236         return 0;
237 out_free_groups:
238         groups_free(creds->cr_group_info);
239         return err;
240 }
241
242 static int gssx_dec_option_array(struct xdr_stream *xdr,
243                                  struct gssx_option_array *oa)
244 {
245         struct svc_cred *creds;
246         u32 count, i;
247         __be32 *p;
248         int err;
249
250         p = xdr_inline_decode(xdr, 4);
251         if (unlikely(p == NULL))
252                 return -ENOSPC;
253         count = be32_to_cpup(p++);
254         if (!count)
255                 return 0;
256
257         /* we recognize only 1 currently: CREDS_VALUE */
258         oa->count = 1;
259
260         oa->data = kmalloc(sizeof(struct gssx_option), GFP_KERNEL);
261         if (!oa->data)
262                 return -ENOMEM;
263
264         creds = kzalloc(sizeof(struct svc_cred), GFP_KERNEL);
265         if (!creds) {
266                 err = -ENOMEM;
267                 goto free_oa;
268         }
269
270         oa->data[0].option.data = CREDS_VALUE;
271         oa->data[0].option.len = sizeof(CREDS_VALUE);
272         oa->data[0].value.data = (void *)creds;
273         oa->data[0].value.len = 0;
274
275         for (i = 0; i < count; i++) {
276                 gssx_buffer dummy = { 0, NULL };
277                 u32 length;
278
279                 /* option buffer */
280                 p = xdr_inline_decode(xdr, 4);
281                 if (unlikely(p == NULL)) {
282                         err = -ENOSPC;
283                         goto free_creds;
284                 }
285
286                 length = be32_to_cpup(p);
287                 p = xdr_inline_decode(xdr, length);
288                 if (unlikely(p == NULL)) {
289                         err = -ENOSPC;
290                         goto free_creds;
291                 }
292
293                 if (length == sizeof(CREDS_VALUE) &&
294                     memcmp(p, CREDS_VALUE, sizeof(CREDS_VALUE)) == 0) {
295                         /* We have creds here. parse them */
296                         err = gssx_dec_linux_creds(xdr, creds);
297                         if (err)
298                                 goto free_creds;
299                         oa->data[0].value.len = 1; /* presence */
300                 } else {
301                         /* consume uninteresting buffer */
302                         err = gssx_dec_buffer(xdr, &dummy);
303                         if (err)
304                                 goto free_creds;
305                 }
306         }
307         return 0;
308
309 free_creds:
310         kfree(creds);
311 free_oa:
312         kfree(oa->data);
313         oa->data = NULL;
314         return err;
315 }
316
317 static int gssx_dec_status(struct xdr_stream *xdr,
318                            struct gssx_status *status)
319 {
320         __be32 *p;
321         int err;
322
323         /* status->major_status */
324         p = xdr_inline_decode(xdr, 8);
325         if (unlikely(p == NULL))
326                 return -ENOSPC;
327         p = xdr_decode_hyper(p, &status->major_status);
328
329         /* status->mech */
330         err = gssx_dec_buffer(xdr, &status->mech);
331         if (err)
332                 return err;
333
334         /* status->minor_status */
335         p = xdr_inline_decode(xdr, 8);
336         if (unlikely(p == NULL))
337                 return -ENOSPC;
338         p = xdr_decode_hyper(p, &status->minor_status);
339
340         /* status->major_status_string */
341         err = gssx_dec_buffer(xdr, &status->major_status_string);
342         if (err)
343                 return err;
344
345         /* status->minor_status_string */
346         err = gssx_dec_buffer(xdr, &status->minor_status_string);
347         if (err)
348                 return err;
349
350         /* status->server_ctx */
351         err = gssx_dec_buffer(xdr, &status->server_ctx);
352         if (err)
353                 return err;
354
355         /* we assume we have no options for now, so simply consume them */
356         /* status->options */
357         err = dummy_dec_opt_array(xdr, &status->options);
358
359         return err;
360 }
361
362 static int gssx_enc_call_ctx(struct xdr_stream *xdr,
363                              const struct gssx_call_ctx *ctx)
364 {
365         struct gssx_option opt;
366         __be32 *p;
367         int err;
368
369         /* ctx->locale */
370         err = gssx_enc_buffer(xdr, &ctx->locale);
371         if (err)
372                 return err;
373
374         /* ctx->server_ctx */
375         err = gssx_enc_buffer(xdr, &ctx->server_ctx);
376         if (err)
377                 return err;
378
379         /* we always want to ask for lucid contexts */
380         /* ctx->options */
381         p = xdr_reserve_space(xdr, 4);
382         *p = cpu_to_be32(2);
383
384         /* we want a lucid_v1 context */
385         opt.option.data = LUCID_OPTION;
386         opt.option.len = sizeof(LUCID_OPTION);
387         opt.value.data = LUCID_VALUE;
388         opt.value.len = sizeof(LUCID_VALUE);
389         err = gssx_enc_option(xdr, &opt);
390
391         /* ..and user creds */
392         opt.option.data = CREDS_OPTION;
393         opt.option.len = sizeof(CREDS_OPTION);
394         opt.value.data = CREDS_VALUE;
395         opt.value.len = sizeof(CREDS_VALUE);
396         err = gssx_enc_option(xdr, &opt);
397
398         return err;
399 }
400
401 static int gssx_dec_name_attr(struct xdr_stream *xdr,
402                              struct gssx_name_attr *attr)
403 {
404         int err;
405
406         /* attr->attr */
407         err = gssx_dec_buffer(xdr, &attr->attr);
408         if (err)
409                 return err;
410
411         /* attr->value */
412         err = gssx_dec_buffer(xdr, &attr->value);
413         if (err)
414                 return err;
415
416         /* attr->extensions */
417         err = dummy_dec_opt_array(xdr, &attr->extensions);
418
419         return err;
420 }
421
422 static int dummy_enc_nameattr_array(struct xdr_stream *xdr,
423                                     struct gssx_name_attr_array *naa)
424 {
425         __be32 *p;
426
427         if (naa->count != 0)
428                 return -EINVAL;
429
430         p = xdr_reserve_space(xdr, 4);
431         if (!p)
432                 return -ENOSPC;
433         *p = 0;
434
435         return 0;
436 }
437
438 static int dummy_dec_nameattr_array(struct xdr_stream *xdr,
439                                     struct gssx_name_attr_array *naa)
440 {
441         struct gssx_name_attr dummy = { .attr = {.len = 0} };
442         u32 count, i;
443         __be32 *p;
444
445         p = xdr_inline_decode(xdr, 4);
446         if (unlikely(p == NULL))
447                 return -ENOSPC;
448         count = be32_to_cpup(p++);
449         for (i = 0; i < count; i++) {
450                 gssx_dec_name_attr(xdr, &dummy);
451         }
452
453         naa->count = 0;
454         naa->data = NULL;
455         return 0;
456 }
457
458 static struct xdr_netobj zero_netobj = {};
459
460 static struct gssx_name_attr_array zero_name_attr_array = {};
461
462 static struct gssx_option_array zero_option_array = {};
463
464 static int gssx_enc_name(struct xdr_stream *xdr,
465                          struct gssx_name *name)
466 {
467         int err;
468
469         /* name->display_name */
470         err = gssx_enc_buffer(xdr, &name->display_name);
471         if (err)
472                 return err;
473
474         /* name->name_type */
475         err = gssx_enc_buffer(xdr, &zero_netobj);
476         if (err)
477                 return err;
478
479         /* name->exported_name */
480         err = gssx_enc_buffer(xdr, &zero_netobj);
481         if (err)
482                 return err;
483
484         /* name->exported_composite_name */
485         err = gssx_enc_buffer(xdr, &zero_netobj);
486         if (err)
487                 return err;
488
489         /* leave name_attributes empty for now, will add once we have any
490          * to pass up at all */
491         /* name->name_attributes */
492         err = dummy_enc_nameattr_array(xdr, &zero_name_attr_array);
493         if (err)
494                 return err;
495
496         /* leave options empty for now, will add once we have any options
497          * to pass up at all */
498         /* name->extensions */
499         err = dummy_enc_opt_array(xdr, &zero_option_array);
500
501         return err;
502 }
503
504
505 static int gssx_dec_name(struct xdr_stream *xdr,
506                          struct gssx_name *name)
507 {
508         struct xdr_netobj dummy_netobj = { .len = 0 };
509         struct gssx_name_attr_array dummy_name_attr_array = { .count = 0 };
510         struct gssx_option_array dummy_option_array = { .count = 0 };
511         int err;
512
513         /* name->display_name */
514         err = gssx_dec_buffer(xdr, &name->display_name);
515         if (err)
516                 return err;
517
518         /* name->name_type */
519         err = gssx_dec_buffer(xdr, &dummy_netobj);
520         if (err)
521                 return err;
522
523         /* name->exported_name */
524         err = gssx_dec_buffer(xdr, &dummy_netobj);
525         if (err)
526                 return err;
527
528         /* name->exported_composite_name */
529         err = gssx_dec_buffer(xdr, &dummy_netobj);
530         if (err)
531                 return err;
532
533         /* we assume we have no attributes for now, so simply consume them */
534         /* name->name_attributes */
535         err = dummy_dec_nameattr_array(xdr, &dummy_name_attr_array);
536         if (err)
537                 return err;
538
539         /* we assume we have no options for now, so simply consume them */
540         /* name->extensions */
541         err = dummy_dec_opt_array(xdr, &dummy_option_array);
542
543         return err;
544 }
545
546 static int dummy_enc_credel_array(struct xdr_stream *xdr,
547                                   struct gssx_cred_element_array *cea)
548 {
549         __be32 *p;
550
551         if (cea->count != 0)
552                 return -EINVAL;
553
554         p = xdr_reserve_space(xdr, 4);
555         if (!p)
556                 return -ENOSPC;
557         *p = 0;
558
559         return 0;
560 }
561
562 static int gssx_enc_cred(struct xdr_stream *xdr,
563                          struct gssx_cred *cred)
564 {
565         int err;
566
567         /* cred->desired_name */
568         err = gssx_enc_name(xdr, &cred->desired_name);
569         if (err)
570                 return err;
571
572         /* cred->elements */
573         err = dummy_enc_credel_array(xdr, &cred->elements);
574         if (err)
575                 return err;
576
577         /* cred->cred_handle_reference */
578         err = gssx_enc_buffer(xdr, &cred->cred_handle_reference);
579         if (err)
580                 return err;
581
582         /* cred->needs_release */
583         err = gssx_enc_bool(xdr, cred->needs_release);
584
585         return err;
586 }
587
588 static int gssx_enc_ctx(struct xdr_stream *xdr,
589                         struct gssx_ctx *ctx)
590 {
591         __be32 *p;
592         int err;
593
594         /* ctx->exported_context_token */
595         err = gssx_enc_buffer(xdr, &ctx->exported_context_token);
596         if (err)
597                 return err;
598
599         /* ctx->state */
600         err = gssx_enc_buffer(xdr, &ctx->state);
601         if (err)
602                 return err;
603
604         /* ctx->need_release */
605         err = gssx_enc_bool(xdr, ctx->need_release);
606         if (err)
607                 return err;
608
609         /* ctx->mech */
610         err = gssx_enc_buffer(xdr, &ctx->mech);
611         if (err)
612                 return err;
613
614         /* ctx->src_name */
615         err = gssx_enc_name(xdr, &ctx->src_name);
616         if (err)
617                 return err;
618
619         /* ctx->targ_name */
620         err = gssx_enc_name(xdr, &ctx->targ_name);
621         if (err)
622                 return err;
623
624         /* ctx->lifetime */
625         p = xdr_reserve_space(xdr, 8+8);
626         if (!p)
627                 return -ENOSPC;
628         p = xdr_encode_hyper(p, ctx->lifetime);
629
630         /* ctx->ctx_flags */
631         p = xdr_encode_hyper(p, ctx->ctx_flags);
632
633         /* ctx->locally_initiated */
634         err = gssx_enc_bool(xdr, ctx->locally_initiated);
635         if (err)
636                 return err;
637
638         /* ctx->open */
639         err = gssx_enc_bool(xdr, ctx->open);
640         if (err)
641                 return err;
642
643         /* leave options empty for now, will add once we have any options
644          * to pass up at all */
645         /* ctx->options */
646         err = dummy_enc_opt_array(xdr, &ctx->options);
647
648         return err;
649 }
650
651 static int gssx_dec_ctx(struct xdr_stream *xdr,
652                         struct gssx_ctx *ctx)
653 {
654         __be32 *p;
655         int err;
656
657         /* ctx->exported_context_token */
658         err = gssx_dec_buffer(xdr, &ctx->exported_context_token);
659         if (err)
660                 return err;
661
662         /* ctx->state */
663         err = gssx_dec_buffer(xdr, &ctx->state);
664         if (err)
665                 return err;
666
667         /* ctx->need_release */
668         err = gssx_dec_bool(xdr, &ctx->need_release);
669         if (err)
670                 return err;
671
672         /* ctx->mech */
673         err = gssx_dec_buffer(xdr, &ctx->mech);
674         if (err)
675                 return err;
676
677         /* ctx->src_name */
678         err = gssx_dec_name(xdr, &ctx->src_name);
679         if (err)
680                 return err;
681
682         /* ctx->targ_name */
683         err = gssx_dec_name(xdr, &ctx->targ_name);
684         if (err)
685                 return err;
686
687         /* ctx->lifetime */
688         p = xdr_inline_decode(xdr, 8+8);
689         if (unlikely(p == NULL))
690                 return -ENOSPC;
691         p = xdr_decode_hyper(p, &ctx->lifetime);
692
693         /* ctx->ctx_flags */
694         p = xdr_decode_hyper(p, &ctx->ctx_flags);
695
696         /* ctx->locally_initiated */
697         err = gssx_dec_bool(xdr, &ctx->locally_initiated);
698         if (err)
699                 return err;
700
701         /* ctx->open */
702         err = gssx_dec_bool(xdr, &ctx->open);
703         if (err)
704                 return err;
705
706         /* we assume we have no options for now, so simply consume them */
707         /* ctx->options */
708         err = dummy_dec_opt_array(xdr, &ctx->options);
709
710         return err;
711 }
712
713 static int gssx_enc_cb(struct xdr_stream *xdr, struct gssx_cb *cb)
714 {
715         __be32 *p;
716         int err;
717
718         /* cb->initiator_addrtype */
719         p = xdr_reserve_space(xdr, 8);
720         if (!p)
721                 return -ENOSPC;
722         p = xdr_encode_hyper(p, cb->initiator_addrtype);
723
724         /* cb->initiator_address */
725         err = gssx_enc_buffer(xdr, &cb->initiator_address);
726         if (err)
727                 return err;
728
729         /* cb->acceptor_addrtype */
730         p = xdr_reserve_space(xdr, 8);
731         if (!p)
732                 return -ENOSPC;
733         p = xdr_encode_hyper(p, cb->acceptor_addrtype);
734
735         /* cb->acceptor_address */
736         err = gssx_enc_buffer(xdr, &cb->acceptor_address);
737         if (err)
738                 return err;
739
740         /* cb->application_data */
741         err = gssx_enc_buffer(xdr, &cb->application_data);
742
743         return err;
744 }
745
746 void gssx_enc_accept_sec_context(struct rpc_rqst *req,
747                                  struct xdr_stream *xdr,
748                                  const void *data)
749 {
750         const struct gssx_arg_accept_sec_context *arg = data;
751         int err;
752
753         err = gssx_enc_call_ctx(xdr, &arg->call_ctx);
754         if (err)
755                 goto done;
756
757         /* arg->context_handle */
758         if (arg->context_handle)
759                 err = gssx_enc_ctx(xdr, arg->context_handle);
760         else
761                 err = gssx_enc_bool(xdr, 0);
762         if (err)
763                 goto done;
764
765         /* arg->cred_handle */
766         if (arg->cred_handle)
767                 err = gssx_enc_cred(xdr, arg->cred_handle);
768         else
769                 err = gssx_enc_bool(xdr, 0);
770         if (err)
771                 goto done;
772
773         /* arg->input_token */
774         err = gssx_enc_in_token(xdr, &arg->input_token);
775         if (err)
776                 goto done;
777
778         /* arg->input_cb */
779         if (arg->input_cb)
780                 err = gssx_enc_cb(xdr, arg->input_cb);
781         else
782                 err = gssx_enc_bool(xdr, 0);
783         if (err)
784                 goto done;
785
786         err = gssx_enc_bool(xdr, arg->ret_deleg_cred);
787         if (err)
788                 goto done;
789
790         /* leave options empty for now, will add once we have any options
791          * to pass up at all */
792         /* arg->options */
793         err = dummy_enc_opt_array(xdr, &arg->options);
794
795         xdr_inline_pages(&req->rq_rcv_buf,
796                 PAGE_SIZE/2 /* pretty arbitrary */,
797                 arg->pages, 0 /* page base */, arg->npages * PAGE_SIZE);
798 done:
799         if (err)
800                 dprintk("RPC:       gssx_enc_accept_sec_context: %d\n", err);
801 }
802
803 int gssx_dec_accept_sec_context(struct rpc_rqst *rqstp,
804                                 struct xdr_stream *xdr,
805                                 void *data)
806 {
807         struct gssx_res_accept_sec_context *res = data;
808         u32 value_follows;
809         int err;
810         struct page *scratch;
811
812         scratch = alloc_page(GFP_KERNEL);
813         if (!scratch)
814                 return -ENOMEM;
815         xdr_set_scratch_buffer(xdr, page_address(scratch), PAGE_SIZE);
816
817         /* res->status */
818         err = gssx_dec_status(xdr, &res->status);
819         if (err)
820                 goto out_free;
821
822         /* res->context_handle */
823         err = gssx_dec_bool(xdr, &value_follows);
824         if (err)
825                 goto out_free;
826         if (value_follows) {
827                 err = gssx_dec_ctx(xdr, res->context_handle);
828                 if (err)
829                         goto out_free;
830         } else {
831                 res->context_handle = NULL;
832         }
833
834         /* res->output_token */
835         err = gssx_dec_bool(xdr, &value_follows);
836         if (err)
837                 goto out_free;
838         if (value_follows) {
839                 err = gssx_dec_buffer(xdr, res->output_token);
840                 if (err)
841                         goto out_free;
842         } else {
843                 res->output_token = NULL;
844         }
845
846         /* res->delegated_cred_handle */
847         err = gssx_dec_bool(xdr, &value_follows);
848         if (err)
849                 goto out_free;
850         if (value_follows) {
851                 /* we do not support upcall servers sending this data. */
852                 err = -EINVAL;
853                 goto out_free;
854         }
855
856         /* res->options */
857         err = gssx_dec_option_array(xdr, &res->options);
858
859 out_free:
860         __free_page(scratch);
861         return err;
862 }