]> Pileus Git - ~andy/linux/blobdiff - fs/ecryptfs/crypto.c
ecryptfs: avoid ctx initialization race
[~andy/linux] / fs / ecryptfs / crypto.c
index 3bd35e24b9e92f2b0cab39d44a49f280ce6a1215..c88e355f7635f61e59f58cb4acc617b84928e4e5 100644 (file)
@@ -614,30 +614,30 @@ 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:
        return rc;
 }