Inside the slicer
A sliced file is somebody else's input
Encrust's readers could, until the fuzzers found it: a header's layer count made one reserve memory for gigabytes of rows. Three guards now refuse such files with an error: a count checked against the file's size, a ceiling on the panel, and an archive entry grown under a cap.
Two of Encrust’s readers, the .ctb and the .pwmx one, once reserved a layer table for gigabytes of rows because a header said so. Fuzzing found both (the fuzzing readme). The readers had believed their headers.
The surprise is how little it took. Our reader sized a table from the header’s layer count before checking it against the file.
This post covers Encrust’s readers only: the three checks that now stand between a header and an allocation, and the limits they use. Opening a sliced file shows the same readers from the screen.
A header is a claim
Every number in a container header comes from whoever made the file: layer count, table address, panel size, run length, the uncompressed size of a zip entry. Our readers treat each as a claim. The design note gives the reason in one line: sizing a buffer from one of them “has handed that person the allocator”.
The file might be corrupt rather than hostile. A download cut short or a damaged disk produces the same header lying about the bytes behind it. Our readers cannot tell which, and do not need to. The security policy promises a malformed file is refused, not crashed on, and that is the property the guards give.
The first guard: a count against the file
Reads::claim(what, count, each_bytes) asks whether the file is large enough to hold the rows the header claims. each_bytes is the least one row can cost in the file: a layer table row is its fixed width. The answer is count * each_bytes, computed as a checked multiplication, compared with the file’s length. If it does not fit, the reader returns ImpossibleCount naming what was claimed and how many bytes were behind it. Only then does it reserve capacity.
The checked multiplication matters. A count near u64::MAX times a row width wraps around to a small number in plain arithmetic and passes. Here the overflow is itself the refusal. Reads::bytes applies the same test to a single length field before allocating (the code).
The bound is loose on purpose. It compares against the whole file, not the bytes behind the table, because that is what one call knows without the caller seeking first. It only has to turn an unbounded allocation into one no larger than the file. The reads behind it fail on their own offsets anyway (design note). A caller that passes a row cost smaller than the truth gets a weaker bound; that is the price of asking each caller to state it.
The second guard: a panel against physics
No count in a file limits width_px * height_px: a mask is a mask, whatever compressed it. So panel_in_range multiplies the two with overflow checking and refuses anything above MAX_PANEL_PX, which is 256 x 1024 x 1024 = 268,435,456 pixels.
That is about three times the largest panel in our printer catalogue. Computed from the code’s own test: 15,120 x 6,230 = 94,197,600 pixels, and 268,435,456 / 94,197,600 is 2.85. No printer we list is refused, and no header can ask for a mask the size of memory. A PNG’s own dimensions go through the same check, because in an archive container that is where the panel comes from (reader.rs).
The ceiling will date. A panel past 256 megapixels would be refused as hostile, and the day one ships the constant has to rise.
The third guard: an entry grown, not reserved
A zip states each entry’s uncompressed size in its own directory. We do not allocate that much before decompressing one byte, because that would let the archive choose. read_entry instead reads through take(MAX_ENTRY_BYTES + 1) into a vector that grows with the bytes that arrive. If more than MAX_ENTRY_BYTES come out, it stops and returns an error naming the entry.
MAX_ENTRY_BYTES is MAX_PANEL_PX bytes: one 8-bit grey mask of the largest panel the first guard allows is the biggest thing an entry legitimately holds (de.rs). The .sl1, .cws and gcode-zip readers go through it.
The 3MF case
A 3MF is also a zip, but our mesh loader does not read it entry by entry the way our sliced-file readers do. A dependency parses the package, so we cannot wrap read_entry around it. Instead refuse_oversized_parts opens the archive first, reads every entry’s stated size from the directory without decompressing anything, and refuses the file if any part claims more than 2 GiB (three_mf.rs). Only then is the archive handed over.
The same file caps component nesting at 64 levels. The 3MF specification forbids a cycle, but nothing in a file stops one.
The limits
| Limit | Value | What it bounds | Where it comes from |
|---|---|---|---|
MAX_PANEL_PX |
268,435,456 px | Panel pixels a header may claim | About 2.85 times the largest catalogue panel (94,197,600 px) |
MAX_ENTRY_BYTES |
268,435,456 bytes | One decompressed archive entry in a sliced file | One 8-bit mask of the largest permitted panel |
MAX_PART_BYTES |
2 GiB | One part a 3MF directory may state | A ceiling we chose; a model of a hundred million triangles is well under it, per the code comment |
MAX_DEPTH |
64 | Nesting of 3MF components | A ceiling we chose |
These are limits we set, not measurements of any machine. Only MAX_PANEL_PX has a physical anchor, and only against the printers in our catalogue.
Nineteen fuzz targets
The fuzzing readme lists nineteen targets: the mesh loaders; the Chitu, .goo, Anycubic, .sl1, gcode-zip, Creality, .svgx and .cws readers; the .pws, .pwsz, .photons, .phz, .fdg, encrypted .ctb and .cxline readers; a font loader; a resin-profile reader; and a sniffer that picks a reader for a file whose name says nothing.
The property they test is not that the file opens but that the reader answers: no panic, no hang, and no allocation proportional to a number in a header. Each reader is given the extension it expects, because a fuzzer spending its session failing a magic-number check tests only the magic-number check.
A crash found this way is kept. The reproducing input goes into the owning crate’s test fixtures and a test is added before the fix, so the fault cannot return unnoticed. The repository holds such files.
CI runs a short session per target on a schedule, not as a gate on a pull request. Two minutes of fuzzing is a smoke test, and failing a pull request over which inputs one random session reached would teach people to ignore it.
What this shows
These are Encrust’s own readers, hardened by three guards and exercised by nineteen fuzz targets, so a damaged or hostile sliced file is refused with an error instead of crashing the program. A fuzzer finds faults; it does not prove their absence, and “no panic, no hang, no allocation proportional to a header number” is the property tested, not a guarantee about every input. The security policy says what to do if you find a file that breaks one.
Sources
- Encrust: reading a file somebody else wrote · The three guards and what each bounds
- Encrust: fuzzing the readers · The nineteen targets, the corpus, and what happens to a crash
- Encrust: security policy · What the project promises about a malformed file
- Encrust: the guards in core-format · Reads::claim, Reads::bytes, read_entry and MAX_ENTRY_BYTES