Skip to contentSkip to conversation

ext4: fix two journal credit shortfalls behind one syzbot report

PATCH In review William Chuang 3 participants 7 messages Nov 17 – Nov 18 ext4 file system

lore
Share & tools
lore
#0 lore raw

cover ext4: fix two journal credit shortfalls behind one syzbot report

syzbot report e8180f5590685cbc9037 ("WARNING in jbd2_journal_dirty_metadata (4)") collects crashes from two separate paths. Both hit the same WARN:
a handle dirties a new metadata block after its credits are used up.

1. EXT4_IOC_SWAP_BOOT: swap_inode_boot_loader() reserves 2 credits, then updates quota under the same handle (crashes of 15 and 16 Nov).

2. Inline data buffered write with a short user copy:
ext4_generic_write_inline_data() reserves 1 credit, and ext4_write_inline_data_end() then calls ext4_orphan_add() (crashes of 12-14 Nov, x86_64 and arm64, including data=journal).

Both were reproduced on UML at 666539eec ("Merge tag 'assoc-array-fixes-v2'") with small standalone reproducers, before and after each fix; details are in each changelog. One patch per path, so each can be reviewed alone. Both patches carry the same Reported-by tag, and the report closes only when both are applied.

William Chuang (2):
ext4: reserve quota credits in swap_inode_boot_loader()
ext4: reserve an orphan credit for inline data writes

 fs/ext4/inline.c | 7 ++++++-
 fs/ext4/ioctl.c  | 7 ++++++-
 2 files changed, 12 insertions(+), 2 deletions(-)
Signature · 1 line
2.43.0
#1 lore raw

PATCH 1/2 ext4: reserve quota credits in swap_inode_boot_loader()

swap_inode_boot_loader() starts its handle with 2 credits, enough for the two inode table blocks it dirties. Since commit aa507b5faf38 ("ext4: update quota information while swapping boot loader inode") it also calls dquot_free_space() or dquot_alloc_space() under the same handle. On a filesystem with quota enabled, that marks the dquots dirty and ext4_write_dquot() writes them to the quota file. The nested ext4_journal_start() in ext4_write_dquot() reuses the running handle and adds no credits, so the quota block write can find the handle with no credits left:

  WARNING: fs/jbd2/transaction.c:1598 at jbd2_journal_dirty_metadata
  Call Trace:
   __ext4_handle_dirty_metadata
   ext4_quota_write
   qtree_write_dquot
   v2_write_dquot
   dquot_commit
   ext4_write_dquot
   mark_all_dquot_dirty
   __dquot_free_space
   swap_inode_boot_loader
   ext4_ioctl

jbd2 returns -ENOSPC and the journal is aborted. The ioctl still returns 0, and the filesystem is left with the wrong i_blocks on the swapped inode and inconsistent quota usage, which e2fsck reports.

Reserve one quota block per quota type, as ext4_ext_migrate() does.

Tested on UML with a filesystem made by "mke2fs -O quota": write and sync a file, then issue EXT4_IOC_SWAP_BOOT on it. Without this patch the WARN fires ("credits 3/0") and e2fsck -fn reports errors; with it there is no warning and e2fsck -fn is clean.

Fixes aa507b5faf38 ("ext4: update quota information while swapping boot loader inode")
Reported-by [email protected]
---
 fs/ext4/ioctl.c | 7 ++++++-
 1 file changed, 6 insertions(+), 1 deletion(-)
fs/ext4/ioctl.c +6 -1
index e8e579498..ae03eb834 100644
@@ -425,7 +425,12 @@ static long swap_inode_boot_loader(struct super_block *sb,
 	truncate_inode_pages(&inode->i_data, 0);
 	truncate_inode_pages(&inode_bl->i_data, 0);
 
-	handle = ext4_journal_start(inode_bl, EXT4_HT_MOVE_EXTENTS, 2);
+	/*
+	 * Two inode table blocks, plus one quota block per quota type for
+	 * the dquot_free_space()/dquot_alloc_space() update below.
+	 */
+	handle = ext4_journal_start(inode_bl, EXT4_HT_MOVE_EXTENTS,
+				    2 + EXT4_MAXQUOTAS_TRANS_BLOCKS(sb));
 	if (IS_ERR(handle)) {
 		err = -EINVAL;
 		goto err_out;
Signature · 1 line
2.43.0
#2 lore raw

Re: [PATCH 1/2] ext4: reserve quota credits in swap_inode_boot_loader()

Thank you for your contribution! Sashiko AI review found no new issues in this patch, but noticed 1 related pre-existing issue it does not introduce: - [Critical] ext4: missing inode_bl type check…

Thank you for your contribution! Sashiko AI review found no new issues in this patch, but noticed 1 related pre-existing issue it does not introduce:
- [Critical] ext4: missing inode_bl type check in swap_inode_boot_loader() [1]
--

commit bcadebc925a37de0440a70c290c23f8fd547aadb
Author: William Chuang <[email protected]>

ext4: reserve quota credits in swap_inode_boot_loader()

This commit reserves quota transaction credits in swap_inode_boot_loader() to prevent running out of journal credits when updating quota information during EXT4_IOC_SWAP_BOOT.

Quoted 8 lines
fs/ext4/ioctl.c +0 -0
index e8e579498c990..ae03eb834c7ac 100644
@@ -425,7 +425,12 @@ static long swap_inode_boot_loader(struct super_block *sb,
 	truncate_inode_pages(&inode->i_data, 0);
 	truncate_inode_pages(&inode_bl->i_data, 0);
 

fs/ext4/ioctl.c static long swap_inode_boot_loader(struct super_block *sb, · line 436

Quoted 10 lines
fs/ext4/ioctl.c +6 -1
-	handle = ext4_journal_start(inode_bl, EXT4_HT_MOVE_EXTENTS, 2);
+	/*
+	 * Two inode table blocks, plus one quota block per quota type for
+	 * the dquot_free_space()/dquot_alloc_space() update below.
+	 */
+	handle = ext4_journal_start(inode_bl, EXT4_HT_MOVE_EXTENTS,
+				    2 + EXT4_MAXQUOTAS_TRANS_BLOCKS(sb));
 	if (IS_ERR(handle)) {
 		err = -EINVAL;
 		goto err_out;

[Severity: Critical]
This problem wasn't introduced by this patch, but can a crafted filesystem where EXT4_BOOT_LOADER_INO is a directory trigger a warning and corrupt VFS state in swap_inode_boot_loader() [1]?

fs/ext4/ioctl.c:swap_inode_boot_loader() {
	...
	inode_bl = ext4_iget(sb, EXT4_BOOT_LOADER_INO,
			EXT4_IGET_SPECIAL | EXT4_IGET_BAD);
	if (IS_ERR(inode_bl))
		return PTR_ERR(inode_bl);
	ei_bl = EXT4_I(inode_bl);

	lock_two_nondirectories(inode, inode_bl);
	...
	if (is_bad_inode(inode_bl) || !S_ISREG(inode_bl->i_mode)) {
		...
		inode_bl->i_mode = S_IFREG;
	}
	...
}

inode_bl is passed to lock_two_nondirectories() before its mode is checked, which triggers WARN_ON_ONCE(S_ISDIR(inode2->i_mode)). Then the !S_ISREG() branch overwrites inode_bl->i_mode with S_IFREG instead of rejecting it, changing the type of a cached directory inode in place.

[1] https://sashiko.dev/#/bug/linux-8cfc8e22-8259-4c89-8a8b-64c16e4481a5

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1
reviewer Reviewed-by on v1 1/2
#3 lore raw

Re: [PATCH 1/2] ext4: reserve quota credits in swap_inode_boot_loader()

In reply to William Chuang
Quoted 34 lines · William Chuang

swap_inode_boot_loader() starts its handle with 2 credits, enough for the two inode table blocks it dirties. Since commit aa507b5faf38 ("ext4: update quota information while swapping boot loader inode") it also calls dquot_free_space() or dquot_alloc_space() under the same handle. On a filesystem with quota enabled, that marks the dquots dirty and ext4_write_dquot() writes them to the quota file. The nested ext4_journal_start() in ext4_write_dquot() reuses the running handle and adds no credits, so the quota block write can find the handle with no credits left:

  WARNING: fs/jbd2/transaction.c:1598 at jbd2_journal_dirty_metadata
  Call Trace:
   __ext4_handle_dirty_metadata
   ext4_quota_write
   qtree_write_dquot
   v2_write_dquot
   dquot_commit
   ext4_write_dquot
   mark_all_dquot_dirty
   __dquot_free_space
   swap_inode_boot_loader
   ext4_ioctl

jbd2 returns -ENOSPC and the journal is aborted. The ioctl still returns 0, and the filesystem is left with the wrong i_blocks on the swapped inode and inconsistent quota usage, which e2fsck reports.

Reserve one quota block per quota type, as ext4_ext_migrate() does.

Tested on UML with a filesystem made by "mke2fs -O quota": write and sync a file, then issue EXT4_IOC_SWAP_BOOT on it. Without this patch the WARN fires ("credits 3/0") and e2fsck -fn reports errors; with it there is no warning and e2fsck -fn is clean.

Quoted 4 lines
Fixes aa507b5faf38 ("ext4: update quota information while swapping boot loader inode")
Reported-by [email protected]

Looks good. Feel free to add:

Honza

Quoted 26 lines
---
 fs/ext4/ioctl.c | 7 ++++++-
 1 file changed, 6 insertions(+), 1 deletion(-)
fs/ext4/ioctl.c +6 -1
index e8e579498..ae03eb834 100644
@@ -425,7 +425,12 @@ static long swap_inode_boot_loader(struct super_block *sb,
 	truncate_inode_pages(&inode->i_data, 0);
 	truncate_inode_pages(&inode_bl->i_data, 0);
 
-	handle = ext4_journal_start(inode_bl, EXT4_HT_MOVE_EXTENTS, 2);
+	/*
+	 * Two inode table blocks, plus one quota block per quota type for
+	 * the dquot_free_space()/dquot_alloc_space() update below.
+	 */
+	handle = ext4_journal_start(inode_bl, EXT4_HT_MOVE_EXTENTS,
+				    2 + EXT4_MAXQUOTAS_TRANS_BLOCKS(sb));
 	if (IS_ERR(handle)) {
 		err = -EINVAL;
 		goto err_out;
Signature · 1 line
2.43.0
Signature · 2 lines
Jan Kara <[email protected]>
SUSE Labs, CR
#4 lore raw

PATCH 2/2 ext4: reserve an orphan credit for inline data writes

In reply to William Chuang

ext4_generic_write_inline_data() starts its handle with one credit. ext4_prepare_inline_data() uses it when it creates or grows the inline data xattr and dirties the inode table block.

If the copy from user space then comes up short, write_end calls ext4_write_inline_data_end(), which sees pos + len > i_size and calls ext4_orphan_add() under the same handle. That dirties a second block, the orphan file block or the superblock, and the handle has no credit left for it:

  WARNING: fs/jbd2/transaction.c:1598 at jbd2_journal_dirty_metadata
  Call Trace:
   __ext4_handle_dirty_metadata
   ext4_orphan_file_add
   ext4_orphan_add
   ext4_write_inline_data_end
   generic_perform_write
   ext4_buffered_write_iter
   ext4_file_write_iter

jbd2 returns -ENOSPC, the journal is aborted and write() fails with ENOSPC. An unprivileged user who can write to a file on such a filesystem can trigger this. The inode table block must not already be dirty in the running transaction, which is the case with lazytime, or when the write is the first change to that block since the last commit. The block write path already reserves an extra credit for this case in ext4_write_begin().

Start the inline handle with two credits.

Tested on UML with filesystems made by "mke2fs -O inline_data", with and without orphan_file, mounted with lazytime: a write to a new file whose user buffer runs into an unmapped page after 10 of 21 bytes. Without this patch the WARN fires ("credits 2/0") and write() fails with ENOSPC, also when run as uid 65534; with it there is no warning, write() returns 10, and e2fsck -fn is clean.

Fixes f19d5870cbf7 ("ext4: add normal write support for inline data")
Reported-by [email protected]
---
 fs/ext4/inline.c | 7 ++++++-
 1 file changed, 6 insertions(+), 1 deletion(-)
fs/ext4/inline.c +6 -1
index ceee69a66..a1ae23888 100644
@@ -709,7 +709,12 @@ int ext4_generic_write_inline_data(struct address_space *mapping,
 		return ret;
 
 retry_journal:
-	handle = ext4_journal_start(inode, EXT4_HT_INODE, 1);
+	/*
+	 * One credit for the inode table block, plus one for the orphan
+	 * file block or superblock that ext4_write_inline_data_end() may
+	 * dirty via ext4_orphan_add() after a short copy.
+	 */
+	handle = ext4_journal_start(inode, EXT4_HT_INODE, 2);
 	if (IS_ERR(handle)) {
 		ret = PTR_ERR(handle);
 		goto out_release_bh;
Signature · 1 line
2.43.0
#5 lore raw

Re: [PATCH 2/2] ext4: reserve an orphan credit for inline data writes

Sashiko has reviewed this patch and found no issues.
Quoted 15 lines

ext4_generic_write_inline_data() starts its handle with one credit. ext4_prepare_inline_data() uses it when it creates or grows the inline data xattr and dirties the inode table block.

If the copy from user space then comes up short, write_end calls ext4_write_inline_data_end(), which sees pos + len > i_size and calls ext4_orphan_add() under the same handle. That dirties a second block, the orphan file block or the superblock, and the handle has no credit left for it:

  WARNING: fs/jbd2/transaction.c:1598 at jbd2_journal_dirty_metadata
  Call Trace:
   __ext4_handle_dirty_metadata
   ext4_orphan_file_add
   ext4_orphan_add
Quoted 6 lines

[ ... ]

Fixes f19d5870cbf7 ("ext4: add normal write support for inline data")
Reported-by [email protected]

Sashiko has reviewed this patch and found no issues.

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=2
reviewer Reviewed-by on v1 2/2
#6 lore raw

Re: [PATCH 2/2] ext4: reserve an orphan credit for inline data writes

In reply to William Chuang
Quoted 37 lines · William Chuang

ext4_generic_write_inline_data() starts its handle with one credit. ext4_prepare_inline_data() uses it when it creates or grows the inline data xattr and dirties the inode table block.

If the copy from user space then comes up short, write_end calls ext4_write_inline_data_end(), which sees pos + len > i_size and calls ext4_orphan_add() under the same handle. That dirties a second block, the orphan file block or the superblock, and the handle has no credit left for it:

  WARNING: fs/jbd2/transaction.c:1598 at jbd2_journal_dirty_metadata
  Call Trace:
   __ext4_handle_dirty_metadata
   ext4_orphan_file_add
   ext4_orphan_add
   ext4_write_inline_data_end
   generic_perform_write
   ext4_buffered_write_iter
   ext4_file_write_iter

jbd2 returns -ENOSPC, the journal is aborted and write() fails with ENOSPC. An unprivileged user who can write to a file on such a filesystem can trigger this. The inode table block must not already be dirty in the running transaction, which is the case with lazytime, or when the write is the first change to that block since the last commit. The block write path already reserves an extra credit for this case in ext4_write_begin().

Start the inline handle with two credits.

Tested on UML with filesystems made by "mke2fs -O inline_data", with and without orphan_file, mounted with lazytime: a write to a new file whose user buffer runs into an unmapped page after 10 of 21 bytes. Without this patch the WARN fires ("credits 2/0") and write() fails with ENOSPC, also when run as uid 65534; with it there is no warning, write() returns 10, and e2fsck -fn is clean.

Quoted 4 lines
Fixes f19d5870cbf7 ("ext4: add normal write support for inline data")
Reported-by [email protected]

Looks good. Feel free to add:

Honza

Quoted 26 lines
---
 fs/ext4/inline.c | 7 ++++++-
 1 file changed, 6 insertions(+), 1 deletion(-)
fs/ext4/inline.c +6 -1
index ceee69a66..a1ae23888 100644
@@ -709,7 +709,12 @@ int ext4_generic_write_inline_data(struct address_space *mapping,
 		return ret;
 
 retry_journal:
-	handle = ext4_journal_start(inode, EXT4_HT_INODE, 1);
+	/*
+	 * One credit for the inode table block, plus one for the orphan
+	 * file block or superblock that ext4_write_inline_data_end() may
+	 * dirty via ext4_orphan_add() after a short copy.
+	 */
+	handle = ext4_journal_start(inode, EXT4_HT_INODE, 2);
 	if (IS_ERR(handle)) {
 		ret = PTR_ERR(handle);
 		goto out_release_bh;
Signature · 1 line
2.43.0
Signature · 2 lines
Jan Kara <[email protected]>
SUSE Labs, CR