All of lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH] nbd: fix possible memory leak in nbd_add_socket()
@ 2019-01-22 14:50 Yafang Shao
  0 siblings, 0 replies; only message in thread
From: Yafang Shao @ 2019-01-22 14:50 UTC (permalink / raw)
  To: josef, axboe; +Cc: linux-block, nbd, shaoyafang, Yafang Shao

If kzalloc() fails, the memory allocated for the new nbd_sock by krealloc()
will not be used until the next nbd_add_socket() call.
That's a memory leak.
We can fix it by adjusting the order.

Signed-off-by: Yafang Shao <laoar.shao@gmail.com>
---
 drivers/block/nbd.c | 12 +++++++-----
 1 file changed, 7 insertions(+), 5 deletions(-)

diff --git a/drivers/block/nbd.c b/drivers/block/nbd.c
index 08696f5..a197109 100644
--- a/drivers/block/nbd.c
+++ b/drivers/block/nbd.c
@@ -944,14 +944,16 @@ static int nbd_add_socket(struct nbd_device *nbd, unsigned long arg,
 		return -EBUSY;
 	}
 
-	socks = krealloc(config->socks, (config->num_connections + 1) *
-			 sizeof(struct nbd_sock *), GFP_KERNEL);
-	if (!socks) {
+	nsock = kzalloc(sizeof(struct nbd_sock), GFP_KERNEL);
+	if (!nsock) {
 		sockfd_put(sock);
 		return -ENOMEM;
 	}
-	nsock = kzalloc(sizeof(struct nbd_sock), GFP_KERNEL);
-	if (!nsock) {
+
+	socks = krealloc(config->socks, (config->num_connections + 1) *
+			 sizeof(struct nbd_sock *), GFP_KERNEL);
+	if (!socks) {
+		kfree(nsock);
 		sockfd_put(sock);
 		return -ENOMEM;
 	}
-- 
1.8.3.1


^ permalink raw reply related	[flat|nested] only message in thread

only message in thread, other threads:[~2019-01-22 14:51 UTC | newest]

Thread overview: (only message) (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2019-01-22 14:50 [PATCH] nbd: fix possible memory leak in nbd_add_socket() Yafang Shao

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.