Skip to contentSkip to conversation

iio: proximity: hx9023s: validate firmware size

PATCH Superseded Laxman Acharya Padhya 5 participants 7 messages Jul 10 – Jul 11

Subsystems (2)

v1 of 3 Superseded Latest is v3 →

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 iio: proximity: hx9023s: validate firmware size

hx9023s_send_cfg() copies the firmware into a counted flexible array and then reads fixed offsets from the copied data before walking register/value pairs starting at FW_DATA_OFFSET. A truncated firmware image can therefore make the driver read past the copied buffer during probe-time configuration loading.

Reject firmware images that cannot contain the fixed header, reject images too large for the u16 fw_size field, and validate that the advertised register count fits in the remaining payload.

Move release_firmware() to the callback so the firmware object is released on all hx9023s_send_cfg() error paths.

Fixes e9ed97be4fcc ("iio: proximity: hx9023s: Added firmware file parsing functionality")
---
 drivers/iio/proximity/hx9023s.c | 14 +++++++++++---
 1 file changed, 11 insertions(+), 3 deletions(-)
drivers/iio/proximity/hx9023s.c +11 -3
index a6ff7cbe9e6..a2f9c077e58 100644
@@ -18,6 +18,7 @@
 #include <linux/i2c.h>
 #include <linux/interrupt.h>
 #include <linux/irqreturn.h>
+#include <linux/limits.h>
 #include <linux/math64.h>
 #include <linux/module.h>
 #include <linux/mutex.h>
@@ -25,6 +26,7 @@
 #include <linux/property.h>
 #include <linux/regmap.h>
 #include <linux/regulator/consumer.h>
+#include <linux/slab.h>
 #include <linux/types.h>
 #include <linux/units.h>
 
@@ -1031,8 +1033,12 @@ static int hx9023s_bin_load(struct hx9023s_data *data, struct hx9023s_bin *bin)
 
 static int hx9023s_send_cfg(const struct firmware *fw, struct hx9023s_data *data)
 {
-	struct hx9023s_bin *bin __free(kfree) =
-		kzalloc(fw->size + sizeof(*bin), GFP_KERNEL);
+	struct hx9023s_bin *bin __free(kfree) = NULL;
+
+	if (fw->size < FW_DATA_OFFSET || fw->size > U16_MAX)
+		return -EINVAL;
+
+	bin = kzalloc(sizeof(*bin) + fw->size, GFP_KERNEL);
 	if (!bin)
 		return -ENOMEM;
 
@@ -1041,7 +1047,8 @@ static int hx9023s_send_cfg(const struct firmware *fw, struct hx9023s_data *data
 	bin->fw_ver = bin->data[FW_VER_OFFSET];
 	bin->reg_count = get_unaligned_le16(bin->data + FW_REG_CNT_OFFSET);
 
-	release_firmware(fw);
+	if (bin->reg_count > (bin->fw_size - FW_DATA_OFFSET) / 2)
+		return -EINVAL;
 
 	return hx9023s_bin_load(data, bin);
 }
@@ -1058,6 +1065,7 @@ static void hx9023s_cfg_update(const struct firmware *fw, void *context)
 	}
 
 	ret = hx9023s_send_cfg(fw, data);
+	release_firmware(fw);
 	if (ret) {
 		dev_warn(dev, "Firmware update failed: %d\n", ret);
 		goto no_fw;

base-commit: 0e35b9b6ec0ffcc5e23cbdec09f5c622ad532b53

Signature · 1 line
2.51.2
Reviewed-by on v1 1/1
#1 lore raw

drivers/iio/proximity/hx9023s.c static void hx9023s_cfg_update(const struct firmware *fw, void *context) · line 1068

Quoted 5 lines · On Fri, 10 Jul 2026 20:07:12 +0545 Laxman Acharya Padhya…
drivers/iio/proximity/hx9023s.c +1 -0
@@ -1058,6 +1065,7 @@ static void hx9023s_cfg_update(const struct firmware *fw, void *context)
 	}
 
 	ret = hx9023s_send_cfg(fw, data);
+	release_firmware(fw);

Why not move this after the if? Keep the call/retval check coupled.

drivers/iio/proximity/hx9023s.c static void hx9023s_cfg_update(const struct firmware *fw, void *context) · line 1071

Quoted 4 lines
drivers/iio/proximity/hx9023s.c +0 -0
 	if (ret) {
 		dev_warn(dev, "Firmware update failed: %d\n", ret);
 		goto no_fw;

Otherwise this looks good. Feel free to add my tag with the v2:

Signature · 3 lines
Kind regards

CJD
reviewer
#2 lore raw
Quoted 9 lines · Joshua Crofts

drivers/iio/proximity/hx9023s.c static void hx9023s_cfg_update(const struct firmware *fw, void *context) · line 1068

Quoted 5 lines · On Fri, 10 Jul 2026 20:07:12 +0545 Laxman Acharya Padhya…
drivers/iio/proximity/hx9023s.c +1 -0
@@ -1058,6 +1065,7 @@ static void hx9023s_cfg_update(const struct firmware *fw, void *context)
 	}
 
 	ret = hx9023s_send_cfg(fw, data);
+	release_firmware(fw);

Why not move this after the if? Keep the call/retval check coupled.

Because the if branch contains a goto and would therefore skip calling release_firmware() in case of error.

Quoted 10 lines

drivers/iio/proximity/hx9023s.c static void hx9023s_cfg_update(const struct firmware *fw, void *context) · line 1071

Quoted 5 lines
drivers/iio/proximity/hx9023s.c +0 -0
 	if (ret) {
 		dev_warn(dev, "Firmware update failed: %d\n", ret);
 		goto no_fw;

Otherwise this looks good. Feel free to add my tag with the v2:

reviewer
#3 lore raw
In reply to Laxman Acharya Padhya
Quoted 6 lines · Laxman Acharya Padhya

hx9023s_send_cfg() copies the firmware into a counted flexible array and then reads fixed offsets from the copied data before walking register/value pairs starting at FW_DATA_OFFSET. A truncated firmware image can therefore make the driver read past the copied buffer during probe-time configuration loading.

Quoted 6 lines

Reject firmware images that cannot contain the fixed header, reject images too large for the u16 fw_size field, and validate that the advertised register count fits in the remaining payload.

Move release_firmware() to the callback so the firmware object is released on all hx9023s_send_cfg() error paths.

This could probably be considered a separate fix (and therefore separate patch) since it looks like there is an existing code path (return -ENOMEM) where it would not be released.

Quoted 29 lines
Fixes e9ed97be4fcc ("iio: proximity: hx9023s: Added firmware file parsing functionality")
---
 drivers/iio/proximity/hx9023s.c | 14 +++++++++++---
 1 file changed, 11 insertions(+), 3 deletions(-)
drivers/iio/proximity/hx9023s.c +2 -0
index a6ff7cbe9e6..a2f9c077e58 100644
@@ -18,6 +18,7 @@
 #include <linux/i2c.h>
 #include <linux/interrupt.h>
 #include <linux/irqreturn.h>
+#include <linux/limits.h>
 #include <linux/math64.h>
 #include <linux/module.h>
 #include <linux/mutex.h>
@@ -25,6 +26,7 @@
 #include <linux/property.h>
 #include <linux/regmap.h>
 #include <linux/regulator/consumer.h>
+#include <linux/slab.h>
 #include <linux/types.h>
 #include <linux/units.h>
 

drivers/iio/proximity/hx9023s.c static int hx9023s_bin_load(struct hx9023s_data *data, struct hx9023s_bin *bin) · line 1036

Quoted 6 lines
drivers/iio/proximity/hx9023s.c +1 -2
@@ -1031,8 +1033,12 @@ static int hx9023s_bin_load(struct hx9023s_data *data, struct hx9023s_bin *bin)
 
 static int hx9023s_send_cfg(const struct firmware *fw, struct hx9023s_data *data)
 {
-	struct hx9023s_bin *bin __free(kfree) =
-		kzalloc(fw->size + sizeof(*bin), GFP_KERNEL);
+	struct hx9023s_bin *bin __free(kfree) = NULL;

We don't initialize autocleanup variables to NULL. So leave this as-is and just put the size check before it.

Quoted 28 lines
drivers/iio/proximity/hx9023s.c +8 -1
+
+	if (fw->size < FW_DATA_OFFSET || fw->size > U16_MAX)
+		return -EINVAL;
+
+	bin = kzalloc(sizeof(*bin) + fw->size, GFP_KERNEL);
 	if (!bin)
 		return -ENOMEM;
 
@@ -1041,7 +1047,8 @@ static int hx9023s_send_cfg(const struct firmware *fw, struct hx9023s_data *data
 	bin->fw_ver = bin->data[FW_VER_OFFSET];
 	bin->reg_count = get_unaligned_le16(bin->data + FW_REG_CNT_OFFSET);
 
-	release_firmware(fw);
+	if (bin->reg_count > (bin->fw_size - FW_DATA_OFFSET) / 2)
+		return -EINVAL;
 
 	return hx9023s_bin_load(data, bin);
 }
@@ -1058,6 +1065,7 @@ static void hx9023s_cfg_update(const struct firmware *fw, void *context)
 	}
 
 	ret = hx9023s_send_cfg(fw, data);
+	release_firmware(fw);
 	if (ret) {
 		dev_warn(dev, "Firmware update failed: %d\n", ret);
 		goto no_fw;

base-commit: 0e35b9b6ec0ffcc5e23cbdec09f5c622ad532b53

#4 lore raw

PATCH v2 1/1 iio: proximity: hx9023s: validate firmware size

In reply to Laxman Acharya Padhya

hx9023s_send_cfg() copies the firmware into a counted flexible array and then reads fixed offsets from the copied data before walking register/value pairs starting at FW_DATA_OFFSET. A truncated firmware image can therefore make the driver read past the copied buffer during probe-time configuration loading.

Reject firmware images that cannot contain the fixed header, reject images too large for the u16 fw_size field, and validate that the advertised register count fits in the remaining payload.

Fixes e9ed97be4fcc ("iio: proximity: hx9023s: Added firmware file parsing functionality")
---
 drivers/iio/proximity/hx9023s.c | 10 ++++++++--
 1 file changed, 8 insertions(+), 2 deletions(-)
drivers/iio/proximity/hx9023s.c +8 -2
index a6ff7cbe9e6..9d91ce681ac 100644
@@ -18,6 +18,7 @@
 #include <linux/i2c.h>
 #include <linux/interrupt.h>
 #include <linux/irqreturn.h>
+#include <linux/limits.h>
 #include <linux/math64.h>
 #include <linux/module.h>
 #include <linux/mutex.h>
@@ -1031,8 +1032,11 @@ static int hx9023s_bin_load(struct hx9023s_data *data, struct hx9023s_bin *bin)
 
 static int hx9023s_send_cfg(const struct firmware *fw, struct hx9023s_data *data)
 {
+	if (fw->size < FW_DATA_OFFSET || fw->size > U16_MAX)
+		return -EINVAL;
+
 	struct hx9023s_bin *bin __free(kfree) =
-		kzalloc(fw->size + sizeof(*bin), GFP_KERNEL);
+		kzalloc(sizeof(*bin) + fw->size, GFP_KERNEL);
 	if (!bin)
 		return -ENOMEM;
 
@@ -1041,7 +1045,8 @@ static int hx9023s_send_cfg(const struct firmware *fw, struct hx9023s_data *data
 	bin->fw_ver = bin->data[FW_VER_OFFSET];
 	bin->reg_count = get_unaligned_le16(bin->data + FW_REG_CNT_OFFSET);
 
-	release_firmware(fw);
+	if (bin->reg_count > (bin->fw_size - FW_DATA_OFFSET) / 2)
+		return -EINVAL;
 
 	return hx9023s_bin_load(data, bin);
 }
@@ -1058,6 +1063,7 @@ static void hx9023s_cfg_update(const struct firmware *fw, void *context)
 	}
 
 	ret = hx9023s_send_cfg(fw, data);
+	release_firmware(fw);
 	if (ret) {
 		dev_warn(dev, "Firmware update failed: %d\n", ret);
 		goto no_fw;
Signature · 1 line
2.51.2
#5 lore raw
Quoted 6 lines · On Fri, 10 Jul 2026 21:13:42 +0545 Laxman Acharya Padhya…

hx9023s_send_cfg() copies the firmware into a counted flexible array and then reads fixed offsets from the copied data before walking register/value pairs starting at FW_DATA_OFFSET. A truncated firmware image can therefore make the driver read past the copied buffer during probe-time configuration loading.

Quoted 8 lines

Reject firmware images that cannot contain the fixed header, reject images too large for the u16 fw_size field, and validate that the advertised register count fits in the remaining payload.

Fixes e9ed97be4fcc ("iio: proximity: hx9023s: Added firmware file parsing functionality")

A few minor comments.

Thanks,

Jonathan

Quoted 17 lines
---
 drivers/iio/proximity/hx9023s.c | 10 ++++++++--
 1 file changed, 8 insertions(+), 2 deletions(-)
drivers/iio/proximity/hx9023s.c +1 -0
index a6ff7cbe9e6..9d91ce681ac 100644
@@ -18,6 +18,7 @@
 #include <linux/i2c.h>
 #include <linux/interrupt.h>
 #include <linux/irqreturn.h>
+#include <linux/limits.h>
 #include <linux/math64.h>
 #include <linux/module.h>
 #include <linux/mutex.h>

drivers/iio/proximity/hx9023s.c static int hx9023s_bin_load(struct hx9023s_data *data, struct hx9023s_bin *bin) · line 1035

Quoted 4 lines
drivers/iio/proximity/hx9023s.c +1 -0
@@ -1031,8 +1032,11 @@ static int hx9023s_bin_load(struct hx9023s_data *data, struct hx9023s_bin *bin)
 
 static int hx9023s_send_cfg(const struct firmware *fw, struct hx9023s_data *data)
 {
+	if (fw->size < FW_DATA_OFFSET || fw->size > U16_MAX)

Add a comment on why you've picked that upper limit.

drivers/iio/proximity/hx9023s.c static int hx9023s_bin_load(struct hx9023s_data *data, struct hx9023s_bin *bin) · line 1039

Quoted 5 lines
drivers/iio/proximity/hx9023s.c +3 -1
+		return -EINVAL;
+
 	struct hx9023s_bin *bin __free(kfree) =
-		kzalloc(fw->size + sizeof(*bin), GFP_KERNEL);
+		kzalloc(sizeof(*bin) + fw->size, GFP_KERNEL);

This doesn't belong in the fix given it is just a reorder. Maybe it makes sense but if it does, separate patch with an explanation of why.

Quoted 21 lines
drivers/iio/proximity/hx9023s.c +3 -1
 	if (!bin)
 		return -ENOMEM;
 
@@ -1041,7 +1045,8 @@ static int hx9023s_send_cfg(const struct firmware *fw, struct hx9023s_data *data
 	bin->fw_ver = bin->data[FW_VER_OFFSET];
 	bin->reg_count = get_unaligned_le16(bin->data + FW_REG_CNT_OFFSET);
 
-	release_firmware(fw);
+	if (bin->reg_count > (bin->fw_size - FW_DATA_OFFSET) / 2)
+		return -EINVAL;
 
 	return hx9023s_bin_load(data, bin);
 }
@@ -1058,6 +1063,7 @@ static void hx9023s_cfg_update(const struct firmware *fw, void *context)
 	}
 
 	ret = hx9023s_send_cfg(fw, data);
+	release_firmware(fw);
 	if (ret) {
 		dev_warn(dev, "Firmware update failed: %d\n", ret);
 		goto no_fw;
#6 lore raw
Quoted 2 lines · Jonathan Cameron

On Fri, 10 Jul 2026 21:13:42 +0545
Laxman Acharya Padhya <[email protected]> wrote:

...

Quoted 6 lines

drivers/iio/proximity/hx9023s.c static int hx9023s_bin_load(struct hx9023s_data *data, struct hx9023s_bin *bin) · line 1039

Quoted 3 lines
drivers/iio/proximity/hx9023s.c +1 -1
 	struct hx9023s_bin *bin __free(kfree) =
-		kzalloc(fw->size + sizeof(*bin), GFP_KERNEL);
+		kzalloc(sizeof(*bin) + fw->size, GFP_KERNEL);

This doesn't belong in the fix given it is just a reorder. Maybe it makes sense but if it does, separate patch with an explanation of why.

Semantically this should use struct_size() which brings us to the kzalloc_flex(). And this will be a separate patch.

Quoted 2 lines
Quoted 2 lines
drivers/iio/proximity/hx9023s.c +0 -0
 	if (!bin)
 		return -ENOMEM;
Signature · 2 lines
With Best Regards,
Andy Shevchenko