* [PATCH] evm: Fix memleak in init_desc
@ 2021-01-09 11:33 Dinghao Liu
2021-01-09 17:07 ` Eric Biggers
0 siblings, 1 reply; 5+ messages in thread
From: Dinghao Liu @ 2021-01-09 11:33 UTC (permalink / raw)
To: dinghao.liu, kjlu
Cc: Mimi Zohar, James Morris, Serge E. Hallyn, Dmitry Kasatkin,
linux-integrity, linux-security-module, linux-kernel
When kmalloc() fails, tmp_tfm allocated by
crypto_alloc_shash() has not been freed, which
leads to memleak.
Fixes: d46eb3699502b ("evm: crypto hash replaced by shash")
Signed-off-by: Dinghao Liu <dinghao.liu@zju.edu.cn>
---
security/integrity/evm/evm_crypto.c | 9 +++++++--
1 file changed, 7 insertions(+), 2 deletions(-)
diff --git a/security/integrity/evm/evm_crypto.c b/security/integrity/evm/evm_crypto.c
index 168c3b78ac47..39fb31a638ac 100644
--- a/security/integrity/evm/evm_crypto.c
+++ b/security/integrity/evm/evm_crypto.c
@@ -73,7 +73,7 @@ static struct shash_desc *init_desc(char type, uint8_t hash_algo)
{
long rc;
const char *algo;
- struct crypto_shash **tfm, *tmp_tfm;
+ struct crypto_shash **tfm, *tmp_tfm = NULL;
struct shash_desc *desc;
if (type == EVM_XATTR_HMAC) {
@@ -118,13 +118,18 @@ static struct shash_desc *init_desc(char type, uint8_t hash_algo)
alloc:
desc = kmalloc(sizeof(*desc) + crypto_shash_descsize(*tfm),
GFP_KERNEL);
- if (!desc)
+ if (!desc) {
+ if (tmp_tfm)
+ crypto_free_shash(tmp_tfm);
return ERR_PTR(-ENOMEM);
+ }
desc->tfm = *tfm;
rc = crypto_shash_init(desc);
if (rc) {
+ if (tmp_tfm)
+ crypto_free_shash(tmp_tfm);
kfree(desc);
return ERR_PTR(rc);
}
--
2.17.1
^ permalink raw reply related [flat|nested] 5+ messages in thread
* Re: [PATCH] evm: Fix memleak in init_desc
2021-01-09 11:33 [PATCH] evm: Fix memleak in init_desc Dinghao Liu
@ 2021-01-09 17:07 ` Eric Biggers
2021-01-10 5:27 ` dinghao.liu
0 siblings, 1 reply; 5+ messages in thread
From: Eric Biggers @ 2021-01-09 17:07 UTC (permalink / raw)
To: Dinghao Liu
Cc: kjlu, Mimi Zohar, James Morris, Serge E. Hallyn, Dmitry Kasatkin,
linux-integrity, linux-security-module, linux-kernel
On Sat, Jan 09, 2021 at 07:33:05PM +0800, Dinghao Liu wrote:
> When kmalloc() fails, tmp_tfm allocated by
> crypto_alloc_shash() has not been freed, which
> leads to memleak.
>
> Fixes: d46eb3699502b ("evm: crypto hash replaced by shash")
> Signed-off-by: Dinghao Liu <dinghao.liu@zju.edu.cn>
> ---
> security/integrity/evm/evm_crypto.c | 9 +++++++--
> 1 file changed, 7 insertions(+), 2 deletions(-)
>
> diff --git a/security/integrity/evm/evm_crypto.c b/security/integrity/evm/evm_crypto.c
> index 168c3b78ac47..39fb31a638ac 100644
> --- a/security/integrity/evm/evm_crypto.c
> +++ b/security/integrity/evm/evm_crypto.c
> @@ -73,7 +73,7 @@ static struct shash_desc *init_desc(char type, uint8_t hash_algo)
> {
> long rc;
> const char *algo;
> - struct crypto_shash **tfm, *tmp_tfm;
> + struct crypto_shash **tfm, *tmp_tfm = NULL;
> struct shash_desc *desc;
>
> if (type == EVM_XATTR_HMAC) {
> @@ -118,13 +118,18 @@ static struct shash_desc *init_desc(char type, uint8_t hash_algo)
> alloc:
> desc = kmalloc(sizeof(*desc) + crypto_shash_descsize(*tfm),
> GFP_KERNEL);
> - if (!desc)
> + if (!desc) {
> + if (tmp_tfm)
> + crypto_free_shash(tmp_tfm);
> return ERR_PTR(-ENOMEM);
> + }
>
> desc->tfm = *tfm;
>
> rc = crypto_shash_init(desc);
> if (rc) {
> + if (tmp_tfm)
> + crypto_free_shash(tmp_tfm);
> kfree(desc);
> return ERR_PTR(rc);
> }
There's no need to check for NULL before calling crypto_free_shash().
- Eric
^ permalink raw reply [flat|nested] 5+ messages in thread
* Re: Re: [PATCH] evm: Fix memleak in init_desc
2021-01-09 17:07 ` Eric Biggers
@ 2021-01-10 5:27 ` dinghao.liu
2021-01-10 6:53 ` Eric Biggers
0 siblings, 1 reply; 5+ messages in thread
From: dinghao.liu @ 2021-01-10 5:27 UTC (permalink / raw)
To: Eric Biggers
Cc: kjlu, Mimi Zohar, James Morris, Serge E. Hallyn, Dmitry Kasatkin,
linux-integrity, linux-security-module, linux-kernel
> On Sat, Jan 09, 2021 at 07:33:05PM +0800, Dinghao Liu wrote:
> > When kmalloc() fails, tmp_tfm allocated by
> > crypto_alloc_shash() has not been freed, which
> > leads to memleak.
> >
> > Fixes: d46eb3699502b ("evm: crypto hash replaced by shash")
> > Signed-off-by: Dinghao Liu <dinghao.liu@zju.edu.cn>
> > ---
> > security/integrity/evm/evm_crypto.c | 9 +++++++--
> > 1 file changed, 7 insertions(+), 2 deletions(-)
> >
> > diff --git a/security/integrity/evm/evm_crypto.c b/security/integrity/evm/evm_crypto.c
> > index 168c3b78ac47..39fb31a638ac 100644
> > --- a/security/integrity/evm/evm_crypto.c
> > +++ b/security/integrity/evm/evm_crypto.c
> > @@ -73,7 +73,7 @@ static struct shash_desc *init_desc(char type, uint8_t hash_algo)
> > {
> > long rc;
> > const char *algo;
> > - struct crypto_shash **tfm, *tmp_tfm;
> > + struct crypto_shash **tfm, *tmp_tfm = NULL;
> > struct shash_desc *desc;
> >
> > if (type == EVM_XATTR_HMAC) {
> > @@ -118,13 +118,18 @@ static struct shash_desc *init_desc(char type, uint8_t hash_algo)
> > alloc:
> > desc = kmalloc(sizeof(*desc) + crypto_shash_descsize(*tfm),
> > GFP_KERNEL);
> > - if (!desc)
> > + if (!desc) {
> > + if (tmp_tfm)
> > + crypto_free_shash(tmp_tfm);
> > return ERR_PTR(-ENOMEM);
> > + }
> >
> > desc->tfm = *tfm;
> >
> > rc = crypto_shash_init(desc);
> > if (rc) {
> > + if (tmp_tfm)
> > + crypto_free_shash(tmp_tfm);
> > kfree(desc);
> > return ERR_PTR(rc);
> > }
>
> There's no need to check for NULL before calling crypto_free_shash().
>
I find there is a crypto_shash_tfm() in the definition of
crypto_free_shash(). Will this lead to null pointer dereference
when we use it to free a NULL pointer?
Regards,
Dinghao
^ permalink raw reply [flat|nested] 5+ messages in thread
* Re: Re: [PATCH] evm: Fix memleak in init_desc
2021-01-10 5:27 ` dinghao.liu
@ 2021-01-10 6:53 ` Eric Biggers
2021-01-10 7:01 ` dinghao.liu
0 siblings, 1 reply; 5+ messages in thread
From: Eric Biggers @ 2021-01-10 6:53 UTC (permalink / raw)
To: dinghao.liu
Cc: kjlu, Mimi Zohar, James Morris, Serge E. Hallyn, Dmitry Kasatkin,
linux-integrity, linux-security-module, linux-kernel
On Sun, Jan 10, 2021 at 01:27:09PM +0800, dinghao.liu@zju.edu.cn wrote:
> > On Sat, Jan 09, 2021 at 07:33:05PM +0800, Dinghao Liu wrote:
> > > When kmalloc() fails, tmp_tfm allocated by
> > > crypto_alloc_shash() has not been freed, which
> > > leads to memleak.
> > >
> > > Fixes: d46eb3699502b ("evm: crypto hash replaced by shash")
> > > Signed-off-by: Dinghao Liu <dinghao.liu@zju.edu.cn>
> > > ---
> > > security/integrity/evm/evm_crypto.c | 9 +++++++--
> > > 1 file changed, 7 insertions(+), 2 deletions(-)
> > >
> > > diff --git a/security/integrity/evm/evm_crypto.c b/security/integrity/evm/evm_crypto.c
> > > index 168c3b78ac47..39fb31a638ac 100644
> > > --- a/security/integrity/evm/evm_crypto.c
> > > +++ b/security/integrity/evm/evm_crypto.c
> > > @@ -73,7 +73,7 @@ static struct shash_desc *init_desc(char type, uint8_t hash_algo)
> > > {
> > > long rc;
> > > const char *algo;
> > > - struct crypto_shash **tfm, *tmp_tfm;
> > > + struct crypto_shash **tfm, *tmp_tfm = NULL;
> > > struct shash_desc *desc;
> > >
> > > if (type == EVM_XATTR_HMAC) {
> > > @@ -118,13 +118,18 @@ static struct shash_desc *init_desc(char type, uint8_t hash_algo)
> > > alloc:
> > > desc = kmalloc(sizeof(*desc) + crypto_shash_descsize(*tfm),
> > > GFP_KERNEL);
> > > - if (!desc)
> > > + if (!desc) {
> > > + if (tmp_tfm)
> > > + crypto_free_shash(tmp_tfm);
> > > return ERR_PTR(-ENOMEM);
> > > + }
> > >
> > > desc->tfm = *tfm;
> > >
> > > rc = crypto_shash_init(desc);
> > > if (rc) {
> > > + if (tmp_tfm)
> > > + crypto_free_shash(tmp_tfm);
> > > kfree(desc);
> > > return ERR_PTR(rc);
> > > }
> >
> > There's no need to check for NULL before calling crypto_free_shash().
> >
>
> I find there is a crypto_shash_tfm() in the definition of
> crypto_free_shash(). Will this lead to null pointer dereference
> when we use it to free a NULL pointer?
>
No. It does &tfm->base, not tfm->base.
- Eric
^ permalink raw reply [flat|nested] 5+ messages in thread
* Re: Re: Re: [PATCH] evm: Fix memleak in init_desc
2021-01-10 6:53 ` Eric Biggers
@ 2021-01-10 7:01 ` dinghao.liu
0 siblings, 0 replies; 5+ messages in thread
From: dinghao.liu @ 2021-01-10 7:01 UTC (permalink / raw)
To: Eric Biggers
Cc: kjlu, Mimi Zohar, James Morris, Serge E. Hallyn, Dmitry Kasatkin,
linux-integrity, linux-security-module, linux-kernel
> On Sun, Jan 10, 2021 at 01:27:09PM +0800, dinghao.liu@zju.edu.cn wrote:
> > > On Sat, Jan 09, 2021 at 07:33:05PM +0800, Dinghao Liu wrote:
> > > > When kmalloc() fails, tmp_tfm allocated by
> > > > crypto_alloc_shash() has not been freed, which
> > > > leads to memleak.
> > > >
> > > > Fixes: d46eb3699502b ("evm: crypto hash replaced by shash")
> > > > Signed-off-by: Dinghao Liu <dinghao.liu@zju.edu.cn>
> > > > ---
> > > > security/integrity/evm/evm_crypto.c | 9 +++++++--
> > > > 1 file changed, 7 insertions(+), 2 deletions(-)
> > > >
> > > > diff --git a/security/integrity/evm/evm_crypto.c b/security/integrity/evm/evm_crypto.c
> > > > index 168c3b78ac47..39fb31a638ac 100644
> > > > --- a/security/integrity/evm/evm_crypto.c
> > > > +++ b/security/integrity/evm/evm_crypto.c
> > > > @@ -73,7 +73,7 @@ static struct shash_desc *init_desc(char type, uint8_t hash_algo)
> > > > {
> > > > long rc;
> > > > const char *algo;
> > > > - struct crypto_shash **tfm, *tmp_tfm;
> > > > + struct crypto_shash **tfm, *tmp_tfm = NULL;
> > > > struct shash_desc *desc;
> > > >
> > > > if (type == EVM_XATTR_HMAC) {
> > > > @@ -118,13 +118,18 @@ static struct shash_desc *init_desc(char type, uint8_t hash_algo)
> > > > alloc:
> > > > desc = kmalloc(sizeof(*desc) + crypto_shash_descsize(*tfm),
> > > > GFP_KERNEL);
> > > > - if (!desc)
> > > > + if (!desc) {
> > > > + if (tmp_tfm)
> > > > + crypto_free_shash(tmp_tfm);
> > > > return ERR_PTR(-ENOMEM);
> > > > + }
> > > >
> > > > desc->tfm = *tfm;
> > > >
> > > > rc = crypto_shash_init(desc);
> > > > if (rc) {
> > > > + if (tmp_tfm)
> > > > + crypto_free_shash(tmp_tfm);
> > > > kfree(desc);
> > > > return ERR_PTR(rc);
> > > > }
> > >
> > > There's no need to check for NULL before calling crypto_free_shash().
> > >
> >
> > I find there is a crypto_shash_tfm() in the definition of
> > crypto_free_shash(). Will this lead to null pointer dereference
> > when we use it to free a NULL pointer?
> >
>
> No. It does &tfm->base, not tfm->base.
>
Thank you for your advice! I will resend a new patch soon.
Regards,
Dinghao
^ permalink raw reply [flat|nested] 5+ messages in thread
end of thread, other threads:[~2021-01-10 7:02 UTC | newest]
Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2021-01-09 11:33 [PATCH] evm: Fix memleak in init_desc Dinghao Liu
2021-01-09 17:07 ` Eric Biggers
2021-01-10 5:27 ` dinghao.liu
2021-01-10 6:53 ` Eric Biggers
2021-01-10 7:01 ` dinghao.liu
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).