linux-kernel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH] drm/ch7006: Fix a wild pointer dereference in ch7006_encoder_get_modes()
@ 2021-11-30 13:19 Zhou Qingyang
  0 siblings, 0 replies; only message in thread
From: Zhou Qingyang @ 2021-11-30 13:19 UTC (permalink / raw)
  To: zhou1615
  Cc: kjlu, David Airlie, Daniel Vetter, Ben Skeggs, Dave Airlie,
	dri-devel, linux-kernel

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 <zhou1615@umn.edu>
---
 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


^ permalink raw reply related	[flat|nested] only message in thread

only message in thread, other threads:[~2021-11-30 13:20 UTC | newest]

Thread overview: (only message) (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2021-11-30 13:19 [PATCH] drm/ch7006: Fix a wild pointer dereference in ch7006_encoder_get_modes() Zhou Qingyang

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).