fs-verity support for XFS with post EOF merkle tree
Subsystems (9)
v20 cover fs-verity support for XFS with post EOF merkle tree
Hi all,
This series add fsverity support to XFS. This series include more changes to the common code:
- No-op'ing FS_VERITY_FL in FS_IOC_SETFLAGS ioctl()
- Minor fix of setting S_APPEND on inode with fsverity
- Runtime fsverity construction flag on inode
The second part of the patchset is fsverity support for XFS. This revision includes a bit more improvements to the xfs scrub/repair.
Testing of the common changes
-g verity passes for all fs, except btrfs/277 and btrfs+generic/574.
The btrfs/277 seems to fail due to difference between what kernel and userspace versions support (btrfs-progs v7.1 doesn't seem to support latest streams v3).
The generic/574 on BTRFS fails only for 1024 block size no matter the change (fails on v7.3 too). I haven't checked why.
Patches without review:
[PATCH v20 04/23] fs,fsverity: reject S_APPEND on fsverity inodes [PATCH v20 05/23] iomap: use fsverity_is_building() to check for [PATCH v20 08/23] f2fs: use I_VERITY_BUILDING instead of private [PATCH v20 13/23] xfs: handle fsverity I/O in write/read path [PATCH v20 16/23] xfs: don't remove written extents past EOF on [PATCH v20 17/23] xfs: add fs-verity support
[PATCH v20 21/23] xfs: check and repair the verity inode flag state
This series based on v7.4-rc4
kernel:
https://git.kernel.org/pub/scm/linux/kernel/git/aalbersh/xfs-linux.git/log/?h=fsverity
xfsprogs:
https://git.kernel.org/pub/scm/linux/kernel/git/aalbersh/xfsprogs-dev.git/log/?h=fsverity
xfstests:
https://git.kernel.org/pub/scm/linux/kernel/git/aalbersh/xfstests-dev.git/log/?h=fsverity
v19:
https://lore.kernel.org/fsverity/[email protected]/T/#t
v18:
https://lore.kernel.org/fsverity/[email protected]/T/#t
v17:
https://lore.kernel.org/fsverity/[email protected]/T/#u
v16:
https://lore.kernel.org/fsverity/[email protected]/T/#t
v15:
https://lore.kernel.org/fsverity/[email protected]/T/#t
To: [email protected]
To: [email protected]
To: Carlos Maiolino <[email protected]>
To: [email protected]
To: [email protected]
---
Changes in v20:
- Merge with common fsverity code changes patchset
- Add a fix for S_APPEND
- Comments and commit message updates
- Convert WARN in writeback into strict check for folio position
- In xfs scrub/repair zap fsverity metadata on fsverity flag removal
- Treat EINVAL and EFBIG as reason to repair fsverity
- xfs scrub/repair detect invalid fsverity flag combinations on inode
- Refactor xfs_free_eofblocks() to not clear EOF tag on fsverity inode
- Don't treat buf_size smaller than desc_size as a corruption
- Add a corruption check for too small desc_size
Changes in v19:
- Add guard to forbid use on Zoned devices
- Add a guard against using with LBS (compound pages no-go for fsverity)
- move IOMAP_F_FSVERITY to xfs_bmbt_to_iomap()
- Use I_VERITY_BUILDING instead of XFS inode flag
- Add guard against DAX during fsverity enable
- Change XFS_FSVERITY_LARGEST_FILE to macro for 32-bit kernel check
- Forbid opening too large fsverity files on 32-bit kernels
- xfs_free_eofblocks() doesn't depend on I_VERITY (disk flag instead)
- Keep EOF cleanup tag on inode if fsverity build failed
- Invalidate past EOF page cache in scrub on corrupted inodes
- Remove past EOF extents in scrub on corrupted inodes
Changes in v18:
- Rebase to latest origin/for-next and drop core fsverity patches
- Move truncate_inode_pages() before xfs_trans_alloc()
- Move xfs_fsverity_metadata_offset() to libxfs as used in xfs_bmap.c
Changes in v17:
- Add mempool for fsverity ioends cache
- Add xfs_fsverity_reset_inode() as a common clean up function
- Changed ILOCK_SHARED to ILOCK_EXCL for xfs_bmap_last_extent()
- Swap order of truncate_inode_pages() and ilock() due to ABBA
- Add xfs_fsverity_is_hashes_of_zeroed_blocks() helper
- Removed EINVAL and EFBIG from scrub as ambiguous
Changes in v16:
- Rebase to block/for-next
- Removed work_struct from ioend in favor of kmem_cache structs
- Minor adjustments from v15 review
- Skip written extents in lower level of __xfs_bunmapi()
Changes in v15:
- Pull BIO in task context patches
- Drop flag argument in xfs_free_eofblocks()
- Call xfs_free_eofblocks() on fsverity inodes
- Comments and commit messages updates
- Rebased to v7.2-rc7
- Dropped patch for fsverity_fill_zerohash() with highmem optimization
Changes in v14:
- Rebase to lazy-bounce@hch-misc
- Adjust read ioends to BIO in task context flow
- MMAPLOCK/sb_internal deadlock fix reported by sashiko
- Add missing delalloc clean up in verity_end_enable
- Use xfs_free_eofblocks() instead of writing own clean up routine
- Modify xfs_free_eofblocks() to be able to clean unwritten only
- Dropped fix patch for truncate/set_size check
- Various minor code and #include rearrangements for bisecting
Changes in v13:
- Hoisted statx reporting to common code
- Added read ioend sorted for worker self-deadlock fix
- Adjusted fsverity flags in zoned write path
Changes in v12:
- Refactored xfs_fsverity_cancel_unwritten()
- Switched to using inode_set_flags()
- Add a lock for COW fork reading
- Add pagecache truncation in cleanup path
- Added ERANGE and EBADMSG to fsverity scrub handling
- Add missing XFS_FSVERITY_CONSTRUCTION in various xfs_iomap
- Rebase to -rc3
Changes in v11:
- Drop wrong overlayfs patch
- Drop already merged iomap and fsverity patches
- Update to I_INO() use instead of ip->i_ino
- Sashiko.dev fixes. See list of issues below.
Changes in v10:
- Rebase to v7.1-rc3 with relevant adjustments
- Initialize ioend->io_vi to NULL to not get write work onto verity wq
- Range diff below
Changes in v9:
- Fix fsverity_fill_zerohash() parameter names
- A few fixes found by sashiko.dev:
- Replace ip->i_mount->m_attr_geo->blksize with m_sb.sb_blocksize
- Don't call xfs_trans_cancel() after xfs_trans_commit() in xfs_fsverity_end_enable()
- Call xfs_fsverity_delete_metadata() if verity enable failed
- Change start/end type from xfs_fileoff_t to loff_t
- Return xfs_trans_commit() error from
xfs_fsverity_cancel_unwritten()
Changes in v8:
- Return fsverity_ensure_verity_info() errors from
ovl_ensure_verity_loaded()
Changes in v7:
- Move kerneldoc to fsverity_ensure_verity_info() definition
- Drop patch adding XFS traces
- Fix overly long line in the comment
- Make order of fserror and fsverity_error consistent
- Add overlay patch converting to fsverity_ensure_verity_info()
Changes in v6:
- Removed stub for fsverity_ensure_verity_info() as it's optimized out
- Rename fsverity_folio_zero_hash() to fsverify_fill_zerohash()
- Merge patches 8 to 10 into one
- Merge patch gerating zero_hash and fsverity_fill_zerohash() into one
- Add kerneldoc to fsverity_ensure_verity_info()
- Add comments to iomap_block_needs_zeroing()
Changes in v5:
- Add fserror_report_data_lost() for data blocks in page spanning EOF
- Issue fsverity metadata readahead in data readahead
- iomap_fsverity_write() return type fix
- Use of S_ISREG(mode)
- Make 65536 #define instead of open-coded
- Use transaction per unwritten extent removal
- Fetch fsverity_info for all fsverity metadata
- Revert fsverity_folio_zero_hash() stub as used in iomap
- Extend cancel_unwritten to whole file range to remove cow leftovers
- Drop delayed allocation on the COW fork on fsverity completion
Changes in v4:
- Use fserror interface in fsverity instead of fs callback
- Hoist pagecache_read from f2fs/ext4 to fsverity
- Refactor iomap code
- Fetch fsverity_info only for file data and merkle tree holes
- Do not disable preallocation, remove unwritten extents instead
- Offload fsverity hash I/O to fsverity workqueue in read path
- Store merkle tree at round_up(i_size, 64k)
- Add a spacing between merkle tree and fsverity descriptor as next 64k
aligned block
- Squash helpers into first user commits
- Squash on-disk format changes into single commit
- Drop different offset for pagecache/on-disk
- Don't zero out pages in higher order folios in write path
- Link to v3: https://lore.kernel.org/fsverity/[email protected]/T/#t
Changes in v3:
- Different on-disk and pagecache offset
- Use read path ioends
- Switch to hashtable fsverity info
- Synthesize merkle tree blocks full of zeroes
- Other minor refactors
- Link to v2: https://lore.kernel.org/fsverity/20260114164210.GO15583@frogsfrogsfrogs/T/#t
Changes in v2:
- Move to VFS interface for merkle tree block reading
- Drop patchset for per filesystem workqueues
- Change how offsets of the descriptor and tree metadata is calculated
- Store fs-verity descriptor in data fork side by side with merkle tree
- Simplify iomap changes, remove interface for post eof read/write
- Get rid of extended attribute implementation
- Link to v1: https://lore.kernel.org/r/[email protected]
Andrey Albershteyn (21):
fs: constify inode argument of inode_state_read_once()
fs: strip FS_VERITY_FL from FS_IOC_SETFLAGS fs,fsverity: add I_VERITY_BUILDING inode state flag fs,fsverity: reject S_APPEND on fsverity inodes
iomap: use fsverity_is_building() to check for fsverity IO
btrfs: use I_VERITY_BUILDING instead of private flags
ext4: use I_VERITY_BUILDING instead of private flags
f2fs: use I_VERITY_BUILDING instead of private flags
xfs: introduce fsverity on-disk changes
xfs: don't allow to enable DAX on fs-verity sealed inode
xfs: disable direct read path for fs-verity files
xfs: don't report dio_mem_align and dio_offset_align for fsverity
files
xfs: handle fsverity I/O in write/read path
xfs: use read ioend for fsverity data verification
xfs: add XFS_BMAPI_UNWRITTEN to unmap unwritten extents in
__xfs_bunmapi()
xfs: don't remove written extents past EOF on fsverity inodes
xfs: add fs-verity support
xfs: initialize fs-verity on file open
xfs: add fs-verity ioctls
xfs: introduce health state for corrupted fsverity metadata
xfs: enable ro-compat fs-verity flag
Darrick J. Wong (2):
xfs: advertise fs-verity being available on filesystem
xfs: check and repair the verity inode flag state
fs/btrfs/btrfs_inode.h | 7 - fs/btrfs/verity.c | 12 +- fs/ext4/ext4.h | 7 - fs/ext4/file.c | 2 +- fs/ext4/inode.c | 17 +- fs/ext4/ioctl.c | 3 +- fs/ext4/verity.c | 19 +- fs/f2fs/data.c | 10 +- fs/f2fs/f2fs.h | 7 - fs/f2fs/verity.c | 12 +- fs/file_attr.c | 23 ++ fs/iomap/buffered-io.c | 11 +- fs/verity/enable.c | 30 +- fs/xfs/Makefile | 2 + fs/xfs/libxfs/xfs_bmap.c | 26 +- fs/xfs/libxfs/xfs_bmap.h | 6 +- fs/xfs/libxfs/xfs_format.h | 31 +- fs/xfs/libxfs/xfs_fs.h | 2 + fs/xfs/libxfs/xfs_health.h | 4 +- fs/xfs/libxfs/xfs_inode_buf.c | 27 ++ fs/xfs/libxfs/xfs_inode_util.c | 5 +- fs/xfs/libxfs/xfs_sb.c | 4 + fs/xfs/libxfs/xfs_verity.c | 20 ++ fs/xfs/libxfs/xfs_verity.h | 13 + fs/xfs/scrub/common.c | 59 ++++ fs/xfs/scrub/common.h | 2 + fs/xfs/scrub/inode.c | 40 +++ fs/xfs/scrub/inode_repair.c | 121 +++++++- fs/xfs/xfs_aops.c | 19 +- fs/xfs/xfs_bmap_util.c | 44 ++- fs/xfs/xfs_file.c | 71 +++-- fs/xfs/xfs_fsverity.c | 513 +++++++++++++++++++++++++++++++ fs/xfs/xfs_fsverity.h | 42 +++ fs/xfs/xfs_health.c | 1 + fs/xfs/xfs_icache.c | 11 +- fs/xfs/xfs_ioctl.c | 33 ++ fs/xfs/xfs_ioend.c | 53 +++- fs/xfs/xfs_ioend.h | 4 +- fs/xfs/xfs_iomap.c | 61 +++- fs/xfs/xfs_iomap.h | 5 +- fs/xfs/xfs_iops.c | 20 +- fs/xfs/xfs_message.c | 4 + fs/xfs/xfs_message.h | 1 + fs/xfs/xfs_mount.h | 4 + fs/xfs/xfs_pnfs.c | 2 +- fs/xfs/xfs_super.c | 31 +- include/linux/fs.h | 9 +- include/linux/fsverity.h | 29 +- include/trace/events/writeback.h | 3 +- 49 files changed, 1320 insertions(+), 162 deletions(-) create mode 100644 fs/xfs/libxfs/xfs_verity.c create mode 100644 fs/xfs/libxfs/xfs_verity.h create mode 100644 fs/xfs/xfs_fsverity.c create mode 100644 fs/xfs/xfs_fsverity.h
Range-diff against v19:
-: ------------ > 1: a0f90a3dc2e4 fs: constify inode argument of inode_state_read_once() 1: e26f4b6bcb69 ! 2: 30209262280a fs: strip FS_VERITY_FL from FS_IOC_SETFLAGS
@@ Commit message
however, BTRFS returns -EOPNOTSUPP. So, the modification of other flags doesn't work on BTRFS for fsverity inodes.
- Fix it for BTRFS and anyone else, the ext4 and f2fs masking becomes
- no-op. The file_get/setattr() path is fine here as this flag is cleared
- right before fileattr_set_prepare(). The future fsverity support in XFS
- would also need it.
+ Make FS_VERITY_FL be silently ignored if it's attempted to be set on a
+ file that didn't have it before. This fixes get-modify-set loop for
+ BTRFS and anyone else, the ext4 and f2fs masking becomes no-op. The
+ file_get/setattr() path is fine here as this flag is cleared right
+ before fileattr_set_prepare(). The future fsverity support in XFS would
+ also need it.
Cc: Boris Burkov <[email protected]>
Cc: David Sterba <[email protected]>
2: 8286293f4f2c ! 3: 899e1c8a85a6 fs,fsverity: add I_VERITY_BUILDING inode state flag
@@ Commit message
flag meaning Merkle tree is currently being built *_VERITY_IN_PROGRESS. They all serve the same two purposes: reject a concurrent enable with -EBUSY and let the write paths recognize the special writes done while
- the tree is under construction.
+ the tree is under construction. Except in BTRFS, which use only the
+ former.
Hoist this flag into the VFS as a new inode state flag, I_VERITY_BUILDING, and manage its lifetime in the generic fs-verity
@@ include/linux/fs.h: is_uncached_acl(struct posix_acl *acl)
*
+ * I_VERITY_BUILDING fs-verity is being enabled on the inode: the Merkle
+ * tree is under construction. The file is effectively
-+ * read-only to userspace; the only writes happening are
-+ * for the fs-verity metadata.
++ * read-only to userspace. Most of the writes are merkle
++ * tree blocks but inode still can have data writeback
++ * in-flight.
+ *
* Q: What is the difference between I_WILL_FREE and I_FREEING?
*
@@ include/linux/fsverity.h: struct fsverity_operations {
*
* i_rwsem is held for write.
*
-+ * I_VERITY_BUILDING is set on the inode for the duration of enabling.
+@@ include/linux/fsverity.h: struct fsverity_operations {
+ * i_rwsem is held for write, but it may have been dropped between
+ * ->begin_enable_verity() and ->end_enable_verity().
+ *
++ * I_VERITY_BUILDING flag is set for the duration of the enablement. The
++ * flag is set right after ->begin_enable_verity() is done and cleared
++ * after ->end_enable_verity().
+ *
* Return: 0 on success, -errno on failure
*/
- int (*begin_enable_verity)(struct file *filp);
+ int (*end_enable_verity)(struct file *filp, const void *desc,
@@ include/linux/fsverity.h: static inline bool fsverity_active(const struct inode *inode)
return false;
}
@@ include/linux/fsverity.h: static inline bool fsverity_active(const struct inode
+ *
+ * This checks whether the inode's Merkle tree is currently being built.
+ * While this is the case the file is read-only to userspace and the only writes
-+ * allowed are for the fs-verity metadata. This becomes false when fsverity -+ * metadata is fully created and written to disk or building failed.
++ * allowed are for the fs-verity metadata, although, inode can still have ++ * in-flight data writeback. This becomes false when fsverity metadata is fully ++ * created and written to disk or building failed.
+ *
+ * Return: true if fs-verity is being enabled on the inode
+ */
7: 28c72f9b678d ! 4: 3871964438a6 fs-verity support for XFS with post EOF merkle tree
@@ Metadata
Author: Andrey Albershteyn <[email protected]>
## Commit message ##
- fs-verity support for XFS with post EOF merkle tree
-
- Hi all,
-
- This is next revision of fsverity for XFS. Changes:
- - Add guard to forbid use on Zoned devices
- - Add a guard against using with LBS (compound pages no-go for fsverity)
- - move IOMAP_F_FSVERITY to xfs_bmbt_to_iomap()
- - Use I_VERITY_BUILDING instead of XFS inode flag
- - Add guard against DAX during fsverity enable
- - Change XFS_FSVERITY_LARGEST_FILE to macro for 32-bit kernel check
- - Forbid opening too large fsverity files on 32-bit kernels
- - xfs_free_eofblocks() doesn't depend on I_VERITY (disk flag instead)
- - Keep EOF cleanup tag on inode if fsverity build failed
- - Invalidate past EOF page cache in scrub on corrupted inodes
- - Remove past EOF extents in scrub on corrupted inodes
-
- Patches without review:
- [PATCH v19 05/15] xfs: handle fsverity I/O in write/read path
- [PATCH v19 09/15] xfs: add fs-verity support
- [PATCH v19 13/15] xfs: check and repair the verity inode flag state
-
- The RO_COMPAT with CONFIG_FS_VERITY=n should be fine as fsverity inode
- won't open (this is same for all fs).
-
- This series based on v7.4-rc3 and my series adding I_VERITY_BUILDING [1]
-
- 1: https://lore.kernel.org/linux-xfs/[email protected]
-
- kernel:
- https://git.kernel.org/pub/scm/linux/kernel/git/aalbersh/xfs-linux.git/log/?h=fsverity
-
- xfsprogs:
- https://git.kernel.org/pub/scm/linux/kernel/git/aalbersh/xfsprogs-dev.git/log/?h=fsverity
-
- xfstests:
- https://git.kernel.org/pub/scm/linux/kernel/git/aalbersh/xfstests-dev.git/log/?h=fsverity
-
- v18:
- https://lore.kernel.org/fsverity/[email protected]/T/#t
-
- v17:
- https://lore.kernel.org/fsverity/[email protected]/T/#u
-
- v16:
- https://lore.kernel.org/fsverity/[email protected]/T/#t
-
- v15:
- https://lore.kernel.org/fsverity/[email protected]/T/#t
-
- To: [email protected]
- To: [email protected]
- To: Carlos Maiolino <[email protected]>
- To: [email protected]
- To: [email protected]
-
- Cc: [email protected]
- Cc: [email protected]
-
- ---
- Changes in v19:
- - Add guard to forbid use on Zoned devices
- - Add a guard against using with LBS (compound pages no-go for fsverity)
- - move IOMAP_F_FSVERITY to xfs_bmbt_to_iomap()
- - Use I_VERITY_BUILDING instead of XFS inode flag
- - Add guard against DAX during fsverity enable
- - Change XFS_FSVERITY_LARGEST_FILE to macro for 32-bit kernel check
- - Forbid opening too large fsverity files on 32-bit kernels
- - xfs_free_eofblocks() doesn't depend on I_VERITY (disk flag instead)
- - Keep EOF cleanup tag on inode if fsverity build failed
- - Invalidate past EOF page cache in scrub on corrupted inodes
- - Remove past EOF extents in scrub on corrupted inodes
- Changes in v18:
- - Rebase to latest origin/for-next and drop core fsverity patches
- - Move truncate_inode_pages() before xfs_trans_alloc()
- - Move xfs_fsverity_metadata_offset() to libxfs as used in xfs_bmap.c
- Changes in v17:
- - Add mempool for fsverity ioends cache
- - Add xfs_fsverity_reset_inode() as a common clean up function
- - Changed ILOCK_SHARED to ILOCK_EXCL for xfs_bmap_last_extent()
- - Swap order of truncate_inode_pages() and ilock() due to ABBA
- - Add xfs_fsverity_is_hashes_of_zeroed_blocks() helper
- - Removed EINVAL and EFBIG from scrub as ambiguous
- Changes in v16:
- - Rebase to block/for-next
- - Removed work_struct from ioend in favor of kmem_cache structs
- - Minor adjustments from v15 review
- - Skip written extents in lower level of __xfs_bunmapi()
- Changes in v15:
- - Pull BIO in task context patches
- - Drop flag argument in xfs_free_eofblocks()
- - Call xfs_free_eofblocks() on fsverity inodes
- - Comments and commit messages updates
- - Rebased to v7.2-rc7
- - Dropped patch for fsverity_fill_zerohash() with highmem optimization
- Changes in v14:
- - Rebase to lazy-bounce@hch-misc
- - Adjust read ioends to BIO in task context flow
- - MMAPLOCK/sb_internal deadlock fix reported by sashiko
- - Add missing delalloc clean up in verity_end_enable
- - Use xfs_free_eofblocks() instead of writing own clean up routine
- - Modify xfs_free_eofblocks() to be able to clean unwritten only
- - Dropped fix patch for truncate/set_size check
- - Various minor code and #include rearrangements for bisecting
- Changes in v13:
- - Hoisted statx reporting to common code
- - Added read ioend sorted for worker self-deadlock fix
- - Adjusted fsverity flags in zoned write path
- Changes in v12:
- - Refactored xfs_fsverity_cancel_unwritten()
- - Switched to using inode_set_flags()
- - Add a lock for COW fork reading
- - Add pagecache truncation in cleanup path
- - Added ERANGE and EBADMSG to fsverity scrub handling
- - Add missing XFS_FSVERITY_CONSTRUCTION in various xfs_iomap
- - Rebase to -rc3
- Changes in v11:
- - Drop wrong overlayfs patch
- - Drop already merged iomap and fsverity patches
- - Update to I_INO() use instead of ip->i_ino
- - Sashiko.dev fixes. See list of issues below.
- Changes in v10:
- - Rebase to v7.1-rc3 with relevant adjustments
- - Initialize ioend->io_vi to NULL to not get write work onto verity wq
- - Range diff below
- Changes in v9:
- - Fix fsverity_fill_zerohash() parameter names
- - A few fixes found by sashiko.dev:
- - Replace ip->i_mount->m_attr_geo->blksize with m_sb.sb_blocksize
- - Don't call xfs_trans_cancel() after xfs_trans_commit() in
- xfs_fsverity_end_enable()
- - Call xfs_fsverity_delete_metadata() if verity enable failed
- - Change start/end type from xfs_fileoff_t to loff_t
- - Return xfs_trans_commit() error from
- xfs_fsverity_cancel_unwritten()
- Changes in v8:
- - Return fsverity_ensure_verity_info() errors from
- ovl_ensure_verity_loaded()
- Changes in v7:
- - Move kerneldoc to fsverity_ensure_verity_info() definition
- - Drop patch adding XFS traces
- - Fix overly long line in the comment
- - Make order of fserror and fsverity_error consistent
- - Add overlay patch converting to fsverity_ensure_verity_info()
- Changes in v6:
- - Removed stub for fsverity_ensure_verity_info() as it's optimized out
- - Rename fsverity_folio_zero_hash() to fsverify_fill_zerohash()
- - Merge patches 8 to 10 into one
- - Merge patch gerating zero_hash and fsverity_fill_zerohash() into one
- - Add kerneldoc to fsverity_ensure_verity_info()
- - Add comments to iomap_block_needs_zeroing()
- Changes in v5:
- - Add fserror_report_data_lost() for data blocks in page spanning EOF
- - Issue fsverity metadata readahead in data readahead
- - iomap_fsverity_write() return type fix
- - Use of S_ISREG(mode)
- - Make 65536 #define instead of open-coded
- - Use transaction per unwritten extent removal
- - Fetch fsverity_info for all fsverity metadata
- - Revert fsverity_folio_zero_hash() stub as used in iomap
- - Extend cancel_unwritten to whole file range to remove cow leftovers
- - Drop delayed allocation on the COW fork on fsverity completion
- Changes in v4:
- - Use fserror interface in fsverity instead of fs callback
- - Hoist pagecache_read from f2fs/ext4 to fsverity
- - Refactor iomap code
- - Fetch fsverity_info only for file data and merkle tree holes
- - Do not disable preallocation, remove unwritten extents instead
- - Offload fsverity hash I/O to fsverity workqueue in read path
- - Store merkle tree at round_up(i_size, 64k)
- - Add a spacing between merkle tree and fsverity descriptor as next 64k
- aligned block
- - Squash helpers into first user commits
- - Squash on-disk format changes into single commit
- - Drop different offset for pagecache/on-disk
- - Don't zero out pages in higher order folios in write path
- - Link to v3: https://lore.kernel.org/fsverity/[email protected]/T/#t
- Changes in v3:
- - Different on-disk and pagecache offset
- - Use read path ioends
- - Switch to hashtable fsverity info
- - Synthesize merkle tree blocks full of zeroes
- - Other minor refactors
- - Link to v2: https://lore.kernel.org/fsverity/20260114164210.GO15583@frogsfrogsfrogs/T/#t
- Changes in v2:
- - Move to VFS interface for merkle tree block reading
- - Drop patchset for per filesystem workqueues
- - Change how offsets of the descriptor and tree metadata is calculated
- - Store fs-verity descriptor in data fork side by side with merkle tree
- - Simplify iomap changes, remove interface for post eof read/write
- - Get rid of extended attribute implementation
- - Link to v1: https://lore.kernel.org/r/[email protected]
+ fs,fsverity: reject S_APPEND on fsverity inodes
+
+ fs-verity rejects enabling fsverity on append only files. However, there
+ are two cases when FS_IOC_SETFLAGS can turn inode append only.
+
+ The first one is right before the lock is taken in the enable_verity(),
+ making original check useless.
+
+ The second one is after lock in enable_verity() is unlocked, therefore,
+ when merkle tree is being build and after fs-verity inode is sealed.
+ This one is not critical, it just sets incompatible flags on inode but
+ append-only inode will not get any writes anyway.
+
+ Fix it by checking S_APPEND again under the lock, serializing this with
+ vfs_fileattr_set() and reject S_APPEND on fsverity inodes.
+
+ Fixes: 3fda4c617e84 ("fs-verity: implement FS_IOC_ENABLE_VERITY ioctl")
+ Signed-off-by: Andrey Albershteyn <[email protected]>
+
+ ## fs/file_attr.c ##
+@@
+ #include <linux/fs.h>
+ #include <linux/security.h>
+ #include <linux/fscrypt.h>
++#include <linux/fsverity.h>
+ #include <linux/fsnotify.h>
+ #include <linux/fileattr.h>
+ #include <linux/export.h>
+@@ fs/file_attr.c: static int fileattr_set_prepare(struct inode *inode,
+ fa->flags &= ~FS_VERITY_FL;
+ fa->fsx_xflags &= ~FS_XFLAG_VERITY;
+
++ /*
++ * FS_APPEND_FL is incompatible with fs-verity. FS_IOC_ENABLE_VERITY
++ * rejects append-only files, also reject turning a verity file
++ * append-only.
++ *
++ * Reject in both cases, after it has been sealed and while its Merkle
++ * tree is being built.
++ */
++ if ((fa->flags & FS_APPEND_FL) &&
++ ((old_ma->flags & FS_VERITY_FL) || fsverity_is_building(inode)))
++ return -EPERM;
++
+ /*
+ * Project Quota ID state is only allowed to change from within the init
+ * namespace. Enforce that restriction only if we are trying to change
+
+ ## fs/verity/enable.c ##
+@@ fs/verity/enable.c: static int enable_verity(struct file *filp,
+ err = -EEXIST;
+ } else if (fsverity_is_building(inode)) {
+ err = -EBUSY;
++ } else if (IS_APPEND(inode)) {
++ /*
++ * The check in fsverity_ioctl_enable() is done without the
++ * inode lock, so a concurrent FS_IOC_SETFLAGS could have set
++ * S_APPEND in the meantime. Recheck it here, serialized with
++ * ->fileattr_set().
++ */
++ err = -EPERM;
+ } else {
+ err = vops->begin_enable_verity(filp);
+ if (!err) {
3: 522b11667d13 ! 5: b3e905834f43 iomap: use fsverity_is_building() to check for fsverity IO
@@ Commit message
Recently, inode gained new I_VERITY_BUILDING flag. This flag indicates that inode is undergoing fsverity metadata construction. Previously, it was indicated by stale iomap flag on the IO, now we can check the inode
- instead. As fsverity inodes are read-only, the only write IO happening
- during I_VERITY_BUILDING is fsverity metadata.
+ instead.
## fs/iomap/buffered-io.c ##
-@@ fs/iomap/buffered-io.c: static bool iomap_writeback_handle_eof(struct folio *folio,
- struct inode *inode = wpc->inode;
+@@ fs/iomap/buffered-io.c: static int iomap_writeback_range(struct iomap_writepage_ctx *wpc,
+ * i_size, adjust end_pos and zero all data beyond i_size. Don't skip fsverity
+ * folios as those are beyond i_size.
+ */
+-static bool iomap_writeback_handle_eof(struct folio *folio,
+- struct iomap_writepage_ctx *wpc, u64 *end_pos)
++static bool iomap_writeback_handle_eof(struct folio *folio, struct inode *inode,
++ u64 *end_pos)
+ {
+- struct inode *inode = wpc->inode;
u64 isize = i_size_read(inode);
- if (wpc->iomap.flags & IOMAP_F_FSVERITY) {
-+ if (fsverity_is_building(inode)) {
- WARN_ON_ONCE(folio_pos(folio) < isize);
+- WARN_ON_ONCE(folio_pos(folio) < isize);
++ if (fsverity_is_building(inode) && folio_pos(folio) >= isize)
return true;
- }
+- }
+
+ if (*end_pos > isize) {
+ size_t poff = offset_in_folio(folio, isize);
+@@ fs/iomap/buffered-io.c: int iomap_writeback_folio(struct iomap_writepage_ctx *wpc, struct folio *folio) +
+ trace_iomap_writeback_folio(inode, pos, folio_size(folio));
+
+- if (!iomap_writeback_handle_eof(folio, wpc, &end_pos))
++ if (!iomap_writeback_handle_eof(folio, inode, &end_pos))
+ return 0;
+ WARN_ON_ONCE(end_pos <= pos);
+
4: 0145d2d05fb0 = 6: f1b9fd251602 btrfs: use I_VERITY_BUILDING instead of private flags 5: 77fce535e46e = 7: 79eed12171a8 ext4: use I_VERITY_BUILDING instead of private flags 6: 084d6adc7d1c ! 8: 65a9eea27c75 f2fs: use I_VERITY_BUILDING instead of private flags
@@ Commit message
duplicated this state.
Replace f2fs own helper f2fs_verity_in_progress() with
- fsverity_is_building(), and remove the and FI_VERITY_IN_PROGRESS flags.
+ fsverity_is_building(), and remove the FI_VERITY_IN_PROGRESS flag.
It's fine to remove IS_ENABLED() guard as fsverity_is_building() has stub.
8: ca9aa48fa04b = 9: a14c4c3e59f2 xfs: introduce fsverity on-disk changes 9: a019dc4ec4b8 = 10: 8c105dee8aae xfs: don't allow to enable DAX on fs-verity sealed inode 10: b8f1466cf632 = 11: d015dbb96f4c xfs: disable direct read path for fs-verity files 11: d7810bcef90a = 12: d88b9f4e83c5 xfs: don't report dio_mem_align and dio_offset_align for fsverity files 12: 30fa010febff ! 13: ad90aa77fc54 xfs: handle fsverity I/O in write/read path
@@ Metadata
## Commit message ##
xfs: handle fsverity I/O in write/read path
- For write/writeback set IOMAP_F_FSVERITY flag telling iomap to not
- update inode size and to not skip folios beyond EOF.
+ For write path set IOMAP_F_FSVERITY flag telling iomap to not update
+ inode size and to not skip folios beyond EOF.
- Initiate fsverity writeback with IOMAP_F_FSVERITY set to tell iomap
- should not skip folio that is dirty beyond EOF.
-
- In read path let iomap know that we are reading fsverity metadata. So,
+ For read path let iomap know that we are reading fsverity metadata. So,
treat holes in the tree as request to synthesize tree blocks and hole after descriptor as end of the fsverity region.
13: fe7a17bb65c6 ! 14: 3f2349fca7a8 xfs: use read ioend for fsverity data verification
@@ fs/xfs/xfs_super.c: init_xfs_fs(void)
xfs_destroy_caches();
out:
@@ fs/xfs/xfs_super.c: exit_xfs_fs(void)
- xfs_cleanup_procfs();
+ free_percpu(xfsstats.xs_stats);
xfs_mru_cache_uninit();
xfs_destroy_workqueues();
+ xfs_fsverity_exit();14: c428bb5ceba4 ! 15: 550e52c75b98 xfs: add XFS_BMAPI_UNWRITTEN to unmap unwritten extents in __xfs_bunmapi()
@@ Commit message
+ Reviewed-by: Christoph Hellwig <[email protected]>
## fs/xfs/libxfs/xfs_bmap.c ##
@@ fs/xfs/libxfs/xfs_bmap.c: __xfs_bunmapi(
15: f62ffd5f8991 ! 16: f7787e59edb3 xfs: don't remove written extents past EOF on fsverity inodes
@@ Commit message
takes place.
Signed-off-by: Andrey Albershteyn <[email protected]>
- Reviewed-by: "Darrick J. Wong" <[email protected]>
## fs/xfs/xfs_bmap_util.c ##
@@
@@ fs/xfs/xfs_bmap_util.c: xfs_free_eofblocks(
struct xfs_mount *mp = ip->i_mount;
int error;
+ int bmapi_flags = XFS_BMAPI_NODISCARD;
-+ bool verity = fsverity_is_building(VFS_I(ip));
++ xfs_off_t aligned_size;
/* Attach the dquots to the inode up front. */
error = xfs_qm_dqattach(ip);
@@ fs/xfs/xfs_bmap_util.c: xfs_free_eofblocks(
+ inode_dio_wait(VFS_I(ip));
+
+ /*
+- * For preallocated files only free delayed allocations.
++ * On fs-verity inodes, while fs-verity builds metadata past EOF, it can
++ * leave delayed preallocations and unwritten extents. Clear delayed
++ * preallocation here and tell bmapi to remove unwritten extents.
*
- * Note that this means we also leave speculative preallocations in
- * place for preallocated files.
-+ *
-+ * Clean up delalloc reservations for fsverity too as those won't be
-+ * used
+- * Note that this means we also leave speculative preallocations in
+- * place for preallocated files.
++ * The xfs_free_eofblocks() is called by fs-verity when building is
++ * complete but fsverity_is_building() is still not cleared.
*/
- if (ip->i_diflags & (XFS_DIFLAG_PREALLOC | XFS_DIFLAG_APPEND)) {
-+ if (ip->i_diflags & (XFS_DIFLAG_PREALLOC | XFS_DIFLAG_APPEND) ||
-+ verity) {
- if (ip->i_delayed_blks) {
+- if (ip->i_delayed_blks) {
++ aligned_size = round_up(XFS_ISIZE(ip), mp->m_sb.sb_blocksize);
++ if (fsverity_is_building(VFS_I(ip))) {
++ if (ip->i_delayed_blks)
xfs_bmap_punch_delalloc_range(ip, XFS_DATA_FORK,
- round_up(XFS_ISIZE(ip), mp->m_sb.sb_blocksize),
- LLONG_MAX, NULL);
- }
+- round_up(XFS_ISIZE(ip), mp->m_sb.sb_blocksize),
+- LLONG_MAX, NULL);
+- }
++ aligned_size, LLONG_MAX, NULL);
++ bmapi_flags |= XFS_BMAPI_UNWRITTEN;
++ } else if (ip->i_diflags & (XFS_DIFLAG_PREALLOC | XFS_DIFLAG_APPEND)) {
++ /*
++ * For preallocated files only free delayed allocations.
++ *
++ * Note that this means we also leave speculative preallocations
++ * in place for preallocated files.
++ */
++ if (ip->i_delayed_blks)
++ xfs_bmap_punch_delalloc_range(ip, XFS_DATA_FORK,
++ aligned_size, LLONG_MAX, NULL);
xfs_inode_clear_eofblocks_tag(ip);
-- return 0;
-+ if (!verity)
-+ return 0;
+ return 0;
}
-
- error = xfs_trans_alloc(mp, &M_RES(mp)->tr_itruncate, 0, 0, 0, &tp);
-@@ fs/xfs/xfs_bmap_util.c: xfs_free_eofblocks(
- xfs_ilock(ip, XFS_ILOCK_EXCL);
- xfs_trans_ijoin(tp, ip, 0);
-
-+ /*
-+ * While fs-verity writes metadata after EOF, it can leave unwritten
-+ * preallocations. Clear all that out.
-+ */
-+ if (verity)
-+ bmapi_flags |= XFS_BMAPI_UNWRITTEN;
-+
- /*
- * Do not update the on-disk file size. If we update the on-disk file
- * size and then the system crashes before the contents of the file are
@@ fs/xfs/xfs_bmap_util.c: xfs_free_eofblocks(
* bug).
*/
16: 5f6f04fde6e4 ! 17: 63971a4f3d09 xfs: add fs-verity support
@@ Commit message
region iomap doesn't call into fsverity to verify it against Merkle
tree. For data, verification is done at ioend completion in a workqueue.
- When fs-verity is enabled on an inode, the XFS_IVERITY_CONSTRUCTION
- flag is set meaning that the Merkle tree is being build. The
- initialization ends with storing of verity descriptor and setting
- inode on-disk flag (XFS_DIFLAG2_VERITY). Lastly, the
- XFS_IVERITY_CONSTRUCTION is dropped and I_VERITY is set on inode.
+ The metadata building starts with flushing pagecache and cleaning up any
+ residue extents past EOF. Over whole initialization period
+ I_VERITY_BUILDING indicates that merkle tree building is in progress. No
+ iolock is held over this period. After core fsverity code done with
+ building the tree, XFS stores verity descriptor, then flushes page cache
+ again, then pro-actively remove any delayed allocation and unwritten
+ extents past EOF and ends up with setting inode on-disk flag
+ (XFS_DIFLAG2_VERITY).
The descriptor is stored in a new block aligned to 64k after the last
Merkle tree block. The size of the descriptor is stored at the end of
the last descriptor block (descriptor can be multiple blocks).
- Pro-actively remove any unwritten extents as we use last extent to
- locate descriptor.
-
Signed-off-by: Andrey Albershteyn <[email protected]>
## fs/xfs/libxfs/xfs_inode_buf.c ##
@@ fs/xfs/xfs_fsverity.c: xfs_fsverity_is_file_data(
+ if (!buf_size)
+ return desc_size;
+
-+ if (XFS_IS_CORRUPT(mp, desc_size > buf_size))
++ if (desc_size > buf_size)
++ return -ERANGE;
++
++ if (XFS_IS_CORRUPT(mp,
++ desc_size < sizeof(struct fsverity_descriptor)))
+ return -ERANGE;
+
+ desc_pos = round_down(desc_size_pos - desc_size, blocksize);
@@ fs/xfs/xfs_fsverity.h
bool xfs_fsverity_is_file_data(const struct xfs_inode *ip, loff_t offset);
## fs/xfs/xfs_ioctl.c ##
+@@
+
+ #include <linux/mount.h>
+ #include <linux/fileattr.h>
++#include <linux/fsverity.h>
+
+ /* Return 0 on success or positive error */
+ int
@@ fs/xfs/xfs_ioctl.c: xfs_ioctl_setattr_xflags(
if (xfs_inode_has_filedata(ip))
return -EINVAL;
@@ fs/xfs/xfs_ioctl.c: xfs_ioctl_setattr_xflags(
+
/*
* If S_DAX is enabled on this file, we can only switch the
- * device if both support fsdax. We can't update S_DAX because
+ * device if both support fsdax, and the zoned code does not.
@@ fs/xfs/xfs_ioctl.c: xfs_ioctl_setattr_xflags(
return -EINVAL;
}
@@ fs/xfs/xfs_super.c
#include "xfs_ondisk.h"
#include "xfs_rmap_item.h"
#include "xfs_refcount_item.h"
+@@
+ #include "xfs_rtalloc.h"
+ #include "xfs_zone_alloc.h"
+ #include "xfs_healthmon.h"
+-#include "xfs_fsverity.h"
+ #include "scrub/stats.h"
+ #include "scrub/rcbag_btree.h"
+
@@ fs/xfs/xfs_super.c: xfs_fs_fill_super(
sb->s_quota_types = QTYPE_MASK_USR | QTYPE_MASK_GRP | QTYPE_MASK_PRJ;
#endif
17: 9206c4a644f1 = 18: 8bb18397606d xfs: initialize fs-verity on file open
18: 856483dbd534 ! 19: 595f010968d3 xfs: add fs-verity ioctls@@ Commit message
## fs/xfs/xfs_ioctl.c ##
-@@
-
- #include <linux/mount.h>
- #include <linux/fileattr.h>
-+#include <linux/fsverity.h>
-
- /* Return 0 on success or positive error */
- int
@@ fs/xfs/xfs_ioctl.c: xfs_file_ioctl(
case XFS_IOC_VERIFY_MEDIA:
return xfs_ioc_verify_media(filp, arg);
19: 31f7e8a2669e = 20: 2ab9a2b11d75 xfs: advertise fs-verity being available on filesystem
20: dfabd8df8c89 ! 21: 80d1aaf6e39c xfs: check and repair the verity inode flag state
@@ Commit message
Signed-off-by: Darrick J. Wong <[email protected]>
Signed-off-by: Andrey Albershteyn <[email protected]>
+ ## fs/xfs/libxfs/xfs_inode_buf.c ##
+@@ fs/xfs/libxfs/xfs_inode_buf.c: xfs_dinode_verify(
+ */
+ if ((flags & XFS_DIFLAG_REALTIME) && mp->m_sb.sb_rextsize > 1)
+ return __this_address;
++
++ /*
++ * fs-verity doesn't work on zoned filesystems, where all
++ * data writes are directed to the sequential zones of the
++ * realtime device.
++ */
++ if (xfs_has_zoned(mp) && (flags & XFS_DIFLAG_REALTIME))
++ return __this_address;
+ }
+
+ if (xfs_has_zoned(mp) &&
+
## fs/xfs/scrub/common.c ##
@@
#include "scrub/health.h"
@@ fs/xfs/scrub/common.c: xchk_inode_count_blocks(
+ case 0:
+ /* fsverity is active */
+ break;
++ case -EFBIG:
++ case -EINVAL:
+ case -ENODATA:
+ case -EMSGSIZE:
+ case -EFSCORRUPTED:
@@ fs/xfs/scrub/common.c: xchk_inode_count_blocks(
+ /*
+ * The nonzero errno codes above are the error codes that can
+ * be returned from fsverity on metadata validation errors.
++ *
++ * EINVAL and EFBIG are not always mean corruption. For example,
++ * missing hash algorithm could mean kernel doesn't support it
++ * or fsverity descriptor has corrupted value. Let's clear it.
+ */
+ return 0;
+ default:
@@ fs/xfs/scrub/inode.c: xchk_prepare_iscrub(
error = xchk_trans_alloc(sc, 0);
if (error)
return error;
+@@ fs/xfs/scrub/inode.c: xchk_inode_flags2(
+ if ((flags2 & XFS_DIFLAG2_NREXT64) && !xfs_has_large_extent_counts(mp))
+ goto bad;
+
++ /* no verity iflag without the filesystem feature or on non-files */
++ if ((flags2 & XFS_DIFLAG2_VERITY) &&
++ (!xfs_has_verity(mp) || !S_ISREG(mode)))
++ goto bad;
++
++ /* verity and DAX do not mix */
++ if ((flags2 & XFS_DIFLAG2_VERITY) && (flags2 & XFS_DIFLAG2_DAX))
++ goto bad;
++
++ /*
++ * no verity iflag on realtime files that cannot support it, i.e.
++ * zoned files and files with a realtime allocation unit larger
++ * than a filesystem block
++ */
++ if ((flags2 & XFS_DIFLAG2_VERITY) && (flags & XFS_DIFLAG_REALTIME) &&
++ (xfs_has_zoned(mp) || mp->m_sb.sb_rextsize > 1))
++ goto bad;
++
+ return;
+ bad:
+ xchk_ino_set_corrupt(sc, ino);
@@ fs/xfs/scrub/inode.c: xchk_inode(
if (S_ISREG(VFS_I(sc->ip)->i_mode))
xchk_inode_check_reflink_iflag(sc, I_INO(sc->ip));
@@ fs/xfs/scrub/inode.c: xchk_inode(
xchk_inode_xref(sc, I_INO(sc->ip), &di);
## fs/xfs/scrub/inode_repair.c ##
+@@ fs/xfs/scrub/inode_repair.c: struct xrep_inode {
+ /* Must we remove all access from this file? */
+ bool zap_acls;
+
++ /*
++ * Did we have to clear the fsverity flag while repairing the ondisk
++ * inode? If so, free the orphaned Merkle tree after reloading the
++ * inode.
++ */
++ bool zap_verity;
++
+ /* Inode scanner to see if we can find the ftype from dirents */
+ struct xchk_iscan ftype_iscan;
+ uint8_t alleged_ftype;
+@@ fs/xfs/scrub/inode_repair.c: xrep_dinode_nlinks(
+ /* Fix any conflicting flags that the verifiers complain about. */
+ STATIC void
+ xrep_dinode_flags(
+- struct xfs_scrub *sc,
+- struct xfs_dinode *dip,
+- bool isrt)
++ struct xrep_inode *ri,
++ struct xfs_dinode *dip)
+ {
++ struct xfs_scrub *sc = ri->sc;
+ struct xfs_mount *mp = sc->mp;
+ uint64_t flags2 = be64_to_cpu(dip->di_flags2);
+ uint16_t flags = be16_to_cpu(dip->di_flags);
+ uint16_t mode = be16_to_cpu(dip->di_mode);
++ bool isrt = ri->rt_extents > 0;
+
+ trace_xrep_dinode_flags(sc, dip);
+
@@ fs/xfs/scrub/inode_repair.c: xrep_dinode_flags(
dip->di_nrext64_pad = 0;
else if (dip->di_version >= 3)
dip->di_v3_pad = 0;
+ if (!xfs_has_verity(mp) || !S_ISREG(mode))
+ flags2 &= ~XFS_DIFLAG2_VERITY;
++
++ /*
++ * fs-verity does not work on zoned inodes or on realtime inodes
++ * with a realtime allocation unit larger than a filesystem block.
++ * The realtime flag cannot be cleared here because the file's data
++ * is already stored on the realtime device, so the fsverity flag
++ * has to go. Free the orphaned Merkle tree past EOF once the inode
++ * can be loaded again.
++ */
++ if (IS_ENABLED(CONFIG_FS_VERITY) &&
++ (flags & XFS_DIFLAG_REALTIME) && (flags2 & XFS_DIFLAG2_VERITY) &&
++ (xfs_has_zoned(mp) || mp->m_sb.sb_rextsize > 1)) {
++ flags2 &= ~XFS_DIFLAG2_VERITY;
++ if (xfs_has_verity(mp) && S_ISREG(mode))
++ ri->zap_verity = true;
++ }
if (flags2 & XFS_DIFLAG2_METADATA) {
xfs_failaddr_t fa;
+@@ fs/xfs/scrub/inode_repair.c: xrep_dinode_core(
+ if (iget_error)
+ goto write;
+ xrep_dinode_nlinks(dip);
+- xrep_dinode_flags(sc, dip, ri->rt_extents > 0);
++ xrep_dinode_flags(ri, dip);
+ xrep_dinode_size(ri, dip);
+ xrep_dinode_extsize_hints(sc, dip);
+ xrep_dinode_zap_forks(ri, dip);
@@ fs/xfs/scrub/inode_repair.c: xrep_dinode_core(
if (iget_error)
return iget_error;
@@ fs/xfs/scrub/inode_repair.c: xrep_dinode_core(
+ error = xchk_inode_setup_verity(sc);
+ if (error)
+ return error;
++
++ /*
++ * If verity activation failed, the descriptor read above may have left
++ * uptodate post-EOF folios in the page cache. Drop them before
++ * xrep_inode_verity frees the Merkle tree, as xchk_prepare_iscrub does
++ * for the normal setup path.
++ */
++ if (xchk_inode_verity_broken(sc->ip) || ri->zap_verity) {
++ xchk_ilock(sc, XFS_MMAPLOCK_EXCL);
++ truncate_inode_pages(VFS_I(sc->ip)->i_mapping,
++ XFS_ISIZE(sc->ip));
++ }
+
error = xchk_trans_alloc(sc, 0);
if (error)
return error;
+@@ fs/xfs/scrub/inode_repair.c: xrep_inode_flags(
+ /* DAX only applies to files and dirs. */
+ if (!(S_ISREG(mode) || S_ISDIR(mode)))
+ sc->ip->i_diflags2 &= ~XFS_DIFLAG2_DAX;
++
++ /* DAX and fsverity do not mix. */
++ if (sc->ip->i_diflags2 & XFS_DIFLAG2_VERITY)
++ sc->ip->i_diflags2 &= ~XFS_DIFLAG2_DAX;
++
++ /* APPEND and fsverity do not mix. */
++ if (sc->ip->i_diflags2 & XFS_DIFLAG2_VERITY)
++ sc->ip->i_diflags &= ~XFS_DIFLAG_APPEND;
+ }
+
+ /*
@@ fs/xfs/scrub/inode_repair.c: xrep_inode_reset_dquots(
# define xrep_inode_reset_dquots(sc) (0)
#endif
+/*
-+ * If this file is a fsverity file, xchk_prepare_iscrub or xrep_dinode_core
-+ * should have activated it. If it's still not active, then there's something
-+ * wrong with the verity descriptor, so turn verity off and free the orphaned
-+ * Merkle tree that XFS keeps in extents beyond EOF.
++ * Disable fsverity on this file and free the Merkle tree, which XFS stores
++ * in extents beyond EOF. The stale Merkle tree page cache must have been
++ * dropped before calling this function.
+ */
+STATIC int
-+xrep_inode_verity(
++xrep_inode_zap_verity(
+ struct xfs_scrub *sc)
+{
+ struct inode *inode = VFS_I(sc->ip);
+ struct xfs_mount *mp = sc->mp;
+ int error;
+
-+ if (!xchk_inode_verity_broken(sc->ip))
-+ return 0;
-+
-+ /*
-+ * The fsverity descriptor cannot be activated, so disable verity and
-+ * free the Merkle tree, which XFS stores in beyond EOF. The stale
-+ * Merkle tree page cache was already dropped during scrub setup.
-+ */
+ error = xfs_bunmapi_range(&sc->tp, sc->ip, 0,
+ XFS_B_TO_FSB(mp, XFS_ISIZE(sc->ip)), XFS_MAX_FILEOFF);
+ if (error)
@@ fs/xfs/scrub/inode_repair.c: xrep_inode_reset_dquots(
+ xfs_trans_log_inode(sc->tp, sc->ip, XFS_ILOG_CORE);
+ return 0;
+}
++
++/*
++ * If this file was a fsverity file, xchk_prepare_iscrub or xrep_dinode_core
++ * should have activated it. If it's still not active, then there's something
++ * wrong with the verity descriptor, so turn verity off and free the orphaned
++ * Merkle tree that XFS keeps in extents beyond EOF. We also free the Merkle
++ * tree if dinode repair had to clear the verity flag.
++ */
++STATIC int
++xrep_inode_verity(
++ struct xfs_scrub *sc)
++{
++ struct xrep_inode *ri = sc->buf;
++
++ /*
++ * If we had to clear the fsverity flag while repairing the ondisk
++ * inode or we failed to activate the fsverity descriptor, clear the
++ * fsverity flag and free the orphaned Merkle tree that XFS stores
++ * past EOF.
++ */
++ if (!xchk_inode_verity_broken(sc->ip) && !(ri && ri->zap_verity))
++ return 0;
++
++ /*
++ * The post-EOF verity folios were dropped by xchk_prepare_iscrub or
++ * xrep_dinode_core before we got here.
++ */
++ return xrep_inode_zap_verity(sc);
++}
+
/* Repair an inode's fields. */
int
21: 6e4fdcf643b6 ! 22: cb61498e6095 xfs: introduce health state for corrupted fsverity metadata
@@ fs/xfs/xfs_fsverity.c: xfs_fsverity_get_descriptor(
if (!buf_size)
return desc_size;
+@@ fs/xfs/xfs_fsverity.c: xfs_fsverity_get_descriptor(
+ return -ERANGE;
-- if (XFS_IS_CORRUPT(mp, desc_size > buf_size))
-+ if (XFS_IS_CORRUPT(mp, desc_size > buf_size)) {
+ if (XFS_IS_CORRUPT(mp,
+- desc_size < sizeof(struct fsverity_descriptor)))
++ desc_size < sizeof(struct fsverity_descriptor))) {
+ xfs_inode_mark_sick(XFS_I(inode), XFS_SICK_INO_FSVERITY);
return -ERANGE;
+ }22: 28c74513a1bf = 23: 53d1aafb63f3 xfs: enable ro-compat fs-verity flag
Signature · 1 line
2.54.0
PATCH v20 1/23 fs: constify inode argument of inode_state_read_once()
Constify inode parameter so that callers can pass const inode. Callers do not need a change.
--- include/linux/fs.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
include/linux/fs.h +1 -1
@@ -878,7 +878,7 @@ struct inode {* * We hide all of it behind helpers so that we can validate consumers. */ -static inline enum inode_state_flags_enum inode_state_read_once (struct inode *inode) +static inline enum inode_state_flags_enum inode_state_read_once (const struct inode *inode) {return READ_ONCE (inode->i_state.__state); }
Signature · 1 line
2.54.0
PATCH v20 2/23 fs: strip FS_VERITY_FL from FS_IOC_SETFLAGS
In reply to Andrey AlbershteynThe common use of FS_IOC_GETFLAGS/SETFLAGS is get-modify-set loop. Filesystems could return verity status with GETFLAGS, attempting to then pass FS_VERITY_FL to SETFLAGS is not allowed.
The ext4 and f2fs already treat FS_VERITY_FL in the set path as no-op, however, BTRFS returns -EOPNOTSUPP. So, the modification of other flags doesn't work on BTRFS for fsverity inodes.
Make FS_VERITY_FL be silently ignored if it's attempted to be set on a file that didn't have it before. This fixes get-modify-set loop for BTRFS and anyone else, the ext4 and f2fs masking becomes no-op. The file_get/setattr() path is fine here as this flag is cleared right before fileattr_set_prepare(). The future fsverity support in XFS would also need it.
--- fs/file_attr.c | 10 ++++++++++ 1 file changed, 10 insertions(+)
fs/file_attr.c +10 -0
@@ -199,6 +199,16 @@ static int fileattr_set_prepare(struct inode *inode,if (err)return err; +/* +* FS_VERITY_FL is settable only via FS_IOC_ENABLE_VERITY; never through +* fileattr +* +* Treat it as a no-op so the FS_IOC_GETFLAGS/SETFLAGS round-trip +* doesn't break for other flags +*/ + fa->flags &= ~FS_VERITY_FL ; + fa->fsx_xflags &= ~FS_XFLAG_VERITY ; +/* * Project Quota ID state is only allowed to change from within the init * namespace. Enforce that restriction only if we are trying to change
Signature · 1 line
2.54.0
PATCH v20 3/23 fs,fsverity: add I_VERITY_BUILDING inode state flag
In reply to Andrey AlbershteynEvery filesystem that supports fs-verity keeps a private runtime inode flag meaning Merkle tree is currently being built *_VERITY_IN_PROGRESS. They all serve the same two purposes: reject a concurrent enable with -EBUSY and let the write paths recognize the special writes done while the tree is under construction. Except in BTRFS, which use only the former.
Hoist this flag into the VFS as a new inode state flag, I_VERITY_BUILDING, and manage its lifetime in the generic fs-verity enable path. The flag is set after ->begin_enable_verity() succeeds and cleared after ->end_enable_verity() returns (both on the success and the rollback paths). Add the concurrent-enable guard.
A new helper, fsverity_is_building(), is provided for filesystems to query the flag. No filesystem uses it yet; the per-fs flags are converted in following patches.
--- fs/verity/enable.c | 22 +++++++++++++++++++--- include/linux/fs.h | 7 +++++++ include/linux/fsverity.h | 29 +++++++++++++++++++++++++++-- include/trace/events/writeback.h | 3 ++- 4 files changed, 55 insertions(+), 6 deletions(-)
fs/verity/enable.c +19 -3
@@ -229,13 +229,23 @@ static int enable_verity(struct file *filp,/* * Start enabling verity on this file, serialized by the inode lock. -* Fail if verity is already enabled or is already being enabled. +* Fail if verity is already enabled or is already being enabled. Set +* I_VERITY_BUILDING inode runtime flag which will be cleared when +* enablement succeeded or rolled back. */ inode_lock (inode); -if (IS_VERITY (inode)) +if (IS_VERITY (inode)) { err = -EEXIST ; -else + }else if (fsverity_is_building (inode)) { + err = -EBUSY ; + }else { err = vops->begin_enable_verity (filp); +if (!err) { +spin_lock (&inode->i_lock); +inode_state_set (inode,I_VERITY_BUILDING ); +spin_unlock (&inode->i_lock); + } + }inode_unlock (inode);if (err) goto out;
@@ -291,6 +301,9 @@ static int enable_verity(struct file *filp,*/ inode_lock (inode); err = vops->end_enable_verity (filp, desc, desc_size, params.tree_size); +spin_lock (&inode->i_lock); +inode_state_clear (inode,I_VERITY_BUILDING ); +spin_unlock (&inode->i_lock);inode_unlock (inode);if (err) { fsverity_err(inode, "%ps() failed with err %d",
@@ -317,6 +330,9 @@ static int enable_verity(struct file *filp, rollback:inode_lock (inode); (void )vops->end_enable_verity (filp,NULL ,0 , params.tree_size); +spin_lock (&inode->i_lock); +inode_state_clear (inode,I_VERITY_BUILDING ); +spin_unlock (&inode->i_lock);inode_unlock (inode); goto out; }
include/linux/fs.h +7 -0
@@ -708,6 +708,12 @@ is_uncached_acl(struct posix_acl *acl)* I_LRU_ISOLATING Inode is pinned being isolated from LRU without holding * i_count. * +* I_VERITY_BUILDING fs-verity is being enabled on the inode: the Merkle +* tree is under construction. The file is effectively +* read-only to userspace. Most of the writes are merkle +* tree blocks but inode still can have data writeback +* in-flight. +* * Q: What is the difference between I_WILL_FREE and I_FREEING? * * __I_{SYNC,NEW,LRU_ISOLATING} are used to derive unique addresses to wait
@@ -742,6 +748,7 @@ enum inode_state_flags_enum {I_SYNC_QUEUED = (1U <<17 ),I_PINNING_NETFS_WB = (1U <<18 ),I_METADATA_WRITEBACK = (1U <<19 ), + I_VERITY_BUILDING = (1U << 20), };#define I_DIRTY_INODE (I_DIRTY_SYNC | I_DIRTY_DATASYNC)
include/linux/fsverity.h +27 -2
@@ -36,8 +36,7 @@ struct fsverity_operations {* @filp: a readonly file descriptor for the file * * The filesystem must do any needed filesystem-specific preparations -* for enabling verity, e.g. evicting inline data. It also must return -* -EBUSY if verity is already being enabled on the given file. +* for enabling verity, e.g. evicting inline data. * * i_rwsem is held for write. *
@@ -65,6 +64,10 @@ struct fsverity_operations {* i_rwsem is held for write, but it may have been dropped between * ->begin_enable_verity() and ->end_enable_verity(). * +* I_VERITY_BUILDING flag is set for the duration of the enablement. The +* flag is set right after ->begin_enable_verity() is done and cleared +* after ->end_enable_verity(). +* * Return: 0 on success, -errno on failure */ int (*end_enable_verity)(struct file *filp, const void *desc,
@@ -164,6 +167,23 @@ static inline bool fsverity_active(const struct inode *inode)return false; } +/** +* fsverity_is_building() - is fs-verity being enabled on the inode +* @inode: inode to check +* +* This checks whether the inode's Merkle tree is currently being built. +* While this is the case the file is read-only to userspace and the only writes +* allowed are for the fs-verity metadata, although, inode can still have +* in-flight data writeback. This becomes false when fsverity metadata is fully +* created and written to disk or building failed. +* +* Return: true if fs-verity is being enabled on the inode +*/ +static inline bool fsverity_is_building (const struct inode *inode) +{ +return inode_state_read_once (inode) &I_VERITY_BUILDING ; +} +struct fsverity_info *__fsverity_get_info (const struct inode *inode);/** * fsverity_get_info - get fsverity information for an inode
@@ -217,6 +237,11 @@ static inline bool fsverity_active(const struct inode *inode)return false; } +static inline bool fsverity_is_building (const struct inode *inode) +{ +return false; +} +static inline struct fsverity_info *fsverity_get_info (const struct inode *inode) {return NULL ;
include/trace/events/writeback.h +2 -1
@@ -28,7 +28,8 @@ {I_DONTCACHE, "I_DONTCACHE"}, \ {I_SYNC_QUEUED, "I_SYNC_QUEUED"}, \ {I_PINNING_NETFS_WB, "I_PINNING_NETFS_WB"}, \ - {I_LRU_ISOLATING, "I_LRU_ISOLATING"} \ + {I_LRU_ISOLATING, "I_LRU_ISOLATING"}, \ + {I_VERITY_BUILDING, "I_VERITY_BUILDING"} \ )/* enums need to be exported to user space */
Signature · 1 line
2.54.0
PATCH v20 4/23 fs,fsverity: reject S_APPEND on fsverity inodes
In reply to Andrey Albershteynfs-verity rejects enabling fsverity on append only files. However, there are two cases when FS_IOC_SETFLAGS can turn inode append only.
The first one is right before the lock is taken in the enable_verity(), making original check useless.
The second one is after lock in enable_verity() is unlocked, therefore, when merkle tree is being build and after fs-verity inode is sealed. This one is not critical, it just sets incompatible flags on inode but append-only inode will not get any writes anyway.
Fix it by checking S_APPEND again under the lock, serializing this with vfs_fileattr_set() and reject S_APPEND on fsverity inodes.
--- fs/file_attr.c | 13 +++++++++++++ fs/verity/enable.c | 8 ++++++++ 2 files changed, 21 insertions(+)
fs/file_attr.c +13 -0
@@ -2,6 +2,7 @@#include <linux/fs.h> #include <linux/security.h> #include <linux/fscrypt.h> +#include <linux/fsverity.h> #include <linux/fsnotify.h> #include <linux/fileattr.h> #include <linux/export.h>
@@ -209,6 +210,18 @@ static int fileattr_set_prepare(struct inode *inode, fa->flags &= ~FS_VERITY_FL ; fa->fsx_xflags &= ~FS_XFLAG_VERITY ; +/* +* FS_APPEND_FL is incompatible with fs-verity. FS_IOC_ENABLE_VERITY +* rejects append-only files, also reject turning a verity file +* append-only. +* +* Reject in both cases, after it has been sealed and while its Merkle +* tree is being built. +*/ +if ((fa->flags &FS_APPEND_FL ) && + ((old_ma->flags &FS_VERITY_FL ) ||fsverity_is_building (inode))) +return -EPERM ; +/* * Project Quota ID state is only allowed to change from within the init * namespace. Enforce that restriction only if we are trying to change
fs/verity/enable.c +8 -0
@@ -238,6 +238,14 @@ static int enable_verity(struct file *filp, err = -EEXIST ; }else if (fsverity_is_building (inode)) { err = -EBUSY ; + }else if (IS_APPEND (inode)) { +/* +* The check in fsverity_ioctl_enable() is done without the +* inode lock, so a concurrent FS_IOC_SETFLAGS could have set +* S_APPEND in the meantime. Recheck it here, serialized with +* ->fileattr_set(). +*/ + err = -EPERM ; }else { err = vops->begin_enable_verity (filp);if (!err) {
Signature · 1 line
2.54.0
PATCH v20 5/23 iomap: use fsverity_is_building() to check for fsverity IO
In reply to Andrey AlbershteynRecently, inode gained new I_VERITY_BUILDING flag. This flag indicates that inode is undergoing fsverity metadata construction. Previously, it was indicated by stale iomap flag on the IO, now we can check the inode instead.
--- fs/iomap/buffered-io.c | 11 ++++------- 1 file changed, 4 insertions(+), 7 deletions(-)
fs/iomap/buffered-io.c +4 -7
@@ -1950,16 +1950,13 @@ static int iomap_writeback_range(struct iomap_writepage_ctx *wpc,* i_size, adjust end_pos and zero all data beyond i_size. Don't skip fsverity * folios as those are beyond i_size. */ -static bool iomap_writeback_handle_eof (struct folio *folio, -struct iomap_writepage_ctx *wpc,u64 *end_pos) +static bool iomap_writeback_handle_eof (struct folio *folio,struct inode *inode, +u64 *end_pos) { -struct inode *inode = wpc->inode;u64 isize =i_size_read (inode); -if (wpc->iomap.flags &IOMAP_F_FSVERITY ) { -WARN_ON_ONCE (folio_pos (folio) < isize); +if (fsverity_is_building (inode) &&folio_pos (folio) >= isize)return true; - }if (*end_pos > isize) {size_t poff =offset_in_folio (folio, isize);
@@ -2026,7 +2023,7 @@ int iomap_writeback_folio(struct iomap_writepage_ctx *wpc, struct folio *folio)trace_iomap_writeback_folio (inode, pos,folio_size (folio)); -if (!iomap_writeback_handle_eof (folio, wpc, &end_pos)) +if (!iomap_writeback_handle_eof (folio, inode, &end_pos))return 0 ;WARN_ON_ONCE (end_pos <= pos);
Signature · 1 line
2.54.0
[email protected]
AI reviewRe: [PATCH v20 05/23] iomap: use fsverity_is_building() to check for fsverity IO
Thank you for your contribution! Sashiko AI review found 1 potential issue to consider: - [Low] iomap: missing Fixes trailer for stale wpc->iomap.flags check in writeback --
Thank you for your contribution! Sashiko AI review found 1 potential issue to consider:
- [Low] iomap: missing Fixes trailer for stale wpc->iomap.flags check in writeback
--
commit f4f7538054fc0ce5e97bd1cf5ad27c0075c5ad9c
Author: Andrey Albershteyn <[email protected]>
Quoted 8 lines
iomap: use
fsverity_is_building()to check for fsverity IORecently, inode gained new
I_VERITY_BUILDINGflag. This flag indicates that inode is undergoing fsverity metadata construction. Previously, it was indicated by stale iomap flag on the IO, now we can check the inode instead.Signed-off-by Andrey Albershteyn <[email protected]>
[Severity: Low]
This fixes checking unmapped or stale wpc->iomap.flags in iomap_writeback_handle_eof() before iomap_writeback_range() maps wpc->iomap for the current folio. Should this include:
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=5
PATCH v20 6/23 btrfs: use I_VERITY_BUILDING instead of private flags
In reply to Andrey AlbershteynNow that the VFS tracks an inode whose Merkle tree is under construction via I_VERITY_BUILDING and handles the concurrent-enable guard in the generic fs-verity enable path, drop the inode runtime flags that duplicated this.
btrfs only used BTRFS_INODE_VERITY_IN_PROGRESS internally, so remove it outright.
--- fs/btrfs/btrfs_inode.h | 7 ------- fs/btrfs/verity.c | 12 ++---------- 2 files changed, 2 insertions(+), 17 deletions(-)
fs/btrfs/btrfs_inode.h +0 -7
@@ -78,13 +78,6 @@ enum {* the file range, inode's io_tree). */ BTRFS_INODE_NO_DELALLOC_FLUSH , -/* -* Set when we are working on enabling verity for a file. Computing and -* writing the whole Merkle tree can take a while so we want to prevent -* races where two separate tasks attempt to simultaneously start verity -* on the same file. -*/ -BTRFS_INODE_VERITY_IN_PROGRESS ,/* Set when this inode is a free space inode. */ BTRFS_INODE_FREE_SPACE_INODE,/* Set when there are no capabilities in XATTs for the inode. */
fs/btrfs/verity.c +2 -10
@@ -461,7 +461,6 @@ static int rollback_verity(struct btrfs_inode *inode)btrfs_assert_inode_locked (inode);truncate_inode_pages (inode->vfs_inode.i_mapping, inode->vfs_inode.i_size); -clear_bit (BTRFS_INODE_VERITY_IN_PROGRESS , &inode->runtime_flags); ret =btrfs_drop_verity_items (inode);if (ret) { btrfs_handle_fs_error(root->fs_info, ret,
@@ -513,7 +512,6 @@ static int rollback_verity(struct btrfs_inode *inode)* - mark the inode with the verity flag * - delete the orphan item * - mark the ro compat bit -* - clear the in progress bit * * Returns 0 on success, negative error code on failure. */
@@ -555,7 +553,6 @@ static int finish_verity(struct btrfs_inode *inode, const void *desc, goto end_trans;/* Now that rollback is no longer possible, set S_VERITY. */ btrfs_sync_inode_flags_to_i_flags (inode); -clear_bit (BTRFS_INODE_VERITY_IN_PROGRESS , &inode->runtime_flags);btrfs_set_fs_compat_ro (root->fs_info,VERITY ); end_trans:if (ret)
@@ -570,8 +567,8 @@ static int finish_verity(struct btrfs_inode *inode, const void *desc,* * @filp: file to enable verity on * -* Begin enabling fsverity for the file. We drop any existing verity items, add -* an orphan and set the in progress bit. +* Begin enabling fsverity for the file. We drop any existing verity items and +* add an orphan. * * Returns 0 on success, negative error code on failure. */
@@ -587,9 +584,6 @@ static int btrfs_begin_enable_verity(struct file *filp)if (IS_ENCRYPTED (&inode->vfs_inode))return -EOPNOTSUPP ; -if (test_bit (BTRFS_INODE_VERITY_IN_PROGRESS , &inode->runtime_flags)) -return -EBUSY ; -/* * This should almost never do anything, but theoretically, it's * possible that we failed to enable verity on a file, then were
@@ -606,8 +600,6 @@ static int btrfs_begin_enable_verity(struct file *filp)return PTR_ERR (trans); ret =btrfs_orphan_add (trans, inode); -if (!ret) -set_bit (BTRFS_INODE_VERITY_IN_PROGRESS , &inode->runtime_flags);btrfs_end_transaction (trans);return ret;
Signature · 1 line
2.54.0
PATCH v20 7/23 ext4: use I_VERITY_BUILDING instead of private flags
In reply to Andrey AlbershteynNow that the VFS tracks an inode whose Merkle tree is under construction via I_VERITY_BUILDING and handles the concurrent-enable guard in the generic fs-verity enable path, drop the per-filesystem runtime flags that duplicated this state.
Replace ext4 own helper ext4_verity_in_progress() with fsverity_is_building(), and remove the EXT4_STATE_VERITY_IN_PROGRESS flags. The IS_ENABLED() guard is fine to remove now as fsverity_is_building() has stub for disabled fsverity.
--- fs/ext4/ext4.h | 7 ------- fs/ext4/file.c | 2 +- fs/ext4/inode.c | 17 +++++++++-------- fs/ext4/ioctl.c | 3 +-- fs/ext4/verity.c | 19 ++++++------------- 5 files changed, 17 insertions(+), 31 deletions(-)
fs/ext4/ext4.h +0 -7
@@ -2050,7 +2050,6 @@ enum {EXT4_STATE_MAY_INLINE_DATA ,/* may have in-inode data */ EXT4_STATE_EXT_PRECACHED ,/* extents have been precached */ EXT4_STATE_LUSTRE_EA_INODE ,/* Lustre-style ea_inode */ -EXT4_STATE_VERITY_IN_PROGRESS ,/* building fs-verity Merkle tree */ EXT4_STATE_FC_COMMITTING ,/* Fast commit ongoing */ EXT4_STATE_FC_FLUSHING_DATA ,/* Fast commit flushing data */ EXT4_STATE_ORPHAN_FILE, /* Inode orphaned in orphan file */
@@ -2129,12 +2128,6 @@ static inline int ext4_inode_state_wait_bit(int bit)#define EXT4_SB (sb) (sb)#endif -static inline bool ext4_verity_in_progress (struct inode *inode) -{ -return IS_ENABLED (CONFIG_FS_VERITY ) && -ext4_test_inode_state (inode,EXT4_STATE_VERITY_IN_PROGRESS ); -} -#define NEXT_ORPHAN (inode) EXT4_I(inode)->i_dtime/*
fs/ext4/file.c +1 -1
@@ -318,7 +318,7 @@ static ssize_t ext4_write_checks(struct kiocb *iocb, struct iov_iter *from)* partial block that beyond the existing EOF, as it may contains * stale data written through mmap. */ -if (iocb->ki_pos > old_size && !ext4_verity_in_progress (inode)) { +if (iocb->ki_pos > old_size && !fsverity_is_building (inode)) {if (iocb->ki_flags &IOCB_NOWAIT )return -EAGAIN ;
fs/ext4/inode.c +9 -8
@@ -42,6 +42,7 @@#include <linux/bitops.h> #include <linux/iomap.h> #include <linux/iversion.h> +#include <linux/fsverity.h> #include "ext4_jbd2.h" #include "ext4_extents.h"
@@ -506,7 +507,7 @@ void ext4_check_map_extents_env(struct inode *inode)is_special_ino (inode->i_sb, inode->i_ino) || (inode_state_read_once (inode) & (I_FREEING |I_WILL_FREE |I_NEW )) ||ext4_test_inode_flag (inode,EXT4_INODE_EA_INODE ) || - ext4_verity_in_progress(inode)) + fsverity_is_building(inode)) return; WARN_ON_ONCE(!inode_is_locked(inode) &&
@@ -1493,7 +1494,7 @@ static int ext4_write_begin(const struct kiocb *iocb,if (ret) {bool extended = (pos + len > inode->i_size) && - !ext4_verity_in_progress (inode); + !fsverity_is_building (inode);folio_unlock (folio);/*
@@ -1560,7 +1561,7 @@ static int ext4_write_end(const struct kiocb *iocb,loff_t old_size = inode->i_size;int ret =0 , ret2;int i_size_changed =0 ; -bool verity =ext4_verity_in_progress (inode); +bool verity =fsverity_is_building (inode);trace_ext4_write_end (inode, pos, len, copied);
@@ -1665,7 +1666,7 @@ static int ext4_journalled_write_end(const struct kiocb *iocb,int partial =0 ;unsigned from, to;int size_changed =0 ; -bool verity =ext4_verity_in_progress (inode); +bool verity =fsverity_is_building (inode);trace_ext4_journalled_write_end (inode, pos, len, copied); from = pos & (PAGE_SIZE -1 );
@@ -2210,7 +2211,7 @@ static void mpage_submit_folio(struct mpage_da_data *mpd, struct folio *folio) size =i_size_read (mpd->inode); len =folio_size (folio);if (folio_pos (folio) + len > size && - !ext4_verity_in_progress (mpd->inode)) + !fsverity_is_building (mpd->inode)) len = size & (len -1 );ext4_bio_write_folio (&mpd->io_submit, folio, len); }
@@ -2300,7 +2301,7 @@ static int mpage_process_page_bufs(struct mpage_da_data *mpd,ext4_lblk_t blocks = (i_size_read (inode) +i_blocksize (inode) -1 ) >> inode->i_blkbits; -if (ext4_verity_in_progress (inode)) +if (fsverity_is_building (inode)) blocks =EXT_MAX_BLOCKS ; do {
@@ -2706,7 +2707,7 @@ static int mpage_journal_page_buffers(handle_t *handle, mpd->wbc->nr_to_write -=folio_nr_pages (folio);if (folio_pos (folio) + len > size && - !ext4_verity_in_progress (inode)) + !fsverity_is_building (inode)) len = size & (len -1 );return ext4_journal_folio_buffers (handle, folio, len);
@@ -3271,7 +3272,7 @@ static int ext4_da_write_begin(const struct kiocb *iocb, index = pos >>PAGE_SHIFT ; -if (ext4_nonda_switch (inode->i_sb) ||ext4_verity_in_progress (inode)) { +if (ext4_nonda_switch (inode->i_sb) ||fsverity_is_building (inode)) { *fsdata = (void *)FALL_BACK_TO_NONDELALLOC ;return ext4_write_begin (iocb, mapping, pos, len, foliop, fsdata);
fs/ext4/ioctl.c +1 -2
@@ -575,8 +575,7 @@ static bool dax_compatible(struct inode *inode, unsigned int oldflags,if (flags &EXT4_DAX_FL ) {if ((oldflags &EXT4_DAX_MUT_EXCL ) || -ext4_test_inode_state (inode, -EXT4_STATE_VERITY_IN_PROGRESS )) { +fsverity_is_building (inode)) {return false; } }
fs/ext4/verity.c +6 -13
@@ -83,9 +83,6 @@ static int ext4_begin_enable_verity(struct file *filp)if (IS_DAX (inode) ||ext4_test_inode_flag (inode,EXT4_INODE_DAX ))return -EINVAL ; -if (ext4_verity_in_progress (inode)) -return -EBUSY ; -/* * Since the file was opened readonly, we have to initialize the jbd * inode and quotas here and not rely on ->open() doing it. This must
@@ -123,8 +120,6 @@ static int ext4_begin_enable_verity(struct file *filp)return PTR_ERR (handle); err =ext4_orphan_add (handle, inode); -if (err ==0 ) -ext4_set_inode_state (inode,EXT4_STATE_VERITY_IN_PROGRESS );ext4_journal_stop (handle);return err;
@@ -185,10 +180,10 @@ static int ext4_end_enable_verity(struct file *filp, const void *desc, goto cleanup;/* -* Write all pages (both data and verity metadata). Note that this must -* happen before clearing EXT4_STATE_VERITY_IN_PROGRESS; otherwise pages -* beyond i_size won't be written properly. For crash consistency, this -* also must happen before the verity inode flag gets persisted. +* Write all pages (both data and verity metadata). Note that this must +* happen before I_VERITY_BUILDING is cleared; otherwise pages beyond +* i_size won't be written properly. For crash consistency, this also +* must happen before the verity inode flag gets persisted. */ err =filemap_write_and_wait (inode->i_mapping);if (err)
@@ -224,7 +219,6 @@ static int ext4_end_enable_verity(struct file *filp, const void *desc,/* Now that rollback is no longer possible, set S_VERITY. */ ext4_set_inode_flags (inode, false); -ext4_clear_inode_state (inode,EXT4_STATE_VERITY_IN_PROGRESS );return 0 ; stop_and_cleanup:
@@ -234,13 +228,12 @@ static int ext4_end_enable_verity(struct file *filp, const void *desc,/* * Verity failed to be enabled, so clean up by truncating any verity * metadata that was written beyond i_size (both from cache and from -* disk), removing the inode from the orphan list (if it wasn't done -* already), and clearing EXT4_STATE_VERITY_IN_PROGRESS. +* disk) and removing the inode from the orphan list (if it wasn't done +* already). */ truncate_inode_pages (inode->i_mapping, inode->i_size);ext4_truncate (inode);ext4_orphan_del (NULL , inode); -ext4_clear_inode_state (inode,EXT4_STATE_VERITY_IN_PROGRESS );return err; }
Signature · 1 line
2.54.0
PATCH v20 8/23 f2fs: use I_VERITY_BUILDING instead of private flags
In reply to Andrey AlbershteynNow that the VFS tracks an inode whose Merkle tree is under construction via I_VERITY_BUILDING and handles the concurrent-enable guard in the generic fs-verity enable path, drop the inode runtime flags that duplicated this state.
Replace f2fs own helper f2fs_verity_in_progress() with fsverity_is_building(), and remove the FI_VERITY_IN_PROGRESS flag. It's fine to remove IS_ENABLED() guard as fsverity_is_building() has stub.
--- fs/f2fs/data.c | 10 +++++----- fs/f2fs/f2fs.h | 7 ------- fs/f2fs/verity.c | 12 +++--------- 3 files changed, 8 insertions(+), 21 deletions(-)
fs/f2fs/data.c +5 -5
@@ -3356,7 +3356,7 @@ int f2fs_write_single_data_page(struct folio *folio, int *submitted, goto redirty_out;if (folio->index < end_index || -f2fs_verity_in_progress (inode) || +fsverity_is_building (inode) || compr_blocks) goto write;
@@ -3889,7 +3889,7 @@ void f2fs_write_failed(struct inode *inode, loff_t to)return ;/* In the fs-verity case, f2fs_end_enable_verity() does the truncate */ -if (to > i_size && !f2fs_verity_in_progress (inode)) { +if (to > i_size && !fsverity_is_building (inode)) {f2fs_down_write (&F2FS_I (inode)->i_gc_rwsem[WRITE ]);filemap_invalidate_lock (inode->i_mapping);
@@ -4205,7 +4205,7 @@ static int f2fs_write_begin(const struct kiocb *iocb,return 0 ;if (!(pos & (PAGE_SIZE -1 )) && (pos + len) >=i_size_read (inode) && - !f2fs_verity_in_progress (inode)) { + !fsverity_is_building (inode)) {folio_zero_segment (folio, len,folio_size (folio));return 0 ; }
@@ -4279,7 +4279,7 @@ static int f2fs_write_end(const struct kiocb *iocb,f2fs_update_time (F2FS_I_SB (inode),REQ_TIME );if (pos + copied >i_size_read (inode) && - !f2fs_verity_in_progress (inode)) + !fsverity_is_building (inode))f2fs_i_size_write (inode, pos + copied);return copied; }
@@ -4294,7 +4294,7 @@ static int f2fs_write_end(const struct kiocb *iocb,folio_set_f2fs_atomic (folio);if (pos + copied >i_size_read (inode) && - !f2fs_verity_in_progress (inode)) { + !fsverity_is_building (inode)) {f2fs_i_size_write (inode, pos + copied);if (f2fs_is_atomic_file (inode)) f2fs_i_size_write(F2FS_I(inode)->cow_inode,
fs/f2fs/f2fs.h +0 -7
@@ -926,7 +926,6 @@ enum {FI_EXTRA_ATTR ,/* indicate file has extra attribute */ FI_PROJ_INHERIT ,/* indicate file inherits projectid */ FI_PIN_FILE ,/* indicate file should not be gced */ -FI_VERITY_IN_PROGRESS ,/* building fs-verity Merkle tree */ FI_COMPRESSED_FILE ,/* indicate file's data can be compressed */ FI_COMPRESS_CORRUPT ,/* indicate compressed cluster is corrupted */ FI_MMAP_FILE, /* indicate file was mmapped */
@@ -3536,12 +3535,6 @@ static inline void clear_inode_flag(struct inode *inode, int flag)__mark_inode_dirty_flag (inode, flag, false); } -static inline bool f2fs_verity_in_progress (struct inode *inode) -{ -return IS_ENABLED (CONFIG_FS_VERITY ) && -is_inode_flag_set (inode,FI_VERITY_IN_PROGRESS ); -} -static inline void set_acl_inode (struct inode *inode,umode_t mode) {F2FS_I (inode)->i_acl_mode = mode;
fs/f2fs/verity.c +3 -9
@@ -94,9 +94,6 @@ static int f2fs_begin_enable_verity(struct file *filp)struct inode *inode =file_inode (filp);int err; -if (f2fs_verity_in_progress (inode)) -return -EBUSY ; -if (f2fs_is_atomic_file (inode))return -EOPNOTSUPP ;
@@ -113,7 +110,6 @@ static int f2fs_begin_enable_verity(struct file *filp)if (err)return err; -set_inode_flag (inode,FI_VERITY_IN_PROGRESS );return 0 ; }
@@ -143,8 +139,8 @@ static int f2fs_end_enable_verity(struct file *filp, const void *desc, goto cleanup;/* -* Write all pages (both data and verity metadata). Note that this must -* happen before clearing FI_VERITY_IN_PROGRESS; otherwise pages beyond +* Write all pages (both data and verity metadata). Note that this must +* happen before I_VERITY_BUILDING is cleared; otherwise pages beyond * i_size won't be written properly. For crash consistency, this also * must happen before the verity inode flag gets persisted. */
@@ -164,14 +160,13 @@ static int f2fs_end_enable_verity(struct file *filp, const void *desc,f2fs_set_inode_flags (inode);f2fs_mark_inode_dirty_sync (inode, true); -clear_inode_flag (inode,FI_VERITY_IN_PROGRESS );return 0 ; cleanup:/* * Verity failed to be enabled, so clean up by truncating any verity * metadata that was written beyond i_size (both from cache and from -* disk) and clearing FI_VERITY_IN_PROGRESS. +* disk). * * Taking i_gc_rwsem[WRITE] is needed to stop f2fs garbage collection * from re-instantiating cached pages we are truncating (since unlike
@@ -186,7 +181,6 @@ static int f2fs_end_enable_verity(struct file *filp, const void *desc,set_sbi_flag (sbi,SBI_NEED_FSCK ); }f2fs_up_write (&F2FS_I (inode)->i_gc_rwsem[WRITE ]); -clear_inode_flag (inode,FI_VERITY_IN_PROGRESS );return err ?: err2; }
Signature · 1 line
2.54.0
PATCH v20 9/23 xfs: introduce fsverity on-disk changes
In reply to Andrey AlbershteynIntroduce XFS_DIFLAG2_VERITY for inodes with fsverity. This flag indicates that inode has fs-verity enabled (i.e. descriptor exist, tree is built and file is read-only).
Introduce XFS_SB_FEAT_RO_COMPAT_VERITY for filesystems having fsverity inodes. As on-disk changes applies to fsverity inodes only, let older kernels read-only access. This will be enabled in the further patch after full fsverity support.
--- fs/xfs/libxfs/xfs_format.h | 26 +++++++++++++++++++++++++- fs/xfs/libxfs/xfs_inode_buf.c | 8 ++++++++ fs/xfs/libxfs/xfs_inode_util.c | 5 ++++- fs/xfs/libxfs/xfs_sb.c | 2 ++ fs/xfs/xfs_iops.c | 5 ++++- fs/xfs/xfs_mount.h | 2 ++ 6 files changed, 45 insertions(+), 3 deletions(-)
fs/xfs/libxfs/xfs_format.h +25 -1
@@ -374,6 +374,7 @@ xfs_sb_has_compat_feature(#define XFS_SB_FEAT_RO_COMPAT_RMAPBT (1 << 1)/* reverse map btree */ #define XFS_SB_FEAT_RO_COMPAT_REFLINK (1 << 2)/* reflinked files */ #define XFS_SB_FEAT_RO_COMPAT_INOBTCNT (1 << 3)/* inobt block counts */ +#define XFS_SB_FEAT_RO_COMPAT_VERITY (1 << 4)/* fs-verity */ #define XFS_SB_FEAT_RO_COMPAT_ALL \ (XFS_SB_FEAT_RO_COMPAT_FINOBT | \ XFS_SB_FEAT_RO_COMPAT_RMAPBT | \
@@ -1234,16 +1235,21 @@ static inline void xfs_dinode_put_rdev(struct xfs_dinode *dip, xfs_dev_t rdev)*/ #define XFS_DIFLAG2_METADATA_BIT 5 +/* inodes sealed with fs-verity */ +#define XFS_DIFLAG2_VERITY_BIT 6 +#define XFS_DIFLAG2_DAX (1ULL << XFS_DIFLAG2_DAX_BIT)#define XFS_DIFLAG2_REFLINK (1ULL << XFS_DIFLAG2_REFLINK_BIT)#define XFS_DIFLAG2_COWEXTSIZE (1ULL << XFS_DIFLAG2_COWEXTSIZE_BIT)#define XFS_DIFLAG2_BIGTIME (1ULL << XFS_DIFLAG2_BIGTIME_BIT)#define XFS_DIFLAG2_NREXT64 (1ULL << XFS_DIFLAG2_NREXT64_BIT)#define XFS_DIFLAG2_METADATA (1ULL << XFS_DIFLAG2_METADATA_BIT) +#define XFS_DIFLAG2_VERITY (1ULL << XFS_DIFLAG2_VERITY_BIT)#define XFS_DIFLAG2_ANY \ (XFS_DIFLAG2_DAX | XFS_DIFLAG2_REFLINK | XFS_DIFLAG2_COWEXTSIZE | \ - XFS_DIFLAG2_BIGTIME | XFS_DIFLAG2_NREXT64 | XFS_DIFLAG2_METADATA) + XFS_DIFLAG2_BIGTIME | XFS_DIFLAG2_NREXT64 | XFS_DIFLAG2_METADATA | \ + XFS_DIFLAG2_VERITY)static inline bool xfs_dinode_has_bigtime (const struct xfs_dinode *dip) {
@@ -2031,4 +2037,22 @@ struct xfs_acl {#define SGI_ACL_FILE_SIZE (sizeof(SGI_ACL_FILE)-1)#define SGI_ACL_DEFAULT_SIZE (sizeof(SGI_ACL_DEFAULT)-1) +/* +* fs-verity stores the Merkle tree in the data fork past EOF, so the file data +* blocks and the tree blocks share the inode's xfs_fileoff_t address space. +* Cap a verity file at half of the addressable file-offset range (in filesystem +* blocks) so the Merkle tree always fits in the other half. +*/ +#define XFS_FSVERITY_LARGEST_FILE_FSB (mp) \ + (XFS_B_TO_FSBT((mp), MAX_LFS_FILESIZE) >> 1) + +/* +* Alignment of the fsverity metadata placement. This is largest supported PAGE +* SIZE for fsverity. This is used to space out data and metadata in page cache. +* The spacing is necessary for non-exposure of metadata to userspace and +* correct merkle tree synthesis in the iomap. +*/ +#define XFS_FSVERITY_START_ALIGN (65536) + +#endif /* __XFS_FORMAT_H__ */
fs/xfs/libxfs/xfs_inode_buf.c +8 -0
@@ -758,6 +758,14 @@ xfs_dinode_verify( !xfs_has_rtreflink(mp))return __this_address; +/* only regular files can have fsverity */ +if (flags2 &XFS_DIFLAG2_VERITY ) { +if (!xfs_has_verity (mp)) +return __this_address; +if (!S_ISREG (mode)) +return __this_address; + } +if (xfs_has_zoned (mp) && dip->di_metatype ==cpu_to_be16 (XFS_METAFILE_RTRMAP )) {if (be32_to_cpu (dip->di_used_blocks) > mp->m_sb.sb_rgextents)
fs/xfs/libxfs/xfs_inode_util.c +4 -1
@@ -73,7 +73,8 @@ xfs_flags2diflags2(uint64_t di_flags2 = (ip->i_diflags2 & (XFS_DIFLAG2_REFLINK |XFS_DIFLAG2_BIGTIME | -XFS_DIFLAG2_NREXT64 )); +XFS_DIFLAG2_NREXT64 | +XFS_DIFLAG2_VERITY ));if (xflags &FS_XFLAG_DAX ) di_flags2 |=XFS_DIFLAG2_DAX ;
@@ -125,6 +126,8 @@ xfs_ip2xflags( flags |=FS_XFLAG_DAX ;if (ip->i_diflags2 &XFS_DIFLAG2_COWEXTSIZE ) flags |=FS_XFLAG_COWEXTSIZE ; +if (ip->i_diflags2 &XFS_DIFLAG2_VERITY ) + flags |=FS_XFLAG_VERITY ; }if (xfs_inode_has_attr_fork (ip))
fs/xfs/libxfs/xfs_sb.c +2 -0
@@ -164,6 +164,8 @@ xfs_sb_version_to_features( features |=XFS_FEAT_REFLINK ;if (sbp->sb_features_ro_compat &XFS_SB_FEAT_RO_COMPAT_INOBTCNT ) features |=XFS_FEAT_INOBTCNT ; +if (sbp->sb_features_ro_compat &XFS_SB_FEAT_RO_COMPAT_VERITY ) + features |=XFS_FEAT_VERITY ;if (sbp->sb_features_incompat &XFS_SB_FEAT_INCOMPAT_FTYPE ) features |=XFS_FEAT_FTYPE ;if (sbp->sb_features_incompat &XFS_SB_FEAT_INCOMPAT_SPINODES )
fs/xfs/xfs_iops.c +4 -1
@@ -1419,12 +1419,15 @@ xfs_diflags_to_iflags( flags |=S_NOATIME ;if (init &&xfs_inode_should_enable_dax (ip)) flags |=S_DAX ; +if (xflags &FS_XFLAG_VERITY ) + flags |=S_VERITY ;/* * S_DAX can only be set during inode initialization and is never set by * the VFS, so we cannot mask off S_DAX in i_flags. */ - inode->i_flags &= ~(S_IMMUTABLE |S_APPEND |S_SYNC |S_NOATIME ); + inode->i_flags &= + ~(S_IMMUTABLE |S_APPEND |S_SYNC |S_NOATIME |S_VERITY ); inode->i_flags |= flags; }
fs/xfs/xfs_mount.h +2 -0
@@ -403,6 +403,7 @@ typedef struct xfs_mount {#define XFS_FEAT_EXCHANGE_RANGE (1ULL << 27)/* exchange range */ #define XFS_FEAT_METADIR (1ULL << 28)/* metadata directory tree */ #define XFS_FEAT_ZONED (1ULL << 29)/* zoned RT device */ +#define XFS_FEAT_VERITY (1ULL << 30)/* fs-verity */ /* Mount features */ #define XFS_FEAT_NOLIFETIME (1ULL << 47)/* disable lifetime hints */
@@ -460,6 +461,7 @@ __XFS_HAS_FEAT(exchange_range, EXCHANGE_RANGE) __XFS_HAS_FEAT(metadir, METADIR) __XFS_HAS_FEAT(zoned, ZONED) __XFS_HAS_FEAT(nolifetime, NOLIFETIME) +__XFS_HAS_FEAT(verity, VERITY)static inline bool xfs_has_rtgroups (const struct xfs_mount *mp) {
Signature · 1 line
2.54.0
PATCH v20 10/23 xfs: don't allow to enable DAX on fs-verity sealed inode
In reply to Andrey Albershteynfs-verity doesn't support DAX. Forbid filesystem to enable DAX on inodes which already have fs-verity enabled. The opposite is checked when fs-verity is enabled, it won't be enabled if DAX is.
--- fs/xfs/xfs_iops.c | 2 ++ 1 file changed, 2 insertions(+)
fs/xfs/xfs_iops.c +2 -0
@@ -1391,6 +1391,8 @@ xfs_inode_should_enable_dax(return false;if (!xfs_inode_supports_dax (ip))return false; +if (ip->i_diflags2 &XFS_DIFLAG2_VERITY ) +return false;if (xfs_has_dax_always (ip->i_mount))return true;if (ip->i_diflags2 &XFS_DIFLAG2_DAX )
Signature · 1 line
2.54.0
PATCH v20 11/23 xfs: disable direct read path for fs-verity files
In reply to Andrey AlbershteynThe direct path is not supported on verity files. Attempts to use direct I/O path on such files should fall back to buffered I/O path.
Add a fall back to buffered I/O at two place, in a common fast path and latter when lock is acquired. The second check prevents TOCTOU issue with reading fsverity_active() status and resetting IOCB_DIRECT flag.
If one thread saw fsverity_active() to be false, and then second thread acquired XFS_IOLOCK_EXCL and enabled fsverity. The first thread will go through the DIO path.
--- fs/xfs/xfs_file.c | 61 +++++++++++++++++++++++++++++++---------------- 1 file changed, 41 insertions(+), 20 deletions(-)
fs/xfs/xfs_file.c +41 -20
@@ -38,6 +38,7 @@#include <linux/mount.h> #include <linux/filelock.h> #include <linux/bio-integrity.h> +#include <linux/fsverity.h> static const struct vm_operations_struct xfs_file_vm_ops;
@@ -189,6 +190,25 @@ static const struct iomap_dio_ops xfs_dio_read_bounce_ops = { .bio_set = &iomap_ioend_bioset, }; +STATIC ssize_t +xfs_file_buffered_read ( +struct kiocb *iocb, +struct iov_iter *to) +{ +struct xfs_inode *ip =XFS_I (file_inode (iocb->ki_filp)); +ssize_t ret; + +trace_xfs_file_buffered_read (iocb, to); + + ret =xfs_ilock_iocb (iocb,XFS_IOLOCK_SHARED ); +if (ret) +return ret; + ret =generic_file_read_iter (iocb, to); +xfs_iunlock (ip,XFS_IOLOCK_SHARED ); + +return ret; +} + STATIC ssize_t xfs_file_dio_read( struct kiocb *iocb,
@@ -207,6 +227,17 @@ xfs_file_dio_read( ret =xfs_ilock_iocb (iocb,XFS_IOLOCK_SHARED );if (ret)return ret; + +/* +* Re-check verity status after acquiring lock. This prevents TOCTOU in +* xfs_file_read_iter() while falling back from DIO to buffered I/O as +* now we are holding a lock +*/ +if (fsverity_active (VFS_I (ip))) { +xfs_iunlock (ip,XFS_IOLOCK_SHARED ); + iocb->ki_flags &= ~IOCB_DIRECT ; +return xfs_file_buffered_read (iocb, to); + }if (mapping_stable_writes (iocb->ki_filp->f_mapping)) { ret =iomap_dio_rw (iocb, to, xfs_read_iomap_next, &xfs_dio_read_bounce_ops,0 ,NULL ,0 );
@@ -226,7 +257,8 @@ xfs_file_dax_read(struct kiocb *iocb, struct iov_iter *to) { - struct xfs_inode *ip = XFS_I(iocb->ki_filp->f_mapping->host); + struct inode *inode = iocb->ki_filp->f_mapping->host; +struct xfs_inode *ip =XFS_I (inode);ssize_t ret =0 ;trace_xfs_file_dax_read (iocb, to);
@@ -244,25 +276,6 @@ xfs_file_dax_read(return ret; } -STATIC ssize_t -xfs_file_buffered_read ( -struct kiocb *iocb, -struct iov_iter *to) -{ -struct xfs_inode *ip =XFS_I (file_inode (iocb->ki_filp)); -ssize_t ret; - -trace_xfs_file_buffered_read (iocb, to); - - ret =xfs_ilock_iocb (iocb,XFS_IOLOCK_SHARED ); -if (ret) -return ret; - ret =generic_file_read_iter (iocb, to); -xfs_iunlock (ip,XFS_IOLOCK_SHARED ); - -return ret; -} - STATIC ssize_t xfs_file_read_iter( struct kiocb *iocb,
@@ -277,6 +290,14 @@ xfs_file_read_iter(if (xfs_is_shutdown (mp))return -EIO ; +/* +* In case fs-verity is enabled, we also fallback to the buffered read +* from the direct read path. Therefore, IOCB_DIRECT is set and need to +* be cleared (see generic_file_read_iter()) +*/ +if (fsverity_active (inode)) + iocb->ki_flags &= ~IOCB_DIRECT ; +if (IS_DAX (inode)) ret =xfs_file_dax_read (iocb, to);else if (iocb->ki_flags &IOCB_DIRECT )
Signature · 1 line
2.54.0
PATCH v20 12/23 xfs: don't report dio_mem_align and dio_offset_align for fsverity files
In reply to Andrey AlbershteynSashiko.dev reported that while fsverity files falls back to the buffered IO for Direct I/O, they should not report non-zero values in dio_mem_align and dio_offset_align, meaning it's not supported.
The STATX_DIOALIGN | STATX_DIO_READ_ALIGN flags are used to explicitly report if DIO is supported or not. They can not indicate that fallback to the buffered IO is used in this case. The zero alignment values also mean that DIO is not supported on this file, see statx(2).
--- fs/xfs/xfs_iops.c | 13 +++++++++++++ 1 file changed, 13 insertions(+)
fs/xfs/xfs_iops.c +13 -0
@@ -35,6 +35,7 @@#include <linux/security.h> #include <linux/iversion.h> #include <linux/fiemap.h> +#include <linux/fsverity.h> /* * Directories have different lock order w.r.t. mmap_lock compared to regular
@@ -579,6 +580,18 @@ xfs_report_dioalign(struct block_device *bdev = target->bt_bdev; stat->result_mask |=STATX_DIOALIGN |STATX_DIO_READ_ALIGN ; + +/* +* The STATX_DIOALIGN | STATX_DIO_READ_ALIGN flags are used to +* explicitly report if DIO is supported or not. They can not indicate +* that fallback to the buffered IO is used for fsverity. +* +* Return zero alignment values which also mean that DIO is not +* supported on this file, see statx(2). +*/ +if (fsverity_active (VFS_I (ip))) +return ; + stat->dio_mem_align =bdev_dma_alignment (bdev) +1 ;/*
Signature · 1 line
2.54.0
PATCH v20 13/23 xfs: handle fsverity I/O in write/read path
In reply to Andrey AlbershteynFor write path set IOMAP_F_FSVERITY flag telling iomap to not update inode size and to not skip folios beyond EOF.
For read path let iomap know that we are reading fsverity metadata. So, treat holes in the tree as request to synthesize tree blocks and hole after descriptor as end of the fsverity region.
--- fs/xfs/Makefile | 1 + fs/xfs/libxfs/xfs_bmap.c | 4 +-- fs/xfs/libxfs/xfs_verity.c | 20 +++++++++++++ fs/xfs/libxfs/xfs_verity.h | 13 ++++++++ fs/xfs/xfs_aops.c | 6 ++-- fs/xfs/xfs_iomap.c | 61 ++++++++++++++++++++++++++++---------- fs/xfs/xfs_iomap.h | 5 ++-- fs/xfs/xfs_pnfs.c | 2 +- 8 files changed, 89 insertions(+), 23 deletions(-) create mode 100644 fs/xfs/libxfs/xfs_verity.c create mode 100644 fs/xfs/libxfs/xfs_verity.h
fs/xfs/Makefile +1 -0
@@ -59,6 +59,7 @@ xfs-y += $(addprefix libxfs/, \ xfs_trans_resv.o \ xfs_trans_space.o \ xfs_types.o \ + xfs_verity.o \ ) # xfs_rtbitmap is shared with libxfs xfs-$(CONFIG_XFS_RT) += $(addprefix libxfs/, \
fs/xfs/libxfs/xfs_bmap.c +2 -2
@@ -4414,7 +4414,7 @@ xfs_bmapi_convert_one_delalloc(* the extent. Just return the real extent at this offset. */ if (!isnullstartblock (bma.got.br_startblock)) { -xfs_bmbt_to_iomap (ip, iomap, &bma.got,0 , flags, +xfs_bmbt_to_iomap (ip, iomap, &bma.got, offset,0 , flags,xfs_iomap_inode_sequence (ip, flags));if (seq) *seq =READ_ONCE (ifp->if_seq);
@@ -4462,7 +4462,7 @@ xfs_bmapi_convert_one_delalloc(XFS_STATS_INC (mp, xs_xstrat_quick);ASSERT (!isnullstartblock (bma.got.br_startblock)); -xfs_bmbt_to_iomap (ip, iomap, &bma.got,0 , flags, +xfs_bmbt_to_iomap (ip, iomap, &bma.got, offset,0 , flags,xfs_iomap_inode_sequence (ip, flags));if (seq) *seq =READ_ONCE (ifp->if_seq);
fs/xfs/libxfs/xfs_verity.c new file +20 -0
@@ -0,0 +1,20 @@ +// SPDX-License-Identifier: GPL-2.0 +/* +* Copyright (C) 2026 Red Hat, Inc. +*/ +#include "xfs_platform.h" +#include "xfs_fs.h" +#include "xfs_shared.h" +#include "xfs_format.h" +#include "xfs_log_format.h" +#include "xfs_trans_resv.h" +#include "xfs_mount.h" +#include "xfs_inode.h" +#include "xfs_verity.h" + +loff_t +xfs_fsverity_metadata_offset ( +const struct xfs_inode *ip) +{ +return round_up (i_size_read (VFS_IC (ip)),XFS_FSVERITY_START_ALIGN ); +}
fs/xfs/libxfs/xfs_verity.h new file +13 -0
@@ -0,0 +1,13 @@ +// SPDX-License-Identifier: GPL-2.0 +/* +* Copyright (C) 2026 Red Hat, Inc. +*/ +#ifndef __XFS_VERITY_H__ +#define __XFS_VERITY_H__ + +#include "xfs_platform.h" +#include <linux/fsverity.h> + +loff_t xfs_fsverity_metadata_offset (const struct xfs_inode *ip); + +#endif /* __XFS_VERITY_H__ */
fs/xfs/xfs_aops.c +4 -2
@@ -23,6 +23,7 @@#include "xfs_ioend.h" #include "xfs_zone_alloc.h" #include "xfs_rtgroup.h" +#include "xfs_verity.h" struct xfs_writepage_ctx {struct iomap_writepage_ctx ctx;
@@ -265,7 +266,8 @@ xfs_map_blocks( isnullstartblock(imap.br_startblock)) goto allocate_blocks; -xfs_bmbt_to_iomap (ip, &wpc->iomap, &imap,0 ,0 ,XFS_WPC (wpc)->data_seq); +xfs_bmbt_to_iomap (ip, &wpc->iomap, &imap, offset,0 ,0 , +XFS_WPC (wpc)->data_seq);trace_xfs_map_blocks_found (ip, offset, count, whichfork, &imap);return 0 ; allocate_blocks:
@@ -441,7 +443,7 @@ xfs_zoned_map_blocks( imap.br_startblock =HOLESTARTBLOCK ; imap.br_state =XFS_EXT_NORM ;xfs_iunlock (ip,XFS_ILOCK_EXCL ); -xfs_bmbt_to_iomap (ip, &wpc->iomap, &imap,0 ,0 ,0 ); +xfs_bmbt_to_iomap (ip, &wpc->iomap, &imap, offset,0 ,0 ,0 );return 0 ; } end_fsb =min (end_fsb, imap.br_startoff + imap.br_blockcount);
fs/xfs/xfs_iomap.c +45 -16
168 lines not on this page · raw message
fs/xfs/xfs_iomap.h +3 -2
@@ -22,8 +22,9 @@ xfs_fileoff_t xfs_iomap_eof_align_last_fsb(struct xfs_inode *ip,u64 xfs_iomap_inode_sequence (struct xfs_inode *ip,u16 iomap_flags);int xfs_bmbt_to_iomap (struct xfs_inode *ip,struct iomap *iomap, -struct xfs_bmbt_irec *imap,unsigned int mapping_flags, -u16 iomap_flags,u64 sequence_cookie); +struct xfs_bmbt_irec *imap,loff_t offset, +unsigned int mapping_flags,u16 iomap_flags, +u64 sequence_cookie);int xfs_zero_range (struct xfs_inode *ip,loff_t pos,loff_t len,struct xfs_zone_alloc_ctx *ac,bool *did_zero);
fs/xfs/xfs_pnfs.c +1 -1
@@ -258,7 +258,7 @@ xfs_fs_map_blocks( }xfs_iunlock (ip,XFS_IOLOCK_EXCL |XFS_MMAPLOCK_EXCL ); - error =xfs_bmbt_to_iomap (ip, iomap, &imap,0 ,0 , seq); + error =xfs_bmbt_to_iomap (ip, iomap, &imap, offset,0 ,0 , seq); *device_generation = mp->m_generation;return error; out_unlock:
Signature · 1 line
2.54.0
[email protected]
AI reviewRe: [PATCH v20 13/23] xfs: handle fsverity I/O in write/read path
Thank you for your contribution! Sashiko AI review found 1 potential issue to consider: - [Medium] xfs: signed loff_t overflow in xfs_fsverity_metadata_offset() --
Thank you for your contribution! Sashiko AI review found 1 potential issue to consider:
- [Medium] xfs: signed loff_t overflow in xfs_fsverity_metadata_offset() --
commit 4a03a70110386ebb0ec2056edfff5268071a3316
Author: Andrey Albershteyn <[email protected]>
xfs: handle fsverity I/O in write/read path
This commit passes the mapping offset to xfs_bmbt_to_iomap() and sets IOMAP_F_FSVERITY when the offset falls within the inode's fs-verity metadata region.
Quoted 30 lines
fs/xfs/libxfs/xfs_verity.c new file +20
-0@@ -0,0 +1,20 @@ +// SPDX-License-Identifier: GPL-2.0 +/* +* Copyright (C) 2026 Red Hat, Inc. +*/ +#include "xfs_platform.h" +#include "xfs_fs.h" +#include "xfs_shared.h" +#include "xfs_format.h" +#include "xfs_log_format.h" +#include "xfs_trans_resv.h" +#include "xfs_mount.h" +#include "xfs_inode.h" +#include "xfs_verity.h" + +loff_t +xfs_fsverity_metadata_offset ( +const struct xfs_inode *ip) +{ +return round_up (i_size_read (VFS_IC (ip)),XFS_FSVERITY_START_ALIGN ); +}fs/xfs/xfs_iomap.c +0
-0
[ ... ]
fs/xfs/xfs_iomap.c xfs_alert_fsblock_zero( · line 66
Quoted 12 lines
fs/xfs/xfs_iomap.c +8
-0@@ -54,6 +56,15 @@ xfs_alert_fsblock_zero(return -EFSCORRUPTED ; } +static bool +xfs_iomap_is_fsverity_metadata ( +struct xfs_inode *ip, +loff_t offset) +{ +return offset >=xfs_fsverity_metadata_offset (ip) && + (fsverity_is_building (VFS_I (ip)) ||fsverity_active (VFS_I (ip))); +}
[Severity: Medium]
Can round_up() in xfs_fsverity_metadata_offset() overflow a signed 64-bit loff_t and wrap to LLONG_MIN when i_size > LLONG_MAX - (XFS_FSVERITY_START_ALIGN - 1)?
Because xfs_iomap_is_fsverity_metadata() calls xfs_fsverity_metadata_offset() before checking fsverity_is_building() or fsverity_active(), every non-xattr xfs_bmbt_to_iomap() call executes this calculation even on non-verity files (which can be truncated up to s_maxbytes == LLONG_MAX) and when CONFIG_FS_VERITY is disabled. In addition, on a crafted or corrupted on-disk image where an XFS_DIFLAG2_VERITY inode has di_size > 0x7fffffffffff0000, the wrap to LLONG_MIN causes xfs_iomap_is_fsverity_metadata() to return true and set IOMAP_F_FSVERITY for every non-negative file offset.
Checking fsverity_is_building() || fsverity_active() before calling xfs_fsverity_metadata_offset(), and validating di_size against the maximum fs-verity file size for XFS_DIFLAG2_VERITY inodes in xfs_dinode_verify(), would avoid both issues.
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=13
PATCH v20 14/23 xfs: use read ioend for fsverity data verification
In reply to Andrey AlbershteynUse read ioends for fsverity verification. Do not issue fsverity metadata I/O through the same workqueue due to risk of a deadlock by a filled workqueue.
Pass fsverity_info from iomap context down to the ioend as hashtable lookups are expensive.
Add a simple helper to check that this is not fsverity metadata but file data that needs verification.
--- fs/xfs/Makefile | 1 + fs/xfs/xfs_aops.c | 13 +++++++---- fs/xfs/xfs_file.c | 3 ++- fs/xfs/xfs_fsverity.c | 46 +++++++++++++++++++++++++++++++++++++ fs/xfs/xfs_fsverity.h | 40 ++++++++++++++++++++++++++++++++ fs/xfs/xfs_ioend.c | 53 ++++++++++++++++++++++++++++++++++++++----- fs/xfs/xfs_ioend.h | 4 +++- fs/xfs/xfs_super.c | 25 +++++++++++++++++++- 8 files changed, 171 insertions(+), 14 deletions(-) create mode 100644 fs/xfs/xfs_fsverity.c create mode 100644 fs/xfs/xfs_fsverity.h
fs/xfs/Makefile +1 -0
@@ -151,6 +151,7 @@ xfs-$(CONFIG_XFS_POSIX_ACL) += xfs_acl.o xfs-$(CONFIG_SYSCTL) += xfs_sysctl.o xfs-$(CONFIG_COMPAT) += xfs_ioctl32.o xfs-$(CONFIG_EXPORTFS_BLOCK_OPS) += xfs_pnfs.o +xfs-$(CONFIG_FS_VERITY) += xfs_fsverity.o # notify failure ifeq ($(CONFIG_MEMORY_FAILURE),y)
fs/xfs/xfs_aops.c +8 -5
@@ -24,6 +24,7 @@#include "xfs_zone_alloc.h" #include "xfs_rtgroup.h" #include "xfs_verity.h" +#include "xfs_fsverity.h" struct xfs_writepage_ctx {struct iomap_writepage_ctx ctx;
@@ -587,7 +588,7 @@ xfs_bio_submit_read( {xfs_ioend_submit_read (iter->inode, ctx->read_ctx, ctx->read_ctx_file_offset, -iomap_ioend_flags (&iter->iomap)); +iomap_ioend_flags (&iter->iomap), ctx->vi); ctx->read_ctx =NULL ; }
@@ -599,11 +600,13 @@ static const struct iomap_read_ops xfs_iomap_read_ops = {static inline const struct iomap_read_ops *xfs_get_iomap_read_ops ( -const struct address_space *mapping) +const struct address_space *mapping, +loff_t pos) {struct xfs_inode *ip =XFS_I (mapping->host); -if (bdev_has_integrity_csum (xfs_inode_buftarg (ip)->bt_bdev)) +if (bdev_has_integrity_csum (xfs_inode_buftarg (ip)->bt_bdev) || +xfs_fsverity_is_file_data (ip, pos))return &xfs_iomap_read_ops;return &iomap_bio_read_ops; }
@@ -615,7 +618,7 @@ xfs_vm_read_folio( {struct iomap_read_folio_ctx ctx = { .cur_folio = folio }; - ctx.ops =xfs_get_iomap_read_ops (folio->mapping); + ctx.ops =xfs_get_iomap_read_ops (folio->mapping,folio_pos (folio));iomap_read_folio (xfs_read_iomap_next, &ctx,NULL );return 0 ; }
@@ -626,7 +629,7 @@ xfs_vm_readahead( {struct iomap_read_folio_ctx ctx = { .rac = rac }; - ctx.ops =xfs_get_iomap_read_ops (rac->mapping), + ctx.ops =xfs_get_iomap_read_ops (rac->mapping,readahead_pos (rac));iomap_readahead (xfs_read_iomap_next, &ctx,NULL ); }
fs/xfs/xfs_file.c +2 -1
@@ -182,7 +182,8 @@ xfs_dio_read_bounce_submit_io( loff_t file_offset) {xfs_ioend_submit_read (iter->inode, bio, file_offset, -iomap_ioend_flags (&iter->iomap) |IOMAP_IOEND_DIRECT ); +iomap_ioend_flags (&iter->iomap) |IOMAP_IOEND_DIRECT , +NULL ); }static const struct iomap_dio_ops xfs_dio_read_bounce_ops = {
fs/xfs/xfs_fsverity.c new file +46 -0
46 lines not on this page · raw message
fs/xfs/xfs_fsverity.h new file +40 -0
@@ -0,0 +1,40 @@ +// SPDX-License-Identifier: GPL-2.0 +/* +* Copyright (C) 2026 Red Hat, Inc. +*/ +#ifndef __XFS_FSVERITY_H__ +#define __XFS_FSVERITY_H__ + +#include "xfs_platform.h" +#include "xfs_verity.h" +#include <linux/iomap.h> +#include <linux/mempool.h> + +#ifdef CONFIG_FS_VERITY +int xfs_fsverity_init (void ); +void xfs_fsverity_exit (void ); +bool xfs_fsverity_is_file_data (const struct xfs_inode *ip,loff_t offset); +#else +static inline int xfs_fsverity_init (void ) +{ +return 0 ; +} +static inline void xfs_fsverity_exit (void ) +{ +} +static inline bool xfs_fsverity_is_file_data (const struct xfs_inode *ip, +loff_t offset) +{ +return false; +} +#endif /* CONFIG_FS_VERITY */ + +struct xfs_fsverity_ioend { +struct iomap_ioend *ioend; +struct work_struct work; +}; + +extern struct kmem_cache *xfs_fsverity_ioend_cache; +extern mempool_t xfs_fsverity_ioend_pool; + +#endif /* __XFS_FSVERITY_H__ */
fs/xfs/xfs_ioend.c +47 -6
85 lines not on this page · raw message
fs/xfs/xfs_ioend.h +3 -1
@@ -2,6 +2,8 @@#ifndef __XFS_IOEND_H#define __XFS_IOEND_H +#include <linux/fsverity.h> +/* * Fast and loose check if this write could update the on-disk inode size. */
@@ -13,6 +15,6 @@ static inline bool xfs_ioend_is_append(struct iomap_ioend *ioend)void xfs_end_bio (struct bio *bio);void xfs_ioend_submit_read (struct inode *inode,struct bio *bio, -loff_t file_offset,u16 ioend_flags); +loff_t file_offset,u16 ioend_flags,struct fsverity_info *vi);#endif /* __XFS_IOEND_H */
fs/xfs/xfs_super.c +24 -1
@@ -50,6 +50,7 @@#include "xfs_rtalloc.h" #include "xfs_zone_alloc.h" #include "xfs_healthmon.h" +#include "xfs_fsverity.h" #include "scrub/stats.h" #include "scrub/rcbag_btree.h"
@@ -2516,8 +2517,20 @@ xfs_init_caches(void)if (!xfs_parent_args_cache) goto out_destroy_xmi_cache; +#ifdef CONFIG_FS_VERITY + xfs_fsverity_ioend_cache =kmem_cache_create ("xfs_fsverity_ioend" , +sizeof (struct xfs_fsverity_ioend ), +0 ,0 ,NULL ); +if (!xfs_fsverity_ioend_cache) + goto out_destroy_parent_args_cache; +#endif +return 0 ; +#ifdef CONFIG_FS_VERITY + out_destroy_parent_args_cache: +kmem_cache_destroy (xfs_parent_args_cache); +#endif out_destroy_xmi_cache:kmem_cache_destroy (xfs_xmi_cache); out_destroy_xmd_cache:
@@ -2580,6 +2593,9 @@ xfs_destroy_caches(void)* destroy caches. */ rcu_barrier (); +#ifdef CONFIG_FS_VERITY +kmem_cache_destroy (xfs_fsverity_ioend_cache); +#endif kmem_cache_destroy (xfs_parent_args_cache);kmem_cache_destroy (xfs_xmd_cache);kmem_cache_destroy (xfs_xmi_cache);
@@ -2660,10 +2676,14 @@ init_xfs_fs(void)if (error) goto out; - error =xfs_init_workqueues (); + error =xfs_fsverity_init ();if (error) goto out_destroy_caches; + error =xfs_init_workqueues (); +if (error) + goto out_fsverity_exit; + error =xfs_mru_cache_init ();if (error) goto out_destroy_wq;
@@ -2740,6 +2760,8 @@ init_xfs_fs(void)xfs_mru_cache_uninit (); out_destroy_wq:xfs_destroy_workqueues (); + out_fsverity_exit: +xfs_fsverity_exit (); out_destroy_caches:xfs_destroy_caches (); out:
@@ -2763,6 +2785,7 @@ exit_xfs_fs(void)free_percpu (xfsstats.xs_stats);xfs_mru_cache_uninit ();xfs_destroy_workqueues (); +xfs_fsverity_exit ();xfs_destroy_caches ();xfs_uuid_table_free (); }
Signature · 1 line
2.54.0
[email protected]
AI reviewRe: [PATCH v20 14/23] xfs: use read ioend for fsverity data verification
Thank you for your contribution! Sashiko AI review found 1 potential issue to consider: - [High] xfs: workqueue deadlock on mempool_alloc() in xfs_end_io_read() --
Thank you for your contribution! Sashiko AI review found 1 potential issue to consider:
- [High] xfs: workqueue deadlock on mempool_alloc() in xfs_end_io_read() --
commit 8bb973f44a8ada1e9937d03966b5a5b1e3c30932
Author: Andrey Albershteyn <[email protected]>
xfs: use read ioend for fsverity data verification
This commit uses read ioends to defer fsverity data verification to the fsverity workqueue, passing fsverity_info down from the iomap read context.
Quoted 28 lines
fs/xfs/xfs_ioend.c +19
-0@@ -18,7 +18,26 @@#include "xfs_ioend.h" #include "xfs_error.h" #include "xfs_errortag.h" +#include "xfs_fsverity.h" #include <linux/bio-integrity.h> +#include <linux/fsverity.h> + +static void +xfs_end_fsverity_io_read ( +struct work_struct *work) +{ +struct xfs_fsverity_ioend *fsv_ioend = + container_of(work, struct xfs_fsverity_ioend, work); +struct iomap_ioend *ioend = fsv_ioend->ioend; +struct bio *bio = &ioend->io_bio; + +mempool_free (fsv_ioend, &xfs_fsverity_ioend_pool); + +if (!bio->bi_status) +fsverity_verify_bio (ioend->io_vi, bio); + +iomap_finish_ioends (ioend,blk_status_to_errno (bio->bi_status)); +}
[ ... ]
Quoted 14 lines
fs/xfs/xfs_ioend.c +10
-0@@ -117,6 +138,24 @@ xfs_end_io_read( } } +/* +* If we have fsverity on this bio, we need to run fsverity verification +* of data folios from a separate fsverity workqueue. This is necessary +* to avoid deadlocking due to fsverity issuing more reads of fsverity +* metadata which would be processed by the same worker in the BIO +* completion workqueue. +*/ +if (IS_ENABLED (CONFIG_FS_VERITY ) && !error && ioend->io_vi && +xfs_fsverity_is_file_data (ip, ioend->io_offset)) { + fsv_ioend = mempool_alloc(&xfs_fsverity_ioend_pool,
fs/xfs/xfs_ioend.c xfs_end_io_read( · line 160
Quoted 10 lines
fs/xfs/xfs_ioend.c +8
-0+ GFP_NOFS); + fsv_ioend->ioend = ioend; +INIT_WORK (&fsv_ioend->work, xfs_end_fsverity_io_read); + +fsverity_enqueue_verify_work (&fsv_ioend->work); +return ; + } +iomap_finish_ioends (ioend, error); }
[Severity: High]
Can mempool_alloc(&xfs_fsverity_ioend_pool, GFP_NOFS) here deadlock bio_complete_wq against fsverity_read_workqueue?
xfs_ioend_submit_read() sets BIO_COMPLETE_IN_TASK, so xfs_end_io_read() runs from bio_complete_work_fn() on the per-CPU bio_complete_wq. Because bio_complete_batch has a single work_struct per CPU and drains completed bios sequentially, blocking in xfs_end_io_read() stalls all later BIO_COMPLETE_IN_TASK completions queued on that CPU.
Meanwhile, fsv_ioend is only returned to xfs_fsverity_ioend_pool once xfs_end_fsverity_io_read() begins executing on fsverity_read_workqueue. Under memory pressure, if the active fsverity_read_workqueue workers are blocked in fsverity_verify_bio() waiting for Merkle tree metadata reads or memory reclaim, pending work items on fsverity_read_workqueue can hold all 128 preallocated pool elements:
bio_complete_wq fsverity_read_workqueue --------------- ----------------------- bio_complete_work_fn() xfs_end_fsverity_io_read()
(128 data bios allocate fsverity_verify_bio()
fsv_ioend and queue work) read_merkle_tree_page()
(waits for metadata read /
reclaim BIO_COMPLETE_IN_TASK completion on bio_complete_wq)
xfs_end_io_read() (129th bio)
mempool_alloc(&xfs_fsverity_ioend_pool, GFP_NOFS)
<-- sleeps waiting for queued xfs_end_fsverity_io_read()
to call mempool_free()
*** DEADLOCK ***
When bdev_has_integrity_csum() is true, xfs_get_iomap_read_ops() selects xfs_iomap_read_ops for Merkle tree metadata reads as well, so their completions also require bio_complete_work_fn() on bio_complete_wq, as do failed metadata read bios that defer completion via bio_complete_in_task() in iomap_read_end_io() and reclaim I/O completions on bio_complete_wq.
Could fsv_ioend be allocated before submit_bio() (or embedded in the read ioend) so the bio completion path never blocks on the mempool?
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=14
PATCH v20 15/23 xfs: add XFS_BMAPI_UNWRITTEN to unmap unwritten extents in __xfs_bunmapi()
In reply to Andrey AlbershteynThe __xfs_bunmapi() is used to remove all extents past EOF. Add XFS_BMAPI_UNWRITTEN to remove only unwritten extents, while leaving written ones in place. This will be used in following patch to clean up unwritten extents on fsverity inodes.
--- fs/xfs/libxfs/xfs_bmap.c | 22 +++++++++++++++++++++- fs/xfs/libxfs/xfs_bmap.h | 6 +++++- 2 files changed, 26 insertions(+), 2 deletions(-)
fs/xfs/libxfs/xfs_bmap.c +21 -1
@@ -5220,6 +5220,22 @@ __xfs_bunmapi(ASSERT (len >0 );ASSERT (nexts >=0 ); +/* +* XFS_BMAPI_UNWRITTEN is only used by fs-verity to drop unwritten +* blocks past EOF while keeping written ones (fsverity metadata). +* +* The XFS_BMAPI_UNWRITTEN could not be used with realtime inodes with +* realtime extent larger than 1 filesystem block. This is because the +* realtime inode with larger extents could have EOF in the middle of +* the extent. Removing these unwritten extent beyond EOF could lead to +* removal of data before EOF. +* +* fs-verity is not allowed to be enabled on realtime files whose +* extent size is larger than 1. +*/ +ASSERT (!((flags &XFS_BMAPI_UNWRITTEN ) && +xfs_inode_has_bigrtalloc (ip))); + error =xfs_iread_extents (tp, ip, whichfork);if (error)return error;
@@ -5281,6 +5297,10 @@ __xfs_bunmapi(if (del.br_startoff + del.br_blockcount > end +1 ) del.br_blockcount = end +1 - del.br_startoff; +if ((flags &XFS_BMAPI_UNWRITTEN ) && + del.br_state !=XFS_EXT_UNWRITTEN ) + goto skip; +if (!isrt || (flags &XFS_BMAPI_REMAP )) goto delete;
@@ -5415,7 +5435,7 @@ __xfs_bunmapi(if (error) goto error0; } - +skip: end = del.br_startoff -1 ; nodelete:/*
fs/xfs/libxfs/xfs_bmap.h +5 -1
@@ -97,6 +97,9 @@ struct xfs_bmalloca {*/ #define XFS_BMAPI_CONVERT_ONLY (1u << 12) +/* Process unwritten extents only. Used for unmapping */ +#define XFS_BMAPI_UNWRITTEN (1u << 13) +#define XFS_BMAPI_FLAGS \ { XFS_BMAPI_ENTIRE, "ENTIRE" }, \ { XFS_BMAPI_METADATA, "METADATA" }, \
@@ -110,7 +113,8 @@ struct xfs_bmalloca { { XFS_BMAPI_NODISCARD, "NODISCARD" }, \ { XFS_BMAPI_NORMAP, "NORMAP" },\ { XFS_BMAPI_EXTSZALIGN, "EXTSZALIGN" }, \ - { XFS_BMAPI_CONVERT_ONLY, "CONVERT_ONLY" } + { XFS_BMAPI_CONVERT_ONLY, "CONVERT_ONLY" }, \ + { XFS_BMAPI_UNWRITTEN, "UNWRITTEN" } static inline int xfs_bmapi_aflag(int w)
Signature · 1 line
2.54.0
[email protected]
AI reviewRe: [PATCH v20 15/23] xfs: add XFS_BMAPI_UNWRITTEN to unmap unwritten extents in __xfs_bunmapi()
Thank you for your contribution! Sashiko AI review found 1 potential issue to consider: - [Medium] xfs: skipped written extents count toward nexts limit in __xfs_bunmapi() --
Thank you for your contribution! Sashiko AI review found 1 potential issue to consider:
- [Medium] xfs: skipped written extents count toward nexts limit in __xfs_bunmapi()
--
commit d810ef6f3efad46182e39525768f25badd06f758
Author: Andrey Albershteyn <[email protected]>
xfs: add XFS_BMAPI_UNWRITTEN to unmap unwritten extents in __xfs_bunmapi()
This commit introduces the XFS_BMAPI_UNWRITTEN flag for __xfs_bunmapi() so callers can unmap unwritten extents while leaving written extents intact.
Quoted 4 lines
fs/xfs/libxfs/xfs_bmap.c +0
-0
[ ... ]
Quoted 12 lines
fs/xfs/libxfs/xfs_bmap.c +4
-0@@ -5281,6 +5297,10 @@ __xfs_bunmapi(if (del.br_startoff + del.br_blockcount > end +1 ) del.br_blockcount = end +1 - del.br_startoff; +if ((flags &XFS_BMAPI_UNWRITTEN ) && + del.br_state !=XFS_EXT_UNWRITTEN ) + goto skip; +if (!isrt || (flags &XFS_BMAPI_REMAP )) goto delete;
fs/xfs/libxfs/xfs_bmap.c __xfs_bunmapi( · line 5441
Quoted 8 lines
fs/xfs/libxfs/xfs_bmap.c +1
-1@@ -5415,7 +5435,7 @@ __xfs_bunmapi(if (error) goto error0; } - +skip: end = del.br_startoff -1 ; nodelete:/*
[Severity: Medium]
Should skipped written extents count against nexts?
Jumping to skip falls through into nodelete and increments extno even though the extent was not modified:
fs/xfs/libxfs/xfs_bmap.c:__xfs_bunmapi() {
...
skip:
end = del.br_startoff - 1;
nodelete:
/*
* If not done go on to the next (previous) record.
*/
if (end != (xfs_fileoff_t)-1 && end >= start) {
if (!xfs_iext_get_extent(ifp, &icur, &got) ||
(got.br_startoff > end &&
!xfs_iext_prev_extent(ifp, &icur, &got))) {
done = true;
break;
}
extno++;
}
}Because xfs_bunmapi_range() calls __xfs_bunmapi() in a loop with nexts set to XFS_ITRUNC_MAX_EXTENTS (2), walking backward across post-EOF written fs-verity extents exits and restarts __xfs_bunmapi() every two skipped extents, re-allocating the bmbt cursor, re-searching the extent tree, and running xfs_defer_finish() without having unmapped anything. Also, any direct caller passing a bounded non-zero nexts with XFS_BMAPI_UNWRITTEN can return early without unmapping unwritten extents if at least nexts written extents appear first from the end of the range.
Other paths in __xfs_bunmapi() that skip an untouched extent advance icur to the previous extent and continue without incrementing extno.
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=15
PATCH v20 16/23 xfs: don't remove written extents past EOF on fsverity inodes
In reply to Andrey Albershteynxfs_free_eofblocks() removes all extents past EOF unless the XFS_DIFLAG_PREALLOC or XFS_DIFLAG_APPEND flags are set. This is undesired for fsverity as it stores metadata beyond EOF. However, while merkle tree is being built, delayed preallocation and unwritten extents are used. After metadata construction is done, fsverity inodes becomes read-only and won't be changed anymore, none of these unwritten extents or preallocations in post EOF region will be used.
Let xfs_free_eofblocks() be called on fsverity inode as usual to remove anything which is not written extent. However, inodes which are undergoing merkle tree construction need to be skipped in case reclaim takes place.
--- fs/xfs/xfs_bmap_util.c | 44 +++++++++++++++++++++++++++++++++--------- fs/xfs/xfs_icache.c | 11 ++++++++++- 2 files changed, 45 insertions(+), 10 deletions(-)
fs/xfs/xfs_bmap_util.c +35 -9
@@ -31,6 +31,7 @@#include "xfs_rtbitmap.h" #include "xfs_rtgroup.h" #include "xfs_zone_alloc.h" +#include <linux/fsverity.h> /* Kernel only BMAP related definitions and functions */
@@ -553,6 +554,17 @@ xfs_can_free_eofblocks(if (last_fsb <= end_fsb)return false; +/* +* Don't clean fsverity inodes as they already have been cleaned up and +* are read-only. Skip inodes which have merkle tree being built, the +* merkle tree is written beyond EOF. Skip inodes which have +* XFS_DIFLAG2_VERITY in case this kernel has fsverity disabled. +*/ +if (fsverity_active (VFS_IC (ip)) || +fsverity_is_building (VFS_I (ip)) || + ip->i_diflags2 &XFS_DIFLAG2_VERITY ) +return false; +/* * Check if there is an post-EOF extent to free. If there are any * delalloc blocks attached to the inode (data fork delalloc
@@ -579,6 +591,8 @@ xfs_free_eofblocks(struct xfs_trans *tp;struct xfs_mount *mp = ip->i_mount;int error; +int bmapi_flags =XFS_BMAPI_NODISCARD ; +xfs_off_t aligned_size;/* Attach the dquots to the inode up front. */ error =xfs_qm_dqattach (ip);
@@ -589,17 +603,29 @@ xfs_free_eofblocks(inode_dio_wait (VFS_I (ip));/* -* For preallocated files only free delayed allocations. +* On fs-verity inodes, while fs-verity builds metadata past EOF, it can +* leave delayed preallocations and unwritten extents. Clear delayed +* preallocation here and tell bmapi to remove unwritten extents. * -* Note that this means we also leave speculative preallocations in -* place for preallocated files. +* The xfs_free_eofblocks() is called by fs-verity when building is +* complete but fsverity_is_building() is still not cleared. */ -if (ip->i_diflags & (XFS_DIFLAG_PREALLOC |XFS_DIFLAG_APPEND )) { -if (ip->i_delayed_blks) { + aligned_size =round_up (XFS_ISIZE (ip), mp->m_sb.sb_blocksize); +if (fsverity_is_building (VFS_I (ip))) { +if (ip->i_delayed_blks)xfs_bmap_punch_delalloc_range (ip,XFS_DATA_FORK , -round_up (XFS_ISIZE (ip), mp->m_sb.sb_blocksize), -LLONG_MAX ,NULL ); - } + aligned_size,LLONG_MAX ,NULL ); + bmapi_flags |=XFS_BMAPI_UNWRITTEN ; + }else if (ip->i_diflags & (XFS_DIFLAG_PREALLOC |XFS_DIFLAG_APPEND )) { +/* +* For preallocated files only free delayed allocations. +* +* Note that this means we also leave speculative preallocations +* in place for preallocated files. +*/ +if (ip->i_delayed_blks) +xfs_bmap_punch_delalloc_range (ip,XFS_DATA_FORK , + aligned_size,LLONG_MAX ,NULL );xfs_inode_clear_eofblocks_tag (ip);return 0 ; }
@@ -620,7 +646,7 @@ xfs_free_eofblocks(* bug). */ error =xfs_itruncate_extents_flags (&tp, ip,XFS_DATA_FORK , -XFS_ISIZE (ip),XFS_BMAPI_NODISCARD ); +XFS_ISIZE (ip), bmapi_flags);if (error) goto err_cancel;
fs/xfs/xfs_icache.c +10 -1
@@ -28,7 +28,7 @@#include "xfs_da_format.h" #include "xfs_dir2.h" #include "xfs_metafile.h" - +#include <linux/fsverity.h> #include <linux/iversion.h> /* Radix tree tags for incore inode tree. */
@@ -1260,6 +1260,15 @@ xfs_inode_free_eofblocks(if (xfs_can_free_eofblocks (ip))return xfs_free_eofblocks (ip); +/* +* The above xfs_free_eofblocks() is skipped for inodes with fsverity +* tree is being built. However, we don't want to clear EOF tag in this +* case. If building the tree fails, inode will have EOF blocks which +* needs removal. +*/ +if (fsverity_is_building (VFS_IC (ip))) +return 0 ; +/* inode could be preallocated */ trace_xfs_inode_free_eofblocks_invalid (ip);xfs_inode_clear_eofblocks_tag (ip);
Signature · 1 line
2.54.0
PATCH v20 17/23 xfs: add fs-verity support
In reply to Andrey AlbershteynAdd integration with fs-verity. XFS stores fs-verity descriptor and Merkle tree in the inode data fork at first block aligned to 64k past EOF.
The Merkle tree reading/writing is done through iomap interface. The data itself is read to the inode's page cache. When XFS reads from this region iomap doesn't call into fsverity to verify it against Merkle tree. For data, verification is done at ioend completion in a workqueue.
The metadata building starts with flushing pagecache and cleaning up any residue extents past EOF. Over whole initialization period I_VERITY_BUILDING indicates that merkle tree building is in progress. No iolock is held over this period. After core fsverity code done with building the tree, XFS stores verity descriptor, then flushes page cache again, then pro-actively remove any delayed allocation and unwritten extents past EOF and ends up with setting inode on-disk flag (XFS_DIFLAG2_VERITY).
The descriptor is stored in a new block aligned to 64k after the last Merkle tree block. The size of the descriptor is stored at the end of the last descriptor block (descriptor can be multiple blocks).
--- fs/xfs/libxfs/xfs_inode_buf.c | 11 + fs/xfs/xfs_fsverity.c | 456 +++++++++++++++++++++++++++++++++- fs/xfs/xfs_fsverity.h | 2 + fs/xfs/xfs_ioctl.c | 20 ++ fs/xfs/xfs_message.c | 4 + fs/xfs/xfs_message.h | 1 + fs/xfs/xfs_mount.h | 2 + fs/xfs/xfs_super.c | 8 +- 8 files changed, 502 insertions(+), 2 deletions(-)
fs/xfs/libxfs/xfs_inode_buf.c +11 -0
@@ -764,6 +764,17 @@ xfs_dinode_verify(return __this_address;if (!S_ISREG (mode))return __this_address; +/* +* fs-verity stores the Merkle tree past EOF, so it cannot +* coexist with a realtime allocation unit larger than a single +* filesystem block. This is because we remove any unwritten +* extents beyond EOF for fs-verity inodes. The realtime inode +* with larger extents could have EOF in the middle of the +* extent. Enabling fsverity on such inode could lead to removal +* of data before EOF. +*/ +if ((flags &XFS_DIFLAG_REALTIME ) && mp->m_sb.sb_rextsize >1 ) +return __this_address; } if (xfs_has_zoned(mp) &&
fs/xfs/xfs_fsverity.c +455 -1
471 lines not on this page · raw message
fs/xfs/xfs_fsverity.h +2 -0
@@ -8,9 +8,11 @@#include "xfs_platform.h" #include "xfs_verity.h" #include <linux/iomap.h> +#include <linux/fsverity.h> #include <linux/mempool.h> #ifdef CONFIG_FS_VERITY +extern const struct fsverity_operations xfs_fsverity_ops;int xfs_fsverity_init (void );void xfs_fsverity_exit (void );bool xfs_fsverity_is_file_data (const struct xfs_inode *ip,loff_t offset);
fs/xfs/xfs_ioctl.c +20 -0
@@ -49,6 +49,7 @@#include <linux/mount.h> #include <linux/fileattr.h> +#include <linux/fsverity.h> /* Return 0 on success or positive error */ int
@@ -562,6 +563,14 @@ xfs_ioctl_setattr_xflags(if (xfs_inode_has_filedata (ip))return -EINVAL ; +/* +* fs-verity is incompatible with a realtime allocation unit +* larger than a single filesystem block, so don't let a file be +* converted to realtime while verity is being enabled. +*/ +if (fsverity_is_building (VFS_IC (ip))) +return -EINVAL ; +/* * If S_DAX is enabled on this file, we can only switch the * device if both support fsdax, and the zoned code does not.
@@ -576,6 +585,17 @@ xfs_ioctl_setattr_xflags(return -EINVAL ; } +/* +* fs-verity is incompatible with DAX, and xfs_fsverity_begin_enable() +* verified the inode was not DAX under the inode lock. That lock is +* dropped while the Merkle tree is built, so reject enabling DAX while +* verity is being enabled. +*/ +if ((fa->fsx_xflags &FS_XFLAG_DAX ) && + !(ip->i_diflags2 &XFS_DIFLAG2_DAX ) && +fsverity_is_building (VFS_IC (ip))) +return -EINVAL ; +if (rtflag) {/* If realtime flag is set then must have realtime device */ if (mp->m_sb.sb_rblocks == 0 || mp->m_sb.sb_rextsize == 0 ||
fs/xfs/xfs_message.c +4 -0
@@ -149,6 +149,10 @@ xfs_warn_experimental( .opstate = XFS_OPSTATE_WARNED_LARP, .name = "logged extended attributes", }, + [XFS_EXPERIMENTAL_FSVERITY] = { + .opstate = XFS_OPSTATE_WARNED_FSVERITY, + .name = "fsverity", + }, };ASSERT (feat >=0 && feat <XFS_EXPERIMENTAL_MAX );BUILD_BUG_ON (ARRAY_SIZE (features) !=XFS_EXPERIMENTAL_MAX );
fs/xfs/xfs_message.h +1 -0
@@ -93,6 +93,7 @@ void xfs_buf_alert_ratelimited(struct xfs_buf *bp, const char *rlmsg,enum xfs_experimental_feat {XFS_EXPERIMENTAL_SHRINK ,XFS_EXPERIMENTAL_LARP , +XFS_EXPERIMENTAL_FSVERITY ,XFS_EXPERIMENTAL_MAX , };
fs/xfs/xfs_mount.h +2 -0
@@ -599,6 +599,8 @@ __XFS_HAS_FEAT(nouuid, NOUUID)#define XFS_OPSTATE_RESUMING_QUOTAON 18/* (Zoned) GC is in progress */ #define XFS_OPSTATE_ZONEGC_RUNNING 20 +/* Kernel has logged a warning about fsverity support */ +#define XFS_OPSTATE_WARNED_FSVERITY 21#define __XFS_IS_OPSTATE (name,NAME ) \ static inline bool xfs_is_ ## name (struct xfs_mount *mp) \
fs/xfs/xfs_super.c +7 -1
@@ -30,6 +30,7 @@#include "xfs_filestream.h" #include "xfs_quota.h" #include "xfs_sysfs.h" +#include "xfs_fsverity.h" #include "xfs_ondisk.h" #include "xfs_rmap_item.h" #include "xfs_refcount_item.h"
@@ -50,7 +51,6 @@#include "xfs_rtalloc.h" #include "xfs_zone_alloc.h" #include "xfs_healthmon.h" -#include "xfs_fsverity.h" #include "scrub/stats.h" #include "scrub/rcbag_btree.h"
@@ -1737,6 +1737,9 @@ xfs_fs_fill_super( sb->s_quota_types =QTYPE_MASK_USR |QTYPE_MASK_GRP |QTYPE_MASK_PRJ ;#endif sb->s_op = &xfs_super_operations; +#ifdef CONFIG_FS_VERITY + sb->s_vop = &xfs_fsverity_ops; +#endif /* * Delay mount work if the debug hook is set. This is debug
@@ -1989,6 +1992,9 @@ xfs_fs_fill_super(if (error) goto out_filestream_unmount; +if (xfs_has_verity (mp)) +xfs_warn_experimental (mp,XFS_EXPERIMENTAL_FSVERITY ); + root =igrab (VFS_I (mp->m_rootip));if (!root) { error = -ENOENT ;
Signature · 1 line
2.54.0
[email protected]
AI reviewRe: [f2fs-dev] [PATCH v20 17/23] xfs: add fs-verity support
Thank you for your contribution! Sashiko AI review found 8 potential issues to consider: - [High] xfs: unlocked iomap_fsverity_write() in xfs_fsverity_write_merkle() - [Medium] xfs: broken error…
Thank you for your contribution! Sashiko AI review found 8 potential issues to consider:
- [High] xfs: unlocked iomap_fsverity_write() in xfs_fsverity_write_merkle()
- [Medium] xfs: broken error rollback in xfs_fsverity_reset_inode()
- [Medium] xfs: missing verity di_size check in xfs_dinode_verify() causes overflow
- [Medium] xfs: missing extent bounds check in xfs_fsverity_get_descriptor()
- [Medium] xfs: xfs_ioctl_setattr_xflags() allows setting RT flag on extentless verity inode
- [Medium] xfs: race changing i_extsize in xfs_ioctl_setattr_check_extsize() during verity
- [Low] xfs: !buf_size return in xfs_fsverity_get_descriptor() skips checks
- [Low] xfs: off-by-one s_maxbytes check in xfs_fsverity_get_descriptor()
--
commit b9564e67a9b1a0520488741ca90d7b445b9145e2
Author: Andrey Albershteyn <[email protected]>
xfs: add fs-verity support
This commit integrates XFS with fs-verity, storing the Merkle tree and verity descriptor in the inode data fork at 64k-aligned offsets past EOF and wiring up the fsverity_operations callbacks.
Quoted 10 lines
fs/xfs/libxfs/xfs_inode_buf.c +2
-0@@ -764,6 +764,17 @@ xfs_dinode_verify(return __this_address;if (!S_ISREG (mode))return __this_address; +/* +* fs-verity stores the Merkle tree past EOF, so it cannot
fs/xfs/libxfs/xfs_inode_buf.c xfs_dinode_verify( · line 778
Quoted 10 lines
fs/xfs/libxfs/xfs_inode_buf.c +9
-0+* coexist with a realtime allocation unit larger than a single +* filesystem block. This is because we remove any unwritten +* extents beyond EOF for fs-verity inodes. The realtime inode +* with larger extents could have EOF in the middle of the +* extent. Enabling fsverity on such inode could lead to removal +* of data before EOF. +*/ +if ((flags &XFS_DIFLAG_REALTIME ) && mp->m_sb.sb_rextsize >1 ) +return __this_address; }
[Severity: Medium]
Should xfs_dinode_verify() also verify that XFS_B_TO_FSB(mp, di_size) does not exceed XFS_FSVERITY_LARGEST_FILE_FSB(mp), matching xfs_fsverity_begin_enable()?
Without that check on disk, a corrupted or crafted verity inode with di_size > LLONG_MAX - XFS_FSVERITY_START_ALIGN + 1 passes verification and causes round_up(i_size_read(VFS_IC(ip)), XFS_FSVERITY_START_ALIGN) in xfs_fsverity_metadata_offset() to overflow signed loff_t to LLONG_MIN. xfs_fsverity_is_file_data() then returns false for all non-negative file offsets, treating all file data reads as unverified metadata.
Quoted 4 lines
fs/xfs/xfs_fsverity.c +0
-0
[ ... ]
Quoted 6 lines
fs/xfs/xfs_fsverity.c +6
-0+static int +xfs_fsverity_get_descriptor ( +struct inode *inode, +void *buf, +size_t buf_size) +{
[ ... ]
fs/xfs/xfs_fsverity.c xfs_fsverity_is_file_data( · line 112
Quoted 9 lines
fs/xfs/xfs_fsverity.c +9
-0+/* +* Make sure we can open large file on a 32-bit kernel +*/ +if (rec.br_startoff + rec.br_blockcount > +XFS_B_TO_FSB (mp, inode->i_sb->s_maxbytes)) +return -EFBIG ; + + last_block_offset = +XFS_FSB_TO_B (mp, rec.br_startoff + rec.br_blockcount);
[Severity: Low]
Should this bound check use XFS_B_TO_FSBT() instead of XFS_B_TO_FSB()?
On 64-bit systems s_maxbytes is LLONG_MAX, which XFS_B_TO_FSB() rounds up to 1ULL << (63 - sb_blocklog). An extent ending at that block passes the check, and XFS_FSB_TO_B() then overflows signed xfs_fsize_t to LLONG_MIN (s_maxbytes + 1).
Quoted 18 lines
fs/xfs/xfs_fsverity.c +18
-0+if (last_block_offset <=xfs_fsverity_metadata_offset (ip)) +return -ENODATA ; + + desc_size_pos = last_block_offset -sizeof (__be32); + error =fsverity_pagecache_read (inode, (char *)&d_desc_size, +sizeof (d_desc_size), desc_size_pos); +if (error) +return error; + + desc_size =be32_to_cpu (d_desc_size); +if (XFS_IS_CORRUPT (mp, desc_size >FS_VERITY_MAX_DESCRIPTOR_SIZE )) +return -ERANGE ; +if (XFS_IS_CORRUPT (mp, desc_size > desc_size_pos)) +return -ERANGE ; + +if (!buf_size) +return desc_size; +
fs/xfs/xfs_fsverity.c xfs_fsverity_is_file_data( · line 140
Quoted 10 lines
fs/xfs/xfs_fsverity.c +10
-0+if (desc_size > buf_size) +return -ERANGE ; + +if (XFS_IS_CORRUPT (mp, + desc_size <sizeof (struct fsverity_descriptor ))) +return -ERANGE ; + + desc_pos =round_down (desc_size_pos - desc_size, blocksize); +if (desc_pos <xfs_fsverity_metadata_offset (ip)) +return -ERANGE ;
[Severity: Low]
Does returning early on !buf_size make thedesc_size < sizeof(struct fsverity_descriptor) corruption check unreachable from fsverity_get_descriptor()?
fsverity_get_descriptor() always calls ->get_verity_descriptor() with buf_size == 0 first and aborts with -EMSGSIZE if the returned size is smaller than sizeof(struct fsverity_descriptor), so it never makes the second call that reaches XFS_IS_CORRUPT() (or the XFS_SICK_INO_FSVERITY health update added later in commit "xfs: introduce health state for corrupted fsverity metadata"). Checking desc_size and desc_pos before if (!buf_size) would also avoid allocating a buffer and re-reading desc_size when desc_pos < xfs_fsverity_metadata_offset(ip).
fs/xfs/xfs_fsverity.c xfs_fsverity_is_file_data( · line 147
Quoted 7 lines
fs/xfs/xfs_fsverity.c +7
-0+ + error =fsverity_pagecache_read (inode, buf, desc_size, desc_pos); +if (error) +return error; + +return desc_size; +}
[Severity: Medium]
What happens if the descriptor range [desc_pos, last_block_offset) on a corrupted image starts before rec.br_startoff or spans a hole?
xfs_bmap_last_extent() only returns the last extent, and xfs_fsverity_get_descriptor() does not check that
rec.br_state == XFS_EXT_NORM anddesc_pos >= XFS_FSB_TO_B(mp, rec.br_startoff). When fsverity_pagecache_read() reads a hole in the verity metadata region while fsverity_info is not yet set on the inode (ctx->vi == NULL), iomap_read_folio_iter() skips fsverity_fill_zerohash() without zeroing the folio and marks the uninitialized folio range uptodate:
fs/iomap/buffered-io.c:iomap_read_folio_iter() {
...
if ((iomap->flags & IOMAP_F_FSVERITY) &&
iomap->type == IOMAP_HOLE) {
if (ctx->vi)
fsverity_fill_zerohash(folio, poff, plen,
ctx->vi);
iomap_set_range_uptodate(folio, poff, plen);
}
...
}That leaves uninitialized kernel page memory cached as uptodate in inode->i_mapping, where FS_IOC_READ_VERITY_METADATA can read it.
[ ... ]
Quoted 35 lines
fs/xfs/xfs_fsverity.c +35
-0+static int +xfs_fsverity_reset_inode ( +struct xfs_inode *ip) +{ +int error; +struct xfs_mount *mp = ip->i_mount; +struct xfs_trans *tp; + + error =xfs_fsverity_delete_metadata (ip); +if (error) +return error; + +/* +* First, let's clean in-memory fsverity flag and then reset it on the +* disk +*/ +inode_set_flags (VFS_I (ip),0 ,S_VERITY ); + +/* +* Set fsverity inode flag +*/ + error =xfs_trans_alloc_inode (ip, &M_RES (mp)->tr_ichange, +0 ,0 , false, &tp); +if (error) +return error; + + ip->i_diflags2 &= ~XFS_DIFLAG2_VERITY ; + +xfs_trans_log_inode (tp, ip,XFS_ILOG_CORE ); +xfs_trans_set_sync (tp); + + error =xfs_trans_commit (tp); +xfs_iunlock (ip,XFS_ILOCK_EXCL ); +return error; +}
[ ... ]
Quoted 7 lines
fs/xfs/xfs_fsverity.c +7
-0+static int +xfs_fsverity_end_enable ( +struct file *file, +const void *desc, +size_t desc_size, +u64 merkle_tree_size) +{
[ ... ]
Quoted 24 lines
fs/xfs/xfs_fsverity.c +24
-0+/* +* Set fsverity inode flag +*/ + error =xfs_trans_alloc_inode (ip, &M_RES (mp)->tr_ichange, +0 ,0 , false, &tp); +if (error) + goto out; + +/* +* Ensure that we've persisted the verity information before we enable +* it on the inode and tell the caller we have sealed the inode. +*/ + ip->i_diflags2 |=XFS_DIFLAG2_VERITY ; + +xfs_trans_log_inode (tp, ip,XFS_ILOG_CORE ); +xfs_trans_set_sync (tp); + + error =xfs_trans_commit (tp); +xfs_iunlock (ip,XFS_ILOCK_EXCL ); +if (error) + goto out; + +inode_set_flags (inode,S_VERITY ,S_VERITY ); +return error;
fs/xfs/xfs_fsverity.c xfs_fsverity_is_file_data( · line 400
Quoted 10 lines
fs/xfs/xfs_fsverity.c +10
-0+ +out: + error2 =xfs_fsverity_reset_inode (ip); +if (error2) +xfs_alert (ip->i_mount, +"ino 0x%llx failed to clean up new fsverity metadata, err %d" , +I_INO (ip), error2); + +return error; +}
[Severity: Medium]
Does xfs_fsverity_reset_inode() ever need to run a transaction to clear XFS_DIFLAG2_VERITY on disk, and can it leave XFS_DIFLAG2_VERITY set in memory when xfs_trans_commit() fails?
Every goto out before xfs_trans_commit() runs before XFS_DIFLAG2_VERITY or S_VERITY has been set in memory or on disk, so xfs_fsverity_reset_inode() commits an unnecessary synchronous tr_ichange transaction on every failed enablement (such as desc == NULL). Conversely, if xfs_trans_commit() in xfs_fsverity_end_enable() fails, the transaction aborts without persisting XFS_DIFLAG2_VERITY and shuts down the mount; xfs_fsverity_delete_metadata() then fails at xfs_trans_alloc() and returns early from xfs_fsverity_reset_inode(), leaving XFS_DIFLAG2_VERITY set in ip->i_diflags2 while S_VERITY is clear.
[ ... ]
Quoted 18 lines
fs/xfs/xfs_fsverity.c +18
-0+/* +* Write a merkle tree block. +*/ +static int +xfs_fsverity_write_merkle ( +struct file *file, +const void *buf, +u64 pos, +unsigned int size, +const u8 *zero_digest, +unsigned int digest_size) +{ +struct inode *inode =file_inode (file); +struct xfs_inode *ip =XFS_I (inode); +loff_t position = pos + +xfs_fsverity_metadata_offset (ip); + +if (position + size > inode->i_sb->s_maxbytes)
fs/xfs/xfs_fsverity.c xfs_fsverity_is_file_data( · line 491
Quoted 10 lines
fs/xfs/xfs_fsverity.c +10
-0+return -EFBIG ; + +if (xfs_fsverity_is_hashes_of_zeroed_blocks (ip, buf, size, zero_digest, + digest_size)) +return 0 ; + +return iomap_fsverity_write (file, position, size, buf, + xfs_buffered_write_iomap_next, + &xfs_iomap_write_ops); +}
[Severity: High]
Is it safe to call iomap_fsverity_write() here without holding XFS_IOLOCK_EXCL?
enable_verity() drops inode_lock() across build_merkle_tree(), and deny_write_access() only blocks FMODE_WRITE opens. Other XFS paths rely on XFS_IOLOCK_EXCL | XFS_MMAPLOCK_EXCL to serialize against buffered writes and do not check fsverity_is_building(), including background blockgc (xfs_inode_free_cowblocks()), O_RDONLY ioctls (FS_IOC_FSSETXATTR, FICLONE, FIDEDUPERANGE), and path-based file_setattr():
CPU0 CPU1enable_verity()
build_merkle_tree()
xfs_fsverity_write_merkle()
iomap_fsverity_write()
xfs_buffered_write_iomap_begin()
/* allocates COW delalloc */
iomap_write_begin()
xfs_iomap_valid()
xfs_inode_free_cowblocks()
xfs_ilock(ip, XFS_IOLOCK_EXCL |
XFS_MMAPLOCK_EXCL)
xfs_reflink_cancel_cow_range()
/* cancels COW delalloc */
xfs_iunlock(...)
iomap_write_end()
/* dirties folio over canceled reservation;
writeback maps a hole and drops the Merkle block */Acquiring XFS_IOLOCK_EXCL around iomap_fsverity_write() (or checking fsverity_is_building() in the racing paths) would prevent concurrent COW block freeing, reflink remapping, and fileattr changes from racing with Merkle tree writes.
Quoted 5 lines
fs/xfs/xfs_ioctl.c +0
-0
fs/xfs/xfs_ioctl.c xfs_ioctl_setattr_xflags( · line 572
Quoted 10 lines
fs/xfs/xfs_ioctl.c +7
-0@@ -562,6 +563,14 @@ xfs_ioctl_setattr_xflags(if (xfs_inode_has_filedata (ip))return -EINVAL ; +/* +* fs-verity is incompatible with a realtime allocation unit +* larger than a single filesystem block, so don't let a file be +* converted to realtime while verity is being enabled. +*/ +if (fsverity_is_building (VFS_IC (ip))) +return -EINVAL ;
[Severity: Medium]
Should this also reject changing the realtime flag when verity is already enabled on the inode (IS_VERITY(VFS_IC(ip)) or (ip->i_diflags2 & XFS_DIFLAG2_VERITY))?
If a corrupted on-disk inode has XFS_DIFLAG2_VERITY set with zero extents, xfs_inode_has_filedata(ip) is false, and the path-based file_setattr() syscall can reach xfs_ioctl_setattr_xflags() without opening the file. Setting FS_XFLAG_REALTIME on a filesystem with sb_rextsize > 1 then logs an inode with both XFS_DIFLAG_REALTIME and XFS_DIFLAG2_VERITY, which trips xfs_dinode_verify() on writeback and shuts down the filesystem.
Quoted 7 lines
fs/xfs/xfs_ioctl.c +0
-0/* * If S_DAX is enabled on this file, we can only switch the * device if both support fsdax, and the zoned code does not. @@ -576,6 +585,17 @@ xfs_ioctl_setattr_xflags(return -EINVAL ; }
fs/xfs/xfs_ioctl.c xfs_ioctl_setattr_xflags( · line 597
Quoted 10 lines
fs/xfs/xfs_ioctl.c +10
-0+/* +* fs-verity is incompatible with DAX, and xfs_fsverity_begin_enable() +* verified the inode was not DAX under the inode lock. That lock is +* dropped while the Merkle tree is built, so reject enabling DAX while +* verity is being enabled. +*/ +if ((fa->fsx_xflags &FS_XFLAG_DAX ) && + !(ip->i_diflags2 &XFS_DIFLAG2_DAX ) && +fsverity_is_building (VFS_IC (ip))) +return -EINVAL ;
[Severity: Medium]
Should xfs_ioctl_setattr_check_extsize() also reject changing ip->i_extsize when fsverity_is_building(VFS_IC(ip)) is true?
On a sparse file where i_size > 0 but xfs_inode_has_filedata(ip) is still false before the first Merkle tree extent is allocated, xfs_iomap_write_direct() computes the transaction block reservation from xfs_get_extsz_hint(ip) before acquiring XFS_ILOCK_EXCL. A concurrent FS_IOC_FSSETXATTR or file_setattr() increasing ip->i_extsize in that window causes xfs_bmapi_write() to allocate using the larger extent size hint, overrunning tp->t_blk_res and shutting down the filesystem:
CPU0 CPU1xfs_fsverity_write_merkle()
iomap_fsverity_write()
...
xfs_iomap_write_direct()
resaligned = xfs_aligned_fsb_count(...,
xfs_get_extsz_hint(ip))
xfs_fileattr_set()
xfs_ioctl_setattr_check_extsize()
/* xfs_inode_has_filedata(ip) == 0 */
ip->i_extsize = ...; /* larger */
xfs_trans_alloc_inode(...)
xfs_bmapi_write()
/* allocates with new i_extsize,
overrunning tp->t_blk_res */Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=17
PATCH v20 18/23 xfs: initialize fs-verity on file open
In reply to Andrey Albershteynfs-verity will read and attach metadata (not the tree itself) from a disk for those inodes which already have fs-verity enabled.
--- fs/xfs/xfs_file.c | 7 +++++++ 1 file changed, 7 insertions(+)
fs/xfs/xfs_file.c +7 -0
@@ -1813,11 +1813,18 @@ xfs_file_open(struct inode *inode, struct file *file) { + int error; +if (xfs_is_shutdown (XFS_M (inode->i_sb)))return -EIO ; file->f_mode |=FMODE_NOWAIT |FMODE_CAN_ODIRECT ;if (xfs_get_atomic_write_min (XFS_I (inode)) >0 ) file->f_mode |=FMODE_CAN_ATOMIC_WRITE ; + + error =fsverity_file_open (inode, file); +if (error) +return error; +return generic_file_open (inode, file); }
Signature · 1 line
2.54.0
PATCH v20 19/23 xfs: add fs-verity ioctls
In reply to Andrey AlbershteynAdd fs-verity ioctls to enable, dump metadata (descriptor and Merkle tree pages) and obtain file's digest.
[djwong: remove unnecessary casting]
--- fs/xfs/xfs_ioctl.c | 13 +++++++++++++ 1 file changed, 13 insertions(+)
fs/xfs/xfs_ioctl.c +13 -0
@@ -1610,6 +1610,19 @@ xfs_file_ioctl(case XFS_IOC_VERIFY_MEDIA :return xfs_ioc_verify_media (filp, arg); +case FS_IOC_ENABLE_VERITY : +if (!xfs_has_verity (mp)) +return -EOPNOTSUPP ; +return fsverity_ioctl_enable (filp, arg); +case FS_IOC_MEASURE_VERITY : +if (!xfs_has_verity (mp)) +return -EOPNOTSUPP ; +return fsverity_ioctl_measure (filp, arg); +case FS_IOC_READ_VERITY_METADATA : +if (!xfs_has_verity (mp)) +return -EOPNOTSUPP ; +return fsverity_ioctl_read_metadata (filp, arg); +default :return -ENOTTY ; }
Signature · 1 line
2.54.0
PATCH v20 20/23 xfs: advertise fs-verity being available on filesystem
In reply to Andrey AlbershteynFrom: "Darrick J. Wong" <[email protected]>
Advertise that this filesystem supports fsverity.
--- fs/xfs/libxfs/xfs_fs.h | 1 + fs/xfs/libxfs/xfs_sb.c | 2 ++ 2 files changed, 3 insertions(+)
fs/xfs/libxfs/xfs_fs.h +1 -0
@@ -250,6 +250,7 @@ typedef struct xfs_fsop_resblks {#define XFS_FSOP_GEOM_FLAGS_PARENT (1 << 25)/* linux parent pointers */ #define XFS_FSOP_GEOM_FLAGS_METADIR (1 << 26)/* metadata directories */ #define XFS_FSOP_GEOM_FLAGS_ZONED (1 << 27)/* zoned rt device */ +#define XFS_FSOP_GEOM_FLAGS_VERITY (1 << 28)/* fs-verity */ /* * Minimum and maximum sizes need for growth checks.
fs/xfs/libxfs/xfs_sb.c +2 -0
@@ -1546,6 +1546,8 @@ xfs_fs_geometry( geo->flags |=XFS_FSOP_GEOM_FLAGS_METADIR ;if (xfs_has_zoned (mp)) geo->flags |=XFS_FSOP_GEOM_FLAGS_ZONED ; +if (xfs_has_verity (mp)) + geo->flags |=XFS_FSOP_GEOM_FLAGS_VERITY ; geo->rtsectsize = sbp->sb_blocksize; geo->dirblocksize =xfs_dir2_dirblock_bytes (sbp);
Signature · 1 line
2.54.0
PATCH v20 21/23 xfs: check and repair the verity inode flag state
In reply to Andrey AlbershteynFrom: "Darrick J. Wong" <[email protected]>
If an inode has the incore verity iflag set, make sure that we can actually activate fsverity on that inode. If activation fails due to a fsverity metadata validation error, clear the flag and delete fs-verity metadata. The usage model for fsverity requires that any program that cares about verity state is required to call statx/getflags to check that the flag is set after opening the file, so clearing the flag will not compromise that model.
--- fs/xfs/libxfs/xfs_inode_buf.c | 8 +++ fs/xfs/scrub/common.c | 59 +++++++++++++++++ fs/xfs/scrub/common.h | 2 + fs/xfs/scrub/inode.c | 40 +++++++++++ fs/xfs/scrub/inode_repair.c | 121 ++++++++++++++++++++++++++++++++-- 5 files changed, 226 insertions(+), 4 deletions(-)
fs/xfs/libxfs/xfs_inode_buf.c +8 -0
@@ -775,6 +775,14 @@ xfs_dinode_verify(*/ if ((flags &XFS_DIFLAG_REALTIME ) && mp->m_sb.sb_rextsize >1 )return __this_address; + +/* +* fs-verity doesn't work on zoned filesystems, where all +* data writes are directed to the sequential zones of the +* realtime device. +*/ +if (xfs_has_zoned (mp) && (flags &XFS_DIFLAG_REALTIME )) +return __this_address; } if (xfs_has_zoned(mp) &&
fs/xfs/scrub/common.c +59 -0
68 lines not on this page · raw message
fs/xfs/scrub/common.h +2 -0
@@ -261,6 +261,8 @@ int xchk_inode_is_allocated(struct xfs_scrub *sc, xfs_agino_t agino, bool *inuse);int xchk_inode_count_blocks (struct xfs_scrub *sc,int whichfork,xfs_extnum_t *nextents,xfs_filblks_t *count); +int xchk_inode_setup_verity (struct xfs_scrub *sc); +bool xchk_inode_verity_broken (struct xfs_inode *ip);bool xchk_inode_is_dirtree_root (const struct xfs_inode *ip);bool xchk_inode_is_sb_rooted (const struct xfs_inode *ip);
fs/xfs/scrub/inode.c +40 -0
@@ -33,9 +33,28 @@ xchk_prepare_iscrub( struct xfs_scrub *sc) {int error; +struct xfs_inode *ip = sc->ip;xchk_ilock (sc,XFS_IOLOCK_EXCL ); + error =xchk_inode_setup_verity (sc); +if (error) +return error; + +/* +* If we're going to repair a file whose fsverity descriptor cannot be +* activated, drop the stale Merkle tree folios. Once the +* post-EOF Merkle extents are freed during repair, those uptodate +* folios would otherwise sit over a hole and could be exposed as file +* data by a later extending write or truncate. +* +* The lock would be unlocked by global scrub machinery. +*/ +if (xchk_could_repair (sc) &&xchk_inode_verity_broken (ip)) { +xchk_ilock (sc,XFS_MMAPLOCK_EXCL ); +truncate_inode_pages (VFS_I (ip)->i_mapping,XFS_ISIZE (ip)); + } + error =xchk_trans_alloc (sc,0 );if (error)return error;
@@ -388,6 +407,24 @@ xchk_inode_flags2(if ((flags2 &XFS_DIFLAG2_NREXT64 ) && !xfs_has_large_extent_counts (mp)) goto bad; +/* no verity iflag without the filesystem feature or on non-files */ +if ((flags2 &XFS_DIFLAG2_VERITY ) && + (!xfs_has_verity (mp) || !S_ISREG (mode))) + goto bad; + +/* verity and DAX do not mix */ +if ((flags2 &XFS_DIFLAG2_VERITY ) && (flags2 &XFS_DIFLAG2_DAX )) + goto bad; + +/* +* no verity iflag on realtime files that cannot support it, i.e. +* zoned files and files with a realtime allocation unit larger +* than a filesystem block +*/ +if ((flags2 &XFS_DIFLAG2_VERITY ) && (flags &XFS_DIFLAG_REALTIME ) && + (xfs_has_zoned (mp) || mp->m_sb.sb_rextsize >1 )) + goto bad; +return ; bad:xchk_ino_set_corrupt (sc, ino);
@@ -833,6 +870,9 @@ xchk_inode(if (S_ISREG (VFS_I (sc->ip)->i_mode))xchk_inode_check_reflink_iflag (sc,I_INO (sc->ip)); +if (xchk_inode_verity_broken (sc->ip)) +xchk_ino_set_corrupt (sc, sc->sm->sm_ino); +xchk_inode_check_unlinked (sc);xchk_inode_xref (sc,I_INO (sc->ip), &di);
fs/xfs/scrub/inode_repair.c +117 -4
174 lines not on this page · raw message
Signature · 1 line
2.54.0
[email protected]
AI reviewRe: [f2fs-dev] [PATCH v20 21/23] xfs: check and repair the verity inode flag state
Thank you for your contribution! Sashiko AI review found 9 potential issues to consider: - [High] xfs: XFS_DIFLAG2_VERITY on metadata inodes causes repair to unmap quota blocks - [Medium] xfs…
Thank you for your contribution! Sashiko AI review found 9 potential issues to consider:
- [High] xfs: XFS_DIFLAG2_VERITY on metadata inodes causes repair to unmap quota blocks
- [Medium] xfs: CONFIG_FS_VERITY guard in xrep_dinode_flags() fails dinode repair
- [Medium] xfs: unhandled error codes in xchk_inode_setup_verity() abort scrub and repair
- [Medium] xfs: missing VERITY+APPEND check in xchk_inode_flags2() and flag sync in repair
- [Medium] xfs: zero block reservation in xrep_dinode_core() for bigrtalloc verity zap
- [Medium] xfs: verity scrub and repair mishandle corrupt or zapped data forks
- [Medium] xfs: race in xchk_prepare_iscrub() leaves stale post-EOF verity folios
- [Low] xfs: xchk_inode() passes sm_ino instead of I_INO(sc->ip) on broken verity
- [Low] xfs: xrep_inode_verity() runs after reflink and flag repairs in xrep_inode()
--
commit bc2ede889b804f9a3cfcdae388d72f25dd72bc0e
Author: Darrick J. Wong <[email protected]>
xfs: check and repair the verity inode flag state
This commit adds dinode verification, online scrub checks, and repair support for the fsverity inode flag and post-EOF Merkle tree extents.
Quoted 50 lines
fs/xfs/scrub/common.c +35
-0@@ -45,6 +45,8 @@#include "scrub/health.h" #include "scrub/tempfile.h" +#include <linux/fsverity.h> +/* Common code for the metadata scrubbers. */ /* @@ -1765,3 +1767,60 @@ xchk_inode_count_blocks(return xfs_bmap_count_blocks (sc->tp, sc->ip, whichfork, nextents, count); } + +/* +* If this inode has S_VERITY set on it, read the verity info. If the reading +* fails with anything other than ENOMEM, the file is corrupt, which we can +* detect later with fsverity_active. +* +* Callers must hold the IOLOCK and must not hold the ILOCK of sc->ip because +* activation reads inode data. +*/ +int +xchk_inode_setup_verity ( +struct xfs_scrub *sc) +{ +int error; + +if (!fsverity_active (VFS_I (sc->ip))) +return 0 ; + + error =fsverity_ensure_verity_info (VFS_I (sc->ip)); +switch (error) { +case 0 : +/* fsverity is active */ +break ; +case -EFBIG : +case -EINVAL : +case -ENODATA : +case -EMSGSIZE : +case -EFSCORRUPTED : +case -ERANGE : +case -EBADMSG : +/* +* The nonzero errno codes above are the error codes that can +* be returned from fsverity on metadata validation errors.
fs/xfs/scrub/common.c xchk_inode_count_blocks( · line 1812
Quoted 10 lines
fs/xfs/scrub/common.c +10
-0+* +* EINVAL and EFBIG are not always mean corruption. For example, +* missing hash algorithm could mean kernel doesn't support it +* or fsverity descriptor has corrupted value. Let's clear it. +*/ +return 0 ; +default : +/* runtime errors */ +return error; + }
[Severity: Medium]
Can unhandled validation errors from fsverity_ensure_verity_info() abort inode scrub and repair before xchk_inode() or xrep_inode() can run?
xfs_fsverity_get_descriptor() returns -EOPNOTSUPP when sb_blocksize > PAGE_SIZE, xfs_bmap_last_extent() can return -EFSBADCRC on a BMBT block with a bad CRC, and fsverity_verify_signature() can return -ENOKEY, -EKEYREJECTED, -EPERM, -ENOPKG, -EKEYEXPIRED, or -EKEYREVOKED when CONFIG_FS_VERITY_BUILTIN_SIGNATURES is enabled (for example, -ENOKEY for any non-zero sig_size when the .fs-verity keyring is empty). Because those codes hit the default branch, xchk_prepare_iscrub() and xrep_dinode_core() fail instead of treating verity activation as broken.
[ ... ]
Quoted 19 lines
fs/xfs/scrub/inode.c +8
-0@@ -33,9 +33,28 @@ xchk_prepare_iscrub( struct xfs_scrub *sc) {int error; +struct xfs_inode *ip = sc->ip;xchk_ilock (sc,XFS_IOLOCK_EXCL ); + error =xchk_inode_setup_verity (sc); +if (error) +return error; + +/* +* If we're going to repair a file whose fsverity descriptor cannot be +* activated, drop the stale Merkle tree folios. Once the
fs/xfs/scrub/inode.c xchk_prepare_iscrub( · line 56
Quoted 10 lines
fs/xfs/scrub/inode.c +10
-0+* post-EOF Merkle extents are freed during repair, those uptodate +* folios would otherwise sit over a hole and could be exposed as file +* data by a later extending write or truncate. +* +* The lock would be unlocked by global scrub machinery. +*/ +if (xchk_could_repair (sc) &&xchk_inode_verity_broken (ip)) { +xchk_ilock (sc,XFS_MMAPLOCK_EXCL ); +truncate_inode_pages (VFS_I (ip)->i_mapping,XFS_ISIZE (ip)); + }
[Severity: Medium]
Can a concurrent open() fault post-EOF verity folios back into i_mapping after truncate_inode_pages() runs here (and in xrep_dinode_core()) before xrep_inode_zap_verity() clears S_VERITY?
CPU0 CPU1xchk_prepare_iscrub()
xchk_ilock(sc, XFS_MMAPLOCK_EXCL)
truncate_inode_pages(..., isize)
xfs_file_open()
fsverity_file_open()
...
xfs_fsverity_get_descriptor()
xfs_ilock(ip, XFS_ILOCK_EXCL)
/* S_VERITY is still set */
xfs_bmap_last_extent(...)
xfs_iunlock(ip, XFS_ILOCK_EXCL)
fsverity_pagecache_read()
read_mapping_folio()
/* re-populates post-EOF
* folio in i_mapping */ xchk_trans_alloc(sc, 0)
xchk_ino_dqattach(sc)
xchk_ilock(sc, XFS_ILOCK_EXCL)
...xrep_inode_zap_verity()
xfs_bunmapi_range(...)
inode_set_flags(inode, 0, S_VERITY)
<-- uptodate post-EOF folio remains
over unmapped hole
Neither xfs_fsverity_get_descriptor() nor read_mapping_folio() takes XFS_MMAPLOCK (mapping->invalidate_lock) or XFS_IOLOCK, so holding XFS_MMAPLOCK_EXCL does not serialize against concurrent descriptor reads while S_VERITY is still set. Once xrep_inode_zap_verity() unmaps the post-EOF blocks and clears S_VERITY, those newly faulted folios remain in the page cache and can be exposed as file data by a later extending write or truncate.
[ ... ]
fs/xfs/scrub/inode.c xchk_inode( · line 874
Quoted 6 lines
fs/xfs/scrub/inode.c +2
-0@@ -833,6 +870,9 @@ xchk_inode(if (S_ISREG (VFS_I (sc->ip)->i_mode))xchk_inode_check_reflink_iflag (sc,I_INO (sc->ip)); +if (xchk_inode_verity_broken (sc->ip)) +xchk_ino_set_corrupt (sc, sc->sm->sm_ino);
[Severity: Low]
Should this pass I_INO(sc->ip) instead of sc->sm->sm_ino to xchk_ino_set_corrupt()? When scrubbing by file descriptor with sm_ino == 0, passing sc->sm->sm_ino records inode 0 in trace_xchk_ino_error().
Quoted 4 lines
fs/xfs/scrub/inode_repair.c +0
-0
[ ... ]
Quoted 12 lines
fs/xfs/scrub/inode_repair.c +8
-0@@ -577,6 +585,24 @@ xrep_dinode_flags( dip->di_nrext64_pad =0 ; else if (dip->di_version >= 3) dip->di_v3_pad = 0; +if (!xfs_has_verity (mp) || !S_ISREG (mode)) + flags2 &= ~XFS_DIFLAG2_VERITY ; + +/* +* fs-verity does not work on zoned inodes or on realtime inodes +* with a realtime allocation unit larger than a filesystem block. +* The realtime flag cannot be cleared here because the file's data +* is already stored on the realtime device, so the fsverity flag
fs/xfs/scrub/inode_repair.c xrep_dinode_flags( · line 605
Quoted 10 lines
fs/xfs/scrub/inode_repair.c +10
-0+* has to go. Free the orphaned Merkle tree past EOF once the inode +* can be loaded again. +*/ +if (IS_ENABLED (CONFIG_FS_VERITY ) && + (flags &XFS_DIFLAG_REALTIME ) && (flags2 &XFS_DIFLAG2_VERITY ) && + (xfs_has_zoned (mp) || mp->m_sb.sb_rextsize >1 )) { + flags2 &= ~XFS_DIFLAG2_VERITY ; +if (xfs_has_verity (mp) &&S_ISREG (mode)) + ri->zap_verity = true; + }
[Severity: Medium]
Does guarding this block with IS_ENABLED(CONFIG_FS_VERITY) cause dinode repair to fail when CONFIG_FS_VERITY is disabled? xfs_dinode_verify() rejects XFS_DIFLAG2_VERITY combined with XFS_DIFLAG_REALTIME on zoned or sb_rextsize > 1 filesystems unconditionally, so leaving XFS_DIFLAG2_VERITY set here causes the subsequent xchk_iget() in xrep_dinode_core() to fail with -EFSCORRUPTED.
[ ... ]
fs/xfs/scrub/inode_repair.c xrep_dinode_core( · line 1690
Quoted 7 lines
fs/xfs/scrub/inode_repair.c +3
-0@@ -1659,6 +1685,22 @@ xrep_dinode_core(if (iget_error)return iget_error; + error =xchk_inode_setup_verity (sc); +if (error) +return error;
[Severity: Medium]
What happens when xrep_dinode_core() zaps a corrupt data fork (setting XFS_SICK_INO_BMBTD_ZAPPED and resetting the fork to 0 extents) or when an inode has a corrupt out-of-line data fork BMBT?
If xrep_dinode_zap_forks() just zapped the data fork, calling xchk_inode_setup_verity() on the empty fork fails with -ENODATA, which causes xrep_inode_verity() to clear XFS_DIFLAG2_VERITY while xfs_bunmapi_range() is a no-op on the empty fork; when BMBT repair later reconstructs the data fork from rmapbt, it reattaches the post-EOF Merkle tree extents onto a non-verity file. Conversely, if an inode passes xfs_iget() with XFS_DIFLAG2_VERITY set but has a corrupt out-of-line BMBT, xchk_inode_setup_verity() swallows -EFSCORRUPTED from xfs_iread_extents(), causing xchk_inode() to mark the inode core corrupt and xrep_inode() to fail in xrep_inode_blockcounts() before BMBT repair can fix the data fork.
Quoted 6 lines
fs/xfs/scrub/inode_repair.c +6
-0+ +/* +* If verity activation failed, the descriptor read above may have left +* uptodate post-EOF folios in the page cache. Drop them before +* xrep_inode_verity frees the Merkle tree, as xchk_prepare_iscrub does +* for the normal setup path.
fs/xfs/scrub/inode_repair.c xrep_dinode_core( · line 1706
Quoted 10 lines
fs/xfs/scrub/inode_repair.c +7
-0+*/ +if (xchk_inode_verity_broken (sc->ip) || ri->zap_verity) { +xchk_ilock (sc,XFS_MMAPLOCK_EXCL ); +truncate_inode_pages (VFS_I (sc->ip)->i_mapping, +XFS_ISIZE (sc->ip)); + } + error =xchk_trans_alloc (sc,0 );if (error)return error;
[Severity: Medium]
Is a zero block reservation sufficient here when ri->zap_verity is set on a realtime inode with sb_rextsize > 1?
When xrep_inode_zap_verity() calls xfs_bunmapi_range() starting at XFS_B_TO_FSB(mp, XFS_ISIZE(sc->ip)) on an inode with xfs_inode_has_bigrtalloc(ip), __xfs_bunmapi() requires block reservation (such as XFS_DIOSTRAT_SPACE_RES(mp, 0)) to split or convert a partial realtime extent to unwritten; with t_blk_res == 0 it can trip ASSERT(tp->t_blk_res > 0), skip unmapping the partial tail extent at startoff, or fail a BMBT split with -ENOSPC on a dirty transaction.
fs/xfs/scrub/inode_repair.c xrep_inode_flags( · line 1946
Quoted 13 lines
fs/xfs/scrub/inode_repair.c +8
-0@@ -1893,6 +1935,14 @@ xrep_inode_flags(/* DAX only applies to files and dirs. */ if (!(S_ISREG (mode) ||S_ISDIR (mode))) sc->ip->i_diflags2 &= ~XFS_DIFLAG2_DAX ; + +/* DAX and fsverity do not mix. */ +if (sc->ip->i_diflags2 &XFS_DIFLAG2_VERITY ) + sc->ip->i_diflags2 &= ~XFS_DIFLAG2_DAX ; + +/* APPEND and fsverity do not mix. */ +if (sc->ip->i_diflags2 &XFS_DIFLAG2_VERITY ) + sc->ip->i_diflags &= ~XFS_DIFLAG_APPEND ; }
[Severity: Medium]
Does clearing XFS_DIFLAG_APPEND here leave S_APPEND set in VFS_I(sc->ip)->i_flags, and is xchk_inode_flags2() missing the corresponding check for (flags2 & XFS_DIFLAG2_VERITY) && (flags & XFS_DIFLAG_APPEND)?
xchk_inode_flags2() checks for VERITY combined with DAX but never checks for VERITY combined with APPEND, so xchk_inode() will not flag an inode with both flags set as corrupt. When xrep_inode_flags() does run and clears XFS_DIFLAG_APPEND, nothing syncs VFS_I(sc->ip)->i_flags (unlike xrep_inode_zap_verity(), which calls inode_set_flags()), leaving the in-core VFS inode append-only.
Quoted 18 lines
fs/xfs/scrub/inode_repair.c +14
-0@@ -2198,6 +2248,60 @@ xrep_inode_reset_dquots(# define xrep_inode_reset_dquots (sc) (0)#endif +/* +* Disable fsverity on this file and free the Merkle tree, which XFS stores +* in extents beyond EOF. The stale Merkle tree page cache must have been +* dropped before calling this function. +*/ +STATIC int +xrep_inode_zap_verity ( +struct xfs_scrub *sc) +{ +struct inode *inode =VFS_I (sc->ip); +struct xfs_mount *mp = sc->mp; +int error; + + error = xfs_bunmapi_range(&sc->tp, sc->ip, 0,
fs/xfs/scrub/inode_repair.c xrep_inode_reset_dquots( · line 2274
Quoted 10 lines
fs/xfs/scrub/inode_repair.c +10
-0+ XFS_B_TO_FSB(mp, XFS_ISIZE(sc->ip)), XFS_MAX_FILEOFF); +if (error) +return error; + + sc->ip->i_diflags2 &= ~XFS_DIFLAG2_VERITY ; +inode_set_flags (inode,0 ,S_VERITY ); + +xfs_trans_log_inode (sc->tp, sc->ip,XFS_ILOG_CORE ); +return 0 ; +}
[Severity: High]
What happens if XFS_DIFLAG2_VERITY is corruptly set on a metadata regular file such as a quota inode or a META_BTREE inode?
Neither xfs_dinode_verify(), xfs_dinode_verify_metadir(), xchk_inode_flags2(), xrep_dinode_flags(), nor xrep_inode_flags() forbids or clears XFS_DIFLAG2_VERITY on metadata inodes. When a metadata inode is repaired via xrep_metadata_inode_forks() -> xrep_inode(), xrep_inode_zap_verity() unmaps from XFS_B_TO_FSB(mp, XFS_ISIZE(sc->ip)) to XFS_MAX_FILEOFF. On a quota inode, XFS_ISIZE(sc->ip) is always 0 while allocated dquot chunks reside in the data fork, so xfs_bunmapi_range() frees all dquot blocks from offset 0 while live in-core dquots still reference them. On an XFS_DINODE_FMT_META_BTREE inode, __xfs_bunmapi() fails with -EFSCORRUPTED before XFS_DIFLAG2_VERITY can be cleared.
Quoted 35 lines
fs/xfs/scrub/inode_repair.c +30
-0+ +/* +* If this file was a fsverity file, xchk_prepare_iscrub or xrep_dinode_core +* should have activated it. If it's still not active, then there's something +* wrong with the verity descriptor, so turn verity off and free the orphaned +* Merkle tree that XFS keeps in extents beyond EOF. We also free the Merkle +* tree if dinode repair had to clear the verity flag. +*/ +STATIC int +xrep_inode_verity ( +struct xfs_scrub *sc) +{ +struct xrep_inode *ri = sc->buf; + +/* +* If we had to clear the fsverity flag while repairing the ondisk +* inode or we failed to activate the fsverity descriptor, clear the +* fsverity flag and free the orphaned Merkle tree that XFS stores +* past EOF. +*/ +if (!xchk_inode_verity_broken (sc->ip) && !(ri && ri->zap_verity)) +return 0 ; + +/* +* The post-EOF verity folios were dropped by xchk_prepare_iscrub or +* xrep_dinode_core before we got here. +*/ +return xrep_inode_zap_verity (sc); +} +/* Repair an inode's fields. */ int xrep_inode(@@ -2247,6 +2351,15 @@ xrep_inode(return error;
fs/xfs/scrub/inode_repair.c xrep_inode( · line 2361
Quoted 10 lines
fs/xfs/scrub/inode_repair.c +8
-0} +/* +* Disable fsverity if it cannot be activated. Activation failure +* prohibits the file from being opened, so there cannot be another +* program with an open fd to what it thinks is a verity file. +*/ + error =xrep_inode_verity (sc); +if (error) +return error;
[Severity: Low]
Should xrep_inode_verity() run before xrep_inode_problems() and xfs_reflink_clear_inode_flag()?
Running xrep_inode_problems() first causes xrep_inode_flags() to strip XFS_DIFLAG2_DAX and XFS_DIFLAG_APPEND on the basis of a broken XFS_DIFLAG2_VERITY flag that xrep_inode_verity() is about to clear. Running xfs_reflink_clear_inode_flag() before xrep_inode_verity() also scans post-EOF Merkle tree extents before they are unmapped; if only those post-EOF extents were shared, XFS_DIFLAG2_REFLINK is left set on an inode with no remaining shared extents.
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=21
PATCH v20 22/23 xfs: introduce health state for corrupted fsverity metadata
In reply to Andrey AlbershteynReport corrupted fsverity descriptor through health system.
--- fs/xfs/libxfs/xfs_fs.h | 1 + fs/xfs/libxfs/xfs_health.h | 4 +++- fs/xfs/xfs_fsverity.c | 25 +++++++++++++++++++------ fs/xfs/xfs_health.c | 1 + 4 files changed, 24 insertions(+), 7 deletions(-)
fs/xfs/libxfs/xfs_fs.h +1 -0
@@ -422,6 +422,7 @@ struct xfs_bulkstat {#define XFS_BS_SICK_SYMLINK (1 << 6)/* symbolic link remote target */ #define XFS_BS_SICK_PARENT (1 << 7)/* parent pointers */ #define XFS_BS_SICK_DIRTREE (1 << 8)/* directory tree structure */ +#define XFS_BS_SICK_FSVERITY (1 << 9)/* fsverity metadata */ /* * Project quota id helpers (previously projid was 16bit only
fs/xfs/libxfs/xfs_health.h +3 -1
@@ -104,6 +104,7 @@ struct xfs_rtgroup;/* Don't propagate sick status to ag health summary during inactivation */ #define XFS_SICK_INO_FORGET (1 << 12)#define XFS_SICK_INO_DIRTREE (1 << 13)/* directory tree structure */ +#define XFS_SICK_INO_FSVERITY (1 << 14)/* fsverity metadata */ /* Primary evidence of health problems in a given group. */ #define XFS_SICK_FS_PRIMARY (XFS_SICK_FS_COUNTERS | \
@@ -140,7 +141,8 @@ struct xfs_rtgroup; XFS_SICK_INO_XATTR | \ XFS_SICK_INO_SYMLINK | \ XFS_SICK_INO_PARENT | \ - XFS_SICK_INO_DIRTREE) + XFS_SICK_INO_DIRTREE | \ + XFS_SICK_INO_FSVERITY)#define XFS_SICK_INO_ZAPPED (XFS_SICK_INO_BMBTD_ZAPPED | \ XFS_SICK_INO_BMBTA_ZAPPED | \
fs/xfs/xfs_fsverity.c +19 -6
@@ -98,8 +98,10 @@ xfs_fsverity_get_descriptor(if (error)return error; -if (is_empty) +if (is_empty) { +xfs_inode_mark_sick (XFS_I (inode),XFS_SICK_INO_FSVERITY );return -ENODATA ; + }/* * Make sure we can open large file on a 32-bit kernel
@@ -110,8 +112,10 @@ xfs_fsverity_get_descriptor( last_block_offset =XFS_FSB_TO_B (mp, rec.br_startoff + rec.br_blockcount); -if (last_block_offset <=xfs_fsverity_metadata_offset (ip)) +if (last_block_offset <=xfs_fsverity_metadata_offset (ip)) { +xfs_inode_mark_sick (XFS_I (inode),XFS_SICK_INO_FSVERITY );return -ENODATA ; + } desc_size_pos = last_block_offset -sizeof (__be32); error = fsverity_pagecache_read(inode, (char *)&d_desc_size,
@@ -120,10 +124,15 @@ xfs_fsverity_get_descriptor(return error; desc_size =be32_to_cpu (d_desc_size); -if (XFS_IS_CORRUPT (mp, desc_size >FS_VERITY_MAX_DESCRIPTOR_SIZE )) +if (XFS_IS_CORRUPT (mp, desc_size >FS_VERITY_MAX_DESCRIPTOR_SIZE )) { +xfs_inode_mark_sick (XFS_I (inode),XFS_SICK_INO_FSVERITY );return -ERANGE ; -if (XFS_IS_CORRUPT (mp, desc_size > desc_size_pos)) + } + +if (XFS_IS_CORRUPT (mp, desc_size > desc_size_pos)) { +xfs_inode_mark_sick (XFS_I (inode),XFS_SICK_INO_FSVERITY );return -ERANGE ; + }if (!buf_size)return desc_size;
@@ -132,12 +141,16 @@ xfs_fsverity_get_descriptor(return -ERANGE ;if (XFS_IS_CORRUPT (mp, - desc_size <sizeof (struct fsverity_descriptor ))) + desc_size <sizeof (struct fsverity_descriptor ))) { +xfs_inode_mark_sick (XFS_I (inode),XFS_SICK_INO_FSVERITY );return -ERANGE ; + } desc_pos =round_down (desc_size_pos - desc_size, blocksize); -if (desc_pos <xfs_fsverity_metadata_offset (ip)) +if (desc_pos <xfs_fsverity_metadata_offset (ip)) { +xfs_inode_mark_sick (XFS_I (inode),XFS_SICK_INO_FSVERITY );return -ERANGE ; + } error =fsverity_pagecache_read (inode, buf, desc_size, desc_pos);if (error)
fs/xfs/xfs_health.c +1 -0
@@ -625,6 +625,7 @@ static const struct ioctl_sick_map ino_map[] = { { XFS_SICK_INO_DIR_ZAPPED, XFS_BS_SICK_DIR }, { XFS_SICK_INO_SYMLINK_ZAPPED, XFS_BS_SICK_SYMLINK }, { XFS_SICK_INO_DIRTREE, XFS_BS_SICK_DIRTREE }, + { XFS_SICK_INO_FSVERITY, XFS_BS_SICK_FSVERITY }, };/* Fill out bulkstat health info. */
Signature · 1 line
2.54.0
[email protected]
AI reviewRe: [f2fs-dev] [PATCH v20 22/23] xfs: introduce health state for corrupted fsverity metadata
Thank you for your contribution! Sashiko AI review found 3 potential issues to consider: - [Medium] xfs: unreachable minimum descriptor size check in xfs_fsverity_get_descriptor() - [Medium] xfs…
Thank you for your contribution! Sashiko AI review found 3 potential issues to consider:
- [Medium] xfs: unreachable minimum descriptor size check in xfs_fsverity_get_descriptor()
- [Medium] xfs: XFS_SICK_INO_FSVERITY is never cleared by online scrub or repair
- [Medium] xfs: race between xfs_fsverity_get_descriptor() and online verity repair
--
commit 63bf6ece967dd5f4837d3ecdcb95059db67d9179
Author: Andrey Albershteyn <[email protected]>
xfs: introduce health state for corrupted fsverity metadata
This commit adds XFS_SICK_INO_FSVERITY and XFS_BS_SICK_FSVERITY to track and report corrupted fsverity metadata in the XFS health system.
Quoted 13 lines
fs/xfs/libxfs/xfs_health.h +1
-0@@ -104,6 +104,7 @@ struct xfs_rtgroup;/* Don't propagate sick status to ag health summary during inactivation */ #define XFS_SICK_INO_FORGET (1 << 12)#define XFS_SICK_INO_DIRTREE (1 << 13)/* directory tree structure */ +#define XFS_SICK_INO_FSVERITY (1 << 14)/* fsverity metadata */ /* Primary evidence of health problems in a given group. */ #define XFS_SICK_FS_PRIMARY (XFS_SICK_FS_COUNTERS | \
fs/xfs/libxfs/xfs_health.h struct xfs_rtgroup; · line 145
Quoted 6 lines
fs/xfs/libxfs/xfs_health.h +2
-1@@ -140,7 +141,8 @@ struct xfs_rtgroup;XFS_SICK_INO_XATTR | \XFS_SICK_INO_SYMLINK | \XFS_SICK_INO_PARENT | \ - XFS_SICK_INO_DIRTREE) +XFS_SICK_INO_DIRTREE | \ + XFS_SICK_INO_FSVERITY)
[Severity: Medium]
Does online scrub or repair ever clear XFS_SICK_INO_FSVERITY?
When xchk_prepare_iscrub() scrubs an inode with corrupted verity metadata, xchk_inode_setup_verity() -> xfs_fsverity_get_descriptor() sets XFS_SICK_INO_FSVERITY on sc->ip->i_sick. Neither type_to_health_flag[] for XFS_SCRUB_TYPE_INODE nor xchk_inode() includes XFS_SICK_INO_FSVERITY in the health mask. After xrep_inode_zap_verity() repairs the inode and the re-scrub pass calls xfs_inode_mark_healthy(), XFS_SICK_INO_CORE and XFS_SICK_INO_FORGET are cleared while XFS_SICK_INO_FSVERITY remains set. When the repaired inode is later inactivated, xfs_inactive_health() sees XFS_SICK_INO_FSVERITY without XFS_SICK_INO_FORGET and marks the AG sick with XFS_SICK_AG_INODES.
fs/xfs/xfs_fsverity.c xfs_fsverity_get_descriptor( · line 104
Quoted 13 lines
fs/xfs/xfs_fsverity.c +3
-1@@ -98,8 +98,10 @@ xfs_fsverity_get_descriptor(if (error)return error; -if (is_empty) +if (is_empty) { +xfs_inode_mark_sick (XFS_I (inode),XFS_SICK_INO_FSVERITY );return -ENODATA ; + }
[Severity: Medium]
Can an in-flight xfs_fsverity_get_descriptor() race with online verity repair and mark a repaired inode sick?
xfs_fsverity_get_descriptor() drops XFS_ILOCK_EXCL right after checking S_VERITY and calling xfs_bmap_last_extent(), before reading post-EOF folios and calling xfs_inode_mark_sick():
CPU0 CPU1xfs_fsverity_get_descriptor()
xfs_ilock(ip, XFS_ILOCK_EXCL)
/* checks S_VERITY, reads rec */
xfs_iunlock(ip, XFS_ILOCK_EXCL)
xchk_prepare_iscrub()
truncate_inode_pages(...)
xchk_ilock(sc, XFS_ILOCK_EXCL)
...
xrep_inode_zap_verity()
xfs_bunmapi_range(...)
inode_set_flags(..., 0, S_VERITY)
xchk_teardown_inode()
xfs_iunlock(ip, XFS_ILOCK_EXCL) fsverity_pagecache_read(...)
/* sees corrupted desc_size */
xfs_inode_mark_sick(ip,
XFS_SICK_INO_FSVERITY) <-- marks repaired non-verity inode sick
CPU0 can also repopulate post-EOF pagecache folios after truncate_inode_pages(), and the late XFS_SICK_INO_FSVERITY mark will propagate XFS_SICK_AG_INODES when the inode is inactivated.
Quoted 36 lines
fs/xfs/xfs_fsverity.c +11
-4@@ -110,8 +112,10 @@ xfs_fsverity_get_descriptor( last_block_offset =XFS_FSB_TO_B (mp, rec.br_startoff + rec.br_blockcount); -if (last_block_offset <=xfs_fsverity_metadata_offset (ip)) +if (last_block_offset <=xfs_fsverity_metadata_offset (ip)) { +xfs_inode_mark_sick (XFS_I (inode),XFS_SICK_INO_FSVERITY );return -ENODATA ; + } desc_size_pos = last_block_offset -sizeof (__be32); error = fsverity_pagecache_read(inode, (char *)&d_desc_size,@@ -120,10 +124,15 @@ xfs_fsverity_get_descriptor(return error; desc_size =be32_to_cpu (d_desc_size); -if (XFS_IS_CORRUPT (mp, desc_size >FS_VERITY_MAX_DESCRIPTOR_SIZE )) +if (XFS_IS_CORRUPT (mp, desc_size >FS_VERITY_MAX_DESCRIPTOR_SIZE )) { +xfs_inode_mark_sick (XFS_I (inode),XFS_SICK_INO_FSVERITY );return -ERANGE ; -if (XFS_IS_CORRUPT (mp, desc_size > desc_size_pos)) + } + +if (XFS_IS_CORRUPT (mp, desc_size > desc_size_pos)) { +xfs_inode_mark_sick (XFS_I (inode),XFS_SICK_INO_FSVERITY );return -ERANGE ; + }if (!buf_size)return desc_size;@@ -132,12 +141,16 @@ xfs_fsverity_get_descriptor(return -ERANGE ;if (XFS_IS_CORRUPT (mp, - desc_size <sizeof (struct fsverity_descriptor ))) + desc_size <sizeof (struct fsverity_descriptor ))) {
fs/xfs/xfs_fsverity.c xfs_fsverity_get_descriptor( · line 153
Quoted 10 lines
fs/xfs/xfs_fsverity.c +5
-1+xfs_inode_mark_sick (XFS_I (inode),XFS_SICK_INO_FSVERITY );return -ERANGE ; + } desc_pos =round_down (desc_size_pos - desc_size, blocksize); -if (desc_pos <xfs_fsverity_metadata_offset (ip)) +if (desc_pos <xfs_fsverity_metadata_offset (ip)) { +xfs_inode_mark_sick (XFS_I (inode),XFS_SICK_INO_FSVERITY );return -ERANGE ; + }
[Severity: Medium]
Can the desc_size < sizeof(struct fsverity_descriptor) corruption check ever be reached when the on-disk descriptor size is too small?
fsverity_get_descriptor() always probes the size with buf_size == 0 first:
fs/verity/open.c:fsverity_get_descriptor() {
...
size = inode->i_sb->s_vop->get_verity_descriptor(inode, NULL, 0);
if (size < 0) {
...
return size;
}
if (size < sizeof(*desc) || size > FS_VERITY_MAX_DESCRIPTOR_SIZE) {
...
return -EMSGSIZE;
}
}Because xfs_fsverity_get_descriptor() returns desc_size when !buf_size before checking desc_size < sizeof(struct fsverity_descriptor), an undersized on-disk desc_size is returned to fsverity_get_descriptor(), which fails with -EMSGSIZE without calling get_verity_descriptor() a second time. As a result, neither XFS_IS_CORRUPT() nor xfs_inode_mark_sick() runs for undersized descriptors. Moving the minimum size and desc_pos checks above if (!buf_size) would ensure they run on the initial probe.
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=22
PATCH v20 23/23 xfs: enable ro-compat fs-verity flag
In reply to Andrey AlbershteynFinalize fs-verity integration in XFS by making kernel fs-verity aware with ro-compat flag.
--- fs/xfs/libxfs/xfs_format.h | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-)
fs/xfs/libxfs/xfs_format.h +3 -2
@@ -378,8 +378,9 @@ xfs_sb_has_compat_feature(#define XFS_SB_FEAT_RO_COMPAT_ALL \ (XFS_SB_FEAT_RO_COMPAT_FINOBT | \ XFS_SB_FEAT_RO_COMPAT_RMAPBT | \ - XFS_SB_FEAT_RO_COMPAT_REFLINK| \ - XFS_SB_FEAT_RO_COMPAT_INOBTCNT) + XFS_SB_FEAT_RO_COMPAT_REFLINK | \ + XFS_SB_FEAT_RO_COMPAT_INOBTCNT | \ + XFS_SB_FEAT_RO_COMPAT_VERITY)#define XFS_SB_FEAT_RO_COMPAT_UNKNOWN ~XFS_SB_FEAT_RO_COMPAT_ALLstatic inline bool xfs_sb_has_ro_compat_feature(
Signature · 1 line
2.54.0
[email protected]
AI reviewRe: [f2fs-dev] [PATCH v20 23/23] xfs: enable ro-compat fs-verity flag
Thank you for your contribution! Sashiko AI review found 2 potential issues to consider: - [High] xfs: XFS_SB_FEAT_RO_COMPAT_VERITY enabled when CONFIG_FS_VERITY is unset - [Medium] xfs: missing…
Thank you for your contribution! Sashiko AI review found 2 potential issues to consider:
- [High] xfs: XFS_SB_FEAT_RO_COMPAT_VERITY enabled when CONFIG_FS_VERITY is unset
- [Medium] xfs: missing XFS_SICK_INO_FSVERITY clearing in xchk_inode() and repair
--
commit 5ecf493df61beb05b9242fc3c51aecbbc49d52d6
Author: Andrey Albershteyn <[email protected]>
xfs: enable ro-compat fs-verity flag
This commit adds XFS_SB_FEAT_RO_COMPAT_VERITY to XFS_SB_FEAT_RO_COMPAT_ALL so that filesystems with the fs-verity ro-compat feature flag can be mounted read-write.
Quoted 6 lines
fs/xfs/libxfs/xfs_format.h +0
-0@@ -378,8 +378,9 @@ xfs_sb_has_compat_feature(#define XFS_SB_FEAT_RO_COMPAT_ALL \
fs/xfs/libxfs/xfs_format.h xfs_sb_has_compat_feature( · line 386
Quoted 10 lines
fs/xfs/libxfs/xfs_format.h +3
-2(XFS_SB_FEAT_RO_COMPAT_FINOBT | \XFS_SB_FEAT_RO_COMPAT_RMAPBT | \ -XFS_SB_FEAT_RO_COMPAT_REFLINK | \ - XFS_SB_FEAT_RO_COMPAT_INOBTCNT) +XFS_SB_FEAT_RO_COMPAT_REFLINK | \ +XFS_SB_FEAT_RO_COMPAT_INOBTCNT | \ + XFS_SB_FEAT_RO_COMPAT_VERITY)#define XFS_SB_FEAT_RO_COMPAT_UNKNOWN ~XFS_SB_FEAT_RO_COMPAT_ALLstatic inline bool xfs_sb_has_ro_compat_feature(
[Severity: High]
As reported by Christoph Hellwig on v18 [1], should XFS_SB_FEAT_RO_COMPAT_VERITY be excluded from XFS_SB_FEAT_RO_COMPAT_ALL when CONFIG_FS_VERITY is disabled?
Allowing read-write mounts on a !CONFIG_FS_VERITY kernel lets online scrub and repair run with fsverity_active() stubbed out to false, and xrep_dinode_flags() skips clearing XFS_DIFLAG2_VERITY on realtime or zoned inodes because of its IS_ENABLED(CONFIG_FS_VERITY) guard:
fs/xfs/scrub/inode_repair.c:xrep_dinode_flags() {
...
if (IS_ENABLED(CONFIG_FS_VERITY) &&
(flags & XFS_DIFLAG_REALTIME) && (flags2 & XFS_DIFLAG2_VERITY) &&
(xfs_has_zoned(mp) || mp->m_sb.sb_rextsize > 1)) {
flags2 &= ~XFS_DIFLAG2_VERITY;
if (xfs_has_verity(mp) && S_ISREG(mode))
ri->zap_verity = true;
}
...
}Because xfs_dinode_verify() rejects that flag combination whenever xfs_has_verity(mp) is true, xrep_dinode_core() logs and commits an inode buffer that trips the write verifier and shuts down the filesystem.
[Severity: Medium]
Now that XFS_SB_FEAT_RO_COMPAT_VERITY filesystems can be mounted read-write and repaired online, does inode scrub or repair ever clear XFS_SICK_INO_FSVERITY?
When xfs_fsverity_get_descriptor() fails (including during xchk_prepare_iscrub() -> xchk_inode_setup_verity()), it sets XFS_SICK_INO_FSVERITY on ip->i_sick. However, type_to_health_flag[XFS_SCRUB_TYPE_INODE] only maps to XFS_SICK_INO_CORE, and neither xchk_inode() nor xrep_inode_zap_verity() adds XFS_SICK_INO_FSVERITY to sc->healthy_mask via xchk_mark_healthy_if_clean().
After xrep_inode_zap_verity() repairs a broken verity inode, the post-repair xchk_update_health() pass clears XFS_SICK_INO_CORE and XFS_SICK_INO_FORGET while leaving XFS_SICK_INO_FSVERITY set. When the repaired inode is later evicted, xfs_inactive_health() sees XFS_SICK_INO_FSVERITY without XFS_SICK_INO_FORGET and marks the parent AG sick with XFS_SICK_AG_INODES.
[1] https://lore.kernel.org/r/[email protected]
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=23
[email protected]
AI reviewSashiko has finished reviewing this 23-patch series:
Sashiko has finished reviewing this 23-patch series:
- Patches 1-4, 6-12, 16, and 18-20: no issues found.
- Patch 5 ("iomap: use fsverity_is_building() to check for fsver..."): 1 potential issue (1 Low)
- Patch 13 ("xfs: handle fsverity I/O in write/read path"): 1 potential issue (1 Medium)
- Patch 14 ("xfs: use read ioend for fsverity data verification"): 1 potential issue (1 High)
- Patch 15 ("xfs: add XFS_BMAPI_UNWRITTEN to unmap unwritten exte..."):
1 potential issue (1 Medium)
- Patch 17 ("xfs: add fs-verity support"): 8 potential issues (1 High, 5 Medium, 2 Low)
- Patch 21 ("xfs: check and repair the verity inode flag state"): 9 potential issues (1 High, 6 Medium, 2 Low)
- Patch 22 ("xfs: introduce health state for corrupted fsverity m..."):
3 potential issues (3 Medium)
- Patch 23 ("xfs: enable ro-compat fs-verity flag"): 2 potential issues (1 High, 1 Medium)
Detailed reviews have been sent as separate replies to patches 5, 13-15, 17, and 21-23.
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]