Skip to contentSkip to conversation

ecryptfs: avoid ctx initialization race

PATCH In review Kees Cook 2 participants 3 messages Aug 13, 2013 – Sep 7, 2013 Ecrypt file system

lore
Share & tools
lore

Kees Cook

author
#0 lore raw

PATCH 1/1 ecryptfs: avoid ctx initialization race

It might be possible for two callers to race the mutex lock after the NULL ctx check. Instead, move the lock above the check so there isn't the possibility of leaking a crypto ctx. Additionally, report the full algo name when failing.

---
 fs/ecryptfs/crypto.c |   11 ++++++-----
 1 file changed, 6 insertions(+), 5 deletions(-)
fs/ecryptfs/crypto.c +6 -5
index d107576..c134346 100644
@@ -618,27 +618,28 @@ int ecryptfs_init_crypt_ctx(struct ecryptfs_crypt_stat *crypt_stat)
 			"key_size_bits = [%zd]\n",
 			crypt_stat->cipher, (int)strlen(crypt_stat->cipher),
 			crypt_stat->key_size << 3);
+	mutex_lock(&crypt_stat->cs_tfm_mutex);
 	if (crypt_stat->tfm) {
 		rc = 0;
-		goto out;
+		goto out_unlock;
 	}
-	mutex_lock(&crypt_stat->cs_tfm_mutex);
 	rc = ecryptfs_crypto_api_algify_cipher_name(&full_alg_name,
 						    crypt_stat->cipher, "cbc");
 	if (rc)
 		goto out_unlock;
 	crypt_stat->tfm = crypto_alloc_ablkcipher(full_alg_name, 0, 0);
-	kfree(full_alg_name);
 	if (IS_ERR(crypt_stat->tfm)) {
 		rc = PTR_ERR(crypt_stat->tfm);
 		crypt_stat->tfm = NULL;
 		ecryptfs_printk(KERN_ERR, "cryptfs: init_crypt_ctx(): "
 				"Error initializing cipher [%s]\n",
-				crypt_stat->cipher);
-		goto out_unlock;
+				full_alg_name);
+		goto out_free;
 	}
 	crypto_ablkcipher_set_flags(crypt_stat->tfm, CRYPTO_TFM_REQ_WEAK_KEY);
 	rc = 0;
+out_free:
+	kfree(full_alg_name);
 out_unlock:
 	mutex_unlock(&crypt_stat->cs_tfm_mutex);
 out:
Signature · 6 lines
1.7.9.5


-- 
Kees Cook
Chrome OS Security
#1 lore raw
Quoted 6 lines · Kees Cook

It might be possible for two callers to race the mutex lock after the NULL ctx check. Instead, move the lock above the check so there isn't the possibility of leaking a crypto ctx. Additionally, report the full algo name when failing.

Thanks, Kees!

I've pushed this to my next branch and it'll be included in a pull request early next week.

I made one small change to this patch. See below.

Quoted 32 lines
---
 fs/ecryptfs/crypto.c |   11 ++++++-----
 1 file changed, 6 insertions(+), 5 deletions(-)
fs/ecryptfs/crypto.c +2 -5
index d107576..c134346 100644
@@ -618,27 +618,28 @@ int ecryptfs_init_crypt_ctx(struct ecryptfs_crypt_stat *crypt_stat)
 			"key_size_bits = [%zd]\n",
 			crypt_stat->cipher, (int)strlen(crypt_stat->cipher),
 			crypt_stat->key_size << 3);
+	mutex_lock(&crypt_stat->cs_tfm_mutex);
 	if (crypt_stat->tfm) {
 		rc = 0;
-		goto out;
+		goto out_unlock;
 	}
-	mutex_lock(&crypt_stat->cs_tfm_mutex);
 	rc = ecryptfs_crypto_api_algify_cipher_name(&full_alg_name,
 						    crypt_stat->cipher, "cbc");
 	if (rc)
 		goto out_unlock;
 	crypt_stat->tfm = crypto_alloc_ablkcipher(full_alg_name, 0, 0);
-	kfree(full_alg_name);
 	if (IS_ERR(crypt_stat->tfm)) {
 		rc = PTR_ERR(crypt_stat->tfm);
 		crypt_stat->tfm = NULL;
 		ecryptfs_printk(KERN_ERR, "cryptfs: init_crypt_ctx(): "
 				"Error initializing cipher [%s]\n",
-				crypt_stat->cipher);
-		goto out_unlock;

fs/ecryptfs/crypto.c int ecryptfs_init_crypt_ctx(struct ecryptfs_crypt_stat *crypt_stat) · line 645

Quoted 10 lines
fs/ecryptfs/crypto.c +4 -0
+				full_alg_name);
+		goto out_free;
 	}
 	crypto_ablkcipher_set_flags(crypt_stat->tfm, CRYPTO_TFM_REQ_WEAK_KEY);
 	rc = 0;
+out_free:
+	kfree(full_alg_name);
 out_unlock:
 	mutex_unlock(&crypt_stat->cs_tfm_mutex);
 out:

The out label is no longer used. I removed it when I committed this patch.

Tyler

author
#2 lore raw
Quoted 52 lines · Tyler Hicks
Quoted 6 lines · Kees Cook

It might be possible for two callers to race the mutex lock after the NULL ctx check. Instead, move the lock above the check so there isn't the possibility of leaking a crypto ctx. Additionally, report the full algo name when failing.

Thanks, Kees!

I've pushed this to my next branch and it'll be included in a pull request early next week.

I made one small change to this patch. See below.

Quoted 37 lines
---
 fs/ecryptfs/crypto.c |   11 ++++++-----
 1 file changed, 6 insertions(+), 5 deletions(-)
fs/ecryptfs/crypto.c +4 -5
index d107576..c134346 100644
@@ -618,27 +618,28 @@ int ecryptfs_init_crypt_ctx(struct ecryptfs_crypt_stat *crypt_stat)
                      "key_size_bits = [%zd]\n",
                      crypt_stat->cipher, (int)strlen(crypt_stat->cipher),
                      crypt_stat->key_size << 3);
+     mutex_lock(&crypt_stat->cs_tfm_mutex);
      if (crypt_stat->tfm) {
              rc = 0;
-             goto out;
+             goto out_unlock;
      }
-     mutex_lock(&crypt_stat->cs_tfm_mutex);
      rc = ecryptfs_crypto_api_algify_cipher_name(&full_alg_name,
                                                  crypt_stat->cipher, "cbc");
      if (rc)
              goto out_unlock;
      crypt_stat->tfm = crypto_alloc_ablkcipher(full_alg_name, 0, 0);
-     kfree(full_alg_name);
      if (IS_ERR(crypt_stat->tfm)) {
              rc = PTR_ERR(crypt_stat->tfm);
              crypt_stat->tfm = NULL;
              ecryptfs_printk(KERN_ERR, "cryptfs: init_crypt_ctx(): "
                              "Error initializing cipher [%s]\n",
-                             crypt_stat->cipher);
-             goto out_unlock;
+                             full_alg_name);
+             goto out_free;
      }
      crypto_ablkcipher_set_flags(crypt_stat->tfm, CRYPTO_TFM_REQ_WEAK_KEY);
      rc = 0;
Quoted 8 lines

fs/ecryptfs/crypto.c int ecryptfs_init_crypt_ctx(struct ecryptfs_crypt_stat *crypt_stat) · line 645

Quoted 5 lines
fs/ecryptfs/crypto.c +2 -0
+out_free:
+     kfree(full_alg_name);
 out_unlock:
      mutex_unlock(&crypt_stat->cs_tfm_mutex);
 out:

The out label is no longer used. I removed it when I committed this patch.

Ah! Yes, good call. Thanks,

-Kees

Signature · 2 lines
Kees Cook
Chrome OS Security