Skip to contentSkip to conversation

drm/amdgpu/gfx8: fix register_list_format leak in gfx_v8_0_init_microcode

PATCH Applied Willian Oliveira 3 participants 3 messages Sep 5 – Sep 8

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

PATCH 1/1 drm/amdgpu/gfx8: fix register_list_format leak in gfx_v8_0_init_microcode

gfx_v8_0_init_microcode() allocates rlc.register_list_format before loading the MEC firmware.

If loading one of the subsequent firmware files fails, the function returns through the error path without freeing register_list_format.

Use gfx_v8_0_free_microcode() in the error path so all previously allocated firmware resources, including register_list_format, are released.

---
 drivers/gpu/drm/amd/amdgpu/gfx_v8_0.c | 7 +------
 1 file changed, 1 insertion(+), 6 deletions(-)
drivers/gpu/drm/amd/amdgpu/gfx_v8_0.c +1 -6
index 7f91186ef1d1..b213870ecb96 100644
@@ -1211,12 +1211,7 @@ static int gfx_v8_0_init_microcode(struct amdgpu_device *adev)
 out:
 	if (err) {
 		dev_err(adev->dev, "gfx8: Failed to load firmware %s gfx firmware\n", chip_name);
-		amdgpu_ucode_release(&adev->gfx.pfp_fw);
-		amdgpu_ucode_release(&adev->gfx.me_fw);
-		amdgpu_ucode_release(&adev->gfx.ce_fw);
-		amdgpu_ucode_release(&adev->gfx.rlc_fw);
-		amdgpu_ucode_release(&adev->gfx.mec_fw);
-		amdgpu_ucode_release(&adev->gfx.mec2_fw);
+		gfx_v8_0_free_microcode(adev);
 	}
 	return err;
 }
Signature · 1 line
2.47.3
#1 lore raw
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:

Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:

Pre-existing issues:
- [Critical] Missing bounds checking and potential integer overflow in firmware header parsing leading to a heap buffer overflow. --

commit 85ca74ba24e5f9c96f4d94f3bcb428aa05e58c7a
Author: Willian Oliveira <[email protected]>

drm/amdgpu/gfx8: fix register_list_format leak in gfx_v8_0_init_microcode

gfx_v8_0_init_microcode() allocates rlc.register_list_format before loading the MEC firmware.

If loading one of the subsequent firmware files fails, the function returns through the error path without freeing register_list_format.

Use gfx_v8_0_free_microcode() in the error path so all previously allocated firmware resources, including register_list_format, are released.

Quoted 5 lines
drivers/gpu/drm/amd/amdgpu/gfx_v8_0.c +0 -0
index 6cf4279950787..92c56d811975c 100644

drivers/gpu/drm/amd/amdgpu/gfx_v8_0.c static int gfx_v8_0_init_microcode(struct amdgpu_device *adev) · line 1214

Quoted 10 lines
drivers/gpu/drm/amd/amdgpu/gfx_v8_0.c +1 -6
@@ -1211,12 +1211,7 @@ static int gfx_v8_0_init_microcode(struct amdgpu_device *adev)
 out:
 	if (err) {
 		dev_err(adev->dev, "gfx8: Failed to load firmware %s gfx firmware\n", chip_name);
-		amdgpu_ucode_release(&adev->gfx.pfp_fw);
-		amdgpu_ucode_release(&adev->gfx.me_fw);
-		amdgpu_ucode_release(&adev->gfx.ce_fw);
-		amdgpu_ucode_release(&adev->gfx.rlc_fw);
-		amdgpu_ucode_release(&adev->gfx.mec_fw);
-		amdgpu_ucode_release(&adev->gfx.mec2_fw);
+		gfx_v8_0_free_microcode(adev);

[Severity: Critical]
This is a pre-existing issue, but while reviewing this function's error handling, I noticed a potential integer overflow in the allocation of register_list_format higher up in gfx_v8_0_init_microcode():

	adev->gfx.rlc.reg_list_format_size_bytes =
			le32_to_cpu(rlc_hdr->reg_list_format_size_bytes);
	adev->gfx.rlc.reg_list_size_bytes =
			le32_to_cpu(rlc_hdr->reg_list_size_bytes);

	adev->gfx.rlc.register_list_format =
			kmalloc(adev->gfx.rlc.reg_list_format_size_bytes +
					adev->gfx.rlc.reg_list_size_bytes, GFP_KERNEL);

Are we sure reg_list_format_size_bytes and reg_list_size_bytes can't overflow when added together?

If a maliciously crafted RLC firmware provides large values for these two variables, the 32-bit addition could wrap around, resulting in a very small allocation.

Because the subsequent loops iterate over the original un-overflowed values like this:

	for (i = 0 ; i < (adev->gfx.rlc.reg_list_format_size_bytes >> 2); i++)
		adev->gfx.rlc.register_list_format[i] = le32_to_cpu(tmp[i]);

could this cause out-of-bounds writes into the undersized heap buffer?

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1
Applied
#2 lore raw
In reply to Willian Oliveira

Applied. Thanks!

Quoted 37 lines · Willian Oliveira

gfx_v8_0_init_microcode() allocates rlc.register_list_format before loading the MEC firmware.

If loading one of the subsequent firmware files fails, the function returns through the error path without freeing register_list_format.

Use gfx_v8_0_free_microcode() in the error path so all previously allocated firmware resources, including register_list_format, are released.

---
 drivers/gpu/drm/amd/amdgpu/gfx_v8_0.c | 7 +------
 1 file changed, 1 insertion(+), 6 deletions(-)
drivers/gpu/drm/amd/amdgpu/gfx_v8_0.c +1 -7
index 7f91186ef1d1..b213870ecb96 100644
@@ -1211,12 +1211,7 @@ static int gfx_v8_0_init_microcode(struct amdgpu_device *adev)
 out:
        if (err) {
                dev_err(adev->dev, "gfx8: Failed to load firmware %s gfx firmware\n", chip_name);
-               amdgpu_ucode_release(&adev->gfx.pfp_fw);
-               amdgpu_ucode_release(&adev->gfx.me_fw);
-               amdgpu_ucode_release(&adev->gfx.ce_fw);
-               amdgpu_ucode_release(&adev->gfx.rlc_fw);
-               amdgpu_ucode_release(&adev->gfx.mec_fw);
-               amdgpu_ucode_release(&adev->gfx.mec2_fw);
+               gfx_v8_0_free_microcode(adev);
        }
        return err;
 }
--

2.47.3