nilfs2: handle corrupted checkpoint count gracefully during deletion
Ryusuke Konishi
maintainerPATCH 1/1 nilfs2: handle corrupted checkpoint count gracefully during deletion
From: Igor Putko <[email protected]>
Syzkaller reported a kernel warning in nilfs_cpfile_delete_checkpoints() due to a corrupted checkpoint count on the storage medium where le32_to_cpu(cp->cp_checkpoints_count) is less than the number of checkpoints being deleted.
Triggering a WARN_ON() for disk image corruption is suboptimal. Fix this by returning -EIO and reporting a filesystem error via nilfs_error() instead of interrupting execution with a kernel warning.
---
Hi Viacheslav,
Please apply this for the next cycle.
This fixes a kernel warning and a missed inconsistency check that could occur with file system images containing corrupted checkpoint metadata.
Thanks,
Ryusuke Konishi
fs/nilfs2/cpfile.c | 20 ++++++++++++++++---- 1 file changed, 16 insertions(+), 4 deletions(-)
fs/nilfs2/cpfile.c +16 -4
@@ -81,18 +81,26 @@ nilfs_cpfile_block_add_valid_checkpoints(const struct inode *cpfile,return count; } -static unsigned int +static int nilfs_cpfile_block_sub_valid_checkpoints (const struct inode *cpfile,struct buffer_head *bh,unsigned int n) {struct nilfs_checkpoint *cp; -unsigned int count; +unsigned int checkpoints_count; +int count; cp =kmap_local_folio (bh->b_folio,offset_in_folio (bh->b_folio, bh->b_data)); -WARN_ON (le32_to_cpu (cp->cp_checkpoints_count) < n); - count =le32_to_cpu (cp->cp_checkpoints_count) - n; + checkpoints_count =le32_to_cpu (cp->cp_checkpoints_count); +if (unlikely (checkpoints_count < n)) { +nilfs_error (cpfile->i_sb, +"deleted checkpoints count %u exceeds block count %u" , + n, checkpoints_count); +kunmap_local (cp); +return -EIO ; + } + count = checkpoints_count - n; cp->cp_checkpoints_count =cpu_to_le32 (count);kunmap_local (cp);return count;
@@ -522,6 +530,10 @@ int nilfs_cpfile_delete_checkpoints(struct inode *cpfile, count =nilfs_cpfile_block_sub_valid_checkpoints (cpfile, cp_bh, nicps);brelse (cp_bh); +if (unlikely (count <0 )) { + ret = count; +break ; + }if (count)continue ;
Signature · 1 line
2.43.0
Quoted 85 lines · Ryusuke Konishi
From: Igor Putko <[email protected]>
Syzkaller reported a kernel warning in
nilfs_cpfile_delete_checkpoints()
due to a corrupted checkpoint count on the storage medium wherele32_to_cpu(cp->cp_checkpoints_count)is less than the number of checkpoints being deleted.
Triggering aWARN_ON()for disk image corruption is suboptimal. Fix this by returning-EIOand reporting a filesystem error vianilfs_error()instead of interrupting execution with a kernel warning.Reported-by [email protected]Signed-off-by Igor Putko <[email protected]>Fixes 1f5abe7e7dbc ("nilfs2: replace BUG_ON and BUG callstriggerable from ioctl")
Cc: <[email protected]> # Warning suppression primarily; will request backport individually if neededSigned-off-by Ryusuke Konishi <[email protected]>---
Hi Viacheslav,Please apply this for the next cycle.
This fixes a kernel warning and a missed inconsistency check that could
occur with file system images containing corrupted checkpoint metadata.Thanks,
Ryusuke Konishifs/nilfs2/cpfile.c | 20 ++++++++++++++++----
1 file changed, 16 insertions(+), 4 deletions(-)fs/nilfs2/cpfile.c +0
-0@@ -81,18 +81,26 @@ nilfs_cpfile_block_add_valid_checkpoints(conststruct inode *cpfile, return count; }fs/nilfs2/cpfile.c +1
-1-static unsigned int +static intnilfs_cpfile_block_sub_valid_checkpoints(const struct inode *cpfile, struct
buffer_head*bh, unsigned int n)
{
structnilfs_checkpoint*cp;fs/nilfs2/cpfile.c +2
-1-unsigned int count; +unsigned int checkpoints_count; +int count;
cp = kmap_local_folio(bh->b_folio, offset_in_folio(bh->b_folio, bh-Quoted 1 line
b_data));fs/nilfs2/cpfile.c +4
-2-WARN_ON (le32_to_cpu (cp->cp_checkpoints_count) < n); - count =le32_to_cpu (cp->cp_checkpoints_count) - n; + checkpoints_count =le32_to_cpu (cp->cp_checkpoints_count); +if (unlikely (checkpoints_count < n)) { + nilfs_error(cpfile->i_sb, + "deleted checkpoints count %u exceedsblock count %u",
fs/nilfs2/cpfile.c +5
-0+ n, checkpoints_count); +kunmap_local (cp); +return -EIO ; + } + count = checkpoints_count - n;
cp->cp_checkpoints_count=cpu_to_le32(count);kunmap_local(cp);
return count;fs/nilfs2/cpfile.c +0
-0@@ -522,6 +530,10 @@ int nilfs_cpfile_delete_checkpoints(struct inode *cpfile,count = nilfs_cpfile_block_sub_valid_checkpoints(cpfile,
cp_bh,
nicps);
brelse(cp_bh);fs/nilfs2/cpfile.c +4
-0+if (unlikely (count <0 )) { + ret = count; +break ; + }
Quoted 3 lines
if (count)
continue;
Applied.
Thanks,
Slava.