Skip to contentSkip to conversation

ocfs2: fix slab-out-of-bounds in ocfs2_rotate_requires_path_adjustment

PATCH In review Jiasheng Jiang 2 participants 3 messages Jan 16 – Jan 19 OCFS2

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 ocfs2: fix slab-out-of-bounds in ocfs2_rotate_requires_path_adjustment

In ocfs2_rotate_requires_path_adjustment(), the variable 'next_free' is assigned the value of 'left_el->l_next_free_rec'. This value is then used to index the 'l_recs' array as 'next_free - 1'.

If the filesystem is corrupted or in an unexpected state such that 'l_next_free_rec' is 0, this leads to an array index of -1, triggering a slab-out-of-bounds access.

Other functions in alloc.c, such as ocfs2_rotate_leaf() and ocfs2_update_edge_lengths(), effectively guard against this condition. This patch adds a similar check to return 0 immediately if 'next_free' is 0, preventing the out-of-bounds access.

---
 fs/ocfs2/alloc.c | 4 ++++
 1 file changed, 4 insertions(+)
fs/ocfs2/alloc.c +4 -0
index 58bf58b68955..b15c6f751d99 100644
@@ -2327,6 +2327,10 @@ static int ocfs2_rotate_requires_path_adjustment(struct ocfs2_path *left_path,
 
 	left_el = path_leaf_el(left_path);
 	next_free = le16_to_cpu(left_el->l_next_free_rec);
+
+	if (unlikely(next_free == 0))
+		return 0;
+
 	rec = &left_el->l_recs[next_free - 1];
 
 	if (insert_cpos > le32_to_cpu(rec->e_cpos))
Signature · 1 line
2.25.1
reviewer
#1 lore raw
Quoted 4 lines · Jiasheng Jiang

In ocfs2_rotate_requires_path_adjustment(), the variable 'next_free' is assigned the value of 'left_el->l_next_free_rec'. This value is then used to index the 'l_recs' array as 'next_free - 1'.

Quoted 8 lines

If the filesystem is corrupted or in an unexpected state such that 'l_next_free_rec' is 0, this leads to an array index of -1, triggering a slab-out-of-bounds access.

Other functions in alloc.c, such as ocfs2_rotate_leaf() and ocfs2_update_edge_lengths(), effectively guard against this condition. This patch adds a similar check to return 0 immediately if 'next_free' is 0, preventing the out-of-bounds access.

Do you have reproduction steps to support your analysis?

- Heming

Quoted 25 lines
---
 fs/ocfs2/alloc.c | 4 ++++
 1 file changed, 4 insertions(+)
fs/ocfs2/alloc.c +4 -0
index 58bf58b68955..b15c6f751d99 100644
@@ -2327,6 +2327,10 @@ static int ocfs2_rotate_requires_path_adjustment(struct ocfs2_path *left_path,
 
 	left_el = path_leaf_el(left_path);
 	next_free = le16_to_cpu(left_el->l_next_free_rec);
+
+	if (unlikely(next_free == 0))
+		return 0;
+
 	rec = &left_el->l_recs[next_free - 1];
 
 	if (insert_cpos > le32_to_cpu(rec->e_cpos))
Signature · 1 line
2.25.1
#2 lore raw
Quoted 9 lines · Heming Zhao
Quoted 8 lines · Jiasheng Jiang

In ocfs2_rotate_requires_path_adjustment(), the variable 'next_free' is assigned the value of 'left_el->l_next_free_rec'. This value is then used to index the 'l_recs' array as 'next_free - 1'.

If the filesystem is corrupted or in an unexpected state such that 'l_next_free_rec' is 0, this leads to an array index of -1, triggering a slab-out-of-bounds access.

Quoted 8 lines
Quoted 4 lines

Other functions in alloc.c, such as ocfs2_rotate_leaf() and ocfs2_update_edge_lengths(), effectively guard against this condition. This patch adds a similar check to return 0 immediately if 'next_free' is 0, preventing the out-of-bounds access.

Do you have reproduction steps to support your analysis?

- Heming

Regarding the reproduction steps, we detected this issue through static analysis. We do not have a specific script to reproduce this as it requires a corrupted filesystem image.

However, if the filesystem metadata is corrupted (e.g., due to bit rot or software bugs), it is possible that l_next_free_rec reads as 0. In the current implementation, this leads to accessing l_recs with index -1, causing a slab-out-of-bounds access.

This patch is intended as a hardening measure. Instead of crashing the kernel when encountering such invalid on-disk data, it is safer to abort the operation gracefully.

This change also improves consistency within fs/ocfs2/alloc.c. Other functions such as ocfs2_rotate_leaf() and ocfs2_update_edge_lengths() already guard against this specific condition (next_free == 0) to avoid invalid access.

This also aligns with recent fixes like Commit 44acc46d182f ("ocfs2:
avoid NULL pointer dereference in dx_dir_lookup_rec()"), which addressed issues caused by empty extent lists.

I will update the commit message in the next version to explicitly state that this is a hardening measure against on-disk corruption.

- Jiasheng

Quoted 24 lines
Quoted 24 lines
---
 fs/ocfs2/alloc.c | 4 ++++
 1 file changed, 4 insertions(+)
fs/ocfs2/alloc.c +4 -0
index 58bf58b68955..b15c6f751d99 100644
@@ -2327,6 +2327,10 @@ static int ocfs2_rotate_requires_path_adjustment(struct ocfs2_path *left_path,
 
 	left_el = path_leaf_el(left_path);
 	next_free = le16_to_cpu(left_el->l_next_free_rec);
+
+	if (unlikely(next_free == 0))
+		return 0;
+
 	rec = &left_el->l_recs[next_free - 1];
 
 	if (insert_cpos > le32_to_cpu(rec->e_cpos))
Signature · 1 line
2.25.1