From: "Darrick J. Wong" <djwong@kernel.org>
To: tytso@mit.edu
Cc: linux-ext4@vger.kernel.org
Subject: [PATCH 15/8] libext2fs: fix data corruption when writing to inline data files
Date: Thu, 17 Jul 2025 07:59:50 -0700 [thread overview]
Message-ID: <20250717145950.GJ2672022@frogsfrogsfrogs> (raw)
In-Reply-To: <175182662934.1984706.3737778061161342509.stgit@frogsfrogsfrogs>
From: Darrick J. Wong <djwong@kernel.org>
Fix various bugs in ext2fs_file_write_inline_data:
- "count = nbytes - pos" makes no sense since nbytes is already a
length value and results in short writes if pos > 0.
- Pass the correct file size to ext2fs_inline_data_set because count
will not be the file size if pos > 0.
- Simplify the decision to increase the file size.
- Don't let a huge write corrupt memory beyond file->buf.
- Zero the buffer between isize and pos if we're doing a sparse write
past EOF.
Cc: <linux-ext4@vger.kernel.org> # v1.43
Fixes: 54e880b870f7fe ("libext2fs: handle inline data in read/write function")
Signed-off-by: "Darrick J. Wong" <djwong@kernel.org>
---
lib/ext2fs/fileio.c | 27 +++++++++++++++++++--------
1 file changed, 19 insertions(+), 8 deletions(-)
diff --git a/lib/ext2fs/fileio.c b/lib/ext2fs/fileio.c
index 900002c5295682..3a36e9e7fff43b 100644
--- a/lib/ext2fs/fileio.c
+++ b/lib/ext2fs/fileio.c
@@ -349,31 +349,42 @@ ext2fs_file_write_inline_data(ext2_file_t file, const void *buf,
unsigned int nbytes, unsigned int *written)
{
ext2_filsys fs;
+ uint64_t old_isize = EXT2_I_SIZE(&file->inode);
+ uint64_t new_isize = old_isize;
errcode_t retval;
- unsigned int count = 0;
size_t size;
+ if (file->pos + nbytes > old_isize)
+ new_isize = file->pos + nbytes;
+
fs = file->fs;
retval = ext2fs_inline_data_get(fs, file->ino, &file->inode,
file->buf, &size);
if (retval)
return retval;
- if (file->pos < size) {
- count = nbytes - file->pos;
- memcpy(file->buf + file->pos, buf, count);
+ /*
+ * Only try to set new inline data if it won't go past the end of
+ * @file->buf; if there's not enough space in the ondisk inode, we'll
+ * jump out to the expand code.
+ */
+ if (new_isize < fs->blocksize) {
+ if (file->pos > old_isize)
+ memset(file->buf + old_isize, 0, file->pos - old_isize);
+
+ memcpy(file->buf + file->pos, buf, nbytes);
retval = ext2fs_inline_data_set(fs, file->ino, &file->inode,
- file->buf, count);
+ file->buf, new_isize);
if (retval == EXT2_ET_INLINE_DATA_NO_SPACE)
goto expand;
if (retval)
return retval;
- file->pos += count;
+ file->pos += nbytes;
/* Update inode size */
- if (count != 0 && EXT2_I_SIZE(&file->inode) < file->pos) {
+ if (old_isize < new_isize) {
errcode_t rc;
rc = ext2fs_file_set_size2(file, file->pos);
@@ -382,7 +393,7 @@ ext2fs_file_write_inline_data(ext2_file_t file, const void *buf,
}
if (written)
- *written = count;
+ *written = nbytes;
return 0;
}
next prev parent reply other threads:[~2025-07-17 14:59 UTC|newest]
Thread overview: 23+ messages / expand[flat|nested] mbox.gz Atom feed top
2025-07-06 18:30 [PATCHSET] fuse2fs: more bug fixes Darrick J. Wong
2025-07-06 18:31 ` [PATCH 1/8] libext2fs: fix off-by-one bug in punch_extent_blocks Darrick J. Wong
2025-07-06 18:31 ` [PATCH 2/8] libext2fs: fix arguments passed to ->block_alloc_stats_range Darrick J. Wong
2025-07-06 18:31 ` [PATCH 3/8] fuse2fs: refactor uid/gid setting Darrick J. Wong
2025-07-06 18:31 ` [PATCH 4/8] fuse2fs: fix gid inheritance on sgid parent directories Darrick J. Wong
2025-07-06 18:32 ` [PATCH 5/8] fuse2fs: don't truncate when creating a new file Darrick J. Wong
2025-07-06 18:32 ` [PATCH 6/8] fuse2fs: fix incorrect EOFS input handling in FITRIM Darrick J. Wong
2025-07-06 18:32 ` [PATCH 7/8] fuse2fs: fix incorrect unit conversion at the end of FITRIM Darrick J. Wong
2025-07-06 18:32 ` [PATCH 8/8] fuse2fs: don't try to mount after option parsing errors Darrick J. Wong
2025-07-07 16:05 ` [PATCH 9/8] fuse2fs: fix relatime comparisons Darrick J. Wong
2025-07-08 17:33 ` [PATCH 10/8] fuse2fs: fix lockfile creation, again Darrick J. Wong
2025-07-09 16:51 ` [PATCH 11/8] fuse2fs: fix race condition in op_destroy Darrick J. Wong
2025-07-09 16:52 ` [PATCH 12/8] fuse2fs: fix races in statfs Darrick J. Wong
2025-07-17 14:59 ` [PATCH 13/8] fuse2fs: fix ST_RDONLY setting Darrick J. Wong
2025-07-17 14:59 ` [PATCH 14/8] libext2fs: fix data read corruption in ext2fs_file_read_inline_data Darrick J. Wong
2025-07-17 14:59 ` Darrick J. Wong [this message]
2025-07-17 22:01 ` [PATCH 16/8] fuse2fs: fix clean_block_middle when punching byte 0 of a block Darrick J. Wong
2025-07-17 22:01 ` [PATCH 17/8] fuse2fs: fix punch-out range calculation in fuse2fs_punch_range Darrick J. Wong
2025-07-22 19:40 ` [PATCH 18/8] fuse2fs: fix logging redirection Darrick J. Wong
2025-07-25 15:56 ` [PATCH 19/8] fuse2fs: don't record every errno in the superblock as an fs failure Darrick J. Wong
2025-07-26 16:28 ` [PATCH 20/8] fuse2fs: fix punching post-EOF blocks during truncate Darrick J. Wong
2025-07-30 17:23 ` [PATCH 21/8] fuse2fs: fix block parameter truncation on 32-bit Darrick J. Wong
2025-07-31 14:47 ` [PATCHSET] fuse2fs: more bug fixes Theodore Ts'o
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=20250717145950.GJ2672022@frogsfrogsfrogs \
--to=djwong@kernel.org \
--cc=linux-ext4@vger.kernel.org \
--cc=tytso@mit.edu \
/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