diff options
Diffstat (limited to 'fs/ecryptfs/keystore.c')
| -rw-r--r-- | fs/ecryptfs/keystore.c | 1078 |
1 files changed, 611 insertions, 467 deletions
diff --git a/fs/ecryptfs/keystore.c b/fs/ecryptfs/keystore.c index b550dea8eee6..89d9710dd63d 100644 --- a/fs/ecryptfs/keystore.c +++ b/fs/ecryptfs/keystore.c | |||
| @@ -39,7 +39,7 @@ | |||
| 39 | * determine the type of error, make appropriate log entries, and | 39 | * determine the type of error, make appropriate log entries, and |
| 40 | * return an error code. | 40 | * return an error code. |
| 41 | */ | 41 | */ |
| 42 | int process_request_key_err(long err_code) | 42 | static int process_request_key_err(long err_code) |
| 43 | { | 43 | { |
| 44 | int rc = 0; | 44 | int rc = 0; |
| 45 | 45 | ||
| @@ -71,7 +71,7 @@ int process_request_key_err(long err_code) | |||
| 71 | * address; zero on error | 71 | * address; zero on error |
| 72 | * @length_size: The number of bytes occupied by the encoded length | 72 | * @length_size: The number of bytes occupied by the encoded length |
| 73 | * | 73 | * |
| 74 | * Returns Zero on success | 74 | * Returns zero on success; non-zero on error |
| 75 | */ | 75 | */ |
| 76 | static int parse_packet_length(unsigned char *data, size_t *size, | 76 | static int parse_packet_length(unsigned char *data, size_t *size, |
| 77 | size_t *length_size) | 77 | size_t *length_size) |
| @@ -106,11 +106,11 @@ out: | |||
| 106 | 106 | ||
| 107 | /** | 107 | /** |
| 108 | * write_packet_length | 108 | * write_packet_length |
| 109 | * @dest: The byte array target into which to write the | 109 | * @dest: The byte array target into which to write the length. Must |
| 110 | * length. Must have at least 5 bytes allocated. | 110 | * have at least 5 bytes allocated. |
| 111 | * @size: The length to write. | 111 | * @size: The length to write. |
| 112 | * @packet_size_length: The number of bytes used to encode the | 112 | * @packet_size_length: The number of bytes used to encode the packet |
| 113 | * packet length is written to this address. | 113 | * length is written to this address. |
| 114 | * | 114 | * |
| 115 | * Returns zero on success; non-zero on error. | 115 | * Returns zero on success; non-zero on error. |
| 116 | */ | 116 | */ |
| @@ -396,26 +396,53 @@ out: | |||
| 396 | return rc; | 396 | return rc; |
| 397 | } | 397 | } |
| 398 | 398 | ||
| 399 | static int | ||
| 400 | ecryptfs_get_auth_tok_sig(char **sig, struct ecryptfs_auth_tok *auth_tok) | ||
| 401 | { | ||
| 402 | int rc = 0; | ||
| 403 | |||
| 404 | (*sig) = NULL; | ||
| 405 | switch (auth_tok->token_type) { | ||
| 406 | case ECRYPTFS_PASSWORD: | ||
| 407 | (*sig) = auth_tok->token.password.signature; | ||
| 408 | break; | ||
| 409 | case ECRYPTFS_PRIVATE_KEY: | ||
| 410 | (*sig) = auth_tok->token.private_key.signature; | ||
| 411 | break; | ||
| 412 | default: | ||
| 413 | printk(KERN_ERR "Cannot get sig for auth_tok of type [%d]\n", | ||
| 414 | auth_tok->token_type); | ||
| 415 | rc = -EINVAL; | ||
| 416 | } | ||
| 417 | return rc; | ||
| 418 | } | ||
| 419 | |||
| 399 | /** | 420 | /** |
| 400 | * decrypt_pki_encrypted_session_key - Decrypt the session key with | 421 | * decrypt_pki_encrypted_session_key - Decrypt the session key with the given auth_tok. |
| 401 | * the given auth_tok. | 422 | * @auth_tok: The key authentication token used to decrypt the session key |
| 423 | * @crypt_stat: The cryptographic context | ||
| 402 | * | 424 | * |
| 403 | * Returns Zero on success; non-zero error otherwise. | 425 | * Returns zero on success; non-zero error otherwise. |
| 404 | */ | 426 | */ |
| 405 | static int decrypt_pki_encrypted_session_key( | 427 | static int |
| 406 | struct ecryptfs_mount_crypt_stat *mount_crypt_stat, | 428 | decrypt_pki_encrypted_session_key(struct ecryptfs_auth_tok *auth_tok, |
| 407 | struct ecryptfs_auth_tok *auth_tok, | 429 | struct ecryptfs_crypt_stat *crypt_stat) |
| 408 | struct ecryptfs_crypt_stat *crypt_stat) | ||
| 409 | { | 430 | { |
| 410 | u16 cipher_code = 0; | 431 | u16 cipher_code = 0; |
| 411 | struct ecryptfs_msg_ctx *msg_ctx; | 432 | struct ecryptfs_msg_ctx *msg_ctx; |
| 412 | struct ecryptfs_message *msg = NULL; | 433 | struct ecryptfs_message *msg = NULL; |
| 434 | char *auth_tok_sig; | ||
| 413 | char *netlink_message; | 435 | char *netlink_message; |
| 414 | size_t netlink_message_length; | 436 | size_t netlink_message_length; |
| 415 | int rc; | 437 | int rc; |
| 416 | 438 | ||
| 417 | rc = write_tag_64_packet(mount_crypt_stat->global_auth_tok_sig, | 439 | rc = ecryptfs_get_auth_tok_sig(&auth_tok_sig, auth_tok); |
| 418 | &(auth_tok->session_key), | 440 | if (rc) { |
| 441 | printk(KERN_ERR "Unrecognized auth tok type: [%d]\n", | ||
| 442 | auth_tok->token_type); | ||
| 443 | goto out; | ||
| 444 | } | ||
| 445 | rc = write_tag_64_packet(auth_tok_sig, &(auth_tok->session_key), | ||
| 419 | &netlink_message, &netlink_message_length); | 446 | &netlink_message, &netlink_message_length); |
| 420 | if (rc) { | 447 | if (rc) { |
| 421 | ecryptfs_printk(KERN_ERR, "Failed to write tag 64 packet"); | 448 | ecryptfs_printk(KERN_ERR, "Failed to write tag 64 packet"); |
| @@ -465,40 +492,33 @@ out: | |||
| 465 | 492 | ||
| 466 | static void wipe_auth_tok_list(struct list_head *auth_tok_list_head) | 493 | static void wipe_auth_tok_list(struct list_head *auth_tok_list_head) |
| 467 | { | 494 | { |
| 468 | struct list_head *walker; | ||
| 469 | struct ecryptfs_auth_tok_list_item *auth_tok_list_item; | 495 | struct ecryptfs_auth_tok_list_item *auth_tok_list_item; |
| 496 | struct ecryptfs_auth_tok_list_item *auth_tok_list_item_tmp; | ||
| 470 | 497 | ||
| 471 | walker = auth_tok_list_head->next; | 498 | list_for_each_entry_safe(auth_tok_list_item, auth_tok_list_item_tmp, |
| 472 | while (walker != auth_tok_list_head) { | 499 | auth_tok_list_head, list) { |
| 473 | auth_tok_list_item = | 500 | list_del(&auth_tok_list_item->list); |
| 474 | list_entry(walker, struct ecryptfs_auth_tok_list_item, | ||
| 475 | list); | ||
| 476 | walker = auth_tok_list_item->list.next; | ||
| 477 | memset(auth_tok_list_item, 0, | ||
| 478 | sizeof(struct ecryptfs_auth_tok_list_item)); | ||
| 479 | kmem_cache_free(ecryptfs_auth_tok_list_item_cache, | 501 | kmem_cache_free(ecryptfs_auth_tok_list_item_cache, |
| 480 | auth_tok_list_item); | 502 | auth_tok_list_item); |
| 481 | } | 503 | } |
| 482 | auth_tok_list_head->next = NULL; | ||
| 483 | } | 504 | } |
| 484 | 505 | ||
| 485 | struct kmem_cache *ecryptfs_auth_tok_list_item_cache; | 506 | struct kmem_cache *ecryptfs_auth_tok_list_item_cache; |
| 486 | 507 | ||
| 487 | |||
| 488 | /** | 508 | /** |
| 489 | * parse_tag_1_packet | 509 | * parse_tag_1_packet |
| 490 | * @crypt_stat: The cryptographic context to modify based on packet | 510 | * @crypt_stat: The cryptographic context to modify based on packet contents |
| 491 | * contents. | ||
| 492 | * @data: The raw bytes of the packet. | 511 | * @data: The raw bytes of the packet. |
| 493 | * @auth_tok_list: eCryptfs parses packets into authentication tokens; | 512 | * @auth_tok_list: eCryptfs parses packets into authentication tokens; |
| 494 | * a new authentication token will be placed at the end | 513 | * a new authentication token will be placed at the |
| 495 | * of this list for this packet. | 514 | * end of this list for this packet. |
| 496 | * @new_auth_tok: Pointer to a pointer to memory that this function | 515 | * @new_auth_tok: Pointer to a pointer to memory that this function |
| 497 | * allocates; sets the memory address of the pointer to | 516 | * allocates; sets the memory address of the pointer to |
| 498 | * NULL on error. This object is added to the | 517 | * NULL on error. This object is added to the |
| 499 | * auth_tok_list. | 518 | * auth_tok_list. |
| 500 | * @packet_size: This function writes the size of the parsed packet | 519 | * @packet_size: This function writes the size of the parsed packet |
| 501 | * into this memory location; zero on error. | 520 | * into this memory location; zero on error. |
| 521 | * @max_packet_size: The maximum allowable packet size | ||
| 502 | * | 522 | * |
| 503 | * Returns zero on success; non-zero on error. | 523 | * Returns zero on success; non-zero on error. |
| 504 | */ | 524 | */ |
| @@ -515,72 +535,65 @@ parse_tag_1_packet(struct ecryptfs_crypt_stat *crypt_stat, | |||
| 515 | 535 | ||
| 516 | (*packet_size) = 0; | 536 | (*packet_size) = 0; |
| 517 | (*new_auth_tok) = NULL; | 537 | (*new_auth_tok) = NULL; |
| 518 | 538 | /** | |
| 519 | /* we check that: | 539 | * This format is inspired by OpenPGP; see RFC 2440 |
| 520 | * one byte for the Tag 1 ID flag | 540 | * packet tag 1 |
| 521 | * two bytes for the body size | 541 | * |
| 522 | * do not exceed the maximum_packet_size | 542 | * Tag 1 identifier (1 byte) |
| 543 | * Max Tag 1 packet size (max 3 bytes) | ||
| 544 | * Version (1 byte) | ||
| 545 | * Key identifier (8 bytes; ECRYPTFS_SIG_SIZE) | ||
| 546 | * Cipher identifier (1 byte) | ||
| 547 | * Encrypted key size (arbitrary) | ||
| 548 | * | ||
| 549 | * 12 bytes minimum packet size | ||
| 523 | */ | 550 | */ |
| 524 | if (unlikely((*packet_size) + 3 > max_packet_size)) { | 551 | if (unlikely(max_packet_size < 12)) { |
| 525 | ecryptfs_printk(KERN_ERR, "Packet size exceeds max\n"); | 552 | printk(KERN_ERR "Invalid max packet size; must be >=12\n"); |
| 526 | rc = -EINVAL; | 553 | rc = -EINVAL; |
| 527 | goto out; | 554 | goto out; |
| 528 | } | 555 | } |
| 529 | /* check for Tag 1 identifier - one byte */ | ||
| 530 | if (data[(*packet_size)++] != ECRYPTFS_TAG_1_PACKET_TYPE) { | 556 | if (data[(*packet_size)++] != ECRYPTFS_TAG_1_PACKET_TYPE) { |
| 531 | ecryptfs_printk(KERN_ERR, "Enter w/ first byte != 0x%.2x\n", | 557 | printk(KERN_ERR "Enter w/ first byte != 0x%.2x\n", |
| 532 | ECRYPTFS_TAG_1_PACKET_TYPE); | 558 | ECRYPTFS_TAG_1_PACKET_TYPE); |
| 533 | rc = -EINVAL; | 559 | rc = -EINVAL; |
| 534 | goto out; | 560 | goto out; |
| 535 | } | 561 | } |
| 536 | /* Released: wipe_auth_tok_list called in ecryptfs_parse_packet_set or | 562 | /* Released: wipe_auth_tok_list called in ecryptfs_parse_packet_set or |
| 537 | * at end of function upon failure */ | 563 | * at end of function upon failure */ |
| 538 | auth_tok_list_item = | 564 | auth_tok_list_item = |
| 539 | kmem_cache_alloc(ecryptfs_auth_tok_list_item_cache, | 565 | kmem_cache_zalloc(ecryptfs_auth_tok_list_item_cache, |
| 540 | GFP_KERNEL); | 566 | GFP_KERNEL); |
| 541 | if (!auth_tok_list_item) { | 567 | if (!auth_tok_list_item) { |
| 542 | ecryptfs_printk(KERN_ERR, "Unable to allocate memory\n"); | 568 | printk(KERN_ERR "Unable to allocate memory\n"); |
| 543 | rc = -ENOMEM; | 569 | rc = -ENOMEM; |
| 544 | goto out; | 570 | goto out; |
| 545 | } | 571 | } |
| 546 | memset(auth_tok_list_item, 0, | ||
| 547 | sizeof(struct ecryptfs_auth_tok_list_item)); | ||
| 548 | (*new_auth_tok) = &auth_tok_list_item->auth_tok; | 572 | (*new_auth_tok) = &auth_tok_list_item->auth_tok; |
| 549 | /* check for body size - one to two bytes | ||
| 550 | * | ||
| 551 | * ***** TAG 1 Packet Format ***** | ||
| 552 | * | version number | 1 byte | | ||
| 553 | * | key ID | 8 bytes | | ||
| 554 | * | public key algorithm | 1 byte | | ||
| 555 | * | encrypted session key | arbitrary | | ||
| 556 | */ | ||
| 557 | rc = parse_packet_length(&data[(*packet_size)], &body_size, | 573 | rc = parse_packet_length(&data[(*packet_size)], &body_size, |
| 558 | &length_size); | 574 | &length_size); |
| 559 | if (rc) { | 575 | if (rc) { |
| 560 | ecryptfs_printk(KERN_WARNING, "Error parsing packet length; " | 576 | printk(KERN_WARNING "Error parsing packet length; " |
| 561 | "rc = [%d]\n", rc); | 577 | "rc = [%d]\n", rc); |
| 562 | goto out_free; | 578 | goto out_free; |
| 563 | } | 579 | } |
| 564 | if (unlikely(body_size < (0x02 + ECRYPTFS_SIG_SIZE))) { | 580 | if (unlikely(body_size < (ECRYPTFS_SIG_SIZE + 2))) { |
| 565 | ecryptfs_printk(KERN_WARNING, "Invalid body size ([%d])\n", | 581 | printk(KERN_WARNING "Invalid body size ([%td])\n", body_size); |
| 566 | body_size); | ||
| 567 | rc = -EINVAL; | 582 | rc = -EINVAL; |
| 568 | goto out_free; | 583 | goto out_free; |
| 569 | } | 584 | } |
| 570 | (*packet_size) += length_size; | 585 | (*packet_size) += length_size; |
| 571 | if (unlikely((*packet_size) + body_size > max_packet_size)) { | 586 | if (unlikely((*packet_size) + body_size > max_packet_size)) { |
| 572 | ecryptfs_printk(KERN_ERR, "Packet size exceeds max\n"); | 587 | printk(KERN_WARNING "Packet size exceeds max\n"); |
| 573 | rc = -EINVAL; | 588 | rc = -EINVAL; |
| 574 | goto out_free; | 589 | goto out_free; |
| 575 | } | 590 | } |
| 576 | /* Version 3 (from RFC2440) - one byte */ | ||
| 577 | if (unlikely(data[(*packet_size)++] != 0x03)) { | 591 | if (unlikely(data[(*packet_size)++] != 0x03)) { |
| 578 | ecryptfs_printk(KERN_DEBUG, "Unknown version number " | 592 | printk(KERN_WARNING "Unknown version number [%d]\n", |
| 579 | "[%d]\n", data[(*packet_size) - 1]); | 593 | data[(*packet_size) - 1]); |
| 580 | rc = -EINVAL; | 594 | rc = -EINVAL; |
| 581 | goto out_free; | 595 | goto out_free; |
| 582 | } | 596 | } |
| 583 | /* Read Signature */ | ||
| 584 | ecryptfs_to_hex((*new_auth_tok)->token.private_key.signature, | 597 | ecryptfs_to_hex((*new_auth_tok)->token.private_key.signature, |
| 585 | &data[(*packet_size)], ECRYPTFS_SIG_SIZE); | 598 | &data[(*packet_size)], ECRYPTFS_SIG_SIZE); |
| 586 | *packet_size += ECRYPTFS_SIG_SIZE; | 599 | *packet_size += ECRYPTFS_SIG_SIZE; |
| @@ -588,27 +601,23 @@ parse_tag_1_packet(struct ecryptfs_crypt_stat *crypt_stat, | |||
| 588 | * know which public key encryption algorithm was used */ | 601 | * know which public key encryption algorithm was used */ |
| 589 | (*packet_size)++; | 602 | (*packet_size)++; |
| 590 | (*new_auth_tok)->session_key.encrypted_key_size = | 603 | (*new_auth_tok)->session_key.encrypted_key_size = |
| 591 | body_size - (0x02 + ECRYPTFS_SIG_SIZE); | 604 | body_size - (ECRYPTFS_SIG_SIZE + 2); |
| 592 | if ((*new_auth_tok)->session_key.encrypted_key_size | 605 | if ((*new_auth_tok)->session_key.encrypted_key_size |
| 593 | > ECRYPTFS_MAX_ENCRYPTED_KEY_BYTES) { | 606 | > ECRYPTFS_MAX_ENCRYPTED_KEY_BYTES) { |
| 594 | ecryptfs_printk(KERN_ERR, "Tag 1 packet contains key larger " | 607 | printk(KERN_WARNING "Tag 1 packet contains key larger " |
| 595 | "than ECRYPTFS_MAX_ENCRYPTED_KEY_BYTES"); | 608 | "than ECRYPTFS_MAX_ENCRYPTED_KEY_BYTES"); |
| 596 | rc = -EINVAL; | 609 | rc = -EINVAL; |
| 597 | goto out; | 610 | goto out; |
| 598 | } | 611 | } |
| 599 | ecryptfs_printk(KERN_DEBUG, "Encrypted key size = [%d]\n", | ||
| 600 | (*new_auth_tok)->session_key.encrypted_key_size); | ||
| 601 | memcpy((*new_auth_tok)->session_key.encrypted_key, | 612 | memcpy((*new_auth_tok)->session_key.encrypted_key, |
| 602 | &data[(*packet_size)], (body_size - 0x02 - ECRYPTFS_SIG_SIZE)); | 613 | &data[(*packet_size)], (body_size - (ECRYPTFS_SIG_SIZE + 2))); |
| 603 | (*packet_size) += (*new_auth_tok)->session_key.encrypted_key_size; | 614 | (*packet_size) += (*new_auth_tok)->session_key.encrypted_key_size; |
| 604 | (*new_auth_tok)->session_key.flags &= | 615 | (*new_auth_tok)->session_key.flags &= |
| 605 | ~ECRYPTFS_CONTAINS_DECRYPTED_KEY; | 616 | ~ECRYPTFS_CONTAINS_DECRYPTED_KEY; |
| 606 | (*new_auth_tok)->session_key.flags |= | 617 | (*new_auth_tok)->session_key.flags |= |
| 607 | ECRYPTFS_CONTAINS_ENCRYPTED_KEY; | 618 | ECRYPTFS_CONTAINS_ENCRYPTED_KEY; |
| 608 | (*new_auth_tok)->token_type = ECRYPTFS_PRIVATE_KEY; | 619 | (*new_auth_tok)->token_type = ECRYPTFS_PRIVATE_KEY; |
| 609 | (*new_auth_tok)->flags |= ECRYPTFS_PRIVATE_KEY; | 620 | (*new_auth_tok)->flags = 0; |
| 610 | /* TODO: Why are we setting this flag here? Don't we want the | ||
| 611 | * userspace to decrypt the session key? */ | ||
| 612 | (*new_auth_tok)->session_key.flags &= | 621 | (*new_auth_tok)->session_key.flags &= |
| 613 | ~(ECRYPTFS_USERSPACE_SHOULD_TRY_TO_DECRYPT); | 622 | ~(ECRYPTFS_USERSPACE_SHOULD_TRY_TO_DECRYPT); |
| 614 | (*new_auth_tok)->session_key.flags &= | 623 | (*new_auth_tok)->session_key.flags &= |
| @@ -658,22 +667,30 @@ parse_tag_3_packet(struct ecryptfs_crypt_stat *crypt_stat, | |||
| 658 | 667 | ||
| 659 | (*packet_size) = 0; | 668 | (*packet_size) = 0; |
| 660 | (*new_auth_tok) = NULL; | 669 | (*new_auth_tok) = NULL; |
| 661 | 670 | /** | |
| 662 | /* we check that: | 671 | *This format is inspired by OpenPGP; see RFC 2440 |
| 663 | * one byte for the Tag 3 ID flag | 672 | * packet tag 3 |
| 664 | * two bytes for the body size | 673 | * |
| 665 | * do not exceed the maximum_packet_size | 674 | * Tag 3 identifier (1 byte) |
| 675 | * Max Tag 3 packet size (max 3 bytes) | ||
| 676 | * Version (1 byte) | ||
| 677 | * Cipher code (1 byte) | ||
| 678 | * S2K specifier (1 byte) | ||
| 679 | * Hash identifier (1 byte) | ||
| 680 | * Salt (ECRYPTFS_SALT_SIZE) | ||
| 681 | * Hash iterations (1 byte) | ||
| 682 | * Encrypted key (arbitrary) | ||
| 683 | * | ||
| 684 | * (ECRYPTFS_SALT_SIZE + 7) minimum packet size | ||
| 666 | */ | 685 | */ |
| 667 | if (unlikely((*packet_size) + 3 > max_packet_size)) { | 686 | if (max_packet_size < (ECRYPTFS_SALT_SIZE + 7)) { |
| 668 | ecryptfs_printk(KERN_ERR, "Packet size exceeds max\n"); | 687 | printk(KERN_ERR "Max packet size too large\n"); |
| 669 | rc = -EINVAL; | 688 | rc = -EINVAL; |
| 670 | goto out; | 689 | goto out; |
| 671 | } | 690 | } |
| 672 | |||
| 673 | /* check for Tag 3 identifyer - one byte */ | ||
| 674 | if (data[(*packet_size)++] != ECRYPTFS_TAG_3_PACKET_TYPE) { | 691 | if (data[(*packet_size)++] != ECRYPTFS_TAG_3_PACKET_TYPE) { |
| 675 | ecryptfs_printk(KERN_ERR, "Enter w/ first byte != 0x%.2x\n", | 692 | printk(KERN_ERR "First byte != 0x%.2x; invalid packet\n", |
| 676 | ECRYPTFS_TAG_3_PACKET_TYPE); | 693 | ECRYPTFS_TAG_3_PACKET_TYPE); |
| 677 | rc = -EINVAL; | 694 | rc = -EINVAL; |
| 678 | goto out; | 695 | goto out; |
| 679 | } | 696 | } |
| @@ -682,56 +699,37 @@ parse_tag_3_packet(struct ecryptfs_crypt_stat *crypt_stat, | |||
| 682 | auth_tok_list_item = | 699 | auth_tok_list_item = |
| 683 | kmem_cache_zalloc(ecryptfs_auth_tok_list_item_cache, GFP_KERNEL); | 700 | kmem_cache_zalloc(ecryptfs_auth_tok_list_item_cache, GFP_KERNEL); |
| 684 | if (!auth_tok_list_item) { | 701 | if (!auth_tok_list_item) { |
| 685 | ecryptfs_printk(KERN_ERR, "Unable to allocate memory\n"); | 702 | printk(KERN_ERR "Unable to allocate memory\n"); |
| 686 | rc = -ENOMEM; | 703 | rc = -ENOMEM; |
| 687 | goto out; | 704 | goto out; |
| 688 | } | 705 | } |
| 689 | (*new_auth_tok) = &auth_tok_list_item->auth_tok; | 706 | (*new_auth_tok) = &auth_tok_list_item->auth_tok; |
| 690 | |||
| 691 | /* check for body size - one to two bytes */ | ||
| 692 | rc = parse_packet_length(&data[(*packet_size)], &body_size, | 707 | rc = parse_packet_length(&data[(*packet_size)], &body_size, |
| 693 | &length_size); | 708 | &length_size); |
| 694 | if (rc) { | 709 | if (rc) { |
| 695 | ecryptfs_printk(KERN_WARNING, "Error parsing packet length; " | 710 | printk(KERN_WARNING "Error parsing packet length; rc = [%d]\n", |
| 696 | "rc = [%d]\n", rc); | 711 | rc); |
| 697 | goto out_free; | 712 | goto out_free; |
| 698 | } | 713 | } |
| 699 | if (unlikely(body_size < (0x05 + ECRYPTFS_SALT_SIZE))) { | 714 | if (unlikely(body_size < (ECRYPTFS_SALT_SIZE + 5))) { |
| 700 | ecryptfs_printk(KERN_WARNING, "Invalid body size ([%d])\n", | 715 | printk(KERN_WARNING "Invalid body size ([%td])\n", body_size); |
| 701 | body_size); | ||
| 702 | rc = -EINVAL; | 716 | rc = -EINVAL; |
| 703 | goto out_free; | 717 | goto out_free; |
| 704 | } | 718 | } |
| 705 | (*packet_size) += length_size; | 719 | (*packet_size) += length_size; |
| 706 | |||
| 707 | /* now we know the length of the remainting Tag 3 packet size: | ||
| 708 | * 5 fix bytes for: version string, cipher, S2K ID, hash algo, | ||
| 709 | * number of hash iterations | ||
| 710 | * ECRYPTFS_SALT_SIZE bytes for salt | ||
| 711 | * body_size bytes minus the stuff above is the encrypted key size | ||
| 712 | */ | ||
| 713 | if (unlikely((*packet_size) + body_size > max_packet_size)) { | 720 | if (unlikely((*packet_size) + body_size > max_packet_size)) { |
| 714 | ecryptfs_printk(KERN_ERR, "Packet size exceeds max\n"); | 721 | printk(KERN_ERR "Packet size exceeds max\n"); |
| 715 | rc = -EINVAL; | 722 | rc = -EINVAL; |
| 716 | goto out_free; | 723 | goto out_free; |
| 717 | } | 724 | } |
| 718 | |||
| 719 | /* There are 5 characters of additional information in the | ||
| 720 | * packet */ | ||
| 721 | (*new_auth_tok)->session_key.encrypted_key_size = | 725 | (*new_auth_tok)->session_key.encrypted_key_size = |
| 722 | body_size - (0x05 + ECRYPTFS_SALT_SIZE); | 726 | (body_size - (ECRYPTFS_SALT_SIZE + 5)); |
| 723 | ecryptfs_printk(KERN_DEBUG, "Encrypted key size = [%d]\n", | ||
| 724 | (*new_auth_tok)->session_key.encrypted_key_size); | ||
| 725 | |||
| 726 | /* Version 4 (from RFC2440) - one byte */ | ||
| 727 | if (unlikely(data[(*packet_size)++] != 0x04)) { | 727 | if (unlikely(data[(*packet_size)++] != 0x04)) { |
| 728 | ecryptfs_printk(KERN_DEBUG, "Unknown version number " | 728 | printk(KERN_WARNING "Unknown version number [%d]\n", |
| 729 | "[%d]\n", data[(*packet_size) - 1]); | 729 | data[(*packet_size) - 1]); |
| 730 | rc = -EINVAL; | 730 | rc = -EINVAL; |
| 731 | goto out_free; | 731 | goto out_free; |
| 732 | } | 732 | } |
| 733 | |||
| 734 | /* cipher - one byte */ | ||
| 735 | ecryptfs_cipher_code_to_string(crypt_stat->cipher, | 733 | ecryptfs_cipher_code_to_string(crypt_stat->cipher, |
| 736 | (u16)data[(*packet_size)]); | 734 | (u16)data[(*packet_size)]); |
| 737 | /* A little extra work to differentiate among the AES key | 735 | /* A little extra work to differentiate among the AES key |
| @@ -745,33 +743,26 @@ parse_tag_3_packet(struct ecryptfs_crypt_stat *crypt_stat, | |||
| 745 | (*new_auth_tok)->session_key.encrypted_key_size; | 743 | (*new_auth_tok)->session_key.encrypted_key_size; |
| 746 | } | 744 | } |
| 747 | ecryptfs_init_crypt_ctx(crypt_stat); | 745 | ecryptfs_init_crypt_ctx(crypt_stat); |
| 748 | /* S2K identifier 3 (from RFC2440) */ | ||
| 749 | if (unlikely(data[(*packet_size)++] != 0x03)) { | 746 | if (unlikely(data[(*packet_size)++] != 0x03)) { |
| 750 | ecryptfs_printk(KERN_ERR, "Only S2K ID 3 is currently " | 747 | printk(KERN_WARNING "Only S2K ID 3 is currently supported\n"); |
| 751 | "supported\n"); | ||
| 752 | rc = -ENOSYS; | 748 | rc = -ENOSYS; |
| 753 | goto out_free; | 749 | goto out_free; |
| 754 | } | 750 | } |
| 755 | |||
| 756 | /* TODO: finish the hash mapping */ | 751 | /* TODO: finish the hash mapping */ |
| 757 | /* hash algorithm - one byte */ | ||
| 758 | switch (data[(*packet_size)++]) { | 752 | switch (data[(*packet_size)++]) { |
| 759 | case 0x01: /* See RFC2440 for these numbers and their mappings */ | 753 | case 0x01: /* See RFC2440 for these numbers and their mappings */ |
| 760 | /* Choose MD5 */ | 754 | /* Choose MD5 */ |
| 761 | /* salt - ECRYPTFS_SALT_SIZE bytes */ | ||
| 762 | memcpy((*new_auth_tok)->token.password.salt, | 755 | memcpy((*new_auth_tok)->token.password.salt, |
| 763 | &data[(*packet_size)], ECRYPTFS_SALT_SIZE); | 756 | &data[(*packet_size)], ECRYPTFS_SALT_SIZE); |
| 764 | (*packet_size) += ECRYPTFS_SALT_SIZE; | 757 | (*packet_size) += ECRYPTFS_SALT_SIZE; |
| 765 | |||
| 766 | /* This conversion was taken straight from RFC2440 */ | 758 | /* This conversion was taken straight from RFC2440 */ |
| 767 | /* number of hash iterations - one byte */ | ||
| 768 | (*new_auth_tok)->token.password.hash_iterations = | 759 | (*new_auth_tok)->token.password.hash_iterations = |
| 769 | ((u32) 16 + (data[(*packet_size)] & 15)) | 760 | ((u32) 16 + (data[(*packet_size)] & 15)) |
| 770 | << ((data[(*packet_size)] >> 4) + 6); | 761 | << ((data[(*packet_size)] >> 4) + 6); |
| 771 | (*packet_size)++; | 762 | (*packet_size)++; |
| 772 | 763 | /* Friendly reminder: | |
| 773 | /* encrypted session key - | 764 | * (*new_auth_tok)->session_key.encrypted_key_size = |
| 774 | * (body_size-5-ECRYPTFS_SALT_SIZE) bytes */ | 765 | * (body_size - (ECRYPTFS_SALT_SIZE + 5)); */ |
| 775 | memcpy((*new_auth_tok)->session_key.encrypted_key, | 766 | memcpy((*new_auth_tok)->session_key.encrypted_key, |
| 776 | &data[(*packet_size)], | 767 | &data[(*packet_size)], |
| 777 | (*new_auth_tok)->session_key.encrypted_key_size); | 768 | (*new_auth_tok)->session_key.encrypted_key_size); |
| @@ -781,7 +772,7 @@ parse_tag_3_packet(struct ecryptfs_crypt_stat *crypt_stat, | |||
| 781 | ~ECRYPTFS_CONTAINS_DECRYPTED_KEY; | 772 | ~ECRYPTFS_CONTAINS_DECRYPTED_KEY; |
| 782 | (*new_auth_tok)->session_key.flags |= | 773 | (*new_auth_tok)->session_key.flags |= |
| 783 | ECRYPTFS_CONTAINS_ENCRYPTED_KEY; | 774 | ECRYPTFS_CONTAINS_ENCRYPTED_KEY; |
| 784 | (*new_auth_tok)->token.password.hash_algo = 0x01; | 775 | (*new_auth_tok)->token.password.hash_algo = 0x01; /* MD5 */ |
| 785 | break; | 776 | break; |
| 786 | default: | 777 | default: |
| 787 | ecryptfs_printk(KERN_ERR, "Unsupported hash algorithm: " | 778 | ecryptfs_printk(KERN_ERR, "Unsupported hash algorithm: " |
| @@ -837,82 +828,61 @@ parse_tag_11_packet(unsigned char *data, unsigned char *contents, | |||
| 837 | 828 | ||
| 838 | (*packet_size) = 0; | 829 | (*packet_size) = 0; |
| 839 | (*tag_11_contents_size) = 0; | 830 | (*tag_11_contents_size) = 0; |
| 840 | 831 | /* This format is inspired by OpenPGP; see RFC 2440 | |
| 841 | /* check that: | 832 | * packet tag 11 |
| 842 | * one byte for the Tag 11 ID flag | 833 | * |
| 843 | * two bytes for the Tag 11 length | 834 | * Tag 11 identifier (1 byte) |
| 844 | * do not exceed the maximum_packet_size | 835 | * Max Tag 11 packet size (max 3 bytes) |
| 836 | * Binary format specifier (1 byte) | ||
| 837 | * Filename length (1 byte) | ||
| 838 | * Filename ("_CONSOLE") (8 bytes) | ||
| 839 | * Modification date (4 bytes) | ||
| 840 | * Literal data (arbitrary) | ||
| 841 | * | ||
| 842 | * We need at least 16 bytes of data for the packet to even be | ||
| 843 | * valid. | ||
| 845 | */ | 844 | */ |
| 846 | if (unlikely((*packet_size) + 3 > max_packet_size)) { | 845 | if (max_packet_size < 16) { |
| 847 | ecryptfs_printk(KERN_ERR, "Packet size exceeds max\n"); | 846 | printk(KERN_ERR "Maximum packet size too small\n"); |
| 848 | rc = -EINVAL; | 847 | rc = -EINVAL; |
| 849 | goto out; | 848 | goto out; |
| 850 | } | 849 | } |
| 851 | |||
| 852 | /* check for Tag 11 identifyer - one byte */ | ||
| 853 | if (data[(*packet_size)++] != ECRYPTFS_TAG_11_PACKET_TYPE) { | 850 | if (data[(*packet_size)++] != ECRYPTFS_TAG_11_PACKET_TYPE) { |
| 854 | ecryptfs_printk(KERN_WARNING, | 851 | printk(KERN_WARNING "Invalid tag 11 packet format\n"); |
| 855 | "Invalid tag 11 packet format\n"); | ||
| 856 | rc = -EINVAL; | 852 | rc = -EINVAL; |
| 857 | goto out; | 853 | goto out; |
| 858 | } | 854 | } |
| 859 | |||
| 860 | /* get Tag 11 content length - one or two bytes */ | ||
| 861 | rc = parse_packet_length(&data[(*packet_size)], &body_size, | 855 | rc = parse_packet_length(&data[(*packet_size)], &body_size, |
| 862 | &length_size); | 856 | &length_size); |
| 863 | if (rc) { | 857 | if (rc) { |
| 864 | ecryptfs_printk(KERN_WARNING, | 858 | printk(KERN_WARNING "Invalid tag 11 packet format\n"); |
| 865 | "Invalid tag 11 packet format\n"); | ||
| 866 | goto out; | 859 | goto out; |
| 867 | } | 860 | } |
| 868 | (*packet_size) += length_size; | 861 | if (body_size < 14) { |
| 869 | 862 | printk(KERN_WARNING "Invalid body size ([%td])\n", body_size); | |
| 870 | if (body_size < 13) { | ||
| 871 | ecryptfs_printk(KERN_WARNING, "Invalid body size ([%d])\n", | ||
| 872 | body_size); | ||
| 873 | rc = -EINVAL; | 863 | rc = -EINVAL; |
| 874 | goto out; | 864 | goto out; |
| 875 | } | 865 | } |
| 876 | /* We have 13 bytes of surrounding packet values */ | 866 | (*packet_size) += length_size; |
| 877 | (*tag_11_contents_size) = (body_size - 13); | 867 | (*tag_11_contents_size) = (body_size - 14); |
| 878 | |||
| 879 | /* now we know the length of the remainting Tag 11 packet size: | ||
| 880 | * 14 fix bytes for: special flag one, special flag two, | ||
| 881 | * 12 skipped bytes | ||
| 882 | * body_size bytes minus the stuff above is the Tag 11 content | ||
| 883 | */ | ||
| 884 | /* FIXME why is the body size one byte smaller than the actual | ||
| 885 | * size of the body? | ||
| 886 | * this seems to be an error here as well as in | ||
| 887 | * write_tag_11_packet() */ | ||
| 888 | if (unlikely((*packet_size) + body_size + 1 > max_packet_size)) { | 868 | if (unlikely((*packet_size) + body_size + 1 > max_packet_size)) { |
| 889 | ecryptfs_printk(KERN_ERR, "Packet size exceeds max\n"); | 869 | printk(KERN_ERR "Packet size exceeds max\n"); |
| 890 | rc = -EINVAL; | 870 | rc = -EINVAL; |
| 891 | goto out; | 871 | goto out; |
| 892 | } | 872 | } |
| 893 | |||
| 894 | /* special flag one - one byte */ | ||
| 895 | if (data[(*packet_size)++] != 0x62) { | 873 | if (data[(*packet_size)++] != 0x62) { |
| 896 | ecryptfs_printk(KERN_WARNING, "Unrecognizable packet\n"); | 874 | printk(KERN_WARNING "Unrecognizable packet\n"); |
| 897 | rc = -EINVAL; | 875 | rc = -EINVAL; |
| 898 | goto out; | 876 | goto out; |
| 899 | } | 877 | } |
| 900 | |||
| 901 | /* special flag two - one byte */ | ||
| 902 | if (data[(*packet_size)++] != 0x08) { | 878 | if (data[(*packet_size)++] != 0x08) { |
| 903 | ecryptfs_printk(KERN_WARNING, "Unrecognizable packet\n"); | 879 | printk(KERN_WARNING "Unrecognizable packet\n"); |
| 904 | rc = -EINVAL; | 880 | rc = -EINVAL; |
| 905 | goto out; | 881 | goto out; |
| 906 | } | 882 | } |
| 907 | 883 | (*packet_size) += 12; /* Ignore filename and modification date */ | |
| 908 | /* skip the next 12 bytes */ | ||
| 909 | (*packet_size) += 12; /* We don't care about the filename or | ||
| 910 | * the timestamp */ | ||
| 911 | |||
| 912 | /* get the Tag 11 contents - tag_11_contents_size bytes */ | ||
| 913 | memcpy(contents, &data[(*packet_size)], (*tag_11_contents_size)); | 884 | memcpy(contents, &data[(*packet_size)], (*tag_11_contents_size)); |
| 914 | (*packet_size) += (*tag_11_contents_size); | 885 | (*packet_size) += (*tag_11_contents_size); |
| 915 | |||
| 916 | out: | 886 | out: |
| 917 | if (rc) { | 887 | if (rc) { |
| 918 | (*packet_size) = 0; | 888 | (*packet_size) = 0; |
| @@ -921,130 +891,229 @@ out: | |||
| 921 | return rc; | 891 | return rc; |
| 922 | } | 892 | } |
| 923 | 893 | ||
| 894 | static int | ||
| 895 | ecryptfs_find_global_auth_tok_for_sig( | ||
| 896 | struct ecryptfs_global_auth_tok **global_auth_tok, | ||
| 897 | struct ecryptfs_mount_crypt_stat *mount_crypt_stat, char *sig) | ||
| 898 | { | ||
| 899 | struct ecryptfs_global_auth_tok *walker; | ||
| 900 | int rc = 0; | ||
| 901 | |||
| 902 | (*global_auth_tok) = NULL; | ||
| 903 | mutex_lock(&mount_crypt_stat->global_auth_tok_list_mutex); | ||
| 904 | list_for_each_entry(walker, | ||
| 905 | &mount_crypt_stat->global_auth_tok_list, | ||
| 906 | mount_crypt_stat_list) { | ||
| 907 | if (memcmp(walker->sig, sig, ECRYPTFS_SIG_SIZE_HEX) == 0) { | ||
| 908 | (*global_auth_tok) = walker; | ||
| 909 | goto out; | ||
| 910 | } | ||
| 911 | } | ||
| 912 | rc = -EINVAL; | ||
| 913 | out: | ||
| 914 | mutex_unlock(&mount_crypt_stat->global_auth_tok_list_mutex); | ||
| 915 | return rc; | ||
| 916 | } | ||
| 917 | |||
| 924 | /** | 918 | /** |
| 925 | * decrypt_session_key - Decrypt the session key with the given auth_tok. | 919 | * ecryptfs_verify_version |
| 920 | * @version: The version number to confirm | ||
| 926 | * | 921 | * |
| 927 | * Returns Zero on success; non-zero error otherwise. | 922 | * Returns zero on good version; non-zero otherwise |
| 928 | */ | 923 | */ |
| 929 | static int decrypt_session_key(struct ecryptfs_auth_tok *auth_tok, | 924 | static int ecryptfs_verify_version(u16 version) |
| 930 | struct ecryptfs_crypt_stat *crypt_stat) | ||
| 931 | { | 925 | { |
| 932 | struct ecryptfs_password *password_s_ptr; | 926 | int rc = 0; |
| 933 | struct scatterlist src_sg[2], dst_sg[2]; | 927 | unsigned char major; |
| 934 | struct mutex *tfm_mutex = NULL; | 928 | unsigned char minor; |
| 935 | char *encrypted_session_key; | 929 | |
| 936 | char *session_key; | 930 | major = ((version >> 8) & 0xFF); |
| 931 | minor = (version & 0xFF); | ||
| 932 | if (major != ECRYPTFS_VERSION_MAJOR) { | ||
| 933 | ecryptfs_printk(KERN_ERR, "Major version number mismatch. " | ||
| 934 | "Expected [%d]; got [%d]\n", | ||
| 935 | ECRYPTFS_VERSION_MAJOR, major); | ||
| 936 | rc = -EINVAL; | ||
| 937 | goto out; | ||
| 938 | } | ||
| 939 | if (minor != ECRYPTFS_VERSION_MINOR) { | ||
| 940 | ecryptfs_printk(KERN_ERR, "Minor version number mismatch. " | ||
| 941 | "Expected [%d]; got [%d]\n", | ||
| 942 | ECRYPTFS_VERSION_MINOR, minor); | ||
| 943 | rc = -EINVAL; | ||
| 944 | goto out; | ||
| 945 | } | ||
| 946 | out: | ||
| 947 | return rc; | ||
| 948 | } | ||
| 949 | |||
| 950 | int ecryptfs_keyring_auth_tok_for_sig(struct key **auth_tok_key, | ||
| 951 | struct ecryptfs_auth_tok **auth_tok, | ||
| 952 | char *sig) | ||
| 953 | { | ||
| 954 | int rc = 0; | ||
| 955 | |||
| 956 | (*auth_tok_key) = request_key(&key_type_user, sig, NULL); | ||
| 957 | if (!(*auth_tok_key) || IS_ERR(*auth_tok_key)) { | ||
| 958 | printk(KERN_ERR "Could not find key with description: [%s]\n", | ||
| 959 | sig); | ||
| 960 | process_request_key_err(PTR_ERR(*auth_tok_key)); | ||
| 961 | rc = -EINVAL; | ||
| 962 | goto out; | ||
| 963 | } | ||
| 964 | (*auth_tok) = ecryptfs_get_key_payload_data(*auth_tok_key); | ||
| 965 | if (ecryptfs_verify_version((*auth_tok)->version)) { | ||
| 966 | printk(KERN_ERR | ||
| 967 | "Data structure version mismatch. " | ||
| 968 | "Userspace tools must match eCryptfs " | ||
| 969 | "kernel module with major version [%d] " | ||
| 970 | "and minor version [%d]\n", | ||
| 971 | ECRYPTFS_VERSION_MAJOR, | ||
| 972 | ECRYPTFS_VERSION_MINOR); | ||
| 973 | rc = -EINVAL; | ||
| 974 | goto out; | ||
| 975 | } | ||
| 976 | if ((*auth_tok)->token_type != ECRYPTFS_PASSWORD | ||
| 977 | && (*auth_tok)->token_type != ECRYPTFS_PRIVATE_KEY) { | ||
| 978 | printk(KERN_ERR "Invalid auth_tok structure " | ||
| 979 | "returned from key query\n"); | ||
| 980 | rc = -EINVAL; | ||
| 981 | goto out; | ||
| 982 | } | ||
| 983 | out: | ||
| 984 | return rc; | ||
| 985 | } | ||
| 986 | |||
| 987 | /** | ||
| 988 | * ecryptfs_find_auth_tok_for_sig | ||
| 989 | * @auth_tok: Set to the matching auth_tok; NULL if not found | ||
| 990 | * @crypt_stat: inode crypt_stat crypto context | ||
| 991 | * @sig: Sig of auth_tok to find | ||
| 992 | * | ||
| 993 | * For now, this function simply looks at the registered auth_tok's | ||
| 994 | * linked off the mount_crypt_stat, so all the auth_toks that can be | ||
| 995 | * used must be registered at mount time. This function could | ||
| 996 | * potentially try a lot harder to find auth_tok's (e.g., by calling | ||
| 997 | * out to ecryptfsd to dynamically retrieve an auth_tok object) so | ||
| 998 | * that static registration of auth_tok's will no longer be necessary. | ||
| 999 | * | ||
| 1000 | * Returns zero on no error; non-zero on error | ||
| 1001 | */ | ||
| 1002 | static int | ||
| 1003 | ecryptfs_find_auth_tok_for_sig( | ||
| 1004 | struct ecryptfs_auth_tok **auth_tok, | ||
| 1005 | struct ecryptfs_crypt_stat *crypt_stat, char *sig) | ||
| 1006 | { | ||
| 1007 | struct ecryptfs_mount_crypt_stat *mount_crypt_stat = | ||
| 1008 | crypt_stat->mount_crypt_stat; | ||
| 1009 | struct ecryptfs_global_auth_tok *global_auth_tok; | ||
| 1010 | int rc = 0; | ||
| 1011 | |||
| 1012 | (*auth_tok) = NULL; | ||
| 1013 | if (ecryptfs_find_global_auth_tok_for_sig(&global_auth_tok, | ||
| 1014 | mount_crypt_stat, sig)) { | ||
| 1015 | struct key *auth_tok_key; | ||
| 1016 | |||
| 1017 | rc = ecryptfs_keyring_auth_tok_for_sig(&auth_tok_key, auth_tok, | ||
| 1018 | sig); | ||
| 1019 | } else | ||
| 1020 | (*auth_tok) = global_auth_tok->global_auth_tok; | ||
| 1021 | return rc; | ||
| 1022 | } | ||
| 1023 | |||
| 1024 | /** | ||
| 1025 | * decrypt_passphrase_encrypted_session_key - Decrypt the session key with the given auth_tok. | ||
| 1026 | * @auth_tok: The passphrase authentication token to use to encrypt the FEK | ||
| 1027 | * @crypt_stat: The cryptographic context | ||
| 1028 | * | ||
| 1029 | * Returns zero on success; non-zero error otherwise | ||
| 1030 | */ | ||
| 1031 | static int | ||
| 1032 | decrypt_passphrase_encrypted_session_key(struct ecryptfs_auth_tok *auth_tok, | ||
| 1033 | struct ecryptfs_crypt_stat *crypt_stat) | ||
| 1034 | { | ||
| 1035 | struct scatterlist dst_sg; | ||
| 1036 | struct scatterlist src_sg; | ||
| 1037 | struct mutex *tfm_mutex; | ||
| 937 | struct blkcipher_desc desc = { | 1038 | struct blkcipher_desc desc = { |
| 938 | .flags = CRYPTO_TFM_REQ_MAY_SLEEP | 1039 | .flags = CRYPTO_TFM_REQ_MAY_SLEEP |
| 939 | }; | 1040 | }; |
| 940 | int rc = 0; | 1041 | int rc = 0; |
| 941 | 1042 | ||
| 942 | password_s_ptr = &auth_tok->token.password; | 1043 | if (unlikely(ecryptfs_verbosity > 0)) { |
| 943 | if (password_s_ptr->flags & ECRYPTFS_SESSION_KEY_ENCRYPTION_KEY_SET) | 1044 | ecryptfs_printk( |
| 944 | ecryptfs_printk(KERN_DEBUG, "Session key encryption key " | 1045 | KERN_DEBUG, "Session key encryption key (size [%d]):\n", |
| 945 | "set; skipping key generation\n"); | 1046 | auth_tok->token.password.session_key_encryption_key_bytes); |
| 946 | ecryptfs_printk(KERN_DEBUG, "Session key encryption key (size [%d])" | 1047 | ecryptfs_dump_hex( |
| 947 | ":\n", | 1048 | auth_tok->token.password.session_key_encryption_key, |
| 948 | password_s_ptr->session_key_encryption_key_bytes); | 1049 | auth_tok->token.password.session_key_encryption_key_bytes); |
| 949 | if (ecryptfs_verbosity > 0) | 1050 | } |
| 950 | ecryptfs_dump_hex(password_s_ptr->session_key_encryption_key, | 1051 | rc = ecryptfs_get_tfm_and_mutex_for_cipher_name(&desc.tfm, &tfm_mutex, |
| 951 | password_s_ptr-> | 1052 | crypt_stat->cipher); |
| 952 | session_key_encryption_key_bytes); | 1053 | if (unlikely(rc)) { |
| 953 | if (!strcmp(crypt_stat->cipher, | 1054 | printk(KERN_ERR "Internal error whilst attempting to get " |
| 954 | crypt_stat->mount_crypt_stat->global_default_cipher_name) | 1055 | "tfm and mutex for cipher name [%s]; rc = [%d]\n", |
| 955 | && crypt_stat->mount_crypt_stat->global_key_tfm) { | 1056 | crypt_stat->cipher, rc); |
| 956 | desc.tfm = crypt_stat->mount_crypt_stat->global_key_tfm; | 1057 | goto out; |
| 957 | tfm_mutex = &crypt_stat->mount_crypt_stat->global_key_tfm_mutex; | ||
| 958 | } else { | ||
| 959 | char *full_alg_name; | ||
| 960 | |||
| 961 | rc = ecryptfs_crypto_api_algify_cipher_name(&full_alg_name, | ||
| 962 | crypt_stat->cipher, | ||
| 963 | "ecb"); | ||
| 964 | if (rc) | ||
| 965 | goto out; | ||
| 966 | desc.tfm = crypto_alloc_blkcipher(full_alg_name, 0, | ||
| 967 | CRYPTO_ALG_ASYNC); | ||
| 968 | kfree(full_alg_name); | ||
| 969 | if (IS_ERR(desc.tfm)) { | ||
| 970 | rc = PTR_ERR(desc.tfm); | ||
| 971 | printk(KERN_ERR "Error allocating crypto context; " | ||
| 972 | "rc = [%d]\n", rc); | ||
| 973 | goto out; | ||
| 974 | } | ||
| 975 | crypto_blkcipher_set_flags(desc.tfm, CRYPTO_TFM_REQ_WEAK_KEY); | ||
| 976 | } | 1058 | } |
| 977 | if (tfm_mutex) | 1059 | rc = virt_to_scatterlist(auth_tok->session_key.encrypted_key, |
| 978 | mutex_lock(tfm_mutex); | 1060 | auth_tok->session_key.encrypted_key_size, |
| 979 | rc = crypto_blkcipher_setkey(desc.tfm, | 1061 | &src_sg, 1); |
| 980 | password_s_ptr->session_key_encryption_key, | 1062 | if (rc != 1) { |
| 981 | crypt_stat->key_size); | 1063 | printk(KERN_ERR "Internal error whilst attempting to convert " |
| 982 | if (rc < 0) { | 1064 | "auth_tok->session_key.encrypted_key to scatterlist; " |
| 1065 | "expected rc = 1; got rc = [%d]. " | ||
| 1066 | "auth_tok->session_key.encrypted_key_size = [%d]\n", rc, | ||
| 1067 | auth_tok->session_key.encrypted_key_size); | ||
| 1068 | goto out; | ||
| 1069 | } | ||
| 1070 | auth_tok->session_key.decrypted_key_size = | ||
| 1071 | auth_tok->session_key.encrypted_key_size; | ||
| 1072 | rc = virt_to_scatterlist(auth_tok->session_key.decrypted_key, | ||
| 1073 | auth_tok->session_key.decrypted_key_size, | ||
| 1074 | &dst_sg, 1); | ||
| 1075 | if (rc != 1) { | ||
| 1076 | printk(KERN_ERR "Internal error whilst attempting to convert " | ||
| 1077 | "auth_tok->session_key.decrypted_key to scatterlist; " | ||
| 1078 | "expected rc = 1; got rc = [%d]\n", rc); | ||
| 1079 | goto out; | ||
| 1080 | } | ||
| 1081 | mutex_lock(tfm_mutex); | ||
| 1082 | rc = crypto_blkcipher_setkey( | ||
| 1083 | desc.tfm, auth_tok->token.password.session_key_encryption_key, | ||
| 1084 | crypt_stat->key_size); | ||
| 1085 | if (unlikely(rc < 0)) { | ||
| 1086 | mutex_unlock(tfm_mutex); | ||
| 983 | printk(KERN_ERR "Error setting key for crypto context\n"); | 1087 | printk(KERN_ERR "Error setting key for crypto context\n"); |
| 984 | rc = -EINVAL; | 1088 | rc = -EINVAL; |
| 985 | goto out_free_tfm; | 1089 | goto out; |
| 986 | } | ||
| 987 | /* TODO: virt_to_scatterlist */ | ||
| 988 | encrypted_session_key = (char *)__get_free_page(GFP_KERNEL); | ||
| 989 | if (!encrypted_session_key) { | ||
| 990 | ecryptfs_printk(KERN_ERR, "Out of memory\n"); | ||
| 991 | rc = -ENOMEM; | ||
| 992 | goto out_free_tfm; | ||
| 993 | } | 1090 | } |
| 994 | session_key = (char *)__get_free_page(GFP_KERNEL); | 1091 | rc = crypto_blkcipher_decrypt(&desc, &dst_sg, &src_sg, |
| 995 | if (!session_key) { | ||
| 996 | kfree(encrypted_session_key); | ||
| 997 | ecryptfs_printk(KERN_ERR, "Out of memory\n"); | ||
| 998 | rc = -ENOMEM; | ||
| 999 | goto out_free_tfm; | ||
| 1000 | } | ||
| 1001 | memcpy(encrypted_session_key, auth_tok->session_key.encrypted_key, | ||
| 1002 | auth_tok->session_key.encrypted_key_size); | ||
| 1003 | src_sg[0].page = virt_to_page(encrypted_session_key); | ||
| 1004 | src_sg[0].offset = 0; | ||
| 1005 | BUG_ON(auth_tok->session_key.encrypted_key_size > PAGE_CACHE_SIZE); | ||
| 1006 | src_sg[0].length = auth_tok->session_key.encrypted_key_size; | ||
| 1007 | dst_sg[0].page = virt_to_page(session_key); | ||
| 1008 | dst_sg[0].offset = 0; | ||
| 1009 | auth_tok->session_key.decrypted_key_size = | ||
| 1010 | auth_tok->session_key.encrypted_key_size; | ||
| 1011 | dst_sg[0].length = auth_tok->session_key.encrypted_key_size; | ||
| 1012 | rc = crypto_blkcipher_decrypt(&desc, dst_sg, src_sg, | ||
| 1013 | auth_tok->session_key.encrypted_key_size); | 1092 | auth_tok->session_key.encrypted_key_size); |
| 1014 | if (rc) { | 1093 | mutex_unlock(tfm_mutex); |
| 1094 | if (unlikely(rc)) { | ||
| 1015 | printk(KERN_ERR "Error decrypting; rc = [%d]\n", rc); | 1095 | printk(KERN_ERR "Error decrypting; rc = [%d]\n", rc); |
| 1016 | goto out_free_memory; | 1096 | goto out; |
| 1017 | } | 1097 | } |
| 1018 | auth_tok->session_key.decrypted_key_size = | ||
| 1019 | auth_tok->session_key.encrypted_key_size; | ||
| 1020 | memcpy(auth_tok->session_key.decrypted_key, session_key, | ||
| 1021 | auth_tok->session_key.decrypted_key_size); | ||
| 1022 | auth_tok->session_key.flags |= ECRYPTFS_CONTAINS_DECRYPTED_KEY; | 1098 | auth_tok->session_key.flags |= ECRYPTFS_CONTAINS_DECRYPTED_KEY; |
| 1023 | memcpy(crypt_stat->key, auth_tok->session_key.decrypted_key, | 1099 | memcpy(crypt_stat->key, auth_tok->session_key.decrypted_key, |
| 1024 | auth_tok->session_key.decrypted_key_size); | 1100 | auth_tok->session_key.decrypted_key_size); |
| 1025 | crypt_stat->flags |= ECRYPTFS_KEY_VALID; | 1101 | crypt_stat->flags |= ECRYPTFS_KEY_VALID; |
| 1026 | ecryptfs_printk(KERN_DEBUG, "Decrypted session key:\n"); | 1102 | if (unlikely(ecryptfs_verbosity > 0)) { |
| 1027 | if (ecryptfs_verbosity > 0) | 1103 | ecryptfs_printk(KERN_DEBUG, "FEK of size [%d]:\n", |
| 1104 | crypt_stat->key_size); | ||
| 1028 | ecryptfs_dump_hex(crypt_stat->key, | 1105 | ecryptfs_dump_hex(crypt_stat->key, |
| 1029 | crypt_stat->key_size); | 1106 | crypt_stat->key_size); |
| 1030 | out_free_memory: | 1107 | } |
| 1031 | memset(encrypted_session_key, 0, PAGE_CACHE_SIZE); | ||
| 1032 | free_page((unsigned long)encrypted_session_key); | ||
| 1033 | memset(session_key, 0, PAGE_CACHE_SIZE); | ||
| 1034 | free_page((unsigned long)session_key); | ||
| 1035 | out_free_tfm: | ||
| 1036 | if (tfm_mutex) | ||
| 1037 | mutex_unlock(tfm_mutex); | ||
| 1038 | else | ||
| 1039 | crypto_free_blkcipher(desc.tfm); | ||
| 1040 | out: | 1108 | out: |
| 1041 | return rc; | 1109 | return rc; |
| 1042 | } | 1110 | } |
| 1043 | 1111 | ||
| 1044 | /** | 1112 | /** |
| 1045 | * ecryptfs_parse_packet_set | 1113 | * ecryptfs_parse_packet_set |
| 1046 | * @dest: The header page in memory | 1114 | * @crypt_stat: The cryptographic context |
| 1047 | * @version: Version of file format, to guide parsing behavior | 1115 | * @src: Virtual address of region of memory containing the packets |
| 1116 | * @ecryptfs_dentry: The eCryptfs dentry associated with the packet set | ||
| 1048 | * | 1117 | * |
| 1049 | * Get crypt_stat to have the file's session key if the requisite key | 1118 | * Get crypt_stat to have the file's session key if the requisite key |
| 1050 | * is available to decrypt the session key. | 1119 | * is available to decrypt the session key. |
| @@ -1058,25 +1127,22 @@ int ecryptfs_parse_packet_set(struct ecryptfs_crypt_stat *crypt_stat, | |||
| 1058 | struct dentry *ecryptfs_dentry) | 1127 | struct dentry *ecryptfs_dentry) |
| 1059 | { | 1128 | { |
| 1060 | size_t i = 0; | 1129 | size_t i = 0; |
| 1061 | size_t found_auth_tok = 0; | 1130 | size_t found_auth_tok; |
| 1062 | size_t next_packet_is_auth_tok_packet; | 1131 | size_t next_packet_is_auth_tok_packet; |
| 1063 | char sig[ECRYPTFS_SIG_SIZE_HEX]; | ||
| 1064 | struct list_head auth_tok_list; | 1132 | struct list_head auth_tok_list; |
| 1065 | struct list_head *walker; | 1133 | struct ecryptfs_auth_tok *matching_auth_tok; |
| 1066 | struct ecryptfs_auth_tok *chosen_auth_tok = NULL; | 1134 | struct ecryptfs_auth_tok *candidate_auth_tok; |
| 1067 | struct ecryptfs_mount_crypt_stat *mount_crypt_stat = | 1135 | char *candidate_auth_tok_sig; |
| 1068 | &ecryptfs_superblock_to_private( | ||
| 1069 | ecryptfs_dentry->d_sb)->mount_crypt_stat; | ||
| 1070 | struct ecryptfs_auth_tok *candidate_auth_tok = NULL; | ||
| 1071 | size_t packet_size; | 1136 | size_t packet_size; |
| 1072 | struct ecryptfs_auth_tok *new_auth_tok; | 1137 | struct ecryptfs_auth_tok *new_auth_tok; |
| 1073 | unsigned char sig_tmp_space[ECRYPTFS_SIG_SIZE]; | 1138 | unsigned char sig_tmp_space[ECRYPTFS_SIG_SIZE]; |
| 1139 | struct ecryptfs_auth_tok_list_item *auth_tok_list_item; | ||
| 1074 | size_t tag_11_contents_size; | 1140 | size_t tag_11_contents_size; |
| 1075 | size_t tag_11_packet_size; | 1141 | size_t tag_11_packet_size; |
| 1076 | int rc = 0; | 1142 | int rc = 0; |
| 1077 | 1143 | ||
| 1078 | INIT_LIST_HEAD(&auth_tok_list); | 1144 | INIT_LIST_HEAD(&auth_tok_list); |
| 1079 | /* Parse the header to find as many packets as we can, these will be | 1145 | /* Parse the header to find as many packets as we can; these will be |
| 1080 | * added the our &auth_tok_list */ | 1146 | * added the our &auth_tok_list */ |
| 1081 | next_packet_is_auth_tok_packet = 1; | 1147 | next_packet_is_auth_tok_packet = 1; |
| 1082 | while (next_packet_is_auth_tok_packet) { | 1148 | while (next_packet_is_auth_tok_packet) { |
| @@ -1155,73 +1221,85 @@ int ecryptfs_parse_packet_set(struct ecryptfs_crypt_stat *crypt_stat, | |||
| 1155 | } | 1221 | } |
| 1156 | } | 1222 | } |
| 1157 | if (list_empty(&auth_tok_list)) { | 1223 | if (list_empty(&auth_tok_list)) { |
| 1158 | rc = -EINVAL; /* Do not support non-encrypted files in | 1224 | printk(KERN_ERR "The lower file appears to be a non-encrypted " |
| 1159 | * the 0.1 release */ | 1225 | "eCryptfs file; this is not supported in this version " |
| 1226 | "of the eCryptfs kernel module\n"); | ||
| 1227 | rc = -EINVAL; | ||
| 1160 | goto out; | 1228 | goto out; |
| 1161 | } | 1229 | } |
| 1162 | /* If we have a global auth tok, then we should try to use | 1230 | /* auth_tok_list contains the set of authentication tokens |
| 1163 | * it */ | 1231 | * parsed from the metadata. We need to find a matching |
| 1164 | if (mount_crypt_stat->global_auth_tok) { | 1232 | * authentication token that has the secret component(s) |
| 1165 | memcpy(sig, mount_crypt_stat->global_auth_tok_sig, | 1233 | * necessary to decrypt the EFEK in the auth_tok parsed from |
| 1166 | ECRYPTFS_SIG_SIZE_HEX); | 1234 | * the metadata. There may be several potential matches, but |
| 1167 | chosen_auth_tok = mount_crypt_stat->global_auth_tok; | 1235 | * just one will be sufficient to decrypt to get the FEK. */ |
| 1168 | } else | 1236 | find_next_matching_auth_tok: |
| 1169 | BUG(); /* We should always have a global auth tok in | 1237 | found_auth_tok = 0; |
| 1170 | * the 0.1 release */ | 1238 | list_for_each_entry(auth_tok_list_item, &auth_tok_list, list) { |
| 1171 | /* Scan list to see if our chosen_auth_tok works */ | ||
| 1172 | list_for_each(walker, &auth_tok_list) { | ||
| 1173 | struct ecryptfs_auth_tok_list_item *auth_tok_list_item; | ||
| 1174 | auth_tok_list_item = | ||
| 1175 | list_entry(walker, struct ecryptfs_auth_tok_list_item, | ||
| 1176 | list); | ||
| 1177 | candidate_auth_tok = &auth_tok_list_item->auth_tok; | 1239 | candidate_auth_tok = &auth_tok_list_item->auth_tok; |
| 1178 | if (unlikely(ecryptfs_verbosity > 0)) { | 1240 | if (unlikely(ecryptfs_verbosity > 0)) { |
| 1179 | ecryptfs_printk(KERN_DEBUG, | 1241 | ecryptfs_printk(KERN_DEBUG, |
| 1180 | "Considering cadidate auth tok:\n"); | 1242 | "Considering cadidate auth tok:\n"); |
| 1181 | ecryptfs_dump_auth_tok(candidate_auth_tok); | 1243 | ecryptfs_dump_auth_tok(candidate_auth_tok); |
| 1182 | } | 1244 | } |
| 1183 | /* TODO: Replace ECRYPTFS_SIG_SIZE_HEX w/ dynamic value */ | 1245 | rc = ecryptfs_get_auth_tok_sig(&candidate_auth_tok_sig, |
| 1184 | if (candidate_auth_tok->token_type == ECRYPTFS_PASSWORD | 1246 | candidate_auth_tok); |
| 1185 | && !strncmp(candidate_auth_tok->token.password.signature, | 1247 | if (rc) { |
| 1186 | sig, ECRYPTFS_SIG_SIZE_HEX)) { | 1248 | printk(KERN_ERR |
| 1187 | found_auth_tok = 1; | 1249 | "Unrecognized candidate auth tok type: [%d]\n", |
| 1188 | goto leave_list; | 1250 | candidate_auth_tok->token_type); |
| 1189 | /* TODO: Transfer the common salt into the | 1251 | rc = -EINVAL; |
| 1190 | * crypt_stat salt */ | 1252 | goto out_wipe_list; |
| 1191 | } else if ((candidate_auth_tok->token_type | 1253 | } |
| 1192 | == ECRYPTFS_PRIVATE_KEY) | 1254 | ecryptfs_find_auth_tok_for_sig(&matching_auth_tok, crypt_stat, |
| 1193 | && !strncmp(candidate_auth_tok->token.private_key.signature, | 1255 | candidate_auth_tok_sig); |
| 1194 | sig, ECRYPTFS_SIG_SIZE_HEX)) { | 1256 | if (matching_auth_tok) { |
| 1195 | found_auth_tok = 1; | 1257 | found_auth_tok = 1; |
| 1196 | goto leave_list; | 1258 | goto found_matching_auth_tok; |
| 1197 | } | 1259 | } |
| 1198 | } | 1260 | } |
| 1199 | if (!found_auth_tok) { | 1261 | if (!found_auth_tok) { |
| 1200 | ecryptfs_printk(KERN_ERR, "Could not find authentication " | 1262 | ecryptfs_printk(KERN_ERR, "Could not find a usable " |
| 1201 | "token on temporary list for sig [%.*s]\n", | 1263 | "authentication token\n"); |
| 1202 | ECRYPTFS_SIG_SIZE_HEX, sig); | ||
| 1203 | rc = -EIO; | 1264 | rc = -EIO; |
| 1204 | goto out_wipe_list; | 1265 | goto out_wipe_list; |
| 1205 | } | 1266 | } |
| 1206 | leave_list: | 1267 | found_matching_auth_tok: |
| 1207 | rc = -ENOTSUPP; | ||
| 1208 | if (candidate_auth_tok->token_type == ECRYPTFS_PRIVATE_KEY) { | 1268 | if (candidate_auth_tok->token_type == ECRYPTFS_PRIVATE_KEY) { |
| 1209 | memcpy(&(candidate_auth_tok->token.private_key), | 1269 | memcpy(&(candidate_auth_tok->token.private_key), |
| 1210 | &(chosen_auth_tok->token.private_key), | 1270 | &(matching_auth_tok->token.private_key), |
| 1211 | sizeof(struct ecryptfs_private_key)); | 1271 | sizeof(struct ecryptfs_private_key)); |
| 1212 | rc = decrypt_pki_encrypted_session_key(mount_crypt_stat, | 1272 | rc = decrypt_pki_encrypted_session_key(candidate_auth_tok, |
| 1213 | candidate_auth_tok, | ||
| 1214 | crypt_stat); | 1273 | crypt_stat); |
| 1215 | } else if (candidate_auth_tok->token_type == ECRYPTFS_PASSWORD) { | 1274 | } else if (candidate_auth_tok->token_type == ECRYPTFS_PASSWORD) { |
| 1216 | memcpy(&(candidate_auth_tok->token.password), | 1275 | memcpy(&(candidate_auth_tok->token.password), |
| 1217 | &(chosen_auth_tok->token.password), | 1276 | &(matching_auth_tok->token.password), |
| 1218 | sizeof(struct ecryptfs_password)); | 1277 | sizeof(struct ecryptfs_password)); |
| 1219 | rc = decrypt_session_key(candidate_auth_tok, crypt_stat); | 1278 | rc = decrypt_passphrase_encrypted_session_key( |
| 1279 | candidate_auth_tok, crypt_stat); | ||
| 1220 | } | 1280 | } |
| 1221 | if (rc) { | 1281 | if (rc) { |
| 1222 | ecryptfs_printk(KERN_ERR, "Error decrypting the " | 1282 | struct ecryptfs_auth_tok_list_item *auth_tok_list_item_tmp; |
| 1223 | "session key; rc = [%d]\n", rc); | 1283 | |
| 1224 | goto out_wipe_list; | 1284 | ecryptfs_printk(KERN_WARNING, "Error decrypting the " |
| 1285 | "session key for authentication token with sig " | ||
| 1286 | "[%.*s]; rc = [%d]. Removing auth tok " | ||
| 1287 | "candidate from the list and searching for " | ||
| 1288 | "the next match.\n", candidate_auth_tok_sig, | ||
| 1289 | ECRYPTFS_SIG_SIZE_HEX, rc); | ||
| 1290 | list_for_each_entry_safe(auth_tok_list_item, | ||
| 1291 | auth_tok_list_item_tmp, | ||
| 1292 | &auth_tok_list, list) { | ||
| 1293 | if (candidate_auth_tok | ||
| 1294 | == &auth_tok_list_item->auth_tok) { | ||
| 1295 | list_del(&auth_tok_list_item->list); | ||
| 1296 | kmem_cache_free( | ||
| 1297 | ecryptfs_auth_tok_list_item_cache, | ||
| 1298 | auth_tok_list_item); | ||
| 1299 | goto find_next_matching_auth_tok; | ||
| 1300 | } | ||
| 1301 | } | ||
| 1302 | BUG(); | ||
| 1225 | } | 1303 | } |
| 1226 | rc = ecryptfs_compute_root_iv(crypt_stat); | 1304 | rc = ecryptfs_compute_root_iv(crypt_stat); |
| 1227 | if (rc) { | 1305 | if (rc) { |
| @@ -1240,6 +1318,7 @@ out_wipe_list: | |||
| 1240 | out: | 1318 | out: |
| 1241 | return rc; | 1319 | return rc; |
| 1242 | } | 1320 | } |
| 1321 | |||
| 1243 | static int | 1322 | static int |
| 1244 | pki_encrypt_session_key(struct ecryptfs_auth_tok *auth_tok, | 1323 | pki_encrypt_session_key(struct ecryptfs_auth_tok *auth_tok, |
| 1245 | struct ecryptfs_crypt_stat *crypt_stat, | 1324 | struct ecryptfs_crypt_stat *crypt_stat, |
| @@ -1284,22 +1363,25 @@ out: | |||
| 1284 | /** | 1363 | /** |
| 1285 | * write_tag_1_packet - Write an RFC2440-compatible tag 1 (public key) packet | 1364 | * write_tag_1_packet - Write an RFC2440-compatible tag 1 (public key) packet |
| 1286 | * @dest: Buffer into which to write the packet | 1365 | * @dest: Buffer into which to write the packet |
| 1287 | * @max: Maximum number of bytes that can be writtn | 1366 | * @remaining_bytes: Maximum number of bytes that can be writtn |
| 1367 | * @auth_tok: The authentication token used for generating the tag 1 packet | ||
| 1368 | * @crypt_stat: The cryptographic context | ||
| 1369 | * @key_rec: The key record struct for the tag 1 packet | ||
| 1288 | * @packet_size: This function will write the number of bytes that end | 1370 | * @packet_size: This function will write the number of bytes that end |
| 1289 | * up constituting the packet; set to zero on error | 1371 | * up constituting the packet; set to zero on error |
| 1290 | * | 1372 | * |
| 1291 | * Returns zero on success; non-zero on error. | 1373 | * Returns zero on success; non-zero on error. |
| 1292 | */ | 1374 | */ |
| 1293 | static int | 1375 | static int |
| 1294 | write_tag_1_packet(char *dest, size_t max, struct ecryptfs_auth_tok *auth_tok, | 1376 | write_tag_1_packet(char *dest, size_t *remaining_bytes, |
| 1377 | struct ecryptfs_auth_tok *auth_tok, | ||
| 1295 | struct ecryptfs_crypt_stat *crypt_stat, | 1378 | struct ecryptfs_crypt_stat *crypt_stat, |
| 1296 | struct ecryptfs_mount_crypt_stat *mount_crypt_stat, | ||
| 1297 | struct ecryptfs_key_record *key_rec, size_t *packet_size) | 1379 | struct ecryptfs_key_record *key_rec, size_t *packet_size) |
| 1298 | { | 1380 | { |
| 1299 | size_t i; | 1381 | size_t i; |
| 1300 | size_t encrypted_session_key_valid = 0; | 1382 | size_t encrypted_session_key_valid = 0; |
| 1301 | size_t key_rec_size; | ||
| 1302 | size_t packet_size_length; | 1383 | size_t packet_size_length; |
| 1384 | size_t max_packet_size; | ||
| 1303 | int rc = 0; | 1385 | int rc = 0; |
| 1304 | 1386 | ||
| 1305 | (*packet_size) = 0; | 1387 | (*packet_size) = 0; |
| @@ -1329,37 +1411,23 @@ write_tag_1_packet(char *dest, size_t max, struct ecryptfs_auth_tok *auth_tok, | |||
| 1329 | ecryptfs_dump_hex(key_rec->enc_key, key_rec->enc_key_size); | 1411 | ecryptfs_dump_hex(key_rec->enc_key, key_rec->enc_key_size); |
| 1330 | } | 1412 | } |
| 1331 | encrypted_session_key_set: | 1413 | encrypted_session_key_set: |
| 1332 | /* Now we have a valid key_rec. Append it to the | 1414 | /* This format is inspired by OpenPGP; see RFC 2440 |
| 1333 | * key_rec set. */ | 1415 | * packet tag 1 */ |
| 1334 | key_rec_size = (sizeof(struct ecryptfs_key_record) | 1416 | max_packet_size = (1 /* Tag 1 identifier */ |
| 1335 | - ECRYPTFS_MAX_ENCRYPTED_KEY_BYTES | 1417 | + 3 /* Max Tag 1 packet size */ |
| 1336 | + (key_rec->enc_key_size)); | 1418 | + 1 /* Version */ |
| 1337 | /* TODO: Include a packet size limit as a parameter to this | 1419 | + ECRYPTFS_SIG_SIZE /* Key identifier */ |
| 1338 | * function once we have multi-packet headers (for versions | 1420 | + 1 /* Cipher identifier */ |
| 1339 | * later than 0.1 */ | 1421 | + key_rec->enc_key_size); /* Encrypted key size */ |
| 1340 | if (key_rec_size >= ECRYPTFS_MAX_KEYSET_SIZE) { | 1422 | if (max_packet_size > (*remaining_bytes)) { |
| 1341 | ecryptfs_printk(KERN_ERR, "Keyset too large\n"); | 1423 | printk(KERN_ERR "Packet length larger than maximum allowable; " |
| 1342 | rc = -EINVAL; | 1424 | "need up to [%td] bytes, but there are only [%td] " |
| 1343 | goto out; | 1425 | "available\n", max_packet_size, (*remaining_bytes)); |
| 1344 | } | ||
| 1345 | /* ***** TAG 1 Packet Format ***** | ||
| 1346 | * | version number | 1 byte | | ||
| 1347 | * | key ID | 8 bytes | | ||
| 1348 | * | public key algorithm | 1 byte | | ||
| 1349 | * | encrypted session key | arbitrary | | ||
| 1350 | */ | ||
| 1351 | if ((0x02 + ECRYPTFS_SIG_SIZE + key_rec->enc_key_size) >= max) { | ||
| 1352 | ecryptfs_printk(KERN_ERR, | ||
| 1353 | "Authentication token is too large\n"); | ||
| 1354 | rc = -EINVAL; | 1426 | rc = -EINVAL; |
| 1355 | goto out; | 1427 | goto out; |
| 1356 | } | 1428 | } |
| 1357 | dest[(*packet_size)++] = ECRYPTFS_TAG_1_PACKET_TYPE; | 1429 | dest[(*packet_size)++] = ECRYPTFS_TAG_1_PACKET_TYPE; |
| 1358 | /* This format is inspired by OpenPGP; see RFC 2440 | 1430 | rc = write_packet_length(&dest[(*packet_size)], (max_packet_size - 4), |
| 1359 | * packet tag 1 */ | ||
| 1360 | rc = write_packet_length(&dest[(*packet_size)], | ||
| 1361 | (0x02 + ECRYPTFS_SIG_SIZE + | ||
| 1362 | key_rec->enc_key_size), | ||
| 1363 | &packet_size_length); | 1431 | &packet_size_length); |
| 1364 | if (rc) { | 1432 | if (rc) { |
| 1365 | ecryptfs_printk(KERN_ERR, "Error generating tag 1 packet " | 1433 | ecryptfs_printk(KERN_ERR, "Error generating tag 1 packet " |
| @@ -1377,13 +1445,15 @@ encrypted_session_key_set: | |||
| 1377 | out: | 1445 | out: |
| 1378 | if (rc) | 1446 | if (rc) |
| 1379 | (*packet_size) = 0; | 1447 | (*packet_size) = 0; |
| 1448 | else | ||
| 1449 | (*remaining_bytes) -= (*packet_size); | ||
| 1380 | return rc; | 1450 | return rc; |
| 1381 | } | 1451 | } |
| 1382 | 1452 | ||
| 1383 | /** | 1453 | /** |
| 1384 | * write_tag_11_packet | 1454 | * write_tag_11_packet |
| 1385 | * @dest: Target into which Tag 11 packet is to be written | 1455 | * @dest: Target into which Tag 11 packet is to be written |
| 1386 | * @max: Maximum packet length | 1456 | * @remaining_bytes: Maximum packet length |
| 1387 | * @contents: Byte array of contents to copy in | 1457 | * @contents: Byte array of contents to copy in |
| 1388 | * @contents_length: Number of bytes in contents | 1458 | * @contents_length: Number of bytes in contents |
| 1389 | * @packet_length: Length of the Tag 11 packet written; zero on error | 1459 | * @packet_length: Length of the Tag 11 packet written; zero on error |
| @@ -1391,54 +1461,59 @@ out: | |||
| 1391 | * Returns zero on success; non-zero on error. | 1461 | * Returns zero on success; non-zero on error. |
| 1392 | */ | 1462 | */ |
| 1393 | static int | 1463 | static int |
| 1394 | write_tag_11_packet(char *dest, int max, char *contents, size_t contents_length, | 1464 | write_tag_11_packet(char *dest, size_t *remaining_bytes, char *contents, |
| 1395 | size_t *packet_length) | 1465 | size_t contents_length, size_t *packet_length) |
| 1396 | { | 1466 | { |
| 1397 | size_t packet_size_length; | 1467 | size_t packet_size_length; |
| 1468 | size_t max_packet_size; | ||
| 1398 | int rc = 0; | 1469 | int rc = 0; |
| 1399 | 1470 | ||
| 1400 | (*packet_length) = 0; | 1471 | (*packet_length) = 0; |
| 1401 | if ((13 + contents_length) > max) { | 1472 | /* This format is inspired by OpenPGP; see RFC 2440 |
| 1473 | * packet tag 11 */ | ||
| 1474 | max_packet_size = (1 /* Tag 11 identifier */ | ||
| 1475 | + 3 /* Max Tag 11 packet size */ | ||
| 1476 | + 1 /* Binary format specifier */ | ||
| 1477 | + 1 /* Filename length */ | ||
| 1478 | + 8 /* Filename ("_CONSOLE") */ | ||
| 1479 | + 4 /* Modification date */ | ||
| 1480 | + contents_length); /* Literal data */ | ||
| 1481 | if (max_packet_size > (*remaining_bytes)) { | ||
| 1482 | printk(KERN_ERR "Packet length larger than maximum allowable; " | ||
| 1483 | "need up to [%td] bytes, but there are only [%td] " | ||
| 1484 | "available\n", max_packet_size, (*remaining_bytes)); | ||
| 1402 | rc = -EINVAL; | 1485 | rc = -EINVAL; |
| 1403 | ecryptfs_printk(KERN_ERR, "Packet length larger than " | ||
| 1404 | "maximum allowable\n"); | ||
| 1405 | goto out; | 1486 | goto out; |
| 1406 | } | 1487 | } |
| 1407 | /* General packet header */ | ||
| 1408 | /* Packet tag */ | ||
| 1409 | dest[(*packet_length)++] = ECRYPTFS_TAG_11_PACKET_TYPE; | 1488 | dest[(*packet_length)++] = ECRYPTFS_TAG_11_PACKET_TYPE; |
| 1410 | /* Packet length */ | ||
| 1411 | rc = write_packet_length(&dest[(*packet_length)], | 1489 | rc = write_packet_length(&dest[(*packet_length)], |
| 1412 | (13 + contents_length), &packet_size_length); | 1490 | (max_packet_size - 4), &packet_size_length); |
| 1413 | if (rc) { | 1491 | if (rc) { |
| 1414 | ecryptfs_printk(KERN_ERR, "Error generating tag 11 packet " | 1492 | printk(KERN_ERR "Error generating tag 11 packet header; cannot " |
| 1415 | "header; cannot generate packet length\n"); | 1493 | "generate packet length. rc = [%d]\n", rc); |
| 1416 | goto out; | 1494 | goto out; |
| 1417 | } | 1495 | } |
| 1418 | (*packet_length) += packet_size_length; | 1496 | (*packet_length) += packet_size_length; |
| 1419 | /* Tag 11 specific */ | 1497 | dest[(*packet_length)++] = 0x62; /* binary data format specifier */ |
| 1420 | /* One-octet field that describes how the data is formatted */ | ||
| 1421 | dest[(*packet_length)++] = 0x62; /* binary data */ | ||
| 1422 | /* One-octet filename length followed by filename */ | ||
| 1423 | dest[(*packet_length)++] = 8; | 1498 | dest[(*packet_length)++] = 8; |
| 1424 | memcpy(&dest[(*packet_length)], "_CONSOLE", 8); | 1499 | memcpy(&dest[(*packet_length)], "_CONSOLE", 8); |
| 1425 | (*packet_length) += 8; | 1500 | (*packet_length) += 8; |
| 1426 | /* Four-octet number indicating modification date */ | ||
| 1427 | memset(&dest[(*packet_length)], 0x00, 4); | 1501 | memset(&dest[(*packet_length)], 0x00, 4); |
| 1428 | (*packet_length) += 4; | 1502 | (*packet_length) += 4; |
| 1429 | /* Remainder is literal data */ | ||
| 1430 | memcpy(&dest[(*packet_length)], contents, contents_length); | 1503 | memcpy(&dest[(*packet_length)], contents, contents_length); |
| 1431 | (*packet_length) += contents_length; | 1504 | (*packet_length) += contents_length; |
| 1432 | out: | 1505 | out: |
| 1433 | if (rc) | 1506 | if (rc) |
| 1434 | (*packet_length) = 0; | 1507 | (*packet_length) = 0; |
| 1508 | else | ||
| 1509 | (*remaining_bytes) -= (*packet_length); | ||
| 1435 | return rc; | 1510 | return rc; |
| 1436 | } | 1511 | } |
| 1437 | 1512 | ||
| 1438 | /** | 1513 | /** |
| 1439 | * write_tag_3_packet | 1514 | * write_tag_3_packet |
| 1440 | * @dest: Buffer into which to write the packet | 1515 | * @dest: Buffer into which to write the packet |
| 1441 | * @max: Maximum number of bytes that can be written | 1516 | * @remaining_bytes: Maximum number of bytes that can be written |
| 1442 | * @auth_tok: Authentication token | 1517 | * @auth_tok: Authentication token |
| 1443 | * @crypt_stat: The cryptographic context | 1518 | * @crypt_stat: The cryptographic context |
| 1444 | * @key_rec: encrypted key | 1519 | * @key_rec: encrypted key |
| @@ -1448,19 +1523,22 @@ write_tag_11_packet(char *dest, int max, char *contents, size_t contents_length, | |||
| 1448 | * Returns zero on success; non-zero on error. | 1523 | * Returns zero on success; non-zero on error. |
| 1449 | */ | 1524 | */ |
| 1450 | static int | 1525 | static int |
| 1451 | write_tag_3_packet(char *dest, size_t max, struct ecryptfs_auth_tok *auth_tok, | 1526 | write_tag_3_packet(char *dest, size_t *remaining_bytes, |
| 1527 | struct ecryptfs_auth_tok *auth_tok, | ||
| 1452 | struct ecryptfs_crypt_stat *crypt_stat, | 1528 | struct ecryptfs_crypt_stat *crypt_stat, |
| 1453 | struct ecryptfs_key_record *key_rec, size_t *packet_size) | 1529 | struct ecryptfs_key_record *key_rec, size_t *packet_size) |
| 1454 | { | 1530 | { |
| 1455 | size_t i; | 1531 | size_t i; |
| 1456 | size_t encrypted_session_key_valid = 0; | 1532 | size_t encrypted_session_key_valid = 0; |
| 1457 | char session_key_encryption_key[ECRYPTFS_MAX_KEY_BYTES]; | 1533 | char session_key_encryption_key[ECRYPTFS_MAX_KEY_BYTES]; |
| 1458 | struct scatterlist dest_sg[2]; | 1534 | struct scatterlist dst_sg; |
| 1459 | struct scatterlist src_sg[2]; | 1535 | struct scatterlist src_sg; |
| 1460 | struct mutex *tfm_mutex = NULL; | 1536 | struct mutex *tfm_mutex = NULL; |
| 1461 | size_t key_rec_size; | ||
| 1462 | size_t packet_size_length; | ||
| 1463 | size_t cipher_code; | 1537 | size_t cipher_code; |
| 1538 | size_t packet_size_length; | ||
| 1539 | size_t max_packet_size; | ||
| 1540 | struct ecryptfs_mount_crypt_stat *mount_crypt_stat = | ||
| 1541 | crypt_stat->mount_crypt_stat; | ||
| 1464 | struct blkcipher_desc desc = { | 1542 | struct blkcipher_desc desc = { |
| 1465 | .tfm = NULL, | 1543 | .tfm = NULL, |
| 1466 | .flags = CRYPTO_TFM_REQ_MAY_SLEEP | 1544 | .flags = CRYPTO_TFM_REQ_MAY_SLEEP |
| @@ -1470,16 +1548,25 @@ write_tag_3_packet(char *dest, size_t max, struct ecryptfs_auth_tok *auth_tok, | |||
| 1470 | (*packet_size) = 0; | 1548 | (*packet_size) = 0; |
| 1471 | ecryptfs_from_hex(key_rec->sig, auth_tok->token.password.signature, | 1549 | ecryptfs_from_hex(key_rec->sig, auth_tok->token.password.signature, |
| 1472 | ECRYPTFS_SIG_SIZE); | 1550 | ECRYPTFS_SIG_SIZE); |
| 1473 | encrypted_session_key_valid = 0; | 1551 | rc = ecryptfs_get_tfm_and_mutex_for_cipher_name(&desc.tfm, &tfm_mutex, |
| 1474 | for (i = 0; i < crypt_stat->key_size; i++) | 1552 | crypt_stat->cipher); |
| 1475 | encrypted_session_key_valid |= | 1553 | if (unlikely(rc)) { |
| 1476 | auth_tok->session_key.encrypted_key[i]; | 1554 | printk(KERN_ERR "Internal error whilst attempting to get " |
| 1477 | if (encrypted_session_key_valid) { | 1555 | "tfm and mutex for cipher name [%s]; rc = [%d]\n", |
| 1478 | memcpy(key_rec->enc_key, | 1556 | crypt_stat->cipher, rc); |
| 1479 | auth_tok->session_key.encrypted_key, | 1557 | goto out; |
| 1480 | auth_tok->session_key.encrypted_key_size); | 1558 | } |
| 1481 | goto encrypted_session_key_set; | 1559 | if (mount_crypt_stat->global_default_cipher_key_size == 0) { |
| 1560 | struct blkcipher_alg *alg = crypto_blkcipher_alg(desc.tfm); | ||
| 1561 | |||
| 1562 | printk(KERN_WARNING "No key size specified at mount; " | ||
| 1563 | "defaulting to [%d]\n", alg->max_keysize); | ||
| 1564 | mount_crypt_stat->global_default_cipher_key_size = | ||
| 1565 | alg->max_keysize; | ||
| 1482 | } | 1566 | } |
| 1567 | if (crypt_stat->key_size == 0) | ||
| 1568 | crypt_stat->key_size = | ||
| 1569 | mount_crypt_stat->global_default_cipher_key_size; | ||
| 1483 | if (auth_tok->session_key.encrypted_key_size == 0) | 1570 | if (auth_tok->session_key.encrypted_key_size == 0) |
| 1484 | auth_tok->session_key.encrypted_key_size = | 1571 | auth_tok->session_key.encrypted_key_size = |
| 1485 | crypt_stat->key_size; | 1572 | crypt_stat->key_size; |
| @@ -1487,9 +1574,24 @@ write_tag_3_packet(char *dest, size_t max, struct ecryptfs_auth_tok *auth_tok, | |||
| 1487 | && strcmp("aes", crypt_stat->cipher) == 0) { | 1574 | && strcmp("aes", crypt_stat->cipher) == 0) { |
| 1488 | memset((crypt_stat->key + 24), 0, 8); | 1575 | memset((crypt_stat->key + 24), 0, 8); |
| 1489 | auth_tok->session_key.encrypted_key_size = 32; | 1576 | auth_tok->session_key.encrypted_key_size = 32; |
| 1490 | } | 1577 | } else |
| 1578 | auth_tok->session_key.encrypted_key_size = crypt_stat->key_size; | ||
| 1491 | key_rec->enc_key_size = | 1579 | key_rec->enc_key_size = |
| 1492 | auth_tok->session_key.encrypted_key_size; | 1580 | auth_tok->session_key.encrypted_key_size; |
| 1581 | encrypted_session_key_valid = 0; | ||
| 1582 | for (i = 0; i < auth_tok->session_key.encrypted_key_size; i++) | ||
| 1583 | encrypted_session_key_valid |= | ||
| 1584 | auth_tok->session_key.encrypted_key[i]; | ||
| 1585 | if (encrypted_session_key_valid) { | ||
| 1586 | ecryptfs_printk(KERN_DEBUG, "encrypted_session_key_valid != 0; " | ||
| 1587 | "using auth_tok->session_key.encrypted_key, " | ||
| 1588 | "where key_rec->enc_key_size = [%d]\n", | ||
| 1589 | key_rec->enc_key_size); | ||
| 1590 | memcpy(key_rec->enc_key, | ||
| 1591 | auth_tok->session_key.encrypted_key, | ||
| 1592 | key_rec->enc_key_size); | ||
| 1593 | goto encrypted_session_key_set; | ||
| 1594 | } | ||
| 1493 | if (auth_tok->token.password.flags & | 1595 | if (auth_tok->token.password.flags & |
| 1494 | ECRYPTFS_SESSION_KEY_ENCRYPTION_KEY_SET) { | 1596 | ECRYPTFS_SESSION_KEY_ENCRYPTION_KEY_SET) { |
| 1495 | ecryptfs_printk(KERN_DEBUG, "Using previously generated " | 1597 | ecryptfs_printk(KERN_DEBUG, "Using previously generated " |
| @@ -1508,54 +1610,32 @@ write_tag_3_packet(char *dest, size_t max, struct ecryptfs_auth_tok *auth_tok, | |||
| 1508 | ecryptfs_printk(KERN_DEBUG, "Session key encryption key:\n"); | 1610 | ecryptfs_printk(KERN_DEBUG, "Session key encryption key:\n"); |
| 1509 | ecryptfs_dump_hex(session_key_encryption_key, 16); | 1611 | ecryptfs_dump_hex(session_key_encryption_key, 16); |
| 1510 | } | 1612 | } |
| 1511 | rc = virt_to_scatterlist(crypt_stat->key, | 1613 | rc = virt_to_scatterlist(crypt_stat->key, key_rec->enc_key_size, |
| 1512 | key_rec->enc_key_size, src_sg, 2); | 1614 | &src_sg, 1); |
| 1513 | if (!rc) { | 1615 | if (rc != 1) { |
| 1514 | ecryptfs_printk(KERN_ERR, "Error generating scatterlist " | 1616 | ecryptfs_printk(KERN_ERR, "Error generating scatterlist " |
| 1515 | "for crypt_stat session key\n"); | 1617 | "for crypt_stat session key; expected rc = 1; " |
| 1618 | "got rc = [%d]. key_rec->enc_key_size = [%d]\n", | ||
| 1619 | rc, key_rec->enc_key_size); | ||
| 1516 | rc = -ENOMEM; | 1620 | rc = -ENOMEM; |
| 1517 | goto out; | 1621 | goto out; |
| 1518 | } | 1622 | } |
| 1519 | rc = virt_to_scatterlist(key_rec->enc_key, | 1623 | rc = virt_to_scatterlist(key_rec->enc_key, key_rec->enc_key_size, |
| 1520 | key_rec->enc_key_size, dest_sg, 2); | 1624 | &dst_sg, 1); |
| 1521 | if (!rc) { | 1625 | if (rc != 1) { |
| 1522 | ecryptfs_printk(KERN_ERR, "Error generating scatterlist " | 1626 | ecryptfs_printk(KERN_ERR, "Error generating scatterlist " |
| 1523 | "for crypt_stat encrypted session key\n"); | 1627 | "for crypt_stat encrypted session key; " |
| 1628 | "expected rc = 1; got rc = [%d]. " | ||
| 1629 | "key_rec->enc_key_size = [%d]\n", rc, | ||
| 1630 | key_rec->enc_key_size); | ||
| 1524 | rc = -ENOMEM; | 1631 | rc = -ENOMEM; |
| 1525 | goto out; | 1632 | goto out; |
| 1526 | } | 1633 | } |
| 1527 | if (!strcmp(crypt_stat->cipher, | 1634 | mutex_lock(tfm_mutex); |
| 1528 | crypt_stat->mount_crypt_stat->global_default_cipher_name) | ||
| 1529 | && crypt_stat->mount_crypt_stat->global_key_tfm) { | ||
| 1530 | desc.tfm = crypt_stat->mount_crypt_stat->global_key_tfm; | ||
| 1531 | tfm_mutex = &crypt_stat->mount_crypt_stat->global_key_tfm_mutex; | ||
| 1532 | } else { | ||
| 1533 | char *full_alg_name; | ||
| 1534 | |||
| 1535 | rc = ecryptfs_crypto_api_algify_cipher_name(&full_alg_name, | ||
| 1536 | crypt_stat->cipher, | ||
| 1537 | "ecb"); | ||
| 1538 | if (rc) | ||
| 1539 | goto out; | ||
| 1540 | desc.tfm = crypto_alloc_blkcipher(full_alg_name, 0, | ||
| 1541 | CRYPTO_ALG_ASYNC); | ||
| 1542 | kfree(full_alg_name); | ||
| 1543 | if (IS_ERR(desc.tfm)) { | ||
| 1544 | rc = PTR_ERR(desc.tfm); | ||
| 1545 | ecryptfs_printk(KERN_ERR, "Could not initialize crypto " | ||
| 1546 | "context for cipher [%s]; rc = [%d]\n", | ||
| 1547 | crypt_stat->cipher, rc); | ||
| 1548 | goto out; | ||
| 1549 | } | ||
| 1550 | crypto_blkcipher_set_flags(desc.tfm, CRYPTO_TFM_REQ_WEAK_KEY); | ||
| 1551 | } | ||
| 1552 | if (tfm_mutex) | ||
| 1553 | mutex_lock(tfm_mutex); | ||
| 1554 | rc = crypto_blkcipher_setkey(desc.tfm, session_key_encryption_key, | 1635 | rc = crypto_blkcipher_setkey(desc.tfm, session_key_encryption_key, |
| 1555 | crypt_stat->key_size); | 1636 | crypt_stat->key_size); |
| 1556 | if (rc < 0) { | 1637 | if (rc < 0) { |
| 1557 | if (tfm_mutex) | 1638 | mutex_unlock(tfm_mutex); |
| 1558 | mutex_unlock(tfm_mutex); | ||
| 1559 | ecryptfs_printk(KERN_ERR, "Error setting key for crypto " | 1639 | ecryptfs_printk(KERN_ERR, "Error setting key for crypto " |
| 1560 | "context; rc = [%d]\n", rc); | 1640 | "context; rc = [%d]\n", rc); |
| 1561 | goto out; | 1641 | goto out; |
| @@ -1563,56 +1643,53 @@ write_tag_3_packet(char *dest, size_t max, struct ecryptfs_auth_tok *auth_tok, | |||
| 1563 | rc = 0; | 1643 | rc = 0; |
| 1564 | ecryptfs_printk(KERN_DEBUG, "Encrypting [%d] bytes of the key\n", | 1644 | ecryptfs_printk(KERN_DEBUG, "Encrypting [%d] bytes of the key\n", |
| 1565 | crypt_stat->key_size); | 1645 | crypt_stat->key_size); |
| 1566 | rc = crypto_blkcipher_encrypt(&desc, dest_sg, src_sg, | 1646 | rc = crypto_blkcipher_encrypt(&desc, &dst_sg, &src_sg, |
| 1567 | (*key_rec).enc_key_size); | 1647 | (*key_rec).enc_key_size); |
| 1648 | mutex_unlock(tfm_mutex); | ||
| 1568 | if (rc) { | 1649 | if (rc) { |
| 1569 | printk(KERN_ERR "Error encrypting; rc = [%d]\n", rc); | 1650 | printk(KERN_ERR "Error encrypting; rc = [%d]\n", rc); |
| 1570 | goto out; | 1651 | goto out; |
| 1571 | } | 1652 | } |
| 1572 | if (tfm_mutex) | ||
| 1573 | mutex_unlock(tfm_mutex); | ||
| 1574 | ecryptfs_printk(KERN_DEBUG, "This should be the encrypted key:\n"); | 1653 | ecryptfs_printk(KERN_DEBUG, "This should be the encrypted key:\n"); |
| 1575 | if (ecryptfs_verbosity > 0) | 1654 | if (ecryptfs_verbosity > 0) { |
| 1655 | ecryptfs_printk(KERN_DEBUG, "EFEK of size [%d]:\n", | ||
| 1656 | key_rec->enc_key_size); | ||
| 1576 | ecryptfs_dump_hex(key_rec->enc_key, | 1657 | ecryptfs_dump_hex(key_rec->enc_key, |
| 1577 | key_rec->enc_key_size); | 1658 | key_rec->enc_key_size); |
| 1578 | encrypted_session_key_set: | ||
| 1579 | /* Now we have a valid key_rec. Append it to the | ||
| 1580 | * key_rec set. */ | ||
| 1581 | key_rec_size = (sizeof(struct ecryptfs_key_record) | ||
| 1582 | - ECRYPTFS_MAX_ENCRYPTED_KEY_BYTES | ||
| 1583 | + (key_rec->enc_key_size)); | ||
| 1584 | /* TODO: Include a packet size limit as a parameter to this | ||
| 1585 | * function once we have multi-packet headers (for versions | ||
| 1586 | * later than 0.1 */ | ||
| 1587 | if (key_rec_size >= ECRYPTFS_MAX_KEYSET_SIZE) { | ||
| 1588 | ecryptfs_printk(KERN_ERR, "Keyset too large\n"); | ||
| 1589 | rc = -EINVAL; | ||
| 1590 | goto out; | ||
| 1591 | } | 1659 | } |
| 1592 | /* TODO: Packet size limit */ | 1660 | encrypted_session_key_set: |
| 1593 | /* We have 5 bytes of surrounding packet data */ | 1661 | /* This format is inspired by OpenPGP; see RFC 2440 |
| 1594 | if ((0x05 + ECRYPTFS_SALT_SIZE | 1662 | * packet tag 3 */ |
| 1595 | + key_rec->enc_key_size) >= max) { | 1663 | max_packet_size = (1 /* Tag 3 identifier */ |
| 1596 | ecryptfs_printk(KERN_ERR, "Authentication token is too " | 1664 | + 3 /* Max Tag 3 packet size */ |
| 1597 | "large\n"); | 1665 | + 1 /* Version */ |
| 1666 | + 1 /* Cipher code */ | ||
| 1667 | + 1 /* S2K specifier */ | ||
| 1668 | + 1 /* Hash identifier */ | ||
| 1669 | + ECRYPTFS_SALT_SIZE /* Salt */ | ||
| 1670 | + 1 /* Hash iterations */ | ||
| 1671 | + key_rec->enc_key_size); /* Encrypted key size */ | ||
| 1672 | if (max_packet_size > (*remaining_bytes)) { | ||
| 1673 | printk(KERN_ERR "Packet too large; need up to [%td] bytes, but " | ||
| 1674 | "there are only [%td] available\n", max_packet_size, | ||
| 1675 | (*remaining_bytes)); | ||
| 1598 | rc = -EINVAL; | 1676 | rc = -EINVAL; |
| 1599 | goto out; | 1677 | goto out; |
| 1600 | } | 1678 | } |
| 1601 | /* This format is inspired by OpenPGP; see RFC 2440 | ||
| 1602 | * packet tag 3 */ | ||
| 1603 | dest[(*packet_size)++] = ECRYPTFS_TAG_3_PACKET_TYPE; | 1679 | dest[(*packet_size)++] = ECRYPTFS_TAG_3_PACKET_TYPE; |
| 1604 | /* ver+cipher+s2k+hash+salt+iter+enc_key */ | 1680 | /* Chop off the Tag 3 identifier(1) and Tag 3 packet size(3) |
| 1605 | rc = write_packet_length(&dest[(*packet_size)], | 1681 | * to get the number of octets in the actual Tag 3 packet */ |
| 1606 | (0x05 + ECRYPTFS_SALT_SIZE | 1682 | rc = write_packet_length(&dest[(*packet_size)], (max_packet_size - 4), |
| 1607 | + key_rec->enc_key_size), | ||
| 1608 | &packet_size_length); | 1683 | &packet_size_length); |
| 1609 | if (rc) { | 1684 | if (rc) { |
| 1610 | ecryptfs_printk(KERN_ERR, "Error generating tag 3 packet " | 1685 | printk(KERN_ERR "Error generating tag 3 packet header; cannot " |
| 1611 | "header; cannot generate packet length\n"); | 1686 | "generate packet length. rc = [%d]\n", rc); |
| 1612 | goto out; | 1687 | goto out; |
| 1613 | } | 1688 | } |
| 1614 | (*packet_size) += packet_size_length; | 1689 | (*packet_size) += packet_size_length; |
| 1615 | dest[(*packet_size)++] = 0x04; /* version 4 */ | 1690 | dest[(*packet_size)++] = 0x04; /* version 4 */ |
| 1691 | /* TODO: Break from RFC2440 so that arbitrary ciphers can be | ||
| 1692 | * specified with strings */ | ||
| 1616 | cipher_code = ecryptfs_code_for_cipher_string(crypt_stat); | 1693 | cipher_code = ecryptfs_code_for_cipher_string(crypt_stat); |
| 1617 | if (cipher_code == 0) { | 1694 | if (cipher_code == 0) { |
| 1618 | ecryptfs_printk(KERN_WARNING, "Unable to generate code for " | 1695 | ecryptfs_printk(KERN_WARNING, "Unable to generate code for " |
| @@ -1631,10 +1708,10 @@ encrypted_session_key_set: | |||
| 1631 | key_rec->enc_key_size); | 1708 | key_rec->enc_key_size); |
| 1632 | (*packet_size) += key_rec->enc_key_size; | 1709 | (*packet_size) += key_rec->enc_key_size; |
| 1633 | out: | 1710 | out: |
| 1634 | if (desc.tfm && !tfm_mutex) | ||
| 1635 | crypto_free_blkcipher(desc.tfm); | ||
| 1636 | if (rc) | 1711 | if (rc) |
| 1637 | (*packet_size) = 0; | 1712 | (*packet_size) = 0; |
| 1713 | else | ||
| 1714 | (*remaining_bytes) -= (*packet_size); | ||
| 1638 | return rc; | 1715 | return rc; |
| 1639 | } | 1716 | } |
| 1640 | 1717 | ||
| @@ -1642,7 +1719,7 @@ struct kmem_cache *ecryptfs_key_record_cache; | |||
| 1642 | 1719 | ||
| 1643 | /** | 1720 | /** |
| 1644 | * ecryptfs_generate_key_packet_set | 1721 | * ecryptfs_generate_key_packet_set |
| 1645 | * @dest: Virtual address from which to write the key record set | 1722 | * @dest_base: Virtual address from which to write the key record set |
| 1646 | * @crypt_stat: The cryptographic context from which the | 1723 | * @crypt_stat: The cryptographic context from which the |
| 1647 | * authentication tokens will be retrieved | 1724 | * authentication tokens will be retrieved |
| 1648 | * @ecryptfs_dentry: The dentry, used to retrieve the mount crypt stat | 1725 | * @ecryptfs_dentry: The dentry, used to retrieve the mount crypt stat |
| @@ -1662,24 +1739,43 @@ ecryptfs_generate_key_packet_set(char *dest_base, | |||
| 1662 | size_t max) | 1739 | size_t max) |
| 1663 | { | 1740 | { |
| 1664 | struct ecryptfs_auth_tok *auth_tok; | 1741 | struct ecryptfs_auth_tok *auth_tok; |
| 1742 | struct ecryptfs_global_auth_tok *global_auth_tok; | ||
| 1665 | struct ecryptfs_mount_crypt_stat *mount_crypt_stat = | 1743 | struct ecryptfs_mount_crypt_stat *mount_crypt_stat = |
| 1666 | &ecryptfs_superblock_to_private( | 1744 | &ecryptfs_superblock_to_private( |
| 1667 | ecryptfs_dentry->d_sb)->mount_crypt_stat; | 1745 | ecryptfs_dentry->d_sb)->mount_crypt_stat; |
| 1668 | size_t written; | 1746 | size_t written; |
| 1669 | struct ecryptfs_key_record *key_rec; | 1747 | struct ecryptfs_key_record *key_rec; |
| 1748 | struct ecryptfs_key_sig *key_sig; | ||
| 1670 | int rc = 0; | 1749 | int rc = 0; |
| 1671 | 1750 | ||
| 1672 | (*len) = 0; | 1751 | (*len) = 0; |
| 1752 | mutex_lock(&crypt_stat->keysig_list_mutex); | ||
| 1673 | key_rec = kmem_cache_alloc(ecryptfs_key_record_cache, GFP_KERNEL); | 1753 | key_rec = kmem_cache_alloc(ecryptfs_key_record_cache, GFP_KERNEL); |
| 1674 | if (!key_rec) { | 1754 | if (!key_rec) { |
| 1675 | rc = -ENOMEM; | 1755 | rc = -ENOMEM; |
| 1676 | goto out; | 1756 | goto out; |
| 1677 | } | 1757 | } |
| 1678 | if (mount_crypt_stat->global_auth_tok) { | 1758 | list_for_each_entry(key_sig, &crypt_stat->keysig_list, |
| 1679 | auth_tok = mount_crypt_stat->global_auth_tok; | 1759 | crypt_stat_list) { |
| 1760 | memset(key_rec, 0, sizeof(*key_rec)); | ||
| 1761 | rc = ecryptfs_find_global_auth_tok_for_sig(&global_auth_tok, | ||
| 1762 | mount_crypt_stat, | ||
| 1763 | key_sig->keysig); | ||
| 1764 | if (rc) { | ||
| 1765 | printk(KERN_ERR "Error attempting to get the global " | ||
| 1766 | "auth_tok; rc = [%d]\n", rc); | ||
| 1767 | goto out_free; | ||
| 1768 | } | ||
| 1769 | if (global_auth_tok->flags & ECRYPTFS_AUTH_TOK_INVALID) { | ||
| 1770 | printk(KERN_WARNING | ||
| 1771 | "Skipping invalid auth tok with sig = [%s]\n", | ||
| 1772 | global_auth_tok->sig); | ||
| 1773 | continue; | ||
| 1774 | } | ||
| 1775 | auth_tok = global_auth_tok->global_auth_tok; | ||
| 1680 | if (auth_tok->token_type == ECRYPTFS_PASSWORD) { | 1776 | if (auth_tok->token_type == ECRYPTFS_PASSWORD) { |
| 1681 | rc = write_tag_3_packet((dest_base + (*len)), | 1777 | rc = write_tag_3_packet((dest_base + (*len)), |
| 1682 | max, auth_tok, | 1778 | &max, auth_tok, |
| 1683 | crypt_stat, key_rec, | 1779 | crypt_stat, key_rec, |
| 1684 | &written); | 1780 | &written); |
| 1685 | if (rc) { | 1781 | if (rc) { |
| @@ -1689,10 +1785,9 @@ ecryptfs_generate_key_packet_set(char *dest_base, | |||
| 1689 | } | 1785 | } |
| 1690 | (*len) += written; | 1786 | (*len) += written; |
| 1691 | /* Write auth tok signature packet */ | 1787 | /* Write auth tok signature packet */ |
| 1692 | rc = write_tag_11_packet( | 1788 | rc = write_tag_11_packet((dest_base + (*len)), &max, |
| 1693 | (dest_base + (*len)), | 1789 | key_rec->sig, |
| 1694 | (max - (*len)), | 1790 | ECRYPTFS_SIG_SIZE, &written); |
| 1695 | key_rec->sig, ECRYPTFS_SIG_SIZE, &written); | ||
| 1696 | if (rc) { | 1791 | if (rc) { |
| 1697 | ecryptfs_printk(KERN_ERR, "Error writing " | 1792 | ecryptfs_printk(KERN_ERR, "Error writing " |
| 1698 | "auth tok signature packet\n"); | 1793 | "auth tok signature packet\n"); |
| @@ -1701,9 +1796,8 @@ ecryptfs_generate_key_packet_set(char *dest_base, | |||
| 1701 | (*len) += written; | 1796 | (*len) += written; |
| 1702 | } else if (auth_tok->token_type == ECRYPTFS_PRIVATE_KEY) { | 1797 | } else if (auth_tok->token_type == ECRYPTFS_PRIVATE_KEY) { |
| 1703 | rc = write_tag_1_packet(dest_base + (*len), | 1798 | rc = write_tag_1_packet(dest_base + (*len), |
| 1704 | max, auth_tok, | 1799 | &max, auth_tok, |
| 1705 | crypt_stat,mount_crypt_stat, | 1800 | crypt_stat, key_rec, &written); |
| 1706 | key_rec, &written); | ||
| 1707 | if (rc) { | 1801 | if (rc) { |
| 1708 | ecryptfs_printk(KERN_WARNING, "Error " | 1802 | ecryptfs_printk(KERN_WARNING, "Error " |
| 1709 | "writing tag 1 packet\n"); | 1803 | "writing tag 1 packet\n"); |
| @@ -1716,19 +1810,69 @@ ecryptfs_generate_key_packet_set(char *dest_base, | |||
| 1716 | rc = -EINVAL; | 1810 | rc = -EINVAL; |
| 1717 | goto out_free; | 1811 | goto out_free; |
| 1718 | } | 1812 | } |
| 1719 | } else | 1813 | } |
| 1720 | BUG(); | 1814 | if (likely(max > 0)) { |
| 1721 | if (likely((max - (*len)) > 0)) { | ||
| 1722 | dest_base[(*len)] = 0x00; | 1815 | dest_base[(*len)] = 0x00; |
| 1723 | } else { | 1816 | } else { |
| 1724 | ecryptfs_printk(KERN_ERR, "Error writing boundary byte\n"); | 1817 | ecryptfs_printk(KERN_ERR, "Error writing boundary byte\n"); |
| 1725 | rc = -EIO; | 1818 | rc = -EIO; |
| 1726 | } | 1819 | } |
| 1727 | |||
| 1728 | out_free: | 1820 | out_free: |
| 1729 | kmem_cache_free(ecryptfs_key_record_cache, key_rec); | 1821 | kmem_cache_free(ecryptfs_key_record_cache, key_rec); |
| 1730 | out: | 1822 | out: |
| 1731 | if (rc) | 1823 | if (rc) |
| 1732 | (*len) = 0; | 1824 | (*len) = 0; |
| 1825 | mutex_unlock(&crypt_stat->keysig_list_mutex); | ||
| 1733 | return rc; | 1826 | return rc; |
| 1734 | } | 1827 | } |
| 1828 | |||
| 1829 | struct kmem_cache *ecryptfs_key_sig_cache; | ||
| 1830 | |||
| 1831 | int ecryptfs_add_keysig(struct ecryptfs_crypt_stat *crypt_stat, char *sig) | ||
| 1832 | { | ||
| 1833 | struct ecryptfs_key_sig *new_key_sig; | ||
| 1834 | int rc = 0; | ||
| 1835 | |||
| 1836 | new_key_sig = kmem_cache_alloc(ecryptfs_key_sig_cache, GFP_KERNEL); | ||
| 1837 | if (!new_key_sig) { | ||
| 1838 | rc = -ENOMEM; | ||
| 1839 | printk(KERN_ERR | ||
| 1840 | "Error allocating from ecryptfs_key_sig_cache\n"); | ||
| 1841 | goto out; | ||
| 1842 | } | ||
| 1843 | memcpy(new_key_sig->keysig, sig, ECRYPTFS_SIG_SIZE_HEX); | ||
| 1844 | mutex_lock(&crypt_stat->keysig_list_mutex); | ||
| 1845 | list_add(&new_key_sig->crypt_stat_list, &crypt_stat->keysig_list); | ||
| 1846 | mutex_unlock(&crypt_stat->keysig_list_mutex); | ||
| 1847 | out: | ||
| 1848 | return rc; | ||
| 1849 | } | ||
| 1850 | |||
| 1851 | struct kmem_cache *ecryptfs_global_auth_tok_cache; | ||
| 1852 | |||
| 1853 | int | ||
| 1854 | ecryptfs_add_global_auth_tok(struct ecryptfs_mount_crypt_stat *mount_crypt_stat, | ||
| 1855 | char *sig) | ||
| 1856 | { | ||
| 1857 | struct ecryptfs_global_auth_tok *new_auth_tok; | ||
| 1858 | int rc = 0; | ||
| 1859 | |||
| 1860 | new_auth_tok = kmem_cache_alloc(ecryptfs_global_auth_tok_cache, | ||
| 1861 | GFP_KERNEL); | ||
| 1862 | if (!new_auth_tok) { | ||
| 1863 | rc = -ENOMEM; | ||
| 1864 | printk(KERN_ERR "Error allocating from " | ||
| 1865 | "ecryptfs_global_auth_tok_cache\n"); | ||
| 1866 | goto out; | ||
| 1867 | } | ||
| 1868 | memcpy(new_auth_tok->sig, sig, ECRYPTFS_SIG_SIZE_HEX); | ||
| 1869 | new_auth_tok->sig[ECRYPTFS_SIG_SIZE_HEX] = '\0'; | ||
| 1870 | mutex_lock(&mount_crypt_stat->global_auth_tok_list_mutex); | ||
| 1871 | list_add(&new_auth_tok->mount_crypt_stat_list, | ||
| 1872 | &mount_crypt_stat->global_auth_tok_list); | ||
| 1873 | mount_crypt_stat->num_global_auth_toks++; | ||
| 1874 | mutex_unlock(&mount_crypt_stat->global_auth_tok_list_mutex); | ||
| 1875 | out: | ||
| 1876 | return rc; | ||
| 1877 | } | ||
| 1878 | |||
