Skip to contentSkip to conversation

ocfs2: Avoid NULL pointer dereference in dx_dir_lookup_rec()

PATCH Applied Ivan Pravdin 2 participants 2 messages Jul 8, 2025 OCFS2

lore
Share & tools
lore
#0 lore raw

PATCH v3 1/1 ocfs2: Avoid NULL pointer dereference in dx_dir_lookup_rec()

When a directory entry is not found, ocfs2_dx_dir_lookup_rec() prints an error message that unconditionally dereferences the 'rec' pointer. However, if 'rec' is NULL, this leads to a NULL pointer dereference and a kernel panic.

Add an explicit check empty extent list to avoid dereferencing NULL 'rec' pointer.

---
v1 -> v2: Changed 'Closes:' tag to point to the correct bug report.
v2 -> v3: Added an explicit check for empty extent list before dereferencing 'rec'.

 fs/ocfs2/dir.c | 8 ++++++++
 1 file changed, 8 insertions(+)
fs/ocfs2/dir.c +8 -0
index 7799f4d16ce9..acbae902ae3a 100644
@@ -798,6 +798,14 @@ static int ocfs2_dx_dir_lookup_rec(struct inode *inode,
 		}
 	}
 
+	if (le16_to_cpu(el->l_next_free_rec) == 0) {
+		ret = ocfs2_error(inode->i_sb,
+				  "Inode %lu has empty extent list at depth %u\n",
+				  inode->i_ino, 
+				  le16_to_cpu(el->l_tree_depth));
+		goto out;
+	}
+
 	found = 0;
 	for (i = le16_to_cpu(el->l_next_free_rec) - 1; i >= 0; i--) {
 		rec = &el->l_recs[i];
Signature · 1 line
2.45.2
maintainer Reviewed-by on v3 1/1
#1 lore raw
Quoted 11 lines · Ivan Pravdin

When a directory entry is not found, ocfs2_dx_dir_lookup_rec() prints an error message that unconditionally dereferences the 'rec' pointer. However, if 'rec' is NULL, this leads to a NULL pointer dereference and a kernel panic.

Add an explicit check empty extent list to avoid dereferencing NULL 'rec' pointer.

Looks fine.

Quoted 26 lines

---
v1 -> v2: Changed 'Closes:' tag to point to the correct bug report.
v2 -> v3: Added an explicit check for empty extent list before dereferencing 'rec'.

 fs/ocfs2/dir.c | 8 ++++++++
 1 file changed, 8 insertions(+)
fs/ocfs2/dir.c +8 -0
index 7799f4d16ce9..acbae902ae3a 100644
@@ -798,6 +798,14 @@ static int ocfs2_dx_dir_lookup_rec(struct inode *inode,
 		}
 	}
 
+	if (le16_to_cpu(el->l_next_free_rec) == 0) {
+		ret = ocfs2_error(inode->i_sb,
+				  "Inode %lu has empty extent list at depth %u\n",
+				  inode->i_ino, 
+				  le16_to_cpu(el->l_tree_depth));
+		goto out;
+	}
+
 	found = 0;
 	for (i = le16_to_cpu(el->l_next_free_rec) - 1; i >= 0; i--) {
 		rec = &el->l_recs[i];