Skip to contentSkip to conversation

drm/bridge: sii902x: Read "sil,i2s-data-lanes" as u32

PATCH Superseded Rob Herring (Arm) 2 participants 2 messages Aug 18

Subsystems (3)

v1 of 3 Superseded Latest is v3 →

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 drm/bridge: sii902x: Read "sil,i2s-data-lanes" as u32

The "sil,i2s-data-lanes" binding and in-tree DTS files use normal uint32 cells. Reading the array as u8 only works because the values are small, but it does not match the documented DT element size.

Use the u32 array helper so the driver follows the binding. Changing to u32 increases a potential OOB access range due to using unchecked values, so add a check on the read values.

---
v2:
- Add bounds check for property values as reported by sashiko

---
 drivers/gpu/drm/bridge/sii902x.c | 15 +++++++++------
 1 file changed, 9 insertions(+), 6 deletions(-)
drivers/gpu/drm/bridge/sii902x.c +9 -6
index 9aac44ed5b67..241b472ab13e 100644
@@ -862,7 +862,7 @@ static int sii902x_audio_codec_init(struct sii902x *sii902x,
 		.max_i2s_channels = 0,
 		.no_capture_mute = 1,
 	};
-	u8 lanes[4];
+	u32 lanes[4];
 	int num_lanes, i;
 
 	if (!of_property_present(dev->of_node, "#sound-dai-cells")) {
@@ -871,10 +871,10 @@ static int sii902x_audio_codec_init(struct sii902x *sii902x,
 		return 0;
 	}
 
-	num_lanes = of_property_read_variable_u8_array(dev->of_node,
-						       "sil,i2s-data-lanes",
-						       lanes, 1,
-						       ARRAY_SIZE(lanes));
+	num_lanes = of_property_read_variable_u32_array(dev->of_node,
+							"sil,i2s-data-lanes",
+							lanes, 1,
+							ARRAY_SIZE(lanes));
 
 	if (num_lanes == -EINVAL) {
 		dev_dbg(dev,
@@ -890,9 +890,12 @@ static int sii902x_audio_codec_init(struct sii902x *sii902x,
 	}
 	codec_data.max_i2s_channels = 2 * num_lanes;
 
-	for (i = 0; i < num_lanes; i++)
+	for (i = 0; i < num_lanes; i++) {
+		if (WARN_ON(lanes[i] >= ARRAY_SIZE(i2s_lane_id)))
+			break;
 		sii902x->audio.i2s_fifo_sequence[i] |= audio_fifo_id[i] |
 			i2s_lane_id[lanes[i]] |	SII902X_TPI_I2S_FIFO_ENABLE;
+	}
 
 	sii902x->audio.mclk = devm_clk_get_optional(dev, "mclk");
 	if (IS_ERR(sii902x->audio.mclk)) {
Signature · 1 line
2.53.0
#1 lore raw
Thank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider: - [Medium] Using WARN_ON() to validate device tree input causes unnecessary kernel splats and potential…

Thank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider:
- [Medium] Using WARN_ON() to validate device tree input causes unnecessary kernel splats and potential kernel panics.
- [Medium] Breaking out of the initialization loop on invalid lane values leaves the audio data structures in an inconsistent state. --

--- Patch [1]: [PATCH] drm/bridge: sii902x: Read "sil,i2s-data-lanes" as u32 --- commit a4b4eceb945fa6ac0b81acc248b81fdebf849bd9
Author: Rob Herring (Arm) <[email protected]>

drm/bridge: sii902x: Read "sil,i2s-data-lanes" as u32

This patch updates the sii902x bridge driver to read the "sil,i2s-data-lanes" property as a u32 array instead of a u8 array to match the device tree bindings. It also introduces a bounds check on the array values to prevent out-of-bounds accesses.

Quoted 3 lines
drivers/gpu/drm/bridge/sii902x.c +0 -0

[ ... ]

drivers/gpu/drm/bridge/sii902x.c static int sii902x_audio_codec_init(struct sii902x *sii902x, · line 894

Quoted 7 lines
drivers/gpu/drm/bridge/sii902x.c +2 -1
@@ -890,9 +890,12 @@ static int sii902x_audio_codec_init(struct sii902x *sii902x,
 	}
 	codec_data.max_i2s_channels = 2 * num_lanes;
 
-	for (i = 0; i < num_lanes; i++)
+	for (i = 0; i < num_lanes; i++) {
+		if (WARN_ON(lanes[i] >= ARRAY_SIZE(i2s_lane_id)))

[Severity: Medium]
Is it safe to use WARN_ON() for validating device tree properties?

If panic_on_warn is enabled, couldn't this crash the system when parsing malformed or untrusted device tree data?

Would it be better to use dev_err() and gracefully fail initialization?

[Severity: Medium]
If this loop breaks early due to an invalid lane, codec_data.max_i2s_channels remains set to 2 * num_lanes.

Could this cause an inconsistent state where ALSA exposes support for more channels than the hardware has actually configured in i2s_fifo_sequence?

Should this path return an error (like -EINVAL) to abort audio initialization, or update the channel count?

Quoted 4 lines
drivers/gpu/drm/bridge/sii902x.c +2 -0
+			break;
 		sii902x->audio.i2s_fifo_sequence[i] |= audio_fifo_id[i] |
 			i2s_lane_id[lanes[i]] |	SII902X_TPI_I2S_FIFO_ENABLE;
+	}
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1