From: Weidong Wang <[email protected]>

cirrusfb_check_var() raises xres_virtual to xres and then checks
xres_virtual * yres_virtual against the video memory in 32-bit
arithmetic. fb_set_var() checks the product for overflow, but only for
the values passed in, before the driver raises xres_virtual. A mode with
xres 65536, xres_virtual 640 and yres_virtual 65536 passes that check,
becomes 65536 x 65536 here, and the product wraps to 0, so the mode is
accepted on a 4 MiB card. The framebuffer console then draws far past
the end of the video memory mapping.

Reject an xres_virtual above 16384 and a yres_virtual above 65536, which
keeps the product below 2^30, and cap the yres_virtual the driver picks
for -1 ("use highest possible virtual resolution") at the same limit.
The yres_virtual limit is above 16384 because at 1 bpp a 4 MiB card
holds 52428 lines of 640 pixels, which the driver picks today.

Found by an AI code review agent. Reproduced on x86_64 under QEMU with
-vga cirrus (GD5446, 4 MiB), KASAN and video=cirrusfb:noaccel: the mode
above was accepted and fbcon faulted in cfb_imageblit(). With this patch
it is rejected, and 1024x768x8 and 640x480x1 with yres_virtual -1
(640x52428) are still accepted.

The reproducer, fix and changelog were drafted with AI assistance
(Claude), then reviewed and tested by the submitter.

Fixes: 09a2910e5464 ("cirrusfb: check_par fixes")
Cc: [email protected]
Cc: Zijie Zhao <[email protected]>
Signed-off-by: Weidong Wang <[email protected]>
---
Tested on v7.4-rc1 (05441d697f7f), x86_64, KASAN, QEMU/KVM -vga cirrus
(GD5446, 4 MiB), video=cirrusfb:noaccel. FBIOPUT_VSCREENINFO:

                                       unpatched           v2
  65536x128x8, virtual 640x65536       accepted as         rejected
                                       65536x65536, fbcon
                                       faults, panic
  640x480x1, yres_virtual -1           640x52428           640x52428
  640x200x8, virtual 16384x200         accepted            accepted
  640x200x8, virtual 16385x200         accepted            rejected
  1024x768x8                           accepted            accepted

No KASAN/UBSAN reports with v2; checkpatch clean. The code is the same
in linux-next. The reproducer is not attached (AI-found issue, see
security-bugs.rst) and can be shared on request.

Changes in v2:
- Bound xres_virtual (16384) and yres_virtual (65536) instead of doing
  the multiplication in 64 bits, as suggested by Helge. yres_virtual
  gets the higher limit so that 1 bpp modes that work today still do.
- Cap the yres_virtual the driver picks for -1 at the same limit.
- Fixes: 09a2910e5464, which added this check (pointed out by Sashiko).
- Shorter changelog.
- Link to v1: 
https://patch.msgid.link/[email protected]
---
 drivers/video/fbdev/cirrusfb.c | 11 +++++++++--
 1 file changed, 9 insertions(+), 2 deletions(-)

diff --git a/drivers/video/fbdev/cirrusfb.c b/drivers/video/fbdev/cirrusfb.c
index 2693b5cc053f..8e6fca3b2fed 100644
--- a/drivers/video/fbdev/cirrusfb.c
+++ b/drivers/video/fbdev/cirrusfb.c
@@ -527,6 +527,10 @@ static int cirrusfb_check_pixclock(struct 
fb_var_screeninfo *var,
        return 0;
 }
 
+/* keep xres_virtual * yres_virtual below 2^30 */
+#define CIRRUSFB_MAX_XRES_VIRTUAL      16384
+#define CIRRUSFB_MAX_YRES_VIRTUAL      65536
+
 static int cirrusfb_check_var(struct fb_var_screeninfo *var,
                              struct fb_info *info)
 {
@@ -579,7 +583,8 @@ static int cirrusfb_check_var(struct fb_var_screeninfo *var,
                var->xres_virtual = var->xres;
        /* use highest possible virtual resolution */
        if (var->yres_virtual == -1) {
-               var->yres_virtual = pixels / var->xres_virtual;
+               var->yres_virtual = min(pixels / var->xres_virtual,
+                                       CIRRUSFB_MAX_YRES_VIRTUAL);
 
                dev_info(info->device,
                         "virtual resolution set to maximum of %dx%d\n",
@@ -588,7 +593,9 @@ static int cirrusfb_check_var(struct fb_var_screeninfo *var,
        if (var->yres_virtual < var->yres)
                var->yres_virtual = var->yres;
 
-       if (var->xres_virtual * var->yres_virtual > pixels) {
+       if (var->xres_virtual > CIRRUSFB_MAX_XRES_VIRTUAL ||
+           var->yres_virtual > CIRRUSFB_MAX_YRES_VIRTUAL ||
+           var->xres_virtual * var->yres_virtual > pixels) {
                dev_err(info->device, "mode %dx%dx%d rejected... "
                      "virtual resolution too high to fit into video memory!\n",
                        var->xres_virtual, var->yres_virtual,

---
base-commit: 05441d697f7f9a927264f4058cbbc6a03dfcac9d
change-id: 20261112-c4-n053-2798a8d8029a

Best regards,
--  
Weidong Wang <[email protected]>


Reply via email to