mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
* Re: [PATCH v2] nbd: Fix memory leak in nbd_add_socket
@ 2020-06-20 12:05 Markus Elfring
  2020-06-22  3:25 ` Zhengbin (OSKernel)
  0 siblings, 1 reply; 4+ messages in thread
From: Markus Elfring @ 2020-06-20 12:05 UTC (permalink / raw)
  To: Zheng Bin, linux-block
  Cc: nbd, Navid Emamdoost, Navid Emamdoost, Kangjie Lu,
	Stephen McCamant, Qiushi Wu, kernel-janitors, linux-kernel,
	Jens Axboe, Josef Bacik, Tuomas Tynkkynen, Yi Zhang

> If we add first socket to nbd, config->socks is malloced but
> num_connections does not update(nsock's allocation fail), the memory
> is leaked. Cause in later nbd_config_put(), will only free config->socks
> when num_connections is not 0.
>
> Let nsock's allocation first to avoid this.

I suggest to improve this change description.
Can an other wording variant be nicer?


…
> +++ b/drivers/block/nbd.c
> @@ -1037,21 +1037,22 @@  static int nbd_add_socket(struct nbd_device *nbd, unsigned long arg,
>  		return -EBUSY;
>  	}
>
> +	nsock = kzalloc(sizeof(struct nbd_sock), GFP_KERNEL);

Please use the following code variant.
https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/Documentation/process/coding-style.rst?id=4333a9b0b67bb4e8bcd91bdd80da80b0ec151162#n854

+	nsock = kzalloc(sizeof(*nsock), GFP_KERNEL);


…
>  	if (!socks) {
>  		sockfd_put(sock);
> +		kfree(nsock);
>  		return -ENOMEM;
>  	}

Please take another software design possibility into account.

 	if (!socks) {
-		sockfd_put(sock);
-		return -ENOMEM;
+		kfree(nsock);
+		goto put_socket;
 	}


Regards,
Markus

^ permalink raw reply	[flat|nested] 4+ messages in thread
* [PATCH v2] nbd: Fix memory leak in nbd_add_socket
@ 2020-06-20  8:48 Zheng Bin
  0 siblings, 0 replies; 4+ messages in thread
From: Zheng Bin @ 2020-06-20  8:48 UTC (permalink / raw)
  To: josef, axboe, navid.emamdoost, linux-block, nbd, linux-kernel
  Cc: yi.zhang, zhengbin13

If we add first socket to nbd, config->socks is malloced but
num_connections does not update(nsock's allocation fail), the memory
is leaked. Cause in later nbd_config_put(), will only free config->socks
when num_connections is not 0.

Let nsock's allocation first to avoid this.

Fixes: 03bf73c315ed ("nbd: prevent memory leak")
Signed-off-by: Zheng Bin <zhengbin13@huawei.com>
---

v1->v2: modify comments

 drivers/block/nbd.c | 13 +++++++------
 1 file changed, 7 insertions(+), 6 deletions(-)

diff --git a/drivers/block/nbd.c b/drivers/block/nbd.c
index 43cff01a5a67..3e7709317b17 100644
--- a/drivers/block/nbd.c
+++ b/drivers/block/nbd.c
@@ -1037,21 +1037,22 @@ static int nbd_add_socket(struct nbd_device *nbd, unsigned long arg,
 		return -EBUSY;
 	}

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

 	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);
--
2.26.0.106.g9fadedd


^ permalink raw reply	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2020-06-22  7:53 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2020-06-20 12:05 [PATCH v2] nbd: Fix memory leak in nbd_add_socket Markus Elfring
2020-06-22  3:25 ` Zhengbin (OSKernel)
2020-06-22  7:52   ` Markus Elfring
  -- strict thread matches above, loose matches on Subject: below --
2020-06-20  8:48 Zheng Bin

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox

all inboxes | Powered by JetHome®