Skip to contentSkip to conversation

drm/amdgpu: Avoid possible buffer overflow

PATCH Superseded Su Hui 2 participants 3 messages Aug 21, 2023

Subsystems (3)

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore

Su Hui

author
#0 lore raw

PATCH 1/1 drm/amdgpu: Avoid possible buffer overflow

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

change the assignment order to avoid buffer overflow.

Fixes c40bdfb2ffa4 ("drm/amdgpu: fix incorrect VCN revision in SRIOV")
---
 drivers/gpu/drm/amd/amdgpu/amdgpu_discovery.c | 5 +++--
 1 file changed, 3 insertions(+), 2 deletions(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_discovery.c +3 -2
index 8e1cfc87122d..ba95526c3d45 100644
@@ -1250,11 +1250,12 @@ 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) {
+					adev->vcn.vcn_config[adev->vcn.num_vcn_inst] =
+						ip->revision & 0xc0;
 					adev->vcn.num_vcn_inst++;
 					adev->vcn.inst_mask |=
 						(1U << ip->instance_number);
Signature · 1 line
2.30.2
#1 lore raw

Le 21/08/2023 à 08:19, Su Hui a écrit :

Quoted 18 lines

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

change the assignment order to avoid buffer overflow.

Fixes c40bdfb2ffa4 ("drm/amdgpu: fix incorrect VCN revision in SRIOV")
---
  drivers/gpu/drm/amd/amdgpu/amdgpu_discovery.c | 5 +++--
  1 file changed, 3 insertions(+), 2 deletions(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_discovery.c +0 -0
index 8e1cfc87122d..ba95526c3d45 100644
@@ -1250,11 +1250,12 @@ static int amdgpu_discovery_reg_base_init(struct amdgpu_device *adev)
  				 *     0b10 : encode is disabled

drivers/gpu/drm/amd/amdgpu/amdgpu_discovery.c static int amdgpu_discovery_reg_base_init(struct amdgpu_device *adev) · line 1258

Quoted 10 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_discovery.c +3 -2
  				 *     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) {
+					adev->vcn.vcn_config[adev->vcn.num_vcn_inst] =
+						ip->revision & 0xc0;

Hi,
I don't think that the patch is correct.

Because of the "ip->revision &= ~0xc0" which is now above it, "ip->revision & 0xc0" should now always be 0.

Maybe both lines should be moved within the "if"?

CJ

Quoted 3 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_discovery.c +0 -0
  					adev->vcn.num_vcn_inst++;
  					adev->vcn.inst_mask |=
  						(1U << ip->instance_number);
author
#2 lore raw
Quoted 34 lines · Christophe JAILLET

Le 21/08/2023 à 08:19, Su Hui a écrit :

Quoted 25 lines

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

change the assignment order to avoid buffer overflow.

Fixes c40bdfb2ffa4 ("drm/amdgpu: fix incorrect VCN revision in SRIOV")

---
  drivers/gpu/drm/amd/amdgpu/amdgpu_discovery.c | 5 +++--

  1 file changed, 3 insertions(+), 2 deletions(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_discovery.c +1 -2
index 8e1cfc87122d..ba95526c3d45 100644
@@ -1250,11 +1250,12 @@ 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;
Quoted 4 lines

                  if (adev->vcn.num_vcn_inst <
                      AMDGPU_MAX_VCN_INSTANCES) {
+ adev->vcn.vcn_config[adev->vcn.num_vcn_inst] =
+                        ip->revision & 0xc0;

Hi,
I don't think that the patch is correct.

Quoted 5 lines

Because of the "ip->revision &= ~0xc0" which is now above it, "ip->revision & 0xc0" should now always be 0.

Maybe both lines should be moved within the "if"?

CJ

Hi,

Oh, really sorry for this, I just missed this line. you are right, I will send v2 soon.
Thanks for your reminder!

Su Hui

Quoted 9 lines
Quoted 8 lines

adev->vcn.num_vcn_inst++;
                      adev->vcn.inst_mask |= (1U << ip->instance_number);