From 3efb8e44f30fb9ad61067d84c89d7444c76b866c Mon Sep 17 00:00:00 2001 From: Colin Foster Date: Wed, 21 Jul 2021 08:56:21 -0700 Subject: [PATCH 1/3] Fail mount when the block size changes When the on-disk block size doesn't match the config block size, it is possible to get file corruption. For instance, if the num blocks was 0x200 and we re-mount with 0x100 files could be corrupt. If we re-mount with a larger number of blocks things should be safer, but could be handled with a resize option or perhaps a mount flag to ignore this parameter. --- lfs.c | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/lfs.c b/lfs.c index d976389..c0092ca 100644 --- a/lfs.c +++ b/lfs.c @@ -3761,6 +3761,12 @@ static int lfs_rawmount(lfs_t *lfs, const struct lfs_config *cfg) { lfs->attr_max = superblock.attr_max; } + + if (superblock.block_count != lfs->cfg->block_count) + { + err = LFS_ERR_CORRUPT; + goto cleanup; + } } // has gstate? From 487df12dde704af32afb3c6488a8abe2d1c405d4 Mon Sep 17 00:00:00 2001 From: Colin Foster Date: Tue, 17 Aug 2021 10:02:27 -0700 Subject: [PATCH 2/3] Fail when block_size doesn't match config With the previous commit, fail if the superblock block_size doesn't match the config block_size. --- lfs.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/lfs.c b/lfs.c index c0092ca..bd466b8 100644 --- a/lfs.c +++ b/lfs.c @@ -3762,7 +3762,8 @@ static int lfs_rawmount(lfs_t *lfs, const struct lfs_config *cfg) { lfs->attr_max = superblock.attr_max; } - if (superblock.block_count != lfs->cfg->block_count) + if ((superblock.block_count != lfs->cfg->block_count) || + (superblock.block_size != lfs->cfg->block_size)) { err = LFS_ERR_CORRUPT; goto cleanup; From cf274e6ec62a26455caae0801c410f9bb8214ef3 Mon Sep 17 00:00:00 2001 From: Colin Foster Date: Tue, 17 Aug 2021 13:49:58 -0700 Subject: [PATCH 3/3] Squash of CR changes - nit: Moving brace to end of if statement line for consistency - mount: add more debug info per CR - Fix compiler error from extra parentheses - Fix superblock typo --- lfs.c | 15 +++++++++++---- 1 file changed, 11 insertions(+), 4 deletions(-) diff --git a/lfs.c b/lfs.c index bd466b8..cbbc28b 100644 --- a/lfs.c +++ b/lfs.c @@ -3762,10 +3762,17 @@ static int lfs_rawmount(lfs_t *lfs, const struct lfs_config *cfg) { lfs->attr_max = superblock.attr_max; } - if ((superblock.block_count != lfs->cfg->block_count) || - (superblock.block_size != lfs->cfg->block_size)) - { - err = LFS_ERR_CORRUPT; + if (superblock.block_count != lfs->cfg->block_count) { + LFS_ERROR("Invalid block count (%"PRIu32" != %"PRIu32")", + superblock.block_count, lfs->cfg->block_count); + err = LFS_ERR_INVAL; + goto cleanup; + } + + if (superblock.block_size != lfs->cfg->block_size) { + LFS_ERROR("Invalid block size (%"PRIu32" != %"PRIu32")", + superblock.block_count, lfs->cfg->block_count); + err = LFS_ERR_INVAL; goto cleanup; } }