Skip to contentSkip to conversation

iio: Fix memory corruption, teardown races and bounds checks

PATCH In review Lee Jones 2 participants 10 messages Nov 23

Subsystems (7)
lore
Share & tools
lore

Lee Jones

author
#0 lore raw

v2 cover iio: Fix memory corruption, teardown races and bounds checks

These issues were reported by Sashiko.

None of these changes have been tested on physical hardware.

v1 => v2:
- Drop the 3 patches already applied upstream (stm32-adc x2, tsl2591)
- Rebase onto fixes-togreg
- CC Radu Sabau on the adis patch (Jonathan)
- adxl313: Return -EIO on excessive FIFO entry count and -ERANGE on sub-base ODR codes (Andy, Jonathan)
- pac1934: Order local variables in reverse Christmas tree style and scope the loop index j to the for-loop (Andy, Jonathan)
- qcom-spmi-adc5-gen3: Manage tm_props and the OF node reference via adc5_gen3_aux_device_release(), register auxiliary_device_destroy() with devm_add_action_or_reset(), and return -ENOENT on an empty reg property (Andy, Jonathan)
- isl29501: Use HZ_PER_MHZ, group range checks by variable, and order local declarations in reverse Christmas tree style (Andy)

v1: https://lore.kernel.org/all/[email protected]/

Cc Alexandru Ardelean <[email protected]>
Cc Andy Shevchenko <[email protected]>
Cc Bogdan Bolocan <[email protected]>
Cc David Lechner <[email protected]>
Cc Jishnu Prakash <[email protected]>
Cc Jonathan Cameron <[email protected]>
Cc Lars-Peter Clausen <[email protected]>
Cc Lothar Rubusch <[email protected]>
Cc Lucas Stankus <[email protected]>
Cc Marius Cristea <[email protected]>
Cc Mathieu Othacehe <[email protected]>
Cc Michael Hennerich <[email protected]>
Cc Nuno Sá <[email protected]>
Cc Radu Sabau <[email protected]>

Lee Jones (7):
iio: imu: adis: Clear pointers after freeing in
adis_update_scan_mode()
iio: accel: adxl313: Prevent FIFO buffer overflow in

    adxl313_fifo_transfer()
  iio: accel: adxl313: Reject sub-base ODR codes in adxl313_read_raw()
  iio: adc: pac1934: Fix custom attribute allocation and channel bounds
  iio: adc: qcom-spmi-adc5-gen3: Free TM auxiliary device wrapper in
    release callback
  iio: adc: qcom-spmi-adc5-gen3: Reject empty reg property in probe
  iio: proximity: isl29501: Fix division by zero and overflow in
    isl29501_set_freq()
 drivers/iio/accel/adxl313.h           |  2 +-
 drivers/iio/accel/adxl313_core.c      | 13 +++++-
 drivers/iio/adc/pac1934.c             | 33 +++++++--------
 drivers/iio/adc/qcom-spmi-adc5-gen3.c | 59 +++++++++++++--------------
 drivers/iio/imu/adis_buffer.c         |  2 +
 drivers/iio/proximity/isl29501.c      | 13 ++++--
 6 files changed, 68 insertions(+), 54 deletions(-)
Signature · 1 line
2.56.1.750.gf70dc682cf-goog
author
#1 lore raw

PATCH v2 1/7 iio: imu: adis: Clear pointers after freeing in adis_update_scan_mode()

adis_update_scan_mode() frees both adis->xfer and adis->buffer at the start of a scan mode update without clearing either pointer to NULL.

In both adis_update_scan_mode_burst() and the non-burst path, adis->xfer is allocated before adis->buffer. If the adis->xfer allocation fails, the function returns -ENOMEM while adis->buffer still holds the freed pointer from the previous scan configuration, causing a double-free on the next scan mode update or during device teardown in adis_buffer_cleanup().

Clear both pointers immediately after freeing them.

Fixes aacff892cbd5 ("staging:iio:adis: Preallocate transfer message")

---

Cc Radu Sabau <[email protected]>

v1 => v2:
- CC Radu Sabau (Jonathan)

 drivers/iio/imu/adis_buffer.c | 2 ++
 1 file changed, 2 insertions(+)
drivers/iio/imu/adis_buffer.c +2 -0
index 501da4eaef69..9d317b627eba 100644
@@ -74,7 +74,9 @@ int adis_update_scan_mode(struct iio_dev *indio_dev,
 	__be16 *tx, *rx;
 
 	kfree(adis->xfer);
+	adis->xfer = NULL;
 	kfree(adis->buffer);
+	adis->buffer = NULL;
 
 	if (adis->data->burst_len)
 		return adis_update_scan_mode_burst(indio_dev, scan_mask);
Signature · 1 line
2.56.1.750.gf70dc682cf-goog
author
#2 lore raw

PATCH v2 2/7 iio: accel: adxl313: Prevent FIFO buffer overflow in adxl313_fifo_transfer()

In reply to Lee Jones

The ADXL313 32-entry FIFO plus one sample held in the output filter can yield up to 33 3-axis samples (ADXL313_FIFO_SIZE + 1). Because of missing parentheses in ADXL313_NUM_AXIS * ADXL313_FIFO_SIZE + 1, fifo_buf is sized at 97 16-bit elements instead of 99, overflowing the buffer by four bytes whenever all 33 entries are read.

In addition, adxl313_get_samples() returns the 6-bit FIFO_STATUS entries field (0..63) without bounds checking, allowing a malfunctioning or spoofed bus device to overflow fifo_buf on the heap in adxl313_fifo_transfer().

Parenthesise (ADXL313_FIFO_SIZE + 1) when sizing fifo_buf and reject entry counts exceeding ADXL313_FIFO_SIZE + 1 with -EIO in adxl313_get_samples().

Fixes ff8093fa6ba4 ("iio: accel: adxl313: add buffered FIFO watermark with interrupt handling")

---

v1 => v2:
- Return -EIO instead of -EINVAL when FIFO entries exceed ADXL313_FIFO_SIZE + 1 (Andy, Jonathan)

 drivers/iio/accel/adxl313.h      | 2 +-
 drivers/iio/accel/adxl313_core.c | 7 ++++++-
 2 files changed, 7 insertions(+), 2 deletions(-)
drivers/iio/accel/adxl313.h +1 -1
index 75ef54b60f75..de0bccfcdd0a 100644
@@ -93,7 +93,7 @@ struct adxl313_data {
 	struct mutex	lock; /* lock to protect transf_buf */
 	u8 watermark;
 	__le16		transf_buf __aligned(IIO_DMA_MINALIGN);
-	__le16		fifo_buf[ADXL313_NUM_AXIS * ADXL313_FIFO_SIZE + 1];
+	__le16		fifo_buf[ADXL313_NUM_AXIS * (ADXL313_FIFO_SIZE + 1)];
 };
 
 struct adxl313_chip_info {
drivers/iio/accel/adxl313_core.c +6 -1
index 6dc918c4ae17..dd0b3efd594e 100644
@@ -921,7 +921,12 @@ static int adxl313_get_samples(struct adxl313_data *data)
 	if (ret)
 		return ret;
 
-	return FIELD_GET(ADXL313_REG_FIFO_STATUS_ENTRIES_MSK, regval);
+	/* 32 FIFO entries plus 1 entry at the output filter */
+	ret = FIELD_GET(ADXL313_REG_FIFO_STATUS_ENTRIES_MSK, regval);
+	if (ret > ADXL313_FIFO_SIZE + 1)
+		return -EIO;
+
+	return ret;
 }
 
 static int adxl313_fifo_transfer(struct adxl313_data *data, int samples)
Signature · 1 line
2.56.1.750.gf70dc682cf-goog
author
#3 lore raw

PATCH v2 3/7 iio: accel: adxl313: Reject sub-base ODR codes in adxl313_read_raw()

In reply to Lee Jones

When reading IIO_CHAN_INFO_SAMP_FREQ, adxl313_read_raw() extracts the 4-bit rate field (0..15) from ADXL313_REG_BW_RATE and subtracts ADXL313_RATE_BASE (6) to index adxl313_odr_freqs[].

ADXL312 and ADXL314 support sub-6.25 Hz rate codes 0..5 and do not perform a soft reset during setup, and debugfs register writes or a faulty bus read can likewise yield codes below ADXL313_RATE_BASE. When that happens, subtracting ADXL313_RATE_BASE produces a negative index and reads before the start of adxl313_odr_freqs[].

Return -ERANGE if the rate field is below ADXL313_RATE_BASE.

Fixes 636d44633039 ("iio: accel: Add driver support for ADXL313")

---

v1 => v2:
- Return -ERANGE instead of -EINVAL for sub-base ODR codes (Andy)

 drivers/iio/accel/adxl313_core.c | 6 +++++-
 1 file changed, 5 insertions(+), 1 deletion(-)
drivers/iio/accel/adxl313_core.c +5 -1
index dd0b3efd594e..aaab1935ac60 100644
@@ -647,7 +647,11 @@ static int adxl313_read_raw(struct iio_dev *indio_dev,
 		if (ret)
 			return ret;
 
-		ret = FIELD_GET(ADXL313_RATE_MSK, regval) - ADXL313_RATE_BASE;
+		regval = FIELD_GET(ADXL313_RATE_MSK, regval);
+		if (regval < ADXL313_RATE_BASE)
+			return -ERANGE;
+
+		ret = regval - ADXL313_RATE_BASE;
 		*val = adxl313_odr_freqs[ret][0];
 		*val2 = adxl313_odr_freqs[ret][1];
 		return IIO_VAL_INT_PLUS_MICRO;
Signature · 1 line
2.56.1.750.gf70dc682cf-goog
author
#4 lore raw

PATCH v2 4/7 iio: adc: pac1934: Fix custom attribute allocation and channel bounds

In reply to Lee Jones

pac1934_prep_custom_attributes() sizes the pac1934_custom_attr pointer array using sizeof(*pac1934_group) instead of sizeof(*pac1934_custom_attr) and adds 1 byte rather than one pointer element for the terminating NULL entry. When active_channels_count is zero, only a single byte is allocated and iio_device_register_sysfs() reads past the allocation when scanning the attribute list.

In addition, active_channels_count is counted over info->phys_channels, whereas the population steps unconditionally inspect all four entries of info->active_channels[], which can write past the end of the array on 1-, 2-, and 3-channel chip variants.

Allocate active_channels_count + 1 attribute pointers with devm_kzalloc_objs() and bound the population loop by info->phys_channels.

Fixes 0fb528c8255b ("iio: adc: adding support for PAC193x")

---

v1 => v2:
- Order local variables in reverse Christmas tree style (Andy, Jonathan)
- Scope loop index j to the for-loop (Andy, Jonathan)

 drivers/iio/adc/pac1934.c | 33 +++++++++++++++------------------
 1 file changed, 15 insertions(+), 18 deletions(-)
drivers/iio/adc/pac1934.c +15 -18
index 5e901ab25c8e..a307689c6814 100644
@@ -1435,13 +1435,20 @@ static IIO_DEVICE_ATTR(in_shunt_resistor3, 0644,
 static IIO_DEVICE_ATTR(in_shunt_resistor4, 0644,
 		       pac1934_shunt_value_show, pac1934_shunt_value_store, 3);
 
+static struct attribute * const pac1934_shunt_attrs[] = {
+	PAC1934_DEV_ATTR(in_shunt_resistor1),
+	PAC1934_DEV_ATTR(in_shunt_resistor2),
+	PAC1934_DEV_ATTR(in_shunt_resistor3),
+	PAC1934_DEV_ATTR(in_shunt_resistor4),
+};
+
 static int pac1934_prep_custom_attributes(struct pac1934_chip_info *info,
 					  struct iio_dev *indio_dev)
 {
-	int i, active_channels_count = 0;
+	struct device *dev = &info->client->dev;
 	struct attribute **pac1934_custom_attr;
 	struct attribute_group *pac1934_group;
-	struct device *dev = &info->client->dev;
+	int i, active_channels_count = 0;
 
 	for (i = 0 ; i < info->phys_channels; i++)
 		if (info->active_channels[i])
@@ -1451,26 +1458,16 @@ static int pac1934_prep_custom_attributes(struct pac1934_chip_info *info,
 	if (!pac1934_group)
 		return -ENOMEM;
 
-	pac1934_custom_attr = devm_kzalloc(dev,
-					   (PAC1934_CUSTOM_ATTR_FOR_CHANNEL *
-					   active_channels_count)
-					   * sizeof(*pac1934_group) + 1,
-					   GFP_KERNEL);
+	pac1934_custom_attr = devm_kzalloc_objs(dev, *pac1934_custom_attr,
+						PAC1934_CUSTOM_ATTR_FOR_CHANNEL *
+						active_channels_count + 1);
 	if (!pac1934_custom_attr)
 		return -ENOMEM;
 
 	i = 0;
-	if (info->active_channels[0])
-		pac1934_custom_attr[i++] = PAC1934_DEV_ATTR(in_shunt_resistor1);
-
-	if (info->active_channels[1])
-		pac1934_custom_attr[i++] = PAC1934_DEV_ATTR(in_shunt_resistor2);
-
-	if (info->active_channels[2])
-		pac1934_custom_attr[i++] = PAC1934_DEV_ATTR(in_shunt_resistor3);
-
-	if (info->active_channels[3])
-		pac1934_custom_attr[i] = PAC1934_DEV_ATTR(in_shunt_resistor4);
+	for (int j = 0; j < info->phys_channels; j++)
+		if (info->active_channels[j])
+			pac1934_custom_attr[i++] = pac1934_shunt_attrs[j];
 
 	pac1934_group->attrs = pac1934_custom_attr;
 	info->iio_info.attrs = pac1934_group;
Signature · 1 line
2.56.1.750.gf70dc682cf-goog
#5 lore raw

Re: [PATCH v2 4/7] iio: adc: pac1934: Fix custom attribute allocation and channel bounds

Quoted 11 lines · Lee Jones

pac1934_prep_custom_attributes() sizes the pac1934_custom_attr pointer array using sizeof(*pac1934_group) instead of sizeof(*pac1934_custom_attr) and adds 1 byte rather than one pointer element for the terminating NULL entry. When active_channels_count is zero, only a single byte is allocated and iio_device_register_sysfs() reads past the allocation when scanning the attribute list.

In addition, active_channels_count is counted over info->phys_channels, whereas the population steps unconditionally inspect all four entries of info->active_channels[], which can write past the end of the array on 1-, 2-, and 3-channel chip variants.

...

Quoted 2 lines

- Order local variables in reverse Christmas tree style (Andy, Jonathan)

Oh, I meant only this for the case when we touch the content. Since now it's not changed there, no need to do it, probably?

...

drivers/iio/adc/pac1934.c static IIO_DEVICE_ATTR(in_shunt_resistor3, 0644, · line 1451

Quoted 9 lines

static int pac1934_prep_custom_attributes(struct pac1934_chip_info *info,

drivers/iio/adc/pac1934.c +2 -2
 					  struct iio_dev *indio_dev)
 {
-	int i, active_channels_count = 0;
+	struct device *dev = &info->client->dev;
 	struct attribute **pac1934_custom_attr;
 	struct attribute_group *pac1934_group;
-	struct device *dev = &info->client->dev;
+	int i, active_channels_count = 0;

It took me a while to understand why, but no need to resend, if Jonathan is not okay with that, this hunk may not be applied while the rest is fine.

TL;DR:
Up to Jonathan (but personally I would leave this as in the original code).

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

PATCH v2 5/7 iio: adc: qcom-spmi-adc5-gen3: Free TM auxiliary device wrapper in release callback

In reply to Lee Jones

struct tm5_aux_dev_wrapper embeds a reference-counted struct auxiliary_device, whose memory must be released from its dev.release callback once the final device reference is dropped.

Allocating the wrapper and its tm_props array with parent-managed devm_*() helpers and providing an empty adc5_gen3_aux_device_release() causes parent devres teardown to free both allocations immediately when the parent unbinds, even if external references to the embedded struct device are still held. It also leaks the OF node reference taken by device_set_of_node_from_dev().

Allocate the wrapper and tm_props array without devm, release them together with the OF node reference in adc5_gen3_aux_device_release(), and register auxiliary_device_destroy() via devm_add_action_or_reset() once the auxiliary device has been added.

Fixes baff45179e90 ("iio: adc: Add support for QCOM PMIC5 Gen3 ADC")

---

v1 => v2:
- Allocate tm_props without devm and release it alongside the OF node reference in adc5_gen3_aux_device_release() (Jonathan)
- Move auxiliary_device_init() earlier and register auxiliary_device_destroy() via devm_add_action_or_reset(), avoiding devm_release_action() (Andy, Jonathan)
- Note: <linux/container_of.h> is already included

 drivers/iio/adc/qcom-spmi-adc5-gen3.c | 57 +++++++++++++--------------
 1 file changed, 27 insertions(+), 30 deletions(-)
drivers/iio/adc/qcom-spmi-adc5-gen3.c +27 -30
index a23eb5c9096d..85450aa37e65 100644
@@ -21,9 +21,11 @@
 #include <linux/kernel.h>
 #include <linux/module.h>
 #include <linux/mutex.h>
+#include <linux/of.h>
 #include <linux/platform_device.h>
 #include <linux/property.h>
 #include <linux/regmap.h>
+#include <linux/slab.h>
 #include <linux/types.h>
 #include <linux/unaligned.h>
 
@@ -598,24 +600,22 @@ static int adc5_get_fw_data(struct adc5_chip *adc)
 	return 0;
 }
 
-static void adc5_gen3_uninit_aux(void *data)
+static void adc5_gen3_aux_device_release(struct device *dev)
 {
-	auxiliary_device_uninit(data);
-}
+	struct tm5_aux_dev_wrapper *aux_device =
+		container_of(dev, struct tm5_aux_dev_wrapper, aux_dev.dev);
 
-static void adc5_gen3_delete_aux(void *data)
-{
-	auxiliary_device_delete(data);
+	of_node_put(dev->of_node);
+	kfree(aux_device->tm_props);
+	kfree(aux_device);
 }
 
-static void adc5_gen3_aux_device_release(struct device *dev) {}
-
 static int adc5_gen3_add_aux_tm_device(struct adc5_chip *adc, int id)
 {
 	struct tm5_aux_dev_wrapper *aux_device;
 	int i, ret, i_tm = 0;
 
-	aux_device = devm_kzalloc(adc->dev, sizeof(*aux_device), GFP_KERNEL);
+	aux_device = kzalloc_obj(*aux_device);
 	if (!aux_device)
 		return -ENOMEM;
 
@@ -624,11 +624,20 @@ static int adc5_gen3_add_aux_tm_device(struct adc5_chip *adc, int id)
 	aux_device->aux_dev.dev.parent = adc->dev;
 	aux_device->aux_dev.dev.release = adc5_gen3_aux_device_release;
 
-	aux_device->tm_props = devm_kcalloc(adc->dev, adc->n_tm_channels,
-					    sizeof(*aux_device->tm_props),
-					    GFP_KERNEL);
-	if (!aux_device->tm_props)
+	ret = auxiliary_device_init(&aux_device->aux_dev);
+	if (ret) {
+		kfree(aux_device);
+		return ret;
+	}
+
+	device_set_of_node_from_dev(&aux_device->aux_dev.dev, adc->dev);
+
+	aux_device->tm_props = kzalloc_objs(*aux_device->tm_props,
+					    adc->n_tm_channels);
+	if (!aux_device->tm_props) {
+		auxiliary_device_uninit(&aux_device->aux_dev);
 		return -ENOMEM;
+	}
 
 	aux_device->dev_data = &adc->dev_data;
 
@@ -639,28 +648,16 @@ static int adc5_gen3_add_aux_tm_device(struct adc5_chip *adc, int id)
 		i_tm++;
 	}
 
-	device_set_of_node_from_dev(&aux_device->aux_dev.dev, adc->dev);
-
 	aux_device->n_tm_channels = adc->n_tm_channels;
 
-	ret = auxiliary_device_init(&aux_device->aux_dev);
-	if (ret)
-		return ret;
-
-	ret = devm_add_action_or_reset(adc->dev, adc5_gen3_uninit_aux,
-				       &aux_device->aux_dev);
-	if (ret)
-		return ret;
-
 	ret = auxiliary_device_add(&aux_device->aux_dev);
-	if (ret)
-		return ret;
-	ret = devm_add_action_or_reset(adc->dev, adc5_gen3_delete_aux,
-				       &aux_device->aux_dev);
-	if (ret)
+	if (ret) {
+		auxiliary_device_uninit(&aux_device->aux_dev);
 		return ret;
+	}
 
-	return 0;
+	return devm_add_action_or_reset(adc->dev, auxiliary_device_destroy,
+					&aux_device->aux_dev);
 }
 
 void adc5_gen3_mutex_lock(struct device *dev)
Signature · 1 line
2.56.1.750.gf70dc682cf-goog
author
#7 lore raw

PATCH v2 6/7 iio: adc: qcom-spmi-adc5-gen3: Reject empty reg property in probe

In reply to Lee Jones

If a firmware node supplies an empty reg property, device_property_count_u32() returns 0, which passes the negative error check in adc5_gen3_probe() and leaves num_sdams set to 0. Subsequent zero-length allocations return ZERO_SIZE_PTR, which is then dereferenced when requesting the VADC SDAM interrupt, and num_sdams * 8 - 1 wraps in unsigned comparisons when counting TM channels.

Return -ENOENT when device_property_count_u32() reports zero elements.

Fixes baff45179e90 ("iio: adc: Add support for QCOM PMIC5 Gen3 ADC")

---

v1 => v2:
- Return -ENOENT instead of -EINVAL when reg is empty (Andy)

 drivers/iio/adc/qcom-spmi-adc5-gen3.c | 2 ++
 1 file changed, 2 insertions(+)
drivers/iio/adc/qcom-spmi-adc5-gen3.c +2 -0
index 85450aa37e65..8e316aa88f48 100644
@@ -736,6 +736,8 @@ static int adc5_gen3_probe(struct platform_device *pdev)
 	ret = device_property_count_u32(dev, "reg");
 	if (ret < 0)
 		return ret;
+	if (!ret)
+		return -ENOENT;
 
 	adc->dev_data.num_sdams = ret;
 
Signature · 1 line
2.56.1.750.gf70dc682cf-goog
author
#8 lore raw

PATCH v2 7/7 iio: proximity: isl29501: Fix division by zero and overflow in isl29501_set_freq()

In reply to Lee Jones

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 using HZ_PER_MHZ as an unsigned int within [1, 2222 * HZ_PER_MHZ], and return -ERANGE otherwise.

Fixes 1c28799257bc ("iio: light: isl29501: Add support for the ISL29501 ToF sensor.")

---

v1 => v2:
- Order local variables in reverse Christmas tree style (Andy)
- Group range checks by variable (Andy)
- Use HZ_PER_MHZ from <linux/units.h> (Andy)

 drivers/iio/proximity/isl29501.c | 13 ++++++++++---
 1 file changed, 10 insertions(+), 3 deletions(-)
drivers/iio/proximity/isl29501.c +10 -3
index a98a9975323c..43be78b82e50 100644
@@ -18,6 +18,7 @@
 #include <linux/iio/trigger_consumer.h>
 #include <linux/iio/buffer.h>
 #include <linux/iio/triggered_buffer.h>
+#include <linux/units.h>
 
 /* Control, setting and status registers */
 #define ISL29501_DEVICE_ID			0x00
@@ -863,12 +864,18 @@ 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 long long sample_time;
+	unsigned int freq;
+
+	if (val < 0 || val > 2222 || val2 < 0)
+		return -ERANGE;
 
 	/* sample_freq = 1 / (0.000450 * (sample_time + 1) * 10^-6) */
-	freq = val * 1000000 + val2 % 1000000;
-	sample_time = 2222ULL * 1000000ULL;
+	freq = val * HZ_PER_MHZ + val2 % HZ_PER_MHZ;
+	if (!freq || freq > 2222U * HZ_PER_MHZ)
+		return -ERANGE;
+
+	sample_time = 2222ULL * HZ_PER_MHZ;
 	do_div(sample_time, freq);
 
 	sample_time -= 1;
Signature · 1 line
2.56.1.750.gf70dc682cf-goog
#9 lore raw

Re: [PATCH v2 7/7] iio: proximity: isl29501: Fix division by zero and overflow in isl29501_set_freq()

Quoted 10 lines · Lee Jones

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 using HZ_PER_MHZ as an unsigned int within [1, 2222 * HZ_PER_MHZ], and return -ERANGE otherwise.

...

drivers/iio/proximity/isl29501.c static int isl29501_set_calibbias(struct isl29501_private *isl29501, · line 875

Quoted 12 lines
drivers/iio/proximity/isl29501.c +6 -3
-	int freq;
 	unsigned long long sample_time;
+	unsigned int freq;
+
+	if (val < 0 || val > 2222 || val2 < 0)
+		return -ERANGE;
 
 	/* sample_freq = 1 / (0.000450 * (sample_time + 1) * 10^-6) */
-	freq = val * 1000000 + val2 % 1000000;
-	sample_time = 2222ULL * 1000000ULL;
+	freq = val * HZ_PER_MHZ + val2 % HZ_PER_MHZ;
+	if (!freq || freq > 2222U * HZ_PER_MHZ)

But the second one is implied already by the val validation above, no?

drivers/iio/proximity/isl29501.c static int isl29501_set_calibbias(struct isl29501_private *isl29501, · line 878

Quoted 3 lines
drivers/iio/proximity/isl29501.c +3 -0
+		return -ERANGE;
+
+	sample_time = 2222ULL * HZ_PER_MHZ;

Taking this into consideration, perhaps not to touch these lines at all? Just add a check for 0 above?

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

?

Signature · 2 lines
With Best Regards,
Andy Shevchenko