The Linux Kernel Mailing List
 help / color / mirror / Atom feed
* [PATCH 0/2] fbdev: Prevent null-ptr-deref in fb_videomode_to_var
@ 2025-04-28 15:34 Murad Masimov
  2025-04-28 15:34 ` [PATCH 1/2] fbdev: Fix do_register_framebuffer to prevent " Murad Masimov
                   ` (2 more replies)
  0 siblings, 3 replies; 4+ messages in thread
From: Murad Masimov @ 2025-04-28 15:34 UTC (permalink / raw)
  To: Simona Vetter
  Cc: Helge Deller, Murad Masimov, Thomas Weißschuh, linux-fbdev,
	dri-devel, linux-kernel, lvc-project

These patches fix the bug that leads to a null-ptr-deref if
fb_videomode_to_var() fails to allocate memory. This bug is present in
do_register_framebuffer() and fb_ser_var().

Found by Linux Verification Center (linuxtesting.org) with Syzkaller.

Murad Masimov (2):
  fbdev: Fix do_register_framebuffer to prevent null-ptr-deref in
    fb_videomode_to_var
  fbdev: Fix fb_ser_var to prevent null-ptr-deref in fb_videomode_to_var

 drivers/video/fbdev/core/fbmem.c | 22 ++++++++++++++--------
 1 file changed, 14 insertions(+), 8 deletions(-)

--
2.39.2


^ permalink raw reply	[flat|nested] 4+ messages in thread

* [PATCH 1/2] fbdev: Fix do_register_framebuffer to prevent null-ptr-deref in fb_videomode_to_var
  2025-04-28 15:34 [PATCH 0/2] fbdev: Prevent null-ptr-deref in fb_videomode_to_var Murad Masimov
@ 2025-04-28 15:34 ` Murad Masimov
  2025-04-28 15:34 ` [PATCH 2/2] fbdev: Fix fb_ser_var " Murad Masimov
  2025-05-29  8:19 ` [PATCH 0/2] fbdev: Prevent " Helge Deller
  2 siblings, 0 replies; 4+ messages in thread
From: Murad Masimov @ 2025-04-28 15:34 UTC (permalink / raw)
  To: Simona Vetter
  Cc: Helge Deller, Murad Masimov, Thomas Weißschuh, linux-fbdev,
	dri-devel, linux-kernel, lvc-project, stable

If fb_add_videomode() in do_register_framebuffer() fails to allocate
memory for fb_videomode, it will later lead to a null-ptr dereference in
fb_videomode_to_var(), as the fb_info is registered while not having the
mode in modelist that is expected to be there, i.e. the one that is
described in fb_info->var.

================================================================
general protection fault, probably for non-canonical address 0xdffffc0000000001: 0000 [#1] PREEMPT SMP KASAN NOPTI
KASAN: null-ptr-deref in range [0x0000000000000008-0x000000000000000f]
CPU: 1 PID: 30371 Comm: syz-executor.1 Not tainted 5.10.226-syzkaller #0
Hardware name: QEMU Standard PC (i440FX + PIIX, 1996), BIOS 1.12.0-1 04/01/2014
RIP: 0010:fb_videomode_to_var+0x24/0x610 drivers/video/fbdev/core/modedb.c:901
Call Trace:
 display_to_var+0x3a/0x7c0 drivers/video/fbdev/core/fbcon.c:929
 fbcon_resize+0x3e2/0x8f0 drivers/video/fbdev/core/fbcon.c:2071
 resize_screen drivers/tty/vt/vt.c:1176 [inline]
 vc_do_resize+0x53a/0x1170 drivers/tty/vt/vt.c:1263
 fbcon_modechanged+0x3ac/0x6e0 drivers/video/fbdev/core/fbcon.c:2720
 fbcon_update_vcs+0x43/0x60 drivers/video/fbdev/core/fbcon.c:2776
 do_fb_ioctl+0x6d2/0x740 drivers/video/fbdev/core/fbmem.c:1128
 fb_ioctl+0xe7/0x150 drivers/video/fbdev/core/fbmem.c:1203
 vfs_ioctl fs/ioctl.c:48 [inline]
 __do_sys_ioctl fs/ioctl.c:753 [inline]
 __se_sys_ioctl fs/ioctl.c:739 [inline]
 __x64_sys_ioctl+0x19a/0x210 fs/ioctl.c:739
 do_syscall_64+0x33/0x40 arch/x86/entry/common.c:46
 entry_SYSCALL_64_after_hwframe+0x67/0xd1
================================================================

Even though fbcon_init() checks beforehand if fb_match_mode() in
var_to_display() fails, it can not prevent the panic because fbcon_init()
does not return error code. Considering this and the comment in the code
about fb_match_mode() returning NULL - "This should not happen" - it is
better to prevent registering the fb_info if its mode was not set
successfully. Also move fb_add_videomode() closer to the beginning of
do_register_framebuffer() to avoid having to do the cleanup on fail.

Found by Linux Verification Center (linuxtesting.org) with Syzkaller.

Fixes: 1da177e4c3f4 ("Linux-2.6.12-rc2")
Cc: stable@vger.kernel.org
Signed-off-by: Murad Masimov <m.masimov@mt-integration.ru>
---
 drivers/video/fbdev/core/fbmem.c | 18 +++++++++++-------
 1 file changed, 11 insertions(+), 7 deletions(-)

diff --git a/drivers/video/fbdev/core/fbmem.c b/drivers/video/fbdev/core/fbmem.c
index 3c568cff2913..e1557d80768f 100644
--- a/drivers/video/fbdev/core/fbmem.c
+++ b/drivers/video/fbdev/core/fbmem.c
@@ -388,7 +388,7 @@ static int fb_check_foreignness(struct fb_info *fi)

 static int do_register_framebuffer(struct fb_info *fb_info)
 {
-	int i;
+	int i, err = 0;
 	struct fb_videomode mode;

 	if (fb_check_foreignness(fb_info))
@@ -397,10 +397,18 @@ static int do_register_framebuffer(struct fb_info *fb_info)
 	if (num_registered_fb == FB_MAX)
 		return -ENXIO;

-	num_registered_fb++;
 	for (i = 0 ; i < FB_MAX; i++)
 		if (!registered_fb[i])
 			break;
+
+	if (!fb_info->modelist.prev || !fb_info->modelist.next)
+		INIT_LIST_HEAD(&fb_info->modelist);
+
+	fb_var_to_videomode(&mode, &fb_info->var);
+	err = fb_add_videomode(&mode, &fb_info->modelist);
+	if (err < 0)
+		return err;
+
 	fb_info->node = i;
 	refcount_set(&fb_info->count, 1);
 	mutex_init(&fb_info->lock);
@@ -426,16 +434,12 @@ static int do_register_framebuffer(struct fb_info *fb_info)
 	if (bitmap_empty(fb_info->pixmap.blit_y, FB_MAX_BLIT_HEIGHT))
 		bitmap_fill(fb_info->pixmap.blit_y, FB_MAX_BLIT_HEIGHT);

-	if (!fb_info->modelist.prev || !fb_info->modelist.next)
-		INIT_LIST_HEAD(&fb_info->modelist);
-
 	if (fb_info->skip_vt_switch)
 		pm_vt_switch_required(fb_info->device, false);
 	else
 		pm_vt_switch_required(fb_info->device, true);

-	fb_var_to_videomode(&mode, &fb_info->var);
-	fb_add_videomode(&mode, &fb_info->modelist);
+	num_registered_fb++;
 	registered_fb[i] = fb_info;

 #ifdef CONFIG_GUMSTIX_AM200EPD
--
2.39.2


^ permalink raw reply related	[flat|nested] 4+ messages in thread

* [PATCH 2/2] fbdev: Fix fb_ser_var to prevent null-ptr-deref in fb_videomode_to_var
  2025-04-28 15:34 [PATCH 0/2] fbdev: Prevent null-ptr-deref in fb_videomode_to_var Murad Masimov
  2025-04-28 15:34 ` [PATCH 1/2] fbdev: Fix do_register_framebuffer to prevent " Murad Masimov
@ 2025-04-28 15:34 ` Murad Masimov
  2025-05-29  8:19 ` [PATCH 0/2] fbdev: Prevent " Helge Deller
  2 siblings, 0 replies; 4+ messages in thread
From: Murad Masimov @ 2025-04-28 15:34 UTC (permalink / raw)
  To: Simona Vetter
  Cc: Helge Deller, Murad Masimov, Thomas Weißschuh, linux-fbdev,
	dri-devel, linux-kernel, lvc-project, stable

If fb_add_videomode() in fb_set_var() fails to allocate memory for
fb_videomode, later it may lead to a null-ptr dereference in
fb_videomode_to_var(), as the fb_info is registered while not having the
mode in modelist that is expected to be there, i.e. the one that is
described in fb_info->var.

================================================================
general protection fault, probably for non-canonical address 0xdffffc0000000001: 0000 [#1] PREEMPT SMP KASAN NOPTI
KASAN: null-ptr-deref in range [0x0000000000000008-0x000000000000000f]
CPU: 1 PID: 30371 Comm: syz-executor.1 Not tainted 5.10.226-syzkaller #0
Hardware name: QEMU Standard PC (i440FX + PIIX, 1996), BIOS 1.12.0-1 04/01/2014
RIP: 0010:fb_videomode_to_var+0x24/0x610 drivers/video/fbdev/core/modedb.c:901
Call Trace:
 display_to_var+0x3a/0x7c0 drivers/video/fbdev/core/fbcon.c:929
 fbcon_resize+0x3e2/0x8f0 drivers/video/fbdev/core/fbcon.c:2071
 resize_screen drivers/tty/vt/vt.c:1176 [inline]
 vc_do_resize+0x53a/0x1170 drivers/tty/vt/vt.c:1263
 fbcon_modechanged+0x3ac/0x6e0 drivers/video/fbdev/core/fbcon.c:2720
 fbcon_update_vcs+0x43/0x60 drivers/video/fbdev/core/fbcon.c:2776
 do_fb_ioctl+0x6d2/0x740 drivers/video/fbdev/core/fbmem.c:1128
 fb_ioctl+0xe7/0x150 drivers/video/fbdev/core/fbmem.c:1203
 vfs_ioctl fs/ioctl.c:48 [inline]
 __do_sys_ioctl fs/ioctl.c:753 [inline]
 __se_sys_ioctl fs/ioctl.c:739 [inline]
 __x64_sys_ioctl+0x19a/0x210 fs/ioctl.c:739
 do_syscall_64+0x33/0x40 arch/x86/entry/common.c:46
 entry_SYSCALL_64_after_hwframe+0x67/0xd1
================================================================

The reason is that fb_info->var is being modified in fb_set_var(), and
then fb_videomode_to_var() is called. If it fails to add the mode to
fb_info->modelist, fb_set_var() returns error, but does not restore the
old value of fb_info->var. Restore fb_info->var on fail the same way it
is done earlier in the function.

Found by Linux Verification Center (linuxtesting.org) with Syzkaller.

Fixes: 1da177e4c3f4 ("Linux-2.6.12-rc2")
Cc: stable@vger.kernel.org
Signed-off-by: Murad Masimov <m.masimov@mt-integration.ru>
---
 drivers/video/fbdev/core/fbmem.c | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)

diff --git a/drivers/video/fbdev/core/fbmem.c b/drivers/video/fbdev/core/fbmem.c
index e1557d80768f..eca2498f2436 100644
--- a/drivers/video/fbdev/core/fbmem.c
+++ b/drivers/video/fbdev/core/fbmem.c
@@ -328,8 +328,10 @@ fb_set_var(struct fb_info *info, struct fb_var_screeninfo *var)
 	    !list_empty(&info->modelist))
 		ret = fb_add_videomode(&mode, &info->modelist);

-	if (ret)
+	if (ret) {
+		info->var = old_var;
 		return ret;
+	}

 	event.info = info;
 	event.data = &mode;
--
2.39.2


^ permalink raw reply related	[flat|nested] 4+ messages in thread

* Re: [PATCH 0/2] fbdev: Prevent null-ptr-deref in fb_videomode_to_var
  2025-04-28 15:34 [PATCH 0/2] fbdev: Prevent null-ptr-deref in fb_videomode_to_var Murad Masimov
  2025-04-28 15:34 ` [PATCH 1/2] fbdev: Fix do_register_framebuffer to prevent " Murad Masimov
  2025-04-28 15:34 ` [PATCH 2/2] fbdev: Fix fb_ser_var " Murad Masimov
@ 2025-05-29  8:19 ` Helge Deller
  2 siblings, 0 replies; 4+ messages in thread
From: Helge Deller @ 2025-05-29  8:19 UTC (permalink / raw)
  To: Murad Masimov; +Cc: linux-fbdev, dri-devel, linux-kernel, lvc-project

On 4/28/25 17:34, Murad Masimov wrote:
> These patches fix the bug that leads to a null-ptr-deref if
> fb_videomode_to_var() fails to allocate memory. This bug is present in
> do_register_framebuffer() and fb_ser_var().
> 
> Found by Linux Verification Center (linuxtesting.org) with Syzkaller.
> 
> Murad Masimov (2):
>    fbdev: Fix do_register_framebuffer to prevent null-ptr-deref in
>      fb_videomode_to_var
>    fbdev: Fix fb_ser_var to prevent null-ptr-deref in fb_videomode_to_var

I've added both patches to fbdev git tree (after fixing two small typos in commit message).

Thanks!
Helge



  
>   drivers/video/fbdev/core/fbmem.c | 22 ++++++++++++++--------
>   1 file changed, 14 insertions(+), 8 deletions(-)
> 
> --
> 2.39.2
> 


^ permalink raw reply	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2025-05-29  8:19 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2025-04-28 15:34 [PATCH 0/2] fbdev: Prevent null-ptr-deref in fb_videomode_to_var Murad Masimov
2025-04-28 15:34 ` [PATCH 1/2] fbdev: Fix do_register_framebuffer to prevent " Murad Masimov
2025-04-28 15:34 ` [PATCH 2/2] fbdev: Fix fb_ser_var " Murad Masimov
2025-05-29  8:19 ` [PATCH 0/2] fbdev: Prevent " Helge Deller

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox