__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")
Signed-off-by: Lin Huiyi <[email protected]>
---
 drivers/gpu/drm/amd/amdgpu/amdgpu_ualink.c | 14 +++++++++++++-
 1 file changed, 13 insertions(+), 1 deletion(-)

diff --git a/drivers/gpu/drm/amd/amdgpu/amdgpu_ualink.c 
b/drivers/gpu/drm/amd/amdgpu/amdgpu_ualink.c
index d025b4183b..12d0a62f8e 100644
--- a/drivers/gpu/drm/amd/amdgpu/amdgpu_ualink.c
+++ b/drivers/gpu/drm/amd/amdgpu/amdgpu_ualink.c
@@ -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;
-- 
2.56.1

Reply via email to