[PATCH] bcachefs: Add checks for entry's _data bytes

Edward Adam Davis posted 1 patch 1 year, 4 months ago
There is a newer version of this series
fs/bcachefs/sb-clean.c | 3 +++
1 file changed, 3 insertions(+)
[PATCH] bcachefs: Add checks for entry's _data bytes
Posted by Edward Adam Davis 1 year, 4 months ago
syzbot report slab-out-of-bounds in journal_entry_dev_usage_to_text,
it because vstruct_bytes(entry) smaller than sizeof(struct 
jset_entry_dev_usage), overflow occurs when calculating the difference in
jset_entry_dev_usage_nr_types(u).

Reported-by: syzbot+05d7520be047c9be86e0@syzkaller.appspotmail.com
Closes: https://syzkaller.appspot.com/bug?extid=05d7520be047c9be86e0
Signed-off-by: Edward Adam Davis <eadavis@qq.com>
---
 fs/bcachefs/sb-clean.c | 3 +++
 1 file changed, 3 insertions(+)

diff --git a/fs/bcachefs/sb-clean.c b/fs/bcachefs/sb-clean.c
index 47f10ab57f40..278e1a25159a 100644
--- a/fs/bcachefs/sb-clean.c
+++ b/fs/bcachefs/sb-clean.c
@@ -310,6 +310,9 @@ static void bch2_sb_clean_to_text(struct printbuf *out, struct bch_sb *sb,
 		    !entry->u64s)
 			continue;
 
+		if (vstruct_bytes(entry) < sizeof(struct jset_entry_dev_usage))
+			continue;
+
 		bch2_journal_entry_to_text(out, NULL, entry);
 		prt_newline(out);
 	}
-- 
2.43.0
Re: [PATCH] bcachefs: Add checks for entry's _data bytes
Posted by Kent Overstreet 1 year, 4 months ago
On Tue, Jul 30, 2024 at 12:10:19PM GMT, Edward Adam Davis wrote:
> syzbot report slab-out-of-bounds in journal_entry_dev_usage_to_text,
> it because vstruct_bytes(entry) smaller than sizeof(struct 
> jset_entry_dev_usage), overflow occurs when calculating the difference in
> jset_entry_dev_usage_nr_types(u).
> 
> Reported-by: syzbot+05d7520be047c9be86e0@syzkaller.appspotmail.com
> Closes: https://syzkaller.appspot.com/bug?extid=05d7520be047c9be86e0
> Signed-off-by: Edward Adam Davis <eadavis@qq.com>
> ---
>  fs/bcachefs/sb-clean.c | 3 +++
>  1 file changed, 3 insertions(+)
> 
> diff --git a/fs/bcachefs/sb-clean.c b/fs/bcachefs/sb-clean.c
> index 47f10ab57f40..278e1a25159a 100644
> --- a/fs/bcachefs/sb-clean.c
> +++ b/fs/bcachefs/sb-clean.c
> @@ -310,6 +310,9 @@ static void bch2_sb_clean_to_text(struct printbuf *out, struct bch_sb *sb,
>  		    !entry->u64s)
>  			continue;
>  
> +		if (vstruct_bytes(entry) < sizeof(struct jset_entry_dev_usage))
> +			continue;

This is wrong: you haven't checked the entry type and you're assuming
it's a dev_usage entry.

The check should go in journal_entry_dev_usage_to_text().
[PATCH V2] bcachefs: Add checks for entry's _data bytes
Posted by Edward Adam Davis 1 year, 4 months ago
syzbot report slab-out-of-bounds in journal_entry_dev_usage_to_text,
it because vstruct_bytes(entry) smaller than sizeof(struct
jset_entry_dev_usage), overflow occurs when calculating the difference in
jset_entry_dev_usage_nr_types(u).

Reported-by: syzbot+05d7520be047c9be86e0@syzkaller.appspotmail.com
Signed-off-by: Edward Adam Davis <eadavis@qq.com>
---
 fs/bcachefs/bcachefs_format.h | 5 +++++
 1 file changed, 5 insertions(+)

diff --git a/fs/bcachefs/bcachefs_format.h b/fs/bcachefs/bcachefs_format.h
index e3b1bde489c3..d536da101e89 100644
--- a/fs/bcachefs/bcachefs_format.h
+++ b/fs/bcachefs/bcachefs_format.h
@@ -1252,6 +1252,11 @@ struct jset_entry_dev_usage {
 
 static inline unsigned jset_entry_dev_usage_nr_types(struct jset_entry_dev_usage *u)
 {
+	if (vstruct_bytes(&u->entry) < sizeof(struct jset_entry_dev_usage)) {
+		pr_info("entry data bytes %u is too small", vstruct_bytes(&u->entry));
+		return 0;
+	}
+
 	return (vstruct_bytes(&u->entry) - sizeof(struct jset_entry_dev_usage)) /
 		sizeof(struct jset_entry_dev_usage_type);
 }
-- 
2.43.0