Skip to contentSkip to conversation

iio: proximity: hx9023s: firmware property, safety fixes

PATCH Superseded Yasin Lee 3 participants 15 messages Feb 12

Subsystems (2)

v2 of 3 Superseded Latest is v3 →

lore
Share & tools
lore

Yasin Lee

author
#0 lore raw

v2 cover iio: proximity: hx9023s: firmware property, safety fixes

Hi,

This series contains a set of small fixes and improvements for the hx9023s proximity sensor driver.

The changes include:

- Fixing a potential out-of-bounds access when copying firmware data.
- Avoiding a division-by-zero case when the sampling frequency is unspecified.
- Allowing the firmware file name to be specified via firmware-name property, along with the corresponding DT binding update.

All changes are independent but related to robustness and platform support, and are grouped together for easier review.

Tested on a DT-based platform with firmware loading enabled.

Thanks for your time and review.

Best regards,
Yasin Lee

---
Changes in v2:
- Patch 1: Added Fixes tag
- Patch 2:
- Added Fixes tag
- Changed conditional logic to positive style (val || val2) as suggested
- Patch 3 (dt-bindings):
- Removed redundant description.
- This patch now comes before driver implementation (documentation first)
- Patch 4 (driver):
- Fixed variable declaration order (reverse xmas tree)
- Split declaration and assignment for fw_name
- Removed unnecessary error checking for device_property_read_string()
- Simplified to: fw_name = "hx9023s.bin"; device_property_read_string(...);
- Patch 5: Temporarily dropped (ACPI support)
- Will resubmit separately once TYHX vendor ID is officially registered with UEFI Forum
- Need to provide verified DSDT evidence
- Link to v1: https://lore.kernel.org/r/[email protected]

---
Yasin Lee (4):
iio: proximity: hx9023s: fix out-of-bounds access when copying firmware
iio: proximity: hx9023s: Protect against division by zero in set_samp_freq
dt-bindings: iio: proximity: hx9023s: support firmware-name property
iio: proximity: hx9023s: support firmware-name property

 .../devicetree/bindings/iio/proximity/tyhx,hx9023s.yaml    |  4 ++++
 drivers/iio/proximity/hx9023s.c                            | 14 ++++++++++----
 2 files changed, 14 insertions(+), 4 deletions(-)

---
base-commit: c8bfb63c902678228a0a265e22a5f55404988a43
change-id: 20260209-upstream-20260219-a7af6d9a85ad

Best regards,

Signature · 1 line
Yasin Lee <[email protected]>
author
#1 lore raw

PATCH v2 1/4 iio: proximity: hx9023s: fix out-of-bounds access when copying firmware

Initialize fw_size before copying firmware data into the flexible array member to match the __counted_by() annotation. This fixes a potential out-of-bounds access that could lead to a kernel crash.

Fixes e9ed97be4fcc ("iio: proximity: hx9023s: Added firmware file parsing functionality")
---
 drivers/iio/proximity/hx9023s.c | 3 +--
 1 file changed, 1 insertion(+), 2 deletions(-)
drivers/iio/proximity/hx9023s.c +1 -2
index 2918dfc0df54..ad839db6b326 100644
@@ -1034,9 +1034,8 @@ static int hx9023s_send_cfg(const struct firmware *fw, struct hx9023s_data *data
 	if (!bin)
 		return -ENOMEM;
 
-	memcpy(bin->data, fw->data, fw->size);
-
 	bin->fw_size = fw->size;
+	memcpy(bin->data, fw->data, bin->fw_size);
 	bin->fw_ver = bin->data[FW_VER_OFFSET];
 	bin->reg_count = get_unaligned_le16(bin->data + FW_REG_CNT_OFFSET);
 
Signature · 1 line
2.43.0
Reviewed-by on v2 1/4
#2 lore raw

Re: [PATCH v2 1/4] iio: proximity: hx9023s: fix out-of-bounds access when copying firmware

Quoted 3 lines · Yasin Lee

Initialize fw_size before copying firmware data into the flexible array member to match the __counted_by() annotation. This fixes a potential out-of-bounds access that could lead to a kernel crash.

Signature · 2 lines
With Best Regards,
Andy Shevchenko
#3 lore raw

Re: [PATCH v2 1/4] iio: proximity: hx9023s: fix out-of-bounds access when copying firmware

In reply to Yasin Lee
Quoted 3 lines · Yasin Lee

Initialize fw_size before copying firmware data into the flexible array member to match the __counted_by() annotation. This fixes a potential out-of-bounds access that could lead to a kernel crash.

I don't think so. Code is equivalent and this was just false positive because compiler could not deduce that in this case counted_by can be by fw->size.

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

Also not appropriate IMO, nothing to fix.

Commit is fine, but this is not a fix, IMO.

Best regards,
Krzysztof

#4 lore raw

Re: [PATCH v2 1/4] iio: proximity: hx9023s: fix out-of-bounds access when copying firmware

Quoted 8 lines · Krzysztof Kozlowski
Quoted 3 lines · Yasin Lee

Initialize fw_size before copying firmware data into the flexible array member to match the __counted_by() annotation. This fixes a potential out-of-bounds access that could lead to a kernel crash.

I don't think so. Code is equivalent and this was just false positive because compiler could not deduce that in this case counted_by can be by fw->size.

In accordance with [1]:

When finding the element count assignment, it must be reordered to before any accesses of the PTR->ARRAY itself, otherwise runtime checking will trigger (i.e. the index of ARRAY will be checked against COUNTER before COUNTER has been assigned the correct COUNT value).

[1]: https://github.com/kees/kernel-tools/blob/trunk/coccinelle/examples/counted_by.cocci#L3

Signature · 2 lines
With Best Regards,
Andy Shevchenko
#5 lore raw

Re: [PATCH v2 1/4] iio: proximity: hx9023s: fix out-of-bounds access when copying firmware

Quoted 10 lines · Andy Shevchenko
Quoted 8 lines · Krzysztof Kozlowski
Quoted 3 lines · Yasin Lee

Initialize fw_size before copying firmware data into the flexible array member to match the __counted_by() annotation. This fixes a potential out-of-bounds access that could lead to a kernel crash.

I don't think so. Code is equivalent and this was just false positive because compiler could not deduce that in this case counted_by can be by fw->size.

Quoted 9 lines

In accordance with [1]:

When finding the element count assignment, it must be reordered to before any accesses of the PTR->ARRAY itself, otherwise runtime checking will trigger (i.e. the index of ARRAY will be checked against COUNTER before COUNTER has been assigned the correct COUNT value).

[1]: https://github.com/kees/kernel-tools/blob/trunk/coccinelle/examples/counted_by.cocci#L3

Thanks for correcting me. I agree this is a fix but, still, I think this will no be an out-of-bounds access, because then the runtime checker will use initial bin->fw_size value of 0.

Best regards,
Krzysztof

author
#6 lore raw

PATCH v2 2/4 iio: proximity: hx9023s: Protect against division by zero in set_samp_freq

In reply to Yasin Lee

Avoid division by zero when sampling frequency is unspecified by falling back to a default 100ms sampling period.

Fixes 60df548277b7 ("iio: proximity: Add driver support for TYHX's HX9023S capacitive proximity sensor")
---
 drivers/iio/proximity/hx9023s.c | 6 +++++-
 1 file changed, 5 insertions(+), 1 deletion(-)
drivers/iio/proximity/hx9023s.c +5 -1
index ad839db6b326..e714fe00a44e 100644
@@ -719,7 +719,11 @@ static int hx9023s_set_samp_freq(struct hx9023s_data *data, int val, int val2)
 	struct device *dev = regmap_get_device(data->regmap);
 	unsigned int i, period_ms;
 
-	period_ms = div_u64(NANO, (val * MEGA + val2));
+	if (val || val2)
+		period_ms = div_u64(NANO, (val * MEGA + val2));
+	else
+		/* Fallback to a safe default sampling period */
+		period_ms = 100;
 
 	for (i = 0; i < ARRAY_SIZE(hx9023s_samp_freq_table); i++) {
 		if (period_ms == hx9023s_samp_freq_table[i])
Signature · 1 line
2.43.0
#7 lore raw

Re: [PATCH v2 2/4] iio: proximity: hx9023s: Protect against division by zero in set_samp_freq

Quoted 2 lines · Yasin Lee

Avoid division by zero when sampling frequency is unspecified by falling back to a default 100ms sampling period.

...

drivers/iio/proximity/hx9023s.c static int hx9023s_set_samp_freq(struct hx9023s_data *data, int val, int val2) · line 723

Quoted 3 lines
drivers/iio/proximity/hx9023s.c +2 -1
-	period_ms = div_u64(NANO, (val * MEGA + val2));
+	if (val || val2)
+		period_ms = div_u64(NANO, (val * MEGA + val2));

While at it, drop unneeded parentheses.

drivers/iio/proximity/hx9023s.c static int hx9023s_set_samp_freq(struct hx9023s_data *data, int val, int val2) · line 726

Quoted 3 lines
drivers/iio/proximity/hx9023s.c +3 -0
+	else
+		/* Fallback to a safe default sampling period */
+		period_ms = 100;

Not sure about this. Perhaps we should rather do

	if (!val || !val2)
		return -EINVAL;

?

Signature · 2 lines
With Best Regards,
Andy Shevchenko
author
#8 lore raw

Re: [PATCH v2 2/4] iio: proximity: hx9023s: Protect against division by zero in set_samp_freq

Quoted 17 lines · Andy Shevchenko
Quoted 2 lines · Yasin Lee

Avoid division by zero when sampling frequency is unspecified by falling back to a default 100ms sampling period.

...

drivers/iio/proximity/hx9023s.c static int hx9023s_set_samp_freq(struct hx9023s_data *data, int val, int val2) · line 723

Quoted 3 lines
drivers/iio/proximity/hx9023s.c +2 -1
-     period_ms = div_u64(NANO, (val * MEGA + val2));
+     if (val || val2)
+             period_ms = div_u64(NANO, (val * MEGA + val2));

While at it, drop unneeded parentheses.

drivers/iio/proximity/hx9023s.c static int hx9023s_set_samp_freq(struct hx9023s_data *data, int val, int val2) · line 726

Quoted 3 lines
drivers/iio/proximity/hx9023s.c +3 -0
+     else
+             /* Fallback to a safe default sampling period */
+             period_ms = 100;
Quoted 7 lines

Not sure about this. Perhaps we should rather do

        if (!val || !val2)
                return -EINVAL;

?

Agreed. I will update this in v3 as below .
...
if (!val && !val2)

    return -EINVAL;

period_ms = div_u64(NANO, val * MEGA + val2);
...

Quoted 5 lines

--
With Best Regards,
Andy Shevchenko

#9 lore raw

Re: [PATCH v2 2/4] iio: proximity: hx9023s: Protect against division by zero in set_samp_freq

Quoted 3 lines · Yasin Lee
Quoted 1 line · Andy Shevchenko

On Thu, Feb 12, 2026 at 02:26:53PM +0800, Yasin Lee wrote:

...

Quoted 5 lines
Quoted 5 lines

Not sure about this. Perhaps we should rather do

        if (!val || !val2)
                return -EINVAL;
Quoted 8 lines
Quoted 1 line

?

Agreed. I will update this in v3 as below .
...
if (!val && !val2)

    return -EINVAL;

period_ms = div_u64(NANO, val * MEGA + val2);

Then don't touch parentheses, as the line will have nothing to do with the scope of the patch.

Signature · 2 lines
With Best Regards,
Andy Shevchenko
author
#10 lore raw

PATCH v2 3/4 dt-bindings: iio: proximity: hx9023s: support firmware-name property

In reply to Yasin Lee

Allow specifying the firmware file name via device tree.

---
 Documentation/devicetree/bindings/iio/proximity/tyhx,hx9023s.yaml | 4 ++++
 1 file changed, 4 insertions(+)
Documentation/devicetree/bindings/iio/proximity/tyhx,hx9023s.yaml +4 -0
index 64ce8bc8bd36..cc5b5284c267 100644
@@ -28,6 +28,9 @@ properties:
 
   vdd-supply: true
 
+  firmware-name:
+    maxItems: 1
+
   "#address-cells":
     const: 1
 
@@ -65,6 +68,7 @@ examples:
         interrupt-parent = <&pio>;
         interrupts = <16 IRQ_TYPE_EDGE_FALLING>;
         vdd-supply = <&pp1800_prox>;
+        firmware-name = "hx9023s.bin";
 
         #address-cells = <1>;
         #size-cells = <0>;
Signature · 1 line
2.43.0
#11 lore raw

Re: [PATCH v2 3/4] dt-bindings: iio: proximity: hx9023s: support firmware-name property

Quoted 1 line · Yasin Lee

Allow specifying the firmware file name via device tree.

Nothing improved here. So again: Why do we want to allow it? Why a device needs a firmware now but before it did not need? What changed?

You already got these questions.

Best regards,
Krzysztof

author
#12 lore raw

Re: [PATCH v2 3/4] dt-bindings: iio: proximity: hx9023s: support firmware-name property

Quoted 9 lines · Krzysztof Kozlowski
Quoted 1 line · Yasin Lee

Allow specifying the firmware file name via device tree.

Nothing improved here. So again: Why do we want to allow it? Why a device needs a firmware now but before it did not need? What changed?

You already got these questions.

Hi Krzysztof,

You are right, the commit message did not sufficiently explain the motivation.

The device has always required firmware; previously the driver assumed a single default firmware file name suitable for the reference design. What changes here is that different boards may use different electrode layouts and mechanical structures, which require distinct calibration and configuration data.

I will update the commit message in v3 to clearly describe this and to clarify that the property represents board-level hardware differences.

Sorry for the confusion, and thank you for the review.

Best regards,
Yasin Lee

Quoted 4 lines

Best regards,
Krzysztof

author
#13 lore raw

PATCH v2 4/4 iio: proximity: hx9023s: support firmware-name property

In reply to Yasin Lee

Add an optional firmware-name property to specify the firmware file. If not provided, the driver falls back to the default firmware name.

---
 drivers/iio/proximity/hx9023s.c | 5 ++++-
 1 file changed, 4 insertions(+), 1 deletion(-)
drivers/iio/proximity/hx9023s.c +4 -1
index e714fe00a44e..b2cd0759fec2 100644
@@ -1089,6 +1089,7 @@ static int hx9023s_probe(struct i2c_client *client)
 	struct device *dev = &client->dev;
 	struct iio_dev *indio_dev;
 	struct hx9023s_data *data;
+	const char *fw_name;
 	int ret;
 
 	indio_dev = devm_iio_device_alloc(dev, sizeof(*data));
@@ -1126,7 +1127,9 @@ static int hx9023s_probe(struct i2c_client *client)
 	if (ret)
 		return dev_err_probe(dev, ret, "channel config failed\n");
 
-	ret = request_firmware_nowait(THIS_MODULE, true, "hx9023s.bin", dev,
+	fw_name = "hx9023s.bin";
+	device_property_read_string(dev, "firmware-name", &fw_name);
+	ret = request_firmware_nowait(THIS_MODULE, true, fw_name, dev,
 				      GFP_KERNEL, data, hx9023s_cfg_update);
 	if (ret)
 		return dev_err_probe(dev, ret, "reg config failed\n");
Signature · 1 line
2.43.0
Reviewed-by on v2 4/4
#14 lore raw

Re: [PATCH v2 4/4] iio: proximity: hx9023s: support firmware-name property

Quoted 2 lines · Yasin Lee

Add an optional firmware-name property to specify the firmware file. If not provided, the driver falls back to the default firmware name.

Signature · 2 lines
With Best Regards,
Andy Shevchenko