When args->size is 0, rocket_ioctl_create_bo() passes it to
drm_gem_shmem_create(), which allocates the GEM object and shmem
backing file before failing in drm_gem_create_mmap_offset() ->
drm_vma_offset_add() -> drm_mm_insert_node_in_range() with -ENOSPC
and tearing the object back down.

Reject zero-sized buffer objects upfront with -EINVAL, matching other
DRM and accel drivers such as panfrost and qaic, and avoiding
unnecessary object allocation and cleanup.

Fixes: 658ebeac3351 ("accel/rocket: Add IOCTL for BO creation")
Signed-off-by: Hu Xinyao <[email protected]>
---
v2:
  - Fix commit message to describe the actual -ENOSPC failure path in
    drm_gem_shmem_create() rather than drm_gem_shmem_get_pages_sgt(),
    and drop Cc: stable as no kernel crash occurs. (Sashiko)
  - Found during a GLM-5.4-assisted code audit, verified manually
    against v7.3.6 and mainline, and compile-tested with -Werror.
  - v1: 
https://lore.kernel.org/r/[email protected]

 drivers/accel/rocket/rocket_gem.c | 3 +++
 1 file changed, 3 insertions(+)

diff --git a/drivers/accel/rocket/rocket_gem.c 
b/drivers/accel/rocket/rocket_gem.c
index 8f802df..be9c217 100644
--- a/drivers/accel/rocket/rocket_gem.c
+++ b/drivers/accel/rocket/rocket_gem.c
@@ -68,6 +68,9 @@ int rocket_ioctl_create_bo(struct drm_device *dev, void 
*data, struct drm_file *
        struct sg_table *sgt;
        int ret;
 
+       if (!args->size)
+               return -EINVAL;
+
        shmem_obj = drm_gem_shmem_create(dev, args->size);
        if (IS_ERR(shmem_obj))
                return PTR_ERR(shmem_obj);
-- 
2.43.0

Reply via email to