Skip to content

Commit c3b9cde

Browse files
authored
Reject invalid section IDs (#6675)
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.
1 parent 0f9f2dc commit c3b9cde

File tree

3 files changed

+8
-8
lines changed

3 files changed

+8
-8
lines changed

scripts/test/shared.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -408,7 +408,6 @@ def get_tests(test_dir, extensions=[], recursive=False):
408408
'utf8-invalid-encoding.wast',
409409
'const.wast',
410410
'address.wast',
411-
'custom.wast', # invalid section ID accepted as Custom, triggering UBSan
412411

413412
# Unlinkable module accepted
414413
'linking.wast',

src/wasm-binary.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1453,7 +1453,7 @@ class WasmBinaryReader {
14531453
Index startIndex = -1;
14541454
std::set<Function::DebugLocation> debugLocation;
14551455
size_t codeSectionLocation;
1456-
std::set<BinaryConsts::Section> seenSections;
1456+
std::unordered_set<uint8_t> seenSections;
14571457

14581458
// All types defined in the type section
14591459
std::vector<HeapType> types;

src/wasm/wasm-binary.cpp

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1784,11 +1784,8 @@ void WasmBinaryReader::read() {
17841784
// note the section in the list of seen sections, as almost no sections can
17851785
// appear more than once, and verify those that shouldn't do not.
17861786
if (sectionCode != BinaryConsts::Section::Custom &&
1787-
sectionCode != BinaryConsts::Section::Code) {
1788-
if (!seenSections.insert(BinaryConsts::Section(sectionCode)).second) {
1789-
throwError("section seen more than once: " +
1790-
std::to_string(sectionCode));
1791-
}
1787+
!seenSections.insert(sectionCode).second) {
1788+
throwError("section seen more than once: " + std::to_string(sectionCode));
17921789
}
17931790

17941791
switch (sectionCode) {
@@ -1837,7 +1834,7 @@ void WasmBinaryReader::read() {
18371834
case BinaryConsts::Section::Tag:
18381835
readTags();
18391836
break;
1840-
default: {
1837+
case BinaryConsts::Section::Custom: {
18411838
readCustomSection(payloadLen);
18421839
if (pos > oldPos + payloadLen) {
18431840
throwError("bad user section size, started at " +
@@ -1846,7 +1843,11 @@ void WasmBinaryReader::read() {
18461843
" not being equal to new position " + std::to_string(pos));
18471844
}
18481845
pos = oldPos + payloadLen;
1846+
break;
18491847
}
1848+
default:
1849+
throwError(std::string("unrecognized section ID: ") +
1850+
std::to_string(sectionCode));
18501851
}
18511852

18521853
// make sure we advanced exactly past this section

0 commit comments

Comments
 (0)