evm: prevent racing during tfm allocation
There is a small chance of racing during tfm allocation. This patch fixes it. Signed-off-by: Dmitry Kasatkin <dmitry.kasatkin@intel.com> Acked-by: Mimi Zohar <zohar@us.ibm.com> Signed-off-by: James Morris <jmorris@namei.org>
This commit is contained in:
parent
d21b594518
commit
97426f9857
1 changed files with 9 additions and 0 deletions
|
@ -27,26 +27,35 @@ static int evmkey_len = MAX_KEY_SIZE;
|
||||||
|
|
||||||
struct crypto_shash *hmac_tfm;
|
struct crypto_shash *hmac_tfm;
|
||||||
|
|
||||||
|
static DEFINE_MUTEX(mutex);
|
||||||
|
|
||||||
static struct shash_desc *init_desc(void)
|
static struct shash_desc *init_desc(void)
|
||||||
{
|
{
|
||||||
int rc;
|
int rc;
|
||||||
struct shash_desc *desc;
|
struct shash_desc *desc;
|
||||||
|
|
||||||
if (hmac_tfm == NULL) {
|
if (hmac_tfm == NULL) {
|
||||||
|
mutex_lock(&mutex);
|
||||||
|
if (hmac_tfm)
|
||||||
|
goto out;
|
||||||
hmac_tfm = crypto_alloc_shash(evm_hmac, 0, CRYPTO_ALG_ASYNC);
|
hmac_tfm = crypto_alloc_shash(evm_hmac, 0, CRYPTO_ALG_ASYNC);
|
||||||
if (IS_ERR(hmac_tfm)) {
|
if (IS_ERR(hmac_tfm)) {
|
||||||
pr_err("Can not allocate %s (reason: %ld)\n",
|
pr_err("Can not allocate %s (reason: %ld)\n",
|
||||||
evm_hmac, PTR_ERR(hmac_tfm));
|
evm_hmac, PTR_ERR(hmac_tfm));
|
||||||
rc = PTR_ERR(hmac_tfm);
|
rc = PTR_ERR(hmac_tfm);
|
||||||
hmac_tfm = NULL;
|
hmac_tfm = NULL;
|
||||||
|
mutex_unlock(&mutex);
|
||||||
return ERR_PTR(rc);
|
return ERR_PTR(rc);
|
||||||
}
|
}
|
||||||
rc = crypto_shash_setkey(hmac_tfm, evmkey, evmkey_len);
|
rc = crypto_shash_setkey(hmac_tfm, evmkey, evmkey_len);
|
||||||
if (rc) {
|
if (rc) {
|
||||||
crypto_free_shash(hmac_tfm);
|
crypto_free_shash(hmac_tfm);
|
||||||
hmac_tfm = NULL;
|
hmac_tfm = NULL;
|
||||||
|
mutex_unlock(&mutex);
|
||||||
return ERR_PTR(rc);
|
return ERR_PTR(rc);
|
||||||
}
|
}
|
||||||
|
out:
|
||||||
|
mutex_unlock(&mutex);
|
||||||
}
|
}
|
||||||
|
|
||||||
desc = kmalloc(sizeof(*desc) + crypto_shash_descsize(hmac_tfm),
|
desc = kmalloc(sizeof(*desc) + crypto_shash_descsize(hmac_tfm),
|
||||||
|
|
Loading…
Add table
Reference in a new issue