[PATCH] nbd: fix possible memory leak in nbd_add_socket()

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



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@xxxxxxxxx>
---
 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




[Index of Archives]     [Linux RAID]     [Linux SCSI]     [Linux ATA RAID]     [IDE]     [Linux Wireless]     [Linux Kernel]     [ATH6KL]     [Linux Bluetooth]     [Linux Netdev]     [Kernel Newbies]     [Security]     [Git]     [Netfilter]     [Bugtraq]     [Yosemite News]     [MIPS Linux]     [ARM Linux]     [Linux Security]     [Device Mapper]

  Powered by Linux