Skip to contentSkip to conversation

erofs: limit the level of fs stacking for file-backed mounts

PATCH Superseded Gao Xiang 4 participants 6 messages Nov 21, 2025 – Nov 24, 2025 EROFS file system

lore
Share & tools
lore

Gao Xiang

author
#0 lore raw

PATCH 1/1 erofs: limit the level of fs stacking for file-backed mounts

Otherwise, it could cause potential kernel stack overflow (e.g., EROFS mounting itself).

Fixes fb176750266a ("erofs: add file-backed mount support")
---
 fs/erofs/super.c | 16 ++++++++++++++++
 1 file changed, 16 insertions(+)
fs/erofs/super.c +16 -0
index f3f8d8c066e4..d408921d74d0 100644
@@ -639,6 +639,22 @@ static int erofs_fc_fill_super(struct super_block *sb, struct fs_context *fc)
 
 	sbi->blkszbits = PAGE_SHIFT;
 	if (!sb->s_bdev) {
+		/*
+		 * (File-backed mounts) EROFS claims it's safe to nest other
+		 * fs contexts (including its own) due to self-controlled RO
+		 * accesses/contexts and no side-effect changes that need to
+		 * context save & restore so it can reuse the current thread
+		 * context.  However, it still needs to bump `s_stack_depth` to
+		 * avoid kernel stack overflow from nested filesystems.
+		 */
+		if (erofs_is_fileio_mode(sbi)) {
+			sb->s_stack_depth =
+				file_inode(sbi->dif0.file)->i_sb->s_stack_depth + 1;
+			if (sb->s_stack_depth > FILESYSTEM_MAX_STACK_DEPTH) {
+				erofs_err(sb, "maximum fs stacking depth exceeded");
+				return -EINVAL;
+			}
+		}
 		sb->s_blocksize = PAGE_SIZE;
 		sb->s_blocksize_bits = PAGE_SHIFT;
 
Signature · 1 line
2.43.5
author
#2 lore raw

PATCH v2 1/1 erofs: limit the level of fs stacking for file-backed mounts

In reply to Gao Xiang

Otherwise, it could cause potential kernel stack overflow (e.g., EROFS mounting itself).

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

---
Change since v1:
- Return -ENOTBLK instead of -EINVAL since userspace tools like util-linux will fall back to using loop to mount again.

Don't use -ELOOP compared to other stacked fses, since -ENOTBLK is more suitable: it means the kernel can't handle it anymore.

 fs/erofs/super.c | 16 ++++++++++++++++
 1 file changed, 16 insertions(+)
fs/erofs/super.c +16 -0
index f3f8d8c066e4..2db534f76464 100644
@@ -639,6 +639,22 @@ static int erofs_fc_fill_super(struct super_block *sb, struct fs_context *fc)
 
 	sbi->blkszbits = PAGE_SHIFT;
 	if (!sb->s_bdev) {
+		/*
+		 * (File-backed mounts) EROFS claims it's safe to nest other
+		 * fs contexts (including its own) due to self-controlled RO
+		 * accesses/contexts and no side-effect changes that need to
+		 * context save & restore so it can reuse the current thread
+		 * context.  However, it still needs to bump `s_stack_depth` to
+		 * avoid kernel stack overflow from nested filesystems.
+		 */
+		if (erofs_is_fileio_mode(sbi)) {
+			sb->s_stack_depth =
+				file_inode(sbi->dif0.file)->i_sb->s_stack_depth + 1;
+			if (sb->s_stack_depth > FILESYSTEM_MAX_STACK_DEPTH) {
+				erofs_err(sb, "maximum fs stacking depth exceeded");
+				return -ENOTBLK;
+			}
+		}
 		sb->s_blocksize = PAGE_SIZE;
 		sb->s_blocksize_bits = PAGE_SHIFT;
 
Signature · 1 line
2.43.5
Reviewed-by on v2 1/1
#4 lore raw
In reply to Gao Xiang

Hi Xiang,

Quoted 28 lines · Gao Xiang

Otherwise, it could cause potential kernel stack overflow (e.g., EROFS mounting itself).

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

---
Change since v1:
- Return -ENOTBLK instead of -EINVAL since userspace tools like util-linux will fall back to using loop to mount again.

Don't use -ELOOP compared to other stacked fses, since -ENOTBLK is more suitable: it means the kernel can't handle it anymore.

  fs/erofs/super.c | 16 ++++++++++++++++
  1 file changed, 16 insertions(+)
fs/erofs/super.c +3 -0
index f3f8d8c066e4..2db534f76464 100644
@@ -639,6 +639,22 @@ static int erofs_fc_fill_super(struct super_block *sb, struct fs_context *fc)
 
  	sbi->blkszbits = PAGE_SHIFT;
  	if (!sb->s_bdev) {
+		/*
+		 * (File-backed mounts) EROFS claims it's safe to nest other
+		 * fs contexts (including its own) due to self-controlled RO

fs/erofs/super.c static int erofs_fc_fill_super(struct super_block *sb, struct fs_context *fc) · line 654

Quoted 10 lines
fs/erofs/super.c +10 -0
+		 * accesses/contexts and no side-effect changes that need to
+		 * context save & restore so it can reuse the current thread
+		 * context.  However, it still needs to bump `s_stack_depth` to
+		 * avoid kernel stack overflow from nested filesystems.
+		 */
+		if (erofs_is_fileio_mode(sbi)) {
+			sb->s_stack_depth =
+				file_inode(sbi->dif0.file)->i_sb->s_stack_depth + 1;
+			if (sb->s_stack_depth > FILESYSTEM_MAX_STACK_DEPTH) {
+				erofs_err(sb, "maximum fs stacking depth exceeded");

Since it will success once the max stack depth is exceeded, a warning would be better? Otherwise it looks good me.

Thanks,
Hongbo

Quoted 6 lines
fs/erofs/super.c +3 -0
+				return -ENOTBLK;
+			}
+		}
  		sb->s_blocksize = PAGE_SIZE;
  		sb->s_blocksize_bits = PAGE_SHIFT;
 
author
#5 lore raw
Quoted 34 lines · Hongbo Li

Hi Xiang,

Quoted 31 lines · Gao Xiang

Otherwise, it could cause potential kernel stack overflow (e.g., EROFS mounting itself).

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

---
Change since v1:
  - Return -ENOTBLK instead of -EINVAL since userspace tools like util-linux will fall back to using loop to mount again.

    Don't use -ELOOP compared to other stacked fses, since -ENOTBLK is more suitable: it means the kernel can't handle it anymore.

  fs/erofs/super.c | 16 ++++++++++++++++

  1 file changed, 16 insertions(+)
fs/erofs/super.c +7 -0
index f3f8d8c066e4..2db534f76464 100644
@@ -639,6 +639,22 @@ static int erofs_fc_fill_super(struct super_block *sb, struct fs_context *fc)
       sbi->blkszbits = PAGE_SHIFT;
       if (!sb->s_bdev) {
+        /*
+         * (File-backed mounts) EROFS claims it's safe to nest other
+         * fs contexts (including its own) due to self-controlled RO
+         * accesses/contexts and no side-effect changes that need to
+         * context save & restore so it can reuse the current thread
+         * context.  However, it still needs to bump `s_stack_depth` to
+         * avoid kernel stack overflow from nested filesystems.
Quoted 8 lines

fs/erofs/super.c static int erofs_fc_fill_super(struct super_block *sb, struct fs_context *fc) · line 654

Quoted 6 lines
fs/erofs/super.c +6 -0
+         */
+        if (erofs_is_fileio_mode(sbi)) {
+            sb->s_stack_depth =
+                file_inode(sbi->dif0.file)->i_sb->s_stack_depth + 1;
+            if (sb->s_stack_depth > FILESYSTEM_MAX_STACK_DEPTH) {
+                erofs_err(sb, "maximum fs stacking depth exceeded");

Since it will success once the max stack depth is exceeded, a warning would be better? Otherwise it looks good me.

But that is not a kernel fallback, and the kernel mount already fails, I think erroring out is more proper.

Thanks,
Gao Xiang

Quoted 11 lines

Thanks,
Hongbo

Quoted 5 lines
fs/erofs/super.c +3 -0
+                return -ENOTBLK;
+            }
+        }
           sb->s_blocksize = PAGE_SIZE;
           sb->s_blocksize_bits = PAGE_SHIFT;