fbdev: udlfb: don't rebuild the mode list while the framebuffer is in use
PATCH 1/1 fbdev: udlfb: don't rebuild the mode list while the framebuffer is in use
dlfb_setup_modes() frees info->modelist with fb_destroy_modelist() and rebuilds it, but fbcon holds raw pointers into that list in fb_display[i].mode and nothing un-publishes them the way do_unregister_framebuffer() and store_modes() do with fbcon_delete_modelist(). The function does refuse to install a new mode while the framebuffer has users (dlfb->fb_count != 0), but only after the list is already destroyed. Reached from the 0666 "edid" sysfs attribute, an unprivileged write to /sys/class/graphics/fb0/edid therefore fails with -EINVAL and still leaves every fb_display[i].mode dangling; the next console switch reads the freed fb_videomode in fb_videomode_to_var().
Test fb_count before touching the list. fbcon takes a reference through fbcon_open(), so that condition covers exactly the states in which fb_display[] points into the list, and the errno returned to userspace is unchanged.
BUG: KASAN: slab-use-after-free in fb_videomode_to_var (drivers/video/fbdev/core/modedb.c:905) Read of size 4 at addr ffff8880107b449c by task kworker/1:1/47 Workqueue: events console_callback Call Trace: fb_videomode_to_var (drivers/video/fbdev/core/modedb.c:905) display_to_var (drivers/video/fbdev/core/fbcon.c:998) fbcon_switch (drivers/video/fbdev/core/fbcon.c:2182) redraw_screen (drivers/tty/vt/vt.c:994) complete_change_console (drivers/tty/vt/vt_ioctl.c:1141) console_callback (drivers/tty/vt/vt.c:3358) process_one_work (kernel/workqueue.c:3396) worker_thread (kernel/workqueue.c:3479 kernel/workqueue.c:3560) kthread (kernel/kthread.c:436) ret_from_fork (arch/x86/kernel/process.c:158) ret_from_fork_asm (arch/x86/entry/entry_64.S:245) ... The buggy address is located 28 bytes inside of freed 96-byte region [ffff8880107b4480, ffff8880107b44e0)
--- drivers/video/fbdev/udlfb.c | 5 +++++ 1 file changed, 5 insertions(+)
drivers/video/fbdev/udlfb.c +5 -0
@@ -1293,6 +1293,11 @@ static int dlfb_setup_modes(struct dlfb_data *dlfb, goto error; } +if (dlfb->fb_count) { + result = -EINVAL ; + goto error; + } +fb_destroy_modelist (&info->modelist);memset (&info->monspecs,0 ,sizeof (info->monspecs));
Signature · 1 line
2.43.0
[email protected]
AI reviewThank you for your contribution! Sashiko AI review found 5 potential issue(s) to consider:
Thank you for your contribution! Sashiko AI review found 5 potential issue(s) to consider:
Pre-existing issues:
- [High] Missing locking in edid_store() leads to data races, double free, and memory corruption.
- [High] The patch fails to prevent a Use-After-Free of fb_info->mode via sysfs attributes.
- [High] The edid sysfs attribute is world-writable (0666), enabling unprivileged users to exploit multiple driver vulnerabilities.
- [Medium] Repeated calls to dlfb_setup_modes() leak the old dlfb->edid buffer.
- [Medium] Unbounded memory leak of the dynamically allocated info->monspecs.modedb array.
--
commit c422e1f65727b9cb9b5d809088416cf97c54aea9
Author: Xiang Mei <[email protected]>
fbdev: udlfb: don't rebuild the mode list while the framebuffer is in use
This commit attempts to fix a use-after-free in fb_videomode_to_var() by checking dlfb->fb_count before destroying the modelist in dlfb_setup_modes(). This prevents fbcon from referencing a freed mode list.
Quoted 5 lines
drivers/video/fbdev/udlfb.c +0
-0
drivers/video/fbdev/udlfb.c static int dlfb_setup_modes(struct dlfb_data *dlfb, · line 1301
Quoted 9 lines
drivers/video/fbdev/udlfb.c +5
-0@@ -1293,6 +1293,11 @@ static int dlfb_setup_modes(struct dlfb_data *dlfb, goto error; } +if (dlfb->fb_count) { + result = -EINVAL ; + goto error; + } +fb_destroy_modelist (&info->modelist);
[Severity: High]
This isn't a bug introduced by this patch, but does writing to the mode sysfs attribute increment dlfb->fb_count?
If an unprivileged user sets fb_info->mode to a node in info->modelist via fbsysfs.c:store_mode(), and then writes to the edid sysfs attribute, dlfb->fb_count remains 0. The check above passes, and fb_destroy_modelist() is executed, leaving fb_info->mode as a dangling pointer.
Subsequent reads from the mode sysfs attribute would invoke show_mode(), triggering a use-after-free.
drivers/video/fbdev/udlfb.c static int dlfb_setup_modes(struct dlfb_data *dlfb, · line 1302
Quoted 1 line
drivers/video/fbdev/udlfb.c +0
-0memset (&info->monspecs,0 ,sizeof (info->monspecs));
[Severity: Medium]
This is a pre-existing issue, but does this memset() leak the dynamically allocated info->monspecs.modedb array?
If fb_edid_to_monspecs() previously parsed the EDID and dynamically allocated the array of struct fb_videomode, unconditionally zeroing the structure here appears to erase the pointer before it can be freed, leaking the memory on every sysfs write.
[Severity: Medium]
While not introduced by this patch, could this leak the old dlfb->edid buffer?
When dlfb_setup_modes() succeeds:
drivers/video/fbdev/udlfb.c:dlfb_setup_modes() {
...
if (info->monspecs.modedb_len > 0) {
dlfb->edid = edid;
dlfb->edid_size = i;
break;
}
...
}Does this overwrite dlfb->edid with a newly allocated 128-byte edid buffer without freeing the previous pointer?
[Severity: High]
This is a pre-existing issue, but is it safe for edid_store() to call dlfb_ops_set_par() without holding info->lock?
drivers/video/fbdev/udlfb.c:edid_store() {
...
ret = dlfb_ops_set_par(fb_info);
...
}dlfb_ops_set_par() invokes dlfb_realloc_framebuffer() which performs memory freeing and list_add() operations, explicitly relying on the lock:
drivers/video/fbdev/udlfb.c:dlfb_realloc_framebuffer() {
/*
* Assumes &info->lock held by caller
*/
static int dlfb_realloc_framebuffer(struct dlfb_data *dlfb,
struct fb_info *info, u32 new_len)
...
}
If an unprivileged user concurrently writes to the edid sysfs attribute, could this lead to data races resulting in a double-free of info->screen_buffer and list corruption on dlfb->deferred_free?
[Severity: High]
Finally, as a pre-existing issue, should the edid sysfs attribute be world-writable?
drivers/video/fbdev/udlfb.c:edid_attr {
static const struct bin_attribute edid_attr = {
.attr.name = "edid",
.attr.mode = 0666,
.size = EDID_LENGTH,
.read = edid_show,
.write = edid_store
};
}
This 0666 permission allows any unprivileged user to alter the physical display configuration, and easily trigger the memory leaks, double frees, and use-after-free vulnerabilities mentioned above.
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1
Helge Deller
maintainerQuoted 41 lines · Xiang Mei
dlfb_setup_modes()freesinfo->modelistwithfb_destroy_modelist()and rebuilds it, but fbcon holds raw pointers into that list in fb_display[i].mode and nothing un-publishes them the waydo_unregister_framebuffer()andstore_modes()do withfbcon_delete_modelist(). The function does refuse to install a new mode while the framebuffer has users (dlfb->fb_count!= 0), but only after the list is already destroyed. Reached from the 0666 "edid" sysfs attribute, an unprivileged write to/sys/class/graphics/fb0/edidtherefore fails with-EINVALand still leaves every fb_display[i].mode dangling; the next console switch reads the freedfb_videomodeinfb_videomode_to_var().Test
fb_countbefore touching the list. fbcon takes a reference throughfbcon_open(), so that condition covers exactly the states in which fb_display[] points into the list, and the errno returned to userspace is unchanged.BUG: KASAN: slab-use-after-free in fb_videomode_to_var (drivers/video/fbdev/core/modedb.c:905) Read of size 4 at addr ffff8880107b449c by task kworker/1:1/47 Workqueue: events console_callback Call Trace: fb_videomode_to_var (drivers/video/fbdev/core/modedb.c:905) display_to_var (drivers/video/fbdev/core/fbcon.c:998) fbcon_switch (drivers/video/fbdev/core/fbcon.c:2182) redraw_screen (drivers/tty/vt/vt.c:994) complete_change_console (drivers/tty/vt/vt_ioctl.c:1141) console_callback (drivers/tty/vt/vt.c:3358) process_one_work (kernel/workqueue.c:3396) worker_thread (kernel/workqueue.c:3479 kernel/workqueue.c:3560) kthread (kernel/kthread.c:436) ret_from_fork (arch/x86/kernel/process.c:158) ret_from_fork_asm (arch/x86/entry/entry_64.S:245) ... The buggy address is located 28 bytes inside of freed 96-byte region [ffff8880107b4480, ffff8880107b44e0)Fixes 7d9485e2c53c ("Staging: udlfb: Add functions to expose sysfs metrics and controls")Reported-by [email protected]Assisted-by Claude:claude-opus-5Signed-off-by Xiang Mei <[email protected]>
Quoted 3 lines
--- drivers/video/fbdev/udlfb.c | 5 +++++ 1 file changed, 5 insertions(+)
Looks good.
Applied.
Thanks!
Helge