Skip to content

Commit

Permalink
nbd: prevent memory leak
Browse files Browse the repository at this point in the history
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.

Reviewed-by: Josef Bacik <josef@toxicpanda.com>
Signed-off-by: Navid Emamdoost <navid.emamdoost@gmail.com>
Signed-off-by: Jens Axboe <axboe@kernel.dk>
  • Loading branch information
Navid Emamdoost authored and Jens Axboe committed Nov 21, 2019
1 parent 866ca95 commit 03bf73c
Showing 1 changed file with 3 additions and 2 deletions.
5 changes: 3 additions & 2 deletions drivers/block/nbd.c
Original file line number Diff line number Diff line change
Expand Up @@ -1004,14 +1004,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);
Expand Down

0 comments on commit 03bf73c

Please sign in to comment.