mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
* [PATCH] IB/core: fix null pointer dereference and memory leak in error handling
@ 2016-06-01 18:06 Colin King
  2016-06-06 23:21 ` Doug Ledford
  0 siblings, 1 reply; 2+ messages in thread
From: Colin King @ 2016-06-01 18:06 UTC (permalink / raw)
  To: Doug Ledford, Sean Hefty, Hal Rosenstock, Ira Weiny,
	Christoph Lameter, Matan Barak, Jason Gunthorpe, linux-rdma
  Cc: linux-kernel

From: Colin Ian King <colin.king@canonical.com>

The current error handling in setup_hw_stats has a couple of issues.
It is possible to generate a null pointer deference on the
kfree of hsag->attrs[i] because two of the early error exit paths
jump to the kfree when hsags NULL and not allocated. Fix this by
moving the kfree on stats and jumping to that, avoiding the hsag
freeing.

Secondly, there is a memory leak of stats if the hsag allocation
fails; instead of returning, jump to the kfree on stats.

Signed-off-by: Colin Ian King <colin.king@canonical.com>
---
 drivers/infiniband/core/sysfs.c | 7 ++++---
 1 file changed, 4 insertions(+), 3 deletions(-)

diff --git a/drivers/infiniband/core/sysfs.c b/drivers/infiniband/core/sysfs.c
index 5e573bb..ed04a7b 100644
--- a/drivers/infiniband/core/sysfs.c
+++ b/drivers/infiniband/core/sysfs.c
@@ -899,14 +899,14 @@ static void setup_hw_stats(struct ib_device *device, struct ib_port *port,
 		return;
 
 	if (!stats->names || stats->num_counters <= 0)
-		goto err;
+		goto err_free_stats;
 
 	hsag = kzalloc(sizeof(*hsag) +
 		       // 1 extra for the lifespan config entry
 		       sizeof(void *) * (stats->num_counters + 1),
 		       GFP_KERNEL);
 	if (!hsag)
-		return;
+		goto err_free_stats;
 
 	ret = device->get_hw_stats(device, stats, port_num,
 				   stats->num_counters);
@@ -946,10 +946,11 @@ static void setup_hw_stats(struct ib_device *device, struct ib_port *port,
 	return;
 
 err:
-	kfree(stats);
 	for (; i >= 0; i--)
 		kfree(hsag->attrs[i]);
 	kfree(hsag);
+err_free_stats:
+	kfree(stats);
 	return;
 }
 
-- 
2.8.1

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

* Re: [PATCH] IB/core: fix null pointer dereference and memory leak in error handling
  2016-06-01 18:06 [PATCH] IB/core: fix null pointer dereference and memory leak in error handling Colin King
@ 2016-06-06 23:21 ` Doug Ledford
  0 siblings, 0 replies; 2+ messages in thread
From: Doug Ledford @ 2016-06-06 23:21 UTC (permalink / raw)
  To: Colin King, Sean Hefty, Hal Rosenstock, Ira Weiny,
	Christoph Lameter, Matan Barak, Jason Gunthorpe, linux-rdma
  Cc: linux-kernel


[-- Attachment #1.1: Type: text/plain, Size: 666 bytes --]

On 6/1/2016 2:06 PM, Colin King wrote:
> From: Colin Ian King <colin.king@canonical.com>
> 
> The current error handling in setup_hw_stats has a couple of issues.
> It is possible to generate a null pointer deference on the
> kfree of hsag->attrs[i] because two of the early error exit paths
> jump to the kfree when hsags NULL and not allocated. Fix this by
> moving the kfree on stats and jumping to that, avoiding the hsag
> freeing.
> 
> Secondly, there is a memory leak of stats if the hsag allocation
> fails; instead of returning, jump to the kfree on stats.
> 
> Signed-off-by: Colin Ian King <colin.king@canonical.com>

Thanks, applied.



[-- Attachment #2: OpenPGP digital signature --]
[-- Type: application/pgp-signature, Size: 884 bytes --]

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

end of thread, other threads:[~2016-06-06 23:21 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2016-06-01 18:06 [PATCH] IB/core: fix null pointer dereference and memory leak in error handling Colin King
2016-06-06 23:21 ` Doug Ledford

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®