Skip to contentSkip to conversation

fuzz: double-fetches in a memory region map session

Question Li Qiuhao 2 participants 3 messages Aug 13, 2021 – Aug 14, 2021

lore
Share & tools
lore

Li Qiuhao

author
#0 lore raw

Hi Alex,

Recently I was reading the DMA call-back functions in the fuzzer. It seems fuzz_dma_read_cb() is inserted into flatview_read_continue() and address_space_map() to make the host read changed content between different DMA actions.

My question is about address_space_map() -- How do we emulate double-fetch bugs in the same map/unmap session? For example:

  FOO *guest_foo = (FOO *) address_space_map(as, ...);

uint64_t size = guest_foo->size; // first fetch
if size > limit

    goto error;

/* time window */

memcpy(dest, src, guest_foo->size); // double-fetch ?

error:
address_space_unmap(as, guest_foo, ...)

Thanks,
Qiuhao Li

#1 lore raw
Quoted 10 lines · Li Qiuhao

Hi Alex,

Recently I was reading the DMA call-back functions in the fuzzer. It seems fuzz_dma_read_cb() is inserted into flatview_read_continue() and address_space_map() to make the host read changed content between different DMA actions.

My question is about address_space_map() -- How do we emulate double-fetch bugs in the same map/unmap session? For example:

Hi Qiuhao,
Right now we don't. One strategy would be to use mprotect. When the code fetches data the first time, we get a SEGV, where we unprotect the page, write a pattern, and enable single-stepping. Then, after the single-step, re-protect the page, and disable single-step.

On OSS-Fuzz, we disabled double-fetch detection, for now, as we did not want reproducers for normal-bugs to inadvertently contain double-fetches. To make the double-fetch detection useful for developers, we probably need to limit the double fetch capability to only fill the DMA regions twice, rather than 10 or 20 times. Then, in the report, we could give the call-stacks (from the SEGV handler, or dma_read hook) of the exact locations in the code that read from the same address twice.

Thanks for your interest in this!
-Alex

Quoted 2 lines
  FOO *guest_foo = (FOO *) address_space_map(as, ...);

// mprotect in address_space_map hook

// SEGV on the read. Un-mprotect, fill with pattern

Quoted 1 line

uint64_t size = guest_foo->size; // first fetch

// Single Step. Re-mprotect (or you could just immediately fill with a // new pattern)

Quoted 5 lines

if size > limit

    goto error;

/* time window */

// SEGV

Quoted 8 lines

memcpy(dest, src, guest_foo->size); // double-fetch ?

error:
address_space_unmap(as, guest_foo, ...)

Thanks,
Qiuhao Li

author
#2 lore raw
Quoted 6 lines · Alexander Bulekov
Quoted 5 lines

My question is about address_space_map() -- How do we emulate double- fetch
bugs in the same map/unmap session? For example:

Quoted 8 lines

Hi Qiuhao,
Right now we don't. One strategy would be to use mprotect. When the code
fetches data the first time, we get a SEGV, where we unprotect the page,
write a pattern, and enable single-stepping. Then, after the single-step, re-protect the page, and disable single-step.

Brilliant! I can always get a lot of inspiration from you :)

Quoted 8 lines

On OSS-Fuzz, we disabled double-fetch detection, for now, as we did not want reproducers for normal-bugs to inadvertently contain double-fetches. To make the double-fetch detection useful for developers, we probably need to limit the double fetch capability to only fill the DMA regions twice, rather than 10 or 20 times. Then, in the report, we could give the call-stacks (from the SEGV handler, or dma_read hook) of the exact locations in the code that read from the same address twice.

Got it, this is indeed the most practical solution. I will try to detect double-fetch bugs via pattern-based analysis [1]. But it may be hard to write PoCs to convince and help developers fix bugs, and we can't identify those bugs caused by the compiler [2] or preprocessor.

[1]
https://www.usenix.org/conference/usenixsecurity17/technical-sessions/presentation/wang-pengfei

[2]
https://www.voidsecurity.in/2018/08/from-compiler-optimization-to-code.html

Thanks,
Qiuhao Li