From: David Laight <David.Laight@ACULAB.COM>
To: 'David Hildenbrand' <david@redhat.com>,
"linux-kernel@vger.kernel.org" <linux-kernel@vger.kernel.org>
Cc: "linux-mm@kvack.org" <linux-mm@kvack.org>,
"stable@vger.kernel.org" <stable@vger.kernel.org>,
Linus Torvalds <torvalds@linux-foundation.org>,
Andrew Morton <akpm@linux-foundation.org>,
Greg Kroah-Hartman <gregkh@linuxfoundation.org>,
Axel Rasmussen <axelrasmussen@google.com>,
Nadav Amit <nadav.amit@gmail.com>, Peter Xu <peterx@redhat.com>,
Hugh Dickins <hughd@google.com>,
Andrea Arcangeli <aarcange@redhat.com>,
Matthew Wilcox <willy@infradead.org>,
Vlastimil Babka <vbabka@suse.cz>,
John Hubbard <jhubbard@nvidia.com>,
Jason Gunthorpe <jgg@nvidia.com>
Subject: RE: [PATCH v2] mm/gup: fix FOLL_FORCE COW security issue and remove FOLL_COW
Date: Wed, 10 Aug 2022 09:12:57 +0000 [thread overview]
Message-ID: <afab7f23d10145b590aef44b3242db64@AcuMS.aculab.com> (raw)
In-Reply-To: <20220809205640.70916-1-david@redhat.com>
From: David Hildenbrand
> Sent: 09 August 2022 21:57
...
These two functions seem to contain a lot of the same tests.
They also seem a bit large for 'inline'.
> -static inline bool can_follow_write_pte(pte_t pte, unsigned int flags)
> +/* FOLL_FORCE can write to even unwritable PTEs in COW mappings. */
> +static inline bool can_follow_write_pte(pte_t pte, struct page *page,
> + struct vm_area_struct *vma,
> + unsigned int flags)
> {
> - return pte_write(pte) ||
> - ((flags & FOLL_FORCE) && (flags & FOLL_COW) && pte_dirty(pte));
> + /* If the pte is writable, we can write to the page. */
> + if (pte_write(pte))
> + return true;
> +
> + /* Maybe FOLL_FORCE is set to override it? */
> + if (!(flags & FOLL_FORCE))
> + return false;
> +
> + /* But FOLL_FORCE has no effect on shared mappings */
> + if (vma->vm_flags & (VM_MAYSHARE | VM_SHARED))
> + return false;
> +
> + /* ... or read-only private ones */
> + if (!(vma->vm_flags & VM_MAYWRITE))
> + return false;
> +
> + /* ... or already writable ones that just need to take a write fault */
> + if (vma->vm_flags & VM_WRITE)
> + return false;
> +
> + /*
> + * See can_change_pte_writable(): we broke COW and could map the page
> + * writable if we have an exclusive anonymous page ...
> + */
> + if (!page || !PageAnon(page) || !PageAnonExclusive(page))
> + return false;
> +
> + /* ... and a write-fault isn't required for other reasons. */
> + if (vma_soft_dirty_enabled(vma) && !pte_soft_dirty(pte))
> + return false;
> + return !userfaultfd_pte_wp(vma, pte);
> }
...
> -static inline bool can_follow_write_pmd(pmd_t pmd, unsigned int flags)
> +/* FOLL_FORCE can write to even unwritable PMDs in COW mappings. */
> +static inline bool can_follow_write_pmd(pmd_t pmd, struct page *page,
> + struct vm_area_struct *vma,
> + unsigned int flags)
> {
> - return pmd_write(pmd) ||
> - ((flags & FOLL_FORCE) && (flags & FOLL_COW) && pmd_dirty(pmd));
> + /* If the pmd is writable, we can write to the page. */
> + if (pmd_write(pmd))
> + return true;
> +
> + /* Maybe FOLL_FORCE is set to override it? */
> + if (!(flags & FOLL_FORCE))
> + return false;
> +
> + /* But FOLL_FORCE has no effect on shared mappings */
> + if (vma->vm_flags & (VM_MAYSHARE | VM_SHARED))
> + return false;
> +
> + /* ... or read-only private ones */
> + if (!(vma->vm_flags & VM_MAYWRITE))
> + return false;
> +
> + /* ... or already writable ones that just need to take a write fault */
> + if (vma->vm_flags & VM_WRITE)
> + return false;
> +
> + /*
> + * See can_change_pte_writable(): we broke COW and could map the page
> + * writable if we have an exclusive anonymous page ...
> + */
> + if (!page || !PageAnon(page) || !PageAnonExclusive(page))
> + return false;
> +
> + /* ... and a write-fault isn't required for other reasons. */
> + if (vma_soft_dirty_enabled(vma) && !pmd_soft_dirty(pmd))
> + return false;
> + return !userfaultfd_huge_pmd_wp(vma, pmd);
> }
Perhaps only the initial call (common success path?) should
be inlined?
With the flags and vma tests being moved to an inline helper.
David
-
Registered Address Lakeside, Bramley Road, Mount Farm, Milton Keynes, MK1 1PT, UK
Registration No: 1397386 (Wales)
next prev parent reply other threads:[~2022-08-10 9:13 UTC|newest]
Thread overview: 3+ messages / expand[flat|nested] mbox.gz Atom feed top
2022-08-09 20:56 David Hildenbrand
2022-08-10 9:12 ` David Laight [this message]
2022-08-10 9:18 ` David Hildenbrand
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=afab7f23d10145b590aef44b3242db64@AcuMS.aculab.com \
--to=david.laight@aculab.com \
--cc=aarcange@redhat.com \
--cc=akpm@linux-foundation.org \
--cc=axelrasmussen@google.com \
--cc=david@redhat.com \
--cc=gregkh@linuxfoundation.org \
--cc=hughd@google.com \
--cc=jgg@nvidia.com \
--cc=jhubbard@nvidia.com \
--cc=linux-kernel@vger.kernel.org \
--cc=linux-mm@kvack.org \
--cc=nadav.amit@gmail.com \
--cc=peterx@redhat.com \
--cc=stable@vger.kernel.org \
--cc=torvalds@linux-foundation.org \
--cc=vbabka@suse.cz \
--cc=willy@infradead.org \
/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