Blame SOURCES/e2fsprogs-1.45.5-e2fsck-fix-use-after-free-in-calculate_tree.patch

393826
From aa011a6deafa48b1015d402d7ec0bd40f9308109 Mon Sep 17 00:00:00 2001
393826
From: Wang Shilong <wshilong@ddn.com>
393826
Date: Mon, 30 Dec 2019 19:52:39 -0500
393826
Subject: [PATCH 09/10] e2fsck: fix use after free in calculate_tree()
393826
393826
The problem is alloc_blocks() will call get_next_block() which might
393826
reallocate outdir->buf, and memory address could be changed after
393826
this.  To fix this, pointers that point into outdir->buf, such as
393826
int_limit and root need to be recaulated based on the new starting
393826
address of outdir->buf.
393826
393826
[ Changed to correctly recalculate int_limit, and to optimize how we
393826
  reallocate outdir->buf.  -TYT ]
393826
393826
Signed-off-by: Wang Shilong <wshilong@ddn.com>
393826
Signed-off-by: Theodore Ts'o <tytso@mit.edu>
393826
---
393826
 e2fsck/rehash.c | 17 ++++++++++++++++-
393826
 1 file changed, 16 insertions(+), 1 deletion(-)
393826
393826
diff --git a/e2fsck/rehash.c b/e2fsck/rehash.c
393826
index 2c908be0..e0764d0b 100644
393826
--- a/e2fsck/rehash.c
393826
+++ b/e2fsck/rehash.c
393826
@@ -297,7 +297,11 @@ static errcode_t get_next_block(ext2_filsys fs, struct out_dir *outdir,
393826
 	errcode_t	retval;
393826
 
393826
 	if (outdir->num >= outdir->max) {
393826
-		retval = alloc_size_dir(fs, outdir, outdir->max + 50);
393826
+		int increment = outdir->max / 10;
393826
+
393826
+		if (increment < 50)
393826
+			increment = 50;
393826
+		retval = alloc_size_dir(fs, outdir, outdir->max + increment);
393826
 		if (retval)
393826
 			return retval;
393826
 	}
393826
@@ -641,6 +645,9 @@ static int alloc_blocks(ext2_filsys fs,
393826
 	if (retval)
393826
 		return retval;
393826
 
393826
+	/* outdir->buf might be reallocated */
393826
+	*prev_ent = (struct ext2_dx_entry *) (outdir->buf + *prev_offset);
393826
+
393826
 	*next_ent = set_int_node(fs, block_start);
393826
 	*limit = (struct ext2_dx_countlimit *)(*next_ent);
393826
 	if (next_offset)
393826
@@ -730,6 +737,9 @@ static errcode_t calculate_tree(ext2_filsys fs,
393826
 					return retval;
393826
 			}
393826
 			if (c3 == 0) {
393826
+				int delta1 = (char *)int_limit - outdir->buf;
393826
+				int delta2 = (char *)root - outdir->buf;
393826
+
393826
 				retval = alloc_blocks(fs, &limit, &int_ent,
393826
 						      &dx_ent, &int_offset,
393826
 						      NULL, outdir, i, &c2,
393826
@@ -737,6 +747,11 @@ static errcode_t calculate_tree(ext2_filsys fs,
393826
 				if (retval)
393826
 					return retval;
393826
 
393826
+				/* outdir->buf might be reallocated */
393826
+				int_limit = (struct ext2_dx_countlimit *)
393826
+					(outdir->buf + delta1);
393826
+				root = (struct ext2_dx_entry *)
393826
+					(outdir->buf + delta2);
393826
 			}
393826
 			dx_ent->block = ext2fs_cpu_to_le32(i);
393826
 			if (c3 != limit->limit)
393826
-- 
393826
2.21.1
393826