[PATCH] ui: fix screendump PPM corruption when the width is not 4-aligned

Chris Bainbridge posted 1 patch 6 days, 12 hours ago
ui/ui-qmp-cmds.c | 3 ++-
1 file changed, 2 insertions(+), 1 deletion(-)
[PATCH] ui: fix screendump PPM corruption when the width is not 4-aligned
Posted by Chris Bainbridge 6 days, 12 hours ago
ppm_save() writes pixman_image_get_stride(linebuf) bytes per row, but
the P6 header declares the image as width x height, i.e. width * 3
bytes per row for the PIXMAN_BE_r8g8b8 line buffer.

pixman rounds scanlines up to 32-bit boundaries, so the line buffer's
stride is 4 * DIV_ROUND_UP(width * 3, 4), which is larger than
width * 3 whenever (width * 3) % 4 != 0.  Every row then carries up to
3 extra padding bytes that PPM consumers do not expect: readers which
trust the header see progressively misaligned pixels (a diagonally
sheared image, with the row padding interpreted as pixel data), and
the file ends up height * (stride - width * 3) bytes larger than the
header describes.

All standard display widths are multiples of 4 (and so is their
width * 3), which has kept the artifact invisible for years; any
non-aligned width triggers it.  Example: after a VNC client requests a
desktop resize to a non-aligned size (SetDesktopSize), or with
-device virtio-vga,edid=on,xres=1050, the screendump of a healthy
guest is diagonally sheared at the correct resolution, while every
other display path (e.g. VNC) shows the correct image.

Write exactly width * 3 bytes per row, matching the header.  The PNG
path is unaffected: it uses the same padded r8g8b8 line buffer, but
png_write_row() only consumes the width * 3 bytes described by the
IHDR.

Verification: on QEMU 10.2.1 with virtio-vga and a 1050-pixel-wide
scanout, stock screendump emits a sheared PPM whose size exceeds the
header by height * (stride - width * 3); with this patch the PPM is
exactly header + width * 3 * height bytes and renders identically to
the PNG screendump.  scripts/checkpatch.pl reports 0 errors, 0
warnings.

Fixes: b12f32c408ef ("pixman: switch screendump function.")
Cc: qemu-stable@nongnu.org
Signed-off-by: Chris Bainbridge <chris.bainbridge@canonical.com>
---
 ui/ui-qmp-cmds.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)

diff --git a/ui/ui-qmp-cmds.c b/ui/ui-qmp-cmds.c
index 753cc2bf52..64027da2c0 100644
--- a/ui/ui-qmp-cmds.c
+++ b/ui/ui-qmp-cmds.c
@@ -309,9 +309,10 @@ static bool ppm_save(int fd, pixman_image_t *image, Error **errp)
     linebuf = qemu_pixman_linebuf_create(PIXMAN_BE_r8g8b8, width);
     for (y = 0; y < height; y++) {
         qemu_pixman_linebuf_fill(linebuf, image, width, 0, y);
+        /* Write width * 3 bytes, not the 4-byte-aligned linebuf stride */
         if (qio_channel_write_all(QIO_CHANNEL(ioc),
                                   (char *)pixman_image_get_data(linebuf),
-                                  pixman_image_get_stride(linebuf), errp) < 0) {
+                                  width * 3, errp) < 0) {
             return false;
         }
     }
-- 
2.53.0
Re: [PATCH] ui: fix screendump PPM corruption when the width is not 4-aligned
Posted by Marc-André Lureau 6 days, 9 hours ago
On Mon, Nov 16, 2026 at 8:11 PM Chris Bainbridge
<chris.bainbridge@canonical.com> wrote:
>
> ppm_save() writes pixman_image_get_stride(linebuf) bytes per row, but
> the P6 header declares the image as width x height, i.e. width * 3
> bytes per row for the PIXMAN_BE_r8g8b8 line buffer.
>
> pixman rounds scanlines up to 32-bit boundaries, so the line buffer's
> stride is 4 * DIV_ROUND_UP(width * 3, 4), which is larger than
> width * 3 whenever (width * 3) % 4 != 0.  Every row then carries up to
> 3 extra padding bytes that PPM consumers do not expect: readers which
> trust the header see progressively misaligned pixels (a diagonally
> sheared image, with the row padding interpreted as pixel data), and
> the file ends up height * (stride - width * 3) bytes larger than the
> header describes.
>
> All standard display widths are multiples of 4 (and so is their
> width * 3), which has kept the artifact invisible for years; any
> non-aligned width triggers it.  Example: after a VNC client requests a
> desktop resize to a non-aligned size (SetDesktopSize), or with
> -device virtio-vga,edid=on,xres=1050, the screendump of a healthy
> guest is diagonally sheared at the correct resolution, while every
> other display path (e.g. VNC) shows the correct image.
>
> Write exactly width * 3 bytes per row, matching the header.  The PNG
> path is unaffected: it uses the same padded r8g8b8 line buffer, but
> png_write_row() only consumes the width * 3 bytes described by the
> IHDR.
>
> Verification: on QEMU 10.2.1 with virtio-vga and a 1050-pixel-wide
> scanout, stock screendump emits a sheared PPM whose size exceeds the
> header by height * (stride - width * 3); with this patch the PPM is
> exactly header + width * 3 * height bytes and renders identically to
> the PNG screendump.  scripts/checkpatch.pl reports 0 errors, 0
> warnings.
>
> Fixes: b12f32c408ef ("pixman: switch screendump function.")
> Cc: qemu-stable@nongnu.org
> Signed-off-by: Chris Bainbridge <chris.bainbridge@canonical.com>

Reviewed-by: Marc-André Lureau <marcandre.lureau@redhat.com>

> ---
>  ui/ui-qmp-cmds.c | 3 ++-
>  1 file changed, 2 insertions(+), 1 deletion(-)
>
> diff --git a/ui/ui-qmp-cmds.c b/ui/ui-qmp-cmds.c
> index 753cc2bf52..64027da2c0 100644
> --- a/ui/ui-qmp-cmds.c
> +++ b/ui/ui-qmp-cmds.c
> @@ -309,9 +309,10 @@ static bool ppm_save(int fd, pixman_image_t *image, Error **errp)
>      linebuf = qemu_pixman_linebuf_create(PIXMAN_BE_r8g8b8, width);
>      for (y = 0; y < height; y++) {
>          qemu_pixman_linebuf_fill(linebuf, image, width, 0, y);
> +        /* Write width * 3 bytes, not the 4-byte-aligned linebuf stride */
>          if (qio_channel_write_all(QIO_CHANNEL(ioc),
>                                    (char *)pixman_image_get_data(linebuf),
> -                                  pixman_image_get_stride(linebuf), errp) < 0) {
> +                                  width * 3, errp) < 0) {
>              return false;
>          }
>      }
> --
> 2.53.0
>
>