Repository navigation
mm: leave a frame untouched when a copy-range write can't read its buffer - #118
Conversation
…ffer domain_copy_range() wrote a frame by copying the caller's page straight into the mapped frame. A copy from guest memory can stop part way, and on x86 one from a PV caller zero-fills what it did not copy, so a write whose buffer could not be read failed with -EFAULT after it had already zeroed the frame, or overwritten part of it. Read the caller's page into a scratch page first, and copy it into the frame only once the whole page has been read. A write allocates one scratch page per call and pays one more page copy per frame, and a call that cannot get the page fails with -ENOMEM before touching any frame. Signed-off-by: Kavi <kavi@edera.dev>
Builds domain_copy_range() from xen/common/memory.c against stand-ins for the page lookup, the allocator and the copy from the caller, which can be made to fault part way and zero-fill the rest as x86 does for a PV caller. A write whose page of the buffer cannot be read, from its start or part way through, must fail with -EFAULT and leave the frame as it was, with earlier frames written. A write with no scratch page must fail with -ENOMEM, and plain writes and reads must copy every frame. Signed-off-by: Kavi <kavi@edera.dev>
There was a problem hiding this comment.
PR Review
Reviewed at 28fd2bb.
The one-byte probe in copy_range_buffer_readable() reads every page a real copy could fail on after it has started writing the frame: x86's copy_from_guest_pv() zero-fills from the first byte it cannot read, while __hvm_copy() and Arm's copy_guest() stop before a page they cannot translate, and the new -EFAULT exit still drops the page and type references. The PV and unaligned-HVM fault cases in tools/tests/domain-copy-range would fail against the old direct copy. Nothing concerning.
Test Coverage
Reviewed at 28fd2bb.
tools/tests/domain-copy-range runs the changed domain_copy_range() on every pull request with an unreadable page in the caller's buffer, for PV and HVM callers and for aligned and unaligned buffers. It fails if a write that returns -EFAULT has changed its frame or kept a page reference, or if an HVM caller's aligned write reads the buffer more than once per frame.
musl's limits.h defines PAGE_SIZE on x86_64, so the harness redefined it and the tools build, which uses -Werror, failed there. Undefine it first, as the pdx and numa tests do. Signed-off-by: Kavi <kavi@edera.dev>
| * straight into the frame would leave it clobbered by a write that is | ||
| * reported as failed, so a write reads the whole page here first. | ||
| */ | ||
| if ( to_guest && !(scratch = alloc_xenheap_page()) ) |
There was a problem hiding this comment.
Can't this be exploited to allow zones to cause other zone's writes to fail, in a way that wasn't possible before?
There was a problem hiding this comment.
I also think this is a false no that can break some of our capability probes - we allocate a page before we check for work. I think this is an API break, so it breaks clients like ex: XenCall::supports_domain_copy_range()
We can absorb that, but I want to make sure the callerside fixes go with this so things don't start breaking.
There was a problem hiding this comment.
Removed the scratch page, so there are no additional allocations. Other zones can no longer cause a write to fail by exhausting memory, and a zero-frame call returns 0 as expected.
Before writing to the target frame, we now check that the copy won't fault partway through by reading one byte from each page that could fault. For PV callers, this means checking the first page since a failed copy can zero-fill the destination. We also check the last page if the buffer spans two pages. For HVM/PVH callers with page-aligned buffers, which is what xencall uses, there are no extra reads. I've also added unit test to cover this.
Bouncing every page of a write through a scratch page made a write depend on a free page of host memory, which other domains can use up, and made a call with no frames, which xencall makes to ask whether the op exists, fail with -ENOMEM when there was none. It also added a page copy for every frame. Check instead that the copy cannot fail part way, and copy straight into the frame as before. A copy from guest memory goes a page at a time and fails before it reads from a page it cannot, except that on x86 a copy for a PV vCPU zero-fills whatever it did not copy. So a PV caller's first page of the window is tried with a one-byte read, and for any caller the last page when the window spans two. An HVM or PVH caller with a page-aligned buffer, which xencall's are, reads nothing extra. Signed-off-by: Kavi <kavi@edera.dev>
A
XENMEM_domain_copy_rangewrite whose buffer the caller cannot read failed with-EFAULTbut left the target frame zeroed or partly written.domain_copy_range()copied the caller's page straight into the frame, and a copy from guest memory that faults part way keeps what it already wrote. On x86 a copy for a PV vCPU also zero-fills the rest.A write now checks the copy cannot fail part way before it touches the frame. A copy fails before reading from a page it cannot, so a PV caller's first page is tried with a one-byte read, and any caller's last page when the window spans two. Nothing is allocated, and an HVM or PVH caller with a page-aligned buffer, as xencall passes, reads nothing extra.
tools/tests/domain-copy-rangecovers PV and HVM callers, aligned and unaligned buffers, and the zero-frame probe. The fault cases fail without the fix.PRT-1137