From: "David Hildenbrand (Arm)" <david@kernel.org>
To: YinFengwei <fengwei_yin@linux.alibaba.com>
Cc: Peter Zijlstra <peterz@infradead.org>,
Ingo Molnar <mingo@redhat.com>,
Arnaldo Carvalho de Melo <acme@kernel.org>,
Namhyung Kim <namhyung@kernel.org>,
Mark Rutland <mark.rutland@arm.com>,
Alexander Shishkin <alexander.shishkin@linux.intel.com>,
Jiri Olsa <jolsa@kernel.org>, Ian Rogers <irogers@google.com>,
Adrian Hunter <adrian.hunter@intel.com>,
James Clark <james.clark@linaro.org>,
Masami Hiramatsu <mhiramat@kernel.org>,
Oleg Nesterov <oleg@redhat.com>,
Andrii Nakryiko <andrii@kernel.org>,
Nicolas Pitre <nico@fluxnic.net>,
"Liam R. Howlett" <liam@infradead.org>,
Vlastimil Babka <vbabka@kernel.org>,
Mike Rapoport <rppt@kernel.org>, Michal Hocko <mhocko@suse.com>,
Andrew Morton <akpm@linux-foundation.org>,
Lorenzo Stoakes <ljs@kernel.org>,
Suren Baghdasaryan <surenb@google.com>,
Shakeel Butt <shakeel.butt@linux.dev>,
Pedro Falcato <pfalcato@suse.de>, Jann Horn <jannh@google.com>,
Jan Kara <jack@suse.cz>,
Stephen Smalley <stephen.smalley.work@gmail.com>,
Paul Moore <paul@paul-moore.com>, Chris Mason <mason@kernel.org>,
linux-kernel@vger.kernel.org, linux-trace-kernel@vger.kernel.org,
linux-perf-users@vger.kernel.org, linux-mm@kvack.org,
selinux@vger.kernel.org
Subject: Re: [PATCH 2/2] mm/memory: store the PFN of CoW PFNMAP mappings in the anon page offset
Date: Thu, 3 Dec 2026 12:57:47 +0100 [thread overview]
Message-ID: <68ea31b0-ca4c-4504-8fc7-99261446f7fe@kernel.org> (raw)
In-Reply-To: <axDUQrXHSCHASqta@U-V2QX163P-2032.local>
On 12/3/26 03:30, YinFengwei wrote:
> On Sat, Nov 28, 2026 at 11:15:11PM +0100, David Hildenbrand (Arm) wrote:
>> remap_pfn_range() and its mmap_prepare variant overwrite vm_pgoff with the
>> first PFN of the range in CoW mappings, such that __vm_normal_page() can
>> reliably identify CoW'ed anon folios without arch support for pte_special.
>>
>> However, vm_pgoff is also exposed to user space as the file offset,
>> through /proc/pid/maps, /proc/pid/smaps, PROCMAP_QUERY, PERF_RECORD_MMAP
>> and the NT_FILE note of core dumps, whereas /proc/pid/pagemap only
>> reports PFNs to CAP_SYS_ADMIN.
>>
>> Consequently, such mappings can leak physical addresses to user space. In
>> particular, since commit 8be71e1ba093 ("selinux: mark status and policy
>> inodes as immutable, drop status mmap write checks"), a MAP_PRIVATE
>> mapping of /sys/fs/selinux/status can leak PFNs that way.
>>
>> We don't want to expose the PFNs of mapped pages to user space.
>>
>> Now that we have the anon page offset per VMA, let's fix it by storing
>> the PFN in there instead. That value is never exported to user space.
>>
>> We already handle the anon page offset properly during VMA splits etc.
>> Only copy_vma() must be taught to not reset it for unmergeable VMAs,
>> which includes any PFNMAP mappings.
>>
>> This has medium urgency. Do not CC stable, as backports would be tricky
>> either way and not worth it.
>>
>> Fixes: 8be71e1ba093 ("selinux: mark status and policy inodes as immutable, drop status mmap write checks")
>> Reported-by: Chris Mason <mason@kernel.org>
>> Signed-off-by: David Hildenbrand (Arm) <david@kernel.org>
>> ---
>> fs/cramfs/inode.c | 7 +-----
>> include/linux/mm.h | 10 +++++---
>> mm/memory.c | 55 ++++++++++++++++++++---------------------
>> mm/vma.c | 6 ++---
>> mm/vma.h | 3 +--
>> tools/testing/vma/include/dup.h | 3 ++-
>> 6 files changed, 41 insertions(+), 43 deletions(-)
>>
>> diff --git a/fs/cramfs/inode.c b/fs/cramfs/inode.c
>> index d4cd03f4f60d5..9b05250b2198c 100644
>> --- a/fs/cramfs/inode.c
>> +++ b/fs/cramfs/inode.c
>> @@ -402,12 +402,7 @@ static int cramfs_physmem_mmap(struct file *file, struct vm_area_struct *vma)
>> }
>>
>> if (pages == vma_pages(vma)) {
>> - /*
>> - * The entire vma is mappable. remap_pfn_range() will
>> - * make it distinguishable from a non-direct mapping
>> - * in /proc/<pid>/maps by substituting the file offset
>> - * with the actual physical address.
>> - */
>> + /* The entire vma is mappable. */
>> ret = remap_pfn_range(vma, vma->vm_start, address >> PAGE_SHIFT,
>> pages * PAGE_SIZE, vma->vm_page_prot);
>> } else {
>> diff --git a/include/linux/mm.h b/include/linux/mm.h
>> index abd427c136409..00abd4d250a99 100644
>> --- a/include/linux/mm.h
>> +++ b/include/linux/mm.h
>> @@ -1766,7 +1766,8 @@ static inline bool vma_flags_can_merge(const vma_flags_t *flags)
>> * mapping not embodied in any attribute of the VMA.
>> *
>> * Additionally, private (CoW) PFN maps encode the source PFN of the
>> - * range in vma->vm_pgoff, which may otherwise cause spurious merges.
>> + * range in the anonymous page offset, which may otherwise cause
>> + * spurious merges.
>> */
>> if (!vma_flags_is_mm_backed(flags))
>> return false;
>> @@ -4625,8 +4626,7 @@ static inline unsigned long vma_pages(const struct vm_area_struct *vma)
>> * If @vma is a MAP_PRIVATE file-backed mapping, then this returns the
>> * page offset within the file.
>> *
>> - * Edge cases: nommu does not abide by these and CoW MAP_PRIVATE-pfnmap regions
>> - * have their page offset set to the first PFN in the range.
>> + * Edge cases: nommu does not abide by these.
>> *
>> * Returns: The page offset of the start of @vma.
>> */
>> @@ -4680,6 +4680,10 @@ static inline pgoff_t vma_last_pgoff(const struct vm_area_struct *vma)
>> * This value is used for tracking MAP_PRIVATE file-backed mappings by their
>> * anonymous page offset.
>> *
>> + * Edge cases: unmergeable VMAs keep their anonymous page offset when moved
>> + * while unfaulted, and CoW MAP_PRIVATE-pfnmap regions have it set to the
>> + * first PFN in the range.
>> + *
>> * Returns: The anonymous page offset of the start of @vma.
>> */
>> static inline pgoff_t vma_start_anon_pgoff(const struct vm_area_struct *vma)
>> diff --git a/mm/memory.c b/mm/memory.c
>> index 90fc7c3951247..82e1ec8e44e74 100644
>> --- a/mm/memory.c
>> +++ b/mm/memory.c
>> @@ -687,10 +687,11 @@ static void print_bad_pte(struct vm_area_struct *vma, unsigned long addr,
>> *
>> * The way we recognize COWed pages within VM_PFNMAP mappings is through the
>> * rules set up by "remap_pfn_range()": the vma will have the VM_PFNMAP bit
>> - * set, and the vm_pgoff will point to the first PFN mapped: thus every special
>> - * mapping will always honor the rule
>> + * set, and the anonymous page offset will point to the first PFN mapped: thus
>> + * every special mapping will always honor the rule
>> *
>> - * pfn_of_page == vma->vm_pgoff + ((addr - vma->vm_start) >> PAGE_SHIFT)
>> + * pfn_of_page == vma_start_anon_pgoff(vma) +
>> + * ((addr - vma->vm_start) >> PAGE_SHIFT)
>> *
>> * And for normal mappings this is false.
>> *
>> @@ -745,13 +746,11 @@ static inline struct page *__vm_normal_page(struct vm_area_struct *vma,
>> if (!pfn_valid(pfn))
>> return NULL;
>> } else {
>> - const pgoff_t index = linear_page_index(vma, addr);
>> -
>> /* Only CoW'ed anon folios are "normal". */
>> - if (pfn == index)
>> - return NULL;
>> if (!vma_is_cow_mapping(vma))
>> return NULL;
>> + if (pfn == linear_anon_page_index(vma, addr))
>> + return NULL;
>> }
>> }
>>
>> @@ -3217,22 +3216,17 @@ static inline int remap_p4d_range(struct mm_struct *mm, pgd_t *pgd,
>> return 0;
>> }
>>
>> -static int get_remap_pgoff(bool is_cow, unsigned long addr,
>> - unsigned long end, unsigned long vm_start, unsigned long vm_end,
>> - unsigned long pfn, pgoff_t *vm_pgoff_p)
>> +static int check_cow_pfnmap_range(unsigned long addr, unsigned long end,
>> + unsigned long vm_start, unsigned long vm_end)
>> {
>> /*
>> * There's a horrible special case to handle copy-on-write
>> * behaviour that some programs depend on. We mark the "original"
>> - * un-COW'ed pages by matching them up with "vma->vm_pgoff".
>> - * See vm_normal_page() for details.
>> + * un-COW'ed pages by matching them up with the anonymous page
>> + * offset. See vm_normal_page() for details.
>> */
Thanks for the review.
> I wonder if the comment above shoud be moved just before
> vma_set_anon_pgoff(vma, pfn);
> in remap_pfn_range_prepare_vma()? As this function doesn't update the
> anonymous page offset.
Ack, I'll move this into the new vma_cow_pfnmap_pfn().
Thanks!
--
Cheers,
David
next prev parent reply other threads:[~2026-12-03 11:57 UTC|newest]
Thread overview: 12+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-11-28 22:15 [PATCH 0/2] mm: don't leak PFNs of CoW PFNMAP mappings to user space David Hildenbrand (Arm)
2026-11-28 22:15 ` [PATCH 1/2] uprobes: skip mappings not managed by core mm David Hildenbrand (Arm)
2026-12-01 0:42 ` Masami Hiramatsu (Google)
2026-12-01 9:36 ` David Hildenbrand (Arm)
2026-11-28 22:15 ` [PATCH 2/2] mm/memory: store the PFN of CoW PFNMAP mappings in the anon page offset David Hildenbrand (Arm)
2026-12-01 18:25 ` Lorenzo Stoakes (ARM)
2026-12-01 20:07 ` David Hildenbrand (Arm)
2026-12-02 21:04 ` Lorenzo Stoakes (ARM)
2026-12-02 21:40 ` David Hildenbrand (Arm)
2026-12-03 2:30 ` YinFengwei
2026-12-03 11:57 ` David Hildenbrand (Arm) [this message]
2026-11-28 22:37 ` [PATCH 0/2] mm: don't leak PFNs of CoW PFNMAP mappings to user space sashiko-bot
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=68ea31b0-ca4c-4504-8fc7-99261446f7fe@kernel.org \
--to=david@kernel.org \
--cc=acme@kernel.org \
--cc=adrian.hunter@intel.com \
--cc=akpm@linux-foundation.org \
--cc=alexander.shishkin@linux.intel.com \
--cc=andrii@kernel.org \
--cc=fengwei_yin@linux.alibaba.com \
--cc=irogers@google.com \
--cc=jack@suse.cz \
--cc=james.clark@linaro.org \
--cc=jannh@google.com \
--cc=jolsa@kernel.org \
--cc=liam@infradead.org \
--cc=linux-kernel@vger.kernel.org \
--cc=linux-mm@kvack.org \
--cc=linux-perf-users@vger.kernel.org \
--cc=linux-trace-kernel@vger.kernel.org \
--cc=ljs@kernel.org \
--cc=mark.rutland@arm.com \
--cc=mason@kernel.org \
--cc=mhiramat@kernel.org \
--cc=mhocko@suse.com \
--cc=mingo@redhat.com \
--cc=namhyung@kernel.org \
--cc=nico@fluxnic.net \
--cc=oleg@redhat.com \
--cc=paul@paul-moore.com \
--cc=peterz@infradead.org \
--cc=pfalcato@suse.de \
--cc=rppt@kernel.org \
--cc=selinux@vger.kernel.org \
--cc=shakeel.butt@linux.dev \
--cc=stephen.smalley.work@gmail.com \
--cc=surenb@google.com \
--cc=vbabka@kernel.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