Skip to contentSkip to conversation

drm/modes: Fix unterminated strncpy

PATCH Superseded Chuhong Yuan 2 participants 2 messages Jul 30, 2019

Subsystems (2)

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 drm/modes: Fix unterminated strncpy

strncpy(dest, src, strlen(src)) leads to unterminated dest, which is dangerous.
Fix it by using strscpy.

---
 drivers/gpu/drm/drm_modes.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/gpu/drm/drm_modes.c +1 -1
index 80fcd5dc1558..170fc24e0f31 100644
@@ -1770,7 +1770,7 @@ bool drm_mode_parse_command_line_for_connector(const char *mode_option,
 	}
 
 	if (named_mode) {
-		strncpy(mode->name, name, mode_end);
+		strscpy(mode->name, name, mode_end + 1);
 	} else {
 		ret = drm_mode_parse_cmdline_res_mode(name, mode_end,
 						      parse_extras,
Signature · 1 line
2.20.1
#1 lore raw
Quoted 14 lines · Chuhong Yuan

strncpy(dest, src, strlen(src)) leads to unterminated dest, which is dangerous.
Fix it by using strscpy.

---
 drivers/gpu/drm/drm_modes.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/gpu/drm/drm_modes.c +0 -0
index 80fcd5dc1558..170fc24e0f31 100644

drivers/gpu/drm/drm_modes.c bool drm_mode_parse_command_line_for_connector(const char *mode_option, · line 1773

Quoted 5 lines
drivers/gpu/drm/drm_modes.c +1 -1
@@ -1770,7 +1770,7 @@ bool drm_mode_parse_command_line_for_connector(const char *mode_option,
 	}
 
 	if (named_mode) {
-		strncpy(mode->name, name, mode_end);
+		strscpy(mode->name, name, mode_end + 1);

Shouldn't you be checking that mode_end + 1 is not > than the size of mode->name (ie: DRM_DISPLAY_MODE_LEN)? This still seems unsafe.

Sean

Quoted 6 lines
drivers/gpu/drm/drm_modes.c +0 -0
 	} else {
 		ret = drm_mode_parse_cmdline_res_mode(name, mode_end,
 						      parse_extras,
Signature · 1 line
2.20.1
Signature · 1 line
Sean Paul, Software Engineer, Google / Chromium OS