Skip to contentSkip to conversation

drm/gud: validate TV mode names before creating enum property

PATCH In review Tao Yu 3 participants 4 messages Aug 19 – Aug 23

Subsystems (4)
lore
Share & tools
lore

Tao Yu

author
#0 lore raw

PATCH 1/1 drm/gud: validate TV mode names before creating enum property

The GUD protocol returns TV mode names as fixed-size GUD_CONNECTOR_TV_MODE_NAME_LEN entries and requires each name to be NUL-terminated.

gud_connector_add_tv_mode() currently passes each fixed-size entry directly to drm_mode_create_tv_properties_legacy(), which eventually reaches drm_property_add_enum() and strlen(). If a device returns an entry without a terminating NUL byte, strlen() reads past the end of the slot and can run beyond the allocated buffer, triggering an out-of-bounds read.

Validate that each returned TV mode name contains a NUL terminator within its fixed-size slot before passing it to the DRM property code. If a malformed entry is found, reject the device response with -EIO.

This fixes the out-of-bounds read without changing the handling of valid devices, and avoids silently truncating malformed protocol data.

Reported-by [email protected]
Fixes 40e1a70b4aed ("drm: Add GUD USB Display driver")
---
 drivers/gpu/drm/gud/gud_connector.c | 12 ++++++++++--
 1 file changed, 10 insertions(+), 2 deletions(-)
drivers/gpu/drm/gud/gud_connector.c +10 -2
index ea0cca58b7c89..8141c3a1e30ad 100644
@@ -396,8 +396,16 @@ static int gud_connector_add_tv_mode(struct gud_device *gdrm, struct drm_connect
 	}
 
 	num_modes = ret / GUD_CONNECTOR_TV_MODE_NAME_LEN;
-	for (i = 0; i < num_modes; i++)
-		modes[i] = &buf[i * GUD_CONNECTOR_TV_MODE_NAME_LEN];
+	for (i = 0; i < num_modes; i++) {
+		char *mode = &buf[i * GUD_CONNECTOR_TV_MODE_NAME_LEN];
+
+		if (!memchr(mode, '\0', GUD_CONNECTOR_TV_MODE_NAME_LEN)) {
+			ret = -EIO;
+			goto free;
+		}
+
+		modes[i] = mode;
+	}
 
 	ret = drm_mode_create_tv_properties_legacy(connector->dev, num_modes, modes);
 free:
Signature · 1 line
2.34.1
#1 lore raw
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:

Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:

Pre-existing issues:
- [High] A malicious or non-compliant USB device can cause a kernel NULL pointer dereference in drm_object_attach_property by ordering TV properties such that GUD_PROPERTY_TV_MODE is returned after other TV properties. --

--- Patch [1]: [PATCH] drm/gud: validate TV mode names before creating enum property --- commit db9280e75e6e04eacd06127a0240a5d76af230f9
Author: Tao Yu <[email protected]>

drm/gud: validate TV mode names before creating enum property

The GUD protocol returns TV mode names as fixed-size entries that require NUL-termination. This commit validates that each returned TV mode name contains a NUL terminator within its fixed-size slot before passing it to the DRM property code, preventing an out-of-bounds read in strlen().

Quoted 12 lines
drivers/gpu/drm/gud/gud_connector.c +2 -2
index ea0cca58b7c89..8141c3a1e30ad 100644
@@ -396,8 +396,16 @@ static int gud_connector_add_tv_mode(struct gud_device *gdrm, struct drm_connect
 	}
 
 	num_modes = ret / GUD_CONNECTOR_TV_MODE_NAME_LEN;
-	for (i = 0; i < num_modes; i++)
-		modes[i] = &buf[i * GUD_CONNECTOR_TV_MODE_NAME_LEN];
+	for (i = 0; i < num_modes; i++) {
+		char *mode = &buf[i * GUD_CONNECTOR_TV_MODE_NAME_LEN];

drivers/gpu/drm/gud/gud_connector.c static int gud_connector_add_tv_mode(struct gud_device *gdrm, struct drm_connect · line 410

Quoted 10 lines
drivers/gpu/drm/gud/gud_connector.c +8 -0
+
+		if (!memchr(mode, '\0', GUD_CONNECTOR_TV_MODE_NAME_LEN)) {
+			ret = -EIO;
+			goto free;
+		}
+
+		modes[i] = mode;
+	}
 
 	ret = drm_mode_create_tv_properties_legacy(connector->dev, num_modes, modes);

[Severity: High]
This is a pre-existing issue, but can a malicious or non-compliant USB device cause a kernel NULL pointer dereference in drm_object_attach_property() by ordering TV properties such that GUD_PROPERTY_TV_MODE is returned after other TV properties?

If a USB device returns GUD_PROPERTY_TV_HUE (or similar TV properties) before GUD_PROPERTY_TV_MODE, gud_connector_add_properties() processes it and calls drm_mode_create_tv_properties_legacy(drm, 0, NULL). This initializes dev->mode_config.tv_select_subconnector_property but leaves legacy_tv_mode_property as NULL since num_modes is 0.

Later, when GUD_PROPERTY_TV_MODE is processed, gud_connector_add_tv_mode() calls drm_mode_create_tv_properties_legacy() here with actual modes. Because tv_select_subconnector_property is already set, this second call hits an early return in drm_mode_create_tv_properties_legacy():

drivers/gpu/drm/drm_connector.c:drm_mode_create_tv_properties_legacy() {
	if (dev->mode_config.tv_select_subconnector_property)
		return 0;

...

This immediately succeeds without creating legacy_tv_mode_property. gud_connector_property_lookup() is then called for GUD_PROPERTY_TV_MODE and returns the uninitialized config->legacy_tv_mode_property, which is NULL.

In gud_connector_add_properties():

	property = gud_connector_property_lookup(connector, prop);
	if (drm_WARN_ON(drm, IS_ERR(property)))
		continue;

The IS_ERR(property) check fails to catch NULL, and drm_object_attach_property() is called with the NULL property, unconditionally dereferencing property->dev.

Quoted 2 lines
drivers/gpu/drm/gud/gud_connector.c +0 -0
 free:

kfree(buf);

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1
author
#2 lore raw

Thanks for the review.

This patch is intentionally limited to fixing the out-of-bounds read caused by malformed TV mode names. The NULL-dereference risk caused by TV property ordering is a separate pre-existing issue, and I am addressing it in a separate follow-up patch.

Thanks,
Tao

maintainer Reviewed-by on v1 1/1
#3 lore raw
In reply to Tao Yu
Quoted 11 lines · Tao Yu

The GUD protocol returns TV mode names as fixed-size GUD_CONNECTOR_TV_MODE_NAME_LEN entries and requires each name to be NUL-terminated.

gud_connector_add_tv_mode() currently passes each fixed-size entry directly to drm_mode_create_tv_properties_legacy(), which eventually reaches drm_property_add_enum() and strlen(). If a device returns an entry without a terminating NUL byte, strlen() reads past the end of the slot and can run beyond the allocated buffer, triggering an out-of-bounds read.

Quoted 3 lines

Validate that each returned TV mode name contains a NUL terminator within its fixed-size slot before passing it to the DRM property code. If a malformed entry is found, reject the device response with -EIO.

Hello, Thank you for the patch. An earlier patch that fixes this issue has been merged, however, this does look like a better fix of the issue, so I will apply this one over the other one, as this does not truncate the data.

Quoted 7 lines

This fixes the out-of-bounds read without changing the handling of valid devices, and avoids silently truncating malformed protocol data.

Reported-by [email protected]
Fixes 40e1a70b4aed ("drm: Add GUD USB Display driver")
Quoted 27 lines
---
 drivers/gpu/drm/gud/gud_connector.c | 12 ++++++++++--
 1 file changed, 10 insertions(+), 2 deletions(-)
drivers/gpu/drm/gud/gud_connector.c +10 -2
index ea0cca58b7c89..8141c3a1e30ad 100644
@@ -396,8 +396,16 @@ static int gud_connector_add_tv_mode(struct gud_device *gdrm, struct drm_connect
 	}
 
 	num_modes = ret / GUD_CONNECTOR_TV_MODE_NAME_LEN;
-	for (i = 0; i < num_modes; i++)
-		modes[i] = &buf[i * GUD_CONNECTOR_TV_MODE_NAME_LEN];
+	for (i = 0; i < num_modes; i++) {
+		char *mode = &buf[i * GUD_CONNECTOR_TV_MODE_NAME_LEN];
+
+		if (!memchr(mode, '\0', GUD_CONNECTOR_TV_MODE_NAME_LEN)) {
+			ret = -EIO;
+			goto free;
+		}
+
+		modes[i] = mode;
+	}
 
 	ret = drm_mode_create_tv_properties_legacy(connector->dev, num_modes, modes);
 free: