Skip to content

Commit

Permalink
nbd: Fix memory leak in nbd_add_socket
Browse files Browse the repository at this point in the history
When adding first socket to nbd, if nsock's allocation failed, the data
structure member "config->socks" was reallocated, but the data structure
member "config->num_connections" was not updated. A memory leak will occur
then because the function "nbd_config_put" will free "config->socks" only
when "config->num_connections" is not zero.

Fixes: 03bf73c ("nbd: prevent memory leak")
Reported-by: syzbot+934037347002901b8d2a@syzkaller.appspotmail.com
Signed-off-by: Zheng Bin <zhengbin13@huawei.com>
Reviewed-by: Eric Biggers <ebiggers@google.com>
Signed-off-by: Jens Axboe <axboe@kernel.dk>
  • Loading branch information
Zheng Bin authored and Jens Axboe committed Jul 8, 2020
1 parent 05a4fed commit 579dd91
Showing 1 changed file with 15 additions and 10 deletions.
25 changes: 15 additions & 10 deletions drivers/block/nbd.c
Original file line number Diff line number Diff line change
Expand Up @@ -1033,25 +1033,26 @@ static int nbd_add_socket(struct nbd_device *nbd, unsigned long arg,
test_bit(NBD_RT_BOUND, &config->runtime_flags))) {
dev_err(disk_to_dev(nbd->disk),
"Device being setup by another task");
sockfd_put(sock);
return -EBUSY;
err = -EBUSY;
goto put_socket;
}

nsock = kzalloc(sizeof(*nsock), GFP_KERNEL);
if (!nsock) {
err = -ENOMEM;
goto put_socket;
}

socks = krealloc(config->socks, (config->num_connections + 1) *
sizeof(struct nbd_sock *), GFP_KERNEL);
if (!socks) {
sockfd_put(sock);
return -ENOMEM;
kfree(nsock);
err = -ENOMEM;
goto put_socket;
}

config->socks = socks;

nsock = kzalloc(sizeof(struct nbd_sock), GFP_KERNEL);
if (!nsock) {
sockfd_put(sock);
return -ENOMEM;
}

nsock->fallback_index = -1;
nsock->dead = false;
mutex_init(&nsock->tx_lock);
Expand All @@ -1063,6 +1064,10 @@ static int nbd_add_socket(struct nbd_device *nbd, unsigned long arg,
atomic_inc(&config->live_connections);

return 0;

put_socket:
sockfd_put(sock);
return err;
}

static int nbd_reconnect_socket(struct nbd_device *nbd, unsigned long arg)
Expand Down

0 comments on commit 579dd91

Please sign in to comment.