ocfs2: Avoid NULL pointer dereference in dx_dir_lookup_rec()
PATCH 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 for a NULL 'rec' and use an alternate error message in that case to avoid unsafe access.
--- fs/ocfs2/dir.c | 16 +++++++++++----- 1 file changed, 11 insertions(+), 5 deletions(-)
fs/ocfs2/dir.c +11 -5
@@ -809,11 +809,17 @@ static int ocfs2_dx_dir_lookup_rec(struct inode *inode, }if (!found) { - ret =ocfs2_error (inode->i_sb, -"Inode %lu has bad extent record (%u, %u, 0) in btree\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 btree\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 no extent records in btree\n" , + inode->i_ino); + } goto out; }
Signature · 1 line
2.45.2
Joseph Qi
maintainerHi,
Quoted 5 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.
This looks possible, but syzbot reports slab-out-of-bounds Read in ocfs2_dx_dir_lookup_rec(), not NULL pointer dereference.
So I think it is because it construct a malicious image and set a wrong l_recs, then access this damaged l_recs.
Thanks,
Joseph
Quoted 37 lines
Add an explicit check for a NULL 'rec' and use an alternate error message in that case to avoid unsafe access.
Reported-by [email protected]Signed-off-by Ivan Pravdin <[email protected]>--- fs/ocfs2/dir.c | 16 +++++++++++----- 1 file changed, 11 insertions(+), 5 deletions(-)fs/ocfs2/dir.c +11
-5@@ -809,11 +809,17 @@ static int ocfs2_dx_dir_lookup_rec(struct inode *inode, }if (!found) { - ret =ocfs2_error (inode->i_sb, -"Inode %lu has bad extent record (%u, %u, 0) in btree\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 btree\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 no extent records in btree\n" , + inode->i_ino); + } goto out; }
Heming Zhao
reviewerQuoted 10 lines · Joseph Qi
Hi,
Quoted 5 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.
Quoted 8 lines
This looks possible, but syzbot reports slab-out-of-bounds Read in
ocfs2_dx_dir_lookup_rec(), not NULL pointer dereference.So I think it is because it construct a malicious image and set a wrong
l_recs, then access this damagedl_recs.Thanks,
Joseph
I think this proposed fix (at least the fix method) is acceptable. the crash occurs at ocfs2_error(), where the pointer 'rec' must be incorrect. look back at the previous code lines:
for (i = le16_to_cpu(el->l_next_free_rec) - 1; i >= 0; i--) {
rec = &el->l_recs[i];
if (le32_to_cpu(rec->e_cpos) <= major_hash) {
found = 1;
break;
}
}either 'el->l_next_free_rec' or 'el->l_recs[i]' has an incorrect value.
we can do nothing about this kind of error, simply returning errno to caller is sufficient.
btw, ocfs2-tools has a similar function "static errcode_t ocfs2_dx_dir_lookup_rec()"@libocfs2/dir_indexed.c, which sets ret with OCFS2_ET_CORRUPT_EXTENT_BLOCK and return.
- Heming
Quoted 41 lines
Quoted 39 lines
Add an explicit check for a NULL 'rec' and use an alternate error message in that case to avoid unsafe access.
Reported-by [email protected]Signed-off-by Ivan Pravdin <[email protected]>--- fs/ocfs2/dir.c | 16 +++++++++++----- 1 file changed, 11 insertions(+), 5 deletions(-)fs/ocfs2/dir.c +11
-5@@ -809,11 +809,17 @@ static int ocfs2_dx_dir_lookup_rec(struct inode *inode, }if (!found) { - ret =ocfs2_error (inode->i_sb, -"Inode %lu has bad extent record (%u, %u, 0) in btree\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 btree\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 no extent records in btree\n" , + inode->i_ino); + } goto out; }
Quoted 32 lines · Heming Zhao
Quoted 10 lines · Joseph Qi
Hi,
Quoted 5 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.Quoted 8 lines
This looks possible, but syzbot reports slab-out-of-bounds Read in
ocfs2_dx_dir_lookup_rec(), not NULL pointer dereference.So I think it is because it construct a malicious image and set a wrong
l_recs, then access this damagedl_recs.Thanks,
JosephI think this proposed fix (at least the fix method) is acceptable. the crash occurs at
ocfs2_error(), where the pointer 'rec' must be incorrect. look back at the previous code lines:for (i = le16_to_cpu(el->l_next_free_rec) - 1; i >= 0; i--) { rec = &el->l_recs[i]; if (le32_to_cpu(rec->e_cpos) <= major_hash) { found = 1; break; } }
Quoted 4 lines
either '
el->l_next_free_rec' or 'el->l_recs[i]' has an incorrect value.
we can do nothing about this kind of error, simply returning errno to caller is sufficient.btw, ocfs2-tools has a similar function "static
errcode_tocfs2_dx_dir_lookup_rec()"@libocfs2/dir_indexed.c, which sets ret withOCFS2_ET_CORRUPT_EXTENT_BLOCKand return.
Thanks, I will include it in v2.
Quoted 43 lines
- Heming
Quoted 39 lines
Quoted 37 lines
Add an explicit check for a NULL 'rec' and use an alternate error message in that case to avoid unsafe access.
Reported-by [email protected]Signed-off-by Ivan Pravdin <[email protected]>--- fs/ocfs2/dir.c | 16 +++++++++++----- 1 file changed, 11 insertions(+), 5 deletions(-)fs/ocfs2/dir.c +11
-5@@ -809,11 +809,17 @@ static int ocfs2_dx_dir_lookup_rec(struct inode *inode, }if (!found) { - ret =ocfs2_error (inode->i_sb, -"Inode %lu has bad extent record (%u, %u, 0) in btree\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 btree\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 no extent records in btree\n" , + inode->i_ino); + } goto out; }
Ivan Pravdin
Quoted 33 lines · Ivan Pravdin
Quoted 32 lines · Heming Zhao
Quoted 10 lines · Joseph Qi
Hi,
Quoted 5 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.Quoted 8 lines
This looks possible, but syzbot reports slab-out-of-bounds Read in
ocfs2_dx_dir_lookup_rec(), not NULL pointer dereference.So I think it is because it construct a malicious image and set a wrong
l_recs, then access this damagedl_recs.Thanks,
JosephI think this proposed fix (at least the fix method) is acceptable. the crash occurs at
ocfs2_error(), where the pointer 'rec' must be incorrect. look back at the previous code lines:for (i = le16_to_cpu(el->l_next_free_rec) - 1; i >= 0; i--) { rec = &el->l_recs[i]; if (le32_to_cpu(rec->e_cpos) <= major_hash) { found = 1; break; } }
Quoted 6 lines
Quoted 4 lines
either '
el->l_next_free_rec' or 'el->l_recs[i]' has an incorrect value.
we can do nothing about this kind of error, simply returning errno to caller is sufficient.btw, ocfs2-tools has a similar function "static
errcode_tocfs2_dx_dir_lookup_rec()"@libocfs2/dir_indexed.c, which sets ret withOCFS2_ET_CORRUPT_EXTENT_BLOCKand return.Thanks, I will include it in v2.
I have realized that ret is already set to EROFS that acts as a general FS errno. Unless there are any other objectives this is the latest version.
Ivan Pravdin
Joseph Qi
maintainerQuoted 14 lines · Heming Zhao
Quoted 13 lines · Joseph Qi
Hi,
Quoted 5 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.This looks possible, but syzbot reports slab-out-of-bounds Read in
ocfs2_dx_dir_lookup_rec(), not NULL pointer dereference.
Quoted 7 lines
Quoted 5 lines
So I think it is because it construct a malicious image and set a wrong
l_recs, then access this damagedl_recs.Thanks,
JosephI think this proposed fix (at least the fix method) is acceptable.
But this seems different from the issue syzbot reports, right?
https://lore.kernel.org/all/[email protected]/T/
Thanks,
Joseph
Quoted 16 lines
the crash occurs at
ocfs2_error(), where the pointer 'rec' must be incorrect. look back at the previous code lines:
for (i =le16_to_cpu(el->l_next_free_rec)- 1; i >= 0; i--) { rec = &el->l_recs[i];if (
le32_to_cpu(rec->e_cpos)<=major_hash) { found = 1;
break;
}
}either '
el->l_next_free_rec' or 'el->l_recs[i]' has an incorrect value.
we can do nothing about this kind of error, simply returning errno to caller is sufficient.btw, ocfs2-tools has a similar function "static
errcode_tocfs2_dx_dir_lookup_rec()"@libocfs2/dir_indexed.c, which sets ret withOCFS2_ET_CORRUPT_EXTENT_BLOCKand return.
Heming Zhao
reviewerQuoted 23 lines · Joseph Qi
Quoted 20 lines · Heming Zhao
Quoted 18 lines · Joseph Qi
Hi,
Quoted 5 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.This looks possible, but syzbot reports slab-out-of-bounds Read in
ocfs2_dx_dir_lookup_rec(), not NULL pointer dereference.So I think it is because it construct a malicious image and set a wrong
l_recs, then access this damagedl_recs.Thanks,
Joseph
Quoted 7 lines
Quoted 1 line
I think this proposed fix (at least the fix method) is acceptable.
But this seems different from the issue syzbot reports, right?
https://lore.kernel.org/all/[email protected]/T/Thanks,
Joseph
OK, I think I may have found the problem. In the URL above, there are two issues/reports:
1> From: syzbot @ 2024-11-28 9:44 UTC
2> From: syzbot @ 2025-03-09 11:40 UTC
Your review comments about report <1>; Ivan's patch is about report <2>.
And the dashboard shows the <2>:
- https://syzkaller.appspot.com/bug?extid=20282c1b2184a857ac4c
It seems syzbot changed the fuzz code and won't trigger issue <1>.
For issue <1>, the rec was an illegal value, and made KASAN report a bug.
Quoted 20 lines
Quoted 17 lines
the crash occurs at
ocfs2_error(), where the pointer 'rec' must be incorrect. look back at the previous code lines:
for (i =le16_to_cpu(el->l_next_free_rec)- 1; i >= 0; i--) { rec = &el->l_recs[i];if (
le32_to_cpu(rec->e_cpos)<=major_hash) { found = 1;
break;
}
}either '
el->l_next_free_rec' or 'el->l_recs[i]' has an incorrect value.
we can do nothing about this kind of error, simply returning errno to caller is sufficient.btw, ocfs2-tools has a similar function "static
errcode_tocfs2_dx_dir_lookup_rec()"@libocfs2/dir_indexed.c, which sets ret withOCFS2_ET_CORRUPT_EXTENT_BLOCKand return.