Skip to contentSkip to conversation

thunderbolt: Fix to check return value of ida_simple_get

PATCH Superseded Aditya Pakki 3 participants 5 messages Mar 20, 2019 – Mar 22, 2019 Thunderbolt driver

v2 of 3 Superseded Latest is v3 →

lore
Share & tools
lore
#0 lore raw

PATCH v2 1/1 thunderbolt: Fix to check return value of ida_simple_get

In enumerate_services, ida_simple_get on failure can return an error and leaks memory during device_register failure. The patch ensures that the dev_set_name is set on non failure cases, and releases memory in case of failure.

---
v1: Missed cleanup of svc in case of allocation failure and device_register failure.

---
 drivers/thunderbolt/xdomain.c | 9 ++++++++-
 1 file changed, 8 insertions(+), 1 deletion(-)
drivers/thunderbolt/xdomain.c +8 -1
index e27dd8beb94b..eb08275185bf 100644
@@ -740,6 +740,7 @@ static void enumerate_services(struct tb_xdomain *xd)
 	struct tb_service *svc;
 	struct tb_property *p;
 	struct device *dev;
+	int id;
 
 	/*
 	 * First remove all services that are not available anymore in
@@ -768,7 +769,12 @@ static void enumerate_services(struct tb_xdomain *xd)
 			break;
 		}
 
-		svc->id = ida_simple_get(&xd->service_ids, 0, 0, GFP_KERNEL);
+		id = ida_simple_get(&xd->service_ids, 0, 0, GFP_KERNEL);
+		if (id < 0) {
+			kfree(svc);
+			break;
+		}
+		svc->id = id;
 		svc->dev.bus = &tb_bus_type;
 		svc->dev.type = &tb_service_type;
 		svc->dev.parent = &xd->dev;
@@ -776,6 +782,7 @@ static void enumerate_services(struct tb_xdomain *xd)
 
 		if (device_register(&svc->dev)) {
 			put_device(&svc->dev);
+			kfree(svc);
 			break;
 		}
 	}
Signature · 1 line
2.17.1
#1 lore raw
Quoted 41 lines · Aditya Pakki

In enumerate_services, ida_simple_get on failure can return an error and leaks memory during device_register failure. The patch ensures that the dev_set_name is set on non failure cases, and releases memory in case of failure.

---
v1: Missed cleanup of svc in case of allocation failure and device_register failure.

---
 drivers/thunderbolt/xdomain.c | 9 ++++++++-
 1 file changed, 8 insertions(+), 1 deletion(-)
drivers/thunderbolt/xdomain.c +7 -1
index e27dd8beb94b..eb08275185bf 100644
@@ -740,6 +740,7 @@ static void enumerate_services(struct tb_xdomain *xd)
 	struct tb_service *svc;
 	struct tb_property *p;
 	struct device *dev;
+	int id;
 
 	/*
 	 * First remove all services that are not available anymore in
@@ -768,7 +769,12 @@ static void enumerate_services(struct tb_xdomain *xd)
 			break;
 		}
 
-		svc->id = ida_simple_get(&xd->service_ids, 0, 0, GFP_KERNEL);
+		id = ida_simple_get(&xd->service_ids, 0, 0, GFP_KERNEL);
+		if (id < 0) {
+			kfree(svc);
+			break;
+		}
+		svc->id = id;
 		svc->dev.bus = &tb_bus_type;
 		svc->dev.type = &tb_service_type;
 		svc->dev.parent = &xd->dev;

drivers/thunderbolt/xdomain.c static void enumerate_services(struct tb_xdomain *xd) · line 785

Quoted 4 lines
drivers/thunderbolt/xdomain.c +1 -0
@@ -776,6 +782,7 @@ static void enumerate_services(struct tb_xdomain *xd)
 
 		if (device_register(&svc->dev)) {
 			put_device(&svc->dev);
+			kfree(svc);

You can't do this after device_register() is called. The put_device() above is sufficient.

#2 lore raw
Quoted 43 lines · Mika Westerberg
Quoted 42 lines · Aditya Pakki

In enumerate_services, ida_simple_get on failure can return an error and leaks memory during device_register failure. The patch ensures that the dev_set_name is set on non failure cases, and releases memory in case of failure.

---
v1: Missed cleanup of svc in case of allocation failure and device_register failure.

---
  drivers/thunderbolt/xdomain.c | 9 ++++++++-
  1 file changed, 8 insertions(+), 1 deletion(-)
drivers/thunderbolt/xdomain.c +7 -1
index e27dd8beb94b..eb08275185bf 100644
@@ -740,6 +740,7 @@ static void enumerate_services(struct tb_xdomain *xd)
  	struct tb_service *svc;
  	struct tb_property *p;
  	struct device *dev;
+	int id;
 
  	/*
  	 * First remove all services that are not available anymore in
@@ -768,7 +769,12 @@ static void enumerate_services(struct tb_xdomain *xd)
  			break;
  		}
 
-		svc->id = ida_simple_get(&xd->service_ids, 0, 0, GFP_KERNEL);
+		id = ida_simple_get(&xd->service_ids, 0, 0, GFP_KERNEL);
+		if (id < 0) {
+			kfree(svc);
+			break;
+		}
+		svc->id = id;
  		svc->dev.bus = &tb_bus_type;
  		svc->dev.type = &tb_service_type;
  		svc->dev.parent = &xd->dev;
@@ -776,6 +782,7 @@ static void enumerate_services(struct tb_xdomain *xd)
 
Quoted 5 lines

drivers/thunderbolt/xdomain.c static void enumerate_services(struct tb_xdomain *xd) · line 785

Quoted 3 lines
drivers/thunderbolt/xdomain.c +1 -0
  		if (device_register(&svc->dev)) {
  			put_device(&svc->dev);
+			kfree(svc);

You can't do this after device_register() is called. The put_device() above is sufficient.

If  device_register fails, how would svc gets freed? we need  to kfree svc here as well.

Thanks,
Mukesh

#3 lore raw
Quoted 49 lines · Mukesh Ojha
Quoted 37 lines · Mika Westerberg
Quoted 36 lines · Aditya Pakki

In enumerate_services, ida_simple_get on failure can return an error and leaks memory during device_register failure. The patch ensures that the dev_set_name is set on non failure cases, and releases memory in case of failure.

---
v1: Missed cleanup of svc in case of allocation failure and device_register failure.

---
  drivers/thunderbolt/xdomain.c | 9 ++++++++-
  1 file changed, 8 insertions(+), 1 deletion(-)
drivers/thunderbolt/xdomain.c +7 -1
index e27dd8beb94b..eb08275185bf 100644
@@ -740,6 +740,7 @@ static void enumerate_services(struct tb_xdomain *xd)
  	struct tb_service *svc;
  	struct tb_property *p;
  	struct device *dev;
+	int id;
  	/*
  	 * First remove all services that are not available anymore in
@@ -768,7 +769,12 @@ static void enumerate_services(struct tb_xdomain *xd)
  			break;
  		}
-		svc->id = ida_simple_get(&xd->service_ids, 0, 0, GFP_KERNEL);
+		id = ida_simple_get(&xd->service_ids, 0, 0, GFP_KERNEL);
+		if (id < 0) {
+			kfree(svc);
+			break;
+		}
+		svc->id = id;
  		svc->dev.bus = &tb_bus_type;
Quoted 8 lines

drivers/thunderbolt/xdomain.c static void enumerate_services(struct tb_xdomain *xd) · line 784

Quoted 6 lines
drivers/thunderbolt/xdomain.c +1 -0
  		svc->dev.type = &tb_service_type;
  		svc->dev.parent = &xd->dev;
@@ -776,6 +782,7 @@ static void enumerate_services(struct tb_xdomain *xd)
  		if (device_register(&svc->dev)) {
  			put_device(&svc->dev);
+			kfree(svc);

You can't do this after device_register() is called. The put_device() above is sufficient.

Quoted 2 lines

If  device_register fails, how would svc gets freed? we need  to kfree svc here as well.

Please read the comment on top of device_register(). It should explain.

So no kfree here.

#4 lore raw
Quoted 48 lines · Mika Westerberg
Quoted 47 lines · Mukesh Ojha
Quoted 45 lines · Mika Westerberg
Quoted 39 lines · Aditya Pakki

In enumerate_services, ida_simple_get on failure can return an error and leaks memory during device_register failure. The patch ensures that the dev_set_name is set on non failure cases, and releases memory in case of failure.

---
v1: Missed cleanup of svc in case of allocation failure and device_register failure.

---
   drivers/thunderbolt/xdomain.c | 9 ++++++++-
   1 file changed, 8 insertions(+), 1 deletion(-)
drivers/thunderbolt/xdomain.c +7 -1
index e27dd8beb94b..eb08275185bf 100644
@@ -740,6 +740,7 @@ static void enumerate_services(struct tb_xdomain *xd)
   	struct tb_service *svc;
   	struct tb_property *p;
   	struct device *dev;
+	int id;
   	/*
   	 * First remove all services that are not available anymore in
@@ -768,7 +769,12 @@ static void enumerate_services(struct tb_xdomain *xd)
   			break;
   		}
-		svc->id = ida_simple_get(&xd->service_ids, 0, 0, GFP_KERNEL);
+		id = ida_simple_get(&xd->service_ids, 0, 0, GFP_KERNEL);
+		if (id < 0) {
+			kfree(svc);
+			break;
+		}
+		svc->id = id;
   		svc->dev.bus = &tb_bus_type;
   		svc->dev.type = &tb_service_type;
   		svc->dev.parent = &xd->dev;

drivers/thunderbolt/xdomain.c static void enumerate_services(struct tb_xdomain *xd) · line 784

Quoted 3 lines
drivers/thunderbolt/xdomain.c +1 -0
@@ -776,6 +782,7 @@ static void enumerate_services(struct tb_xdomain *xd)
   		if (device_register(&svc->dev)) {
   			put_device(&svc->dev);
+			kfree(svc);

You can't do this after device_register() is called. The put_device() above is sufficient.

Quoted 5 lines
Quoted 2 lines

If  device_register fails, how would svc gets freed? we need  to kfree svc here as well.

Please read the comment on top of device_register(). It should explain.

So no kfree here.

Thanks for pointer Mika.

Overlooked the fact that dev is a data member of svc not a pointer, also noticed there are many places in tree where , it is not followed.

Thanks,
Mukesh