For example, libvhost-user.c in QEMU uses for ordinary postcopy:
/*
* In postcopy we're using PROT_NONE here to catch anyone
* accessing it before we userfault.
*/
mmap_addr = mmap(0, dev_region->size + dev_region->mmap_offset,
PROT_NONE, MAP_SHARED | MAP_NORESERVE,
vmsg->fds[0], 0);
I assume this is for missing mode only. More on wr-protect mode below.
Personally I don't see immediately on whether this is needed. If the
process itself is trusted then it should be under control of anyone who
will be accessing the pages.. If the other threads are not trusted, then
there's no way to stop anyone from mprotect(RW) after mprotect(NONE)
anyway..
So I may not really get the gut of it.
Another way to make sure no one access it is right after receiving the
memory range from QEMU (VhostUserMemoryRegion), if VuDev.postcopy_listening
is set, then we register the range with UFFD missing immediately. After
all if postcopy_listening is set it means we passed the advise phase
already (VHOST_USER_POSTCOPY_ADVISE). Any potential access will be blocked
until QEMU starts to read on that uffd.
I'd imagine, when using uffd-wp (VM snapshotting with shmem?) one might use
PROT_READ instead before the write-protection is properly set. Because read
access would be fine in the meantime.
It'll be different for wr-protect IIUC, because unlike missing protections,
we don't worry about writes happening before UFFDIO_WRITEPROTECT.
IMHO the solo thing the vhost-user proc needs to do is one
UFFDIO_WRITEPROTECT for each of the range when QEMU tells it to, then it'll
be fine. Pre-writes are fine.
Sorry I probably went a bit off-topic. I just want to make sure I don't
miss any real use case of having mprotect being useful for uffd-wp being
there, because that used to be a grey area for me.
But I'm just pulling use cases out of my magic hat ;) Nothing stops user
space from doing things that are not clearly forbidden (well, even then
users might complain, but that's a different story).
Yes, I think those are always fine but the user just cannot assume it'll
work as they assumed how it will work.
If "doing things that are not clearly forbidden" triggers a host warning or
crash that's a bug, OTOH if the outcome is limited to the process itself
then from kernel pov I think we're good. I used to even thought about
forbid mprotect() on uffd-wp but I'm not sure whether it's good idea either.
Let's see whether I missed something above, if so I'll rethink.