Skip to contentSkip to conversation

iio: proximity: hx9023s: fix sampling frequency, resume and event value handling

PATCH In review Arnav Kapoor 2 participants 9 messages Nov 23 IIO

lore
Share & tools
lore
#0 lore raw

cover iio: proximity: hx9023s: fix sampling frequency, resume and event value handling

Five fixes for the HX9023S capacitive proximity driver.

1: writing 67108864 (or any value for which val * 10^6 + val2 is a
multiple of 2^32) to sampling_frequency divides by zero: div_u64() takes a u32 divisor. The check added in a318cfc08537 only catches 0.
2: reading sampling_frequency indexes the 32-entry period table with
the PRF_CFG register value unchecked; the configuration file loaded by hx9023s_bin_load() can put any 8-bit value there.
3: resume re-enables the chip interrupts only when the trigger is
enabled, so threshold events stop after the first suspend.
4: out-of-range threshold values are masked into the 10-bit field
instead of being rejected (-32 becomes the largest threshold).
5: same for the 4-bit debounce fields (16 becomes 0).

Patch 2 assumes the whole PRF_CFG register is the period index, as the driver already does; I don't have the datasheet to check whether it has other fields.

Applies to both fixes-togreg and testing.
Compile-tested only (W=1 for every patch, checkpatch --strict clean); I don't have HX9023S hardware. The divisor wrap in 1 was checked with a userspace copy of the expression.

Arnav Kapoor (5):
iio: proximity: hx9023s: fix division by zero in set_samp_freq
iio: proximity: hx9023s: check the sampling frequency index
iio: proximity: hx9023s: re-enable interrupts on resume for events
iio: proximity: hx9023s: reject out-of-range threshold values
iio: proximity: hx9023s: reject out-of-range debounce values

 drivers/iio/proximity/hx9023s.c | 35 ++++++++++++++++++++++++---------
 1 file changed, 26 insertions(+), 9 deletions(-)

base-commit: 793d80b04b6c4f7f896c273567f94eccd0fac54d

Signature · 1 line
2.53.0
#1 lore raw

PATCH 1/5 iio: proximity: hx9023s: fix division by zero in set_samp_freq

hx9023s_set_samp_freq() passes val * MEGA + val2 to div_u64(), whose divisor is a u32, so the sum is truncated to 32 bits. MEGA is an unsigned long, so on 64-bit the product itself does not overflow, but the truncation does, and on 32-bit the product already wraps. Either way, any value for which val * MEGA + val2 is a multiple of 2^32 gives a zero divisor, e.g.

echo 67108864 > sampling_frequency

(67108864 * 10^6 = 15625 * 2^32) triggers a divide error, on both 32-bit and 64-bit architectures. Negative values wrap the same way (-67108864 also gives 0), which the check added by commit a318cfc08537 ("iio:
proximity: hx9023s: Protect against division by zero in set_samp_freq") does not catch, as it only rejects val == 0 && val2 == 0.

Reject negative values and do the division in 64 bits with div64_u64().

Fixes 60df548277b7 ("iio: proximity: Add driver support for TYHX's HX9023S capacitive proximity sensor")
---
 drivers/iio/proximity/hx9023s.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
drivers/iio/proximity/hx9023s.c +2 -2
index 2a6da557f22e..eaf962c29256 100644
@@ -718,10 +718,10 @@ 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;
 
-	if (!val && !val2)
+	if (val < 0 || val2 < 0 || (!val && !val2))
 		return -EINVAL;
 
-	period_ms = div_u64(NANO, (val * MEGA + val2));
+	period_ms = div64_u64(NANO, (u64)val * MEGA + val2);
 
 	for (i = 0; i < ARRAY_SIZE(hx9023s_samp_freq_table); i++) {
 		if (period_ms == hx9023s_samp_freq_table[i])
Signature · 1 line
2.53.0
#2 lore raw

Re: [PATCH 1/5] iio: proximity: hx9023s: fix division by zero in set_samp_freq

Quoted 16 lines · Arnav Kapoor

hx9023s_set_samp_freq() passes val * MEGA + val2 to div_u64(), whose divisor is a u32, so the sum is truncated to 32 bits. MEGA is an unsigned long, so on 64-bit the product itself does not overflow, but the truncation does, and on 32-bit the product already wraps. Either way, any value for which val * MEGA + val2 is a multiple of 2^32 gives a zero divisor, e.g.

echo 67108864 > sampling_frequency

(67108864 * 10^6 = 15625 * 2^32) triggers a divide error, on both 32-bit and 64-bit architectures. Negative values wrap the same way (-67108864 also gives 0), which the check added by commit a318cfc08537 ("iio:
proximity: hx9023s: Protect against division by zero in set_samp_freq") does not catch, as it only rejects val == 0 && val2 == 0.

Reject negative values and do the division in 64 bits with div64_u64().

...

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

Quoted 2 lines
drivers/iio/proximity/hx9023s.c +1 -1
-	if (!val && !val2)
+	if (val < 0 || val2 < 0 || (!val && !val2))

I think in this case the

	if (val < 0 || val2 < 0 || (val == 0 && val2 == 0))

is slightly better as the consistent style in the same expression is used.

Quoted 1 line

return -EINVAL;

...

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

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

LOL! Why is this 64-bit division?! Anything more than NANO will give 0, no? And NANO is 32-bit value.

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

PATCH 2/5 iio: proximity: hx9023s: check the sampling frequency index

In reply to Arnav Kapoor

hx9023s_get_samp_freq() uses the PRF_CFG register value as an index into hx9023s_samp_freq_table[], which has 32 entries, without checking it. The driver only writes valid indexes itself, but the configuration file loaded by hx9023s_bin_load() writes arbitrary register/value pairs, so PRF_CFG can hold any 8-bit value. Reading sampling_frequency then reads past the end of the table and divides KILO by whatever it finds there, which may be zero.

Return -EINVAL if the index is outside the table.

Fixes e9ed97be4fcc ("iio: proximity: hx9023s: Added firmware file parsing functionality")
---
 drivers/iio/proximity/hx9023s.c | 3 +++
 1 file changed, 3 insertions(+)
drivers/iio/proximity/hx9023s.c +3 -0
index eaf962c29256..364ad2fd6095 100644
@@ -681,6 +681,9 @@ static int hx9023s_get_samp_freq(struct hx9023s_data *data, int *val, int *val2)
 	if (ret)
 		return ret;
 
+	if (index >= ARRAY_SIZE(hx9023s_samp_freq_table))
+		return -EINVAL;
+
 	odr = hx9023s_samp_freq_table[index];
 	*val = KILO / odr;
 	*val2 = div_u64((KILO % odr) * MICRO, odr);
Signature · 1 line
2.53.0
#4 lore raw

Re: [PATCH 2/5] iio: proximity: hx9023s: check the sampling frequency index

Quoted 9 lines · Arnav Kapoor

hx9023s_get_samp_freq() uses the PRF_CFG register value as an index into hx9023s_samp_freq_table[], which has 32 entries, without checking it. The driver only writes valid indexes itself, but the configuration file loaded by hx9023s_bin_load() writes arbitrary register/value pairs, so PRF_CFG can hold any 8-bit value. Reading sampling_frequency then reads past the end of the table and divides KILO by whatever it finds there, which may be zero.

Return -EINVAL if the index is outside the table.

...

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

Quoted 3 lines
drivers/iio/proximity/hx9023s.c +0 -0
 	odr = hx9023s_samp_freq_table[index];
 	*val = KILO / odr;
 	*val2 = div_u64((KILO % odr) * MICRO, odr);

Side note: Also here, strange to see 64-bit division where it's not required.

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

PATCH 3/5 iio: proximity: hx9023s: re-enable interrupts on resume for events

In reply to Arnav Kapoor

The chip interrupts are enabled at probe (INTERRUPT_CFG = 0xff in the register defaults) and stay enabled while only threshold events are in use. hx9023s_suspend() disables them, but hx9023s_resume() re-enables them only if the data-ready trigger is enabled. With events enabled and no buffer running, the interrupts therefore stay off after the first suspend/resume cycle and no proximity event is reported any more, until a buffer is started.

Re-enable the interrupts on resume if any event is enabled as well.

Fixes 60df548277b7 ("iio: proximity: Add driver support for TYHX's HX9023S capacitive proximity sensor")
---
 drivers/iio/proximity/hx9023s.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/iio/proximity/hx9023s.c +1 -1
index 364ad2fd6095..5b43dc9b112e 100644
@@ -1185,7 +1185,7 @@ static int hx9023s_resume(struct device *dev)
 	struct hx9023s_data *data = iio_priv(dev_get_drvdata(dev));
 
 	guard(mutex)(&data->mutex);
-	if (data->trigger_enabled)
+	if (data->trigger_enabled || data->chan_event)
 		hx9023s_interrupt_enable(data);
 
 	return 0;
Signature · 1 line
2.53.0
#6 lore raw

PATCH 4/5 iio: proximity: hx9023s: reject out-of-range threshold values

In reply to Arnav Kapoor

The proximity thresholds are 10-bit register fields in units of 32. hx9023s_set_thres_near() and hx9023s_set_thres_far() mask val / 32 with GENMASK(9, 0) instead of checking it, so a value out of range is silently written as a different threshold: 32768 becomes 0, the most sensitive threshold, and -32 becomes 1023 * 32 = 32736, the least sensitive one.

Return -EINVAL for negative values and values above 32767, the largest value that still maps to the field.

Fixes 60df548277b7 ("iio: proximity: Add driver support for TYHX's HX9023S capacitive proximity sensor")
---
 drivers/iio/proximity/hx9023s.c | 16 ++++++++++++----
 1 file changed, 12 insertions(+), 4 deletions(-)
drivers/iio/proximity/hx9023s.c +12 -4
index 5b43dc9b112e..1545b4ed2ead 100644
@@ -441,10 +441,14 @@ static int hx9023s_get_thres_far(struct hx9023s_data *data, u8 ch, int *val)
 
 static int hx9023s_set_thres_near(struct hx9023s_data *data, u8 ch, int val)
 {
-	__le16 val_le16 = cpu_to_le16((val / 32) & GENMASK(9, 0));
+	__le16 val_le16;
 	unsigned int reg;
 
-	data->ch_data[ch].thres.near = ((val / 32) & GENMASK(9, 0)) * 32;
+	if (val < 0 || val / 32 > GENMASK(9, 0))
+		return -EINVAL;
+
+	val_le16 = cpu_to_le16(val / 32);
+	data->ch_data[ch].thres.near = val / 32 * 32;
 	reg = (ch == 4) ? HX9023S_PROX_HIGH_DIFF_CFG_CH4_0 :
 		HX9023S_PROX_HIGH_DIFF_CFG_CH0_0 + (ch * 2);
 
@@ -453,10 +457,14 @@ static int hx9023s_set_thres_near(struct hx9023s_data *data, u8 ch, int val)
 
 static int hx9023s_set_thres_far(struct hx9023s_data *data, u8 ch, int val)
 {
-	__le16 val_le16 = cpu_to_le16((val / 32) & GENMASK(9, 0));
+	__le16 val_le16;
 	unsigned int reg;
 
-	data->ch_data[ch].thres.far = ((val / 32) & GENMASK(9, 0)) * 32;
+	if (val < 0 || val / 32 > GENMASK(9, 0))
+		return -EINVAL;
+
+	val_le16 = cpu_to_le16(val / 32);
+	data->ch_data[ch].thres.far = val / 32 * 32;
 	reg = (ch == 4) ? HX9023S_PROX_LOW_DIFF_CFG_CH4_0 :
 		HX9023S_PROX_LOW_DIFF_CFG_CH0_0 + (ch * 2);
 
Signature · 1 line
2.53.0
#7 lore raw

Re: [PATCH 4/5] iio: proximity: hx9023s: reject out-of-range threshold values

Quoted 9 lines · Arnav Kapoor

The proximity thresholds are 10-bit register fields in units of 32. hx9023s_set_thres_near() and hx9023s_set_thres_far() mask val / 32 with GENMASK(9, 0) instead of checking it, so a value out of range is silently written as a different threshold: 32768 becomes 0, the most sensitive threshold, and -32 becomes 1023 * 32 = 32736, the least sensitive one.

Return -EINVAL for negative values and values above 32767, the largest value that still maps to the field.

...

drivers/iio/proximity/hx9023s.c static int hx9023s_get_thres_far(struct hx9023s_data *data, u8 ch, int *val) · line 447

Quoted 8 lines

static int hx9023s_set_thres_near(struct hx9023s_data *data, u8 ch, int val) {

drivers/iio/proximity/hx9023s.c +2 -2
-	__le16 val_le16 = cpu_to_le16((val / 32) & GENMASK(9, 0));
+	__le16 val_le16;
 	unsigned int reg;
 
-	data->ch_data[ch].thres.near = ((val / 32) & GENMASK(9, 0)) * 32;
+	if (val < 0 || val / 32 > GENMASK(9, 0))

It's too complicated way of saying this

	val >= BIT(15)

drivers/iio/proximity/hx9023s.c static int hx9023s_get_thres_far(struct hx9023s_data *data, u8 ch, int *val) · line 451

Quoted 4 lines
drivers/iio/proximity/hx9023s.c +4 -0
+		return -EINVAL;
+
+	val_le16 = cpu_to_le16(val / 32);
+	data->ch_data[ch].thres.near = val / 32 * 32;

& ~GENMASK(4, 0)

In any case it might need a short comment explaining why these bits are dropped.

Quoted 2 lines
drivers/iio/proximity/hx9023s.c +0 -0
 	reg = (ch == 4) ? HX9023S_PROX_HIGH_DIFF_CFG_CH4_0 :
 		HX9023S_PROX_HIGH_DIFF_CFG_CH0_0 + (ch * 2);
Signature · 2 lines
With Best Regards,
Andy Shevchenko
#8 lore raw

PATCH 5/5 iio: proximity: hx9023s: reject out-of-range debounce values

In reply to Arnav Kapoor

The near and far debounce counts are 4-bit fields. hx9023s_write_far_debounce() and hx9023s_write_near_debounce() pass the value through FIELD_GET(), which only masks it here because the field starts at bit 0, so an out-of-range value is silently written as a different one: 16 becomes 0 (no debounce) and -1 becomes 15.

Return -EINVAL for values outside 0..15, and use FIELD_PREP(), which is the right helper for building a register value.

Fixes 60df548277b7 ("iio: proximity: Add driver support for TYHX's HX9023S capacitive proximity sensor")
---
 drivers/iio/proximity/hx9023s.c | 10 ++++++++--
 1 file changed, 8 insertions(+), 2 deletions(-)
drivers/iio/proximity/hx9023s.c +8 -2
index 1545b4ed2ead..906fd4baa468 100644
@@ -359,18 +359,24 @@ static int hx9023s_ch_cfg(struct hx9023s_data *data)
 
 static int hx9023s_write_far_debounce(struct hx9023s_data *data, int val)
 {
+	if (val < 0 || val > FIELD_MAX(HX9023S_PROX_DEBOUNCE_MASK))
+		return -EINVAL;
+
 	guard(mutex)(&data->mutex);
 	return regmap_update_bits(data->regmap, HX9023S_PROX_INT_LOW_CFG,
 				HX9023S_PROX_DEBOUNCE_MASK,
-				FIELD_GET(HX9023S_PROX_DEBOUNCE_MASK, val));
+				FIELD_PREP(HX9023S_PROX_DEBOUNCE_MASK, val));
 }
 
 static int hx9023s_write_near_debounce(struct hx9023s_data *data, int val)
 {
+	if (val < 0 || val > FIELD_MAX(HX9023S_PROX_DEBOUNCE_MASK))
+		return -EINVAL;
+
 	guard(mutex)(&data->mutex);
 	return regmap_update_bits(data->regmap, HX9023S_PROX_INT_HIGH_CFG,
 				HX9023S_PROX_DEBOUNCE_MASK,
-				FIELD_GET(HX9023S_PROX_DEBOUNCE_MASK, val));
+				FIELD_PREP(HX9023S_PROX_DEBOUNCE_MASK, val));
 }
 
 static int hx9023s_read_far_debounce(struct hx9023s_data *data, int *val)
Signature · 1 line
2.53.0