[PATCH v2] bcachefs: Fix shift-out-of-bounds in bch2_stripe_to_text

Zhao Mengmeng posted 1 patch 1 month, 2 weeks ago
There is a newer version of this series
fs/bcachefs/ec.c      | 6 ++++++
fs/bcachefs/errcode.h | 3 ++-
2 files changed, 8 insertions(+), 1 deletion(-)
[PATCH v2] bcachefs: Fix shift-out-of-bounds in bch2_stripe_to_text
Posted by Zhao Mengmeng 1 month, 2 weeks ago
From: Zhao Mengmeng <zhaomengmeng@kylinos.cn>

syzbot report a shift-out-of-bounds issue:
------------[ cut here ]------------
UBSAN: shift-out-of-bounds in fs/bcachefs/ec.c:147:2
shift exponent 108 is too large for 32-bit type 'unsigned int'
----
Here s.csum_granularity_bits = 108, so shift is impossible for unsigned
int. To fix, add a check in bch2_stripe_validate() to bail out, it has
same checking logic with ec_stripe_key_init().

Reported-by: syzbot+f8c98a50c323635be65d@syzkaller.appspotmail.com
Tested-by: syzbot+f8c98a50c323635be65d@syzkaller.appspotmail.com
Closes: https://syzkaller.appspot.com/bug?extid=f8c98a50c323635be65d
Suggested-by: Hongbo Li <lihongbo22@huawei.com>
Signed-off-by: Zhao Mengmeng <zhaomengmeng@kylinos.cn>
---
 fs/bcachefs/ec.c      | 6 ++++++
 fs/bcachefs/errcode.h | 3 ++-
 2 files changed, 8 insertions(+), 1 deletion(-)

diff --git a/fs/bcachefs/ec.c b/fs/bcachefs/ec.c
index 141a4c63142f..bc5ff1331c6f 100644
--- a/fs/bcachefs/ec.c
+++ b/fs/bcachefs/ec.c
@@ -113,6 +113,12 @@ int bch2_stripe_validate(struct bch_fs *c, struct bkey_s_c k,
 	const struct bch_stripe *s = bkey_s_c_to_stripe(k).v;
 	int ret = 0;
 
+	if (s->csum_granularity_bits >= ilog2(le16_to_cpu(s->sectors))) {
+		bch_err_ratelimited(c, "stripe csum gran bits %u too big",
+				    s->csum_granularity_bits);
+		return -BCH_ERR_stripe_csum_granularity_bits_too_big;
+	}
+
 	bkey_fsck_err_on(bkey_eq(k.k->p, POS_MIN) ||
 			 bpos_gt(k.k->p, POS(0, U32_MAX)),
 			 c, stripe_pos_bad,
diff --git a/fs/bcachefs/errcode.h b/fs/bcachefs/errcode.h
index 742dcdd3e5d7..14ba6bc7a029 100644
--- a/fs/bcachefs/errcode.h
+++ b/fs/bcachefs/errcode.h
@@ -258,7 +258,8 @@
 	x(BCH_ERR_nopromote,		nopromote_no_writes)			\
 	x(BCH_ERR_nopromote,		nopromote_enomem)			\
 	x(0,				invalid_snapshot_node)			\
-	x(0,				option_needs_open_fs)
+	x(0,				option_needs_open_fs)			\
+	x(EINVAL,			stripe_csum_granularity_bits_too_big)
 
 enum bch_errcode {
 	BCH_ERR_START		= 2048,
-- 
2.43.0
Re: [PATCH v2] bcachefs: Fix shift-out-of-bounds in bch2_stripe_to_text
Posted by Hongbo Li 1 month, 2 weeks ago

On 2024/10/9 13:43, Zhao Mengmeng wrote:
> From: Zhao Mengmeng <zhaomengmeng@kylinos.cn>
> 
> syzbot report a shift-out-of-bounds issue:
> ------------[ cut here ]------------
> UBSAN: shift-out-of-bounds in fs/bcachefs/ec.c:147:2
> shift exponent 108 is too large for 32-bit type 'unsigned int'
> ----
> Here s.csum_granularity_bits = 108, so shift is impossible for unsigned
> int. To fix, add a check in bch2_stripe_validate() to bail out, it has
> same checking logic with ec_stripe_key_init().
> 
> Reported-by: syzbot+f8c98a50c323635be65d@syzkaller.appspotmail.com
> Tested-by: syzbot+f8c98a50c323635be65d@syzkaller.appspotmail.com
> Closes: https://syzkaller.appspot.com/bug?extid=f8c98a50c323635be65d
> Suggested-by: Hongbo Li <lihongbo22@huawei.com>
> Signed-off-by: Zhao Mengmeng <zhaomengmeng@kylinos.cn>
> ---
>   fs/bcachefs/ec.c      | 6 ++++++
>   fs/bcachefs/errcode.h | 3 ++-
>   2 files changed, 8 insertions(+), 1 deletion(-)
> 
> diff --git a/fs/bcachefs/ec.c b/fs/bcachefs/ec.c
> index 141a4c63142f..bc5ff1331c6f 100644
> --- a/fs/bcachefs/ec.c
> +++ b/fs/bcachefs/ec.c
> @@ -113,6 +113,12 @@ int bch2_stripe_validate(struct bch_fs *c, struct bkey_s_c k,
>   	const struct bch_stripe *s = bkey_s_c_to_stripe(k).v;
>   	int ret = 0;
>   
> +	if (s->csum_granularity_bits >= ilog2(le16_to_cpu(s->sectors))) {

csum_granularity_bits should do the left shift.

> +		bch_err_ratelimited(c, "stripe csum gran bits %u too big",
> +				    s->csum_granularity_bits);
> +		return -BCH_ERR_stripe_csum_granularity_bits_too_big;
> +	}
> +
>   	bkey_fsck_err_on(bkey_eq(k.k->p, POS_MIN) ||
>   			 bpos_gt(k.k->p, POS(0, U32_MAX)),
>   			 c, stripe_pos_bad,
> diff --git a/fs/bcachefs/errcode.h b/fs/bcachefs/errcode.h
> index 742dcdd3e5d7..14ba6bc7a029 100644
> --- a/fs/bcachefs/errcode.h
> +++ b/fs/bcachefs/errcode.h
> @@ -258,7 +258,8 @@
>   	x(BCH_ERR_nopromote,		nopromote_no_writes)			\
>   	x(BCH_ERR_nopromote,		nopromote_enomem)			\
>   	x(0,				invalid_snapshot_node)			\
> -	x(0,				option_needs_open_fs)
> +	x(0,				option_needs_open_fs)			\
There are some changes, this should rebase on the latest tree.

Thanks,
Hongbo
> +	x(EINVAL,			stripe_csum_granularity_bits_too_big)
>   
>   enum bch_errcode {
>   	BCH_ERR_START		= 2048,
Re: [PATCH v2] bcachefs: Fix shift-out-of-bounds in bch2_stripe_to_text
Posted by Zhao Mengmeng 1 month, 2 weeks ago
On 2024/10/9 17:10, Hongbo Li wrote:
> 
> 
> On 2024/10/9 13:43, Zhao Mengmeng wrote:
>> From: Zhao Mengmeng <zhaomengmeng@kylinos.cn>
>>
>> syzbot report a shift-out-of-bounds issue:
>> ------------[ cut here ]------------
>> UBSAN: shift-out-of-bounds in fs/bcachefs/ec.c:147:2
>> shift exponent 108 is too large for 32-bit type 'unsigned int'
>> ----
>> Here s.csum_granularity_bits = 108, so shift is impossible for unsigned
>> int. To fix, add a check in bch2_stripe_validate() to bail out, it has
>> same checking logic with ec_stripe_key_init().
>>
>> Reported-by: syzbot+f8c98a50c323635be65d@syzkaller.appspotmail.com
>> Tested-by: syzbot+f8c98a50c323635be65d@syzkaller.appspotmail.com
>> Closes: https://syzkaller.appspot.com/bug?extid=f8c98a50c323635be65d
>> Suggested-by: Hongbo Li <lihongbo22@huawei.com>
>> Signed-off-by: Zhao Mengmeng <zhaomengmeng@kylinos.cn>
>> ---
>>   fs/bcachefs/ec.c      | 6 ++++++
>>   fs/bcachefs/errcode.h | 3 ++-
>>   2 files changed, 8 insertions(+), 1 deletion(-)
>>
>> diff --git a/fs/bcachefs/ec.c b/fs/bcachefs/ec.c
>> index 141a4c63142f..bc5ff1331c6f 100644
>> --- a/fs/bcachefs/ec.c
>> +++ b/fs/bcachefs/ec.c
>> @@ -113,6 +113,12 @@ int bch2_stripe_validate(struct bch_fs *c, struct bkey_s_c k,
>>       const struct bch_stripe *s = bkey_s_c_to_stripe(k).v;
>>       int ret = 0;
>>   +    if (s->csum_granularity_bits >= ilog2(le16_to_cpu(s->sectors))) {
> 
> csum_granularity_bits should do the left shift.

If use `1 << s->csum_granularity_bits(108)` to do the check, syzbot will still trigger shift-out-of
bounds warning. So better check the bits directly.

Re: [PATCH v2] bcachefs: Fix shift-out-of-bounds in bch2_stripe_to_text
Posted by Hongbo Li 1 month, 2 weeks ago

On 2024/10/9 17:16, Zhao Mengmeng wrote:
> On 2024/10/9 17:10, Hongbo Li wrote:
>>
>>
>> On 2024/10/9 13:43, Zhao Mengmeng wrote:
>>> From: Zhao Mengmeng <zhaomengmeng@kylinos.cn>
>>>
>>> syzbot report a shift-out-of-bounds issue:
>>> ------------[ cut here ]------------
>>> UBSAN: shift-out-of-bounds in fs/bcachefs/ec.c:147:2
>>> shift exponent 108 is too large for 32-bit type 'unsigned int'
>>> ----
>>> Here s.csum_granularity_bits = 108, so shift is impossible for unsigned
>>> int. To fix, add a check in bch2_stripe_validate() to bail out, it has
>>> same checking logic with ec_stripe_key_init().
>>>
>>> Reported-by: syzbot+f8c98a50c323635be65d@syzkaller.appspotmail.com
>>> Tested-by: syzbot+f8c98a50c323635be65d@syzkaller.appspotmail.com
>>> Closes: https://syzkaller.appspot.com/bug?extid=f8c98a50c323635be65d
>>> Suggested-by: Hongbo Li <lihongbo22@huawei.com>
>>> Signed-off-by: Zhao Mengmeng <zhaomengmeng@kylinos.cn>
>>> ---
>>>    fs/bcachefs/ec.c      | 6 ++++++
>>>    fs/bcachefs/errcode.h | 3 ++-
>>>    2 files changed, 8 insertions(+), 1 deletion(-)
>>>
>>> diff --git a/fs/bcachefs/ec.c b/fs/bcachefs/ec.c
>>> index 141a4c63142f..bc5ff1331c6f 100644
>>> --- a/fs/bcachefs/ec.c
>>> +++ b/fs/bcachefs/ec.c
>>> @@ -113,6 +113,12 @@ int bch2_stripe_validate(struct bch_fs *c, struct bkey_s_c k,
>>>        const struct bch_stripe *s = bkey_s_c_to_stripe(k).v;
>>>        int ret = 0;
>>>    +    if (s->csum_granularity_bits >= ilog2(le16_to_cpu(s->sectors))) {
>>
>> csum_granularity_bits should do the left shift.
> 
> If use `1 << s->csum_granularity_bits(108)` to do the check, syzbot will still trigger shift-out-of
> bounds warning. So better check the bits directly.
> 
You are right! :)

Thanks
Hongbo
Re: [PATCH v2] bcachefs: Fix shift-out-of-bounds in bch2_stripe_to_text
Posted by Hongbo Li 1 month, 2 weeks ago

On 2024/10/9 17:10, Hongbo Li wrote:
> 
> 
> On 2024/10/9 13:43, Zhao Mengmeng wrote:
>> From: Zhao Mengmeng <zhaomengmeng@kylinos.cn>
>>
>> syzbot report a shift-out-of-bounds issue:
>> ------------[ cut here ]------------
>> UBSAN: shift-out-of-bounds in fs/bcachefs/ec.c:147:2
>> shift exponent 108 is too large for 32-bit type 'unsigned int'
>> ----
>> Here s.csum_granularity_bits = 108, so shift is impossible for unsigned
>> int. To fix, add a check in bch2_stripe_validate() to bail out, it has
>> same checking logic with ec_stripe_key_init().
>>
>> Reported-by: syzbot+f8c98a50c323635be65d@syzkaller.appspotmail.com
>> Tested-by: syzbot+f8c98a50c323635be65d@syzkaller.appspotmail.com
>> Closes: https://syzkaller.appspot.com/bug?extid=f8c98a50c323635be65d
>> Suggested-by: Hongbo Li <lihongbo22@huawei.com>
>> Signed-off-by: Zhao Mengmeng <zhaomengmeng@kylinos.cn>
>> ---
>>   fs/bcachefs/ec.c      | 6 ++++++
>>   fs/bcachefs/errcode.h | 3 ++-
>>   2 files changed, 8 insertions(+), 1 deletion(-)
>>
>> diff --git a/fs/bcachefs/ec.c b/fs/bcachefs/ec.c
>> index 141a4c63142f..bc5ff1331c6f 100644
>> --- a/fs/bcachefs/ec.c
>> +++ b/fs/bcachefs/ec.c
>> @@ -113,6 +113,12 @@ int bch2_stripe_validate(struct bch_fs *c, struct 
>> bkey_s_c k,
>>       const struct bch_stripe *s = bkey_s_c_to_stripe(k).v;
>>       int ret = 0;
>> +    if (s->csum_granularity_bits >= ilog2(le16_to_cpu(s->sectors))) {
> 
> csum_granularity_bits should do the left shift.
oh no, I miss your ilog2 helper.
> 
>> +        bch_err_ratelimited(c, "stripe csum gran bits %u too big",
>> +                    s->csum_granularity_bits);
>> +        return -BCH_ERR_stripe_csum_granularity_bits_too_big;
>> +    }
>> +
>>       bkey_fsck_err_on(bkey_eq(k.k->p, POS_MIN) ||
>>                bpos_gt(k.k->p, POS(0, U32_MAX)),
>>                c, stripe_pos_bad,
>> diff --git a/fs/bcachefs/errcode.h b/fs/bcachefs/errcode.h
>> index 742dcdd3e5d7..14ba6bc7a029 100644
>> --- a/fs/bcachefs/errcode.h
>> +++ b/fs/bcachefs/errcode.h
>> @@ -258,7 +258,8 @@
>>       x(BCH_ERR_nopromote,        nopromote_no_writes)            \
>>       x(BCH_ERR_nopromote,        nopromote_enomem)            \
>>       x(0,                invalid_snapshot_node)            \
>> -    x(0,                option_needs_open_fs)
>> +    x(0,                option_needs_open_fs)            \
> There are some changes, this should rebase on the latest tree.
> 
> Thanks,
> Hongbo
>> +    x(EINVAL,            stripe_csum_granularity_bits_too_big)
>>   enum bch_errcode {
>>       BCH_ERR_START        = 2048,