fbcon: Make sure modelist not set on unregistered console
authorKees Cook <kees@kernel.org>
Fri, 9 May 2025 20:06:47 +0000 (13:06 -0700)
committerHelge Deller <deller@gmx.de>
Sat, 31 May 2025 08:24:02 +0000 (10:24 +0200)
It looks like attempting to write to the "store_modes" sysfs node will
run afoul of unregistered consoles:

UBSAN: array-index-out-of-bounds in drivers/video/fbdev/core/fbcon.c:122:28
index -1 is out of range for type 'fb_info *[32]'
...
 fbcon_info_from_console+0x192/0x1a0 drivers/video/fbdev/core/fbcon.c:122
 fbcon_new_modelist+0xbf/0x2d0 drivers/video/fbdev/core/fbcon.c:3048
 fb_new_modelist+0x328/0x440 drivers/video/fbdev/core/fbmem.c:673
 store_modes+0x1c9/0x3e0 drivers/video/fbdev/core/fbsysfs.c:113
 dev_attr_store+0x55/0x80 drivers/base/core.c:2439

static struct fb_info *fbcon_registered_fb[FB_MAX];
...
static signed char con2fb_map[MAX_NR_CONSOLES];
...
static struct fb_info *fbcon_info_from_console(int console)
...
        return fbcon_registered_fb[con2fb_map[console]];

If con2fb_map contains a -1 things go wrong here. Instead, return NULL,
as callers of fbcon_info_from_console() are trying to compare against
existing "info" pointers, so error handling should kick in correctly.

Reported-by: syzbot+a7d4444e7b6e743572f7@syzkaller.appspotmail.com
Closes: https://lore.kernel.org/all/679d0a8f.050a0220.163cdc.000c.GAE@google.com/
Signed-off-by: Kees Cook <kees@kernel.org>
Signed-off-by: Helge Deller <deller@gmx.de>
drivers/video/fbdev/core/fbcon.c

index ac3c99e..2df4803 100644 (file)
@@ -117,9 +117,14 @@ static signed char con2fb_map_boot[MAX_NR_CONSOLES];
 
 static struct fb_info *fbcon_info_from_console(int console)
 {
+       signed char fb;
        WARN_CONSOLE_UNLOCKED();
 
-       return fbcon_registered_fb[con2fb_map[console]];
+       fb = con2fb_map[console];
+       if (fb < 0 || fb >= ARRAY_SIZE(fbcon_registered_fb))
+               return NULL;
+
+       return fbcon_registered_fb[fb];
 }
 
 static int logo_lines;