[RFC PATCH v2 2/4] erofs: use fs_context source_file for file-backed mounts when given

Eric Curtin <[email protected]> Mon, 27 Jul 2026 11:48:43 +0100
Newsgroups dev.linux.lists.fsverity,org.kernel.vger.linux-doc,org.kernel.vger.linux-fsdevel,org.kernel.vger.linux-kernel,org.ozlabs.lists.linux-erofs
Message-ID <[email protected]>
erofs_fc_get_tree() falls back to filp_open(fc->source, ...) itself
when get_tree_bdev_flags() reports the source isn't a block device.
That is the only way to reach erofs's file-backed mount support today,
which means an in-kernel caller that already opened and validated (e.g.
fsverity-checked) that exact file has no way to make erofs mount it: it
can only hand over the path again and trust erofs's independent lookup
to land on the same file, which it has no way to guarantee.

Check fc->source_file first and use it directly when set, taking a
reference and skipping the independent filp_open(fc->source) entirely
(fc->source is unset in this case). This has no effect on the existing
path-based fsconfig()/mount(2) flow, since userspace has no way to set
fc->source_file (see the previous patch); it only enables in-kernel
callers such as the following init/do_mounts.c change.

Signed-off-by: Eric Curtin <[email protected]>
---
 fs/erofs/super.c | 19 +++++++++++++++++++
 1 file changed, 19 insertions(+)

diff --git a/fs/erofs/super.c b/fs/erofs/super.c
index 802add6652fd..b6cd4fde3ca2 100644
--- a/fs/erofs/super.c
+++ b/fs/erofs/super.c
@@ -799,6 +799,25 @@ static int erofs_fc_get_tree(struct fs_context *fc)
 	if (IS_ENABLED(CONFIG_EROFS_FS_ONDEMAND) && sbi->fsid)
 		return get_tree_nodev(fc, erofs_fc_fill_super);
 
+	/*
+	 * An in-kernel caller (see path_mount_file()) has handed us the
+	 * exact, already-open file to mount, rather than a path for us to
+	 * resolve ourselves: use it directly instead of independently
+	 * re-opening fc->source (which isn't set in this case), so that
+	 * whatever the caller validated about this file - e.g. an fsverity
+	 * digest - is guaranteed to still apply to what actually gets
+	 * mounted.
+	 */
+	if (fc->source_file) {
+		if (!IS_ENABLED(CONFIG_EROFS_FS_BACKED_BY_FILE))
+			return invalf(fc, "File-backed mounts not supported");
+		if (!S_ISREG(file_inode(fc->source_file)->i_mode) ||
+		    !fc->source_file->f_mapping->a_ops->read_folio)
+			return invalf(fc, "Source file is not a plain, read-foliable file");
+		sbi->dif0.file = get_file(fc->source_file);
+		return get_tree_nodev(fc, erofs_fc_fill_super);
+	}
+
 	ret = get_tree_bdev_flags(fc, erofs_fc_fill_super,
 		IS_ENABLED(CONFIG_EROFS_FS_BACKED_BY_FILE) ?
 			GET_TREE_BDEV_QUIET_LOOKUP : 0);
-- 
2.43.0