@@ -48,15 +48,18 @@ bool BytecodeUtil::getAbiVersion(std::string_view bytecode, proxy_wasm::AbiVersi
4848 return false ;
4949 }
5050 if (section_type == 7 /* export section */ ) {
51+ const char *section_end = pos + section_len;
5152 uint32_t export_vector_size = 0 ;
52- if (!parseVarint (pos, end, export_vector_size) || pos + export_vector_size > end) {
53+ if (!parseVarint (pos, section_end, export_vector_size) ||
54+ pos + export_vector_size > section_end) {
5355 return false ;
5456 }
5557 // Search thourgh exports.
5658 for (uint32_t i = 0 ; i < export_vector_size; i++) {
5759 // Parse name of the export.
5860 uint32_t export_name_size = 0 ;
59- if (!parseVarint (pos, end, export_name_size) || pos + export_name_size > end) {
61+ if (!parseVarint (pos, section_end, export_name_size) ||
62+ pos + export_name_size > section_end) {
6063 return false ;
6164 }
6265 const auto *const name_begin = pos;
@@ -65,7 +68,7 @@ bool BytecodeUtil::getAbiVersion(std::string_view bytecode, proxy_wasm::AbiVersi
6568 return false ;
6669 }
6770 // Check if it is a function type export
68- if (*pos++ == 0x00 ) {
71+ if (*pos++ == 0x00 /* function */ ) {
6972 const std::string export_name = {name_begin, export_name_size};
7073 // Check the name of the function.
7174 if (export_name == " proxy_abi_version_0_1_0" ) {
@@ -114,24 +117,25 @@ bool BytecodeUtil::getCustomSection(std::string_view bytecode, std::string_view
114117 }
115118 if (section_type == 0 ) {
116119 // Custom section.
117- const auto * const section_data_start = pos;
120+ const char *section_end = pos + section_len ;
118121 uint32_t section_name_len = 0 ;
119- if (!BytecodeUtil::parseVarint (pos, end, section_name_len) || pos + section_name_len > end) {
122+ if (!BytecodeUtil::parseVarint (pos, section_end, section_name_len) ||
123+ pos + section_name_len > section_end) {
120124 return false ;
121125 }
122126 if (section_name_len == name.size () && ::memcmp (pos, name.data (), section_name_len) == 0 ) {
123127 pos += section_name_len;
124- ret = {pos, static_cast <size_t >(section_data_start + section_len - pos)};
128+ ret = {pos, static_cast <size_t >(section_end - pos)};
125129 return true ;
126130 }
127- pos = section_data_start + section_len ;
131+ pos = section_end ;
128132 } else {
129133 // Skip other sections.
130134 pos += section_len;
131135 }
132136 }
133137 return true ;
134- };
138+ }
135139
136140bool BytecodeUtil::getFunctionNameIndex (std::string_view bytecode,
137141 std::unordered_map<uint32_t , std::string> &ret) {
@@ -242,16 +246,32 @@ bool BytecodeUtil::getStrippedSource(std::string_view bytecode, std::string &ret
242246
243247bool BytecodeUtil::parseVarint (const char *&pos, const char *end, uint32_t &ret) {
244248 uint32_t shift = 0 ;
249+ uint32_t total = 0 ;
250+ uint32_t v;
245251 char b;
246- do {
252+ while (pos < end) {
247253 if (pos + 1 > end) {
254+ // overread
248255 return false ;
249256 }
250257 b = *pos++;
251- ret += (b & 0x7f ) << shift;
258+ v = (b & 0x7f );
259+ if (shift == 28 && v > 3 ) {
260+ // overflow
261+ return false ;
262+ }
263+ total += v << shift;
264+ if ((b & 0x80 ) == 0 ) {
265+ ret = total;
266+ return true ;
267+ }
252268 shift += 7 ;
253- } while ((b & 0x80 ) != 0 );
254- return ret != static_cast <uint32_t >(-1 );
269+ if (shift > 28 ) {
270+ // overflow
271+ return false ;
272+ }
273+ }
274+ return false ;
255275}
256276
257277} // namespace proxy_wasm
0 commit comments