libmagic: The Blathering
Summary: Trail of Bits reimplemented libmagic, the C library behind the file command, in pure Python as PolyFile and documented undocumented DSL features and bugs found along the way. Among them: an ambiguous MSDOS indirect-offset expression, an undocumented ${x?true:false} ternary in the elf pattern, an undocumented regex /b modifier in the CAD specification, and an ASF GUID definition two bytes short of RFC 4122 that was present in libmagic from December 2019 until reported in April 2022.
Key Takeaway: libmagic is older than over half the human population and sits in the 99.9th percentile of most frequently installed Ubuntu packages, yet its manually written C parsers for formats like JSON and CSV have produced memory safety bugs and numerous CVEs. A file-identification library that old, that widely installed, and still receiving breaking changes is a large and lightly examined part of the attack surface.
Read the original at Trail of Bits Blog ↗