brackeen/ok-file-formats

bugs found in ok_jpg_convert_data_unit_grayscale and ok_jpg_convert_YCbCr_to_RGB via honggfuzz

pprivulet opened this issue · 3 comments

Test code :

#include <stdio.h>
#include <stdlib.h>
#include "ok_jpg.h"


int main(int argc, char **argv) {
    if(argc<2) return 0;

    FILE *file = fopen(argv[1], "rb");

    ok_jpg image = ok_jpg_read(file, OK_JPG_COLOR_FORMAT_RGBA | OK_JPG_FLIP_Y);
    fclose(file);
    if (image.data) {
        printf("Got image! Size: %li x %li\n", (long)image.width, (long)image.height);
        free(image.data);
    }
    return 0;
}

Tools: honggfuzz 2.4

Target version: master-20210910

Result:

$ ./Testjpg bugs/0.fuzz 2>&1 | grep SUMMARY
SUMMARY: AddressSanitizer: heap-buffer-overflow /home/pan/security_lab/workspace/fuzz/target/ok-file-formats/ok_jpg.c:532 in ok_jpg_convert_data_unit_grayscale
$ ./Testjpg bugs/1.fuzz 2>&1 | grep SUMMARY
SUMMARY: AddressSanitizer: heap-buffer-overflow /home/pan/security_lab/workspace/fuzz/target/ok-file-formats/ok_jpg.c:532 in ok_jpg_convert_data_unit_grayscale
$ ./Testjpg bugs/2.fuzz 2>&1 | grep SUMMARY
SUMMARY: AddressSanitizer: heap-buffer-overflow /home/pan/security_lab/workspace/fuzz/target/ok-file-formats/ok_jpg.c:520 in ok_jpg_convert_YCbCr_to_RGB
$ ./Testjpg bugs/3.fuzz 2>&1 | grep SUMMARY
SUMMARY: AddressSanitizer: heap-buffer-overflow /home/pan/security_lab/workspace/fuzz/target/ok-file-formats/ok_jpg.c:520 in ok_jpg_convert_YCbCr_to_RGB
$ ./Testjpg bugs/4.fuzz 2>&1 | grep SUMMARY
SUMMARY: AddressSanitizer: heap-buffer-overflow /home/pan/security_lab/workspace/fuzz/target/ok-file-formats/ok_jpg.c:520 in ok_jpg_convert_YCbCr_to_RGB
$ ./Testjpg bugs/5.fuzz 2>&1 | grep SUMMARY
SUMMARY: AddressSanitizer: heap-buffer-overflow /home/pan/security_lab/workspace/fuzz/target/ok-file-formats/ok_jpg.c:520 in ok_jpg_convert_YCbCr_to_RGB
$ ./Testjpg bugs/6.fuzz 2>&1 | grep SUMMARY
SUMMARY: AddressSanitizer: heap-buffer-overflow /home/pan/security_lab/workspace/fuzz/target/ok-file-formats/ok_jpg.c:520 in ok_jpg_convert_YCbCr_to_RGB

Here are the poc

@pprivulet can you include the full ASAN output please?

Thank you @pprivulet. These were all caused by EXIF orientation info appearing after the SOF marker. The EXIF orientation info is now ignored if it appears after SOF.

Here are the asan outputs:

  1. ok_jpg_convert_data_unit_grayscale
$ ./Testjpg bugs/0.fuzz 
=================================================================
==26787==ERROR: AddressSanitizer: heap-buffer-overflow on address 0x7f6fdfefb7fc at pc 0x562455ef5043 bp 0x7ffefb83c380 sp 0x7ffefb83c370
WRITE of size 1 at 0x7f6fdfefb7fc thread T0
    #0 0x562455ef5042 in ok_jpg_convert_data_unit_grayscale /home/pan/security_lab/workspace/fuzz/target/ok-file-formats/ok_jpg.c:532
    #1 0x562455ef5042 in ok_jpg_convert_data_unit /home/pan/security_lab/workspace/fuzz/target/ok-file-formats/ok_jpg.c:612
    #2 0x562455eff1e2 in ok_jpg_progressive_finish /home/pan/security_lab/workspace/fuzz/target/ok-file-formats/ok_jpg.c:1336
    #3 0x562455eff1e2 in ok_jpg_decode2 /home/pan/security_lab/workspace/fuzz/target/ok-file-formats/ok_jpg.c:1942
    #4 0x562455eff1e2 in ok_jpg_decode /home/pan/security_lab/workspace/fuzz/target/ok-file-formats/ok_jpg.c:2021
    #5 0x562455f03591 in ok_jpg_read_with_allocator /home/pan/security_lab/workspace/fuzz/target/ok-file-formats/ok_jpg.c:268
    #6 0x562455f036c7 in ok_jpg_read /home/pan/security_lab/workspace/fuzz/target/ok-file-formats/ok_jpg.c:257
    #7 0x562455ef266c in main /home/pan/security_lab/workspace/fuzz/target/ok-file-formats/main.c:11
    #8 0x7f6fdea52bf6 in __libc_start_main (/lib/x86_64-linux-gnu/libc.so.6+0x21bf6)
    #9 0x562455ef24c9 in _start (/home/pan/security_lab/workspace/fuzz/target/ok-file-formats/Testjpg+0x14c9)

0x7f6fdfefb7fc is located 4 bytes to the left of 140800-byte region [0x7f6fdfefb800,0x7f6fdff1de00)
allocated by thread T0 here:
    #0 0x7f6fdef00b40 in __interceptor_malloc (/usr/lib/x86_64-linux-gnu/libasan.so.4+0xdeb40)
    #1 0x562455ef29b6 in ok_stdlib_alloc /home/pan/security_lab/workspace/fuzz/target/ok-file-formats/ok_jpg.c:55
    #2 0x562455efe05f in ok_jpg_read_sof /home/pan/security_lab/workspace/fuzz/target/ok-file-formats/ok_jpg.c:1626
    #3 0x562455efe05f in ok_jpg_decode2 /home/pan/security_lab/workspace/fuzz/target/ok-file-formats/ok_jpg.c:1925
    #4 0x562455efe05f in ok_jpg_decode /home/pan/security_lab/workspace/fuzz/target/ok-file-formats/ok_jpg.c:2021
    #5 0x562455f03591 in ok_jpg_read_with_allocator /home/pan/security_lab/workspace/fuzz/target/ok-file-formats/ok_jpg.c:268
    #6 0x562455f036c7 in ok_jpg_read /home/pan/security_lab/workspace/fuzz/target/ok-file-formats/ok_jpg.c:257
    #7 0x562455ef266c in main /home/pan/security_lab/workspace/fuzz/target/ok-file-formats/main.c:11
    #8 0x7f6fdea52bf6 in __libc_start_main (/lib/x86_64-linux-gnu/libc.so.6+0x21bf6)

SUMMARY: AddressSanitizer: heap-buffer-overflow /home/pan/security_lab/workspace/fuzz/target/ok-file-formats/ok_jpg.c:532 in ok_jpg_convert_data_unit_grayscale
Shadow bytes around the buggy address:
  0x0fee7bfd76a0: fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa
  0x0fee7bfd76b0: fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa
  0x0fee7bfd76c0: fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa
  0x0fee7bfd76d0: fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa
  0x0fee7bfd76e0: fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa
=>0x0fee7bfd76f0: fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa[fa]
  0x0fee7bfd7700: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
  0x0fee7bfd7710: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
  0x0fee7bfd7720: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
  0x0fee7bfd7730: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
  0x0fee7bfd7740: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
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
==26787==ABORTING
  1. ok_jpg_convert_YCbCr_to_RGB
$ ./Testjpg bugs/6.fuzz 
=================================================================
==30742==ERROR: AddressSanitizer: heap-buffer-overflow on address 0x7f4d4c1d37fc at pc 0x55c4c7b171d0 bp 0x7ffc3fb4b620 sp 0x7ffc3fb4b610
WRITE of size 1 at 0x7f4d4c1d37fc thread T0
    #0 0x55c4c7b171cf in ok_jpg_convert_YCbCr_to_RGB /home/pan/security_lab/workspace/fuzz/target/ok-file-formats/ok_jpg.c:520
    #1 0x55c4c7b171cf in ok_jpg_convert_data_unit_color /home/pan/security_lab/workspace/fuzz/target/ok-file-formats/ok_jpg.c:552
    #2 0x55c4c7b171cf in ok_jpg_convert_data_unit /home/pan/security_lab/workspace/fuzz/target/ok-file-formats/ok_jpg.c:614
    #3 0x55c4c7b23da0 in ok_jpg_decode_scan /home/pan/security_lab/workspace/fuzz/target/ok-file-formats/ok_jpg.c:1289
    #4 0x55c4c7b23da0 in ok_jpg_read_sos /home/pan/security_lab/workspace/fuzz/target/ok-file-formats/ok_jpg.c:1755
    #5 0x55c4c7b23da0 in ok_jpg_decode2 /home/pan/security_lab/workspace/fuzz/target/ok-file-formats/ok_jpg.c:1947
    #6 0x55c4c7b23da0 in ok_jpg_decode /home/pan/security_lab/workspace/fuzz/target/ok-file-formats/ok_jpg.c:2021
    #7 0x55c4c7b25591 in ok_jpg_read_with_allocator /home/pan/security_lab/workspace/fuzz/target/ok-file-formats/ok_jpg.c:268
    #8 0x55c4c7b256c7 in ok_jpg_read /home/pan/security_lab/workspace/fuzz/target/ok-file-formats/ok_jpg.c:257
    #9 0x55c4c7b1466c in main /home/pan/security_lab/workspace/fuzz/target/ok-file-formats/main.c:11
    #10 0x7f4d4f9bfbf6 in __libc_start_main (/lib/x86_64-linux-gnu/libc.so.6+0x21bf6)
    #11 0x55c4c7b144c9 in _start (/home/pan/security_lab/workspace/fuzz/target/ok-file-formats/Testjpg+0x14c9)

0x7f4d4c1d37fc is located 4 bytes to the left of 1228800-byte region [0x7f4d4c1d3800,0x7f4d4c2ff800)
allocated by thread T0 here:
    #0 0x7f4d4fe6db40 in __interceptor_malloc (/usr/lib/x86_64-linux-gnu/libasan.so.4+0xdeb40)
    #1 0x55c4c7b149b6 in ok_stdlib_alloc /home/pan/security_lab/workspace/fuzz/target/ok-file-formats/ok_jpg.c:55
    #2 0x55c4c7b2005f in ok_jpg_read_sof /home/pan/security_lab/workspace/fuzz/target/ok-file-formats/ok_jpg.c:1626
    #3 0x55c4c7b2005f in ok_jpg_decode2 /home/pan/security_lab/workspace/fuzz/target/ok-file-formats/ok_jpg.c:1925
    #4 0x55c4c7b2005f in ok_jpg_decode /home/pan/security_lab/workspace/fuzz/target/ok-file-formats/ok_jpg.c:2021
    #5 0x55c4c7b25591 in ok_jpg_read_with_allocator /home/pan/security_lab/workspace/fuzz/target/ok-file-formats/ok_jpg.c:268
    #6 0x55c4c7b256c7 in ok_jpg_read /home/pan/security_lab/workspace/fuzz/target/ok-file-formats/ok_jpg.c:257
    #7 0x55c4c7b1466c in main /home/pan/security_lab/workspace/fuzz/target/ok-file-formats/main.c:11
    #8 0x7f4d4f9bfbf6 in __libc_start_main (/lib/x86_64-linux-gnu/libc.so.6+0x21bf6)


SUMMARY: AddressSanitizer: heap-buffer-overflow /home/pan/security_lab/workspace/fuzz/target/ok-file-formats/ok_jpg.c:520 in ok_jpg_convert_YCbCr_to_RGB
Shadow bytes around the buggy address:
  0x0fea298326a0: fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa
  0x0fea298326b0: fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa
  0x0fea298326c0: fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa
  0x0fea298326d0: fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa
  0x0fea298326e0: fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa
=>0x0fea298326f0: fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa[fa]
  0x0fea29832700: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
  0x0fea29832710: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
  0x0fea29832720: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
  0x0fea29832730: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
  0x0fea29832740: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
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
==30742==ABORTING