Skip to contentSkip to conversation

drm/amdgpu: Fix possible NULL pointer dereference

PATCH Superseded Li Huafei 2 participants 3 messages Oct 28, 2024 – Oct 29, 2024

Subsystems (2)

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore

Li Huafei

author
#0 lore raw

PATCH 1/1 drm/amdgpu: Fix possible NULL pointer dereference

Fix the potential NULL pointer dereference on mem_ranges in amdgpu_discovery_get_nps_info(). Additionally, assign the output parameters nps_type and range_cnt after the kvzalloc() call to prevent modifying the output parameters in case of an error return.

Fixes b194d21b9bcc ("drm/amdgpu: Use NPS ranges from discovery table")
---
 drivers/gpu/drm/amd/amdgpu/amdgpu_discovery.c | 6 ++++--
 1 file changed, 4 insertions(+), 2 deletions(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_discovery.c +4 -2
index 4bd61c169ca8..1b1aedebb3c2 100644
@@ -1757,11 +1757,13 @@ int amdgpu_discovery_get_nps_info(struct amdgpu_device *adev,
 
 	switch (le16_to_cpu(nps_info->v1.header.version_major)) {
 	case 1:
-		*nps_type = nps_info->v1.nps_type;
-		*range_cnt = nps_info->v1.count;
 		mem_ranges = kvzalloc(
 			*range_cnt * sizeof(struct amdgpu_gmc_memrange),
 			GFP_KERNEL);
+		if (!mem_ranges)
+			return -ENOMEM;
+		*nps_type = nps_info->v1.nps_type;
+		*range_cnt = nps_info->v1.count;
 		for (i = 0; i < *range_cnt; i++) {
 			mem_ranges[i].base_address =
 				nps_info->v1.instance_info[i].base_address;
Signature · 1 line
2.25.1
#1 lore raw
Quoted 16 lines · Li Huafei

Fix the potential NULL pointer dereference on mem_ranges in amdgpu_discovery_get_nps_info(). Additionally, assign the output parameters nps_type and range_cnt after the kvzalloc() call to prevent modifying the output parameters in case of an error return.

Fixes b194d21b9bcc ("drm/amdgpu: Use NPS ranges from discovery table")
---
 drivers/gpu/drm/amd/amdgpu/amdgpu_discovery.c | 6 ++++--
 1 file changed, 4 insertions(+), 2 deletions(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_discovery.c +0 -0
index 4bd61c169ca8..1b1aedebb3c2 100644

drivers/gpu/drm/amd/amdgpu/amdgpu_discovery.c int amdgpu_discovery_get_nps_info(struct amdgpu_device *adev, · line 1761

Quoted 5 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_discovery.c +0 -2
@@ -1757,11 +1757,13 @@ int amdgpu_discovery_get_nps_info(struct amdgpu_device *adev,
 
 	switch (le16_to_cpu(nps_info->v1.header.version_major)) {
 	case 1:
-		*nps_type = nps_info->v1.nps_type;
-		*range_cnt = nps_info->v1.count;

This one is used in the allocation call below. If you prefer to not assign any of the out params before memory allocation is successful, then use nps_info->v1.count in the alloc call.

Thanks for adding the alloc fail check.

Thanks,
Lijo

Quoted 10 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_discovery.c +4 -0
 		mem_ranges = kvzalloc(
 			*range_cnt * sizeof(struct amdgpu_gmc_memrange),
 			GFP_KERNEL);
+		if (!mem_ranges)
+			return -ENOMEM;
+		*nps_type = nps_info->v1.nps_type;
+		*range_cnt = nps_info->v1.count;
 		for (i = 0; i < *range_cnt; i++) {
 			mem_ranges[i].base_address =
 				nps_info->v1.instance_info[i].base_address;
author
#2 lore raw
Quoted 20 lines · Lijo
Quoted 17 lines · Li Huafei

Fix the potential NULL pointer dereference on mem_ranges in amdgpu_discovery_get_nps_info(). Additionally, assign the output parameters nps_type and range_cnt after the kvzalloc() call to prevent modifying the output parameters in case of an error return.

Fixes b194d21b9bcc ("drm/amdgpu: Use NPS ranges from discovery table")
---
 drivers/gpu/drm/amd/amdgpu/amdgpu_discovery.c | 6 ++++--
 1 file changed, 4 insertions(+), 2 deletions(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_discovery.c +0 -0
index 4bd61c169ca8..1b1aedebb3c2 100644
@@ -1757,11 +1757,13 @@ int amdgpu_discovery_get_nps_info(struct amdgpu_device *adev,
 
Quoted 9 lines

drivers/gpu/drm/amd/amdgpu/amdgpu_discovery.c int amdgpu_discovery_get_nps_info(struct amdgpu_device *adev, · line 1761

Quoted 4 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_discovery.c +0 -2
 	switch (le16_to_cpu(nps_info->v1.header.version_major)) {
 	case 1:
-		*nps_type = nps_info->v1.nps_type;
-		*range_cnt = nps_info->v1.count;

This one is used in the allocation call below. If you prefer to not assign any of the out params before memory allocation is successful, then use nps_info->v1.count in the alloc call.

too bad! I missed it. I will send v2 to fix it.

Thanks,
Huafei

Quoted 16 lines

Thanks for adding the alloc fail check.

Thanks,
Lijo

drivers/gpu/drm/amd/amdgpu/amdgpu_discovery.c int amdgpu_discovery_get_nps_info(struct amdgpu_device *adev, · line 1769

Quoted 10 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_discovery.c +4 -0
 		mem_ranges = kvzalloc(
 			*range_cnt * sizeof(struct amdgpu_gmc_memrange),
 			GFP_KERNEL);
+		if (!mem_ranges)
+			return -ENOMEM;
+		*nps_type = nps_info->v1.nps_type;
+		*range_cnt = nps_info->v1.count;
 		for (i = 0; i < *range_cnt; i++) {
 			mem_ranges[i].base_address =
 				nps_info->v1.instance_info[i].base_address;

.