Protect vga_switcheroo_client_fb_set() with console lock. Avoids OOB
access in fbcon_remap_all(). Without holding the console lock the call
races with switching outputs.

VGA switcheroo calls fbcon_remap_all() when switching clients. The fbcon
function uses struct fb_info.node, which is set by register_framebuffer().
As the fb-helper code currently sets up VGA switcheroo before registering
the framebuffer, the value of node is -1 and therefore not a legal value.
For example, fbcon uses the value within set_con2fb_map() [1] as an index
into an array.

Moving vga_switcheroo_client_fb_set() after register_framebuffer() can
result in VGA switching that does not switch fbcon correctly.

Therefore move vga_switcheroo_client_fb_set() under fbcon_fb_registered(),
which already holds the console lock. Fbdev calls fbcon_fb_registered()
from within register_framebuffer(). Serializes the helper with VGA
switcheroo's call to fbcon_remap_all().

Although vga_switcheroo_client_fb_set() takes an instance of struct fb_info
as parameter, it really only needs the contained fbcon state. Moving the
call to fbcon initialization is therefore cleaner than before. Only amdgpu,
i915, nouveau and radeon support vga_switcheroo. For all other drivers,
this change does nothing.

Signed-off-by: Thomas Zimmermann <[email protected]>
Link: 
https://elixir.bootlin.com/linux/v6.17/source/drivers/video/fbdev/core/fbcon.c#L2942
 # [1]
---
 drivers/gpu/drm/drm_fb_helper.c  | 14 --------------
 drivers/video/fbdev/core/fbcon.c |  9 +++++++++
 2 files changed, 9 insertions(+), 14 deletions(-)

diff --git a/drivers/gpu/drm/drm_fb_helper.c b/drivers/gpu/drm/drm_fb_helper.c
index 53e9dc0543de..c0343ec16a57 100644
--- a/drivers/gpu/drm/drm_fb_helper.c
+++ b/drivers/gpu/drm/drm_fb_helper.c
@@ -31,9 +31,7 @@
 
 #include <linux/console.h>
 #include <linux/export.h>
-#include <linux/pci.h>
 #include <linux/sysrq.h>
-#include <linux/vga_switcheroo.h>
 
 #include <drm/drm_atomic.h>
 #include <drm/drm_drv.h>
@@ -570,11 +568,6 @@ EXPORT_SYMBOL(drm_fb_helper_release_info);
  */
 void drm_fb_helper_unregister_info(struct drm_fb_helper *fb_helper)
 {
-       struct fb_info *info = fb_helper->info;
-       struct device *dev = info->device;
-
-       if (dev_is_pci(dev))
-               vga_switcheroo_client_fb_set(to_pci_dev(dev), NULL);
        unregister_framebuffer(fb_helper->info);
 }
 EXPORT_SYMBOL(drm_fb_helper_unregister_info);
@@ -1614,7 +1607,6 @@ static int drm_fb_helper_single_fb_probe(struct 
drm_fb_helper *fb_helper)
        struct drm_client_dev *client = &fb_helper->client;
        struct drm_device *dev = fb_helper->dev;
        struct drm_fb_helper_surface_size sizes;
-       struct fb_info *info;
        int ret;
 
        if (drm_WARN_ON(dev, !dev->driver->fbdev_probe))
@@ -1635,12 +1627,6 @@ static int drm_fb_helper_single_fb_probe(struct 
drm_fb_helper *fb_helper)
 
        strcpy(fb_helper->fb->comm, "[fbcon]");
 
-       info = fb_helper->info;
-
-       /* Set the fb info for vgaswitcheroo clients. Does nothing otherwise. */
-       if (dev_is_pci(info->device))
-               vga_switcheroo_client_fb_set(to_pci_dev(info->device), info);
-
        return 0;
 }
 
diff --git a/drivers/video/fbdev/core/fbcon.c b/drivers/video/fbdev/core/fbcon.c
index 7f35ad66b462..863944530c8e 100644
--- a/drivers/video/fbdev/core/fbcon.c
+++ b/drivers/video/fbdev/core/fbcon.c
@@ -66,6 +66,7 @@
 #include <linux/string.h>
 #include <linux/kd.h>
 #include <linux/panic.h>
+#include <linux/pci.h>
 #include <linux/printk.h>
 #include <linux/slab.h>
 #include <linux/fb.h>
@@ -78,6 +79,7 @@
 #include <linux/interrupt.h>
 #include <linux/crc32.h> /* For counting font checksums */
 #include <linux/uaccess.h>
+#include <linux/vga_switcheroo.h>
 #include <asm/irq.h>
 
 #include "fbcon.h"
@@ -2894,6 +2896,9 @@ void fbcon_fb_unregistered(struct fb_info *info)
 
        console_lock();
 
+       if (info->device && dev_is_pci(info->device))
+               vga_switcheroo_client_fb_set(to_pci_dev(info->device), NULL);
+
        fbcon_registered_fb[info->node] = NULL;
        fbcon_num_registered_fb--;
 
@@ -3027,6 +3032,10 @@ static int do_fb_registered(struct fb_info *info)
                }
        }
 
+       /* Set the fb info for vga_switcheroo clients. Does nothing otherwise. 
*/
+       if (info->device && dev_is_pci(info->device))
+               vga_switcheroo_client_fb_set(to_pci_dev(info->device), info);
+
        return ret;
 }
 
-- 
2.51.1

Reply via email to