Skip to contentSkip to conversation

iio: core: Print error in case sample bits do not fit storage bits

PATCH In review Marek Vasut 4 participants 4 messages Mar 28, 2022 – Apr 2, 2022 IIO

lore
Share & tools
lore
#0 lore raw

PATCH v3 1/1 iio: core: Print error in case sample bits do not fit storage bits

Add runtime check to verify whether storagebits are at least as big as shifted realbits. This should help spot broken drivers which may set realbits + shift above storagebits.

Cc Andy Shevchenko <[email protected]>
Cc Daniel Baluta <[email protected]>
Cc Jonathan Cameron <[email protected]>

---
V2: Use dev_err() instead as WARN_ON() may panic() the kernel on existing machines
V3: Abort probe and return -EINVAL in case this condition is triggered

---
 drivers/iio/industrialio-buffer.c | 13 +++++++++++++
 1 file changed, 13 insertions(+)
drivers/iio/industrialio-buffer.c +13 -0
index b078eb2f3c9de..75a1c57b49102 100644
@@ -1629,6 +1629,19 @@ static int __iio_buffer_alloc_sysfs_and_mask(struct iio_buffer *buffer,
 			if (channels[i].scan_index < 0)
 				continue;
 
+			/* Verify that sample bits fit into storage */
+			if (channels[i].scan_type.storagebits <
+			    channels[i].scan_type.realbits +
+			    channels[i].scan_type.shift) {
+				dev_err(&indio_dev->dev,
+					"Channel %d storagebits (%d) < shifted realbits (%d + %d)\n",
+					i, channels[i].scan_type.storagebits,
+					channels[i].scan_type.realbits,
+					channels[i].scan_type.shift);
+				ret = -EINVAL;
+				goto error_cleanup_dynamic;
+			}
+
 			ret = iio_buffer_add_channel_sysfs(indio_dev, buffer,
 							 &channels[i]);
 			if (ret < 0)
Signature · 1 line
2.35.1
Reviewed-by on v3 1/1
#1 lore raw
Quoted 3 lines · Marek Vasut

Add runtime check to verify whether storagebits are at least as big as shifted realbits. This should help spot broken drivers which may set realbits + shift above storagebits.

Let's go with it.

Quoted 38 lines
Cc Andy Shevchenko <[email protected]>
Cc Daniel Baluta <[email protected]>
Cc Jonathan Cameron <[email protected]>

---
V2: Use dev_err() instead as WARN_ON() may panic() the kernel on existing machines
V3: Abort probe and return -EINVAL in case this condition is triggered

---
 drivers/iio/industrialio-buffer.c | 13 +++++++++++++
 1 file changed, 13 insertions(+)
drivers/iio/industrialio-buffer.c +13 -0
index b078eb2f3c9de..75a1c57b49102 100644
@@ -1629,6 +1629,19 @@ static int __iio_buffer_alloc_sysfs_and_mask(struct iio_buffer *buffer,
 			if (channels[i].scan_index < 0)
 				continue;
 
+			/* Verify that sample bits fit into storage */
+			if (channels[i].scan_type.storagebits <
+			    channels[i].scan_type.realbits +
+			    channels[i].scan_type.shift) {
+				dev_err(&indio_dev->dev,
+					"Channel %d storagebits (%d) < shifted realbits (%d + %d)\n",
+					i, channels[i].scan_type.storagebits,
+					channels[i].scan_type.realbits,
+					channels[i].scan_type.shift);
+				ret = -EINVAL;
+				goto error_cleanup_dynamic;
+			}
+
 			ret = iio_buffer_add_channel_sysfs(indio_dev, buffer,
 							 &channels[i]);
 			if (ret < 0)
Signature · 1 line
2.35.1
Signature · 2 lines
With Best Regards,
Andy Shevchenko
maintainer
#2 lore raw
Quoted 7 lines · On Wed, 30 Mar 2022 17:52:13 +0300 Andy Shevchenko…
Quoted 3 lines · Marek Vasut

Add runtime check to verify whether storagebits are at least as big as shifted realbits. This should help spot broken drivers which may set realbits + shift above storagebits.

Let's go with it.

Agreed. Applied with a slight change to the title to make it clear we don't just print the error, we also fail the iio_device_register() call

Applied to the togreg branch of iio.git and pushed out as testing both to let 0-day poke at it and because I will rebase on rc1 once available.

Thanks,

Jonathan

Quoted 40 lines
Quoted 39 lines
Cc Andy Shevchenko <[email protected]>
Cc Daniel Baluta <[email protected]>
Cc Jonathan Cameron <[email protected]>

---
V2: Use dev_err() instead as WARN_ON() may panic() the kernel on existing machines
V3: Abort probe and return -EINVAL in case this condition is triggered

---
 drivers/iio/industrialio-buffer.c | 13 +++++++++++++
 1 file changed, 13 insertions(+)
drivers/iio/industrialio-buffer.c +13 -0
index b078eb2f3c9de..75a1c57b49102 100644
@@ -1629,6 +1629,19 @@ static int __iio_buffer_alloc_sysfs_and_mask(struct iio_buffer *buffer,
 			if (channels[i].scan_index < 0)
 				continue;
 
+			/* Verify that sample bits fit into storage */
+			if (channels[i].scan_type.storagebits <
+			    channels[i].scan_type.realbits +
+			    channels[i].scan_type.shift) {
+				dev_err(&indio_dev->dev,
+					"Channel %d storagebits (%d) < shifted realbits (%d + %d)\n",
+					i, channels[i].scan_type.storagebits,
+					channels[i].scan_type.realbits,
+					channels[i].scan_type.shift);
+				ret = -EINVAL;
+				goto error_cleanup_dynamic;
+			}
+
 			ret = iio_buffer_add_channel_sysfs(indio_dev, buffer,
 							 &channels[i]);
 			if (ret < 0)
Signature · 1 line
2.35.1
reviewer Reviewed-by on v3 1/1
#3 lore raw
In reply to Marek Vasut
Quoted 19 lines

From: Marek Vasut <[email protected]>
Sent: Monday, March 28, 2022 9:53 PM
To: [email protected]
Cc: Marek Vasut <[email protected]>; Andy Shevchenko <[email protected]>; Daniel Baluta <[email protected]>; Jonathan Cameron <[email protected]>
Subject: [PATCH v3] iio: core: Print error in case sample bits do not fit storage bits

[External]

Add runtime check to verify whether storagebits are at least as big as shifted realbits. This should help spot broken drivers which may set realbits + shift above storagebits.

Cc Andy Shevchenko <[email protected]>
Cc Daniel Baluta <[email protected]>
Cc Jonathan Cameron <[email protected]>
Quoted 5 lines

---
V2: Use dev_err() instead as WARN_ON() may panic() the kernel on existing machines
V3: Abort probe and return -EINVAL in case this condition is triggered
---

Quoted 34 lines
 drivers/iio/industrialio-buffer.c | 13 +++++++++++++
 1 file changed, 13 insertions(+)
drivers/iio/industrialio- renamed from drivers/iio/industrialio-buffer.c +0 -0

buffer.c
index b078eb2f3c9de..75a1c57b49102 100644

drivers/iio/industrialio-buffer.c +13 -0
@@ -1629,6 +1629,19 @@ static int
 __iio_buffer_alloc_sysfs_and_mask(struct iio_buffer *buffer,
 			if (channels[i].scan_index < 0)
 				continue;
 
+			/* Verify that sample bits fit into storage */
+			if (channels[i].scan_type.storagebits <
+			    channels[i].scan_type.realbits +
+			    channels[i].scan_type.shift) {
+				dev_err(&indio_dev->dev,
+					"Channel %d storagebits (%d) <
 shifted realbits (%d + %d)\n",
+					i,
 channels[i].scan_type.storagebits,
+					channels[i].scan_type.realbits,
+					channels[i].scan_type.shift);
+				ret = -EINVAL;
+				goto error_cleanup_dynamic;
+			}
+

ret = iio_buffer_add_channel_sysfs(indio_dev,
buffer,

drivers/iio/industrialio-buffer.c +0 -1
 							 &channels[i]);
 			if (ret < 0)
--

2.35.1