From: Pei Li <peili.dev@gmail.com>
To: Chris Mason <clm@fb.com>, Josef Bacik <josef@toxicpanda.com>,
David Sterba <dsterba@suse.com>
Cc: linux-btrfs@vger.kernel.org, linux-kernel@vger.kernel.org,
skhan@linuxfoundation.org, syzkaller-bugs@googlegroups.com,
linux-kernel-mentees@lists.linuxfoundation.org,
syzbot+853d80cba98ce1157ae6@syzkaller.appspotmail.com,
Pei Li <peili.dev@gmail.com>
Subject: [PATCH] btrfs: Fix slab-use-after-free Read in add_ra_bio_pages
Date: Wed, 10 Jul 2024 20:42:47 -0700 [thread overview]
Message-ID: <20240710-bug11-v1-1-aa02297fbbc9@gmail.com> (raw)
We are accessing the start and len field in em after it is free'd.
This patch stores the values that we are going to access from em before
it was free'd so we won't access free'd memory.
Reported-by: syzbot+853d80cba98ce1157ae6@syzkaller.appspotmail.com
Closes: https://syzkaller.appspot.com/bug?extid=853d80cba98ce1157ae6
Signed-off-by: Pei Li <peili.dev@gmail.com>
---
Syzbot reported the following error:
BUG: KASAN: slab-use-after-free in add_ra_bio_pages.constprop.0.isra.0+0xf03/0xfb0 fs/btrfs/compression.c:529
This is because we are reading the values from em right after freeing it
before through free_extent_map(em).
This patch stores the values that we are going to access from em before
it was free'd so we won't access free'd memory.
---
fs/btrfs/compression.c | 10 ++++++++--
1 file changed, 8 insertions(+), 2 deletions(-)
diff --git a/fs/btrfs/compression.c b/fs/btrfs/compression.c
index 6441e47d8a5e..42b528aee63b 100644
--- a/fs/btrfs/compression.c
+++ b/fs/btrfs/compression.c
@@ -449,6 +449,7 @@ static noinline int add_ra_bio_pages(struct inode *inode,
u64 page_end;
u64 pg_index = cur >> PAGE_SHIFT;
u32 add_size;
+ u64 start = 0, len = 0;
if (pg_index > end_index)
break;
@@ -500,12 +501,17 @@ static noinline int add_ra_bio_pages(struct inode *inode,
em = lookup_extent_mapping(em_tree, cur, page_end + 1 - cur);
read_unlock(&em_tree->lock);
+ if (em) {
+ start = em->start;
+ len = em->len;
+ }
+
/*
* At this point, we have a locked page in the page cache for
* these bytes in the file. But, we have to make sure they map
* to this compressed extent on disk.
*/
- if (!em || cur < em->start ||
+ if (!em || cur < start ||
(cur + fs_info->sectorsize > extent_map_end(em)) ||
(em->block_start >> SECTOR_SHIFT) != orig_bio->bi_iter.bi_sector) {
free_extent_map(em);
@@ -526,7 +532,7 @@ static noinline int add_ra_bio_pages(struct inode *inode,
}
}
- add_size = min(em->start + em->len, page_end + 1) - cur;
+ add_size = min(start + len, page_end + 1) - cur;
ret = bio_add_page(orig_bio, page, add_size, offset_in_page(cur));
if (ret != add_size) {
unlock_extent(tree, cur, page_end, NULL);
---
base-commit: 563a50672d8a86ec4b114a4a2f44d6e7ff855f5b
change-id: 20240710-bug11-a8ac18afb724
Best regards,
--
Pei Li <peili.dev@gmail.com>
next reply other threads:[~2024-07-11 3:42 UTC|newest]
Thread overview: 2+ messages / expand[flat|nested] mbox.gz Atom feed top
2024-07-11 3:42 Pei Li [this message]
2024-07-11 3:56 ` [PATCH] btrfs: Fix slab-use-after-free Read in add_ra_bio_pages Qu Wenruo
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20240710-bug11-v1-1-aa02297fbbc9@gmail.com \
--to=peili.dev@gmail.com \
--cc=clm@fb.com \
--cc=dsterba@suse.com \
--cc=josef@toxicpanda.com \
--cc=linux-btrfs@vger.kernel.org \
--cc=linux-kernel-mentees@lists.linuxfoundation.org \
--cc=linux-kernel@vger.kernel.org \
--cc=skhan@linuxfoundation.org \
--cc=syzbot+853d80cba98ce1157ae6@syzkaller.appspotmail.com \
--cc=syzkaller-bugs@googlegroups.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox