adfs: reject disc records smaller than one filesystem block
PATCH 1/1 adfs: reject disc records smaller than one filesystem block
ADFS uses the on-disk disc size to report statfs block counts. The disc record validator checks the sector size, id length, high disc-size bits, map zone count, and reserved bytes, but it accepts a declared disc size smaller than one filesystem block.
A crafted one-zone image with log2secsize 9 and disc_size 1 can pass map checksum validation and mount. A subsequent statfs then reports zero f_blocks from adfs_map_statfs(), and adfs_statfs() divides by that zero while deriving f_ffree.
Reject disc records whose declared disc size is smaller than one filesystem block.
--- fs/adfs/super.c | 4 ++++ 1 file changed, 4 insertions(+)
fs/adfs/super.c +4 -0
@@ -73,6 +73,10 @@ static int adfs_checkdiscrecord(struct adfs_discrecord *dr)if (le32_to_cpu (dr->disc_size_high) >> dr->log2secsize)return 1 ; +/* disc size must contain at least one filesystem block */ +if (adfs_disc_size (dr) < (1ULL << dr->log2secsize)) +return 1 ; +/* * Maximum idlen is limited to 16 bits for new directories by * the three-byte storage of an indirect disc address. For
Signature · 1 line
2.43.0
Quoted 5 lines · Samuel Moelius
ADFS uses the on-disk disc size to report statfs block counts. The disc record validator checks the sector size, id length, high disc-size bits, map zone count, and reserved bytes, but it accepts a declared disc size smaller than one filesystem block.
Quoted 7 lines
A crafted one-zone image with log2secsize 9 and
disc_size1 can pass map checksum validation and mount. A subsequent statfs then reports zerof_blocksfromadfs_map_statfs(), andadfs_statfs()divides by that zero while derivingf_ffree.Reject disc records whose declared disc size is smaller than one filesystem block.
Can you create a tools/testing/selftests/ script that will generate a good and bad image and attempt to mount both, validating the filesystem checking logic you're adding here?
-Kees
Quoted 25 lines
Assisted-by Codex:gpt-5.5-cyber-previewSigned-off-by Samuel Moelius <[email protected]>--- fs/adfs/super.c | 4 ++++ 1 file changed, 4 insertions(+)fs/adfs/super.c +4
-0@@ -73,6 +73,10 @@ static int adfs_checkdiscrecord(struct adfs_discrecord *dr)if (le32_to_cpu (dr->disc_size_high) >> dr->log2secsize)return 1 ; +/* disc size must contain at least one filesystem block */ +if (adfs_disc_size (dr) < (1ULL << dr->log2secsize)) +return 1 ; +/* * Maximum idlen is limited to 16 bits for new directories by * the three-byte storage of an indirect disc address. For Signature · 1 line
2.43.0
Signature · 1 line
Kees Cook
v2 cover adfs: reject disc records smaller than one filesystem block
Hi,
This v2 splits the ADFS fix and the requested selftest into separate patches.
Patch 1 rejects ADFS disc records whose declared disc size is smaller than one filesystem block. Such an image can otherwise mount and later hit a divide-by-zero in statfs because f_blocks is zero.
Patch 2 adds a filesystems/adfs selftest that generates two minimal ADFS
images: a valid image with a one-block disc size, and an otherwise identical invalid image whose disc size is smaller than one filesystem block. The test verifies that the valid image mounts and statfs succeeds, and that the invalid image is rejected at mount time.
Changes since v1:
- Split the selftest into a separate patch.
- Added a tools/testing/selftests/filesystems/adfs test target.
- Added generated-good-image and generated-bad-image mount validation.
Thanks,
Sam
Samuel Moelius (2):
adfs: reject disc records smaller than one filesystem block
selftests: filesystems: add ADFS mount validation test
fs/adfs/super.c | 4 + tools/testing/selftests/Makefile | 1 + .../selftests/filesystems/adfs/Makefile | 5 + .../filesystems/adfs/adfs_mount_check.sh | 113 ++++++++++++++++++ .../testing/selftests/filesystems/adfs/config | 2 + 5 files changed, 125 insertions(+) create mode 100644 tools/testing/selftests/filesystems/adfs/Makefile create mode 100755 tools/testing/selftests/filesystems/adfs/adfs_mount_check.sh create mode 100644 tools/testing/selftests/filesystems/adfs/config
Signature · 1 line
2.43.0
PATCH v2 1/2 adfs: reject disc records smaller than one filesystem block
ADFS uses the on-disk disc size to report statfs block counts. The disc record validator checks the sector size, id length, high disc-size bits, map zone count, and reserved bytes, but it accepts a declared disc size smaller than one filesystem block.
A crafted one-zone image with log2secsize 9 and disc_size 1 can pass map checksum validation and mount. A subsequent statfs then reports zero f_blocks from adfs_map_statfs(), and adfs_statfs() divides by that zero while deriving f_ffree.
Reject disc records whose declared disc size is smaller than one filesystem block.
--- fs/adfs/super.c | 4 ++++ 1 file changed, 4 insertions(+)
fs/adfs/super.c +4 -0
@@ -73,6 +73,10 @@ static int adfs_checkdiscrecord(struct adfs_discrecord *dr)if (le32_to_cpu (dr->disc_size_high) >> dr->log2secsize)return 1 ; +/* disc size must contain at least one filesystem block */ +if (adfs_disc_size (dr) < (1ULL << dr->log2secsize)) +return 1 ; +/* * Maximum idlen is limited to 16 bits for new directories by * the three-byte storage of an indirect disc address. For
Signature · 1 line
2.43.0
Quoted 9 lines · Samuel Moelius
ADFS uses the on-disk disc size to report statfs block counts. The disc record validator checks the sector size, id length, high disc-size bits, map zone count, and reserved bytes, but it accepts a declared disc size smaller than one filesystem block.
A crafted one-zone image with log2secsize 9 and
disc_size1 can pass map checksum validation and mount. A subsequent statfs then reports zerof_blocksfromadfs_map_statfs(), andadfs_statfs()divides by that zero while derivingf_ffree.
I think this is still too low.
1. The disc map is nzones sectors long, and can not be in the same sector as the boot block. The disc record can not share with the map. This means the minimum is 1 + nzones sectors to fit just the map in.
2. If the disc record was found at 0xc00 rather than 0, then that will add sectors to the minimum size.
3. The root directory is also necessary, which is 2048 bytes for an E/F format (format_version=0) or root_size for an F+ format.
So, if we're really trying to avoid mounting something that isn't a proper image, then just checking that the disc size is at least one sector isn't sufficient.
Signature · 2 lines
RMK's Patch system: https://www.armlinux.org.uk/developer/patches/ FTTP is here! 80Mbps down 10Mbps up. Decent connectivity at last!
Quoted 21 lines · Russell King
Quoted 9 lines · Samuel Moelius
ADFS uses the on-disk disc size to report statfs block counts. The disc record validator checks the sector size, id length, high disc-size bits, map zone count, and reserved bytes, but it accepts a declared disc size smaller than one filesystem block.
A crafted one-zone image with log2secsize 9 and
disc_size1 can pass map checksum validation and mount. A subsequent statfs then reports zerof_blocksfromadfs_map_statfs(), andadfs_statfs()divides by that zero while derivingf_ffree.I think this is still too low.
1. The disc map is nzones sectors long, and can not be in the same sector as the boot block. The disc record can not share with the map. This means the minimum is 1 + nzones sectors to fit just the map in.
2. If the disc record was found at
0xc00rather than 0, then that will add sectors to the minimum size.
Quoted 6 lines
3. The root directory is also necessary, which is 2048 bytes for an E/F format (format_version=0) or
root_sizefor an F+ format.So, if we're really trying to avoid mounting something that isn't a proper image, then just checking that the disc size is at least one sector isn't sufficient.
I understand your points, but the intent of this patch was simply to eliminate a divide-by-zero. Is it sufficient for that purpose?
Quoted 25 lines · Samuel Moelius
Quoted 24 lines · Russell King
Quoted 9 lines · Samuel Moelius
ADFS uses the on-disk disc size to report statfs block counts. The disc record validator checks the sector size, id length, high disc-size bits, map zone count, and reserved bytes, but it accepts a declared disc size smaller than one filesystem block.
A crafted one-zone image with log2secsize 9 and
disc_size1 can pass map checksum validation and mount. A subsequent statfs then reports zerof_blocksfromadfs_map_statfs(), andadfs_statfs()divides by that zero while derivingf_ffree.I think this is still too low.
1. The disc map is nzones sectors long, and can not be in the same sector as the boot block. The disc record can not share with the map. This means the minimum is 1 + nzones sectors to fit just the map in.
2. If the disc record was found at
0xc00rather than 0, then that will add sectors to the minimum size.3. The root directory is also necessary, which is 2048 bytes for an E/F format (format_version=0) or
root_sizefor an F+ format.
Quoted 6 lines
Quoted 3 lines
So, if we're really trying to avoid mounting something that isn't a proper image, then just checking that the disc size is at least one sector isn't sufficient.
I understand your points, but the intent of this patch was simply to eliminate a divide-by-zero. Is it sufficient for that purpose?
While it is, you are also introducing selftests that rely on your behaviour, where you merge the disc record with the map for both of your test cases. This should also be rejected, because the bytes in the disc record will be interpreted as the disc map.
Have you checked what happens if you attempt to access such a bad image, or are you purely focussed on what an AI bot told you was a problem, rather than looking at the bigger picture?
Signature · 2 lines
RMK's Patch system: https://www.armlinux.org.uk/developer/patches/ FTTP is here! 80Mbps down 10Mbps up. Decent connectivity at last!
Quoted 34 lines · Russell King
Quoted 25 lines · Samuel Moelius
Quoted 24 lines · Russell King
Quoted 9 lines · Samuel Moelius
ADFS uses the on-disk disc size to report statfs block counts. The disc record validator checks the sector size, id length, high disc-size bits, map zone count, and reserved bytes, but it accepts a declared disc size smaller than one filesystem block.
A crafted one-zone image with log2secsize 9 and
disc_size1 can pass map checksum validation and mount. A subsequent statfs then reports zerof_blocksfromadfs_map_statfs(), andadfs_statfs()divides by that zero while derivingf_ffree.I think this is still too low.
1. The disc map is nzones sectors long, and can not be in the same sector as the boot block. The disc record can not share with the map. This means the minimum is 1 + nzones sectors to fit just the map in.
2. If the disc record was found at
0xc00rather than 0, then that will add sectors to the minimum size.3. The root directory is also necessary, which is 2048 bytes for an E/F format (format_version=0) or
root_sizefor an F+ format.Quoted 6 lines
Quoted 3 lines
So, if we're really trying to avoid mounting something that isn't a proper image, then just checking that the disc size is at least one sector isn't sufficient.
I understand your points, but the intent of this patch was simply to eliminate a divide-by-zero. Is it sufficient for that purpose?
Quoted 8 lines
While it is, you are also introducing selftests that rely on your behaviour, where you merge the disc record with the map for both of your test cases. This should also be rejected, because the bytes in the disc record will be interpreted as the disc map.
Have you checked what happens if you attempt to access such a bad image, or are you purely focussed on what an AI bot told you was a problem, rather than looking at the bigger picture?
I have not attempted to access the bad image. And to be clear, the selftest was an attempt to address Kees Cook's request here:
https://lore.kernel.org/all/202606101323.0DFB06B054@keescook/
Quoted 3 lines
Can you create a
tools/testing/selftests/script that will generate a good and bad image and attempt to mount both, validating the filesystem checking logic you're adding here?
Would another approach be better?
Quoted 48 lines · Samuel Moelius
Quoted 34 lines · Russell King
Quoted 25 lines · Samuel Moelius
Quoted 24 lines · Russell King
Quoted 9 lines · Samuel Moelius
ADFS uses the on-disk disc size to report statfs block counts. The disc record validator checks the sector size, id length, high disc-size bits, map zone count, and reserved bytes, but it accepts a declared disc size smaller than one filesystem block.
A crafted one-zone image with log2secsize 9 and
disc_size1 can pass map checksum validation and mount. A subsequent statfs then reports zerof_blocksfromadfs_map_statfs(), andadfs_statfs()divides by that zero while derivingf_ffree.I think this is still too low.
1. The disc map is nzones sectors long, and can not be in the same sector as the boot block. The disc record can not share with the map. This means the minimum is 1 + nzones sectors to fit just the map in.
2. If the disc record was found at
0xc00rather than 0, then that will add sectors to the minimum size.3. The root directory is also necessary, which is 2048 bytes for an E/F format (format_version=0) or
root_sizefor an F+ format.Quoted 6 lines
Quoted 3 lines
So, if we're really trying to avoid mounting something that isn't a proper image, then just checking that the disc size is at least one sector isn't sufficient.
I understand your points, but the intent of this patch was simply to eliminate a divide-by-zero. Is it sufficient for that purpose?
Quoted 8 lines
While it is, you are also introducing selftests that rely on your behaviour, where you merge the disc record with the map for both of your test cases. This should also be rejected, because the bytes in the disc record will be interpreted as the disc map.
Have you checked what happens if you attempt to access such a bad image, or are you purely focussed on what an AI bot told you was a problem, rather than looking at the bigger picture?
I have not attempted to access the bad image. And to be clear, the selftest was an attempt to address Kees Cook's request here:
https://lore.kernel.org/all/202606101323.0DFB06B054@keescook/
Quoted 5 lines
Quoted 3 lines
Can you create a
tools/testing/selftests/script that will generate a good and bad image and attempt to mount both, validating the filesystem checking logic you're adding here?Would another approach be better?
So, in essence,
- a bug was found (divide by zero) caused by an invalid image, presumably by AI.
- a patch was generated to address _just_ that issue without looking at the bigger picture.
- Kees requested a selftest for it
- A selftest was created which _itself_ generates two filesystem images, both of which are technically invalid, but tests that one mounts but the other doesn't, and the kernel doesn't have the divide by zero.
- No check whether the mounted "good" filesystem doesn't cause other issues.
- Author resists suggestions from filesystem maintainer to improve the check.
Fixing the bigger picture will result in the selftest failing, because the invalid "good" image is still invalid, and that makes patch 2 incorrect.
Sorry, but no.
While I'm here, another thing that should be checked - that the disc size specified in the disc record isn't larger than the block device being mounted.
Signature · 2 lines
RMK's Patch system: https://www.armlinux.org.uk/developer/patches/ FTTP is here! 80Mbps down 10Mbps up. Decent connectivity at last!
PATCH v2 2/2 selftests: filesystems: add ADFS mount validation test
In reply to Samuel MoeliusAdd a selftest for ADFS disc record validation. The test generates a minimal one-zone ADFS image whose declared disc size is exactly one filesystem block, mounts it read-only, and verifies that statfs succeeds.
It also generates an otherwise-identical image whose declared disc size is smaller than one filesystem block, and verifies that the kernel rejects the mount. This covers the malformed image rejected by the preceding ADFS fix without deliberately triggering the old statfs divide-by-zero.
--- tools/testing/selftests/Makefile | 1 + .../selftests/filesystems/adfs/Makefile | 5 + .../filesystems/adfs/adfs_mount_check.sh | 113 ++++++++++++++++++ .../testing/selftests/filesystems/adfs/config | 2 + 4 files changed, 121 insertions(+) create mode 100644 tools/testing/selftests/filesystems/adfs/Makefile create mode 100755 tools/testing/selftests/filesystems/adfs/adfs_mount_check.sh create mode 100644 tools/testing/selftests/filesystems/adfs/config
tools/testing/selftests/Makefile +1 -0
@@ -31,6 +31,7 @@ TARGETS += efivarfs TARGETS += exec TARGETS += fchmodat2 TARGETS += filesystems +TARGETS += filesystems/adfs TARGETS += filesystems/binderfs TARGETS += filesystems/epoll TARGETS += filesystems/fat
tools/testing/selftests/filesystems/adfs/Makefile new file +5 -0
@@ -0,0 +1,5 @@ +# SPDX-License-Identifier: GPL-2.0 + +TEST_PROGS := adfs_mount_check.sh + +include ../../lib.mk
tools/testing/selftests/filesystems/adfs/adfs_mount_check.sh new file +113 -0
113 lines not on this page · raw message
tools/testing/selftests/filesystems/adfs/config new file +2 -0
@@ -0,0 +1,2 @@ +CONFIG_ADFS_FS=m +CONFIG_BLK_DEV_LOOP=m
Signature · 1 line
2.43.0