#373 Two infinite loops (100% CPU hang) on crafted files: GURL::beautify_path() and DjVuMessageLite::InsertArg()
Summary
Two independent infinite loops can be triggered by a crafted DjVu file. In both cases the process spins at 100% CPU and never returns. Both loops have been in the code since the initial import, and they also occur on git master 4f2359a. They are separate from #372. I found them in the same libFuzzer campaign over the public ddjvuapi.
Reproduced with the unmodified Debian/Kali libdjvulibre21 3.5.30-1 (a simple ddjvuapi client hangs at about 98% CPU) and with evince-thumbnailer 49~alpha.1-2.
A. GURL::beautify_path(): path that starts with /../
In the /../ loop, if ptr == start there is no slash before it, so nothing is collapsed and strstr() returns the same position forever:
while((ptr=strstr(start, "/../")))
{
for(char * ptr1=ptr-1;(ptr1>=start);ptr1--)
if (*ptr1==slash) { collapse(ptr1, ptr-ptr1+3); break; }
}
Reached from a file: a single-page document with an NDIR chunk whose entry is ../../..(64 times)x.djvu. The call chain is DjVuDocument::init_thread() -> DjVuFile::decode_ndir() -> DjVuNavDir::decode() -> GURL::UTF8(name, baseURL) -> beautify_path(). This happens while the document is opened. poc_ndir_dotdot_hang.djvu (attached) is a stock single-page file with only that chunk added. evince-thumbnailer hangs on it indefinitely; its 15 s watchdog does not stop it.
B. DjVuMessageLite::InsertArg(): %0! without a closing !
const int format_end=message.search((unsigned long)'!',n);
if(format_end > format_start) { ... replace ... }
format_start=message.search((const char*)target, format_start+arg.length());
If the message contains %0! and no later !, nothing is replaced. The final pass uses ArgId 0 with an empty msg_number, so the search restarts at the same position forever. Strings from the file are pasted into the message before that pass, so a file-controlled %0! is enough.
Reached from a file: a bundled document whose first page has INCL with the id missing%0!. The lookup fails, and the error goes through DjVuPortcaster::notify_error() -> msg_prep_error() -> DjVuMessageLite::LookUp() -> InsertArg(..., 0, ""). poc_incl_msgformat_hang.djvu is a stock 4-page bundled file with one chunk renamed to that INCL (same size, so the DIRM is unchanged). poc_fuzzer_min_msgformat_200b.djvu is the fuzzer-minimized input.
Possible fixes (fix_both_infinite_loops.diff, attached)
- A: if no slash precedes
/../, drop it withcollapse(ptr, 3). - B: advance by at least one character:
format_start + (arg.length() ? arg.length() : 1).
With both changes, all 25 hanging inputs the fuzzer produced finish normally, and the stock sample documents still decode.
Impact
Denial of service only (CPU exhaustion). There is no memory corruption. A viewer freezes on open, and a thumbnailer may hang while a file manager browses the folder. I would like to request CVE IDs for these through VulnCheck, as for #372. I am handling the disclosure with you myself and will keep the details private until a fix is available.
Yamid A. González T. (Ya4id)
Fixed in git head.