Skip to contentSkip to conversation

Fixes for dir_index support in libext2fs

PATCH Applied Jan Kara 3 participants 14 messages Mar 30, 2020 – Apr 10, 2020

lore
Share & tools
lore

Jan Kara

author
#0 lore raw

cover Fixes for dir_index support in libext2fs

Hello,

here are two small fixes that I've spotted in the new support for adding entries into indexed directories.

Honza

author
#1 lore raw

PATCH 1/2 ext2fs: Fix error checking in dx_link()

dx_lookup() uses errcode_t return values. As such anything non-zero is an error, not values less than zero. Fix the error checking to avoid crashes on corrupted filesystems.

---
 lib/ext2fs/link.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
lib/ext2fs/link.c +1 -1
index 6f523aee718c..7b5bb022117c 100644
@@ -571,7 +571,7 @@ static errcode_t dx_link(ext2_filsys fs, ext2_ino_t dir,
 	dx_info.namelen = strlen(name);
 again:
 	retval = dx_lookup(fs, dir, diri, &dx_info);
-	if (retval < 0)
+	if (retval)
 		goto free_buf;
 
 	retval = add_dirent_to_buf(fs,
Signature · 1 line
2.16.4
#2 lore raw

Re: [PATCH 1/2] ext2fs: Fix error checking in dx_link()

Quoted 3 lines · Jan Kara

dx_lookup() uses errcode_t return values. As such anything non-zero is an error, not values less than zero. Fix the error checking to avoid crashes on corrupted filesystems.

Looks good, thanks.

Quoted 22 lines
---
 lib/ext2fs/link.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
lib/ext2fs/link.c +1 -1
index 6f523aee718c..7b5bb022117c 100644
@@ -571,7 +571,7 @@ static errcode_t dx_link(ext2_filsys fs, ext2_ino_t dir,
 	dx_info.namelen = strlen(name);
 again:
 	retval = dx_lookup(fs, dir, diri, &dx_info);
-	if (retval < 0)
+	if (retval)
 		goto free_buf;
 
 	retval = add_dirent_to_buf(fs,
Signature · 1 line
2.16.4
Reviewed-by
#3 lore raw

Re: [PATCH 1/2] ext2fs: Fix error checking in dx_link()

Quoted 8 lines · Lukas Czerner
Quoted 3 lines · Jan Kara

dx_lookup() uses errcode_t return values. As such anything non-zero is an error, not values less than zero. Fix the error checking to avoid crashes on corrupted filesystems.

Looks good, thanks.

of course that should be

-Lukas

Quoted 24 lines
Quoted 23 lines
---
 lib/ext2fs/link.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
lib/ext2fs/link.c +1 -1
index 6f523aee718c..7b5bb022117c 100644
@@ -571,7 +571,7 @@ static errcode_t dx_link(ext2_filsys fs, ext2_ino_t dir,
 	dx_info.namelen = strlen(name);
 again:
 	retval = dx_lookup(fs, dir, diri, &dx_info);
-	if (retval < 0)
+	if (retval)
 		goto free_buf;
 
 	retval = add_dirent_to_buf(fs,
Signature · 1 line
2.16.4
author
#4 lore raw

Re: [PATCH 1/2] ext2fs: Fix error checking in dx_link()

Quoted 6 lines · Lukas Czerner
Quoted 5 lines · Lukas Czerner
Quoted 3 lines · Jan Kara

dx_lookup() uses errcode_t return values. As such anything non-zero is an error, not values less than zero. Fix the error checking to avoid crashes on corrupted filesystems.

Quoted 7 lines
Quoted 3 lines

Looks good, thanks.

of course that should be

Thanks for review!

Honza

Quoted 29 lines

-Lukas

Quoted 24 lines
Quoted 23 lines
---
 lib/ext2fs/link.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
lib/ext2fs/link.c +1 -1
index 6f523aee718c..7b5bb022117c 100644
@@ -571,7 +571,7 @@ static errcode_t dx_link(ext2_filsys fs, ext2_ino_t dir,
 	dx_info.namelen = strlen(name);
 again:
 	retval = dx_lookup(fs, dir, diri, &dx_info);
-	if (retval < 0)
+	if (retval)
 		goto free_buf;
 
 	retval = add_dirent_to_buf(fs,
Signature · 1 line
2.16.4
Signature · 2 lines
Jan Kara <[email protected]>
SUSE Labs, CR
#5 lore raw

Re: [PATCH 1/2] ext2fs: Fix error checking in dx_link()

In reply to Jan Kara
Quoted 5 lines · Jan Kara

dx_lookup() uses errcode_t return values. As such anything non-zero is an error, not values less than zero. Fix the error checking to avoid crashes on corrupted filesystems.

Applied, thanks.

- Ted

author
#6 lore raw

PATCH 2/2 ext2fs: Fix off-by-one in dx_grow_tree()

In reply to Jan Kara

There is an off-by-one error in dx_grow_tree() when checking whether we can add another level to the tree. Thus we can grow tree too much leading to possible crashes in the library or corrupted filesystem. Fix the bug.

---
 lib/ext2fs/link.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
lib/ext2fs/link.c +1 -1
index 7b5bb022117c..469eea8cd06d 100644
@@ -473,7 +473,7 @@ static errcode_t dx_grow_tree(ext2_filsys fs, ext2_ino_t dir,
 		    ext2fs_le16_to_cpu(info->frames[i].head->limit))
 			break;
 	/* Need to grow tree depth? */
-	if (i < 0 && info->levels > ext2_dir_htree_level(fs))
+	if (i < 0 && info->levels >= ext2_dir_htree_level(fs))
 		return EXT2_ET_DIR_NO_SPACE;
 	lblk = size / fs->blocksize;
 	size += fs->blocksize;
Signature · 1 line
2.16.4
Reviewed-by on v1 2/2
#7 lore raw

Re: [PATCH 2/2] ext2fs: Fix off-by-one in dx_grow_tree()

Quoted 4 lines · Jan Kara

There is an off-by-one error in dx_grow_tree() when checking whether we can add another level to the tree. Thus we can grow tree too much leading to possible crashes in the library or corrupted filesystem. Fix the bug.

Looks good, thanks

Don't we have basically the same off-by-one in
e2fsck/pass1.c handle_htree() ?

if ((root->indirect_levels > ext2_dir_htree_level(fs)) &&
fix_problem(ctx, PR_1_HTREE_DEPTH, pctx))

-Lukas

Quoted 22 lines
---
 lib/ext2fs/link.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
lib/ext2fs/link.c +1 -1
index 7b5bb022117c..469eea8cd06d 100644
@@ -473,7 +473,7 @@ static errcode_t dx_grow_tree(ext2_filsys fs, ext2_ino_t dir,
 		    ext2fs_le16_to_cpu(info->frames[i].head->limit))
 			break;
 	/* Need to grow tree depth? */
-	if (i < 0 && info->levels > ext2_dir_htree_level(fs))
+	if (i < 0 && info->levels >= ext2_dir_htree_level(fs))
 		return EXT2_ET_DIR_NO_SPACE;
 	lblk = size / fs->blocksize;
 	size += fs->blocksize;
Signature · 1 line
2.16.4
author
#8 lore raw

Re: [PATCH 2/2] ext2fs: Fix off-by-one in dx_grow_tree()

Quoted 8 lines · Lukas Czerner
Quoted 4 lines · Jan Kara

There is an off-by-one error in dx_grow_tree() when checking whether we can add another level to the tree. Thus we can grow tree too much leading to possible crashes in the library or corrupted filesystem. Fix the bug.

Looks good, thanks

Quoted 7 lines

Don't we have basically the same off-by-one in
e2fsck/pass1.c handle_htree() ?

if ((root->indirect_levels > ext2_dir_htree_level(fs)) &&
fix_problem(ctx, PR_1_HTREE_DEPTH, pctx))

I don't think so. It is indeed correct for root->indirect_levels to be equal to ext2_dir_htree_level(). However dx_grow_tree() is going to increase root->indirect_levels by 1 which is where tree would become invalid...

Honza

Quoted 23 lines
Quoted 22 lines
---
 lib/ext2fs/link.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
lib/ext2fs/link.c +1 -1
index 7b5bb022117c..469eea8cd06d 100644
@@ -473,7 +473,7 @@ static errcode_t dx_grow_tree(ext2_filsys fs, ext2_ino_t dir,
 		    ext2fs_le16_to_cpu(info->frames[i].head->limit))
 			break;
 	/* Need to grow tree depth? */
-	if (i < 0 && info->levels > ext2_dir_htree_level(fs))
+	if (i < 0 && info->levels >= ext2_dir_htree_level(fs))
 		return EXT2_ET_DIR_NO_SPACE;
 	lblk = size / fs->blocksize;
 	size += fs->blocksize;
Signature · 1 line
2.16.4
Signature · 2 lines
Jan Kara <[email protected]>
SUSE Labs, CR
#9 lore raw

Re: [PATCH 2/2] ext2fs: Fix off-by-one in dx_grow_tree()

Quoted 17 lines · Jan Kara
Quoted 8 lines · Lukas Czerner
Quoted 4 lines · Jan Kara

There is an off-by-one error in dx_grow_tree() when checking whether we can add another level to the tree. Thus we can grow tree too much leading to possible crashes in the library or corrupted filesystem. Fix the bug.

Looks good, thanks

Quoted 7 lines

Don't we have basically the same off-by-one in
e2fsck/pass1.c handle_htree() ?

if ((root->indirect_levels > ext2_dir_htree_level(fs)) &&
fix_problem(ctx, PR_1_HTREE_DEPTH, pctx))

Quoted 6 lines

I don't think so. It is indeed correct for root->indirect_levels to be equal to ext2_dir_htree_level(). However dx_grow_tree() is going to increase root->indirect_levels by 1 which is where tree would become invalid...

Honza

Hmm, are you sure ? I think the names are very confusing

root->indirect_levels is zero based, while ext2_dir_htree_level() returns the maximum number of levels (that is 3 by default). If I am right then indirect_levels must always be smaller then ext2_dir_htree_level() and that is how we use it everywhere else - the palce I am pointing out is an exception and I think it's a bug.

Indeed it looks like the bug got introduced in
3f0cf647539970474be8f607017ca7eccfc2fbbe

- if ((root->indirect_levels > 1) &&
+ if ((root->indirect_levels > ext2_dir_htree_level(fs)) &&

Or am I missing something ?

-Lukas

Quoted 28 lines
Quoted 24 lines
Quoted 23 lines
---
 lib/ext2fs/link.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
lib/ext2fs/link.c +1 -1
index 7b5bb022117c..469eea8cd06d 100644
@@ -473,7 +473,7 @@ static errcode_t dx_grow_tree(ext2_filsys fs, ext2_ino_t dir,
 		    ext2fs_le16_to_cpu(info->frames[i].head->limit))
 			break;
 	/* Need to grow tree depth? */
-	if (i < 0 && info->levels > ext2_dir_htree_level(fs))
+	if (i < 0 && info->levels >= ext2_dir_htree_level(fs))
 		return EXT2_ET_DIR_NO_SPACE;
 	lblk = size / fs->blocksize;
 	size += fs->blocksize;
Signature · 1 line
2.16.4
Signature · 2 lines
Jan Kara <[email protected]>
SUSE Labs, CR
author
#10 lore raw

Re: [PATCH 2/2] ext2fs: Fix off-by-one in dx_grow_tree()

Quoted 39 lines · Lukas Czerner
Quoted 17 lines · Jan Kara
Quoted 8 lines · Lukas Czerner
Quoted 4 lines · Jan Kara

There is an off-by-one error in dx_grow_tree() when checking whether we can add another level to the tree. Thus we can grow tree too much leading to possible crashes in the library or corrupted filesystem. Fix the bug.

Looks good, thanks

Quoted 7 lines

Don't we have basically the same off-by-one in
e2fsck/pass1.c handle_htree() ?

if ((root->indirect_levels > ext2_dir_htree_level(fs)) &&
fix_problem(ctx, PR_1_HTREE_DEPTH, pctx))

Quoted 6 lines

I don't think so. It is indeed correct for root->indirect_levels to be equal to ext2_dir_htree_level(). However dx_grow_tree() is going to increase root->indirect_levels by 1 which is where tree would become invalid...

Honza

Hmm, are you sure ? I think the names are very confusing

root->indirect_levels is zero based, while ext2_dir_htree_level() returns the maximum number of levels (that is 3 by default). If I am right then indirect_levels must always be smaller then ext2_dir_htree_level() and that is how we use it everywhere else - the palce I am pointing out is an exception and I think it's a bug.

Indeed it looks like the bug got introduced in
3f0cf647539970474be8f607017ca7eccfc2fbbe

(quoted diff) +1 -1
-       if ((root->indirect_levels > 1) &&
+       if ((root->indirect_levels > ext2_dir_htree_level(fs)) &&
Quoted 1 line

Or am I missing something ?

Ah, you're indeed right! e2fsck/pass2.c even has a correct version of the condition. Just the condition in pass1.c is wrong.

Honza

Quoted 32 lines

-Lukas

Quoted 28 lines
Quoted 24 lines
Quoted 23 lines
---
 lib/ext2fs/link.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
lib/ext2fs/link.c +1 -1
index 7b5bb022117c..469eea8cd06d 100644
@@ -473,7 +473,7 @@ static errcode_t dx_grow_tree(ext2_filsys fs, ext2_ino_t dir,
 		    ext2fs_le16_to_cpu(info->frames[i].head->limit))
 			break;
 	/* Need to grow tree depth? */
-	if (i < 0 && info->levels > ext2_dir_htree_level(fs))
+	if (i < 0 && info->levels >= ext2_dir_htree_level(fs))
 		return EXT2_ET_DIR_NO_SPACE;
 	lblk = size / fs->blocksize;
 	size += fs->blocksize;
Signature · 1 line
2.16.4
Signature · 2 lines
Jan Kara <[email protected]>
SUSE Labs, CR
Signature · 2 lines
Jan Kara <[email protected]>
SUSE Labs, CR
#11 lore raw

Re: [PATCH 2/2] ext2fs: Fix off-by-one in dx_grow_tree()

Quoted 16 lines · Jan Kara
Quoted 16 lines
Quoted 6 lines
Quoted 5 lines

Don't we have basically the same off-by-one in
e2fsck/pass1.c handle_htree() ?

if ((root->indirect_levels > ext2_dir_htree_level(fs)) &&
fix_problem(ctx, PR_1_HTREE_DEPTH, pctx))

root->indirect_levels is zero based, while ext2_dir_htree_level() returns the maximum number of levels (that is 3 by default). If I am right then indirect_levels must always be smaller then ext2_dir_htree_level() and that is how we use it everywhere else - the palce I am pointing out is an exception and I think it's a bug.

Indeed it looks like the bug got introduced in
3f0cf647539970474be8f607017ca7eccfc2fbbe

Quoted 7 lines
Quoted 4 lines
(quoted diff) +1 -1
-       if ((root->indirect_levels > 1) &&
+       if ((root->indirect_levels > ext2_dir_htree_level(fs)) &&

Or am I missing something ?

Ah, you're indeed right! e2fsck/pass2.c even has a correct version of the condition. Just the condition in pass1.c is wrong.

I've applied the following fix on the maint branch.

- Ted

commit 759b387775bfd5c9d3692680e5e4b929c3848d51

Author: Theodore Ts'o <[email protected]>
Date:   Fri Apr 10 00:30:52 2020 -0400

e2fsck: fix off-by-one check when validating depth of an htree

Fixes 3f0cf6475399 ("e2fsprogs: add support for 3-level htree")
e2fsck/pass1.c +1 -1
index c9e8bf82..38afda48 100644
@@ -2685,7 +2685,7 @@ static int handle_htree(e2fsck_t ctx, struct problem_context *pctx,
 		return 1;
 
 	pctx->num = root->indirect_levels;
-	if ((root->indirect_levels > ext2_dir_htree_level(fs)) &&
+	if ((root->indirect_levels >= ext2_dir_htree_level(fs)) &&
 	    fix_problem(ctx, PR_1_HTREE_DEPTH, pctx))
 		return 1;
 
author Reviewed-by
#12 lore raw

Re: [PATCH 2/2] ext2fs: Fix off-by-one in dx_grow_tree()

Quoted 30 lines · Theodore Y. Ts'o
Quoted 16 lines · Jan Kara
Quoted 16 lines
Quoted 6 lines
Quoted 5 lines

Don't we have basically the same off-by-one in
e2fsck/pass1.c handle_htree() ?

if ((root->indirect_levels > ext2_dir_htree_level(fs)) &&
fix_problem(ctx, PR_1_HTREE_DEPTH, pctx))

root->indirect_levels is zero based, while ext2_dir_htree_level() returns the maximum number of levels (that is 3 by default). If I am right then indirect_levels must always be smaller then ext2_dir_htree_level() and that is how we use it everywhere else - the palce I am pointing out is an exception and I think it's a bug.

Indeed it looks like the bug got introduced in
3f0cf647539970474be8f607017ca7eccfc2fbbe

Quoted 7 lines
Quoted 4 lines
(quoted diff) +1 -1
-       if ((root->indirect_levels > 1) &&
+       if ((root->indirect_levels > ext2_dir_htree_level(fs)) &&

Or am I missing something ?

Ah, you're indeed right! e2fsck/pass2.c even has a correct version of the condition. Just the condition in pass1.c is wrong.

I've applied the following fix on the maint branch.

- Ted

commit 759b387775bfd5c9d3692680e5e4b929c3848d51

Quoted 8 lines
Author: Theodore Ts'o <[email protected]>
Date:   Fri Apr 10 00:30:52 2020 -0400

e2fsck: fix off-by-one check when validating depth of an htree

Fixes 3f0cf6475399 ("e2fsprogs: add support for 3-level htree")

Cool, thanks! Feel free to add:

Honza

Quoted 14 lines
e2fsck/pass1.c +1 -1
index c9e8bf82..38afda48 100644
@@ -2685,7 +2685,7 @@ static int handle_htree(e2fsck_t ctx, struct problem_context *pctx,
 		return 1;
 
 	pctx->num = root->indirect_levels;
-	if ((root->indirect_levels > ext2_dir_htree_level(fs)) &&
+	if ((root->indirect_levels >= ext2_dir_htree_level(fs)) &&
 	    fix_problem(ctx, PR_1_HTREE_DEPTH, pctx))
 		return 1;
 
Signature · 2 lines
Jan Kara <[email protected]>
SUSE Labs, CR
#13 lore raw

Re: [PATCH 2/2] ext2fs: Fix off-by-one in dx_grow_tree()

In reply to Jan Kara
Quoted 6 lines · Jan Kara

There is an off-by-one error in dx_grow_tree() when checking whether we can add another level to the tree. Thus we can grow tree too much leading to possible crashes in the library or corrupted filesystem. Fix the bug.

Applied, thanks.

- Ted