[PATCH 1/2] iio: proximity: sx9310: reject unsupported hardware gain values
From: Arnav Kapoor
Date: Sat Nov 07 2026 - 06:21:30 EST
sx9310_write_gain() passes any value from userspace to ilog2() and then
FIELD_PREP() into the 2-bit gain field without checking it. Only gains
of 1, 2, 4 and 8 are supported (and listed in hardwaregain_available),
but other values are not rejected; they are silently changed:
- 3, 5, 6, 7 are rounded down to the next power of two;
- 16 and larger wrap around in the 2-bit field, e.g. 16 sets a gain
of 1 and 32 a gain of 2;
- 0 and negative values set the maximum gain of 8, because ilog2(0)
is -1 and a negative int becomes a large unsigned value.
The write returns success in every case, so userspace cannot tell that
the gain it asked for was not applied.
Return -EINVAL for anything other than 1, 2, 4 or 8.
Fixes: 227c83faa2f8 ("iio: sx9310: Support hardware gain factor")
Signed-off-by: Arnav Kapoor <kapoorarnav43@xxxxxxxxx>
---
drivers/iio/proximity/sx9310.c | 3 +++
1 file changed, 3 insertions(+)
diff --git a/drivers/iio/proximity/sx9310.c b/drivers/iio/proximity/sx9310.c
index 2585b9c6fb69..27b5d604a409 100644
--- a/drivers/iio/proximity/sx9310.c
+++ b/drivers/iio/proximity/sx9310.c
@@ -665,6 +665,9 @@ static int sx9310_write_gain(struct sx_common_data *data,
{
unsigned int gain, mask;
+ if (val <= 0 || val > 8 || !is_power_of_2(val))
+ return -EINVAL;
+
gain = ilog2(val);
switch (chan->channel) {
--
2.53.0