Menu ▾ ▴

#372 Stack overflow (infinite recursion) in DjVuDocument::process_threqs() when a thumbnail chunk is corrupt

djvulibre
closed
nobody
None
5
13 hours ago
16 hours ago
No

Summary

When a client asks for the thumbnail of a page in a bundled document whose thumbnail component is corrupt, DjVuDocument::process_threqs() re-enters itself until the stack is exhausted, and the process dies with SIGSEGV. The entry point is ddjvu_thumbnail_status(doc, pageno, TRUE). The DjVu backends of Evince, Papers, Atril and Xreader call it, and so does evince-thumbnailer, which file managers run automatically on DjVu files.

Affected versions

All releases up to 3.5.30, plus git master 4f2359a. The code involved dates back to the initial import.

Reproduced with:

  • Debian/Kali libdjvulibre21 3.5.30-1 and evince-thumbnailer 49~alpha.1-2 (unmodified distribution packages): segmentation fault
  • git master 4f2359a built with AddressSanitizer: stack-overflow

Reproducer

poc_corrupt_th44_chunk.djvu (attached) is an ordinary 4-page bundled document. I made it with cjb2, c44, cpaldjvu and djvm, then ran djvused -e 'set-thumbnails 32'. The only modification is that the ID of the first TH44 chunk was renamed to XH44. All pages still render normally with ddjvu.

$ evince-thumbnailer -s 128 poc_corrupt_th44_chunk.djvu out.png
Segmentation fault

repro_thumb.c (attached) does the same through the ddjvuapi calls the Evince backend uses: ddjvu_document_create_by_filename(), then ddjvu_thumbnail_status(doc, 0, TRUE) in a message loop, then ddjvu_thumbnail_render(). poc_fuzzer_min_120b.djvu is the fuzzer-minimized input: a DJVM directory with no component data. It crashes the same way.

Analysis

When the FORM:THUM/TH44 extraction in process_threqs() throws, the catch handler calls get_djvu_file() before clearing req->thumb_file:

} G_CATCH(exc) {
  ...
  req->image_file=get_djvu_file(req->page_num);
  req->thumb_file=0;

get_djvu_file() creates a DjVuFile whose data is already present, so the trigger fires synchronously:

DjVuFile::create -> DjVuFile::init -> DataPool::add_trigger
  -> DjVuFile::static_trigger_cb -> DjVuFile::trigger_cb
  -> DjVuPortcaster::notify_file_flags_changed
  -> DjVuDocument::notify_file_flags_changed -> process_threqs()

The critical section is recursive, so the nested process_threqs() runs, finds the same request with thumb_file still set, fails in the same way, and calls get_djvu_file() again. In evince-thumbnailer the stack ran out after about 26,800 frames.

This is not the INCL recursion of CVE-2021-3500. The reproducer contains no INCL chunk, and master already has cd8b5c9 and fbb34d0.

Possible fix

Clearing req->thumb_file before the call to get_djvu_file() in the catch handler stops the recursion. With that change, neither reproducer nor any of the 43 crashing inputs the fuzzer generated crashes any more. ddjvu_thumbnail_render() then returns 0: Evince's sidebar falls back to rendering the page, and evince-thumbnailer writes a blank thumbnail. A re-entrancy guard in process_threqs() would be an alternative.

Impact

As far as I can tell this is a denial of service only: the recursion hits the stack guard page, and I see no memory corruption. I have requested a CVE ID through VulnCheck (CVE ID only; I am handling the disclosure with you myself). I am happy to keep the details private until a fix is available.

Found by fuzzing the public ddjvuapi with libFuzzer and ASan.

Yamid A. González T. (Ya4id)

3 Attachments

Discussion

  • Leon Bottou

    Leon Bottou - 13 hours ago
    • status: open --> closed
    • private: Yes --> No
     
  • Leon Bottou

    Leon Bottou - 13 hours ago

    Fixed in git head (as suggested to backoff to rendering the thumbnails.)

     

Log in to post a comment.