Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Snapshot use after free #2125

Merged
merged 4 commits into from
Mar 6, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions qemu/softmmu/memory.c
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@

void memory_region_transaction_begin(void);
static void memory_region_transaction_commit(MemoryRegion *mr);
static void memory_region_destructor_container(MemoryRegion *mr);

typedef struct AddrRange AddrRange;

Expand Down Expand Up @@ -87,6 +88,7 @@ static void make_contained(struct uc_struct *uc, MemoryRegion *current)
hwaddr addr = current->addr;
MemoryRegion *container = g_new(MemoryRegion, 1);
memory_region_init(uc, container, int128_get64(current->size));
container->destructor = memory_region_destructor_container;
memory_region_del_subregion(uc->system_memory, current);
memory_region_add_subregion_overlap(container, 0, current, current->priority);
memory_region_add_subregion(uc->system_memory, addr, container);
Expand Down Expand Up @@ -1091,6 +1093,11 @@ static void memory_region_destructor_ram(MemoryRegion *mr)
qemu_ram_free(mr->uc, mr->ram_block);
}

static void memory_region_destructor_container(MemoryRegion *mr)
{
memory_region_filter_subregions(mr, 0);
}

void memory_region_init(struct uc_struct *uc,
MemoryRegion *mr,
uint64_t size)
Expand Down
2 changes: 2 additions & 0 deletions tests/unit/test_arm.c
Original file line number Diff line number Diff line change
Expand Up @@ -905,6 +905,7 @@ static void test_arm_tcg_opcode_cmp(void)
TEST_CHECK(cmp_info.v0 == 5 && cmp_info.v1 == 3);
TEST_CHECK(cmp_info.pc == 0x1008);
TEST_CHECK(cmp_info.size == 32);
OK(uc_close(uc));
}

static void test_arm_thumb_tcg_opcode_cmn(void)
Expand All @@ -931,6 +932,7 @@ static void test_arm_thumb_tcg_opcode_cmn(void)
TEST_CHECK(cmp_info.v0 == 5 && cmp_info.v1 == 3);
TEST_CHECK(cmp_info.pc == 0x1006);
TEST_CHECK(cmp_info.size == 32);
OK(uc_close(uc));
}

static void test_arm_cp15_c1_c0_2(void)
Expand Down
1 change: 1 addition & 0 deletions tests/unit/test_arm64.c
Original file line number Diff line number Diff line change
Expand Up @@ -481,6 +481,7 @@ static void test_arm64_mmu(void)
TEST_CHECK(x1 == 0x4444444444444444);
TEST_CHECK(x2 == 0x4444444444444444);
free(data);
OK(uc_close(uc));
}

static void test_arm64_pc_wrap(void)
Expand Down
1 change: 1 addition & 0 deletions tests/unit/test_riscv.c
Original file line number Diff line number Diff line change
Expand Up @@ -718,6 +718,7 @@ static void test_riscv_mmu(void)
OK(uc_mem_read(uc, data_address, &data_result, sizeof(data_result)));

TEST_CHECK(data_value == data_result);
OK(uc_close(uc));
}

static void test_riscv_priv(void)
Expand Down
11 changes: 9 additions & 2 deletions tests/unit/test_x86.c
Original file line number Diff line number Diff line change
Expand Up @@ -632,7 +632,7 @@ static void test_x86_smc_add(void)
{
uc_engine *uc;
uint64_t stack_base = 0x20000;
int r_rsp;
uint64_t r_rsp;
/*
* mov qword ptr [rip+0x10], rax
* mov word ptr [rip], 0x0548
Expand All @@ -647,6 +647,8 @@ static void test_x86_smc_add(void)
r_rsp = stack_base + 0x1800;
OK(uc_reg_write(uc, UC_X86_REG_RSP, &r_rsp));
OK(uc_emu_start(uc, code_start, -1, 0, 0));

OK(uc_close(uc));
}

static void test_x86_smc_mem_hook_callback(uc_engine *uc, uc_mem_type t,
Expand All @@ -667,7 +669,7 @@ static void test_x86_smc_mem_hook(void)
uc_engine *uc;
uc_hook hook;
uint64_t stack_base = 0x20000;
int r_rsp;
uint64_t r_rsp;
unsigned int i = 0;
/*
* mov qword ptr [rip+0x29], rax
Expand All @@ -689,6 +691,8 @@ static void test_x86_smc_mem_hook(void)
r_rsp = stack_base + 0x1800;
OK(uc_reg_write(uc, UC_X86_REG_RSP, &r_rsp));
OK(uc_emu_start(uc, code_start, -1, 0, 0));

OK(uc_close(uc));
}

static uint64_t test_x86_mmio_uc_mem_rw_read_callback(uc_engine *uc,
Expand Down Expand Up @@ -1589,6 +1593,8 @@ static void test_x86_mmu(void)
OK(uc_mem_read(uc, 0x2000, &child, sizeof(child)));
TEST_CHECK(parrent == 60);
TEST_CHECK(child == 42);
OK(uc_context_free(context));
OK(uc_close(uc));
}

static bool test_x86_vtlb_callback(uc_engine *uc, uint64_t addr,
Expand Down Expand Up @@ -1632,6 +1638,7 @@ static void test_x86_segmentation(void)
OK(uc_open(UC_ARCH_X86, UC_MODE_64, &uc));
OK(uc_reg_write(uc, UC_X86_REG_GDTR, &gdtr));
uc_assert_err(UC_ERR_EXCEPTION, uc_reg_write(uc, UC_X86_REG_FS, &fs));
OK(uc_close(uc));
}

static void test_x86_0xff_lcall_callback(uc_engine *uc, uint64_t address,
Expand Down
8 changes: 4 additions & 4 deletions uc.c
Original file line number Diff line number Diff line change
Expand Up @@ -2439,6 +2439,10 @@ uc_err uc_context_restore(uc_engine *uc, uc_context *context)

if (uc->context_content & UC_CTL_CONTEXT_MEMORY) {
uc->snapshot_level = context->snapshot_level;
if (!uc->flatview_copy(uc, uc->address_space_memory.current_map,
context->fv, true)) {
return UC_ERR_NOMEM;
}
ret = uc_restore_latest_snapshot(uc);
if (ret != UC_ERR_OK) {
restore_jit_state(uc);
Expand All @@ -2447,10 +2451,6 @@ uc_err uc_context_restore(uc_engine *uc, uc_context *context)
uc_snapshot(uc);
uc->ram_list.freed = context->ramblock_freed;
uc->ram_list.last_block = context->last_block;
if (!uc->flatview_copy(uc, uc->address_space_memory.current_map,
context->fv, true)) {
return UC_ERR_NOMEM;
}
uc->tcg_flush_tlb(uc);
}

Expand Down
Loading