Skip to content

Commit

Permalink
Reject invalid section IDs (#6675)
Browse files Browse the repository at this point in the history
Rather than treating them as custom sections. Also fix UB where invalid
`Section` enum values could be used as keys in a map. Use the raw `uint8_t`
section IDs as keys instead. Re-enable a disabled spec test that was failing
because of this bug and UB.
  • Loading branch information
tlively authored Jun 18, 2024
1 parent 0f9f2dc commit c3b9cde
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 8 deletions.
1 change: 0 additions & 1 deletion scripts/test/shared.py
Original file line number Diff line number Diff line change
Expand Up @@ -408,7 +408,6 @@ def get_tests(test_dir, extensions=[], recursive=False):
'utf8-invalid-encoding.wast',
'const.wast',
'address.wast',
'custom.wast', # invalid section ID accepted as Custom, triggering UBSan

# Unlinkable module accepted
'linking.wast',
Expand Down
2 changes: 1 addition & 1 deletion src/wasm-binary.h
Original file line number Diff line number Diff line change
Expand Up @@ -1453,7 +1453,7 @@ class WasmBinaryReader {
Index startIndex = -1;
std::set<Function::DebugLocation> debugLocation;
size_t codeSectionLocation;
std::set<BinaryConsts::Section> seenSections;
std::unordered_set<uint8_t> seenSections;

// All types defined in the type section
std::vector<HeapType> types;
Expand Down
13 changes: 7 additions & 6 deletions src/wasm/wasm-binary.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1784,11 +1784,8 @@ void WasmBinaryReader::read() {
// note the section in the list of seen sections, as almost no sections can
// appear more than once, and verify those that shouldn't do not.
if (sectionCode != BinaryConsts::Section::Custom &&
sectionCode != BinaryConsts::Section::Code) {
if (!seenSections.insert(BinaryConsts::Section(sectionCode)).second) {
throwError("section seen more than once: " +
std::to_string(sectionCode));
}
!seenSections.insert(sectionCode).second) {
throwError("section seen more than once: " + std::to_string(sectionCode));
}

switch (sectionCode) {
Expand Down Expand Up @@ -1837,7 +1834,7 @@ void WasmBinaryReader::read() {
case BinaryConsts::Section::Tag:
readTags();
break;
default: {
case BinaryConsts::Section::Custom: {
readCustomSection(payloadLen);
if (pos > oldPos + payloadLen) {
throwError("bad user section size, started at " +
Expand All @@ -1846,7 +1843,11 @@ void WasmBinaryReader::read() {
" not being equal to new position " + std::to_string(pos));
}
pos = oldPos + payloadLen;
break;
}
default:
throwError(std::string("unrecognized section ID: ") +
std::to_string(sectionCode));
}

// make sure we advanced exactly past this section
Expand Down

0 comments on commit c3b9cde

Please sign in to comment.