Skip to contentSkip to conversation

nbd: avoid losing pointer to reallocated config->socks in nbd_add_socket

PATCH RESEND In review Eugene Syromiatnikov 2 participants 2 messages Sep 20, 2019 – Sep 22, 2019

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

PATCH RESEND 1/1 nbd: avoid losing pointer to reallocated config->socks in nbd_add_socket

In the (very unlikely) case of config->socks reallocation success and nsock allocation failure config->nsock will not get updated with the new pointer to socks array. Fix it by updating config->socks right after reallocation successfulness check.

Fixes 9561a7ade0c2 ("nbd: add multi-connection support")
---
 drivers/block/nbd.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
drivers/block/nbd.c +2 -2
index a8e3815..a04c686 100644
@@ -987,14 +987,14 @@ 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.1.4
#1 lore raw
Quoted 20 lines · Eugene Syromiatnikov

In the (very unlikely) case of config->socks reallocation success and nsock allocation failure config->nsock will not get updated with the new pointer to socks array. Fix it by updating config->socks right after reallocation successfulness check.

Fixes 9561a7ade0c2 ("nbd: add multi-connection support")
---
  drivers/block/nbd.c | 4 ++--
  1 file changed, 2 insertions(+), 2 deletions(-)
drivers/block/nbd.c +0 -0
index a8e3815..a04c686 100644
@@ -987,14 +987,14 @@ static int nbd_add_socket(struct nbd_device *nbd, unsigned long arg,
  		sockfd_put(sock);
  		return -ENOMEM;
  	}

drivers/block/nbd.c static int nbd_add_socket(struct nbd_device *nbd, unsigned long arg, · line 997

Quoted 10 lines
drivers/block/nbd.c +2 -2
+	config->socks = socks;
+
  	nsock = kzalloc(sizeof(struct nbd_sock), GFP_KERNEL);
  	if (!nsock) {
  		sockfd_put(sock);
  		return -ENOMEM;
  	}
 
-	config->socks = socks;
-

This makes sense.

If the socks allocating successes, then the old config->socks will be freed by krealloc() and return the new one, but if the nsock allocating fails, the config->socks will hold the released memory, which may cause the kernel crash.

Thanks

BRs

Quoted 3 lines
drivers/block/nbd.c +0 -0
  	nsock->fallback_index = -1;
  	nsock->dead = false;
  	mutex_init(&nsock->tx_lock);