Skip to content

Commit bd0d97b

Browse files
committed
fscrypt: switch fscrypt_do_sha256() to use the SHA-256 library
fscrypt_do_sha256() is only used for hashing encrypted filenames to create no-key tokens, which isn't performance-critical. Therefore a C implementation of SHA-256 is sufficient. Also, the logic to create no-key tokens is always potentially needed. This differs from fscrypt's other dependencies on crypto API algorithms, which are conditionally needed depending on what encryption policies userspace is using. Therefore, for fscrypt there isn't much benefit to allowing SHA-256 to be a loadable module. So, make fscrypt_do_sha256() use the SHA-256 library instead of the crypto_shash API. This is much simpler, since it avoids having to implement one-time-init (which is hard to do correctly, and in fact was implemented incorrectly) and handle failures to allocate the crypto_shash object. Fixes: edc440e ("fscrypt: improve format of no-key names") Cc: Daniel Rosenberg <[email protected]> Link: https://lore.kernel.org/r/[email protected] Signed-off-by: Eric Biggers <[email protected]>
1 parent f000223 commit bd0d97b

File tree

2 files changed

+11
-32
lines changed

2 files changed

+11
-32
lines changed

fs/crypto/Kconfig

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ config FS_ENCRYPTION
44
select CRYPTO
55
select CRYPTO_HASH
66
select CRYPTO_SKCIPHER
7+
select CRYPTO_LIB_SHA256
78
select KEYS
89
help
910
Enable encryption of files and directories. This
@@ -21,7 +22,6 @@ config FS_ENCRYPTION_ALGS
2122
select CRYPTO_CTS
2223
select CRYPTO_ECB
2324
select CRYPTO_HMAC
24-
select CRYPTO_SHA256
2525
select CRYPTO_SHA512
2626
select CRYPTO_XTS
2727

fs/crypto/fname.c

Lines changed: 10 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -61,30 +61,13 @@ struct fscrypt_nokey_name {
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)
64+
static void fscrypt_do_sha256(const u8 *data, unsigned int data_len, u8 *result)
6765
{
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-
}
66+
struct sha256_state sctx;
8667

87-
return crypto_shash_tfm_digest(tfm, data, data_len, result);
68+
sha256_init(&sctx);
69+
sha256_update(&sctx, data, data_len);
70+
sha256_final(&sctx, result);
8871
}
8972

9073
static inline bool fscrypt_is_dot_dotdot(const struct qstr *str)
@@ -349,7 +332,6 @@ int fscrypt_fname_disk_to_usr(const struct inode *inode,
349332
const struct qstr qname = FSTR_TO_QSTR(iname);
350333
struct fscrypt_nokey_name nokey_name;
351334
u32 size; /* size of the unencoded no-key name */
352-
int err;
353335

354336
if (fscrypt_is_dot_dotdot(&qname)) {
355337
oname->name[0] = '.';
@@ -387,11 +369,9 @@ int fscrypt_fname_disk_to_usr(const struct inode *inode,
387369
} else {
388370
memcpy(nokey_name.bytes, iname->name, sizeof(nokey_name.bytes));
389371
/* Compute strong hash of remaining part of name. */
390-
err = fscrypt_do_sha256(&iname->name[sizeof(nokey_name.bytes)],
391-
iname->len - sizeof(nokey_name.bytes),
392-
nokey_name.sha256);
393-
if (err)
394-
return err;
372+
fscrypt_do_sha256(&iname->name[sizeof(nokey_name.bytes)],
373+
iname->len - sizeof(nokey_name.bytes),
374+
nokey_name.sha256);
395375
size = FSCRYPT_NOKEY_NAME_MAX;
396376
}
397377
oname->len = base64_encode((const u8 *)&nokey_name, size, oname->name);
@@ -530,9 +510,8 @@ bool fscrypt_match_name(const struct fscrypt_name *fname,
530510
return false;
531511
if (memcmp(de_name, nokey_name->bytes, sizeof(nokey_name->bytes)))
532512
return false;
533-
if (fscrypt_do_sha256(&de_name[sizeof(nokey_name->bytes)],
534-
de_name_len - sizeof(nokey_name->bytes), sha256))
535-
return false;
513+
fscrypt_do_sha256(&de_name[sizeof(nokey_name->bytes)],
514+
de_name_len - sizeof(nokey_name->bytes), sha256);
536515
return !memcmp(sha256, nokey_name->sha256, sizeof(sha256));
537516
}
538517
EXPORT_SYMBOL_GPL(fscrypt_match_name);

0 commit comments

Comments
 (0)