Skip to content

Commit 8088783

Browse files
authored
Refine is_xip_file and pointer range check (#965)
Refine is_xip_file check, when e_type isn't E_TYPE_XIP, just return false and no need to go through all the other sections of the AOT file. Refine pointer range check, convert pointer to uintptr_t type before comparison to yield possible sanitizer pointer overflow error.
1 parent 552f850 commit 8088783

File tree

4 files changed

+20
-17
lines changed

4 files changed

+20
-17
lines changed

core/iwasm/aot/aot_loader.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,8 @@ static bool
9090
check_buf(const uint8 *buf, const uint8 *buf_end, uint32 length,
9191
char *error_buf, uint32 error_buf_size)
9292
{
93-
if (buf + length < buf || buf + length > buf_end) {
93+
if ((uintptr_t)buf + length < (uintptr_t)buf
94+
|| (uintptr_t)buf + length > (uintptr_t)buf_end) {
9495
set_error_buf(error_buf, error_buf_size, "unexpect end");
9596
return false;
9697
}

core/iwasm/common/wasm_runtime_common.c

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -305,10 +305,11 @@ align_ptr(const uint8 *p, uint32 b)
305305
return (uint8 *)((v + m) & ~m);
306306
}
307307

308-
#define CHECK_BUF(buf, buf_end, length) \
309-
do { \
310-
if (buf + length < buf || buf + length > buf_end) \
311-
return false; \
308+
#define CHECK_BUF(buf, buf_end, length) \
309+
do { \
310+
if ((uintptr_t)buf + length < (uintptr_t)buf \
311+
|| (uintptr_t)buf + length > (uintptr_t)buf_end) \
312+
return false; \
312313
} while (0)
313314

314315
#define read_uint16(p, p_end, res) \
@@ -347,9 +348,7 @@ wasm_runtime_is_xip_file(const uint8 *buf, uint32 size)
347348
if (section_type == AOT_SECTION_TYPE_TARGET_INFO) {
348349
p += 4;
349350
read_uint16(p, p_end, e_type);
350-
if (e_type == E_TYPE_XIP) {
351-
return true;
352-
}
351+
return (e_type == E_TYPE_XIP) ? true : false;
353352
}
354353
else if (section_type >= AOT_SECTION_TYPE_SIGANATURE) {
355354
return false;

core/iwasm/interpreter/wasm_loader.c

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,8 @@ static bool
4747
check_buf(const uint8 *buf, const uint8 *buf_end, uint32 length,
4848
char *error_buf, uint32 error_buf_size)
4949
{
50-
if (buf + length < buf || buf + length > buf_end) {
50+
if ((uintptr_t)buf + length < (uintptr_t)buf
51+
|| (uintptr_t)buf + length > (uintptr_t)buf_end) {
5152
set_error_buf(error_buf, error_buf_size,
5253
"unexpected end of section or function");
5354
return false;
@@ -59,7 +60,8 @@ static bool
5960
check_buf1(const uint8 *buf, const uint8 *buf_end, uint32 length,
6061
char *error_buf, uint32 error_buf_size)
6162
{
62-
if (buf + length < buf || buf + length > buf_end) {
63+
if ((uintptr_t)buf + length < (uintptr_t)buf
64+
|| (uintptr_t)buf + length > (uintptr_t)buf_end) {
6365
set_error_buf(error_buf, error_buf_size, "unexpected end");
6466
return false;
6567
}

product-mini/platforms/linux-sgx/enclave-sample/Enclave/Enclave.cpp

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -131,10 +131,11 @@ align_ptr(const uint8 *p, uint32 b)
131131
#define AOT_SECTION_TYPE_SIGANATURE 6
132132
#define E_TYPE_XIP 4
133133

134-
#define CHECK_BUF(buf, buf_end, length) \
135-
do { \
136-
if (buf + length < buf || buf + length > buf_end) \
137-
return false; \
134+
#define CHECK_BUF(buf, buf_end, length) \
135+
do { \
136+
if ((uintptr_t)buf + length < (uintptr_t)buf \
137+
|| (uintptr_t)buf + length > (uintptr_t)buf_end) \
138+
return false; \
138139
} while (0)
139140

140141
#define read_uint16(p, p_end, res) \
@@ -162,6 +163,7 @@ is_xip_file(const uint8 *buf, uint32 size)
162163

163164
if (get_package_type(buf, size) != Wasm_Module_AoT)
164165
return false;
166+
165167
CHECK_BUF(p, p_end, 8);
166168
p += 8;
167169
while (p < p_end) {
@@ -172,15 +174,14 @@ is_xip_file(const uint8 *buf, uint32 size)
172174
if (section_type == AOT_SECTION_TYPE_TARGET_INFO) {
173175
p += 4;
174176
read_uint16(p, p_end, e_type);
175-
if (e_type == E_TYPE_XIP) {
176-
return true;
177-
}
177+
return (e_type == E_TYPE_XIP) ? true : false;
178178
}
179179
else if (section_type >= AOT_SECTION_TYPE_SIGANATURE) {
180180
return false;
181181
}
182182
p += section_size;
183183
}
184+
184185
return false;
185186
}
186187

0 commit comments

Comments
 (0)