2021-11-30 13:20:02

by Zhou Qingyang

[permalink] [raw]
Subject: [PATCH] drm/ch7006: Fix a wild pointer dereference in ch7006_encoder_get_modes()

In ch7006_encoder_get_modes(), the return value of drm_mode_duplicate()
is directly used in drm_mode_probed_add(). drm_mode_probed_add() will
pass &mode->head to list_add_tail(). list_add_tail() will further
call __list_add() and there is a dereference of mode->head in
__list_add(), which could case a wild pointer dereference on failure
of drm_mode_duplicate().

Fix this bug by separating drm_mode_duplicate() from
drm_mode_probed_add() and adding a check of it.

This bug was found by a static analyzer. The analysis employs
differential checking to identify inconsistent security operations
(e.g., checks or kfrees) between two code paths and confirms that the
inconsistent operations are not recovered in the current function or
the callers, so they constitute bugs.

Note that, as a bug found by static analysis, it can be a false
positive or hard to trigger. Multiple researchers have cross-reviewed
the bug.

Builds with CONFIG_DRM_I2C_CH7006=m show no new warnings,
and our static analyzer no longer warns about this code.

Fixes: 6ee738610f41 ("drm/nouveau: Add DRM driver for NVIDIA GPUs")
Signed-off-by: Zhou Qingyang <[email protected]>
---
drivers/gpu/drm/i2c/ch7006_drv.c | 8 ++++++--
1 file changed, 6 insertions(+), 2 deletions(-)

diff --git a/drivers/gpu/drm/i2c/ch7006_drv.c b/drivers/gpu/drm/i2c/ch7006_drv.c
index b91e48d2190d..1207646562eb 100644
--- a/drivers/gpu/drm/i2c/ch7006_drv.c
+++ b/drivers/gpu/drm/i2c/ch7006_drv.c
@@ -227,6 +227,7 @@ static int ch7006_encoder_get_modes(struct drm_encoder *encoder,
{
struct ch7006_priv *priv = to_ch7006_priv(encoder);
const struct ch7006_mode *mode;
+ struct drm_display_mode *dup_mode;
int n = 0;

for (mode = ch7006_modes; mode->mode.clock; mode++) {
@@ -234,8 +235,11 @@ static int ch7006_encoder_get_modes(struct drm_encoder *encoder,
~mode->valid_norms & 1<<priv->norm)
continue;

- drm_mode_probed_add(connector,
- drm_mode_duplicate(encoder->dev, &mode->mode));
+ dup_mode = drm_mode_duplicate(encoder->dev, &mode->mode);
+ if (!dup_mode)
+ return -ENOMEM;
+
+ drm_mode_probed_add(connector, dup_mode);

n++;
}
--
2.25.1