Skip to contentSkip to conversation

sdl2: Support all virtio-gpu formats

PATCH In review Max Reitz 2 participants 8 messages Oct 8, 2018 – Oct 12, 2018

lore
Share & tools
lore

Max Reitz

author
#0 lore raw

PATCH 1/1 sdl2: Support all virtio-gpu formats

There are some 2D resource formats that can be used through virtio-gpu, but which are not supported by SDL2 when used for a scanout; these are all alpha-channel formats and also XBGR (RGBX in non-BE pixman).

Add these formats in the switch converting pixman to SDL format constants so a guest cannot crash the VM by triggering the g_assert_not_reached() with an unsupported format.

---
 ui/sdl2-2d.c | 9 +++++++++
 1 file changed, 9 insertions(+)
ui/sdl2-2d.c +9 -0
index 85484407be..c9df72636a 100644
@@ -101,15 +101,24 @@ void sdl2_2d_switch(DisplayChangeListener *dcl,
     case PIXMAN_r5g6b5:
         format = SDL_PIXELFORMAT_RGB565;
         break;
+    case PIXMAN_a8r8g8b8:
     case PIXMAN_x8r8g8b8:
         format = SDL_PIXELFORMAT_ARGB8888;
         break;
+    case PIXMAN_a8b8g8r8:
+    case PIXMAN_x8b8g8r8:
+        format = SDL_PIXELFORMAT_ABGR8888;
+        break;
+    case PIXMAN_r8g8b8a8:
     case PIXMAN_r8g8b8x8:
         format = SDL_PIXELFORMAT_RGBA8888;
         break;
     case PIXMAN_b8g8r8x8:
         format = SDL_PIXELFORMAT_BGRX8888;
         break;
+    case PIXMAN_b8g8r8a8:
+        format = SDL_PIXELFORMAT_BGRA8888;
+        break;
     default:
         g_assert_not_reached();
     }
Signature · 1 line
2.17.1
author
#1 lore raw
Quoted 3 lines · Max Reitz

There are some 2D resource formats that can be used through virtio-gpu, but which are not supported by SDL2 when used for a scanout; these are all alpha-channel formats and also XBGR (RGBX in non-BE pixman).

Oops, it's the other way round. The virtio-gpu format is RGBX, and the non-BE pixman (and SDL) constant is XBGR.

Max

Quoted 39 lines

Add these formats in the switch converting pixman to SDL format constants so a guest cannot crash the VM by triggering the g_assert_not_reached() with an unsupported format.

---
 ui/sdl2-2d.c | 9 +++++++++
 1 file changed, 9 insertions(+)
ui/sdl2-2d.c +9 -0
index 85484407be..c9df72636a 100644
@@ -101,15 +101,24 @@ void sdl2_2d_switch(DisplayChangeListener *dcl,
     case PIXMAN_r5g6b5:
         format = SDL_PIXELFORMAT_RGB565;
         break;
+    case PIXMAN_a8r8g8b8:
     case PIXMAN_x8r8g8b8:
         format = SDL_PIXELFORMAT_ARGB8888;
         break;
+    case PIXMAN_a8b8g8r8:
+    case PIXMAN_x8b8g8r8:
+        format = SDL_PIXELFORMAT_ABGR8888;
+        break;
+    case PIXMAN_r8g8b8a8:
     case PIXMAN_r8g8b8x8:
         format = SDL_PIXELFORMAT_RGBA8888;
         break;
     case PIXMAN_b8g8r8x8:
         format = SDL_PIXELFORMAT_BGRX8888;
         break;
+    case PIXMAN_b8g8r8a8:
+        format = SDL_PIXELFORMAT_BGRA8888;
+        break;
     default:
         g_assert_not_reached();
     }
#2 lore raw
In reply to Max Reitz
Quoted 1 line · Max Reitz

There are some 2D resource formats that can be used through virtio-gpu,

Ahem, not really. XRGB is the only one which works in practice, and virtio-gpu kms driver will stop advertising anything else soon (patches should land upstream with the next merge window).

Quoted 3 lines

Add these formats in the switch converting pixman to SDL format constants so a guest cannot crash the VM by triggering the g_assert_not_reached() with an unsupported format.

Do you have a reproducer for that?

There is sdl2_2d_check_format() which reports the supported formats. If we hit sdl2_2d_switch() with a format not whitelisted by sdl2_2d_check_format() we have a bug somewhere in qemu ...

cheers,
Gerd

author
#3 lore raw
Quoted 6 lines · Gerd Hoffmann
Quoted 1 line · Max Reitz

There are some 2D resource formats that can be used through virtio-gpu,

Ahem, not really. XRGB is the only one which works in practice, and virtio-gpu kms driver will stop advertising anything else soon (patches should land upstream with the next merge window).

OK, if virtio-gpu didn't support anything else, that'd be a fix, too. But it sounds like you're talking about the Linux driver, I'm not.

This is not about Linux applications being able to abuse the Linux driver to crash the VM, this is about malicious drivers (not necessarily Linux drivers).

Quoted 5 lines
Quoted 3 lines

Add these formats in the switch converting pixman to SDL format constants so a guest cannot crash the VM by triggering the g_assert_not_reached() with an unsupported format.

Do you have a reproducer for that?

I have attached two RISC-V kernels, one (kernel-rgbx) setting VIRTIO_GPU_FORMAT_R8G8B8X8_UNORM, the other (kernel-bgra) setting VIRTIO_GPU_FORMAT_B8G8R8A8_UNORM. Both crash qemu:

$ $QEMU/build/riscv64-softmmu/qemu-system-riscv64 -kernel kernel-rgbx \
    -serial stdio -M virt -device virtio-gpu-device
[platform-virt] Virt platform detected
[virtio-gpu] Found device @0x10008000
[virtio-gpu] Scanout 0: 0x0:1024x768
**
ERROR:$QEMU/ui/sdl2-2d.c:114:sdl2_2d_switch: code should not be reached
[1]    7151 abort (core dumped)

So this is not about a misbehaving Linux driver but about an own driver.
Of course, if you can insert kernel code, there's noone stopping you
from hitting that assertion with Linux, too.

Quoted 3 lines

There is sdl2_2d_check_format() which reports the supported formats. If we hit sdl2_2d_switch() with a format not whitelisted by sdl2_2d_check_format() we have a bug somewhere in qemu ...

I suppose the other solution would be for virtio_gpu_set_scanout() to check whether the resource's format can actually be used for that display. Or in virtio_gpu_resource_create_2d(), I don't know whether it's possible to use resources in other formats at all.

Max

author
#4 lore raw
Quoted 19 lines · Max Reitz
Quoted 6 lines · Gerd Hoffmann
Quoted 1 line · Max Reitz

There are some 2D resource formats that can be used through virtio-gpu,

Ahem, not really. XRGB is the only one which works in practice, and virtio-gpu kms driver will stop advertising anything else soon (patches should land upstream with the next merge window).

OK, if virtio-gpu didn't support anything else, that'd be a fix, too. But it sounds like you're talking about the Linux driver, I'm not.

This is not about Linux applications being able to abuse the Linux driver to crash the VM, this is about malicious drivers (not necessarily Linux drivers).

Quoted 4 lines
Quoted 3 lines

Add these formats in the switch converting pixman to SDL format constants so a guest cannot crash the VM by triggering the g_assert_not_reached() with an unsupported format.

Quoted 5 lines
Quoted 1 line

Do you have a reproducer for that?

I have attached two RISC-V kernels, one (kernel-rgbx) setting VIRTIO_GPU_FORMAT_R8G8B8X8_UNORM, the other (kernel-bgra) setting VIRTIO_GPU_FORMAT_B8G8R8A8_UNORM. Both crash qemu:

Of course I hadn't.

Quoted 24 lines
$ $QEMU/build/riscv64-softmmu/qemu-system-riscv64 -kernel kernel-rgbx \
    -serial stdio -M virt -device virtio-gpu-device
[platform-virt] Virt platform detected
[virtio-gpu] Found device @0x10008000
[virtio-gpu] Scanout 0: 0x0:1024x768
**
ERROR:$QEMU/ui/sdl2-2d.c:114:sdl2_2d_switch: code should not be reached
[1]    7151 abort (core dumped)

So this is not about a misbehaving Linux driver but about an own driver.
Of course, if you can insert kernel code, there's noone stopping you
from hitting that assertion with Linux, too.

Quoted 3 lines

There is sdl2_2d_check_format() which reports the supported formats. If we hit sdl2_2d_switch() with a format not whitelisted by sdl2_2d_check_format() we have a bug somewhere in qemu ...

I suppose the other solution would be for virtio_gpu_set_scanout() to check whether the resource's format can actually be used for that display. Or in virtio_gpu_resource_create_2d(), I don't know whether it's possible to use resources in other formats at all.

Max

#5 lore raw
In reply to Max Reitz
Quoted 25 lines · Max Reitz
Quoted 6 lines · Gerd Hoffmann
Quoted 1 line · Max Reitz

There are some 2D resource formats that can be used through virtio-gpu,

Ahem, not really. XRGB is the only one which works in practice, and virtio-gpu kms driver will stop advertising anything else soon (patches should land upstream with the next merge window).

OK, if virtio-gpu didn't support anything else, that'd be a fix, too. But it sounds like you're talking about the Linux driver, I'm not.

This is not about Linux applications being able to abuse the Linux driver to crash the VM, this is about malicious drivers (not necessarily Linux drivers).

Quoted 5 lines
Quoted 3 lines

Add these formats in the switch converting pixman to SDL format constants so a guest cannot crash the VM by triggering the g_assert_not_reached() with an unsupported format.

Do you have a reproducer for that?

I have attached two RISC-V kernels, one (kernel-rgbx) setting VIRTIO_GPU_FORMAT_R8G8B8X8_UNORM, the other (kernel-bgra) setting VIRTIO_GPU_FORMAT_B8G8R8A8_UNORM. Both crash qemu:

Quoted 8 lines
$ $QEMU/build/riscv64-softmmu/qemu-system-riscv64 -kernel kernel-rgbx \
    -serial stdio -M virt -device virtio-gpu-device
[platform-virt] Virt platform detected
[virtio-gpu] Found device @0x10008000
[virtio-gpu] Scanout 0: 0x0:1024x768
**
ERROR:$QEMU/ui/sdl2-2d.c:114:sdl2_2d_switch: code should not be reached
[1]    7151 abort (core dumped)

Ok, I see. If sdl2 can support those formats it makes sense to support them, but they must be added to both sdl2_2d_check_format() and sdl2_2d_switch() then.

Quoted 7 lines
Quoted 3 lines

There is sdl2_2d_check_format() which reports the supported formats. If we hit sdl2_2d_switch() with a format not whitelisted by sdl2_2d_check_format() we have a bug somewhere in qemu ...

I suppose the other solution would be for virtio_gpu_set_scanout() to check whether the resource's format can actually be used for that display.

Yes, that must be added (independent from the sdl2 patch).

Quoted 2 lines

Or in virtio_gpu_resource_create_2d(), I don't know whether it's possible to use resources in other formats at all.

Not directly, they must be converted (using pixman) then.

cheers,
Gerd

#6 lore raw
In reply to Max Reitz
Quoted 7 lines · Max Reitz

There are some 2D resource formats that can be used through virtio-gpu, but which are not supported by SDL2 when used for a scanout; these are all alpha-channel formats and also XBGR (RGBX in non-BE pixman).

Add these formats in the switch converting pixman to SDL format constants so a guest cannot crash the VM by triggering the g_assert_not_reached() with an unsupported format.

fixed (sdl2_2d_check_format update) & queued.

cheers,
Gerd

author
#7 lore raw
Quoted 10 lines · Gerd Hoffmann
Quoted 7 lines · Max Reitz

There are some 2D resource formats that can be used through virtio-gpu, but which are not supported by SDL2 when used for a scanout; these are all alpha-channel formats and also XBGR (RGBX in non-BE pixman).

Add these formats in the switch converting pixman to SDL format constants so a guest cannot crash the VM by triggering the g_assert_not_reached() with an unsupported format.

fixed (sdl2_2d_check_format update) & queued.

Thanks!

Max