[PATCH 09/10] iio: proximity: isl29501: Fix division by zero and overflow in isl29501_set_freq()

From: Lee Jones

Date: Thu Nov 19 2026 - 10:34:03 EST


isl29501_set_freq() computes freq = val * 1000000 + val2 % 1000000 in a
signed 32-bit int and passes it directly to do_div() without checking
for zero or negative inputs. Writing 0 to the sampling_frequency
attribute triggers a division by zero in the kernel, while valid
frequencies above 2147 Hz up to the sensor's 2222 Hz limit overflow
signed int.

Validate that val and val2 are non-negative, compute freq as an unsigned
int within [1, 2222000000], and return -ERANGE otherwise.

Fixes: 1c28799257bc ("iio: light: isl29501: Add support for the ISL29501 ToF sensor.")
Closes: https://sashiko.dev/#/bug/linux-e4bc1c30-bbf1-458a-99f9-a36e949af67c
Cc: stable@xxxxxxxxxxxxxxx
Signed-off-by: Lee Jones <lee@xxxxxxxxxx>
---
drivers/iio/proximity/isl29501.c | 10 ++++++++--
1 file changed, 8 insertions(+), 2 deletions(-)

diff --git a/drivers/iio/proximity/isl29501.c b/drivers/iio/proximity/isl29501.c
index b337ee530972..09575d890104 100644
--- a/drivers/iio/proximity/isl29501.c
+++ b/drivers/iio/proximity/isl29501.c
@@ -863,11 +863,17 @@ static int isl29501_set_calibbias(struct isl29501_private *isl29501,
static int isl29501_set_freq(struct isl29501_private *isl29501,
int val, int val2)
{
- int freq;
+ unsigned int freq;
unsigned long long sample_time;

+ if (val < 0 || val2 < 0 || val > 2222)
+ return -ERANGE;
+
/* sample_freq = 1 / (0.000450 * (sample_time + 1) * 10^-6) */
- freq = val * 1000000 + val2 % 1000000;
+ freq = val * 1000000U + val2 % 1000000U;
+ if (!freq || freq > 2222000000U)
+ return -ERANGE;
+
sample_time = 2222ULL * 1000000ULL;
do_div(sample_time, freq);

--
2.56.1.708.g77570a6a1d-goog