Skip to content

Commit 64be6ec

Browse files
authored
Fix potential pointer overflows (#826)
Fix some potential pointer overflows in aot applying relocations and several other places. And add sanitizer compiler flags to wamrc CMakeLists.txt to detect such issues.
1 parent a1ad950 commit 64be6ec

21 files changed

+103
-65
lines changed

core/iwasm/aot/aot_loader.c

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2038,7 +2038,8 @@ load_relocation_section(const uint8 *buf, const uint8 *buf_end,
20382038

20392039
for (j = 0; j < relocation_count; j++) {
20402040
AOTRelocation relocation = { 0 };
2041-
uint32 symbol_index, offset32, addend32;
2041+
uint32 symbol_index, offset32;
2042+
int32 addend32;
20422043
uint16 symbol_name_len;
20432044
uint8 *symbol_name;
20442045

@@ -2050,7 +2051,7 @@ load_relocation_section(const uint8 *buf, const uint8 *buf_end,
20502051
read_uint32(buf, buf_end, offset32);
20512052
relocation.relocation_offset = (uint64)offset32;
20522053
read_uint32(buf, buf_end, addend32);
2053-
relocation.relocation_addend = (uint64)addend32;
2054+
relocation.relocation_addend = (int64)addend32;
20542055
}
20552056
read_uint32(buf, buf_end, relocation.relocation_type);
20562057
read_uint32(buf, buf_end, symbol_index);

core/iwasm/aot/aot_reloc.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -153,7 +153,7 @@ get_current_target(char *target_buf, uint32 target_buf_size);
153153
bool
154154
apply_relocation(AOTModule *module,
155155
uint8 *target_section_addr, uint32 target_section_size,
156-
uint64 reloc_offset, uint64 reloc_addend,
156+
uint64 reloc_offset, int64 reloc_addend,
157157
uint32 reloc_type, void *symbol_addr, int32 symbol_index,
158158
char *error_buf, uint32 error_buf_size);
159159
/* clang-format off */

core/iwasm/aot/aot_runtime.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2087,7 +2087,7 @@ aot_enlarge_memory(AOTModuleInstance *module_inst, uint32 inc_page_count)
20872087
}
20882088
}
20892089

2090-
heap_data = heap_data_old + (memory_data - memory_data_old);
2090+
heap_data = memory_data + (heap_data_old - memory_data_old);
20912091
memory_inst->heap_data.ptr = heap_data;
20922092
memory_inst->heap_data_end.ptr = heap_data + heap_size;
20932093

core/iwasm/aot/aot_runtime.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ typedef struct AOTObjectDataSection {
6464
/* Relocation info */
6565
typedef struct AOTRelocation {
6666
uint64 relocation_offset;
67-
uint64 relocation_addend;
67+
int64 relocation_addend;
6868
uint32 relocation_type;
6969
char *symbol_name;
7070
/* index in the symbol offset field */

core/iwasm/aot/arch/aot_reloc_aarch64.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -138,7 +138,7 @@ check_reloc_offset(uint32 target_section_size, uint64 reloc_offset,
138138
bool
139139
apply_relocation(AOTModule *module, uint8 *target_section_addr,
140140
uint32 target_section_size, uint64 reloc_offset,
141-
uint64 reloc_addend, uint32 reloc_type, void *symbol_addr,
141+
int64 reloc_addend, uint32 reloc_type, void *symbol_addr,
142142
int32 symbol_index, char *error_buf, uint32 error_buf_size)
143143
{
144144
switch (reloc_type) {

core/iwasm/aot/arch/aot_reloc_arc.c

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -164,15 +164,16 @@ middle_endian_convert(uint32 insn)
164164
bool
165165
apply_relocation(AOTModule *module, uint8 *target_section_addr,
166166
uint32 target_section_size, uint64 reloc_offset,
167-
uint64 reloc_addend, uint32 reloc_type, void *symbol_addr,
167+
int64 reloc_addend, uint32 reloc_type, void *symbol_addr,
168168
int32 symbol_index, char *error_buf, uint32 error_buf_size)
169169
{
170170
switch (reloc_type) {
171171
case R_ARC_S25W_PCREL:
172172
{
173173
uint32 insn = LOAD_I32(target_section_addr + reloc_offset);
174174
int32 addend, value;
175-
uintptr_t S, A, P;
175+
uintptr_t S, P;
176+
intptr_t A;
176177

177178
CHECK_RELOC_OFFSET(sizeof(void *));
178179

@@ -190,7 +191,7 @@ apply_relocation(AOTModule *module, uint8 *target_section_addr,
190191

191192
/* (S + A) - P */
192193
S = (uintptr_t)(uint8 *)symbol_addr;
193-
A = (uintptr_t)reloc_addend;
194+
A = (intptr_t)reloc_addend;
194195
P = (uintptr_t)(target_section_addr + reloc_offset);
195196
P &= (uintptr_t)~3;
196197
value = (int32)(S + A + addend - P);
@@ -214,7 +215,7 @@ apply_relocation(AOTModule *module, uint8 *target_section_addr,
214215
CHECK_RELOC_OFFSET(sizeof(void *));
215216

216217
/* (S + A) */
217-
insn = (uint32)(uintptr_t)((uint8 *)symbol_addr + reloc_addend);
218+
insn = (uint32)((uintptr_t)symbol_addr + (intptr_t)reloc_addend);
218219

219220
if (reloc_type == R_ARC_32_ME)
220221
/* Convert to middle endian */

core/iwasm/aot/arch/aot_reloc_arm.c

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -198,7 +198,7 @@ check_reloc_offset(uint32 target_section_size, uint64 reloc_offset,
198198
bool
199199
apply_relocation(AOTModule *module, uint8 *target_section_addr,
200200
uint32 target_section_size, uint64 reloc_offset,
201-
uint64 reloc_addend, uint32 reloc_type, void *symbol_addr,
201+
int64 reloc_addend, uint32 reloc_type, void *symbol_addr,
202202
int32 symbol_index, char *error_buf, uint32 error_buf_size)
203203
{
204204
switch (reloc_type) {
@@ -222,8 +222,10 @@ apply_relocation(AOTModule *module, uint8 *target_section_addr,
222222
*/
223223
/* operation: ((S + A) | T) - P where S is symbol address and T
224224
* is 0 */
225-
result = (intptr_t)((uint8 *)symbol_addr + reloc_addend
226-
- (target_section_addr + reloc_offset));
225+
result =
226+
(intptr_t)((uintptr_t)symbol_addr + (intptr_t)reloc_addend
227+
- (uintptr_t)(target_section_addr
228+
+ reloc_offset));
227229
}
228230
else {
229231
if (reloc_addend > 0) {
@@ -244,8 +246,9 @@ apply_relocation(AOTModule *module, uint8 *target_section_addr,
244246
uint8 *plt = (uint8 *)module->code + module->code_size
245247
- get_plt_table_size()
246248
+ get_plt_item_size() * symbol_index;
247-
result = (intptr_t)(plt + reloc_addend
248-
- (target_section_addr + reloc_offset));
249+
result = (intptr_t)((uintptr_t)plt + (intptr_t)reloc_addend
250+
- (uintptr_t)(target_section_addr
251+
+ reloc_offset));
249252
}
250253

251254
result += initial_addend;
@@ -270,8 +273,9 @@ apply_relocation(AOTModule *module, uint8 *target_section_addr,
270273
CHECK_RELOC_OFFSET(sizeof(void *));
271274
initial_addend =
272275
*(intptr_t *)(target_section_addr + (uint32)reloc_offset);
273-
*(uint8 **)(target_section_addr + reloc_offset) =
274-
(uint8 *)symbol_addr + initial_addend + reloc_addend;
276+
*(uintptr_t *)(target_section_addr + reloc_offset) =
277+
(uintptr_t)symbol_addr + initial_addend
278+
+ (intptr_t)reloc_addend;
275279
break;
276280
}
277281

core/iwasm/aot/arch/aot_reloc_mips.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ get_plt_table_size()
4848
bool
4949
apply_relocation(AOTModule *module, uint8 *target_section_addr,
5050
uint32 target_section_size, uint64 reloc_offset,
51-
uint64 reloc_addend, uint32 reloc_type, void *symbol_addr,
51+
int64 reloc_addend, uint32 reloc_type, void *symbol_addr,
5252
int32 symbol_index, char *error_buf, uint32 error_buf_size)
5353
{
5454
switch (reloc_type) {

core/iwasm/aot/arch/aot_reloc_riscv.c

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -205,7 +205,7 @@ check_reloc_offset(uint32 target_section_size, uint64 reloc_offset,
205205
bool
206206
apply_relocation(AOTModule *module, uint8 *target_section_addr,
207207
uint32 target_section_size, uint64 reloc_offset,
208-
uint64 reloc_addend, uint32 reloc_type, void *symbol_addr,
208+
int64 reloc_addend, uint32 reloc_type, void *symbol_addr,
209209
int32 symbol_index, char *error_buf, uint32 error_buf_size)
210210
{
211211
int32 val, imm_hi, imm_lo, insn;
@@ -216,10 +216,10 @@ apply_relocation(AOTModule *module, uint8 *target_section_addr,
216216
case R_RISCV_32:
217217
{
218218
uint32 val_32 =
219-
(uint32)(uintptr_t)((uint8 *)symbol_addr + reloc_addend);
219+
(uint32)((uintptr_t)symbol_addr + (intptr_t)reloc_addend);
220220

221221
CHECK_RELOC_OFFSET(sizeof(uint32));
222-
if (val_32 != (uintptr_t)((uint8 *)symbol_addr + reloc_addend)) {
222+
if (val_32 != ((uintptr_t)symbol_addr + (intptr_t)reloc_addend)) {
223223
goto fail_addr_out_of_range;
224224
}
225225

@@ -229,7 +229,7 @@ apply_relocation(AOTModule *module, uint8 *target_section_addr,
229229
case R_RISCV_64:
230230
{
231231
uint64 val_64 =
232-
(uint64)(uintptr_t)((uint8 *)symbol_addr + reloc_addend);
232+
(uint64)((uintptr_t)symbol_addr + (intptr_t)reloc_addend);
233233
CHECK_RELOC_OFFSET(sizeof(uint64));
234234
bh_memcpy_s(addr, 8, &val_64, 8);
235235
break;
@@ -273,10 +273,10 @@ apply_relocation(AOTModule *module, uint8 *target_section_addr,
273273

274274
case R_RISCV_HI20:
275275
{
276-
val = (int32)(intptr_t)((uint8 *)symbol_addr + reloc_addend);
276+
val = (int32)((intptr_t)symbol_addr + (intptr_t)reloc_addend);
277277

278278
CHECK_RELOC_OFFSET(sizeof(uint32));
279-
if (val != (intptr_t)((uint8 *)symbol_addr + reloc_addend)) {
279+
if (val != ((intptr_t)symbol_addr + (intptr_t)reloc_addend)) {
280280
goto fail_addr_out_of_range;
281281
}
282282

@@ -290,10 +290,10 @@ apply_relocation(AOTModule *module, uint8 *target_section_addr,
290290

291291
case R_RISCV_LO12_I:
292292
{
293-
val = (int32)(intptr_t)((uint8 *)symbol_addr + reloc_addend);
293+
val = (int32)((intptr_t)symbol_addr + (intptr_t)reloc_addend);
294294

295295
CHECK_RELOC_OFFSET(sizeof(uint32));
296-
if (val != (intptr_t)((uint8 *)symbol_addr + reloc_addend)) {
296+
if (val != (intptr_t)symbol_addr + (intptr_t)reloc_addend) {
297297
goto fail_addr_out_of_range;
298298
}
299299

@@ -307,10 +307,10 @@ apply_relocation(AOTModule *module, uint8 *target_section_addr,
307307

308308
case R_RISCV_LO12_S:
309309
{
310-
val = (int32)(intptr_t)((uint8 *)symbol_addr + reloc_addend);
310+
val = (int32)((intptr_t)symbol_addr + (intptr_t)reloc_addend);
311311

312312
CHECK_RELOC_OFFSET(sizeof(uint32));
313-
if (val != (intptr_t)((uint8 *)symbol_addr + reloc_addend)) {
313+
if (val != ((intptr_t)symbol_addr + (intptr_t)reloc_addend)) {
314314
goto fail_addr_out_of_range;
315315
}
316316

core/iwasm/aot/arch/aot_reloc_thumb.c

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -238,7 +238,7 @@ check_reloc_offset(uint32 target_section_size, uint64 reloc_offset,
238238
bool
239239
apply_relocation(AOTModule *module, uint8 *target_section_addr,
240240
uint32 target_section_size, uint64 reloc_offset,
241-
uint64 reloc_addend, uint32 reloc_type, void *symbol_addr,
241+
int64 reloc_addend, uint32 reloc_type, void *symbol_addr,
242242
int32 symbol_index, char *error_buf, uint32 error_buf_size)
243243
{
244244
switch (reloc_type) {
@@ -269,7 +269,8 @@ apply_relocation(AOTModule *module, uint8 *target_section_addr,
269269
/* operation: ((S + A) | T) - P where S is symbol address
270270
and T is 1 */
271271
result =
272-
(int32)(((intptr_t)((uint8 *)symbol_addr + reloc_addend)
272+
(int32)(((intptr_t)((uintptr_t)symbol_addr
273+
+ (intptr_t)reloc_addend)
273274
| 1)
274275
- (intptr_t)(target_section_addr + reloc_offset));
275276
}

0 commit comments

Comments
 (0)