Skip to contentSkip to conversation

erofs: verify metadata accesses for file-backed mounts

PATCH In review Gao Xiang 2 participants 3 messages Mar 30 – Apr 2 EROFS file system

lore
Share & tools
lore

Gao Xiang

author
#0 lore raw

PATCH 1/1 erofs: verify metadata accesses for file-backed mounts

For file-backed mounts, metadata is fetched via the page cache of backing inodes to avoid double caching and redundant copy ops, which is currently used by Android APEXes, ComposeFS and containerd for example. However, rw_verify_area() was missing prior to metadata accesses.

Similar to vfs_iocb_iter_read(), fix this by:
- Enabling fanotify pre-content hooks on metadata accesses;
- security_file_permission() for security modules.

Verified that fanotify pre-content hooks now works correctly.

Fixes fb176750266a ("erofs: add file-backed mount support")
---
 fs/erofs/data.c | 14 ++++++++++++++
 1 file changed, 14 insertions(+)
fs/erofs/data.c +14 -0
index f79ee80627d9..cf27b8fbaaa1 100644
@@ -30,6 +30,20 @@ void *erofs_bread(struct erofs_buf *buf, erofs_off_t offset, bool need_kmap)
 {
 	pgoff_t index = (buf->off + offset) >> PAGE_SHIFT;
 	struct folio *folio = NULL;
+	loff_t fpos;
+	int err;
+
+	/*
+	 * Metadata access for file-backed mounts reuses page cache of backing
+	 * fs inodes only folio data will be needed) to prevent double caching.
+	 * However, the data access range must be verified here in advance.
+	 */
+	if (buf->file) {
+		fpos = index << PAGE_SHIFT;
+		err = rw_verify_area(READ, buf->file, &fpos, PAGE_SIZE);
+		if (err)
+			return ERR_PTR(err);
+	}
 
 	if (buf->page) {
 		folio = page_folio(buf->page);
Signature · 1 line
2.43.5
author
#1 lore raw

PATCH 1/1 erofs: verify metadata accesses for file-backed mounts

For file-backed mounts, metadata is fetched via the page cache of backing inodes to avoid double caching and redundant copy ops just out of RO uptodate folios, which is useful for Android APEXes, ComposeFS, containerd and more for example. However, rw_verify_area() was missing prior to metadata accesses.

Similar to vfs_iocb_iter_read(), fix this by:
- Enabling fanotify pre-content hooks on metadata accesses;
- security_file_permission() for security modules.

Verified that fanotify pre-content hooks now works correctly.

Fixes fb176750266a ("erofs: add file-backed mount support")

---
v2:
- fix the code comment.

 fs/erofs/data.c | 14 ++++++++++++++
 1 file changed, 14 insertions(+)
fs/erofs/data.c +14 -0
index f79ee80627d9..687cf7e4ded0 100644
@@ -30,6 +30,20 @@ void *erofs_bread(struct erofs_buf *buf, erofs_off_t offset, bool need_kmap)
 {
 	pgoff_t index = (buf->off + offset) >> PAGE_SHIFT;
 	struct folio *folio = NULL;
+	loff_t fpos;
+	int err;
+
+	/*
+	 * Metadata access for file-backed mounts reuses page cache of backing
+	 * fs inodes (only folio data will be needed) to prevent double caching.
+	 * However, the data access range must be verified here in advance.
+	 */
+	if (buf->file) {
+		fpos = index << PAGE_SHIFT;
+		err = rw_verify_area(READ, buf->file, &fpos, PAGE_SIZE);
+		if (err)
+			return ERR_PTR(err);
+	}
 
 	if (buf->page) {
 		folio = page_folio(buf->page);
Signature · 1 line
2.43.5
reviewer Reviewed-by on v1 1/1
#2 lore raw
In reply to Gao Xiang
Quoted 9 lines · Gao Xiang

For file-backed mounts, metadata is fetched via the page cache of backing inodes to avoid double caching and redundant copy ops, which is currently used by Android APEXes, ComposeFS and containerd for example. However, rw_verify_area() was missing prior to metadata accesses.

Similar to vfs_iocb_iter_read(), fix this by:
- Enabling fanotify pre-content hooks on metadata accesses;
- security_file_permission() for security modules.

Quoted 6 lines

Verified that fanotify pre-content hooks now works correctly.

Fixes fb176750266a ("erofs: add file-backed mount support")

---

Thanks,