* [PATCH v4] nbd: Fix memory leak in nbd_add_socket
@ 2020-06-29 1:23 Zheng Bin
2020-07-08 20:32 ` Eric Biggers
2020-07-08 21:42 ` Jens Axboe
0 siblings, 2 replies; 3+ messages in thread
From: Zheng Bin @ 2020-06-29 1:23 UTC (permalink / raw)
To: mchristi, ebiggers, josef, axboe, navid.emamdoost, linux-block,
nbd, linux-kernel
Cc: yi.zhang, zhengbin13
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: 03bf73c315ed ("nbd: prevent memory leak")
Reported-by: syzbot+934037347002901b8d2a@syzkaller.appspotmail.com
Signed-off-by: Zheng Bin <zhengbin13@huawei.com>
---
v1->v2: improve change description
v2->v3: fix some code style issues, improve change description
v3->v4: add "Reported-by" tag
drivers/block/nbd.c | 25 +++++++++++++++----------
1 file changed, 15 insertions(+), 10 deletions(-)
diff --git a/drivers/block/nbd.c b/drivers/block/nbd.c
index 01794cd2b6ca..3ff4054d6834 100644
--- a/drivers/block/nbd.c
+++ b/drivers/block/nbd.c
@@ -1036,25 +1036,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);
@@ -1066,6 +1067,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)
--
2.26.0.106.g9fadedd
^ permalink raw reply related [flat|nested] 3+ messages in thread
* Re: [PATCH v4] nbd: Fix memory leak in nbd_add_socket
2020-06-29 1:23 [PATCH v4] nbd: Fix memory leak in nbd_add_socket Zheng Bin
@ 2020-07-08 20:32 ` Eric Biggers
2020-07-08 21:42 ` Jens Axboe
1 sibling, 0 replies; 3+ messages in thread
From: Eric Biggers @ 2020-07-08 20:32 UTC (permalink / raw)
To: Zheng Bin
Cc: mchristi, josef, axboe, navid.emamdoost, linux-block, nbd,
linux-kernel, yi.zhang
On Mon, Jun 29, 2020 at 09:23:49AM +0800, Zheng Bin wrote:
> 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: 03bf73c315ed ("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>
^ permalink raw reply [flat|nested] 3+ messages in thread
* Re: [PATCH v4] nbd: Fix memory leak in nbd_add_socket
2020-06-29 1:23 [PATCH v4] nbd: Fix memory leak in nbd_add_socket Zheng Bin
2020-07-08 20:32 ` Eric Biggers
@ 2020-07-08 21:42 ` Jens Axboe
1 sibling, 0 replies; 3+ messages in thread
From: Jens Axboe @ 2020-07-08 21:42 UTC (permalink / raw)
To: Zheng Bin, mchristi, ebiggers, josef, navid.emamdoost,
linux-block, nbd, linux-kernel
Cc: yi.zhang
On 6/28/20 7:23 PM, Zheng Bin wrote:
> 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.
Applied, thanks.
--
Jens Axboe
^ permalink raw reply [flat|nested] 3+ messages in thread
end of thread, other threads:[~2020-07-08 21:42 UTC | newest]
Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2020-06-29 1:23 [PATCH v4] nbd: Fix memory leak in nbd_add_socket Zheng Bin
2020-07-08 20:32 ` Eric Biggers
2020-07-08 21:42 ` Jens Axboe
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).