Skip to contentSkip to conversation

x86: don't assume all fb devices are PCI devices

PATCH Superseded Vitaly Kuznetsov 3 participants 4 messages Mar 11, 2016 – Mar 14, 2016 x86

v1 of 3 Superseded Latest is v3 →

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 x86: don't assume all fb devices are PCI devices

When booting Hyper-V Generation 2 guests KASAN reports the following out-of-bounds access:

BUG: KASAN: slab-out-of-bounds in fb_is_primary_device+0x58/0x70 at addr
  ffff880079cf0eb0
Read of size 8 by task swapper/0/1
...
 [<ffffffff81581308>] dump_stack+0x63/0x8b
 [<ffffffff812e1f99>] print_trailer+0xf9/0x150
 [<ffffffff812e7344>] object_err+0x34/0x40
 [<ffffffff812e9630>] kasan_report_error+0x230/0x550
 [<ffffffff812e9ee8>] kasan_report+0x58/0x60
 [<ffffffff812e4500>] ? ___slab_alloc+0x80/0x490
 [<ffffffff81878a28>] ? fb_is_primary_device+0x58/0x70
 [<ffffffff812e87cd>] __asan_load8+0x5d/0x70
 [<ffffffff81878a28>] fb_is_primary_device+0x58/0x70
 [<ffffffff8162357a>] register_framebuffer+0xda/0x5b0
 [<ffffffff816234a0>] ? remove_conflicting_framebuffers+0x50/0x50

...

The issue is caused by the to_pci_dev() call with no check that the given info->device is in fact a pci device and some fb devices (Hyper-V FB, EFI FB,...) are not. fb_is_primary_device() is not on any performance critical path, replace to_pci_dev() with raw scan.

---
 arch/x86/video/fbdev.c | 12 +++++++++---
 1 file changed, 9 insertions(+), 3 deletions(-)
arch/x86/video/fbdev.c +9 -3
index d5644bb..f309d6c 100644
@@ -14,12 +14,18 @@
 int fb_is_primary_device(struct fb_info *info)
 {
 	struct device *device = info->device;
-	struct pci_dev *pci_dev = NULL;
+	struct pci_dev *dev = NULL, *pci_dev = NULL;
 	struct pci_dev *default_device = vga_default_device();
 	struct resource *res = NULL;
 
-	if (device)
-		pci_dev = to_pci_dev(device);
+	/*
+	 * We're not sure info->device is a pci device, do full scan instead
+	 * of to_pci_dev().
+	 */
+	for_each_pci_dev(dev) {
+		if (&dev->dev == device)
+			pci_dev = dev;
+	}
 
 	if (!pci_dev)
 		return 0;
Signature · 1 line
2.5.0
#1 lore raw

So I'd love to have an ACK from Bjorn. (Full mail quoted below)

Thanks,

Ingo

Quoted 56 lines · Vitaly Kuznetsov

When booting Hyper-V Generation 2 guests KASAN reports the following out-of-bounds access:

BUG: KASAN: slab-out-of-bounds in fb_is_primary_device+0x58/0x70 at addr
  ffff880079cf0eb0
Read of size 8 by task swapper/0/1
...
 [<ffffffff81581308>] dump_stack+0x63/0x8b
 [<ffffffff812e1f99>] print_trailer+0xf9/0x150
 [<ffffffff812e7344>] object_err+0x34/0x40
 [<ffffffff812e9630>] kasan_report_error+0x230/0x550
 [<ffffffff812e9ee8>] kasan_report+0x58/0x60
 [<ffffffff812e4500>] ? ___slab_alloc+0x80/0x490
 [<ffffffff81878a28>] ? fb_is_primary_device+0x58/0x70
 [<ffffffff812e87cd>] __asan_load8+0x5d/0x70
 [<ffffffff81878a28>] fb_is_primary_device+0x58/0x70
 [<ffffffff8162357a>] register_framebuffer+0xda/0x5b0
 [<ffffffff816234a0>] ? remove_conflicting_framebuffers+0x50/0x50

...

The issue is caused by the to_pci_dev() call with no check that the given info->device is in fact a pci device and some fb devices (Hyper-V FB, EFI FB,...) are not. fb_is_primary_device() is not on any performance critical path, replace to_pci_dev() with raw scan.

---
 arch/x86/video/fbdev.c | 12 +++++++++---
 1 file changed, 9 insertions(+), 3 deletions(-)
arch/x86/video/fbdev.c +9 -3
index d5644bb..f309d6c 100644
@@ -14,12 +14,18 @@
 int fb_is_primary_device(struct fb_info *info)
 {
 	struct device *device = info->device;
-	struct pci_dev *pci_dev = NULL;
+	struct pci_dev *dev = NULL, *pci_dev = NULL;
 	struct pci_dev *default_device = vga_default_device();
 	struct resource *res = NULL;
 
-	if (device)
-		pci_dev = to_pci_dev(device);
+	/*
+	 * We're not sure info->device is a pci device, do full scan instead
+	 * of to_pci_dev().
+	 */
+	for_each_pci_dev(dev) {
+		if (&dev->dev == device)
+			pci_dev = dev;
+	}
 
 	if (!pci_dev)
 		return 0;
#2 lore raw
In reply to Vitaly Kuznetsov
Quoted 45 lines · Vitaly Kuznetsov

When booting Hyper-V Generation 2 guests KASAN reports the following out-of-bounds access:

BUG: KASAN: slab-out-of-bounds in fb_is_primary_device+0x58/0x70 at addr
  ffff880079cf0eb0
Read of size 8 by task swapper/0/1
...
 [<ffffffff81581308>] dump_stack+0x63/0x8b
 [<ffffffff812e1f99>] print_trailer+0xf9/0x150
 [<ffffffff812e7344>] object_err+0x34/0x40
 [<ffffffff812e9630>] kasan_report_error+0x230/0x550
 [<ffffffff812e9ee8>] kasan_report+0x58/0x60
 [<ffffffff812e4500>] ? ___slab_alloc+0x80/0x490
 [<ffffffff81878a28>] ? fb_is_primary_device+0x58/0x70
 [<ffffffff812e87cd>] __asan_load8+0x5d/0x70
 [<ffffffff81878a28>] fb_is_primary_device+0x58/0x70
 [<ffffffff8162357a>] register_framebuffer+0xda/0x5b0
 [<ffffffff816234a0>] ? remove_conflicting_framebuffers+0x50/0x50

...

The issue is caused by the to_pci_dev() call with no check that the given info->device is in fact a pci device and some fb devices (Hyper-V FB, EFI FB,...) are not. fb_is_primary_device() is not on any performance critical path, replace to_pci_dev() with raw scan.

---
 arch/x86/video/fbdev.c | 12 +++++++++---
 1 file changed, 9 insertions(+), 3 deletions(-)
arch/x86/video/fbdev.c +1 -3
index d5644bb..f309d6c 100644
@@ -14,12 +14,18 @@
 int fb_is_primary_device(struct fb_info *info)
 {
 	struct device *device = info->device;
-	struct pci_dev *pci_dev = NULL;
+	struct pci_dev *dev = NULL, *pci_dev = NULL;
 	struct pci_dev *default_device = vga_default_device();
 	struct resource *res = NULL;
 
-	if (device)
-		pci_dev = to_pci_dev(device);

arch/x86/video/fbdev.c line 30

Quoted 10 lines
arch/x86/video/fbdev.c +8 -0
+	/*
+	 * We're not sure info->device is a pci device, do full scan instead
+	 * of to_pci_dev().
+	 */
+	for_each_pci_dev(dev) {
+		if (&dev->dev == device)
+			pci_dev = dev;
+	}
 	if (!pci_dev)
 		return 0;

I think dev_is_pci() is a better solution:

  if (!device || !dev_is_pci(device))
    return 0;

  pci_dev = to_pci_dev(device);
  if (vga_default_device()) {
    if (vga_default_device() == pci_dev)
      return 1;
    return 0;
  }

  res = &pci_dev->resource[PCI_ROM_RESOURCE];
  if (res->flags & IORESOURCE_ROM_SHADOW)
    return 1;

return 0;

This says a non-PCI device cannot be the primary device. I don't know framebuffers, so I don't know if that's what you really want, but I guess it's what the original code says.

Bjorn

#3 lore raw
Quoted 59 lines · Bjorn Helgaas
Quoted 45 lines · Vitaly Kuznetsov

When booting Hyper-V Generation 2 guests KASAN reports the following out-of-bounds access:

BUG: KASAN: slab-out-of-bounds in fb_is_primary_device+0x58/0x70 at addr
  ffff880079cf0eb0
Read of size 8 by task swapper/0/1
...
 [<ffffffff81581308>] dump_stack+0x63/0x8b
 [<ffffffff812e1f99>] print_trailer+0xf9/0x150
 [<ffffffff812e7344>] object_err+0x34/0x40
 [<ffffffff812e9630>] kasan_report_error+0x230/0x550
 [<ffffffff812e9ee8>] kasan_report+0x58/0x60
 [<ffffffff812e4500>] ? ___slab_alloc+0x80/0x490
 [<ffffffff81878a28>] ? fb_is_primary_device+0x58/0x70
 [<ffffffff812e87cd>] __asan_load8+0x5d/0x70
 [<ffffffff81878a28>] fb_is_primary_device+0x58/0x70
 [<ffffffff8162357a>] register_framebuffer+0xda/0x5b0
 [<ffffffff816234a0>] ? remove_conflicting_framebuffers+0x50/0x50

...

The issue is caused by the to_pci_dev() call with no check that the given info->device is in fact a pci device and some fb devices (Hyper-V FB, EFI FB,...) are not. fb_is_primary_device() is not on any performance critical path, replace to_pci_dev() with raw scan.

---
 arch/x86/video/fbdev.c | 12 +++++++++---
 1 file changed, 9 insertions(+), 3 deletions(-)
arch/x86/video/fbdev.c +1 -3
index d5644bb..f309d6c 100644
@@ -14,12 +14,18 @@
 int fb_is_primary_device(struct fb_info *info)
 {
 	struct device *device = info->device;
-	struct pci_dev *pci_dev = NULL;
+	struct pci_dev *dev = NULL, *pci_dev = NULL;
 	struct pci_dev *default_device = vga_default_device();
 	struct resource *res = NULL;
 
-	if (device)
-		pci_dev = to_pci_dev(device);

arch/x86/video/fbdev.c line 30

Quoted 10 lines
arch/x86/video/fbdev.c +8 -0
+	/*
+	 * We're not sure info->device is a pci device, do full scan instead
+	 * of to_pci_dev().
+	 */
+	for_each_pci_dev(dev) {
+		if (&dev->dev == device)
+			pci_dev = dev;
+	}
 	if (!pci_dev)
 		return 0;

I think dev_is_pci() is a better solution:

Quoted 2 lines
  if (!device || !dev_is_pci(device))
    return 0;

Sure, will do v2.

Quoted 12 lines
  pci_dev = to_pci_dev(device);
  if (vga_default_device()) {
    if (vga_default_device() == pci_dev)
      return 1;
    return 0;
  }

  res = &pci_dev->resource[PCI_ROM_RESOURCE];
  if (res->flags & IORESOURCE_ROM_SHADOW)
    return 1;
Quoted 5 lines

return 0;

This says a non-PCI device cannot be the primary device. I don't know framebuffers, so I don't know if that's what you really want, but I guess it's what the original code says.

Yes, the original code is for pci devices only and I didn't plan to change that, my intention is to prevent out-of-bounds access.

Quoted 2 lines

Bjorn

Signature · 1 line
  Vitaly