[PATCH v2] bcachefs: fix shift oob in alloc_lru_idx_fragmentation

Jeongjun Park posted 1 patch 1 month ago
There is a newer version of this series
fs/bcachefs/alloc_background.h | 3 +++
1 file changed, 3 insertions(+)
[PATCH v2] bcachefs: fix shift oob in alloc_lru_idx_fragmentation
Posted by Jeongjun Park 1 month ago
The size of a.data_type is set abnormally large, causing shift-out-of-bounds.
To fix this, we need to add validation on a.data_type in 
alloc_lru_idx_fragmentation().

Reported-by: syzbot+7f45fa9805c40db3f108@syzkaller.appspotmail.com
Fixes: 260af1562ec1 ("bcachefs: Kill alloc_v4.fragmentation_lru")
Signed-off-by: Jeongjun Park <aha310510@gmail.com>
---
 fs/bcachefs/alloc_background.h | 3 +++
 1 file changed, 3 insertions(+)

diff --git a/fs/bcachefs/alloc_background.h b/fs/bcachefs/alloc_background.h
index f8e87c6721b1..91eb96b19a76 100644
--- a/fs/bcachefs/alloc_background.h
+++ b/fs/bcachefs/alloc_background.h
@@ -168,6 +168,9 @@ static inline bool data_type_movable(enum bch_data_type type)
 static inline u64 alloc_lru_idx_fragmentation(struct bch_alloc_v4 a,
 					      struct bch_dev *ca)
 {
+	if (a.data_type > BCH_DATA_NR)
+		return 0;
+
 	if (!data_type_movable(a.data_type) ||
 	    !bch2_bucket_sectors_fragmented(ca, a))
 		return 0;
--
Re: [PATCH v2] bcachefs: fix shift oob in alloc_lru_idx_fragmentation
Posted by Alan Huang 1 month ago
On Oct 21, 2024, at 23:10, Jeongjun Park <aha310510@gmail.com> wrote:
> 
> The size of a.data_type is set abnormally large, causing shift-out-of-bounds.
> To fix this, we need to add validation on a.data_type in 
> alloc_lru_idx_fragmentation().
> 
> Reported-by: syzbot+7f45fa9805c40db3f108@syzkaller.appspotmail.com
> Fixes: 260af1562ec1 ("bcachefs: Kill alloc_v4.fragmentation_lru")
> Signed-off-by: Jeongjun Park <aha310510@gmail.com>
> ---
> fs/bcachefs/alloc_background.h | 3 +++
> 1 file changed, 3 insertions(+)
> 
> diff --git a/fs/bcachefs/alloc_background.h b/fs/bcachefs/alloc_background.h
> index f8e87c6721b1..91eb96b19a76 100644
> --- a/fs/bcachefs/alloc_background.h
> +++ b/fs/bcachefs/alloc_background.h
> @@ -168,6 +168,9 @@ static inline bool data_type_movable(enum bch_data_type type)
> static inline u64 alloc_lru_idx_fragmentation(struct bch_alloc_v4 a,
>      struct bch_dev *ca)
> {
> + if (a.data_type > BCH_DATA_NR)

This should be  >= ?

> + return 0;
> +
> if (!data_type_movable(a.data_type) ||
>    !bch2_bucket_sectors_fragmented(ca, a))
> return 0;
> --
Re: [PATCH v2] bcachefs: fix shift oob in alloc_lru_idx_fragmentation
Posted by Jeongjun Park 1 month ago
Alan Huang <mmpgouride@gmail.com> wrote:
>
> On Oct 21, 2024, at 23:10, Jeongjun Park <aha310510@gmail.com> wrote:
> >
> > The size of a.data_type is set abnormally large, causing shift-out-of-bounds.
> > To fix this, we need to add validation on a.data_type in
> > alloc_lru_idx_fragmentation().
> >
> > Reported-by: syzbot+7f45fa9805c40db3f108@syzkaller.appspotmail.com
> > Fixes: 260af1562ec1 ("bcachefs: Kill alloc_v4.fragmentation_lru")
> > Signed-off-by: Jeongjun Park <aha310510@gmail.com>
> > ---
> > fs/bcachefs/alloc_background.h | 3 +++
> > 1 file changed, 3 insertions(+)
> >
> > diff --git a/fs/bcachefs/alloc_background.h b/fs/bcachefs/alloc_background.h
> > index f8e87c6721b1..91eb96b19a76 100644
> > --- a/fs/bcachefs/alloc_background.h
> > +++ b/fs/bcachefs/alloc_background.h
> > @@ -168,6 +168,9 @@ static inline bool data_type_movable(enum bch_data_type type)
> > static inline u64 alloc_lru_idx_fragmentation(struct bch_alloc_v4 a,
> >      struct bch_dev *ca)
> > {
> > + if (a.data_type > BCH_DATA_NR)
>
> This should be  >= ?

Oh, that's right. I checked it myself and it's the maximum value + 1, so
I think I should use >=.

Thanks for letting me know!

Regards,

Jeongjun Park

>
> > + return 0;
> > +
> > if (!data_type_movable(a.data_type) ||
> >    !bch2_bucket_sectors_fragmented(ca, a))
> > return 0;
> > --
>