linux-fsdevel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: David Hildenbrand <david@redhat.com>
To: Jann Horn <jannh@google.com>, Suren Baghdasaryan <surenb@google.com>
Cc: akpm@linux-foundation.org, viro@zeniv.linux.org.uk,
	brauner@kernel.org, shuah@kernel.org, aarcange@redhat.com,
	lokeshgidra@google.com, peterx@redhat.com, hughd@google.com,
	mhocko@suse.com, axelrasmussen@google.com, rppt@kernel.org,
	willy@infradead.org, Liam.Howlett@oracle.com,
	zhangpeng362@huawei.com, bgeffon@google.com,
	kaleshsingh@google.com, ngeoffray@google.com, jdduke@google.com,
	linux-mm@kvack.org, linux-fsdevel@vger.kernel.org,
	linux-kernel@vger.kernel.org, linux-kselftest@vger.kernel.org,
	kernel-team@android.com
Subject: Re: [PATCH v2 2/3] userfaultfd: UFFDIO_REMAP uABI
Date: Wed, 27 Sep 2023 15:29:35 +0200	[thread overview]
Message-ID: <03f95e90-82bd-6ee2-7c0d-d4dc5d3e15ee@redhat.com> (raw)
In-Reply-To: <CAG48ez1N2kryy08eo0dcJ5a9O-3xMT8aOrgrcD+CqBN=cBfdDw@mail.gmail.com>

>> +static int remap_anon_pte(struct mm_struct *dst_mm, struct mm_struct *src_mm,
>> +                         struct vm_area_struct *dst_vma,
>> +                         struct vm_area_struct *src_vma,
>> +                         unsigned long dst_addr, unsigned long src_addr,
>> +                         pte_t *dst_pte, pte_t *src_pte,
>> +                         pte_t orig_dst_pte, pte_t orig_src_pte,
>> +                         spinlock_t *dst_ptl, spinlock_t *src_ptl,
>> +                         struct folio *src_folio)
>> +{
>> +       struct anon_vma *dst_anon_vma;
>> +
>> +       double_pt_lock(dst_ptl, src_ptl);
>> +
>> +       if (!pte_same(*src_pte, orig_src_pte) ||
>> +           !pte_same(*dst_pte, orig_dst_pte) ||
>> +           folio_test_large(src_folio) ||
>> +           folio_estimated_sharers(src_folio) != 1) {

^ here you should check PageAnonExclusive. Please get rid of any 
implicit explicit/implcit mapcount checks.

>> +               double_pt_unlock(dst_ptl, src_ptl);
>> +               return -EAGAIN;
>> +       }
>> +
>> +       BUG_ON(!folio_test_anon(src_folio));
>> +
>> +       dst_anon_vma = (void *)dst_vma->anon_vma + PAGE_MAPPING_ANON;
>> +       WRITE_ONCE(src_folio->mapping,
>> +                  (struct address_space *) dst_anon_vma);

I have some cleanups pending for page_move_anon_rmap(), that moves the 
SetPageAnonExclusive hunk out. Here we should be using 
page_move_anon_rmap() [or rather, folio_move_anon_rmap() after my cleanups]

I'll send them out soonish.

>> +       WRITE_ONCE(src_folio->index, linear_page_index(dst_vma,
>> +                                                     dst_addr)); >> +
>> +       orig_src_pte = ptep_clear_flush(src_vma, src_addr, src_pte);
>> +       orig_dst_pte = mk_pte(&src_folio->page, dst_vma->vm_page_prot);
>> +       orig_dst_pte = maybe_mkwrite(pte_mkdirty(orig_dst_pte),
>> +                                    dst_vma);
> 
> I think there's still a theoretical issue here that you could fix by
> checking for the AnonExclusive flag, similar to the huge page case.
> 
> Consider the following scenario:
> 
> 1. process P1 does a write fault in a private anonymous VMA, creating
> and mapping a new anonymous page A1
> 2. process P1 forks and creates two children P2 and P3. afterwards, A1
> is mapped in P1, P2 and P3 as a COW page, with mapcount 3.
> 3. process P1 removes its mapping of A1, dropping its mapcount to 2.
> 4. process P2 uses vmsplice() to grab a reference to A1 with get_user_pages()
> 5. process P2 removes its mapping of A1, dropping its mapcount to 1.
> 
> If at this point P3 does a write fault on its mapping of A1, it will
> still trigger copy-on-write thanks to the AnonExclusive mechanism; and
> this is necessary to avoid P3 mapping A1 as writable and writing data
> into it that will become visible to P2, if P2 and P3 are in different
> security contexts.
> 
> But if P3 instead moves its mapping of A1 to another address with
> remap_anon_pte() which only does a page mapcount check, the
> maybe_mkwrite() will directly make the mapping writable, circumventing
> the AnonExclusive mechanism.
> 

Yes, can_change_pte_writable() contains the exact logic when we can turn 
something easily writable even if it wasn't writable before. which 
includes that PageAnonExclusive is set. (but with uffd-wp or softdirty 
tracking, there is more to consider)

-- 
Cheers,

David / dhildenb


  reply	other threads:[~2023-09-27 13:30 UTC|newest]

Thread overview: 55+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-09-23  1:31 [PATCH v2 0/3] userfaultfd remap option Suren Baghdasaryan
2023-09-23  1:31 ` [PATCH v2 1/3] userfaultfd: UFFDIO_REMAP: rmap preparation Suren Baghdasaryan
2023-09-28 16:23   ` Peter Xu
2023-09-28 20:03     ` Suren Baghdasaryan
2023-10-02 14:42   ` David Hildenbrand
2023-10-02 15:23     ` Peter Xu
2023-10-02 17:30       ` David Hildenbrand
2023-10-03 17:56         ` Suren Baghdasaryan
2023-09-23  1:31 ` [PATCH v2 2/3] userfaultfd: UFFDIO_REMAP uABI Suren Baghdasaryan
2023-09-27 10:06   ` potential new userfaultfd vs khugepaged conflict [was: Re: [PATCH v2 2/3] userfaultfd: UFFDIO_REMAP uABI] Jann Horn
2023-09-27 17:12     ` Suren Baghdasaryan
2023-09-28 15:29       ` Jann Horn
2023-09-27 12:47   ` [PATCH v2 2/3] userfaultfd: UFFDIO_REMAP uABI Jann Horn
2023-09-27 13:29     ` David Hildenbrand [this message]
2023-09-27 18:25       ` Suren Baghdasaryan
2023-09-28 16:28         ` Peter Xu
2023-09-28 17:15         ` David Hildenbrand
2023-09-28 18:32           ` Suren Baghdasaryan
2023-09-28 20:11             ` Suren Baghdasaryan
2023-09-28 19:00           ` Peter Xu
2023-10-02  7:49             ` David Hildenbrand
2023-09-28 16:24       ` Peter Xu
2023-09-28 17:05         ` David Hildenbrand
2023-09-28 17:21           ` Peter Xu
2023-09-28 17:51             ` David Hildenbrand
2023-09-28 18:34               ` Peter Xu
2023-09-28 19:47                 ` Suren Baghdasaryan
2023-10-02  8:00                 ` David Hildenbrand
2023-10-02 15:21                   ` Peter Xu
2023-10-02 15:46                     ` Lokesh Gidra
2023-10-02 15:55                       ` Lokesh Gidra
2023-10-02 17:43                         ` David Hildenbrand
2023-10-02 19:33                           ` Lokesh Gidra
2023-10-03 20:04                             ` Suren Baghdasaryan
2023-10-03 20:21                               ` Peter Xu
2023-10-03 21:08                                 ` David Hildenbrand
2023-10-03 21:20                                   ` Peter Xu
2023-10-03 22:26                                     ` Suren Baghdasaryan
2023-10-03 23:39                                       ` Lokesh Gidra
2023-10-06 12:30                                         ` David Hildenbrand
2023-10-06 15:02                                           ` Suren Baghdasaryan
2023-10-03 21:04                               ` David Hildenbrand
2023-10-02 17:33                     ` David Hildenbrand
2023-10-02 17:36                       ` David Hildenbrand
2023-09-27 18:07     ` Suren Baghdasaryan
2023-09-27 20:04       ` Jann Horn
2023-09-27 20:42         ` Suren Baghdasaryan
2023-09-27 21:08           ` Suren Baghdasaryan
2023-09-27 22:48             ` Jann Horn
2023-09-28 15:36               ` Suren Baghdasaryan
2023-09-28 17:09   ` Peter Xu
2023-09-28 18:23     ` Suren Baghdasaryan
2023-09-28 18:43   ` Peter Xu
2023-09-28 19:50     ` Suren Baghdasaryan
2023-09-23  1:31 ` [PATCH v2 3/3] selftests/mm: add UFFDIO_REMAP ioctl test Suren Baghdasaryan

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=03f95e90-82bd-6ee2-7c0d-d4dc5d3e15ee@redhat.com \
    --to=david@redhat.com \
    --cc=Liam.Howlett@oracle.com \
    --cc=aarcange@redhat.com \
    --cc=akpm@linux-foundation.org \
    --cc=axelrasmussen@google.com \
    --cc=bgeffon@google.com \
    --cc=brauner@kernel.org \
    --cc=hughd@google.com \
    --cc=jannh@google.com \
    --cc=jdduke@google.com \
    --cc=kaleshsingh@google.com \
    --cc=kernel-team@android.com \
    --cc=linux-fsdevel@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-kselftest@vger.kernel.org \
    --cc=linux-mm@kvack.org \
    --cc=lokeshgidra@google.com \
    --cc=mhocko@suse.com \
    --cc=ngeoffray@google.com \
    --cc=peterx@redhat.com \
    --cc=rppt@kernel.org \
    --cc=shuah@kernel.org \
    --cc=surenb@google.com \
    --cc=viro@zeniv.linux.org.uk \
    --cc=willy@infradead.org \
    --cc=zhangpeng362@huawei.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).