hc
2024-05-10 37f49e37ab4cb5d0bc4c60eb5c6d4dd57db767bb
kernel/fs/crypto/fname.c
....@@ -61,38 +61,6 @@
6161 */
6262 #define FSCRYPT_NOKEY_NAME_MAX offsetofend(struct fscrypt_nokey_name, sha256)
6363
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
-
9664 static inline bool fscrypt_is_dot_dotdot(const struct qstr *str)
9765 {
9866 if (str->len == 1 && str->name[0] == '.')
....@@ -121,7 +89,7 @@
12189 struct skcipher_request *req = NULL;
12290 DECLARE_CRYPTO_WAIT(wait);
12391 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;
12593 union fscrypt_iv iv;
12694 struct scatterlist sg;
12795 int res;
....@@ -177,7 +145,7 @@
177145 DECLARE_CRYPTO_WAIT(wait);
178146 struct scatterlist src_sg, dst_sg;
179147 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;
181149 union fscrypt_iv iv;
182150 int res;
183151
....@@ -265,11 +233,11 @@
265233 return cp - dst;
266234 }
267235
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)
270239 {
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) &
273241 FSCRYPT_POLICY_FLAGS_PAD_MASK);
274242 u32 encrypted_len;
275243
....@@ -283,8 +251,6 @@
283251
284252 /**
285253 * 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)
288254 * @max_encrypted_len: maximum length of encrypted filenames the buffer will be
289255 * used to present
290256 * @crypto_str: (output) buffer to allocate
....@@ -294,8 +260,7 @@
294260 *
295261 * Return: 0 on success, -errno on failure
296262 */
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,
299264 struct fscrypt_str *crypto_str)
300265 {
301266 const u32 max_encoded_len = BASE64_CHARS(FSCRYPT_NOKEY_NAME_MAX);
....@@ -355,7 +320,6 @@
355320 const struct qstr qname = FSTR_TO_QSTR(iname);
356321 struct fscrypt_nokey_name nokey_name;
357322 u32 size; /* size of the unencoded no-key name */
358
- int err;
359323
360324 if (fscrypt_is_dot_dotdot(&qname)) {
361325 oname->name[0] = '.';
....@@ -382,17 +346,16 @@
382346
383347 nokey_name.dirhash[0] = hash;
384348 nokey_name.dirhash[1] = minor_hash;
349
+
385350 if (iname->len <= sizeof(nokey_name.bytes)) {
386351 memcpy(nokey_name.bytes, iname->name, iname->len);
387352 size = offsetof(struct fscrypt_nokey_name, bytes[iname->len]);
388353 } else {
389354 memcpy(nokey_name.bytes, iname->name, sizeof(nokey_name.bytes));
390355 /* 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);
396359 size = FSCRYPT_NOKEY_NAME_MAX;
397360 }
398361 oname->len = base64_encode((const u8 *)&nokey_name, size, oname->name);
....@@ -415,9 +378,9 @@
415378 * directory's encryption key, then @iname is the plaintext, so we encrypt it to
416379 * get the disk_name.
417380 *
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.
421384 *
422385 * If successful, fscrypt_free_filename() must be called later to clean up.
423386 *
....@@ -437,12 +400,13 @@
437400 fname->disk_name.len = iname->len;
438401 return 0;
439402 }
440
- ret = fscrypt_get_encryption_info(dir);
403
+ ret = fscrypt_get_encryption_info(dir, lookup);
441404 if (ret)
442405 return ret;
443406
444407 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,
446410 dir->i_sb->s_cop->max_namelen,
447411 &fname->crypto_buf.len))
448412 return -ENAMETOOLONG;
....@@ -461,7 +425,7 @@
461425 }
462426 if (!lookup)
463427 return -ENOKEY;
464
- fname->is_ciphertext_name = true;
428
+ fname->is_nokey_name = true;
465429
466430 /*
467431 * We don't have the key and we are doing a lookup; decode the
....@@ -520,7 +484,7 @@
520484 {
521485 const struct fscrypt_nokey_name *nokey_name =
522486 (const void *)fname->crypto_buf.name;
523
- u8 sha256[SHA256_DIGEST_SIZE];
487
+ u8 digest[SHA256_DIGEST_SIZE];
524488
525489 if (likely(fname->disk_name.name)) {
526490 if (de_name_len != fname->disk_name.len)
....@@ -531,10 +495,9 @@
531495 return false;
532496 if (memcmp(de_name, nokey_name->bytes, sizeof(nokey_name->bytes)))
533497 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));
538501 }
539502 EXPORT_SYMBOL_GPL(fscrypt_match_name);
540503
....@@ -571,17 +534,17 @@
571534
572535 /*
573536 * 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
575538 * -- which implies eviction of the dentries in the directory.
576539 */
577
- if (!(dentry->d_flags & DCACHE_ENCRYPTED_NAME))
540
+ if (!(dentry->d_flags & DCACHE_NOKEY_NAME))
578541 return 1;
579542
580543 /*
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.
582545 *
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
585548 * because a corrupted fs image may contain directory hard links, which
586549 * the VFS handles by moving the directory's dentry tree in the dcache
587550 * each time ->lookup() finds the directory and it already has a dentry
....@@ -593,7 +556,11 @@
593556 return -ECHILD;
594557
595558 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);
597564 valid = !fscrypt_has_encryption_key(d_inode(dir));
598565 dput(dir);
599566