Am 10.10.25 um 10:16 schrieb Quanmin Yan:
Recently, we discovered the following issue through syzkaller:

BUG: KASAN: slab-use-after-free in fb_mode_is_equal+0x285/0x2f0
Read of size 4 at addr ff11000001b3c69c by task syz.xxx
...
Call Trace:
  <TASK>
  dump_stack_lvl+0xab/0xe0
  print_address_description.constprop.0+0x2c/0x390
  print_report+0xb9/0x280
  kasan_report+0xb8/0xf0
  fb_mode_is_equal+0x285/0x2f0
  fbcon_mode_deleted+0x129/0x180
  fb_set_var+0xe7f/0x11d0
  do_fb_ioctl+0x6a0/0x750
  fb_ioctl+0xe0/0x140
  __x64_sys_ioctl+0x193/0x210
  do_syscall_64+0x5f/0x9c0
  entry_SYSCALL_64_after_hwframe+0x76/0x7e

Based on experimentation and analysis, during framebuffer unregistration,
only the memory of fb_info->modelist is freed, without setting the
corresponding fb_display[i]->mode to NULL for the freed modes. This leads
to UAF issues during subsequent accesses. Here's an example of reproduction
steps:
1. With /dev/fb0 already registered in the system, load a kernel module
    to register a new device /dev/fb1;
2. Set fb1's mode to the global fb_display[] array (via FBIOPUT_CON2FBMAP);
3. Switch console from fb to VGA (to allow normal rmmod of the ko);
4. Unload the kernel module, at this point fb1's modelist is freed, leaving
    a wild pointer in fb_display[];
5. Trigger the bug via system calls through fb0 attempting to delete a mode
    from fb0.

Add a check in do_unregister_framebuffer(): if the mode to be freed exists
in fb_display[], set the corresponding mode pointer to NULL.

Signed-off-by: Quanmin Yan <[email protected]>

Reviewed-by: Thomas Zimmermann <[email protected]>

Thanks for fixing the bug.

---

Changes from v1
(https://lore.kernel.org/all/[email protected]/)
- Focus on fixing the issue specifically in the framebuffer unregistration
   path, as other paths do not encounter this problem.
- Adjusted according to Thomas's suggestions.

  drivers/video/fbdev/core/fbcon.c | 19 +++++++++++++++++++
  drivers/video/fbdev/core/fbmem.c |  1 +
  include/linux/fbcon.h            |  2 ++
  3 files changed, 22 insertions(+)

diff --git a/drivers/video/fbdev/core/fbcon.c b/drivers/video/fbdev/core/fbcon.c
index 96cc9b389246..9bd3c3814b5c 100644
--- a/drivers/video/fbdev/core/fbcon.c
+++ b/drivers/video/fbdev/core/fbcon.c
@@ -2810,6 +2810,25 @@ int fbcon_mode_deleted(struct fb_info *info,
        return found;
  }
+static void fbcon_delete_mode(struct fb_videomode *m)
+{
+       struct fbcon_display *p;
+
+       for (int i = first_fb_vc; i <= last_fb_vc; i++) {
+               p = &fb_display[i];
+               if (p->mode == m)
+                       p->mode = NULL;
+       }
+}
+
+void fbcon_delete_modelist(struct list_head *head)
+{
+       struct fb_modelist *modelist;
+
+       list_for_each_entry(modelist, head, list)
+               fbcon_delete_mode(&modelist->mode);
+}
+
  #ifdef CONFIG_VT_HW_CONSOLE_BINDING
  static void fbcon_unbind(void)
  {
diff --git a/drivers/video/fbdev/core/fbmem.c b/drivers/video/fbdev/core/fbmem.c
index 53f1719b1ae1..eff757ebbed1 100644
--- a/drivers/video/fbdev/core/fbmem.c
+++ b/drivers/video/fbdev/core/fbmem.c
@@ -544,6 +544,7 @@ static void do_unregister_framebuffer(struct fb_info 
*fb_info)
                fb_info->pixmap.addr = NULL;
        }
+ fbcon_delete_modelist(&fb_info->modelist);
        fb_destroy_modelist(&fb_info->modelist);
        registered_fb[fb_info->node] = NULL;
        num_registered_fb--;
diff --git a/include/linux/fbcon.h b/include/linux/fbcon.h
index 81f0e698acbf..f206370060e1 100644
--- a/include/linux/fbcon.h
+++ b/include/linux/fbcon.h
@@ -18,6 +18,7 @@ void fbcon_suspended(struct fb_info *info);
  void fbcon_resumed(struct fb_info *info);
  int fbcon_mode_deleted(struct fb_info *info,
                       struct fb_videomode *mode);
+void fbcon_delete_modelist(struct list_head *head);
  void fbcon_new_modelist(struct fb_info *info);
  void fbcon_get_requirement(struct fb_info *info,
                           struct fb_blit_caps *caps);
@@ -38,6 +39,7 @@ static inline void fbcon_suspended(struct fb_info *info) {}
  static inline void fbcon_resumed(struct fb_info *info) {}
  static inline int fbcon_mode_deleted(struct fb_info *info,
                                     struct fb_videomode *mode) { return 0; }
+static inline void fbcon_delete_modelist(struct list_head *head) {}
  static inline void fbcon_new_modelist(struct fb_info *info) {}
  static inline void fbcon_get_requirement(struct fb_info *info,
                                         struct fb_blit_caps *caps) {}

--
--
Thomas Zimmermann
Graphics Driver Developer
SUSE Software Solutions Germany GmbH
Frankenstrasse 146, 90461 Nuernberg, Germany
GF: Ivo Totev, Andrew Myers, Andrew McDonald, Boudien Moerman
HRB 36809 (AG Nuernberg)


Reply via email to