Re: [PATCH v5] mm/gup: disallow GUP writing to file-backed mappings by default

David Hildenbrand
2023-04-28T15:09:51Z

On 28.04.23 16:35, Jason Gunthorpe wrote:
> On Fri, Apr 28, 2023 at 04:20:46PM +0200, David Hildenbrand wrote:
>> Sorry for jumping in late, I'm on vacation :)
>>
>> On 28.04.23 01:42, Lorenzo Stoakes wrote:
>>> Writing to file-backed mappings which require folio dirty tracking using
>>> GUP is a fundamentally broken operation, as kernel write access to GUP
>>> mappings do not adhere to the semantics expected by a file system.
>>>
>>> A GUP caller uses the direct mapping to access the folio, which does not
>>> cause write notify to trigger, nor does it enforce that the caller marks
>>> the folio dirty.
>>
>> How should we enforce it? It would be a BUG in the GUP user.
>
> I hope we don't have these kinds of mistakes.. hard to enforce by
> code.
>

I briefly played with the idea of only allowing to write-pin fs pages
that are dirty (or the pte is dirty). If we adjust writeback code to
leave such (maybe pinned) pages dirty, there would be no need to reset
the pages dirty I guess.

Just an idea, getting the writebackcode fixed (and race-free with
GUP-fast) is the harder problem.

>> This change has the potential to break existing setups. Simple example:
>> libvirt domains configured for file-backed VM memory that also has a vfio
>> device configured. It can easily be configured by users (evolving VM
>> configuration, copy-paste etc.). And it works from a VM perspective, because
>> the guest memory is essentially stale once the VM is shutdown and the pages
>> were unpinned. At least we're not concerned about stale data on
>> disk.
>
> I think this is broken today and we should block it. We know from
> experiments with RDMA that doing exactly this triggers kernel oop's.
>

I never saw similar reports in the wild (especially targeted at RHEL),
so is this still a current issue that has not been mitigated? Or is it
just so hard to actually trigger?

> Run your qemu config once, all the pages in the file become dirty.
>
> Run your qmeu config again and now all the dirty pages are longterm
> pinned.
>
> Something eventually does writeback and FS cleans the page.

At least vmscan does not touch pages that have additional references --
pageout() quits early. So that other thing doesn't happen that often I
guess (manual fsync doesn't usually happen on VM memory if I am not
wrong ...).

>
> Now close your VM and the page is dirtied without make write. FS is
> inconsistent with the MM, kernel will eventually oops.
>
> I'm skeptical that anyone can actually do this combination of things
> successfully without getting kernel crashes or file data corruption -
> ie there is no real user to break.

I am pretty sure that there are such VM users, because on the libvirt
level it's completely unclear which features trigger what behavior :/

I remember (but did not check) that VM memory might usually get deleted
whenever we usually shutdown a VM, another reason why we wouldn't see
this in the wild. libvirt has the "discard" option exactly for that
purpose, to be used with file-based guest memory. [1]

Users tend to copy-paste domain XMLs + edit because it's just so hard to
get right. Changing the VM backing to be backed from a file can be done
with a one-line change in the libvirt XML.

>
>> With your changes, such VMs would no longer start, breaking existing user
>> setups with a kernel update.
>
> Yes, as a matter of security we should break it.
>
> Earlier I suggested making this contingent on kernel lockdown >=
> integrity, if actual users come and complain we should go to that
> option.
>
>> Sure, we could warn, or convert individual users using a flag (io_uring).
>> But maybe we should invest more energy on a fix?
>
> It has been years now, I think we need to admit a fix is still years
> away. Blocking the security problem may even motivate more people to
> work on a fix.

Maybe we should make this a topic this year at LSF/MM (again?). At least
we learned a lot about GUP, what might work, what might not work, and
got a depper understanding (+ motivation to fix? :) ) the issue at hand.

>
> Security is the primary case where we have historically closed uAPI
> items.

As this patch

1) Does not tackle GUP-fast
2) Does not take care of !FOLL_LONGTERM

I am not convinced by the security argument in regard to this patch.

If we want to sells this as a security thing, we have to block it
*completely* and then CC stable.

Everything else sounds like band-aids to me, is insufficient, and might
cause more harm than actually help IMHO. Especially the gup-fast case is
extremely easy to work-around in malicious user space.

[1] https://listman.redhat.com/archives/libvir-list/2018-May/msg00885.html

--
Thanks,

David / dhildenb