memory: clamp cached translation in case it points to an MMIO region
commit4bfb024bc76973d40a359476dc0291f46e435442
authorPaolo Bonzini <pbonzini@redhat.com>
Tue, 1 Dec 2020 14:29:56 +0000 (1 09:29 -0500)
committerPaolo Bonzini <pbonzini@redhat.com>
Tue, 15 Dec 2020 17:52:00 +0000 (15 12:52 -0500)
tree716aa147ab5b9ea0fa97d5cfab4ca6d4c128316e
parent58cf0f86d4ab6b78d11850557c5335ecfd2d2696
memory: clamp cached translation in case it points to an MMIO region

In using the address_space_translate_internal API, address_space_cache_init
forgot one piece of advice that can be found in the code for
address_space_translate_internal:

    /* MMIO registers can be expected to perform full-width accesses based only
     * on their address, without considering adjacent registers that could
     * decode to completely different MemoryRegions.  When such registers
     * exist (e.g. I/O ports 0xcf8 and 0xcf9 on most PC chipsets), MMIO
     * regions overlap wildly.  For this reason we cannot clamp the accesses
     * here.
     *
     * If the length is small (as is the case for address_space_ldl/stl),
     * everything works fine.  If the incoming length is large, however,
     * the caller really has to do the clamping through memory_access_size.
     */

address_space_cache_init is exactly one such case where "the incoming length
is large", therefore we need to clamp the resulting length---not to
memory_access_size though, since we are not doing an access yet, but to
the size of the resulting section.  This ensures that subsequent accesses
to the cached MemoryRegionSection will be in range.

With this patch, the enclosed testcase notices that the used ring does
not fit into the MSI-X table and prints a "qemu-system-x86_64: Cannot map used"
error.

Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
softmmu/physmem.c
tests/qtest/fuzz-test.c