Repository navigation
fix (llamafile chat): uploading a tall, narrow image overflows a heap buffer in print_image() - #1093
Conversation
Code reviewNo issues found. Checked for bugs and CLAUDE.md compliance. Reviewed at bcdcb1d.
Three minor notes, none blocking:
A follow-up commit on this branch addresses all three. 🤖 Generated with Claude Code |
- on_upload() no longer blames the aspect ratio for every failed preview. print_image() also returns -1 when the resize or the write fails, so the message now says no preview was drawn and offers the aspect ratio as the likely cause. - Document that image_scaled_dims() floors the height at two image rows, so an image too wide to fill one line of text is drawn as one line instead of scaling to nothing, and pin it with a test. On main such an image (wider than about 80:1 at 80 columns) drew nothing. - The test header said PNM; the test synthesizes PNGs. print_image_test now makes 1,791 assertions. Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
Description
Fixes a heap buffer overflow in the image preview shown by
/upload.Root cause.
print_image()scaled the uploaded image to the terminal with32-bit arithmetic and no cap on height (
image.cppline 239):A valid PNG 1 pixel wide and 223,697 pixels tall — under a kilobyte compressed —
gives 80 * 17,895,760 * 3 = 4,294,982,400, which wraps to 15,104, and
stbir_resize_uint8_srgb() then writes the full 4.29 GB past that buffer. The
crash lands in stbir__encode_uint8_srgb(). In combined mode the preview runs in
the server's process, so the built-in server goes down with the chat session.
Fix. The scaled destination is now bounded, and the arithmetic is not:
math is int64_t (new image_scaled_dims()), and the allocation is computed
in size_t.
screenshots keep their aspect ratio instead of being squashed into a stripe.
print_image() returns -1. Refusal is the preview only: on_upload() notes
that nothing was drawn and the original bytes still go to the model, so
legitimate tall screenshots keep working.
failed resize meant printing uninitialized memory.
produce a zero-width allocation and an empty preview.
than about 80:1 scaled to a height of 0 and drew nothing; the height is
now floored at two image rows, so it gets one line of text.
Tests. tests/print_image_test.cpp (registered in o/$(MODE)/tests) makes
1,791 assertions that the destination handed to the resize library stays within
max_width × 2 × kImageMaxRows × 3 across extreme widths and heights — including
the reported 1 × 223,697, 1 × 2²⁴, 1 × 2²⁸ — over terminal widths {0, 1, 10,
40, 80, 200} and width caps {1, 2, 8, 80, 1000}, and that ordinary images
(1920×1080 → 80×46, 1080×2400 → 80×178, …) draw exactly what they drew before.
No binary fixtures: the test synthesizes PNGs in memory with deflate uncompressed
blocks, and drives is_image() and print_image() on the reported dimensions.
Verified separately under AddressSanitizer against the production logic with the
reported bytes: refused, nothing allocated, no report; the same bytes through the
pre-fix formula crash in stbir__encode_uint8_srgb().
Severity, as we assess it: CVSS:3.1/AV:L/AC:L/PR:N/UI:R/S:U/C:N/I:N/A:H
(5.5) — linear forward overflow, ~4.3 GB from the end of a 15 KB buffer, no
remote path (the web UI never calls print_image()), and the attacker must
supply the image.
Reported privately; there is no public issue for it yet.
Credit: Aman Verma (WSU), Swarn Priya (WSU), Peng Gao (VT) and Basavesh Ammanaghatta Shivakumar (WSU) 🙏
PR Type
Checklist
AI Usage Information