Thank you for your contribution! Sashiko AI review found 2 potential
issues to consider:
- [Medium] drm/vkms: endianness bug in plane_supported_formats_show()
  and _store()
- [Medium] drm/vkms: UAF on supported_formats in
  plane_supported_formats_store()
--

commit fdd01181d213d1bc98312c36d05a5fcad23b5664
Author: Louis Chauvet <[email protected]>

drm/vkms: Introduce configfs for plane format

This commit adds a configfs attribute to configure the supported pixel
formats for each VKMS plane.

> diff --git a/drivers/gpu/drm/vkms/vkms_configfs.c 
> b/drivers/gpu/drm/vkms/vkms_configfs.c
> index a746fb5e22a37..9f24e9e6ed6d0 100644
> --- a/drivers/gpu/drm/vkms/vkms_configfs.c
> +++ b/drivers/gpu/drm/vkms/vkms_configfs.c
[ ... ]
> @@ -576,6 +577,88 @@ static ssize_t plane_default_color_encoding_store(struct 
> config_item *item,
>       return count;
>  }
>  
> +static ssize_t plane_supported_formats_show(struct config_item *item, char 
> *page)
> +{
> +     struct vkms_configfs_plane *plane;
> +
> +     plane = plane_item_to_vkms_configfs_plane(item);
> +
> +     page[0] = '\0';
> +
> +     scoped_guard(mutex, &plane->dev->config->lock) {
> +             u32 *formats = 
> vkms_config_plane_get_supported_formats(plane->config);
> +
> +             for (int i = 0;
> +                  i < 
> vkms_config_plane_get_supported_formats_count(plane->config);
> +                  i++) {
> +                     char tmp[6] = { 0 };
> +                     const ssize_t ret = snprintf(tmp, ARRAY_SIZE(tmp), 
> "%.*s\n",
> +                                            (int)sizeof(*formats),
> +                                            (char *)&formats[i]);
[ ... ]
> +static ssize_t plane_supported_formats_store(struct config_item *item,
> +                                          const char *page, size_t count)
> +{
[ ... ]
> +     scoped_guard(mutex, &plane->dev->config->lock) {
> +             if (plane->dev->enabled)
> +                     return -EBUSY;
> +
> +             char fmt[4] = {' ', ' ', ' ', ' '};
> +             u32 fourcc;
> +
> +             memcpy(fmt, &page[1], min(count - 1, 4));
> +
> +             if (page[0] == '+' && page[1] == '*')
> +                     ret = vkms_config_plane_add_all_formats(plane->config);
> +             else if (page[0] == '-' && page[1] == '*')
> +                     vkms_config_plane_remove_all_formats(plane->config);
> +             else if (page[0] == '+') {
> +                     memcpy(&fourcc, fmt, sizeof(fourcc));
> +                     ret = vkms_config_plane_add_format(plane->config, 
> fourcc);
> +             } else if (page[0] == '-') {
> +                     memcpy(&fourcc, fmt, sizeof(fourcc));
> +                     vkms_config_plane_remove_format(plane->config, fourcc);
> +             }

[Severity: Medium]
Are formats[i] and fourcc byte-reversed on big-endian architectures?

fourcc_code(a, b, c, d) defines DRM format constants as host-endian u32
values with 'a' in the least significant byte.  On big-endian machines,
casting &formats[i] to char * in plane_supported_formats_show() prints the
four characters in reverse order (for example, "42RA" instead of "AR24"),
and copying fmt into fourcc with memcpy() in
plane_supported_formats_store() produces a byte-swapped value that never
matches vkms_supported_plane_formats[], causing additions to fail with
-EINVAL and removals to be ignored.

Building the format code with fourcc_code(fmt[0], fmt[1], fmt[2], fmt[3])
in _store() and unpacking each byte in _show() would make this independent
of host endianness.

[Severity: Medium]
Can plane_supported_formats_store() cause a use-after-free or NULL pointer
dereference in vkms_config_show()?

vkms_config_show() reads plane_cfg->supported_formats and
plane_cfg->supported_formats_count under RCU without holding config->lock.
When device_enabled_store() disables a device, it clears dev->enabled and
drops dev->config->lock before calling vkms_destroy(), while the
vkms_config debugfs file is still registered:

CPU0                                CPU1
device_enabled_store()
  lock(&dev->config->lock)
  dev->enabled = false
  unlock(&dev->config->lock)
                                    vkms_config_show()
                                      rcu_read_lock()
                                      show_formats(m,
                                        plane_cfg->supported_formats,
                                        plane_cfg->supported_formats_count)
                                    plane_supported_formats_store()
                                      lock(&dev->config->lock)
                                      /* sees !plane->dev->enabled */
                                      vkms_config_plane_add_format()
                                        krealloc_array(supported_formats, ...)
                                      unlock(&dev->config->lock)
                                        ...
                                        seq_printf(m, ..., &formats[i])
                                          <-- use-after-free
  vkms_destroy(dev->config)

Similarly, if plane_supported_formats_store() calls
vkms_config_plane_remove_all_formats(), kfree(plane_cfg->supported_formats)
runs immediately without waiting for an RCU grace period, and a concurrent
vkms_config_show() can pass either a freed pointer or NULL with a non-zero
count to show_formats().

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

Reply via email to