Skip to contentSkip to conversation

leds: pca9532: check i2c_smbus_read_byte_data() return value

PATCH Applied [email protected] 5 participants 5 messages Jul 27 – Sep 22

Subsystems (3)

v1 of 2 Applied Latest is v2 →

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 leds: pca9532: check i2c_smbus_read_byte_data() return value

From: Haibo Chen <[email protected]>

Coverity report INTEGER_OVERFLOW (tainted_data_return -> cast_underflow -> overflow) in pca9532_getled(). pca9532_getled() stored the return value of i2c_smbus_read_byte_data() into a char and used it directly to compute the LED state without checking for an error. i2c_smbus_read_byte_data() returns a negative error code on failure. On an I2C read error the negative value was assigned to an unsigned type, triggering an integer underflow/overflow, and a meaningless LED state was returned to the caller.

Read the value into an int, check for the error, and fall back to PCA9532_OFF while emitting a warning when the read fails.

Assisted-by VeroCoder:vero-1 [cli]
---
 drivers/leds/leds-pca9532.c | 9 ++++++---
 1 file changed, 6 insertions(+), 3 deletions(-)
drivers/leds/leds-pca9532.c +6 -3
index 2d37e00e459dee9f0313830f4852b01f40977311..e8ee17b8bd547a92372a8e74a6781b95ae831fe2 100644
@@ -298,12 +298,15 @@ static enum pca9532_state pca9532_getled(struct pca9532_led *led)
 	struct i2c_client *client = led->client;
 	struct pca9532_data *data = i2c_get_clientdata(client);
 	u8 maxleds = data->chip_info->num_leds;
-	char reg;
-	enum pca9532_state ret;
+	int reg;
+	enum pca9532_state ret = PCA9532_OFF;
 
 	mutex_lock(&data->update_lock);
 	reg = i2c_smbus_read_byte_data(client, LED_REG(maxleds, led->id));
-	ret = (reg & LED_MASK(led->id)) >> LED_SHIFT(led->id);
+	if (reg >= 0)
+		ret = (reg & LED_MASK(led->id)) >> LED_SHIFT(led->id);
+	else
+		dev_warn(&client->dev, "failed to read LED register: %d\n", reg);
 	mutex_unlock(&data->update_lock);
 	return ret;
 }

---
base-commit: c5e32e86ca02b003f86e095d379b38148999293d
change-id: 20260727-led-fix-28f085e12feb

Best regards,

Signature · 1 line
Haibo Chen <[email protected]>
#1 lore raw
Quoted 21 lines

-----Original Message-----
From: Bough Chen (OSS) <[email protected]>
Sent: 2026年7月27日 17:58
To: Riku Voipio <[email protected]>; Lee Jones <[email protected]>; Pavel Machek <[email protected]>
Cc: [email protected]; [email protected]; [email protected]; Bough Chen <[email protected]>
Subject: [PATCH] leds: pca9532: check i2c_smbus_read_byte_data() return value

From: Haibo Chen <[email protected]>

Coverity report INTEGER_OVERFLOW (tainted_data_return -> cast_underflow -> overflow) in pca9532_getled().
pca9532_getled() stored the return value of i2c_smbus_read_byte_data() into a char and used it directly to compute the LED state without checking for an error. i2c_smbus_read_byte_data() returns a negative error code on failure. On an I2C read error the negative value was assigned to an unsigned type, triggering an integer underflow/overflow, and a meaningless LED state was returned to the caller.

Quoted 2 lines

Read the value into an int, check for the error, and fall back to PCA9532_OFF while emitting a warning when the read fails.

Sorry, I forget to add the fix tag:

Fixes 28c5fe99016d ("leds: pca9532: Extend pca9532 device tree support")

Will add it into commit log if need a V2.

Regards
Haibo Chen

Quoted 40 lines
Assisted-by VeroCoder:vero-1 [cli]
---
 drivers/leds/leds-pca9532.c | 9 ++++++---
 1 file changed, 6 insertions(+), 3 deletions(-)
drivers/leds/leds-pca9532.c index renamed from drivers/leds/leds-pca9532.c +0 -0

2d37e00e459dee9f0313830f4852b01f40977311..e8ee17b8bd547a92372a8 e74a6781b95ae831fe2 100644

drivers/leds/leds-pca9532.c +6 -3
@@ -298,12 +298,15 @@ static enum pca9532_state pca9532_getled(struct
 pca9532_led *led)
 	struct i2c_client *client = led->client;
 	struct pca9532_data *data = i2c_get_clientdata(client);
 	u8 maxleds = data->chip_info->num_leds;
-	char reg;
-	enum pca9532_state ret;
+	int reg;
+	enum pca9532_state ret = PCA9532_OFF;
 
 	mutex_lock(&data->update_lock);
 	reg = i2c_smbus_read_byte_data(client, LED_REG(maxleds, led->id));
-	ret = (reg & LED_MASK(led->id)) >> LED_SHIFT(led->id);
+	if (reg >= 0)
+		ret = (reg & LED_MASK(led->id)) >> LED_SHIFT(led->id);
+	else
+		dev_warn(&client->dev, "failed to read LED register: %d\n", reg);
 	mutex_unlock(&data->update_lock);
 	return ret;

}

---
base-commit: c5e32e86ca02b003f86e095d379b38148999293d
change-id: 20260727-led-fix-28f085e12feb

Best regards,
--
Haibo Chen <[email protected]>

#2 lore raw
In reply to [email protected]
Thank you for your contribution! Sashiko AI review found 3 potential issue(s) to consider:

Thank you for your contribution! Sashiko AI review found 3 potential issue(s) to consider:

Pre-existing issues:
- [High] The patch fixes an unchecked I2C read in pca9532_getled() but misses an identical flaw in pca9532_setled(), where I2C read failures cause register corruption.
- [High] pca9532_gpio_get_value() ignores I2C read errors and silently returns meaningless GPIO states.
- [High] A race condition during device unbinding allows the input device to re-queue work after it has been cancelled, leading to a Use-After-Free of the driver data. --

--- Patch [1]: [PATCH] leds: pca9532: check i2c_smbus_read_byte_data() return value --- commit 87ac49dee89ea1bd1132b25134fdbdfad3eb7c48
Author: Haibo Chen <[email protected]>

leds: pca9532: check i2c_smbus_read_byte_data() return value

This commit fixes an integer underflow and overflow issue in pca9532_getled() by checking the return value of i2c_smbus_read_byte_data(). It ensures that a failed I2C read returns a fallback state and emits a warning instead of computing a meaningless LED state.

Quoted 10 lines
drivers/leds/leds-pca9532.c +0 -2
index 2d37e00e459de..e8ee17b8bd547 100644
@@ -298,12 +298,15 @@ static enum pca9532_state pca9532_getled(struct pca9532_led *led)
 	struct i2c_client *client = led->client;
 	struct pca9532_data *data = i2c_get_clientdata(client);
 	u8 maxleds = data->chip_info->num_leds;
-	char reg;
-	enum pca9532_state ret;

drivers/leds/leds-pca9532.c static enum pca9532_state pca9532_getled(struct pca9532_led *led) · line 309

Quoted 10 lines
drivers/leds/leds-pca9532.c +6 -1
+	int reg;
+	enum pca9532_state ret = PCA9532_OFF;
 
 	mutex_lock(&data->update_lock);
 	reg = i2c_smbus_read_byte_data(client, LED_REG(maxleds, led->id));
-	ret = (reg & LED_MASK(led->id)) >> LED_SHIFT(led->id);
+	if (reg >= 0)
+		ret = (reg & LED_MASK(led->id)) >> LED_SHIFT(led->id);
+	else
+		dev_warn(&client->dev, "failed to read LED register: %d\n", reg);

[Severity: High]
This isn't a bug introduced by this patch, but this commit fixes an unchecked I2C read in pca9532_getled() while missing an identical flaw in pca9532_setled().

If i2c_smbus_read_byte_data() fails when called from pca9532_set_brightness(), it returns a negative error code (e.g., -EIO). In pca9532_setled(), this is truncated to a char, bitwise modified, and blindly written back to the device:

drivers/leds/leds-pca9532.c:pca9532_setled() {
    ...
    reg = i2c_smbus_read_byte_data(client, LED_REG(maxleds, led->id));
    /* zero led bits */
    reg = reg & ~LED_MASK(led->id);
    ...
}

Since one I2C register controls four LEDs, doesn't writing this invalid value back corrupt the state of up to three other LEDs sharing the same register?

[Severity: High]
This is a pre-existing issue, but pca9532_gpio_get_value() also ignores I2C read errors and silently returns meaningless GPIO states.

When any subsystem or userspace reads the GPIO value, an I2C error from i2c_smbus_read_byte_data() is truncated to an unsigned byte, and a bit is extracted and returned as a valid GPIO state:

drivers/leds/leds-pca9532.c:pca9532_gpio_get_value() {
    ...
    reg = i2c_smbus_read_byte_data(data->client, PCA9532_REG_INPUT(offset));

    return !!(reg & (1 << (offset % 8)));
}

Could this mask hardware errors and cause userspace or kernel consumers to silently receive corrupt data, leading to logic errors?

[Severity: High]
This is a pre-existing issue, but there appears to be a race condition during device unbinding that allows the input device to re-queue work after it has been cancelled, leading to a Use-After-Free of the driver data.

The driver uses devm_input_allocate_device() for its input device, so it remains registered until devres cleanup runs AFTER .remove() finishes. However, .remove() calls pca9532_destroy_devices() which cancels the workqueue:

drivers/leds/leds-pca9532.c:pca9532_destroy_devices() {
    ...
            if (data->idev != NULL) {
                cancel_work_sync(&data->work);
                data->idev = NULL;
            }
    ...
}

In the window between cancel_work_sync() returning and devres unregistering the input device, if userspace injects an EV_SND event, pca9532_event() can re-schedule data->work:

drivers/leds/leds-pca9532.c:pca9532_event() {
    ...
    schedule_work(&data->work);
    ...
}

Once devres frees data, won't the workqueue dereference a freed pointer, causing a kernel crash or memory corruption?

Quoted 3 lines
drivers/leds/leds-pca9532.c +0 -0
 	mutex_unlock(&data->update_lock);
 	return ret;
 }
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1
Reviewed-by on v1 1/1
#3 lore raw
In reply to [email protected]
Quoted 11 lines · On Mon, Jul 27, 2026 at 05:57:54PM +0800, [email protected] wrote:

From: Haibo Chen <[email protected]>

Coverity report INTEGER_OVERFLOW (tainted_data_return -> cast_underflow -> overflow) in pca9532_getled(). pca9532_getled() stored the return value of i2c_smbus_read_byte_data() into a char and used it directly to compute the LED state without checking for an error. i2c_smbus_read_byte_data() returns a negative error code on failure. On an I2C read error the negative value was assigned to an unsigned type, triggering an integer underflow/overflow, and a meaningless LED state was returned to the caller.

Quoted 6 lines

Read the value into an int, check for the error, and fall back to PCA9532_OFF while emitting a warning when the read fails.

Assisted-by VeroCoder:vero-1 [cli]

---

Quoted 35 lines
 drivers/leds/leds-pca9532.c | 9 ++++++---
 1 file changed, 6 insertions(+), 3 deletions(-)
drivers/leds/leds-pca9532.c +6 -4
index 2d37e00e459dee9f0313830f4852b01f40977311..e8ee17b8bd547a92372a8e74a6781b95ae831fe2 100644
@@ -298,12 +298,15 @@ static enum pca9532_state pca9532_getled(struct pca9532_led *led)
 	struct i2c_client *client = led->client;
 	struct pca9532_data *data = i2c_get_clientdata(client);
 	u8 maxleds = data->chip_info->num_leds;
-	char reg;
-	enum pca9532_state ret;
+	int reg;
+	enum pca9532_state ret = PCA9532_OFF;
 
 	mutex_lock(&data->update_lock);
 	reg = i2c_smbus_read_byte_data(client, LED_REG(maxleds, led->id));
-	ret = (reg & LED_MASK(led->id)) >> LED_SHIFT(led->id);
+	if (reg >= 0)
+		ret = (reg & LED_MASK(led->id)) >> LED_SHIFT(led->id);
+	else
+		dev_warn(&client->dev, "failed to read LED register: %d\n", reg);
 	mutex_unlock(&data->update_lock);
 	return ret;
 }
 
---

base-commit: c5e32e86ca02b003f86e095d379b38148999293d
change-id: 20260727-led-fix-28f085e12feb

Best regards,
--
Haibo Chen <[email protected]>

maintainer Applied
#4 lore raw

Re: (subset) [PATCH] leds: pca9532: check i2c_smbus_read_byte_data() return value

In reply to [email protected]
Quoted 10 lines · On Mon, 27 Jul 2026 17:57:54 +0800, [email protected] wrote:

Coverity report INTEGER_OVERFLOW (tainted_data_return -> cast_underflow -> overflow) in pca9532_getled(). pca9532_getled() stored the return value of i2c_smbus_read_byte_data() into a char and used it directly to compute the LED state without checking for an error. i2c_smbus_read_byte_data() returns a negative error code on failure. On an I2C read error the negative value was assigned to an unsigned type, triggering an integer underflow/overflow, and a meaningless LED state was returned to the caller.

[...]

Applied, thanks!

[1/1] leds: pca9532: check i2c_smbus_read_byte_data() return value

      commit: 1b75f038e5f71ed32ce6593b5763f9776ec58a25

--
Lee Jones [李琼斯]