Skip to content

Fix Heap Buffer Overflow in Scene Change Detection causes Out-of-Bounds Read - #4031

Open
PGZXB wants to merge 1 commit into
cisco:masterfrom
PGZXB:fix-issue-3926
Open

PGZXB wants to merge 1 commit into
cisco:masterfrom
PGZXB:fix-issue-3926

Conversation

@PGZXB

@PGZXB PGZXB commented Sep 24, 2026 •

Copy link
Copy Markdown

Fixes #3926

Fixes the memory-safety issue reported in #3926: Heap Buffer Overflow in Scene Change Detection causes Out-of-Bounds Read.

Fix

Adds the missing bounds/validity check at the faulting site.

diff --git a/codec/processing/src/scenechangedetection/SceneChangeDetection.h b/codec/processing/src/scenechangedetection/SceneChangeDetection.h
index f076851..98e1dcd 100644
--- a/codec/processing/src/scenechangedetection/SceneChangeDetection.h
+++ b/codec/processing/src/scenechangedetection/SceneChangeDetection.h
@@ -225,6 +225,16 @@ class CSceneChangeDetection : public IStrategy {
     m_sLocalParam.iCurStride = pSrcPixMap->iStride[0];
     m_sLocalParam.pStaticBlockIdc = m_sSceneChangeParam.pStaticBlockIdc;
 
+    // The 8x8 block grid used below is derived from the source (current) frame,
+    // while the SAD calculation reads from the reference/destination buffer.
+    // If that buffer is too small to hold the source-sized grid, the SAD loop
+    // in CSceneChangeDetectorVideo::operator() would read out of bounds.
+    // Reject such mismatched inputs here.
+    if (((m_sLocalParam.iBlock8x8Width << 3) > pRefPixMap->iStride[0])
+        || ((m_sLocalParam.iBlock8x8Height << 3) > pRefPixMap->sRect.iRectHeight)) {
+      return RET_INVALIDPARAM;
+    }
+
     int32_t iBlock8x8Num = m_sLocalParam.iBlock8x8Width * m_sLocalParam.iBlock8x8Height;
     int32_t iSceneChangeThresholdLarge = WelsStaticCast (int32_t,
                                          m_cDetector.GetSceneChangeMotionRatioLarge() * iBlock8x8Num + 0.5f + PESN);

Verification Before Fix

908ebb047ec78901b8d4a7fbad63f95504f)

0x524000003f60 is located 0 bytes after 7776-byte region [0x524000002100,0x524000003f60)
allocated by thread T0 here:
    #0 0x5652a40af761 in operator new[](unsigned long, std::nothrow_t const&) (/new_issue/poc_driver+0x141761) (BuildId: 0737a908ebb047ec78901b8d4a7fbad63f95504f)
    #1 0x5652a40b1acd in LLVMFuzzerTestOneInput /new_issue/poc/issue_fuzzer.cpp:33:24
    #2 0x5652a3fbccf4 in fuzzer::Fuzzer::ExecuteCallback(unsigned char const*, unsigned long) (/new_issue/poc_driver+0x4ecf4) (BuildId: 0737a908ebb047ec78901b8d4a7fbad63f95504f)
    #3 0x5652a3fa5e26 in fuzzer::RunOneTest(fuzzer::Fuzzer*, char const*, unsigned long) (/new_issue/poc_driver+0x37e26) (BuildId: 0737a908ebb047ec78901b8d4a7fbad63f95504f)
    #4 0x5652a3fab8da in fuzzer::FuzzerDriver(int*, char***, int (*)(unsigned char const*, unsigned long)) (/new_issue/poc_driver+0x3d8da) (BuildId: 0737a908ebb047ec78901b8d4a7fbad63f95504f)
    #5 0x5652a3fd6096 in main (/new_issue/poc_driver+0x68096) (BuildId: 0737a908ebb047ec78901b8d4a7fbad63f95504f)
    #6 0x7fcf0a30c1c9  (/lib/x86_64-linux-gnu/libc.so.6+0x2a1c9) (BuildId: a4a7992a8e66555c8141ab2a08a8465ff6e0ea65)
    #7 0x7fcf0a30c28a in __libc_start_main (/lib/x86_64-linux-gnu/libc.so.6+0x2a28a) (BuildId: a4a7992a8e66555c8141ab2a08a8465ff6e0ea65)
    #8 0x5652a3fa09f4 in _start (/new_issue/poc_driver+0x329f4) (BuildId: 0737a908ebb047ec78901b8d4a7fbad63f95504f)

SUMMARY: AddressSanitizer: heap-buffer-overflow /src/openh264/codec/common/src/sad_common.cpp:82:16 in WelsSampleSad8x8_c(unsigned char*, int, unsigned char*, int)
Shadow bytes around the buggy address:
  0x524000003c80: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
  0x524000003d00: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
  0x524000003d80: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
  0x524000003e00: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
  0x524000003e80: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
=>0x524000003f00: 00 00 00 00 00 00 00 00 00 00 00 00[fa]fa fa fa
  0x524000003f80: fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa
  0x524000004000: fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa
  0x524000004080: fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa
  0x524000004100: fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa
  0x524000004180: 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
==83==ABORTING

Verification After Fix

INFO: Running with entropic power schedule (0xFF, 100).
INFO: Seed: 2878439475
INFO: Loaded 1 modules   (18 inline 8-bit counters): 18 [0x55f7da49ff10, 0x55f7da49ff22), 
INFO: Loaded 1 PC tables (18 PCs): 18 [0x55f7da49ff28,0x55f7da4a0048), 
/new_issue/poc_driver: Running 1 inputs 1 time(s) each.
Running: /new_issue/poc/crash_input.bin
Executed /new_issue/poc/crash_input.bin in 2 ms
***
*** NOTE: fuzzing was not performed, you have only
***       executed the target code on a fixed set of inputs.
***

@PGZXB PGZXB changed the title Fix Heap Buffer Overflow in Scene Change Detection causes Out-of-Bounds Read (#3926) Fix Heap Buffer Overflow in Scene Change Detection causes Out-of-Bounds Read Sep 24, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Heap Buffer Overflow in Scene Change Detection causes Out-of-Bounds Read

1 participant