ocfs2: bound-check both readdir re-validation scans
v2 cover ocfs2: bound-check both readdir re-validation scans
Andrew, patch 1 replaces
ocfs2-bound-check-dir-entries-in-the-readdir-re-validation-scan.patch
in mm-nonmm-unstable. Patch 2 is new.
Both are against that tree rather than mainline, because the patch queued ahead of them rewrote the ctx->pos line that is now in patch 1's trailing context.
v1 only bounded the position before de is dereferenced. Joseph pointed out that the walk has to stay inside the block as well, which is the second guard ocfs2_check_dir_entry() applies for the emit loop; patch 1 adds it. Checking whether anything else re-scans the same way turned up ocfs2_dir_foreach_blk_id(), which walks the inline data area and is missing both guards -- that is patch 2, which Joseph has not seen yet.
Neither patch carries Fixes: or Cc: stable, matching the trailers kept for v1; please add them if you would rather they were there.
Link to v1: https://lore.kernel.org/all/[email protected]/
Zhan Xusheng (2):
ocfs2: bound-check dir entries in the readdir re-validation scan
ocfs2: bound-check dir entries in the inline-data re-validation scan
fs/ocfs2/dir.c | 26 ++++++++++++++++++-------- 1 file changed, 18 insertions(+), 8 deletions(-)
Signature · 1 line
2.43.0
PATCH v2 1/2 ocfs2: bound-check dir entries in the readdir re-validation scan
When the inode version changed since the last readdir(), ocfs2_dir_foreach_blk_el() re-scans the directory block from its start to relocate the current position:
for (i = 0; i < sb->s_blocksize && i < offset; ) {
de = (struct ocfs2_dir_entry *)(bh->b_data + i);
if (le16_to_cpu(de->rec_len) < OCFS2_DIR_REC_LEN(1))
break;
i += le16_to_cpu(de->rec_len);
}i walks the block on rec_len values taken from the block itself and the only thing tested is that rec_len is not too small, so a single bogus rec_len
leaves i anywhere in the block, including its last OCFS2_DIR_REC_LEN(1) - 1 bytes. @offset comes from ctx->pos, which userspace moves with lseek() on the directory fd, and decides how far the walk gets.
Two bounds are missing, both of which ocfs2_check_dir_entry() applies for the emit loop below.
de->rec_len sits at byte offset 8 within the entry, so dereferencing de in that tail reads past the s_blocksize buffer. ocfs2_check_dir_entry() declines to look at an entry that close to the end:
size - buf_offset < OCFS2_DIR_REC_LEN(1)
Nothing bounds i += rec_len either, so i can end up past the block. The emit loop that follows is guarded by offset < sb->s_blocksize and does not run, but
offset = i; ctx->pos = (ctx->pos & ~((loff_t)sb->s_blocksize - 1)) | offset;
runs first and ORs a value with bits above the block mask into ctx->pos, corrupting the block number readdir() resumes from. ocfs2_check_dir_entry() rejects that as "directory entry overrun":
next_offset = buf_offset + rlen; ... next_offset > size
Apply both bounds. For a consistent directory this changes nothing:
entries are at least OCFS2_DIR_REC_LEN(1) bytes and do not cross the end of the block, so no valid entry is skipped.
Found by the sashiko review tool; fix approach suggested by Joseph Qi.
--- fs/ocfs2/dir.c | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-)
fs/ocfs2/dir.c +8 -4
@@ -1903,7 +1903,10 @@ static int ocfs2_dir_foreach_blk_el(struct inode *inode,* dirent right now. Scan from the start of the block * to make sure. */ if (!inode_eq_iversion (inode, *f_version)) { -for (i =0 ; i < sb->s_blocksize && i < offset; ) { +for (i =0 ; i +OCFS2_DIR_REC_LEN (1 ) <= sb->s_blocksize && + i < offset;) { +unsigned int rec_len; + de = (struct ocfs2_dir_entry *) (bh->b_data + i);/* It's too expensive to do a full * dirent test each time round this
@@ -1911,10 +1914,11 @@ static int ocfs2_dir_foreach_blk_el(struct inode *inode,* least that it is non-zero. A * failure will be detected in the * dirent test below. */ -if (le16_to_cpu (de->rec_len) < -OCFS2_DIR_REC_LEN (1 )) + rec_len =le16_to_cpu (de->rec_len); +if (rec_len <OCFS2_DIR_REC_LEN (1 ) || + i + rec_len > sb->s_blocksize)break ; - i +=le16_to_cpu (de->rec_len); + i += rec_len; } offset = i; ctx->pos = (ctx->pos & ~((loff_t )sb->s_blocksize -1 ))
Signature · 1 line
2.43.0
Re: [PATCH v2 1/2] ocfs2: bound-check dir entries in the readdir re-validation scan
Quoted 47 lines · Zhan Xusheng
When the inode version changed since the last
readdir(),ocfs2_dir_foreach_blk_el()re-scans the directory block from its start to relocate the current position:for (i = 0; i < sb->s_blocksize && i < offset; ) { de = (struct ocfs2_dir_entry *)(bh->b_data + i); if (le16_to_cpu(de->rec_len) < OCFS2_DIR_REC_LEN(1)) break; i += le16_to_cpu(de->rec_len); }i walks the block on
rec_lenvalues taken from the block itself and the only thing tested is thatrec_lenis not too small, so a single bogusrec_len
leaves i anywhere in the block, including its lastOCFS2_DIR_REC_LEN(1)- 1 bytes. @offset comes fromctx->pos, which userspace moves withlseek()on the directory fd, and decides how far the walk gets.Two bounds are missing, both of which
ocfs2_check_dir_entry()applies for the emit loop below.
de->rec_lensits at byte offset 8 within the entry, so dereferencing de in that tail reads past thes_blocksizebuffer.ocfs2_check_dir_entry()declines to look at an entry that close to the end:size -
buf_offset<OCFS2_DIR_REC_LEN(1)Nothing bounds i +=
rec_leneither, so i can end up past the block. The emit loop that follows is guarded by offset <sb->s_blocksizeand does not run, butoffset = i; ctx->pos = (ctx->pos & ~((loff_t)sb->s_blocksize - 1)) | offset;runs first and ORs a value with bits above the block mask into
ctx->pos, corrupting the block numberreaddir()resumes from.ocfs2_check_dir_entry()rejects that as "directory entry overrun":next_offset = buf_offset + rlen; ... next_offset > sizeApply both bounds. For a consistent directory this changes nothing:
entries are at leastOCFS2_DIR_REC_LEN(1)bytes and do not cross the end of the block, so no valid entry is skipped.Found by the sashiko review tool; fix approach suggested by Joseph Qi.
Quoted 9 lines
Suggested-by Joseph Qi <[email protected]>Cc Mark Fasheh <[email protected]>Cc Joel Becker <[email protected]>Cc Junxiao Bi <[email protected]>Cc Changwei Ge <[email protected]>Cc Jun Piao <[email protected]>Cc Heming Zhao <[email protected]>Signed-off-by Zhan Xusheng <[email protected]>
Looks fine.
Quoted 35 lines
--- fs/ocfs2/dir.c | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-)fs/ocfs2/dir.c +8
-4@@ -1903,7 +1903,10 @@ static int ocfs2_dir_foreach_blk_el(struct inode *inode,* dirent right now. Scan from the start of the block * to make sure. */ if (!inode_eq_iversion (inode, *f_version)) { -for (i =0 ; i < sb->s_blocksize && i < offset; ) { +for (i =0 ; i +OCFS2_DIR_REC_LEN (1 ) <= sb->s_blocksize && + i < offset;) { +unsigned int rec_len; + de = (struct ocfs2_dir_entry *) (bh->b_data + i);/* It's too expensive to do a full * dirent test each time round this @@ -1911,10 +1914,11 @@ static int ocfs2_dir_foreach_blk_el(struct inode *inode,* least that it is non-zero. A * failure will be detected in the * dirent test below. */ -if (le16_to_cpu (de->rec_len) < -OCFS2_DIR_REC_LEN (1 )) + rec_len =le16_to_cpu (de->rec_len); +if (rec_len <OCFS2_DIR_REC_LEN (1 ) || + i + rec_len > sb->s_blocksize)break ; - i +=le16_to_cpu (de->rec_len); + i += rec_len; } offset = i; ctx->pos = (ctx->pos & ~((loff_t )sb->s_blocksize -1 ))
Re: [PATCH v2 1/2] ocfs2: bound-check dir entries in the readdir re-validation scan
In reply to Zhan XushengQuoted 3 lines · On Tue, 11 Aug 2026 10:43:36 +0800 Zhan Xusheng <[email protected]>…
From: Zhan Xusheng <[email protected]>
...Signed-off-by Zhan Xusheng <[email protected]>
checkpatch:
WARNING: From:/Signed-off-by: email address mismatch: 'From: Zhan Xusheng <[email protected]>' != 'Signed-off-by: Zhan Xusheng <[email protected]>'
This can be prevented by using an explicit From: line at start-of-changelog. Please let me know which address should be used and I'll make the edit, thanks.
Re: [PATCH v2 1/2] ocfs2: bound-check dir entries in the readdir re-validation scan
Quoted 3 lines · Andrew Morton
This can be prevented by using an explicit From: line at start-of-changelog. Please let me know which address should be used and I'll make the edit, thanks.
[email protected] please, for both patches in the series.
The header was rewritten by the smarthost I send through, which is why it disagreed with the Signed-off-by. I have set format.from so the explicit
From: line is there from now on.
Thanks,
Zhan Xusheng
PATCH v2 2/2 ocfs2: bound-check dir entries in the inline-data re-validation scan
In reply to Zhan Xushengocfs2_dir_foreach_blk_id() re-scans the inline data area the same way ocfs2_dir_foreach_blk_el() re-scans a directory block, and is missing the same two bounds:
for (i = 0; i < i_size_read(inode) && i < offset; ) {
de = (struct ocfs2_dir_entry *)(data->id_data + i);
if (le16_to_cpu(de->rec_len) < OCFS2_DIR_REC_LEN(1))
break;
i += le16_to_cpu(de->rec_len);
}ocfs2_validate_inode_block() keeps i_size inside the inline area:
if (le16_to_cpu(data->id_count) > ocfs2_max_inline_data_with_xattr(sb, di)) if (le64_to_cpu(di->i_size) > le16_to_cpu(data->id_count))
and that area runs to the end of the inode block, so for a full inline directory data->id_data + i_size is the end of di_bh->b_data. A bogus rec_len leaves i in the last OCFS2_DIR_REC_LEN(1) - 1 bytes of it, and de->rec_len, at byte offset 8 within the entry, is then read past the block.
The emit loop below hands i_size_read(inode) to ocfs2_check_dir_entry(), which refuses both an entry that close to the end and one whose rec_len runs past it. Apply the same two bounds to the re-validation scan, reading i_size once into a local as ocfs2_check_dir_entry() takes it as @size.
Unlike the extent case there is no mask to corrupt here: an unbounded i only sets ctx->pos past i_size, which ends the readdir early rather than moving it to the wrong place.
--- fs/ocfs2/dir.c | 14 ++++++++++---- 1 file changed, 10 insertions(+), 4 deletions(-)
fs/ocfs2/dir.c +10 -4
@@ -1812,7 +1812,12 @@ static int ocfs2_dir_foreach_blk_id(struct inode *inode,* dirent right now. Scan from the start of the block * to make sure. */ if (!inode_eq_iversion (inode, *f_version)) { -for (i =0 ; i <i_size_read (inode) && i < offset; ) { +loff_t size =i_size_read (inode); + +for (i =0 ; i +OCFS2_DIR_REC_LEN (1 ) <= size && + i < offset;) { +unsigned int rec_len; + de = (struct ocfs2_dir_entry *) (data->id_data + i);/* It's too expensive to do a full
@@ -1821,10 +1826,11 @@ static int ocfs2_dir_foreach_blk_id(struct inode *inode,* least that it is non-zero. A * failure will be detected in the * dirent test below. */ -if (le16_to_cpu (de->rec_len) < -OCFS2_DIR_REC_LEN (1 )) + rec_len =le16_to_cpu (de->rec_len); +if (rec_len <OCFS2_DIR_REC_LEN (1 ) || + i + rec_len > size)break ; - i +=le16_to_cpu (de->rec_len); + i += rec_len; } ctx->pos = offset = i; *f_version =inode_query_iversion (inode);
Signature · 1 line
2.43.0
Re: [PATCH v2 2/2] ocfs2: bound-check dir entries in the inline-data re-validation scan
Quoted 32 lines · Zhan Xusheng
ocfs2_dir_foreach_blk_id()re-scans the inline data area the same wayocfs2_dir_foreach_blk_el()re-scans a directory block, and is missing the same two bounds:for (i = 0; i < i_size_read(inode) && i < offset; ) { de = (struct ocfs2_dir_entry *)(data->id_data + i); if (le16_to_cpu(de->rec_len) < OCFS2_DIR_REC_LEN(1)) break; i += le16_to_cpu(de->rec_len); }
ocfs2_validate_inode_block()keepsi_sizeinside the inline area:if (le16_to_cpu(data->id_count) > ocfs2_max_inline_data_with_xattr(sb, di)) if (le64_to_cpu(di->i_size) > le16_to_cpu(data->id_count))and that area runs to the end of the inode block, so for a full inline directory
data->id_data+i_sizeis the end ofdi_bh->b_data. A bogusrec_lenleaves i in the lastOCFS2_DIR_REC_LEN(1)- 1 bytes of it, andde->rec_len, at byte offset 8 within the entry, is then read past the block.The emit loop below hands
i_size_read(inode)toocfs2_check_dir_entry(), which refuses both an entry that close to the end and one whoserec_lenruns past it. Apply the same two bounds to the re-validation scan, readingi_sizeonce into a local asocfs2_check_dir_entry()takes it as @size.Unlike the extent case there is no mask to corrupt here: an unbounded i only sets
ctx->pospasti_size, which ends the readdir early rather than moving it to the wrong place.
Quoted 7 lines
Cc Mark Fasheh <[email protected]>Cc Joel Becker <[email protected]>Cc Junxiao Bi <[email protected]>Cc Changwei Ge <[email protected]>Cc Jun Piao <[email protected]>Cc Heming Zhao <[email protected]>Signed-off-by Zhan Xusheng <[email protected]>
Looks fine.
Quoted 37 lines
--- fs/ocfs2/dir.c | 14 ++++++++++---- 1 file changed, 10 insertions(+), 4 deletions(-)fs/ocfs2/dir.c +10
-4@@ -1812,7 +1812,12 @@ static int ocfs2_dir_foreach_blk_id(struct inode *inode,* dirent right now. Scan from the start of the block * to make sure. */ if (!inode_eq_iversion (inode, *f_version)) { -for (i =0 ; i <i_size_read (inode) && i < offset; ) { +loff_t size =i_size_read (inode); + +for (i =0 ; i +OCFS2_DIR_REC_LEN (1 ) <= size && + i < offset;) { +unsigned int rec_len; + de = (struct ocfs2_dir_entry *) (data->id_data + i);/* It's too expensive to do a full @@ -1821,10 +1826,11 @@ static int ocfs2_dir_foreach_blk_id(struct inode *inode,* least that it is non-zero. A * failure will be detected in the * dirent test below. */ -if (le16_to_cpu (de->rec_len) < -OCFS2_DIR_REC_LEN (1 )) + rec_len =le16_to_cpu (de->rec_len); +if (rec_len <OCFS2_DIR_REC_LEN (1 ) || + i + rec_len > size)break ; - i +=le16_to_cpu (de->rec_len); + i += rec_len; } ctx->pos = offset = i; *f_version =inode_query_iversion (inode);