Skip to contentSkip to conversation

exec: avoid possible overwriting of mmaped area in qemu_ram_remap

PATCH In review Paolo Bonzini 2 participants 2 messages Mar 25, 2015 – Mar 26, 2015

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 exec: avoid possible overwriting of mmaped area in qemu_ram_remap

It is not necessary to munmap an area before remapping it with MAP_FIXED; if the memory region specified by addr and len overlaps pages of any existing mapping, then the overlapped part of the existing mapping will be discarded.

On the other hand, if QEMU does munmap the pages, there is a small probability that another mmap sneaks in and catches the just-freed portion of the address space. In effect, munmap followed by mmap(MAP_FIXED) is a use-after-free error, and Coverity flags it as such. Fix it.

---
Please review. :)

 exec.c | 1 -
 1 file changed, 1 deletion(-)
exec.c +0 -1
index 8b922db..6d1e1e4 100644
@@ -1638,7 +1638,6 @@ void qemu_ram_remap(ram_addr_t addr, ram_addr_t length)
                 abort();
             } else {
                 flags = MAP_FIXED;
-                munmap(vaddr, length);
                 if (block->fd >= 0) {
                     flags |= (block->flags & RAM_SHARED ?
                               MAP_SHARED : MAP_PRIVATE);
Signature · 1 line
2.3.3
Reviewed-by on v1 1/1
#1 lore raw
Quoted 5 lines · Paolo Bonzini

It is not necessary to munmap an area before remapping it with MAP_FIXED; if the memory region specified by addr and len overlaps pages of any existing mapping, then the overlapped part of the existing mapping will be discarded.

Yes, it is.

Quoted 19 lines

On the other hand, if QEMU does munmap the pages, there is a small probability that another mmap sneaks in and catches the just-freed portion of the address space. In effect, munmap followed by mmap(MAP_FIXED) is a use-after-free error, and Coverity flags it as such. Fix it.

---
Please review. :)

 exec.c | 1 -
 1 file changed, 1 deletion(-)
exec.c +0 -0
index 8b922db..6d1e1e4 100644

exec.c void qemu_ram_remap(ram_addr_t addr, ram_addr_t length) · line 1643

Quoted 7 lines
exec.c +0 -1
@@ -1638,7 +1638,6 @@ void qemu_ram_remap(ram_addr_t addr, ram_addr_t length)
                 abort();
             } else {
                 flags = MAP_FIXED;
-                munmap(vaddr, length);
                 if (block->fd >= 0) {
                     flags |= (block->flags & RAM_SHARED ?
                               MAP_SHARED : MAP_PRIVATE);

Looks good to me, so