Skip to contentSkip to conversation

fscrypt: avoid data race on fscrypt_mode::logged_impl_name

PATCH Applied Eric Biggers 1 participant 2 messages Oct 21, 2019 – Nov 6, 2019 fscrypt: file system level encryption support

lore
Share & tools
lore

Eric Biggers

author maintainer
#0 lore raw

PATCH v2 1/1 fscrypt: avoid data race on fscrypt_mode::logged_impl_name

From: Eric Biggers <[email protected]>

The access to logged_impl_name is technically a data race, which tools like KCSAN could complain about in the future. See:
https://github.com/google/ktsan/wiki/READ_ONCE-and-WRITE_ONCE

Fix by using xchg(), which also ensures that only one thread does the logging.

This also required switching from bool to int, to avoid a build error on the RISC-V architecture which doesn't implement xchg on bytes.

---
 fs/crypto/fscrypt_private.h | 2 +-
 fs/crypto/keysetup.c        | 6 ++----
 2 files changed, 3 insertions(+), 5 deletions(-)
fs/crypto/fscrypt_private.h +1 -1
index dacf8fcbac3be..d9a3e8614049f 100644
@@ -435,7 +435,7 @@ struct fscrypt_mode {
 	const char *cipher_str;
 	int keysize;
 	int ivsize;
-	bool logged_impl_name;
+	int logged_impl_name;
 };
 
 static inline bool
fs/crypto/keysetup.c +2 -4
index b03b33643e4b2..28bc2da9be3c7 100644
@@ -81,15 +81,13 @@ struct crypto_skcipher *fscrypt_allocate_skcipher(struct fscrypt_mode *mode,
 			    mode->cipher_str, PTR_ERR(tfm));
 		return tfm;
 	}
-	if (unlikely(!mode->logged_impl_name)) {
+	if (!xchg(&mode->logged_impl_name, 1)) {
 		/*
 		 * fscrypt performance can vary greatly depending on which
 		 * crypto algorithm implementation is used.  Help people debug
 		 * performance problems by logging the ->cra_driver_name the
-		 * first time a mode is used.  Note that multiple threads can
-		 * race here, but it doesn't really matter.
+		 * first time a mode is used.
 		 */
-		mode->logged_impl_name = true;
 		pr_info("fscrypt: %s using implementation \"%s\"\n",
 			mode->friendly_name,
 			crypto_skcipher_alg(tfm)->base.cra_driver_name);
Signature · 1 line
2.23.0.866.gb869b98d4c-goog
author maintainer Applied
#1 lore raw
Quoted 6 lines · Eric Biggers

From: Eric Biggers <[email protected]>

The access to logged_impl_name is technically a data race, which tools like KCSAN could complain about in the future. See:
https://github.com/google/ktsan/wiki/READ_ONCE-and-WRITE_ONCE

Quoted 7 lines

Fix by using xchg(), which also ensures that only one thread does the logging.

This also required switching from bool to int, to avoid a build error on the RISC-V architecture which doesn't implement xchg on bytes.

Applied to fscrypt.git#master for 5.5.

- Eric