Skip to contentSkip to conversation

drm/amdgpu: Fix out-of-bounds write warning

PATCH Superseded Ma Jun 5 participants 10 messages Apr 25, 2024 – Apr 26, 2024

Subsystems (2)

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore

Ma Jun

author
#0 lore raw

PATCH 1/1 drm/amdgpu: Fix out-of-bounds write warning

Check the ring type value to fix the out-of-bounds write warning

---
 drivers/gpu/drm/amd/amdgpu/amdgpu_ring.c | 5 +++++
 1 file changed, 5 insertions(+)
drivers/gpu/drm/amd/amdgpu/amdgpu_ring.c +5 -0
index 06f0a6534a94..1e0b5bb47bc9 100644
@@ -353,6 +353,11 @@ int amdgpu_ring_init(struct amdgpu_device *adev, struct amdgpu_ring *ring,
 	ring->hw_prio = hw_prio;
 
 	if (!ring->no_scheduler) {
+		if (ring->funcs->type >= AMDGPU_HW_IP_NUM) {
+			dev_warn(adev->dev, "ring type %d has no scheduler\n", ring->funcs->type);
+			return 0;
+		}
+
 		hw_ip = ring->funcs->type;
 		num_sched = &adev->gpu_sched[hw_ip][hw_prio].num_scheds;
 		adev->gpu_sched[hw_ip][hw_prio].sched[(*num_sched)++] =
Signature · 1 line
2.34.1
author
#1 lore raw

PATCH v2 1/2 drm/amdgpu: Fix uninitialized variable warning in amdgpu_afmt_acr

Assign value to clock to fix the warning below:
"Using uninitialized value res. Field res.clock is uninitialized"

---
 drivers/gpu/drm/amd/amdgpu/amdgpu_afmt.c | 1 +
 1 file changed, 1 insertion(+)
drivers/gpu/drm/amd/amdgpu/amdgpu_afmt.c +1 -0
index a4d65973bf7c..80771b1480ff 100644
@@ -100,6 +100,7 @@ struct amdgpu_afmt_acr amdgpu_afmt_acr(uint32_t clock)
 	amdgpu_afmt_calc_cts(clock, &res.cts_32khz, &res.n_32khz, 32000);
 	amdgpu_afmt_calc_cts(clock, &res.cts_44_1khz, &res.n_44_1khz, 44100);
 	amdgpu_afmt_calc_cts(clock, &res.cts_48khz, &res.n_48khz, 48000);
+	res.clock = clock;
 
 	return res;
 }
Signature · 1 line
2.34.1
Reviewed-by on v2 1/2
#2 lore raw

Re: [PATCH v2 1/2] drm/amdgpu: Fix uninitialized variable warning in amdgpu_afmt_acr

Quoted 5 lines · Ma Jun

Assign value to clock to fix the warning below:
"Using uninitialized value res. Field res.clock is uninitialized"

Quoted 19 lines
---
 drivers/gpu/drm/amd/amdgpu/amdgpu_afmt.c | 1 +
 1 file changed, 1 insertion(+)
drivers/gpu/drm/amd/amdgpu/amdgpu_afmt.c +1 -1
index a4d65973bf7c..80771b1480ff 100644
@@ -100,6 +100,7 @@ struct amdgpu_afmt_acr amdgpu_afmt_acr(uint32_t clock)
        amdgpu_afmt_calc_cts(clock, &res.cts_32khz, &res.n_32khz, 32000);
        amdgpu_afmt_calc_cts(clock, &res.cts_44_1khz, &res.n_44_1khz, 44100);
        amdgpu_afmt_calc_cts(clock, &res.cts_48khz, &res.n_48khz, 48000);
+       res.clock = clock;
 
        return res;
 }
--

2.34.1

author
#3 lore raw

PATCH v2 2/2 drm/amdgpu: Fix the uninitialized variable warning

In reply to Ma Jun

Initialize the phy_id to 0 to fix the warning of
"Using uninitialized value phy_id"

---
 drivers/gpu/drm/amd/amdgpu/amdgpu_securedisplay.c | 6 +++++-
 1 file changed, 5 insertions(+), 1 deletion(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_securedisplay.c +5 -1
index 8ed0e073656f..53d85fafd8ab 100644
@@ -95,7 +95,7 @@ static ssize_t amdgpu_securedisplay_debugfs_write(struct file *f, const char __u
 	struct psp_context *psp = &adev->psp;
 	struct ta_securedisplay_cmd *securedisplay_cmd;
 	struct drm_device *dev = adev_to_drm(adev);
-	uint32_t phy_id;
+	uint32_t phy_id = 0;
 	uint32_t op;
 	char str[64];
 	int ret;
@@ -135,6 +135,10 @@ static ssize_t amdgpu_securedisplay_debugfs_write(struct file *f, const char __u
 		mutex_unlock(&psp->securedisplay_context.mutex);
 		break;
 	case 2:
+		if (size < 3) {
+			dev_err(adev->dev, "Invalid input: %s\n", str);
+			return -EINVAL;
+		}
 		mutex_lock(&psp->securedisplay_context.mutex);
 		psp_prep_securedisplay_cmd_buf(psp, &securedisplay_cmd,
 			TA_SECUREDISPLAY_COMMAND__SEND_ROI_CRC);
Signature · 1 line
2.34.1
#4 lore raw

Re: [PATCH v2 2/2] drm/amdgpu: Fix the uninitialized variable warning

Quoted 22 lines · Ma Jun

Initialize the phy_id to 0 to fix the warning of
"Using uninitialized value phy_id"

---
 drivers/gpu/drm/amd/amdgpu/amdgpu_securedisplay.c | 6 +++++-
 1 file changed, 5 insertions(+), 1 deletion(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_securedisplay.c +1 -1
index 8ed0e073656f..53d85fafd8ab 100644
@@ -95,7 +95,7 @@ static ssize_t amdgpu_securedisplay_debugfs_write(struct file *f, const char __u
 	struct psp_context *psp = &adev->psp;
 	struct ta_securedisplay_cmd *securedisplay_cmd;
 	struct drm_device *dev = adev_to_drm(adev);
-	uint32_t phy_id;
+	uint32_t phy_id = 0;
 	uint32_t op;
 	char str[64];
 	int ret;

drivers/gpu/drm/amd/amdgpu/amdgpu_securedisplay.c static ssize_t amdgpu_securedisplay_debugfs_write(struct file *f, const char __u · line 141

Quoted 7 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_securedisplay.c +4 -0
@@ -135,6 +135,10 @@ static ssize_t amdgpu_securedisplay_debugfs_write(struct file *f, const char __u
 		mutex_unlock(&psp->securedisplay_context.mutex);
 		break;
 	case 2:
+		if (size < 3) {
+			dev_err(adev->dev, "Invalid input: %s\n", str);
+			return -EINVAL;
+		}

Better is to check the return of sscanf to see if phy_id value is successfully scanned. Otherwise, return error.

Thanks,
Lijo

Quoted 3 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_securedisplay.c +0 -0
 		mutex_lock(&psp->securedisplay_context.mutex);
 		psp_prep_securedisplay_cmd_buf(psp, &securedisplay_cmd,
 			TA_SECUREDISPLAY_COMMAND__SEND_ROI_CRC);
#5 lore raw

Re: [PATCH v2 2/2] drm/amdgpu: Fix the uninitialized variable warning

Quoted 27 lines · Lijo
Quoted 24 lines · Ma Jun

Initialize the phy_id to 0 to fix the warning of
"Using uninitialized value phy_id"

---
 drivers/gpu/drm/amd/amdgpu/amdgpu_securedisplay.c | 6 +++++-
 1 file changed, 5 insertions(+), 1 deletion(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_securedisplay.c +1 -1
index 8ed0e073656f..53d85fafd8ab 100644
@@ -95,7 +95,7 @@ static ssize_t amdgpu_securedisplay_debugfs_write(struct file *f, const char __u
 	struct psp_context *psp = &adev->psp;
 	struct ta_securedisplay_cmd *securedisplay_cmd;
 	struct drm_device *dev = adev_to_drm(adev);
-	uint32_t phy_id;
+	uint32_t phy_id = 0;
 	uint32_t op;
 	char str[64];
 	int ret;
@@ -135,6 +135,10 @@ static ssize_t amdgpu_securedisplay_debugfs_write(struct file *f, const char __u
 		mutex_unlock(&psp->securedisplay_context.mutex);
 		break;
Quoted 8 lines

drivers/gpu/drm/amd/amdgpu/amdgpu_securedisplay.c static ssize_t amdgpu_securedisplay_debugfs_write(struct file *f, const char __u · line 141

Quoted 5 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_securedisplay.c +4 -0
 	case 2:
+		if (size < 3) {
+			dev_err(adev->dev, "Invalid input: %s\n", str);
+			return -EINVAL;
+		}

Better is to check the return of sscanf to see if phy_id value is successfully scanned. Otherwise, return error.

Thanks, will fix in v3

Regards,
Ma Jun

Quoted 7 lines

Thanks,
Lijo

Quoted 3 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_securedisplay.c +0 -0
 		mutex_lock(&psp->securedisplay_context.mutex);
 		psp_prep_securedisplay_cmd_buf(psp, &securedisplay_cmd,
 			TA_SECUREDISPLAY_COMMAND__SEND_ROI_CRC);
#6 lore raw

Re: [PATCH v2 2/2] drm/amdgpu: Fix the uninitialized variable warning

In reply to Ma Jun
Quoted 14 lines · Ma Jun

Initialize the phy_id to 0 to fix the warning of
"Using uninitialized value phy_id"

---
 drivers/gpu/drm/amd/amdgpu/amdgpu_securedisplay.c | 6 +++++-
 1 file changed, 5 insertions(+), 1 deletion(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_securedisplay.c +0 -0
index 8ed0e073656f..53d85fafd8ab 100644

drivers/gpu/drm/amd/amdgpu/amdgpu_securedisplay.c static ssize_t amdgpu_securedisplay_debugfs_write(struct file *f, const char __u · line 98

Quoted 5 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_securedisplay.c +1 -1
@@ -95,7 +95,7 @@ static ssize_t amdgpu_securedisplay_debugfs_write(struct file *f, const char __u
        struct psp_context *psp = &adev->psp;
        struct ta_securedisplay_cmd *securedisplay_cmd;
        struct drm_device *dev = adev_to_drm(adev);
-       uint32_t phy_id;
+       uint32_t phy_id = 0;

You can drop this hunk now that you added the check below.

Alex

Quoted 17 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_securedisplay.c +4 -1
        uint32_t op;
        char str[64];
        int ret;
@@ -135,6 +135,10 @@ static ssize_t amdgpu_securedisplay_debugfs_write(struct file *f, const char __u
                mutex_unlock(&psp->securedisplay_context.mutex);
                break;
        case 2:
+               if (size < 3) {
+                       dev_err(adev->dev, "Invalid input: %s\n", str);
+                       return -EINVAL;
+               }
                mutex_lock(&psp->securedisplay_context.mutex);
                psp_prep_securedisplay_cmd_buf(psp, &securedisplay_cmd,
                        TA_SECUREDISPLAY_COMMAND__SEND_ROI_CRC);
--

2.34.1

maintainer
#7 lore raw
In reply to Ma Jun

Am 25.04.24 um 12:00 schrieb Ma Jun:

Quoted 13 lines

Check the ring type value to fix the out-of-bounds write warning

---
  drivers/gpu/drm/amd/amdgpu/amdgpu_ring.c | 5 +++++
  1 file changed, 5 insertions(+)
drivers/gpu/drm/amd/amdgpu/amdgpu_ring.c +0 -0
index 06f0a6534a94..1e0b5bb47bc9 100644

drivers/gpu/drm/amd/amdgpu/amdgpu_ring.c int amdgpu_ring_init(struct amdgpu_device *adev, struct amdgpu_ring *ring, · line 360

Quoted 8 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_ring.c +5 -0
@@ -353,6 +353,11 @@ int amdgpu_ring_init(struct amdgpu_device *adev, struct amdgpu_ring *ring,
  	ring->hw_prio = hw_prio;
 
  	if (!ring->no_scheduler) {
+		if (ring->funcs->type >= AMDGPU_HW_IP_NUM) {
+			dev_warn(adev->dev, "ring type %d has no scheduler\n", ring->funcs->type);
+			return 0;
+		}
+

That check should probably be at the beginning of the function since trying to initialize a ring with an invalid type should be rejected immediately.

Regards,
Christian.

Quoted 3 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_ring.c +0 -0
  		hw_ip = ring->funcs->type;
  		num_sched = &adev->gpu_sched[hw_ip][hw_prio].num_scheds;
  		adev->gpu_sched[hw_ip][hw_prio].sched[(*num_sched)++] =
#8 lore raw
Quoted 18 lines · Christian König

Am 25.04.24 um 12:00 schrieb Ma Jun:

Quoted 15 lines

Check the ring type value to fix the out-of-bounds write warning

---
  drivers/gpu/drm/amd/amdgpu/amdgpu_ring.c | 5 +++++
  1 file changed, 5 insertions(+)
drivers/gpu/drm/amd/amdgpu/amdgpu_ring.c +0 -0
index 06f0a6534a94..1e0b5bb47bc9 100644
@@ -353,6 +353,11 @@ int amdgpu_ring_init(struct amdgpu_device *adev, struct amdgpu_ring *ring,
  	ring->hw_prio = hw_prio;
 
Quoted 11 lines

drivers/gpu/drm/amd/amdgpu/amdgpu_ring.c int amdgpu_ring_init(struct amdgpu_device *adev, struct amdgpu_ring *ring, · line 360

Quoted 6 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_ring.c +5 -0
  	if (!ring->no_scheduler) {
+		if (ring->funcs->type >= AMDGPU_HW_IP_NUM) {
+			dev_warn(adev->dev, "ring type %d has no scheduler\n", ring->funcs->type);
+			return 0;
+		}
+

That check should probably be at the beginning of the function since trying to initialize a ring with an invalid type should be rejected immediately.

This check is used to skip the gpu_sched setting for the rings which don't have scheduler, such as KIQ, MES, UMSCH_MM. Without this check, there could be an potential out-of-bounds writing when ring->no__scheduler is not set correctly.

Regards,
Ma Jun

Quoted 7 lines

Regards,
Christian.

Quoted 3 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_ring.c +0 -0
  		hw_ip = ring->funcs->type;
  		num_sched = &adev->gpu_sched[hw_ip][hw_prio].num_scheds;
  		adev->gpu_sched[hw_ip][hw_prio].sched[(*num_sched)++] =
maintainer
#9 lore raw

Am 26.04.24 um 05:24 schrieb Ma, Jun:

Quoted 27 lines · Christian König
Quoted 16 lines

Am 25.04.24 um 12:00 schrieb Ma Jun:

Quoted 15 lines

Check the ring type value to fix the out-of-bounds write warning

---
   drivers/gpu/drm/amd/amdgpu/amdgpu_ring.c | 5 +++++
   1 file changed, 5 insertions(+)
drivers/gpu/drm/amd/amdgpu/amdgpu_ring.c +0 -0
index 06f0a6534a94..1e0b5bb47bc9 100644
@@ -353,6 +353,11 @@ int amdgpu_ring_init(struct amdgpu_device *adev, struct amdgpu_ring *ring,
   	ring->hw_prio = hw_prio;
 
Quoted 10 lines

drivers/gpu/drm/amd/amdgpu/amdgpu_ring.c int amdgpu_ring_init(struct amdgpu_device *adev, struct amdgpu_ring *ring, · line 360

Quoted 6 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_ring.c +5 -0
   	if (!ring->no_scheduler) {
+		if (ring->funcs->type >= AMDGPU_HW_IP_NUM) {
+			dev_warn(adev->dev, "ring type %d has no scheduler\n", ring->funcs->type);
+			return 0;
+		}
+

That check should probably be at the beginning of the function since trying to initialize a ring with an invalid type should be rejected immediately.

Quoted 4 lines

This check is used to skip the gpu_sched setting for the rings which don't have scheduler, such as KIQ, MES, UMSCH_MM. Without this check, there could be an potential out-of-bounds writing when ring->no__scheduler is not set correctly.

Ah! In this case that that is not really clean.

Probably best approach is to change the "if (!ring->no_scheduler)" instead.

Maybe even move this code here into amdgpu_ctx.c:

                hw_ip = ring->funcs->type;
                num_sched = &adev->gpu_sched[hw_ip][hw_prio].num_scheds; adev->gpu_sched[hw_ip][hw_prio].sched[(*num_sched)++] = &ring->sched;

And then add something like hw_ip <= AMDGPU_HW_IP_NUM.

Background is that it is perfectly valid to have a scheduler for the MES for example.

Regards,
Christian.

Quoted 10 lines

Regards,
Ma Jun

Quoted 6 lines

Regards,
Christian.

Quoted 3 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_ring.c +0 -0
   		hw_ip = ring->funcs->type;
   		num_sched = &adev->gpu_sched[hw_ip][hw_prio].num_scheds;
   		adev->gpu_sched[hw_ip][hw_prio].sched[(*num_sched)++] =