Repository: mm2/little-cms
Fuzzing engine: libfuzzer
Fuzz target: cms_profile_fuzzer
Sanitizer: address
Crash type: Heap-buffer-overflow READ 2

Sanitizer report:
INFO: Running with entropic power schedule (0xFF, 100).
INFO: Seed: 3312627919
INFO: Loaded 1 modules   (7605 inline 8-bit counters): 7605 [0x56357f17cfd0, 0x56357f17ed85), 
INFO: Loaded 1 PC tables (7605 PCs): 7605 [0x56357f17ed88,0x56357f19c8d8), 
/out/cms_profile_fuzzer: Running 1 inputs 1 time(s) each.
Running: /testcase/OSV-2022-626.poc
=================================================================
==7==ERROR: AddressSanitizer: heap-buffer-overflow on address 0x5020000001f4 at pc 0x56357f0a8424 bp 0x7ffe3ed8c9d0 sp 0x7ffe3ed8c9c8
READ of size 2 at 0x5020000001f4 thread T0
SCARINESS: 14 (2-byte-read-heap-buffer-overflow)
    #0 0x56357f0a8423 in Write16bitTables /src/lcms/src/cmstypes.c:2081:19
    #1 0x56357f09ae00 in Type_LUT16_Write /src/lcms/src/cmstypes.c:2276:14
    #2 0x56357f06697c in SaveTags /src/lcms/src/cmsio0.c:1344:18
    #3 0x56357f065994 in cmsSaveProfileToIOhandler /src/lcms/src/cmsio0.c:1417:10
    #4 0x56357f066ea9 in cmsSaveProfileToFile /src/lcms/src/cmsio0.c:1458:11
    #5 0x56357f05fa1d in LLVMFuzzerTestOneInput /src/cms_profile_fuzzer.c:45:5
    #6 0x56357ef143a0 in fuzzer::Fuzzer::ExecuteCallback(unsigned char const*, unsigned long) /src/llvm-project/compiler-rt/lib/fuzzer/FuzzerLoop.cpp:614:13
    #7 0x56357eeff615 in fuzzer::RunOneTest(fuzzer::Fuzzer*, char const*, unsigned long) /src/llvm-project/compiler-rt/lib/fuzzer/FuzzerDriver.cpp:327:6
    #8 0x56357ef050af in fuzzer::FuzzerDriver(int*, char***, int (*)(unsigned char const*, unsigned long)) /src/llvm-project/compiler-rt/lib/fuzzer/FuzzerDriver.cpp:862:9
    #9 0x56357ef30352 in main /src/llvm-project/compiler-rt/lib/fuzzer/FuzzerMain.cpp:20:10
    #10 0x7f16d4447082 in __libc_start_main (/lib/x86_64-linux-gnu/libc.so.6+0x24082) (BuildId: 0702430aef5fa3dda43986563e9ffcc47efbd75e)
    #11 0x56357eef77fd in _start (/out/cms_profile_fuzzer+0x737fd)

DEDUP_TOKEN: Write16bitTables--Type_LUT16_Write--SaveTags
0x5020000001f4 is located 0 bytes after 4-byte region [0x5020000001f0,0x5020000001f4)
allocated by thread T0 here:
    #0 0x56357f02016f in malloc /src/llvm-project/compiler-rt/lib/asan/asan_malloc_linux.cpp:68:3
    #1 0x56357f0c3b65 in _cmsMalloc /src/lcms/src/cmserr.c:267:12
    #2 0x56357f0c3b65 in _cmsMallocZeroDefaultFn /src/lcms/src/cmserr.c:106:16
    #3 0x56357f0c699c in AllocateToneCurveStruct /src/lcms/src/cmsgamma.c:256:42
    #4 0x56357f0e51c6 in cmsStageAllocToneCurves /src/lcms/src/cmslut.c:280:38
    #5 0x56357f0a8cab in ReadSetOfCurves /src/lcms/src/cmstypes.c:2486:11
    #6 0x56357f09ffc8 in Type_LUTB2A_Read /src/lcms/src/cmstypes.c:2872:56
    #7 0x56357f068008 in cmsReadTag /src/lcms/src/cmsio0.c:1676:25
    #8 0x56357f05fa0e in LLVMFuzzerTestOneInput /src/cms_profile_fuzzer.c:42:5
    #9 0x56357ef143a0 in fuzzer::Fuzzer::ExecuteCallback(unsigned char const*, unsigned long) /src/llvm-project/compiler-rt/lib/fuzzer/FuzzerLoop.cpp:614:13
    #10 0x56357eeff615 in fuzzer::RunOneTest(fuzzer::Fuzzer*, char const*, unsigned long) /src/llvm-project/compiler-rt/lib/fuzzer/FuzzerDriver.cpp:327:6
    #11 0x56357ef050af in fuzzer::FuzzerDriver(int*, char***, int (*)(unsigned char const*, unsigned long)) /src/llvm-project/compiler-rt/lib/fuzzer/FuzzerDriver.cpp:862:9
    #12 0x56357ef30352 in main /src/llvm-project/compiler-rt/lib/fuzzer/FuzzerMain.cpp:20:10
    #13 0x7f16d4447082 in __libc_start_main (/lib/x86_64-linux-gnu/libc.so.6+0x24082) (BuildId: 0702430aef5fa3dda43986563e9ffcc47efbd75e)

DEDUP_TOKEN: __interceptor_malloc--_cmsMalloc--_cmsMallocZeroDefaultFn
SUMMARY: AddressSanitizer: heap-buffer-overflow /src/lcms/src/cmstypes.c:2081:19 in Write16bitTables
Shadow bytes around the buggy address:
  0x501fffffff00: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
  0x501fffffff80: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
  0x502000000000: fa fa 00 00 fa fa 00 fa fa fa 00 fa fa fa 00 fa
  0x502000000080: fa fa 00 07 fa fa fd fa fa fa fd fa fa fa fd fa
  0x502000000100: fa fa fd fa fa fa fd fa fa fa 00 00 fa fa 00 00
=>0x502000000180: fa fa 00 fa fa fa 00 fa fa fa 00 fa fa fa[04]fa
  0x502000000200: fa fa 00 fa fa fa 04 fa fa fa fa fa fa fa fa fa
  0x502000000280: fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa
  0x502000000300: fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa
  0x502000000380: fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa
  0x502000000400: 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
==7==ABORTING


Fix patch:
diff --git a/src/cmstypes.c b/src/cmstypes.c
index 47bc7a2..e056731 100644
--- a/src/cmstypes.c
+++ b/src/cmstypes.c
@@ -2071,11 +2071,11 @@ cmsBool Write16bitTables(cmsContext ContextID, cmsIOHANDLER* io, _cmsStageToneCu
     cmsUInt32Number nEntries;
 
     _cmsAssert(Tables != NULL);
-
-    nEntries = Tables->TheCurves[0]->nEntries;
-
+   
     for (i=0; i < Tables ->nCurves; i++) {
 
+        nEntries = Tables->TheCurves[i]->nEntries;
+
         for (j=0; j < nEntries; j++) {
 
             val = Tables->TheCurves[i]->Table16[j];