Skip to content

Commit 1eb70f5

Browse files
Dave Chinnerdchinner
authored andcommitted
xfs: validate inode fork size against fork format
xfs_repair catches fork size/format mismatches, but the in-kernel verifier doesn't, leading to null pointer failures when attempting to perform operations on the fork. This can occur in the xfs_dir_is_empty() where the in-memory fork format does not match the size and so the fork data pointer is accessed incorrectly. Note: this causes new failures in xfs/348 which is testing mode vs ftype mismatches. We now detect a regular file that has been changed to a directory or symlink mode as being corrupt because the data fork is for a symlink or directory should be in local form when there are only 3 bytes of data in the data fork. Hence the inode verify for the regular file now fires w/ -EFSCORRUPTED because the inode fork format does not match the format the corrupted mode says it should be in. Signed-off-by: Dave Chinner <dchinner@redhat.com> Reviewed-by: Christoph Hellwig <hch@lst.de> Reviewed-by: Darrick J. Wong <djwong@kernel.org> Signed-off-by: Dave Chinner <david@fromorbit.com>
1 parent dc04db2 commit 1eb70f5

File tree

1 file changed

+26
-9
lines changed

1 file changed

+26
-9
lines changed

fs/xfs/libxfs/xfs_inode_buf.c

Lines changed: 26 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -357,21 +357,38 @@ xfs_dinode_verify_fork(
357357
{
358358
xfs_extnum_t di_nextents;
359359
xfs_extnum_t max_extents;
360+
mode_t mode = be16_to_cpu(dip->di_mode);
361+
uint32_t fork_size = XFS_DFORK_SIZE(dip, mp, whichfork);
362+
uint32_t fork_format = XFS_DFORK_FORMAT(dip, whichfork);
360363

361364
di_nextents = xfs_dfork_nextents(dip, whichfork);
362365

363-
switch (XFS_DFORK_FORMAT(dip, whichfork)) {
366+
/*
367+
* For fork types that can contain local data, check that the fork
368+
* format matches the size of local data contained within the fork.
369+
*
370+
* For all types, check that when the size says the should be in extent
371+
* or btree format, the inode isn't claiming it is in local format.
372+
*/
373+
if (whichfork == XFS_DATA_FORK) {
374+
if (S_ISDIR(mode) || S_ISLNK(mode)) {
375+
if (be64_to_cpu(dip->di_size) <= fork_size &&
376+
fork_format != XFS_DINODE_FMT_LOCAL)
377+
return __this_address;
378+
}
379+
380+
if (be64_to_cpu(dip->di_size) > fork_size &&
381+
fork_format == XFS_DINODE_FMT_LOCAL)
382+
return __this_address;
383+
}
384+
385+
switch (fork_format) {
364386
case XFS_DINODE_FMT_LOCAL:
365387
/*
366-
* no local regular files yet
388+
* No local regular files yet.
367389
*/
368-
if (whichfork == XFS_DATA_FORK) {
369-
if (S_ISREG(be16_to_cpu(dip->di_mode)))
370-
return __this_address;
371-
if (be64_to_cpu(dip->di_size) >
372-
XFS_DFORK_SIZE(dip, mp, whichfork))
373-
return __this_address;
374-
}
390+
if (S_ISREG(mode) && whichfork == XFS_DATA_FORK)
391+
return __this_address;
375392
if (di_nextents)
376393
return __this_address;
377394
break;

0 commit comments

Comments
 (0)