Skip to content

Commit 649cd4b

Browse files
authored
Merge pull request #454 from onekey-sec/453-fix-xz-handler
fix(xz): fix early return and EOFError on stream size calculation
2 parents 080eb14 + a64b6b3 commit 649cd4b

File tree

1 file changed

+10
-4
lines changed
  • unblob/handlers/compression

1 file changed

+10
-4
lines changed

unblob/handlers/compression/xz.py

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,9 @@ def get_stream_size(footer_offset: int, file: File) -> int:
9494
stored_backward_size = convert_int32(backward_bytes, Endian.LITTLE)
9595
real_backward_size = (stored_backward_size + 1) * 4
9696

97+
if real_backward_size > footer_offset - CRC32_LEN - BACKWARD_SIZE_LEN:
98+
raise InvalidInputFormat("Invalid backward size.")
99+
97100
# skip backwards to the end of the Index
98101
file.seek(-CRC32_LEN - BACKWARD_SIZE_LEN, io.SEEK_CUR)
99102

@@ -135,16 +138,19 @@ def get_stream_size(footer_offset: int, file: File) -> int:
135138
def _hyperscan_match(
136139
pattern_id: int, offset: int, end: int, flags: int, context: XZSearchContext
137140
) -> bool:
138-
139141
# if we matched before our start offset, continue looking
140142
end_offset = offset + FLAG_LEN + EOS_MAGIC_LEN
141143
if end_offset < context.start_offset:
142144
return False
143145

144-
stream_size = get_stream_size(offset, context.file)
146+
try:
147+
stream_size = get_stream_size(offset, context.file)
148+
except InvalidInputFormat:
149+
return False
145150

151+
# stream_size does not match, we continue our search
146152
if stream_size != (end_offset - context.start_offset):
147-
return True
153+
return False
148154

149155
# stream padding validation
150156
# padding MUST contain only null bytes and be 4 bytes aligned
@@ -153,7 +159,7 @@ def _hyperscan_match(
153159
padding_size = end_padding_offset - end_offset
154160
if padding_size % 4 != 0:
155161
context.end_streams_offset = end_offset
156-
return True
162+
return False
157163

158164
# next magic validation
159165
context.end_streams_offset = end_padding_offset

0 commit comments

Comments
 (0)