From 74be2e202fd8c503eb00a870519fdf5c14b39fb7 Mon Sep 17 00:00:00 2001 From: Philipp Maier Date: Tue, 4 Mar 2025 12:32:51 +0100 Subject: [PATCH] filesystem: do not decode short TransRecEF records A TransRecEF is based on a TransparentEF. This means that a TransRecEF is basically normal TransparentEF that holds a record oriented data structure. This also requires that the total length of the TransRecEF is a multiple of the record length of the data structure that is stored in it. When this is not the case, the last record will be cut short and the decoding will fail. We should guard against this case. Related: OS#6598 Change-Id: Ib1dc4d7ce306f1f0b080bb4b6abc36e72431d3fa --- pySim/filesystem.py | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/pySim/filesystem.py b/pySim/filesystem.py index 246afb0b..7f350eca 100644 --- a/pySim/filesystem.py +++ b/pySim/filesystem.py @@ -1224,6 +1224,13 @@ class TransRecEF(TransparentEF): Returns: abstract_data; dict representing the decoded data """ + + # The record data length should always be equal or at least greater than the record length defined for the + # TransRecEF. Short records may be occur when the length of the underlying TransparentEF is not a multiple + # of the TransRecEF record length. + if len(raw_hex_data) // 2 < self.__get_rec_len(): + return {'raw': raw_hex_data} + method = getattr(self, '_decode_record_hex', None) if callable(method): return method(raw_hex_data) @@ -1251,6 +1258,11 @@ class TransRecEF(TransparentEF): Returns: abstract_data; dict representing the decoded data """ + + # See comment in decode_record_hex (above) + if len(raw_bin_data) < self.__get_rec_len(): + return {'raw': b2h(raw_bin_data)} + method = getattr(self, '_decode_record_bin', None) if callable(method): return method(raw_bin_data)