Skip to content

Commit

Permalink
Fixed a crash while handling non-UTF-8 encoded partition entry names …
Browse files Browse the repository at this point in the history
…in ME region (extension of #118) (#126)
  • Loading branch information
Moondarker authored Feb 28, 2024
1 parent f4b0ff7 commit 990f312
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 10 deletions.
7 changes: 2 additions & 5 deletions uefi_firmware/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
import os
import ctypes

from .utils import dump_data, sguid, blue
from .utils import dump_data, sguid, blue, utf8_decode_safe


class BaseObject(object):
Expand All @@ -27,10 +27,7 @@ def name(self):
@name.setter
def name(self, name):
if isinstance(name, bytes):
try:
name = name.decode("utf-8")
except UnicodeDecodeError:
name = "0x[{}]".format(name.hex())
name = utf8_decode_safe(name)
self._name = name

@property
Expand Down
10 changes: 5 additions & 5 deletions uefi_firmware/me.py
Original file line number Diff line number Diff line change
Expand Up @@ -356,7 +356,7 @@ def showinfo(self, ts='', index=None):
print("%s%s type= %d, subtype= %d, partition name= %s" % (
ts, blue("ME Module Manifest"),
self.structure.ModuleType, self.structure.ModuleSubType,
purple(self.structure.PartitionName.decode("utf-8"))))
purple(utf8_decode_safe(self.structure.PartitionName))))
for module in self.modules:
module.showinfo(ts=" %s" % ts)
for module in self.variable_modules:
Expand Down Expand Up @@ -480,7 +480,7 @@ def dump(self, parent=""):
module.dump(parent)
if self.huffman_llut is not None:
self.huffman_llut.dump(
os.path.join(parent, self.structure.PartitionName.decode("utf-8")))
os.path.join(parent, utf8_decode_safe(self.structure.PartitionName)))


class CPDEntry(MeObject):
Expand Down Expand Up @@ -622,17 +622,17 @@ def process(self):
def showinfo(self, ts='', index=None):
print("%s%s name= %s owner= %s offset= 0x%x size= 0x%x (%d bytes) flags= 0x%x" % (
ts, blue("ME Partition Entry"),
purple(self.structure.Name.decode("utf-8")), purple(self.structure.Owner),
purple(utf8_decode_safe(self.structure.Name)), purple(self.structure.Owner),
self.structure.Offset, self.structure.Size, self.structure.Size, self.structure.Flags))
if self.manifest is not None:
self.manifest.showinfo("%s " % ts)

def dump(self, parent=""):
if self.has_content:
dump_data(os.path.join(parent, "%s.partition" % self.structure.Name.decode("utf-8")),
dump_data(os.path.join(parent, "%s.partition" % utf8_decode_safe(self.structure.Name)),
self.data)
if self.manifest is not None:
self.manifest.dump(os.path.join(parent, self.structure.Name.decode("utf-8")))
self.manifest.dump(os.path.join(parent, utf8_decode_safe(self.structure.Name)))

class MeContainer(MeObject):

Expand Down
15 changes: 15 additions & 0 deletions uefi_firmware/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -165,3 +165,18 @@ def flatten_firmware_objects(base_objects):
if "objects" in _object and len(_object["objects"]) > 0:
objects += flatten_firmware_objects(_object["objects"])
return objects

def utf8_decode_safe(byte_array):
'''Safely decode a byte array into a UTF-8 string.
Args:
byte_array (bytes): Unsafe (presumably) UTF-8 encoded string
Returns:
str: A proper string OR a 0x-prefixed hex representation of input byte array
'''

try:
return byte_array.decode("utf-8")
except UnicodeDecodeError:
return "0x[{}]".format(byte_array.hex())

0 comments on commit 990f312

Please sign in to comment.