iio: adc: MCP3564: fix calib_bias and calib_scale range checks
Subsystems (2)
PATCH 1/1 iio: adc: MCP3564: fix calib_bias and calib_scale range checks
The current implementation uses the AND (&&) operator to check if the value to write for IIO_CHAN_INFO_CALIBBIAS and IIO_CHAN_INFO_CALIBSCALE is within the valid ranges.
The evaluated values are the lower and upper limits of the ranges, so this operation always evaluates to false.
The OR (||) operator must be used instead.
---
This bug has been found when looking for use cases of IIO_AVAIL_RANGE and the fix could not be tested with real hardware. The issue and its soulution are rather simple, but still some testing and confirmation would be welcome.
--- drivers/iio/adc/mcp3564.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-)
drivers/iio/adc/mcp3564.c +2 -2
@@ -918,7 +918,7 @@ static int mcp3564_write_raw(struct iio_dev *indio_dev,mutex_unlock (&adc->lock);return ret;case IIO_CHAN_INFO_CALIBBIAS : -if (val < mcp3564_calib_bias[0 ] && val > mcp3564_calib_bias[2 ]) +if (val < mcp3564_calib_bias[0 ] || val > mcp3564_calib_bias[2 ])return -EINVAL ;mutex_lock (&adc->lock);
@@ -928,7 +928,7 @@ static int mcp3564_write_raw(struct iio_dev *indio_dev,mutex_unlock (&adc->lock);return ret;case IIO_CHAN_INFO_CALIBSCALE : -if (val < mcp3564_calib_scale[0 ] && val > mcp3564_calib_scale[2 ]) +if (val < mcp3564_calib_scale[0 ] || val > mcp3564_calib_scale[2 ])return -EINVAL ;if (adc->calib_scale == val)
---
base-commit: 994d5c58e50e91bb02c7be4a91d5186292a895c8
change-id: 20231201-mcp3564_range_checks-221708838130
Best regards,
Signature · 1 line
Javier Carrasco <[email protected]>
Hi Javier Carrasco,
Thank you for looking over the driver, for spotting and reporting
the bug. Your fix is OK.
Thanks,
Marius
Quoted 59 lines · Javier Carrasco
EXTERNAL EMAIL: Do not click links or open attachments unless you know the content is safe
The current implementation uses the AND (&&) operator to check if the value to write for
IIO_CHAN_INFO_CALIBBIASandIIO_CHAN_INFO_CALIBSCALE
is within the valid ranges.
The evaluated values are the lower and upper limits of the ranges, so this operation always evaluates to false.The OR (||) operator must be used instead.
Signed-off-by Javier Carrasco <[email protected]>---
This bug has been found when looking for use cases ofIIO_AVAIL_RANGEand the fix could not be tested with real hardware. The issue and its soulution are rather simple, but still some testing and confirmation would be welcome.
---
drivers/iio/adc/mcp3564.c | 4 ++--1 file changed, 2 insertions(+), 2 deletions(-)drivers/iio/adc/mcp3564.c +0
-0@@ -918,7 +918,7 @@ static int mcp3564_write_raw(struct iio_dev *indio_dev, mutex_unlock(&adc->lock); return ret;case
IIO_CHAN_INFO_CALIBBIAS:
- if (val < mcp3564_calib_bias[0] && val > mcp3564_calib_bias[2])
+ if (val < mcp3564_calib_bias[0] || val > mcp3564_calib_bias[2])
return-EINVAL;
mutex_lock(&adc->lock);drivers/iio/adc/mcp3564.c +0
-0@@ -928,7 +928,7 @@ static int mcp3564_write_raw(struct iio_dev *indio_dev, mutex_unlock(&adc->lock); return ret;case
IIO_CHAN_INFO_CALIBSCALE:
- if (val < mcp3564_calib_scale[0] && val > mcp3564_calib_scale[2])
+ if (val < mcp3564_calib_scale[0] || val > mcp3564_calib_scale[2])
return-EINVAL;if (
adc->calib_scale== val)---
base-commit:994d5c58e50e91bb02c7be4a91d5186292a895c8
change-id: 20231201-mcp3564_range_checks-221708838130Best regards,
--
Javier Carrasco <[email protected]>
A message that is not in the archive (sent off-list or not yet received): IA1PR11MB63958455F969F1506B3084769986A@IA1PR11MB6395.namprd11.prod.outlook.com
Quoted 6 lines · On Mon, 4 Dec 2023 09:23:45 +0000 <[email protected]> wrote:
Hi Javier Carrasco,
Thank you for looking over the driver and spotting and reporting the bug. Your fix is OK.
Reviewed-by Marius Cristea <[email protected]>
Applied to the fixes-togreg branch of iio.git and marked for stable.
Thanks,
Jonathan
Quoted 61 lines
Thanks,
Marius________________________________
From: Javier Carrasco <[email protected]>
Sent: Friday, December 1, 2023 11:48 AM
To: Marius Cristea - M70257 <[email protected]>; Jonathan Cameron <[email protected]>; Lars-Peter Clausen <[email protected]>
Cc: [email protected] <[email protected]>; [email protected] <[email protected]>; Javier Carrasco <[email protected]>
Subject: [PATCH] iio: adc: MCP3564: fixcalib_biasandcalib_scalerange checksEXTERNAL EMAIL: Do not click links or open attachments unless you know the content is safe
The current implementation uses the AND (&&) operator to check if the value to write for
IIO_CHAN_INFO_CALIBBIASandIIO_CHAN_INFO_CALIBSCALEis within the valid ranges.
The evaluated values are the lower and upper limits of the ranges, so this operation always evaluates to false.The OR (||) operator must be used instead.
Signed-off-by Javier Carrasco <[email protected]>---
This bug has been found when looking for use cases ofIIO_AVAIL_RANGEand the fix could not be tested with real hardware. The issue and its soulution are rather simple, but still some testing and confirmation would be welcome.--- drivers/iio/adc/mcp3564.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-)drivers/iio/adc/mcp3564.c +2
-3@@ -918,7 +918,7 @@ static int mcp3564_write_raw(struct iio_dev *indio_dev,mutex_unlock (&adc->lock);return ret;case IIO_CHAN_INFO_CALIBBIAS : -if (val < mcp3564_calib_bias[0 ] && val > mcp3564_calib_bias[2 ]) +if (val < mcp3564_calib_bias[0 ] || val > mcp3564_calib_bias[2 ])return -EINVAL ;mutex_lock (&adc->lock);@@ -928,7 +928,7 @@ static int mcp3564_write_raw(struct iio_dev *indio_dev,mutex_unlock (&adc->lock);return ret;case IIO_CHAN_INFO_CALIBSCALE : -if (val < mcp3564_calib_scale[0 ] && val > mcp3564_calib_scale[2 ]) +if (val < mcp3564_calib_scale[0 ] || val > mcp3564_calib_scale[2 ])return -EINVAL ;if (adc->calib_scale == val) ---base-commit:
994d5c58e50e91bb02c7be4a91d5186292a895c8
change-id: 20231201-mcp3564_range_checks-221708838130Best regards,
--
Javier Carrasco <[email protected]>