dropbox / lepton

Lepton is a tool and file format for losslessly compressing JPEGs by an average of 22%.
https://blogs.dropbox.com/tech/2016/07/lepton-image-compression-saving-22-losslessly-from-images-at-15mbs/
Apache License 2.0
5.01k stars 355 forks source link

AddressSanitizer: heap-buffer-overflow #144

Closed giantbranch closed 3 years ago

giantbranch commented 3 years ago

Credit: giantbranch of NSFOCUS Security Team

What's the problem?

A heap buffer overflow write in lepton(https://github.com/dropbox/lepton/commit/3d1bc19da9f13a6e817938afd0f61a81110be4da).

ASAN reports:

$ ./lepton -unjailed ./poc_lepton103.jpg
lepton v1.0-3d1bc19
START ACHIEVED 1605510405 108011
TS_MAIN (0) 0.000000
TS_MODEL_INIT_BEGIN (0) 0.023684
TS_MODEL_INIT (0) 0.028371
TS_THREAD_STARTED (0) 0.021059
TS_READ_STARTED (0) 0.021163
TS_READ_FINISHED  (0) 0.021829
TS_JPEG_DECODE_STARTED  (0) 0.021829
TS_JPEG_DECODE_FINISHED (0) 0.022058
TS_STREAM_MULTIPLEX_STARTED (0) 0.028561
TS_STREAM_MULTIPLEX_FINISHED  (0) 0.028690
TS_ARITH_STARTED  (0) 0.023680
TS_ARITH_FINISHED (0) 0.028540
TS_STREAM_FLUSH_STARTED (0) 0.028690
TS_STREAM_FLUSH_FINISHED  (0) 0.028693
TS_DONE (0) 0.029254
6647943 bytes needed to decompress this file
226 213
106.10%
START ACHIEVED 1605510405 118279
=================================================================
==9435==ERROR: AddressSanitizer: heap-buffer-overflow on address 0x6050000001b2 at pc 0x7f57e83b3e6d bp 0x7fff945a2a50 sp 0x7fff945a21f8
WRITE of size 4 at 0x6050000001b2 thread T0
    #0 0x7f57e83b3e6c  (/usr/lib/x86_64-linux-gnu/libasan.so.5+0x66e6c)
    #1 0x451983 in read_ujpg() src/lepton/jpgcoder.cc:4277
    #2 0x46c0b6 in std::_Function_handler<bool (), bool (*)()>::_M_invoke(std::_Any_data const&) /usr/include/c++/9/bits/std_function.h:285
    #3 0x465b91 in std::function<bool ()>::operator()() const (/root/fuzz/lepton/lepton+0x465b91)
    #4 0x43c737 in execute(std::function<bool ()> const&) src/lepton/jpgcoder.cc:2048
    #5 0x43a8f8 in process_file(IOUtil::FileReader*, IOUtil::FileWriter*, int, bool) src/lepton/jpgcoder.cc:1862
    #6 0x432cbe in app_main(int, char**) src/lepton/jpgcoder.cc:941
    #7 0x48721a in main src/lepton/main.cc:17
    #8 0x7f57e769a83f in __libc_start_main (/lib/x86_64-linux-gnu/libc.so.6+0x2083f)
    #9 0x4047b8 in _start (/root/fuzz/lepton/lepton+0x4047b8)

0x6050000001b2 is located 0 bytes to the right of 18-byte region [0x6050000001a0,0x6050000001b2)
allocated by thread T0 here:
    #0 0x7f57e8459f3d in posix_memalign (/usr/lib/x86_64-linux-gnu/libasan.so.5+0x10cf3d)
    #1 0x4ea100 in custom_malloc src/vp8/util/memory.cc:68
    #2 0x408cda in aligned_alloc(unsigned long) src/lepton/bitops.cc:112
    #3 0x451963 in read_ujpg() src/lepton/jpgcoder.cc:4276
    #4 0x46c0b6 in std::_Function_handler<bool (), bool (*)()>::_M_invoke(std::_Any_data const&) /usr/include/c++/9/bits/std_function.h:285
    #5 0x465b91 in std::function<bool ()>::operator()() const (/root/fuzz/lepton/lepton+0x465b91)
    #6 0x43c737 in execute(std::function<bool ()> const&) src/lepton/jpgcoder.cc:2048
    #7 0x43a8f8 in process_file(IOUtil::FileReader*, IOUtil::FileWriter*, int, bool) src/lepton/jpgcoder.cc:1862
    #8 0x432cbe in app_main(int, char**) src/lepton/jpgcoder.cc:941
    #9 0x48721a in main src/lepton/main.cc:17
    #10 0x7f57e769a83f in __libc_start_main (/lib/x86_64-linux-gnu/libc.so.6+0x2083f)

SUMMARY: AddressSanitizer: heap-buffer-overflow (/usr/lib/x86_64-linux-gnu/libasan.so.5+0x66e6c)
Shadow bytes around the buggy address:
  0x0c0a7fff7fe0: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
  0x0c0a7fff7ff0: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
  0x0c0a7fff8000: fa fa fa fa 00 00 00 fa fa fa fa fa 00 00 00 fa
  0x0c0a7fff8010: fa fa fa fa fa fa fa fa 00 00 00 fa fa fa fa fa
  0x0c0a7fff8020: fd fd fd fa fa fa fa fa fa fa fa fa 00 00 fa fa
=>0x0c0a7fff8030: fa fa fa fa 00 00[02]fa fa fa fa fa fa fa fa fa
  0x0c0a7fff8040: fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa
  0x0c0a7fff8050: fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa
  0x0c0a7fff8060: fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa
  0x0c0a7fff8070: fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa
  0x0c0a7fff8080: fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa
Shadow byte legend (one shadow byte represents 8 application bytes):
  Addressable:           00
  Partially addressable: 01 02 03 04 05 06 07
  Heap left redzone:       fa
  Freed heap region:       fd
  Stack left redzone:      f1
  Stack mid redzone:       f2
  Stack right redzone:     f3
  Stack after return:      f5
  Stack use after scope:   f8
  Global redzone:          f9
  Global init order:       f6
  Poisoned by user:        f7
  Container overflow:      fc
  Array cookie:            ac
  Intra object redzone:    bb
  ASan internal:           fe
  Left alloca redzone:     ca
  Right alloca redzone:    cb
  Shadow gap:              cc
==9435==ABORTING
Input Size 213 != Roundtrip Size 0
a3f5910646549712dcfd00be41c737bc != d41d8cd98f00b204e9800998ecf8427e
ROUNDTRIP_FAILURE

I set a watchpoint,and then run and continue

watch *0x6050000001b2
r -unjailed ./poc_lepton103.jpg
c

image and then see the stack,may be the problem is makeEncoder function image

How can we reproduce the issue?

Compile command I use:

export CC="gcc-9"
export CXX="g++-9"
export CFLAGS="-g -fsanitize=address"
export CXXFLAGS="-g -fsanitize=address"
./autogen.sh && ./configure && make

reproduce the issue

unzip poc_lepton103.zip
./lepton -unjailed ./poc_lepton103.jpg

poc: poc_lepton103.zip

the details about my environment.