Andrew Geissler | 82c905d | 2020-04-13 13:39:40 -0500 | [diff] [blame] | 1 | From: Wang Shilong <wshilong@ddn.com> |
| 2 | Date: Mon, 30 Dec 2019 19:52:39 -0500 |
| 3 | Subject: e2fsck: fix use after free in calculate_tree() |
| 4 | |
| 5 | The problem is alloc_blocks() will call get_next_block() which might |
| 6 | reallocate outdir->buf, and memory address could be changed after |
| 7 | this. To fix this, pointers that point into outdir->buf, such as |
| 8 | int_limit and root need to be recaulated based on the new starting |
| 9 | address of outdir->buf. |
| 10 | |
| 11 | [ Changed to correctly recalculate int_limit, and to optimize how we |
| 12 | reallocate outdir->buf. -TYT ] |
| 13 | |
| 14 | Addresses-Debian-Bug: 948517 |
| 15 | Signed-off-by: Wang Shilong <wshilong@ddn.com> |
| 16 | Signed-off-by: Theodore Ts'o <tytso@mit.edu> |
| 17 | (cherry picked from commit 101e73e99ccafa0403fcb27dd7413033b587ca01) |
| 18 | |
| 19 | Signed-off-by: Anuj Mittal <anuj.mittal@intel.com> |
| 20 | Upstream-Status: Backport [https://git.kernel.org/pub/scm/fs/ext2/e2fsprogs.git/commit/?id=101e73e99ccafa0403fcb27dd7413033b587ca01] |
| 21 | --- |
| 22 | e2fsck/rehash.c | 17 ++++++++++++++++- |
| 23 | 1 file changed, 16 insertions(+), 1 deletion(-) |
| 24 | |
| 25 | diff --git a/e2fsck/rehash.c b/e2fsck/rehash.c |
| 26 | index 0a5888a9..2574e151 100644 |
| 27 | --- a/e2fsck/rehash.c |
| 28 | +++ b/e2fsck/rehash.c |
| 29 | @@ -295,7 +295,11 @@ static errcode_t get_next_block(ext2_filsys fs, struct out_dir *outdir, |
| 30 | errcode_t retval; |
| 31 | |
| 32 | if (outdir->num >= outdir->max) { |
| 33 | - retval = alloc_size_dir(fs, outdir, outdir->max + 50); |
| 34 | + int increment = outdir->max / 10; |
| 35 | + |
| 36 | + if (increment < 50) |
| 37 | + increment = 50; |
| 38 | + retval = alloc_size_dir(fs, outdir, outdir->max + increment); |
| 39 | if (retval) |
| 40 | return retval; |
| 41 | } |
| 42 | @@ -637,6 +641,9 @@ static int alloc_blocks(ext2_filsys fs, |
| 43 | if (retval) |
| 44 | return retval; |
| 45 | |
| 46 | + /* outdir->buf might be reallocated */ |
| 47 | + *prev_ent = (struct ext2_dx_entry *) (outdir->buf + *prev_offset); |
| 48 | + |
| 49 | *next_ent = set_int_node(fs, block_start); |
| 50 | *limit = (struct ext2_dx_countlimit *)(*next_ent); |
| 51 | if (next_offset) |
| 52 | @@ -726,6 +733,9 @@ static errcode_t calculate_tree(ext2_filsys fs, |
| 53 | return retval; |
| 54 | } |
| 55 | if (c3 == 0) { |
| 56 | + int delta1 = (char *)int_limit - outdir->buf; |
| 57 | + int delta2 = (char *)root - outdir->buf; |
| 58 | + |
| 59 | retval = alloc_blocks(fs, &limit, &int_ent, |
| 60 | &dx_ent, &int_offset, |
| 61 | NULL, outdir, i, &c2, |
| 62 | @@ -733,6 +743,11 @@ static errcode_t calculate_tree(ext2_filsys fs, |
| 63 | if (retval) |
| 64 | return retval; |
| 65 | |
| 66 | + /* outdir->buf might be reallocated */ |
| 67 | + int_limit = (struct ext2_dx_countlimit *) |
| 68 | + (outdir->buf + delta1); |
| 69 | + root = (struct ext2_dx_entry *) |
| 70 | + (outdir->buf + delta2); |
| 71 | } |
| 72 | dx_ent->block = ext2fs_cpu_to_le32(i); |
| 73 | if (c3 != limit->limit) |
| 74 | -- |
| 75 | 2.24.1 |
| 76 | |