2021-05-06 19:55:22

by Anirudh Rayabharam

[permalink] [raw]
Subject: [PATCH v2] ext4: fix kernel infoleak via ext4_extent_header

Initialize eh_generation of struct ext4_extent_header to prevent leaking
info to userspace. Fixes KMSAN kernel-infoleak bug reported by syzbot at:
http://syzkaller.appspot.com/bug?id=78e9ad0e6952a3ca16e8234724b2fa92d041b9b8

Reported-by: [email protected]
Fixes: a86c61812637 ("[PATCH] ext3: add extent map support")
Signed-off-by: Anirudh Rayabharam <[email protected]>
---

Changes in v2:
Added a "Fixes:" tag.

v1: https://lore.kernel.org/lkml/[email protected]/

---
fs/ext4/extents.c | 3 +++
1 file changed, 3 insertions(+)

diff --git a/fs/ext4/extents.c b/fs/ext4/extents.c
index 77c84d6f1af6..677d4821bcc1 100644
--- a/fs/ext4/extents.c
+++ b/fs/ext4/extents.c
@@ -825,6 +825,7 @@ void ext4_ext_tree_init(handle_t *handle, struct inode *inode)
eh->eh_entries = 0;
eh->eh_magic = EXT4_EXT_MAGIC;
eh->eh_max = cpu_to_le16(ext4_ext_space_root(inode, 0));
+ eh->eh_generation = 0;
ext4_mark_inode_dirty(handle, inode);
}

@@ -1090,6 +1091,7 @@ static int ext4_ext_split(handle_t *handle, struct inode *inode,
neh->eh_max = cpu_to_le16(ext4_ext_space_block(inode, 0));
neh->eh_magic = EXT4_EXT_MAGIC;
neh->eh_depth = 0;
+ neh->eh_generation = 0;

/* move remainder of path[depth] to the new leaf */
if (unlikely(path[depth].p_hdr->eh_entries !=
@@ -1167,6 +1169,7 @@ static int ext4_ext_split(handle_t *handle, struct inode *inode,
neh->eh_magic = EXT4_EXT_MAGIC;
neh->eh_max = cpu_to_le16(ext4_ext_space_block_idx(inode, 0));
neh->eh_depth = cpu_to_le16(depth - i);
+ neh->eh_generation = 0;
fidx = EXT_FIRST_INDEX(neh);
fidx->ei_block = border;
ext4_idx_store_pblock(fidx, oldblock);
--
2.26.2


2021-06-17 02:38:07

by Theodore Ts'o

[permalink] [raw]
Subject: Re: [PATCH v2] ext4: fix kernel infoleak via ext4_extent_header

On Fri, May 07, 2021 at 12:26:54AM +0530, Anirudh Rayabharam wrote:
> Initialize eh_generation of struct ext4_extent_header to prevent leaking
> info to userspace. Fixes KMSAN kernel-infoleak bug reported by syzbot at:
> http://syzkaller.appspot.com/bug?id=78e9ad0e6952a3ca16e8234724b2fa92d041b9b8
>
> Reported-by: [email protected]
> Fixes: a86c61812637 ("[PATCH] ext3: add extent map support")
> Signed-off-by: Anirudh Rayabharam <[email protected]>

Applied, thanks.

- Ted

2022-12-26 14:50:46

by Lizhi Xu

[permalink] [raw]
Subject: How can this fix prevent information from leaking to user space and prevent the kernel from crashing?

Hi, Anirudh Rayabharam

I verify this patch in the following environment, using reproducer: https://syzkaller.appspot.com/x/repro.c?x=122870ff800000

1. kernel version: kernel version 5.15.72
2. gcc 11.3
3. libc 2.35

Because the kernel version 5.15.72 already contains this patch ce3aba43599f0b50adbebff133df8d08a3d5fffe,
So I deleted this patch to make a kernel image to reproduce the problem,
On the other hand, I reserve this patch to verify that the problem has been fixed,
The result of the experiment is that no matter whether this patch is applied or not,
this problem cannot be reproduced in kernel version 5.15.72.

In addition, I am also very confused. There are three places to initialize "eh_generation".
There is no other reference to the parameter "eh_generation" in all the source code of the kernel,
At the same time, there is no indirect operation on the parameter "eh_generation" in reproducer,
How can this fix prevent information from leaking to user space and prevent the kernel from crashing?

> diff --git a/fs/ext4/extents.c b/fs/ext4/extents.c
> index 77c84d6f1af6..677d4821bcc1 100644
> --- a/fs/ext4/extents.c
> +++ b/fs/ext4/extents.c
> @@ -825,6 +825,7 @@ void ext4_ext_tree_init(handle_t *handle, struct inode *inode)
> eh->eh_entries = 0;
> eh->eh_magic = EXT4_EXT_MAGIC;
> eh->eh_max = cpu_to_le16(ext4_ext_space_root(inode, 0));
> + eh->eh_generation = 0;
> ext4_mark_inode_dirty(handle, inode);
> }
>
> @@ -1090,6 +1091,7 @@ static int ext4_ext_split(handle_t *handle, struct inode *inode,
> neh->eh_max = cpu_to_le16(ext4_ext_space_block(inode, 0));
> neh->eh_magic = EXT4_EXT_MAGIC;
> neh->eh_depth = 0;
> + neh->eh_generation = 0;
>
> /* move remainder of path[depth] to the new leaf */
> if (unlikely(path[depth].p_hdr->eh_entries !=
> @@ -1167,6 +1169,7 @@ static int ext4_ext_split(handle_t *handle, struct inode *inode,
> neh->eh_magic = EXT4_EXT_MAGIC;
> neh->eh_max = cpu_to_le16(ext4_ext_space_block_idx(inode, 0));
> neh->eh_depth = cpu_to_le16(depth - i);
> + neh->eh_generation = 0;
> fidx = EXT_FIRST_INDEX(neh);
> fidx->ei_block = border;
> ext4_idx_store_pblock(fidx, oldblock);
> --
> 2.26.2

2022-12-29 20:38:49

by Eric Biggers

[permalink] [raw]
Subject: Re: How can this fix prevent information from leaking to user space and prevent the kernel from crashing?

On Mon, Dec 26, 2022 at 10:31:19PM +0800, Lizhi Xu wrote:
> Hi, Anirudh Rayabharam
>
> I verify this patch in the following environment, using reproducer: https://syzkaller.appspot.com/x/repro.c?x=122870ff800000
>
> 1. kernel version: kernel version 5.15.72
> 2. gcc 11.3
> 3. libc 2.35
>
> Because the kernel version 5.15.72 already contains this patch ce3aba43599f0b50adbebff133df8d08a3d5fffe,
> So I deleted this patch to make a kernel image to reproduce the problem,
> On the other hand, I reserve this patch to verify that the problem has been fixed,
> The result of the experiment is that no matter whether this patch is applied or not,
> this problem cannot be reproduced in kernel version 5.15.72.
>
> In addition, I am also very confused. There are three places to initialize "eh_generation".
> There is no other reference to the parameter "eh_generation" in all the source code of the kernel,
> At the same time, there is no indirect operation on the parameter "eh_generation" in reproducer,
> How can this fix prevent information from leaking to user space and prevent the kernel from crashing?
>
> > diff --git a/fs/ext4/extents.c b/fs/ext4/extents.c
> > index 77c84d6f1af6..677d4821bcc1 100644
> > --- a/fs/ext4/extents.c
> > +++ b/fs/ext4/extents.c
> > @@ -825,6 +825,7 @@ void ext4_ext_tree_init(handle_t *handle, struct inode *inode)
> > eh->eh_entries = 0;
> > eh->eh_magic = EXT4_EXT_MAGIC;
> > eh->eh_max = cpu_to_le16(ext4_ext_space_root(inode, 0));
> > + eh->eh_generation = 0;
> > ext4_mark_inode_dirty(handle, inode);
> > }
> >
> > @@ -1090,6 +1091,7 @@ static int ext4_ext_split(handle_t *handle, struct inode *inode,
> > neh->eh_max = cpu_to_le16(ext4_ext_space_block(inode, 0));
> > neh->eh_magic = EXT4_EXT_MAGIC;
> > neh->eh_depth = 0;
> > + neh->eh_generation = 0;
> >
> > /* move remainder of path[depth] to the new leaf */
> > if (unlikely(path[depth].p_hdr->eh_entries !=
> > @@ -1167,6 +1169,7 @@ static int ext4_ext_split(handle_t *handle, struct inode *inode,
> > neh->eh_magic = EXT4_EXT_MAGIC;
> > neh->eh_max = cpu_to_le16(ext4_ext_space_block_idx(inode, 0));
> > neh->eh_depth = cpu_to_le16(depth - i);
> > + neh->eh_generation = 0;
> > fidx = EXT_FIRST_INDEX(neh);
> > fidx->ei_block = border;
> > ext4_idx_store_pblock(fidx, oldblock);

The information leak was that uninitialized memory was being written to disk.

The way this bug was detected is with KMSAN. If your kernel does not have KMSAN
enabled, the reproducer will not appear to do anything. Note that KMSAN
requires Linux v6.1 or later and clang v14.0.6 or later.

- Eric