Skip to contentSkip to conversation

ocfs2: fix NULL pointer dereference in ocfs2_xattr_get_rec

PATCH Superseded Jiasheng Jiang 2 participants 3 messages Jan 17 – Jan 19 OCFS2

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 ocfs2: fix NULL pointer dereference in ocfs2_xattr_get_rec

In ocfs2_xattr_get_rec, the variable 'rec' is initialized to NULL. If the extent list 'el' is empty (l_next_free_rec == 0), the loop iterating over the records is skipped, leaving 'rec' as NULL.

Since 'e_blkno' is initialized to 0, the function enters the error handling block 'if (!e_blkno)'. Inside this block, the function calls ocfs2_error() and attempts to dereference 'rec' via 'le32_to_cpu(rec->e_cpos)' and 'ocfs2_rec_clusters(el, rec)'. This results in a NULL pointer dereference and a kernel panic.

Fix this by ensuring 'rec' is not NULL before accessing its members within the error handling path, or by checking for an empty list explicitly.

---
 fs/ocfs2/xattr.c | 14 ++++++++++----
 1 file changed, 10 insertions(+), 4 deletions(-)
fs/ocfs2/xattr.c +10 -4
index 1b21fbc16d73..b018c84dbc05 100644
@@ -3757,10 +3757,16 @@ static int ocfs2_xattr_get_rec(struct inode *inode,
 	}
 
 	if (!e_blkno) {
-		ret = ocfs2_error(inode->i_sb, "Inode %lu has bad extent record (%u, %u, 0) in xattr\n",
-				  inode->i_ino,
-				  le32_to_cpu(rec->e_cpos),
-				  ocfs2_rec_clusters(el, rec));
+		if (rec)
+			ret = ocfs2_error(inode->i_sb,
+					  "Inode %lu has bad extent record (%u, %u, 0) in xattr\n",
+					  inode->i_ino,
+					  le32_to_cpu(rec->e_cpos),
+					  ocfs2_rec_clusters(el, rec));
+		else
+			ret = ocfs2_error(inode->i_sb,
+					  "Inode %lu has bad extent record (NULL) in xattr\n",
+					  inode->i_ino);
 		goto out;
 	}
 
Signature · 1 line
2.25.1
reviewer
#1 lore raw
Quoted 9 lines · Jiasheng Jiang

In ocfs2_xattr_get_rec, the variable 'rec' is initialized to NULL. If the extent list 'el' is empty (l_next_free_rec == 0), the loop iterating over the records is skipped, leaving 'rec' as NULL.

Since 'e_blkno' is initialized to 0, the function enters the error handling block 'if (!e_blkno)'. Inside this block, the function calls ocfs2_error() and attempts to dereference 'rec' via 'le32_to_cpu(rec->e_cpos)' and 'ocfs2_rec_clusters(el, rec)'. This results in a NULL pointer dereference and a kernel panic.

Do you have reproduction steps to support your analysis?

- Heming

Quoted 39 lines

Fix this by ensuring 'rec' is not NULL before accessing its members within the error handling path, or by checking for an empty list explicitly.

---
 fs/ocfs2/xattr.c | 14 ++++++++++----
 1 file changed, 10 insertions(+), 4 deletions(-)
fs/ocfs2/xattr.c +10 -4
index 1b21fbc16d73..b018c84dbc05 100644
@@ -3757,10 +3757,16 @@ static int ocfs2_xattr_get_rec(struct inode *inode,
 	}
 
 	if (!e_blkno) {
-		ret = ocfs2_error(inode->i_sb, "Inode %lu has bad extent record (%u, %u, 0) in xattr\n",
-				  inode->i_ino,
-				  le32_to_cpu(rec->e_cpos),
-				  ocfs2_rec_clusters(el, rec));
+		if (rec)
+			ret = ocfs2_error(inode->i_sb,
+					  "Inode %lu has bad extent record (%u, %u, 0) in xattr\n",
+					  inode->i_ino,
+					  le32_to_cpu(rec->e_cpos),
+					  ocfs2_rec_clusters(el, rec));
+		else
+			ret = ocfs2_error(inode->i_sb,
+					  "Inode %lu has bad extent record (NULL) in xattr\n",
+					  inode->i_ino);
 		goto out;
 	}
 
Signature · 1 line
2.25.1
#2 lore raw
Quoted 11 lines · Heming Zhao
Quoted 9 lines · Jiasheng Jiang

In ocfs2_xattr_get_rec, the variable 'rec' is initialized to NULL. If the extent list 'el' is empty (l_next_free_rec == 0), the loop iterating over the records is skipped, leaving 'rec' as NULL.

Since 'e_blkno' is initialized to 0, the function enters the error handling block 'if (!e_blkno)'. Inside this block, the function calls ocfs2_error() and attempts to dereference 'rec' via 'le32_to_cpu(rec->e_cpos)' and 'ocfs2_rec_clusters(el, rec)'. This results in a NULL pointer dereference and a kernel panic.

Quoted 3 lines

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, such inconsistency leads to a NULL pointer dereference and panics the system.

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

This approach aligns with recent fixes in OCFS2. For example, Commit 44acc46d182f ("ocfs2: avoid NULL pointer dereference in dx_dir_lookup_rec()") addressed an identical issue where an empty extent list left the 'rec' pointer NULL.

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

- Jiasheng

Quoted 39 lines
Quoted 39 lines

Fix this by ensuring 'rec' is not NULL before accessing its members within the error handling path, or by checking for an empty list explicitly.

---
 fs/ocfs2/xattr.c | 14 ++++++++++----
 1 file changed, 10 insertions(+), 4 deletions(-)
fs/ocfs2/xattr.c +10 -4
index 1b21fbc16d73..b018c84dbc05 100644
@@ -3757,10 +3757,16 @@ static int ocfs2_xattr_get_rec(struct inode *inode,
 	}
 
 	if (!e_blkno) {
-		ret = ocfs2_error(inode->i_sb, "Inode %lu has bad extent record (%u, %u, 0) in xattr\n",
-				  inode->i_ino,
-				  le32_to_cpu(rec->e_cpos),
-				  ocfs2_rec_clusters(el, rec));
+		if (rec)
+			ret = ocfs2_error(inode->i_sb,
+					  "Inode %lu has bad extent record (%u, %u, 0) in xattr\n",
+					  inode->i_ino,
+					  le32_to_cpu(rec->e_cpos),
+					  ocfs2_rec_clusters(el, rec));
+		else
+			ret = ocfs2_error(inode->i_sb,
+					  "Inode %lu has bad extent record (NULL) in xattr\n",
+					  inode->i_ino);
 		goto out;
 	}
 
Signature · 1 line
2.25.1