* [PATCH 1/5] iio: proximity: hx9023s: fix division by zero in set_samp_freq
2026-11-23 16:52 [PATCH 0/5] iio: proximity: hx9023s: fix sampling frequency, resume and event value handling Arnav Kapoor
@ 2026-11-23 16:52 ` Arnav Kapoor
2026-11-23 19:14 ` Andy Shevchenko
2026-11-23 16:52 ` [PATCH 2/5] iio: proximity: hx9023s: check the sampling frequency index Arnav Kapoor
` (3 subsequent siblings)
4 siblings, 1 reply; 9+ messages in thread
From: Arnav Kapoor @ 2026-11-23 16:52 UTC (permalink / raw)
To: Jonathan Cameron
Cc: David Lechner, Nuno Sá, Andy Shevchenko, Andy Shevchenko,
Yasin Lee, linux-iio, linux-kernel, 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().
Fixes: 60df548277b7 ("iio: proximity: Add driver support for TYHX's HX9023S capacitive proximity sensor")
Signed-off-by: Arnav Kapoor <kapoorarnav43@gmail.com>
---
drivers/iio/proximity/hx9023s.c | 4 ++--
1 file changed, 2 insertions(+), 2 deletions(-)
diff --git a/drivers/iio/proximity/hx9023s.c b/drivers/iio/proximity/hx9023s.c
index 2a6da557f22e..eaf962c29256 100644
--- a/drivers/iio/proximity/hx9023s.c
+++ b/drivers/iio/proximity/hx9023s.c
@@ -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])
--
2.53.0
^ permalink raw reply related [flat|nested] 9+ messages in thread* Re: [PATCH 1/5] iio: proximity: hx9023s: fix division by zero in set_samp_freq
2026-11-23 16:52 ` [PATCH 1/5] iio: proximity: hx9023s: fix division by zero in set_samp_freq Arnav Kapoor
@ 2026-11-23 19:14 ` Andy Shevchenko
0 siblings, 0 replies; 9+ messages in thread
From: Andy Shevchenko @ 2026-11-23 19:14 UTC (permalink / raw)
To: Arnav Kapoor
Cc: Jonathan Cameron, David Lechner, Nuno Sá, Andy Shevchenko,
Yasin Lee, linux-iio, linux-kernel
On Mon, Nov 23, 2026 at 10:22:47PM +0530, Arnav Kapoor wrote:
> 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().
...
> - 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.
> return -EINVAL;
...
> - 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.
--
With Best Regards,
Andy Shevchenko
^ permalink raw reply [flat|nested] 9+ messages in thread
* [PATCH 2/5] iio: proximity: hx9023s: check the sampling frequency index
2026-11-23 16:52 [PATCH 0/5] iio: proximity: hx9023s: fix sampling frequency, resume and event value handling Arnav Kapoor
2026-11-23 16:52 ` [PATCH 1/5] iio: proximity: hx9023s: fix division by zero in set_samp_freq Arnav Kapoor
@ 2026-11-23 16:52 ` Arnav Kapoor
2026-11-23 19:15 ` Andy Shevchenko
2026-11-23 16:52 ` [PATCH 3/5] iio: proximity: hx9023s: re-enable interrupts on resume for events Arnav Kapoor
` (2 subsequent siblings)
4 siblings, 1 reply; 9+ messages in thread
From: Arnav Kapoor @ 2026-11-23 16:52 UTC (permalink / raw)
To: Jonathan Cameron
Cc: David Lechner, Nuno Sá, Andy Shevchenko, Andy Shevchenko,
Yasin Lee, linux-iio, linux-kernel, 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")
Signed-off-by: Arnav Kapoor <kapoorarnav43@gmail.com>
---
drivers/iio/proximity/hx9023s.c | 3 +++
1 file changed, 3 insertions(+)
diff --git a/drivers/iio/proximity/hx9023s.c b/drivers/iio/proximity/hx9023s.c
index eaf962c29256..364ad2fd6095 100644
--- a/drivers/iio/proximity/hx9023s.c
+++ b/drivers/iio/proximity/hx9023s.c
@@ -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);
--
2.53.0
^ permalink raw reply related [flat|nested] 9+ messages in thread* Re: [PATCH 2/5] iio: proximity: hx9023s: check the sampling frequency index
2026-11-23 16:52 ` [PATCH 2/5] iio: proximity: hx9023s: check the sampling frequency index Arnav Kapoor
@ 2026-11-23 19:15 ` Andy Shevchenko
0 siblings, 0 replies; 9+ messages in thread
From: Andy Shevchenko @ 2026-11-23 19:15 UTC (permalink / raw)
To: Arnav Kapoor
Cc: Jonathan Cameron, David Lechner, Nuno Sá, Andy Shevchenko,
Yasin Lee, linux-iio, linux-kernel
On Mon, Nov 23, 2026 at 10:22:48PM +0530, Arnav Kapoor wrote:
> 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.
...
> 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.
--
With Best Regards,
Andy Shevchenko
^ permalink raw reply [flat|nested] 9+ messages in thread
* [PATCH 3/5] iio: proximity: hx9023s: re-enable interrupts on resume for events
2026-11-23 16:52 [PATCH 0/5] iio: proximity: hx9023s: fix sampling frequency, resume and event value handling Arnav Kapoor
2026-11-23 16:52 ` [PATCH 1/5] iio: proximity: hx9023s: fix division by zero in set_samp_freq Arnav Kapoor
2026-11-23 16:52 ` [PATCH 2/5] iio: proximity: hx9023s: check the sampling frequency index Arnav Kapoor
@ 2026-11-23 16:52 ` Arnav Kapoor
2026-11-23 16:52 ` [PATCH 4/5] iio: proximity: hx9023s: reject out-of-range threshold values Arnav Kapoor
2026-11-23 16:52 ` [PATCH 5/5] iio: proximity: hx9023s: reject out-of-range debounce values Arnav Kapoor
4 siblings, 0 replies; 9+ messages in thread
From: Arnav Kapoor @ 2026-11-23 16:52 UTC (permalink / raw)
To: Jonathan Cameron
Cc: David Lechner, Nuno Sá, Andy Shevchenko, Andy Shevchenko,
Yasin Lee, linux-iio, linux-kernel, 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")
Signed-off-by: Arnav Kapoor <kapoorarnav43@gmail.com>
---
drivers/iio/proximity/hx9023s.c | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
diff --git a/drivers/iio/proximity/hx9023s.c b/drivers/iio/proximity/hx9023s.c
index 364ad2fd6095..5b43dc9b112e 100644
--- a/drivers/iio/proximity/hx9023s.c
+++ b/drivers/iio/proximity/hx9023s.c
@@ -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;
--
2.53.0
^ permalink raw reply related [flat|nested] 9+ messages in thread* [PATCH 4/5] iio: proximity: hx9023s: reject out-of-range threshold values
2026-11-23 16:52 [PATCH 0/5] iio: proximity: hx9023s: fix sampling frequency, resume and event value handling Arnav Kapoor
` (2 preceding siblings ...)
2026-11-23 16:52 ` [PATCH 3/5] iio: proximity: hx9023s: re-enable interrupts on resume for events Arnav Kapoor
@ 2026-11-23 16:52 ` Arnav Kapoor
2026-11-23 19:26 ` Andy Shevchenko
2026-11-23 16:52 ` [PATCH 5/5] iio: proximity: hx9023s: reject out-of-range debounce values Arnav Kapoor
4 siblings, 1 reply; 9+ messages in thread
From: Arnav Kapoor @ 2026-11-23 16:52 UTC (permalink / raw)
To: Jonathan Cameron
Cc: David Lechner, Nuno Sá, Andy Shevchenko, Andy Shevchenko,
Yasin Lee, linux-iio, linux-kernel, 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")
Signed-off-by: Arnav Kapoor <kapoorarnav43@gmail.com>
---
drivers/iio/proximity/hx9023s.c | 16 ++++++++++++----
1 file changed, 12 insertions(+), 4 deletions(-)
diff --git a/drivers/iio/proximity/hx9023s.c b/drivers/iio/proximity/hx9023s.c
index 5b43dc9b112e..1545b4ed2ead 100644
--- a/drivers/iio/proximity/hx9023s.c
+++ b/drivers/iio/proximity/hx9023s.c
@@ -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);
--
2.53.0
^ permalink raw reply related [flat|nested] 9+ messages in thread* Re: [PATCH 4/5] iio: proximity: hx9023s: reject out-of-range threshold values
2026-11-23 16:52 ` [PATCH 4/5] iio: proximity: hx9023s: reject out-of-range threshold values Arnav Kapoor
@ 2026-11-23 19:26 ` Andy Shevchenko
0 siblings, 0 replies; 9+ messages in thread
From: Andy Shevchenko @ 2026-11-23 19:26 UTC (permalink / raw)
To: Arnav Kapoor
Cc: Jonathan Cameron, David Lechner, Nuno Sá, Andy Shevchenko,
Yasin Lee, linux-iio, linux-kernel
On Mon, Nov 23, 2026 at 10:22:50PM +0530, Arnav Kapoor wrote:
> 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.
...
> 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))
It's too complicated way of saying this
val >= BIT(15)
> + 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.
> reg = (ch == 4) ? HX9023S_PROX_HIGH_DIFF_CFG_CH4_0 :
> HX9023S_PROX_HIGH_DIFF_CFG_CH0_0 + (ch * 2);
--
With Best Regards,
Andy Shevchenko
^ permalink raw reply [flat|nested] 9+ messages in thread
* [PATCH 5/5] iio: proximity: hx9023s: reject out-of-range debounce values
2026-11-23 16:52 [PATCH 0/5] iio: proximity: hx9023s: fix sampling frequency, resume and event value handling Arnav Kapoor
` (3 preceding siblings ...)
2026-11-23 16:52 ` [PATCH 4/5] iio: proximity: hx9023s: reject out-of-range threshold values Arnav Kapoor
@ 2026-11-23 16:52 ` Arnav Kapoor
4 siblings, 0 replies; 9+ messages in thread
From: Arnav Kapoor @ 2026-11-23 16:52 UTC (permalink / raw)
To: Jonathan Cameron
Cc: David Lechner, Nuno Sá, Andy Shevchenko, Andy Shevchenko,
Yasin Lee, linux-iio, linux-kernel, 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")
Signed-off-by: Arnav Kapoor <kapoorarnav43@gmail.com>
---
drivers/iio/proximity/hx9023s.c | 10 ++++++++--
1 file changed, 8 insertions(+), 2 deletions(-)
diff --git a/drivers/iio/proximity/hx9023s.c b/drivers/iio/proximity/hx9023s.c
index 1545b4ed2ead..906fd4baa468 100644
--- a/drivers/iio/proximity/hx9023s.c
+++ b/drivers/iio/proximity/hx9023s.c
@@ -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)
--
2.53.0
^ permalink raw reply related [flat|nested] 9+ messages in thread