Report privately through GitHub: the repository's Security tab, Report a vulnerability.
Please do not open a public issue for a vulnerability. Include the input that triggers it (or
how to build it), the call and Options used, and the module path and version from go.mod.
softmagic is pre-1.0. Fixes go into the latest minor release only; there are no backports.
The module is published as code.eitanity.com/softmagic from v0.4.1. Earlier releases were
published as github.com/eitanity/softmagic and stay available under that path, but fixes are
released under the new path only: to receive them, replace the old path with the new one in
your imports and go.mod.
| Version | Module path | Supported |
|---|---|---|
| 0.4.1 and later 0.4.x | code.eitanity.com/softmagic |
yes |
| 0.4.0 | github.com/eitanity/softmagic |
no: move to code.eitanity.com/softmagic |
| < 0.4 | github.com/eitanity/softmagic |
no |
| Input | Trust | Why |
|---|---|---|
The bytes being identified (Identify* data, an io.ReaderAt) |
untrusted | the reason the library exists; every per-file cost is limited |
Magic rules (compile.Compile, CompileSources, Join) |
trusted, like code | rules choose the answer for every file |
A compiled database (Load, LoadEager) |
trusted for its answers, not for safety | it chooses the answer for every file, like rules; but Load refuses any record Compile could not have produced, so its bytes can come from anywhere |
The embedded database (Default) |
trusted | built from the vendored file 5.48 Magdir in this repository |
A rule can only read the input and write the answer: it cannot open a file, start a process or
use the network. Load checks the format and structure, the type, relation, strength and
printf conversion of every record (a conversion's width is at most 1024, as for compiled rules),
which set each entry belongs to, and that every regex compiles. FuzzLoad mutates whole
compiled databases and FuzzLoadFields sets any field of any record to any value.
The library itself has no unsafe, cgo, os/exec or network code, and links no module outside
the standard library.
The description contains text from the input. A rule may print a string it read from the
file (a title, a name, a version) or a single byte of it (%c). Unless Options.Raw is set,
every answer goes through file's output escaping: a character glibc's iswprint rejects is
written as \ooo, so control characters and terminal escapes do not survive. Printable
characters do: <script>, quotes, a convincing type name, and Unicode that glibc counts as
printable (bidi controls such as U+202E among it) are passed through unchanged, as file passes
them. Failure.Buffer and Failure.Text are not escaped, as file prints its errors.
Encode Description and the Continued lists for wherever they go: HTML,
SQL, a shell command, a log line, a spreadsheet cell. Or set Options.SafeText: every byte the
file contributes is then \xHH unless it is printable ASCII other than \ < > & " ' and the
backquote, and every answer and failure buffer is printable ASCII, at the cost of no longer being
file's exact text. Do not set Options.Raw for untrusted input.
The answer is not an allow-list on its own. softmagic gives the answer file 5.48 gives.
The first strong match wins, and only the first Options.MaxBytes (7 MiB by default) are
consulted, so a file can be built to be two things at once or to hide content past the window.
Use the answer to route a file, not to admit it. When a decision depends on it:
- check
Result.Examined.Truncated: when it is set, a limit or the deadline cut the match short and the answer may be incomplete; - check
Result.Failures: a limit that is an error infile(indirect, name, ELF note size) is reported there; - confirm the type with a parser for that format before trusting the content.
Give every call a deadline. Identify(data) has no deadline. For untrusted input call
IdentifyWith or IdentifyAt with a context that has one; when it expires the call stops and
Examined.Truncated is "time". The deadline is checked every 64 rule steps and every 64 KiB
of input that a single step searches, matches, parses, classifies or reads, so a search over a
large window, the JSON and CSV detectors, the text checks and reads past the window stop part
way. A run of whitespace or digits in JSON, and one pass of the encoding check (at most
Limits.Encoding bytes), run to their end before the next check. Each rule step is also
bounded by the limits below.
Bound concurrency. A Database is safe for concurrent use. Each call in flight holds a
pooled scan with the MaxBytes window, about six times the encoding window (384 KiB at the
default 64 KiB), and at most Options.MaxMemory (64 MiB by default) for data read past the
window: Composite Document File tables and streams, ELF data and the file's tail. A scan that
held more than 8 MiB of that, or a window over 8 MiB, drops it on returning to the pool. Limit
the number of calls in flight to bound memory.
A ReaderAt can stall a call. IdentifyAt reads through the caller's io.ReaderAt, and a
read already in progress is not interrupted by the context. Give the reader its own timeout if
it can block.
A panic is a defect and is not recovered. The release build has no deliberate panic: a
panic means a bounds check in softmagic is wrong, and it is left to surface with its stack
rather than be turned into an answer. Please report it with the input. A caller that must keep
serving may recover around the call itself; it should then use an eager database
(DefaultEager, LoadEager or compile.Compile), which has no lazily built state for an
interrupted call to leave behind.
These are file's limits, with its defaults, and two of the library's own. Options.Limits,
Options.MaxBytes and Options.MaxMemory change them; zero means the default.
| Limit | Default |
|---|---|
bytes consulted (MaxBytes) |
7 MiB |
| indirect offsets per match | 50 |
nested use (name) |
150 |
use/indirect frames |
32 |
| regex region | 8192 bytes |
| encoding window | 64 KiB |
| ELF program headers, sections, notes, note size | file 5.48's values |
| answer length | 4096 bytes |
memory for data read past the window (MaxMemory, not in file) |
64 MiB |
one Composite Document File sector chain (not in file) |
10000 × 4096 bytes |
go.mod requires the linters and vulnerability scanner that make lint runs, as tool
dependencies. None of them is linked into the library or a program that uses it: go version -m
on such a binary lists code.eitanity.com/softmagic as its only dependency. They do appear in
a consumer's module graph (go list -m all).
make test runs every test with assertions on and the race detector. make lint runs go vet,
staticcheck, govulncheck, gosec and golangci-lint. Fuzzers cover identification from a slice and
from a reader (FuzzIdentify, FuzzIdentifyAt), the rule compiler with joined and reloaded
databases (FuzzCompile), the compiled-database decoder (FuzzLoad, FuzzLoadFields), and
files built from typed fields, so that offsets, sizes and lengths reach their boundary values:
ELF headers and tables (FuzzELF), Composite Document File headers and chains (FuzzCDF),
nested ASN.1 DER lengths (FuzzDER) and length-prefixed strings (FuzzPString). FuzzRule
builds a rule from typed fields (type, offset form, range, flags, value) and runs it on fuzzed
input; it also checks that a numeral past 64 bits reads as file reads it, and that rules run
again through indirect answer as they do over the same bytes at the start of the file.
make fuzz runs each of them on amd64 and 386, with assertions on and as released, and every
fuzzed call fails if it allocates more than 64 MiB or takes more than 10 seconds (a bound
that catches a hang, set for slow CI runners). Beyond
FuzzRule's two comparisons, the fuzz targets check that nothing fails, not that the answer is
file's. softmagic-cli's make differential checks that: it gives every case in the fuzz corpora (seeds, testdata/fuzz
and the fuzz cache) to softmagic and to file 5.48 and lists each difference in their output.