1 // SPDX-License-Identifier: GPL-2.0+
3 * GSS Proxy upcall module
5 * Copyright (C) 2012 Simo Sorce <simo@redhat.com>
8 #include <linux/sunrpc/svcauth.h>
9 #include "gss_rpc_xdr.h"
11 static int gssx_enc_bool(struct xdr_stream *xdr, int v)
15 p = xdr_reserve_space(xdr, 4);
16 if (unlikely(p == NULL))
18 *p = v ? xdr_one : xdr_zero;
22 static int gssx_dec_bool(struct xdr_stream *xdr, u32 *v)
26 p = xdr_inline_decode(xdr, 4);
27 if (unlikely(p == NULL))
33 static int gssx_enc_buffer(struct xdr_stream *xdr,
34 const gssx_buffer *buf)
38 p = xdr_reserve_space(xdr, sizeof(u32) + buf->len);
41 xdr_encode_opaque(p, buf->data, buf->len);
45 static int gssx_enc_in_token(struct xdr_stream *xdr,
46 const struct gssp_in_token *in)
50 p = xdr_reserve_space(xdr, 4);
53 *p = cpu_to_be32(in->page_len);
55 /* all we need to do is to write pages */
56 xdr_write_pages(xdr, in->pages, in->page_base, in->page_len);
62 static int gssx_dec_buffer(struct xdr_stream *xdr,
68 p = xdr_inline_decode(xdr, 4);
69 if (unlikely(p == NULL))
72 length = be32_to_cpup(p);
73 p = xdr_inline_decode(xdr, length);
74 if (unlikely(p == NULL))
78 /* we intentionally are not interested in this buffer */
81 if (length > buf->len)
85 buf->data = kmemdup(p, length, GFP_KERNEL);
89 memcpy(buf->data, p, length);
95 static int gssx_enc_option(struct xdr_stream *xdr,
96 struct gssx_option *opt)
100 err = gssx_enc_buffer(xdr, &opt->option);
103 err = gssx_enc_buffer(xdr, &opt->value);
107 static int gssx_dec_option(struct xdr_stream *xdr,
108 struct gssx_option *opt)
112 err = gssx_dec_buffer(xdr, &opt->option);
115 err = gssx_dec_buffer(xdr, &opt->value);
119 static int dummy_enc_opt_array(struct xdr_stream *xdr,
120 const struct gssx_option_array *oa)
127 p = xdr_reserve_space(xdr, 4);
135 static int dummy_dec_opt_array(struct xdr_stream *xdr,
136 struct gssx_option_array *oa)
138 struct gssx_option dummy;
142 p = xdr_inline_decode(xdr, 4);
143 if (unlikely(p == NULL))
145 count = be32_to_cpup(p++);
146 memset(&dummy, 0, sizeof(dummy));
147 for (i = 0; i < count; i++) {
148 gssx_dec_option(xdr, &dummy);
156 static int get_host_u32(struct xdr_stream *xdr, u32 *res)
160 p = xdr_inline_decode(xdr, 4);
163 /* Contents of linux creds are all host-endian: */
164 memcpy(res, p, sizeof(u32));
168 static int gssx_dec_linux_creds(struct xdr_stream *xdr,
169 struct svc_cred *creds)
177 p = xdr_inline_decode(xdr, 4);
178 if (unlikely(p == NULL))
181 length = be32_to_cpup(p);
183 if (length > (3 + NGROUPS_MAX) * sizeof(u32))
187 err = get_host_u32(xdr, &tmp);
190 creds->cr_uid = make_kuid(&init_user_ns, tmp);
193 err = get_host_u32(xdr, &tmp);
196 creds->cr_gid = make_kgid(&init_user_ns, tmp);
198 /* number of additional gid's */
199 err = get_host_u32(xdr, &tmp);
203 if ((3 + N) * sizeof(u32) != length)
205 creds->cr_group_info = groups_alloc(N);
206 if (creds->cr_group_info == NULL)
210 for (i = 0; i < N; i++) {
212 err = get_host_u32(xdr, &tmp);
214 goto out_free_groups;
216 kgid = make_kgid(&init_user_ns, tmp);
217 if (!gid_valid(kgid))
218 goto out_free_groups;
219 creds->cr_group_info->gid[i] = kgid;
221 groups_sort(creds->cr_group_info);
225 groups_free(creds->cr_group_info);
229 static int gssx_dec_option_array(struct xdr_stream *xdr,
230 struct gssx_option_array *oa)
232 struct svc_cred *creds;
237 p = xdr_inline_decode(xdr, 4);
238 if (unlikely(p == NULL))
240 count = be32_to_cpup(p++);
244 /* we recognize only 1 currently: CREDS_VALUE */
247 oa->data = kmalloc(sizeof(struct gssx_option), GFP_KERNEL);
251 creds = kzalloc(sizeof(struct svc_cred), GFP_KERNEL);
257 oa->data[0].option.data = CREDS_VALUE;
258 oa->data[0].option.len = sizeof(CREDS_VALUE);
259 oa->data[0].value.data = (void *)creds;
260 oa->data[0].value.len = 0;
262 for (i = 0; i < count; i++) {
263 gssx_buffer dummy = { 0, NULL };
267 p = xdr_inline_decode(xdr, 4);
268 if (unlikely(p == NULL)) {
273 length = be32_to_cpup(p);
274 p = xdr_inline_decode(xdr, length);
275 if (unlikely(p == NULL)) {
280 if (length == sizeof(CREDS_VALUE) &&
281 memcmp(p, CREDS_VALUE, sizeof(CREDS_VALUE)) == 0) {
282 /* We have creds here. parse them */
283 err = gssx_dec_linux_creds(xdr, creds);
286 oa->data[0].value.len = 1; /* presence */
288 /* consume uninteresting buffer */
289 err = gssx_dec_buffer(xdr, &dummy);
304 static int gssx_dec_status(struct xdr_stream *xdr,
305 struct gssx_status *status)
310 /* status->major_status */
311 p = xdr_inline_decode(xdr, 8);
312 if (unlikely(p == NULL))
314 p = xdr_decode_hyper(p, &status->major_status);
317 err = gssx_dec_buffer(xdr, &status->mech);
321 /* status->minor_status */
322 p = xdr_inline_decode(xdr, 8);
323 if (unlikely(p == NULL))
325 p = xdr_decode_hyper(p, &status->minor_status);
327 /* status->major_status_string */
328 err = gssx_dec_buffer(xdr, &status->major_status_string);
332 /* status->minor_status_string */
333 err = gssx_dec_buffer(xdr, &status->minor_status_string);
337 /* status->server_ctx */
338 err = gssx_dec_buffer(xdr, &status->server_ctx);
342 /* we assume we have no options for now, so simply consume them */
343 /* status->options */
344 err = dummy_dec_opt_array(xdr, &status->options);
349 static int gssx_enc_call_ctx(struct xdr_stream *xdr,
350 const struct gssx_call_ctx *ctx)
352 struct gssx_option opt;
357 err = gssx_enc_buffer(xdr, &ctx->locale);
361 /* ctx->server_ctx */
362 err = gssx_enc_buffer(xdr, &ctx->server_ctx);
366 /* we always want to ask for lucid contexts */
368 p = xdr_reserve_space(xdr, 4);
371 /* we want a lucid_v1 context */
372 opt.option.data = LUCID_OPTION;
373 opt.option.len = sizeof(LUCID_OPTION);
374 opt.value.data = LUCID_VALUE;
375 opt.value.len = sizeof(LUCID_VALUE);
376 err = gssx_enc_option(xdr, &opt);
378 /* ..and user creds */
379 opt.option.data = CREDS_OPTION;
380 opt.option.len = sizeof(CREDS_OPTION);
381 opt.value.data = CREDS_VALUE;
382 opt.value.len = sizeof(CREDS_VALUE);
383 err = gssx_enc_option(xdr, &opt);
388 static int gssx_dec_name_attr(struct xdr_stream *xdr,
389 struct gssx_name_attr *attr)
394 err = gssx_dec_buffer(xdr, &attr->attr);
399 err = gssx_dec_buffer(xdr, &attr->value);
403 /* attr->extensions */
404 err = dummy_dec_opt_array(xdr, &attr->extensions);
409 static int dummy_enc_nameattr_array(struct xdr_stream *xdr,
410 struct gssx_name_attr_array *naa)
417 p = xdr_reserve_space(xdr, 4);
425 static int dummy_dec_nameattr_array(struct xdr_stream *xdr,
426 struct gssx_name_attr_array *naa)
428 struct gssx_name_attr dummy = { .attr = {.len = 0} };
432 p = xdr_inline_decode(xdr, 4);
433 if (unlikely(p == NULL))
435 count = be32_to_cpup(p++);
436 for (i = 0; i < count; i++) {
437 gssx_dec_name_attr(xdr, &dummy);
445 static struct xdr_netobj zero_netobj = {};
447 static struct gssx_name_attr_array zero_name_attr_array = {};
449 static struct gssx_option_array zero_option_array = {};
451 static int gssx_enc_name(struct xdr_stream *xdr,
452 struct gssx_name *name)
456 /* name->display_name */
457 err = gssx_enc_buffer(xdr, &name->display_name);
461 /* name->name_type */
462 err = gssx_enc_buffer(xdr, &zero_netobj);
466 /* name->exported_name */
467 err = gssx_enc_buffer(xdr, &zero_netobj);
471 /* name->exported_composite_name */
472 err = gssx_enc_buffer(xdr, &zero_netobj);
476 /* leave name_attributes empty for now, will add once we have any
477 * to pass up at all */
478 /* name->name_attributes */
479 err = dummy_enc_nameattr_array(xdr, &zero_name_attr_array);
483 /* leave options empty for now, will add once we have any options
484 * to pass up at all */
485 /* name->extensions */
486 err = dummy_enc_opt_array(xdr, &zero_option_array);
492 static int gssx_dec_name(struct xdr_stream *xdr,
493 struct gssx_name *name)
495 struct xdr_netobj dummy_netobj = { .len = 0 };
496 struct gssx_name_attr_array dummy_name_attr_array = { .count = 0 };
497 struct gssx_option_array dummy_option_array = { .count = 0 };
500 /* name->display_name */
501 err = gssx_dec_buffer(xdr, &name->display_name);
505 /* name->name_type */
506 err = gssx_dec_buffer(xdr, &dummy_netobj);
510 /* name->exported_name */
511 err = gssx_dec_buffer(xdr, &dummy_netobj);
515 /* name->exported_composite_name */
516 err = gssx_dec_buffer(xdr, &dummy_netobj);
520 /* we assume we have no attributes for now, so simply consume them */
521 /* name->name_attributes */
522 err = dummy_dec_nameattr_array(xdr, &dummy_name_attr_array);
526 /* we assume we have no options for now, so simply consume them */
527 /* name->extensions */
528 err = dummy_dec_opt_array(xdr, &dummy_option_array);
533 static int dummy_enc_credel_array(struct xdr_stream *xdr,
534 struct gssx_cred_element_array *cea)
541 p = xdr_reserve_space(xdr, 4);
549 static int gssx_enc_cred(struct xdr_stream *xdr,
550 struct gssx_cred *cred)
554 /* cred->desired_name */
555 err = gssx_enc_name(xdr, &cred->desired_name);
560 err = dummy_enc_credel_array(xdr, &cred->elements);
564 /* cred->cred_handle_reference */
565 err = gssx_enc_buffer(xdr, &cred->cred_handle_reference);
569 /* cred->needs_release */
570 err = gssx_enc_bool(xdr, cred->needs_release);
575 static int gssx_enc_ctx(struct xdr_stream *xdr,
576 struct gssx_ctx *ctx)
581 /* ctx->exported_context_token */
582 err = gssx_enc_buffer(xdr, &ctx->exported_context_token);
587 err = gssx_enc_buffer(xdr, &ctx->state);
591 /* ctx->need_release */
592 err = gssx_enc_bool(xdr, ctx->need_release);
597 err = gssx_enc_buffer(xdr, &ctx->mech);
602 err = gssx_enc_name(xdr, &ctx->src_name);
607 err = gssx_enc_name(xdr, &ctx->targ_name);
612 p = xdr_reserve_space(xdr, 8+8);
615 p = xdr_encode_hyper(p, ctx->lifetime);
618 p = xdr_encode_hyper(p, ctx->ctx_flags);
620 /* ctx->locally_initiated */
621 err = gssx_enc_bool(xdr, ctx->locally_initiated);
626 err = gssx_enc_bool(xdr, ctx->open);
630 /* leave options empty for now, will add once we have any options
631 * to pass up at all */
633 err = dummy_enc_opt_array(xdr, &ctx->options);
638 static int gssx_dec_ctx(struct xdr_stream *xdr,
639 struct gssx_ctx *ctx)
644 /* ctx->exported_context_token */
645 err = gssx_dec_buffer(xdr, &ctx->exported_context_token);
650 err = gssx_dec_buffer(xdr, &ctx->state);
654 /* ctx->need_release */
655 err = gssx_dec_bool(xdr, &ctx->need_release);
660 err = gssx_dec_buffer(xdr, &ctx->mech);
665 err = gssx_dec_name(xdr, &ctx->src_name);
670 err = gssx_dec_name(xdr, &ctx->targ_name);
675 p = xdr_inline_decode(xdr, 8+8);
676 if (unlikely(p == NULL))
678 p = xdr_decode_hyper(p, &ctx->lifetime);
681 p = xdr_decode_hyper(p, &ctx->ctx_flags);
683 /* ctx->locally_initiated */
684 err = gssx_dec_bool(xdr, &ctx->locally_initiated);
689 err = gssx_dec_bool(xdr, &ctx->open);
693 /* we assume we have no options for now, so simply consume them */
695 err = dummy_dec_opt_array(xdr, &ctx->options);
700 static int gssx_enc_cb(struct xdr_stream *xdr, struct gssx_cb *cb)
705 /* cb->initiator_addrtype */
706 p = xdr_reserve_space(xdr, 8);
709 p = xdr_encode_hyper(p, cb->initiator_addrtype);
711 /* cb->initiator_address */
712 err = gssx_enc_buffer(xdr, &cb->initiator_address);
716 /* cb->acceptor_addrtype */
717 p = xdr_reserve_space(xdr, 8);
720 p = xdr_encode_hyper(p, cb->acceptor_addrtype);
722 /* cb->acceptor_address */
723 err = gssx_enc_buffer(xdr, &cb->acceptor_address);
727 /* cb->application_data */
728 err = gssx_enc_buffer(xdr, &cb->application_data);
733 void gssx_enc_accept_sec_context(struct rpc_rqst *req,
734 struct xdr_stream *xdr,
737 const struct gssx_arg_accept_sec_context *arg = data;
740 err = gssx_enc_call_ctx(xdr, &arg->call_ctx);
744 /* arg->context_handle */
745 if (arg->context_handle)
746 err = gssx_enc_ctx(xdr, arg->context_handle);
748 err = gssx_enc_bool(xdr, 0);
752 /* arg->cred_handle */
753 if (arg->cred_handle)
754 err = gssx_enc_cred(xdr, arg->cred_handle);
756 err = gssx_enc_bool(xdr, 0);
760 /* arg->input_token */
761 err = gssx_enc_in_token(xdr, &arg->input_token);
767 err = gssx_enc_cb(xdr, arg->input_cb);
769 err = gssx_enc_bool(xdr, 0);
773 err = gssx_enc_bool(xdr, arg->ret_deleg_cred);
777 /* leave options empty for now, will add once we have any options
778 * to pass up at all */
780 err = dummy_enc_opt_array(xdr, &arg->options);
782 xdr_inline_pages(&req->rq_rcv_buf,
783 PAGE_SIZE/2 /* pretty arbitrary */,
784 arg->pages, 0 /* page base */, arg->npages * PAGE_SIZE);
787 dprintk("RPC: gssx_enc_accept_sec_context: %d\n", err);
790 int gssx_dec_accept_sec_context(struct rpc_rqst *rqstp,
791 struct xdr_stream *xdr,
794 struct gssx_res_accept_sec_context *res = data;
797 struct page *scratch;
799 scratch = alloc_page(GFP_KERNEL);
802 xdr_set_scratch_page(xdr, scratch);
805 err = gssx_dec_status(xdr, &res->status);
809 /* res->context_handle */
810 err = gssx_dec_bool(xdr, &value_follows);
814 err = gssx_dec_ctx(xdr, res->context_handle);
818 res->context_handle = NULL;
821 /* res->output_token */
822 err = gssx_dec_bool(xdr, &value_follows);
826 err = gssx_dec_buffer(xdr, res->output_token);
830 res->output_token = NULL;
833 /* res->delegated_cred_handle */
834 err = gssx_dec_bool(xdr, &value_follows);
838 /* we do not support upcall servers sending this data. */
844 err = gssx_dec_option_array(xdr, &res->options);
847 __free_page(scratch);