1dc728923Sopenharmony_ciFrom 7464397a0c5df0416a7ef3436747045b36fb7882 Mon Sep 17 00:00:00 2001
2dc728923Sopenharmony_ciFrom: Theodore Ts'o <tytso@mit.edu>
3dc728923Sopenharmony_ciDate: Tue, 9 Aug 2022 10:52:57 -0400
4dc728923Sopenharmony_ciSubject: e2fsck: fix potential fencepost error in
5dc728923Sopenharmony_ci e2fsck_should_rebuild_extents()
6dc728923Sopenharmony_ci
7dc728923Sopenharmony_ciThe ext2_extent_info.max_depth is zero-based (e.g., it is zero when
8dc728923Sopenharmony_cithe entire extent tree fits in the inode).  Hence, if it is equal to
9dc728923Sopenharmony_ciMAX_EXTENT_DEPTH_COUNT we should always rebuild the extent tree to
10dc728923Sopenharmony_cishorten it.
11dc728923Sopenharmony_ci
12dc728923Sopenharmony_ciAlso, for 1k block file systems, it's possible for the worst-case
13dc728923Sopenharmony_ciextent tree in its most compact form to have a maximum depth of 6, not
14dc728923Sopenharmony_ci5.  So set MAX_EXTENT_DEPTH_COUNT to 8 just to be sure we have plenty
15dc728923Sopenharmony_ciof headroom.  (The kernel supports an extent depth up to 2**16, but
16dc728923Sopenharmony_cie2fsck only keeps statistics up to MAX_EXTENT_DEPTH_COUNT, and if it's
17dc728923Sopenharmony_cideeper than that, we know that it will be profitable to rebuild the
18dc728923Sopenharmony_ciextent tree in any case.)
19dc728923Sopenharmony_ci
20dc728923Sopenharmony_ciAddresses-Coverity-Bug: 1507761
21dc728923Sopenharmony_ciSigned-off-by: Theodore Ts'o <tytso@mit.edu>
22dc728923Sopenharmony_ci---
23dc728923Sopenharmony_ci e2fsck/e2fsck.h  | 2 +-
24dc728923Sopenharmony_ci e2fsck/extents.c | 2 +-
25dc728923Sopenharmony_ci 2 files changed, 2 insertions(+), 2 deletions(-)
26dc728923Sopenharmony_ci
27dc728923Sopenharmony_cidiff --git a/e2fsck/e2fsck.h b/e2fsck/e2fsck.h
28dc728923Sopenharmony_ciindex 00b20919..75baf2cd 100644
29dc728923Sopenharmony_ci--- a/e2fsck/e2fsck.h
30dc728923Sopenharmony_ci+++ b/e2fsck/e2fsck.h
31dc728923Sopenharmony_ci@@ -233,7 +233,7 @@ typedef struct ea_refcount *ext2_refcount_t;
32dc728923Sopenharmony_ci  */
33dc728923Sopenharmony_ci typedef struct e2fsck_struct *e2fsck_t;
34dc728923Sopenharmony_ci 
35dc728923Sopenharmony_ci-#define MAX_EXTENT_DEPTH_COUNT 5
36dc728923Sopenharmony_ci+#define MAX_EXTENT_DEPTH_COUNT 8
37dc728923Sopenharmony_ci 
38dc728923Sopenharmony_ci /*
39dc728923Sopenharmony_ci  * This strucutre is used to manage the list of extents in a file. Placing
40dc728923Sopenharmony_cidiff --git a/e2fsck/extents.c b/e2fsck/extents.c
41dc728923Sopenharmony_ciindex 86fe00e7..70798f34 100644
42dc728923Sopenharmony_ci--- a/e2fsck/extents.c
43dc728923Sopenharmony_ci+++ b/e2fsck/extents.c
44dc728923Sopenharmony_ci@@ -583,7 +583,7 @@ errcode_t e2fsck_should_rebuild_extents(e2fsck_t ctx,
45dc728923Sopenharmony_ci 			    sizeof(struct ext3_extent);
46dc728923Sopenharmony_ci 
47dc728923Sopenharmony_ci 	/* If the extent tree is too deep, then rebuild it. */
48dc728923Sopenharmony_ci-	if (info->max_depth > MAX_EXTENT_DEPTH_COUNT) {
49dc728923Sopenharmony_ci+	if (info->max_depth > MAX_EXTENT_DEPTH_COUNT-1) {
50dc728923Sopenharmony_ci 		pctx->blk = info->max_depth;
51dc728923Sopenharmony_ci 		op = PR_1E_CAN_COLLAPSE_EXTENT_TREE;
52dc728923Sopenharmony_ci 		goto rebuild;
53dc728923Sopenharmony_ci-- 
54dc728923Sopenharmony_cicgit 
55dc728923Sopenharmony_ci
56