CVE-2026-58971 AI-generated analysis
platform/packages/services/BuiltInPrintService, jni libwfds mupdf decode plugin (jni/plugins/wprint_mupdf.c)
Adds validation in _mupdf_get_hdr (jni/plugins/wprint_mupdf.c) of the page dimensions returned by the pdf renderer and of pdf_render_resolution so that the scaled width, height and width*height*3 byte count cannot wrap a 32-bit integer before the pixel buffer is malloc'ed, plus NULL checks and a bitmap_ptr allocation-failure path, with a new gtest (libwfds_test).
Root cause
In _mupdf_get_hdr, pageWidth/pageHeight (the page dimensions from the pdf renderer; the test mocks them through getPageAttributes, but the lines that fill them are not shown in the diff) were multiplied by zoom (pdf_render_resolution / 72) and truncated to unsigned int with no range check. The resolution itself was not checked for being positive. The buffer size was then computed as `size = imageWidth * imageHeight * 3` in 32-bit unsigned arithmetic and stored in an int. Large page dimensions or a large resolution therefore wrap the product, and malloc((size_t) size) returns a buffer smaller than the width*height*3 pixel area that image_info->width/height later describe. The second allocation, bitmap_ptr = malloc(width * 3), also had no NULL check.
How the fix works
- jni/plugins/wprint_mupdf.c:_mupdf_get_hdr adds early rejections (return ERROR with LOGE) for image_info == NULL, image_info->decoder_data.urlPath == NULL, image_info->pdf_render_resolution <= 0 and pdf_render == NULL, before openDocument is called.
- jni/plugins/wprint_mupdf.c:_mupdf_get_hdr computes scaledWidth = pageWidth * zoom and scaledHeight = pageHeight * zoom as doubles. It rejects them if either is not finite (isfinite, new <math.h> include), is <= 0.0, or is greater than MAX_DIMENSION = INT32_MAX / RGB_NUMBER_PIXELS_NUM_COMPONENTS (new <stdint.h> include).
- jni/plugins/wprint_mupdf.c:_mupdf_get_hdr then computes maxBytes = scaledWidth * scaledHeight * 3 in double and rejects the page if this exceeds INT32_MAX, so the later integer product cannot wrap. Only after that are imageWidth/imageHeight truncated to unsigned int, and a final check rejects a zero width or height.
- jni/plugins/wprint_mupdf.c:_mupdf_get_hdr replaces the literal 3 with RGB_NUMBER_PIXELS_NUM_COMPONENTS and casts the (now bounded) product to int for `size`.
- jni/plugins/wprint_mupdf.c:_mupdf_get_hdr checks the result of the bitmap_ptr malloc. On failure it frees rawBuffer, clears fz_pixmap_ptr and returns ERROR instead of continuing with a NULL bitmap_ptr.
- jni/Android.bp: cc_test libwfds_ipphelper_test is renamed to libwfds_test. It gains plugins/wprint_mupdf_test.cpp, plugins/wprint_mupdf.c, plugins/wprint_image.c and plugins/wprint_image_platform.c as srcs, plus header_libs: ["jni_headers"]. The new jni/plugins/wprint_mupdf_test.cpp mocks pdf_render_ifc_t (by defining create_pdf_render_ifc) and tests valid, overflow, negative, zero and NaN dimensions, plus extreme and negative resolution cases.
Completeness
- Both dimension-derived allocations in _mupdf_get_hdr are covered: rawBuffer (width*height*3) and bitmap_ptr (width*3). The per-dimension bound INT32_MAX/3 keeps width*3 from overflowing, and the maxBytes bound keeps width*height*3 within int32 before the unsigned product is formed.
- Validation is applied in double before truncation, so NaN, infinite and negative values from the renderer are rejected rather than cast to unsigned int.
- The diff changes nothing in wprint_mupdf.c outside _mupdf_get_hdr. Other code that consumes image_info->width/height is not shown and relies on get_hdr having validated the values.
- The existing `if (pages < 1) return ERROR;` after openDocument is kept. The lines between openDocument and the zoom computation are not shown in the diff, so the material does not show whether the renderer's page-attribute status is checked.
- The bound is a fixed INT32_MAX limit on bytes, not a product-specific memory budget. Allocations up to about 2 GiB are still permitted and may fail at malloc, which the existing `if (!rawBuffer) return ERROR;` handles.
Behavior changes
- _mupdf_get_hdr now returns ERROR (and logs via LOGE) for PDF pages whose scaled width or height exceeds INT32_MAX/3 pixels, whose total RGB byte count exceeds INT32_MAX, or whose scaled dimensions are zero, negative, NaN or infinite. Per the test comments, such pages could previously proceed with a wrapped buffer size.
- _mupdf_get_hdr returns ERROR for pdf_render_resolution <= 0, a NULL urlPath, a NULL image_info or an uninitialised pdf_render, instead of calling into the renderer.
- If the bitmap_ptr allocation fails, _mupdf_get_hdr now frees rawBuffer, resets fz_pixmap_ptr to NULL and returns ERROR, instead of returning OK with a NULL bitmap_ptr.
- Build/test: the cc_test module is renamed from libwfds_ipphelper_test to libwfds_test and now also compiles the mupdf plugin sources and wprint_image*.c.
Per major
- Android 14: same fix: d2087a448a7f in android-security-14.0.0_r31. The wprint_mupdf.c hunks (blob 59a0d10..cda4750) and the test file are identical; the Android.bp hunks are at slightly different line offsets.
- Android 15: same fix: a11e78ca1f5c in android-security-15.0.0_r19 (the analysed commit).
- Android 16: same fix: 5d09fa8f8ddc in android-security-16.0.0_r10. The wprint_mupdf.c blob change (59a0d10..cda4750) is identical; the Android.bp offsets differ.
- Android 17: same fix: 45d991a0d6b7 in android-security-17.0.0_r3. The wprint_mupdf.c base differs slightly (blob 2a3b168..e7744e1, final hunk at line 88 instead of 86), but the added validation code is identical.
Unknowns
- How the undersized rawBuffer is later consumed (the render/decode path is not in the diff), and therefore the concrete mechanism behind the bulletin's ID classification.
- How the renderer's page dimensions are obtained in the omitted lines of _mupdf_get_hdr, and whether the renderer's status return is checked there.
- Where pdf_render_resolution originates, and whether an attacker can influence it or the print pipeline fixes it.
- Whether plugins/pdf_render.c would clash with the test's mock create_pdf_render_ifc if added to the test srcs.