Skip to content

[Bug]: EXT4Reader.readFile drops holes: sparse files (e.g. from mke2fs -d) read back short and shifted #979

Open

Description

@kunalkushwaha

I have done the following

  • I have searched the existing issues
  • If possible, I've reproduced the issue using the 'main' branch of this project

Steps to reproduce

  1. Make an ext4 image with e2fsprogs from a directory holding a file with zero blocks in the middle (e2fsprogs stores them as holes):

    mkdir src
    python3 -c "import sys; sys.stdout.buffer.write(b'A'*4096 + b'\0'*12288 + b'B'*4096 + b'C'*100)" > src/sparse
    truncate -s 8M img.ext4
    mke2fs -q -F -t ext4 -d src img.ext4
  2. Read the file back with EXT4.EXT4Reader:

    let reader = try EXT4.EXT4Reader(blockDevice: FilePath("img.ext4"))
    let data = try reader.readFile(at: FilePath("/sparse"))
    print(data.count)   // 12288, expected 20580

Current behavior

readFile returns 12288 of 20580 bytes, and the bytes after the hole are shifted. getExtents keeps each extent's physical range but drops its logical block (ExtentLeaf.block). performRead and readFileBytesFromExtents then lay the extents out back to back, so holes vanish, and the tail past the summed extent length is cut off.

We hit this in practice. scripts/build-initfs.sh falls back to mkfs.ext4 -d when no loop mount is available, and the resulting initfs stores /sbin/vminitd sparse (a depth-1 extent tree with gaps). Reading /sbin/vminitd from it gave 92,725,248 of 92,916,784 bytes, and the extracted binary segfaulted on start. debugfs dump of the same file is byte-identical to the built binary.

Images written by EXT4.Formatter are not affected, because it writes zero blocks out rather than leaving holes.

Unwritten extents (ee_len > 32768) are also taken at face value. They should read as zeros.

Expected behavior

readFile returns the file's exact bytes, with holes and unwritten extents read as zeros.

A fix with tests (getFileExtents returns logical block numbers; both read paths zero-fill and place each extent at its logical offset; getExtents keeps its shape for the directory code). It was made against 0.47.0, cherry-picks cleanly onto main, and passes all 64 ContainerizationEXT4Tests on Linux: kunalkushwaha@e870949. Happy to open a PR.

Environment

- OS: Ubuntu 24.04.4 (x86_64), in the swift:6.3.3-noble image
- Xcode: n/a
- Swift: 6.3.3
- e2fsprogs 1.47.0
- containerization 0.47.0; the same code is on main (0cb4d5b)

Relevant log output

✘ Test readFileOfAnE2fsprogsSparseFile() recorded an issue: Expectation failed: (read.count → 12288) == (contents.count → 20580)

Code of Conduct

  • I agree to follow this project's Code of Conduct

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions