Kyle Swenson | 8d8f654 | 2021-03-15 11:02:55 -0600 | [diff] [blame] | 1 | /* 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 | |
| 15 | #include <linux/module.h> |
| 16 | #include <linux/net.h> |
| 17 | #include <linux/skbuff.h> |
| 18 | #include <linux/key-type.h> |
| 19 | #include <linux/crypto.h> |
| 20 | #include <linux/ctype.h> |
| 21 | #include <linux/slab.h> |
| 22 | #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 | |
| 28 | static int rxrpc_vet_description_s(const char *); |
| 29 | static int rxrpc_preparse(struct key_preparsed_payload *); |
| 30 | static int rxrpc_preparse_s(struct key_preparsed_payload *); |
| 31 | static void rxrpc_free_preparse(struct key_preparsed_payload *); |
| 32 | static void rxrpc_free_preparse_s(struct key_preparsed_payload *); |
| 33 | static void rxrpc_destroy(struct key *); |
| 34 | static void rxrpc_destroy_s(struct key *); |
| 35 | static void rxrpc_describe(const struct key *, struct seq_file *); |
| 36 | static long rxrpc_read(const struct key *, char __user *, size_t); |
| 37 | |
| 38 | /* |
| 39 | * rxrpc defined keys take an arbitrary string as the description and an |
| 40 | * arbitrary blob of data as the payload |
| 41 | */ |
| 42 | struct key_type key_type_rxrpc = { |
| 43 | .name = "rxrpc", |
| 44 | .preparse = rxrpc_preparse, |
| 45 | .free_preparse = rxrpc_free_preparse, |
| 46 | .instantiate = generic_key_instantiate, |
| 47 | .destroy = rxrpc_destroy, |
| 48 | .describe = rxrpc_describe, |
| 49 | .read = rxrpc_read, |
| 50 | }; |
| 51 | EXPORT_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 | */ |
| 57 | struct key_type key_type_rxrpc_s = { |
| 58 | .name = "rxrpc_s", |
| 59 | .vet_description = rxrpc_vet_description_s, |
| 60 | .preparse = rxrpc_preparse_s, |
| 61 | .free_preparse = rxrpc_free_preparse_s, |
| 62 | .instantiate = generic_key_instantiate, |
| 63 | .destroy = rxrpc_destroy_s, |
| 64 | .describe = rxrpc_describe, |
| 65 | }; |
| 66 | |
| 67 | /* |
| 68 | * Vet the description for an RxRPC server key |
| 69 | */ |
| 70 | static 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 | /* |
| 85 | * parse an RxKAD type XDR format token |
| 86 | * - the caller guarantees we have at least 4 words |
| 87 | */ |
| 88 | static int rxrpc_preparse_xdr_rxkad(struct key_preparsed_payload *prep, |
| 89 | size_t datalen, |
| 90 | const __be32 *xdr, unsigned int toklen) |
| 91 | { |
| 92 | struct rxrpc_key_token *token, **pptoken; |
| 93 | size_t plen; |
| 94 | u32 tktlen; |
| 95 | |
| 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; |
| 106 | if (toklen < 8 * 4 + tktlen) |
| 107 | return -EKEYREJECTED; |
| 108 | |
| 109 | plen = sizeof(*token) + sizeof(*token->kad) + tktlen; |
| 110 | prep->quotalen = datalen + plen; |
| 111 | |
| 112 | plen -= sizeof(*token); |
| 113 | token = kzalloc(sizeof(*token), GFP_KERNEL); |
| 114 | if (!token) |
| 115 | return -ENOMEM; |
| 116 | |
| 117 | token->kad = kzalloc(plen, GFP_KERNEL); |
| 118 | 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 */ |
| 151 | prep->payload.data[1] = (void *)((unsigned long)prep->payload.data[1] + 1); |
| 152 | |
| 153 | /* attach the data */ |
| 154 | for (pptoken = (struct rxrpc_key_token **)&prep->payload.data[0]; |
| 155 | *pptoken; |
| 156 | pptoken = &(*pptoken)->next) |
| 157 | continue; |
| 158 | *pptoken = token; |
| 159 | if (token->kad->expiry < prep->expiry) |
| 160 | prep->expiry = token->kad->expiry; |
| 161 | |
| 162 | _leave(" = 0"); |
| 163 | return 0; |
| 164 | } |
| 165 | |
| 166 | static 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 | |
| 178 | static 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 | */ |
| 186 | static 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 | */ |
| 213 | static int rxrpc_krb5_decode_principal(struct krb5_principal *princ, |
| 214 | const __be32 **_xdr, |
| 215 | unsigned int *_toklen) |
| 216 | { |
| 217 | const __be32 *xdr = *_xdr; |
| 218 | unsigned int toklen = *_toklen, n_parts, loop, tmp, paddedlen; |
| 219 | |
| 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 | |
| 237 | princ->name_parts = kcalloc(n_parts, sizeof(char *), GFP_KERNEL); |
| 238 | 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 | paddedlen = (tmp + 3) & ~3; |
| 249 | if (paddedlen > toklen) |
| 250 | return -EINVAL; |
| 251 | princ->name_parts[loop] = kmalloc(tmp + 1, GFP_KERNEL); |
| 252 | if (!princ->name_parts[loop]) |
| 253 | return -ENOMEM; |
| 254 | memcpy(princ->name_parts[loop], xdr, tmp); |
| 255 | princ->name_parts[loop][tmp] = 0; |
| 256 | toklen -= paddedlen; |
| 257 | xdr += paddedlen >> 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 | paddedlen = (tmp + 3) & ~3; |
| 267 | if (paddedlen > toklen) |
| 268 | return -EINVAL; |
| 269 | princ->realm = kmalloc(tmp + 1, GFP_KERNEL); |
| 270 | if (!princ->realm) |
| 271 | return -ENOMEM; |
| 272 | memcpy(princ->realm, xdr, tmp); |
| 273 | princ->realm[tmp] = 0; |
| 274 | toklen -= paddedlen; |
| 275 | xdr += paddedlen >> 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 | */ |
| 288 | static int rxrpc_krb5_decode_tagged_data(struct krb5_tagged_data *td, |
| 289 | size_t max_data_size, |
| 290 | const __be32 **_xdr, |
| 291 | unsigned int *_toklen) |
| 292 | { |
| 293 | const __be32 *xdr = *_xdr; |
| 294 | unsigned int toklen = *_toklen, len, paddedlen; |
| 295 | |
| 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 | paddedlen = (len + 3) & ~3; |
| 309 | if (paddedlen > toklen) |
| 310 | return -EINVAL; |
| 311 | td->data_len = len; |
| 312 | |
| 313 | if (len > 0) { |
| 314 | td->data = kmemdup(xdr, len, GFP_KERNEL); |
| 315 | if (!td->data) |
| 316 | return -ENOMEM; |
| 317 | toklen -= paddedlen; |
| 318 | xdr += paddedlen >> 2; |
| 319 | } |
| 320 | |
| 321 | _debug("tag %x len %x", td->tag, td->data_len); |
| 322 | |
| 323 | *_xdr = xdr; |
| 324 | *_toklen = toklen; |
| 325 | _leave(" = 0 [toklen=%u]", toklen); |
| 326 | return 0; |
| 327 | } |
| 328 | |
| 329 | /* |
| 330 | * extract an array of tagged data |
| 331 | */ |
| 332 | static int rxrpc_krb5_decode_tagged_array(struct krb5_tagged_data **_td, |
| 333 | u8 *_n_elem, |
| 334 | u8 max_n_elem, |
| 335 | size_t max_elem_size, |
| 336 | const __be32 **_xdr, |
| 337 | unsigned int *_toklen) |
| 338 | { |
| 339 | struct krb5_tagged_data *td; |
| 340 | const __be32 *xdr = *_xdr; |
| 341 | unsigned int toklen = *_toklen, n_elem, loop; |
| 342 | int ret; |
| 343 | |
| 344 | /* there must be at least one count */ |
| 345 | if (toklen < 4) |
| 346 | return -EINVAL; |
| 347 | |
| 348 | _enter(",,%u,%zu,{%x},%u", |
| 349 | max_n_elem, max_elem_size, ntohl(xdr[0]), toklen); |
| 350 | |
| 351 | n_elem = ntohl(*xdr++); |
| 352 | toklen -= 4; |
| 353 | if (n_elem > max_n_elem) |
| 354 | return -EINVAL; |
| 355 | *_n_elem = n_elem; |
| 356 | if (n_elem > 0) { |
| 357 | if (toklen <= (n_elem + 1) * 4) |
| 358 | return -EINVAL; |
| 359 | |
| 360 | _debug("n_elem %d", n_elem); |
| 361 | |
| 362 | td = kcalloc(n_elem, sizeof(struct krb5_tagged_data), |
| 363 | GFP_KERNEL); |
| 364 | if (!td) |
| 365 | return -ENOMEM; |
| 366 | *_td = td; |
| 367 | |
| 368 | for (loop = 0; loop < n_elem; loop++) { |
| 369 | ret = rxrpc_krb5_decode_tagged_data(&td[loop], |
| 370 | max_elem_size, |
| 371 | &xdr, &toklen); |
| 372 | if (ret < 0) |
| 373 | return ret; |
| 374 | } |
| 375 | } |
| 376 | |
| 377 | *_xdr = xdr; |
| 378 | *_toklen = toklen; |
| 379 | _leave(" = 0 [toklen=%u]", toklen); |
| 380 | return 0; |
| 381 | } |
| 382 | |
| 383 | /* |
| 384 | * extract a krb5 ticket |
| 385 | */ |
| 386 | static int rxrpc_krb5_decode_ticket(u8 **_ticket, u16 *_tktlen, |
| 387 | const __be32 **_xdr, unsigned int *_toklen) |
| 388 | { |
| 389 | const __be32 *xdr = *_xdr; |
| 390 | unsigned int toklen = *_toklen, len, paddedlen; |
| 391 | |
| 392 | /* there must be at least one length word */ |
| 393 | if (toklen <= 4) |
| 394 | return -EINVAL; |
| 395 | |
| 396 | _enter(",{%x},%u", ntohl(xdr[0]), toklen); |
| 397 | |
| 398 | len = ntohl(*xdr++); |
| 399 | toklen -= 4; |
| 400 | if (len > AFSTOKEN_K5_TIX_MAX) |
| 401 | return -EINVAL; |
| 402 | paddedlen = (len + 3) & ~3; |
| 403 | if (paddedlen > toklen) |
| 404 | return -EINVAL; |
| 405 | *_tktlen = len; |
| 406 | |
| 407 | _debug("ticket len %u", len); |
| 408 | |
| 409 | if (len > 0) { |
| 410 | *_ticket = kmemdup(xdr, len, GFP_KERNEL); |
| 411 | if (!*_ticket) |
| 412 | return -ENOMEM; |
| 413 | toklen -= paddedlen; |
| 414 | xdr += paddedlen >> 2; |
| 415 | } |
| 416 | |
| 417 | *_xdr = xdr; |
| 418 | *_toklen = toklen; |
| 419 | _leave(" = 0 [toklen=%u]", toklen); |
| 420 | return 0; |
| 421 | } |
| 422 | |
| 423 | /* |
| 424 | * parse an RxK5 type XDR format token |
| 425 | * - the caller guarantees we have at least 4 words |
| 426 | */ |
| 427 | static int rxrpc_preparse_xdr_rxk5(struct key_preparsed_payload *prep, |
| 428 | size_t datalen, |
| 429 | const __be32 *xdr, unsigned int toklen) |
| 430 | { |
| 431 | struct rxrpc_key_token *token, **pptoken; |
| 432 | struct rxk5_key *rxk5; |
| 433 | const __be32 *end_xdr = xdr + (toklen >> 2); |
| 434 | int ret; |
| 435 | |
| 436 | _enter(",{%x,%x,%x,%x},%u", |
| 437 | ntohl(xdr[0]), ntohl(xdr[1]), ntohl(xdr[2]), ntohl(xdr[3]), |
| 438 | toklen); |
| 439 | |
| 440 | /* reserve some payload space for this subkey - the length of the token |
| 441 | * is a reasonable approximation */ |
| 442 | prep->quotalen = datalen + toklen; |
| 443 | |
| 444 | token = kzalloc(sizeof(*token), GFP_KERNEL); |
| 445 | if (!token) |
| 446 | return -ENOMEM; |
| 447 | |
| 448 | rxk5 = kzalloc(sizeof(*rxk5), GFP_KERNEL); |
| 449 | if (!rxk5) { |
| 450 | kfree(token); |
| 451 | return -ENOMEM; |
| 452 | } |
| 453 | |
| 454 | token->security_index = RXRPC_SECURITY_RXK5; |
| 455 | token->k5 = rxk5; |
| 456 | |
| 457 | /* extract the principals */ |
| 458 | ret = rxrpc_krb5_decode_principal(&rxk5->client, &xdr, &toklen); |
| 459 | if (ret < 0) |
| 460 | goto error; |
| 461 | ret = rxrpc_krb5_decode_principal(&rxk5->server, &xdr, &toklen); |
| 462 | if (ret < 0) |
| 463 | goto error; |
| 464 | |
| 465 | /* extract the session key and the encoding type (the tag field -> |
| 466 | * ENCTYPE_xxx) */ |
| 467 | ret = rxrpc_krb5_decode_tagged_data(&rxk5->session, AFSTOKEN_DATA_MAX, |
| 468 | &xdr, &toklen); |
| 469 | if (ret < 0) |
| 470 | goto error; |
| 471 | |
| 472 | if (toklen < 4 * 8 + 2 * 4) |
| 473 | goto inval; |
| 474 | rxk5->authtime = be64_to_cpup((const __be64 *) xdr); |
| 475 | xdr += 2; |
| 476 | rxk5->starttime = be64_to_cpup((const __be64 *) xdr); |
| 477 | xdr += 2; |
| 478 | rxk5->endtime = be64_to_cpup((const __be64 *) xdr); |
| 479 | xdr += 2; |
| 480 | rxk5->renew_till = be64_to_cpup((const __be64 *) xdr); |
| 481 | xdr += 2; |
| 482 | rxk5->is_skey = ntohl(*xdr++); |
| 483 | rxk5->flags = ntohl(*xdr++); |
| 484 | toklen -= 4 * 8 + 2 * 4; |
| 485 | |
| 486 | _debug("times: a=%llx s=%llx e=%llx rt=%llx", |
| 487 | rxk5->authtime, rxk5->starttime, rxk5->endtime, |
| 488 | rxk5->renew_till); |
| 489 | _debug("is_skey=%x flags=%x", rxk5->is_skey, rxk5->flags); |
| 490 | |
| 491 | /* extract the permitted client addresses */ |
| 492 | ret = rxrpc_krb5_decode_tagged_array(&rxk5->addresses, |
| 493 | &rxk5->n_addresses, |
| 494 | AFSTOKEN_K5_ADDRESSES_MAX, |
| 495 | AFSTOKEN_DATA_MAX, |
| 496 | &xdr, &toklen); |
| 497 | if (ret < 0) |
| 498 | goto error; |
| 499 | |
| 500 | ASSERTCMP((end_xdr - xdr) << 2, ==, toklen); |
| 501 | |
| 502 | /* extract the tickets */ |
| 503 | ret = rxrpc_krb5_decode_ticket(&rxk5->ticket, &rxk5->ticket_len, |
| 504 | &xdr, &toklen); |
| 505 | if (ret < 0) |
| 506 | goto error; |
| 507 | ret = rxrpc_krb5_decode_ticket(&rxk5->ticket2, &rxk5->ticket2_len, |
| 508 | &xdr, &toklen); |
| 509 | if (ret < 0) |
| 510 | goto error; |
| 511 | |
| 512 | ASSERTCMP((end_xdr - xdr) << 2, ==, toklen); |
| 513 | |
| 514 | /* extract the typed auth data */ |
| 515 | ret = rxrpc_krb5_decode_tagged_array(&rxk5->authdata, |
| 516 | &rxk5->n_authdata, |
| 517 | AFSTOKEN_K5_AUTHDATA_MAX, |
| 518 | AFSTOKEN_BDATALN_MAX, |
| 519 | &xdr, &toklen); |
| 520 | if (ret < 0) |
| 521 | goto error; |
| 522 | |
| 523 | ASSERTCMP((end_xdr - xdr) << 2, ==, toklen); |
| 524 | |
| 525 | if (toklen != 0) |
| 526 | goto inval; |
| 527 | |
| 528 | /* attach the payload */ |
| 529 | for (pptoken = (struct rxrpc_key_token **)&prep->payload.data[0]; |
| 530 | *pptoken; |
| 531 | pptoken = &(*pptoken)->next) |
| 532 | continue; |
| 533 | *pptoken = token; |
| 534 | if (token->kad->expiry < prep->expiry) |
| 535 | prep->expiry = token->kad->expiry; |
| 536 | |
| 537 | _leave(" = 0"); |
| 538 | return 0; |
| 539 | |
| 540 | inval: |
| 541 | ret = -EINVAL; |
| 542 | error: |
| 543 | rxrpc_rxk5_free(rxk5); |
| 544 | kfree(token); |
| 545 | _leave(" = %d", ret); |
| 546 | return ret; |
| 547 | } |
| 548 | |
| 549 | /* |
| 550 | * attempt to parse the data as the XDR format |
| 551 | * - the caller guarantees we have more than 7 words |
| 552 | */ |
| 553 | static int rxrpc_preparse_xdr(struct key_preparsed_payload *prep) |
| 554 | { |
| 555 | const __be32 *xdr = prep->data, *token; |
| 556 | const char *cp; |
| 557 | unsigned int len, paddedlen, loop, ntoken, toklen, sec_ix; |
| 558 | size_t datalen = prep->datalen; |
| 559 | int ret; |
| 560 | |
| 561 | _enter(",{%x,%x,%x,%x},%zu", |
| 562 | ntohl(xdr[0]), ntohl(xdr[1]), ntohl(xdr[2]), ntohl(xdr[3]), |
| 563 | prep->datalen); |
| 564 | |
| 565 | if (datalen > AFSTOKEN_LENGTH_MAX) |
| 566 | goto not_xdr; |
| 567 | |
| 568 | /* XDR is an array of __be32's */ |
| 569 | if (datalen & 3) |
| 570 | goto not_xdr; |
| 571 | |
| 572 | /* the flags should be 0 (the setpag bit must be handled by |
| 573 | * userspace) */ |
| 574 | if (ntohl(*xdr++) != 0) |
| 575 | goto not_xdr; |
| 576 | datalen -= 4; |
| 577 | |
| 578 | /* check the cell name */ |
| 579 | len = ntohl(*xdr++); |
| 580 | if (len < 1 || len > AFSTOKEN_CELL_MAX) |
| 581 | goto not_xdr; |
| 582 | datalen -= 4; |
| 583 | paddedlen = (len + 3) & ~3; |
| 584 | if (paddedlen > datalen) |
| 585 | goto not_xdr; |
| 586 | |
| 587 | cp = (const char *) xdr; |
| 588 | for (loop = 0; loop < len; loop++) |
| 589 | if (!isprint(cp[loop])) |
| 590 | goto not_xdr; |
| 591 | for (; loop < paddedlen; loop++) |
| 592 | if (cp[loop]) |
| 593 | goto not_xdr; |
| 594 | _debug("cellname: [%u/%u] '%*.*s'", |
| 595 | len, paddedlen, len, len, (const char *) xdr); |
| 596 | datalen -= paddedlen; |
| 597 | xdr += paddedlen >> 2; |
| 598 | |
| 599 | /* get the token count */ |
| 600 | if (datalen < 12) |
| 601 | goto not_xdr; |
| 602 | ntoken = ntohl(*xdr++); |
| 603 | datalen -= 4; |
| 604 | _debug("ntoken: %x", ntoken); |
| 605 | if (ntoken < 1 || ntoken > AFSTOKEN_MAX) |
| 606 | goto not_xdr; |
| 607 | |
| 608 | /* check each token wrapper */ |
| 609 | token = xdr; |
| 610 | loop = ntoken; |
| 611 | do { |
| 612 | if (datalen < 8) |
| 613 | goto not_xdr; |
| 614 | toklen = ntohl(*xdr++); |
| 615 | sec_ix = ntohl(*xdr); |
| 616 | datalen -= 4; |
| 617 | _debug("token: [%x/%zx] %x", toklen, datalen, sec_ix); |
| 618 | paddedlen = (toklen + 3) & ~3; |
| 619 | if (toklen < 20 || toklen > datalen || paddedlen > datalen) |
| 620 | goto not_xdr; |
| 621 | datalen -= paddedlen; |
| 622 | xdr += paddedlen >> 2; |
| 623 | |
| 624 | } while (--loop > 0); |
| 625 | |
| 626 | _debug("remainder: %zu", datalen); |
| 627 | if (datalen != 0) |
| 628 | goto not_xdr; |
| 629 | |
| 630 | /* okay: we're going to assume it's valid XDR format |
| 631 | * - we ignore the cellname, relying on the key to be correctly named |
| 632 | */ |
| 633 | do { |
| 634 | xdr = token; |
| 635 | toklen = ntohl(*xdr++); |
| 636 | token = xdr + ((toklen + 3) >> 2); |
| 637 | sec_ix = ntohl(*xdr++); |
| 638 | toklen -= 4; |
| 639 | |
| 640 | _debug("TOKEN type=%u [%p-%p]", sec_ix, xdr, token); |
| 641 | |
| 642 | switch (sec_ix) { |
| 643 | case RXRPC_SECURITY_RXKAD: |
| 644 | ret = rxrpc_preparse_xdr_rxkad(prep, datalen, xdr, toklen); |
| 645 | if (ret != 0) |
| 646 | goto error; |
| 647 | break; |
| 648 | |
| 649 | case RXRPC_SECURITY_RXK5: |
| 650 | ret = rxrpc_preparse_xdr_rxk5(prep, datalen, xdr, toklen); |
| 651 | if (ret != 0) |
| 652 | goto error; |
| 653 | break; |
| 654 | |
| 655 | default: |
| 656 | ret = -EPROTONOSUPPORT; |
| 657 | goto error; |
| 658 | } |
| 659 | |
| 660 | } while (--ntoken > 0); |
| 661 | |
| 662 | _leave(" = 0"); |
| 663 | return 0; |
| 664 | |
| 665 | not_xdr: |
| 666 | _leave(" = -EPROTO"); |
| 667 | return -EPROTO; |
| 668 | error: |
| 669 | _leave(" = %d", ret); |
| 670 | return ret; |
| 671 | } |
| 672 | |
| 673 | /* |
| 674 | * Preparse an rxrpc defined key. |
| 675 | * |
| 676 | * Data should be of the form: |
| 677 | * OFFSET LEN CONTENT |
| 678 | * 0 4 key interface version number |
| 679 | * 4 2 security index (type) |
| 680 | * 6 2 ticket length |
| 681 | * 8 4 key expiry time (time_t) |
| 682 | * 12 4 kvno |
| 683 | * 16 8 session key |
| 684 | * 24 [len] ticket |
| 685 | * |
| 686 | * if no data is provided, then a no-security key is made |
| 687 | */ |
| 688 | static int rxrpc_preparse(struct key_preparsed_payload *prep) |
| 689 | { |
| 690 | const struct rxrpc_key_data_v1 *v1; |
| 691 | struct rxrpc_key_token *token, **pp; |
| 692 | size_t plen; |
| 693 | u32 kver; |
| 694 | int ret; |
| 695 | |
| 696 | _enter("%zu", prep->datalen); |
| 697 | |
| 698 | /* handle a no-security key */ |
| 699 | if (!prep->data && prep->datalen == 0) |
| 700 | return 0; |
| 701 | |
| 702 | /* determine if the XDR payload format is being used */ |
| 703 | if (prep->datalen > 7 * 4) { |
| 704 | ret = rxrpc_preparse_xdr(prep); |
| 705 | if (ret != -EPROTO) |
| 706 | return ret; |
| 707 | } |
| 708 | |
| 709 | /* get the key interface version number */ |
| 710 | ret = -EINVAL; |
| 711 | if (prep->datalen <= 4 || !prep->data) |
| 712 | goto error; |
| 713 | memcpy(&kver, prep->data, sizeof(kver)); |
| 714 | prep->data += sizeof(kver); |
| 715 | prep->datalen -= sizeof(kver); |
| 716 | |
| 717 | _debug("KEY I/F VERSION: %u", kver); |
| 718 | |
| 719 | ret = -EKEYREJECTED; |
| 720 | if (kver != 1) |
| 721 | goto error; |
| 722 | |
| 723 | /* deal with a version 1 key */ |
| 724 | ret = -EINVAL; |
| 725 | if (prep->datalen < sizeof(*v1)) |
| 726 | goto error; |
| 727 | |
| 728 | v1 = prep->data; |
| 729 | if (prep->datalen != sizeof(*v1) + v1->ticket_length) |
| 730 | goto error; |
| 731 | |
| 732 | _debug("SCIX: %u", v1->security_index); |
| 733 | _debug("TLEN: %u", v1->ticket_length); |
| 734 | _debug("EXPY: %x", v1->expiry); |
| 735 | _debug("KVNO: %u", v1->kvno); |
| 736 | _debug("SKEY: %02x%02x%02x%02x%02x%02x%02x%02x", |
| 737 | v1->session_key[0], v1->session_key[1], |
| 738 | v1->session_key[2], v1->session_key[3], |
| 739 | v1->session_key[4], v1->session_key[5], |
| 740 | v1->session_key[6], v1->session_key[7]); |
| 741 | if (v1->ticket_length >= 8) |
| 742 | _debug("TCKT: %02x%02x%02x%02x%02x%02x%02x%02x", |
| 743 | v1->ticket[0], v1->ticket[1], |
| 744 | v1->ticket[2], v1->ticket[3], |
| 745 | v1->ticket[4], v1->ticket[5], |
| 746 | v1->ticket[6], v1->ticket[7]); |
| 747 | |
| 748 | ret = -EPROTONOSUPPORT; |
| 749 | if (v1->security_index != RXRPC_SECURITY_RXKAD) |
| 750 | goto error; |
| 751 | |
| 752 | plen = sizeof(*token->kad) + v1->ticket_length; |
| 753 | prep->quotalen = plen + sizeof(*token); |
| 754 | |
| 755 | ret = -ENOMEM; |
| 756 | token = kzalloc(sizeof(*token), GFP_KERNEL); |
| 757 | if (!token) |
| 758 | goto error; |
| 759 | token->kad = kzalloc(plen, GFP_KERNEL); |
| 760 | if (!token->kad) |
| 761 | goto error_free; |
| 762 | |
| 763 | token->security_index = RXRPC_SECURITY_RXKAD; |
| 764 | token->kad->ticket_len = v1->ticket_length; |
| 765 | token->kad->expiry = v1->expiry; |
| 766 | token->kad->kvno = v1->kvno; |
| 767 | memcpy(&token->kad->session_key, &v1->session_key, 8); |
| 768 | memcpy(&token->kad->ticket, v1->ticket, v1->ticket_length); |
| 769 | |
| 770 | /* count the number of tokens attached */ |
| 771 | prep->payload.data[1] = (void *)((unsigned long)prep->payload.data[1] + 1); |
| 772 | |
| 773 | /* attach the data */ |
| 774 | pp = (struct rxrpc_key_token **)&prep->payload.data[0]; |
| 775 | while (*pp) |
| 776 | pp = &(*pp)->next; |
| 777 | *pp = token; |
| 778 | if (token->kad->expiry < prep->expiry) |
| 779 | prep->expiry = token->kad->expiry; |
| 780 | token = NULL; |
| 781 | ret = 0; |
| 782 | |
| 783 | error_free: |
| 784 | kfree(token); |
| 785 | error: |
| 786 | return ret; |
| 787 | } |
| 788 | |
| 789 | /* |
| 790 | * Free token list. |
| 791 | */ |
| 792 | static void rxrpc_free_token_list(struct rxrpc_key_token *token) |
| 793 | { |
| 794 | struct rxrpc_key_token *next; |
| 795 | |
| 796 | for (; token; token = next) { |
| 797 | next = token->next; |
| 798 | switch (token->security_index) { |
| 799 | case RXRPC_SECURITY_RXKAD: |
| 800 | kfree(token->kad); |
| 801 | break; |
| 802 | case RXRPC_SECURITY_RXK5: |
| 803 | if (token->k5) |
| 804 | rxrpc_rxk5_free(token->k5); |
| 805 | break; |
| 806 | default: |
| 807 | printk(KERN_ERR "Unknown token type %x on rxrpc key\n", |
| 808 | token->security_index); |
| 809 | BUG(); |
| 810 | } |
| 811 | |
| 812 | kfree(token); |
| 813 | } |
| 814 | } |
| 815 | |
| 816 | /* |
| 817 | * Clean up preparse data. |
| 818 | */ |
| 819 | static void rxrpc_free_preparse(struct key_preparsed_payload *prep) |
| 820 | { |
| 821 | rxrpc_free_token_list(prep->payload.data[0]); |
| 822 | } |
| 823 | |
| 824 | /* |
| 825 | * Preparse a server secret key. |
| 826 | * |
| 827 | * The data should be the 8-byte secret key. |
| 828 | */ |
| 829 | static int rxrpc_preparse_s(struct key_preparsed_payload *prep) |
| 830 | { |
| 831 | struct crypto_blkcipher *ci; |
| 832 | |
| 833 | _enter("%zu", prep->datalen); |
| 834 | |
| 835 | if (prep->datalen != 8) |
| 836 | return -EINVAL; |
| 837 | |
| 838 | memcpy(&prep->payload.data[2], prep->data, 8); |
| 839 | |
| 840 | ci = crypto_alloc_blkcipher("pcbc(des)", 0, CRYPTO_ALG_ASYNC); |
| 841 | if (IS_ERR(ci)) { |
| 842 | _leave(" = %ld", PTR_ERR(ci)); |
| 843 | return PTR_ERR(ci); |
| 844 | } |
| 845 | |
| 846 | if (crypto_blkcipher_setkey(ci, prep->data, 8) < 0) |
| 847 | BUG(); |
| 848 | |
| 849 | prep->payload.data[0] = ci; |
| 850 | _leave(" = 0"); |
| 851 | return 0; |
| 852 | } |
| 853 | |
| 854 | /* |
| 855 | * Clean up preparse data. |
| 856 | */ |
| 857 | static void rxrpc_free_preparse_s(struct key_preparsed_payload *prep) |
| 858 | { |
| 859 | if (prep->payload.data[0]) |
| 860 | crypto_free_blkcipher(prep->payload.data[0]); |
| 861 | } |
| 862 | |
| 863 | /* |
| 864 | * dispose of the data dangling from the corpse of a rxrpc key |
| 865 | */ |
| 866 | static void rxrpc_destroy(struct key *key) |
| 867 | { |
| 868 | rxrpc_free_token_list(key->payload.data[0]); |
| 869 | } |
| 870 | |
| 871 | /* |
| 872 | * dispose of the data dangling from the corpse of a rxrpc key |
| 873 | */ |
| 874 | static void rxrpc_destroy_s(struct key *key) |
| 875 | { |
| 876 | if (key->payload.data[0]) { |
| 877 | crypto_free_blkcipher(key->payload.data[0]); |
| 878 | key->payload.data[0] = NULL; |
| 879 | } |
| 880 | } |
| 881 | |
| 882 | /* |
| 883 | * describe the rxrpc key |
| 884 | */ |
| 885 | static void rxrpc_describe(const struct key *key, struct seq_file *m) |
| 886 | { |
| 887 | seq_puts(m, key->description); |
| 888 | } |
| 889 | |
| 890 | /* |
| 891 | * grab the security key for a socket |
| 892 | */ |
| 893 | int rxrpc_request_key(struct rxrpc_sock *rx, char __user *optval, int optlen) |
| 894 | { |
| 895 | struct key *key; |
| 896 | char *description; |
| 897 | |
| 898 | _enter(""); |
| 899 | |
| 900 | if (optlen <= 0 || optlen > PAGE_SIZE - 1) |
| 901 | return -EINVAL; |
| 902 | |
| 903 | description = kmalloc(optlen + 1, GFP_KERNEL); |
| 904 | if (!description) |
| 905 | return -ENOMEM; |
| 906 | |
| 907 | if (copy_from_user(description, optval, optlen)) { |
| 908 | kfree(description); |
| 909 | return -EFAULT; |
| 910 | } |
| 911 | description[optlen] = 0; |
| 912 | |
| 913 | key = request_key(&key_type_rxrpc, description, NULL); |
| 914 | if (IS_ERR(key)) { |
| 915 | kfree(description); |
| 916 | _leave(" = %ld", PTR_ERR(key)); |
| 917 | return PTR_ERR(key); |
| 918 | } |
| 919 | |
| 920 | rx->key = key; |
| 921 | kfree(description); |
| 922 | _leave(" = 0 [key %x]", key->serial); |
| 923 | return 0; |
| 924 | } |
| 925 | |
| 926 | /* |
| 927 | * grab the security keyring for a server socket |
| 928 | */ |
| 929 | int rxrpc_server_keyring(struct rxrpc_sock *rx, char __user *optval, |
| 930 | int optlen) |
| 931 | { |
| 932 | struct key *key; |
| 933 | char *description; |
| 934 | |
| 935 | _enter(""); |
| 936 | |
| 937 | if (optlen <= 0 || optlen > PAGE_SIZE - 1) |
| 938 | return -EINVAL; |
| 939 | |
| 940 | description = kmalloc(optlen + 1, GFP_KERNEL); |
| 941 | if (!description) |
| 942 | return -ENOMEM; |
| 943 | |
| 944 | if (copy_from_user(description, optval, optlen)) { |
| 945 | kfree(description); |
| 946 | return -EFAULT; |
| 947 | } |
| 948 | description[optlen] = 0; |
| 949 | |
| 950 | key = request_key(&key_type_keyring, description, NULL); |
| 951 | if (IS_ERR(key)) { |
| 952 | kfree(description); |
| 953 | _leave(" = %ld", PTR_ERR(key)); |
| 954 | return PTR_ERR(key); |
| 955 | } |
| 956 | |
| 957 | rx->securities = key; |
| 958 | kfree(description); |
| 959 | _leave(" = 0 [key %x]", key->serial); |
| 960 | return 0; |
| 961 | } |
| 962 | |
| 963 | /* |
| 964 | * generate a server data key |
| 965 | */ |
| 966 | int rxrpc_get_server_data_key(struct rxrpc_connection *conn, |
| 967 | const void *session_key, |
| 968 | time_t expiry, |
| 969 | u32 kvno) |
| 970 | { |
| 971 | const struct cred *cred = current_cred(); |
| 972 | struct key *key; |
| 973 | int ret; |
| 974 | |
| 975 | struct { |
| 976 | u32 kver; |
| 977 | struct rxrpc_key_data_v1 v1; |
| 978 | } data; |
| 979 | |
| 980 | _enter(""); |
| 981 | |
| 982 | key = key_alloc(&key_type_rxrpc, "x", |
| 983 | GLOBAL_ROOT_UID, GLOBAL_ROOT_GID, cred, 0, |
| 984 | KEY_ALLOC_NOT_IN_QUOTA); |
| 985 | if (IS_ERR(key)) { |
| 986 | _leave(" = -ENOMEM [alloc %ld]", PTR_ERR(key)); |
| 987 | return -ENOMEM; |
| 988 | } |
| 989 | |
| 990 | _debug("key %d", key_serial(key)); |
| 991 | |
| 992 | data.kver = 1; |
| 993 | data.v1.security_index = RXRPC_SECURITY_RXKAD; |
| 994 | data.v1.ticket_length = 0; |
| 995 | data.v1.expiry = expiry; |
| 996 | data.v1.kvno = 0; |
| 997 | |
| 998 | memcpy(&data.v1.session_key, session_key, sizeof(data.v1.session_key)); |
| 999 | |
| 1000 | ret = key_instantiate_and_link(key, &data, sizeof(data), NULL, NULL); |
| 1001 | if (ret < 0) |
| 1002 | goto error; |
| 1003 | |
| 1004 | conn->key = key; |
| 1005 | _leave(" = 0 [%d]", key_serial(key)); |
| 1006 | return 0; |
| 1007 | |
| 1008 | error: |
| 1009 | key_revoke(key); |
| 1010 | key_put(key); |
| 1011 | _leave(" = -ENOMEM [ins %d]", ret); |
| 1012 | return -ENOMEM; |
| 1013 | } |
| 1014 | EXPORT_SYMBOL(rxrpc_get_server_data_key); |
| 1015 | |
| 1016 | /** |
| 1017 | * rxrpc_get_null_key - Generate a null RxRPC key |
| 1018 | * @keyname: The name to give the key. |
| 1019 | * |
| 1020 | * Generate a null RxRPC key that can be used to indicate anonymous security is |
| 1021 | * required for a particular domain. |
| 1022 | */ |
| 1023 | struct key *rxrpc_get_null_key(const char *keyname) |
| 1024 | { |
| 1025 | const struct cred *cred = current_cred(); |
| 1026 | struct key *key; |
| 1027 | int ret; |
| 1028 | |
| 1029 | key = key_alloc(&key_type_rxrpc, keyname, |
| 1030 | GLOBAL_ROOT_UID, GLOBAL_ROOT_GID, cred, |
| 1031 | KEY_POS_SEARCH, KEY_ALLOC_NOT_IN_QUOTA); |
| 1032 | if (IS_ERR(key)) |
| 1033 | return key; |
| 1034 | |
| 1035 | ret = key_instantiate_and_link(key, NULL, 0, NULL, NULL); |
| 1036 | if (ret < 0) { |
| 1037 | key_revoke(key); |
| 1038 | key_put(key); |
| 1039 | return ERR_PTR(ret); |
| 1040 | } |
| 1041 | |
| 1042 | return key; |
| 1043 | } |
| 1044 | EXPORT_SYMBOL(rxrpc_get_null_key); |
| 1045 | |
| 1046 | /* |
| 1047 | * read the contents of an rxrpc key |
| 1048 | * - this returns the result in XDR form |
| 1049 | */ |
| 1050 | static long rxrpc_read(const struct key *key, |
| 1051 | char __user *buffer, size_t buflen) |
| 1052 | { |
| 1053 | const struct rxrpc_key_token *token; |
| 1054 | const struct krb5_principal *princ; |
| 1055 | size_t size; |
| 1056 | __be32 __user *xdr, *oldxdr; |
| 1057 | u32 cnlen, toksize, ntoks, tok, zero; |
| 1058 | u16 toksizes[AFSTOKEN_MAX]; |
| 1059 | int loop; |
| 1060 | |
| 1061 | _enter(""); |
| 1062 | |
| 1063 | /* we don't know what form we should return non-AFS keys in */ |
| 1064 | if (memcmp(key->description, "afs@", 4) != 0) |
| 1065 | return -EOPNOTSUPP; |
| 1066 | cnlen = strlen(key->description + 4); |
| 1067 | |
| 1068 | #define RND(X) (((X) + 3) & ~3) |
| 1069 | |
| 1070 | /* AFS keys we return in XDR form, so we need to work out the size of |
| 1071 | * the XDR */ |
| 1072 | size = 2 * 4; /* flags, cellname len */ |
| 1073 | size += RND(cnlen); /* cellname */ |
| 1074 | size += 1 * 4; /* token count */ |
| 1075 | |
| 1076 | ntoks = 0; |
| 1077 | for (token = key->payload.data[0]; token; token = token->next) { |
| 1078 | toksize = 4; /* sec index */ |
| 1079 | |
| 1080 | switch (token->security_index) { |
| 1081 | case RXRPC_SECURITY_RXKAD: |
| 1082 | toksize += 8 * 4; /* viceid, kvno, key*2, begin, |
| 1083 | * end, primary, tktlen */ |
| 1084 | toksize += RND(token->kad->ticket_len); |
| 1085 | break; |
| 1086 | |
| 1087 | case RXRPC_SECURITY_RXK5: |
| 1088 | princ = &token->k5->client; |
| 1089 | toksize += 4 + princ->n_name_parts * 4; |
| 1090 | for (loop = 0; loop < princ->n_name_parts; loop++) |
| 1091 | toksize += RND(strlen(princ->name_parts[loop])); |
| 1092 | toksize += 4 + RND(strlen(princ->realm)); |
| 1093 | |
| 1094 | princ = &token->k5->server; |
| 1095 | toksize += 4 + princ->n_name_parts * 4; |
| 1096 | for (loop = 0; loop < princ->n_name_parts; loop++) |
| 1097 | toksize += RND(strlen(princ->name_parts[loop])); |
| 1098 | toksize += 4 + RND(strlen(princ->realm)); |
| 1099 | |
| 1100 | toksize += 8 + RND(token->k5->session.data_len); |
| 1101 | |
| 1102 | toksize += 4 * 8 + 2 * 4; |
| 1103 | |
| 1104 | toksize += 4 + token->k5->n_addresses * 8; |
| 1105 | for (loop = 0; loop < token->k5->n_addresses; loop++) |
| 1106 | toksize += RND(token->k5->addresses[loop].data_len); |
| 1107 | |
| 1108 | toksize += 4 + RND(token->k5->ticket_len); |
| 1109 | toksize += 4 + RND(token->k5->ticket2_len); |
| 1110 | |
| 1111 | toksize += 4 + token->k5->n_authdata * 8; |
| 1112 | for (loop = 0; loop < token->k5->n_authdata; loop++) |
| 1113 | toksize += RND(token->k5->authdata[loop].data_len); |
| 1114 | break; |
| 1115 | |
| 1116 | default: /* we have a ticket we can't encode */ |
| 1117 | BUG(); |
| 1118 | continue; |
| 1119 | } |
| 1120 | |
| 1121 | _debug("token[%u]: toksize=%u", ntoks, toksize); |
| 1122 | ASSERTCMP(toksize, <=, AFSTOKEN_LENGTH_MAX); |
| 1123 | |
| 1124 | toksizes[ntoks++] = toksize; |
| 1125 | size += toksize + 4; /* each token has a length word */ |
| 1126 | } |
| 1127 | |
| 1128 | #undef RND |
| 1129 | |
| 1130 | if (!buffer || buflen < size) |
| 1131 | return size; |
| 1132 | |
| 1133 | xdr = (__be32 __user *) buffer; |
| 1134 | zero = 0; |
| 1135 | #define ENCODE(x) \ |
| 1136 | do { \ |
| 1137 | __be32 y = htonl(x); \ |
| 1138 | if (put_user(y, xdr++) < 0) \ |
| 1139 | goto fault; \ |
| 1140 | } while(0) |
| 1141 | #define ENCODE_DATA(l, s) \ |
| 1142 | do { \ |
| 1143 | u32 _l = (l); \ |
| 1144 | ENCODE(l); \ |
| 1145 | if (copy_to_user(xdr, (s), _l) != 0) \ |
| 1146 | goto fault; \ |
| 1147 | if (_l & 3 && \ |
| 1148 | copy_to_user((u8 __user *)xdr + _l, &zero, 4 - (_l & 3)) != 0) \ |
| 1149 | goto fault; \ |
| 1150 | xdr += (_l + 3) >> 2; \ |
| 1151 | } while(0) |
| 1152 | #define ENCODE64(x) \ |
| 1153 | do { \ |
| 1154 | __be64 y = cpu_to_be64(x); \ |
| 1155 | if (copy_to_user(xdr, &y, 8) != 0) \ |
| 1156 | goto fault; \ |
| 1157 | xdr += 8 >> 2; \ |
| 1158 | } while(0) |
| 1159 | #define ENCODE_STR(s) \ |
| 1160 | do { \ |
| 1161 | const char *_s = (s); \ |
| 1162 | ENCODE_DATA(strlen(_s), _s); \ |
| 1163 | } while(0) |
| 1164 | |
| 1165 | ENCODE(0); /* flags */ |
| 1166 | ENCODE_DATA(cnlen, key->description + 4); /* cellname */ |
| 1167 | ENCODE(ntoks); |
| 1168 | |
| 1169 | tok = 0; |
| 1170 | for (token = key->payload.data[0]; token; token = token->next) { |
| 1171 | toksize = toksizes[tok++]; |
| 1172 | ENCODE(toksize); |
| 1173 | oldxdr = xdr; |
| 1174 | ENCODE(token->security_index); |
| 1175 | |
| 1176 | switch (token->security_index) { |
| 1177 | case RXRPC_SECURITY_RXKAD: |
| 1178 | ENCODE(token->kad->vice_id); |
| 1179 | ENCODE(token->kad->kvno); |
| 1180 | ENCODE_DATA(8, token->kad->session_key); |
| 1181 | ENCODE(token->kad->start); |
| 1182 | ENCODE(token->kad->expiry); |
| 1183 | ENCODE(token->kad->primary_flag); |
| 1184 | ENCODE_DATA(token->kad->ticket_len, token->kad->ticket); |
| 1185 | break; |
| 1186 | |
| 1187 | case RXRPC_SECURITY_RXK5: |
| 1188 | princ = &token->k5->client; |
| 1189 | ENCODE(princ->n_name_parts); |
| 1190 | for (loop = 0; loop < princ->n_name_parts; loop++) |
| 1191 | ENCODE_STR(princ->name_parts[loop]); |
| 1192 | ENCODE_STR(princ->realm); |
| 1193 | |
| 1194 | princ = &token->k5->server; |
| 1195 | ENCODE(princ->n_name_parts); |
| 1196 | for (loop = 0; loop < princ->n_name_parts; loop++) |
| 1197 | ENCODE_STR(princ->name_parts[loop]); |
| 1198 | ENCODE_STR(princ->realm); |
| 1199 | |
| 1200 | ENCODE(token->k5->session.tag); |
| 1201 | ENCODE_DATA(token->k5->session.data_len, |
| 1202 | token->k5->session.data); |
| 1203 | |
| 1204 | ENCODE64(token->k5->authtime); |
| 1205 | ENCODE64(token->k5->starttime); |
| 1206 | ENCODE64(token->k5->endtime); |
| 1207 | ENCODE64(token->k5->renew_till); |
| 1208 | ENCODE(token->k5->is_skey); |
| 1209 | ENCODE(token->k5->flags); |
| 1210 | |
| 1211 | ENCODE(token->k5->n_addresses); |
| 1212 | for (loop = 0; loop < token->k5->n_addresses; loop++) { |
| 1213 | ENCODE(token->k5->addresses[loop].tag); |
| 1214 | ENCODE_DATA(token->k5->addresses[loop].data_len, |
| 1215 | token->k5->addresses[loop].data); |
| 1216 | } |
| 1217 | |
| 1218 | ENCODE_DATA(token->k5->ticket_len, token->k5->ticket); |
| 1219 | ENCODE_DATA(token->k5->ticket2_len, token->k5->ticket2); |
| 1220 | |
| 1221 | ENCODE(token->k5->n_authdata); |
| 1222 | for (loop = 0; loop < token->k5->n_authdata; loop++) { |
| 1223 | ENCODE(token->k5->authdata[loop].tag); |
| 1224 | ENCODE_DATA(token->k5->authdata[loop].data_len, |
| 1225 | token->k5->authdata[loop].data); |
| 1226 | } |
| 1227 | break; |
| 1228 | |
| 1229 | default: |
| 1230 | BUG(); |
| 1231 | break; |
| 1232 | } |
| 1233 | |
| 1234 | ASSERTCMP((unsigned long)xdr - (unsigned long)oldxdr, ==, |
| 1235 | toksize); |
| 1236 | } |
| 1237 | |
| 1238 | #undef ENCODE_STR |
| 1239 | #undef ENCODE_DATA |
| 1240 | #undef ENCODE64 |
| 1241 | #undef ENCODE |
| 1242 | |
| 1243 | ASSERTCMP(tok, ==, ntoks); |
| 1244 | ASSERTCMP((char __user *) xdr - buffer, ==, size); |
| 1245 | _leave(" = %zu", size); |
| 1246 | return size; |
| 1247 | |
| 1248 | fault: |
| 1249 | _leave(" = -EFAULT"); |
| 1250 | return -EFAULT; |
| 1251 | } |