Thank you for your contribution! Sashiko AI review found 1 potential
issue to consider:
- [Medium] drm/atomic_sro: out-of-bounds write in
  drm_atomic_sro_set_connector_state()
--

commit b5d5ae4218e75eb40be891ff0550153b170b927a
Author: Maxime Ripard <[email protected]>

drm/atomic_sro: Provide helpers to implement hardware state readout

This commit adds helpers to read out hardware state across KMS objects,
build a drm_atomic_sro_state container, and install the read-out states.

> diff --git a/drivers/gpu/drm/drm_atomic_state_readout_helper.c 
> b/drivers/gpu/drm/drm_atomic_state_readout_helper.c
> new file mode 100644
> index 0000000000000..f290d2fe5496e
> --- /dev/null
> +++ b/drivers/gpu/drm/drm_atomic_state_readout_helper.c

[ ... ]

> +static int
> +drm_atomic_helper_sro_readout_connector_state(struct drm_device *dev,
> +                                           struct drm_atomic_sro_state 
> *state,
> +                                           struct drm_connector *connector)
> +{
> +     const struct drm_connector_funcs *conn_funcs = connector->funcs;
> +     struct drm_connector_state *conn_state;
> +     int ret;
> +
> +     if (!conn_funcs->atomic_sro_readout_state) {
> +             /*
> +              * Missing readout implementations are only allowed
> +              * when drm.atomic_readout skips them, but the object
> +              * still needs a valid state: fall back to a pristine
> +              * state, like drm_mode_config_create_state() would.
> +              */
> +             drm_warn(dev, "No connector readout implementation.");
> +
> +             conn_state = conn_funcs->atomic_create_state(connector);
> +             if (drm_WARN_ON(dev, IS_ERR(conn_state)))
> +                     return PTR_ERR(conn_state);
> +
> +             drm_atomic_sro_set_connector_state(state, connector, 
> conn_state);
> +             return 0;
> +     }
> +
> +     drm_dbg_kms(dev, "Initializing connector %s state.\n",
> +                 connector->name);
> +
> +     conn_state = conn_funcs->atomic_create_state(connector);
> +     if (drm_WARN_ON(dev, IS_ERR(conn_state)))
> +             return PTR_ERR(conn_state);
> +
> +     ret = conn_funcs->atomic_sro_readout_state(connector, state,
> +                                                conn_state);
> +     if (ret) {
> +             drm_dbg_kms(dev, "Connector %s readout failed: %pe\n",
> +                         connector->name, ERR_PTR(ret));
> +             conn_funcs->atomic_destroy_state(connector, conn_state);
> +             return ret;
> +     }
> +
> +     drm_atomic_sro_set_connector_state(state, connector, conn_state);
> +
> +     return 0;
> +}

[ ... ]

> +int
> +drm_atomic_helper_sro_readout_connectors_state(struct drm_device *dev,
> +                                            struct drm_atomic_sro_state 
> *state,
> +                                            struct drm_atomic_commit *commit)
> +{
> +     struct drm_connector_list_iter conn_iter;
> +     struct drm_connector *connector;
> +     int ret;
> +
> +     might_alloc(GFP_KERNEL);
> +
> +     if (commit)
> +             return 
> drm_atomic_helper_sro_readout_commit_connectors_state(dev, state, commit);
> +
> +     drm_connector_list_iter_begin(dev, &conn_iter);
> +     drm_for_each_connector_iter(connector, &conn_iter) {
> +             ret = drm_atomic_helper_sro_readout_connector_state(dev, state, 
> connector);

[Severity: Medium]
Can this write out of bounds of state->connectors[] if a dynamic connector
is registered after drm_atomic_sro_state_alloc()?

drm_atomic_sro_state_alloc() sizes state->connectors to
state->max_connector_index + 1, and the kerneldoc for max_connector_index
notes that connectors registered afterwards have no entry in
state->connectors.  However, drm_connector_dynamic_register() adds new
connectors under config->connector_list_lock without taking modeset locks.
If a DP MST hotplug worker registers a connector between
drm_atomic_sro_state_alloc() and
drm_atomic_helper_sro_readout_connectors_state(), the connector list
iterator visits it with drm_connector_index(connector) >
state->max_connector_index, and drm_atomic_sro_set_connector_state()
writes past the end of state->connectors[]:

drivers/gpu/drm/drm_atomic_state_readout.c:drm_atomic_sro_set_connector_state() 
{
        unsigned int index = drm_connector_index(conn);

        drm_connector_get(conn);
        state->connectors[index].ptr = conn;
        state->connectors[index].state = conn_state;
    ...
}

Because drm_atomic_helper_sro_install_state() and
drm_atomic_sro_state_free() only iterate up to state->max_connector_index,
this also leaks the connector reference and conn_state.  Should
drm_atomic_helper_sro_readout_connector_state() skip connectors with
drm_connector_index(connector) > state->max_connector_index?

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

Reply via email to