Repository navigation
Fixed out-of-bounds token reads and urlDecode check in DcmJSONReader - #169
Open
lauyeehow1986-hub wants to merge 1 commit into
Open
lauyeehow1986-hub wants to merge 1 commit into
lauyeehow1986-hub wants to merge 1 commit into
Conversation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Fixes an ASan
heap-buffer-overflowread inDcmJSONReader(dcmdata/libsrc/dcjsonrd.cc) discovered by Cloud CI LibFuzzer testing (fuzz_dcmdata_json_roundtrip, Run45116035219), together with related token-stream bounds issues and an inverted bounds check inDcmJSONReader::urlDecode().Root Cause
DcmJSONReader::readAndConvertJSONFile()ignoredparseJSON()errors whenstopOnErrorPolicy_isOFFalse(default): After a JSMN parse failure,readAndConvertJSONFile()continued traversingtokenArray_using incomplete/zero-initialized tokens.tokenArray_advancement across JSMN consumers:reserveTokens()allocatestokenNumber_ + 1tokens with a single sentinel (tokenArray_[tokenNumber_]), whileparseElement(),parseSequence(),parseDataSet(),parsePersonName(),parseElementValueArray(),getTokenContent(), andreadAndConvertJSONFile()incrementedcurrent++multiple times without validating pointer bounds against[tokenArray_, tokenArray_ + tokenNumber_). Minimal 9-byte input{""{"vr"2advances past the sentinel and causes an out-of-bounds heap read atDcmJSONReader::parseElement(dcjsonrd.cc:613).parseElement(): On empty attribute values ("vr":""),tmpToken->start == tmpToken->end, causingwhile (current->start < tmpToken->end)to skip advancing pasttmpToken.%25re-scan inDcmJSONReader::urlDecode():if (uri.length() + 3 < pos)was effectively unreachable for valid indices, allowing out-of-bounds element access on truncated%/%xsuffixes, and restartingfind("%")at index0caused%25to re-scan the decoded'%'.Changes
dcmdata/include/dcmtk/dcmdata/dcjsonrd.h: Added private helperOFBool isValidToken(OFJsmnTokenPtr t) const;and updatedgetTokenContent()documentation.dcmdata/libsrc/dcjsonrd.cc:DcmJSONReader::isValidToken()and guardedgetTokenContent().readAndConvertJSONFile()immediately whenparseJSON()fails regardless ofstopOnErrorPolicy_, and validatedcurrentaround array indexing andtokensToSkip.isValidToken()guards at function entry, loop headers, trace macros, and everycurrent++inparseElement(),parseSequence(),parseDataSet(),parsePersonName(), andparseElementValueArray().parseElement()token skipping todo { current++; } while (isValidToken(current) && (current->start < tmpToken->end));.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)dcmdata_tests.exe dcmdata_json_malformed_token_oobreproducedERROR: AddressSanitizer: heap-buffer-overflow(READ of size 4, 0 bytes after 96-bytetokenArray_) atdcmdata/libsrc/dcjsonrd.cc:613inDcmJSONReader::parseElement.dcmdata_tests.exewith MSVC/fsanitize=address /Z7and ran all tests withDCMDICTPATHandDCMICONVPATHconfigured:dcmdata_json_malformed_token_oob:1 succeeded, 0 faileddcmdata_json_empty_vr_and_desync_bounds:1 succeeded, 0 faileddcmdata_json_urlDecode_truncated:1 succeeded, 0 faileddcmdata_tests.exesuite:165 succeeded, 0 failed, 0 ASan errors.Cloud CI (
stresstest-dcmtk.yml, Run45136753745)137564766790): Passed (120sper harness acrossfuzz_dcmdata_json_roundtrip,fuzz_dcmdata_parser,fuzz_dcmimgle_decode, andfuzz_dcmnet_pduwith ASan + UBSan + nullability sanitizers) ->0 unique crash(es).137564767103): 552 / 553 passed (99% tests passed), including 165 / 165dcmdatatests (dcmdata_json_malformed_token_oob,dcmdata_json_empty_vr_and_desync_bounds,dcmdata_json_urlDecode_truncatedall passed). Sole failure was pre-existing304 - dcmtls_scp_pool_tls(DCMTK/dcmtk#168/ Redmine#1309, pending mirror to publicmaster).