Thank you for your contribution! Sashiko AI review found 5 potential
issues to consider:
- [High] drm/imagination: undefined pvr_exp_fw_support and broken
  version check
- [Medium] drm/imagination: signed offset bypasses size check in
  pvr_fw_load_metadata()
- [Medium] drm/imagination: metadata leak on error paths in
  pvr_device_gpu_init()
- [Medium] drm/imagination: UAF and NULL deref of metadata in
  pvr_fw_info_get()
- [Low] drm/imagination: misplaced closing parenthesis in drm_err()
  format strings
--

commit 61d39e2fd09561a4d18973b66a4042a907478335
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 pvr_fw_metadata structure.

> 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 this dereference a freed or NULL pvr_dev->fw_dev.metadata during
device removal?

Previously, pvr_fw_info_get() read pvr_dev->fw_version, which was embedded
in struct pvr_device.  Because pvr_remove() runs pvr_device_fini() before
drm_dev_unplug(), a concurrent DRM_IOCTL_PVR_DEV_QUERY passes
drm_dev_enter() and can race with pvr_fw_free_metadata_and_device_info():

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

> 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..2e7e9fb291b35
> --- /dev/null
> +++ b/drivers/gpu/drm/imagination/pvr_fw_info.c
> @@ -0,0 +1,338 @@

[ ... ]

> +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)) {

[Severity: High]
Where is pvr_exp_fw_support defined?  It is declared extern in pvr_fw.h,
but nothing in the tree defines it, so building with CONFIG_DRM_POWERVR
fails at link/modpost time with an undefined symbol.

Also, when pvr_exp_fw_support is true, the condition
(version.major != PVR_FW_MAX_SUPPORTED_MAJOR_VERSION && pvr_exp_fw_support)
rejects every major version other than PVR_FW_MAX_SUPPORTED_MAJOR_VERSION,
preventing experimental major versions (> PVR_FW_MAX_SUPPORTED_MAJOR_VERSION)
from loading.

> +             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;
> +     }
> +
> +     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]
This isn't a bug, but these three drm_err() format strings end with "\n)"
instead of ")\n", putting a stray closing parenthesis on the next log line.

[ ... ]

> +static const struct pvr_fw_metadata *
> +pvr_fw_load_metadata(struct pvr_device *pvr_dev, ptrdiff_t 
> *device_info_offset_out)
> +{
> +     const struct firmware *firmware = pvr_dev->fw_dev.firmware;
> +     struct drm_device *drm_dev = from_pvr_device(pvr_dev);
> +     const struct pvr_fw_layout_entry *layout_entries;
> +     const struct pvr_fw_info_header *header;
> +     struct pvr_fw_version fw_version;
> +     const void *fw = firmware->data;
> +     struct pvr_fw_metadata *meta;
> +     ptrdiff_t header_offset;
> +     int err;
> +
> +     if (check_sub_overflow(firmware->size, FW_BLOCK_SIZE, &header_offset)) {
> +             drm_err(drm_dev, "Firmware is smaller than one block\n");
> +             return ERR_PTR(-EINVAL);
> +     }
> +
> +     if (firmware->size % FW_BLOCK_SIZE) {
> +             drm_err(drm_dev, "Firmware size is not block-aligned\n");
> +             return ERR_PTR(-EINVAL);
> +     }
> +
> +     header = fw + header_offset;
> +
> +     err = pvr_fw_info_header_validate(pvr_dev, header, &fw_version);
> +     if (err)
> +             return ERR_PTR(err);
> +
> +     layout_entries = fw + header_offset + header->header_len;
> +
> +     meta = kzalloc_flex(*meta, sections, header->layout_entry_num);
> +     if (!meta)
> +             return ERR_PTR(-ENOMEM);
> +
> +     meta->version = fw_version;
> +     meta->num_sections = header->layout_entry_num;
> +
> +     for (int i = 0; i < header->layout_entry_num; i++) {
> +             err = pvr_fw_section_desc_for_layout_entry(pvr_dev,
> +                                                        &layout_entries[i],
> +                                                        &meta->sections[i]);
> +             if (err)
> +                     goto err_free_meta;
> +     }
> +
> +     *device_info_offset_out = header_offset - header->device_info_size;

[Severity: Medium]
Can header_offset and *device_info_offset_out become negative here and
read before firmware->data?

Because header_offset is a signed ptrdiff_t, check_sub_overflow() checks
whether firmware->size - FW_BLOCK_SIZE fits in a signed ptrdiff_t.  When
firmware->size is 0, check_sub_overflow() returns false with header_offset
set to -4096, firmware->size % FW_BLOCK_SIZE is 0, and header = fw +
header_offset points 4096 bytes before firmware->data.

Similarly, subtracting header->device_info_size from header_offset into a
signed ptrdiff_t without checking header->device_info_size <= header_offset
lets firmware->data + device_info_offset underflow before firmware->data.
Making the offsets size_t (and using check_sub_overflow() for both) would
catch both underflows.

[ ... ]

> +int
> +pvr_fw_load_metadata_and_device_info(struct pvr_device *pvr_dev)
> +{
> +     const struct firmware *firmware = pvr_dev->fw_dev.firmware;
> +     const struct pvr_fw_metadata *metadata;
> +     ptrdiff_t device_info_offset = 0;
> +
> +     metadata = pvr_fw_load_metadata(pvr_dev, &device_info_offset);
> +     if (IS_ERR(metadata))
> +             return PTR_ERR(metadata);
> +
> +     pvr_dev->fw_dev.metadata = metadata;
> +
> +     return pvr_fw_get_device_info(pvr_dev, firmware->data + 
> device_info_offset);
> +}

[Severity: Medium]
Does this leak pvr_dev->fw_dev.metadata if pvr_fw_get_device_info() or a
later step in pvr_device_gpu_init() fails?

pvr_fw_load_metadata_and_device_info() does not free metadata when
pvr_fw_get_device_info() returns an error, and on later failures in
pvr_device_gpu_init() (such as pvr_device_info_validate() or
pvr_fw_init()), pvr_device_init() jumps past pvr_device_gpu_fini() without
calling pvr_fw_free_metadata_and_device_info().

-- 
Sashiko AI review ยท 
https://sashiko.dev/#/patchset/20261125-b4-commonize-fw-segment-loading-v1-0-69657fa52...@imgtec.com?part=3

Reply via email to