* [PATCH] ext4: check folio uptodate state in ext4_page_mkwrite()
@ 2025-11-21 13:13 Deepanshu Kartikey
2025-11-21 16:27 ` Darrick J. Wong
0 siblings, 1 reply; 3+ messages in thread
From: Deepanshu Kartikey @ 2025-11-21 13:13 UTC (permalink / raw)
To: tytso, adilger.kernel
Cc: linux-ext4, linux-kernel, Deepanshu Kartikey,
syzbot+b0a0670332b6b3230a0a
When a write fault occurs on a memory-mapped ext4 file, ext4_page_mkwrite()
is called to prepare the folio for writing. However, if the folio could
not be read successfully due to filesystem corruption or I/O errors, it
will not be marked uptodate.
Attempting to write to a non-uptodate folio is problematic because:
1. We don't have valid data from the backing store to preserve
2. A subsequent writeback could write uninitialized data to disk
3. It triggers a warning in __folio_mark_dirty():
WARN_ON_ONCE(warn && !folio_test_uptodate(folio))
This issue can be reproduced by:
1. Creating a corrupted ext4 filesystem with invalid extent entries
2. Memory-mapping a file on that filesystem
3. Attempting to write to the mapped region
The sequence of events is:
- User accesses mmap region -> page fault
- ext4_filemap_fault() -> ext4_map_blocks() detects corruption
- Returns error, folio allocated but NOT marked uptodate
- User writes to same region -> ext4_page_mkwrite() called
- Without check: folio marked dirty -> WARNING
- With check: return VM_FAULT_SIGBUS immediately
Fix this by checking folio_test_uptodate() early in ext4_page_mkwrite(),
before any code paths (delalloc, journal data, or normal). This ensures
all paths are protected. If the folio is not uptodate, unlock it and
return VM_FAULT_SIGBUS to signal the error to userspace.
Reported-by: syzbot+b0a0670332b6b3230a0a@syzkaller.appspotmail.com
Closes: https://syzkaller.appspot.com/bug?extid=b0a0670332b6b3230a0a
Signed-off-by: Deepanshu Kartikey <kartikey406@gmail.com>
---
fs/ext4/inode.c | 8 ++++++++
1 file changed, 8 insertions(+)
diff --git a/fs/ext4/inode.c b/fs/ext4/inode.c
index e99306a8f47c..18a029362c1f 100644
--- a/fs/ext4/inode.c
+++ b/fs/ext4/inode.c
@@ -6688,6 +6688,14 @@ vm_fault_t ext4_page_mkwrite(struct vm_fault *vmf)
if (err)
goto out_ret;
+ folio_lock(folio);
+ if (!folio_test_uptodate(folio)) {
+ folio_unlock(folio);
+ ret = VM_FAULT_SIGBUS;
+ goto out;
+ }
+ folio_unlock(folio);
+
/*
* On data journalling we skip straight to the transaction handle:
* there's no delalloc; page truncated will be checked later; the
--
2.43.0
^ permalink raw reply related [flat|nested] 3+ messages in thread
* Re: [PATCH] ext4: check folio uptodate state in ext4_page_mkwrite()
2025-11-21 13:13 [PATCH] ext4: check folio uptodate state in ext4_page_mkwrite() Deepanshu Kartikey
@ 2025-11-21 16:27 ` Darrick J. Wong
2025-11-21 16:49 ` Deepanshu Kartikey
0 siblings, 1 reply; 3+ messages in thread
From: Darrick J. Wong @ 2025-11-21 16:27 UTC (permalink / raw)
To: Deepanshu Kartikey
Cc: tytso, adilger.kernel, linux-ext4, linux-kernel,
syzbot+b0a0670332b6b3230a0a
On Fri, Nov 21, 2025 at 06:43:05PM +0530, Deepanshu Kartikey wrote:
> When a write fault occurs on a memory-mapped ext4 file, ext4_page_mkwrite()
> is called to prepare the folio for writing. However, if the folio could
> not be read successfully due to filesystem corruption or I/O errors, it
> will not be marked uptodate.
>
> Attempting to write to a non-uptodate folio is problematic because:
> 1. We don't have valid data from the backing store to preserve
> 2. A subsequent writeback could write uninitialized data to disk
> 3. It triggers a warning in __folio_mark_dirty():
> WARN_ON_ONCE(warn && !folio_test_uptodate(folio))
> This issue can be reproduced by:
> 1. Creating a corrupted ext4 filesystem with invalid extent entries
> 2. Memory-mapping a file on that filesystem
> 3. Attempting to write to the mapped region
>
> The sequence of events is:
> - User accesses mmap region -> page fault
> - ext4_filemap_fault() -> ext4_map_blocks() detects corruption
^^^^^^^^^^^^^^^^^^ what function is this?
$ grep ext4_filemap_fault fs/ext4/
$
> - Returns error, folio allocated but NOT marked uptodate
> - User writes to same region -> ext4_page_mkwrite() called
> - Without check: folio marked dirty -> WARNING
> - With check: return VM_FAULT_SIGBUS immediately
Doesn't filemap_fault bring the contents into the folio and return
VM_FAULT_SIGBUS if that fails?
--D
>
> Fix this by checking folio_test_uptodate() early in ext4_page_mkwrite(),
> before any code paths (delalloc, journal data, or normal). This ensures
> all paths are protected. If the folio is not uptodate, unlock it and
> return VM_FAULT_SIGBUS to signal the error to userspace.
>
> Reported-by: syzbot+b0a0670332b6b3230a0a@syzkaller.appspotmail.com
> Closes: https://syzkaller.appspot.com/bug?extid=b0a0670332b6b3230a0a
> Signed-off-by: Deepanshu Kartikey <kartikey406@gmail.com>
> ---
> fs/ext4/inode.c | 8 ++++++++
> 1 file changed, 8 insertions(+)
>
> diff --git a/fs/ext4/inode.c b/fs/ext4/inode.c
> index e99306a8f47c..18a029362c1f 100644
> --- a/fs/ext4/inode.c
> +++ b/fs/ext4/inode.c
> @@ -6688,6 +6688,14 @@ vm_fault_t ext4_page_mkwrite(struct vm_fault *vmf)
> if (err)
> goto out_ret;
>
> + folio_lock(folio);
> + if (!folio_test_uptodate(folio)) {
> + folio_unlock(folio);
> + ret = VM_FAULT_SIGBUS;
> + goto out;
> + }
> + folio_unlock(folio);
> +
> /*
> * On data journalling we skip straight to the transaction handle:
> * there's no delalloc; page truncated will be checked later; the
> --
> 2.43.0
>
>
^ permalink raw reply [flat|nested] 3+ messages in thread
* Re: [PATCH] ext4: check folio uptodate state in ext4_page_mkwrite()
2025-11-21 16:27 ` Darrick J. Wong
@ 2025-11-21 16:49 ` Deepanshu Kartikey
0 siblings, 0 replies; 3+ messages in thread
From: Deepanshu Kartikey @ 2025-11-21 16:49 UTC (permalink / raw)
To: Darrick J. Wong
Cc: tytso, adilger.kernel, linux-ext4, linux-kernel,
syzbot+b0a0670332b6b3230a0a
On Fri, Nov 21, 2025 at 9:57 PM Darrick J. Wong <djwong@kernel.org> wrote:
>
> On Fri, Nov 21, 2025 at 06:43:05PM +0530, Deepanshu Kartikey wrote:
> > When a write fault occurs on a memory-mapped ext4 file, ext4_page_mkwrite()
> > is called to prepare the folio for writing. However, if the folio could
> > not be read successfully due to filesystem corruption or I/O errors, it
> > will not be marked uptodate.
> >
> > Attempting to write to a non-uptodate folio is problematic because:
> > 1. We don't have valid data from the backing store to preserve
> > 2. A subsequent writeback could write uninitialized data to disk
> > 3. It triggers a warning in __folio_mark_dirty():
> > WARN_ON_ONCE(warn && !folio_test_uptodate(folio))
> > This issue can be reproduced by:
> > 1. Creating a corrupted ext4 filesystem with invalid extent entries
> > 2. Memory-mapping a file on that filesystem
> > 3. Attempting to write to the mapped region
> >
> > The sequence of events is:
> > - User accesses mmap region -> page fault
> > - ext4_filemap_fault() -> ext4_map_blocks() detects corruption
>
> ^^^^^^^^^^^^^^^^^^ what function is this?
>
> $ grep ext4_filemap_fault fs/ext4/
> $
>
> > - Returns error, folio allocated but NOT marked uptodate
> > - User writes to same region -> ext4_page_mkwrite() called
> > - Without check: folio marked dirty -> WARNING
> > - With check: return VM_FAULT_SIGBUS immediately
>
> Doesn't filemap_fault bring the contents into the folio and return
> VM_FAULT_SIGBUS if that fails?
>
Thank you for the review and for catching these issues!
You're absolutely right - ext4_filemap_fault() doesn't exist, and I
apologize for the confusion in my commit message. I need to do a more
thorough investigation of the actual code paths and understand precisely
when and how a non-uptodate folio can reach ext4_page_mkwrite().
I'll trace through the code more carefully, verify the exact reproduction
scenario with the syzbot reproducer, and ensure my analysis is accurate
before sending a v2. I want to make sure I fully understand:
1. The actual function call path (filemap_fault() and its error handling)
2. Under what conditions a non-uptodate folio can exist at mkwrite time
3. Whether filemap_fault() already handles all error cases appropriately
I'll get back to you once I have a clearer understanding of the issue.
Thank you again for your patience and guidance!
Best regards,
Deepanshu
^ permalink raw reply [flat|nested] 3+ messages in thread
end of thread, other threads:[~2025-11-21 16:49 UTC | newest]
Thread overview: 3+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2025-11-21 13:13 [PATCH] ext4: check folio uptodate state in ext4_page_mkwrite() Deepanshu Kartikey
2025-11-21 16:27 ` Darrick J. Wong
2025-11-21 16:49 ` Deepanshu Kartikey
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox