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.

Backports commit f18c69cfc554cf9776eb3c35b7510e17541afacb from qemu
This commit is contained in:
Paolo Bonzini 2018-02-18 19:09:34 -05:00 committed by Lioncash
parent aad1027c96
commit 71beea204c
No known key found for this signature in database
GPG key ID: 4E3C3CC1031BA9C7

View file

@ -1242,7 +1242,6 @@ void qemu_ram_remap(struct uc_struct *uc, ram_addr_t addr, ram_addr_t length)
; ;
} else { } else {
flags = MAP_FIXED; flags = MAP_FIXED;
munmap(vaddr, length);
if (block->fd >= 0) { if (block->fd >= 0) {
flags |= (block->flags & RAM_SHARED ? flags |= (block->flags & RAM_SHARED ?
MAP_SHARED : MAP_PRIVATE); MAP_SHARED : MAP_PRIVATE);