| .. | .. |
|---|
| 61 | 61 | */ |
|---|
| 62 | 62 | #define FSCRYPT_NOKEY_NAME_MAX offsetofend(struct fscrypt_nokey_name, sha256) |
|---|
| 63 | 63 | |
|---|
| 64 | | -static struct crypto_shash *sha256_hash_tfm; |
|---|
| 65 | | - |
|---|
| 66 | | -static int fscrypt_do_sha256(const u8 *data, unsigned int data_len, u8 *result) |
|---|
| 67 | | -{ |
|---|
| 68 | | - struct crypto_shash *tfm = READ_ONCE(sha256_hash_tfm); |
|---|
| 69 | | - |
|---|
| 70 | | - if (unlikely(!tfm)) { |
|---|
| 71 | | - struct crypto_shash *prev_tfm; |
|---|
| 72 | | - |
|---|
| 73 | | - tfm = crypto_alloc_shash("sha256", 0, 0); |
|---|
| 74 | | - if (IS_ERR(tfm)) { |
|---|
| 75 | | - fscrypt_err(NULL, |
|---|
| 76 | | - "Error allocating SHA-256 transform: %ld", |
|---|
| 77 | | - PTR_ERR(tfm)); |
|---|
| 78 | | - return PTR_ERR(tfm); |
|---|
| 79 | | - } |
|---|
| 80 | | - prev_tfm = cmpxchg(&sha256_hash_tfm, NULL, tfm); |
|---|
| 81 | | - if (prev_tfm) { |
|---|
| 82 | | - crypto_free_shash(tfm); |
|---|
| 83 | | - tfm = prev_tfm; |
|---|
| 84 | | - } |
|---|
| 85 | | - } |
|---|
| 86 | | - { |
|---|
| 87 | | - SHASH_DESC_ON_STACK(desc, tfm); |
|---|
| 88 | | - |
|---|
| 89 | | - desc->tfm = tfm; |
|---|
| 90 | | - desc->flags = 0; |
|---|
| 91 | | - |
|---|
| 92 | | - return crypto_shash_digest(desc, data, data_len, result); |
|---|
| 93 | | - } |
|---|
| 94 | | -} |
|---|
| 95 | | - |
|---|
| 96 | 64 | static inline bool fscrypt_is_dot_dotdot(const struct qstr *str) |
|---|
| 97 | 65 | { |
|---|
| 98 | 66 | if (str->len == 1 && str->name[0] == '.') |
|---|
| .. | .. |
|---|
| 121 | 89 | struct skcipher_request *req = NULL; |
|---|
| 122 | 90 | DECLARE_CRYPTO_WAIT(wait); |
|---|
| 123 | 91 | const struct fscrypt_info *ci = inode->i_crypt_info; |
|---|
| 124 | | - struct crypto_skcipher *tfm = ci->ci_key.tfm; |
|---|
| 92 | + struct crypto_skcipher *tfm = ci->ci_enc_key.tfm; |
|---|
| 125 | 93 | union fscrypt_iv iv; |
|---|
| 126 | 94 | struct scatterlist sg; |
|---|
| 127 | 95 | int res; |
|---|
| .. | .. |
|---|
| 177 | 145 | DECLARE_CRYPTO_WAIT(wait); |
|---|
| 178 | 146 | struct scatterlist src_sg, dst_sg; |
|---|
| 179 | 147 | const struct fscrypt_info *ci = inode->i_crypt_info; |
|---|
| 180 | | - struct crypto_skcipher *tfm = ci->ci_key.tfm; |
|---|
| 148 | + struct crypto_skcipher *tfm = ci->ci_enc_key.tfm; |
|---|
| 181 | 149 | union fscrypt_iv iv; |
|---|
| 182 | 150 | int res; |
|---|
| 183 | 151 | |
|---|
| .. | .. |
|---|
| 265 | 233 | return cp - dst; |
|---|
| 266 | 234 | } |
|---|
| 267 | 235 | |
|---|
| 268 | | -bool fscrypt_fname_encrypted_size(const struct inode *inode, u32 orig_len, |
|---|
| 269 | | - u32 max_len, u32 *encrypted_len_ret) |
|---|
| 236 | +bool fscrypt_fname_encrypted_size(const union fscrypt_policy *policy, |
|---|
| 237 | + u32 orig_len, u32 max_len, |
|---|
| 238 | + u32 *encrypted_len_ret) |
|---|
| 270 | 239 | { |
|---|
| 271 | | - const struct fscrypt_info *ci = inode->i_crypt_info; |
|---|
| 272 | | - int padding = 4 << (fscrypt_policy_flags(&ci->ci_policy) & |
|---|
| 240 | + int padding = 4 << (fscrypt_policy_flags(policy) & |
|---|
| 273 | 241 | FSCRYPT_POLICY_FLAGS_PAD_MASK); |
|---|
| 274 | 242 | u32 encrypted_len; |
|---|
| 275 | 243 | |
|---|
| .. | .. |
|---|
| 283 | 251 | |
|---|
| 284 | 252 | /** |
|---|
| 285 | 253 | * fscrypt_fname_alloc_buffer() - allocate a buffer for presented filenames |
|---|
| 286 | | - * @inode: inode of the parent directory (for regular filenames) |
|---|
| 287 | | - * or of the symlink (for symlink targets) |
|---|
| 288 | 254 | * @max_encrypted_len: maximum length of encrypted filenames the buffer will be |
|---|
| 289 | 255 | * used to present |
|---|
| 290 | 256 | * @crypto_str: (output) buffer to allocate |
|---|
| .. | .. |
|---|
| 294 | 260 | * |
|---|
| 295 | 261 | * Return: 0 on success, -errno on failure |
|---|
| 296 | 262 | */ |
|---|
| 297 | | -int fscrypt_fname_alloc_buffer(const struct inode *inode, |
|---|
| 298 | | - u32 max_encrypted_len, |
|---|
| 263 | +int fscrypt_fname_alloc_buffer(u32 max_encrypted_len, |
|---|
| 299 | 264 | struct fscrypt_str *crypto_str) |
|---|
| 300 | 265 | { |
|---|
| 301 | 266 | const u32 max_encoded_len = BASE64_CHARS(FSCRYPT_NOKEY_NAME_MAX); |
|---|
| .. | .. |
|---|
| 355 | 320 | const struct qstr qname = FSTR_TO_QSTR(iname); |
|---|
| 356 | 321 | struct fscrypt_nokey_name nokey_name; |
|---|
| 357 | 322 | u32 size; /* size of the unencoded no-key name */ |
|---|
| 358 | | - int err; |
|---|
| 359 | 323 | |
|---|
| 360 | 324 | if (fscrypt_is_dot_dotdot(&qname)) { |
|---|
| 361 | 325 | oname->name[0] = '.'; |
|---|
| .. | .. |
|---|
| 382 | 346 | |
|---|
| 383 | 347 | nokey_name.dirhash[0] = hash; |
|---|
| 384 | 348 | nokey_name.dirhash[1] = minor_hash; |
|---|
| 349 | + |
|---|
| 385 | 350 | if (iname->len <= sizeof(nokey_name.bytes)) { |
|---|
| 386 | 351 | memcpy(nokey_name.bytes, iname->name, iname->len); |
|---|
| 387 | 352 | size = offsetof(struct fscrypt_nokey_name, bytes[iname->len]); |
|---|
| 388 | 353 | } else { |
|---|
| 389 | 354 | memcpy(nokey_name.bytes, iname->name, sizeof(nokey_name.bytes)); |
|---|
| 390 | 355 | /* Compute strong hash of remaining part of name. */ |
|---|
| 391 | | - err = fscrypt_do_sha256(&iname->name[sizeof(nokey_name.bytes)], |
|---|
| 392 | | - iname->len - sizeof(nokey_name.bytes), |
|---|
| 393 | | - nokey_name.sha256); |
|---|
| 394 | | - if (err) |
|---|
| 395 | | - return err; |
|---|
| 356 | + sha256(&iname->name[sizeof(nokey_name.bytes)], |
|---|
| 357 | + iname->len - sizeof(nokey_name.bytes), |
|---|
| 358 | + nokey_name.sha256); |
|---|
| 396 | 359 | size = FSCRYPT_NOKEY_NAME_MAX; |
|---|
| 397 | 360 | } |
|---|
| 398 | 361 | oname->len = base64_encode((const u8 *)&nokey_name, size, oname->name); |
|---|
| .. | .. |
|---|
| 415 | 378 | * directory's encryption key, then @iname is the plaintext, so we encrypt it to |
|---|
| 416 | 379 | * get the disk_name. |
|---|
| 417 | 380 | * |
|---|
| 418 | | - * Else, for keyless @lookup operations, @iname is the presented ciphertext, so |
|---|
| 419 | | - * we decode it to get the fscrypt_nokey_name. Non-@lookup operations will be |
|---|
| 420 | | - * impossible in this case, so we fail them with ENOKEY. |
|---|
| 381 | + * Else, for keyless @lookup operations, @iname should be a no-key name, so we |
|---|
| 382 | + * decode it to get the struct fscrypt_nokey_name. Non-@lookup operations will |
|---|
| 383 | + * be impossible in this case, so we fail them with ENOKEY. |
|---|
| 421 | 384 | * |
|---|
| 422 | 385 | * If successful, fscrypt_free_filename() must be called later to clean up. |
|---|
| 423 | 386 | * |
|---|
| .. | .. |
|---|
| 437 | 400 | fname->disk_name.len = iname->len; |
|---|
| 438 | 401 | return 0; |
|---|
| 439 | 402 | } |
|---|
| 440 | | - ret = fscrypt_get_encryption_info(dir); |
|---|
| 403 | + ret = fscrypt_get_encryption_info(dir, lookup); |
|---|
| 441 | 404 | if (ret) |
|---|
| 442 | 405 | return ret; |
|---|
| 443 | 406 | |
|---|
| 444 | 407 | if (fscrypt_has_encryption_key(dir)) { |
|---|
| 445 | | - if (!fscrypt_fname_encrypted_size(dir, iname->len, |
|---|
| 408 | + if (!fscrypt_fname_encrypted_size(&dir->i_crypt_info->ci_policy, |
|---|
| 409 | + iname->len, |
|---|
| 446 | 410 | dir->i_sb->s_cop->max_namelen, |
|---|
| 447 | 411 | &fname->crypto_buf.len)) |
|---|
| 448 | 412 | return -ENAMETOOLONG; |
|---|
| .. | .. |
|---|
| 461 | 425 | } |
|---|
| 462 | 426 | if (!lookup) |
|---|
| 463 | 427 | return -ENOKEY; |
|---|
| 464 | | - fname->is_ciphertext_name = true; |
|---|
| 428 | + fname->is_nokey_name = true; |
|---|
| 465 | 429 | |
|---|
| 466 | 430 | /* |
|---|
| 467 | 431 | * We don't have the key and we are doing a lookup; decode the |
|---|
| .. | .. |
|---|
| 520 | 484 | { |
|---|
| 521 | 485 | const struct fscrypt_nokey_name *nokey_name = |
|---|
| 522 | 486 | (const void *)fname->crypto_buf.name; |
|---|
| 523 | | - u8 sha256[SHA256_DIGEST_SIZE]; |
|---|
| 487 | + u8 digest[SHA256_DIGEST_SIZE]; |
|---|
| 524 | 488 | |
|---|
| 525 | 489 | if (likely(fname->disk_name.name)) { |
|---|
| 526 | 490 | if (de_name_len != fname->disk_name.len) |
|---|
| .. | .. |
|---|
| 531 | 495 | return false; |
|---|
| 532 | 496 | if (memcmp(de_name, nokey_name->bytes, sizeof(nokey_name->bytes))) |
|---|
| 533 | 497 | return false; |
|---|
| 534 | | - if (fscrypt_do_sha256(&de_name[sizeof(nokey_name->bytes)], |
|---|
| 535 | | - de_name_len - sizeof(nokey_name->bytes), sha256)) |
|---|
| 536 | | - return false; |
|---|
| 537 | | - return !memcmp(sha256, nokey_name->sha256, sizeof(sha256)); |
|---|
| 498 | + sha256(&de_name[sizeof(nokey_name->bytes)], |
|---|
| 499 | + de_name_len - sizeof(nokey_name->bytes), digest); |
|---|
| 500 | + return !memcmp(digest, nokey_name->sha256, sizeof(digest)); |
|---|
| 538 | 501 | } |
|---|
| 539 | 502 | EXPORT_SYMBOL_GPL(fscrypt_match_name); |
|---|
| 540 | 503 | |
|---|
| .. | .. |
|---|
| 571 | 534 | |
|---|
| 572 | 535 | /* |
|---|
| 573 | 536 | * Plaintext names are always valid, since fscrypt doesn't support |
|---|
| 574 | | - * reverting to ciphertext names without evicting the directory's inode |
|---|
| 537 | + * reverting to no-key names without evicting the directory's inode |
|---|
| 575 | 538 | * -- which implies eviction of the dentries in the directory. |
|---|
| 576 | 539 | */ |
|---|
| 577 | | - if (!(dentry->d_flags & DCACHE_ENCRYPTED_NAME)) |
|---|
| 540 | + if (!(dentry->d_flags & DCACHE_NOKEY_NAME)) |
|---|
| 578 | 541 | return 1; |
|---|
| 579 | 542 | |
|---|
| 580 | 543 | /* |
|---|
| 581 | | - * Ciphertext name; valid if the directory's key is still unavailable. |
|---|
| 544 | + * No-key name; valid if the directory's key is still unavailable. |
|---|
| 582 | 545 | * |
|---|
| 583 | | - * Although fscrypt forbids rename() on ciphertext names, we still must |
|---|
| 584 | | - * use dget_parent() here rather than use ->d_parent directly. That's |
|---|
| 546 | + * Although fscrypt forbids rename() on no-key names, we still must use |
|---|
| 547 | + * dget_parent() here rather than use ->d_parent directly. That's |
|---|
| 585 | 548 | * because a corrupted fs image may contain directory hard links, which |
|---|
| 586 | 549 | * the VFS handles by moving the directory's dentry tree in the dcache |
|---|
| 587 | 550 | * each time ->lookup() finds the directory and it already has a dentry |
|---|
| .. | .. |
|---|
| 593 | 556 | return -ECHILD; |
|---|
| 594 | 557 | |
|---|
| 595 | 558 | dir = dget_parent(dentry); |
|---|
| 596 | | - err = fscrypt_get_encryption_info(d_inode(dir)); |
|---|
| 559 | + /* |
|---|
| 560 | + * Pass allow_unsupported=true, so that files with an unsupported |
|---|
| 561 | + * encryption policy can be deleted. |
|---|
| 562 | + */ |
|---|
| 563 | + err = fscrypt_get_encryption_info(d_inode(dir), true); |
|---|
| 597 | 564 | valid = !fscrypt_has_encryption_key(d_inode(dir)); |
|---|
| 598 | 565 | dput(dir); |
|---|
| 599 | 566 | |
|---|