xfs: add a log item verification layer to recovery
v3 cover xfs: add a log item verification layer to recovery
Log recovery rebuilds each log item from the ophdr regions in the journal and then casts those regions back to their format structures to drive replay. The region layout it trusts to do this - the item type, the region count, and the region sizes - all come from the log, so a crafted or corrupt image can present a malformed item that recovery dereferences. The reported case is an XFS_LI_INODE item that declares two regions but logs only one, leaving ri_buf[1] NULL and faulting mount-time recovery in xlog_recover_inode_commit_pass2().
v2 rejected an item whose logged region count did not reach the declared count with a single check in xlog_recover_commit_trans(). As Dave pointed out, that only found the one missing check and left the comprehensive verification of the recovered item structure undone, at the wrong layer. v3 instead verifies each item where it is first decoded from the log, in xlog_recover_add_to_trans(), before that data is used to size, build, decode or replay anything:
- When an item header is decoded, resolve the item type and bound the region count it declares within the minimum and maximum a log item of that type is formatted with. The upper bound keeps the region array from being oversized; the lower bound stops an item that declares fewer regions than its replay code indexes, e.g. a dquot with qlf_size == 1 that still reaches ri_buf[1]. Reject a continuation that has no region to extend.
- When an item is fully decoded (the next item's header arrives, or the commit record does), confirm it received all its declared regions and run the item type's verifier, if it has one.
- Add the first per-type verifier, for inode items, and drop the equivalent open-coded checks that were scattered through the inode pass2 path.
The generic checks bound and complete the region layout - the item type, the region count within a per-type [min, max], and that every declared region arrived - for every item type. The per-region size and structure checks are type-specific and live in the ->verify method; this series adds the inode verifier, and the other types can grow their own the same way.
Not addressed here are the ->verify checks for the remaining types, including the non-header region size checks and the content-driven indices that the region count alone does not bound. Two such cases are known: the btree-root fork formats convert from a larger in-core form on replay, so their region size does not bound bb_numrecs in xfs_bmbt_to_bmdr() and the rt converters; and buffer replay walks ri_buf[] by the blf_data_map bit runs rather than by the region count. A buffer ->verify and a bb_numrecs clamp are left as follow-ups.
v3:
- Reworked from the single completeness check in xlog_recover_commit_trans() into a verification layer at the region-assembly boundary in xlog_recover_add_to_trans(), as Dave
suggested: a header check at decode time (item type known, region count within [min, max]) and a completeness/structure check when the item is fully decoded, with per-type verifiers hanging off xlog_recover_item_ops. Added the inode verifier and a continuation underflow guard.
v2:
- Dropped the piecemeal per-check validation in the inode pass2 path.
Weiming Shi (3):
xfs: verify log item headers when they are decoded during recovery
xfs: verify recovered log items are complete before replaying them
xfs: add an inode log item recovery verifier
fs/xfs/libxfs/xfs_log_recover.h | 17 +++++ fs/xfs/xfs_attr_item.c | 4 ++ fs/xfs/xfs_bmap_item.c | 4 ++ fs/xfs/xfs_dquot_item_recover.c | 4 ++ fs/xfs/xfs_exchmaps_item.c | 4 ++ fs/xfs/xfs_extfree_item.c | 8 +++ fs/xfs/xfs_icreate_item.c | 2 + fs/xfs/xfs_inode_item_recover.c | 89 ++++++++++++++++++----- fs/xfs/xfs_log_recover.c | 121 +++++++++++++++++++++++--------- fs/xfs/xfs_refcount_item.c | 8 +++ fs/xfs/xfs_rmap_item.c | 8 +++ 11 files changed, 221 insertions(+), 48 deletions(-)
base-commit: 8c13415c8a4383447c21ec832b20b3b283f0e01a
Signature · 1 line
2.43.0
PATCH v3 1/3 xfs: verify log item headers when they are decoded during recovery
Log recovery rebuilds each log item in xlog_recover_add_to_trans() from the ophdr regions in the journal. The first region carries the item's format header, and recovery reads the type from its first two bytes and the region count from the next two to size and populate the item's ri_buf[] array. Those regions are later cast back to their format structures and used to drive replay. This is the first point at which recovery trusts data read from the log, and the geometry it reads here is used to build the structures every later check and decode step relies on, so the header should be verified comprehensively here rather than trusted until something dereferences it.
Do that in xlog_recover_verify_item_header(): require the region to be large enough to hold the type and count fields before they are read, resolve the item type against the known xlog_recover_item_ops (rejecting unrecognised types), and bound the declared region count within the minimum and maximum a log item of that type is formatted with. A lower bound matters as much as an upper one: an item that declares fewer regions than its replay code indexes still passes the completeness check added later in the series and then reads past its ri_buf[] array, e.g. a dquot item with qlf_size == 1 still reaches ri_buf[1] in xlog_recover_dquot_commit_pass2(). Record both bounds in new min_regions and max_regions fields on xlog_recover_item_ops, where a max_regions of 0 keeps the generic XLOG_MAX_REGIONS_IN_ITEM ceiling used by buffer items. Resolving the type here also lets us set item->ri_ops at decode time, so xlog_recover_reorder_trans() no longer has to look it up and can no longer meet an unrecognised item.
While decoding the continuation of a region in xlog_recover_add_to_cont_trans(), also reject a continuation that has no started region to extend, instead of indexing ri_buf[-1] on a never-populated item.
--- fs/xfs/libxfs/xfs_log_recover.h | 8 ++++ fs/xfs/xfs_attr_item.c | 4 ++ fs/xfs/xfs_bmap_item.c | 4 ++ fs/xfs/xfs_dquot_item_recover.c | 4 ++ fs/xfs/xfs_exchmaps_item.c | 4 ++ fs/xfs/xfs_extfree_item.c | 8 ++++ fs/xfs/xfs_icreate_item.c | 2 + fs/xfs/xfs_inode_item_recover.c | 2 + fs/xfs/xfs_log_recover.c | 80 ++++++++++++++++++++------------- fs/xfs/xfs_refcount_item.c | 8 ++++ fs/xfs/xfs_rmap_item.c | 8 ++++ 11 files changed, 101 insertions(+), 31 deletions(-)
fs/xfs/libxfs/xfs_log_recover.h +8 -0
@@ -24,6 +24,14 @@ enum xlog_recover_reorder {struct xlog_recover_item_ops {uint16_t item_type;/* XFS_LI_* type code. */ +/* +* Bounds on the item's declared region count, checked before the +* region array is allocated. max_regions 0 means no fixed maximum, so +* the generic XLOG_MAX_REGIONS_IN_ITEM ceiling applies (buffers). +*/ +unsigned int min_regions; +unsigned int max_regions; +/* * Help sort recovered log items into the order required to replay them * correctly. Log item types that always use XLOG_REORDER_ITEM_LIST do
fs/xfs/xfs_attr_item.c +4 -0
@@ -1189,6 +1189,8 @@ static const struct xfs_item_ops xfs_attri_item_ops = {const struct xlog_recover_item_ops xlog_attri_item_ops = { .item_type =XFS_LI_ATTRI , + .max_regions =5 , + .min_regions =2 , .commit_pass2 = xlog_recover_attri_commit_pass2, };
@@ -1203,5 +1205,7 @@ static const struct xfs_item_ops xfs_attrd_item_ops = {const struct xlog_recover_item_ops xlog_attrd_item_ops = { .item_type =XFS_LI_ATTRD , + .max_regions =1 , + .min_regions =1 , .commit_pass2 = xlog_recover_attrd_commit_pass2, };
fs/xfs/xfs_bmap_item.c +4 -0
@@ -684,6 +684,8 @@ xlog_recover_bui_commit_pass2(const struct xlog_recover_item_ops xlog_bui_item_ops = { .item_type =XFS_LI_BUI , + .max_regions =1 , + .min_regions =1 , .commit_pass2 = xlog_recover_bui_commit_pass2, };
@@ -716,5 +718,7 @@ xlog_recover_bud_commit_pass2(const struct xlog_recover_item_ops xlog_bud_item_ops = { .item_type =XFS_LI_BUD , + .max_regions =1 , + .min_regions =1 , .commit_pass2 = xlog_recover_bud_commit_pass2, };
fs/xfs/xfs_dquot_item_recover.c +4 -0
@@ -178,6 +178,8 @@ xlog_recover_dquot_commit_pass2(const struct xlog_recover_item_ops xlog_dquot_item_ops = { .item_type =XFS_LI_DQUOT , + .max_regions =2 , + .min_regions =2 , .ra_pass2 = xlog_recover_dquot_ra_pass2, .commit_pass2 = xlog_recover_dquot_commit_pass2, };
@@ -211,6 +213,8 @@ xlog_recover_quotaoff_commit_pass1(const struct xlog_recover_item_ops xlog_quotaoff_item_ops = { .item_type =XFS_LI_QUOTAOFF , + .max_regions =1 , + .min_regions =1 , .commit_pass1 = xlog_recover_quotaoff_commit_pass1,/* nothing to commit in pass2 */ };
fs/xfs/xfs_exchmaps_item.c +4 -0
@@ -576,6 +576,8 @@ xlog_recover_xmi_commit_pass2(const struct xlog_recover_item_ops xlog_xmi_item_ops = { .item_type =XFS_LI_XMI , + .max_regions =1 , + .min_regions =1 , .commit_pass2 = xlog_recover_xmi_commit_pass2, };
@@ -607,5 +609,7 @@ xlog_recover_xmd_commit_pass2(const struct xlog_recover_item_ops xlog_xmd_item_ops = { .item_type =XFS_LI_XMD , + .max_regions =1 , + .min_regions =1 , .commit_pass2 = xlog_recover_xmd_commit_pass2, };
fs/xfs/xfs_extfree_item.c +8 -0
@@ -889,6 +889,8 @@ xlog_recover_efi_commit_pass2(const struct xlog_recover_item_ops xlog_efi_item_ops = { .item_type =XFS_LI_EFI , + .max_regions =1 , + .min_regions =1 , .commit_pass2 = xlog_recover_efi_commit_pass2, };
@@ -941,6 +943,8 @@ xlog_recover_rtefi_commit_pass2(const struct xlog_recover_item_ops xlog_rtefi_item_ops = { .item_type =XFS_LI_EFI_RT , + .max_regions =1 , + .min_regions =1 , .commit_pass2 = xlog_recover_rtefi_commit_pass2, };
@@ -984,6 +988,8 @@ xlog_recover_efd_commit_pass2(const struct xlog_recover_item_ops xlog_efd_item_ops = { .item_type =XFS_LI_EFD , + .max_regions =1 , + .min_regions =1 , .commit_pass2 = xlog_recover_efd_commit_pass2, };
@@ -1025,5 +1031,7 @@ xlog_recover_rtefd_commit_pass2(const struct xlog_recover_item_ops xlog_rtefd_item_ops = { .item_type =XFS_LI_EFD_RT , + .max_regions =1 , + .min_regions =1 , .commit_pass2 = xlog_recover_rtefd_commit_pass2, };
fs/xfs/xfs_icreate_item.c +2 -0
@@ -255,6 +255,8 @@ xlog_recover_icreate_commit_pass2(const struct xlog_recover_item_ops xlog_icreate_item_ops = { .item_type =XFS_LI_ICREATE , + .max_regions =1 , + .min_regions =1 , .reorder = xlog_recover_icreate_reorder, .commit_pass2 = xlog_recover_icreate_commit_pass2, };
fs/xfs/xfs_inode_item_recover.c +2 -0
@@ -599,6 +599,8 @@ xlog_recover_inode_commit_pass2(const struct xlog_recover_item_ops xlog_inode_item_ops = { .item_type =XFS_LI_INODE , + .max_regions =4 , + .min_regions =2 , .ra_pass2 = xlog_recover_inode_ra_pass2, .commit_pass2 = xlog_recover_inode_commit_pass2, };
fs/xfs/xfs_log_recover.c +49 -31
145 lines not on this page · raw message
fs/xfs/xfs_refcount_item.c +8 -0
@@ -741,6 +741,8 @@ xlog_recover_cui_commit_pass2(const struct xlog_recover_item_ops xlog_cui_item_ops = { .item_type =XFS_LI_CUI , + .max_regions =1 , + .min_regions =1 , .commit_pass2 = xlog_recover_cui_commit_pass2, };
@@ -796,6 +798,8 @@ xlog_recover_rtcui_commit_pass2(const struct xlog_recover_item_ops xlog_rtcui_item_ops = { .item_type =XFS_LI_CUI_RT , + .max_regions =1 , + .min_regions =1 , .commit_pass2 = xlog_recover_rtcui_commit_pass2, };
@@ -828,6 +832,8 @@ xlog_recover_cud_commit_pass2(const struct xlog_recover_item_ops xlog_cud_item_ops = { .item_type =XFS_LI_CUD , + .max_regions =1 , + .min_regions =1 , .commit_pass2 = xlog_recover_cud_commit_pass2, };
@@ -858,5 +864,7 @@ xlog_recover_rtcud_commit_pass2(const struct xlog_recover_item_ops xlog_rtcud_item_ops = { .item_type =XFS_LI_CUD_RT , + .max_regions =1 , + .min_regions =1 , .commit_pass2 = xlog_recover_rtcud_commit_pass2, };
fs/xfs/xfs_rmap_item.c +8 -0
@@ -770,6 +770,8 @@ xlog_recover_rui_commit_pass2(const struct xlog_recover_item_ops xlog_rui_item_ops = { .item_type =XFS_LI_RUI , + .max_regions =1 , + .min_regions =1 , .commit_pass2 = xlog_recover_rui_commit_pass2, };
@@ -825,6 +827,8 @@ xlog_recover_rtrui_commit_pass2(const struct xlog_recover_item_ops xlog_rtrui_item_ops = { .item_type =XFS_LI_RUI_RT , + .max_regions =1 , + .min_regions =1 , .commit_pass2 = xlog_recover_rtrui_commit_pass2, };
@@ -857,6 +861,8 @@ xlog_recover_rud_commit_pass2(const struct xlog_recover_item_ops xlog_rud_item_ops = { .item_type =XFS_LI_RUD , + .max_regions =1 , + .min_regions =1 , .commit_pass2 = xlog_recover_rud_commit_pass2, };
@@ -887,5 +893,7 @@ xlog_recover_rtrud_commit_pass2(const struct xlog_recover_item_ops xlog_rtrud_item_ops = { .item_type =XFS_LI_RUD_RT , + .max_regions =1 , + .min_regions =1 , .commit_pass2 = xlog_recover_rtrud_commit_pass2, };
Signature · 1 line
2.43.0
PATCH v3 2/3 xfs: verify recovered log items are complete before replaying them
In reply to Weiming Shixlog_recover_add_to_trans() assembles each recovered log item into an ri_buf[] of ri_total slots, where ri_total is the region count the item's format header declared, and counts the regions actually logged in ri_cnt. Nothing checked that ri_cnt reached ri_total once the item was fully decoded, so a crafted or truncated log can present an item whose header declares more regions than were logged. The trailing ri_buf[] slots stay NULL, and the reorder, readahead and replay code then dereference them.
For example, an XFS_LI_INODE item declaring two regions but logging only the format region leaves ri_buf[1] NULL, and mount-time recovery faults dereferencing it as the log dinode:
KASAN: null-ptr-deref in range [0x0000000000000000-0x0000000000000007] RIP: xlog_recover_inode_commit_pass2 (fs/xfs/xfs_inode_item_recover.c:370) Call Trace: xlog_recover_items_pass2 (fs/xfs/xfs_log_recover.c:2011) xlog_recover_commit_trans (fs/xfs/xfs_log_recover.c:2078) xlog_recovery_process_trans (fs/xfs/xfs_log_recover.c:2328) xlog_recover_process_data (fs/xfs/xfs_log_recover.c:2502) xlog_recover (fs/xfs/xfs_log_recover.c:3486) xfs_log_mount (fs/xfs/xfs_log.c:667) xfs_mountfs (fs/xfs/xfs_mount.c:1039) xfs_fs_fill_super (fs/xfs/xfs_super.c:1965) get_tree_bdev_flags (fs/super.c:1680) __x64_sys_mount (fs/namespace.c:4433)
An item is fully decoded once every region its header declared has arrived. That happens when the next item's header starts (the current item is closed out in xlog_recover_add_to_trans()) or, for the last item in the transaction, when the commit record arrives (xlog_recover_commit_trans()). Verify the item at both of those points with xlog_recover_verify_item(): confirm it received all its declared regions, and run the item type's verifier if one is provided. Item types opt in with a new xlog_recover_item_ops->verify method; the first, for inode items, is added in the next patch.
--- fs/xfs/libxfs/xfs_log_recover.h | 9 ++++++++ fs/xfs/xfs_log_recover.c | 41 ++++++++++++++++++++++++++++++++- 2 files changed, 49 insertions(+), 1 deletion(-)
fs/xfs/libxfs/xfs_log_recover.h +9 -0
@@ -41,6 +41,15 @@ struct xlog_recover_item_ops {*/ enum xlog_recover_reorder (*reorder)(struct xlog_recover_item *item); +/* +* Comprehensively validate a fully decoded item's formatted log +* structures, if provided: the region count and sizes the item type +* requires, and any header fields that can be checked without the +* on-disk buffer. Called once the item is complete, before it is +* queued for replay. Returning an error aborts recovery. +*/ +int (*verify)(struct xlog *log,struct xlog_recover_item *item); +/* Start readahead for pass2, if provided. */ void (*ra_pass2)(struct xlog *log,struct xlog_recover_item *item);
fs/xfs/xfs_log_recover.c +40 -1
65 lines not on this page · raw message
Signature · 1 line
2.43.0
PATCH v3 3/3 xfs: add an inode log item recovery verifier
In reply to Weiming ShiThe previous patches let each log item type validate its own formatted structures through an xlog_recover_item_ops->verify method, run once the item is fully decoded. Add the first verifier, for inode items.
Log recovery previously validated a recovered inode item's structures inside the pass2 decode and replay code, one open-coded check at a time, which was hard to read and to audit for what was still unchecked. xlog_recover_inode_verify() instead checks in one place that the core and each fork region implied by ilf_fields is declared, that the log dinode is present and large enough, that its version matches the mount, that di_forkoff is within the literal area, and that the verbatim-copied fork regions fit their destination fork.
Because the log dinode checks now run before pass2, drop the equivalent open-coded checks (the log dinode magic and the dead di_forkoff bound) from xlog_recover_inode_commit_pass2(). The checks that need the on-disk inode buffer (its magic, the LSN and di_flushiter replay-ordering decisions, the di_mode/di_format consistency, and the final xfs_dinode_verify()) cannot be hoisted and stay in pass2.
This covers the self-contained log dinode structure. The btree-root fork formats are converted from a larger in-core form on replay and their record count is not bounded here yet; clamping xfs_bmbt_to_bmdr() and the rt btree converters against the destination fork is left as follow-up.
--- fs/xfs/xfs_inode_item_recover.c | 87 +++++++++++++++++++++++++++------ 1 file changed, 71 insertions(+), 16 deletions(-)
fs/xfs/xfs_inode_item_recover.c +71 -16
@@ -367,13 +367,6 @@ xlog_recover_inode_commit_pass2( goto out_release; } ldip = item->ri_buf[1 ].iov_base; -if (XFS_IS_CORRUPT (mp, ldip->di_magic !=XFS_DINODE_MAGIC )) { -xfs_alert (mp, -"%s: Bad inode log record, rec ptr " PTR_FMT ", ino %lld" , - __func__, item, in_f->ilf_ino); - error = -EFSCORRUPTED ; - goto out_release; - }/* * If the inode has an LSN in it, recover the inode only if the on-disk
@@ -462,15 +455,6 @@ xlog_recover_inode_commit_pass2(if (error) goto out_release; -if (unlikely (ldip->di_forkoff > mp->m_sb.sb_inodesize)) { -XFS_CORRUPTION_ERROR ("Bad log dinode fork offset" , -XFS_ERRLEVEL_LOW , mp, ldip,sizeof (*ldip)); -xfs_alert (mp, -"Bad inode 0x%llx, di_forkoff 0x%x" , - in_f->ilf_ino, ldip->di_forkoff); - error = -EFSCORRUPTED ; - goto out_release; - } isize =xfs_log_dinode_size (mp);if (unlikely (item->ri_buf[1 ].iov_len > isize)) { XFS_CORRUPTION_ERROR("Bad log dinode size", XFS_ERRLEVEL_LOW,
@@ -597,10 +581,81 @@ xlog_recover_inode_commit_pass2(return error; } +/* +* Validate the log dinode and fork regions of a decoded inode item. Checks +* that need the on-disk inode buffer stay in xlog_recover_inode_commit_pass2(). +*/ +STATIC int +xlog_recover_inode_verify ( +struct xlog *log, +struct xlog_recover_item *item) +{ +struct xfs_mount *mp = log->l_mp; +struct xfs_inode_log_format *in_f; +struct xfs_inode_log_format in_f_buf; +struct xfs_log_dinode *ldip; +unsigned int litino =XFS_LITINO (mp); +unsigned int dsize, asize; +int attr_index; +int error; + +if (item->ri_buf[0 ].iov_len ==sizeof (struct xfs_inode_log_format )) { + in_f = item->ri_buf[0 ].iov_base; + }else { + in_f = &in_f_buf; + error =xfs_inode_item_format_convert (&item->ri_buf[0 ], in_f); +if (error) +return error; + } + +/* The inode core is always logged as the log dinode in ri_buf[1]. */ +if (XFS_IS_CORRUPT (mp, in_f->ilf_size <2 ) || +XFS_IS_CORRUPT (mp, + item->ri_buf[1 ].iov_len <xfs_log_dinode_size (mp))) +return -EFSCORRUPTED ; + + ldip = item->ri_buf[1 ].iov_base; +if (XFS_IS_CORRUPT (mp, ldip->di_magic !=XFS_DINODE_MAGIC ) || +XFS_IS_CORRUPT (mp, !xfs_dinode_good_version (mp, ldip->di_version)) || +XFS_IS_CORRUPT (mp, ldip->di_forkoff >= (litino >>3 ))) +return -EFSCORRUPTED ; + +if (ldip->di_forkoff) { + dsize = ldip->di_forkoff <<3 ; + asize = litino - (ldip->di_forkoff <<3 ); + }else { + dsize = litino; + asize =0 ; + } + +/* +* Btree-root forks are logged in a larger in-core form and converted on +* replay, so their region is not bounded by the on-disk fork size here. +*/ +if (in_f->ilf_fields &XFS_ILOG_DFORK ) { +if (XFS_IS_CORRUPT (mp, in_f->ilf_size <3 )) +return -EFSCORRUPTED ; +if ((in_f->ilf_fields &XFS_ILOG_DFORK ) !=XFS_ILOG_DBROOT && +XFS_IS_CORRUPT (mp, item->ri_buf[2 ].iov_len > dsize)) +return -EFSCORRUPTED ; + } +if (in_f->ilf_fields &XFS_ILOG_AFORK ) { + attr_index = (in_f->ilf_fields &XFS_ILOG_DFORK ) ?3 :2 ; +if (XFS_IS_CORRUPT (mp, in_f->ilf_size < attr_index +1 )) +return -EFSCORRUPTED ; +if ((in_f->ilf_fields &XFS_ILOG_AFORK ) !=XFS_ILOG_ABROOT && +XFS_IS_CORRUPT (mp, item->ri_buf[attr_index].iov_len > asize)) +return -EFSCORRUPTED ; + } + +return 0 ; +} +const struct xlog_recover_item_ops xlog_inode_item_ops = { .item_type =XFS_LI_INODE , .max_regions =4 , .min_regions =2 , + .verify = xlog_recover_inode_verify, .ra_pass2 = xlog_recover_inode_ra_pass2, .commit_pass2 = xlog_recover_inode_commit_pass2, };
Signature · 1 line
2.43.0