Skip to contentSkip to conversation

btrfs: don't clobber ret in btrfs_validate_super()

PATCH In review Mark Harmstone 5 participants 6 messages Mar 11, 2025 – Mar 12, 2025 Btrfs file system

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 btrfs: don't clobber ret in btrfs_validate_super()

Commit 2a9bb78cfd36 introduces a call to validate_sys_chunk_array() in btrfs_validate_super(), which clobbers the value of ret set earlier. This has the effect of negating the validity checks done earlier, making it so btrfs could potentially try to mount invalid filesystems.

Cc Qu Wenruo <[email protected]>
Fixes 2a9bb78cfd36 ("btrfs: validate system chunk array at btrfs_validate_super()")
---
 fs/btrfs/disk-io.c | 3 +++
 1 file changed, 3 insertions(+)
fs/btrfs/disk-io.c +3 -0
index 0afd3c0f2fab..4421c946a53c 100644
@@ -2562,6 +2562,9 @@ int btrfs_validate_super(const struct btrfs_fs_info *fs_info,
 		ret = -EINVAL;
 	}
 
+	if (ret)
+		return ret;
+
 	ret = validate_sys_chunk_array(fs_info, sb);
 
 	/*
Signature · 1 line
2.45.3
Reviewed-by on v1 1/1
#1 lore raw
Quoted 18 lines · Mark Harmstone

Commit 2a9bb78cfd36 introduces a call to validate_sys_chunk_array() in btrfs_validate_super(), which clobbers the value of ret set earlier. This has the effect of negating the validity checks done earlier, making it so btrfs could potentially try to mount invalid filesystems.

Cc Qu Wenruo <[email protected]>
Fixes 2a9bb78cfd36 ("btrfs: validate system chunk array at btrfs_validate_super()")
---
 fs/btrfs/disk-io.c | 3 +++
 1 file changed, 3 insertions(+)
fs/btrfs/disk-io.c +0 -0
index 0afd3c0f2fab..4421c946a53c 100644

fs/btrfs/disk-io.c int btrfs_validate_super(const struct btrfs_fs_info *fs_info, · line 2568

Quoted 7 lines
fs/btrfs/disk-io.c +3 -0
@@ -2562,6 +2562,9 @@ int btrfs_validate_super(const struct btrfs_fs_info *fs_info,
                ret = -EINVAL;
        }
 
+       if (ret)
+               return ret;
+
        ret = validate_sys_chunk_array(fs_info, sb);

While this fixes the problem, the function is structured in a way that is easy to get into this sort of issue.
Rather than set 'ret' to -EINVAL every time some check fails and then continue, I'd rather have it return -EINVAL immediately.

Anyway, this fixes a bug, so:

Thanks.

Quoted 6 lines
fs/btrfs/disk-io.c +0 -1
        /*
--

2.45.3

#2 lore raw
Quoted 33 lines · Filipe Manana
Quoted 1 line
Quoted 18 lines · Mark Harmstone

Commit 2a9bb78cfd36 introduces a call to validate_sys_chunk_array() in btrfs_validate_super(), which clobbers the value of ret set earlier. This has the effect of negating the validity checks done earlier, making it so btrfs could potentially try to mount invalid filesystems.

Cc Qu Wenruo <[email protected]>
Fixes 2a9bb78cfd36 ("btrfs: validate system chunk array at btrfs_validate_super()")
---
  fs/btrfs/disk-io.c | 3 +++
  1 file changed, 3 insertions(+)
fs/btrfs/disk-io.c +0 -0
index 0afd3c0f2fab..4421c946a53c 100644

fs/btrfs/disk-io.c int btrfs_validate_super(const struct btrfs_fs_info *fs_info, · line 2568

Quoted 7 lines
fs/btrfs/disk-io.c +3 -0
@@ -2562,6 +2562,9 @@ int btrfs_validate_super(const struct btrfs_fs_info *fs_info,
                 ret = -EINVAL;
         }
 
+       if (ret)
+               return ret;
+
         ret = validate_sys_chunk_array(fs_info, sb);

While this fixes the problem, the function is structured in a way that is easy to get into this sort of issue.
Rather than set 'ret' to -EINVAL every time some check fails and then continue, I'd rather have it return -EINVAL immediately.

Quoted 3 lines

Anyway, this fixes a bug, so:

Thanks. Yes, I did think that, but if there's multiple reasons why your FS won't mount you probably want to know all of them.

Quoted 9 lines

Thanks.

Quoted 6 lines
fs/btrfs/disk-io.c +0 -1
         /*
--

2.45.3

Reviewed-by on v1 1/1
#3 lore raw
In reply to Mark Harmstone
Quoted 8 lines · Mark Harmstone

Commit 2a9bb78cfd36 introduces a call to validate_sys_chunk_array() in btrfs_validate_super(), which clobbers the value of ret set earlier. This has the effect of negating the validity checks done earlier, making it so btrfs could potentially try to mount invalid filesystems.

Cc Qu Wenruo <[email protected]>
Fixes 2a9bb78cfd36 ("btrfs: validate system chunk array at btrfs_validate_super()")

Thanks for catching this.

Although I agree with Filipe that it's better to return the error immediately in the future.

Yes, currently it will report all the errors, but if the first check against magic number fails, the super block can just be garbage and all the remaining checks will be triggered.

Other than a single "no valid fs found" message, we got all kinds of errors, which is not that easy for end users to understand.

Thanks,
Qu

Quoted 18 lines
---
  fs/btrfs/disk-io.c | 3 +++
  1 file changed, 3 insertions(+)
fs/btrfs/disk-io.c +3 -0
index 0afd3c0f2fab..4421c946a53c 100644
@@ -2562,6 +2562,9 @@ int btrfs_validate_super(const struct btrfs_fs_info *fs_info,
  		ret = -EINVAL;
  	}
 
+	if (ret)
+		return ret;
+
  	ret = validate_sys_chunk_array(fs_info, sb);
 
  	/*
#4 lore raw
Quoted 14 lines · Qu Wenruo
Quoted 8 lines · Mark Harmstone

Commit 2a9bb78cfd36 introduces a call to validate_sys_chunk_array() in btrfs_validate_super(), which clobbers the value of ret set earlier. This has the effect of negating the validity checks done earlier, making it so btrfs could potentially try to mount invalid filesystems.

Cc Qu Wenruo <[email protected]>
Fixes 2a9bb78cfd36 ("btrfs: validate system chunk array at btrfs_validate_super()")
Quoted 8 lines

Thanks for catching this.

Although I agree with Filipe that it's better to return the error immediately in the future.

Yes, currently it will report all the errors, but if the first check against magic number fails, the super block can just be garbage and all the remaining checks will be triggered.

We can return immediately when the magic does not match, it's more likely that the sb would be garbage so the following checks will fail and be just noise.

However if the magic is correct then I find it better to print which errors were found, they get all logged to the syslog and it can help analyzing the problem.

#5 lore raw
In reply to Mark Harmstone
Quoted 1 line · Mark Harmstone

Commit 2a9bb78cfd36 introduces a call to validate_sys_chunk_array() in

Please use full commit references in changelogs, like

2a9bb78cfd36 ("btrfs: validate system chunk array at btrfs_validate_super()")

there's a commonly used git alias:

[alias]

	one = show -s --pretty='format:%h (\"%s\")
Quoted 16 lines

btrfs_validate_super(), which clobbers the value of ret set earlier. This has the effect of negating the validity checks done earlier, making it so btrfs could potentially try to mount invalid filesystems.

Cc Qu Wenruo <[email protected]>
Fixes 2a9bb78cfd36 ("btrfs: validate system chunk array at btrfs_validate_super()")
---
 fs/btrfs/disk-io.c | 3 +++
 1 file changed, 3 insertions(+)
fs/btrfs/disk-io.c +0 -0
index 0afd3c0f2fab..4421c946a53c 100644

fs/btrfs/disk-io.c int btrfs_validate_super(const struct btrfs_fs_info *fs_info, · line 2568

Quoted 7 lines
fs/btrfs/disk-io.c +3 -0
@@ -2562,6 +2562,9 @@ int btrfs_validate_super(const struct btrfs_fs_info *fs_info,
 		ret = -EINVAL;
 	}
 
+	if (ret)
+		return ret;
+
 	ret = validate_sys_chunk_array(fs_info, sb);

This is ok as a fix for the return value cloberring.

Tips for followup cleanups:

Right after the call to validate_sys_chunk_array() there are two checks that duplicate the sys_array validation (maximum and minimum length). So they can be removed from btrfs_validate_super(). Then there are two more checks of generation of chunk tree and "cache_generation". Both can be moved in front of the sys_array validation as they're simple like the other checks.

As discussed in the thread, the magic check can return immediately as its mismatch will likely trigger several other checks and would not bring much help.