[Date Prev][Date Next] [Thread Prev][Thread Next] [Date Index] [Thread Index]

Re: [PATCH] nbd: Fix memory leak in nbd_add_socket



On 6/12/20 1:57 AM, Zheng Bin wrote:
> nbd_add_socket
>    socks = krealloc(num_connections+1) -->if num_connections is 0, alloc 1
>    nsock = kzalloc                     -->If fail, will return
> 
> nbd_config_put
>    if (config->num_connections)        -->0, not free
>      kfree(config->socks)
> 
> Thus memleak happens, this patch fixes that.
> 
> Signed-off-by: Zheng Bin<zhengbin13@huawei.com>

Not an nbd expert but wouldn't it be easier use following which matches 
the + 1 in the nbd_add_socket() :-

diff --git a/drivers/block/nbd.c b/drivers/block/nbd.c
index 01794cd2b6ca..e67c790039c9 100644
--- a/drivers/block/nbd.c
+++ b/drivers/block/nbd.c
@@ -1209,9 +1209,9 @@ static void nbd_config_put(struct nbd_device *nbd)
                         device_remove_file(disk_to_dev(nbd->disk), 
&pid_attr);
                 nbd->task_recv = NULL;
                 nbd_clear_sock(nbd);
-               if (config->num_connections) {
+               if (config->num_connections + 1) {
                         int i;
-                       for (i = 0; i < config->num_connections; i++) {
+                       for (i = 0; i < (config->num_connections + 1); 
i++) {
                                 sockfd_put(config->socks[i]->sock);
                                 kfree(config->socks[i]);
                         }


Reply to: