Skip to contentSkip to conversation

bcachefs: Fix invalid shift in validate_sb_layout()

PATCH In review Gianfranco Trad 2 participants 5 messages Oct 23, 2024 – Oct 24, 2024

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 bcachefs: Fix invalid shift in validate_sb_layout()

Add check on layout->sb_max_size_bits against BCH_SB_LAYOUT_SIZE_BITS_MAX to prevent UBSAN shift-out-of-bounds in validate_sb_layout().

Reported-by [email protected]
Fixes 03ef80b469d5 ("bcachefs: Ignore unknown mount options")
---
 fs/bcachefs/errcode.h  | 1 +
 fs/bcachefs/super-io.c | 5 +++++
 2 files changed, 6 insertions(+)
fs/bcachefs/errcode.h +1 -0
index 649263516ab1..b6cbd716000b 100644
@@ -222,6 +222,7 @@
 	x(BCH_ERR_invalid_sb_layout,	invalid_sb_layout_type)			\
 	x(BCH_ERR_invalid_sb_layout,	invalid_sb_layout_nr_superblocks)	\
 	x(BCH_ERR_invalid_sb_layout,	invalid_sb_layout_superblocks_overlap)	\
+	x(BCH_ERR_invalid_sb_layout,    invalid_sb_layout_sb_max_size_bits)     \
 	x(BCH_ERR_invalid_sb,		invalid_sb_members_missing)		\
 	x(BCH_ERR_invalid_sb,		invalid_sb_members)			\
 	x(BCH_ERR_invalid_sb,		invalid_sb_disk_groups)			\
fs/bcachefs/super-io.c +5 -0
index ce7410d72089..44d0ac9b00dd 100644
@@ -287,6 +287,11 @@ static int validate_sb_layout(struct bch_sb_layout *layout, struct printbuf *out
 		return -BCH_ERR_invalid_sb_layout_nr_superblocks;
 	}
 
+	if (layout->sb_max_size_bits > BCH_SB_LAYOUT_SIZE_BITS_MAX) {
+		prt_printf(out, "Invalid superblock layout: max_size_bits too high");
+		return -BCH_ERR_invalid_sb_layout_sb_max_size_bits;
+	}
+
 	max_sectors = 1 << layout->sb_max_size_bits;
 
 	prev_offset = le64_to_cpu(layout->sb_offset[0]);
Signature · 1 line
2.43.0
#1 lore raw
Quoted 31 lines · Gianfranco Trad

Add check on layout->sb_max_size_bits against BCH_SB_LAYOUT_SIZE_BITS_MAX to prevent UBSAN shift-out-of-bounds in validate_sb_layout().

Reported-by [email protected]
Fixes 03ef80b469d5 ("bcachefs: Ignore unknown mount options")
---
  fs/bcachefs/errcode.h  | 1 +
  fs/bcachefs/super-io.c | 5 +++++
  2 files changed, 6 insertions(+)
fs/bcachefs/errcode.h +1 -0
index 649263516ab1..b6cbd716000b 100644
@@ -222,6 +222,7 @@
  	x(BCH_ERR_invalid_sb_layout,	invalid_sb_layout_type)			\
  	x(BCH_ERR_invalid_sb_layout,	invalid_sb_layout_nr_superblocks)	\
  	x(BCH_ERR_invalid_sb_layout,	invalid_sb_layout_superblocks_overlap)	\
+	x(BCH_ERR_invalid_sb_layout,    invalid_sb_layout_sb_max_size_bits)     \
  	x(BCH_ERR_invalid_sb,		invalid_sb_members_missing)		\
  	x(BCH_ERR_invalid_sb,		invalid_sb_members)			\
  	x(BCH_ERR_invalid_sb,		invalid_sb_disk_groups)			\
fs/bcachefs/super-io.c +0 -0
index ce7410d72089..44d0ac9b00dd 100644
@@ -287,6 +287,11 @@ static int validate_sb_layout(struct bch_sb_layout *layout, struct printbuf *out
  		return -BCH_ERR_invalid_sb_layout_nr_superblocks;

fs/bcachefs/super-io.c static int validate_sb_layout(struct bch_sb_layout *layout, struct printbuf *out · line 297

Quoted 10 lines
fs/bcachefs/super-io.c +5 -0
  	}
 
+	if (layout->sb_max_size_bits > BCH_SB_LAYOUT_SIZE_BITS_MAX) {
+		prt_printf(out, "Invalid superblock layout: max_size_bits too high");
+		return -BCH_ERR_invalid_sb_layout_sb_max_size_bits;
+	}
+
  	max_sectors = 1 << layout->sb_max_size_bits;
 
  	prev_offset = le64_to_cpu(layout->sb_offset[0]);

Wondering if this other patch might be considered more correct to prevent shift oob, given also [0]:

fs/bcachefs/super-io.c +1 -1
index ce7410d72089..428172897501 100644
@@ -287,7 +287,7 @@ static int validate_sb_layout(struct bch_sb_layout *layout, struct printbuf *out
 		return -BCH_ERR_invalid_sb_layout_nr_superblocks;
 	}
 
-	max_sectors = 1 << layout->sb_max_size_bits;
+	max_sectors = 1 << min(BCH_SB_LAYOUT_SIZE_BITS_MAX, layout->sb_max_size_bits);
 
 	prev_offset = le64_to_cpu(layout->sb_offset[0]);
 

Also this patch was already tested by syzbot [1]

[0] 71dac2482ad3c8d4a8b8998a96751f009bad895f ("bcachefs: BCH_SB_LAYOUT_SIZE_BITS_MAX")
[1] https://syzkaller.appspot.com/x/log.txt?x=1640b640580000

Thanks for your time,

-- Gian

#4 lore raw
In reply to Gianfranco Trad
Quoted 8 lines · Gianfranco Trad

Add check on layout->sb_max_size_bits against BCH_SB_LAYOUT_SIZE_BITS_MAX to prevent UBSAN shift-out-of-bounds in validate_sb_layout().

Reported-by [email protected]
Fixes 03ef80b469d5 ("bcachefs: Ignore unknown mount options")

looks correct, applied

Quoted 36 lines
---
 fs/bcachefs/errcode.h  | 1 +
 fs/bcachefs/super-io.c | 5 +++++
 2 files changed, 6 insertions(+)
fs/bcachefs/errcode.h +1 -0
index 649263516ab1..b6cbd716000b 100644
@@ -222,6 +222,7 @@
 	x(BCH_ERR_invalid_sb_layout,	invalid_sb_layout_type)			\
 	x(BCH_ERR_invalid_sb_layout,	invalid_sb_layout_nr_superblocks)	\
 	x(BCH_ERR_invalid_sb_layout,	invalid_sb_layout_superblocks_overlap)	\
+	x(BCH_ERR_invalid_sb_layout,    invalid_sb_layout_sb_max_size_bits)     \
 	x(BCH_ERR_invalid_sb,		invalid_sb_members_missing)		\
 	x(BCH_ERR_invalid_sb,		invalid_sb_members)			\
 	x(BCH_ERR_invalid_sb,		invalid_sb_disk_groups)			\
fs/bcachefs/super-io.c +5 -0
index ce7410d72089..44d0ac9b00dd 100644
@@ -287,6 +287,11 @@ static int validate_sb_layout(struct bch_sb_layout *layout, struct printbuf *out
 		return -BCH_ERR_invalid_sb_layout_nr_superblocks;
 	}
 
+	if (layout->sb_max_size_bits > BCH_SB_LAYOUT_SIZE_BITS_MAX) {
+		prt_printf(out, "Invalid superblock layout: max_size_bits too high");
+		return -BCH_ERR_invalid_sb_layout_sb_max_size_bits;
+	}
+
 	max_sectors = 1 << layout->sb_max_size_bits;
 
 	prev_offset = le64_to_cpu(layout->sb_offset[0]);
Signature · 1 line
2.43.0
#2 lore raw
In reply to Gianfranco Trad
Quoted 35 lines · Gianfranco Trad
Quoted 34 lines · Gianfranco Trad

Add check on layout->sb_max_size_bits against BCH_SB_LAYOUT_SIZE_BITS_MAX to prevent UBSAN shift-out-of-bounds in validate_sb_layout().

Reported-by [email protected]
Fixes 03ef80b469d5 ("bcachefs: Ignore unknown mount options")
---
  fs/bcachefs/errcode.h  | 1 +
  fs/bcachefs/super-io.c | 5 +++++
  2 files changed, 6 insertions(+)
fs/bcachefs/errcode.h +1 -0
index 649263516ab1..b6cbd716000b 100644
@@ -222,6 +222,7 @@
  	x(BCH_ERR_invalid_sb_layout,	invalid_sb_layout_type)			\
  	x(BCH_ERR_invalid_sb_layout,	invalid_sb_layout_nr_superblocks)	\
  	x(BCH_ERR_invalid_sb_layout,	invalid_sb_layout_superblocks_overlap)	\
+	x(BCH_ERR_invalid_sb_layout,    invalid_sb_layout_sb_max_size_bits)     \
  	x(BCH_ERR_invalid_sb,		invalid_sb_members_missing)		\
  	x(BCH_ERR_invalid_sb,		invalid_sb_members)			\
  	x(BCH_ERR_invalid_sb,		invalid_sb_disk_groups)			\
fs/bcachefs/super-io.c +2 -0
index ce7410d72089..44d0ac9b00dd 100644
@@ -287,6 +287,11 @@ static int validate_sb_layout(struct bch_sb_layout *layout, struct printbuf *out
  		return -BCH_ERR_invalid_sb_layout_nr_superblocks;
  	}
+	if (layout->sb_max_size_bits > BCH_SB_LAYOUT_SIZE_BITS_MAX) {
+		prt_printf(out, "Invalid superblock layout: max_size_bits too high");
Quoted 8 lines

fs/bcachefs/super-io.c static int validate_sb_layout(struct bch_sb_layout *layout, struct printbuf *out · line 295

Quoted 5 lines
fs/bcachefs/super-io.c +3 -0
+		return -BCH_ERR_invalid_sb_layout_sb_max_size_bits;
+	}
+
  	max_sectors = 1 << layout->sb_max_size_bits;
  	prev_offset = le64_to_cpu(layout->sb_offset[0]);

Wondering if this other patch might be considered more correct to prevent shift oob, given also [0]:

Your first patch looks better, we want to know if we're feeding it garbage

Quoted 30 lines
fs/bcachefs/super-io.c +1 -1
index ce7410d72089..428172897501 100644
@@ -287,7 +287,7 @@ static int validate_sb_layout(struct bch_sb_layout
 *layout, struct printbuf *out
 		return -BCH_ERR_invalid_sb_layout_nr_superblocks;
 	}
 
-	max_sectors = 1 << layout->sb_max_size_bits;
+	max_sectors = 1 << min(BCH_SB_LAYOUT_SIZE_BITS_MAX,
 layout->sb_max_size_bits);
 

prev_offset = le64_to_cpu(layout->sb_offset[0]);

Also this patch was already tested by syzbot [1]

[0] 71dac2482ad3c8d4a8b8998a96751f009bad895f ("bcachefs:
BCH_SB_LAYOUT_SIZE_BITS_MAX")
[1] https://syzkaller.appspot.com/x/log.txt?x=1640b640580000

Thanks for your time,

-- Gian

#3 lore raw
Quoted 45 lines · Kent Overstreet
Quoted 35 lines · Gianfranco Trad
Quoted 34 lines · Gianfranco Trad

Add check on layout->sb_max_size_bits against BCH_SB_LAYOUT_SIZE_BITS_MAX to prevent UBSAN shift-out-of-bounds in validate_sb_layout().

Reported-by [email protected]
Fixes 03ef80b469d5 ("bcachefs: Ignore unknown mount options")
---
   fs/bcachefs/errcode.h  | 1 +
   fs/bcachefs/super-io.c | 5 +++++
   2 files changed, 6 insertions(+)
fs/bcachefs/errcode.h +1 -0
index 649263516ab1..b6cbd716000b 100644
@@ -222,6 +222,7 @@
   	x(BCH_ERR_invalid_sb_layout,	invalid_sb_layout_type)			\
   	x(BCH_ERR_invalid_sb_layout,	invalid_sb_layout_nr_superblocks)	\
   	x(BCH_ERR_invalid_sb_layout,	invalid_sb_layout_superblocks_overlap)	\
+	x(BCH_ERR_invalid_sb_layout,    invalid_sb_layout_sb_max_size_bits)     \
   	x(BCH_ERR_invalid_sb,		invalid_sb_members_missing)		\
   	x(BCH_ERR_invalid_sb,		invalid_sb_members)			\
   	x(BCH_ERR_invalid_sb,		invalid_sb_disk_groups)			\
fs/bcachefs/super-io.c +2 -0
index ce7410d72089..44d0ac9b00dd 100644
@@ -287,6 +287,11 @@ static int validate_sb_layout(struct bch_sb_layout *layout, struct printbuf *out
   		return -BCH_ERR_invalid_sb_layout_nr_superblocks;
   	}
+	if (layout->sb_max_size_bits > BCH_SB_LAYOUT_SIZE_BITS_MAX) {
+		prt_printf(out, "Invalid superblock layout: max_size_bits too high");
Quoted 8 lines

fs/bcachefs/super-io.c static int validate_sb_layout(struct bch_sb_layout *layout, struct printbuf *out · line 295

Quoted 5 lines
fs/bcachefs/super-io.c +3 -0
+		return -BCH_ERR_invalid_sb_layout_sb_max_size_bits;
+	}
+
   	max_sectors = 1 << layout->sb_max_size_bits;
   	prev_offset = le64_to_cpu(layout->sb_offset[0]);

Wondering if this other patch might be considered more correct to prevent shift oob, given also [0]:

Quoted 3 lines

Your first patch looks better, we want to know if we're feeding it garbage

Understood. Thanks Kent.
Have a nice day,
--Gian

Quoted 26 lines
Quoted 26 lines
fs/bcachefs/super-io.c +1 -1
index ce7410d72089..428172897501 100644
@@ -287,7 +287,7 @@ static int validate_sb_layout(struct bch_sb_layout
 *layout, struct printbuf *out
  		return -BCH_ERR_invalid_sb_layout_nr_superblocks;
  	}
 
-	max_sectors = 1 << layout->sb_max_size_bits;
+	max_sectors = 1 << min(BCH_SB_LAYOUT_SIZE_BITS_MAX,
 layout->sb_max_size_bits);
 
  	prev_offset = le64_to_cpu(layout->sb_offset[0]);

Also this patch was already tested by syzbot [1]

[0] 71dac2482ad3c8d4a8b8998a96751f009bad895f ("bcachefs:
BCH_SB_LAYOUT_SIZE_BITS_MAX")
[1] https://syzkaller.appspot.com/x/log.txt?x=1640b640580000

Thanks for your time,

-- Gian