crypto: aesni-intel - Don't leak memory in rfc4106_set_hash_subkey
authorJesper Juhl <jj@chaosbits.net>
Sun, 23 Jan 2011 07:56:36 +0000 (18:56 +1100)
committerHerbert Xu <herbert@gondor.apana.org.au>
Sun, 23 Jan 2011 07:59:17 +0000 (18:59 +1100)
There's a small memory leak in
arch/x86/crypto/aesni-intel_glue.c::rfc4106_set_hash_subkey(). If the call
to kmalloc() fails and returns NULL then the memory allocated previously
by ablkcipher_request_alloc() is not freed when we leave the function.

I could have just added a call to ablkcipher_request_free() before we
return -ENOMEM, but that started to look too much like the code we
already had at the end of the function, so I chose instead to rework the
code a bit so that there are now a few labels at the end that we goto when
various allocations fail, so we don't have to repeat the same blocks of
code (this also reduces the object code size slightly).

Signed-off-by: Jesper Juhl <jj@chaosbits.net>
Signed-off-by: Herbert Xu <herbert@gondor.apana.org.au>
arch/x86/crypto/aesni-intel_glue.c

index e1e60c7d5813c8c3f095b255a5f6f97b1b0f0a10..e0135526345dd542dc4177d0610d6d3393d76ec9 100644 (file)
@@ -873,21 +873,19 @@ rfc4106_set_hash_subkey(u8 *hash_subkey, const u8 *key, unsigned int key_len)
        crypto_ablkcipher_clear_flags(ctr_tfm, ~0);
 
        ret = crypto_ablkcipher_setkey(ctr_tfm, key, key_len);
-       if (ret) {
-               crypto_free_ablkcipher(ctr_tfm);
-               return ret;
-       }
+       if (ret)
+               goto out;
 
        req = ablkcipher_request_alloc(ctr_tfm, GFP_KERNEL);
        if (!req) {
-               crypto_free_ablkcipher(ctr_tfm);
-               return -EINVAL;
+               ret = -EINVAL;
+               goto out_free_ablkcipher;
        }
 
        req_data = kmalloc(sizeof(*req_data), GFP_KERNEL);
        if (!req_data) {
-               crypto_free_ablkcipher(ctr_tfm);
-               return -ENOMEM;
+               ret = -ENOMEM;
+               goto out_free_request;
        }
        memset(req_data->iv, 0, sizeof(req_data->iv));
 
@@ -913,9 +911,12 @@ rfc4106_set_hash_subkey(u8 *hash_subkey, const u8 *key, unsigned int key_len)
                if (!ret)
                        ret = req_data->result.err;
        }
+out_free_request:
        ablkcipher_request_free(req);
        kfree(req_data);
+out_free_ablkcipher:
        crypto_free_ablkcipher(ctr_tfm);
+out:
        return ret;
 }