2019-09-25 23:30:33

by Navid Emamdoost

[permalink] [raw]
Subject: [PATCH] nbd: prevent memory leak

In nbd_add_socket when krealloc succeeds, if nsock's allocation fail the
reallocted memory is leak. The correct behaviour should be assigning the
reallocted memory to config->socks right after success.

Signed-off-by: Navid Emamdoost <[email protected]>
---
drivers/block/nbd.c | 5 +++--
1 file changed, 3 insertions(+), 2 deletions(-)

diff --git a/drivers/block/nbd.c b/drivers/block/nbd.c
index a8e3815295fe..8ae3bd2e7b30 100644
--- a/drivers/block/nbd.c
+++ b/drivers/block/nbd.c
@@ -987,14 +987,15 @@ static int nbd_add_socket(struct nbd_device *nbd, unsigned long arg,
sockfd_put(sock);
return -ENOMEM;
}
+
+ config->socks = socks;
+
nsock = kzalloc(sizeof(struct nbd_sock), GFP_KERNEL);
if (!nsock) {
sockfd_put(sock);
return -ENOMEM;
}

- config->socks = socks;
-
nsock->fallback_index = -1;
nsock->dead = false;
mutex_init(&nsock->tx_lock);
--
2.17.1


2019-11-21 18:26:12

by Navid Emamdoost

[permalink] [raw]
Subject: Re: [PATCH] nbd: prevent memory leak

On Mon, Sep 23, 2019 at 3:10 PM Navid Emamdoost
<[email protected]> wrote:
>
> In nbd_add_socket when krealloc succeeds, if nsock's allocation fail the
> reallocted memory is leak. The correct behaviour should be assigning the
> reallocted memory to config->socks right after success.
>
> Signed-off-by: Navid Emamdoost <[email protected]>
> ---

Would you please review this patch?

Thanks,

> drivers/block/nbd.c | 5 +++--
> 1 file changed, 3 insertions(+), 2 deletions(-)
>
> diff --git a/drivers/block/nbd.c b/drivers/block/nbd.c
> index a8e3815295fe..8ae3bd2e7b30 100644
> --- a/drivers/block/nbd.c
> +++ b/drivers/block/nbd.c
> @@ -987,14 +987,15 @@ static int nbd_add_socket(struct nbd_device *nbd, unsigned long arg,
> sockfd_put(sock);
> return -ENOMEM;
> }
> +
> + config->socks = socks;
> +
> nsock = kzalloc(sizeof(struct nbd_sock), GFP_KERNEL);
> if (!nsock) {
> sockfd_put(sock);
> return -ENOMEM;
> }
>
> - config->socks = socks;
> -
> nsock->fallback_index = -1;
> nsock->dead = false;
> mutex_init(&nsock->tx_lock);
> --
> 2.17.1
>


--
Navid.

2019-11-21 18:32:44

by Josef Bacik

[permalink] [raw]
Subject: Re: [PATCH] nbd: prevent memory leak

On Mon, Sep 23, 2019 at 03:09:58PM -0500, Navid Emamdoost wrote:
> In nbd_add_socket when krealloc succeeds, if nsock's allocation fail the
> reallocted memory is leak. The correct behaviour should be assigning the
> reallocted memory to config->socks right after success.
>
> Signed-off-by: Navid Emamdoost <[email protected]>
> ---

Reviewed-by: Josef Bacik <[email protected]>

Thanks,

Josef

2019-11-21 18:40:04

by Jens Axboe

[permalink] [raw]
Subject: Re: [PATCH] nbd: prevent memory leak

On 11/21/19 11:30 AM, Josef Bacik wrote:
> On Mon, Sep 23, 2019 at 03:09:58PM -0500, Navid Emamdoost wrote:
>> In nbd_add_socket when krealloc succeeds, if nsock's allocation fail the
>> reallocted memory is leak. The correct behaviour should be assigning the
>> reallocted memory to config->socks right after success.
>>
>> Signed-off-by: Navid Emamdoost <[email protected]>
>> ---
>
> Reviewed-by: Josef Bacik <[email protected]>

Now applied, thanks.

--
Jens Axboe