Thank you for your contribution! Sashiko AI review found 3 potential
issues to consider:
- [High] drm/vkms: premature config put in vkms_create() due to __free()
- [High] drm/vkms: use-after-free in vkms_config_connector_release() on
  config drain
- [Low] drm/vkms: duplicate linux/list.h include in vkms_config.h
--

commit 58266f4f95b4eb422dfae060ca602a9c586df7ee
Author: Louis Chauvet <[email protected]>

drm/vkms: Reference-count vkms_config and vkms_config_connector

This commit adds reference counting to struct vkms_config and struct
vkms_config_connector so configurations and connectors stay alive while
referenced outside configfs.

> diff --git a/drivers/gpu/drm/vkms/vkms_config.c 
> b/drivers/gpu/drm/vkms/vkms_config.c
> index 6699ddae71260..8cd4fe6551fb8 100644
> --- a/drivers/gpu/drm/vkms/vkms_config.c
> +++ b/drivers/gpu/drm/vkms/vkms_config.c
[ ... ]
> @@ -128,12 +133,31 @@ void vkms_config_destroy(struct vkms_config *config)
>       list_for_each_entry_safe(encoder_cfg, encoder_tmp, &config->encoders, 
> link)
>               vkms_config_destroy_encoder(config, encoder_cfg);
>  
> -     list_for_each_entry_safe(connector_cfg, connector_tmp, 
> &config->connectors, link)
> -             vkms_config_destroy_connector(connector_cfg);
> +     list_for_each_entry_safe(connector_cfg, connector_tmp, 
> &config->connectors, link) {
> +             /* The config is dying: drop the list's reference on each 
> connector */
> +             vkms_config_connector_put(connector_cfg);
> +     }
>  
>       mutex_destroy(&config->lock);
>       kfree_const(config->dev_name);
> -     kfree(config);
> +     kfree_rcu(config, rcu);
> +}
[ ... ]
> @@ -618,12 +644,48 @@ struct vkms_config_connector 
> *vkms_config_create_connector(struct vkms_config *c
>  }
>  EXPORT_SYMBOL_IF_KUNIT(vkms_config_create_connector);
>  
> -void vkms_config_destroy_connector(struct vkms_config_connector 
> *connector_cfg)
> +static void vkms_config_connector_release(struct kref *ref)
>  {
> +     struct vkms_config_connector *connector_cfg =
> +             container_of(ref, struct vkms_config_connector, refcnt);
>       xa_destroy(&connector_cfg->possible_encoders);
> -     list_del_rcu(&connector_cfg->link);
> +     if (connector_cfg->unlinked) {
> +             /* Detached by vkms_config_destroy_connector(): drop its config 
> pin */
> +             vkms_config_put(connector_cfg->config);
> +     } else {
> +             /* The config is dying and is draining us: just unlink */
> +             list_del_init(&connector_cfg->link);
> +     }
>       kfree_rcu(connector_cfg, rcu);
>  }

[Severity: High]
Can vkms_config_connector_release() trigger a use-after-free on
config->connectors when a connector outlives its config without going
through vkms_config_destroy_connector()?

vkms_config_connector_get() increments connector_cfg->refcnt without
pinning connector_cfg->config.  If config is released while a connector
still linked in config->connectors holds an extra reference,
vkms_config_release() drops the list reference and frees config via
kfree_rcu() while connector_cfg->link stays attached to config->connectors
with connector_cfg->unlinked set to false.  When the final reference on
connector_cfg is later dropped, vkms_config_connector_release() runs
list_del_init(&connector_cfg->link) and writes into the freed
config->connectors list head.

> diff --git a/drivers/gpu/drm/vkms/vkms_config.h 
> b/drivers/gpu/drm/vkms/vkms_config.h
> index 125273f67e7d7..95e5c3694742f 100644
> --- a/drivers/gpu/drm/vkms/vkms_config.h
> +++ b/drivers/gpu/drm/vkms/vkms_config.h
> @@ -3,9 +3,14 @@
>  #ifndef _VKMS_CONFIG_H_
>  #define _VKMS_CONFIG_H_
>  
> +#include <linux/cleanup.h>
> +#include <linux/kref.h>
>  #include <linux/list.h>
>  #include <linux/rculist.h>
>  #include <linux/mutex.h>
> +#include <linux/rcupdate.h>
> +#include <linux/string.h>
> +#include <linux/list.h>

[Severity: Low]
linux/list.h is included twice here.

> diff --git a/drivers/gpu/drm/vkms/vkms_drv.c b/drivers/gpu/drm/vkms/vkms_drv.c
> index 4cbb1173fcc62..24572bc779b41 100644
> --- a/drivers/gpu/drm/vkms/vkms_drv.c
> +++ b/drivers/gpu/drm/vkms/vkms_drv.c
> @@ -163,6 +163,10 @@ int vkms_create(struct vkms_config *config)
>       struct faux_device *fdev;
>       struct vkms_device *vkms_device;
>       const char *dev_name;
> +     /* The device keeps the config alive for its whole lifetime */
> +     struct vkms_config *cfg __free(vkms_config_put) = config;
> +
> +     vkms_config_get(cfg);

[Severity: High]
Does __free(vkms_config_put) drop the device's reference on cfg as soon as
vkms_create() returns?

Because cfg is never passed to no_free_ptr() on the success path (and
vkms_destroy() does not call vkms_config_put()), the cleanup handler runs
when vkms_create() exits scope.  That leaves vkms_device->config without a
device-owned reference, so freeing the configuration while the DRM device
is still active causes a use-after-free when DRM or debugfs paths access
vkms_device->config.

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

Reply via email to