Skip to content

Fixed out-of-bounds token reads and urlDecode check in DcmJSONReader - #169

Open
lauyeehow1986-hub wants to merge 1 commit into
DCMTK:masterfrom
lauyeehow1986-hub:fix/dcmdata-dcjsonrd-token-oob
Open

lauyeehow1986-hub wants to merge 1 commit into
DCMTK:masterfrom
lauyeehow1986-hub:fix/dcmdata-dcjsonrd-token-oob

Conversation

@lauyeehow1986-hub

Copy link
Copy Markdown

Summary

Fixes an ASan heap-buffer-overflow read in DcmJSONReader (dcmdata/libsrc/dcjsonrd.cc) discovered by Cloud CI LibFuzzer testing (fuzz_dcmdata_json_roundtrip, Run 45116035219), together with related token-stream bounds issues and an inverted bounds check in DcmJSONReader::urlDecode().

Root Cause

  1. DcmJSONReader::readAndConvertJSONFile() ignored parseJSON() errors when stopOnErrorPolicy_ is OFFalse (default): After a JSMN parse failure, readAndConvertJSONFile() continued traversing tokenArray_ using incomplete/zero-initialized tokens.
  2. Unchecked tokenArray_ advancement across JSMN consumers: reserveTokens() allocates tokenNumber_ + 1 tokens with a single sentinel (tokenArray_[tokenNumber_]), while parseElement(), parseSequence(), parseDataSet(), parsePersonName(), parseElementValueArray(), getTokenContent(), and readAndConvertJSONFile() incremented current++ multiple times without validating pointer bounds against [tokenArray_, tokenArray_ + tokenNumber_). Minimal 9-byte input {""{"vr"2 advances past the sentinel and causes an out-of-bounds heap read at DcmJSONReader::parseElement (dcjsonrd.cc:613).
  3. Zero-length empty string advancement in parseElement(): On empty attribute values ("vr":""), tmpToken->start == tmpToken->end, causing while (current->start < tmpToken->end) to skip advancing past tmpToken.
  4. Inverted bounds check and %25 re-scan in DcmJSONReader::urlDecode(): if (uri.length() + 3 < pos) was effectively unreachable for valid indices, allowing out-of-bounds element access on truncated % / %x suffixes, and restarting find("%") at index 0 caused %25 to re-scan the decoded '%'.

Changes

  • dcmdata/include/dcmtk/dcmdata/dcjsonrd.h: Added private helper OFBool isValidToken(OFJsmnTokenPtr t) const; and updated getTokenContent() documentation.
  • dcmdata/libsrc/dcjsonrd.cc:
    • Implemented DcmJSONReader::isValidToken() and guarded getTokenContent().
    • Stopped readAndConvertJSONFile() immediately when parseJSON() fails regardless of stopOnErrorPolicy_, and validated current around array indexing and tokensToSkip.
    • Added isValidToken() guards at function entry, loop headers, trace macros, and every current++ in parseElement(), parseSequence(), parseDataSet(), parsePersonName(), and parseElementValueArray().
    • Updated parseElement() token skipping to do { current++; } while (isValidToken(current) && (current->start < tmpToken->end));.
    • Fixed urlDecode() bounds check (uri.length() - pos < 3), search offset (uri.find("%", pos)), and cursor advance (++pos;).
  • dcmdata/tests/tjsonrd.cc, dcmdata/tests/tests.cc, dcmdata/tests/CMakeLists.txt: Added and registered 3 regression tests (dcmdata_json_malformed_token_oob, dcmdata_json_empty_vr_and_desync_bounds, dcmdata_json_urlDecode_truncated).

Verification

Local MSVC /fsanitize=address (RED -> GREEN)

  • Pre-fix (RED): dcmdata_tests.exe dcmdata_json_malformed_token_oob reproduced ERROR: AddressSanitizer: heap-buffer-overflow (READ of size 4, 0 bytes after 96-byte tokenArray_) at dcmdata/libsrc/dcjsonrd.cc:613 in DcmJSONReader::parseElement.
  • Post-fix (GREEN): Rebuilt dcmdata_tests.exe with MSVC /fsanitize=address /Z7 and ran all tests with DCMDICTPATH and DCMICONVPATH configured:
    • dcmdata_json_malformed_token_oob: 1 succeeded, 0 failed
    • dcmdata_json_empty_vr_and_desync_bounds: 1 succeeded, 0 failed
    • dcmdata_json_urlDecode_truncated: 1 succeeded, 0 failed
    • Full dcmdata_tests.exe suite: 165 succeeded, 0 failed, 0 ASan errors.

Cloud CI (stresstest-dcmtk.yml, Run 45136753745)

  • LibFuzzer Coverage & Triage (137564766790): Passed (120s per harness across fuzz_dcmdata_json_roundtrip, fuzz_dcmdata_parser, fuzz_dcmimgle_decode, and fuzz_dcmnet_pdu with ASan + UBSan + nullability sanitizers) -> 0 unique crash(es).
  • CTest Suite (ASan + UBSan + LSan) (137564767103): 552 / 553 passed (99% tests passed), including 165 / 165 dcmdata tests (dcmdata_json_malformed_token_oob, dcmdata_json_empty_vr_and_desync_bounds, dcmdata_json_urlDecode_truncated all passed). Sole failure was pre-existing 304 - dcmtls_scp_pool_tls (DCMTK/dcmtk#168 / Redmine #1309, pending mirror to public master).

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.

1 participant