Skip to content

Commit 2a4528c

Browse files
greenknotwenyongh
authored andcommitted
Fix out-of-bounds read in wasm loader (#156)
1 parent aa24fc5 commit 2a4528c

File tree

1 file changed

+9
-8
lines changed

1 file changed

+9
-8
lines changed

core/iwasm/runtime/vmcore-wasm/wasm_loader.c

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,15 @@ read_leb(const uint8 *buf, const uint8 *buf_end,
5555
uint64 byte;
5656

5757
while (true) {
58-
CHECK_BUF(buf, buf_end, 1);
58+
/* Check if the byte count exteeds the max byte count allowed */
59+
if (bcnt + 1 > (maxbits + 6) / 7) {
60+
set_error_buf(error_buf, error_buf_size,
61+
"WASM module load failed: "
62+
"integer representation too long");
63+
return false;
64+
}
65+
/* Check buffer */
66+
CHECK_BUF(buf, buf_end, *p_offset + 1);
5967
byte = buf[*p_offset];
6068
*p_offset += 1;
6169
result |= ((byte & 0x7f) << shift);
@@ -66,13 +74,6 @@ read_leb(const uint8 *buf, const uint8 *buf_end,
6674
}
6775
}
6876

69-
if (bcnt > (maxbits + 6) / 7) {
70-
set_error_buf(error_buf, error_buf_size,
71-
"WASM module load failed: "
72-
"integer representation too long");
73-
return false;
74-
}
75-
7677
if (!sign && maxbits == 32 && shift >= maxbits) {
7778
/* The top bits set represent values > 32 bits */
7879
if (((uint8)byte) & 0xf0)

0 commit comments

Comments
 (0)