From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=100.103.45.18 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1796301241; b=ryWeNaMvl90dUr9V8eZ8pTWLInTQH83IWifKgutBJXVrmkUg/GU1au0cOkvAT2Yjz3XY dboytBnQtTKvMzRr/4qhTv3Wo2SERlhxJjrwImlZ+w8n+AWT6C3caTRfwxgIZSS+ftj9W VEE8GPz2GoRD0+GgtpAuT/AccKSHrhSJVb3rukFFzH68FL0mGErPMeyrMWbYzBke+LKyl h78Hz35FPVp6EbK8gB109Cuc5hpcVmdVC/QSe7yRr/TrP+OZ+0EIZezxM7pRFrfW28BxR u4D2Ahl6g05qls1fzyyQE8m6qmm8ExSTwoL0yRPusvHYCDfLdmoTfLS/joQ51NoMLOQ== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796301241; h=Received:DKIM-Signature:From:Date:Subject:MIME-Version:Content-Type: Content-Transfer-Encoding:Message-Id:References:In-Reply-To:To:Cc: X-Mailer:X-Developer-Signature:X-Developer-Key; bh=pCGpOPGwljivawzBtMAB6yKs4YewFkHyLEJMsjRVfug=; b=xT60BvxgU80b4i6L4joiCV9SW15iajDFBOyD9t78XBMr4wysHL1RTBRUaI2XU4UPsulG 5vXr9ii9Ol1w+UTr7Tgy8aoao2vT5jT7T6be3dvEnBK7WIgU2NdKlomUa2MtJxDvzmKfv zyrUx55rss/J2ArR8vPkKRe3DLB3ZZg3Gn9N92DfEPVRGBHZhByGYVoM/2Z7+Hz72Z0JQ AElsgDTUtle/kxN/otRNr7UseXAerp7/lduTPrMuET4IewVAd7uvtyCFd5xTJap6ebFSD 64Afzg7wUIo/RLFi03JZ8bcMB3BdV8DWN2kbw5Dt1G6IASpj9ol8yOoY0ukYu+rUnAg== ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass header.d=kernel.org header.i=@kernel.org header.a=rsa-sha256 header.s=k20260515 header.b="lgo/n7rZ"; arc=none smtp.remote-ip=100.103.45.18 Received: from smtp.kernel.org (quasi.space.kernel.org [100.103.45.18]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (prime256v1)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id E7DAE606A336; Thu, 3 Dec 2026 12:34:00 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.a=rsa-sha256 header.s=k20260515 header.b=lgo/n7rZ Received: by smtp.kernel.org (Postfix) with ESMTPSA id C30A31F000C1; Thu, 3 Dec 2026 12:33:51 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=kernel.org; s=k20260515; t=1796301240; bh=pCGpOPGwljivawzBtMAB6yKs4YewFkHyLEJMsjRVfug=; h=From:Date:Subject:References:In-Reply-To:To:Cc; b=lgo/n7rZkr4TjxNgcF35NkkeRFv67vYzC0lu+/QUMvlZ7pB1LjJ/xzgNsp53WZ1Lc snXItLEVmOKU2MmlIqTfHvE/EKfhm7+ePsU7SSM2lcqaxU372+diINaJBEZcaeJgFg imexY7mn+/u2XNbgyHELBeRDLxmzI9HUNWJYIT2kyLmzltz4J5pgOdLdBOa6wXvutN DkbiHiMNgVjhfR2bQ0kRMm1U802QOWkYEDhw4jDMPrksZ2PlSQePJdfysgj6IIAltL 7CPupE21lFStaelXOUZTUCb7J6/UmFuqQwN1w8XV1s4Ap6suWZ9/No2jgJx7JF0Cx0 +98MUmGZs9qFQ== From: "David Hildenbrand (Arm)" Date: Thu, 03 Dec 2026 13:33:25 +0100 Subject: [PATCH v2 2/2] mm/memory: store the PFN of CoW PFNMAP mappings in the anon page offset X-Mailing-List: linux-trace-kernel@vger.kernel.org List-Id: List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: 7bit Message-Id: <20261203-cow_pfnmap_pgoff-v2-2-2e06735c100d@kernel.org> References: <20261203-cow_pfnmap_pgoff-v2-0-2e06735c100d@kernel.org> In-Reply-To: <20261203-cow_pfnmap_pgoff-v2-0-2e06735c100d@kernel.org> To: Peter Zijlstra , Ingo Molnar , Arnaldo Carvalho de Melo , Namhyung Kim , Mark Rutland , Alexander Shishkin , Jiri Olsa , Ian Rogers , Adrian Hunter , James Clark , Masami Hiramatsu , Oleg Nesterov , Andrii Nakryiko , Nicolas Pitre , David Hildenbrand , "Liam R. Howlett" , Vlastimil Babka , Mike Rapoport , Michal Hocko , Andrew Morton , Lorenzo Stoakes , Suren Baghdasaryan , Shakeel Butt , Pedro Falcato , Jann Horn , Jan Kara , Stephen Smalley , Paul Moore Cc: Chris Mason , 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 X-Mailer: b4 0.16.0 X-Developer-Signature: v=1; a=openpgp-sha256; l=10841; i=david@kernel.org; h=from:subject:message-id; bh=tXds8F7cL0Z6JOGzEMPYKWZLoS9P51IuOrX47JZXkkU=; b=owGbwMvMwCF2/OVpCdfQ3ZWMp9WSGLIFE+e4Bd0LXn/j9NMi0+XPw2bybPTilPvMr3fjcu+0v GXtj1JvdJSyMIhxMMiKKbIEBd83ePlyXW21Gx8zzBxWJpAhDFycAjCRxSmMDH9T9saHCxfvZPnt vk7RIYcjYY7j3bBaocbNoqwLL5o8i2VkOPl4h0aItKlb/OXjQV5KSyMXPxPPWFoTd29V0XPblf/ /cQMA X-Developer-Key: i=david@kernel.org; a=openpgp; fpr=1BD9CAAD735C4C3A460DFCCA4DDE10F700FF835A 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 VMAs not managed by core mm, 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 Signed-off-by: David Hildenbrand (Arm) --- fs/cramfs/inode.c | 7 +--- include/linux/mm.h | 9 +++-- mm/memory.c | 81 +++++++++++++++++++++++++---------------- mm/vma.c | 6 +-- mm/vma.h | 3 +- tools/testing/vma/include/dup.h | 3 +- 6 files changed, 63 insertions(+), 46 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//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..778b1354f5d18 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,9 @@ 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: 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..d11074b461e14 100644 --- a/mm/memory.c +++ b/mm/memory.c @@ -649,6 +649,30 @@ static void print_bad_pte(struct vm_area_struct *vma, unsigned long addr, print_bad_page_map(vma, addr, &entry, sizeof(entry), page, PGTABLE_LEVEL_PTE); } +static inline void vma_set_cow_pfnmap_pfn(struct vm_area_struct *vma, + unsigned long pfn) +{ + VM_WARN_ON_ONCE(!vma_test(vma, VMA_PFNMAP_BIT)); + VM_WARN_ON_ONCE(!vma_is_cow_mapping(vma)); + + /* + * 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 the anonymous page + * offset. See vm_normal_page() for details. + */ + vma_set_anon_pgoff(vma, pfn); +} + +static inline unsigned long vma_cow_pfnmap_pfn(const struct vm_area_struct *vma, + unsigned long addr) +{ + VM_WARN_ON_ONCE(!vma_test(vma, VMA_PFNMAP_BIT)); + VM_WARN_ON_ONCE(!vma_is_cow_mapping(vma)); + + return linear_anon_page_index(vma, addr); +} + /** * __vm_normal_page() - Get the "struct page" associated with a page table entry. * @vma: The VMA mapping the page table entry. @@ -687,10 +711,10 @@ 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_cow_pfnmap_pfn(vma, addr) * * And for normal mappings this is false. * @@ -745,13 +769,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 == vma_cow_pfnmap_pfn(vma, addr)) + return NULL; } } @@ -3217,22 +3239,12 @@ 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. - */ - if (is_cow) { - if (addr != vm_start || end != vm_end) - return -EINVAL; - *vm_pgoff_p = pfn; - } - + /* We must span the entire VMA, see vma_set_cow_pfnmap_pfn(). */ + if (addr != vm_start || end != vm_end) + return -EINVAL; return 0; } @@ -3370,17 +3382,18 @@ int remap_pfn_range_prepare(struct vm_area_desc *desc) const struct mmap_action *action = &desc->action; const unsigned long start = action->remap.start; const unsigned long end = start + action->remap.size; - const unsigned long pfn = action->remap.start_pfn; const bool is_cow = vma_desc_is_cow_mapping(desc); int err; if (!range_in_vma_desc(desc, start, end)) return -EFAULT; - err = get_remap_pgoff(is_cow, start, end, desc->start, desc->end, pfn, - &desc->pgoff); - if (err) - return err; + if (is_cow) { + err = check_cow_pfnmap_range(start, end, desc->start, + desc->end); + if (err) + return err; + } vma_desc_set_flags_mask(desc, VMA_REMAP_FLAGS); return 0; @@ -3394,12 +3407,16 @@ static int remap_pfn_range_prepare_vma(struct vm_area_struct *vma, const bool is_cow = vma_is_cow_mapping(vma); int err; - err = get_remap_pgoff(is_cow, addr, end, vma->vm_start, vma->vm_end, - pfn, &vma->vm_pgoff); - if (err) - return err; + if (is_cow) { + err = check_cow_pfnmap_range(addr, end, vma->vm_start, + vma->vm_end); + if (err) + return err; + } vma_set_flags_mask(vma, VMA_REMAP_FLAGS); + if (is_cow) + vma_set_cow_pfnmap_pfn(vma, pfn); return 0; } @@ -3436,6 +3453,8 @@ int remap_pfn_range_complete(struct vm_area_struct *vma, const unsigned long size = action->remap.size; const pgprot_t prot = action->remap.pgprot; + if (vma_is_cow_mapping(vma)) + vma_set_cow_pfnmap_pfn(vma, pfn); return do_remap_pfn_range(vma, start, pfn, size, prot); } diff --git a/mm/vma.c b/mm/vma.c index c82e9306b11ac..f99f50ca94777 100644 --- a/mm/vma.c +++ b/mm/vma.c @@ -2006,10 +2006,10 @@ struct vm_area_struct *copy_vma(struct vm_area_struct **vmap, VMG_VMA_STATE(vmg, &vmi, NULL, vma, addr, addr + len); /* - * If a vma has not yet been faulted, update its anonymous pgoff to - * match the new location to increase its chance of merging. + * If an mm-managed vma has not yet been faulted, update its anonymous + * pgoff to match the new location to increase its chance of merging. */ - if (!vma_has_anon_rmap(vma)) { + if (!vma_has_anon_rmap(vma) && vma_is_mm_managed(vma)) { anon_pgoff = addr >> PAGE_SHIFT; if (vma_is_anonymous(vma)) { diff --git a/mm/vma.h b/mm/vma.h index a74143bd22746..8e506f5d15a79 100644 --- a/mm/vma.h +++ b/mm/vma.h @@ -290,8 +290,7 @@ static inline void assert_sane_pgoff(struct vm_area_struct *vma, pgoff_t pgoff) return; /* * File-backed VMAs have arbitrary page offset (either page offset into - * file or for pfnmap the PFN of the start of the range or drivers may - * set arbitrary page offset). + * file or drivers may set arbitrary page offset). */ if (!vma_is_anonymous(vma)) return; diff --git a/tools/testing/vma/include/dup.h b/tools/testing/vma/include/dup.h index f9bafe4e20158..9e72d5f06e10e 100644 --- a/tools/testing/vma/include/dup.h +++ b/tools/testing/vma/include/dup.h @@ -1741,7 +1741,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; -- 2.56.1