Skip to contentSkip to conversation

fbdev: Add bounds checking in bit_putcs to fix vmalloc-out-of-bounds

PATCH Applied Albin Babu Varghese 4 participants 6 messages Oct 3, 2025 – Oct 18

Subsystems (3)
lore
Share & tools
lore
#0 lore raw

PATCH v2 1/1 fbdev: Add bounds checking in bit_putcs to fix vmalloc-out-of-bounds

Add bounds checking to prevent writes past framebuffer boundaries when rendering text near screen edges. Return early if the Y position is off-screen and clip image height to screen boundary. Break from the rendering loop if the X position is off-screen. When clipping image width to fit the screen, update the character count to match the clipped width to prevent buffer size mismatches.

Without the character count update, bit_putcs_aligned and bit_putcs_unaligned receive mismatched parameters where the buffer is allocated for the clipped width but cnt reflects the original larger count, causing out-of-bounds writes.

---
Changes in v2:
- Partially render when height exceeding screen boundaries instead of skipping
- Update character count when width is clipped to prevent buffer mismatch

Link to v1:
https://lore.kernel.org/all/[email protected]/

---
 drivers/video/fbdev/core/bitblit.c | 20 ++++++++++++++++++++
 1 file changed, 20 insertions(+)
drivers/video/fbdev/core/bitblit.c +20 -0
index f9475c14f733..116b4bf5e94c 100644
@@ -160,6 +160,11 @@ static void bit_putcs(struct vc_data *vc, struct fb_info *info,
 	image.height = vc->vc_font.height;
 	image.depth = 1;
 
+	if (image.dy >= info->var.yres)
+		return;
+
+	image.height = min(image.height, info->var.yres - image.dy);
+
 	if (attribute) {
 		buf = kmalloc(cellsize, GFP_ATOMIC);
 		if (!buf)
@@ -173,6 +178,21 @@ static void bit_putcs(struct vc_data *vc, struct fb_info *info,
 			cnt = count;
 
 		image.width = vc->vc_font.width * cnt;
+
+		if (image.dx >= info->var.xres)
+			break;
+
+		if (image.dx + image.width > info->var.xres) {
+			image.width = info->var.xres - image.dx;
+
+			cnt = image.width / vc->vc_font.width;
+
+			if (cnt == 0)
+				break;
+
+			image.width = cnt * vc->vc_font.width;
+		}
+
 		pitch = DIV_ROUND_UP(image.width, 8) + scan_align;
 		pitch &= ~scan_align;
 		size = pitch * image.height + buf_align;
Signature · 1 line
2.51.0
maintainer Applied
#1 lore raw
Quoted 21 lines · Albin Babu Varghese

Add bounds checking to prevent writes past framebuffer boundaries when rendering text near screen edges. Return early if the Y position is off-screen and clip image height to screen boundary. Break from the rendering loop if the X position is off-screen. When clipping image width to fit the screen, update the character count to match the clipped width to prevent buffer size mismatches.

Without the character count update, bit_putcs_aligned and bit_putcs_unaligned receive mismatched parameters where the buffer is allocated for the clipped width but cnt reflects the original larger count, causing out-of-bounds writes.

---
Changes in v2:
- Partially render when height exceeding screen boundaries instead of skipping
- Update character count when width is clipped to prevent buffer mismatch

Quoted 5 lines

Link to v1:
https://lore.kernel.org/all/[email protected]/

---
  drivers/video/fbdev/core/bitblit.c | 20 ++++++++++++++++++++
  1 file changed, 20 insertions(+)

applied.

Thanks!
Helge

#2 lore raw
Quoted 28 lines · Helge Deller
Quoted 21 lines · Albin Babu Varghese

Add bounds checking to prevent writes past framebuffer boundaries when rendering text near screen edges. Return early if the Y position is off-screen and clip image height to screen boundary. Break from the rendering loop if the X position is off-screen. When clipping image width to fit the screen, update the character count to match the clipped width to prevent buffer size mismatches.

Without the character count update, bit_putcs_aligned and bit_putcs_unaligned receive mismatched parameters where the buffer is allocated for the clipped width but cnt reflects the original larger count, causing out-of-bounds writes.

---
Changes in v2:
- Partially render when height exceeding screen boundaries instead of skipping
- Update character count when width is clipped to prevent buffer mismatch

Quoted 5 lines

Link to v1:
https://lore.kernel.org/all/[email protected]/

---
  drivers/video/fbdev/core/bitblit.c | 20 ++++++++++++++++++++
  1 file changed, 20 insertions(+)
Quoted 4 lines

applied.

Thanks!
Helge

Thank you for merging the patch.

After the patch appeared in mainline, I observed that syzbot continues to find the same bug through different execution paths. My fix addressed bit_putcs, but the crashes now occur through bit_cursor and cw_putcs, which bypass bit_putcs entirely and go directly to sys_imageblit():

Crash 1 (cursor path):
https://syzkaller.appspot.com/text?tag=CrashReport&x=11fe95e2580000

Call trace: hide_cursor → bit_cursor → soft_cursor → sys_imageblit

Crash 2 (rotation path):
https://syzkaller.appspot.com/text?tag=CrashReport&x=164f0b04580000

Call trace: fbcon_modechanged → cw_putcs → sys_imageblit

The original syzbot reproducer depended on character height going out of bounds, so I focused on bit_putcs where character images are drawn. I naively overlooked cursor drawing - apologies for that. That's why I looked at the other crash reports after the merge, because it seemed odd that it was still hitting the bug after the fix.

I believe adding the same clipping logic in sys_imageblit() would provide comprehensive protection. Something like this:

void sys_imageblit(struct fb_info *p, const struct fb_image *image)
{
+ struct fb_image clipped;
+ u32 width, height;
+

         if (!(p->flags & FBINFO_VIRTFB))
                 fb_warn_once(p, "%s: framebuffer is not in virtual address space.\n", __func__);

- fb_imageblit(p, image);
+ if (!image || image->dx >= p->var.xres || image->dy >= p->var.yres)
+ return;
+
+ if (image->dx + image->width > p->var.xres || image->dy + image->height > p->var.yres) {
+ clipped = *image;
+
+ height = clipped.height;
+ if (clipped.dy + height > p->var.yres)
+ height = p->var.yres - clipped.dy;
+
+ clipped.height = height;
+
+ width = clipped.width;
+ if (clipped.dx + width > p->var.xres)
+ width = p->var.xres - clipped.dx;
+
+ clipped.width = width;
+
+ if (clipped.width == 0 || clipped.height == 0)
+ return;
+
+ fb_imageblit(p, &clipped);
+ } else {
+ fb_imageblit(p, image);
+ }
}

I don't have any way to test this other than testing it with the existing reproducers. I can prepare a patch if this approach sounds reasonable.

Thanks,
Albin

#3 lore raw
In reply to Helge Deller

Hi

Am 04.10.25 um 02:43 schrieb Helge Deller:

Quoted 24 lines · Albin Babu Varghese
Quoted 23 lines

Add bounds checking to prevent writes past framebuffer boundaries when rendering text near screen edges. Return early if the Y position is off-screen and clip image height to screen boundary. Break from the rendering loop if the X position is off-screen. When clipping image width to fit the screen, update the character count to match the clipped width to prevent buffer size mismatches.

Without the character count update, bit_putcs_aligned and bit_putcs_unaligned receive mismatched parameters where the buffer is allocated for the clipped width but cnt reflects the original larger count, causing out-of-bounds writes.

---
Changes in v2:
- Partially render when height exceeding screen boundaries instead of skipping
- Update character count when width is clipped to prevent buffer mismatch

Quoted 8 lines
Quoted 6 lines

Link to v1:
https://lore.kernel.org/all/[email protected]/

---
  drivers/video/fbdev/core/bitblit.c | 20 ++++++++++++++++++++

  1 file changed, 20 insertions(+)

applied.

This patch mangles fbcon output on fbdev HW drivers. It's reproducible on matroxfb and sisfb. Reverting the change restores these driver's functionality.

Fbcon from within DRM's fbdev emulation works.

Best regards
Thomas

Quoted 4 lines

Thanks!
Helge

Signature · 6 lines
--
Thomas Zimmermann
Graphics Driver Developer
SUSE Software Solutions Germany GmbH
Frankenstr. 146, 90461 Nürnberg, Germany, www.suse.com
GF: Stefan Gaiser, Jochen Jaser, Abhinav Puri, (HRB 36809, AG Nürnberg)
maintainer
#4 lore raw
Quoted 31 lines · Thomas Zimmermann

Hi

Am 04.10.25 um 02:43 schrieb Helge Deller:

Quoted 28 lines · Albin Babu Varghese
Quoted 26 lines

Add bounds checking to prevent writes past framebuffer boundaries when rendering text near screen edges. Return early if the Y position is off-screen and clip image height to screen boundary. Break from the rendering loop if the X position is off-screen. When clipping image width to fit the screen, update the character count to match the clipped width to prevent buffer size mismatches.

Without the character count update, bit_putcs_aligned and bit_putcs_unaligned receive mismatched parameters where the buffer is allocated for the clipped width but cnt reflects the original larger count, causing out-of-bounds writes.

---
Changes in v2:
- Partially render when height exceeding screen boundaries instead of skipping
- Update character count when width is clipped to prevent buffer mismatch

Link to v1:
https://lore.kernel.org/all/[email protected]/
---
  drivers/video/fbdev/core/bitblit.c | 20 ++++++++++++++++++++

  1 file changed, 20 insertions(+)
Quoted 5 lines
Quoted 1 line

applied.

This patch mangles fbcon output on fbdev HW drivers. It's reproducible on matroxfb and sisfb. Reverting the change restores these driver's functionality.

Huh?
Are you sure?
This patch was upstreamed one year ago:

commit 3637d34b35b287ab830e66048841ace404382b67

Author: Albin Babu Varghese <[email protected]>
Date:   Fri Oct 3 03:32:09 2025 -0400

fbdev: Add bounds checking in bit_putcs to fix vmalloc-out-of-bounds

I don't see any specific issues with stifb.

Helge

#5 lore raw
Quoted 7 lines · Helge Deller
Quoted 3 lines · Thomas Zimmermann

This patch mangles fbcon output on fbdev HW drivers. It's reproducible on matroxfb and sisfb. Reverting the change restores these driver's functionality.

Huh?
Are you sure?

[...]

Quoted 1 line

I don't see any specific issues with stifb.

Hi Helge, Thomas,

I believe this is the regression I sent a fix for yesterday:
https://lore.kernel.org/linux-fbdev/TYYP301MB1525C03FE71D47908263EBFCF69A2@TYYP301MB1525.JPNP301.PROD.OUTLOOK.COM/

matroxfb and sisfb set FBINFO_HWACCEL_YPAN and start with yres_virtual larger than yres. With CONFIG_FRAMEBUFFER_CONSOLE_LEGACY_ACCELERATION=y, fbcon then scrolls by panning and draws text rows up to yres_virtual. bit_putcs() clips to var.yres, so it drops these rows. stifb does not set FBINFO_HWACCEL_YPAN, so fbcon uses SCROLL_MOVE there and never draws below var.yres, which is why it looks fine.

The fix clips to GETVYRES() instead, so the bound from 3637d34b35b2 stays in place and the syzbot report should remain fixed.

Thomas, could you test whether the patch fixes matroxfb and sisfb for you?

Thanks,
CatMe0w