Skip to contentSkip to conversation

bcachefs: fix shift oob in alloc_lru_idx_fragmentation

PATCH Superseded Jeongjun Park 2 participants 3 messages Oct 21, 2024

v1 of 3 Superseded Latest is v3 →

lore
Share & tools
lore
#0 lore raw

PATCH 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 +++
 fs/bcachefs/disk_accounting_format.h | 2 ++
 2 files changed, 5 insertions(+)
fs/bcachefs/alloc_background.h +3 -0
index f8e87c6721b1..66a334e2edcd 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_TYPE_MAX)
+		return 0;
+	
 	if (!data_type_movable(a.data_type) ||
 	    !bch2_bucket_sectors_fragmented(ca, a))
 		return 0;
fs/bcachefs/disk_accounting_format.h +2 -0
index 7b6e6c97e6aa..0232bc9f590d 100644
@@ -72,6 +72,8 @@ enum bch_data_type {
 	BCH_DATA_NR
 };
 
+#define BCH_DATA_TYPE_MAX 10
+
 static inline bool data_type_is_empty(enum bch_data_type type)
 {
 	switch (type) {

--

#1 lore raw
Quoted 35 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 +++
fs/bcachefs/disk_accounting_format.h | 2 ++

2 files changed, 5 insertions(+)
fs/bcachefs/alloc_background.h +3 -0
index f8e87c6721b1..66a334e2edcd 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_TYPE_MAX)
+ return 0;
+ 

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

fs/bcachefs/disk_accounting_format.h +0 -0
index 7b6e6c97e6aa..0232bc9f590d 100644
@@ -72,6 +72,8 @@ enum bch_data_type {

BCH_DATA_NR
};

Quoted 1 line

+#define BCH_DATA_TYPE_MAX 10

Use BCH_DATA_NR instead.

Quoted 6 lines

+
static inline bool data_type_is_empty(enum bch_data_type type) {
switch (type) {
--

#2 lore raw
Quoted 37 lines · Alan Huang
Quoted 35 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 +++
fs/bcachefs/disk_accounting_format.h | 2 ++

2 files changed, 5 insertions(+)
fs/bcachefs/alloc_background.h +3 -0
index f8e87c6721b1..66a334e2edcd 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_TYPE_MAX)
+ return 0;
+

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

fs/bcachefs/disk_accounting_format.h +0 -0
index 7b6e6c97e6aa..0232bc9f590d 100644
@@ -72,6 +72,8 @@ enum bch_data_type {

BCH_DATA_NR
};

Quoted 3 lines
Quoted 1 line

+#define BCH_DATA_TYPE_MAX 10

Use BCH_DATA_NR instead.

Thanks for letting us know. It's a simple change, so we'll send out a v2 patch right away.

Regards,

Jeongjun Park

Quoted 8 lines
Quoted 7 lines

+
static inline bool data_type_is_empty(enum bch_data_type type) {
switch (type) {
--