Skip to content

Commit de87989

Browse files
deepanshu406akpm00
authored andcommitted
mm/memfd: fix information leak in hugetlb folios
When allocating hugetlb folios for memfd, three initialization steps are missing: 1. Folios are not zeroed, leading to kernel memory disclosure to userspace 2. Folios are not marked uptodate before adding to page cache 3. hugetlb_fault_mutex is not taken before hugetlb_add_to_page_cache() The memfd allocation path bypasses the normal page fault handler (hugetlb_no_page) which would handle all of these initialization steps. This is problematic especially for udmabuf use cases where folios are pinned and directly accessed by userspace via DMA. Fix by matching the initialization pattern used in hugetlb_no_page(): - Zero the folio using folio_zero_user() which is optimized for huge pages - Mark it uptodate with folio_mark_uptodate() - Take hugetlb_fault_mutex before adding to page cache to prevent races The folio_zero_user() change also fixes a potential security issue where uninitialized kernel memory could be disclosed to userspace through read() or mmap() operations on the memfd. Link: https://lkml.kernel.org/r/20251112145034.2320452-1-kartikey406@gmail.com Fixes: 89c1905 ("mm/gup: introduce memfd_pin_folios() for pinning memfd folios") Signed-off-by: Deepanshu Kartikey <kartikey406@gmail.com> Reported-by: syzbot+f64019ba229e3a5c411b@syzkaller.appspotmail.com Link: https://lore.kernel.org/all/20251112031631.2315651-1-kartikey406@gmail.com/ [v1] Closes: https://syzkaller.appspot.com/bug?extid=f64019ba229e3a5c411b Suggested-by: Oscar Salvador <osalvador@suse.de> Suggested-by: David Hildenbrand <david@redhat.com> Tested-by: syzbot+f64019ba229e3a5c411b@syzkaller.appspotmail.com Acked-by: Oscar Salvador <osalvador@suse.de> Acked-by: David Hildenbrand (Red Hat) <david@kernel.org> Acked-by: Hugh Dickins <hughd@google.com> Cc: Vivek Kasireddy <vivek.kasireddy@intel.com> Cc: Jason Gunthorpe <jgg@nvidia.com> Cc: Jason Gunthorpe <jgg@nvidia.com> (v2) Cc: Christoph Hellwig <hch@lst.de> (v6) Cc: Dave Airlie <airlied@redhat.com> Cc: Gerd Hoffmann <kraxel@redhat.com> Cc: <stable@vger.kernel.org> Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
1 parent f5e31a1 commit de87989

1 file changed

Lines changed: 27 additions & 0 deletions

File tree

mm/memfd.c

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,9 +96,36 @@ struct folio *memfd_alloc_folio(struct file *memfd, pgoff_t idx)
9696
NULL,
9797
gfp_mask);
9898
if (folio) {
99+
u32 hash;
100+
101+
/*
102+
* Zero the folio to prevent information leaks to userspace.
103+
* Use folio_zero_user() which is optimized for huge/gigantic
104+
* pages. Pass 0 as addr_hint since this is not a faulting path
105+
* and we don't have a user virtual address yet.
106+
*/
107+
folio_zero_user(folio, 0);
108+
109+
/*
110+
* Mark the folio uptodate before adding to page cache,
111+
* as required by filemap.c and other hugetlb paths.
112+
*/
113+
__folio_mark_uptodate(folio);
114+
115+
/*
116+
* Serialize hugepage allocation and instantiation to prevent
117+
* races with concurrent allocations, as required by all other
118+
* callers of hugetlb_add_to_page_cache().
119+
*/
120+
hash = hugetlb_fault_mutex_hash(memfd->f_mapping, idx);
121+
mutex_lock(&hugetlb_fault_mutex_table[hash]);
122+
99123
err = hugetlb_add_to_page_cache(folio,
100124
memfd->f_mapping,
101125
idx);
126+
127+
mutex_unlock(&hugetlb_fault_mutex_table[hash]);
128+
102129
if (err) {
103130
folio_put(folio);
104131
goto err_unresv;

0 commit comments

Comments
 (0)