onekey-sec / ubi_reader

Collection of Python scripts for reading information about and extracting data from UBI and UBIFS images.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

error when extracting a ubi file in blk.data_crc() function from ubireader

arunmagesh opened this issue · comments

There appears to be a bug in the ubireader which checks for crc information in the header and fails to catch if it doesn't recognise it properly.

To Reproduce
Steps to reproduce the behavior:

  1. Launch unblob with command unblob file.ubi
  2. See error

Detail
Error from unblob

code=0x1 pid=22412 severity=<Severity.WARNING: 'WARNING'> stderr=Traceback (most recent call last):
  File "/home/arunmag_001/.local/bin/ubireader_extract_images", line 143, in <module>
    ubi_obj = ubi(ufile_obj)
  File "/home/arunmag_001/.local/lib/python3.10/site-packages/ubireader/ubi/__init__.py", line 146, in __init__
    super(ubi, self).__init__(ubi_file)
  File "/home/arunmag_001/.local/lib/python3.10/site-packages/ubireader/ubi/__init__.py", line 46, in __init__
    self._blocks = extract_blocks(self)
  File "/home/arunmag_001/.local/lib/python3.10/site-packages/ubireader/ubi/block/__init__.py", line 131, in extract_blocks
    blk.data_crc = (~crc32(buf[blk.ec_hdr.data_offset:blk.ec_hdr.data_offset+blk.vid_hdr.data_size]) & UBI_CRC32_INIT)
AttributeError: 'NoneType' object has no attribute 'data_size'

Same error from ubireader as well.

 ubireader_display_info 20000.ubi
Traceback (most recent call last):
  File "/home/arunmag_001/.local/bin/ubireader_display_info", line 132, in <module>
    ubi_obj = ubi(ufile_obj)
  File "/home/arunmag_001/.local/lib/python3.10/site-packages/ubireader/ubi/__init__.py", line 146, in __init__
    super(ubi, self).__init__(ubi_file)
  File "/home/arunmag_001/.local/lib/python3.10/site-packages/ubireader/ubi/__init__.py", line 46, in __init__
    self._blocks = extract_blocks(self)
  File "/home/arunmag_001/.local/lib/python3.10/site-packages/ubireader/ubi/block/__init__.py", line 131, in extract_blocks
    blk.data_crc = (~crc32(buf[blk.ec_hdr.data_offset:blk.ec_hdr.data_offset+blk.vid_hdr.data_size]) & UBI_CRC32_INIT)
AttributeError: 'NoneType' object has no attribute 'data_size'

Expected behavior
The script crash and fails to extract.

Environment information :
Linux arunmag 5.15.90.1-microsoft-standard-WSL2 onekey-sec/unblob#1 SMP Fri Jan 27 02:56:13 UTC 2023 x86_64 x86_64 x86_64 GNU/Linux
"Ubuntu 22.04.1 LTS"
"unblob==23.4.17"

83886080-84803584.zip

The bug stems from this line: https://github.com/jrspruitt/ubi_reader/blob/master/ubireader/ubi/block/__init__.py#L65

When a block header error is encountered, the vid_hdr value of the block is not set which leads to the exception you're seeing.

A fix to have a better error message would be:

diff --git a/ubireader/ubi/block/__init__.py b/ubireader/ubi/block/__init__.py
index ce80f96..ae66e35 100755
--- a/ubireader/ubi/block/__init__.py
+++ b/ubireader/ubi/block/__init__.py
@@ -62,7 +62,7 @@ class description(object):
         # TODO better understanding of block types/errors
         self.ec_hdr = ec_hdr(block_buf[0:UBI_EC_HDR_SZ])
 
-        if not self.ec_hdr.errors or settings.ignore_block_header_errors:
+        if not self.ec_hdr.errors or settings.ignore_block_header_errors or settings.warn_only_block_read_errors:
             self.vid_hdr = vid_hdr(block_buf[self.ec_hdr.vid_hdr_offset:self.ec_hdr.vid_hdr_offset+UBI_VID_HDR_SZ])
 
             if not self.vid_hdr.errors or settings.ignore_block_header_errors:
@@ -72,7 +72,8 @@ class description(object):
                     self.vtbl_recs = vtbl_recs(block_buf[self.ec_hdr.data_offset:])
 
                 self.leb_num = self.vid_hdr.lnum
-
+        else:
+            raise Exception("ECC error in block header. Use either --warn-only-block-read-errors or --ignore-block-header-errors.")
         self.is_vtbl = bool(self.vtbl_recs) or False
         self.is_valid = not self.ec_hdr.errors and not self.vid_hdr.errors or settings.ignore_block_header_errors

If you do apply that fix, or use ubireader with --ignore-block-header-errors you'll get this with your sample:

UBI Fatal: Less than 2 layout blocks found.