Skip to content

Commit afca6c5

Browse files
Dave Chinnerdjwong
Dave Chinner
authored andcommitted
xfs: validate cached inodes are free when allocated
A recent fuzzed filesystem image cached random dcache corruption when the reproducer was run. This often showed up as panics in lookup_slow() on a null inode->i_ops pointer when doing pathwalks. BUG: unable to handle kernel NULL pointer dereference at 0000000000000000 .... Call Trace: lookup_slow+0x44/0x60 walk_component+0x3dd/0x9f0 link_path_walk+0x4a7/0x830 path_lookupat+0xc1/0x470 filename_lookup+0x129/0x270 user_path_at_empty+0x36/0x40 path_listxattr+0x98/0x110 SyS_listxattr+0x13/0x20 do_syscall_64+0xf5/0x280 entry_SYSCALL_64_after_hwframe+0x42/0xb7 but had many different failure modes including deadlocks trying to lock the inode that was just allocated or KASAN reports of use-after-free violations. The cause of the problem was a corrupt INOBT on a v4 fs where the root inode was marked as free in the inobt record. Hence when we allocated an inode, it chose the root inode to allocate, found it in the cache and re-initialised it. We recently fixed a similar inode allocation issue caused by inobt record corruption problem in xfs_iget_cache_miss() in commit ee45700 ("xfs: catch inode allocation state mismatch corruption"). This change adds similar checks to the cache-hit path to catch it, and turns the reproducer into a corruption shutdown situation. Reported-by: Wen Xu <wen.xu@gatech.edu> Signed-Off-By: Dave Chinner <dchinner@redhat.com> Reviewed-by: Christoph Hellwig <hch@lst.de> Reviewed-by: Carlos Maiolino <cmaiolino@redhat.com> Reviewed-by: Darrick J. Wong <darrick.wong@oracle.com> [darrick: fix typos in comment] Signed-off-by: Darrick J. Wong <darrick.wong@oracle.com>
1 parent 75bc37f commit afca6c5

File tree

1 file changed

+48
-25
lines changed

1 file changed

+48
-25
lines changed

Diff for: fs/xfs/xfs_icache.c

+48-25
Original file line numberDiff line numberDiff line change
@@ -308,6 +308,46 @@ xfs_reinit_inode(
308308
return error;
309309
}
310310

311+
/*
312+
* If we are allocating a new inode, then check what was returned is
313+
* actually a free, empty inode. If we are not allocating an inode,
314+
* then check we didn't find a free inode.
315+
*
316+
* Returns:
317+
* 0 if the inode free state matches the lookup context
318+
* -ENOENT if the inode is free and we are not allocating
319+
* -EFSCORRUPTED if there is any state mismatch at all
320+
*/
321+
static int
322+
xfs_iget_check_free_state(
323+
struct xfs_inode *ip,
324+
int flags)
325+
{
326+
if (flags & XFS_IGET_CREATE) {
327+
/* should be a free inode */
328+
if (VFS_I(ip)->i_mode != 0) {
329+
xfs_warn(ip->i_mount,
330+
"Corruption detected! Free inode 0x%llx not marked free! (mode 0x%x)",
331+
ip->i_ino, VFS_I(ip)->i_mode);
332+
return -EFSCORRUPTED;
333+
}
334+
335+
if (ip->i_d.di_nblocks != 0) {
336+
xfs_warn(ip->i_mount,
337+
"Corruption detected! Free inode 0x%llx has blocks allocated!",
338+
ip->i_ino);
339+
return -EFSCORRUPTED;
340+
}
341+
return 0;
342+
}
343+
344+
/* should be an allocated inode */
345+
if (VFS_I(ip)->i_mode == 0)
346+
return -ENOENT;
347+
348+
return 0;
349+
}
350+
311351
/*
312352
* Check the validity of the inode we just found it the cache
313353
*/
@@ -357,12 +397,12 @@ xfs_iget_cache_hit(
357397
}
358398

359399
/*
360-
* If lookup is racing with unlink return an error immediately.
400+
* Check the inode free state is valid. This also detects lookup
401+
* racing with unlinks.
361402
*/
362-
if (VFS_I(ip)->i_mode == 0 && !(flags & XFS_IGET_CREATE)) {
363-
error = -ENOENT;
403+
error = xfs_iget_check_free_state(ip, flags);
404+
if (error)
364405
goto out_error;
365-
}
366406

367407
/*
368408
* If IRECLAIMABLE is set, we've torn down the VFS inode already.
@@ -485,29 +525,12 @@ xfs_iget_cache_miss(
485525

486526

487527
/*
488-
* If we are allocating a new inode, then check what was returned is
489-
* actually a free, empty inode. If we are not allocating an inode,
490-
* the check we didn't find a free inode.
528+
* Check the inode free state is valid. This also detects lookup
529+
* racing with unlinks.
491530
*/
492-
if (flags & XFS_IGET_CREATE) {
493-
if (VFS_I(ip)->i_mode != 0) {
494-
xfs_warn(mp,
495-
"Corruption detected! Free inode 0x%llx not marked free on disk",
496-
ino);
497-
error = -EFSCORRUPTED;
498-
goto out_destroy;
499-
}
500-
if (ip->i_d.di_nblocks != 0) {
501-
xfs_warn(mp,
502-
"Corruption detected! Free inode 0x%llx has blocks allocated!",
503-
ino);
504-
error = -EFSCORRUPTED;
505-
goto out_destroy;
506-
}
507-
} else if (VFS_I(ip)->i_mode == 0) {
508-
error = -ENOENT;
531+
error = xfs_iget_check_free_state(ip, flags);
532+
if (error)
509533
goto out_destroy;
510-
}
511534

512535
/*
513536
* Preload the radix tree so we can insert safely under the

0 commit comments

Comments
 (0)