blob: 1021b4c0bdd21e776481e771a4f3a1385e67f5a6 [file] [log] [blame]
David Howells17926a72007-04-26 15:48:28 -07001/* RxRPC key management
2 *
3 * Copyright (C) 2007 Red Hat, Inc. All Rights Reserved.
4 * Written by David Howells (dhowells@redhat.com)
5 *
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * as published by the Free Software Foundation; either version
9 * 2 of the License, or (at your option) any later version.
10 *
11 * RxRPC keys should have a description of describing their purpose:
12 * "afs@CAMBRIDGE.REDHAT.COM>
13 */
14
Herbert Xu1afe5932016-01-24 21:19:01 +080015#include <crypto/skcipher.h>
David Howells17926a72007-04-26 15:48:28 -070016#include <linux/module.h>
17#include <linux/net.h>
18#include <linux/skbuff.h>
David Howells76181c12007-10-16 23:29:46 -070019#include <linux/key-type.h>
David Howells33941282009-09-14 01:17:35 +000020#include <linux/ctype.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090021#include <linux/slab.h>
David Howells17926a72007-04-26 15:48:28 -070022#include <net/sock.h>
23#include <net/af_rxrpc.h>
24#include <keys/rxrpc-type.h>
25#include <keys/user-type.h>
26#include "ar-internal.h"
27
David Howellsb9fffa32011-03-07 15:05:59 +000028static int rxrpc_vet_description_s(const char *);
David Howells8a7a3eb2014-07-18 18:56:36 +010029static int rxrpc_preparse(struct key_preparsed_payload *);
30static int rxrpc_preparse_s(struct key_preparsed_payload *);
31static void rxrpc_free_preparse(struct key_preparsed_payload *);
32static void rxrpc_free_preparse_s(struct key_preparsed_payload *);
David Howells17926a72007-04-26 15:48:28 -070033static void rxrpc_destroy(struct key *);
34static void rxrpc_destroy_s(struct key *);
35static void rxrpc_describe(const struct key *, struct seq_file *);
David Howellsed6dd182009-09-14 01:17:40 +000036static long rxrpc_read(const struct key *, char __user *, size_t);
David Howells17926a72007-04-26 15:48:28 -070037
38/*
39 * rxrpc defined keys take an arbitrary string as the description and an
40 * arbitrary blob of data as the payload
41 */
42struct key_type key_type_rxrpc = {
43 .name = "rxrpc",
David Howells8a7a3eb2014-07-18 18:56:36 +010044 .preparse = rxrpc_preparse,
45 .free_preparse = rxrpc_free_preparse,
46 .instantiate = generic_key_instantiate,
David Howells17926a72007-04-26 15:48:28 -070047 .destroy = rxrpc_destroy,
48 .describe = rxrpc_describe,
David Howellsed6dd182009-09-14 01:17:40 +000049 .read = rxrpc_read,
David Howells17926a72007-04-26 15:48:28 -070050};
David Howells17926a72007-04-26 15:48:28 -070051EXPORT_SYMBOL(key_type_rxrpc);
52
53/*
54 * rxrpc server defined keys take "<serviceId>:<securityIndex>" as the
55 * description and an 8-byte decryption key as the payload
56 */
57struct key_type key_type_rxrpc_s = {
58 .name = "rxrpc_s",
David Howellsb9fffa32011-03-07 15:05:59 +000059 .vet_description = rxrpc_vet_description_s,
David Howells8a7a3eb2014-07-18 18:56:36 +010060 .preparse = rxrpc_preparse_s,
61 .free_preparse = rxrpc_free_preparse_s,
62 .instantiate = generic_key_instantiate,
David Howells17926a72007-04-26 15:48:28 -070063 .destroy = rxrpc_destroy_s,
64 .describe = rxrpc_describe,
65};
66
67/*
David Howellsb9fffa32011-03-07 15:05:59 +000068 * Vet the description for an RxRPC server key
69 */
70static int rxrpc_vet_description_s(const char *desc)
71{
72 unsigned long num;
73 char *p;
74
75 num = simple_strtoul(desc, &p, 10);
76 if (*p != ':' || num > 65535)
77 return -EINVAL;
78 num = simple_strtoul(p + 1, &p, 10);
79 if (*p || num < 1 || num > 255)
80 return -EINVAL;
81 return 0;
82}
83
84/*
David Howells33941282009-09-14 01:17:35 +000085 * parse an RxKAD type XDR format token
86 * - the caller guarantees we have at least 4 words
87 */
David Howells8a7a3eb2014-07-18 18:56:36 +010088static int rxrpc_preparse_xdr_rxkad(struct key_preparsed_payload *prep,
89 size_t datalen,
90 const __be32 *xdr, unsigned int toklen)
David Howells33941282009-09-14 01:17:35 +000091{
David Howells994551532009-09-14 01:17:46 +000092 struct rxrpc_key_token *token, **pptoken;
David Howells33941282009-09-14 01:17:35 +000093 size_t plen;
94 u32 tktlen;
David Howells33941282009-09-14 01:17:35 +000095
96 _enter(",{%x,%x,%x,%x},%u",
97 ntohl(xdr[0]), ntohl(xdr[1]), ntohl(xdr[2]), ntohl(xdr[3]),
98 toklen);
99
100 if (toklen <= 8 * 4)
101 return -EKEYREJECTED;
102 tktlen = ntohl(xdr[7]);
103 _debug("tktlen: %x", tktlen);
104 if (tktlen > AFSTOKEN_RK_TIX_MAX)
105 return -EKEYREJECTED;
Nathaniel W Filardofde01332014-05-15 15:51:22 +0100106 if (toklen < 8 * 4 + tktlen)
David Howells33941282009-09-14 01:17:35 +0000107 return -EKEYREJECTED;
108
109 plen = sizeof(*token) + sizeof(*token->kad) + tktlen;
David Howells8a7a3eb2014-07-18 18:56:36 +0100110 prep->quotalen = datalen + plen;
David Howells33941282009-09-14 01:17:35 +0000111
112 plen -= sizeof(*token);
Anton Blanchard0a93ea22011-02-25 15:33:17 +0000113 token = kzalloc(sizeof(*token), GFP_KERNEL);
David Howells33941282009-09-14 01:17:35 +0000114 if (!token)
115 return -ENOMEM;
116
Anton Blanchard0a93ea22011-02-25 15:33:17 +0000117 token->kad = kzalloc(plen, GFP_KERNEL);
David Howells33941282009-09-14 01:17:35 +0000118 if (!token->kad) {
119 kfree(token);
120 return -ENOMEM;
121 }
122
123 token->security_index = RXRPC_SECURITY_RXKAD;
124 token->kad->ticket_len = tktlen;
125 token->kad->vice_id = ntohl(xdr[0]);
126 token->kad->kvno = ntohl(xdr[1]);
127 token->kad->start = ntohl(xdr[4]);
128 token->kad->expiry = ntohl(xdr[5]);
129 token->kad->primary_flag = ntohl(xdr[6]);
130 memcpy(&token->kad->session_key, &xdr[2], 8);
131 memcpy(&token->kad->ticket, &xdr[8], tktlen);
132
133 _debug("SCIX: %u", token->security_index);
134 _debug("TLEN: %u", token->kad->ticket_len);
135 _debug("EXPY: %x", token->kad->expiry);
136 _debug("KVNO: %u", token->kad->kvno);
137 _debug("PRIM: %u", token->kad->primary_flag);
138 _debug("SKEY: %02x%02x%02x%02x%02x%02x%02x%02x",
139 token->kad->session_key[0], token->kad->session_key[1],
140 token->kad->session_key[2], token->kad->session_key[3],
141 token->kad->session_key[4], token->kad->session_key[5],
142 token->kad->session_key[6], token->kad->session_key[7]);
143 if (token->kad->ticket_len >= 8)
144 _debug("TCKT: %02x%02x%02x%02x%02x%02x%02x%02x",
145 token->kad->ticket[0], token->kad->ticket[1],
146 token->kad->ticket[2], token->kad->ticket[3],
147 token->kad->ticket[4], token->kad->ticket[5],
148 token->kad->ticket[6], token->kad->ticket[7]);
149
150 /* count the number of tokens attached */
David Howells146aa8b2015-10-21 14:04:48 +0100151 prep->payload.data[1] = (void *)((unsigned long)prep->payload.data[1] + 1);
David Howells33941282009-09-14 01:17:35 +0000152
153 /* attach the data */
David Howells146aa8b2015-10-21 14:04:48 +0100154 for (pptoken = (struct rxrpc_key_token **)&prep->payload.data[0];
David Howells994551532009-09-14 01:17:46 +0000155 *pptoken;
156 pptoken = &(*pptoken)->next)
157 continue;
158 *pptoken = token;
David Howells8a7a3eb2014-07-18 18:56:36 +0100159 if (token->kad->expiry < prep->expiry)
160 prep->expiry = token->kad->expiry;
David Howells33941282009-09-14 01:17:35 +0000161
162 _leave(" = 0");
163 return 0;
164}
165
David Howells994551532009-09-14 01:17:46 +0000166static void rxrpc_free_krb5_principal(struct krb5_principal *princ)
167{
168 int loop;
169
170 if (princ->name_parts) {
171 for (loop = princ->n_name_parts - 1; loop >= 0; loop--)
172 kfree(princ->name_parts[loop]);
173 kfree(princ->name_parts);
174 }
175 kfree(princ->realm);
176}
177
178static void rxrpc_free_krb5_tagged(struct krb5_tagged_data *td)
179{
180 kfree(td->data);
181}
182
183/*
184 * free up an RxK5 token
185 */
186static void rxrpc_rxk5_free(struct rxk5_key *rxk5)
187{
188 int loop;
189
190 rxrpc_free_krb5_principal(&rxk5->client);
191 rxrpc_free_krb5_principal(&rxk5->server);
192 rxrpc_free_krb5_tagged(&rxk5->session);
193
194 if (rxk5->addresses) {
195 for (loop = rxk5->n_addresses - 1; loop >= 0; loop--)
196 rxrpc_free_krb5_tagged(&rxk5->addresses[loop]);
197 kfree(rxk5->addresses);
198 }
199 if (rxk5->authdata) {
200 for (loop = rxk5->n_authdata - 1; loop >= 0; loop--)
201 rxrpc_free_krb5_tagged(&rxk5->authdata[loop]);
202 kfree(rxk5->authdata);
203 }
204
205 kfree(rxk5->ticket);
206 kfree(rxk5->ticket2);
207 kfree(rxk5);
208}
209
210/*
211 * extract a krb5 principal
212 */
213static int rxrpc_krb5_decode_principal(struct krb5_principal *princ,
214 const __be32 **_xdr,
Eric Dumazet95c96172012-04-15 05:58:06 +0000215 unsigned int *_toklen)
David Howells994551532009-09-14 01:17:46 +0000216{
217 const __be32 *xdr = *_xdr;
Eric Dumazet95c96172012-04-15 05:58:06 +0000218 unsigned int toklen = *_toklen, n_parts, loop, tmp;
David Howells994551532009-09-14 01:17:46 +0000219
220 /* there must be at least one name, and at least #names+1 length
221 * words */
222 if (toklen <= 12)
223 return -EINVAL;
224
225 _enter(",{%x,%x,%x},%u",
226 ntohl(xdr[0]), ntohl(xdr[1]), ntohl(xdr[2]), toklen);
227
228 n_parts = ntohl(*xdr++);
229 toklen -= 4;
230 if (n_parts <= 0 || n_parts > AFSTOKEN_K5_COMPONENTS_MAX)
231 return -EINVAL;
232 princ->n_name_parts = n_parts;
233
234 if (toklen <= (n_parts + 1) * 4)
235 return -EINVAL;
236
Axel Linf65bd5e2012-02-13 20:19:14 +0000237 princ->name_parts = kcalloc(n_parts, sizeof(char *), GFP_KERNEL);
David Howells994551532009-09-14 01:17:46 +0000238 if (!princ->name_parts)
239 return -ENOMEM;
240
241 for (loop = 0; loop < n_parts; loop++) {
242 if (toklen < 4)
243 return -EINVAL;
244 tmp = ntohl(*xdr++);
245 toklen -= 4;
246 if (tmp <= 0 || tmp > AFSTOKEN_STRING_MAX)
247 return -EINVAL;
248 if (tmp > toklen)
249 return -EINVAL;
250 princ->name_parts[loop] = kmalloc(tmp + 1, GFP_KERNEL);
251 if (!princ->name_parts[loop])
252 return -ENOMEM;
253 memcpy(princ->name_parts[loop], xdr, tmp);
254 princ->name_parts[loop][tmp] = 0;
255 tmp = (tmp + 3) & ~3;
256 toklen -= tmp;
257 xdr += tmp >> 2;
258 }
259
260 if (toklen < 4)
261 return -EINVAL;
262 tmp = ntohl(*xdr++);
263 toklen -= 4;
264 if (tmp <= 0 || tmp > AFSTOKEN_K5_REALM_MAX)
265 return -EINVAL;
266 if (tmp > toklen)
267 return -EINVAL;
268 princ->realm = kmalloc(tmp + 1, GFP_KERNEL);
269 if (!princ->realm)
270 return -ENOMEM;
271 memcpy(princ->realm, xdr, tmp);
272 princ->realm[tmp] = 0;
273 tmp = (tmp + 3) & ~3;
274 toklen -= tmp;
275 xdr += tmp >> 2;
276
277 _debug("%s/...@%s", princ->name_parts[0], princ->realm);
278
279 *_xdr = xdr;
280 *_toklen = toklen;
281 _leave(" = 0 [toklen=%u]", toklen);
282 return 0;
283}
284
285/*
286 * extract a piece of krb5 tagged data
287 */
288static int rxrpc_krb5_decode_tagged_data(struct krb5_tagged_data *td,
289 size_t max_data_size,
290 const __be32 **_xdr,
Eric Dumazet95c96172012-04-15 05:58:06 +0000291 unsigned int *_toklen)
David Howells994551532009-09-14 01:17:46 +0000292{
293 const __be32 *xdr = *_xdr;
Eric Dumazet95c96172012-04-15 05:58:06 +0000294 unsigned int toklen = *_toklen, len;
David Howells994551532009-09-14 01:17:46 +0000295
296 /* there must be at least one tag and one length word */
297 if (toklen <= 8)
298 return -EINVAL;
299
300 _enter(",%zu,{%x,%x},%u",
301 max_data_size, ntohl(xdr[0]), ntohl(xdr[1]), toklen);
302
303 td->tag = ntohl(*xdr++);
304 len = ntohl(*xdr++);
305 toklen -= 8;
306 if (len > max_data_size)
307 return -EINVAL;
308 td->data_len = len;
309
310 if (len > 0) {
Thomas Meyer65d9d2c2011-11-17 12:43:40 +0000311 td->data = kmemdup(xdr, len, GFP_KERNEL);
David Howells994551532009-09-14 01:17:46 +0000312 if (!td->data)
313 return -ENOMEM;
David Howells994551532009-09-14 01:17:46 +0000314 len = (len + 3) & ~3;
315 toklen -= len;
316 xdr += len >> 2;
317 }
318
319 _debug("tag %x len %x", td->tag, td->data_len);
320
321 *_xdr = xdr;
322 *_toklen = toklen;
323 _leave(" = 0 [toklen=%u]", toklen);
324 return 0;
325}
326
327/*
328 * extract an array of tagged data
329 */
330static int rxrpc_krb5_decode_tagged_array(struct krb5_tagged_data **_td,
331 u8 *_n_elem,
332 u8 max_n_elem,
333 size_t max_elem_size,
334 const __be32 **_xdr,
Eric Dumazet95c96172012-04-15 05:58:06 +0000335 unsigned int *_toklen)
David Howells994551532009-09-14 01:17:46 +0000336{
337 struct krb5_tagged_data *td;
338 const __be32 *xdr = *_xdr;
Eric Dumazet95c96172012-04-15 05:58:06 +0000339 unsigned int toklen = *_toklen, n_elem, loop;
David Howells994551532009-09-14 01:17:46 +0000340 int ret;
341
342 /* there must be at least one count */
343 if (toklen < 4)
344 return -EINVAL;
345
346 _enter(",,%u,%zu,{%x},%u",
347 max_n_elem, max_elem_size, ntohl(xdr[0]), toklen);
348
349 n_elem = ntohl(*xdr++);
350 toklen -= 4;
Andrey Utkinfa4eff42014-07-18 18:31:57 +0300351 if (n_elem > max_n_elem)
David Howells994551532009-09-14 01:17:46 +0000352 return -EINVAL;
353 *_n_elem = n_elem;
354 if (n_elem > 0) {
355 if (toklen <= (n_elem + 1) * 4)
356 return -EINVAL;
357
358 _debug("n_elem %d", n_elem);
359
Axel Linf65bd5e2012-02-13 20:19:14 +0000360 td = kcalloc(n_elem, sizeof(struct krb5_tagged_data),
David Howells994551532009-09-14 01:17:46 +0000361 GFP_KERNEL);
362 if (!td)
363 return -ENOMEM;
364 *_td = td;
365
366 for (loop = 0; loop < n_elem; loop++) {
367 ret = rxrpc_krb5_decode_tagged_data(&td[loop],
368 max_elem_size,
369 &xdr, &toklen);
370 if (ret < 0)
371 return ret;
372 }
373 }
374
375 *_xdr = xdr;
376 *_toklen = toklen;
377 _leave(" = 0 [toklen=%u]", toklen);
378 return 0;
379}
380
381/*
382 * extract a krb5 ticket
383 */
David Howells4e36a952009-09-16 00:01:13 -0700384static int rxrpc_krb5_decode_ticket(u8 **_ticket, u16 *_tktlen,
Eric Dumazet95c96172012-04-15 05:58:06 +0000385 const __be32 **_xdr, unsigned int *_toklen)
David Howells994551532009-09-14 01:17:46 +0000386{
387 const __be32 *xdr = *_xdr;
Eric Dumazet95c96172012-04-15 05:58:06 +0000388 unsigned int toklen = *_toklen, len;
David Howells994551532009-09-14 01:17:46 +0000389
390 /* there must be at least one length word */
391 if (toklen <= 4)
392 return -EINVAL;
393
394 _enter(",{%x},%u", ntohl(xdr[0]), toklen);
395
396 len = ntohl(*xdr++);
397 toklen -= 4;
398 if (len > AFSTOKEN_K5_TIX_MAX)
399 return -EINVAL;
400 *_tktlen = len;
401
402 _debug("ticket len %u", len);
403
404 if (len > 0) {
Thomas Meyer65d9d2c2011-11-17 12:43:40 +0000405 *_ticket = kmemdup(xdr, len, GFP_KERNEL);
David Howells994551532009-09-14 01:17:46 +0000406 if (!*_ticket)
407 return -ENOMEM;
David Howells994551532009-09-14 01:17:46 +0000408 len = (len + 3) & ~3;
409 toklen -= len;
410 xdr += len >> 2;
411 }
412
413 *_xdr = xdr;
414 *_toklen = toklen;
415 _leave(" = 0 [toklen=%u]", toklen);
416 return 0;
417}
418
419/*
420 * parse an RxK5 type XDR format token
421 * - the caller guarantees we have at least 4 words
422 */
David Howells8a7a3eb2014-07-18 18:56:36 +0100423static int rxrpc_preparse_xdr_rxk5(struct key_preparsed_payload *prep,
424 size_t datalen,
425 const __be32 *xdr, unsigned int toklen)
David Howells994551532009-09-14 01:17:46 +0000426{
427 struct rxrpc_key_token *token, **pptoken;
428 struct rxk5_key *rxk5;
429 const __be32 *end_xdr = xdr + (toklen >> 2);
430 int ret;
431
432 _enter(",{%x,%x,%x,%x},%u",
433 ntohl(xdr[0]), ntohl(xdr[1]), ntohl(xdr[2]), ntohl(xdr[3]),
434 toklen);
435
436 /* reserve some payload space for this subkey - the length of the token
437 * is a reasonable approximation */
David Howells8a7a3eb2014-07-18 18:56:36 +0100438 prep->quotalen = datalen + toklen;
David Howells994551532009-09-14 01:17:46 +0000439
440 token = kzalloc(sizeof(*token), GFP_KERNEL);
441 if (!token)
442 return -ENOMEM;
443
444 rxk5 = kzalloc(sizeof(*rxk5), GFP_KERNEL);
445 if (!rxk5) {
446 kfree(token);
447 return -ENOMEM;
448 }
449
450 token->security_index = RXRPC_SECURITY_RXK5;
451 token->k5 = rxk5;
452
453 /* extract the principals */
454 ret = rxrpc_krb5_decode_principal(&rxk5->client, &xdr, &toklen);
455 if (ret < 0)
456 goto error;
457 ret = rxrpc_krb5_decode_principal(&rxk5->server, &xdr, &toklen);
458 if (ret < 0)
459 goto error;
460
461 /* extract the session key and the encoding type (the tag field ->
462 * ENCTYPE_xxx) */
463 ret = rxrpc_krb5_decode_tagged_data(&rxk5->session, AFSTOKEN_DATA_MAX,
464 &xdr, &toklen);
465 if (ret < 0)
466 goto error;
467
468 if (toklen < 4 * 8 + 2 * 4)
469 goto inval;
470 rxk5->authtime = be64_to_cpup((const __be64 *) xdr);
471 xdr += 2;
472 rxk5->starttime = be64_to_cpup((const __be64 *) xdr);
473 xdr += 2;
474 rxk5->endtime = be64_to_cpup((const __be64 *) xdr);
475 xdr += 2;
476 rxk5->renew_till = be64_to_cpup((const __be64 *) xdr);
477 xdr += 2;
478 rxk5->is_skey = ntohl(*xdr++);
479 rxk5->flags = ntohl(*xdr++);
480 toklen -= 4 * 8 + 2 * 4;
481
482 _debug("times: a=%llx s=%llx e=%llx rt=%llx",
483 rxk5->authtime, rxk5->starttime, rxk5->endtime,
484 rxk5->renew_till);
485 _debug("is_skey=%x flags=%x", rxk5->is_skey, rxk5->flags);
486
487 /* extract the permitted client addresses */
488 ret = rxrpc_krb5_decode_tagged_array(&rxk5->addresses,
489 &rxk5->n_addresses,
490 AFSTOKEN_K5_ADDRESSES_MAX,
491 AFSTOKEN_DATA_MAX,
492 &xdr, &toklen);
493 if (ret < 0)
494 goto error;
495
496 ASSERTCMP((end_xdr - xdr) << 2, ==, toklen);
497
498 /* extract the tickets */
499 ret = rxrpc_krb5_decode_ticket(&rxk5->ticket, &rxk5->ticket_len,
500 &xdr, &toklen);
501 if (ret < 0)
502 goto error;
503 ret = rxrpc_krb5_decode_ticket(&rxk5->ticket2, &rxk5->ticket2_len,
504 &xdr, &toklen);
505 if (ret < 0)
506 goto error;
507
508 ASSERTCMP((end_xdr - xdr) << 2, ==, toklen);
509
510 /* extract the typed auth data */
511 ret = rxrpc_krb5_decode_tagged_array(&rxk5->authdata,
512 &rxk5->n_authdata,
513 AFSTOKEN_K5_AUTHDATA_MAX,
514 AFSTOKEN_BDATALN_MAX,
515 &xdr, &toklen);
516 if (ret < 0)
517 goto error;
518
519 ASSERTCMP((end_xdr - xdr) << 2, ==, toklen);
520
521 if (toklen != 0)
522 goto inval;
523
David Howells8a7a3eb2014-07-18 18:56:36 +0100524 /* attach the payload */
David Howells146aa8b2015-10-21 14:04:48 +0100525 for (pptoken = (struct rxrpc_key_token **)&prep->payload.data[0];
David Howells994551532009-09-14 01:17:46 +0000526 *pptoken;
527 pptoken = &(*pptoken)->next)
528 continue;
529 *pptoken = token;
David Howells8a7a3eb2014-07-18 18:56:36 +0100530 if (token->kad->expiry < prep->expiry)
531 prep->expiry = token->kad->expiry;
David Howells994551532009-09-14 01:17:46 +0000532
533 _leave(" = 0");
534 return 0;
535
536inval:
537 ret = -EINVAL;
538error:
539 rxrpc_rxk5_free(rxk5);
540 kfree(token);
541 _leave(" = %d", ret);
542 return ret;
543}
544
David Howells33941282009-09-14 01:17:35 +0000545/*
546 * attempt to parse the data as the XDR format
547 * - the caller guarantees we have more than 7 words
548 */
David Howells8a7a3eb2014-07-18 18:56:36 +0100549static int rxrpc_preparse_xdr(struct key_preparsed_payload *prep)
David Howells33941282009-09-14 01:17:35 +0000550{
David Howells8a7a3eb2014-07-18 18:56:36 +0100551 const __be32 *xdr = prep->data, *token;
David Howells33941282009-09-14 01:17:35 +0000552 const char *cp;
Eric Dumazet95c96172012-04-15 05:58:06 +0000553 unsigned int len, tmp, loop, ntoken, toklen, sec_ix;
David Howells8a7a3eb2014-07-18 18:56:36 +0100554 size_t datalen = prep->datalen;
David Howells33941282009-09-14 01:17:35 +0000555 int ret;
556
557 _enter(",{%x,%x,%x,%x},%zu",
558 ntohl(xdr[0]), ntohl(xdr[1]), ntohl(xdr[2]), ntohl(xdr[3]),
David Howells8a7a3eb2014-07-18 18:56:36 +0100559 prep->datalen);
David Howells33941282009-09-14 01:17:35 +0000560
561 if (datalen > AFSTOKEN_LENGTH_MAX)
562 goto not_xdr;
563
564 /* XDR is an array of __be32's */
565 if (datalen & 3)
566 goto not_xdr;
567
568 /* the flags should be 0 (the setpag bit must be handled by
569 * userspace) */
570 if (ntohl(*xdr++) != 0)
571 goto not_xdr;
572 datalen -= 4;
573
574 /* check the cell name */
575 len = ntohl(*xdr++);
576 if (len < 1 || len > AFSTOKEN_CELL_MAX)
577 goto not_xdr;
578 datalen -= 4;
579 tmp = (len + 3) & ~3;
580 if (tmp > datalen)
581 goto not_xdr;
582
583 cp = (const char *) xdr;
584 for (loop = 0; loop < len; loop++)
585 if (!isprint(cp[loop]))
586 goto not_xdr;
587 if (len < tmp)
588 for (; loop < tmp; loop++)
589 if (cp[loop])
590 goto not_xdr;
591 _debug("cellname: [%u/%u] '%*.*s'",
592 len, tmp, len, len, (const char *) xdr);
593 datalen -= tmp;
594 xdr += tmp >> 2;
595
596 /* get the token count */
597 if (datalen < 12)
598 goto not_xdr;
599 ntoken = ntohl(*xdr++);
600 datalen -= 4;
601 _debug("ntoken: %x", ntoken);
602 if (ntoken < 1 || ntoken > AFSTOKEN_MAX)
603 goto not_xdr;
604
605 /* check each token wrapper */
606 token = xdr;
607 loop = ntoken;
608 do {
609 if (datalen < 8)
610 goto not_xdr;
611 toklen = ntohl(*xdr++);
612 sec_ix = ntohl(*xdr);
613 datalen -= 4;
614 _debug("token: [%x/%zx] %x", toklen, datalen, sec_ix);
615 if (toklen < 20 || toklen > datalen)
616 goto not_xdr;
617 datalen -= (toklen + 3) & ~3;
618 xdr += (toklen + 3) >> 2;
619
620 } while (--loop > 0);
621
622 _debug("remainder: %zu", datalen);
623 if (datalen != 0)
624 goto not_xdr;
625
626 /* okay: we're going to assume it's valid XDR format
627 * - we ignore the cellname, relying on the key to be correctly named
628 */
629 do {
630 xdr = token;
631 toklen = ntohl(*xdr++);
632 token = xdr + ((toklen + 3) >> 2);
633 sec_ix = ntohl(*xdr++);
634 toklen -= 4;
635
David Howells994551532009-09-14 01:17:46 +0000636 _debug("TOKEN type=%u [%p-%p]", sec_ix, xdr, token);
637
David Howells33941282009-09-14 01:17:35 +0000638 switch (sec_ix) {
639 case RXRPC_SECURITY_RXKAD:
David Howells8a7a3eb2014-07-18 18:56:36 +0100640 ret = rxrpc_preparse_xdr_rxkad(prep, datalen, xdr, toklen);
David Howells33941282009-09-14 01:17:35 +0000641 if (ret != 0)
642 goto error;
643 break;
644
David Howells994551532009-09-14 01:17:46 +0000645 case RXRPC_SECURITY_RXK5:
David Howells8a7a3eb2014-07-18 18:56:36 +0100646 ret = rxrpc_preparse_xdr_rxk5(prep, datalen, xdr, toklen);
David Howells994551532009-09-14 01:17:46 +0000647 if (ret != 0)
648 goto error;
649 break;
650
David Howells33941282009-09-14 01:17:35 +0000651 default:
652 ret = -EPROTONOSUPPORT;
653 goto error;
654 }
655
656 } while (--ntoken > 0);
657
658 _leave(" = 0");
659 return 0;
660
661not_xdr:
662 _leave(" = -EPROTO");
663 return -EPROTO;
664error:
665 _leave(" = %d", ret);
666 return ret;
667}
668
669/*
David Howells8a7a3eb2014-07-18 18:56:36 +0100670 * Preparse an rxrpc defined key.
671 *
672 * Data should be of the form:
David Howells17926a72007-04-26 15:48:28 -0700673 * OFFSET LEN CONTENT
674 * 0 4 key interface version number
675 * 4 2 security index (type)
676 * 6 2 ticket length
677 * 8 4 key expiry time (time_t)
678 * 12 4 kvno
679 * 16 8 session key
680 * 24 [len] ticket
681 *
682 * if no data is provided, then a no-security key is made
683 */
David Howells8a7a3eb2014-07-18 18:56:36 +0100684static int rxrpc_preparse(struct key_preparsed_payload *prep)
David Howells17926a72007-04-26 15:48:28 -0700685{
David Howells33941282009-09-14 01:17:35 +0000686 const struct rxrpc_key_data_v1 *v1;
687 struct rxrpc_key_token *token, **pp;
David Howells17926a72007-04-26 15:48:28 -0700688 size_t plen;
689 u32 kver;
690 int ret;
691
David Howells8a7a3eb2014-07-18 18:56:36 +0100692 _enter("%zu", prep->datalen);
David Howells17926a72007-04-26 15:48:28 -0700693
694 /* handle a no-security key */
David Howellscf7f6012012-09-13 13:06:29 +0100695 if (!prep->data && prep->datalen == 0)
David Howells17926a72007-04-26 15:48:28 -0700696 return 0;
697
David Howells33941282009-09-14 01:17:35 +0000698 /* determine if the XDR payload format is being used */
David Howellscf7f6012012-09-13 13:06:29 +0100699 if (prep->datalen > 7 * 4) {
David Howells8a7a3eb2014-07-18 18:56:36 +0100700 ret = rxrpc_preparse_xdr(prep);
David Howells33941282009-09-14 01:17:35 +0000701 if (ret != -EPROTO)
702 return ret;
703 }
704
David Howells17926a72007-04-26 15:48:28 -0700705 /* get the key interface version number */
706 ret = -EINVAL;
David Howellscf7f6012012-09-13 13:06:29 +0100707 if (prep->datalen <= 4 || !prep->data)
David Howells17926a72007-04-26 15:48:28 -0700708 goto error;
David Howellscf7f6012012-09-13 13:06:29 +0100709 memcpy(&kver, prep->data, sizeof(kver));
710 prep->data += sizeof(kver);
711 prep->datalen -= sizeof(kver);
David Howells17926a72007-04-26 15:48:28 -0700712
713 _debug("KEY I/F VERSION: %u", kver);
714
715 ret = -EKEYREJECTED;
716 if (kver != 1)
717 goto error;
718
719 /* deal with a version 1 key */
720 ret = -EINVAL;
David Howellscf7f6012012-09-13 13:06:29 +0100721 if (prep->datalen < sizeof(*v1))
David Howells17926a72007-04-26 15:48:28 -0700722 goto error;
723
David Howellscf7f6012012-09-13 13:06:29 +0100724 v1 = prep->data;
725 if (prep->datalen != sizeof(*v1) + v1->ticket_length)
David Howells17926a72007-04-26 15:48:28 -0700726 goto error;
727
David Howells33941282009-09-14 01:17:35 +0000728 _debug("SCIX: %u", v1->security_index);
729 _debug("TLEN: %u", v1->ticket_length);
730 _debug("EXPY: %x", v1->expiry);
731 _debug("KVNO: %u", v1->kvno);
David Howells17926a72007-04-26 15:48:28 -0700732 _debug("SKEY: %02x%02x%02x%02x%02x%02x%02x%02x",
David Howells33941282009-09-14 01:17:35 +0000733 v1->session_key[0], v1->session_key[1],
734 v1->session_key[2], v1->session_key[3],
735 v1->session_key[4], v1->session_key[5],
736 v1->session_key[6], v1->session_key[7]);
737 if (v1->ticket_length >= 8)
David Howells17926a72007-04-26 15:48:28 -0700738 _debug("TCKT: %02x%02x%02x%02x%02x%02x%02x%02x",
David Howells33941282009-09-14 01:17:35 +0000739 v1->ticket[0], v1->ticket[1],
740 v1->ticket[2], v1->ticket[3],
741 v1->ticket[4], v1->ticket[5],
742 v1->ticket[6], v1->ticket[7]);
David Howells17926a72007-04-26 15:48:28 -0700743
744 ret = -EPROTONOSUPPORT;
David Howells33941282009-09-14 01:17:35 +0000745 if (v1->security_index != RXRPC_SECURITY_RXKAD)
David Howells17926a72007-04-26 15:48:28 -0700746 goto error;
747
David Howells33941282009-09-14 01:17:35 +0000748 plen = sizeof(*token->kad) + v1->ticket_length;
David Howells8a7a3eb2014-07-18 18:56:36 +0100749 prep->quotalen = plen + sizeof(*token);
David Howells17926a72007-04-26 15:48:28 -0700750
751 ret = -ENOMEM;
Anton Blanchard0a93ea22011-02-25 15:33:17 +0000752 token = kzalloc(sizeof(*token), GFP_KERNEL);
David Howells33941282009-09-14 01:17:35 +0000753 if (!token)
David Howells17926a72007-04-26 15:48:28 -0700754 goto error;
Anton Blanchard0a93ea22011-02-25 15:33:17 +0000755 token->kad = kzalloc(plen, GFP_KERNEL);
David Howells33941282009-09-14 01:17:35 +0000756 if (!token->kad)
757 goto error_free;
758
759 token->security_index = RXRPC_SECURITY_RXKAD;
760 token->kad->ticket_len = v1->ticket_length;
761 token->kad->expiry = v1->expiry;
762 token->kad->kvno = v1->kvno;
763 memcpy(&token->kad->session_key, &v1->session_key, 8);
764 memcpy(&token->kad->ticket, v1->ticket, v1->ticket_length);
David Howells17926a72007-04-26 15:48:28 -0700765
David Howells8a7a3eb2014-07-18 18:56:36 +0100766 /* count the number of tokens attached */
David Howells146aa8b2015-10-21 14:04:48 +0100767 prep->payload.data[1] = (void *)((unsigned long)prep->payload.data[1] + 1);
David Howells33941282009-09-14 01:17:35 +0000768
David Howells8a7a3eb2014-07-18 18:56:36 +0100769 /* attach the data */
David Howells146aa8b2015-10-21 14:04:48 +0100770 pp = (struct rxrpc_key_token **)&prep->payload.data[0];
David Howells33941282009-09-14 01:17:35 +0000771 while (*pp)
772 pp = &(*pp)->next;
773 *pp = token;
David Howells8a7a3eb2014-07-18 18:56:36 +0100774 if (token->kad->expiry < prep->expiry)
775 prep->expiry = token->kad->expiry;
David Howells33941282009-09-14 01:17:35 +0000776 token = NULL;
David Howells17926a72007-04-26 15:48:28 -0700777 ret = 0;
778
David Howells33941282009-09-14 01:17:35 +0000779error_free:
780 kfree(token);
David Howells17926a72007-04-26 15:48:28 -0700781error:
782 return ret;
783}
784
785/*
David Howells8a7a3eb2014-07-18 18:56:36 +0100786 * Free token list.
David Howells17926a72007-04-26 15:48:28 -0700787 */
David Howells8a7a3eb2014-07-18 18:56:36 +0100788static void rxrpc_free_token_list(struct rxrpc_key_token *token)
David Howells17926a72007-04-26 15:48:28 -0700789{
David Howells8a7a3eb2014-07-18 18:56:36 +0100790 struct rxrpc_key_token *next;
David Howells17926a72007-04-26 15:48:28 -0700791
David Howells8a7a3eb2014-07-18 18:56:36 +0100792 for (; token; token = next) {
793 next = token->next;
David Howells33941282009-09-14 01:17:35 +0000794 switch (token->security_index) {
795 case RXRPC_SECURITY_RXKAD:
796 kfree(token->kad);
797 break;
David Howells994551532009-09-14 01:17:46 +0000798 case RXRPC_SECURITY_RXK5:
799 if (token->k5)
800 rxrpc_rxk5_free(token->k5);
801 break;
David Howells33941282009-09-14 01:17:35 +0000802 default:
803 printk(KERN_ERR "Unknown token type %x on rxrpc key\n",
804 token->security_index);
805 BUG();
806 }
807
808 kfree(token);
809 }
David Howells17926a72007-04-26 15:48:28 -0700810}
811
812/*
David Howells8a7a3eb2014-07-18 18:56:36 +0100813 * Clean up preparse data.
814 */
815static void rxrpc_free_preparse(struct key_preparsed_payload *prep)
816{
David Howells146aa8b2015-10-21 14:04:48 +0100817 rxrpc_free_token_list(prep->payload.data[0]);
David Howells8a7a3eb2014-07-18 18:56:36 +0100818}
819
820/*
821 * Preparse a server secret key.
822 *
823 * The data should be the 8-byte secret key.
824 */
825static int rxrpc_preparse_s(struct key_preparsed_payload *prep)
826{
Herbert Xu1afe5932016-01-24 21:19:01 +0800827 struct crypto_skcipher *ci;
David Howells8a7a3eb2014-07-18 18:56:36 +0100828
829 _enter("%zu", prep->datalen);
830
831 if (prep->datalen != 8)
832 return -EINVAL;
833
David Howells146aa8b2015-10-21 14:04:48 +0100834 memcpy(&prep->payload.data[2], prep->data, 8);
David Howells8a7a3eb2014-07-18 18:56:36 +0100835
Herbert Xu1afe5932016-01-24 21:19:01 +0800836 ci = crypto_alloc_skcipher("pcbc(des)", 0, CRYPTO_ALG_ASYNC);
David Howells8a7a3eb2014-07-18 18:56:36 +0100837 if (IS_ERR(ci)) {
838 _leave(" = %ld", PTR_ERR(ci));
839 return PTR_ERR(ci);
840 }
841
Herbert Xu1afe5932016-01-24 21:19:01 +0800842 if (crypto_skcipher_setkey(ci, prep->data, 8) < 0)
David Howells8a7a3eb2014-07-18 18:56:36 +0100843 BUG();
844
David Howells146aa8b2015-10-21 14:04:48 +0100845 prep->payload.data[0] = ci;
David Howells8a7a3eb2014-07-18 18:56:36 +0100846 _leave(" = 0");
847 return 0;
848}
849
850/*
851 * Clean up preparse data.
852 */
853static void rxrpc_free_preparse_s(struct key_preparsed_payload *prep)
854{
David Howells146aa8b2015-10-21 14:04:48 +0100855 if (prep->payload.data[0])
Herbert Xu1afe5932016-01-24 21:19:01 +0800856 crypto_free_skcipher(prep->payload.data[0]);
David Howells8a7a3eb2014-07-18 18:56:36 +0100857}
858
859/*
860 * dispose of the data dangling from the corpse of a rxrpc key
861 */
862static void rxrpc_destroy(struct key *key)
863{
David Howells146aa8b2015-10-21 14:04:48 +0100864 rxrpc_free_token_list(key->payload.data[0]);
David Howells8a7a3eb2014-07-18 18:56:36 +0100865}
866
867/*
David Howells17926a72007-04-26 15:48:28 -0700868 * dispose of the data dangling from the corpse of a rxrpc key
869 */
870static void rxrpc_destroy_s(struct key *key)
871{
David Howells146aa8b2015-10-21 14:04:48 +0100872 if (key->payload.data[0]) {
Herbert Xu1afe5932016-01-24 21:19:01 +0800873 crypto_free_skcipher(key->payload.data[0]);
David Howells146aa8b2015-10-21 14:04:48 +0100874 key->payload.data[0] = NULL;
David Howells17926a72007-04-26 15:48:28 -0700875 }
876}
877
878/*
879 * describe the rxrpc key
880 */
881static void rxrpc_describe(const struct key *key, struct seq_file *m)
882{
883 seq_puts(m, key->description);
884}
885
886/*
887 * grab the security key for a socket
888 */
889int rxrpc_request_key(struct rxrpc_sock *rx, char __user *optval, int optlen)
890{
891 struct key *key;
892 char *description;
893
894 _enter("");
895
896 if (optlen <= 0 || optlen > PAGE_SIZE - 1)
897 return -EINVAL;
898
Al Viro16e5c1f2015-12-24 00:06:05 -0500899 description = memdup_user_nul(optval, optlen);
900 if (IS_ERR(description))
901 return PTR_ERR(description);
David Howells17926a72007-04-26 15:48:28 -0700902
903 key = request_key(&key_type_rxrpc, description, NULL);
904 if (IS_ERR(key)) {
905 kfree(description);
906 _leave(" = %ld", PTR_ERR(key));
907 return PTR_ERR(key);
908 }
909
910 rx->key = key;
911 kfree(description);
912 _leave(" = 0 [key %x]", key->serial);
913 return 0;
914}
915
916/*
917 * grab the security keyring for a server socket
918 */
919int rxrpc_server_keyring(struct rxrpc_sock *rx, char __user *optval,
920 int optlen)
921{
922 struct key *key;
923 char *description;
924
925 _enter("");
926
927 if (optlen <= 0 || optlen > PAGE_SIZE - 1)
928 return -EINVAL;
929
Al Viro16e5c1f2015-12-24 00:06:05 -0500930 description = memdup_user_nul(optval, optlen);
931 if (IS_ERR(description))
932 return PTR_ERR(description);
David Howells17926a72007-04-26 15:48:28 -0700933
934 key = request_key(&key_type_keyring, description, NULL);
935 if (IS_ERR(key)) {
936 kfree(description);
937 _leave(" = %ld", PTR_ERR(key));
938 return PTR_ERR(key);
939 }
940
941 rx->securities = key;
942 kfree(description);
943 _leave(" = 0 [key %x]", key->serial);
944 return 0;
945}
946
947/*
948 * generate a server data key
949 */
950int rxrpc_get_server_data_key(struct rxrpc_connection *conn,
951 const void *session_key,
952 time_t expiry,
953 u32 kvno)
954{
David Howellsd84f4f92008-11-14 10:39:23 +1100955 const struct cred *cred = current_cred();
David Howells17926a72007-04-26 15:48:28 -0700956 struct key *key;
957 int ret;
958
959 struct {
960 u32 kver;
David Howells33941282009-09-14 01:17:35 +0000961 struct rxrpc_key_data_v1 v1;
David Howells17926a72007-04-26 15:48:28 -0700962 } data;
963
964 _enter("");
965
Eric W. Biedermanc6089732012-05-25 16:37:54 -0600966 key = key_alloc(&key_type_rxrpc, "x",
967 GLOBAL_ROOT_UID, GLOBAL_ROOT_GID, cred, 0,
David Howells5ac7eac2016-04-06 16:14:24 +0100968 KEY_ALLOC_NOT_IN_QUOTA, NULL);
David Howells17926a72007-04-26 15:48:28 -0700969 if (IS_ERR(key)) {
970 _leave(" = -ENOMEM [alloc %ld]", PTR_ERR(key));
971 return -ENOMEM;
972 }
973
974 _debug("key %d", key_serial(key));
975
976 data.kver = 1;
David Howells33941282009-09-14 01:17:35 +0000977 data.v1.security_index = RXRPC_SECURITY_RXKAD;
978 data.v1.ticket_length = 0;
979 data.v1.expiry = expiry;
980 data.v1.kvno = 0;
David Howells17926a72007-04-26 15:48:28 -0700981
David Howells33941282009-09-14 01:17:35 +0000982 memcpy(&data.v1.session_key, session_key, sizeof(data.v1.session_key));
David Howells17926a72007-04-26 15:48:28 -0700983
984 ret = key_instantiate_and_link(key, &data, sizeof(data), NULL, NULL);
985 if (ret < 0)
986 goto error;
987
988 conn->key = key;
989 _leave(" = 0 [%d]", key_serial(key));
990 return 0;
991
992error:
993 key_revoke(key);
994 key_put(key);
995 _leave(" = -ENOMEM [ins %d]", ret);
996 return -ENOMEM;
997}
David Howells17926a72007-04-26 15:48:28 -0700998EXPORT_SYMBOL(rxrpc_get_server_data_key);
David Howells76181c12007-10-16 23:29:46 -0700999
1000/**
1001 * rxrpc_get_null_key - Generate a null RxRPC key
1002 * @keyname: The name to give the key.
1003 *
1004 * Generate a null RxRPC key that can be used to indicate anonymous security is
1005 * required for a particular domain.
1006 */
1007struct key *rxrpc_get_null_key(const char *keyname)
1008{
David Howellsd84f4f92008-11-14 10:39:23 +11001009 const struct cred *cred = current_cred();
David Howells76181c12007-10-16 23:29:46 -07001010 struct key *key;
1011 int ret;
1012
Eric W. Biedermanc6089732012-05-25 16:37:54 -06001013 key = key_alloc(&key_type_rxrpc, keyname,
1014 GLOBAL_ROOT_UID, GLOBAL_ROOT_GID, cred,
David Howells5ac7eac2016-04-06 16:14:24 +01001015 KEY_POS_SEARCH, KEY_ALLOC_NOT_IN_QUOTA, NULL);
David Howells76181c12007-10-16 23:29:46 -07001016 if (IS_ERR(key))
1017 return key;
1018
1019 ret = key_instantiate_and_link(key, NULL, 0, NULL, NULL);
1020 if (ret < 0) {
1021 key_revoke(key);
1022 key_put(key);
1023 return ERR_PTR(ret);
1024 }
1025
1026 return key;
1027}
1028EXPORT_SYMBOL(rxrpc_get_null_key);
David Howellsed6dd182009-09-14 01:17:40 +00001029
1030/*
1031 * read the contents of an rxrpc key
1032 * - this returns the result in XDR form
1033 */
1034static long rxrpc_read(const struct key *key,
1035 char __user *buffer, size_t buflen)
1036{
David Howells994551532009-09-14 01:17:46 +00001037 const struct rxrpc_key_token *token;
1038 const struct krb5_principal *princ;
1039 size_t size;
1040 __be32 __user *xdr, *oldxdr;
1041 u32 cnlen, toksize, ntoks, tok, zero;
1042 u16 toksizes[AFSTOKEN_MAX];
1043 int loop;
David Howellsed6dd182009-09-14 01:17:40 +00001044
1045 _enter("");
1046
1047 /* we don't know what form we should return non-AFS keys in */
1048 if (memcmp(key->description, "afs@", 4) != 0)
1049 return -EOPNOTSUPP;
1050 cnlen = strlen(key->description + 4);
1051
David Howells994551532009-09-14 01:17:46 +00001052#define RND(X) (((X) + 3) & ~3)
1053
David Howellsed6dd182009-09-14 01:17:40 +00001054 /* AFS keys we return in XDR form, so we need to work out the size of
1055 * the XDR */
1056 size = 2 * 4; /* flags, cellname len */
David Howells994551532009-09-14 01:17:46 +00001057 size += RND(cnlen); /* cellname */
David Howellsed6dd182009-09-14 01:17:40 +00001058 size += 1 * 4; /* token count */
1059
1060 ntoks = 0;
David Howells146aa8b2015-10-21 14:04:48 +01001061 for (token = key->payload.data[0]; token; token = token->next) {
David Howells994551532009-09-14 01:17:46 +00001062 toksize = 4; /* sec index */
1063
David Howellsed6dd182009-09-14 01:17:40 +00001064 switch (token->security_index) {
1065 case RXRPC_SECURITY_RXKAD:
David Howells994551532009-09-14 01:17:46 +00001066 toksize += 8 * 4; /* viceid, kvno, key*2, begin,
1067 * end, primary, tktlen */
1068 toksize += RND(token->kad->ticket_len);
David Howellsed6dd182009-09-14 01:17:40 +00001069 break;
1070
David Howells994551532009-09-14 01:17:46 +00001071 case RXRPC_SECURITY_RXK5:
1072 princ = &token->k5->client;
1073 toksize += 4 + princ->n_name_parts * 4;
1074 for (loop = 0; loop < princ->n_name_parts; loop++)
1075 toksize += RND(strlen(princ->name_parts[loop]));
1076 toksize += 4 + RND(strlen(princ->realm));
1077
1078 princ = &token->k5->server;
1079 toksize += 4 + princ->n_name_parts * 4;
1080 for (loop = 0; loop < princ->n_name_parts; loop++)
1081 toksize += RND(strlen(princ->name_parts[loop]));
1082 toksize += 4 + RND(strlen(princ->realm));
1083
1084 toksize += 8 + RND(token->k5->session.data_len);
1085
1086 toksize += 4 * 8 + 2 * 4;
1087
1088 toksize += 4 + token->k5->n_addresses * 8;
1089 for (loop = 0; loop < token->k5->n_addresses; loop++)
1090 toksize += RND(token->k5->addresses[loop].data_len);
1091
1092 toksize += 4 + RND(token->k5->ticket_len);
1093 toksize += 4 + RND(token->k5->ticket2_len);
1094
1095 toksize += 4 + token->k5->n_authdata * 8;
1096 for (loop = 0; loop < token->k5->n_authdata; loop++)
1097 toksize += RND(token->k5->authdata[loop].data_len);
David Howellsed6dd182009-09-14 01:17:40 +00001098 break;
David Howells994551532009-09-14 01:17:46 +00001099
1100 default: /* we have a ticket we can't encode */
1101 BUG();
1102 continue;
David Howellsed6dd182009-09-14 01:17:40 +00001103 }
David Howells994551532009-09-14 01:17:46 +00001104
1105 _debug("token[%u]: toksize=%u", ntoks, toksize);
1106 ASSERTCMP(toksize, <=, AFSTOKEN_LENGTH_MAX);
1107
1108 toksizes[ntoks++] = toksize;
1109 size += toksize + 4; /* each token has a length word */
David Howellsed6dd182009-09-14 01:17:40 +00001110 }
1111
David Howells994551532009-09-14 01:17:46 +00001112#undef RND
1113
David Howellsed6dd182009-09-14 01:17:40 +00001114 if (!buffer || buflen < size)
1115 return size;
1116
1117 xdr = (__be32 __user *) buffer;
1118 zero = 0;
1119#define ENCODE(x) \
1120 do { \
1121 __be32 y = htonl(x); \
1122 if (put_user(y, xdr++) < 0) \
1123 goto fault; \
1124 } while(0)
David Howells994551532009-09-14 01:17:46 +00001125#define ENCODE_DATA(l, s) \
1126 do { \
1127 u32 _l = (l); \
1128 ENCODE(l); \
1129 if (copy_to_user(xdr, (s), _l) != 0) \
1130 goto fault; \
1131 if (_l & 3 && \
David Howellsed3bfdf2014-09-09 15:19:44 +01001132 copy_to_user((u8 __user *)xdr + _l, &zero, 4 - (_l & 3)) != 0) \
David Howells994551532009-09-14 01:17:46 +00001133 goto fault; \
1134 xdr += (_l + 3) >> 2; \
1135 } while(0)
1136#define ENCODE64(x) \
1137 do { \
1138 __be64 y = cpu_to_be64(x); \
1139 if (copy_to_user(xdr, &y, 8) != 0) \
1140 goto fault; \
1141 xdr += 8 >> 2; \
1142 } while(0)
1143#define ENCODE_STR(s) \
1144 do { \
1145 const char *_s = (s); \
1146 ENCODE_DATA(strlen(_s), _s); \
1147 } while(0)
David Howellsed6dd182009-09-14 01:17:40 +00001148
David Howells994551532009-09-14 01:17:46 +00001149 ENCODE(0); /* flags */
1150 ENCODE_DATA(cnlen, key->description + 4); /* cellname */
1151 ENCODE(ntoks);
David Howellsed6dd182009-09-14 01:17:40 +00001152
David Howells994551532009-09-14 01:17:46 +00001153 tok = 0;
David Howells146aa8b2015-10-21 14:04:48 +01001154 for (token = key->payload.data[0]; token; token = token->next) {
David Howells994551532009-09-14 01:17:46 +00001155 toksize = toksizes[tok++];
1156 ENCODE(toksize);
1157 oldxdr = xdr;
1158 ENCODE(token->security_index);
David Howellsed6dd182009-09-14 01:17:40 +00001159
1160 switch (token->security_index) {
1161 case RXRPC_SECURITY_RXKAD:
David Howellsed6dd182009-09-14 01:17:40 +00001162 ENCODE(token->kad->vice_id);
1163 ENCODE(token->kad->kvno);
David Howells994551532009-09-14 01:17:46 +00001164 ENCODE_DATA(8, token->kad->session_key);
David Howellsed6dd182009-09-14 01:17:40 +00001165 ENCODE(token->kad->start);
1166 ENCODE(token->kad->expiry);
1167 ENCODE(token->kad->primary_flag);
David Howells994551532009-09-14 01:17:46 +00001168 ENCODE_DATA(token->kad->ticket_len, token->kad->ticket);
1169 break;
1170
1171 case RXRPC_SECURITY_RXK5:
1172 princ = &token->k5->client;
1173 ENCODE(princ->n_name_parts);
1174 for (loop = 0; loop < princ->n_name_parts; loop++)
1175 ENCODE_STR(princ->name_parts[loop]);
1176 ENCODE_STR(princ->realm);
1177
1178 princ = &token->k5->server;
1179 ENCODE(princ->n_name_parts);
1180 for (loop = 0; loop < princ->n_name_parts; loop++)
1181 ENCODE_STR(princ->name_parts[loop]);
1182 ENCODE_STR(princ->realm);
1183
1184 ENCODE(token->k5->session.tag);
1185 ENCODE_DATA(token->k5->session.data_len,
1186 token->k5->session.data);
1187
1188 ENCODE64(token->k5->authtime);
1189 ENCODE64(token->k5->starttime);
1190 ENCODE64(token->k5->endtime);
1191 ENCODE64(token->k5->renew_till);
1192 ENCODE(token->k5->is_skey);
1193 ENCODE(token->k5->flags);
1194
1195 ENCODE(token->k5->n_addresses);
1196 for (loop = 0; loop < token->k5->n_addresses; loop++) {
1197 ENCODE(token->k5->addresses[loop].tag);
1198 ENCODE_DATA(token->k5->addresses[loop].data_len,
1199 token->k5->addresses[loop].data);
1200 }
1201
1202 ENCODE_DATA(token->k5->ticket_len, token->k5->ticket);
1203 ENCODE_DATA(token->k5->ticket2_len, token->k5->ticket2);
1204
1205 ENCODE(token->k5->n_authdata);
1206 for (loop = 0; loop < token->k5->n_authdata; loop++) {
1207 ENCODE(token->k5->authdata[loop].tag);
1208 ENCODE_DATA(token->k5->authdata[loop].data_len,
1209 token->k5->authdata[loop].data);
1210 }
David Howellsed6dd182009-09-14 01:17:40 +00001211 break;
1212
1213 default:
David Howells994551532009-09-14 01:17:46 +00001214 BUG();
David Howellsed6dd182009-09-14 01:17:40 +00001215 break;
1216 }
David Howells994551532009-09-14 01:17:46 +00001217
1218 ASSERTCMP((unsigned long)xdr - (unsigned long)oldxdr, ==,
1219 toksize);
David Howellsed6dd182009-09-14 01:17:40 +00001220 }
1221
David Howells994551532009-09-14 01:17:46 +00001222#undef ENCODE_STR
1223#undef ENCODE_DATA
1224#undef ENCODE64
David Howellsed6dd182009-09-14 01:17:40 +00001225#undef ENCODE
1226
David Howells994551532009-09-14 01:17:46 +00001227 ASSERTCMP(tok, ==, ntoks);
David Howellsed6dd182009-09-14 01:17:40 +00001228 ASSERTCMP((char __user *) xdr - buffer, ==, size);
1229 _leave(" = %zu", size);
1230 return size;
1231
1232fault:
1233 _leave(" = -EFAULT");
1234 return -EFAULT;
1235}