Skip to contentSkip to conversation

nbd: prevent memory leak

PATCH In review Navid Emamdoost 3 participants 4 messages Sep 23, 2019 – Nov 21, 2019

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 nbd: prevent memory leak

In nbd_add_socket when krealloc succeeds, if nsock's allocation fail the reallocted memory is leak. The correct behaviour should be assigning the reallocted memory to config->socks right after success.

---
 drivers/block/nbd.c | 5 +++--
 1 file changed, 3 insertions(+), 2 deletions(-)
drivers/block/nbd.c +3 -2
index a8e3815295fe..8ae3bd2e7b30 100644
@@ -987,14 +987,15 @@ static int nbd_add_socket(struct nbd_device *nbd, unsigned long arg,
 		sockfd_put(sock);
 		return -ENOMEM;
 	}
+
+	config->socks = socks;
+
 	nsock = kzalloc(sizeof(struct nbd_sock), GFP_KERNEL);
 	if (!nsock) {
 		sockfd_put(sock);
 		return -ENOMEM;
 	}
 
-	config->socks = socks;
-
 	nsock->fallback_index = -1;
 	nsock->dead = false;
 	mutex_init(&nsock->tx_lock);
Signature · 1 line
2.17.1
#1 lore raw
Quoted 7 lines · Navid Emamdoost

In nbd_add_socket when krealloc succeeds, if nsock's allocation fail the reallocted memory is leak. The correct behaviour should be assigning the reallocted memory to config->socks right after success.

---

Would you please review this patch?

Thanks,

Quoted 28 lines
 drivers/block/nbd.c | 5 +++--
 1 file changed, 3 insertions(+), 2 deletions(-)
drivers/block/nbd.c +3 -3
index a8e3815295fe..8ae3bd2e7b30 100644
@@ -987,14 +987,15 @@ static int nbd_add_socket(struct nbd_device *nbd, unsigned long arg,
                sockfd_put(sock);
                return -ENOMEM;
        }
+
+       config->socks = socks;
+
        nsock = kzalloc(sizeof(struct nbd_sock), GFP_KERNEL);
        if (!nsock) {
                sockfd_put(sock);
                return -ENOMEM;
        }
 
-       config->socks = socks;
-
        nsock->fallback_index = -1;
        nsock->dead = false;
        mutex_init(&nsock->tx_lock);
--

2.17.1

Signature · 1 line
Navid.
maintainer
#3 lore raw
Quoted 9 lines · Josef Bacik
Quoted 6 lines · Navid Emamdoost

In nbd_add_socket when krealloc succeeds, if nsock's allocation fail the reallocted memory is leak. The correct behaviour should be assigning the reallocted memory to config->socks right after success.

---

Now applied, thanks.

Signature · 1 line
Jens Axboe