Skip to contentSkip to conversation

udf: Inline format expansion fixes

PATCH In review Jan Kara 3 participants 7 messages Jan 18, 2022 – Jan 20, 2022

Subsystems (3)
lore
Share & tools
lore

Jan Kara

author reviewer
#0 lore raw

cover udf: Inline format expansion fixes

Hello,

these two patches fix problems identified by a syzkaller in expansion of inodes with inline data. I plan to queue them in my tree.

Honza

author reviewer
#1 lore raw

PATCH 1/2 udf: Fix NULL ptr deref when converting from inline format

udf_expand_file_adinicb() calls directly ->writepage to write data expanded into a page. This however misses to setup inode for writeback properly and so we can crash on inode->i_wb dereference when submitting page for IO like:

  BUG: kernel NULL pointer dereference, address: 0000000000000158
  #PF: supervisor read access in kernel mode
...
  <TASK>
  __folio_start_writeback+0x2ac/0x350
  __block_write_full_page+0x37d/0x490
  udf_expand_file_adinicb+0x255/0x400 [udf]
  udf_file_write_iter+0xbe/0x1b0 [udf]
  new_sync_write+0x125/0x1c0
  vfs_write+0x28e/0x400

Fix the problem by marking the page dirty and going through the standard writeback path to write the page. Strictly speaking we would not even have to write the page but we want to catch e.g. ENOSPC errors early.

Fixes 52ebea749aae ("writeback: make backing_dev_info host cgroup-specific bdi_writebacks")
---
 fs/udf/inode.c | 8 +++-----
 1 file changed, 3 insertions(+), 5 deletions(-)
fs/udf/inode.c +3 -5
index 1d6b7a50736b..d6aa506b6b58 100644
@@ -258,10 +258,6 @@ int udf_expand_file_adinicb(struct inode *inode)
 	char *kaddr;
 	struct udf_inode_info *iinfo = UDF_I(inode);
 	int err;
-	struct writeback_control udf_wbc = {
-		.sync_mode = WB_SYNC_NONE,
-		.nr_to_write = 1,
-	};
 
 	WARN_ON_ONCE(!inode_is_locked(inode));
 	if (!iinfo->i_lenAlloc) {
@@ -305,8 +301,10 @@ int udf_expand_file_adinicb(struct inode *inode)
 		iinfo->i_alloc_type = ICBTAG_FLAG_AD_LONG;
 	/* from now on we have normal address_space methods */
 	inode->i_data.a_ops = &udf_aops;
+	set_page_dirty(page);
+	unlock_page(page);
 	up_write(&iinfo->i_data_sem);
-	err = inode->i_data.a_ops->writepage(page, &udf_wbc);
+	err = filemap_fdatawrite(inode->i_mapping);
 	if (err) {
 		/* Restore everything back so that we don't lose data... */
 		lock_page(page);
Signature · 1 line
2.31.1
author reviewer
#3 lore raw

PATCH 2/2 udf: Restore i_lenAlloc when inode expansion fails

In reply to Jan Kara

When we fail to expand inode from inline format to a normal format, we restore inode to contain the original inline formatting but we forgot to set i_lenAlloc back. The mismatch between i_lenAlloc and i_size was then causing further problems such as warnings and lost data down the line.

Fixes 7e49b6f2480c ("udf: Convert UDF to new truncate calling sequence")
---
 fs/udf/inode.c | 1 +
 1 file changed, 1 insertion(+)
fs/udf/inode.c +1 -0
index d6aa506b6b58..ea8f6cd01f50 100644
@@ -315,6 +315,7 @@ int udf_expand_file_adinicb(struct inode *inode)
 		unlock_page(page);
 		iinfo->i_alloc_type = ICBTAG_FLAG_AD_IN_ICB;
 		inode->i_data.a_ops = &udf_adinicb_aops;
+		iinfo->i_lenAlloc = inode->i_size;
 		up_write(&iinfo->i_data_sem);
 	}
 	put_page(page);
Signature · 1 line
2.31.1
#6 lore raw
In reply to Jan Kara

Thanks for the update.

Regards,
butt3rflyh4ck.

Quoted 7 lines · Jan Kara

Hello,

these two patches fix problems identified by a syzkaller in expansion of inodes with inline data. I plan to queue them in my tree.

Honza

Signature · 1 line
Active Defense Lab of Venustech
Reviewed-by on v1 2/2
#4 lore raw

Re: [PATCH 2/2] udf: Restore i_lenAlloc when inode expansion fails

In reply to Jan Kara
Quoted 4 lines · Jan Kara

When we fail to expand inode from inline format to a normal format, we restore inode to contain the original inline formatting but we forgot to set i_lenAlloc back. The mismatch between i_lenAlloc and i_size was then causing further problems such as warnings and lost data down the line.

Looks good,

Btw, how did the reported even hit that failure in a way where the file system continues working? If we fail to write back data we'd probably better stop modifying anything and bail out..

author reviewer
#5 lore raw

Re: [PATCH 2/2] udf: Restore i_lenAlloc when inode expansion fails

Quoted 6 lines · Christoph Hellwig
Quoted 4 lines · Jan Kara

When we fail to expand inode from inline format to a normal format, we restore inode to contain the original inline formatting but we forgot to set i_lenAlloc back. The mismatch between i_lenAlloc and i_size was then causing further problems such as warnings and lost data down the line.

Quoted 7 lines

Looks good,

Btw, how did the reported even hit that failure in a way where the file system continues working? If we fail to write back data we'd probably better stop modifying anything and bail out..

We can fail the expansion from inline to out-of-line format e.g. when the filesystem is full (ENOSPC). So we have to handle that case gracefully and the filesystem should be fully operational after this.

Thanks for review!

Honza

Signature · 2 lines
Jan Kara <[email protected]>
SUSE Labs, CR