fs/ntfs3: Fix slab-out-of-bounds read in hdr_delete_de()
authorZeng Heng <zengheng4@huawei.com>
Mon, 12 Dec 2022 01:31:34 +0000 (09:31 +0800)
committerKonstantin Komarov <almaz.alexandrovich@paragon-software.com>
Mon, 27 Mar 2023 12:59:12 +0000 (16:59 +0400)
Here is a BUG report from syzbot:

BUG: KASAN: slab-out-of-bounds in hdr_delete_de+0xe0/0x150 fs/ntfs3/index.c:806
Read of size 16842960 at addr ffff888079cc0600 by task syz-executor934/3631

Call Trace:
 memmove+0x25/0x60 mm/kasan/shadow.c:54
 hdr_delete_de+0xe0/0x150 fs/ntfs3/index.c:806
 indx_delete_entry+0x74f/0x3670 fs/ntfs3/index.c:2193
 ni_remove_name+0x27a/0x980 fs/ntfs3/frecord.c:2910
 ntfs_unlink_inode+0x3d4/0x720 fs/ntfs3/inode.c:1712
 ntfs_rename+0x41a/0xcb0 fs/ntfs3/namei.c:276

Before using the meta-data in struct INDEX_HDR, we need to
check index header valid or not. Otherwise, the corruptedi
(or malicious) fs image can cause out-of-bounds access which
could make kernel panic.

Fixes: 82cae269cfa9 ("fs/ntfs3: Add initialization of super block")
Reported-by: syzbot+9c2811fd56591639ff5f@syzkaller.appspotmail.com
Signed-off-by: Zeng Heng <zengheng4@huawei.com>
Signed-off-by: Konstantin Komarov <almaz.alexandrovich@paragon-software.com>
fs/ntfs3/fslog.c
fs/ntfs3/index.c
fs/ntfs3/ntfs_fs.h

index dc723f03d6bb202d7c19dba149c3330b23b6d931..bf7396447284597bf7b999ed065055a1a941757c 100644 (file)
@@ -2575,7 +2575,7 @@ static int read_next_log_rec(struct ntfs_log *log, struct lcb *lcb, u64 *lsn)
        return find_log_rec(log, *lsn, lcb);
 }
 
-static inline bool check_index_header(const struct INDEX_HDR *hdr, size_t bytes)
+bool check_index_header(const struct INDEX_HDR *hdr, size_t bytes)
 {
        __le16 mask;
        u32 min_de, de_off, used, total;
index ae9616becec15b0f675972e73bcc9305be93250d..7a1e01a2ed9ae534f90b26ee325978f857ef6804 100644 (file)
@@ -848,6 +848,10 @@ static inline struct NTFS_DE *hdr_delete_de(struct INDEX_HDR *hdr,
        u32 off = PtrOffset(hdr, re);
        int bytes = used - (off + esize);
 
+       /* check INDEX_HDR valid before using INDEX_HDR */
+       if (!check_index_header(hdr, le32_to_cpu(hdr->total)))
+               return NULL;
+
        if (off >= used || esize < sizeof(struct NTFS_DE) ||
            bytes < sizeof(struct NTFS_DE))
                return NULL;
index 80072e5f96f709f2fb77538b48f0fd8c20c143e3..15296f5690b5a0df5a2a65b20296d5d440f44f42 100644 (file)
@@ -581,6 +581,7 @@ int ni_rename(struct ntfs_inode *dir_ni, struct ntfs_inode *new_dir_ni,
 bool ni_is_dirty(struct inode *inode);
 
 /* Globals from fslog.c */
+bool check_index_header(const struct INDEX_HDR *hdr, size_t bytes);
 int log_replay(struct ntfs_inode *ni, bool *initialized);
 
 /* Globals from fsntfs.c */