mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: Zi Yan <ziy@nvidia.com>
To: David Hildenbrand <david@kernel.org>,
	 Baolin Wang <baolin.wang@linux.alibaba.com>,
	 "Liam R. Howlett" <liam@infradead.org>,
	Nico Pache <nico.pache@linux.dev>,
	 Ryan Roberts <ryan.roberts@arm.com>, Dev Jain <dev.jain@arm.com>,
	 Barry Song <baohua@kernel.org>,
	Lance Yang <lance.yang@linux.dev>,
	 Usama Arif <usama.arif@linux.dev>,
	Kiryl Shutsemau <kas@kernel.org>,
	 Lorenzo Stoakes <ljs@kernel.org>,
	Rik van Riel <riel@surriel.com>,
	 Vlastimil Babka <vbabka@kernel.org>,
	Harry Yoo <harry@kernel.org>,  Jann Horn <jannh@google.com>,
	Kyle Zeng <kylebot@openai.com>,
	 Andrew Morton <akpm@linux-foundation.org>
Cc: linux-mm@kvack.org, linux-kernel@vger.kernel.org,
	 Zi Yan <ziy@nvidia.com>,
	stable@vger.kernel.org,  Sashiko <sashiko-bot@kernel.org>,
	Jisheng Zhang <jszhang@kernel.org>,
	 Paul Walmsley <pjw@kernel.org>,
	Palmer Dabbelt <palmer@dabbelt.com>,
	 Albert Ou <aou@eecs.berkeley.edu>,
	Alexandre Ghiti <alex@ghiti.fr>,  Zong Li <zong.li@sifive.com>,
	Deepak Gupta <debug@rivosinc.com>,
	 Palmer Dabbelt <palmer@rivosinc.com>,
	John Hubbard <jhubbard@nvidia.com>,  Peter Xu <peterx@redhat.com>,
	linux-riscv@lists.infradead.org,  Jason Gunthorpe <jgg@ziepe.ca>
Subject: [PATCH v2 3/9] riscv: mm: don't let GUP-fast follow entries without _PAGE_READ
Date: Thu, 03 Dec 2026 23:17:51 -0500	[thread overview]
Message-ID: <20261203-anon_vma_uaf_fix-v2-3-3d4c746e8839@nvidia.com> (raw)
In-Reply-To: <20261203-anon_vma_uaf_fix-v2-0-3d4c746e8839@nvidia.com>

riscv maps PROT_EXEC alone as PAGE_EXEC, which has
_PAGE_EXEC but not _PAGE_READ, and a writable shadow stack entry is
XWR = 010, which has _PAGE_WRITE but not _PAGE_READ. riscv uses the
generic pte_access_permitted(), pmd_access_permitted() and
pud_access_permitted(), which only check that the entry is present and,
for a write, writable. So GUP-fast follows both kinds of entries:

- a read, e.g. from vmsplice() or an O_DIRECT write, can read an
  execute-only page, which userspace cannot load from;
- a write, e.g. from an O_DIRECT read, can modify a shadow stack page,
  as pte_write() is true for it.

Slow GUP rejects both: check_vma_flags() fails a read from a VMA without
VM_READ and a write to a VM_SHADOW_STACK VMA, unless FOLL_FORCE is set.

Fix it by requiring both _PAGE_PRESENT and _PAGE_READ in
pte_access_permitted(), and using it for pmd_access_permitted() and
pud_access_permitted() too.

Requiring _PAGE_PRESENT also stops GUP-fast from following a THP PMD
invalidated by pmdp_invalidate(), whose pmd_present() is still true.
Code that invalidates a PMD and then decides, from the folio refcount,
whether it may drop the mapping relies on that. x86, arm64, and powerpc
do the same thing.

Fixes: 3f910b7a522e ("riscv: enable HAVE_FAST_GUP if MMU")
Fixes: c68c2ef9d641 ("riscv/mm: teach pte_mkwrite to manufacture shadow stack PTEs")
Cc: stable@vger.kernel.org
Reported-by: Sashiko <sashiko-bot@kernel.org>
Closes: https://sashiko.dev/#/patchset/20261015-anon_vma_uaf_fix-v1-0-7b977303b42b%40nvidia.com?part=2
Reviewed-by: Jisheng Zhang <jszhang@kernel.org>
Signed-off-by: Zi Yan <ziy@nvidia.com>
To: Paul Walmsley <pjw@kernel.org>
To: Palmer Dabbelt <palmer@dabbelt.com>
To: Albert Ou <aou@eecs.berkeley.edu>
Cc: Alexandre Ghiti <alex@ghiti.fr>
Cc: Jisheng Zhang <jszhang@kernel.org>
Cc: Zong Li <zong.li@sifive.com>
Cc: Deepak Gupta <debug@rivosinc.com>
Cc: Palmer Dabbelt <palmer@rivosinc.com>
Cc: Jason Gunthorpe <jgg@nvidia.com>
Cc: John Hubbard <jhubbard@nvidia.com>
Cc: Peter Xu <peterx@redhat.com>
Cc: linux-riscv@lists.infradead.org
Cc: linux-kernel@vger.kernel.org
---
 arch/riscv/include/asm/pgtable.h | 27 +++++++++++++++++++++++++++
 1 file changed, 27 insertions(+)

diff --git a/arch/riscv/include/asm/pgtable.h b/arch/riscv/include/asm/pgtable.h
index 07ccacd51ab54..dcbcfa38a27c2 100644
--- a/arch/riscv/include/asm/pgtable.h
+++ b/arch/riscv/include/asm/pgtable.h
@@ -373,6 +373,21 @@ static inline int pte_write(pte_t pte)
 	return pte_val(pte) & _PAGE_WRITE;
 }
 
+/*
+ * GUP-fast only follows entries that are present and have _PAGE_READ,
+ * and leaves the rest to slow GUP, which checks the VMA: an execute-only
+ * entry and a writable shadow stack entry (XWR = 010) lack _PAGE_READ, a
+ * PROT_NONE or invalidated entry lacks _PAGE_PRESENT. Any other writable
+ * entry is readable too.
+ */
+#define pte_access_permitted pte_access_permitted
+static inline bool pte_access_permitted(pte_t pte, bool write)
+{
+	const unsigned long need = _PAGE_PRESENT | _PAGE_READ;
+
+	return (pte_val(pte) & need) == need && (!write || pte_write(pte));
+}
+
 static inline int pte_exec(pte_t pte)
 {
 	return pte_val(pte) & _PAGE_EXEC;
@@ -817,12 +832,24 @@ static inline int pmd_write(pmd_t pmd)
 	return pte_write(pmd_pte(pmd));
 }
 
+#define pmd_access_permitted pmd_access_permitted
+static inline bool pmd_access_permitted(pmd_t pmd, bool write)
+{
+	return pte_access_permitted(pmd_pte(pmd), write);
+}
+
 #define pud_write pud_write
 static inline int pud_write(pud_t pud)
 {
 	return pte_write(pud_pte(pud));
 }
 
+#define pud_access_permitted pud_access_permitted
+static inline bool pud_access_permitted(pud_t pud, bool write)
+{
+	return pte_access_permitted(pud_pte(pud), write);
+}
+
 #define pmd_dirty pmd_dirty
 static inline int pmd_dirty(pmd_t pmd)
 {

-- 
2.53.0


  parent reply	other threads:[~2026-12-04  4:18 UTC|newest]

Thread overview: 22+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-12-04  4:17 [PATCH v2 0/9] Re-enable discarding lazyfree PMD THPs and transient none PMD fixes Zi Yan
2026-12-04  4:17 ` [PATCH v2 1/9] mm/migrate: do not clear device private entry in set_pmd_migration_entry() Zi Yan
2026-12-05  4:06   ` Lance Yang
2026-12-04  4:17 ` [PATCH v2 2/9] powerpc/book3s64/mm: don't count invalidated PMDs in page table check Zi Yan
2026-12-04  4:17 ` Zi Yan [this message]
2026-12-04  4:17 ` [PATCH v2 4/9] LoongArch: mm: don't let GUP-fast follow invalidated THP PMDs Zi Yan
2026-12-04  4:17 ` [PATCH v2 5/9] s390/mm: always clear _SEGMENT_ENTRY_READ in pmdp_invalidate() Zi Yan
2026-12-04 12:31   ` Alexander Gordeev
2026-12-04  4:17 ` [PATCH v2 6/9] sparc64: mm: fix thp_pte_count accounting around pmdp_invalidate() Zi Yan
2026-12-04  4:17 ` [PATCH v2 7/9] mm/huge_memory: avoid transient none PMDs during lazyfree reclaim Zi Yan
2026-12-04  4:35   ` Liam R. Howlett
2026-12-04  9:38   ` Baolin Wang
2026-12-04 13:23   ` Lorenzo Stoakes (ARM)
2026-12-05  4:35   ` Lance Yang
2026-12-05  5:20     ` Lance Yang
2026-12-04  4:17 ` [PATCH v2 8/9] mm/rmap: re-enable discarding lazyfree THPs at PMD level Zi Yan
2026-12-04  4:37   ` Liam R. Howlett
2026-12-04  9:49   ` Baolin Wang
2026-12-05  5:30   ` Lance Yang
2026-12-04  4:17 ` [PATCH v2 9/9] userfaultfd: avoid transient none PMDs during UFFDIO_MOVE Zi Yan
2026-12-04 12:52   ` Lorenzo Stoakes (ARM)
2026-12-05  5:45   ` Lance Yang

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=20261203-anon_vma_uaf_fix-v2-3-3d4c746e8839@nvidia.com \
    --to=ziy@nvidia.com \
    --cc=akpm@linux-foundation.org \
    --cc=alex@ghiti.fr \
    --cc=aou@eecs.berkeley.edu \
    --cc=baohua@kernel.org \
    --cc=baolin.wang@linux.alibaba.com \
    --cc=david@kernel.org \
    --cc=debug@rivosinc.com \
    --cc=dev.jain@arm.com \
    --cc=harry@kernel.org \
    --cc=jannh@google.com \
    --cc=jgg@ziepe.ca \
    --cc=jhubbard@nvidia.com \
    --cc=jszhang@kernel.org \
    --cc=kas@kernel.org \
    --cc=kylebot@openai.com \
    --cc=lance.yang@linux.dev \
    --cc=liam@infradead.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-mm@kvack.org \
    --cc=linux-riscv@lists.infradead.org \
    --cc=ljs@kernel.org \
    --cc=nico.pache@linux.dev \
    --cc=palmer@dabbelt.com \
    --cc=palmer@rivosinc.com \
    --cc=peterx@redhat.com \
    --cc=pjw@kernel.org \
    --cc=riel@surriel.com \
    --cc=ryan.roberts@arm.com \
    --cc=sashiko-bot@kernel.org \
    --cc=stable@vger.kernel.org \
    --cc=usama.arif@linux.dev \
    --cc=vbabka@kernel.org \
    --cc=zong.li@sifive.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

all inboxes | Powered by JetHome®