Thank you for your contribution! Sashiko AI review found 3 potential
issues to consider:
- [High] drm/imagination: undefined pvr_exp_fw_support in
  pvr_fw_load_version()
- [Medium] drm/imagination: UAF and NULL deref of metadata in
  pvr_fw_info_get()
- [Low] drm/imagination: misplaced parenthesis in
  pvr_fw_info_header_validate()
--

commit b39417857398d201ed66dd9476480df29442e140
Author: Matt Coster <[email protected]>

drm/imagination: Save metadata from firmware

This commit extracts firmware version and section layout metadata from the
firmware header into an allocated struct pvr_fw_metadata during device
initialization.

> diff --git a/drivers/gpu/drm/imagination/pvr_fw.c 
> b/drivers/gpu/drm/imagination/pvr_fw.c
> index cbde54c7291c7..2a5dcd9aeed71 100644
> --- a/drivers/gpu/drm/imagination/pvr_fw.c
> +++ b/drivers/gpu/drm/imagination/pvr_fw.c
[ ... ]
> @@ -1549,8 +1387,8 @@ pvr_fw_info_get(const struct pvr_device *pvr_dev,
>       if (fw_info._padding_4)
>               return -EINVAL;
>  
> -     fw_info.major_version = pvr_dev->fw_version.major;
> -     fw_info.minor_version = pvr_dev->fw_version.minor;
> +     fw_info.major_version = pvr_dev->fw_dev.metadata->version.major;
> +     fw_info.minor_version = pvr_dev->fw_dev.metadata->version.minor;
>  
>       err = PVR_UOBJ_SET(args->pointer, args->size, fw_info);
>       if (err < 0)

[Severity: Medium]
Can pvr_fw_info_get() trigger a use-after-free or NULL pointer dereference
on pvr_dev->fw_dev.metadata during device removal?

Previously, pvr_fw_info_get() read pvr_dev->fw_version, which lived inside
struct pvr_device for the lifetime of the DRM device.  With this change,
pvr_fw_free_metadata_and_device_info() frees and clears
pvr_dev->fw_dev.metadata during pvr_device_fini(), which pvr_remove() runs
before drm_dev_unplug():

CPU0                                    CPU1
pvr_remove()                            pvr_ioctl_dev_query()
                                          drm_dev_enter()
  pvr_device_fini()
    ...
      pvr_fw_free_metadata_and_device_info()
        kfree(pvr_dev->fw_dev.metadata)
        pvr_dev->fw_dev.metadata = NULL
                                          pvr_fw_info_get()
                                            pvr_dev->fw_dev.metadata->version
                                            <-- use-after-free / NULL deref
  drm_dev_unplug()

> diff --git a/drivers/gpu/drm/imagination/pvr_fw.h 
> b/drivers/gpu/drm/imagination/pvr_fw.h
> index dab8292a84791..51acefde73ee2 100644
> --- a/drivers/gpu/drm/imagination/pvr_fw.h
> +++ b/drivers/gpu/drm/imagination/pvr_fw.h
> @@ -23,6 +22,30 @@ struct pvr_vm_context;
>  
>  #define ROGUE_FWIF_KCCB_NUMCMDS_LOG2_DEFAULT 7
>  
> +extern bool pvr_exp_fw_support;

[ ... ]

> diff --git a/drivers/gpu/drm/imagination/pvr_fw_info.c 
> b/drivers/gpu/drm/imagination/pvr_fw_info.c
> new file mode 100644
> index 0000000000000..b0f2dad91fc32
> --- /dev/null
> +++ b/drivers/gpu/drm/imagination/pvr_fw_info.c
> @@ -0,0 +1,345 @@
[ ... ]
> +static bool
> +pvr_fw_load_version(struct pvr_device *pvr_dev,
> +                 const struct pvr_fw_info_header *header,
> +                 struct pvr_fw_version *version_out)
> +{
> +     struct drm_device *drm_dev = from_pvr_device(pvr_dev);
> +     bool invalid = false;
> +
> +     struct pvr_fw_version version = {
> +             .major = header->fw_version_major,
> +             .minor = header->fw_version_minor,
> +             .build = header->fw_version_build,
> +             .open_source = header->flags & PVR_FW_FLAGS_OPEN_SOURCE,
> +     };
> +
> +     if (!version.open_source) {
> +             drm_err(drm_dev, "Firmware is not open-source compatible (found 
> version " PVR_FW_VERSION_FMT ")\n",
> +                     PVR_FW_VERSION_FMT_ARGS(&version));
> +             invalid = true;
> +     }
> +
> +     if (version.major == 0 ||
> +         (version.major > PVR_FW_MAX_SUPPORTED_MAJOR_VERSION && 
> !pvr_exp_fw_support) ||
> +         (version.major != PVR_FW_MAX_SUPPORTED_MAJOR_VERSION && 
> pvr_exp_fw_support)) {
> +             drm_err(drm_dev, "Firmware version is unsupported (known range 
> is 1..%u inclusive, found "
> +                              PVR_FW_VERSION_FMT ")\n",
> +                     PVR_FW_MAX_SUPPORTED_MAJOR_VERSION,
> +                     PVR_FW_VERSION_FMT_ARGS(&version));
> +             invalid = true;
> +     }

[Severity: High]
As reported by kernel test robot on v1 [1], is the definition of
pvr_exp_fw_support missing?  Nothing in the tree defines it, so linking
vmlinux or modpost for powervr.ko fails with an undefined symbol error.

Also, when pvr_exp_fw_support is true, the check rejects any
version.major != PVR_FW_MAX_SUPPORTED_MAJOR_VERSION, which blocks
experimental firmware major versions above PVR_FW_MAX_SUPPORTED_MAJOR_VERSION
instead of allowing them.

> +
> +     if (invalid)
> +             return false;
> +
> +     *version_out = version;
> +     return true;
> +}
> +
> +static int
> +pvr_fw_info_header_validate(struct pvr_device *pvr_dev,
> +                         const struct pvr_fw_info_header *header,
> +                         struct pvr_fw_version *fw_version_out)
> +{
> +     struct drm_device *drm_dev = from_pvr_device(pvr_dev);
> +     bool invalid = false;
> +
> +     if (header->info_version != PVR_FW_INFO_VERSION) {
> +             drm_err(drm_dev,
> +                     "Firmware version is unsupported (known version is %u, 
> found %u)\n",
> +                     PVR_FW_INFO_VERSION, header->info_version);
> +             return -EINVAL;
> +     }
> +
> +     if (header->header_len != sizeof(struct pvr_fw_info_header)) {
> +             drm_err(drm_dev, "Firmware header size mismatch (found=%u, 
> expected=%zu)\n)",
> +                     header->header_len, sizeof(struct pvr_fw_info_header));
> +             return -EINVAL;
> +     }
> +
> +     if (!header->layout_entry_num || !header->layout_entry_size) {
> +             drm_err(drm_dev, "Firmware layout table missing\n");
> +             invalid = true;
> +     } else if (header->layout_entry_size != sizeof(struct 
> pvr_fw_layout_entry)) {
> +             drm_err(drm_dev,
> +                     "Firmware layout table entry size mismatch (found=%u, 
> expected=%zu)\n)",
> +                     header->layout_entry_size, sizeof(struct 
> pvr_fw_layout_entry));
> +             invalid = true;
> +     } else if (header->layout_entry_num > PVR_FW_INFO_MAX_NUM_ENTRIES) {
> +             drm_err(drm_dev, "Firmware layout table is too large (found=%u, 
> max=%u)\n)",
> +                     header->layout_entry_num, PVR_FW_INFO_MAX_NUM_ENTRIES);
> +             invalid = true;

[Severity: Low]
These three drm_err() format strings end with "\n)" instead of ")\n", which
prints the closing parenthesis on the next log line.

[1] https://lore.kernel.org/r/[email protected]

-- 
Sashiko AI review ยท 
https://sashiko.dev/#/patchset/20261202-b4-commonize-fw-segment-loading-v2-0-45fd5153c...@imgtec.com?part=3

Reply via email to