1 // SPDX-License-Identifier: GPL-2.0 2 3 #include <linux/ceph/ceph_debug.h> 4 5 #include <linux/err.h> 6 #include <linux/module.h> 7 #include <linux/random.h> 8 #include <linux/slab.h> 9 10 #include <linux/ceph/decode.h> 11 #include <linux/ceph/auth.h> 12 #include <linux/ceph/ceph_features.h> 13 #include <linux/ceph/libceph.h> 14 #include <linux/ceph/messenger.h> 15 16 #include "crypto.h" 17 #include "auth_x.h" 18 #include "auth_x_protocol.h" 19 20 static const u32 ticket_key_usages[] = { 21 CEPHX_KEY_USAGE_TICKET_SESSION_KEY, 22 CEPHX_KEY_USAGE_TICKET_BLOB, 23 CEPHX_KEY_USAGE_AUTH_CONNECTION_SECRET 24 }; 25 26 static const u32 authorizer_key_usages[] = { 27 CEPHX_KEY_USAGE_AUTHORIZE, 28 CEPHX_KEY_USAGE_AUTHORIZE_CHALLENGE, 29 CEPHX_KEY_USAGE_AUTHORIZE_REPLY 30 }; 31 32 static const u32 client_key_usages[] = { 33 CEPHX_KEY_USAGE_TICKET_SESSION_KEY 34 }; 35 36 static void ceph_x_validate_tickets(struct ceph_auth_client *ac, int *pneed); 37 38 static int ceph_x_is_authenticated(struct ceph_auth_client *ac) 39 { 40 struct ceph_x_info *xi = ac->private; 41 int missing; 42 int need; /* missing + need renewal */ 43 44 ceph_x_validate_tickets(ac, &need); 45 missing = ac->want_keys & ~xi->have_keys; 46 WARN_ON((need & missing) != missing); 47 dout("%s want 0x%x have 0x%x missing 0x%x -> %d\n", __func__, 48 ac->want_keys, xi->have_keys, missing, !missing); 49 return !missing; 50 } 51 52 static int ceph_x_should_authenticate(struct ceph_auth_client *ac) 53 { 54 struct ceph_x_info *xi = ac->private; 55 int need; 56 57 ceph_x_validate_tickets(ac, &need); 58 dout("%s want 0x%x have 0x%x need 0x%x -> %d\n", __func__, 59 ac->want_keys, xi->have_keys, need, !!need); 60 return !!need; 61 } 62 63 static int __ceph_x_encrypt_offset(const struct ceph_crypto_key *key) 64 { 65 return ceph_crypt_data_offset(key) + 66 sizeof(struct ceph_x_encrypt_header); 67 } 68 69 static int ceph_x_encrypt_offset(const struct ceph_crypto_key *key) 70 { 71 return sizeof(u32) + __ceph_x_encrypt_offset(key); 72 } 73 74 /* 75 * AES: ciphertext_len | hdr | data... | padding 76 * AES256KRB5: ciphertext_len | confounder | hdr | data... | hmac 77 */ 78 static int ceph_x_encrypt_buflen(const struct ceph_crypto_key *key, 79 int data_len) 80 { 81 int encrypt_len = sizeof(struct ceph_x_encrypt_header) + data_len; 82 return sizeof(u32) + ceph_crypt_buflen(key, encrypt_len); 83 } 84 85 static int ceph_x_encrypt(const struct ceph_crypto_key *key, int usage_slot, 86 void *buf, int buf_len, int plaintext_len) 87 { 88 struct ceph_x_encrypt_header *hdr; 89 int ciphertext_len; 90 int ret; 91 92 hdr = buf + sizeof(u32) + ceph_crypt_data_offset(key); 93 hdr->struct_v = 1; 94 hdr->magic = cpu_to_le64(CEPHX_ENC_MAGIC); 95 96 ret = ceph_crypt(key, usage_slot, true, buf + sizeof(u32), 97 buf_len - sizeof(u32), plaintext_len + sizeof(*hdr), 98 &ciphertext_len); 99 if (ret) 100 return ret; 101 102 ceph_encode_32(&buf, ciphertext_len); 103 return sizeof(u32) + ciphertext_len; 104 } 105 106 static int __ceph_x_decrypt(const struct ceph_crypto_key *key, int usage_slot, 107 void *p, int ciphertext_len) 108 { 109 struct ceph_x_encrypt_header *hdr; 110 int plaintext_len; 111 int ret; 112 113 ret = ceph_crypt(key, usage_slot, false, p, ciphertext_len, 114 ciphertext_len, &plaintext_len); 115 if (ret) 116 return ret; 117 118 if (plaintext_len < sizeof(*hdr)) { 119 pr_err("%s plaintext too small %d\n", __func__, plaintext_len); 120 return -EINVAL; 121 } 122 123 hdr = p + ceph_crypt_data_offset(key); 124 if (le64_to_cpu(hdr->magic) != CEPHX_ENC_MAGIC) { 125 pr_err("%s bad magic\n", __func__); 126 return -EINVAL; 127 } 128 129 return plaintext_len - sizeof(*hdr); 130 } 131 132 static int ceph_x_decrypt(const struct ceph_crypto_key *key, int usage_slot, 133 void **p, void *end) 134 { 135 int ciphertext_len; 136 int ret; 137 138 ceph_decode_32_safe(p, end, ciphertext_len, e_inval); 139 ceph_decode_need(p, end, ciphertext_len, e_inval); 140 141 ret = __ceph_x_decrypt(key, usage_slot, *p, ciphertext_len); 142 if (ret < 0) 143 return ret; 144 145 *p += ciphertext_len; 146 return ret; 147 148 e_inval: 149 return -EINVAL; 150 } 151 152 /* 153 * get existing (or insert new) ticket handler 154 */ 155 static struct ceph_x_ticket_handler * 156 get_ticket_handler(struct ceph_auth_client *ac, int service) 157 { 158 struct ceph_x_ticket_handler *th; 159 struct ceph_x_info *xi = ac->private; 160 struct rb_node *parent = NULL, **p = &xi->ticket_handlers.rb_node; 161 162 while (*p) { 163 parent = *p; 164 th = rb_entry(parent, struct ceph_x_ticket_handler, node); 165 if (service < th->service) 166 p = &(*p)->rb_left; 167 else if (service > th->service) 168 p = &(*p)->rb_right; 169 else 170 return th; 171 } 172 173 /* add it */ 174 th = kzalloc_obj(*th, GFP_NOFS); 175 if (!th) 176 return ERR_PTR(-ENOMEM); 177 th->service = service; 178 rb_link_node(&th->node, parent, p); 179 rb_insert_color(&th->node, &xi->ticket_handlers); 180 return th; 181 } 182 183 static void remove_ticket_handler(struct ceph_auth_client *ac, 184 struct ceph_x_ticket_handler *th) 185 { 186 struct ceph_x_info *xi = ac->private; 187 188 dout("remove_ticket_handler %p %d\n", th, th->service); 189 rb_erase(&th->node, &xi->ticket_handlers); 190 ceph_crypto_key_destroy(&th->session_key); 191 if (th->ticket_blob) 192 ceph_buffer_put(th->ticket_blob); 193 kfree(th); 194 } 195 196 static int process_one_ticket(struct ceph_auth_client *ac, 197 struct ceph_crypto_key *secret, 198 void **p, void *end) 199 { 200 struct ceph_x_info *xi = ac->private; 201 int type; 202 u8 tkt_struct_v, blob_struct_v; 203 struct ceph_x_ticket_handler *th; 204 void *dp, *dend; 205 int dlen; 206 char is_enc; 207 struct timespec64 validity; 208 void *tp, *tpend; 209 void **ptp; 210 struct ceph_crypto_key new_session_key = { 0 }; 211 struct ceph_buffer *new_ticket_blob; 212 time64_t new_expires, new_renew_after; 213 u64 new_secret_id; 214 int ret; 215 216 ceph_decode_need(p, end, sizeof(u32) + 1, bad); 217 218 type = ceph_decode_32(p); 219 dout(" ticket type %d %s\n", type, ceph_entity_type_name(type)); 220 221 tkt_struct_v = ceph_decode_8(p); 222 if (tkt_struct_v != 1) 223 goto bad; 224 225 th = get_ticket_handler(ac, type); 226 if (IS_ERR(th)) { 227 ret = PTR_ERR(th); 228 goto out; 229 } 230 231 /* blob for me */ 232 dp = *p + ceph_x_encrypt_offset(secret); 233 ret = ceph_x_decrypt(secret, 234 0 /* CEPHX_KEY_USAGE_TICKET_SESSION_KEY */, 235 p, end); 236 if (ret < 0) 237 goto out; 238 dout(" decrypted %d bytes\n", ret); 239 dend = dp + ret; 240 241 ceph_decode_8_safe(&dp, dend, tkt_struct_v, bad); 242 if (tkt_struct_v != 1) 243 goto bad; 244 245 ret = ceph_crypto_key_decode(&new_session_key, &dp, dend); 246 if (ret) 247 goto out; 248 249 ret = ceph_crypto_key_prepare(&new_session_key, ticket_key_usages, 250 ARRAY_SIZE(ticket_key_usages)); 251 if (ret) 252 goto out; 253 254 ceph_decode_need(&dp, dend, sizeof(struct ceph_timespec), bad); 255 ceph_decode_timespec64(&validity, dp); 256 dp += sizeof(struct ceph_timespec); 257 new_expires = ktime_get_real_seconds() + validity.tv_sec; 258 new_renew_after = new_expires - (validity.tv_sec / 4); 259 dout(" expires=%llu renew_after=%llu\n", new_expires, 260 new_renew_after); 261 262 /* ticket blob for service */ 263 ceph_decode_8_safe(p, end, is_enc, bad); 264 if (is_enc) { 265 /* encrypted */ 266 tp = *p + ceph_x_encrypt_offset(&th->session_key); 267 ret = ceph_x_decrypt(&th->session_key, 268 1 /* CEPHX_KEY_USAGE_TICKET_BLOB */, 269 p, end); 270 if (ret < 0) 271 goto out; 272 dout(" encrypted ticket, decrypted %d bytes\n", ret); 273 ptp = &tp; 274 tpend = tp + ret; 275 } else { 276 /* unencrypted */ 277 ptp = p; 278 tpend = end; 279 } 280 ceph_decode_32_safe(ptp, tpend, dlen, bad); 281 dout(" ticket blob is %d bytes\n", dlen); 282 ceph_decode_need(ptp, tpend, 1 + sizeof(u64), bad); 283 blob_struct_v = ceph_decode_8(ptp); 284 if (blob_struct_v != 1) 285 goto bad; 286 287 new_secret_id = ceph_decode_64(ptp); 288 ret = ceph_decode_buffer(&new_ticket_blob, ptp, tpend); 289 if (ret) 290 goto out; 291 292 /* all is well, update our ticket */ 293 ceph_crypto_key_destroy(&th->session_key); 294 if (th->ticket_blob) 295 ceph_buffer_put(th->ticket_blob); 296 th->session_key = new_session_key; 297 th->ticket_blob = new_ticket_blob; 298 th->secret_id = new_secret_id; 299 th->expires = new_expires; 300 th->renew_after = new_renew_after; 301 th->have_key = true; 302 dout(" got ticket service %d (%s) secret_id %lld len %d\n", 303 type, ceph_entity_type_name(type), th->secret_id, 304 (int)th->ticket_blob->vec.iov_len); 305 xi->have_keys |= th->service; 306 return 0; 307 308 bad: 309 ret = -EINVAL; 310 out: 311 ceph_crypto_key_destroy(&new_session_key); 312 return ret; 313 } 314 315 static int ceph_x_proc_ticket_reply(struct ceph_auth_client *ac, 316 struct ceph_crypto_key *secret, 317 void **p, void *end) 318 { 319 u8 reply_struct_v; 320 u32 num; 321 int ret; 322 323 ceph_decode_8_safe(p, end, reply_struct_v, bad); 324 if (reply_struct_v != 1) 325 return -EINVAL; 326 327 ceph_decode_32_safe(p, end, num, bad); 328 dout("%d tickets\n", num); 329 330 while (num--) { 331 ret = process_one_ticket(ac, secret, p, end); 332 if (ret) 333 return ret; 334 } 335 336 return 0; 337 338 bad: 339 return -EINVAL; 340 } 341 342 /* 343 * Encode and encrypt the second part (ceph_x_authorize_b) of the 344 * authorizer. The first part (ceph_x_authorize_a) should already be 345 * encoded. 346 */ 347 static int encrypt_authorizer(struct ceph_x_authorizer *au, 348 u64 *server_challenge) 349 { 350 struct ceph_x_authorize_a *msg_a; 351 struct ceph_x_authorize_b *msg_b; 352 void *p, *end; 353 int ret; 354 355 msg_a = au->buf->vec.iov_base; 356 WARN_ON(msg_a->ticket_blob.secret_id != cpu_to_le64(au->secret_id)); 357 p = (void *)(msg_a + 1) + le32_to_cpu(msg_a->ticket_blob.blob_len); 358 end = au->buf->vec.iov_base + au->buf->vec.iov_len; 359 360 msg_b = p + ceph_x_encrypt_offset(&au->session_key); 361 msg_b->struct_v = 2; 362 msg_b->nonce = cpu_to_le64(au->nonce); 363 if (server_challenge) { 364 msg_b->have_challenge = 1; 365 msg_b->server_challenge_plus_one = 366 cpu_to_le64(*server_challenge + 1); 367 } else { 368 msg_b->have_challenge = 0; 369 msg_b->server_challenge_plus_one = 0; 370 } 371 372 ret = ceph_x_encrypt(&au->session_key, 373 0 /* CEPHX_KEY_USAGE_AUTHORIZE */, 374 p, end - p, sizeof(*msg_b)); 375 if (ret < 0) 376 return ret; 377 378 p += ret; 379 if (server_challenge) { 380 WARN_ON(p != end); 381 } else { 382 WARN_ON(p > end); 383 au->buf->vec.iov_len = p - au->buf->vec.iov_base; 384 } 385 386 return 0; 387 } 388 389 static void ceph_x_authorizer_cleanup(struct ceph_x_authorizer *au) 390 { 391 ceph_crypto_key_destroy(&au->session_key); 392 if (au->buf) { 393 ceph_buffer_put(au->buf); 394 au->buf = NULL; 395 } 396 } 397 398 static int ceph_x_build_authorizer(struct ceph_auth_client *ac, 399 struct ceph_x_ticket_handler *th, 400 struct ceph_x_authorizer *au) 401 { 402 int maxlen; 403 struct ceph_x_authorize_a *msg_a; 404 struct ceph_x_authorize_b *msg_b; 405 int ret; 406 int ticket_blob_len = 407 (th->ticket_blob ? th->ticket_blob->vec.iov_len : 0); 408 409 dout("build_authorizer for %s %p\n", 410 ceph_entity_type_name(th->service), au); 411 412 ceph_crypto_key_destroy(&au->session_key); 413 ret = ceph_crypto_key_clone(&au->session_key, &th->session_key); 414 if (ret) 415 goto out_au; 416 417 ret = ceph_crypto_key_prepare(&au->session_key, authorizer_key_usages, 418 ARRAY_SIZE(authorizer_key_usages)); 419 if (ret) 420 goto out_au; 421 422 maxlen = sizeof(*msg_a) + ticket_blob_len + 423 ceph_x_encrypt_buflen(&au->session_key, sizeof(*msg_b)); 424 dout(" need len %d\n", maxlen); 425 if (au->buf && au->buf->alloc_len < maxlen) { 426 ceph_buffer_put(au->buf); 427 au->buf = NULL; 428 } 429 if (!au->buf) { 430 au->buf = ceph_buffer_new(maxlen, GFP_NOFS); 431 if (!au->buf) { 432 ret = -ENOMEM; 433 goto out_au; 434 } 435 } 436 au->service = th->service; 437 WARN_ON(!th->secret_id); 438 au->secret_id = th->secret_id; 439 440 msg_a = au->buf->vec.iov_base; 441 msg_a->struct_v = 1; 442 msg_a->global_id = cpu_to_le64(ac->global_id); 443 msg_a->service_id = cpu_to_le32(th->service); 444 msg_a->ticket_blob.struct_v = 1; 445 msg_a->ticket_blob.secret_id = cpu_to_le64(th->secret_id); 446 msg_a->ticket_blob.blob_len = cpu_to_le32(ticket_blob_len); 447 if (ticket_blob_len) { 448 memcpy(msg_a->ticket_blob.blob, th->ticket_blob->vec.iov_base, 449 th->ticket_blob->vec.iov_len); 450 } 451 dout(" th %p secret_id %lld %lld\n", th, th->secret_id, 452 le64_to_cpu(msg_a->ticket_blob.secret_id)); 453 454 get_random_bytes(&au->nonce, sizeof(au->nonce)); 455 ret = encrypt_authorizer(au, NULL); 456 if (ret) { 457 pr_err("failed to encrypt authorizer: %d", ret); 458 goto out_au; 459 } 460 461 dout(" built authorizer nonce %llx len %d\n", au->nonce, 462 (int)au->buf->vec.iov_len); 463 return 0; 464 465 out_au: 466 ceph_x_authorizer_cleanup(au); 467 return ret; 468 } 469 470 static int ceph_x_encode_ticket(struct ceph_x_ticket_handler *th, 471 void **p, void *end) 472 { 473 ceph_decode_need(p, end, 1 + sizeof(u64), bad); 474 ceph_encode_8(p, 1); 475 ceph_encode_64(p, th->secret_id); 476 if (th->ticket_blob) { 477 const char *buf = th->ticket_blob->vec.iov_base; 478 u32 len = th->ticket_blob->vec.iov_len; 479 480 ceph_encode_32_safe(p, end, len, bad); 481 ceph_encode_copy_safe(p, end, buf, len, bad); 482 } else { 483 ceph_encode_32_safe(p, end, 0, bad); 484 } 485 486 return 0; 487 bad: 488 return -ERANGE; 489 } 490 491 static bool need_key(struct ceph_x_ticket_handler *th) 492 { 493 if (!th->have_key) 494 return true; 495 496 return ktime_get_real_seconds() >= th->renew_after; 497 } 498 499 static bool have_key(struct ceph_x_ticket_handler *th) 500 { 501 if (th->have_key && ktime_get_real_seconds() >= th->expires) { 502 dout("ticket %d (%s) secret_id %llu expired\n", th->service, 503 ceph_entity_type_name(th->service), th->secret_id); 504 th->have_key = false; 505 } 506 507 return th->have_key; 508 } 509 510 static void ceph_x_validate_tickets(struct ceph_auth_client *ac, int *pneed) 511 { 512 int want = ac->want_keys; 513 struct ceph_x_info *xi = ac->private; 514 int service; 515 516 *pneed = ac->want_keys & ~(xi->have_keys); 517 518 for (service = 1; service <= want; service <<= 1) { 519 struct ceph_x_ticket_handler *th; 520 521 if (!(ac->want_keys & service)) 522 continue; 523 524 if (*pneed & service) 525 continue; 526 527 th = get_ticket_handler(ac, service); 528 if (IS_ERR(th)) { 529 *pneed |= service; 530 continue; 531 } 532 533 if (need_key(th)) 534 *pneed |= service; 535 if (!have_key(th)) 536 xi->have_keys &= ~service; 537 } 538 } 539 540 static int ceph_x_build_request(struct ceph_auth_client *ac, 541 void *buf, void *end) 542 { 543 struct ceph_x_info *xi = ac->private; 544 int need; 545 struct ceph_x_request_header *head = buf; 546 void *p; 547 int ret; 548 struct ceph_x_ticket_handler *th = 549 get_ticket_handler(ac, CEPH_ENTITY_TYPE_AUTH); 550 551 if (IS_ERR(th)) 552 return PTR_ERR(th); 553 554 ceph_x_validate_tickets(ac, &need); 555 dout("%s want 0x%x have 0x%x need 0x%x\n", __func__, ac->want_keys, 556 xi->have_keys, need); 557 558 if (need & CEPH_ENTITY_TYPE_AUTH) { 559 struct ceph_x_authenticate *auth = (void *)(head + 1); 560 void *enc_buf = xi->auth_authorizer.enc_buf; 561 struct ceph_x_challenge_blob *blob; 562 u64 *u; 563 564 p = auth + 1; 565 if (p > end) 566 return -ERANGE; 567 568 dout(" get_auth_session_key\n"); 569 head->op = cpu_to_le16(CEPHX_GET_AUTH_SESSION_KEY); 570 571 if (xi->secret.type == CEPH_CRYPTO_AES) { 572 blob = enc_buf + ceph_x_encrypt_offset(&xi->secret); 573 } else { 574 BUILD_BUG_ON(SHA256_DIGEST_SIZE + sizeof(*blob) > 575 CEPHX_AU_ENC_BUF_LEN); 576 blob = enc_buf + SHA256_DIGEST_SIZE; 577 } 578 579 get_random_bytes(&auth->client_challenge, sizeof(u64)); 580 blob->client_challenge = auth->client_challenge; 581 blob->server_challenge = cpu_to_le64(xi->server_challenge); 582 583 if (xi->secret.type == CEPH_CRYPTO_AES) { 584 ret = ceph_x_encrypt(&xi->secret, 0 /* dummy */, 585 enc_buf, CEPHX_AU_ENC_BUF_LEN, 586 sizeof(*blob)); 587 if (ret < 0) 588 return ret; 589 } else { 590 ceph_hmac_sha256(&xi->secret, blob, sizeof(*blob), 591 enc_buf); 592 ret = SHA256_DIGEST_SIZE; 593 } 594 595 auth->struct_v = 3; /* nautilus+ */ 596 auth->key = 0; 597 for (u = (u64 *)enc_buf; u + 1 <= (u64 *)(enc_buf + ret); u++) 598 auth->key ^= *(__le64 *)u; 599 dout(" server_challenge %llx client_challenge %llx key %llx\n", 600 xi->server_challenge, le64_to_cpu(auth->client_challenge), 601 le64_to_cpu(auth->key)); 602 603 /* now encode the old ticket if exists */ 604 ret = ceph_x_encode_ticket(th, &p, end); 605 if (ret < 0) 606 return ret; 607 608 /* nautilus+: request service tickets at the same time */ 609 need = ac->want_keys & ~CEPH_ENTITY_TYPE_AUTH; 610 WARN_ON(!need); 611 ceph_encode_32_safe(&p, end, need, e_range); 612 return p - buf; 613 } 614 615 if (need) { 616 dout(" get_principal_session_key\n"); 617 ret = ceph_x_build_authorizer(ac, th, &xi->auth_authorizer); 618 if (ret) 619 return ret; 620 621 p = buf; 622 ceph_encode_16_safe(&p, end, CEPHX_GET_PRINCIPAL_SESSION_KEY, 623 e_range); 624 ceph_encode_copy_safe(&p, end, 625 xi->auth_authorizer.buf->vec.iov_base, 626 xi->auth_authorizer.buf->vec.iov_len, e_range); 627 ceph_encode_8_safe(&p, end, 1, e_range); 628 ceph_encode_32_safe(&p, end, need, e_range); 629 return p - buf; 630 } 631 632 return 0; 633 634 e_range: 635 return -ERANGE; 636 } 637 638 static int decode_con_secret(void **p, void *end, u8 *con_secret, 639 int *con_secret_len) 640 { 641 int len; 642 643 ceph_decode_32_safe(p, end, len, bad); 644 ceph_decode_need(p, end, len, bad); 645 646 dout("%s len %d\n", __func__, len); 647 if (con_secret) { 648 if (len > CEPH_MAX_CON_SECRET_LEN) { 649 pr_err("connection secret too big %d\n", len); 650 goto bad_memzero; 651 } 652 memcpy(con_secret, *p, len); 653 *con_secret_len = len; 654 } 655 memzero_explicit(*p, len); 656 *p += len; 657 return 0; 658 659 bad_memzero: 660 memzero_explicit(*p, len); 661 bad: 662 pr_err("failed to decode connection secret\n"); 663 return -EINVAL; 664 } 665 666 static int handle_auth_session_key(struct ceph_auth_client *ac, u64 global_id, 667 void **p, void *end, 668 u8 *session_key, int *session_key_len, 669 u8 *con_secret, int *con_secret_len) 670 { 671 struct ceph_x_info *xi = ac->private; 672 struct ceph_x_ticket_handler *th; 673 void *dp, *dend; 674 int len; 675 int ret; 676 677 /* AUTH ticket */ 678 ret = ceph_x_proc_ticket_reply(ac, &xi->secret, p, end); 679 if (ret) 680 return ret; 681 682 ceph_auth_set_global_id(ac, global_id); 683 if (*p == end) { 684 /* pre-nautilus (or didn't request service tickets!) */ 685 WARN_ON(session_key || con_secret); 686 return 0; 687 } 688 689 th = get_ticket_handler(ac, CEPH_ENTITY_TYPE_AUTH); 690 if (IS_ERR(th)) 691 return PTR_ERR(th); 692 693 if (session_key) { 694 memcpy(session_key, th->session_key.key, th->session_key.len); 695 *session_key_len = th->session_key.len; 696 } 697 698 /* connection secret */ 699 ceph_decode_32_safe(p, end, len, e_inval); 700 ceph_decode_need(p, end, len, e_inval); 701 dout("%s connection secret blob len %d\n", __func__, len); 702 if (len > 0) { 703 dp = *p + ceph_x_encrypt_offset(&th->session_key); 704 ret = ceph_x_decrypt(&th->session_key, 705 2 /* CEPHX_KEY_USAGE_AUTH_CONNECTION_SECRET */, 706 p, *p + len); 707 if (ret < 0) 708 return ret; 709 710 dout("%s decrypted %d bytes\n", __func__, ret); 711 dend = dp + ret; 712 713 ret = decode_con_secret(&dp, dend, con_secret, con_secret_len); 714 if (ret) 715 return ret; 716 } 717 718 /* service tickets */ 719 ceph_decode_32_safe(p, end, len, e_inval); 720 ceph_decode_need(p, end, len, e_inval); 721 dout("%s service tickets blob len %d\n", __func__, len); 722 if (len > 0) { 723 ret = ceph_x_proc_ticket_reply(ac, &th->session_key, 724 p, *p + len); 725 if (ret) 726 return ret; 727 } 728 729 return 0; 730 731 e_inval: 732 return -EINVAL; 733 } 734 735 static int ceph_x_handle_reply(struct ceph_auth_client *ac, u64 global_id, 736 void *buf, void *end, 737 u8 *session_key, int *session_key_len, 738 u8 *con_secret, int *con_secret_len) 739 { 740 struct ceph_x_info *xi = ac->private; 741 struct ceph_x_ticket_handler *th; 742 int len = end - buf; 743 int result; 744 void *p; 745 int op; 746 int ret; 747 748 if (xi->starting) { 749 /* it's a hello */ 750 struct ceph_x_server_challenge *sc = buf; 751 752 if (len != sizeof(*sc)) 753 return -EINVAL; 754 xi->server_challenge = le64_to_cpu(sc->server_challenge); 755 dout("handle_reply got server challenge %llx\n", 756 xi->server_challenge); 757 xi->starting = false; 758 xi->have_keys &= ~CEPH_ENTITY_TYPE_AUTH; 759 return -EAGAIN; 760 } 761 762 p = buf; 763 ceph_decode_16_safe(&p, end, op, e_inval); 764 ceph_decode_32_safe(&p, end, result, e_inval); 765 dout("handle_reply op %d result %d\n", op, result); 766 switch (op) { 767 case CEPHX_GET_AUTH_SESSION_KEY: 768 /* AUTH ticket + [connection secret] + service tickets */ 769 ret = handle_auth_session_key(ac, global_id, &p, end, 770 session_key, session_key_len, 771 con_secret, con_secret_len); 772 break; 773 774 case CEPHX_GET_PRINCIPAL_SESSION_KEY: 775 th = get_ticket_handler(ac, CEPH_ENTITY_TYPE_AUTH); 776 if (IS_ERR(th)) 777 return PTR_ERR(th); 778 779 /* service tickets */ 780 ret = ceph_x_proc_ticket_reply(ac, &th->session_key, &p, end); 781 break; 782 783 default: 784 return -EINVAL; 785 } 786 if (ret) 787 return ret; 788 if (ac->want_keys == xi->have_keys) 789 return 0; 790 return -EAGAIN; 791 792 e_inval: 793 return -EINVAL; 794 } 795 796 static void ceph_x_destroy_authorizer(struct ceph_authorizer *a) 797 { 798 struct ceph_x_authorizer *au = (void *)a; 799 800 ceph_x_authorizer_cleanup(au); 801 kfree(au); 802 } 803 804 static int ceph_x_create_authorizer( 805 struct ceph_auth_client *ac, int peer_type, 806 struct ceph_auth_handshake *auth) 807 { 808 struct ceph_x_authorizer *au; 809 struct ceph_x_ticket_handler *th; 810 int ret; 811 812 th = get_ticket_handler(ac, peer_type); 813 if (IS_ERR(th)) 814 return PTR_ERR(th); 815 816 au = kzalloc_obj(*au, GFP_NOFS); 817 if (!au) 818 return -ENOMEM; 819 820 au->base.destroy = ceph_x_destroy_authorizer; 821 822 ret = ceph_x_build_authorizer(ac, th, au); 823 if (ret) { 824 kfree(au); 825 return ret; 826 } 827 828 auth->authorizer = (struct ceph_authorizer *) au; 829 auth->authorizer_buf = au->buf->vec.iov_base; 830 auth->authorizer_buf_len = au->buf->vec.iov_len; 831 auth->authorizer_reply_buf = au->enc_buf; 832 auth->authorizer_reply_buf_len = CEPHX_AU_ENC_BUF_LEN; 833 auth->sign_message = ac->ops->sign_message; 834 auth->check_message_signature = ac->ops->check_message_signature; 835 836 return 0; 837 } 838 839 static int ceph_x_update_authorizer( 840 struct ceph_auth_client *ac, int peer_type, 841 struct ceph_auth_handshake *auth) 842 { 843 struct ceph_x_authorizer *au; 844 struct ceph_x_ticket_handler *th; 845 846 th = get_ticket_handler(ac, peer_type); 847 if (IS_ERR(th)) 848 return PTR_ERR(th); 849 850 au = (struct ceph_x_authorizer *)auth->authorizer; 851 if (au->secret_id < th->secret_id) { 852 dout("ceph_x_update_authorizer service %u secret %llu < %llu\n", 853 au->service, au->secret_id, th->secret_id); 854 return ceph_x_build_authorizer(ac, th, au); 855 } 856 return 0; 857 } 858 859 /* 860 * CephXAuthorizeChallenge 861 */ 862 static int decrypt_authorizer_challenge(struct ceph_crypto_key *secret, 863 void *challenge, int challenge_len, 864 u64 *server_challenge) 865 { 866 void *dp, *dend; 867 int ret; 868 869 /* no leading len */ 870 ret = __ceph_x_decrypt(secret, 871 1 /* CEPHX_KEY_USAGE_AUTHORIZE_CHALLENGE */, 872 challenge, challenge_len); 873 if (ret < 0) 874 return ret; 875 876 dout("%s decrypted %d bytes\n", __func__, ret); 877 dp = challenge + __ceph_x_encrypt_offset(secret); 878 dend = dp + ret; 879 880 ceph_decode_skip_8(&dp, dend, e_inval); /* struct_v */ 881 ceph_decode_64_safe(&dp, dend, *server_challenge, e_inval); 882 dout("%s server_challenge %llu\n", __func__, *server_challenge); 883 return 0; 884 885 e_inval: 886 return -EINVAL; 887 } 888 889 static int ceph_x_add_authorizer_challenge(struct ceph_auth_client *ac, 890 struct ceph_authorizer *a, 891 void *challenge, int challenge_len) 892 { 893 struct ceph_x_authorizer *au = (void *)a; 894 u64 server_challenge; 895 int ret; 896 897 ret = decrypt_authorizer_challenge(&au->session_key, challenge, 898 challenge_len, &server_challenge); 899 if (ret) { 900 pr_err("failed to decrypt authorize challenge: %d", ret); 901 return ret; 902 } 903 904 ret = encrypt_authorizer(au, &server_challenge); 905 if (ret) { 906 pr_err("failed to encrypt authorizer w/ challenge: %d", ret); 907 return ret; 908 } 909 910 return 0; 911 } 912 913 /* 914 * CephXAuthorizeReply 915 */ 916 static int decrypt_authorizer_reply(struct ceph_crypto_key *secret, 917 void **p, void *end, u64 *nonce_plus_one, 918 u8 *con_secret, int *con_secret_len) 919 { 920 void *dp, *dend; 921 u8 struct_v; 922 int ret; 923 924 dp = *p + ceph_x_encrypt_offset(secret); 925 ret = ceph_x_decrypt(secret, 2 /* CEPHX_KEY_USAGE_AUTHORIZE_REPLY */, 926 p, end); 927 if (ret < 0) 928 return ret; 929 930 dout("%s decrypted %d bytes\n", __func__, ret); 931 dend = dp + ret; 932 933 ceph_decode_8_safe(&dp, dend, struct_v, e_inval); 934 ceph_decode_64_safe(&dp, dend, *nonce_plus_one, e_inval); 935 dout("%s nonce_plus_one %llu\n", __func__, *nonce_plus_one); 936 if (struct_v >= 2) { 937 ret = decode_con_secret(&dp, dend, con_secret, con_secret_len); 938 if (ret) 939 return ret; 940 } 941 942 return 0; 943 944 e_inval: 945 return -EINVAL; 946 } 947 948 static int ceph_x_verify_authorizer_reply(struct ceph_auth_client *ac, 949 struct ceph_authorizer *a, 950 void *reply, int reply_len, 951 u8 *session_key, int *session_key_len, 952 u8 *con_secret, int *con_secret_len) 953 { 954 struct ceph_x_authorizer *au = (void *)a; 955 u64 nonce_plus_one; 956 int ret; 957 958 if (session_key) { 959 memcpy(session_key, au->session_key.key, au->session_key.len); 960 *session_key_len = au->session_key.len; 961 } 962 963 ret = decrypt_authorizer_reply(&au->session_key, &reply, 964 reply + reply_len, &nonce_plus_one, 965 con_secret, con_secret_len); 966 if (ret) 967 return ret; 968 969 if (nonce_plus_one != au->nonce + 1) { 970 pr_err("failed to authenticate server\n"); 971 return -EPERM; 972 } 973 974 return 0; 975 } 976 977 static void ceph_x_reset(struct ceph_auth_client *ac) 978 { 979 struct ceph_x_info *xi = ac->private; 980 981 dout("reset\n"); 982 xi->starting = true; 983 xi->server_challenge = 0; 984 } 985 986 static void ceph_x_destroy(struct ceph_auth_client *ac) 987 { 988 struct ceph_x_info *xi = ac->private; 989 struct rb_node *p; 990 991 dout("ceph_x_destroy %p\n", ac); 992 ceph_crypto_key_destroy(&xi->secret); 993 994 while ((p = rb_first(&xi->ticket_handlers)) != NULL) { 995 struct ceph_x_ticket_handler *th = 996 rb_entry(p, struct ceph_x_ticket_handler, node); 997 remove_ticket_handler(ac, th); 998 } 999 1000 ceph_x_authorizer_cleanup(&xi->auth_authorizer); 1001 1002 kfree(ac->private); 1003 ac->private = NULL; 1004 } 1005 1006 static void invalidate_ticket(struct ceph_auth_client *ac, int peer_type) 1007 { 1008 struct ceph_x_ticket_handler *th; 1009 1010 th = get_ticket_handler(ac, peer_type); 1011 if (IS_ERR(th)) 1012 return; 1013 1014 if (th->have_key) { 1015 dout("ticket %d (%s) secret_id %llu invalidated\n", 1016 th->service, ceph_entity_type_name(th->service), 1017 th->secret_id); 1018 th->have_key = false; 1019 } 1020 } 1021 1022 static void ceph_x_invalidate_authorizer(struct ceph_auth_client *ac, 1023 int peer_type) 1024 { 1025 /* 1026 * We are to invalidate a service ticket in the hopes of 1027 * getting a new, hopefully more valid, one. But, we won't get 1028 * it unless our AUTH ticket is good, so invalidate AUTH ticket 1029 * as well, just in case. 1030 */ 1031 invalidate_ticket(ac, peer_type); 1032 invalidate_ticket(ac, CEPH_ENTITY_TYPE_AUTH); 1033 } 1034 1035 static int calc_signature(struct ceph_x_authorizer *au, struct ceph_msg *msg, 1036 __le64 *psig) 1037 { 1038 void *enc_buf = au->enc_buf; 1039 int ret; 1040 1041 if (!CEPH_HAVE_FEATURE(msg->con->peer_features, CEPHX_V2)) { 1042 struct { 1043 __le32 len; 1044 __le32 header_crc; 1045 __le32 front_crc; 1046 __le32 middle_crc; 1047 __le32 data_crc; 1048 } __packed *sigblock = enc_buf + 1049 ceph_x_encrypt_offset(&au->session_key); 1050 1051 sigblock->len = cpu_to_le32(4*sizeof(u32)); 1052 sigblock->header_crc = msg->hdr.crc; 1053 sigblock->front_crc = msg->footer.front_crc; 1054 sigblock->middle_crc = msg->footer.middle_crc; 1055 sigblock->data_crc = msg->footer.data_crc; 1056 1057 ret = ceph_x_encrypt(&au->session_key, 0 /* dummy */, 1058 enc_buf, CEPHX_AU_ENC_BUF_LEN, 1059 sizeof(*sigblock)); 1060 if (ret < 0) 1061 return ret; 1062 1063 *psig = *(__le64 *)(enc_buf + sizeof(u32)); 1064 } else { 1065 struct { 1066 __le32 header_crc; 1067 __le32 front_crc; 1068 __le32 front_len; 1069 __le32 middle_crc; 1070 __le32 middle_len; 1071 __le32 data_crc; 1072 __le32 data_len; 1073 __le32 seq_lower_word; 1074 } __packed *sigblock; 1075 struct { 1076 __le64 a, b, c, d; 1077 } __packed *penc = enc_buf; 1078 1079 if (au->session_key.type == CEPH_CRYPTO_AES) { 1080 /* no leading len, no ceph_x_encrypt_header */ 1081 sigblock = enc_buf; 1082 } else { 1083 BUILD_BUG_ON(SHA256_DIGEST_SIZE + sizeof(*sigblock) > 1084 CEPHX_AU_ENC_BUF_LEN); 1085 sigblock = enc_buf + SHA256_DIGEST_SIZE; 1086 } 1087 1088 sigblock->header_crc = msg->hdr.crc; 1089 sigblock->front_crc = msg->footer.front_crc; 1090 sigblock->front_len = msg->hdr.front_len; 1091 sigblock->middle_crc = msg->footer.middle_crc; 1092 sigblock->middle_len = msg->hdr.middle_len; 1093 sigblock->data_crc = msg->footer.data_crc; 1094 sigblock->data_len = msg->hdr.data_len; 1095 sigblock->seq_lower_word = *(__le32 *)&msg->hdr.seq; 1096 1097 if (au->session_key.type == CEPH_CRYPTO_AES) { 1098 int ciphertext_len; /* unused */ 1099 1100 ret = ceph_crypt(&au->session_key, 0 /* dummy */, 1101 true, enc_buf, CEPHX_AU_ENC_BUF_LEN, 1102 sizeof(*sigblock), &ciphertext_len); 1103 if (ret) 1104 return ret; 1105 } else { 1106 ceph_hmac_sha256(&au->session_key, sigblock, 1107 sizeof(*sigblock), enc_buf); 1108 } 1109 1110 *psig = penc->a ^ penc->b ^ penc->c ^ penc->d; 1111 } 1112 1113 return 0; 1114 } 1115 1116 static int ceph_x_sign_message(struct ceph_auth_handshake *auth, 1117 struct ceph_msg *msg) 1118 { 1119 __le64 sig; 1120 int ret; 1121 1122 if (ceph_test_opt(from_msgr(msg->con->msgr), NOMSGSIGN)) 1123 return 0; 1124 1125 ret = calc_signature((struct ceph_x_authorizer *)auth->authorizer, 1126 msg, &sig); 1127 if (ret) 1128 return ret; 1129 1130 msg->footer.sig = sig; 1131 msg->footer.flags |= CEPH_MSG_FOOTER_SIGNED; 1132 return 0; 1133 } 1134 1135 static int ceph_x_check_message_signature(struct ceph_auth_handshake *auth, 1136 struct ceph_msg *msg) 1137 { 1138 __le64 sig_check; 1139 int ret; 1140 1141 if (ceph_test_opt(from_msgr(msg->con->msgr), NOMSGSIGN)) 1142 return 0; 1143 1144 ret = calc_signature((struct ceph_x_authorizer *)auth->authorizer, 1145 msg, &sig_check); 1146 if (ret) 1147 return ret; 1148 if (sig_check == msg->footer.sig) 1149 return 0; 1150 if (msg->footer.flags & CEPH_MSG_FOOTER_SIGNED) 1151 dout("ceph_x_check_message_signature %p has signature %llx " 1152 "expect %llx\n", msg, msg->footer.sig, sig_check); 1153 else 1154 dout("ceph_x_check_message_signature %p sender did not set " 1155 "CEPH_MSG_FOOTER_SIGNED\n", msg); 1156 return -EBADMSG; 1157 } 1158 1159 static const struct ceph_auth_client_ops ceph_x_ops = { 1160 .is_authenticated = ceph_x_is_authenticated, 1161 .should_authenticate = ceph_x_should_authenticate, 1162 .build_request = ceph_x_build_request, 1163 .handle_reply = ceph_x_handle_reply, 1164 .create_authorizer = ceph_x_create_authorizer, 1165 .update_authorizer = ceph_x_update_authorizer, 1166 .add_authorizer_challenge = ceph_x_add_authorizer_challenge, 1167 .verify_authorizer_reply = ceph_x_verify_authorizer_reply, 1168 .invalidate_authorizer = ceph_x_invalidate_authorizer, 1169 .reset = ceph_x_reset, 1170 .destroy = ceph_x_destroy, 1171 .sign_message = ceph_x_sign_message, 1172 .check_message_signature = ceph_x_check_message_signature, 1173 }; 1174 1175 1176 int ceph_x_init(struct ceph_auth_client *ac) 1177 { 1178 struct ceph_x_info *xi; 1179 int ret; 1180 1181 dout("ceph_x_init %p\n", ac); 1182 xi = kzalloc_obj(*xi, GFP_NOFS); 1183 if (!xi) 1184 return -ENOMEM; 1185 1186 ret = -EINVAL; 1187 if (!ac->key) { 1188 pr_err("no secret set (for auth_x protocol)\n"); 1189 goto err_xi; 1190 } 1191 1192 ret = ceph_crypto_key_clone(&xi->secret, ac->key); 1193 if (ret < 0) { 1194 pr_err("cannot clone key: %d\n", ret); 1195 goto err_xi; 1196 } 1197 1198 ret = ceph_crypto_key_prepare(&xi->secret, client_key_usages, 1199 ARRAY_SIZE(client_key_usages)); 1200 if (ret) { 1201 pr_err("cannot prepare key: %d\n", ret); 1202 goto err_secret; 1203 } 1204 1205 xi->starting = true; 1206 xi->ticket_handlers = RB_ROOT; 1207 1208 ac->protocol = CEPH_AUTH_CEPHX; 1209 ac->private = xi; 1210 ac->ops = &ceph_x_ops; 1211 return 0; 1212 1213 err_secret: 1214 ceph_crypto_key_destroy(&xi->secret); 1215 err_xi: 1216 kfree(xi); 1217 return ret; 1218 } 1219