1 // SPDX-License-Identifier: GPL-2.0-or-later 2 /* 3 * eCryptfs: Linux filesystem encryption layer 4 * In-kernel key management code. Includes functions to parse and 5 * write authentication token-related packets with the underlying 6 * file. 7 * 8 * Copyright (C) 2004-2006 International Business Machines Corp. 9 * Author(s): Michael A. Halcrow <mhalcrow@us.ibm.com> 10 * Michael C. Thompson <mcthomps@us.ibm.com> 11 * Trevor S. Highland <trevor.highland@gmail.com> 12 */ 13 14 #include <crypto/skcipher.h> 15 #include <linux/string.h> 16 #include <linux/pagemap.h> 17 #include <linux/key.h> 18 #include <linux/random.h> 19 #include <linux/scatterlist.h> 20 #include <linux/slab.h> 21 #include "ecryptfs_kernel.h" 22 23 /* 24 * request_key returned an error instead of a valid key address; 25 * determine the type of error, make appropriate log entries, and 26 * return an error code. 27 */ 28 static int process_request_key_err(long err_code) 29 { 30 int rc = 0; 31 32 switch (err_code) { 33 case -ENOKEY: 34 ecryptfs_printk(KERN_WARNING, "No key\n"); 35 rc = -ENOENT; 36 break; 37 case -EKEYEXPIRED: 38 ecryptfs_printk(KERN_WARNING, "Key expired\n"); 39 rc = -ETIME; 40 break; 41 case -EKEYREVOKED: 42 ecryptfs_printk(KERN_WARNING, "Key revoked\n"); 43 rc = -EINVAL; 44 break; 45 default: 46 ecryptfs_printk(KERN_WARNING, "Unknown error code: " 47 "[0x%.16lx]\n", err_code); 48 rc = -EINVAL; 49 } 50 return rc; 51 } 52 53 static int process_find_global_auth_tok_for_sig_err(int err_code) 54 { 55 int rc = err_code; 56 57 switch (err_code) { 58 case -ENOENT: 59 ecryptfs_printk(KERN_WARNING, "Missing auth tok\n"); 60 break; 61 case -EINVAL: 62 ecryptfs_printk(KERN_WARNING, "Invalid auth tok\n"); 63 break; 64 default: 65 rc = process_request_key_err(err_code); 66 break; 67 } 68 return rc; 69 } 70 71 /** 72 * ecryptfs_parse_packet_length 73 * @data: Pointer to memory containing length at offset 74 * @size: This function writes the decoded size to this memory 75 * address; zero on error 76 * @length_size: The number of bytes occupied by the encoded length 77 * 78 * Returns zero on success; non-zero on error 79 */ 80 int ecryptfs_parse_packet_length(unsigned char *data, size_t *size, 81 size_t *length_size) 82 { 83 int rc = 0; 84 85 (*length_size) = 0; 86 (*size) = 0; 87 if (data[0] < 192) { 88 /* One-byte length */ 89 (*size) = data[0]; 90 (*length_size) = 1; 91 } else if (data[0] < 224) { 92 /* Two-byte length */ 93 (*size) = (data[0] - 192) * 256; 94 (*size) += data[1] + 192; 95 (*length_size) = 2; 96 } else if (data[0] == 255) { 97 /* If support is added, adjust ECRYPTFS_MAX_PKT_LEN_SIZE */ 98 ecryptfs_printk(KERN_ERR, "Five-byte packet length not " 99 "supported\n"); 100 rc = -EINVAL; 101 goto out; 102 } else { 103 ecryptfs_printk(KERN_ERR, "Error parsing packet length\n"); 104 rc = -EINVAL; 105 goto out; 106 } 107 out: 108 return rc; 109 } 110 111 /** 112 * ecryptfs_write_packet_length 113 * @dest: The byte array target into which to write the length. Must 114 * have at least ECRYPTFS_MAX_PKT_LEN_SIZE bytes allocated. 115 * @size: The length to write. 116 * @packet_size_length: The number of bytes used to encode the packet 117 * length is written to this address. 118 * 119 * Returns zero on success; non-zero on error. 120 */ 121 int ecryptfs_write_packet_length(char *dest, size_t size, 122 size_t *packet_size_length) 123 { 124 int rc = 0; 125 126 if (size < 192) { 127 dest[0] = size; 128 (*packet_size_length) = 1; 129 } else if (size < 65536) { 130 dest[0] = (((size - 192) / 256) + 192); 131 dest[1] = ((size - 192) % 256); 132 (*packet_size_length) = 2; 133 } else { 134 /* If support is added, adjust ECRYPTFS_MAX_PKT_LEN_SIZE */ 135 rc = -EINVAL; 136 ecryptfs_printk(KERN_WARNING, 137 "Unsupported packet size: [%zd]\n", size); 138 } 139 return rc; 140 } 141 142 static int 143 write_tag_64_packet(char *signature, struct ecryptfs_session_key *session_key, 144 char **packet, size_t *packet_len) 145 { 146 size_t i = 0; 147 size_t data_len; 148 size_t packet_size_len; 149 char *message; 150 int rc; 151 152 /* 153 * ***** TAG 64 Packet Format ***** 154 * | Content Type | 1 byte | 155 * | Key Identifier Size | 1 or 2 bytes | 156 * | Key Identifier | arbitrary | 157 * | Encrypted File Encryption Key Size | 1 or 2 bytes | 158 * | Encrypted File Encryption Key | arbitrary | 159 */ 160 data_len = (5 + ECRYPTFS_SIG_SIZE_HEX 161 + session_key->encrypted_key_size); 162 *packet = kmalloc(data_len, GFP_KERNEL); 163 message = *packet; 164 if (!message) { 165 ecryptfs_printk(KERN_ERR, "Unable to allocate memory\n"); 166 rc = -ENOMEM; 167 goto out; 168 } 169 message[i++] = ECRYPTFS_TAG_64_PACKET_TYPE; 170 rc = ecryptfs_write_packet_length(&message[i], ECRYPTFS_SIG_SIZE_HEX, 171 &packet_size_len); 172 if (rc) { 173 ecryptfs_printk(KERN_ERR, "Error generating tag 64 packet " 174 "header; cannot generate packet length\n"); 175 goto out; 176 } 177 i += packet_size_len; 178 memcpy(&message[i], signature, ECRYPTFS_SIG_SIZE_HEX); 179 i += ECRYPTFS_SIG_SIZE_HEX; 180 rc = ecryptfs_write_packet_length(&message[i], 181 session_key->encrypted_key_size, 182 &packet_size_len); 183 if (rc) { 184 ecryptfs_printk(KERN_ERR, "Error generating tag 64 packet " 185 "header; cannot generate packet length\n"); 186 goto out; 187 } 188 i += packet_size_len; 189 memcpy(&message[i], session_key->encrypted_key, 190 session_key->encrypted_key_size); 191 i += session_key->encrypted_key_size; 192 *packet_len = i; 193 out: 194 return rc; 195 } 196 197 static int 198 parse_tag_65_packet(struct ecryptfs_session_key *session_key, u8 *cipher_code, 199 struct ecryptfs_message *msg) 200 { 201 size_t i = 0; 202 char *data; 203 size_t data_len; 204 size_t m_size; 205 size_t message_len; 206 u16 checksum = 0; 207 u16 expected_checksum = 0; 208 int rc; 209 210 /* 211 * ***** TAG 65 Packet Format ***** 212 * | Content Type | 1 byte | 213 * | Status Indicator | 1 byte | 214 * | File Encryption Key Size | 1 or 2 bytes | 215 * | File Encryption Key | arbitrary | 216 */ 217 message_len = msg->data_len; 218 data = msg->data; 219 if (message_len < 4) { 220 rc = -EIO; 221 goto out; 222 } 223 if (data[i++] != ECRYPTFS_TAG_65_PACKET_TYPE) { 224 ecryptfs_printk(KERN_ERR, "Type should be ECRYPTFS_TAG_65\n"); 225 rc = -EIO; 226 goto out; 227 } 228 if (data[i++]) { 229 ecryptfs_printk(KERN_ERR, "Status indicator has non-zero value " 230 "[%d]\n", data[i-1]); 231 rc = -EIO; 232 goto out; 233 } 234 rc = ecryptfs_parse_packet_length(&data[i], &m_size, &data_len); 235 if (rc) { 236 ecryptfs_printk(KERN_WARNING, "Error parsing packet length; " 237 "rc = [%d]\n", rc); 238 goto out; 239 } 240 i += data_len; 241 if (message_len < (i + m_size)) { 242 ecryptfs_printk(KERN_ERR, "The message received from ecryptfsd " 243 "is shorter than expected\n"); 244 rc = -EIO; 245 goto out; 246 } 247 if (m_size < 3) { 248 ecryptfs_printk(KERN_ERR, 249 "The decrypted key is not long enough to " 250 "include a cipher code and checksum\n"); 251 rc = -EIO; 252 goto out; 253 } 254 *cipher_code = data[i++]; 255 /* The decrypted key includes 1 byte cipher code and 2 byte checksum */ 256 session_key->decrypted_key_size = m_size - 3; 257 if (session_key->decrypted_key_size > ECRYPTFS_MAX_KEY_BYTES) { 258 ecryptfs_printk(KERN_ERR, "key_size [%d] larger than " 259 "the maximum key size [%d]\n", 260 session_key->decrypted_key_size, 261 ECRYPTFS_MAX_ENCRYPTED_KEY_BYTES); 262 rc = -EIO; 263 goto out; 264 } 265 memcpy(session_key->decrypted_key, &data[i], 266 session_key->decrypted_key_size); 267 i += session_key->decrypted_key_size; 268 expected_checksum += (unsigned char)(data[i++]) << 8; 269 expected_checksum += (unsigned char)(data[i++]); 270 for (i = 0; i < session_key->decrypted_key_size; i++) 271 checksum += session_key->decrypted_key[i]; 272 if (expected_checksum != checksum) { 273 ecryptfs_printk(KERN_ERR, "Invalid checksum for file " 274 "encryption key; expected [%x]; calculated " 275 "[%x]\n", expected_checksum, checksum); 276 rc = -EIO; 277 } 278 out: 279 return rc; 280 } 281 282 283 static int 284 write_tag_66_packet(char *signature, u8 cipher_code, 285 struct ecryptfs_crypt_stat *crypt_stat, char **packet, 286 size_t *packet_len) 287 { 288 size_t i = 0; 289 size_t j; 290 size_t data_len; 291 size_t checksum = 0; 292 size_t packet_size_len; 293 char *message; 294 int rc; 295 296 /* 297 * ***** TAG 66 Packet Format ***** 298 * | Content Type | 1 byte | 299 * | Key Identifier Size | 1 or 2 bytes | 300 * | Key Identifier | arbitrary | 301 * | File Encryption Key Size | 1 or 2 bytes | 302 * | Cipher Code | 1 byte | 303 * | File Encryption Key | arbitrary | 304 * | Checksum | 2 bytes | 305 */ 306 data_len = (8 + ECRYPTFS_SIG_SIZE_HEX + crypt_stat->key_size); 307 *packet = kmalloc(data_len, GFP_KERNEL); 308 message = *packet; 309 if (!message) { 310 ecryptfs_printk(KERN_ERR, "Unable to allocate memory\n"); 311 rc = -ENOMEM; 312 goto out; 313 } 314 message[i++] = ECRYPTFS_TAG_66_PACKET_TYPE; 315 rc = ecryptfs_write_packet_length(&message[i], ECRYPTFS_SIG_SIZE_HEX, 316 &packet_size_len); 317 if (rc) { 318 ecryptfs_printk(KERN_ERR, "Error generating tag 66 packet " 319 "header; cannot generate packet length\n"); 320 goto out; 321 } 322 i += packet_size_len; 323 memcpy(&message[i], signature, ECRYPTFS_SIG_SIZE_HEX); 324 i += ECRYPTFS_SIG_SIZE_HEX; 325 /* The encrypted key includes 1 byte cipher code and 2 byte checksum */ 326 rc = ecryptfs_write_packet_length(&message[i], crypt_stat->key_size + 3, 327 &packet_size_len); 328 if (rc) { 329 ecryptfs_printk(KERN_ERR, "Error generating tag 66 packet " 330 "header; cannot generate packet length\n"); 331 goto out; 332 } 333 i += packet_size_len; 334 message[i++] = cipher_code; 335 memcpy(&message[i], crypt_stat->key, crypt_stat->key_size); 336 i += crypt_stat->key_size; 337 for (j = 0; j < crypt_stat->key_size; j++) 338 checksum += crypt_stat->key[j]; 339 message[i++] = (checksum / 256) % 256; 340 message[i++] = (checksum % 256); 341 *packet_len = i; 342 out: 343 return rc; 344 } 345 346 static int 347 parse_tag_67_packet(struct ecryptfs_key_record *key_rec, 348 struct ecryptfs_message *msg) 349 { 350 size_t i = 0; 351 char *data; 352 size_t data_len; 353 size_t message_len; 354 int rc; 355 356 /* 357 * ***** TAG 67 Packet Format ***** 358 * | Content Type | 1 byte | 359 * | Status Indicator | 1 byte | 360 * | Encrypted File Encryption Key Size | 1 or 2 bytes | 361 * | Encrypted File Encryption Key | arbitrary | 362 */ 363 message_len = msg->data_len; 364 data = msg->data; 365 /* verify that everything through the encrypted FEK size is present */ 366 if (message_len < 4) { 367 rc = -EIO; 368 printk(KERN_ERR "%s: message_len is [%zd]; minimum acceptable " 369 "message length is [%d]\n", __func__, message_len, 4); 370 goto out; 371 } 372 if (data[i++] != ECRYPTFS_TAG_67_PACKET_TYPE) { 373 rc = -EIO; 374 printk(KERN_ERR "%s: Type should be ECRYPTFS_TAG_67\n", 375 __func__); 376 goto out; 377 } 378 if (data[i++]) { 379 rc = -EIO; 380 printk(KERN_ERR "%s: Status indicator has non zero " 381 "value [%d]\n", __func__, data[i-1]); 382 383 goto out; 384 } 385 rc = ecryptfs_parse_packet_length(&data[i], &key_rec->enc_key_size, 386 &data_len); 387 if (rc) { 388 ecryptfs_printk(KERN_WARNING, "Error parsing packet length; " 389 "rc = [%d]\n", rc); 390 goto out; 391 } 392 i += data_len; 393 if (message_len < (i + key_rec->enc_key_size)) { 394 rc = -EIO; 395 printk(KERN_ERR "%s: message_len [%zd]; max len is [%zd]\n", 396 __func__, message_len, (i + key_rec->enc_key_size)); 397 goto out; 398 } 399 if (key_rec->enc_key_size > ECRYPTFS_MAX_ENCRYPTED_KEY_BYTES) { 400 rc = -EIO; 401 printk(KERN_ERR "%s: Encrypted key_size [%zd] larger than " 402 "the maximum key size [%d]\n", __func__, 403 key_rec->enc_key_size, 404 ECRYPTFS_MAX_ENCRYPTED_KEY_BYTES); 405 goto out; 406 } 407 memcpy(key_rec->enc_key, &data[i], key_rec->enc_key_size); 408 out: 409 return rc; 410 } 411 412 /** 413 * ecryptfs_verify_version 414 * @version: The version number to confirm 415 * 416 * Returns zero on good version; non-zero otherwise 417 */ 418 static int ecryptfs_verify_version(u16 version) 419 { 420 int rc = 0; 421 unsigned char major; 422 unsigned char minor; 423 424 major = ((version >> 8) & 0xFF); 425 minor = (version & 0xFF); 426 if (major != ECRYPTFS_VERSION_MAJOR) { 427 ecryptfs_printk(KERN_ERR, "Major version number mismatch. " 428 "Expected [%d]; got [%d]\n", 429 ECRYPTFS_VERSION_MAJOR, major); 430 rc = -EINVAL; 431 goto out; 432 } 433 if (minor != ECRYPTFS_VERSION_MINOR) { 434 ecryptfs_printk(KERN_ERR, "Minor version number mismatch. " 435 "Expected [%d]; got [%d]\n", 436 ECRYPTFS_VERSION_MINOR, minor); 437 rc = -EINVAL; 438 goto out; 439 } 440 out: 441 return rc; 442 } 443 444 /** 445 * ecryptfs_verify_auth_tok_from_key 446 * @auth_tok_key: key containing the authentication token 447 * @auth_tok: authentication token 448 * 449 * Returns zero on valid auth tok; -EINVAL if the payload is invalid; or 450 * -EKEYREVOKED if the key was revoked before we acquired its semaphore. 451 */ 452 static int 453 ecryptfs_verify_auth_tok_from_key(struct key *auth_tok_key, 454 struct ecryptfs_auth_tok **auth_tok) 455 { 456 int rc = 0; 457 458 (*auth_tok) = ecryptfs_get_key_payload_data(auth_tok_key); 459 if (IS_ERR(*auth_tok)) { 460 rc = PTR_ERR(*auth_tok); 461 *auth_tok = NULL; 462 goto out; 463 } 464 465 if (ecryptfs_verify_version((*auth_tok)->version)) { 466 printk(KERN_ERR "Data structure version mismatch. Userspace " 467 "tools must match eCryptfs kernel module with major " 468 "version [%d] and minor version [%d]\n", 469 ECRYPTFS_VERSION_MAJOR, ECRYPTFS_VERSION_MINOR); 470 rc = -EINVAL; 471 goto out; 472 } 473 if ((*auth_tok)->token_type != ECRYPTFS_PASSWORD 474 && (*auth_tok)->token_type != ECRYPTFS_PRIVATE_KEY) { 475 printk(KERN_ERR "Invalid auth_tok structure " 476 "returned from key query\n"); 477 rc = -EINVAL; 478 goto out; 479 } 480 out: 481 return rc; 482 } 483 484 static int 485 ecryptfs_find_global_auth_tok_for_sig( 486 struct key **auth_tok_key, 487 struct ecryptfs_auth_tok **auth_tok, 488 struct ecryptfs_mount_crypt_stat *mount_crypt_stat, char *sig) 489 { 490 struct ecryptfs_global_auth_tok *walker; 491 int rc = 0; 492 493 (*auth_tok_key) = NULL; 494 (*auth_tok) = NULL; 495 mutex_lock(&mount_crypt_stat->global_auth_tok_list_mutex); 496 list_for_each_entry(walker, 497 &mount_crypt_stat->global_auth_tok_list, 498 mount_crypt_stat_list) { 499 if (memcmp(walker->sig, sig, ECRYPTFS_SIG_SIZE_HEX)) 500 continue; 501 502 if (walker->flags & ECRYPTFS_AUTH_TOK_INVALID) { 503 rc = -EINVAL; 504 goto out; 505 } 506 507 rc = key_validate(walker->global_auth_tok_key); 508 if (rc) { 509 if (rc == -EKEYEXPIRED) 510 goto out; 511 goto out_invalid_auth_tok; 512 } 513 514 down_write(&(walker->global_auth_tok_key->sem)); 515 rc = ecryptfs_verify_auth_tok_from_key( 516 walker->global_auth_tok_key, auth_tok); 517 if (rc) 518 goto out_invalid_auth_tok_unlock; 519 520 (*auth_tok_key) = walker->global_auth_tok_key; 521 key_get(*auth_tok_key); 522 goto out; 523 } 524 rc = -ENOENT; 525 goto out; 526 out_invalid_auth_tok_unlock: 527 up_write(&(walker->global_auth_tok_key->sem)); 528 out_invalid_auth_tok: 529 printk(KERN_WARNING "Invalidating auth tok with sig = [%s]\n", sig); 530 walker->flags |= ECRYPTFS_AUTH_TOK_INVALID; 531 key_put(walker->global_auth_tok_key); 532 walker->global_auth_tok_key = NULL; 533 out: 534 mutex_unlock(&mount_crypt_stat->global_auth_tok_list_mutex); 535 return rc; 536 } 537 538 /** 539 * ecryptfs_find_auth_tok_for_sig 540 * @auth_tok_key: key containing the authentication token 541 * @auth_tok: Set to the matching auth_tok; NULL if not found 542 * @mount_crypt_stat: inode crypt_stat crypto context 543 * @sig: Sig of auth_tok to find 544 * 545 * For now, this function simply looks at the registered auth_tok's 546 * linked off the mount_crypt_stat, so all the auth_toks that can be 547 * used must be registered at mount time. This function could 548 * potentially try a lot harder to find auth_tok's (e.g., by calling 549 * out to ecryptfsd to dynamically retrieve an auth_tok object) so 550 * that static registration of auth_tok's will no longer be necessary. 551 * 552 * Returns zero on no error; non-zero on error 553 */ 554 static int 555 ecryptfs_find_auth_tok_for_sig( 556 struct key **auth_tok_key, 557 struct ecryptfs_auth_tok **auth_tok, 558 struct ecryptfs_mount_crypt_stat *mount_crypt_stat, 559 char *sig) 560 { 561 int rc = 0; 562 563 rc = ecryptfs_find_global_auth_tok_for_sig(auth_tok_key, auth_tok, 564 mount_crypt_stat, sig); 565 if (rc == -ENOENT) { 566 /* if the flag ECRYPTFS_GLOBAL_MOUNT_AUTH_TOK_ONLY is set in the 567 * mount_crypt_stat structure, we prevent to use auth toks that 568 * are not inserted through the ecryptfs_add_global_auth_tok 569 * function. 570 */ 571 if (mount_crypt_stat->flags 572 & ECRYPTFS_GLOBAL_MOUNT_AUTH_TOK_ONLY) 573 return -EINVAL; 574 575 rc = ecryptfs_keyring_auth_tok_for_sig(auth_tok_key, auth_tok, 576 sig); 577 } 578 return rc; 579 } 580 581 /* 582 * write_tag_70_packet can gobble a lot of stack space. We stuff most 583 * of the function's parameters in a kmalloc'd struct to help reduce 584 * eCryptfs' overall stack usage. 585 */ 586 struct ecryptfs_write_tag_70_packet_silly_stack { 587 u8 cipher_code; 588 size_t max_packet_size; 589 size_t packet_size_len; 590 size_t block_aligned_filename_size; 591 size_t block_size; 592 size_t i; 593 size_t j; 594 size_t num_rand_bytes; 595 struct mutex *tfm_mutex; 596 char *block_aligned_filename; 597 struct ecryptfs_auth_tok *auth_tok; 598 struct scatterlist src_sg[2]; 599 struct scatterlist dst_sg[2]; 600 struct crypto_skcipher *skcipher_tfm; 601 struct skcipher_request *skcipher_req; 602 char iv[ECRYPTFS_MAX_IV_BYTES]; 603 char hash[MD5_DIGEST_SIZE]; 604 }; 605 606 /* 607 * write_tag_70_packet - Write encrypted filename (EFN) packet against FNEK 608 * @filename: NULL-terminated filename string 609 * 610 * This is the simplest mechanism for achieving filename encryption in 611 * eCryptfs. It encrypts the given filename with the mount-wide 612 * filename encryption key (FNEK) and stores it in a packet to @dest, 613 * which the callee will encode and write directly into the dentry 614 * name. 615 */ 616 int 617 ecryptfs_write_tag_70_packet(char *dest, size_t *remaining_bytes, 618 size_t *packet_size, 619 struct ecryptfs_mount_crypt_stat *mount_crypt_stat, 620 char *filename, size_t filename_size) 621 { 622 struct ecryptfs_write_tag_70_packet_silly_stack *s; 623 struct key *auth_tok_key = NULL; 624 int rc = 0; 625 626 s = kzalloc_obj(*s, GFP_KERNEL); 627 if (!s) 628 return -ENOMEM; 629 630 (*packet_size) = 0; 631 rc = ecryptfs_find_auth_tok_for_sig( 632 &auth_tok_key, 633 &s->auth_tok, mount_crypt_stat, 634 mount_crypt_stat->global_default_fnek_sig); 635 if (rc) { 636 printk(KERN_ERR "%s: Error attempting to find auth tok for " 637 "fnek sig [%s]; rc = [%d]\n", __func__, 638 mount_crypt_stat->global_default_fnek_sig, rc); 639 goto out; 640 } 641 rc = ecryptfs_get_tfm_and_mutex_for_cipher_name( 642 &s->skcipher_tfm, 643 &s->tfm_mutex, mount_crypt_stat->global_default_fn_cipher_name); 644 if (unlikely(rc)) { 645 printk(KERN_ERR "Internal error whilst attempting to get " 646 "tfm and mutex for cipher name [%s]; rc = [%d]\n", 647 mount_crypt_stat->global_default_fn_cipher_name, rc); 648 goto out; 649 } 650 mutex_lock(s->tfm_mutex); 651 s->block_size = crypto_skcipher_blocksize(s->skcipher_tfm); 652 /* Plus one for the \0 separator between the random prefix 653 * and the plaintext filename */ 654 s->num_rand_bytes = (ECRYPTFS_FILENAME_MIN_RANDOM_PREPEND_BYTES + 1); 655 s->block_aligned_filename_size = (s->num_rand_bytes + filename_size); 656 if ((s->block_aligned_filename_size % s->block_size) != 0) { 657 s->num_rand_bytes += (s->block_size 658 - (s->block_aligned_filename_size 659 % s->block_size)); 660 s->block_aligned_filename_size = (s->num_rand_bytes 661 + filename_size); 662 } 663 /* Octet 0: Tag 70 identifier 664 * Octets 1-N1: Tag 70 packet size (includes cipher identifier 665 * and block-aligned encrypted filename size) 666 * Octets N1-N2: FNEK sig (ECRYPTFS_SIG_SIZE) 667 * Octet N2-N3: Cipher identifier (1 octet) 668 * Octets N3-N4: Block-aligned encrypted filename 669 * - Consists of a minimum number of random characters, a \0 670 * separator, and then the filename */ 671 s->max_packet_size = (ECRYPTFS_TAG_70_MAX_METADATA_SIZE 672 + s->block_aligned_filename_size); 673 if (!dest) { 674 (*packet_size) = s->max_packet_size; 675 goto out_unlock; 676 } 677 if (s->max_packet_size > (*remaining_bytes)) { 678 printk(KERN_WARNING "%s: Require [%zd] bytes to write; only " 679 "[%zd] available\n", __func__, s->max_packet_size, 680 (*remaining_bytes)); 681 rc = -EINVAL; 682 goto out_unlock; 683 } 684 685 s->skcipher_req = skcipher_request_alloc(s->skcipher_tfm, GFP_KERNEL); 686 if (!s->skcipher_req) { 687 printk(KERN_ERR "%s: Out of kernel memory whilst attempting to " 688 "skcipher_request_alloc for %s\n", __func__, 689 crypto_skcipher_driver_name(s->skcipher_tfm)); 690 rc = -ENOMEM; 691 goto out_unlock; 692 } 693 694 skcipher_request_set_callback(s->skcipher_req, 695 CRYPTO_TFM_REQ_MAY_SLEEP, NULL, NULL); 696 697 s->block_aligned_filename = kzalloc(s->block_aligned_filename_size, 698 GFP_KERNEL); 699 if (!s->block_aligned_filename) { 700 rc = -ENOMEM; 701 goto out_unlock; 702 } 703 dest[s->i++] = ECRYPTFS_TAG_70_PACKET_TYPE; 704 rc = ecryptfs_write_packet_length(&dest[s->i], 705 (ECRYPTFS_SIG_SIZE 706 + 1 /* Cipher code */ 707 + s->block_aligned_filename_size), 708 &s->packet_size_len); 709 if (rc) { 710 printk(KERN_ERR "%s: Error generating tag 70 packet " 711 "header; cannot generate packet length; rc = [%d]\n", 712 __func__, rc); 713 goto out_free_unlock; 714 } 715 s->i += s->packet_size_len; 716 ecryptfs_from_hex(&dest[s->i], 717 mount_crypt_stat->global_default_fnek_sig, 718 ECRYPTFS_SIG_SIZE); 719 s->i += ECRYPTFS_SIG_SIZE; 720 s->cipher_code = ecryptfs_code_for_cipher_string( 721 mount_crypt_stat->global_default_fn_cipher_name, 722 mount_crypt_stat->global_default_fn_cipher_key_bytes); 723 if (s->cipher_code == 0) { 724 printk(KERN_WARNING "%s: Unable to generate code for " 725 "cipher [%s] with key bytes [%zd]\n", __func__, 726 mount_crypt_stat->global_default_fn_cipher_name, 727 mount_crypt_stat->global_default_fn_cipher_key_bytes); 728 rc = -EINVAL; 729 goto out_free_unlock; 730 } 731 dest[s->i++] = s->cipher_code; 732 /* TODO: Support other key modules than passphrase for 733 * filename encryption */ 734 if (s->auth_tok->token_type != ECRYPTFS_PASSWORD) { 735 rc = -EOPNOTSUPP; 736 printk(KERN_INFO "%s: Filename encryption only supports " 737 "password tokens\n", __func__); 738 goto out_free_unlock; 739 } 740 741 md5(s->auth_tok->token.password.session_key_encryption_key, 742 s->auth_tok->token.password.session_key_encryption_key_bytes, 743 s->hash); 744 for (s->j = 0; s->j < (s->num_rand_bytes - 1); s->j++) { 745 s->block_aligned_filename[s->j] = 746 s->hash[s->j % MD5_DIGEST_SIZE]; 747 if ((s->j % MD5_DIGEST_SIZE) == (MD5_DIGEST_SIZE - 1)) 748 md5(s->hash, MD5_DIGEST_SIZE, s->hash); 749 if (s->block_aligned_filename[s->j] == '\0') 750 s->block_aligned_filename[s->j] = ECRYPTFS_NON_NULL; 751 } 752 memcpy(&s->block_aligned_filename[s->num_rand_bytes], filename, 753 filename_size); 754 rc = virt_to_scatterlist(s->block_aligned_filename, 755 s->block_aligned_filename_size, s->src_sg, 2); 756 if (rc < 1) { 757 printk(KERN_ERR "%s: Internal error whilst attempting to " 758 "convert filename memory to scatterlist; rc = [%d]. " 759 "block_aligned_filename_size = [%zd]\n", __func__, rc, 760 s->block_aligned_filename_size); 761 goto out_free_unlock; 762 } 763 rc = virt_to_scatterlist(&dest[s->i], s->block_aligned_filename_size, 764 s->dst_sg, 2); 765 if (rc < 1) { 766 printk(KERN_ERR "%s: Internal error whilst attempting to " 767 "convert encrypted filename memory to scatterlist; " 768 "rc = [%d]. block_aligned_filename_size = [%zd]\n", 769 __func__, rc, s->block_aligned_filename_size); 770 goto out_free_unlock; 771 } 772 /* The characters in the first block effectively do the job 773 * of the IV here, so we just use 0's for the IV. Note the 774 * constraint that ECRYPTFS_FILENAME_MIN_RANDOM_PREPEND_BYTES 775 * >= ECRYPTFS_MAX_IV_BYTES. */ 776 rc = crypto_skcipher_setkey( 777 s->skcipher_tfm, 778 s->auth_tok->token.password.session_key_encryption_key, 779 mount_crypt_stat->global_default_fn_cipher_key_bytes); 780 if (rc < 0) { 781 printk(KERN_ERR "%s: Error setting key for crypto context; " 782 "rc = [%d]. s->auth_tok->token.password.session_key_" 783 "encryption_key = [0x%p]; mount_crypt_stat->" 784 "global_default_fn_cipher_key_bytes = [%zd]\n", __func__, 785 rc, 786 s->auth_tok->token.password.session_key_encryption_key, 787 mount_crypt_stat->global_default_fn_cipher_key_bytes); 788 goto out_free_unlock; 789 } 790 skcipher_request_set_crypt(s->skcipher_req, s->src_sg, s->dst_sg, 791 s->block_aligned_filename_size, s->iv); 792 rc = crypto_skcipher_encrypt(s->skcipher_req); 793 if (rc) { 794 printk(KERN_ERR "%s: Error attempting to encrypt filename; " 795 "rc = [%d]\n", __func__, rc); 796 goto out_free_unlock; 797 } 798 s->i += s->block_aligned_filename_size; 799 (*packet_size) = s->i; 800 (*remaining_bytes) -= (*packet_size); 801 out_free_unlock: 802 kfree_sensitive(s->block_aligned_filename); 803 out_unlock: 804 mutex_unlock(s->tfm_mutex); 805 out: 806 if (auth_tok_key) { 807 up_write(&(auth_tok_key->sem)); 808 key_put(auth_tok_key); 809 } 810 skcipher_request_free(s->skcipher_req); 811 kfree(s); 812 return rc; 813 } 814 815 struct ecryptfs_parse_tag_70_packet_silly_stack { 816 u8 cipher_code; 817 size_t max_packet_size; 818 size_t packet_size_len; 819 size_t parsed_tag_70_packet_size; 820 size_t block_aligned_filename_size; 821 size_t block_size; 822 size_t i; 823 struct mutex *tfm_mutex; 824 char *decrypted_filename; 825 struct ecryptfs_auth_tok *auth_tok; 826 struct scatterlist src_sg[2]; 827 struct scatterlist dst_sg[2]; 828 struct crypto_skcipher *skcipher_tfm; 829 struct skcipher_request *skcipher_req; 830 char fnek_sig_hex[ECRYPTFS_SIG_SIZE_HEX + 1]; 831 char iv[ECRYPTFS_MAX_IV_BYTES]; 832 char cipher_string[ECRYPTFS_MAX_CIPHER_NAME_SIZE + 1]; 833 }; 834 835 /** 836 * ecryptfs_parse_tag_70_packet - Parse and process FNEK-encrypted passphrase packet 837 * @filename: This function kmalloc's the memory for the filename 838 * @filename_size: This function sets this to the amount of memory 839 * kmalloc'd for the filename 840 * @packet_size: This function sets this to the number of octets 841 * in the packet parsed 842 * @mount_crypt_stat: The mount-wide cryptographic context 843 * @data: The memory location containing the start of the tag 70 844 * packet 845 * @max_packet_size: The maximum legal size of the packet to be parsed 846 * from @data 847 * 848 * Returns zero on success; non-zero otherwise 849 */ 850 int 851 ecryptfs_parse_tag_70_packet(char **filename, size_t *filename_size, 852 size_t *packet_size, 853 struct ecryptfs_mount_crypt_stat *mount_crypt_stat, 854 char *data, size_t max_packet_size) 855 { 856 struct ecryptfs_parse_tag_70_packet_silly_stack *s; 857 struct key *auth_tok_key = NULL; 858 int rc = 0; 859 860 (*packet_size) = 0; 861 (*filename_size) = 0; 862 (*filename) = NULL; 863 s = kzalloc_obj(*s, GFP_KERNEL); 864 if (!s) 865 return -ENOMEM; 866 867 if (max_packet_size < ECRYPTFS_TAG_70_MIN_METADATA_SIZE) { 868 printk(KERN_WARNING "%s: max_packet_size is [%zd]; it must be " 869 "at least [%d]\n", __func__, max_packet_size, 870 ECRYPTFS_TAG_70_MIN_METADATA_SIZE); 871 rc = -EINVAL; 872 goto out; 873 } 874 /* Octet 0: Tag 70 identifier 875 * Octets 1-N1: Tag 70 packet size (includes cipher identifier 876 * and block-aligned encrypted filename size) 877 * Octets N1-N2: FNEK sig (ECRYPTFS_SIG_SIZE) 878 * Octet N2-N3: Cipher identifier (1 octet) 879 * Octets N3-N4: Block-aligned encrypted filename 880 * - Consists of a minimum number of random numbers, a \0 881 * separator, and then the filename */ 882 if (data[(*packet_size)++] != ECRYPTFS_TAG_70_PACKET_TYPE) { 883 printk(KERN_WARNING "%s: Invalid packet tag [0x%.2x]; must be " 884 "tag [0x%.2x]\n", __func__, 885 data[((*packet_size) - 1)], ECRYPTFS_TAG_70_PACKET_TYPE); 886 rc = -EINVAL; 887 goto out; 888 } 889 rc = ecryptfs_parse_packet_length(&data[(*packet_size)], 890 &s->parsed_tag_70_packet_size, 891 &s->packet_size_len); 892 if (rc) { 893 printk(KERN_WARNING "%s: Error parsing packet length; " 894 "rc = [%d]\n", __func__, rc); 895 goto out; 896 } 897 s->block_aligned_filename_size = (s->parsed_tag_70_packet_size 898 - ECRYPTFS_SIG_SIZE - 1); 899 if ((1 + s->packet_size_len + s->parsed_tag_70_packet_size) 900 > max_packet_size) { 901 printk(KERN_WARNING "%s: max_packet_size is [%zd]; real packet " 902 "size is [%zd]\n", __func__, max_packet_size, 903 (1 + s->packet_size_len + 1 904 + s->block_aligned_filename_size)); 905 rc = -EINVAL; 906 goto out; 907 } 908 (*packet_size) += s->packet_size_len; 909 ecryptfs_to_hex(s->fnek_sig_hex, &data[(*packet_size)], 910 ECRYPTFS_SIG_SIZE); 911 (*packet_size) += ECRYPTFS_SIG_SIZE; 912 s->cipher_code = data[(*packet_size)++]; 913 rc = ecryptfs_cipher_code_to_string(s->cipher_string, 914 sizeof(s->cipher_string), 915 s->cipher_code); 916 if (rc) { 917 printk(KERN_WARNING "%s: Cipher code [%d] is invalid\n", 918 __func__, s->cipher_code); 919 goto out; 920 } 921 rc = ecryptfs_find_auth_tok_for_sig(&auth_tok_key, 922 &s->auth_tok, mount_crypt_stat, 923 s->fnek_sig_hex); 924 if (rc) { 925 printk(KERN_ERR "%s: Error attempting to find auth tok for " 926 "fnek sig [%s]; rc = [%d]\n", __func__, s->fnek_sig_hex, 927 rc); 928 goto out; 929 } 930 rc = ecryptfs_get_tfm_and_mutex_for_cipher_name(&s->skcipher_tfm, 931 &s->tfm_mutex, 932 s->cipher_string); 933 if (unlikely(rc)) { 934 printk(KERN_ERR "Internal error whilst attempting to get " 935 "tfm and mutex for cipher name [%s]; rc = [%d]\n", 936 s->cipher_string, rc); 937 goto out; 938 } 939 mutex_lock(s->tfm_mutex); 940 rc = virt_to_scatterlist(&data[(*packet_size)], 941 s->block_aligned_filename_size, s->src_sg, 2); 942 if (rc < 1) { 943 printk(KERN_ERR "%s: Internal error whilst attempting to " 944 "convert encrypted filename memory to scatterlist; " 945 "rc = [%d]. block_aligned_filename_size = [%zd]\n", 946 __func__, rc, s->block_aligned_filename_size); 947 goto out_unlock; 948 } 949 (*packet_size) += s->block_aligned_filename_size; 950 s->decrypted_filename = kmalloc(s->block_aligned_filename_size, 951 GFP_KERNEL); 952 if (!s->decrypted_filename) { 953 rc = -ENOMEM; 954 goto out_unlock; 955 } 956 rc = virt_to_scatterlist(s->decrypted_filename, 957 s->block_aligned_filename_size, s->dst_sg, 2); 958 if (rc < 1) { 959 printk(KERN_ERR "%s: Internal error whilst attempting to " 960 "convert decrypted filename memory to scatterlist; " 961 "rc = [%d]. block_aligned_filename_size = [%zd]\n", 962 __func__, rc, s->block_aligned_filename_size); 963 goto out_free_unlock; 964 } 965 966 s->skcipher_req = skcipher_request_alloc(s->skcipher_tfm, GFP_KERNEL); 967 if (!s->skcipher_req) { 968 printk(KERN_ERR "%s: Out of kernel memory whilst attempting to " 969 "skcipher_request_alloc for %s\n", __func__, 970 crypto_skcipher_driver_name(s->skcipher_tfm)); 971 rc = -ENOMEM; 972 goto out_free_unlock; 973 } 974 975 skcipher_request_set_callback(s->skcipher_req, 976 CRYPTO_TFM_REQ_MAY_SLEEP, NULL, NULL); 977 978 /* The characters in the first block effectively do the job of 979 * the IV here, so we just use 0's for the IV. Note the 980 * constraint that ECRYPTFS_FILENAME_MIN_RANDOM_PREPEND_BYTES 981 * >= ECRYPTFS_MAX_IV_BYTES. */ 982 /* TODO: Support other key modules than passphrase for 983 * filename encryption */ 984 if (s->auth_tok->token_type != ECRYPTFS_PASSWORD) { 985 rc = -EOPNOTSUPP; 986 printk(KERN_INFO "%s: Filename encryption only supports " 987 "password tokens\n", __func__); 988 goto out_free_unlock; 989 } 990 rc = crypto_skcipher_setkey( 991 s->skcipher_tfm, 992 s->auth_tok->token.password.session_key_encryption_key, 993 mount_crypt_stat->global_default_fn_cipher_key_bytes); 994 if (rc < 0) { 995 printk(KERN_ERR "%s: Error setting key for crypto context; " 996 "rc = [%d]. s->auth_tok->token.password.session_key_" 997 "encryption_key = [0x%p]; mount_crypt_stat->" 998 "global_default_fn_cipher_key_bytes = [%zd]\n", __func__, 999 rc, 1000 s->auth_tok->token.password.session_key_encryption_key, 1001 mount_crypt_stat->global_default_fn_cipher_key_bytes); 1002 goto out_free_unlock; 1003 } 1004 skcipher_request_set_crypt(s->skcipher_req, s->src_sg, s->dst_sg, 1005 s->block_aligned_filename_size, s->iv); 1006 rc = crypto_skcipher_decrypt(s->skcipher_req); 1007 if (rc) { 1008 printk(KERN_ERR "%s: Error attempting to decrypt filename; " 1009 "rc = [%d]\n", __func__, rc); 1010 goto out_free_unlock; 1011 } 1012 1013 while (s->i < s->block_aligned_filename_size && 1014 s->decrypted_filename[s->i] != '\0') 1015 s->i++; 1016 if (s->i == s->block_aligned_filename_size) { 1017 printk(KERN_WARNING "%s: Invalid tag 70 packet; could not " 1018 "find valid separator between random characters and " 1019 "the filename\n", __func__); 1020 rc = -EINVAL; 1021 goto out_free_unlock; 1022 } 1023 s->i++; 1024 (*filename_size) = (s->block_aligned_filename_size - s->i); 1025 if (!((*filename_size) > 0 && (*filename_size < PATH_MAX))) { 1026 printk(KERN_WARNING "%s: Filename size is [%zd], which is " 1027 "invalid\n", __func__, (*filename_size)); 1028 rc = -EINVAL; 1029 goto out_free_unlock; 1030 } 1031 (*filename) = kmalloc(((*filename_size) + 1), GFP_KERNEL); 1032 if (!(*filename)) { 1033 rc = -ENOMEM; 1034 goto out_free_unlock; 1035 } 1036 memcpy((*filename), &s->decrypted_filename[s->i], (*filename_size)); 1037 (*filename)[(*filename_size)] = '\0'; 1038 out_free_unlock: 1039 kfree(s->decrypted_filename); 1040 out_unlock: 1041 mutex_unlock(s->tfm_mutex); 1042 out: 1043 if (rc) { 1044 (*packet_size) = 0; 1045 (*filename_size) = 0; 1046 (*filename) = NULL; 1047 } 1048 if (auth_tok_key) { 1049 up_write(&(auth_tok_key->sem)); 1050 key_put(auth_tok_key); 1051 } 1052 skcipher_request_free(s->skcipher_req); 1053 kfree(s); 1054 return rc; 1055 } 1056 1057 static int 1058 ecryptfs_get_auth_tok_sig(char **sig, struct ecryptfs_auth_tok *auth_tok) 1059 { 1060 int rc = 0; 1061 1062 (*sig) = NULL; 1063 switch (auth_tok->token_type) { 1064 case ECRYPTFS_PASSWORD: 1065 (*sig) = auth_tok->token.password.signature; 1066 break; 1067 case ECRYPTFS_PRIVATE_KEY: 1068 (*sig) = auth_tok->token.private_key.signature; 1069 break; 1070 default: 1071 printk(KERN_ERR "Cannot get sig for auth_tok of type [%d]\n", 1072 auth_tok->token_type); 1073 rc = -EINVAL; 1074 } 1075 return rc; 1076 } 1077 1078 /** 1079 * decrypt_pki_encrypted_session_key - Decrypt the session key with the given auth_tok. 1080 * @auth_tok: The key authentication token used to decrypt the session key 1081 * @crypt_stat: The cryptographic context 1082 * 1083 * Returns zero on success; non-zero error otherwise. 1084 */ 1085 static int 1086 decrypt_pki_encrypted_session_key(struct ecryptfs_auth_tok *auth_tok, 1087 struct ecryptfs_crypt_stat *crypt_stat) 1088 { 1089 u8 cipher_code = 0; 1090 struct ecryptfs_msg_ctx *msg_ctx; 1091 struct ecryptfs_message *msg = NULL; 1092 char *auth_tok_sig; 1093 char *payload = NULL; 1094 size_t payload_len = 0; 1095 int rc; 1096 1097 rc = ecryptfs_get_auth_tok_sig(&auth_tok_sig, auth_tok); 1098 if (rc) { 1099 printk(KERN_ERR "Unrecognized auth tok type: [%d]\n", 1100 auth_tok->token_type); 1101 goto out; 1102 } 1103 rc = write_tag_64_packet(auth_tok_sig, &(auth_tok->session_key), 1104 &payload, &payload_len); 1105 if (rc) { 1106 ecryptfs_printk(KERN_ERR, "Failed to write tag 64 packet\n"); 1107 goto out; 1108 } 1109 rc = ecryptfs_send_message(payload, payload_len, &msg_ctx); 1110 if (rc) { 1111 ecryptfs_printk(KERN_ERR, "Error sending message to " 1112 "ecryptfsd: %d\n", rc); 1113 goto out; 1114 } 1115 rc = ecryptfs_wait_for_response(msg_ctx, &msg); 1116 if (rc) { 1117 ecryptfs_printk(KERN_ERR, "Failed to receive tag 65 packet " 1118 "from the user space daemon\n"); 1119 rc = -EIO; 1120 goto out; 1121 } 1122 rc = parse_tag_65_packet(&(auth_tok->session_key), 1123 &cipher_code, msg); 1124 if (rc) { 1125 printk(KERN_ERR "Failed to parse tag 65 packet; rc = [%d]\n", 1126 rc); 1127 goto out; 1128 } 1129 auth_tok->session_key.flags |= ECRYPTFS_CONTAINS_DECRYPTED_KEY; 1130 memcpy(crypt_stat->key, auth_tok->session_key.decrypted_key, 1131 auth_tok->session_key.decrypted_key_size); 1132 crypt_stat->key_size = auth_tok->session_key.decrypted_key_size; 1133 rc = ecryptfs_cipher_code_to_string(crypt_stat->cipher, 1134 sizeof(crypt_stat->cipher), 1135 cipher_code); 1136 if (rc) { 1137 ecryptfs_printk(KERN_ERR, "Cipher code [%d] is invalid\n", 1138 cipher_code); 1139 goto out; 1140 } 1141 crypt_stat->flags |= ECRYPTFS_KEY_VALID; 1142 if (ecryptfs_verbosity > 0) { 1143 ecryptfs_printk(KERN_DEBUG, "Decrypted session key:\n"); 1144 ecryptfs_dump_hex(crypt_stat->key, 1145 crypt_stat->key_size); 1146 } 1147 out: 1148 kfree(msg); 1149 kfree(payload); 1150 return rc; 1151 } 1152 1153 static void wipe_auth_tok_list(struct list_head *auth_tok_list_head) 1154 { 1155 struct ecryptfs_auth_tok_list_item *auth_tok_list_item; 1156 struct ecryptfs_auth_tok_list_item *auth_tok_list_item_tmp; 1157 1158 list_for_each_entry_safe(auth_tok_list_item, auth_tok_list_item_tmp, 1159 auth_tok_list_head, list) { 1160 list_del(&auth_tok_list_item->list); 1161 kmem_cache_free(ecryptfs_auth_tok_list_item_cache, 1162 auth_tok_list_item); 1163 } 1164 } 1165 1166 struct kmem_cache *ecryptfs_auth_tok_list_item_cache; 1167 1168 /** 1169 * parse_tag_1_packet 1170 * @crypt_stat: The cryptographic context to modify based on packet contents 1171 * @data: The raw bytes of the packet. 1172 * @auth_tok_list: eCryptfs parses packets into authentication tokens; 1173 * a new authentication token will be placed at the 1174 * end of this list for this packet. 1175 * @new_auth_tok: Pointer to a pointer to memory that this function 1176 * allocates; sets the memory address of the pointer to 1177 * NULL on error. This object is added to the 1178 * auth_tok_list. 1179 * @packet_size: This function writes the size of the parsed packet 1180 * into this memory location; zero on error. 1181 * @max_packet_size: The maximum allowable packet size 1182 * 1183 * Returns zero on success; non-zero on error. 1184 */ 1185 static int 1186 parse_tag_1_packet(struct ecryptfs_crypt_stat *crypt_stat, 1187 unsigned char *data, struct list_head *auth_tok_list, 1188 struct ecryptfs_auth_tok **new_auth_tok, 1189 size_t *packet_size, size_t max_packet_size) 1190 { 1191 size_t body_size; 1192 struct ecryptfs_auth_tok_list_item *auth_tok_list_item; 1193 size_t length_size; 1194 int rc = 0; 1195 1196 (*packet_size) = 0; 1197 (*new_auth_tok) = NULL; 1198 /** 1199 * This format is inspired by OpenPGP; see RFC 2440 1200 * packet tag 1 1201 * 1202 * Tag 1 identifier (1 byte) 1203 * Max Tag 1 packet size (max 3 bytes) 1204 * Version (1 byte) 1205 * Key identifier (8 bytes; ECRYPTFS_SIG_SIZE) 1206 * Cipher identifier (1 byte) 1207 * Encrypted key size (arbitrary) 1208 * 1209 * 12 bytes minimum packet size 1210 */ 1211 if (unlikely(max_packet_size < 12)) { 1212 printk(KERN_ERR "Invalid max packet size; must be >=12\n"); 1213 rc = -EINVAL; 1214 goto out; 1215 } 1216 if (data[(*packet_size)++] != ECRYPTFS_TAG_1_PACKET_TYPE) { 1217 printk(KERN_ERR "Enter w/ first byte != 0x%.2x\n", 1218 ECRYPTFS_TAG_1_PACKET_TYPE); 1219 rc = -EINVAL; 1220 goto out; 1221 } 1222 /* Released: wipe_auth_tok_list called in ecryptfs_parse_packet_set or 1223 * at end of function upon failure */ 1224 auth_tok_list_item = 1225 kmem_cache_zalloc(ecryptfs_auth_tok_list_item_cache, 1226 GFP_KERNEL); 1227 if (!auth_tok_list_item) { 1228 printk(KERN_ERR "Unable to allocate memory\n"); 1229 rc = -ENOMEM; 1230 goto out; 1231 } 1232 (*new_auth_tok) = &auth_tok_list_item->auth_tok; 1233 rc = ecryptfs_parse_packet_length(&data[(*packet_size)], &body_size, 1234 &length_size); 1235 if (rc) { 1236 printk(KERN_WARNING "Error parsing packet length; " 1237 "rc = [%d]\n", rc); 1238 goto out_free; 1239 } 1240 if (unlikely(body_size < (ECRYPTFS_SIG_SIZE + 2))) { 1241 printk(KERN_WARNING "Invalid body size ([%td])\n", body_size); 1242 rc = -EINVAL; 1243 goto out_free; 1244 } 1245 (*packet_size) += length_size; 1246 if (unlikely((*packet_size) + body_size > max_packet_size)) { 1247 printk(KERN_WARNING "Packet size exceeds max\n"); 1248 rc = -EINVAL; 1249 goto out_free; 1250 } 1251 if (unlikely(data[(*packet_size)++] != 0x03)) { 1252 printk(KERN_WARNING "Unknown version number [%d]\n", 1253 data[(*packet_size) - 1]); 1254 rc = -EINVAL; 1255 goto out_free; 1256 } 1257 ecryptfs_to_hex((*new_auth_tok)->token.private_key.signature, 1258 &data[(*packet_size)], ECRYPTFS_SIG_SIZE); 1259 *packet_size += ECRYPTFS_SIG_SIZE; 1260 /* This byte is skipped because the kernel does not need to 1261 * know which public key encryption algorithm was used */ 1262 (*packet_size)++; 1263 (*new_auth_tok)->session_key.encrypted_key_size = 1264 body_size - (ECRYPTFS_SIG_SIZE + 2); 1265 if ((*new_auth_tok)->session_key.encrypted_key_size 1266 > ECRYPTFS_MAX_ENCRYPTED_KEY_BYTES) { 1267 printk(KERN_WARNING "Tag 1 packet contains key larger " 1268 "than ECRYPTFS_MAX_ENCRYPTED_KEY_BYTES\n"); 1269 rc = -EINVAL; 1270 goto out_free; 1271 } 1272 memcpy((*new_auth_tok)->session_key.encrypted_key, 1273 &data[(*packet_size)], (body_size - (ECRYPTFS_SIG_SIZE + 2))); 1274 (*packet_size) += (*new_auth_tok)->session_key.encrypted_key_size; 1275 (*new_auth_tok)->session_key.flags &= 1276 ~ECRYPTFS_CONTAINS_DECRYPTED_KEY; 1277 (*new_auth_tok)->session_key.flags |= 1278 ECRYPTFS_CONTAINS_ENCRYPTED_KEY; 1279 (*new_auth_tok)->token_type = ECRYPTFS_PRIVATE_KEY; 1280 (*new_auth_tok)->flags = 0; 1281 (*new_auth_tok)->session_key.flags &= 1282 ~(ECRYPTFS_USERSPACE_SHOULD_TRY_TO_DECRYPT); 1283 (*new_auth_tok)->session_key.flags &= 1284 ~(ECRYPTFS_USERSPACE_SHOULD_TRY_TO_ENCRYPT); 1285 list_add(&auth_tok_list_item->list, auth_tok_list); 1286 goto out; 1287 out_free: 1288 (*new_auth_tok) = NULL; 1289 memset(auth_tok_list_item, 0, 1290 sizeof(struct ecryptfs_auth_tok_list_item)); 1291 kmem_cache_free(ecryptfs_auth_tok_list_item_cache, 1292 auth_tok_list_item); 1293 out: 1294 if (rc) 1295 (*packet_size) = 0; 1296 return rc; 1297 } 1298 1299 /** 1300 * parse_tag_3_packet 1301 * @crypt_stat: The cryptographic context to modify based on packet 1302 * contents. 1303 * @data: The raw bytes of the packet. 1304 * @auth_tok_list: eCryptfs parses packets into authentication tokens; 1305 * a new authentication token will be placed at the end 1306 * of this list for this packet. 1307 * @new_auth_tok: Pointer to a pointer to memory that this function 1308 * allocates; sets the memory address of the pointer to 1309 * NULL on error. This object is added to the 1310 * auth_tok_list. 1311 * @packet_size: This function writes the size of the parsed packet 1312 * into this memory location; zero on error. 1313 * @max_packet_size: maximum number of bytes to parse 1314 * 1315 * Returns zero on success; non-zero on error. 1316 */ 1317 static int 1318 parse_tag_3_packet(struct ecryptfs_crypt_stat *crypt_stat, 1319 unsigned char *data, struct list_head *auth_tok_list, 1320 struct ecryptfs_auth_tok **new_auth_tok, 1321 size_t *packet_size, size_t max_packet_size) 1322 { 1323 size_t body_size; 1324 struct ecryptfs_auth_tok_list_item *auth_tok_list_item; 1325 size_t length_size; 1326 int rc = 0; 1327 1328 (*packet_size) = 0; 1329 (*new_auth_tok) = NULL; 1330 /** 1331 *This format is inspired by OpenPGP; see RFC 2440 1332 * packet tag 3 1333 * 1334 * Tag 3 identifier (1 byte) 1335 * Max Tag 3 packet size (max 3 bytes) 1336 * Version (1 byte) 1337 * Cipher code (1 byte) 1338 * S2K specifier (1 byte) 1339 * Hash identifier (1 byte) 1340 * Salt (ECRYPTFS_SALT_SIZE) 1341 * Hash iterations (1 byte) 1342 * Encrypted key (arbitrary) 1343 * 1344 * (ECRYPTFS_SALT_SIZE + 7) minimum packet size 1345 */ 1346 if (max_packet_size < (ECRYPTFS_SALT_SIZE + 7)) { 1347 printk(KERN_ERR "Max packet size too large\n"); 1348 rc = -EINVAL; 1349 goto out; 1350 } 1351 if (data[(*packet_size)++] != ECRYPTFS_TAG_3_PACKET_TYPE) { 1352 printk(KERN_ERR "First byte != 0x%.2x; invalid packet\n", 1353 ECRYPTFS_TAG_3_PACKET_TYPE); 1354 rc = -EINVAL; 1355 goto out; 1356 } 1357 /* Released: wipe_auth_tok_list called in ecryptfs_parse_packet_set or 1358 * at end of function upon failure */ 1359 auth_tok_list_item = 1360 kmem_cache_zalloc(ecryptfs_auth_tok_list_item_cache, GFP_KERNEL); 1361 if (!auth_tok_list_item) { 1362 printk(KERN_ERR "Unable to allocate memory\n"); 1363 rc = -ENOMEM; 1364 goto out; 1365 } 1366 (*new_auth_tok) = &auth_tok_list_item->auth_tok; 1367 rc = ecryptfs_parse_packet_length(&data[(*packet_size)], &body_size, 1368 &length_size); 1369 if (rc) { 1370 printk(KERN_WARNING "Error parsing packet length; rc = [%d]\n", 1371 rc); 1372 goto out_free; 1373 } 1374 if (unlikely(body_size < (ECRYPTFS_SALT_SIZE + 5))) { 1375 printk(KERN_WARNING "Invalid body size ([%td])\n", body_size); 1376 rc = -EINVAL; 1377 goto out_free; 1378 } 1379 (*packet_size) += length_size; 1380 if (unlikely((*packet_size) + body_size > max_packet_size)) { 1381 printk(KERN_ERR "Packet size exceeds max\n"); 1382 rc = -EINVAL; 1383 goto out_free; 1384 } 1385 (*new_auth_tok)->session_key.encrypted_key_size = 1386 (body_size - (ECRYPTFS_SALT_SIZE + 5)); 1387 if ((*new_auth_tok)->session_key.encrypted_key_size 1388 > ECRYPTFS_MAX_ENCRYPTED_KEY_BYTES) { 1389 printk(KERN_WARNING "Tag 3 packet contains key larger " 1390 "than ECRYPTFS_MAX_ENCRYPTED_KEY_BYTES\n"); 1391 rc = -EINVAL; 1392 goto out_free; 1393 } 1394 if (unlikely(data[(*packet_size)++] != 0x04)) { 1395 printk(KERN_WARNING "Unknown version number [%d]\n", 1396 data[(*packet_size) - 1]); 1397 rc = -EINVAL; 1398 goto out_free; 1399 } 1400 rc = ecryptfs_cipher_code_to_string(crypt_stat->cipher, 1401 sizeof(crypt_stat->cipher), 1402 (u16)data[(*packet_size)]); 1403 if (rc) 1404 goto out_free; 1405 /* A little extra work to differentiate among the AES key 1406 * sizes; see RFC2440 */ 1407 switch(data[(*packet_size)++]) { 1408 case RFC2440_CIPHER_AES_192: 1409 crypt_stat->key_size = 24; 1410 break; 1411 default: 1412 crypt_stat->key_size = 1413 (*new_auth_tok)->session_key.encrypted_key_size; 1414 } 1415 rc = ecryptfs_init_crypt_ctx(crypt_stat); 1416 if (rc) 1417 goto out_free; 1418 if (unlikely(data[(*packet_size)++] != 0x03)) { 1419 printk(KERN_WARNING "Only S2K ID 3 is currently supported\n"); 1420 rc = -ENOSYS; 1421 goto out_free; 1422 } 1423 /* TODO: finish the hash mapping */ 1424 switch (data[(*packet_size)++]) { 1425 case 0x01: /* See RFC2440 for these numbers and their mappings */ 1426 /* Choose MD5 */ 1427 memcpy((*new_auth_tok)->token.password.salt, 1428 &data[(*packet_size)], ECRYPTFS_SALT_SIZE); 1429 (*packet_size) += ECRYPTFS_SALT_SIZE; 1430 /* This conversion was taken straight from RFC2440 */ 1431 (*new_auth_tok)->token.password.hash_iterations = 1432 ((u32) 16 + (data[(*packet_size)] & 15)) 1433 << ((data[(*packet_size)] >> 4) + 6); 1434 (*packet_size)++; 1435 /* Friendly reminder: 1436 * (*new_auth_tok)->session_key.encrypted_key_size = 1437 * (body_size - (ECRYPTFS_SALT_SIZE + 5)); */ 1438 memcpy((*new_auth_tok)->session_key.encrypted_key, 1439 &data[(*packet_size)], 1440 (*new_auth_tok)->session_key.encrypted_key_size); 1441 (*packet_size) += 1442 (*new_auth_tok)->session_key.encrypted_key_size; 1443 (*new_auth_tok)->session_key.flags &= 1444 ~ECRYPTFS_CONTAINS_DECRYPTED_KEY; 1445 (*new_auth_tok)->session_key.flags |= 1446 ECRYPTFS_CONTAINS_ENCRYPTED_KEY; 1447 (*new_auth_tok)->token.password.hash_algo = 0x01; /* MD5 */ 1448 break; 1449 default: 1450 ecryptfs_printk(KERN_ERR, "Unsupported hash algorithm: " 1451 "[%d]\n", data[(*packet_size) - 1]); 1452 rc = -ENOSYS; 1453 goto out_free; 1454 } 1455 (*new_auth_tok)->token_type = ECRYPTFS_PASSWORD; 1456 /* TODO: Parametarize; we might actually want userspace to 1457 * decrypt the session key. */ 1458 (*new_auth_tok)->session_key.flags &= 1459 ~(ECRYPTFS_USERSPACE_SHOULD_TRY_TO_DECRYPT); 1460 (*new_auth_tok)->session_key.flags &= 1461 ~(ECRYPTFS_USERSPACE_SHOULD_TRY_TO_ENCRYPT); 1462 list_add(&auth_tok_list_item->list, auth_tok_list); 1463 goto out; 1464 out_free: 1465 (*new_auth_tok) = NULL; 1466 memset(auth_tok_list_item, 0, 1467 sizeof(struct ecryptfs_auth_tok_list_item)); 1468 kmem_cache_free(ecryptfs_auth_tok_list_item_cache, 1469 auth_tok_list_item); 1470 out: 1471 if (rc) 1472 (*packet_size) = 0; 1473 return rc; 1474 } 1475 1476 /** 1477 * parse_tag_11_packet 1478 * @data: The raw bytes of the packet 1479 * @contents: This function writes the data contents of the literal 1480 * packet into this memory location 1481 * @max_contents_bytes: The maximum number of bytes that this function 1482 * is allowed to write into contents 1483 * @tag_11_contents_size: This function writes the size of the parsed 1484 * contents into this memory location; zero on 1485 * error 1486 * @packet_size: This function writes the size of the parsed packet 1487 * into this memory location; zero on error 1488 * @max_packet_size: maximum number of bytes to parse 1489 * 1490 * Returns zero on success; non-zero on error. 1491 */ 1492 static int 1493 parse_tag_11_packet(unsigned char *data, unsigned char *contents, 1494 size_t max_contents_bytes, size_t *tag_11_contents_size, 1495 size_t *packet_size, size_t max_packet_size) 1496 { 1497 size_t body_size; 1498 size_t length_size; 1499 int rc = 0; 1500 1501 (*packet_size) = 0; 1502 (*tag_11_contents_size) = 0; 1503 /* This format is inspired by OpenPGP; see RFC 2440 1504 * packet tag 11 1505 * 1506 * Tag 11 identifier (1 byte) 1507 * Max Tag 11 packet size (max 3 bytes) 1508 * Binary format specifier (1 byte) 1509 * Filename length (1 byte) 1510 * Filename ("_CONSOLE") (8 bytes) 1511 * Modification date (4 bytes) 1512 * Literal data (arbitrary) 1513 * 1514 * We need at least 16 bytes of data for the packet to even be 1515 * valid. 1516 */ 1517 if (max_packet_size < 16) { 1518 printk(KERN_ERR "Maximum packet size too small\n"); 1519 rc = -EINVAL; 1520 goto out; 1521 } 1522 if (data[(*packet_size)++] != ECRYPTFS_TAG_11_PACKET_TYPE) { 1523 printk(KERN_WARNING "Invalid tag 11 packet format\n"); 1524 rc = -EINVAL; 1525 goto out; 1526 } 1527 rc = ecryptfs_parse_packet_length(&data[(*packet_size)], &body_size, 1528 &length_size); 1529 if (rc) { 1530 printk(KERN_WARNING "Invalid tag 11 packet format\n"); 1531 goto out; 1532 } 1533 if (body_size < 14) { 1534 printk(KERN_WARNING "Invalid body size ([%td])\n", body_size); 1535 rc = -EINVAL; 1536 goto out; 1537 } 1538 (*packet_size) += length_size; 1539 (*tag_11_contents_size) = (body_size - 14); 1540 if (unlikely((*packet_size) + body_size + 1 > max_packet_size)) { 1541 printk(KERN_ERR "Packet size exceeds max\n"); 1542 rc = -EINVAL; 1543 goto out; 1544 } 1545 if (unlikely((*tag_11_contents_size) > max_contents_bytes)) { 1546 printk(KERN_ERR "Literal data section in tag 11 packet exceeds " 1547 "expected size\n"); 1548 rc = -EINVAL; 1549 goto out; 1550 } 1551 if (data[(*packet_size)++] != 0x62) { 1552 printk(KERN_WARNING "Unrecognizable packet\n"); 1553 rc = -EINVAL; 1554 goto out; 1555 } 1556 if (data[(*packet_size)++] != 0x08) { 1557 printk(KERN_WARNING "Unrecognizable packet\n"); 1558 rc = -EINVAL; 1559 goto out; 1560 } 1561 (*packet_size) += 12; /* Ignore filename and modification date */ 1562 memcpy(contents, &data[(*packet_size)], (*tag_11_contents_size)); 1563 (*packet_size) += (*tag_11_contents_size); 1564 out: 1565 if (rc) { 1566 (*packet_size) = 0; 1567 (*tag_11_contents_size) = 0; 1568 } 1569 return rc; 1570 } 1571 1572 int ecryptfs_keyring_auth_tok_for_sig(struct key **auth_tok_key, 1573 struct ecryptfs_auth_tok **auth_tok, 1574 char *sig) 1575 { 1576 int rc = 0; 1577 1578 (*auth_tok_key) = request_key(&key_type_user, sig, NULL); 1579 if (IS_ERR(*auth_tok_key)) { 1580 (*auth_tok_key) = ecryptfs_get_encrypted_key(sig); 1581 if (IS_ERR(*auth_tok_key)) { 1582 printk(KERN_ERR "Could not find key with description: [%s]\n", 1583 sig); 1584 rc = process_request_key_err(PTR_ERR(*auth_tok_key)); 1585 (*auth_tok_key) = NULL; 1586 goto out; 1587 } 1588 } 1589 down_write(&(*auth_tok_key)->sem); 1590 rc = ecryptfs_verify_auth_tok_from_key(*auth_tok_key, auth_tok); 1591 if (rc) { 1592 up_write(&(*auth_tok_key)->sem); 1593 key_put(*auth_tok_key); 1594 (*auth_tok_key) = NULL; 1595 goto out; 1596 } 1597 out: 1598 return rc; 1599 } 1600 1601 /** 1602 * decrypt_passphrase_encrypted_session_key - Decrypt the session key with the given auth_tok. 1603 * @auth_tok: The passphrase authentication token to use to encrypt the FEK 1604 * @crypt_stat: The cryptographic context 1605 * 1606 * Returns zero on success; non-zero error otherwise 1607 */ 1608 static int 1609 decrypt_passphrase_encrypted_session_key(struct ecryptfs_auth_tok *auth_tok, 1610 struct ecryptfs_crypt_stat *crypt_stat) 1611 { 1612 struct scatterlist dst_sg[2]; 1613 struct scatterlist src_sg[2]; 1614 struct mutex *tfm_mutex; 1615 struct crypto_skcipher *tfm; 1616 struct skcipher_request *req = NULL; 1617 int rc = 0; 1618 1619 if (unlikely(ecryptfs_verbosity > 0)) { 1620 ecryptfs_printk( 1621 KERN_DEBUG, "Session key encryption key (size [%d]):\n", 1622 auth_tok->token.password.session_key_encryption_key_bytes); 1623 ecryptfs_dump_hex( 1624 auth_tok->token.password.session_key_encryption_key, 1625 auth_tok->token.password.session_key_encryption_key_bytes); 1626 } 1627 rc = ecryptfs_get_tfm_and_mutex_for_cipher_name(&tfm, &tfm_mutex, 1628 crypt_stat->cipher); 1629 if (unlikely(rc)) { 1630 printk(KERN_ERR "Internal error whilst attempting to get " 1631 "tfm and mutex for cipher name [%s]; rc = [%d]\n", 1632 crypt_stat->cipher, rc); 1633 goto out; 1634 } 1635 rc = virt_to_scatterlist(auth_tok->session_key.encrypted_key, 1636 auth_tok->session_key.encrypted_key_size, 1637 src_sg, 2); 1638 if (rc < 1 || rc > 2) { 1639 printk(KERN_ERR "Internal error whilst attempting to convert " 1640 "auth_tok->session_key.encrypted_key to scatterlist; " 1641 "expected rc = 1; got rc = [%d]. " 1642 "auth_tok->session_key.encrypted_key_size = [%d]\n", rc, 1643 auth_tok->session_key.encrypted_key_size); 1644 goto out; 1645 } 1646 auth_tok->session_key.decrypted_key_size = 1647 auth_tok->session_key.encrypted_key_size; 1648 rc = virt_to_scatterlist(auth_tok->session_key.decrypted_key, 1649 auth_tok->session_key.decrypted_key_size, 1650 dst_sg, 2); 1651 if (rc < 1 || rc > 2) { 1652 printk(KERN_ERR "Internal error whilst attempting to convert " 1653 "auth_tok->session_key.decrypted_key to scatterlist; " 1654 "expected rc = 1; got rc = [%d]\n", rc); 1655 goto out; 1656 } 1657 mutex_lock(tfm_mutex); 1658 req = skcipher_request_alloc(tfm, GFP_KERNEL); 1659 if (!req) { 1660 mutex_unlock(tfm_mutex); 1661 printk(KERN_ERR "%s: Out of kernel memory whilst attempting to " 1662 "skcipher_request_alloc for %s\n", __func__, 1663 crypto_skcipher_driver_name(tfm)); 1664 rc = -ENOMEM; 1665 goto out; 1666 } 1667 1668 skcipher_request_set_callback(req, CRYPTO_TFM_REQ_MAY_SLEEP, 1669 NULL, NULL); 1670 rc = crypto_skcipher_setkey( 1671 tfm, auth_tok->token.password.session_key_encryption_key, 1672 crypt_stat->key_size); 1673 if (unlikely(rc < 0)) { 1674 mutex_unlock(tfm_mutex); 1675 printk(KERN_ERR "Error setting key for crypto context\n"); 1676 rc = -EINVAL; 1677 goto out; 1678 } 1679 skcipher_request_set_crypt(req, src_sg, dst_sg, 1680 auth_tok->session_key.encrypted_key_size, 1681 NULL); 1682 rc = crypto_skcipher_decrypt(req); 1683 mutex_unlock(tfm_mutex); 1684 if (unlikely(rc)) { 1685 printk(KERN_ERR "Error decrypting; rc = [%d]\n", rc); 1686 goto out; 1687 } 1688 auth_tok->session_key.flags |= ECRYPTFS_CONTAINS_DECRYPTED_KEY; 1689 memcpy(crypt_stat->key, auth_tok->session_key.decrypted_key, 1690 auth_tok->session_key.decrypted_key_size); 1691 crypt_stat->flags |= ECRYPTFS_KEY_VALID; 1692 if (unlikely(ecryptfs_verbosity > 0)) { 1693 ecryptfs_printk(KERN_DEBUG, "FEK of size [%zd]:\n", 1694 crypt_stat->key_size); 1695 ecryptfs_dump_hex(crypt_stat->key, 1696 crypt_stat->key_size); 1697 } 1698 out: 1699 skcipher_request_free(req); 1700 return rc; 1701 } 1702 1703 /** 1704 * ecryptfs_parse_packet_set 1705 * @crypt_stat: The cryptographic context 1706 * @src: Virtual address of region of memory containing the packets 1707 * @ecryptfs_dentry: The eCryptfs dentry associated with the packet set 1708 * 1709 * Get crypt_stat to have the file's session key if the requisite key 1710 * is available to decrypt the session key. 1711 * 1712 * Returns Zero if a valid authentication token was retrieved and 1713 * processed; negative value for file not encrypted or for error 1714 * conditions. 1715 */ 1716 int ecryptfs_parse_packet_set(struct ecryptfs_crypt_stat *crypt_stat, 1717 unsigned char *src, 1718 struct dentry *ecryptfs_dentry) 1719 { 1720 size_t i = 0; 1721 size_t found_auth_tok; 1722 size_t next_packet_is_auth_tok_packet; 1723 LIST_HEAD(auth_tok_list); 1724 struct ecryptfs_auth_tok *matching_auth_tok; 1725 struct ecryptfs_auth_tok *candidate_auth_tok; 1726 char *candidate_auth_tok_sig; 1727 size_t packet_size; 1728 struct ecryptfs_auth_tok *new_auth_tok; 1729 unsigned char sig_tmp_space[ECRYPTFS_SIG_SIZE]; 1730 struct ecryptfs_auth_tok_list_item *auth_tok_list_item; 1731 size_t tag_11_contents_size; 1732 size_t tag_11_packet_size; 1733 struct key *auth_tok_key = NULL; 1734 int rc = 0; 1735 1736 /* Parse the header to find as many packets as we can; these will be 1737 * added the our &auth_tok_list */ 1738 next_packet_is_auth_tok_packet = 1; 1739 while (next_packet_is_auth_tok_packet) { 1740 size_t max_packet_size = ((PAGE_SIZE - 8) - i); 1741 1742 switch (src[i]) { 1743 case ECRYPTFS_TAG_3_PACKET_TYPE: 1744 rc = parse_tag_3_packet(crypt_stat, 1745 (unsigned char *)&src[i], 1746 &auth_tok_list, &new_auth_tok, 1747 &packet_size, max_packet_size); 1748 if (rc) { 1749 ecryptfs_printk(KERN_ERR, "Error parsing " 1750 "tag 3 packet\n"); 1751 rc = -EIO; 1752 goto out_wipe_list; 1753 } 1754 i += packet_size; 1755 rc = parse_tag_11_packet((unsigned char *)&src[i], 1756 sig_tmp_space, 1757 ECRYPTFS_SIG_SIZE, 1758 &tag_11_contents_size, 1759 &tag_11_packet_size, 1760 max_packet_size); 1761 if (rc) { 1762 ecryptfs_printk(KERN_ERR, "No valid " 1763 "(ecryptfs-specific) literal " 1764 "packet containing " 1765 "authentication token " 1766 "signature found after " 1767 "tag 3 packet\n"); 1768 rc = -EIO; 1769 goto out_wipe_list; 1770 } 1771 i += tag_11_packet_size; 1772 if (ECRYPTFS_SIG_SIZE != tag_11_contents_size) { 1773 ecryptfs_printk(KERN_ERR, "Expected " 1774 "signature of size [%d]; " 1775 "read size [%zd]\n", 1776 ECRYPTFS_SIG_SIZE, 1777 tag_11_contents_size); 1778 rc = -EIO; 1779 goto out_wipe_list; 1780 } 1781 ecryptfs_to_hex(new_auth_tok->token.password.signature, 1782 sig_tmp_space, tag_11_contents_size); 1783 crypt_stat->flags |= ECRYPTFS_ENCRYPTED; 1784 break; 1785 case ECRYPTFS_TAG_1_PACKET_TYPE: 1786 rc = parse_tag_1_packet(crypt_stat, 1787 (unsigned char *)&src[i], 1788 &auth_tok_list, &new_auth_tok, 1789 &packet_size, max_packet_size); 1790 if (rc) { 1791 ecryptfs_printk(KERN_ERR, "Error parsing " 1792 "tag 1 packet\n"); 1793 rc = -EIO; 1794 goto out_wipe_list; 1795 } 1796 i += packet_size; 1797 crypt_stat->flags |= ECRYPTFS_ENCRYPTED; 1798 break; 1799 case ECRYPTFS_TAG_11_PACKET_TYPE: 1800 ecryptfs_printk(KERN_WARNING, "Invalid packet set " 1801 "(Tag 11 not allowed by itself)\n"); 1802 rc = -EIO; 1803 goto out_wipe_list; 1804 default: 1805 ecryptfs_printk(KERN_DEBUG, "No packet at offset [%zd] " 1806 "of the file header; hex value of " 1807 "character is [0x%.2x]\n", i, src[i]); 1808 next_packet_is_auth_tok_packet = 0; 1809 } 1810 } 1811 if (list_empty(&auth_tok_list)) { 1812 printk(KERN_ERR "The lower file appears to be a non-encrypted " 1813 "eCryptfs file; this is not supported in this version " 1814 "of the eCryptfs kernel module\n"); 1815 rc = -EINVAL; 1816 goto out; 1817 } 1818 /* auth_tok_list contains the set of authentication tokens 1819 * parsed from the metadata. We need to find a matching 1820 * authentication token that has the secret component(s) 1821 * necessary to decrypt the EFEK in the auth_tok parsed from 1822 * the metadata. There may be several potential matches, but 1823 * just one will be sufficient to decrypt to get the FEK. */ 1824 find_next_matching_auth_tok: 1825 found_auth_tok = 0; 1826 list_for_each_entry(auth_tok_list_item, &auth_tok_list, list) { 1827 candidate_auth_tok = &auth_tok_list_item->auth_tok; 1828 if (unlikely(ecryptfs_verbosity > 0)) { 1829 ecryptfs_printk(KERN_DEBUG, 1830 "Considering candidate auth tok:\n"); 1831 ecryptfs_dump_auth_tok(candidate_auth_tok); 1832 } 1833 rc = ecryptfs_get_auth_tok_sig(&candidate_auth_tok_sig, 1834 candidate_auth_tok); 1835 if (rc) { 1836 printk(KERN_ERR 1837 "Unrecognized candidate auth tok type: [%d]\n", 1838 candidate_auth_tok->token_type); 1839 rc = -EINVAL; 1840 goto out_wipe_list; 1841 } 1842 rc = ecryptfs_find_auth_tok_for_sig(&auth_tok_key, 1843 &matching_auth_tok, 1844 crypt_stat->mount_crypt_stat, 1845 candidate_auth_tok_sig); 1846 if (!rc) { 1847 found_auth_tok = 1; 1848 goto found_matching_auth_tok; 1849 } 1850 } 1851 if (!found_auth_tok) { 1852 ecryptfs_printk(KERN_ERR, "Could not find a usable " 1853 "authentication token\n"); 1854 rc = -EIO; 1855 goto out_wipe_list; 1856 } 1857 found_matching_auth_tok: 1858 if (candidate_auth_tok->token_type == ECRYPTFS_PRIVATE_KEY) { 1859 memcpy(&(candidate_auth_tok->token.private_key), 1860 &(matching_auth_tok->token.private_key), 1861 sizeof(struct ecryptfs_private_key)); 1862 up_write(&(auth_tok_key->sem)); 1863 key_put(auth_tok_key); 1864 rc = decrypt_pki_encrypted_session_key(candidate_auth_tok, 1865 crypt_stat); 1866 } else if (candidate_auth_tok->token_type == ECRYPTFS_PASSWORD) { 1867 memcpy(&(candidate_auth_tok->token.password), 1868 &(matching_auth_tok->token.password), 1869 sizeof(struct ecryptfs_password)); 1870 up_write(&(auth_tok_key->sem)); 1871 key_put(auth_tok_key); 1872 rc = decrypt_passphrase_encrypted_session_key( 1873 candidate_auth_tok, crypt_stat); 1874 } else { 1875 up_write(&(auth_tok_key->sem)); 1876 key_put(auth_tok_key); 1877 rc = -EINVAL; 1878 } 1879 if (rc) { 1880 struct ecryptfs_auth_tok_list_item *auth_tok_list_item_tmp; 1881 1882 ecryptfs_printk(KERN_WARNING, "Error decrypting the " 1883 "session key for authentication token with sig " 1884 "[%.*s]; rc = [%d]. Removing auth tok " 1885 "candidate from the list and searching for " 1886 "the next match.\n", ECRYPTFS_SIG_SIZE_HEX, 1887 candidate_auth_tok_sig, rc); 1888 list_for_each_entry_safe(auth_tok_list_item, 1889 auth_tok_list_item_tmp, 1890 &auth_tok_list, list) { 1891 if (candidate_auth_tok 1892 == &auth_tok_list_item->auth_tok) { 1893 list_del(&auth_tok_list_item->list); 1894 kmem_cache_free( 1895 ecryptfs_auth_tok_list_item_cache, 1896 auth_tok_list_item); 1897 goto find_next_matching_auth_tok; 1898 } 1899 } 1900 BUG(); 1901 } 1902 rc = ecryptfs_compute_root_iv(crypt_stat); 1903 if (rc) { 1904 ecryptfs_printk(KERN_ERR, "Error computing " 1905 "the root IV\n"); 1906 goto out_wipe_list; 1907 } 1908 rc = ecryptfs_init_crypt_ctx(crypt_stat); 1909 if (rc) { 1910 ecryptfs_printk(KERN_ERR, "Error initializing crypto " 1911 "context for cipher [%s]; rc = [%d]\n", 1912 crypt_stat->cipher, rc); 1913 } 1914 out_wipe_list: 1915 wipe_auth_tok_list(&auth_tok_list); 1916 out: 1917 return rc; 1918 } 1919 1920 static int 1921 pki_encrypt_session_key(struct key *auth_tok_key, 1922 struct ecryptfs_auth_tok *auth_tok, 1923 struct ecryptfs_crypt_stat *crypt_stat, 1924 struct ecryptfs_key_record *key_rec) 1925 { 1926 struct ecryptfs_msg_ctx *msg_ctx = NULL; 1927 char *payload = NULL; 1928 size_t payload_len = 0; 1929 struct ecryptfs_message *msg; 1930 int rc; 1931 1932 rc = write_tag_66_packet(auth_tok->token.private_key.signature, 1933 ecryptfs_code_for_cipher_string( 1934 crypt_stat->cipher, 1935 crypt_stat->key_size), 1936 crypt_stat, &payload, &payload_len); 1937 up_write(&(auth_tok_key->sem)); 1938 key_put(auth_tok_key); 1939 if (rc) { 1940 ecryptfs_printk(KERN_ERR, "Error generating tag 66 packet\n"); 1941 goto out; 1942 } 1943 rc = ecryptfs_send_message(payload, payload_len, &msg_ctx); 1944 if (rc) { 1945 ecryptfs_printk(KERN_ERR, "Error sending message to " 1946 "ecryptfsd: %d\n", rc); 1947 goto out; 1948 } 1949 rc = ecryptfs_wait_for_response(msg_ctx, &msg); 1950 if (rc) { 1951 ecryptfs_printk(KERN_ERR, "Failed to receive tag 67 packet " 1952 "from the user space daemon\n"); 1953 rc = -EIO; 1954 goto out; 1955 } 1956 rc = parse_tag_67_packet(key_rec, msg); 1957 if (rc) 1958 ecryptfs_printk(KERN_ERR, "Error parsing tag 67 packet\n"); 1959 kfree(msg); 1960 out: 1961 kfree(payload); 1962 return rc; 1963 } 1964 /** 1965 * write_tag_1_packet - Write an RFC2440-compatible tag 1 (public key) packet 1966 * @dest: Buffer into which to write the packet 1967 * @remaining_bytes: Maximum number of bytes that can be writtn 1968 * @auth_tok_key: The authentication token key to unlock and put when done with 1969 * @auth_tok 1970 * @auth_tok: The authentication token used for generating the tag 1 packet 1971 * @crypt_stat: The cryptographic context 1972 * @key_rec: The key record struct for the tag 1 packet 1973 * @packet_size: This function will write the number of bytes that end 1974 * up constituting the packet; set to zero on error 1975 * 1976 * Returns zero on success; non-zero on error. 1977 */ 1978 static int 1979 write_tag_1_packet(char *dest, size_t *remaining_bytes, 1980 struct key *auth_tok_key, struct ecryptfs_auth_tok *auth_tok, 1981 struct ecryptfs_crypt_stat *crypt_stat, 1982 struct ecryptfs_key_record *key_rec, size_t *packet_size) 1983 { 1984 size_t i; 1985 size_t encrypted_session_key_valid = 0; 1986 size_t packet_size_length; 1987 size_t max_packet_size; 1988 int rc = 0; 1989 1990 (*packet_size) = 0; 1991 ecryptfs_from_hex(key_rec->sig, auth_tok->token.private_key.signature, 1992 ECRYPTFS_SIG_SIZE); 1993 encrypted_session_key_valid = 0; 1994 for (i = 0; i < crypt_stat->key_size; i++) 1995 encrypted_session_key_valid |= 1996 auth_tok->session_key.encrypted_key[i]; 1997 if (encrypted_session_key_valid) { 1998 memcpy(key_rec->enc_key, 1999 auth_tok->session_key.encrypted_key, 2000 auth_tok->session_key.encrypted_key_size); 2001 up_write(&(auth_tok_key->sem)); 2002 key_put(auth_tok_key); 2003 goto encrypted_session_key_set; 2004 } 2005 if (auth_tok->session_key.encrypted_key_size == 0) 2006 auth_tok->session_key.encrypted_key_size = 2007 auth_tok->token.private_key.key_size; 2008 rc = pki_encrypt_session_key(auth_tok_key, auth_tok, crypt_stat, 2009 key_rec); 2010 if (rc) { 2011 printk(KERN_ERR "Failed to encrypt session key via a key " 2012 "module; rc = [%d]\n", rc); 2013 goto out; 2014 } 2015 if (ecryptfs_verbosity > 0) { 2016 ecryptfs_printk(KERN_DEBUG, "Encrypted key:\n"); 2017 ecryptfs_dump_hex(key_rec->enc_key, key_rec->enc_key_size); 2018 } 2019 encrypted_session_key_set: 2020 /* This format is inspired by OpenPGP; see RFC 2440 2021 * packet tag 1 */ 2022 max_packet_size = (1 /* Tag 1 identifier */ 2023 + 3 /* Max Tag 1 packet size */ 2024 + 1 /* Version */ 2025 + ECRYPTFS_SIG_SIZE /* Key identifier */ 2026 + 1 /* Cipher identifier */ 2027 + key_rec->enc_key_size); /* Encrypted key size */ 2028 if (max_packet_size > (*remaining_bytes)) { 2029 printk(KERN_ERR "Packet length larger than maximum allowable; " 2030 "need up to [%td] bytes, but there are only [%td] " 2031 "available\n", max_packet_size, (*remaining_bytes)); 2032 rc = -EINVAL; 2033 goto out; 2034 } 2035 dest[(*packet_size)++] = ECRYPTFS_TAG_1_PACKET_TYPE; 2036 rc = ecryptfs_write_packet_length(&dest[(*packet_size)], 2037 (max_packet_size - 4), 2038 &packet_size_length); 2039 if (rc) { 2040 ecryptfs_printk(KERN_ERR, "Error generating tag 1 packet " 2041 "header; cannot generate packet length\n"); 2042 goto out; 2043 } 2044 (*packet_size) += packet_size_length; 2045 dest[(*packet_size)++] = 0x03; /* version 3 */ 2046 memcpy(&dest[(*packet_size)], key_rec->sig, ECRYPTFS_SIG_SIZE); 2047 (*packet_size) += ECRYPTFS_SIG_SIZE; 2048 dest[(*packet_size)++] = RFC2440_CIPHER_RSA; 2049 memcpy(&dest[(*packet_size)], key_rec->enc_key, 2050 key_rec->enc_key_size); 2051 (*packet_size) += key_rec->enc_key_size; 2052 out: 2053 if (rc) 2054 (*packet_size) = 0; 2055 else 2056 (*remaining_bytes) -= (*packet_size); 2057 return rc; 2058 } 2059 2060 /** 2061 * write_tag_11_packet 2062 * @dest: Target into which Tag 11 packet is to be written 2063 * @remaining_bytes: Maximum packet length 2064 * @contents: Byte array of contents to copy in 2065 * @contents_length: Number of bytes in contents 2066 * @packet_length: Length of the Tag 11 packet written; zero on error 2067 * 2068 * Returns zero on success; non-zero on error. 2069 */ 2070 static int 2071 write_tag_11_packet(char *dest, size_t *remaining_bytes, char *contents, 2072 size_t contents_length, size_t *packet_length) 2073 { 2074 size_t packet_size_length; 2075 size_t max_packet_size; 2076 int rc = 0; 2077 2078 (*packet_length) = 0; 2079 /* This format is inspired by OpenPGP; see RFC 2440 2080 * packet tag 11 */ 2081 max_packet_size = (1 /* Tag 11 identifier */ 2082 + 3 /* Max Tag 11 packet size */ 2083 + 1 /* Binary format specifier */ 2084 + 1 /* Filename length */ 2085 + 8 /* Filename ("_CONSOLE") */ 2086 + 4 /* Modification date */ 2087 + contents_length); /* Literal data */ 2088 if (max_packet_size > (*remaining_bytes)) { 2089 printk(KERN_ERR "Packet length larger than maximum allowable; " 2090 "need up to [%td] bytes, but there are only [%td] " 2091 "available\n", max_packet_size, (*remaining_bytes)); 2092 rc = -EINVAL; 2093 goto out; 2094 } 2095 dest[(*packet_length)++] = ECRYPTFS_TAG_11_PACKET_TYPE; 2096 rc = ecryptfs_write_packet_length(&dest[(*packet_length)], 2097 (max_packet_size - 4), 2098 &packet_size_length); 2099 if (rc) { 2100 printk(KERN_ERR "Error generating tag 11 packet header; cannot " 2101 "generate packet length. rc = [%d]\n", rc); 2102 goto out; 2103 } 2104 (*packet_length) += packet_size_length; 2105 dest[(*packet_length)++] = 0x62; /* binary data format specifier */ 2106 dest[(*packet_length)++] = 8; 2107 memcpy(&dest[(*packet_length)], "_CONSOLE", 8); 2108 (*packet_length) += 8; 2109 memset(&dest[(*packet_length)], 0x00, 4); 2110 (*packet_length) += 4; 2111 memcpy(&dest[(*packet_length)], contents, contents_length); 2112 (*packet_length) += contents_length; 2113 out: 2114 if (rc) 2115 (*packet_length) = 0; 2116 else 2117 (*remaining_bytes) -= (*packet_length); 2118 return rc; 2119 } 2120 2121 /** 2122 * write_tag_3_packet 2123 * @dest: Buffer into which to write the packet 2124 * @remaining_bytes: Maximum number of bytes that can be written 2125 * @auth_tok: Authentication token 2126 * @crypt_stat: The cryptographic context 2127 * @key_rec: encrypted key 2128 * @packet_size: This function will write the number of bytes that end 2129 * up constituting the packet; set to zero on error 2130 * 2131 * Returns zero on success; non-zero on error. 2132 */ 2133 static int 2134 write_tag_3_packet(char *dest, size_t *remaining_bytes, 2135 struct ecryptfs_auth_tok *auth_tok, 2136 struct ecryptfs_crypt_stat *crypt_stat, 2137 struct ecryptfs_key_record *key_rec, size_t *packet_size) 2138 { 2139 size_t i; 2140 size_t encrypted_session_key_valid = 0; 2141 char session_key_encryption_key[ECRYPTFS_MAX_KEY_BYTES]; 2142 struct scatterlist dst_sg[2]; 2143 struct scatterlist src_sg[2]; 2144 struct mutex *tfm_mutex = NULL; 2145 u8 cipher_code; 2146 size_t packet_size_length; 2147 size_t max_packet_size; 2148 struct ecryptfs_mount_crypt_stat *mount_crypt_stat = 2149 crypt_stat->mount_crypt_stat; 2150 struct crypto_skcipher *tfm; 2151 struct skcipher_request *req; 2152 int rc = 0; 2153 2154 (*packet_size) = 0; 2155 ecryptfs_from_hex(key_rec->sig, auth_tok->token.password.signature, 2156 ECRYPTFS_SIG_SIZE); 2157 rc = ecryptfs_get_tfm_and_mutex_for_cipher_name(&tfm, &tfm_mutex, 2158 crypt_stat->cipher); 2159 if (unlikely(rc)) { 2160 printk(KERN_ERR "Internal error whilst attempting to get " 2161 "tfm and mutex for cipher name [%s]; rc = [%d]\n", 2162 crypt_stat->cipher, rc); 2163 goto out; 2164 } 2165 if (mount_crypt_stat->global_default_cipher_key_size == 0) { 2166 printk(KERN_WARNING "No key size specified at mount; " 2167 "defaulting to [%d]\n", 2168 crypto_skcipher_max_keysize(tfm)); 2169 mount_crypt_stat->global_default_cipher_key_size = 2170 crypto_skcipher_max_keysize(tfm); 2171 } 2172 if (crypt_stat->key_size == 0) 2173 crypt_stat->key_size = 2174 mount_crypt_stat->global_default_cipher_key_size; 2175 if (auth_tok->session_key.encrypted_key_size == 0) 2176 auth_tok->session_key.encrypted_key_size = 2177 crypt_stat->key_size; 2178 if (crypt_stat->key_size == 24 2179 && strcmp("aes", crypt_stat->cipher) == 0) { 2180 memset((crypt_stat->key + 24), 0, 8); 2181 auth_tok->session_key.encrypted_key_size = 32; 2182 } else 2183 auth_tok->session_key.encrypted_key_size = crypt_stat->key_size; 2184 key_rec->enc_key_size = 2185 auth_tok->session_key.encrypted_key_size; 2186 encrypted_session_key_valid = 0; 2187 for (i = 0; i < auth_tok->session_key.encrypted_key_size; i++) 2188 encrypted_session_key_valid |= 2189 auth_tok->session_key.encrypted_key[i]; 2190 if (encrypted_session_key_valid) { 2191 ecryptfs_printk(KERN_DEBUG, "encrypted_session_key_valid != 0; " 2192 "using auth_tok->session_key.encrypted_key, " 2193 "where key_rec->enc_key_size = [%zd]\n", 2194 key_rec->enc_key_size); 2195 memcpy(key_rec->enc_key, 2196 auth_tok->session_key.encrypted_key, 2197 key_rec->enc_key_size); 2198 goto encrypted_session_key_set; 2199 } 2200 if (auth_tok->token.password.flags & 2201 ECRYPTFS_SESSION_KEY_ENCRYPTION_KEY_SET) { 2202 ecryptfs_printk(KERN_DEBUG, "Using previously generated " 2203 "session key encryption key of size [%d]\n", 2204 auth_tok->token.password. 2205 session_key_encryption_key_bytes); 2206 memcpy(session_key_encryption_key, 2207 auth_tok->token.password.session_key_encryption_key, 2208 crypt_stat->key_size); 2209 ecryptfs_printk(KERN_DEBUG, 2210 "Cached session key encryption key:\n"); 2211 if (ecryptfs_verbosity > 0) 2212 ecryptfs_dump_hex(session_key_encryption_key, 16); 2213 } 2214 if (unlikely(ecryptfs_verbosity > 0)) { 2215 ecryptfs_printk(KERN_DEBUG, "Session key encryption key:\n"); 2216 ecryptfs_dump_hex(session_key_encryption_key, 16); 2217 } 2218 rc = virt_to_scatterlist(crypt_stat->key, key_rec->enc_key_size, 2219 src_sg, 2); 2220 if (rc < 1 || rc > 2) { 2221 ecryptfs_printk(KERN_ERR, "Error generating scatterlist " 2222 "for crypt_stat session key; expected rc = 1; " 2223 "got rc = [%d]. key_rec->enc_key_size = [%zd]\n", 2224 rc, key_rec->enc_key_size); 2225 rc = -ENOMEM; 2226 goto out; 2227 } 2228 rc = virt_to_scatterlist(key_rec->enc_key, key_rec->enc_key_size, 2229 dst_sg, 2); 2230 if (rc < 1 || rc > 2) { 2231 ecryptfs_printk(KERN_ERR, "Error generating scatterlist " 2232 "for crypt_stat encrypted session key; " 2233 "expected rc = 1; got rc = [%d]. " 2234 "key_rec->enc_key_size = [%zd]\n", rc, 2235 key_rec->enc_key_size); 2236 rc = -ENOMEM; 2237 goto out; 2238 } 2239 mutex_lock(tfm_mutex); 2240 rc = crypto_skcipher_setkey(tfm, session_key_encryption_key, 2241 crypt_stat->key_size); 2242 if (rc < 0) { 2243 mutex_unlock(tfm_mutex); 2244 ecryptfs_printk(KERN_ERR, "Error setting key for crypto " 2245 "context; rc = [%d]\n", rc); 2246 goto out; 2247 } 2248 2249 req = skcipher_request_alloc(tfm, GFP_KERNEL); 2250 if (!req) { 2251 mutex_unlock(tfm_mutex); 2252 ecryptfs_printk(KERN_ERR, "Out of kernel memory whilst " 2253 "attempting to skcipher_request_alloc for " 2254 "%s\n", crypto_skcipher_driver_name(tfm)); 2255 rc = -ENOMEM; 2256 goto out; 2257 } 2258 2259 skcipher_request_set_callback(req, CRYPTO_TFM_REQ_MAY_SLEEP, 2260 NULL, NULL); 2261 2262 rc = 0; 2263 ecryptfs_printk(KERN_DEBUG, "Encrypting [%zd] bytes of the key\n", 2264 crypt_stat->key_size); 2265 skcipher_request_set_crypt(req, src_sg, dst_sg, 2266 (*key_rec).enc_key_size, NULL); 2267 rc = crypto_skcipher_encrypt(req); 2268 mutex_unlock(tfm_mutex); 2269 skcipher_request_free(req); 2270 if (rc) { 2271 printk(KERN_ERR "Error encrypting; rc = [%d]\n", rc); 2272 goto out; 2273 } 2274 ecryptfs_printk(KERN_DEBUG, "This should be the encrypted key:\n"); 2275 if (ecryptfs_verbosity > 0) { 2276 ecryptfs_printk(KERN_DEBUG, "EFEK of size [%zd]:\n", 2277 key_rec->enc_key_size); 2278 ecryptfs_dump_hex(key_rec->enc_key, 2279 key_rec->enc_key_size); 2280 } 2281 encrypted_session_key_set: 2282 /* This format is inspired by OpenPGP; see RFC 2440 2283 * packet tag 3 */ 2284 max_packet_size = (1 /* Tag 3 identifier */ 2285 + 3 /* Max Tag 3 packet size */ 2286 + 1 /* Version */ 2287 + 1 /* Cipher code */ 2288 + 1 /* S2K specifier */ 2289 + 1 /* Hash identifier */ 2290 + ECRYPTFS_SALT_SIZE /* Salt */ 2291 + 1 /* Hash iterations */ 2292 + key_rec->enc_key_size); /* Encrypted key size */ 2293 if (max_packet_size > (*remaining_bytes)) { 2294 printk(KERN_ERR "Packet too large; need up to [%td] bytes, but " 2295 "there are only [%td] available\n", max_packet_size, 2296 (*remaining_bytes)); 2297 rc = -EINVAL; 2298 goto out; 2299 } 2300 dest[(*packet_size)++] = ECRYPTFS_TAG_3_PACKET_TYPE; 2301 /* Chop off the Tag 3 identifier(1) and Tag 3 packet size(3) 2302 * to get the number of octets in the actual Tag 3 packet */ 2303 rc = ecryptfs_write_packet_length(&dest[(*packet_size)], 2304 (max_packet_size - 4), 2305 &packet_size_length); 2306 if (rc) { 2307 printk(KERN_ERR "Error generating tag 3 packet header; cannot " 2308 "generate packet length. rc = [%d]\n", rc); 2309 goto out; 2310 } 2311 (*packet_size) += packet_size_length; 2312 dest[(*packet_size)++] = 0x04; /* version 4 */ 2313 /* TODO: Break from RFC2440 so that arbitrary ciphers can be 2314 * specified with strings */ 2315 cipher_code = ecryptfs_code_for_cipher_string(crypt_stat->cipher, 2316 crypt_stat->key_size); 2317 if (cipher_code == 0) { 2318 ecryptfs_printk(KERN_WARNING, "Unable to generate code for " 2319 "cipher [%s]\n", crypt_stat->cipher); 2320 rc = -EINVAL; 2321 goto out; 2322 } 2323 dest[(*packet_size)++] = cipher_code; 2324 dest[(*packet_size)++] = 0x03; /* S2K */ 2325 dest[(*packet_size)++] = 0x01; /* MD5 (TODO: parameterize) */ 2326 memcpy(&dest[(*packet_size)], auth_tok->token.password.salt, 2327 ECRYPTFS_SALT_SIZE); 2328 (*packet_size) += ECRYPTFS_SALT_SIZE; /* salt */ 2329 dest[(*packet_size)++] = 0x60; /* hash iterations (65536) */ 2330 memcpy(&dest[(*packet_size)], key_rec->enc_key, 2331 key_rec->enc_key_size); 2332 (*packet_size) += key_rec->enc_key_size; 2333 out: 2334 if (rc) 2335 (*packet_size) = 0; 2336 else 2337 (*remaining_bytes) -= (*packet_size); 2338 return rc; 2339 } 2340 2341 struct kmem_cache *ecryptfs_key_record_cache; 2342 2343 /** 2344 * ecryptfs_generate_key_packet_set 2345 * @dest_base: Virtual address from which to write the key record set 2346 * @crypt_stat: The cryptographic context from which the 2347 * authentication tokens will be retrieved 2348 * @ecryptfs_dentry: The dentry, used to retrieve the mount crypt stat 2349 * for the global parameters 2350 * @len: The amount written 2351 * @max: The maximum amount of data allowed to be written 2352 * 2353 * Generates a key packet set and writes it to the virtual address 2354 * passed in. 2355 * 2356 * Returns zero on success; non-zero on error. 2357 */ 2358 int 2359 ecryptfs_generate_key_packet_set(char *dest_base, 2360 struct ecryptfs_crypt_stat *crypt_stat, 2361 struct dentry *ecryptfs_dentry, size_t *len, 2362 size_t max) 2363 { 2364 struct ecryptfs_auth_tok *auth_tok; 2365 struct key *auth_tok_key = NULL; 2366 struct ecryptfs_mount_crypt_stat *mount_crypt_stat = 2367 &ecryptfs_superblock_to_private( 2368 ecryptfs_dentry->d_sb)->mount_crypt_stat; 2369 size_t written; 2370 struct ecryptfs_key_record *key_rec; 2371 struct ecryptfs_key_sig *key_sig; 2372 int rc = 0; 2373 2374 (*len) = 0; 2375 mutex_lock(&crypt_stat->keysig_list_mutex); 2376 key_rec = kmem_cache_alloc(ecryptfs_key_record_cache, GFP_KERNEL); 2377 if (!key_rec) { 2378 rc = -ENOMEM; 2379 goto out; 2380 } 2381 list_for_each_entry(key_sig, &crypt_stat->keysig_list, 2382 crypt_stat_list) { 2383 memset(key_rec, 0, sizeof(*key_rec)); 2384 rc = ecryptfs_find_global_auth_tok_for_sig(&auth_tok_key, 2385 &auth_tok, 2386 mount_crypt_stat, 2387 key_sig->keysig); 2388 if (rc) { 2389 printk(KERN_WARNING "Unable to retrieve auth tok with " 2390 "sig = [%s]\n", key_sig->keysig); 2391 rc = process_find_global_auth_tok_for_sig_err(rc); 2392 goto out_free; 2393 } 2394 if (auth_tok->token_type == ECRYPTFS_PASSWORD) { 2395 rc = write_tag_3_packet((dest_base + (*len)), 2396 &max, auth_tok, 2397 crypt_stat, key_rec, 2398 &written); 2399 up_write(&(auth_tok_key->sem)); 2400 key_put(auth_tok_key); 2401 if (rc) { 2402 ecryptfs_printk(KERN_WARNING, "Error " 2403 "writing tag 3 packet\n"); 2404 goto out_free; 2405 } 2406 (*len) += written; 2407 /* Write auth tok signature packet */ 2408 rc = write_tag_11_packet((dest_base + (*len)), &max, 2409 key_rec->sig, 2410 ECRYPTFS_SIG_SIZE, &written); 2411 if (rc) { 2412 ecryptfs_printk(KERN_ERR, "Error writing " 2413 "auth tok signature packet\n"); 2414 goto out_free; 2415 } 2416 (*len) += written; 2417 } else if (auth_tok->token_type == ECRYPTFS_PRIVATE_KEY) { 2418 rc = write_tag_1_packet(dest_base + (*len), &max, 2419 auth_tok_key, auth_tok, 2420 crypt_stat, key_rec, &written); 2421 if (rc) { 2422 ecryptfs_printk(KERN_WARNING, "Error " 2423 "writing tag 1 packet\n"); 2424 goto out_free; 2425 } 2426 (*len) += written; 2427 } else { 2428 up_write(&(auth_tok_key->sem)); 2429 key_put(auth_tok_key); 2430 ecryptfs_printk(KERN_WARNING, "Unsupported " 2431 "authentication token type\n"); 2432 rc = -EINVAL; 2433 goto out_free; 2434 } 2435 } 2436 if (likely(max > 0)) { 2437 dest_base[(*len)] = 0x00; 2438 } else { 2439 ecryptfs_printk(KERN_ERR, "Error writing boundary byte\n"); 2440 rc = -EIO; 2441 } 2442 out_free: 2443 kmem_cache_free(ecryptfs_key_record_cache, key_rec); 2444 out: 2445 if (rc) 2446 (*len) = 0; 2447 mutex_unlock(&crypt_stat->keysig_list_mutex); 2448 return rc; 2449 } 2450 2451 struct kmem_cache *ecryptfs_key_sig_cache; 2452 2453 int ecryptfs_add_keysig(struct ecryptfs_crypt_stat *crypt_stat, char *sig) 2454 { 2455 struct ecryptfs_key_sig *new_key_sig; 2456 2457 new_key_sig = kmem_cache_alloc(ecryptfs_key_sig_cache, GFP_KERNEL); 2458 if (!new_key_sig) 2459 return -ENOMEM; 2460 2461 memcpy(new_key_sig->keysig, sig, ECRYPTFS_SIG_SIZE_HEX); 2462 new_key_sig->keysig[ECRYPTFS_SIG_SIZE_HEX] = '\0'; 2463 /* Caller must hold keysig_list_mutex */ 2464 list_add(&new_key_sig->crypt_stat_list, &crypt_stat->keysig_list); 2465 2466 return 0; 2467 } 2468 2469 struct kmem_cache *ecryptfs_global_auth_tok_cache; 2470 2471 int 2472 ecryptfs_add_global_auth_tok(struct ecryptfs_mount_crypt_stat *mount_crypt_stat, 2473 char *sig, u32 global_auth_tok_flags) 2474 { 2475 struct ecryptfs_global_auth_tok *new_auth_tok; 2476 2477 new_auth_tok = kmem_cache_zalloc(ecryptfs_global_auth_tok_cache, 2478 GFP_KERNEL); 2479 if (!new_auth_tok) 2480 return -ENOMEM; 2481 2482 memcpy(new_auth_tok->sig, sig, ECRYPTFS_SIG_SIZE_HEX); 2483 new_auth_tok->flags = global_auth_tok_flags; 2484 new_auth_tok->sig[ECRYPTFS_SIG_SIZE_HEX] = '\0'; 2485 mutex_lock(&mount_crypt_stat->global_auth_tok_list_mutex); 2486 list_add(&new_auth_tok->mount_crypt_stat_list, 2487 &mount_crypt_stat->global_auth_tok_list); 2488 mutex_unlock(&mount_crypt_stat->global_auth_tok_list_mutex); 2489 return 0; 2490 } 2491 2492