[bug report] drm/amdkfd: Export DMABufs from KFD using GEM handles

Dan Carpenter dan.carpenter at linaro.org
Tue Jan 23 10:21:50 UTC 2024


Hello Felix Kuehling,

The patch 1819200166ce: "drm/amdkfd: Export DMABufs from KFD using
GEM handles" from Aug 24, 2023 (linux-next), leads to the following
Smatch static checker warning:

	drivers/dma-buf/dma-buf.c:729 dma_buf_get()
	warn: fd used after fd_install() 'fd'

drivers/gpu/drm/amd/amdgpu/amdgpu_amdkfd_gpuvm.c
   809  static int kfd_mem_export_dmabuf(struct kgd_mem *mem)
   810  {
   811          if (!mem->dmabuf) {
   812                  struct amdgpu_device *bo_adev;
   813                  struct dma_buf *dmabuf;
   814                  int r, fd;
   815  
   816                  bo_adev = amdgpu_ttm_adev(mem->bo->tbo.bdev);
   817                  r = drm_gem_prime_handle_to_fd(&bo_adev->ddev, bo_adev->kfd.client.file,
   818                                                 mem->gem_handle,
   819                          mem->alloc_flags & KFD_IOC_ALLOC_MEM_FLAGS_WRITABLE ?
   820                                                 DRM_RDWR : 0, &fd);
                                                                     ^^^
The drm_gem_prime_handle_to_fd() function does an fd_install() and
returns the result as "fd".

   821                  if (r)
   822                          return r;
   823                  dmabuf = dma_buf_get(fd);
                                             ^^
Then we do another fget() inside dma_buf_get().  I'm not an expert,
but this looks wrong.  We can't assume that the dmabuf here is the
same one from drm_gem_prime_handle_to_fd() because the user could
change it after the fd_install().  I suspect drm_gem_prime_handle_to_fd()
should pass the dmabuf back instead.

We had several CVEs similar to this such as CVE-2022-1998.

   824                  close_fd(fd);
   825                  if (WARN_ON_ONCE(IS_ERR(dmabuf)))
   826                          return PTR_ERR(dmabuf);
   827                  mem->dmabuf = dmabuf;
   828          }
   829  
   830          return 0;
   831  }

regards,
dan carpenter


More information about the dri-devel mailing list