fuzz: double-fetches in a memory region map session
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
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 intoflatview_read_continue()andaddress_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_tsize =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
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_readhook) 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