Skip to contentSkip to conversation

drm/amdgpu: Fix stack buffer overflow in UALink vPod integrity check

PATCH Superseded Lin Huiyi 3 participants 12 messages Nov 30 – Dec 1

Subsystems (2)
lore
Share & tools
lore

Lin Huiyi

author
#0 lore raw

PATCH 1/1 drm/amdgpu: Fix stack buffer overflow in UALink vPod integrity check

__check_local_vpod_integrity() collects the accelerator IDs of all local GPUs that belong to the vPod into a fixed-size local_accels[] array. The loop is bounded by mgpu_info.num_gpu (up to MAX_GPU_INSTANCE, i.e. 64) and only filters peers by vPod membership, so every matching peer is appended unconditionally:

	local_accels[n_local_accels++] = accel_id;

There is no check that n_local_accels stays below AMDGPU_UALINK_LOCAL_ACCELS_MAX, so a pod with more local GPUs than the array can hold writes past the end of a stack array. The oversized count is then propagated to peer_info->n_local_accels, and the downstream loops in amdgpu_ualink_is_local_accel() and activate_local_vpod() walk past the end of the peer's local_accels[] array as well.

The firmware interface allows up to PSP_GFX_MAX_LOCAL_GPUS_UAL_V1 (16) local GPUs per node, so fix the capacity to that value and reject a pod that reports more local peers than the array can hold.

Fixes 0e8eb0dcea51 ("drm/amdgpu: Assign accel state based on ASP config")
---
 drivers/gpu/drm/amd/amdgpu/amdgpu_ualink.c | 8 +++++++-
 drivers/gpu/drm/amd/amdgpu/amdgpu_ualink.h | 2 +-
 2 files changed, 8 insertions(+), 2 deletions(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_ualink.c +7 -1
index d025b4183b..b4fac5ecab 100644
@@ -1243,6 +1243,12 @@ static int __check_local_vpod_integrity(struct amdgpu_device *adev)
 				 accel_id);
 			return -EINVAL;
 		}
+		if (n_local_accels >= AMDGPU_UALINK_LOCAL_ACCELS_MAX) {
+			dev_warn(adev->dev,
+				 "UALINK: integrity fail: too many local vpod peers: %u (max %u)\n",
+				 n_local_accels + 1, AMDGPU_UALINK_LOCAL_ACCELS_MAX);
+			return -EINVAL;
+		}
 		local_accels[n_local_accels++] = accel_id;
 	}
 
@@ -1257,7 +1263,7 @@ static int __check_local_vpod_integrity(struct amdgpu_device *adev)
 
 		peer_info->n_local_accels = n_local_accels;
 		memcpy(peer_info->local_accels, local_accels,
-		       sizeof(local_accels));
+		       n_local_accels * sizeof(*local_accels));
 	}
 
 	return 0;
drivers/gpu/drm/amd/amdgpu/amdgpu_ualink.h +1 -1
index 85c9a9634a..c8577a94a4 100644
@@ -34,7 +34,7 @@ struct amdgpu_device;
 struct amdgpu_xcp;
 
 #define AMDGPU_UALINK_ACCEL_MAX 256
-#define AMDGPU_UALINK_LOCAL_ACCELS_MAX 8
+#define AMDGPU_UALINK_LOCAL_ACCELS_MAX 16
 #define AMDGPU_UALINK_STATIONS_MAX 64
 
 /* A vpod_id of 0 is reserved and treated as invalid/no vPod */
Signature · 1 line
2.56.1
#1 lore raw
Quoted 47 lines · Lin Huiyi

[Some people who received this message don't often get email from [email protected]. Learn why this is important at https://aka.ms/LearnAboutSenderIdentification ]

__check_local_vpod_integrity() collects the accelerator IDs of all local GPUs that belong to the vPod into a fixed-size local_accels[] array. The loop is bounded by mgpu_info.num_gpu (up to MAX_GPU_INSTANCE, i.e. 64) and only filters peers by vPod membership, so every matching peer is appended unconditionally:

         local_accels[n_local_accels++] = accel_id;

There is no check that n_local_accels stays below AMDGPU_UALINK_LOCAL_ACCELS_MAX, so a pod with more local GPUs than the array can hold writes past the end of a stack array. The oversized count is then propagated to peer_info->n_local_accels, and the downstream loops in amdgpu_ualink_is_local_accel() and activate_local_vpod() walk past the end of the peer's local_accels[] array as well.

The firmware interface allows up to PSP_GFX_MAX_LOCAL_GPUS_UAL_V1 (16) local GPUs per node, so fix the capacity to that value and reject a pod that reports more local peers than the array can hold.

Fixes 0e8eb0dcea51 ("drm/amdgpu: Assign accel state based on ASP config")
---
  drivers/gpu/drm/amd/amdgpu/amdgpu_ualink.c | 8 +++++++-
  drivers/gpu/drm/amd/amdgpu/amdgpu_ualink.h | 2 +-
  2 files changed, 8 insertions(+), 2 deletions(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_ualink.c +6 -0
index d025b4183b..b4fac5ecab 100644
@@ -1243,6 +1243,12 @@ static int __check_local_vpod_integrity(struct amdgpu_device *adev)
                                  accel_id);
                         return -EINVAL;
                 }
+               if (n_local_accels >= AMDGPU_UALINK_LOCAL_ACCELS_MAX) {
+                       dev_warn(adev->dev,
+                                "UALINK: integrity fail: too many local vpod peers: %u (max %u)\n",
+                                n_local_accels + 1, AMDGPU_UALINK_LOCAL_ACCELS_MAX);
+                       return -EINVAL;
+               }
                 local_accels[n_local_accels++] = accel_id;
         }
 

drivers/gpu/drm/amd/amdgpu/amdgpu_ualink.c static int __check_local_vpod_integrity(struct amdgpu_device *adev) · line 1266

Quoted 5 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_ualink.c +1 -1
@@ -1257,7 +1263,7 @@ static int __check_local_vpod_integrity(struct amdgpu_device *adev)
 
                 peer_info->n_local_accels = n_local_accels;
                 memcpy(peer_info->local_accels, local_accels,
-                      sizeof(local_accels));
+                      n_local_accels * sizeof(*local_accels));

This is incorrect.

drivers/gpu/drm/amd/amdgpu/amdgpu_ualink.h struct amdgpu_device; · line 37

Quoted 13 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_ualink.c +0 -0
         }
 
         return 0;
drivers/gpu/drm/amd/amdgpu/amdgpu_ualink.h +1 -1
index 85c9a9634a..c8577a94a4 100644
@@ -34,7 +34,7 @@ struct amdgpu_device;
  struct amdgpu_xcp;
 
  #define AMDGPU_UALINK_ACCEL_MAX 256
-#define AMDGPU_UALINK_LOCAL_ACCELS_MAX 8
+#define AMDGPU_UALINK_LOCAL_ACCELS_MAX 16

Current max of 8 is sufficient.

Thanks,
Lijo

Quoted 5 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_ualink.h +0 -1
  #define AMDGPU_UALINK_STATIONS_MAX 64
 
  /* A vpod_id of 0 is reserved and treated as invalid/no vPod */
--

2.56.1

#2 lore raw
Quoted 55 lines · Lijo
Quoted 52 lines · Lin Huiyi

[Some people who received this message don't often get email from [email protected]. Learn why this is important at https://aka.ms/ LearnAboutSenderIdentification ]

__check_local_vpod_integrity() collects the accelerator IDs of all local GPUs that belong to the vPod into a fixed-size local_accels[] array. The loop is bounded by mgpu_info.num_gpu (up to MAX_GPU_INSTANCE, i.e. 64) and only filters peers by vPod membership, so every matching peer is appended unconditionally:

         local_accels[n_local_accels++] = accel_id;

There is no check that n_local_accels stays below AMDGPU_UALINK_LOCAL_ACCELS_MAX, so a pod with more local GPUs than the array can hold writes past the end of a stack array. The oversized count is then propagated to peer_info->n_local_accels, and the downstream loops in amdgpu_ualink_is_local_accel() and activate_local_vpod() walk past the end of the peer's local_accels[] array as well.

The firmware interface allows up to PSP_GFX_MAX_LOCAL_GPUS_UAL_V1 (16) local GPUs per node, so fix the capacity to that value and reject a pod that reports more local peers than the array can hold.

Fixes 0e8eb0dcea51 ("drm/amdgpu: Assign accel state based on ASP config")

---
  drivers/gpu/drm/amd/amdgpu/amdgpu_ualink.c | 8 +++++++- drivers/gpu/drm/amd/amdgpu/amdgpu_ualink.h | 2 +-

  2 files changed, 8 insertions(+), 2 deletions(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_ualink.c +6 -0
index d025b4183b..b4fac5ecab 100644
@@ -1243,6 +1243,12 @@ static int __check_local_vpod_integrity(struct 
 amdgpu_device *adev)
                                   accel_id);
                          return -EINVAL;
                  }
+               if (n_local_accels >= AMDGPU_UALINK_LOCAL_ACCELS_MAX) {
+                       dev_warn(adev->dev,
+                                "UALINK: integrity fail: too many 
 local vpod peers: %u (max %u)\n",
+                                n_local_accels + 1, 
 AMDGPU_UALINK_LOCAL_ACCELS_MAX);
+                       return -EINVAL;
+               }

                 local_accels[n_local_accels++] = accel_id; }

drivers/gpu/drm/amd/amdgpu/amdgpu_ualink.c +0 -0
@@ -1257,7 +1263,7 @@ static int __check_local_vpod_integrity(struct 
 amdgpu_device *adev)
 
Quoted 6 lines

drivers/gpu/drm/amd/amdgpu/amdgpu_ualink.c static int __check_local_vpod_integrity(struct amdgpu_device *adev) · line 1266

Quoted 4 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_ualink.c +1 -1
                  peer_info->n_local_accels = n_local_accels;
                  memcpy(peer_info->local_accels, local_accels,
-                      sizeof(local_accels));
+                      n_local_accels * sizeof(*local_accels));

This is incorrect.

On a second look, this optimization is fine.

Thanks,
Lijo

Quoted 26 lines
Quoted 9 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_ualink.c +0 -0
          }
 

         return 0;

drivers/gpu/drm/amd/amdgpu/amdgpu_ualink.h +0 -0
index 85c9a9634a..c8577a94a4 100644

drivers/gpu/drm/amd/amdgpu/amdgpu_ualink.h struct amdgpu_device; · line 37

Quoted 5 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_ualink.h +1 -1
@@ -34,7 +34,7 @@ struct amdgpu_device;
   struct amdgpu_xcp;
 
   #define AMDGPU_UALINK_ACCEL_MAX 256
-#define AMDGPU_UALINK_LOCAL_ACCELS_MAX 8
+#define AMDGPU_UALINK_LOCAL_ACCELS_MAX 16

Current max of 8 is sufficient.

Thanks,
Lijo

Quoted 5 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_ualink.h +0 -0
   #define AMDGPU_UALINK_STATIONS_MAX 64
 
   /* A vpod_id of 0 is reserved and treated as invalid/no vPod */
Signature · 1 line
2.56.1
author
#3 lore raw

PATCH v2 1/1 drm/amdgpu: Fix stack buffer overflow in UALink vPod integrity check

In reply to Lin Huiyi

__check_local_vpod_integrity() collects the accelerator IDs of all local GPUs that belong to the vPod into a fixed-size local_accels[] array. The loop is bounded by mgpu_info.num_gpu (up to MAX_GPU_INSTANCE, i.e. 64) and only filters peers by vPod membership, so every matching peer is appended unconditionally:

		local_accels[n_local_accels++] = accel_id;

There is no check that n_local_accels stays below AMDGPU_UALINK_LOCAL_ACCELS_MAX, so a pod with more local GPUs than the array can hold writes past the end of a stack array. The oversized count is then propagated to peer_info->n_local_accels, and the downstream loops in amdgpu_ualink_is_local_accel() and activate_local_vpod() walk past the end of the peer's local_accels[] array as well.

Reject a pod that reports more local peers than the array can hold, and only copy the entries that were filled in.

Changes since v1:
- Keep AMDGPU_UALINK_LOCAL_ACCELS_MAX at 8 as the current limit is sufficient (Lijo Lazar).

Fixes 0e8eb0dcea51 ("drm/amdgpu: Assign accel state based on ASP config")
---
 drivers/gpu/drm/amd/amdgpu/amdgpu_ualink.c | 8 +++++++-
 1 file changed, 7 insertions(+), 1 deletion(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_ualink.c +7 -1
index d025b4183b..b4fac5ecab 100644
@@ -1243,6 +1243,12 @@ static int __check_local_vpod_integrity(struct amdgpu_device *adev)
 				 accel_id);
 			return -EINVAL;
 		}
+		if (n_local_accels >= AMDGPU_UALINK_LOCAL_ACCELS_MAX) {
+			dev_warn(adev->dev,
+				 "UALINK: integrity fail: too many local vpod peers: %u (max %u)\n",
+				 n_local_accels + 1, AMDGPU_UALINK_LOCAL_ACCELS_MAX);
+			return -EINVAL;
+		}
 		local_accels[n_local_accels++] = accel_id;
 	}
 
@@ -1257,7 +1263,7 @@ static int __check_local_vpod_integrity(struct amdgpu_device *adev)
 
 		peer_info->n_local_accels = n_local_accels;
 		memcpy(peer_info->local_accels, local_accels,
-		       sizeof(local_accels));
+		       n_local_accels * sizeof(*local_accels));
 	}
 
 	return 0;
Signature · 1 line
2.56.1
#4 lore raw
Quoted 34 lines · Lin Huiyi

__check_local_vpod_integrity() collects the accelerator IDs of all local GPUs that belong to the vPod into a fixed-size local_accels[] array. The loop is bounded by mgpu_info.num_gpu (up to MAX_GPU_INSTANCE, i.e. 64) and only filters peers by vPod membership, so every matching peer is appended unconditionally:

		local_accels[n_local_accels++] = accel_id;

There is no check that n_local_accels stays below AMDGPU_UALINK_LOCAL_ACCELS_MAX, so a pod with more local GPUs than the array can hold writes past the end of a stack array. The oversized count is then propagated to peer_info->n_local_accels, and the downstream loops in amdgpu_ualink_is_local_accel() and activate_local_vpod() walk past the end of the peer's local_accels[] array as well.

Reject a pod that reports more local peers than the array can hold, and only copy the entries that were filled in.

Changes since v1:
- Keep AMDGPU_UALINK_LOCAL_ACCELS_MAX at 8 as the current limit is sufficient (Lijo Lazar).

Fixes 0e8eb0dcea51 ("drm/amdgpu: Assign accel state based on ASP config")
---
  drivers/gpu/drm/amd/amdgpu/amdgpu_ualink.c | 8 +++++++-
  1 file changed, 7 insertions(+), 1 deletion(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_ualink.c +0 -0
index d025b4183b..b4fac5ecab 100644

drivers/gpu/drm/amd/amdgpu/amdgpu_ualink.c static int __check_local_vpod_integrity(struct amdgpu_device *adev) · line 1251

Quoted 9 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_ualink.c +6 -0
@@ -1243,6 +1243,12 @@ static int __check_local_vpod_integrity(struct amdgpu_device *adev)
  				 accel_id);
  			return -EINVAL;
  		}
+		if (n_local_accels >= AMDGPU_UALINK_LOCAL_ACCELS_MAX) {
+			dev_warn(adev->dev,
+				 "UALINK: integrity fail: too many local vpod peers: %u (max %u)\n",
+				 n_local_accels + 1, AMDGPU_UALINK_LOCAL_ACCELS_MAX);
+			return -EINVAL;
+		}

Missed this earlier. This check could be a generic one done outside the loop

unlikely (mgpu_info.num_gpu >= AMDGPU_UALINK_LOCAL_ACCELS_MAX)

Thanks,
Lijo

Quoted 12 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_ualink.c +1 -1
  		local_accels[n_local_accels++] = accel_id;
  	}
 
@@ -1257,7 +1263,7 @@ static int __check_local_vpod_integrity(struct amdgpu_device *adev)
 
  		peer_info->n_local_accels = n_local_accels;
  		memcpy(peer_info->local_accels, local_accels,
-		       sizeof(local_accels));
+		       n_local_accels * sizeof(*local_accels));
  	}
 
  	return 0;
#5 lore raw
Quoted 51 lines · Lijo
Quoted 35 lines · Lin Huiyi

__check_local_vpod_integrity() collects the accelerator IDs of all local GPUs that belong to the vPod into a fixed-size local_accels[] array.  The loop is bounded by mgpu_info.num_gpu (up to MAX_GPU_INSTANCE, i.e. 64) and only filters peers by vPod membership, so every matching peer is appended unconditionally:

        local_accels[n_local_accels++] = accel_id;

There is no check that n_local_accels stays below AMDGPU_UALINK_LOCAL_ACCELS_MAX, so a pod with more local GPUs than the array can hold writes past the end of a stack array.  The oversized count is then propagated to peer_info->n_local_accels, and the downstream loops in amdgpu_ualink_is_local_accel() and activate_local_vpod() walk past the end of the peer's local_accels[] array as well.

Reject a pod that reports more local peers than the array can hold, and only copy the entries that were filled in.

Changes since v1:
- Keep AMDGPU_UALINK_LOCAL_ACCELS_MAX at 8 as the current limit is sufficient (Lijo Lazar).

Fixes 0e8eb0dcea51 ("drm/amdgpu: Assign accel state based on ASP config")

---
  drivers/gpu/drm/amd/amdgpu/amdgpu_ualink.c | 8 +++++++-

  1 file changed, 7 insertions(+), 1 deletion(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_ualink.c +0 -0
index d025b4183b..b4fac5ecab 100644
@@ -1243,6 +1243,12 @@ static int __check_local_vpod_integrity(struct 
 amdgpu_device *adev)

drivers/gpu/drm/amd/amdgpu/amdgpu_ualink.c static int __check_local_vpod_integrity(struct · line 1253

Quoted 10 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_ualink.c +6 -0
                    accel_id);
               return -EINVAL;
           }
+        if (n_local_accels >= AMDGPU_UALINK_LOCAL_ACCELS_MAX) {
+            dev_warn(adev->dev,
+                 "UALINK: integrity fail: too many local vpod peers: 
 %u (max %u)\n",
+                 n_local_accels + 1, AMDGPU_UALINK_LOCAL_ACCELS_MAX);
+            return -EINVAL;
+        }

Missed this earlier. This check could be a generic one done outside the loop

Quoted 2 lines

unlikely (mgpu_info.num_gpu >= AMDGPU_UALINK_LOCAL_ACCELS_MAX)

Correction, error check on mgpu_info.num_gpu > AMDGPU_UALINK_LOCAL_ACCELS_MAX

For this config, we don't expect the number of GPUs detected in the system to be > maximum number of local accelerators.

Thanks,
Lijo

Quoted 15 lines

Thanks,
Lijo

Quoted 10 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_ualink.c +0 -0
           local_accels[n_local_accels++] = accel_id;

      }

drivers/gpu/drm/amd/amdgpu/amdgpu_ualink.c +1 -1
@@ -1257,7 +1263,7 @@ static int __check_local_vpod_integrity(struct 
 amdgpu_device *adev)
           peer_info->n_local_accels = n_local_accels;
           memcpy(peer_info->local_accels, local_accels,
-               sizeof(local_accels));
+               n_local_accels * sizeof(*local_accels));
       }
       return 0;
author
#6 lore raw

PATCH v3 1/1 drm/amdgpu: Fix stack buffer overflow in UALink vPod integrity check

In reply to Lin Huiyi

__check_local_vpod_integrity() collects the accelerator IDs of all local GPUs that belong to the vPod into a fixed-size local_accels[] array. The loop is bounded by mgpu_info.num_gpu (up to MAX_GPU_INSTANCE, i.e. 64) and only filters peers by vPod membership, so every matching peer is appended unconditionally:

		local_accels[n_local_accels++] = accel_id;

There is no check that n_local_accels stays below AMDGPU_UALINK_LOCAL_ACCELS_MAX, so a pod with more local GPUs than the array can hold writes past the end of a stack array. The oversized count is then propagated to peer_info->n_local_accels, and the downstream loops in amdgpu_ualink_is_local_accel() and activate_local_vpod() walk past the end of the peer's local_accels[] array as well.

Reject a pod with more local GPUs than the array can hold before the loop starts, and only copy the entries that were filled in.

Changes since v2:
- Move the bounds check out of the loop and base it on the number of local GPUs, as suggested by Lijo Lazar.

Changes since v1:
- Keep AMDGPU_UALINK_LOCAL_ACCELS_MAX at 8 as the current limit is sufficient (Lijo Lazar).

Fixes 0e8eb0dcea51 ("drm/amdgpu: Assign accel state based on ASP config")
---
 drivers/gpu/drm/amd/amdgpu/amdgpu_ualink.c | 13 ++++++++++++-
 1 file changed, 12 insertions(+), 1 deletion(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_ualink.c +12 -1
index d025b4183b..7240a57ab6 100644
@@ -1164,6 +1164,17 @@ static int __check_local_vpod_integrity(struct amdgpu_device *adev)
 	__set_bit(info->ppod.accel_id, local_accel_ids);
 	local_accels[n_local_accels++] = info->ppod.accel_id;
 
+	/*
+	 * local_accels[] holds the local GPU itself plus one entry per peer,
+	 * so more GPUs than AMDGPU_UALINK_LOCAL_ACCELS_MAX cannot fit.
+	 */
+	if (unlikely(mgpu_info.num_gpu > AMDGPU_UALINK_LOCAL_ACCELS_MAX)) {
+		dev_warn(adev->dev,
+			 "UALINK: integrity fail: too many local GPUs: %u (max %u)\n",
+			 mgpu_info.num_gpu, AMDGPU_UALINK_LOCAL_ACCELS_MAX);
+		return -EINVAL;
+	}
+
 	for (i = 0; i < mgpu_info.num_gpu; i++) {
 		peer_adev = mgpu_info.gpu_ins[i].adev;
 		if (peer_adev == adev || !peer_adev->ualink.info)
@@ -1257,7 +1268,7 @@ static int __check_local_vpod_integrity(struct amdgpu_device *adev)
 
 		peer_info->n_local_accels = n_local_accels;
 		memcpy(peer_info->local_accels, local_accels,
-		       sizeof(local_accels));
+		       n_local_accels * sizeof(*local_accels));
 	}
 
 	return 0;
Signature · 1 line
2.56.1
#7 lore raw
Thank you for your contribution! Sashiko AI review found 1 potential issue to consider: - [Medium] drm/amdgpu: flawed mgpu_info.num_gpu check in __check_local_vpod_integrity() --

Thank you for your contribution! Sashiko AI review found 1 potential issue to consider:
- [Medium] drm/amdgpu: flawed mgpu_info.num_gpu check in __check_local_vpod_integrity()
--

commit 1d7eca73ed4f664c879aa6472e418261effb5a74
Author: Lin Huiyi <[email protected]>

drm/amdgpu: Fix stack buffer overflow in UALink vPod integrity check

This commit adds a check on mgpu_info.num_gpu against AMDGPU_UALINK_LOCAL_ACCELS_MAX in __check_local_vpod_integrity() and limits the memcpy() length to the populated entries in local_accels[].

Quoted 12 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_ualink.c +4 -0
index fa4c3d27d2720..f697dde5a0d4d 100644
@@ -1162,6 +1162,17 @@ static int __check_local_vpod_integrity(struct amdgpu_device *adev)
 	__set_bit(info->ppod.accel_id, local_accel_ids);
 	local_accels[n_local_accels++] = info->ppod.accel_id;
 
+	/*
+	 * local_accels[] holds the local GPU itself plus one entry per peer,
+	 * so more GPUs than AMDGPU_UALINK_LOCAL_ACCELS_MAX cannot fit.
+	 */

drivers/gpu/drm/amd/amdgpu/amdgpu_ualink.c static int __check_local_vpod_integrity(struct amdgpu_device *adev) · line 1178

Quoted 10 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_ualink.c +7 -0
+	if (unlikely(mgpu_info.num_gpu > AMDGPU_UALINK_LOCAL_ACCELS_MAX)) {
+		dev_warn(adev->dev,
+			 "UALINK: integrity fail: too many local GPUs: %u (max %u)\n",
+			 mgpu_info.num_gpu, AMDGPU_UALINK_LOCAL_ACCELS_MAX);
+		return -EINVAL;
+	}
+
 	for (i = 0; i < mgpu_info.num_gpu; i++) {
 		peer_adev = mgpu_info.gpu_ins[i].adev;
 		if (peer_adev == adev || !peer_adev->ualink.info)

[Severity: Medium]
Should the bounds check test n_local_accels against AMDGPU_UALINK_LOCAL_ACCELS_MAX inside the loop after filtering for vPod membership, instead of checking mgpu_info.num_gpu up front?

mgpu_info.num_gpu counts all registered amdgpu instances in the system, whereas the loop skips non-UALink GPUs (!peer_adev->ualink.info) and GPUs in other vPods (!test_bit(accel_id, info->vpod.active_accel_bits)). Checking mgpu_info.num_gpu > AMDGPU_UALINK_LOCAL_ACCELS_MAX before filtering rejects valid vPods of 8 or fewer GPUs on systems that also have an integrated or non-UALink GPU, or that partition more than 8 local GPUs across multiple vPods.

In addition, local_accels[0] is already populated with info->ppod.accel_id before the loop (n_local_accels == 1). Allowing mgpu_info.num_gpu == 8 assumes adev itself is in mgpu_info.gpu_ins[] and will be skipped by peer_adev == adev. However, amdgpu_unregister_gpu_instance() removes adev from mgpu_info.gpu_ins[] before interrupts are disabled in amdgpu_driver_unload_kms() and during reset in amdgpu_device_halt_activities(). If __check_local_vpod_integrity() runs from amdgpu_ualink_config_update_handler() while adev is not in mgpu_info.gpu_ins[] and 8 matching peers remain, n_local_accels reaches 9 and writes one element past the end of local_accels[] and peer_info->local_accels[].

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1
author
#8 lore raw

PATCH v4 1/1 drm/amdgpu: Fix stack buffer overflow in UALink vPod integrity check

In reply to Lin Huiyi

__check_local_vpod_integrity() collects the accelerator IDs of all local GPUs that belong to the vPod into a fixed-size local_accels[] array. The loop is bounded by mgpu_info.num_gpu (up to MAX_GPU_INSTANCE, i.e. 64) and only filters peers by vPod membership, so every matching peer is appended unconditionally:

		local_accels[n_local_accels++] = accel_id;

There is no check that n_local_accels stays below AMDGPU_UALINK_LOCAL_ACCELS_MAX, so a pod with more local GPUs than the array can hold writes past the end of a stack array. The oversized count is then propagated to peer_info->n_local_accels, and the downstream loops in amdgpu_ualink_is_local_accel() and activate_local_vpod() walk past the end of the peer's local_accels[] array as well.

Reject a pod that reports more local peers than the array can hold, and only copy the entries that were filled in.

Changes since v3:
- Move the bounds check back into the loop. Testing mgpu_info.num_gpu before the loop is not equivalent: that counter includes non-UALink GPUs and GPUs in other vPods, so valid configurations would be rejected; and it misses the overflow when adev was already removed from mgpu_info.gpu_ins[] by amdgpu_unregister_gpu_instance() during reset or unload, where n_local_accels still reaches 9 with 8 remaining peers.

Changes since v1:
- Keep AMDGPU_UALINK_LOCAL_ACCELS_MAX at 8 as the current limit is sufficient (Lijo Lazar).

Fixes 0e8eb0dcea51 ("drm/amdgpu: Assign accel state based on ASP config")
---
 drivers/gpu/drm/amd/amdgpu/amdgpu_ualink.c | 8 +++++++-
 1 file changed, 7 insertions(+), 1 deletion(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_ualink.c +7 -1
index d025b4183b..b4fac5ecab 100644
@@ -1243,6 +1243,12 @@ static int __check_local_vpod_integrity(struct amdgpu_device *adev)
 				 accel_id);
 			return -EINVAL;
 		}
+		if (n_local_accels >= AMDGPU_UALINK_LOCAL_ACCELS_MAX) {
+			dev_warn(adev->dev,
+				 "UALINK: integrity fail: too many local vpod peers: %u (max %u)\n",
+				 n_local_accels + 1, AMDGPU_UALINK_LOCAL_ACCELS_MAX);
+			return -EINVAL;
+		}
 		local_accels[n_local_accels++] = accel_id;
 	}
 
@@ -1257,7 +1263,7 @@ static int __check_local_vpod_integrity(struct amdgpu_device *adev)
 
 		peer_info->n_local_accels = n_local_accels;
 		memcpy(peer_info->local_accels, local_accels,
-		       sizeof(local_accels));
+		       n_local_accels * sizeof(*local_accels));
 	}
 
 	return 0;
Signature · 1 line
2.56.1
#9 lore raw
Quoted 19 lines · Lin Huiyi

__check_local_vpod_integrity() collects the accelerator IDs of all local GPUs that belong to the vPod into a fixed-size local_accels[] array. The loop is bounded by mgpu_info.num_gpu (up to MAX_GPU_INSTANCE, i.e. 64) and only filters peers by vPod membership, so every matching peer is appended unconditionally:

		local_accels[n_local_accels++] = accel_id;

There is no check that n_local_accels stays below AMDGPU_UALINK_LOCAL_ACCELS_MAX, so a pod with more local GPUs than the array can hold writes past the end of a stack array. The oversized count is then propagated to peer_info->n_local_accels, and the downstream loops in amdgpu_ualink_is_local_accel() and activate_local_vpod() walk past the end of the peer's local_accels[] array as well.

Reject a pod that reports more local peers than the array can hold, and only copy the entries that were filled in.

Quoted 9 lines

Changes since v3:
- Move the bounds check back into the loop. Testing mgpu_info.num_gpu before the loop is not equivalent: that counter includes non-UALink GPUs and GPUs in other vPods, so valid configurations would be rejected; and it misses the overflow when adev was already removed from mgpu_info.gpu_ins[] by amdgpu_unregister_gpu_instance() during reset or unload, where n_local_accels still reaches 9 with 8 remaining peers.

Sashiko analysis is a theoretical possibility.

The actual intention of the code and the actual platform configuration is -

AMDGPU_UALINK_LOCAL_ACCELS_MAX represents the maximum possible local GPUs which are part of a physical pod (not VPOD). The number of GPUs that are registered in mgpu_info.num_gpu will also be restricted to the same number because of the way platform is configured.

Thanks,
Lijo

Quoted 36 lines

Changes since v1:
- Keep AMDGPU_UALINK_LOCAL_ACCELS_MAX at 8 as the current limit is sufficient (Lijo Lazar).

Fixes 0e8eb0dcea51 ("drm/amdgpu: Assign accel state based on ASP config")
---
  drivers/gpu/drm/amd/amdgpu/amdgpu_ualink.c | 8 +++++++-
  1 file changed, 7 insertions(+), 1 deletion(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_ualink.c +7 -1
index d025b4183b..b4fac5ecab 100644
@@ -1243,6 +1243,12 @@ static int __check_local_vpod_integrity(struct amdgpu_device *adev)
  				 accel_id);
  			return -EINVAL;
  		}
+		if (n_local_accels >= AMDGPU_UALINK_LOCAL_ACCELS_MAX) {
+			dev_warn(adev->dev,
+				 "UALINK: integrity fail: too many local vpod peers: %u (max %u)\n",
+				 n_local_accels + 1, AMDGPU_UALINK_LOCAL_ACCELS_MAX);
+			return -EINVAL;
+		}
  		local_accels[n_local_accels++] = accel_id;
  	}
 
@@ -1257,7 +1263,7 @@ static int __check_local_vpod_integrity(struct amdgpu_device *adev)
 
  		peer_info->n_local_accels = n_local_accels;
  		memcpy(peer_info->local_accels, local_accels,
-		       sizeof(local_accels));
+		       n_local_accels * sizeof(*local_accels));
  	}
 
  	return 0;
author
#10 lore raw

PATCH v5 1/1 drm/amdgpu: Fix stack buffer overflow in UALink vPod integrity check

In reply to Lin Huiyi

__check_local_vpod_integrity() collects the accelerator IDs of all local GPUs that belong to the vPod into a fixed-size local_accels[] array. The loop is bounded by mgpu_info.num_gpu (up to MAX_GPU_INSTANCE, i.e. 64) and only filters peers by vPod membership, so every matching peer is appended unconditionally:

		local_accels[n_local_accels++] = accel_id;

There is no check that n_local_accels stays below AMDGPU_UALINK_LOCAL_ACCELS_MAX, so a pod with more local GPUs than the array can hold writes past the end of a stack array. The oversized count is then propagated to peer_info->n_local_accels, and the downstream loops in amdgpu_ualink_is_local_accel() and activate_local_vpod() walk past the end of the peer's local_accels[] array as well.

Reject a pod with more local GPUs than the array can hold, and only copy the entries that were filled in.

Changes since v4:
- Move the bounds check out of the loop and base it on the GPU count, as suggested by Lijo Lazar. amdgpu_unregister_gpu_instance() removes the device from mgpu_info and decrements num_gpu in the same place, so n_local_accels cannot grow beyond num_gpu and the overflow window described in v4 does not exist. Thanks to Lijo for clarifying how the platform configuration limits the number of registered GPUs.

Changes since v1:
- Keep AMDGPU_UALINK_LOCAL_ACCELS_MAX at 8 as the current limit is sufficient (Lijo Lazar).

Fixes 0e8eb0dcea51 ("drm/amdgpu: Assign accel state based on ASP config")
---
 drivers/gpu/drm/amd/amdgpu/amdgpu_ualink.c | 14 +++++++++++++-
 1 file changed, 13 insertions(+), 1 deletion(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_ualink.c +13 -1
index d025b4183b..12d0a62f8e 100644
@@ -1164,6 +1164,18 @@ static int __check_local_vpod_integrity(struct amdgpu_device *adev)
 	__set_bit(info->ppod.accel_id, local_accel_ids);
 	local_accels[n_local_accels++] = info->ppod.accel_id;
 
+	/*
+	 * local_accels[] holds the local GPU itself plus one entry per peer.
+	 * The platform never exposes more than AMDGPU_UALINK_LOCAL_ACCELS_MAX
+	 * local GPUs, so reject a configuration that cannot fit.
+	 */
+	if (unlikely(mgpu_info.num_gpu > AMDGPU_UALINK_LOCAL_ACCELS_MAX)) {
+		dev_warn(adev->dev,
+			 "UALINK: integrity fail: too many local GPUs: %u (max %u)\n",
+			 mgpu_info.num_gpu, AMDGPU_UALINK_LOCAL_ACCELS_MAX);
+		return -EINVAL;
+	}
+
 	for (i = 0; i < mgpu_info.num_gpu; i++) {
 		peer_adev = mgpu_info.gpu_ins[i].adev;
 		if (peer_adev == adev || !peer_adev->ualink.info)
@@ -1257,7 +1269,7 @@ static int __check_local_vpod_integrity(struct amdgpu_device *adev)
 
 		peer_info->n_local_accels = n_local_accels;
 		memcpy(peer_info->local_accels, local_accels,
-		       sizeof(local_accels));
+		       n_local_accels * sizeof(*local_accels));
 	}
 
 	return 0;
Signature · 1 line
2.56.1
#11 lore raw
Thank you for your contribution! Sashiko AI review found 1 potential issue to consider: - [Medium] drm/amdgpu: flawed mgpu_info.num_gpu check in __check_local_vpod_integrity() --

Thank you for your contribution! Sashiko AI review found 1 potential issue to consider:
- [Medium] drm/amdgpu: flawed mgpu_info.num_gpu check in __check_local_vpod_integrity()
--

commit 5534f8d736ae8e09c0ee2bd1e6fbe48b3f50a027
Author: Lin Huiyi <[email protected]>

drm/amdgpu: Fix stack buffer overflow in UALink vPod integrity check

This commit bounds-checks mgpu_info.num_gpu against AMDGPU_UALINK_LOCAL_ACCELS_MAX in __check_local_vpod_integrity() and limits the peer_info->local_accels memcpy() to the populated entries.

Quoted 13 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_ualink.c +5 -0
index fa4c3d27d2720..c0016e8815e93 100644
@@ -1162,6 +1162,18 @@ static int __check_local_vpod_integrity(struct amdgpu_device *adev)
 	__set_bit(info->ppod.accel_id, local_accel_ids);
 	local_accels[n_local_accels++] = info->ppod.accel_id;
 
+	/*
+	 * local_accels[] holds the local GPU itself plus one entry per peer.
+	 * The platform never exposes more than AMDGPU_UALINK_LOCAL_ACCELS_MAX
+	 * local GPUs, so reject a configuration that cannot fit.
+	 */

drivers/gpu/drm/amd/amdgpu/amdgpu_ualink.c static int __check_local_vpod_integrity(struct amdgpu_device *adev) · line 1179

Quoted 10 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_ualink.c +7 -0
+	if (unlikely(mgpu_info.num_gpu > AMDGPU_UALINK_LOCAL_ACCELS_MAX)) {
+		dev_warn(adev->dev,
+			 "UALINK: integrity fail: too many local GPUs: %u (max %u)\n",
+			 mgpu_info.num_gpu, AMDGPU_UALINK_LOCAL_ACCELS_MAX);
+		return -EINVAL;
+	}
+
 	for (i = 0; i < mgpu_info.num_gpu; i++) {
 		peer_adev = mgpu_info.gpu_ins[i].adev;
 		if (peer_adev == adev || !peer_adev->ualink.info)

[Severity: Medium]
Can this still overflow local_accels[] and peer_info->local_accels[] by one element, while also rejecting valid vPods when other AMD GPUs are registered?

adev is placed in local_accels[0] before the loop, so n_local_accels stays within mgpu_info.num_gpu only when one of the entries in mgpu_info.gpu_ins[] is adev itself and gets skipped by peer_adev == adev. During GPU reset, amdgpu_device_halt_activities() calls amdgpu_unregister_gpu_instance(adev), and soc_v1_0_mode2_restore_ip() resumes the IH ring before soc_v1_0_mode2_restore_hwcontext() re-registers adev (or leaves it unregistered if a later restore step fails). If __check_local_vpod_integrity() runs while adev is absent from mgpu_info.gpu_ins[] and mgpu_info.num_gpu is AMDGPU_UALINK_LOCAL_ACCELS_MAX, the loop does not skip adev and can append all AMDGPU_UALINK_LOCAL_ACCELS_MAX peers after local_accels[0], writing one entry past the end of local_accels[] and peer_info->local_accels[].

Conversely, mgpu_info.num_gpu counts all registered amdgpu devices on the host, including non-UALink GPUs (!peer_adev->ualink.info) and GPUs in other vPods, so checking mgpu_info.num_gpu rejects a valid vPod of up to AMDGPU_UALINK_LOCAL_ACCELS_MAX accelerators whenever another AMD GPU is present. Checking n_local_accels >= AMDGPU_UALINK_LOCAL_ACCELS_MAX before appending each matching vPod peer in the loop would avoid both issues.

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1