Skip to contentSkip to conversation

drm/amd/amdgpu: Validate ip discovery blob

PATCH In review Ernst Sjöstrand 2 participants 2 messages Sep 26, 2021 – Sep 27, 2021

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

PATCH 1/1 drm/amd/amdgpu: Validate ip discovery blob

We use the number_instance index that we get from the fw discovery blob to index into an array for example.

---
 drivers/gpu/drm/amd/amdgpu/amdgpu_discovery.c | 19 +++++++++++++++++++
 1 file changed, 19 insertions(+)
drivers/gpu/drm/amd/amdgpu/amdgpu_discovery.c +19 -0
index ada7bc19118a..b3fc46ba8144 100644
@@ -245,6 +245,20 @@ void amdgpu_discovery_fini(struct amdgpu_device *adev)
 	adev->mman.discovery_bin = NULL;
 }
 
+static int amdgpu_discovery_validate_ip(const struct ip *ip)
+{
+	if (ip->number_instance >= HWIP_MAX_INSTANCE) {
+		DRM_ERROR("Unexpected number_instance from ip discovery blob\n");
+		return -EINVAL;
+	}
+	if (le16_to_cpu(ip->hw_id) >= HW_ID_MAX) {
+		DRM_ERROR("Unexpected hw_id from ip discovery blob\n");
+		return -EINVAL;
+	}
+
+	return 0;
+}
+
 int amdgpu_discovery_reg_base_init(struct amdgpu_device *adev)
 {
 	struct binary_header *bhdr;
@@ -290,6 +304,10 @@ int amdgpu_discovery_reg_base_init(struct amdgpu_device *adev)
 
 		for (j = 0; j < num_ips; j++) {
 			ip = (struct ip *)(adev->mman.discovery_bin + ip_offset);
+
+			if (amdgpu_discovery_validate_ip(ip))
+				goto next_ip;
+
 			num_base_address = ip->num_base_address;
 
 			DRM_DEBUG("%s(%d) #%d v%d.%d.%d:\n",
@@ -321,6 +339,7 @@ int amdgpu_discovery_reg_base_init(struct amdgpu_device *adev)
 
 			}
 
+next_ip:
 			ip_offset += sizeof(*ip) + 4 * (ip->num_base_address - 1);
 		}
 	}
Signature · 1 line
2.30.2
#1 lore raw

Applied with minor updates to the error messages.

Thanks!

Alex

Quoted 56 lines · Ernst Sjöstrand

We use the number_instance index that we get from the fw discovery blob to index into an array for example.

---
 drivers/gpu/drm/amd/amdgpu/amdgpu_discovery.c | 19 +++++++++++++++++++
 1 file changed, 19 insertions(+)
drivers/gpu/drm/amd/amdgpu/amdgpu_discovery.c +19 -1
index ada7bc19118a..b3fc46ba8144 100644
@@ -245,6 +245,20 @@ void amdgpu_discovery_fini(struct amdgpu_device *adev)
        adev->mman.discovery_bin = NULL;
 }
 
+static int amdgpu_discovery_validate_ip(const struct ip *ip)
+{
+       if (ip->number_instance >= HWIP_MAX_INSTANCE) {
+               DRM_ERROR("Unexpected number_instance from ip discovery blob\n");
+               return -EINVAL;
+       }
+       if (le16_to_cpu(ip->hw_id) >= HW_ID_MAX) {
+               DRM_ERROR("Unexpected hw_id from ip discovery blob\n");
+               return -EINVAL;
+       }
+
+       return 0;
+}
+
 int amdgpu_discovery_reg_base_init(struct amdgpu_device *adev)
 {
        struct binary_header *bhdr;
@@ -290,6 +304,10 @@ int amdgpu_discovery_reg_base_init(struct amdgpu_device *adev)
 
                for (j = 0; j < num_ips; j++) {
                        ip = (struct ip *)(adev->mman.discovery_bin + ip_offset);
+
+                       if (amdgpu_discovery_validate_ip(ip))
+                               goto next_ip;
+
                        num_base_address = ip->num_base_address;
 
                        DRM_DEBUG("%s(%d) #%d v%d.%d.%d:\n",
@@ -321,6 +339,7 @@ int amdgpu_discovery_reg_base_init(struct amdgpu_device *adev)
 
                        }
 
+next_ip:
                        ip_offset += sizeof(*ip) + 4 * (ip->num_base_address - 1);
                }
        }
--

2.30.2