Thank you for your contribution! Sashiko AI review found 4 potential
issues to consider:
- [High] drm/panthor: NULL pointer dereference in
  panthor_fw_query_protm_info()
- [Medium] drm/panthor: UAF or NULL dereference of fw->vm outside
  drm_dev_enter()
- [Low] drm/panthor: missing kernel-doc for protm member in struct
  panthor_fw
- [Low] drm/panthor: NULL shared_section->mem in
  panthor_fw_load_section_entry()
--

commit 5ac4027cd6058f1c3359662cdf5433f1c22a832b
Author: Boris Brezillon <[email protected]>

drm/panthor: Support FW init of protm

This commit adds UAPI and firmware support to query protected mode
information and initialize the protected firmware section from a
userspace-supplied GEM buffer object.

> diff --git a/drivers/gpu/drm/panthor/panthor_fw.c 
> b/drivers/gpu/drm/panthor/panthor_fw.c
> index b565b362f401f..af86b1844ead8 100644
> --- a/drivers/gpu/drm/panthor/panthor_fw.c
> +++ b/drivers/gpu/drm/panthor/panthor_fw.c
[ ... ]
> @@ -267,6 +269,18 @@ struct panthor_fw {
>               struct delayed_work ping_work;
>       } watchdog;
>  
> +     struct  {
> +             /** @protm.init_lock: Used to serialize protm initialization. */
> +             struct mutex init_lock;
> +
> +             /** @protm.inited: set to true when protm.section is 
> initialized */
> +             bool inited;
> +
> +             /** @protm.section: The protected mode section. */
> +             struct panthor_fw_section *section;
> +
> +     } protm;

[Severity: Low]
This isn't a bug, but struct panthor_fw has a kernel-doc comment block and
protm is missing its top-level @protm description, which triggers a
kernel-doc warning with W=1 builds.

[ ... ]
> @@ -637,12 +659,6 @@ static int panthor_fw_load_section_entry(struct 
> panthor_device *ptdev,
>               return -EINVAL;
>       }
>  
> -     if (hdr.flags & CSF_FW_BINARY_IFACE_ENTRY_PROT) {
> -             drm_warn(&ptdev->base,
> -                      "Firmware protected mode entry is not supported, 
> ignoring");
> -             return 0;
> -     }
> -
>       if (hdr.va.start == CSF_MCU_SHARED_REGION_START &&
>           !(hdr.flags & CSF_FW_BINARY_IFACE_ENTRY_SHARED)) {
>               drm_err(&ptdev->base,
> @@ -694,7 +710,22 @@ static int panthor_fw_load_section_entry(struct 
> panthor_device *ptdev,
>       section->size = section_size;
>       section->va = hdr.va.start;
>  
> -     if (section_size) {
> +     if (hdr.flags & CSF_FW_BINARY_IFACE_ENTRY_PROT) {
> +             guard(mutex)(&ptdev->fw->protm.init_lock);
> +
> +             if (ptdev->fw->protm.section) {
> +                     drm_err(&ptdev->base,
> +                             "Only one protected section supported\n");
> +                     return -EINVAL;
> +             }
> +
> +             ptdev->fw->protm.section = section;
> +     }
> +
> +     /* Defer the section->mem creation if this is a protected entry.
> +      * This will be populated when DRM_IOCTL_PANTHOR_PROTM_INIT is called.
> +      */
> +     if (section->size && !(hdr.flags & CSF_FW_BINARY_IFACE_ENTRY_PROT)) {
>               u32 vm_map_flags = section_vm_map_flags(section);
>  
>               section->mem = panthor_kernel_bo_create(

[Severity: Low]
Can a malformed firmware binary cause a NULL pointer dereference of
ptdev->fw->shared_section->mem in panthor_fw_init_ifaces()?

If an entry at CSF_MCU_SHARED_REGION_START has both
CSF_FW_BINARY_IFACE_ENTRY_SHARED and CSF_FW_BINARY_IFACE_ENTRY_PROT set,
panthor_fw_load_section_entry() skips allocating section->mem and still
assigns ptdev->fw->shared_section = section, which
panthor_fw_init_ifaces() then dereferences via panthor_kernel_bo_size().
Rejecting CSF_FW_BINARY_IFACE_ENTRY_PROT when
CSF_FW_BINARY_IFACE_ENTRY_SHARED is set would prevent this.

[ ... ]
> @@ -1610,6 +1641,101 @@ int panthor_fw_protm_exit(struct panthor_device 
> *ptdev, u32 timeout_ms)
>       return panthor_fw_protm_exit_wait(ptdev, timeout_ms);
>  }
>  
> +int panthor_fw_protm_init(struct drm_file *file,
> +                       struct drm_panthor_protm_init *args)
> +{
> +     struct panthor_file *pfile = file->driver_priv;
> +     struct panthor_device *ptdev = pfile->ptdev;
> +     ssize_t vm_pgsz = panthor_vm_page_size(ptdev->fw->vm);
> +     struct panthor_fw_section *protm_section;
> +     struct panthor_kernel_bo *mem;
> +     struct drm_gem_object *obj;
> +     u32 vm_map_flags;
> +     int cookie, ret = 0;
> +
> +     if (!capable(CAP_SYS_MODULE))
> +             return -EPERM;
> +
> +     if (args->pad)
> +             return -EINVAL;
> +
> +     mutex_lock(&ptdev->fw->protm.init_lock);
> +
> +     protm_section = ptdev->fw->protm.section;
> +
> +     if (!protm_section || !protm_section->size) {
> +             ret = -EINVAL;
> +             goto out_unlock;
> +     }
> +
> +     if (ptdev->fw->protm.inited)
> +             goto out_unlock;
> +
> +     if (!drm_dev_enter(&ptdev->base, &cookie)) {
> +             ret = -ENODEV;
> +             goto out_unlock;
> +     }

[Severity: Medium]
Can calling panthor_vm_page_size(ptdev->fw->vm) before drm_dev_enter() here
(and without drm_dev_enter() in panthor_fw_query_protm_info() below) cause
a use-after-free or NULL pointer dereference if the device is unplugged
concurrently?

CPU0                                    CPU1
drm_ioctl_kernel()
  panthor_ioctl_protm_init()
    panthor_fw_protm_init()
                                        panthor_device_unplug()
                                          drm_dev_unplug(&ptdev->base)
                                          panthor_fw_unplug(ptdev)
                                            panthor_vm_put(ptdev->fw->vm)
                                            ptdev->fw->vm = NULL
      panthor_vm_page_size(ptdev->fw->vm)
        <-- NULL / UAF dereference
      ...
      drm_dev_enter(&ptdev->base, &cookie)

Evaluating panthor_vm_page_size(ptdev->fw->vm) only inside a
drm_dev_enter() section in both functions would avoid racing with
panthor_fw_unplug().

> +
> +     obj = drm_gem_object_lookup(file,
> +                                 args->fw_protected_sections_bo_handle);
> +     if (!obj) {
> +             ret = -ENOENT;
> +             goto out_dev_exit;
> +     }
> +
> +     if (obj->size < ALIGN(ptdev->fw->protm.section->size, vm_pgsz)) {
> +             ret = -EINVAL;
> +             goto out_gem_put;
> +     }
> +
> +     vm_map_flags = section_vm_map_flags(protm_section);
> +
> +     mem = panthor_kernel_bo_create_from_gem(
> +             ptdev, panthor_fw_vm(ptdev), to_panthor_bo(obj), vm_map_flags,
> +             protm_section->va, protm_section->size);
> +     if (IS_ERR(mem)) {
> +             ret = PTR_ERR(mem);
> +             goto out_gem_put;
> +     }
> +
> +     protm_section->mem = mem;
> +
> +     ptdev->fw->protm.inited = true;
> +
> +out_gem_put:
> +     drm_gem_object_put(obj);
> +
> +out_dev_exit:
> +     drm_dev_exit(cookie);
> +
> +out_unlock:
> +     mutex_unlock(&ptdev->fw->protm.init_lock);
> +
> +     return ret;
> +}
> +
> +bool panthor_fw_protm_is_inited(struct panthor_device *ptdev)
> +{
> +     guard(mutex)(&ptdev->fw->protm.init_lock);
> +     return ptdev->fw->protm.inited;
> +}
> +
> +void panthor_fw_query_protm_info(struct drm_file *file,
> +                              struct drm_panthor_protm_info *arg)
> +{
> +     struct panthor_file *pfile = file->driver_priv;
> +     struct panthor_device *ptdev = pfile->ptdev;
> +     ssize_t vm_pgsz = panthor_vm_page_size(ptdev->fw->vm);
> +
> +     guard(mutex)(&ptdev->fw->protm.init_lock);
> +
> +     memset(arg, 0, sizeof(*arg));
> +     arg->state = ptdev->fw->protm.inited ? DRM_PANTHOR_PROTM_INITIALIZED : 
> 0;
> +     arg->group_protected_suspend_buf_size = ptdev->fw->protm_suspend_size;
> +     arg->fw_protected_sections_size =  ALIGN(ptdev->fw->protm.section->size,
> +                                              vm_pgsz);
> +}

[Severity: High]
Does panthor_fw_query_protm_info() dereference a NULL
ptdev->fw->protm.section when the loaded firmware has no protected section?

Unlike panthor_fw_protm_init(), which checks whether protm_section is NULL,
querying DRM_PANTHOR_DEV_QUERY_PROTM_INFO unconditionally reads
ptdev->fw->protm.section->size, allowing any user with render node access
to trigger a kernel NULL pointer dereference if the firmware binary lacks a
non-empty CSF_FW_BINARY_IFACE_ENTRY_PROT entry.

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

Reply via email to