xfs: validate log record version against superblock log version
PATCH v6 1/1 xfs: validate log record version against superblock log version
Syzbot creates a fuzzed record where xfs_has_logv2() but the xlog_rec_header h_version != XLOG_VERSION_2. This causes a
KASAN: slab-out-of-bounds read in xlog_do_recovery_pass() ->
xlog_recover_process() -> xlog_cksum().
Fix by adding a check to xlog_valid_rec_header() to abort journal recovery if the xlog_rec_header h_version does not match the super block log version.
A file system with a version 2 log will only ever set XLOG_VERSION_2 in its headers (and v1 will only ever set V_1), so if there is any mismatch, either the journal or the superblock has been corrupted and therefore we abort processing with a -EFSCORRUPTED error immediately.
Also, refactor the structure of the validity checks for better readability. At the default error level (LOW), XFS_IS_CORRUPT() emits the condition that failed, the file and line number it is located at, then dumps the stack. This gives us everything we need to know about the failure if we do a single validity check per XFS_IS_CORRUPT().
---
Changelog
v1 -> v2:
- reject the mount for h_size > XLOG_HEADER_CYCLE_SIZE && !XLOG_VERSION_2 v2 -> v3:
- abort journal recovery if the xlog_rec_header h_version does not match the super block log version
v3 -> v4:
- refactor for readability
v4 -> v5:
- stop pretending h_version is a bitmap, remove check using XLOG_VERSION_OKBITS
v5 -> v6:
- added Reviewed-by tags
It seems that this patch has fallen through the cracks, so I have resend'd with the Reviewed-by tags.
Link to original thread:
https://lore.kernel.org/all/[email protected]/
fs/xfs/xfs_log_recover.c | 27 ++++++++++++++++----------- 1 file changed, 16 insertions(+), 11 deletions(-)
fs/xfs/xfs_log_recover.c +16 -11
@@ -2953,18 +2953,23 @@ xlog_valid_rec_header( xfs_daddr_t blkno, int bufsize) { +struct xfs_mount *mp = log->l_mp; +u32 h_version =be32_to_cpu (rhead->h_version);int hlen; -if (XFS_IS_CORRUPT (log->l_mp, +if (XFS_IS_CORRUPT (mp, rhead->h_magicno !=cpu_to_be32 (XLOG_HEADER_MAGIC_NUM )))return -EFSCORRUPTED ; -if (XFS_IS_CORRUPT (log->l_mp, - (!rhead->h_version || - (be32_to_cpu (rhead->h_version) & - (~XLOG_VERSION_OKBITS ))))) { -xfs_warn (log->l_mp,"%s: unrecognised log version (%d)." , - __func__,be32_to_cpu (rhead->h_version)); -return -EFSCORRUPTED ; + +/* +* The log version must match the superblock +*/ +if (xfs_has_logv2 (mp)) { +if (XFS_IS_CORRUPT (mp, h_version !=XLOG_VERSION_2 )) +return -EFSCORRUPTED ; + }else { +if (XFS_IS_CORRUPT (mp, h_version !=XLOG_VERSION_1 )) +return -EFSCORRUPTED ; }/*
@@ -2972,12 +2977,12 @@ xlog_valid_rec_header(* and h_len must not be greater than LR buffer size. */ hlen =be32_to_cpu (rhead->h_len); -if (XFS_IS_CORRUPT (log->l_mp, hlen <=0 || hlen > bufsize)) +if (XFS_IS_CORRUPT (mp, hlen <=0 || hlen > bufsize))return -EFSCORRUPTED ; -if (XFS_IS_CORRUPT (log->l_mp, - blkno > log->l_logBBsize || blkno >INT_MAX )) +if (XFS_IS_CORRUPT (mp, blkno > log->l_logBBsize || blkno >INT_MAX ))return -EFSCORRUPTED ; +return 0 ; }
Signature · 1 line
2.43.0
Carlos Maiolino
maintainerQuoted 44 lines · Raphael Pinsonneault-Thibeault
Syzbot creates a fuzzed record where
xfs_has_logv2()but thexlog_rec_headerh_version!=XLOG_VERSION_2. This causes aKASAN: slab-out-of-bounds read in xlog_do_recovery_pass() ->
xlog_recover_process()->xlog_cksum().Fix by adding a check to
xlog_valid_rec_header()to abort journal recovery if thexlog_rec_headerh_versiondoes not match the super block log version.A file system with a version 2 log will only ever set
XLOG_VERSION_2in its headers (and v1 will only ever setV_1), so if there is any mismatch, either the journal or the superblock has been corrupted and therefore we abort processing with a-EFSCORRUPTEDerror immediately.Also, refactor the structure of the validity checks for better readability. At the default error level (LOW),
XFS_IS_CORRUPT()emits the condition that failed, the file and line number it is located at, then dumps the stack. This gives us everything we need to know about the failure if we do a single validity check perXFS_IS_CORRUPT().Reported-by [email protected]Tested-by [email protected]Fixes 45cf976008dd ("xfs: fix log recovery buffer allocation for the legacy h_size fixup")Signed-off-by Raphael Pinsonneault-Thibeault <[email protected]>Reviewed-by "Darrick J. Wong" <[email protected]>Reviewed-by Christoph Hellwig <[email protected]>---
Changelog
v1 -> v2:
- reject the mount forh_size>XLOG_HEADER_CYCLE_SIZE&& !XLOG_VERSION_2 v2 -> v3:
- abort journal recovery if thexlog_rec_headerh_versiondoes not match the super block log version
v3 -> v4:
- refactor for readability
v4 -> v5:
- stop pretendingh_versionis a bitmap, remove check usingXLOG_VERSION_OKBITS
v5 -> v6:
- added Reviewed-by tags
Quoted 4 lines
It seems that this patch has fallen through the cracks, so I have resend'd with the Reviewed-by tags.
Link to original thread:
https://lore.kernel.org/all/[email protected]/
Yes, thanks, please avoid sending new versions "In-Reply-To" old ones, that tends to hide new versions.
Also I had issues with my (now old) email provider.
I'll queue this one for 7.0
Quoted 60 lines
fs/xfs/xfs_log_recover.c | 27 ++++++++++++++++----------- 1 file changed, 16 insertions(+), 11 deletions(-)fs/xfs/xfs_log_recover.c +16
-11@@ -2953,18 +2953,23 @@ xlog_valid_rec_header( xfs_daddr_t blkno, int bufsize) { +struct xfs_mount *mp = log->l_mp; +u32 h_version =be32_to_cpu (rhead->h_version);int hlen; -if (XFS_IS_CORRUPT (log->l_mp, +if (XFS_IS_CORRUPT (mp, rhead->h_magicno !=cpu_to_be32 (XLOG_HEADER_MAGIC_NUM )))return -EFSCORRUPTED ; -if (XFS_IS_CORRUPT (log->l_mp, - (!rhead->h_version || - (be32_to_cpu (rhead->h_version) & - (~XLOG_VERSION_OKBITS ))))) { -xfs_warn (log->l_mp,"%s: unrecognised log version (%d)." , - __func__,be32_to_cpu (rhead->h_version)); -return -EFSCORRUPTED ; + +/* +* The log version must match the superblock +*/ +if (xfs_has_logv2 (mp)) { +if (XFS_IS_CORRUPT (mp, h_version !=XLOG_VERSION_2 )) +return -EFSCORRUPTED ; + }else { +if (XFS_IS_CORRUPT (mp, h_version !=XLOG_VERSION_1 )) +return -EFSCORRUPTED ; }/* @@ -2972,12 +2977,12 @@ xlog_valid_rec_header(* and h_len must not be greater than LR buffer size. */ hlen =be32_to_cpu (rhead->h_len); -if (XFS_IS_CORRUPT (log->l_mp, hlen <=0 || hlen > bufsize)) +if (XFS_IS_CORRUPT (mp, hlen <=0 || hlen > bufsize))return -EFSCORRUPTED ; -if (XFS_IS_CORRUPT (log->l_mp, - blkno > log->l_logBBsize || blkno >INT_MAX )) +if (XFS_IS_CORRUPT (mp, blkno > log->l_logBBsize || blkno >INT_MAX ))return -EFSCORRUPTED ; +return 0 ; }Signature · 1 line
2.43.0
Quoted 10 lines · Raphael Pinsonneault-Thibeault
Syzbot creates a fuzzed record where
xfs_has_logv2()but thexlog_rec_headerh_version!=XLOG_VERSION_2. This causes aKASAN: slab-out-of-bounds read in xlog_do_recovery_pass() ->
xlog_recover_process()->xlog_cksum().Fix by adding a check to
xlog_valid_rec_header()to abort journal recovery if thexlog_rec_headerh_versiondoes not match the super block log version.[...]
Applied to for-next, thanks!
[1/1] xfs: validate log record version against superblock log version
commit: 44b9553c3dd043f14903d8ae5d4e7a9797c6d92e
Best regards,
Signature · 1 line
Carlos Maiolino <[email protected]>