Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ext2.py: ValueError: requested invalid -8 amount of bytes #662

Open
milahu opened this issue Apr 2, 2023 · 1 comment
Open

ext2.py: ValueError: requested invalid -8 amount of bytes #662

milahu opened this issue Apr 2, 2023 · 1 comment
Labels

Comments

@milahu
Copy link

milahu commented Apr 2, 2023

ext2 parser is broken

repro:

dd if=/dev/zero of=ext2.bin bs=1024 count=1000
mkfs.ext2 ext2.bin
mkdir mnt
sudo mount -o loop ext2.bin mnt/
echo hello world | sudo tee mnt/hello.txt
sudo umount mnt/

kaitai-struct-compiler -t python filesystem/ext2.ksy

python -c '
import ext2
e = ext2.Ext2.from_file("ext2.bin")
print(e.root_dir)
print([ n.name for n in e.root_dir.entries ])
'
  File "ext2.py", line 217, in _read
    self.entries.append(Ext2.DirEntry(self._io, self, self._root))
  File "ext2.py", line 120, in __init__
    self._read()
  File "ext2.py", line 128, in _read
    self.padding = self._io.read_bytes(((self.rec_len - self.name_len) - 8))
  File "kaitaistruct.py", line 298, in read_bytes
    raise ValueError(
ValueError: requested invalid -8 amount of bytes

fix?

--- a/filesystem/ext2.ksy
+++ b/filesystem/ext2.ksy
@@ -254,10 +254,14 @@ types:
         type: str
         encoding: UTF-8
       - id: padding
-        size: rec_len - name_len - 8
+        size: 'padding_size_raw > 0 ? padding_size_raw : 0'
     instances:
       inode:
         value: '_root.bg1.block_groups[(inode_ptr - 1) / _root.bg1.super_block.inodes_per_group].inodes[(inode_ptr - 1) % _root.bg1.super_block.inodes_per_group]'
+      # workaround: kaitai_struct_compiler has no max(a, b) function
+      # padding_size = max(0, (rec_len - name_len - 8))
+      padding_size_raw:
+        value: 'rec_len - name_len - 8'
     enums:
       # https://www.nongnu.org/ext2-doc/ext2.html#IFDIR-FILE-TYPE
       file_type_enum:

still looks broken, as i dont see the hello.txt file

>>> [ n.name for n in e.root_dir.entries ]
['', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '']

todo: use the array method max (docs)

padding_size = max(0, (rec_len - name_len - 8))

[0, (rec_len - name_len - 8)].max
@armijnhemel
Copy link
Collaborator

The ext2 parser is, in my opinion, only useful to parse the super block. There are many features (such as the sparse superblock ( https://www.nongnu.org/ext2-doc/ext2.html#def-superblock ) that are not properly supported.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

3 participants