Skip to contentSkip to conversation

drm/amdgpu: Fix potential out-of-bounds access in 'amdgpu_discovery_reg_base_init()'

PATCH New Vasiliy Kovalev 1 participant 1 message Oct 24, 2025 Stable branch

lore
Share & tools
lore
#0 lore raw

PATCH v2 1/1 drm/amdgpu: Fix potential out-of-bounds access in 'amdgpu_discovery_reg_base_init()'

From: Srinivasan Shanmugam <[email protected]>

commit cdb637d339572398821204a1142d8d615668f1e9 upstream.

The issue arises when the array 'adev->vcn.vcn_config' is accessed before checking if the index 'adev->vcn.num_vcn_inst' is within the bounds of the array.

The fix involves moving the bounds check before the array access. This ensures that 'adev->vcn.num_vcn_inst' is within the bounds of the array before it is used as an index.

Fixes the below:
drivers/gpu/drm/amd/amdgpu/amdgpu_discovery.c:1289 amdgpu_discovery_reg_base_init() error: testing array offset 'adev->vcn.num_vcn_inst' after use.

Fixes a0ccc717c4ab ("drm/amdgpu/discovery: validate VCN and SDMA instances")
Cc Christian König <[email protected]>
Cc Alex Deucher <[email protected]>
[ kovalev: bp to fix CVE-2024-27042 ]

---
v2: Added braces around the single statement in the else block
to comply with kernel coding style.

---
 drivers/gpu/drm/amd/amdgpu/amdgpu_discovery.c | 11 ++++++-----
 1 file changed, 6 insertions(+), 5 deletions(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_discovery.c +6 -5
index d8441e273cb5..9274ac361203 100644
@@ -1128,15 +1128,16 @@ static int amdgpu_discovery_reg_base_init(struct amdgpu_device *adev)
 				 *     0b10 : encode is disabled
 				 *     0b01 : decode is disabled
 				 */
-				adev->vcn.vcn_config[adev->vcn.num_vcn_inst] =
-					ip->revision & 0xc0;
-				ip->revision &= ~0xc0;
-				if (adev->vcn.num_vcn_inst < AMDGPU_MAX_VCN_INSTANCES)
+				if (adev->vcn.num_vcn_inst < AMDGPU_MAX_VCN_INSTANCES) {
+					adev->vcn.vcn_config[adev->vcn.num_vcn_inst] =
+						ip->revision & 0xc0;
 					adev->vcn.num_vcn_inst++;
-				else
+				} else {
 					dev_err(adev->dev, "Too many VCN instances: %d vs %d\n",
 						adev->vcn.num_vcn_inst + 1,
 						AMDGPU_MAX_VCN_INSTANCES);
+				}
+				ip->revision &= ~0xc0;
 			}
 			if (le16_to_cpu(ip->hw_id) == SDMA0_HWID ||
 			    le16_to_cpu(ip->hw_id) == SDMA1_HWID ||
Signature · 1 line
2.50.1