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.

> diff --git a/drivers/gpu/drm/amd/amdgpu/amdgpu_ualink.c 
> b/drivers/gpu/drm/amd/amdgpu/amdgpu_ualink.c
> index fa4c3d27d2720..c0016e8815e93 100644
> --- a/drivers/gpu/drm/amd/amdgpu/amdgpu_ualink.c
> +++ b/drivers/gpu/drm/amd/amdgpu/amdgpu_ualink.c
> @@ -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.
> +      */
> +     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.

-- 
Sashiko AI review ยท 
https://sashiko.dev/#/patchset/[email protected]?part=1

Reply via email to