laforge has submitted this change. ( 
https://gerrit.osmocom.org/c/pysim/+/43978?usp=email )

 (

1 is the latest approved patch-set.
No files were changed between the latest approved patch-set and the submitted 
one.
 )Change subject: esim.saip: a fillFileOffset gap reads back as 'FF', not NUL
......................................................................

esim.saip: a fillFileOffset gap reads back as 'FF', not NUL

File.file_content_from_tuples() builds the body in an io.BytesIO. When
a fillFileOffset moves the pointer past everything written so far, the
next write makes BytesIO pad the gap with NUL bytes. On the eUICC that
gap is never written and keeps the content the file was created with,
'FF' for a file created without a fill or repeat pattern, and
file_content_to_tuples() then re-encodes the NULs as explicit content.

Pad such a gap with 'FF'. Bytes written explicitly, and bytes a pattern
or template default already covers, are unchanged.

The folder osmocom-pysim-saip-fill-file-offset-erased-state of
hampushellsberg-dev/PySIM-upstream-patches on GitHub has the analysis
and a repro.

Change-Id: Ibdf7fec50fe70ebab5972a26dc2fbbe7469c8f67
---
M pySim/esim/saip/__init__.py
1 file changed, 30 insertions(+), 1 deletion(-)

Approvals:
  jolly: Looks good to me, but someone else must approve
  Jenkins Builder: Verified
  laforge: Looks good to me, approved




diff --git a/pySim/esim/saip/__init__.py b/pySim/esim/saip/__init__.py
index f0abfab..8b81c9c 100644
--- a/pySim/esim/saip/__init__.py
+++ b/pySim/esim/saip/__init__.py
@@ -121,6 +121,27 @@
     NaaIsim.name: NaaIsim,
 }
 
+ERASED_STATE_BYTE = b'\xff'
+
+
+class ErasedStateBytesIO(io.BytesIO):
+    """BytesIO whose implicit growth carries the erased state instead of NUL.
+
+    Writing at a position past the end of a BytesIO pads the gap with NUL. In a
+    TCA profile file that gap is a region the Profile Package deliberately 
skipped,
+    which on the eUICC keeps the content the file was created with -- 'FF' 
when it
+    was created without a fill/repeat pattern.
+    """
+    def write(self, b):
+        position = self.tell()
+        end = self.getbuffer().nbytes
+        if position > end:
+            self.seek(end)
+            super().write(ERASED_STATE_BYTE * (position - end))
+            self.seek(position)
+        return super().write(b)
+
+
 class File:
     """Internal representation of a file in a profile filesystem.

@@ -421,7 +442,15 @@

     def file_content_from_tuples(self, l: List[Tuple]) -> Optional[bytes]:
         """linearize a list of fillFileContent / fillFileOffset tuples into a 
stream of bytes."""
-        stream = io.BytesIO()
+        # A gap that 'fillFileOffset' skips past everything written so far is 
not content:
+        # TCA SAIP V3.4.1 section 8.3.5 makes the offset a pointer move that 
writes nothing, and
+        # section 8.3.3 gives the tuple list the effect of "creating a file 
with a fill/repeat
+        # pattern, or non-pattern content and thereafter updating the content 
via Update". A file
+        # created without a pattern is erased, so those bytes read back as 
'FF' (section 9.10:
+        # the default EF-ARR image is "padded with 'FF...FF'"). A plain 
BytesIO grows with NUL,
+        # which file_content_to_tuples() -- baselining on b'\xff' * 
len(self.body) when no template
+        # applies -- then re-encodes as explicit fillFileContent zeros.
+        stream = ErasedStateBytesIO()
         # Providing file content within "fillFileContent" / "fillFileOffset" 
shall have the same effect as
         # creating a file with a fill/repeat pattern and thereafter updating 
the content via Update.
         # Step 1: Fill with pattern from Fcp or Template

--
To view, visit https://gerrit.osmocom.org/c/pysim/+/43978?usp=email
To unsubscribe, or for help writing mail filters, visit 
https://gerrit.osmocom.org/settings?usp=email

Gerrit-MessageType: merged
Gerrit-Project: pysim
Gerrit-Branch: master
Gerrit-Change-Id: Ibdf7fec50fe70ebab5972a26dc2fbbe7469c8f67
Gerrit-Change-Number: 43978
Gerrit-PatchSet: 2
Gerrit-Owner: laforge <[email protected]>
Gerrit-Reviewer: Jenkins Builder
Gerrit-Reviewer: dexter <[email protected]>
Gerrit-Reviewer: jolly <[email protected]>
Gerrit-Reviewer: laforge <[email protected]>
Gerrit-Reviewer: neels <[email protected]>
Gerrit-CC: hampushellsberg <[email protected]>

Reply via email to