mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
* [PATCH v3] bcachefs: Fix shift-out-of-bounds in bch2_stripe_to_text
@ 2024-10-09 14:07 Zhao Mengmeng
  2024-10-10  1:39 ` Hongbo Li
  2024-10-10  2:03 ` Kent Overstreet
  0 siblings, 2 replies; 4+ messages in thread
From: Zhao Mengmeng @ 2024-10-09 14:07 UTC (permalink / raw)
  To: kent.overstreet, zhaomengmeng, lihongbo22; +Cc: linux-bcachefs, linux-kernel

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 | 4 +++-
 2 files changed, 9 insertions(+), 1 deletion(-)

diff --git a/fs/bcachefs/ec.c b/fs/bcachefs/ec.c
index 564841e5a24b..f6a02123144d 100644
--- a/fs/bcachefs/ec.c
+++ b/fs/bcachefs/ec.c
@@ -114,6 +114,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 26990ad584d5..83659cdb93c8 100644
--- a/fs/bcachefs/errcode.h
+++ b/fs/bcachefs/errcode.h
@@ -270,7 +270,9 @@
 	x(BCH_ERR_nopromote,		nopromote_enomem)			\
 	x(0,				invalid_snapshot_node)			\
 	x(0,				option_needs_open_fs)			\
-	x(0,				remove_disk_accounting_entry)
+	x(0,				remove_disk_accounting_entry)		\
+	x(EINVAL,			stripe_csum_granularity_bits_too_big)
+
 
 enum bch_errcode {
 	BCH_ERR_START		= 2048,
-- 
2.43.0


^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: [PATCH v3] bcachefs: Fix shift-out-of-bounds in bch2_stripe_to_text
  2024-10-09 14:07 [PATCH v3] bcachefs: Fix shift-out-of-bounds in bch2_stripe_to_text Zhao Mengmeng
@ 2024-10-10  1:39 ` Hongbo Li
  2024-10-10  2:03 ` Kent Overstreet
  1 sibling, 0 replies; 4+ messages in thread
From: Hongbo Li @ 2024-10-10  1:39 UTC (permalink / raw)
  To: Zhao Mengmeng, kent.overstreet, zhaomengmeng; +Cc: linux-bcachefs, linux-kernel



On 2024/10/9 22:07, 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>

Reviewed-by: Hongbo Li <lihongbo22@huawei.com>

Thanks,
Hongbo

> ---
>   fs/bcachefs/ec.c      | 6 ++++++
>   fs/bcachefs/errcode.h | 4 +++-
>   2 files changed, 9 insertions(+), 1 deletion(-)
> 
> diff --git a/fs/bcachefs/ec.c b/fs/bcachefs/ec.c
> index 564841e5a24b..f6a02123144d 100644
> --- a/fs/bcachefs/ec.c
> +++ b/fs/bcachefs/ec.c
> @@ -114,6 +114,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 26990ad584d5..83659cdb93c8 100644
> --- a/fs/bcachefs/errcode.h
> +++ b/fs/bcachefs/errcode.h
> @@ -270,7 +270,9 @@
>   	x(BCH_ERR_nopromote,		nopromote_enomem)			\
>   	x(0,				invalid_snapshot_node)			\
>   	x(0,				option_needs_open_fs)			\
> -	x(0,				remove_disk_accounting_entry)
> +	x(0,				remove_disk_accounting_entry)		\
> +	x(EINVAL,			stripe_csum_granularity_bits_too_big)
> +
>   
>   enum bch_errcode {
>   	BCH_ERR_START		= 2048,

^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: [PATCH v3] bcachefs: Fix shift-out-of-bounds in bch2_stripe_to_text
  2024-10-09 14:07 [PATCH v3] bcachefs: Fix shift-out-of-bounds in bch2_stripe_to_text Zhao Mengmeng
  2024-10-10  1:39 ` Hongbo Li
@ 2024-10-10  2:03 ` Kent Overstreet
  2024-10-10  3:30   ` Zhao Mengmeng
  1 sibling, 1 reply; 4+ messages in thread
From: Kent Overstreet @ 2024-10-10  2:03 UTC (permalink / raw)
  To: Zhao Mengmeng; +Cc: zhaomengmeng, lihongbo22, linux-bcachefs, linux-kernel

On Wed, Oct 09, 2024 at 10:07:55PM GMT, 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 | 4 +++-
>  2 files changed, 9 insertions(+), 1 deletion(-)
> 
> diff --git a/fs/bcachefs/ec.c b/fs/bcachefs/ec.c
> index 564841e5a24b..f6a02123144d 100644
> --- a/fs/bcachefs/ec.c
> +++ b/fs/bcachefs/ec.c
> @@ -114,6 +114,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;
> +	}
> +

that should be a bkey_fsck_err_on(), and you'll add to the enum in
sb-errors_format.h

>  	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 26990ad584d5..83659cdb93c8 100644
> --- a/fs/bcachefs/errcode.h
> +++ b/fs/bcachefs/errcode.h
> @@ -270,7 +270,9 @@
>  	x(BCH_ERR_nopromote,		nopromote_enomem)			\
>  	x(0,				invalid_snapshot_node)			\
>  	x(0,				option_needs_open_fs)			\
> -	x(0,				remove_disk_accounting_entry)
> +	x(0,				remove_disk_accounting_entry)		\
> +	x(EINVAL,			stripe_csum_granularity_bits_too_big)
> +
>  
>  enum bch_errcode {
>  	BCH_ERR_START		= 2048,
> -- 
> 2.43.0
> 

^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: [PATCH v3] bcachefs: Fix shift-out-of-bounds in bch2_stripe_to_text
  2024-10-10  2:03 ` Kent Overstreet
@ 2024-10-10  3:30   ` Zhao Mengmeng
  0 siblings, 0 replies; 4+ messages in thread
From: Zhao Mengmeng @ 2024-10-10  3:30 UTC (permalink / raw)
  To: Kent Overstreet; +Cc: zhaomengmeng, lihongbo22, linux-bcachefs, linux-kernel

On 2024/10/10 10:03, Kent Overstreet wrote:
> On Wed, Oct 09, 2024 at 10:07:55PM GMT, 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 | 4 +++-
>>  2 files changed, 9 insertions(+), 1 deletion(-)
>>
>> diff --git a/fs/bcachefs/ec.c b/fs/bcachefs/ec.c
>> index 564841e5a24b..f6a02123144d 100644
>> --- a/fs/bcachefs/ec.c
>> +++ b/fs/bcachefs/ec.c
>> @@ -114,6 +114,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;
>> +	}
>> +
> 
> that should be a bkey_fsck_err_on(), and you'll add to the enum in
> sb-errors_format.h

Hi Kent, I try to use bkey_fsck_err_on() at the first time, but it still triggered
syzbot warning, cause bch2_stripe_to_text() is called before __bch2_fsck_err(), before
it throws new errro, it will trigger the shift out of bounds check.

Cheers!



^ permalink raw reply	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2024-10-10  3:31 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2024-10-09 14:07 [PATCH v3] bcachefs: Fix shift-out-of-bounds in bch2_stripe_to_text Zhao Mengmeng
2024-10-10  1:39 ` Hongbo Li
2024-10-10  2:03 ` Kent Overstreet
2024-10-10  3:30   ` Zhao Mengmeng

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox

all inboxes | Powered by JetHome®