Skip to contentSkip to conversation

fscrypt: don't load ->i_crypt_info before it's known to be valid

PATCH Applied Eric Biggers 1 participant 2 messages Jul 27, 2020 – Jul 30, 2020

Subsystems (2)
lore
Share & tools
lore

Eric Biggers

author maintainer
#0 lore raw

PATCH 1/1 fscrypt: don't load ->i_crypt_info before it's known to be valid

From: Eric Biggers <[email protected]>

In fscrypt_set_bio_crypt_ctx(), ->i_crypt_info isn't known to be non-NULL until we check fscrypt_inode_uses_inline_crypto(). So, load ->i_crypt_info after the check rather than before. This makes no difference currently, but it prevents people from introducing bugs where the pointer is dereferenced when it may be NULL.

---
 fs/crypto/inline_crypt.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
fs/crypto/inline_crypt.c +2 -1
index dfb06375099ae..b6b8574caa13c 100644
@@ -244,11 +244,12 @@ static void fscrypt_generate_dun(const struct fscrypt_info *ci, u64 lblk_num,
 void fscrypt_set_bio_crypt_ctx(struct bio *bio, const struct inode *inode,
 			       u64 first_lblk, gfp_t gfp_mask)
 {
-	const struct fscrypt_info *ci = inode->i_crypt_info;
+	const struct fscrypt_info *ci;
 	u64 dun[BLK_CRYPTO_DUN_ARRAY_SIZE];
 
 	if (!fscrypt_inode_uses_inline_crypto(inode))
 		return;
+	ci = inode->i_crypt_info;
 
 	fscrypt_generate_dun(ci, first_lblk, dun);
 	bio_crypt_set_ctx(bio, &ci->ci_enc_key.blk_key->base, dun, gfp_mask);
Signature · 1 line
2.27.0
author maintainer Applied
#1 lore raw
Quoted 23 lines · Eric Biggers

From: Eric Biggers <[email protected]>

In fscrypt_set_bio_crypt_ctx(), ->i_crypt_info isn't known to be non-NULL until we check fscrypt_inode_uses_inline_crypto(). So, load ->i_crypt_info after the check rather than before. This makes no difference currently, but it prevents people from introducing bugs where the pointer is dereferenced when it may be NULL.

---
 fs/crypto/inline_crypt.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
fs/crypto/inline_crypt.c +0 -0
index dfb06375099ae..b6b8574caa13c 100644
@@ -244,11 +244,12 @@ static void fscrypt_generate_dun(const struct fscrypt_info *ci, u64 lblk_num,
 void fscrypt_set_bio_crypt_ctx(struct bio *bio, const struct inode *inode,
 			       u64 first_lblk, gfp_t gfp_mask)
 {

fs/crypto/inline_crypt.c static void fscrypt_generate_dun(const struct fscrypt_info *ci, u64 lblk_num, · line 255

Quoted 10 lines
fs/crypto/inline_crypt.c +2 -1
-	const struct fscrypt_info *ci = inode->i_crypt_info;
+	const struct fscrypt_info *ci;
 	u64 dun[BLK_CRYPTO_DUN_ARRAY_SIZE];
 
 	if (!fscrypt_inode_uses_inline_crypto(inode))
 		return;
+	ci = inode->i_crypt_info;
 
 	fscrypt_generate_dun(ci, first_lblk, dun);
 	bio_crypt_set_ctx(bio, &ci->ci_enc_key.blk_key->base, dun, gfp_mask);

Applied to fscrypt.git#master for 5.9.

- Eric