November 29, 2026 by debugactiveprocess6 minutes
When YARA-X runs inside a service rather than on your laptop, it receives three very different kinds of input: the files it scans, the rule source it compiles, and sometimes a compiled rule set that was serialized somewhere else. People tend to reason about all three as “input”, and then protect the wrong one.
They are not equivalent. One of them is assumed to be hostile, one of them is bounded, and one of them is trusted completely. This post is about which is which, and what that means for the way you wire YARA-X into something.
This is the assumption the engine makes, and the last three releases are mostly the work of making it true. Of the 61 entries in the release notes for 1.23.0, 1.24.0 and 1.25.0, 45 are bug fixes, and 21 of those are specifically about surviving input that was built to break the scanner rather than to be scanned.
A few of them give the flavour. Version 1.24.0 hardened the crx, olecf,
authenticode, pe, dotnet, elf, macho and dex parsers in a single
release against excessive memory allocation, unbounded recursion and
algorithmic complexity attacks. Version 1.25.0 capped decompressed ZIP entries
at 64 MiB so that a decompression bomb inside an Office document cannot exhaust
memory through the vba module, and bounded the CPU that Authenticode
verification will spend on crafted signatures and countersignatures.
The practical consequence is the useful part: you do not need to pre-validate samples. You do not need to sniff the file type before choosing which modules to enable, or refuse archives because they might be bombs, or cap file sizes out of fear of the parsers. That work has been done inside the engine, where it belongs, and it keeps being done. If you find an input that breaks a module, that is a bug worth reporting, not a gap you are expected to cover in your own code.
Letting users write rules that your service compiles is a different question, and the honest answer is that it is defensible, because the ways it can go wrong now have names and numbers.
Nesting is the clearest example. A deeply nested condition used to be a way to run the compiler out of stack. Today the abstract syntax tree is capped, and a rule past the limit is rejected as an ordinary error:
error[E001]: syntax error
--> deep.yar:1:319
= error: nesting limit reachedPattern expansion is bounded in the same spirit, with two different strategies
that are worth telling apart. Across the whole compiler there is a hard ceiling:
once the atoms extracted from all the rules would cross MAX_ATOMS, the rule
that crossed it is rolled back and rejected with E049. Within a single pattern
there is no ceiling, there is a budget. When a masked pattern would expand past
MAX_ATOMS_PER_REGEXP, which is 16384, the compiler narrows the mask and
settles for fewer, less specific atoms instead of refusing the pattern.
That second one is the reason a pattern like /[a-p][a-p][a-p][a-p][a-p]/
compiles without complaint even though a naive expansion of it is a million
atoms. You pay for it at scan time, in a weaker prefilter, not at compile time
in an error.
The include directive deserves its own mention, because it is the one feature
that turns rule text into filesystem access. Since 1.24.0 an include can only
resolve to a regular file inside a directory you configured, so enabling
includes does not hand the author of a rule a path traversal primitive. If you
do not need them at all, they can be turned off entirely with
enable_includes(false).
What these limits do not do is make compilation free. A rule can still be slow to compile, and a rule set can still be large. Bounding the damage is not the same as removing the cost, so compiling untrusted rules still belongs behind the same timeouts and quotas you would put around any other user-submitted work.
Here is the one that surprises people, and it is deliberate.
Rules::deserialize checks that the stream starts with the right magic bytes,
that the serialization version matches the running build, and that the structure
decodes. It then validates that the internal identifiers point inside the arrays
they index. What it does not do is treat the content as adversarial, and its own
documentation has said so for months:
As long as you are deserializing rules from binary content produced by
Rules::serializeyou are safe. But you should never attempt to do so from binary content that was produced or could be manipulated by a third party. This implies a security risk and your program may panic during scanning.
The reason is in what a compiled rule set actually contains. Rule conditions are compiled to WebAssembly, and that module travels inside the serialized bytes. On load it is parsed, which means it has to be a well formed WASM binary, but it is not checked against the host functions and globals it will be linked against when the scanner runs. A stream can decode perfectly and still be wrong in a way that only appears later, while scanning.
So the rule is simple, and it is worth saying in one line: do not deserialize
rules you did not produce. A .yarc file is closer to a shared library than to
data. You would not load someone else’s .so because it parsed as ELF.
This matters most for the pattern where it is easiest to get wrong, which is
distributing compiled rules to a fleet. Shipping .yarc files over the network
to machines that deserialize whatever arrives puts the integrity of those bytes
on the critical path. That is a code distribution problem, not a scanning
problem, and it wants the answers code distribution already has: sign them, or
compile from source on the machine that will use them. The engine will not do
that check for you, and it does not pretend to.
Samples are hostile, and handled. Rule source is semi-trusted, and bounded. Compiled rules are trusted, so the only question that matters is where they came from.
If you are embedding YARA-X, that is the whole decision. Spend your effort on the third one, because the engine has already spent its effort on the first.