Skip to content

Commit

Permalink
Fix race condition in mem_segv_reason
Browse files Browse the repository at this point in the history
There's a brief delay between the page fault and calling
mem_segv_reason, and in that window the access could become valid.
  • Loading branch information
tbodt committed Jan 2, 2020
1 parent 282d53d commit dd8d3fb
Show file tree
Hide file tree
Showing 7 changed files with 4 additions and 21 deletions.
1 change: 0 additions & 1 deletion emu/cpu.h
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,6 @@ struct cpu_state {

// for the page fault handler
addr_t segfault_addr;
uint8_t segfault_type;

dword_t trapno;
};
Expand Down
8 changes: 2 additions & 6 deletions emu/memory.c
Original file line number Diff line number Diff line change
Expand Up @@ -285,15 +285,11 @@ void *mem_ptr(struct mem *mem, addr_t addr, int type) {
return entry->data->data + entry->offset + PGOFFSET(addr);
}

int mem_segv_reason(struct mem *mem, addr_t addr, int type) {
assert(type == MEM_READ || type == MEM_WRITE);
int mem_segv_reason(struct mem *mem, addr_t addr) {
struct pt_entry *pt = mem_pt(mem, PAGE(addr));
if (pt == NULL)
return SEGV_MAPERR_;
if ((type == MEM_READ && !(pt->flags & P_READ)) ||
(type == MEM_WRITE && !(pt->flags & P_WRITE)))
return SEGV_ACCERR_;
die("caught segv for valid access");
return SEGV_ACCERR_;
}

size_t real_page_size;
Expand Down
2 changes: 1 addition & 1 deletion emu/memory.h
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ int pt_copy_on_write(struct mem *src, struct mem *dst, page_t start, page_t page
#define MEM_WRITE 1
// Must call with mem read-locked.
void *mem_ptr(struct mem *mem, addr_t addr, int type);
int mem_segv_reason(struct mem *mem, addr_t addr, int type);
int mem_segv_reason(struct mem *mem, addr_t addr);

extern size_t real_page_size;

Expand Down
6 changes: 0 additions & 6 deletions jit/gadgets-aarch64/memory.S
Original file line number Diff line number Diff line change
Expand Up @@ -85,12 +85,6 @@ NAME(si_gadgets):

segfault_\type:
str _addr, [_cpu, CPU_segfault_addr]
.ifc \type,read
mov w0, MEM_READ
.else
mov w0, MEM_WRITE
.endif
strb w0, [_cpu, CPU_segfault_type]
ldr eip, [_ip]
mov x0, INT_GPF
b jit_exit
Expand Down
5 changes: 0 additions & 5 deletions jit/gadgets-x86_64/memory.S
Original file line number Diff line number Diff line change
Expand Up @@ -79,11 +79,6 @@

segfault_\type:
movl %_addr, CPU_segfault_addr(%_cpu)
.ifc \type,read
movb $MEM_READ, CPU_segfault_type(%_cpu)
.else
movb $MEM_WRITE, CPU_segfault_type(%_cpu)
.endif
movl (%_ip), %_eip
movl $INT_GPF, %_tmp
jmp jit_exit
Expand Down
1 change: 0 additions & 1 deletion jit/offsets.c
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,6 @@ void cpu() {
OFFSET(LOCAL, jit_frame, last_block);
OFFSET(LOCAL, jit_frame, ret_cache);
OFFSET(CPU, cpu_state, segfault_addr);
OFFSET(CPU, cpu_state, segfault_type);
MACRO(MEM_READ);
MACRO(MEM_WRITE);

Expand Down
2 changes: 1 addition & 1 deletion kernel/calls.c
Original file line number Diff line number Diff line change
Expand Up @@ -242,7 +242,7 @@ void handle_interrupt(int interrupt) {
} else if (interrupt == INT_GPF) {
printk("%d page fault on 0x%x at 0x%x\n", current->pid, cpu->segfault_addr, cpu->eip);
struct siginfo_ info = {
.code = mem_segv_reason(cpu->mem, cpu->segfault_addr, cpu->segfault_type),
.code = mem_segv_reason(cpu->mem, cpu->segfault_addr),
.fault.addr = cpu->segfault_addr,
};
deliver_signal(current, SIGSEGV_, info);
Expand Down

0 comments on commit dd8d3fb

Please sign in to comment.