Heap over-read and crashes in winpr image conversion
Key facts
- Attack vector
- —
- Privileges required
- —
- User interaction
- —
- Attack complexity
- —
- Impact type
- —
- Confidentiality · integrity · availability
- —
- Fix status
- Fixed
No CVSS vector from any source. Fix status from GitHub.
Summary
FreeRDP through 3.32.1 includes winpr image code that mishandles malformed image data. When a 16 bpp DIB is converted to PNG, the encoder reads past the end of each row buffer, a heap buffer over-read. A malformed PNG makes the process abort, and a malformed JPEG makes it exit, because the decoders keep libpng and libjpeg's default error handlers. The flaw is reachable through local clipboard conversion in FreeRDP clients, and an attacker must supply the image data.
Written with Claude Haiku 5.5 from the sources below · 2026-11-12
Description from the GitHub advisory
## Summary Three problems in `winpr/libwinpr/utils/image.c`, all reachable through the winpr clipboard synthesizers that FreeRDP clients use for clipboard redirection: 1. **Heap buffer over-read (server-controlled data).** `save_png_to_buffer()` picks `PNG_COLOR_TYPE_RGB` for any bpp above 8. For a 16 bpp DIB, each row it builds is `stride` bytes and holds only 2 bytes per pixel, but libpng reads 3 bytes per pixel from it. `png_write_row` therefore reads past the end of every row allocation (ASan: heap-buffer-overflow, READ of size 15, `image.c:1102` on master e56295c53). The bytes read past the row end go into the PNG handed to the local clipboard. 2. **Process abort on a malformed PNG.** `winpr_read_png_from_buffer()` never installs a `setjmp` handler. Any `png_error()` (truncated data, bad CRC, corrupt IDAT) calls libpng's default error handler, which calls `abort()`. 3. **Process exit on a malformed JPEG** (builds with `WINPR_UTILS_IMAGE_JPEG`). `winpr_convert_from_jpeg()` keeps libjpeg's default `error_exit`, which calls `exit()`. ## Reachability - **1:** a server offers `CF_DIB` and sends a 16 bpp `BI_BITFIELDS` DIB. When a local application asks the client for `image/png`, the SDL3 client (and any client that converts `CF_DIB` → `image/png` through `ClipboardGetData`) runs `clipboard_synthesize_image_bmp_to_png` → `winpr_image_write_buffer` → `save_png_to_buffer`. Stack from the reproduction: `png_write_row` ← `save_png_to_buffer` (`image.c:1102`) ← `winpr_image_write_buffer` (`image.c:1533`) ← `clipboard_synthesize_image_bmp_to_format` (`synthetic.c:516`) ← `clipboard_synthesize_image_bmp_to_png` (`synthetic.c:539`) - **2 and 3:** local clipboard content that the client converts for the server: a `CF_DIB` request answered from `image/png` or `image/jpeg`, and the "HTML Format" synthesizers. The same decoders run on any image data a winpr-based component decodes from a peer. Only the local-clipboard path was verified here. ## Reproduction `ct.c` (attached) is a small program: `ClipboardCreate` → `ClipboardRegisterFormat`/`GetFormatId` → `ClipboardSetData(src)` → `ClipboardGetData(dst)`. It was linked against winpr from master e56295c53, built with `-fsanitize=address,undefined` and `WINPR_UTILS_IMAGE_PNG/JPEG/WEBP/DIBv5=ON`: ``` ./ct CF_DIB bf16_565.dib image/png out # ASan heap-buffer-overflow, READ of size 15 in png_write_row ./ct image/png truncated.png CF_DIB out # "libpng error: read error in read_data_memory", abort (exit 134) ./ct image/jpeg truncated.jpg CF_DIB out # "Premature end of JPEG file", exit(1) ``` `bf16_565.dib` is a 5x3, 16 bpp, `BI_BITFIELDS` 5-6-5 DIB (generator `mkdib.py` attached). The truncated inputs are any valid PNG or JPEG cut to half its length. ## Suggested fix (attached: winpr-image-decoders.master.patch, against master e56295c53) - `save_png_to_buffer`: encode only 24 and 32 bpp, which is what the LodePNG branch of `winpr_convert_to_png` already does. Other depths fail the conversion instead of over-reading. - `winpr_read_png_from_buffer`: run `png_read_png` behind `setjmp(png_jmpbuf())` and fail the read on error. - `winpr_convert_from_jpeg`: add an `error_exit` that `longjmp`s back into the decoder (`volatile` locals), and free the partial output on failure. The same patch fixes some non-security decoder issues that make valid images fail or come out wrong. We found them in the same pass and are happy to split them into a public PR if you prefer: - PNG reads normalize to 8-bit BGR(A) via `PNG_TRANSFORM_EXPAND | STRIP_16 | PACKING | GRAY_TO_RGB`. Before, grayscale, palette and 16-bit PNGs failed or produced unusable 1/16/48/64 bpp DIBs. - PNG and JPEG rows are padded to 4 bytes, like the BMP reader already does. Before, odd-width RGB images failed in `winpr_bitmap_write_buffer`. - JPEG reports its actual output bpp. Before, grayscale JPEGs reported 8 bpp for BGR output. - 32 bpp DIBs whose alpha bytes are all 0 (the usual `BI_RGB` output from Windows) are encoded opaque instead of fully transparent. Verification with the patch: - ASan clean across a corpus of truncated, bad-CRC, grayscale, palette, 16-bit and odd-width PNGs; truncated and corrupt JPEGs; and 16 bpp, palette, BITFIELDS, V5 and oversized DIBs. Every output matches its source pixel for pixel. - `TestImage` and `TestClipboardFormats` pass. - Tested end to end in the SDL3 client against gnome-remote-desktop. ## Affected - 3.x, including 3.32.1, and master e56295c53. - 1 needs `WINPR_UTILS_IMAGE_PNG`. - 2 needs `WINPR_UTILS_IMAGE_PNG`. - 3 needs `WINPR_UTILS_IMAGE_JPEG`. ## Credit Nick Allevato (found with AI assistance, Claude, while fixing SDL3 clipboard handling in #13560). <details><summary>winpr-image-decoders.master.patch</summary> ```diff diff --git a/winpr/libwinpr/utils/image.c b/winpr/libwinpr/utils/image.c index 4207be215..fe53469ba 100644 --- a/winpr/libwinpr/utils/image.c +++ b/winpr/libwinpr/utils/image.c @@ -36,6 +36,7 @@ #if defined(WINPR_UTILS_IMAGE_JPEG) #define INT32 INT32_WINPR +#include <setjmp.h> #include <jpeglib.h> #undef INT32 #endif @@ -824,6 +825,24 @@ fail: #endif } +#if defined(WINPR_UTILS_IMAGE_JPEG) +/* libjpeg's default error_exit calls exit(): return to the decoder instead */ +struct winpr_jpeg_error_mgr +{ + struct jpeg_error_mgr pub; + jmp_buf jmp; +}; + +static void winpr_jpeg_error_exit(j_common_ptr cinfo) +{ + struct winpr_jpeg_error_mgr* err = (struct winpr_jpeg_error_mgr*)cinfo->err; + char msg[JMSG_LENGTH_MAX] = WINPR_C_ARRAY_INIT; + (*cinfo->err->format_message)(cinfo, msg); + WLog_WARN(TAG, "JPEG decoding failed: %s", msg); + longjmp(err->jmp, 1); +} +#endif + // NOLINTBEGIN(readability-non-const-parameter) SSIZE_T winpr_convert_from_jpeg(WINPR_ATTR_UNUSED const BYTE* comp_data, WINPR_ATTR_UNUSED size_t comp_data_bytes, @@ -850,43 +869,52 @@ SSIZE_T winpr_convert_from_jpeg(WINPR_ATTR_UNUSED const BYTE* comp_data, return -1; #else struct jpeg_decompress_struct cinfo = WINPR_C_ARRAY_INIT; - struct jpeg_error_mgr jerr; - SSIZE_T size = -1; - BYTE* decomp_data = nullptr; + struct winpr_jpeg_error_mgr jerr = WINPR_C_ARRAY_INIT; + /* modified after setjmp and read after longjmp */ + volatile SSIZE_T size = -1; + BYTE* volatile decomp_data = nullptr; + + cinfo.err = jpeg_std_error(&jerr.pub); + jerr.pub.error_exit = winpr_jpeg_error_exit; + if (setjmp(jerr.jmp)) + goto fail; - cinfo.err = jpeg_std_error(&jerr); jpeg_create_decompress(&cinfo); jpeg_mem_src(&cinfo, comp_data, comp_data_bytes); if (jpeg_read_header(&cinfo, 1) != JPEG_HEADER_OK) goto fail; + /* grayscale is expanded to BGR as well */ cinfo.out_color_space = cinfo.num_components > 3 ? JCS_EXT_RGBA : JCS_EXT_BGR; - const int components = MAX(3, cinfo.num_components); - - *width = WINPR_ASSERTING_INT_CAST(uint32_t, cinfo.image_width); - *height = WINPR_ASSERTING_INT_CAST(uint32_t, cinfo.image_height); - *bpp = WINPR_ASSERTING_INT_CAST(uint32_t, cinfo.num_components * 8); if (!jpeg_start_decompress(&cinfo)) goto fail; - size_t stride = 1ULL * cinfo.image_width * WINPR_ASSERTING_INT_CAST(size_t, components); + *width = WINPR_ASSERTING_INT_CAST(uint32_t, cinfo.output_width); + *height = WINPR_ASSERTING_INT_CAST(uint32_t, cinfo.output_height); + *bpp = WINPR_ASSERTING_INT_CAST(uint32_t, cinfo.output_components * 8); - if ((stride == 0) || (cinfo.image_height == 0) || (stride > UINT32_MAX)) + /* rows 4 byte aligned, like every other wImage */ + size_t stride = + 1ULL * cinfo.output_width * WINPR_ASSERTING_INT_CAST(size_t, cinfo.output_components); + if ((stride % 4) != 0) + stride += 4 - (stride % 4); + + if ((stride == 0) || (cinfo.output_height == 0) || (stride > UINT32_MAX)) goto fail; *scanline = (UINT32)stride; - decomp_data = calloc(stride, cinfo.image_height); + decomp_data = calloc(stride, cinfo.output_height); if (decomp_data) { - while (cinfo.output_scanline < cinfo.image_height) + while (cinfo.output_scanline < cinfo.output_height) { JSAMPROW row = &decomp_data[cinfo.output_scanline * stride]; if (jpeg_read_scanlines(&cinfo, &row, 1) != 1) goto fail; } - const size_t ssize = stride * cinfo.image_height; + const size_t ssize = stride * cinfo.output_height; WINPR_ASSERT(ssize < SSIZE_MAX); size = (SSIZE_T)ssize; } @@ -894,6 +922,15 @@ SSIZE_T winpr_convert_from_jpeg(WINPR_ATTR_UNUSED const BYTE* comp_data, fail: jpeg_destroy_decompress(&cinfo); + if (size < 0) + { + free(decomp_data); + decomp_data = nullptr; + *width = 0; + *height = 0; + *bpp = 0; + *scanline = 0; + } *ppdecomp_data = decomp_data; return size; #endif @@ -1050,6 +1087,31 @@ static SSIZE_T save_png_to_buffer(UINT32 bpp, UINT32 width, uint32_t stride, UIN if (size < (1ULL * stride * height)) goto fail; + /* The rows below are written as 8 bit RGB or RGBA; any other depth would make libpng read + * past the end of each row. */ + if ((bpp != 24) && (bpp != 32)) + goto fail; + + /* 32 bpp DIBs from Windows usually leave the alpha bytes 0 (BI_RGB has no alpha): written + * as is, that is a fully transparent image. */ + BOOL opaque = FALSE; + if (bpp == 32) + { + opaque = TRUE; + for (size_t y = 0; opaque && (y < height); ++y) + { + const uint8_t* line = &data[y * stride]; + for (size_t x = 0; x < width; ++x) + { + if (line[x * 4 + 3] != 0) + { + opaque = FALSE; + break; + } + } + } + } + /* Initialize the write struct. */ png_ptr = png_create_write_struct(PNG_LIBPNG_VER_STRING, nullptr, nullptr, nullptr); if (png_ptr == nullptr) @@ -1092,7 +1154,10 @@ static SSIZE_T save_png_to_buffer(UINT32 bpp, UINT32 width, uint32_t stride, UIN if (bpp > 16) *row++ = *line++; if (bpp > 24) - *row++ = *line++; + { + *row++ = opaque ? 0xFF : *line; + line++; + } } } @@ -1137,6 +1202,15 @@ static void read_data_memory(png_structp png_ptr, png_bytep data, size_t length) } } +/* libpng reports errors with longjmp and aborts the process when nobody catches it */ +static BOOL winpr_png_read_image(png_structp png_ptr, png_infop info_ptr, int transforms) +{ + if (setjmp(png_jmpbuf(png_ptr))) + return FALSE; + png_read_png(png_ptr, info_ptr, transforms, nullptr); + return TRUE; +} + WINPR_ATTR_MALLOC(free, 1) static void* winpr_read_png_from_buffer(const void* data, size_t SrcSize, size_t* pSize, UINT32* pWidth, UINT32* pHeight, UINT32* pBpp, @@ -1175,8 +1249,14 @@ static void* winpr_read_png_from_buffer(const void* data, size_t SrcSize, size_t png_set_read_fn(png_ptr, &memory_reader_state, read_data_memory); - transforms |= PNG_TRANSFORM_BGR; - png_read_png(png_ptr, info_ptr, transforms, nullptr); + /* always 8 bit BGR or BGRA: expand palette, gray and low bit depths, strip 16 bit */ + transforms |= PNG_TRANSFORM_BGR | PNG_TRANSFORM_EXPAND | PNG_TRANSFORM_STRIP_16 | + PNG_TRANSFORM_PACKING | PNG_TRANSFORM_GRAY_TO_RGB; + if (!winpr_png_read_image(png_ptr, info_ptr, transforms)) + { + WLog_WARN(TAG, "PNG decoding failed"); + goto fail; + } if (png_get_IHDR(png_ptr, info_ptr, &width, &height, &bit_depth, &color_type, &interlace_type, nullptr, nullptr) != 1) @@ -1193,25 +1273,30 @@ static void* winpr_read_png_from_buffer(const void* data, size_t SrcSize, size_t goto fail; const size_t png_stride = png_get_rowbytes(png_ptr, info_ptr); - if ((png_stride == 0) || (height == 0) || (png_stride > SIZE_MAX / height)) + /* rows 4 byte aligned, like every other wImage */ + size_t stride = png_stride; + if ((stride % 4) != 0) + stride += 4 - (stride % 4); + if ((png_stride == 0) || (height == 0) || (stride > UINT32_MAX) || + (stride > SIZE_MAX / height)) goto fail; - const size_t size = 1ULL * png_stride * height; + const size_t size = 1ULL * stride * height; - rc = calloc(png_stride, height); + rc = calloc(stride, height); if (rc) { char* cur = rc; for (png_uint_32 i = 0; i < height; i++) { memcpy(cur, row_pointers[i], png_stride); - cur += png_stride; + cur += stride; } *pSize = size; *pWidth = width; *pHeight = height; WINPR_ASSERT(bpp <= UINT32_MAX); *pBpp = (UINT32)bpp; - *pScanline = (UINT32)png_stride; + *pScanline = (UINT32)stride; } } fail: ``` </details> <details><summary>ct.c (reproducer)</summary> ```c /* ct: winpr clipboard conversion probe. * usage: ct <src> <infile> <dst> [outfile] * src/dst: CF_TEXT CF_OEMTEXT CF_UNICODETEXT CF_DIB CF_DIBV5 CF_TIFF CF_RAW, or a mime/format name. * src names are resolved like sdl_clip getLocalData does: * prefix "get:" -> ClipboardGetFormatId (may return 0), otherwise ClipboardRegisterFormat. */ #include <stdio.h> #include <stdlib.h> #include <string.h> #include <stdint.h> #include <winpr/clipboard.h> #include <winpr/user.h> #include <winpr/wlog.h> static const char* cfnames[] = { "CF_RAW", "CF_TEXT", "CF_BITMAP", "CF_METAFILEPICT", "CF_SYLK", "CF_DIF", "CF_TIFF", "CF_OEMTEXT", "CF_DIB", "CF_PALETTE", "CF_PENDATA", "CF_RIFF", "CF_WAVE", "CF_UNICODETEXT", "CF_ENHMETAFILE", "CF_HDROP", "CF_LOCALE", "CF_DIBV5" }; static UINT32 resolve(wClipboard* c, const char* n) { for (UINT32 i = 0; i < sizeof(cfnames) / sizeof(cfnames[0]); i++) if (strcmp(n, cfnames[i]) == 0) return i; if (strncmp(n, "get:", 4) == 0) return ClipboardGetFormatId(c, n + 4); return ClipboardRegisterFormat(c, n); } static void* slurp(const char* f, size_t* len) { FILE* fp = fopen(f, "rb"); if (!fp) return NULL; fseek(fp, 0, SEEK_END); long l = ftell(fp); fseek(fp, 0, SEEK_SET); void* b = malloc(l ? (size_t)l : 1); *len = fread(b, 1, (size_t)l, fp); fclose(fp); return b; } static void dump_dib(const BYTE* p, UINT32 n) { if (n < 40) { printf(" dib: too short (%u)\n", n); return; } uint32_t biSize, comp, sizeImage, clrUsed; int32_t w, h, xppm; uint16_t planes, bpp; memcpy(&biSize, p, 4); memcpy(&w, p + 4, 4); memcpy(&h, p + 8, 4); memcpy(&planes, p + 12, 2); memcpy(&bpp, p + 14, 2); memcpy(&comp, p + 16, 4); memcpy(&sizeImage, p + 20, 4); memcpy(&xppm, p + 24, 4); memcpy(&clrUsed, p + 32, 4); printf(" dib: biSize=%u w=%d h=%d(%s) planes=%u bpp=%u comp=%u sizeImage=%u xppm=%d clrUsed=%u " "total=%u\n", biSize, w, h, h < 0 ? "top-down" : "bottom-up", planes, bpp, comp, sizeImage, xppm, clrUsed, n); } int main(int argc, char** argv) { if (argc < 4) { fprintf(stderr, "usage\n"); return 2; } wClipboard* c = ClipboardCreate(); UINT32 src = resolve(c, argv[1]); UINT32 dst = resolve(c, argv[3]); size_t len = 0; void* in = slurp(argv[2], &len); if (!in) { printf("cannot read %s\n", argv[2]); return 2; } BOOL ok = ClipboardSetData(c, src, in, (UINT32)len); printf("SetData(%s=0x%04x [%s], %zu bytes) -> %s\n", argv[1], src, ClipboardGetFormatName(c, src) ? ClipboardGetFormatName(c, src) : "?", len, ok ? "TRUE" : "FALSE"); UINT32 outlen = 0; void* out = ClipboardGetData(c, dst, &outlen); printf("GetData(%s=0x%04x) -> %s, %u bytes\n", argv[3], dst, out ? "OK" : "NULL", outlen); if (out && ((dst == CF_DIB) || (dst == CF_DIBV5))) dump_dib(out, outlen); if (out && argc > 4) { FILE* fp = fopen(argv[4], "wb"); if (fp) { fwrite(out, 1, outlen, fp); fclose(fp); } } free(out); free(in); ClipboardDestroy(c); return out ? 0 : 1; } ``` </details> <details><summary>mkdib.py (DIB generator)</summary> ```python import struct W,H=5,3 # odd width on purpose def px(x,y): return (x*50%256, y*80%256, (x+y)*30%256) # r,g,b def hdr(biSize,w,h,bpp,comp,simg,clr=0): return struct.pack('<IiiHHIIiiII',biSize,w,h,1,bpp,comp,simg,2835,2835,clr,0) def rows(bpp,topdown,alpha=None): out=b'' ys=range(H) if topdown else range(H-1,-1,-1) for y in ys: r=b'' for x in range(W): R,G,B=px(x,y) if bpp==24: r+=bytes([B,G,R]) elif bpp==32: r+=bytes([B,G,R,alpha if alpha is not None else 255]) elif bpp==16: r+=struct.pack('<H',((R>>3)<<11)|((G>>2)<<5)|(B>>3)) r+=b'\0'*((-len(r))%4) out+=r return out def w(name,data): open('dib/'+name,'wb').write(data) d=rows(24,False); w('rgb24_bu.dib',hdr(40,W,H,24,0,len(d))+d) d=rows(24,True); w('rgb24_td.dib',hdr(40,W,-H,24,0,len(d))+d) d=rows(24,False); w('rgb24_simg0.dib',hdr(40,W,H,24,0,0)+d) d=rows(32,False,0); w('rgb32_a0.dib',hdr(40,W,H,32,0,len(d))+d) # Windows-typical: alpha byte 0 d=rows(32,False,255); w('rgb32_a255.dib',hdr(40,W,H,32,0,len(d))+d) masks=struct.pack('<III',0xff0000,0xff00,0xff) d=rows(32,False,0); w('bf32.dib',hdr(40,W,H,32,3,len(d))+masks+d) d=rows(32,False,0); w('bf32_simg0.dib',hdr(40,W,H,32,3,0)+masks+d) m565=struct.pack('<III',0xf800,0x07e0,0x001f) d=rows(16,False); w('bf16_565.dib',hdr(40,W,H,16,3,len(d))+m565+d) # 8bpp palette: grayscale ramp palette, index = x*50 pal=b''.join(bytes([i,i,i,0]) for i in range(256)) d=b'' for y in range(H-1,-1,-1): r=bytes([(x*50)%256 for x in range(W)]); r+=b'\0'*((-len(r))%4); d+=r w('pal8.dib',hdr(40,W,H,8,0,len(d),256)+pal+d) # DIBV5 (124-byte header) 32bpp BI_BITFIELDS with alpha mask, alpha=128 v5=struct.pack('<IiiHHIIiiII',124,W,H,1,32,3,W*4*H,2835,2835,0,0)+struct.pack('<IIII',0xff0000,0xff00,0xff,0xff000000)+b'BGRs'+b'\0'*36+b'\0'*12+struct.pack('<IIII',4,0,0,0) assert len(v5)==124,len(v5) d=rows(32,False,128); w('v5_32a.dib',v5+d) # 1x1 d=rows(24,False)[:0] one=bytes([0x56,0x34,0x12,0]) w('one24.dib',hdr(40,1,1,24,0,4)+one) # malicious: claims 60000x60000 but only 100 bytes w('evil_dims.dib',hdr(40,60000,60000,32,0,0)+b'\0'*100) w('evil_simg.dib',hdr(40,W,H,24,0,0xffffff00)+rows(24,False)) w('tiny.dib',b'\x28\0\0\0') ``` </details>
Why it matters now
SSVC decision points
Not available: CISA has not published SSVC decision points for this record.
Affected products
| Vendor | Product | Affected versions | Fixed in | Status | Source |
|---|---|---|---|---|---|
| — | FreeRDP (c) | <= 3.32.1 | 3.32.2 | Patch available | GHSA-x7w3-32ch-x2jq |
- FreeRDP (c) Vendor— Affected <= 3.32.1 Fixed in 3.32.2 Patch available GHSA-x7w3-32ch-x2jq
Remediation
Upgrade FreeRDP to version 3.32.2, which fixes the issue.
Written with Claude Haiku 5.5 from the sources below · 2026-11-12
- Fix · GHSA-x7w3-32ch-x2jq FreeRDP (c) Fixed in 3.32.2 Advisory →
Timeline
- 2026-11-12 Threatist record created from GHSA-x7w3-32ch-x2jq
References
Sources
This product uses the NVD API but is not endorsed or certified by the NVD. Sources and licences