Skip to contentSkip to conversation

usb: misc: ljca: Fix double free in error handling path

PATCH Superseded Yongzhi Liu 2 participants 6 messages Mar 6, 2024 – Mar 11, 2024

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

PATCH 1/1 usb: misc: ljca: Fix double free in error handling path

When auxiliary_device_add() returns error and then calls auxiliary_device_uninit(), callback function ljca_auxdev_release calls kfree(auxdev->dev.platform_data) to free the parameter data of the function ljca_new_client_device. The callers of ljca_new_client_device shouldn't call kfree() again
in the error handling path to free the platform data.

Fix this by cleaning up the redundant kfree().

Fixes acd6199f195d ("usb: Add support for Intel LJCA device")
---
 drivers/usb/misc/usb-ljca.c | 18 +++---------------
 1 file changed, 3 insertions(+), 15 deletions(-)
drivers/usb/misc/usb-ljca.c +3 -15
index 35770e608c64..be702364be08 100644
@@ -590,12 +590,8 @@ static int ljca_enumerate_gpio(struct ljca_adapter *adap)
 		valid_pin[i] = get_unaligned_le32(&desc->bank_desc[i].valid_pins);
 	bitmap_from_arr32(gpio_info->valid_pin_map, valid_pin, gpio_num);
 
-	ret = ljca_new_client_device(adap, LJCA_CLIENT_GPIO, 0, "ljca-gpio",
+	return ljca_new_client_device(adap, LJCA_CLIENT_GPIO, 0, "ljca-gpio",
 				     gpio_info, LJCA_GPIO_ACPI_ADR);
-	if (ret)
-		kfree(gpio_info);
-
-	return ret;
 }
 
 static int ljca_enumerate_i2c(struct ljca_adapter *adap)
@@ -626,13 +622,9 @@ static int ljca_enumerate_i2c(struct ljca_adapter *adap)
 		i2c_info->capacity = desc->info[i].capacity;
 		i2c_info->intr_pin = desc->info[i].intr_pin;
 
-		ret = ljca_new_client_device(adap, LJCA_CLIENT_I2C, i,
+		return ljca_new_client_device(adap, LJCA_CLIENT_I2C, i,
 					     "ljca-i2c", i2c_info,
 					     LJCA_I2C1_ACPI_ADR + i);
-		if (ret) {
-			kfree(i2c_info);
-			return ret;
-		}
 	}
 
 	return 0;
@@ -666,13 +658,9 @@ static int ljca_enumerate_spi(struct ljca_adapter *adap)
 		spi_info->id = desc->info[i].id;
 		spi_info->capacity = desc->info[i].capacity;
 
-		ret = ljca_new_client_device(adap, LJCA_CLIENT_SPI, i,
+		return ljca_new_client_device(adap, LJCA_CLIENT_SPI, i,
 					     "ljca-spi", spi_info,
 					     LJCA_SPI1_ACPI_ADR + i);
-		if (ret) {
-			kfree(spi_info);
-			return ret;
-		}
 	}
 
 	return 0;
Signature · 1 line
2.36.1
#1 lore raw

Hi,

Quoted 8 lines · Yongzhi Liu

When auxiliary_device_add() returns error and then calls auxiliary_device_uninit(), callback function ljca_auxdev_release calls kfree(auxdev->dev.platform_data) to free the parameter data of the function ljca_new_client_device. The callers of ljca_new_client_device shouldn't call kfree() again
in the error handling path to free the platform data.

Fix this by cleaning up the redundant kfree().

If things fail in ljca_new_client_device() before auxiliary_device_init() gets called (or if that call fails) then auxiliary_device_uninit() will NOT be called leading to a memory-leak.

So this patch is no good as is.

To properly fix this you must make ljca_new_client_device() always take ownership of the passed in platform_data and make it also kfree() the passed in platform_data on errors which happen before auxiliary_device_init() succeeds.

Regards,

Hans

Quoted 55 lines
Fixes acd6199f195d ("usb: Add support for Intel LJCA device")
---
 drivers/usb/misc/usb-ljca.c | 18 +++---------------
 1 file changed, 3 insertions(+), 15 deletions(-)
drivers/usb/misc/usb-ljca.c +3 -15
index 35770e608c64..be702364be08 100644
@@ -590,12 +590,8 @@ static int ljca_enumerate_gpio(struct ljca_adapter *adap)
 		valid_pin[i] = get_unaligned_le32(&desc->bank_desc[i].valid_pins);
 	bitmap_from_arr32(gpio_info->valid_pin_map, valid_pin, gpio_num);
 
-	ret = ljca_new_client_device(adap, LJCA_CLIENT_GPIO, 0, "ljca-gpio",
+	return ljca_new_client_device(adap, LJCA_CLIENT_GPIO, 0, "ljca-gpio",
 				     gpio_info, LJCA_GPIO_ACPI_ADR);
-	if (ret)
-		kfree(gpio_info);
-
-	return ret;
 }
 
 static int ljca_enumerate_i2c(struct ljca_adapter *adap)
@@ -626,13 +622,9 @@ static int ljca_enumerate_i2c(struct ljca_adapter *adap)
 		i2c_info->capacity = desc->info[i].capacity;
 		i2c_info->intr_pin = desc->info[i].intr_pin;
 
-		ret = ljca_new_client_device(adap, LJCA_CLIENT_I2C, i,
+		return ljca_new_client_device(adap, LJCA_CLIENT_I2C, i,
 					     "ljca-i2c", i2c_info,
 					     LJCA_I2C1_ACPI_ADR + i);
-		if (ret) {
-			kfree(i2c_info);
-			return ret;
-		}
 	}
 
 	return 0;
@@ -666,13 +658,9 @@ static int ljca_enumerate_spi(struct ljca_adapter *adap)
 		spi_info->id = desc->info[i].id;
 		spi_info->capacity = desc->info[i].capacity;
 
-		ret = ljca_new_client_device(adap, LJCA_CLIENT_SPI, i,
+		return ljca_new_client_device(adap, LJCA_CLIENT_SPI, i,
 					     "ljca-spi", spi_info,
 					     LJCA_SPI1_ACPI_ADR + i);
-		if (ret) {
-			kfree(spi_info);
-			return ret;
-		}
 	}
 
 	return 0;
#2 lore raw

PATCH v2 1/1 usb: misc: ljca: Fix double free in error handling path

When auxiliary_device_add() returns error and then calls auxiliary_device_uninit(), callback function ljca_auxdev_release calls kfree(auxdev->dev.platform_data) to free the parameter data of the function ljca_new_client_device. The callers of ljca_new_client_device shouldn't call kfree() again
in the error handling path to free the platform data.

Fix this by cleaning up the redundant kfree() in all callers and adding kfree() the passed in platform_data on errors which happen before auxiliary_device_init() succeeds .

Fixes acd6199f195d ("usb: Add support for Intel LJCA device")
---
 drivers/usb/misc/usb-ljca.c | 26 +++++++++-----------------
 1 file changed, 9 insertions(+), 17 deletions(-)
drivers/usb/misc/usb-ljca.c +9 -17
index 35770e608c64..bd9ccbea6e72 100644
@@ -518,8 +518,10 @@ static int ljca_new_client_device(struct ljca_adapter *adap, u8 type, u8 id,
 	int ret;
 
 	client = kzalloc(sizeof *client, GFP_KERNEL);
-	if (!client)
+	if (!client) {
+		kfree(data);
 		return -ENOMEM;
+	}
 
 	client->type = type;
 	client->id = id;
@@ -535,8 +537,10 @@ static int ljca_new_client_device(struct ljca_adapter *adap, u8 type, u8 id,
 	auxdev->dev.release = ljca_auxdev_release;
 
 	ret = auxiliary_device_init(auxdev);
-	if (ret)
+	if (ret) {
+		kfree(data);
 		goto err_free;
+	}
 
 	ljca_auxdev_acpi_bind(adap, auxdev, adr, id);
 
@@ -590,12 +594,8 @@ static int ljca_enumerate_gpio(struct ljca_adapter *adap)
 		valid_pin[i] = get_unaligned_le32(&desc->bank_desc[i].valid_pins);
 	bitmap_from_arr32(gpio_info->valid_pin_map, valid_pin, gpio_num);
 
-	ret = ljca_new_client_device(adap, LJCA_CLIENT_GPIO, 0, "ljca-gpio",
+	return ljca_new_client_device(adap, LJCA_CLIENT_GPIO, 0, "ljca-gpio",
 				     gpio_info, LJCA_GPIO_ACPI_ADR);
-	if (ret)
-		kfree(gpio_info);
-
-	return ret;
 }
 
 static int ljca_enumerate_i2c(struct ljca_adapter *adap)
@@ -626,13 +626,9 @@ static int ljca_enumerate_i2c(struct ljca_adapter *adap)
 		i2c_info->capacity = desc->info[i].capacity;
 		i2c_info->intr_pin = desc->info[i].intr_pin;
 
-		ret = ljca_new_client_device(adap, LJCA_CLIENT_I2C, i,
+		return ljca_new_client_device(adap, LJCA_CLIENT_I2C, i,
 					     "ljca-i2c", i2c_info,
 					     LJCA_I2C1_ACPI_ADR + i);
-		if (ret) {
-			kfree(i2c_info);
-			return ret;
-		}
 	}
 
 	return 0;
@@ -666,13 +662,9 @@ static int ljca_enumerate_spi(struct ljca_adapter *adap)
 		spi_info->id = desc->info[i].id;
 		spi_info->capacity = desc->info[i].capacity;
 
-		ret = ljca_new_client_device(adap, LJCA_CLIENT_SPI, i,
+		return ljca_new_client_device(adap, LJCA_CLIENT_SPI, i,
 					     "ljca-spi", spi_info,
 					     LJCA_SPI1_ACPI_ADR + i);
-		if (ret) {
-			kfree(spi_info);
-			return ret;
-		}
 	}
 
 	return 0;
Signature · 1 line
2.36.1
#3 lore raw

Hi Yongzhi Liu,

Quoted 61 lines · Yongzhi Liu

When auxiliary_device_add() returns error and then calls auxiliary_device_uninit(), callback function ljca_auxdev_release calls kfree(auxdev->dev.platform_data) to free the parameter data of the function ljca_new_client_device. The callers of ljca_new_client_device shouldn't call kfree() again
in the error handling path to free the platform data.

Fix this by cleaning up the redundant kfree() in all callers and adding kfree() the passed in platform_data on errors which happen before auxiliary_device_init() succeeds .

Fixes acd6199f195d ("usb: Add support for Intel LJCA device")
---
 drivers/usb/misc/usb-ljca.c | 26 +++++++++-----------------
 1 file changed, 9 insertions(+), 17 deletions(-)
drivers/usb/misc/usb-ljca.c +7 -7
index 35770e608c64..bd9ccbea6e72 100644
@@ -518,8 +518,10 @@ static int ljca_new_client_device(struct ljca_adapter *adap, u8 type, u8 id,
 	int ret;
 
 	client = kzalloc(sizeof *client, GFP_KERNEL);
-	if (!client)
+	if (!client) {
+		kfree(data);
 		return -ENOMEM;
+	}
 
 	client->type = type;
 	client->id = id;
@@ -535,8 +537,10 @@ static int ljca_new_client_device(struct ljca_adapter *adap, u8 type, u8 id,
 	auxdev->dev.release = ljca_auxdev_release;
 
 	ret = auxiliary_device_init(auxdev);
-	if (ret)
+	if (ret) {
+		kfree(data);
 		goto err_free;
+	}
 
 	ljca_auxdev_acpi_bind(adap, auxdev, adr, id);
 
@@ -590,12 +594,8 @@ static int ljca_enumerate_gpio(struct ljca_adapter *adap)
 		valid_pin[i] = get_unaligned_le32(&desc->bank_desc[i].valid_pins);
 	bitmap_from_arr32(gpio_info->valid_pin_map, valid_pin, gpio_num);
 
-	ret = ljca_new_client_device(adap, LJCA_CLIENT_GPIO, 0, "ljca-gpio",
+	return ljca_new_client_device(adap, LJCA_CLIENT_GPIO, 0, "ljca-gpio",
 				     gpio_info, LJCA_GPIO_ACPI_ADR);
-	if (ret)
-		kfree(gpio_info);
-
-	return ret;
 }
 
 static int ljca_enumerate_i2c(struct ljca_adapter *adap)
@@ -626,13 +626,9 @@ static int ljca_enumerate_i2c(struct ljca_adapter *adap)
 		i2c_info->capacity = desc->info[i].capacity;

drivers/usb/misc/usb-ljca.c static int ljca_enumerate_i2c(struct ljca_adapter *adap) · line 635

Quoted 10 lines
drivers/usb/misc/usb-ljca.c +1 -5
 		i2c_info->intr_pin = desc->info[i].intr_pin;
 
-		ret = ljca_new_client_device(adap, LJCA_CLIENT_I2C, i,
+		return ljca_new_client_device(adap, LJCA_CLIENT_I2C, i,
 					     "ljca-i2c", i2c_info,
 					     LJCA_I2C1_ACPI_ADR + i);
-		if (ret) {
-			kfree(i2c_info);
-			return ret;
-		}

This is in a for loop, by using:

		return ljca_new_client_device(...);

you are now only creating the first ljca-i2c controller, while there may be more instead just drop the kfree() from the if (ret) {} block:

		ret = ljca_new_client_device(...);
		if (ret)
			return ret;

Sorry for not noticing this with the v1 posting.

Quoted 6 lines
drivers/usb/misc/usb-ljca.c +0 -0
 	}
 
 	return 0;
@@ -666,13 +662,9 @@ static int ljca_enumerate_spi(struct ljca_adapter *adap)
 		spi_info->id = desc->info[i].id;
 		spi_info->capacity = desc->info[i].capacity;

drivers/usb/misc/usb-ljca.c static int ljca_enumerate_spi(struct ljca_adapter *adap) · line 668

Quoted 10 lines
drivers/usb/misc/usb-ljca.c +1 -5
 
-		ret = ljca_new_client_device(adap, LJCA_CLIENT_SPI, i,
+		return ljca_new_client_device(adap, LJCA_CLIENT_SPI, i,
 					     "ljca-spi", spi_info,
 					     LJCA_SPI1_ACPI_ADR + i);
-		if (ret) {
-			kfree(spi_info);
-			return ret;
-		}
 	}

Same remark as with the creation of the i2c controllers, please use:

		ret = ljca_new_client_device(...);
		if (ret)
			return ret;

Regards,

Hans

#4 lore raw

PATCH v3 1/1 usb: misc: ljca: Fix double free in error handling path

When auxiliary_device_add() returns error and then calls auxiliary_device_uninit(), callback function ljca_auxdev_release calls kfree(auxdev->dev.platform_data) to free the parameter data of the function ljca_new_client_device. The callers of ljca_new_client_device shouldn't call kfree() again
in the error handling path to free the platform data.

Fix this by cleaning up the redundant kfree() in all callers and adding kfree() the passed in platform_data on errors which happen before auxiliary_device_init() succeeds .

Fixes acd6199f195d ("usb: Add support for Intel LJCA device")
---
 drivers/usb/misc/usb-ljca.c | 22 +++++++++-------------
 1 file changed, 9 insertions(+), 13 deletions(-)
drivers/usb/misc/usb-ljca.c +9 -13
index 35770e608c64..2d30fc1be306 100644
@@ -518,8 +518,10 @@ static int ljca_new_client_device(struct ljca_adapter *adap, u8 type, u8 id,
 	int ret;
 
 	client = kzalloc(sizeof *client, GFP_KERNEL);
-	if (!client)
+	if (!client) {
+		kfree(data);
 		return -ENOMEM;
+	}
 
 	client->type = type;
 	client->id = id;
@@ -535,8 +537,10 @@ static int ljca_new_client_device(struct ljca_adapter *adap, u8 type, u8 id,
 	auxdev->dev.release = ljca_auxdev_release;
 
 	ret = auxiliary_device_init(auxdev);
-	if (ret)
+	if (ret) {
+		kfree(data);
 		goto err_free;
+	}
 
 	ljca_auxdev_acpi_bind(adap, auxdev, adr, id);
 
@@ -590,12 +594,8 @@ static int ljca_enumerate_gpio(struct ljca_adapter *adap)
 		valid_pin[i] = get_unaligned_le32(&desc->bank_desc[i].valid_pins);
 	bitmap_from_arr32(gpio_info->valid_pin_map, valid_pin, gpio_num);
 
-	ret = ljca_new_client_device(adap, LJCA_CLIENT_GPIO, 0, "ljca-gpio",
+	return ljca_new_client_device(adap, LJCA_CLIENT_GPIO, 0, "ljca-gpio",
 				     gpio_info, LJCA_GPIO_ACPI_ADR);
-	if (ret)
-		kfree(gpio_info);
-
-	return ret;
 }
 
 static int ljca_enumerate_i2c(struct ljca_adapter *adap)
@@ -629,10 +629,8 @@ static int ljca_enumerate_i2c(struct ljca_adapter *adap)
 		ret = ljca_new_client_device(adap, LJCA_CLIENT_I2C, i,
 					     "ljca-i2c", i2c_info,
 					     LJCA_I2C1_ACPI_ADR + i);
-		if (ret) {
-			kfree(i2c_info);
+		if (ret)
 			return ret;
-		}
 	}
 
 	return 0;
@@ -669,10 +667,8 @@ static int ljca_enumerate_spi(struct ljca_adapter *adap)
 		ret = ljca_new_client_device(adap, LJCA_CLIENT_SPI, i,
 					     "ljca-spi", spi_info,
 					     LJCA_SPI1_ACPI_ADR + i);
-		if (ret) {
-			kfree(spi_info);
+		if (ret)
 			return ret;
-		}
 	}
 
 	return 0;
Signature · 1 line
2.36.1
Reviewed-by on v3 1/1
#5 lore raw

Hi,

Quoted 7 lines · Yongzhi Liu

When auxiliary_device_add() returns error and then calls auxiliary_device_uninit(), callback function ljca_auxdev_release calls kfree(auxdev->dev.platform_data) to free the parameter data of the function ljca_new_client_device. The callers of ljca_new_client_device shouldn't call kfree() again
in the error handling path to free the platform data.

Quoted 6 lines

Fix this by cleaning up the redundant kfree() in all callers and adding kfree() the passed in platform_data on errors which happen before auxiliary_device_init() succeeds .

Fixes acd6199f195d ("usb: Add support for Intel LJCA device")

Thanks, patch looks good to me:

Regards,

Hans

Quoted 70 lines
---
 drivers/usb/misc/usb-ljca.c | 22 +++++++++-------------
 1 file changed, 9 insertions(+), 13 deletions(-)
drivers/usb/misc/usb-ljca.c +9 -13
index 35770e608c64..2d30fc1be306 100644
@@ -518,8 +518,10 @@ static int ljca_new_client_device(struct ljca_adapter *adap, u8 type, u8 id,
 	int ret;
 
 	client = kzalloc(sizeof *client, GFP_KERNEL);
-	if (!client)
+	if (!client) {
+		kfree(data);
 		return -ENOMEM;
+	}
 
 	client->type = type;
 	client->id = id;
@@ -535,8 +537,10 @@ static int ljca_new_client_device(struct ljca_adapter *adap, u8 type, u8 id,
 	auxdev->dev.release = ljca_auxdev_release;
 
 	ret = auxiliary_device_init(auxdev);
-	if (ret)
+	if (ret) {
+		kfree(data);
 		goto err_free;
+	}
 
 	ljca_auxdev_acpi_bind(adap, auxdev, adr, id);
 
@@ -590,12 +594,8 @@ static int ljca_enumerate_gpio(struct ljca_adapter *adap)
 		valid_pin[i] = get_unaligned_le32(&desc->bank_desc[i].valid_pins);
 	bitmap_from_arr32(gpio_info->valid_pin_map, valid_pin, gpio_num);
 
-	ret = ljca_new_client_device(adap, LJCA_CLIENT_GPIO, 0, "ljca-gpio",
+	return ljca_new_client_device(adap, LJCA_CLIENT_GPIO, 0, "ljca-gpio",
 				     gpio_info, LJCA_GPIO_ACPI_ADR);
-	if (ret)
-		kfree(gpio_info);
-
-	return ret;
 }
 
 static int ljca_enumerate_i2c(struct ljca_adapter *adap)
@@ -629,10 +629,8 @@ static int ljca_enumerate_i2c(struct ljca_adapter *adap)
 		ret = ljca_new_client_device(adap, LJCA_CLIENT_I2C, i,
 					     "ljca-i2c", i2c_info,
 					     LJCA_I2C1_ACPI_ADR + i);
-		if (ret) {
-			kfree(i2c_info);
+		if (ret)
 			return ret;
-		}
 	}
 
 	return 0;
@@ -669,10 +667,8 @@ static int ljca_enumerate_spi(struct ljca_adapter *adap)
 		ret = ljca_new_client_device(adap, LJCA_CLIENT_SPI, i,
 					     "ljca-spi", spi_info,
 					     LJCA_SPI1_ACPI_ADR + i);
-		if (ret) {
-			kfree(spi_info);
+		if (ret)
 			return ret;
-		}
 	}
 
 	return 0;