Takashi Iwai 546216
From f2a772c51206b0c3f262e4f6a3812c89a650191b Mon Sep 17 00:00:00 2001
Takashi Iwai 546216
From: Jonathan Cameron <Jonathan.Cameron@huawei.com>
Takashi Iwai 546216
Date: Thu, 13 May 2021 15:07:42 +0300
Takashi Iwai 546216
Subject: [PATCH] iio: adc: ad7124: Fix potential overflow due to non sequential channel numbers
Takashi Iwai 546216
Git-commit: f2a772c51206b0c3f262e4f6a3812c89a650191b
Takashi Iwai 546216
Patch-mainline: v5.13-rc4
Takashi Iwai 546216
References: git-fixes
Takashi Iwai 546216
Takashi Iwai 546216
Channel numbering must start at 0 and then not have any holes, or
Takashi Iwai 546216
it is possible to overflow the available storage.  Note this bug was
Takashi Iwai 546216
introduced as part of a fix to ensure we didn't rely on the ordering
Takashi Iwai 546216
of child nodes.  So we need to support arbitrary ordering but they all
Takashi Iwai 546216
need to be there somewhere.
Takashi Iwai 546216
Takashi Iwai 546216
Note I hit this when using qemu to test the rest of this series.
Takashi Iwai 546216
Arguably this isn't the best fix, but it is probably the most minimal
Takashi Iwai 546216
option for backporting etc.
Takashi Iwai 546216
Takashi Iwai 546216
Alexandru's sign-off is here because he carried this patch in a larger
Takashi Iwai 546216
set that Jonathan then applied.
Takashi Iwai 546216
Takashi Iwai 546216
Fixes: d7857e4ee1ba6 ("iio: adc: ad7124: Fix DT channel configuration")
Takashi Iwai 546216
Reviewed-by: Alexandru Ardelean <ardeleanalex@gmail.com>
Takashi Iwai 546216
Signed-off-by: Jonathan Cameron <Jonathan.Cameron@huawei.com>
Takashi Iwai 546216
Signed-off-by: Alexandru Ardelean <aardelean@deviqon.com>
Takashi Iwai 546216
Cc: <Stable@vger.kernel.org>
Takashi Iwai 546216
Acked-by: Takashi Iwai <tiwai@suse.de>
Takashi Iwai 546216
Takashi Iwai 546216
---
Takashi Iwai 546216
 drivers/iio/adc/ad7124.c | 7 +++++++
Takashi Iwai 546216
 1 file changed, 7 insertions(+)
Takashi Iwai 546216
Takashi Iwai 546216
diff --git a/drivers/iio/adc/ad7124.c b/drivers/iio/adc/ad7124.c
Takashi Iwai 546216
index 437116a07cf1..a27db78ea13e 100644
Takashi Iwai 546216
--- a/drivers/iio/adc/ad7124.c
Takashi Iwai 546216
+++ b/drivers/iio/adc/ad7124.c
Takashi Iwai 546216
@@ -771,6 +771,13 @@ static int ad7124_of_parse_channel_config(struct iio_dev *indio_dev,
Takashi Iwai 546216
 		if (ret)
Takashi Iwai 546216
 			goto err;
Takashi Iwai 546216
 
Takashi Iwai 546216
+		if (channel >= indio_dev->num_channels) {
Takashi Iwai 546216
+			dev_err(indio_dev->dev.parent,
Takashi Iwai 546216
+				"Channel index >= number of channels\n");
Takashi Iwai 546216
+			ret = -EINVAL;
Takashi Iwai 546216
+			goto err;
Takashi Iwai 546216
+		}
Takashi Iwai 546216
+
Takashi Iwai 546216
 		ret = of_property_read_u32_array(child, "diff-channels",
Takashi Iwai 546216
 						 ain, 2);
Takashi Iwai 546216
 		if (ret)
Takashi Iwai 546216
-- 
Takashi Iwai 546216
2.26.2
Takashi Iwai 546216