Step one towards uc_mem_protect, uc_mem_unmap, and support for UC_PROT_EXEC and NX regions
This commit is contained in:
88
uc.c
88
uc.c
@ -568,7 +568,7 @@ uc_err uc_mem_map_ex(uch handle, uint64_t address, size_t size, uint32_t perms)
|
||||
return UC_ERR_MAP;
|
||||
|
||||
// check for only valid permissions
|
||||
if ((perms & ~(UC_PROT_READ | UC_PROT_WRITE)) != 0)
|
||||
if ((perms & ~(UC_PROT_READ | UC_PROT_WRITE | UC_PROT_EXEC)) != 0)
|
||||
return UC_ERR_MAP;
|
||||
|
||||
if ((uc->mapped_block_count & (MEM_BLOCK_INCR - 1)) == 0) { //time to grow
|
||||
@ -588,7 +588,91 @@ UNICORN_EXPORT
|
||||
uc_err uc_mem_map(uch handle, uint64_t address, size_t size)
|
||||
{
|
||||
//old api, maps RW by default
|
||||
return uc_mem_map_ex(handle, address, size, UC_PROT_READ | UC_PROT_WRITE);
|
||||
return uc_mem_map_ex(handle, address, size, UC_PROT_READ | UC_PROT_WRITE | UC_PROT_EXEC);
|
||||
}
|
||||
|
||||
UNICORN_EXPORT
|
||||
uc_err uc_mem_protect(uch handle, uint64_t start, size_t block_size, uint32_t perms)
|
||||
{
|
||||
uint64_t address;
|
||||
uint64_t size;
|
||||
struct uc_struct* uc = (struct uc_struct *)handle;
|
||||
|
||||
if (handle == 0)
|
||||
// invalid handle
|
||||
return UC_ERR_UCH;
|
||||
|
||||
if (block_size == 0)
|
||||
// invalid memory mapping
|
||||
return UC_ERR_MAP;
|
||||
|
||||
// address must be aligned to 4KB
|
||||
if ((start & (4*1024 - 1)) != 0)
|
||||
return UC_ERR_MAP;
|
||||
|
||||
// size must be multiple of 4KB
|
||||
if ((block_size & (4*1024 - 1)) != 0)
|
||||
return UC_ERR_MAP;
|
||||
|
||||
// check for only valid permissions
|
||||
if ((perms & ~(UC_PROT_READ | UC_PROT_WRITE | UC_PROT_EXEC)) != 0)
|
||||
return UC_ERR_MAP;
|
||||
|
||||
//check that users entire requested block is mapped
|
||||
address = start;
|
||||
size = block_size;
|
||||
while (size > 0) {
|
||||
uint64_t region_size;
|
||||
MemoryRegion *mr = memory_mapping(uc, address);
|
||||
if (mr == NULL) {
|
||||
return UC_ERR_MAP;
|
||||
}
|
||||
region_size = int128_get64(mr->size);
|
||||
if (address > mr->addr) {
|
||||
//in case start address is not aligned with start of region
|
||||
region_size -= address - mr->addr;
|
||||
}
|
||||
if (size < region_size) {
|
||||
//entire region is covered
|
||||
break;
|
||||
}
|
||||
size -= region_size;
|
||||
address += region_size;
|
||||
}
|
||||
|
||||
//Now we know entire region is mapped, so change permissions
|
||||
address = start;
|
||||
size = block_size;
|
||||
while (size > 0) {
|
||||
MemoryRegion *mr = memory_mapping(uc, address);
|
||||
uint64_t region_size = int128_get64(mr->size);
|
||||
if (address > mr->addr) {
|
||||
//in case start address is not aligned with start of region
|
||||
region_size -= address - mr->addr;
|
||||
//TODO Learn how to split regions
|
||||
//In this case some proper subset of the region is having it's permissions changed
|
||||
//need to split region and add new portions into uc->mapped_blocks list
|
||||
//In this case, there is a portion of the region with original perms: mr->addr..start
|
||||
//and a portion getting new perms: start..start+block_size
|
||||
|
||||
//split the block and stay in the loop
|
||||
}
|
||||
if (size < int128_get64(mr->size)) {
|
||||
//TODO Learn how to split regions
|
||||
//In this case some proper subset of the region is having it's permissions changed
|
||||
//need to split region and add new portions into uc->mapped_blocks list
|
||||
//In this case, there is a portion of the region with new perms: start..start+block_size
|
||||
//and a portion getting new perms: mr->addr+size..mr->addr+mr->size
|
||||
|
||||
//split the block and break
|
||||
break;
|
||||
}
|
||||
size -= int128_get64(mr->size);
|
||||
address += int128_get64(mr->size);
|
||||
mr->perms = perms;
|
||||
uc->readonly_mem(mr, (perms & UC_PROT_WRITE) == 0);
|
||||
}
|
||||
return UC_ERR_OK;
|
||||
}
|
||||
|
||||
MemoryRegion *memory_mapping(struct uc_struct* uc, uint64_t address)
|
||||
|
Reference in New Issue
Block a user