Skip to contentSkip to conversation

tools: binman: Test signing an encrypted FIT with a preload header

PATCH Superseded yan wang 3 participants 7 messages Apr 1 – Apr 2

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore

yan wang

author
#0 lore raw

PATCH 1/3 tools: binman: Test signing an encrypted FIT with a preload header

From: Paul HENRYS <[email protected]>

This test is added to work on issue currently faced with binman. Binman calls multiple times mkimage and thus generates the FIT multiple times and the last call happens after the preload header has been generated. When encrypting the image with a random IV or if the timestamp in the FIT has changed between the 2 last calls of mkimage, the preload header would not sign the correct data leading to a corrupted image.

---
 tools/binman/ftest.py                         | 17 +++++
 .../test/336_pre_load_fit_encrypted.dts       | 63 +++++++++++++++++++
 2 files changed, 80 insertions(+)
 create mode 100644 tools/binman/test/336_pre_load_fit_encrypted.dts
tools/binman/ftest.py +17 -0
index a53e37f31b3..3b5014e1e87 100644
@@ -5894,6 +5894,23 @@ fdt         fdtmap                Extract the devicetree blob from the fdtmap
             data = self._DoReadFileDtb('236_pre_load_invalid_key.dts',
                                        entry_args=entry_args)
 
+    def testPreLoadEncryptedFit(self):
+        """Test an encrypted FIT image with a pre-load header"""
+        entry_args = {
+            'pre-load-key-path': os.path.join(self._binman_dir, 'test'),
+        }
+        data = self._DoReadFileDtb(
+            '336_pre_load_fit_encrypted.dts', entry_args=entry_args,
+            extra_indirs=[os.path.join(self._binman_dir, 'test')])[0]
+
+        image_fname = tools.get_output_filename('image.bin')
+        is_signed = self._CheckPreload(image_fname, self.TestFile("dev.key"))
+
+        self.assertEqual(PRE_LOAD_MAGIC, data[:len(PRE_LOAD_MAGIC)])
+        self.assertEqual(PRE_LOAD_VERSION, data[4:4 + len(PRE_LOAD_VERSION)])
+        self.assertEqual(PRE_LOAD_HDR_SIZE, data[8:8 + len(PRE_LOAD_HDR_SIZE)])
+        self.assertEqual(is_signed, True)
+
     def _CheckSafeUniqueNames(self, *images):
         """Check all entries of given images for unsafe unique names"""
         for image in images:
tools/binman/test/336_pre_load_fit_encrypted.dts new file +63 -0

63 lines not on this page · raw message

Signature · 1 line
2.25.1
author
#1 lore raw

PATCH 2/3 binman: Generate the preload header and sign the data only once

From: Paul HENRYS <[email protected]>

To optimize the preload generation, the header and signatures are only generated once all data have been collected. This avoids to sign multiple times.

---
 tools/binman/etype/pre_load.py | 10 ++++------
 1 file changed, 4 insertions(+), 6 deletions(-)
tools/binman/etype/pre_load.py +4 -6
index 00f1a896767..b950a540861 100644
@@ -152,14 +152,12 @@ class Entry_pre_load(Entry_collection):
         return data + pad
 
     def ObtainContents(self):
-        """Obtain a placeholder for the header contents"""
-        # wait that the image is available
-        self.image = self.GetContents(False)
-        if self.image is None:
-            return False
-        self.SetContents(self._CreateHeader())
+        """Do nothing here.
+        The data will be obtained and handled in ProcessContents
+        """
         return True
 
     def ProcessContents(self):
+        self.image = self.GetContents(True)
         data = self._CreateHeader()
         return self.ProcessContentsUpdate(data)
Signature · 1 line
2.25.1
#2 lore raw

Re: [2/3] binman: Generate the preload header and sign the data only once

Hi Paul,

Quoted 11 lines · yan wang

binman: Generate the preload header and sign the data only once
binman: Generate the preload header and sign the data only once

To optimize the preload generation, the header and signatures are only generated once all data have been collected. This avoids to sign multiple times.

tools/binman/etype/pre_load.py class Entry_pre_load(Entry_collection): · line 156

Quoted 5 lines
tools/binman/etype/pre_load.py +5 -0
@@ -152,14 +152,12 @@ class Entry_pre_load(Entry_collection):
+    def ObtainContents(self):
+        """Do nothing here.
+        The data will be obtained and handled in ProcessContents
+        """
+        return True

Since ObtainContents() no longer calls SetContents(), contents_size remains 0 during Pack(). This forces an extra packing pass when ProcessContentsUpdate() detects the size change from 0 to header_size.

Since header_size is known upfront (from __init__), you could set contents_size here to avoid the re-pack:

def ObtainContents(self):
self.contents_size = self.header_size
return True

This would allow Pack() to reserve the correct space immediately.

tools/binman/etype/pre_load.py class Entry_pre_load(Entry_collection): · line 155

Quoted 6 lines
tools/binman/etype/pre_load.py +4 -0
@@ -152,14 +152,12 @@ class Entry_pre_load(Entry_collection):
+    def ProcessContents(self):
+        self.image = self.GetContents(True)
+        data = self._CreateHeader()
+        return self.ProcessContentsUpdate(data)

Looking at the series as a whole, patch 3/3 adds the build_done marking in GetContents() which prevents referenced entries from being rebuilt. However, this patch calls GetContents() unconditionally with required=True. If the referenced entry's data somehow becomes unavailable between passes, this would raise an exception rather than returning gracefully.

I suspect this is fine since ProcessContents() runs after ObtainContents() has succeeded for all entries, but please confirm this is the intended behaviour.

Regards,
Simon

#3 lore raw

Re: [2/3] binman: Generate the preload header and sign the data only once

Hi Simon,

Quoted 24 lines · Simon Glass

This Mail comes from Outside of SoftAtHome: Do not answer, click links or open attachments unless you recognize the sender and know the content is safe.

Hi Paul,

Quoted 10 lines · yan wang

binman: Generate the preload header and sign the data only once
binman: Generate the preload header and sign the data only once

To optimize the preload generation, the header and signatures are only generated once all data have been collected. This avoids to sign multiple times.

tools/binman/etype/pre_load.py class Entry_pre_load(Entry_collection): · line 156

Quoted 5 lines
tools/binman/etype/pre_load.py +5 -0
@@ -152,14 +152,12 @@ class Entry_pre_load(Entry_collection):
+    def ObtainContents(self):
+        """Do nothing here.
+        The data will be obtained and handled in ProcessContents
+        """
+        return True

Since ObtainContents() no longer calls SetContents(), contents_size remains 0 during Pack(). This forces an extra packing pass when ProcessContentsUpdate() detects the size change from 0 to header_size.

Quoted 8 lines

Since header_size is known upfront (from __init__), you could set contents_size here to avoid the re-pack:

def ObtainContents(self):
self.contents_size = self.header_size
return True

This would allow Pack() to reserve the correct space immediately.

Good idea. Thx.

Quoted 14 lines

tools/binman/etype/pre_load.py class Entry_pre_load(Entry_collection): · line 155

Quoted 7 lines
tools/binman/etype/pre_load.py +4 -0
@@ -152,14 +152,12 @@ class Entry_pre_load(Entry_collection):
+    def ProcessContents(self):
+        self.image = self.GetContents(True)
+        data = self._CreateHeader()
+        return self.ProcessContentsUpdate(data)

Looking at the series as a whole, patch 3/3 adds the build_done marking in GetContents() which prevents referenced entries from being rebuilt. However, this patch calls GetContents() unconditionally with required=True. If the referenced entry's data somehow becomes unavailable between passes, this would raise an exception rather than returning gracefully.

Quoted 3 lines

I suspect this is fine since ProcessContents() runs after ObtainContents() has succeeded for all entries, but please confirm this is the intended behaviour.

Actually, the default implementation of ProcessContents() in the collection class calls GetContents(required=True).
The patch restores this call before header generation because the data are no longer retrieved and the header is no longer generated in ObtainContents().

In addition, the GetContents() implementation in the collection class would not raise an exception if the data were None; instead, no data would be added for that entry, if my understanding is correct.

This series of patches aims to generate the preload header only once in ProcessContents() as an optimization and to prevent the data from changing after signing the entry contents with the preload header, the build_done property is set to True for every entry referenced by the preload.

Quoted 3 lines

Regards,
Simon

We will push a v2 of the initial patches taking into account the remarks you made. Thx.

Regards,
Paul

author
#4 lore raw

PATCH 3/3 binman: etype: collection: Set build_done on the referenced entries

In reply to yan wang

The collection etype uses phandles in the 'content' property to reference other entries. Resolve each phandle to its Entry and mark it with build_done to avoid rebuilding the same entry data multiple times.

This is important for certain cases where rebuilding may change the data content, e.g. due to timestamp or random IVs.

---
 tools/binman/etype/collection.py | 8 ++++++++
 1 file changed, 8 insertions(+)
tools/binman/etype/collection.py +8 -0
index c532aafe3e7..20e2640026f 100644
@@ -51,6 +51,14 @@ class Entry_collection(Entry):
                 self.Info('Contents not available yet')
                 # Data not available yet
                 return None
+
+            # Mark referenced entries as build_done to avoid rebuilding
+            node = self.section._node.GetFdt().LookupPhandle(entry_phandle)
+            if node:
+                entry = self.section.FindEntryByNode(node)
+                if entry:
+                    entry.build_done = True
+
             data += entry_data
 
         self.Info('Returning contents size %x' % len(data))
Signature · 1 line
2.25.1
#5 lore raw

Re: [3/3] binman: etype: collection: Set build_done on the referenced entries

Hi Yan,

Quoted 15 lines · yan wang

binman: etype: collection: Set build_done on the referenced entries
binman: etype: collection: Set build_done on the referenced entries

The collection etype uses phandles in the 'content' property to reference other entries. Resolve each phandle to its Entry and mark it with build_done to avoid rebuilding the same entry data multiple times.

This is important for certain cases where rebuilding may change the data content, e.g. due to timestamp or random IVs.

tools/binman/etype/collection.py class Entry_collection(Entry): · line 56

Quoted 6 lines
tools/binman/etype/collection.py +6 -0
@@ -51,6 +51,14 @@ class Entry_collection(Entry):
+            # Mark referenced entries as build_done to avoid rebuilding
+            node = self.section._node.GetFdt().LookupPhandle(entry_phandle)
+            if node:
+                entry = self.section.FindEntryByNode(node)
+                if entry:
+                    entry.build_done = True

This duplicates the phandle lookup that GetContentsByPhandle() already performs. GetContentsByPhandle() calls LookupPhandle() and FindEntryByNode(), and then you repeat those same calls here.

Also, GetContentsByPhandle() raises an exception if node or entry is not found, so the conditional checks here would never evaluate to false after a successful call returns entry_data.

Please can you refactor this to avoid the duplicate lookup? One option is to have GetContentsByPhandle() return the entry as well as the data, or set build_done itself. What do you think?

Regards,
Simon

#6 lore raw
In reply to yan wang

Hi Paul,

tools/binman/test/336_pre_load_fit_encrypted.dts

Quoted 14 lines · yan wang

tools: binman: Test signing an encrypted FIT with a preload header
tools: binman: Test signing an encrypted FIT with a preload header

This test is added to work on issue currently faced with binman. Binman calls multiple times mkimage and thus generates the FIT multiple times and the last call happens after the preload header has been generated. When encrypting the image with a random IV or if the timestamp in the FIT has changed between the 2 last calls of mkimage, the preload header would not sign the correct data leading to a corrupted image.

tools/binman/test/336_pre_load_fit_encrypted.dts new file +0 -0
new file mode 100644

Test file number 336 is already in use by 336_symbols_base.dts. Please can you renumber this to use the next available number (351).

tools/binman/ftest.py fdt fdtmap Extract the devicetree blob from the fdtmap · line 5895

Quoted 4 lines
tools/binman/ftest.py +2 -0
@@ -5894,6 +5894,23 @@ fdt         fdtmap                Extract the devicetree blob from the fdtmap
+    def testPreLoadEncryptedFit(self):
+        """Test an encrypted FIT image with a pre-load header"""

The commit message says this test is added to "work on" an issue, but it doesn't clearly explain that this test exposes a bug fixed by later patches in the series. Please can you clarify in the commit message that this test would fail without patches 2/3 and why? Something like "Add a test to verify the preload header correctly signs an encrypted FIT. This test exercises the case where encryption uses random IVs that would change between mkimage calls."

Regards,
Simon