Skip to contentSkip to conversation

bcachefs: fix shift oob in alloc_lru_idx_fragmentation

PATCH Applied Jeongjun Park 4 participants 9 messages Oct 21, 2024 – Oct 24, 2024

lore
Share & tools
lore
#0 lore raw

PATCH v3 1/1 bcachefs: fix shift oob in alloc_lru_idx_fragmentation

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 [email protected]
Fixes 260af1562ec1 ("bcachefs: Kill alloc_v4.fragmentation_lru")
---
 fs/bcachefs/alloc_background.h | 3 +++
 1 file changed, 3 insertions(+)
fs/bcachefs/alloc_background.h +3 -0
index f8e87c6721b1..163a67b97a40 100644
@@ -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;

--

#1 lore raw
Quoted 16 lines · Jeongjun Park

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 [email protected]
Fixes 260af1562ec1 ("bcachefs: Kill alloc_v4.fragmentation_lru")
---
  fs/bcachefs/alloc_background.h | 3 +++
  1 file changed, 3 insertions(+)
fs/bcachefs/alloc_background.h +0 -0
index f8e87c6721b1..163a67b97a40 100644

fs/bcachefs/alloc_background.h static inline bool data_type_movable(enum bch_data_type type) · line 173

Quoted 6 lines
fs/bcachefs/alloc_background.h +3 -0
@@ -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;
+

oh, I found I have done the same thing in [1]("Re: [syzbot] [bcachefs?] UBSAN: shift-out-of-bounds in bch2_alloc_to_text"). But in my humble opinion, the validation changes also should be added. And in addition, move the condition about a.data_type into data_type_movable will be better. Just my personal opinion.:)

[1] https://www.spinics.net/lists/kernel/msg5412619.html

Thanks,
Hongbo

Quoted 5 lines
fs/bcachefs/alloc_background.h +0 -1
  	if (!data_type_movable(a.data_type) ||
  	    !bch2_bucket_sectors_fragmented(ca, a))
  		return 0;
--
#2 lore raw
Quoted 18 lines · Hongbo Li
Quoted 14 lines · Jeongjun Park

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 [email protected]
Fixes 260af1562ec1 ("bcachefs: Kill alloc_v4.fragmentation_lru")
---
 fs/bcachefs/alloc_background.h | 3 +++
 1 file changed, 3 insertions(+)
fs/bcachefs/alloc_background.h +0 -0
index f8e87c6721b1..163a67b97a40 100644
@@ -168,6 +168,9 @@ static inline bool data_type_movable(enum bch_data_type type)
Quoted 8 lines

fs/bcachefs/alloc_background.h static inline bool data_type_movable(enum bch_data_type type) · line 173

Quoted 6 lines
fs/bcachefs/alloc_background.h +3 -0
 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;
+

oh, I found I have done the same thing in [1]("Re: [syzbot] [bcachefs?] UBSAN: shift-out-of-bounds in bch2_alloc_to_text"). But

Your patch there is still triggering the issue.

Quoted 1 line

in my humble opinion, the validation changes also should be added. And in addition, move the condition about a.data_type into

There is already the validation:

bkey_fsck_err_on(alloc_data_type(a, a.data_type) != a.data_type

And the unknown data type is already printed in bch2_prt_data_type, additional validation doesn’t help much.

Quoted 1 line

data_type_movable will be better. Just my personal opinion.:)

In my personal opinion, I don’t think so :)

Quoted 10 lines

[1] https://www.spinics.net/lists/kernel/msg5412619.html

Thanks,
Hongbo

Quoted 4 lines
fs/bcachefs/alloc_background.h +0 -1
  if (!data_type_movable(a.data_type) ||
      !bch2_bucket_sectors_fragmented(ca, a))
  return 0;
--
#3 lore raw
Quoted 28 lines · Alan Huang
Quoted 18 lines · Hongbo Li
Quoted 14 lines · Jeongjun Park

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 [email protected]
Fixes 260af1562ec1 ("bcachefs: Kill alloc_v4.fragmentation_lru")
---
  fs/bcachefs/alloc_background.h | 3 +++
  1 file changed, 3 insertions(+)
fs/bcachefs/alloc_background.h +0 -0
index f8e87c6721b1..163a67b97a40 100644
@@ -168,6 +168,9 @@ static inline bool data_type_movable(enum bch_data_type type)
Quoted 8 lines

fs/bcachefs/alloc_background.h static inline bool data_type_movable(enum bch_data_type type) · line 173

Quoted 6 lines
fs/bcachefs/alloc_background.h +3 -0
  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;
+

oh, I found I have done the same thing in [1]("Re: [syzbot] [bcachefs?] UBSAN: shift-out-of-bounds in bch2_alloc_to_text"). But

Quoted 1 line

Your patch there is still triggering the issue.

Yeah, it just notify the issue and not prevent the issue. So I found it should add a.data_type condition indeed. :)

Quoted 6 lines
Quoted 2 lines

in my humble opinion, the validation changes also should be added. And in addition, move the condition about a.data_type into

There is already the validation:

bkey_fsck_err_on(alloc_data_type(a, a.data_type) != a.data_type

This is actually not enough. This only do some transition check. For example, if a.data_type break when bch2_bucket_sectors_dirty (the data corruption can lead to various situations occurring) is true, the helper does noting.

Thanks,
Hongbo

Quoted 19 lines

And the unknown data type is already printed in bch2_prt_data_type, additional validation doesn’t help much.

Quoted 1 line

data_type_movable will be better. Just my personal opinion.:)

In my personal opinion, I don’t think so :)

Quoted 10 lines

[1] https://www.spinics.net/lists/kernel/msg5412619.html

Thanks,
Hongbo

Quoted 4 lines
fs/bcachefs/alloc_background.h +0 -1
   if (!data_type_movable(a.data_type) ||
       !bch2_bucket_sectors_fragmented(ca, a))
   return 0;
--
#4 lore raw
Quoted 31 lines · Hongbo Li
Quoted 27 lines · Alan Huang
Quoted 26 lines · Hongbo Li
Quoted 14 lines · Jeongjun Park

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 [email protected]
Fixes 260af1562ec1 ("bcachefs: Kill alloc_v4.fragmentation_lru")
---
 fs/bcachefs/alloc_background.h | 3 +++
 1 file changed, 3 insertions(+)
fs/bcachefs/alloc_background.h +0 -0
index f8e87c6721b1..163a67b97a40 100644

fs/bcachefs/alloc_background.h static inline bool data_type_movable(enum bch_data_type type) · line 173

Quoted 6 lines
fs/bcachefs/alloc_background.h +3 -0
@@ -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;
+

oh, I found I have done the same thing in [1]("Re: [syzbot] [bcachefs?] UBSAN: shift-out-of-bounds in bch2_alloc_to_text"). But

Quoted 7 lines
Quoted 1 line

Your patch there is still triggering the issue.

Yeah, it just notify the issue and not prevent the issue. So I found it should add a.data_type condition indeed. :)

Quoted 3 lines
Quoted 1 line

in my humble opinion, the validation changes also should be added. And in addition, move the condition about a.data_type into

There is already the validation:
bkey_fsck_err_on(alloc_data_type(a, a.data_type) != a.data_type

This is actually not enough. This only do some transition check. For example, if a.data_type break when bch2_bucket_sectors_dirty (the data corruption can lead to various situations occurring) is true, the helper does noting.

Make sense.

Quoted 17 lines

Thanks,
Hongbo

Quoted 13 lines

And the unknown data type is already printed in bch2_prt_data_type, additional validation doesn’t help much.

Quoted 1 line

data_type_movable will be better. Just my personal opinion.:)

In my personal opinion, I don’t think so :)

Quoted 10 lines

[1] https://www.spinics.net/lists/kernel/msg5412619.html

Thanks,
Hongbo

Quoted 4 lines
fs/bcachefs/alloc_background.h +0 -1
  if (!data_type_movable(a.data_type) ||
      !bch2_bucket_sectors_fragmented(ca, a))
  return 0;
--
#5 lore raw
In reply to Hongbo Li
Quoted 30 lines

Hongbo Li <[email protected]> wrote:



Quoted 15 lines · Jeongjun Park

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 [email protected]
Fixes 260af1562ec1 ("bcachefs: Kill alloc_v4.fragmentation_lru")
---
 fs/bcachefs/alloc_background.h | 3 +++
 1 file changed, 3 insertions(+)
fs/bcachefs/alloc_background.h +0 -0
index f8e87c6721b1..163a67b97a40 100644

fs/bcachefs/alloc_background.h static inline bool data_type_movable(enum bch_data_type type) · line 173

Quoted 6 lines
fs/bcachefs/alloc_background.h +3 -0
@@ -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;
+

oh, I found I have done the same thing in [1]("Re: [syzbot] [bcachefs?] UBSAN: shift-out-of-bounds in bch2_alloc_to_text"). But in my humble opinion, the validation changes also should be added. And in addition, move the condition about a.data_type into data_type_movable will be better. Just my personal opinion.:)

Quoted 1 line

[1] https://www.spinics.net/lists/kernel/msg5412619.html

I still disagree with the fix to make data_type_movable() do the validation, but I think [1] is definitely a patch that needs to be added.

However, [1] is far from preventing the shift oob vulnerability described in that syzbot report. Therefore, I think [1] should be written as a standalone patch and committed, rather than as a patch for that syzbot report.

Quoted 8 lines

Thanks,
Hongbo

Quoted 4 lines
fs/bcachefs/alloc_background.h +0 -1
     if (!data_type_movable(a.data_type) ||
         !bch2_bucket_sectors_fragmented(ca, a))
         return 0;
--
#6 lore raw
Quoted 30 lines · Jeongjun Park
Quoted 30 lines

Hongbo Li <[email protected]> wrote:



Quoted 15 lines · Jeongjun Park

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 [email protected]
Fixes 260af1562ec1 ("bcachefs: Kill alloc_v4.fragmentation_lru")
---
  fs/bcachefs/alloc_background.h | 3 +++
  1 file changed, 3 insertions(+)
fs/bcachefs/alloc_background.h +0 -0
index f8e87c6721b1..163a67b97a40 100644

fs/bcachefs/alloc_background.h static inline bool data_type_movable(enum bch_data_type type) · line 173

Quoted 6 lines
fs/bcachefs/alloc_background.h +3 -0
@@ -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;
+

oh, I found I have done the same thing in [1]("Re: [syzbot] [bcachefs?] UBSAN: shift-out-of-bounds in bch2_alloc_to_text"). But in my humble opinion, the validation changes also should be added. And in addition, move the condition about a.data_type into data_type_movable will be better. Just my personal opinion.:)

Quoted 8 lines
Quoted 1 line

[1] https://www.spinics.net/lists/kernel/msg5412619.html

I still disagree with the fix to make data_type_movable() do the validation, but I think [1] is definitely a patch that needs to be added.

However, [1] is far from preventing the shift oob vulnerability described in that syzbot report. Therefore, I think [1] should be written as a standalone patch and committed, rather than as a patch for that

I'm fine for this.:)

Thanks,
Hongbo

Quoted 10 lines

syzbot report.

Quoted 8 lines

Thanks,
Hongbo

Quoted 4 lines
fs/bcachefs/alloc_background.h +0 -1
      if (!data_type_movable(a.data_type) ||
          !bch2_bucket_sectors_fragmented(ca, a))
          return 0;
--
#7 lore raw
In reply to Hongbo Li
Quoted 27 lines · Hongbo Li
Quoted 16 lines · Jeongjun Park

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 [email protected]
Fixes 260af1562ec1 ("bcachefs: Kill alloc_v4.fragmentation_lru")
---
  fs/bcachefs/alloc_background.h | 3 +++
  1 file changed, 3 insertions(+)
fs/bcachefs/alloc_background.h +0 -0
index f8e87c6721b1..163a67b97a40 100644

fs/bcachefs/alloc_background.h static inline bool data_type_movable(enum bch_data_type type) · line 173

Quoted 6 lines
fs/bcachefs/alloc_background.h +3 -0
@@ -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;
+

oh, I found I have done the same thing in [1]("Re: [syzbot] [bcachefs?]

Quoted 4 lines
UBSAN: shift-out-of-bounds in bch2_alloc_to_text"). But in my humble

opinion, the validation changes also should be added. And in addition, move the condition about a.data_type into data_type_movable will be better. Just my personal opinion.:)

Unknown data types (and key types, btree IDs, etc.) are allowed for forwards compatibility - they should just be ignored

#8 lore raw
In reply to Jeongjun Park
Quoted 7 lines · Jeongjun Park

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 [email protected]
Fixes 260af1562ec1 ("bcachefs: Kill alloc_v4.fragmentation_lru")

Thanks, applied

Quoted 19 lines
---
 fs/bcachefs/alloc_background.h | 3 +++
 1 file changed, 3 insertions(+)
fs/bcachefs/alloc_background.h +3 -1
index f8e87c6721b1..163a67b97a40 100644
@@ -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;
--