[PATCH] ext4: check folio uptodate state in ext4_page_mkwrite()

Deepanshu Kartikey posted 1 patch 1 week, 3 days ago
There is a newer version of this series
fs/ext4/inode.c | 8 ++++++++
1 file changed, 8 insertions(+)
[PATCH] ext4: check folio uptodate state in ext4_page_mkwrite()
Posted by Deepanshu Kartikey 1 week, 3 days ago
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
Re: [PATCH] ext4: check folio uptodate state in ext4_page_mkwrite()
Posted by Darrick J. Wong 1 week, 3 days ago
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
> 
>
Re: [PATCH] ext4: check folio uptodate state in ext4_page_mkwrite()
Posted by Deepanshu Kartikey 1 week, 3 days ago
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