2022-06-07 07:47:33

by Theodore Ts'o

[permalink] [raw]
Subject: [PATCH 7/7] libext2fs: check for invalid blocks in ext2fs_punch_blocks()

If the extent tree has out-of-range physical block numbers, don't try
to release them.

Also add a similar check in ext2fs_block_alloc_stats2() to avoid a
NULL pointer dereference.

Reported-by: Nils Bars <[email protected]>
Reported-by: Moritz Schlögel <[email protected]>
Reported-by: Nico Schiller <[email protected]>
Signed-off-by: Theodore Ts'o <[email protected]>
---
lib/ext2fs/alloc_stats.c | 3 ++-
lib/ext2fs/punch.c | 4 ++++
2 files changed, 6 insertions(+), 1 deletion(-)

diff --git a/lib/ext2fs/alloc_stats.c b/lib/ext2fs/alloc_stats.c
index 3949f618..6f98bcc7 100644
--- a/lib/ext2fs/alloc_stats.c
+++ b/lib/ext2fs/alloc_stats.c
@@ -62,7 +62,8 @@ void ext2fs_block_alloc_stats2(ext2_filsys fs, blk64_t blk, int inuse)
{
int group = ext2fs_group_of_blk2(fs, blk);

- if (blk >= ext2fs_blocks_count(fs->super)) {
+ if (blk < fs->super->s_first_data_block ||
+ blk >= ext2fs_blocks_count(fs->super)) {
#ifndef OMIT_COM_ERR
com_err("ext2fs_block_alloc_stats", 0,
"Illegal block number: %lu", (unsigned long) blk);
diff --git a/lib/ext2fs/punch.c b/lib/ext2fs/punch.c
index effa1e2d..e2543e1e 100644
--- a/lib/ext2fs/punch.c
+++ b/lib/ext2fs/punch.c
@@ -200,6 +200,10 @@ static errcode_t punch_extent_blocks(ext2_filsys fs, ext2_ino_t ino,
__u32 cluster_freed;
errcode_t retval = 0;

+ if (free_start < fs->super->s_first_data_block ||
+ (free_start + free_count) >= ext2fs_blocks_count(fs->super))
+ return EXT2_ET_BAD_BLOCK_NUM;
+
/* No bigalloc? Just free each block. */
if (EXT2FS_CLUSTER_RATIO(fs) == 1) {
*freed += free_count;
--
2.31.0


2022-06-07 15:54:02

by Lukas Czerner

[permalink] [raw]
Subject: Re: [PATCH 7/7] libext2fs: check for invalid blocks in ext2fs_punch_blocks()

Looks good.

Reviewed-by: Lukas Czerner <[email protected]>


Thanks!
-Lukas

On Tue, Jun 07, 2022 at 12:24:44AM -0400, Theodore Ts'o wrote:
> If the extent tree has out-of-range physical block numbers, don't try
> to release them.
>
> Also add a similar check in ext2fs_block_alloc_stats2() to avoid a
> NULL pointer dereference.
>
> Reported-by: Nils Bars <[email protected]>
> Reported-by: Moritz Schl?gel <[email protected]>
> Reported-by: Nico Schiller <[email protected]>
> Signed-off-by: Theodore Ts'o <[email protected]>
> ---
> lib/ext2fs/alloc_stats.c | 3 ++-
> lib/ext2fs/punch.c | 4 ++++
> 2 files changed, 6 insertions(+), 1 deletion(-)
>
> diff --git a/lib/ext2fs/alloc_stats.c b/lib/ext2fs/alloc_stats.c
> index 3949f618..6f98bcc7 100644
> --- a/lib/ext2fs/alloc_stats.c
> +++ b/lib/ext2fs/alloc_stats.c
> @@ -62,7 +62,8 @@ void ext2fs_block_alloc_stats2(ext2_filsys fs, blk64_t blk, int inuse)
> {
> int group = ext2fs_group_of_blk2(fs, blk);
>
> - if (blk >= ext2fs_blocks_count(fs->super)) {
> + if (blk < fs->super->s_first_data_block ||
> + blk >= ext2fs_blocks_count(fs->super)) {
> #ifndef OMIT_COM_ERR
> com_err("ext2fs_block_alloc_stats", 0,
> "Illegal block number: %lu", (unsigned long) blk);
> diff --git a/lib/ext2fs/punch.c b/lib/ext2fs/punch.c
> index effa1e2d..e2543e1e 100644
> --- a/lib/ext2fs/punch.c
> +++ b/lib/ext2fs/punch.c
> @@ -200,6 +200,10 @@ static errcode_t punch_extent_blocks(ext2_filsys fs, ext2_ino_t ino,
> __u32 cluster_freed;
> errcode_t retval = 0;
>
> + if (free_start < fs->super->s_first_data_block ||
> + (free_start + free_count) >= ext2fs_blocks_count(fs->super))
> + return EXT2_ET_BAD_BLOCK_NUM;
> +
> /* No bigalloc? Just free each block. */
> if (EXT2FS_CLUSTER_RATIO(fs) == 1) {
> *freed += free_count;
> --
> 2.31.0
>