iio: adc: xilinx-ams: fix out-of-bounds accesses when parsing channels
Subsystems (5)
v3 cover iio: adc: xilinx-ams: fix out-of-bounds accesses when parsing channels
The driver sizes its channel array for 10 fixed and 21 external PL channels, but accepts external channel nodes numbered 20 to 50, and it never checks the remaining room while filling the array. Channels 20 to 29 are the fixed ones, so listing them adds duplicates and overflows the array.
Patch 1 limits the external channels in the binding to 30-50, as Sai pointed out. Patch 2 does the same in the driver and adds the bounds checks from v2.
Changes in v3:
- New patch 1: limit the binding to channels 30-50 (Sai)
- Patch 2: only accept channels 30-50, so a channel node can no longer duplicate a fixed channel (Sai), and add a static_assert() that ams_pl_channels[] holds the fixed plus the external channels
- Patch 2: rework the commit message around the duplicate channels
- Rebase on fixes-togreg (now on 7.4-rc1)
v2: https://lore.kernel.org/all/[email protected]/
v1: https://lore.kernel.org/all/[email protected]/
Compile-tested only (W=1); I don't have ZynqMP hardware. dt_binding_check passes, and rejects a channel@25 node in the example.
Arnav Kapoor (2):
dt-bindings: iio: adc: xlnx,zynqmp-ams: limit external channels to
30-50
iio: adc: xilinx-ams: fix out-of-bounds accesses when parsing channels
.../bindings/iio/adc/xlnx,zynqmp-ams.yaml | 7 ++- drivers/iio/adc/xilinx-ams.c | 57 +++++++++++++++---- 2 files changed, 50 insertions(+), 14 deletions(-)
base-commit: c293f0db308fe76a0a9b0f6674cd2dea149b03aa
Signature · 1 line
2.53.0
PATCH v3 1/2 dt-bindings: iio: adc: xlnx,zynqmp-ams: limit external channels to 30-50
Channels 20 to 29 of the PL-SYSMON block are its fixed channels (PL temperature to VCCADC), which the driver always provides. Only channels 30 to 50 (the differential input, VUser0-3 and VAux0-15) are external channels that can be described as "channel@N" child nodes, yet the binding accepts any N from 20 to 50. Listing one of the fixed channels there only makes the driver add it a second time.
Limit the child nodes to channels 30 to 50.
--- .../devicetree/bindings/iio/adc/xlnx,zynqmp-ams.yaml | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-)
Documentation/devicetree/bindings/iio/adc/xlnx,zynqmp-ams.yaml +4 -3
@@ -160,17 +160,18 @@ properties: const: 0 patternProperties: - "^channel@([2-4][0-9]|50)$": + "^channel@([34][0-9]|50)$": type: object description: - Describes the external channels connected. + Describes the external channels connected. Channels 20 to 29 are + always available and are not listed here. properties: reg: description: Pair of pins the channel is connected to. This value is same as Channel Number for a particular channel. - minimum: 20 + minimum: 30 maximum: 50 xlnx,bipolar:
Signature · 1 line
2.53.0
Re: [PATCH v3 1/2] dt-bindings: iio: adc: xlnx,zynqmp-ams: limit external channels to 30-50
Quoted 7 lines · Arnav Kapoor
Channels 20 to 29 of the PL-SYSMON block are its fixed channels (PL temperature to VCCADC), which the driver always provides. Only channels 30 to 50 (the differential input, VUser0-3 and VAux0-15) are external channels that can be described as "channel@N" child nodes, yet the binding accepts any N from 20 to 50. Listing one of the fixed channels there only makes the driver add it a second time.
Quoted 6 lines
Limit the child nodes to channels 30 to 50.
Fixes 39dd2d1e251d ("dt-bindings: iio: adc: Add Xilinx AMS binding documentation")Suggested-by Sai Krishna Potthuri <[email protected]>Signed-off-by Arnav Kapoor <[email protected]>---
Looks good to me.
Regards
Sai krishna
Quoted 29 lines
.../devicetree/bindings/iio/adc/xlnx,zynqmp-ams.yaml | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-)Documentation/devicetree/bindings/iio/adc/xlnx,zynqmp-ams.yaml +4
-3@@ -160,17 +160,18 @@ properties: const: 0 patternProperties: - "^channel@([2-4][0-9]|50)$": + "^channel@([34][0-9]|50)$": type: object description: - Describes the external channels connected. + Describes the external channels connected. Channels 20 to 29 are + always available and are not listed here. properties: reg: description: Pair of pins the channel is connected to. This value is same as Channel Number for a particular channel. - minimum: 20 + minimum: 30 maximum: 50 xlnx,bipolar:
Re: [PATCH v3 1/2] dt-bindings: iio: adc: xlnx,zynqmp-ams: limit external channels to 30-50
In reply to Arnav KapoorQuoted 10 lines · Arnav Kapoor
Channels 20 to 29 of the PL-SYSMON block are its fixed channels (PL temperature to VCCADC), which the driver always provides. Only channels 30 to 50 (the differential input, VUser0-3 and VAux0-15) are external channels that can be described as "channel@N" child nodes, yet the binding accepts any N from 20 to 50. Listing one of the fixed channels there only makes the driver add it a second time.
Limit the child nodes to channels 30 to 50.
Fixes 39dd2d1e251d ("dt-bindings: iio: adc: Add Xilinx AMS binding documentation")
Drop, no bug is fixed here. Driver should simply not register such channels second time, so it's a driver bug, not a binding.
Best regards,
Krzysztof
PATCH v3 2/2 iio: adc: xilinx-ams: fix out-of-bounds accesses when parsing channels
In reply to Arnav Kapoorams_parse_firmware() allocates room for ARRAY_SIZE(ams_ps_channels) + ARRAY_SIZE(ams_pl_channels) + ARRAY_SIZE(ams_ctrl_channels) = 51 channel specs and lets ams_init_module() fill them for the AMS node and each of its children, without telling it how much room is left.
For the PL-SYSMON node, ams_init_module() always adds the 10 fixed PL channels, numbered 20 to 29, and ams_get_ext_chan() then appends one channel per "channel@N" child as ams_pl_channels[N - 20]. Only channels 30 to 50 are external ones, but any N from 20 to 50 is accepted, so a child numbered 20 to 29 duplicates a fixed channel. A PL node can then have up to 41 channels while only 31 are budgeted for it, and together with the 13 PS and 7 AMS control channels this writes past the end of the buffer. Since the modules are filled in device tree order, the fixed channel blocks copied after the PL node can overflow as well.
ams_get_ext_chan() also only checks the upper bound of reg. ext_chan is unsigned, so a reg below 20 makes it wrap and ams_pl_channels[] is read far out of bounds.
Only accept the external channels 30 to 50. Also pass the remaining capacity down to ams_init_module() and ams_get_ext_chan() and fail with -ENOSPC when a module does not fit, so that a device tree repeating a reg value can't write past the buffer either.
--- drivers/iio/adc/xilinx-ams.c | 57 +++++++++++++++++++++++++++++------- 1 file changed, 46 insertions(+), 11 deletions(-)
drivers/iio/adc/xilinx-ams.c +46 -11
@@ -144,7 +144,14 @@#define AMS_PL_CSTS_ACCESS_MASK BIT(1)#define AMS_PL_MAX_FIXED_CHANNEL 10 -#define AMS_PL_MAX_EXT_CHANNEL 20 +/* +* The PL channels are numbered from 20 in the binding. The first +* AMS_PL_MAX_FIXED_CHANNEL of them are always present, the external ones +* from AMS_PL_EXT_CHANNEL_MIN to AMS_PL_EXT_CHANNEL_MAX follow them in +* ams_pl_channels[]. +*/ +#define AMS_PL_EXT_CHANNEL_MIN 30 +#define AMS_PL_EXT_CHANNEL_MAX 50#define AMS_INIT_POLL_TIME_US 200#define AMS_INIT_TIMEOUT_US 10000
@@ -1131,6 +1138,9 @@ static const struct iio_chan_spec ams_pl_channels[] = {AMS_PL_AUX_CHAN_VOLTAGE (15 ), }; +static_assert (ARRAY_SIZE (ams_pl_channels) == +AMS_PL_MAX_FIXED_CHANNEL +AMS_PL_EXT_CHANNEL_MAX -AMS_PL_EXT_CHANNEL_MIN +1 ); +static const struct iio_chan_spec ams_ctrl_channels[] = {AMS_CTRL_CHAN_VOLTAGE (AMS_SEQ_VCC_PSPLL ,AMS_VCC_PSPLL0 ,"VCC_PSPLL" ), AMS_CTRL_CHAN_VOLTAGE(AMS_SEQ_VCC_PSBATT, AMS_VCC_PSPLL3, "VCC_PSBATT"),
@@ -1142,20 +1152,31 @@ static const struct iio_chan_spec ams_ctrl_channels[] = { };static int ams_get_ext_chan (struct fwnode_handle *chan_node, -struct iio_chan_spec *channels,int num_channels) +struct iio_chan_spec *channels,int num_channels, +unsigned int max_channels) {struct iio_chan_spec *chan; -struct fwnode_handle *child;unsigned int reg, ext_chan;int ret; -fwnode_for_each_child_node (chan_node, child) { +fwnode_for_each_child_node_scoped (chan_node, child) { ret =fwnode_property_read_u32 (child,"reg" , ®); -if (ret || reg >AMS_PL_MAX_EXT_CHANNEL +30 ) +if (ret) +continue ; + +/* +* The fixed channels have been added already, anything else +* outside of the external range is not a valid channel. +*/ +if (reg <AMS_PL_EXT_CHANNEL_MIN || + reg >AMS_PL_EXT_CHANNEL_MAX )continue ; +if (num_channels >= max_channels) +return -ENOSPC ; + chan = &channels[num_channels]; - ext_chan = reg +AMS_PL_MAX_FIXED_CHANNEL -30 ; + ext_chan = reg -AMS_PL_EXT_CHANNEL_MIN +AMS_PL_MAX_FIXED_CHANNEL ;memcpy (chan, &ams_pl_channels[ext_chan],sizeof (*channels));if (fwnode_property_read_bool (child,"xlnx,bipolar" ))
@@ -1183,7 +1204,8 @@ static void ams_iounmap_pl(void *data)static int ams_init_module (struct iio_dev *indio_dev,struct fwnode_handle *fwnode, -struct iio_chan_spec *channels) +struct iio_chan_spec *channels, +unsigned int max_channels) {struct device *dev = indio_dev->dev.parent;struct ams *ams =iio_priv (indio_dev);
@@ -1191,6 +1213,9 @@ static int ams_init_module(struct iio_dev *indio_dev,int ret;if (fwnode_device_is_compatible (fwnode,"xlnx,zynqmp-ams-ps" )) { +if (max_channels <ARRAY_SIZE (ams_ps_channels)) +return -ENOSPC ; + ams->ps_base =fwnode_iomap (fwnode,0 );if (!ams->ps_base)return -ENXIO ;
@@ -1202,6 +1227,9 @@ static int ams_init_module(struct iio_dev *indio_dev,memcpy (channels, ams_ps_channels,sizeof (ams_ps_channels)); num_channels =ARRAY_SIZE (ams_ps_channels); }else if (fwnode_device_is_compatible (fwnode,"xlnx,zynqmp-ams-pl" )) { +if (max_channels <AMS_PL_MAX_FIXED_CHANNEL ) +return -ENOSPC ; + ams->pl_base =fwnode_iomap (fwnode,0 );if (!ams->pl_base)return -ENXIO ;
@@ -1213,9 +1241,14 @@ static int ams_init_module(struct iio_dev *indio_dev,/* Copy only first 10 fix channels */ memcpy (channels, ams_pl_channels,AMS_PL_MAX_FIXED_CHANNEL *sizeof (*channels)); num_channels +=AMS_PL_MAX_FIXED_CHANNEL ; - num_channels =ams_get_ext_chan (fwnode, channels, - num_channels); + ret =ams_get_ext_chan (fwnode, channels, num_channels, max_channels); +if (ret <0 ) +return ret; + num_channels = ret; }else if (fwnode_device_is_compatible (fwnode,"xlnx,zynqmp-ams" )) { +if (max_channels <ARRAY_SIZE (ams_ctrl_channels)) +return -ENOSPC ; +/* add AMS channels to iio device channels */ memcpy (channels, ams_ctrl_channels,sizeof (ams_ctrl_channels)); num_channels +=ARRAY_SIZE (ams_ctrl_channels);
@@ -1245,7 +1278,7 @@ static int ams_parse_firmware(struct iio_dev *indio_dev)return -ENOMEM ;if (fwnode_device_is_available (fwnode)) { - ret =ams_init_module (indio_dev, fwnode, ams_channels); + ret =ams_init_module (indio_dev, fwnode, ams_channels, ams_size);if (ret <0 )return ret;
@@ -1253,7 +1286,9 @@ static int ams_parse_firmware(struct iio_dev *indio_dev) }device_for_each_child_node_scoped (dev, child) { - ret =ams_init_module (indio_dev, child, ams_channels + num_channels); + ret =ams_init_module (indio_dev, child, + ams_channels + num_channels, + ams_size - num_channels);if (ret <0 )return ret;
Signature · 1 line
2.53.0
Quoted 19 lines · Arnav Kapoor
ams_parse_firmware()allocates room forARRAY_SIZE(ams_ps_channels)+ARRAY_SIZE(ams_pl_channels)+ARRAY_SIZE(ams_ctrl_channels)= 51 channel specs and letsams_init_module()fill them for the AMS node and each of its children, without telling it how much room is left.For the PL-SYSMON node,
ams_init_module()always adds the 10 fixed PL channels, numbered 20 to 29, andams_get_ext_chan()then appends one channel per "channel@N" child as ams_pl_channels[N - 20]. Only channels 30 to 50 are external ones, but any N from 20 to 50 is accepted, so a child numbered 20 to 29 duplicates a fixed channel. A PL node can then have up to 41 channels while only 31 are budgeted for it, and together with the 13 PS and 7 AMS control channels this writes past the end of the buffer. Since the modules are filled in device tree order, the fixed channel blocks copied after the PL node can overflow as well.
ams_get_ext_chan()also only checks the upper bound of reg.ext_chanis unsigned, so a reg below 20 makes it wrap and ams_pl_channels[] is read far out of bounds.
Quoted 8 lines
Only accept the external channels 30 to 50. Also pass the remaining capacity down to
ams_init_module()andams_get_ext_chan()and fail with-ENOSPCwhen a module does not fit, so that a device tree repeating a reg value can't write past the buffer either.Fixes d5c70627a794 ("iio: adc: Add Xilinx AMS driver")Signed-off-by Arnav Kapoor <[email protected]>
Looks good to me.
Regards
Sai krishna
Quoted 138 lines
--- drivers/iio/adc/xilinx-ams.c | 57 +++++++++++++++++++++++++++++------- 1 file changed, 46 insertions(+), 11 deletions(-)drivers/iio/adc/xilinx-ams.c +46
-11@@ -144,7 +144,14 @@#define AMS_PL_CSTS_ACCESS_MASK BIT(1)#define AMS_PL_MAX_FIXED_CHANNEL 10 -#define AMS_PL_MAX_EXT_CHANNEL 20 +/* +* The PL channels are numbered from 20 in the binding. The first +* AMS_PL_MAX_FIXED_CHANNEL of them are always present, the external ones +* from AMS_PL_EXT_CHANNEL_MIN to AMS_PL_EXT_CHANNEL_MAX follow them in +* ams_pl_channels[]. +*/ +#define AMS_PL_EXT_CHANNEL_MIN 30 +#define AMS_PL_EXT_CHANNEL_MAX 50#define AMS_INIT_POLL_TIME_US 200#define AMS_INIT_TIMEOUT_US 10000@@ -1131,6 +1138,9 @@ static const struct iio_chan_spec ams_pl_channels[] = {AMS_PL_AUX_CHAN_VOLTAGE (15 ), }; +static_assert (ARRAY_SIZE (ams_pl_channels) == +AMS_PL_MAX_FIXED_CHANNEL +AMS_PL_EXT_CHANNEL_MAX -AMS_PL_EXT_CHANNEL_MIN +1 ); +static const struct iio_chan_spec ams_ctrl_channels[] = {AMS_CTRL_CHAN_VOLTAGE (AMS_SEQ_VCC_PSPLL ,AMS_VCC_PSPLL0 ,"VCC_PSPLL" ), AMS_CTRL_CHAN_VOLTAGE(AMS_SEQ_VCC_PSBATT, AMS_VCC_PSPLL3, "VCC_PSBATT"),@@ -1142,20 +1152,31 @@ static const struct iio_chan_spec ams_ctrl_channels[] = { };static int ams_get_ext_chan (struct fwnode_handle *chan_node, -struct iio_chan_spec *channels,int num_channels) +struct iio_chan_spec *channels,int num_channels, +unsigned int max_channels) {struct iio_chan_spec *chan; -struct fwnode_handle *child;unsigned int reg, ext_chan;int ret; -fwnode_for_each_child_node (chan_node, child) { +fwnode_for_each_child_node_scoped (chan_node, child) { ret =fwnode_property_read_u32 (child,"reg" , ®); -if (ret || reg >AMS_PL_MAX_EXT_CHANNEL +30 ) +if (ret) +continue ; + +/* +* The fixed channels have been added already, anything else +* outside of the external range is not a valid channel. +*/ +if (reg <AMS_PL_EXT_CHANNEL_MIN || + reg >AMS_PL_EXT_CHANNEL_MAX )continue ; +if (num_channels >= max_channels) +return -ENOSPC ; + chan = &channels[num_channels]; - ext_chan = reg +AMS_PL_MAX_FIXED_CHANNEL -30 ; + ext_chan = reg -AMS_PL_EXT_CHANNEL_MIN +AMS_PL_MAX_FIXED_CHANNEL ;memcpy (chan, &ams_pl_channels[ext_chan],sizeof (*channels));if (fwnode_property_read_bool (child,"xlnx,bipolar" ))@@ -1183,7 +1204,8 @@ static void ams_iounmap_pl(void *data)static int ams_init_module (struct iio_dev *indio_dev,struct fwnode_handle *fwnode, -struct iio_chan_spec *channels) +struct iio_chan_spec *channels, +unsigned int max_channels) {struct device *dev = indio_dev->dev.parent;struct ams *ams =iio_priv (indio_dev);@@ -1191,6 +1213,9 @@ static int ams_init_module(struct iio_dev *indio_dev,int ret;if (fwnode_device_is_compatible (fwnode,"xlnx,zynqmp-ams-ps" )) { +if (max_channels <ARRAY_SIZE (ams_ps_channels)) +return -ENOSPC ; + ams->ps_base =fwnode_iomap (fwnode,0 );if (!ams->ps_base)return -ENXIO ;@@ -1202,6 +1227,9 @@ static int ams_init_module(struct iio_dev *indio_dev,memcpy (channels, ams_ps_channels,sizeof (ams_ps_channels)); num_channels =ARRAY_SIZE (ams_ps_channels); }else if (fwnode_device_is_compatible (fwnode,"xlnx,zynqmp-ams-pl" )) { +if (max_channels <AMS_PL_MAX_FIXED_CHANNEL ) +return -ENOSPC ; + ams->pl_base =fwnode_iomap (fwnode,0 );if (!ams->pl_base)return -ENXIO ;@@ -1213,9 +1241,14 @@ static int ams_init_module(struct iio_dev *indio_dev,/* Copy only first 10 fix channels */ memcpy (channels, ams_pl_channels,AMS_PL_MAX_FIXED_CHANNEL *sizeof (*channels)); num_channels +=AMS_PL_MAX_FIXED_CHANNEL ; - num_channels =ams_get_ext_chan (fwnode, channels, - num_channels); + ret =ams_get_ext_chan (fwnode, channels, num_channels, max_channels); +if (ret <0 ) +return ret; + num_channels = ret; }else if (fwnode_device_is_compatible (fwnode,"xlnx,zynqmp-ams" )) { +if (max_channels <ARRAY_SIZE (ams_ctrl_channels)) +return -ENOSPC ; +/* add AMS channels to iio device channels */ memcpy (channels, ams_ctrl_channels,sizeof (ams_ctrl_channels)); num_channels +=ARRAY_SIZE (ams_ctrl_channels);@@ -1245,7 +1278,7 @@ static int ams_parse_firmware(struct iio_dev *indio_dev)return -ENOMEM ;if (fwnode_device_is_available (fwnode)) { - ret =ams_init_module (indio_dev, fwnode, ams_channels); + ret =ams_init_module (indio_dev, fwnode, ams_channels, ams_size);if (ret <0 )return ret;@@ -1253,7 +1286,9 @@ static int ams_parse_firmware(struct iio_dev *indio_dev) }device_for_each_child_node_scoped (dev, child) { - ret =ams_init_module (indio_dev, child, ams_channels + num_channels); + ret =ams_init_module (indio_dev, child, + ams_channels + num_channels, + ams_size - num_channels);if (ret <0 )return ret;
Jonathan Cameron
maintainerQuoted 9 lines · On Wed, 4 Nov 2026 22:14:59 +0530 Arnav Kapoor <[email protected]> wrote:
The driver sizes its channel array for 10 fixed and 21 external PL channels, but accepts external channel nodes numbered 20 to 50, and it never checks the remaining room while filling the array. Channels 20 to 29 are the fixed ones, so listing them adds duplicates and overflows the array.
Patch 1 limits the external channels in the binding to 30-50, as Sai pointed out. Patch 2 does the same in the driver and adds the bounds checks from v2.
Applied with the fixes tag for dt binding dropped. Note I'll take this the slow way so this will only merge next cycle.
Thanks,
Jonathan
Quoted 26 lines
Changes in v3:
- New patch 1: limit the binding to channels 30-50 (Sai)
- Patch 2: only accept channels 30-50, so a channel node can no longer duplicate a fixed channel (Sai), and add astatic_assert()that ams_pl_channels[] holds the fixed plus the external channels
- Patch 2: rework the commit message around the duplicate channels
- Rebase on fixes-togreg (now on 7.4-rc1)v2: https://lore.kernel.org/all/[email protected]/
v1: https://lore.kernel.org/all/[email protected]/Compile-tested only (W=1); I don't have ZynqMP hardware.
dt_binding_checkpasses, and rejects a channel@25 node in the example.Arnav Kapoor (2):
dt-bindings: iio: adc: xlnx,zynqmp-ams: limit external channels to30-50iio: adc: xilinx-ams: fix out-of-bounds accesses when parsing channels
.../bindings/iio/adc/xlnx,zynqmp-ams.yaml | 7 ++- drivers/iio/adc/xilinx-ams.c | 57 +++++++++++++++---- 2 files changed, 50 insertions(+), 14 deletions(-)base-commit:
c293f0db308fe76a0a9b0f6674cd2dea149b03aa