Skip to contentSkip to conversation

iio: adc: xilinx-ams: fix OOB read in ams_get_ext_chan()

PATCH In review Manush Prajwal 2 participants 2 messages Sep 6

Subsystems (3)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 iio: adc: xilinx-ams: fix OOB read in ams_get_ext_chan()

The PL external-channel "reg" property is only checked against its upper bound (AMS_PL_MAX_EXT_CHANNEL + 30 == 50), matching the 'maximum: 50' constraint in the devicetree binding (Documentation/devicetree/bindings/iio/adc/xlnx,zynqmp-ams.yaml), but the binding also documents 'minimum: 20' which the driver never enforces at runtime.

ext_chan is computed as 'reg + AMS_PL_MAX_FIXED_CHANNEL - 30' in unsigned arithmetic. For any reg < 20 (e.g. a hand-written or malformed devicetree overlay with reg = <0>), this underflows to a huge unsigned value, and the following

	memcpy(chan, &ams_pl_channels[ext_chan], sizeof(*channels));

reads far outside the 31-entry ams_pl_channels[] array.

Reject any reg value that would produce an out-of-range ext_chan before it is used to index ams_pl_channels[], instead of relying only on the upper-bound check.

---
 drivers/iio/adc/xilinx-ams.c | 5 ++++-
 1 file changed, 4 insertions(+), 1 deletion(-)
drivers/iio/adc/xilinx-ams.c +4 -1
index 158e6133a..cd778d053 100644
@@ -1154,8 +1154,11 @@ static int ams_get_ext_chan(struct fwnode_handle *chan_node,
 		if (ret || reg > AMS_PL_MAX_EXT_CHANNEL + 30)
 			continue;
 
-		chan = &channels[num_channels];
 		ext_chan = reg + AMS_PL_MAX_FIXED_CHANNEL - 30;
+		if (ext_chan >= ARRAY_SIZE(ams_pl_channels))
+			continue;
+
+		chan = &channels[num_channels];
 		memcpy(chan, &ams_pl_channels[ext_chan], sizeof(*channels));
 
 		if (fwnode_property_read_bool(child, "xlnx,bipolar"))
Signature · 1 line
2.46.2.windows.1
maintainer
#1 lore raw
Quoted 14 lines · On 6 Sep 2026 16:35:18 +0530 "Manush Prajwal" <[email protected]>…

The PL external-channel "reg" property is only checked against its upper bound (AMS_PL_MAX_EXT_CHANNEL + 30 == 50), matching the 'maximum: 50' constraint in the devicetree binding (Documentation/devicetree/bindings/iio/adc/xlnx,zynqmp-ams.yaml), but the binding also documents 'minimum: 20' which the driver never enforces at runtime.

ext_chan is computed as 'reg + AMS_PL_MAX_FIXED_CHANNEL - 30' in unsigned arithmetic. For any reg < 20 (e.g. a hand-written or malformed devicetree overlay with reg = <0>), this underflows to a huge unsigned value, and the following

	memcpy(chan, &ams_pl_channels[ext_chan], sizeof(*channels));
Quoted 7 lines

reads far outside the 31-entry ams_pl_channels[] array.

Reject any reg value that would produce an out-of-range ext_chan before it is used to index ams_pl_channels[], instead of relying only on the upper-bound check.

Explanation seems valid. I'll wait for the AMD folk to have time to take a look though before considering picking this up.

One comment inline.

Quoted 9 lines
---
 drivers/iio/adc/xilinx-ams.c | 5 ++++-
 1 file changed, 4 insertions(+), 1 deletion(-)
drivers/iio/adc/xilinx-ams.c +0 -0
index 158e6133a..cd778d053 100644

drivers/iio/adc/xilinx-ams.c static int ams_get_ext_chan(struct fwnode_handle *chan_node, · line 1159

Quoted 7 lines
drivers/iio/adc/xilinx-ams.c +2 -1
@@ -1154,8 +1154,11 @@ static int ams_get_ext_chan(struct fwnode_handle *chan_node,
 		if (ret || reg > AMS_PL_MAX_EXT_CHANNEL + 30)
 			continue;
 
-		chan = &channels[num_channels];
 		ext_chan = reg + AMS_PL_MAX_FIXED_CHANNEL - 30;
+		if (ext_chan >= ARRAY_SIZE(ams_pl_channels))
+			continue;

I haven't looked closely but is it ever fine to go off the top of this? If not, error out and fail probe.
I'd also prefer to have more direct handling of the wrap around case rather than relying on it being a big value and so failing this.

Thanks,

Jonathan

Quoted 5 lines
drivers/iio/adc/xilinx-ams.c +2 -0
+
+		chan = &channels[num_channels];
 		memcpy(chan, &ams_pl_channels[ext_chan], sizeof(*channels));
 
 		if (fwnode_property_read_bool(child, "xlnx,bipolar"))