drm/drv: Add buffer bounds check in drm_dev_wedged_event()
Subsystems (2)
PATCH 1/1 drm/drv: Add buffer bounds check in drm_dev_wedged_event()
event_string[] has a fixed size of WEDGE_STR_LEN (32) bytes. Since scnprintf() reserves space for the terminating null byte, it can write at most 31 characters.
When len reaches WEDGE_STR_LEN - 1, only the null terminator fits in the remaining space. Any further scnprintf() calls return 0 and additional recovery method names are silently dropped, making the truncation hard to detect.
Add a drm_WARN_ON() check for len >= WEDGE_STR_LEN - 1 before attempting another write. If the buffer is already full, emit a warning and stop processing further entries. This makes buffer truncation visible and allows the loop to exit cleanly instead of silently ignoring recovery methods.
--- drivers/gpu/drm/drm_drv.c | 3 +++ 1 file changed, 3 insertions(+)
drivers/gpu/drm/drm_drv.c +3 -0
@@ -578,6 +578,9 @@ int drm_dev_wedged_event(struct drm_device *dev, unsigned long method,if (drm_WARN_ONCE (dev, !recovery,"invalid recovery method %u\n" , opt))break ; +if (drm_WARN_ON (dev, len >=WEDGE_STR_LEN -1 )) +break ; + len +=scnprintf (event_string + len,sizeof (event_string) - len,"%s," , recovery); }
Signature · 1 line
2.48.1
[email protected]
AI reviewThank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider: - [Medium] The newly added buffer bounds check evaluates truncation at the wrong time, missing partial…
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [Medium] The newly added buffer bounds check evaluates truncation at the wrong time, missing partial truncations on the final recovery method and causing string corruption. --
--- Patch [1]: [PATCH] drm/drv: Add buffer bounds check in drm_dev_wedged_event() --- commit d685223ec7cc48ba38c7ea409895bbfb9a46aadc
Author: Mallesh Koujalagi <[email protected]>
drm/drv: Add buffer bounds check in drm_dev_wedged_event()
This patch adds a bounds check to prevent silent truncation when formatting the wedged event string. It emits a warning and breaks the loop if the buffer is full.
drivers/gpu/drm/drm_drv.c int drm_dev_wedged_event(struct drm_device *dev, unsigned long method, · line 584
Quoted 12 lines
drivers/gpu/drm/drm_drv.c +3
-0@@ -578,6 +578,9 @@ int drm_dev_wedged_event(struct drm_device *dev, unsigned long method,if (drm_WARN_ONCE (dev, !recovery,"invalid recovery method %u\n" , opt))break ; +if (drm_WARN_ON (dev, len >=WEDGE_STR_LEN -1 )) +break ; + len +=scnprintf (event_string + len,sizeof (event_string) - len,"%s," , recovery);
[Severity: Medium]
Since this bounds check is placed before the scnprintf() call, what happens if the string is partially truncated during the final iteration of the loop?
If multiple recovery methods are present and their combined length exceeds the 32-byte buffer during the last iteration, scnprintf() will partially truncate the string. Because this is the last iteration, the loop terminates without ever reaching the bounds check on the next iteration.
Consequently, the truncation goes unnoticed, and the post-loop cleanup code:
if (recovery) /* Get rid of trailing comma */ event_string[len - 1] = '\0';
will overwrite the last valid character of the truncated string rather than a trailing comma, emitting a malformed string to userspace.
Should the truncation check also evaluate the result of the scnprintf() call to ensure partial truncations are caught on the final loop iteration?
Quoted 1 line
drivers/gpu/drm/drm_drv.c +0
-0}
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1
Quoted 26 lines · Mallesh Koujalagi
event_string[] has a fixed size of
WEDGE_STR_LEN(32) bytes. Sincescnprintf()reserves space for the terminating null byte, it can write at most 31 characters.When len reaches
WEDGE_STR_LEN- 1, only the null terminator fits in the remaining space. Any furtherscnprintf()calls return 0 and additional recovery method names are silently dropped, making the truncation hard to detect.Add a
drm_WARN_ON()check for len >=WEDGE_STR_LEN- 1 before attempting another write. If the buffer is already full, emit a warning and stop processing further entries. This makes buffer truncation visible and allows the loop to exit cleanly instead of silently ignoring recovery methods.Fixes b7cf9f4ac1b8 ("drm: Introduce device wedged event")Signed-off-by Mallesh Koujalagi <[email protected]>--- drivers/gpu/drm/drm_drv.c | 3 +++ 1 file changed, 3 insertions(+)drivers/gpu/drm/drm_drv.c +0
-0
drivers/gpu/drm/drm_drv.c int drm_dev_wedged_event(struct drm_device *dev, unsigned long method, · line 581
Quoted 4 lines
drivers/gpu/drm/drm_drv.c +1
-0@@ -578,6 +578,9 @@ int drm_dev_wedged_event(struct drm_device *dev, unsigned long method,if (drm_WARN_ONCE (dev, !recovery,"invalid recovery method %u\n" , opt))break ; +if (drm_WARN_ON (dev, len >=WEDGE_STR_LEN -1 ))
len is the starting offset for current method, so the string can still be truncated. And we also have to account for the trailing comma which we later drop, so I think the correct logic would be something like
if (drm_WARN_ON(dev, len + strlen(recovery) + 1 >= WEDGE_STR_LEN - 1))
which looks worth an explanation here.
Raag
Quoted 8 lines
drivers/gpu/drm/drm_drv.c +2
-0+break ; + len +=scnprintf (event_string + len,sizeof (event_string) - len,"%s," , recovery); }Signature · 1 line
2.48.1