Streaming & memory
Every file gets the same scan whatever its size, except the few checks that parse it whole (below):
- every
.ndbform: wildcards, gaps, anchored offsets, file-type targets; .ldblogical signatures, with their regexes and byte comparisons;- the normalised views of HTML, text and scripts that those signatures are written against;
- YARA rules, bytecode programs, whole-file hashes;
- archives decoded as they are read, embedded files, heuristics.
What changes with size is where the file is while that runs.
Can a file be larger than the RAM?
Section titled “Can a file be larger than the RAM?”Most can. A file on disk is never loaded past --max-object-bytes, so what a
scan holds does not grow with the file. The time does: a file past the limit is
read several times over, and each pass goes through the whole signature
database, so a 20 GB file takes minutes with a handful of signatures and longer
with a full database. Served over HTTP and scanned with only the four built-in
signatures, 20 GB objects of text, RAR, OLE and PDF took from 2 to 21 minutes
and held 50 to 61 MB at their peak (the figures).
A real database is the larger part of the memory:
sizing a server works through a 4 GiB machine.
What happens past --max-object-bytes (256 MiB), by kind of input:
| Input | Held in memory | Needs disk | A 20 GB input |
|---|---|---|---|
| A file on disk: executable, script, document, raw disk or filesystem image | Block cache, 32 MiB | No | Every signature on its bytes is matched. A PE is parsed from its image (the headers and sections, up to the end of the last section’s data), and YARA’s elf module reads an ELF’s own image, so a small executable with a large overlay gets every check; a PE whose image is past the limit, and an ELF or Mach-O past it for the packer checks, are LIMITS-EXCEEDED if nothing is found |
ZIP, tar, CAB, ISO, LHA, ar, cpio, 7z, WIM, xar, RAR (and RAR volume sets), CHM, OLE (MSI included), email and mailboxes, the single-stream compressors, self-extracting executables, and the formats built on them |
One member, up to 256 MiB | Only for a compressed member over 256 MiB, up to --max-spill-bytes (2 GiB) |
Scanned member by member. A stored member over 256 MiB is read where it lies, with no copy, no disk and no spill ceiling (--max-matcher-bytes counts it). A compressed member past the spill ceiling, or with --spill-dir off, has only its first 256 MiB scanned and the file is LIMITS-EXCEEDED unless they hold a detection. A RAR with encrypted headers, encrypted members or RAR5 links is decoded whole, under 256 MiB |
| Disk images (VHD, VHDX, QCOW2, VMDK, DMG) and the filesystems inside them or on their own: FAT, exFAT, NTFS, ext, SquashFS, cramfs, romfs, EROFS, HFS+, APFS | A cache of up to 32 MiB, and one file up to 256 MiB | Only for a file over 256 MiB, up to --max-spill-bytes (2 GiB) |
Walked in place, each file read from where the volume has it, and one past 256 MiB goes to a spill file as a ZIP member does. A file stored compressed is read a unit at a time: a block of HFS+ and APFS (decmpfs), an extent of EROFS, a compression unit of NTFS. A raw image’s own bytes are matched as part of the image |
| PDF, ARJ and the other formats read whole | Not held: the container is not opened | No | The file’s own bytes are scanned, its members are not unpacked, and the result is LIMITS-EXCEEDED unless something is found. Raising --max-object-bytes to cover it takes that much RAM |
Stdin, INSTREAM, an ICAP body |
16 MiB | Yes: the stream is written to a temporary file to make it seekable | The first 2 GiB is scanned and the result is LIMITS-EXCEEDED PARTIAL. With --spill-dir off, the first 16 MiB. Raise --max-spill-bytes and give it the disk, or hand the daemon a path instead |
An http(s):// URL |
See HTTP sources | No | Scanned in full, over the network about twice |
Two other limits apply whatever the memory. --max-scan-secs stops a daemon
job after 120 seconds by default (a one-shot run has no time limit), and a
file past --max-object-bytes is read several times over, so a very large one
can reach it. --max-matcher-bytes (10 GiB) counts what members and carved
objects feed to the matcher, not the top-level file, so it did not stop the 20
GiB file above. It does count the files of an archive or a disk image, and the
holes of a sparse file, which read as zeros: an archive or image holding more
than that is scanned as far as it, then reported LIMITS-EXCEEDED, and raising
the limit takes it further.
Up to --max-object-bytes: in memory
Section titled “Up to --max-object-bytes: in memory”A file up to --max-object-bytes (256 MiB by default) is read into memory, and
so is an HTTP source that size. Memory is roughly the file’s size, plus a
lowercase copy when a case-insensitive signature has to be checked and, for
text, one normalised copy at a time. The deobfuscated
JavaScript view stops at 32 MiB; a script whose view is longer is reported
LIMITS-EXCEEDED unless something is found, though its raw bytes are still
scanned in full.
Past it: through a block cache
Section titled “Past it: through a block cache”A larger file is read through a cache of 1 MiB blocks, of which at most 32 MiB are held. So are an HTTP source of that size, a spill file (below) and a stored archive member past the limit. Verifying a match can still look anywhere in the file: the cache bounds what is held at once, not how far a check may reach, and a block that was dropped is read again when it is needed. The scan is the same one, with the same results, only slower: the file is read once for carving, the digests, the signature sweep (every anchor at once), YARA’s patterns and the static model’s byte counts, and again in the parts a check needs. Format detection reads only the file’s first 4 MiB, its end and the places its structure names, such as the bytes after an executable’s image. A signature with an unbounded gap adds a walk over the rest of the object after the sweep, and a PE’s image can add a read. The block cache below says how it works and what its three settings trade.
A few things need the file whole and are not done past the limit:
- the structure of a PE whose image, the part up to the end of its last section, is past the limit: entry-point and section offsets, section hashes, imports, icons; and UPX and packer unpacking, which takes the executable whole;
- YARA’s
pe,elfanddotnetmodules, when the image is past the limit; - a PCRE subsignature with a lookaround or a backreference, and the
fuzzy_imgimage hash; - containers whose format is read whole (PDF, ARJ and others; see Supported formats).
A file for which one of these applied is reported LIMITS-EXCEEDED, naming
--max-object-bytes, unless something is found. Raise the limit to have them
run, at the cost of memory.
The normalised views of a large text file are as large as the file, so they go to a spill file.
The block cache
Section titled “The block cache”Picture a window of a few large blocks sliding over the object, with a short memory of the blocks it has just left.
- What it is. Each object read in place (a file, an HTTP source, a spill
file) has one cache. It cuts the object into aligned blocks of
--object-cache-block-bytes(1 MiB) and keeps up to--object-cache-bytes(32 MiB) of them, so 32 blocks by default. Never fewer than two. A stored archive member read in place goes through its archive’s cache. - A read. Every check asks for bytes at an offset. If the block holding them is held, the bytes are copied out of memory. If not, the whole block is read from the source, in one request over HTTP. A read that spans two blocks takes both.
- What it drops. Once the cache is full, a block read right after the one before it is part of a pass, which will not need it again until it has read everything else: it goes into two blocks set apart for passes, and the pass streams through them. Any other block replaces the least recently used. So a pass leaves the rest of the cache in place: what an earlier pass left, and the places scattered reads come back to.
- Two kinds of read. The sweeps (signature sweep, hashes, typing, carving) go through the object in order, so each block is read once per pass and is never asked for again. Verifying a match, reading a ZIP’s directory and then its members, or stepping a regex, ask at scattered offsets, and each miss costs a whole block, however few bytes it needed.
- Over HTTP, runs are merged. A read that goes on from where the last
request stopped asks for twice as much as that one did, from one block up to
--http-request-bytes(8 MiB). A pass over a large object costs one request per 8 MiB, not one per block, while scattered reads stay one block each. - Passes. An object past
--max-object-bytesis read once for the type search, carving, the digests, the signature sweep, YARA’s patterns, the UPX header search and the static model’s byte counts; a signature with an unbounded gap adds a walk over the rest of the object after the sweep, and a PE’s image can add a read. An object larger than the cache is read again from the source at each of those, except the blocks the cache kept from the pass before (all but two). An object that fits in the cache is read from the source once and held, so every pass after the first costs no request at all. - What it costs in memory. The cache’s size for each object being scanned in
place, plus up to one request held by the HTTP reader while it is read, plus
a copy of the bytes a check is looking at (a 1 MiB chunk for the sweep).
Objects nested in one another each have their own cache: an archive on HTTP
whose big member is spilled to disk holds two. A daemon with
--max-process-byteslowers the cache and the request to an eighth of a job’s memory each, at most. - What it does not do. It reads nothing ahead of the block asked for (apart from the doubling of HTTP requests above), shares nothing between objects, does not write, and does not change what the scan finds, only how many reads of the source it takes.
Which setting to change:
| Setting | Raise it when | What it costs |
|---|---|---|
--object-cache-block-bytes |
requests are slow (HTTP, network storage) and there are many small misses: fewer requests for the same scattered reads | each miss moves a whole block: 1 MiB to verify a 100-byte match. Past about 1 MiB the misses stop falling |
--object-cache-bytes |
the object is not much bigger than the cache: every pass but the first is then free, and a few more blocks hold more of the places verification goes back to | that much memory per object being scanned in place, nested ones included |
--http-request-bytes |
a pass over a large object is bound by round trips, not bandwidth | that much memory held while the object is read |
What each setting did against a local server that waits before answering each
request, so the delay stands for the round trip. The first object is a 300 MB
file under a database with a pattern, a logical signature with a gap and the
hash of that size. It is read about 4.4 times over, and 300 MB is past
--max-object-bytes, so it goes through the cache. Block, cache and request
sizes in the first three columns. These two tables were measured when the cache
dropped the least recently used block in every case, before passes were
streamed through it (below):
| Block | Cache | Request | Requests | Fetched | No delay | 20 ms | 100 ms |
|---|---|---|---|---|---|---|---|
| 64K | 8M | 8M | 506 | 1.23 GB | 4.4 s | 15.0 s | 55.9 s |
| 256K | 8M | 8M | 361 | 1.29 GB | 4.1 s | 12.0 s | 41.2 s |
| 1M | 8M | 8M | 176 | 1.33 GB | 4.0 s | 8.0 s | 22.1 s |
| 4M | 8M | 8M | 170 | 1.35 GB | 4.1 s | 7.9 s | 21.4 s |
| 1M | 32M | 8M | 176 | 1.33 GB | 4.2 s | 8.2 s | 22.1 s |
| 1M | 8M | 1M | 1,275 | 1.33 GB | 4.4 s | 31.7 s | 136.0 s |
| 1M | 8M | 32M | 66 | 1.33 GB | 4.0 s | 5.4 s | 10.7 s |
The second is a 20 MB ZIP of 2,000 small members, read in place (with
--max-object-bytes 1M, as a much larger archive would be). The cache is
smaller than the file, so the archive is read six times over; a cache that holds
it reads it once:
| Block | Cache | Requests | Fetched | No delay | 20 ms | 100 ms |
|---|---|---|---|---|---|---|
| 64K | 8M | 56 | 121 MB | 0.7 s | 1.8 s | 6.3 s |
| 1M | 8M | 32 | 122 MB | 0.7 s | 1.3 s | 4.0 s |
| 4M | 8M | 20 | 125 MB | 0.7 s | 1.1 s | 2.7 s |
| 64K | 32M | 10 | 20 MB | 0.6 s | 0.8 s | 1.7 s |
| 1M | 32M | 6 | 20 MB | 0.6 s | 0.7 s | 1.3 s |
How much the cache keeps from one pass to the next: the blocks those two scans asked for, without a YARA rule, replayed through each way of dropping blocks, and the blocks read from the source (1 MiB blocks):
| Object | Cache | Least recently used | Passes streamed through (now) | The fewest possible |
|---|---|---|---|---|
| 300 MB file | 8M | 865 | 853 | 845 |
| 300 MB file | 32M | 865 | 805 | 797 |
| 300 MB file | 64M | 865 | 741 | 733 |
| 20 MB ZIP | 8M | 124 | 92 | 82 |
| 20 MB ZIP | 16M | 122 | 52 | 40 |
| 20 MB ZIP | 32M | 20 | 20 | 20 |
Dropping the least recently used block throws out each block of a pass just before the next pass needs it, so on an object larger than the cache it keeps nothing from one pass to the next. Kept apart, a pass leaves all but two blocks in place.
On a local disk the block size changed nothing measurable (the operating system’s page cache absorbs it), so the defaults are chosen for the network. The tables measure round trips, not bandwidth: a server on the same host answered at several GB/s.
HTTP sources
Section titled “HTTP sources”An http(s):// target (--allow-http-scan, and SCANURL on the daemon) is
read by range requests, and written to disk only with --http-spill. It takes
the same two paths as a file, with the network in place of the disk:
- Up to
--max-object-bytes: fetched in full into memory, once. A 100 MB object cost 16 requests, 1.0 times its size on the wire, and a 226 MB peak. - Past it: read through the block cache, so every pass
over the file fetches it again. A 300 MB object, under a database with a hash
of that size, a gap signature and a pattern, cost 4.4 times its size on the
wire (1.33 GB in 176 requests). How many passes there are depends on the
database: detection and the signature sweep make two at least, and rules that
need more (YARA’s prefilter reads the file twice more) transfer more. So a
20 GB object is tens of gigabytes of egress or more. To pay in disk instead,
--http-spilldownloads it once into a spill file, up to--max-spill-bytes, and scans it from there; one that does not fit is read by range requests.--object-cache-block-bytes,--object-cache-bytesand--http-request-bytesset how many requests that takes.
What the source must and must not do:
- It should answer range requests with
206and aContent-Rangethat carries the total size. A server that answers with all of the object instead has it downloaded whole, into memory up to--max-object-bytesand into a spill file past it; with nowhere to spill it, the URL is an error. Past--max-input-bytesonly the bytes up to it are kept and the transfer stops there.--http-spilldoes not download an object past that limit either: its head is read by range requests. - Each request has a timeout of its own,
--http-timeout-secs(120 s, from connecting to the last byte of the body).--max-scan-secsbounds the scan as a whole (set by default for a daemon job, off for a one-shot run). - Up to
--http-max-redirects(10) redirects are followed, once, when the object is first asked for; later requests go where they led. A redirect to another scheme is not followed. A redirect to another host is refused if the host has a loopback, private, link-local, shared-address (100.64.0.0/10) or unspecified address, an IPv6 address that carries one of these included, unless--http-redirect-privateis set. The addresses are filtered where each connection is made, the first and every one after it, so a name whose answer changes between requests cannot lead the scan to a refused address. The URL itself is not filtered: a daemon with--allow-http-scanfetches any address a client names, including ones only it can reach, and the host it names is not filtered when a redirect leads back to it. Behind a proxy named by the environment (HTTPS_PROXY), the proxy looks the host up, and the filter sees only an address written out in the redirect. - The object is checked for change while it is read. Each answer must give the
range asked for of an object of the size the first one gave, or it is an
error. When the first answer has a strong
ETag, or failing that aLast-Modified, every request after it carries that asIf-Range: a server that honours it answers a changed object with all of it, which is an error, not a scan, and an answer that carries anotherETagthan the first is one too. A weakETagis not used. A server that gives neither header, or ignoresIf-Rangeand sends noETag, is held to the object’s size and no further: a change that keeps the size goes unseen. For such a server, download the object once and scan the file.
Archive members
Section titled “Archive members”Archives decoded as they are read (ZIP, tar, CAB, ISO/UDF, LHA, ar, cpio, 7z,
WIM, xar, the disk images and filesystems, the single-stream compressors,
self-extracting executables) are walked member by member at any size and any
depth. Such a member is held in memory up to the same limit. Past it, a member
stored as it stands in the archive (a ZIP member with no compression, a tar,
ar, cpio, ISO, RPM, OneNote, TNEF or fat Mach-O member, a self-extractor’s
payload) is read where it lies, through the archive’s own reads: no copy, no
disk, and --max-spill-bytes does not apply. --max-matcher-bytes and
--max-scan-secs still count it. It is read over a file or HTTP alike, and an
archive inside it is opened there too. A compressed
member past the limit is written to a temporary file and scanned from there;
with --spill-dir off, or past --max-spill-bytes, only its first
--max-object-bytes bytes are scanned and the file is LIMITS-EXCEEDED unless
they hold a detection. A stored member past the limit whose size runs past
the end of its archive is scanned as far as the archive goes and reported
UNSCANNABLE.
A member that has to be decoded whole (a decrypted ZIP member, a file HFS+ or
APFS keeps compressed in its attribute, a member of a format read whole) is
left out past --max-object-bytes, and the other members of its container are
still scanned; the file is LIMITS-EXCEEDED unless something is found.
Stdin, INSTREAM and ICAP bodies
Section titled “Stdin, INSTREAM and ICAP bodies”Container formats need to seek (a ZIP’s directory is at its end), so a stream is
buffered before it is scanned: in memory up to --spill-threshold-bytes
(16 MiB), then in a temporary file up to --max-spill-bytes (2 GiB). The
buffered stream then goes through the same scan as a file. A stream past
--max-input-bytes or the spill ceiling is scanned as far as it was held and is
LIMITS-EXCEEDED unless that finds something, as a file past
--max-input-bytes is. See
buffering a stream.
Spill files
Section titled “Spill files”The library never writes to disk itself: the host hands a scan somewhere to
spill (the exav binary passes its --spill-dir files and budgets). Only a
member decoded as it is read can spill; one decoded whole is bounded by
--max-object-bytes, and one stored as it stands needs no spill. Without a
spill, a compressed member that decodes past
--max-object-bytes has only its first --max-object-bytes bytes scanned, the
normalised views of a text file that
large are skipped, and a scan that found nothing is LIMITS-EXCEEDED rather
than OK.
The database, not the file, is what uses memory
Section titled “The database, not the file, is what uses memory”Building the signature index from raw ClamAV databases briefly takes more memory
than the index itself. A prebuilt .exavdb
is built once on a capable host and loads at about its final size.
Tuning
Section titled “Tuning”--max-object-bytes (256M) is the most memory one object may use, a file, a
decompressed member, an LZ window or a decrypted blob; several such buffers are
alive at once across nesting levels, so it does not bound the total. What
formats decoded whole hold across one top-level file is charged against 1G, and
--max-process-bytes keeps both inside the memory a scan gets. Every bound, and
which one to change, is in Limits and tuning; a
worked example is in Sizing a server.