Thank you for your contribution! Sashiko AI review found 3 potential
issues to consider:
- [High] drm/bridge: tda998x: UAF on cached bridge_connector in
  tda998x_audio_get_eld()
- [Medium] drm/bridge: tda998x: tda998x_detect_work() drops HPD before
  detect/edid_read
- [Medium] drm/bridge: tda998x: unlocked connector->eld read in
  tda998x_audio_get_eld()
--

commit 99202632262d8379c5cf8d903d42f1dd5e5ce3eb
Author: Luo Liu <[email protected]>

drm/bridge: tda998x: fix DRM_BRIDGE_ATTACH_NO_CONNECTOR support

This commit caches the drm_connector pointer passed to the bridge detect and
EDID read callbacks so mode setting, infoframe generation, ELD retrieval, and
hotplug notifications use the external bridge connector when attached with
DRM_BRIDGE_ATTACH_NO_CONNECTOR, and enables interlaced mode support on the
bridge.

> diff --git a/drivers/gpu/drm/bridge/tda998x_drv.c 
> b/drivers/gpu/drm/bridge/tda998x_drv.c
> index 0b966c9844e70..40379d74a53ac 100644
> --- a/drivers/gpu/drm/bridge/tda998x_drv.c
> +++ b/drivers/gpu/drm/bridge/tda998x_drv.c

[ ... ]

> @@ -769,6 +783,14 @@ static int tda998x_edid_delay_wait(struct tda998x_priv 
> *priv)
>       return wait_event_killable(priv->edid_delay_waitq, 
> !priv->edid_delay_active);
>  }
>  
> +static enum drm_connector_status tda998x_conn_detect(struct tda998x_priv 
> *priv)
> +{
> +     u8 val = cec_read(priv, REG_CEC_RXSHPDLEV);
> +
> +     return (val & CEC_RXSHPDLEV_HPD) ? connector_status_connected :
> +                     connector_status_disconnected;
> +}
> +
>  /*
>   * We need to run the KMS hotplug event helper outside of our threaded
>   * interrupt routine as this can call back into our get_modes method,
> @@ -778,10 +800,11 @@ static void tda998x_detect_work(struct work_struct 
> *work)
>  {
>       struct tda998x_priv *priv =
>               container_of(work, struct tda998x_priv, detect_work);
> -     struct drm_device *dev = priv->connector.dev;
>  
> -     if (dev)
> -             drm_kms_helper_hotplug_event(dev);
> +     if (priv->bridge_connector)
> +             drm_bridge_hpd_notify(&priv->bridge, tda998x_conn_detect(priv));
> +     else if (priv->connector.dev)
> +             drm_kms_helper_hotplug_event(priv->connector.dev);
>  }

[Severity: Medium]
Does checking priv->bridge_connector here drop HPD events that arrive before
tda998x_bridge_detect() or tda998x_bridge_edid_read() has been called?

When attached with DRM_BRIDGE_ATTACH_NO_CONNECTOR, priv->connector.dev is
NULL and priv->bridge_connector is only populated inside .detect() and
.edid_read().  drm_bridge_connector_init() and drm_kms_helper_poll_init() ->
drm_bridge_connector_enable_hpd() enable HPD without calling either callback
and without starting periodic polling when DRM_BRIDGE_OP_HPD is set.  If an
HPD interrupt fires before any caller probes the connector (or if a
downstream bridge in the chain handles DRM_BRIDGE_OP_DETECT and
DRM_BRIDGE_OP_EDID), both pointers are NULL and the notification is lost,
even though drm_bridge_hpd_notify() only needs &priv->bridge.

[ ... ]

> @@ -1152,10 +1176,10 @@ static int tda998x_audio_get_eld(struct device *dev, 
> void *data,
>                                uint8_t *buf, size_t len)
>  {
>       struct tda998x_priv *priv = dev_get_drvdata(dev);
> +     struct drm_connector *connector = tda998x_get_connector(priv);
>  
>       mutex_lock(&priv->audio_mutex);
> -     memcpy(buf, priv->connector.eld,
> -            min(sizeof(priv->connector.eld), len));
> +     memcpy(buf, connector->eld, min(sizeof(connector->eld), len));
>       mutex_unlock(&priv->audio_mutex);
>  
>       return 0;
>  }

[Severity: Medium]
Should tda998x_audio_get_eld() hold connector->eld_mutex while copying
connector->eld?

drm_edid_connector_update() zeroes and populates connector->eld under
connector->eld_mutex without holding priv->audio_mutex, so a concurrent
ALSA ELD read can copy a partially updated or transiently zeroed ELD buffer.

[ ... ]

> @@ -1390,6 +1406,8 @@ static void tda998x_bridge_detach(struct drm_bridge 
> *bridge)
>  {
>       struct tda998x_priv *priv = bridge_to_tda998x_priv(bridge);
>  
> +     priv->bridge_connector = NULL;
> +
>       if (priv->connector.dev)
>               drm_connector_cleanup(&priv->connector);
>  }

[ ... ]

> @@ -1701,6 +1720,9 @@ tda998x_bridge_edid_read(struct drm_bridge *bridge,
>       const struct drm_edid *drm_edid;
>       const struct edid *edid;
>  
> +     if (connector)
> +             priv->bridge_connector = connector;
> +
>       drm_edid = tda998x_edid_read(priv, connector);
>       if (!drm_edid) {
>               dev_dbg(&priv->hdmi->dev, "failed to get edid\n");
> @@ -1729,6 +1751,9 @@ tda998x_bridge_detect(struct drm_bridge *bridge,
>  {
>       struct tda998x_priv *priv = bridge_to_tda998x_priv(bridge);
>  
> +     if (connector)
> +             priv->bridge_connector = connector;
> +
>       return tda998x_conn_detect(priv);
>  }

[Severity: High]
Can caching the raw connector pointer in priv->bridge_connector cause a
use-after-free in tda998x_audio_get_eld()?

include/drm/drm_bridge.h forbids storing the connector parameter passed to
.edid_read() for future use.  Display controller drivers such as tilcdc
register their encoder with drmm before calling drm_bridge_connector_init():

drivers/gpu/drm/tilcdc/tilcdc_encoder.c:tilcdc_encoder_create() {
    ...
        encoder = drmm_simple_encoder_alloc(ddev, struct tilcdc_encoder,
                                            base, DRM_MODE_ENCODER_NONE);
    ...
        return tilcdc_attach_bridge(ddev, bridge);
}

Because drmm release actions run in reverse registration order while
priv->audio_pdev remains registered until tda998x_remove(), DRM teardown
frees the drm_bridge_connector before tda998x_bridge_detach() clears
priv->bridge_connector (and neither access is serialized by
priv->audio_mutex):

CPU0                                  CPU1
drm_managed_release()
  drmm_connector_init_release()
  drmm_kfree()  /* frees connector */
                                      tda998x_audio_get_eld()
                                        connector = tda998x_get_connector()
                                        memcpy(buf, connector->eld, ...)
                                          <-- use-after-free
  drmm_encoder_alloc_release()
    drm_encoder_cleanup()
      drm_bridge_detach()
        tda998x_bridge_detach()
          priv->bridge_connector = NULL;

In addition, if a downstream bridge in the chain handles
DRM_BRIDGE_OP_DETECT and DRM_BRIDGE_OP_EDID, drm_bridge_connector never
calls tda998x_bridge_detect() or tda998x_bridge_edid_read(), leaving
priv->bridge_connector NULL during tda998x_bridge_mode_set().

-- 
Sashiko AI review · 
https://sashiko.dev/#/patchset/[email protected]?part=4

Reply via email to