From 2d6ae4319ccecdde7a87d12cb439a892afdec943 Mon Sep 17 00:00:00 2001 From: Daan De Meyer Date: Wed, 29 Jan 2025 14:43:06 +0100 Subject: [PATCH 1/2] ukify: Fix two typing issues --- src/ukify/ukify.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/ukify/ukify.py b/src/ukify/ukify.py index 5f821297c1..091e5a45e1 100755 --- a/src/ukify/ukify.py +++ b/src/ukify/ukify.py @@ -1510,7 +1510,7 @@ def inspect_section( data = section.get_data(length=size) digest = sha256(data).hexdigest() - struct = { + struct: dict[str, Union[int, str]] = { 'size': size, 'sha256': digest, } @@ -1529,7 +1529,7 @@ def inspect_section( if opts.json == 'off': print(f'{name}:\n size: {size} bytes\n sha256: {digest}') if ttype == 'text': - text = textwrap.indent(struct['text'].rstrip(), ' ' * 4) + text = textwrap.indent(cast(str, struct['text']).rstrip(), ' ' * 4) print(f' text:\n{text}') return name, struct From 33b25fa11c408ae40f2aa4300220504329a23a52 Mon Sep 17 00:00:00 2001 From: Daan De Meyer Date: Wed, 29 Jan 2025 14:44:27 +0100 Subject: [PATCH 2/2] ukify: Calculate section size more correctly We should only use Misc_VirtualSize if it's smaller than SizeOfRawData, since in that case it'll be the non-aligned section size. Otherwise we have to use SizeOfRawData to get the size on disk. --- src/ukify/ukify.py | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-) diff --git a/src/ukify/ukify.py b/src/ukify/ukify.py index 091e5a45e1..42890aae84 100755 --- a/src/ukify/ukify.py +++ b/src/ukify/ukify.py @@ -732,6 +732,10 @@ def pe_strip_section_name(name: bytes) -> str: return name.rstrip(b'\x00').decode() +def pe_section_size(section: pefile.SectionStructure) -> int: + return cast(int, min(section.Misc_VirtualSize, section.SizeOfRawData)) + + def call_systemd_measure(uki: UKI, opts: UkifyConfig, profile_start: int = 0) -> None: measure_tool = find_tool( 'systemd-measure', @@ -1314,16 +1318,16 @@ def make_uki(opts: UkifyConfig) -> None: continue print( - f"Copying section '{n}' from '{profile}': {pesection.Misc_VirtualSize} bytes", + f"Copying section '{n}' from '{profile}': {pe_section_size(pesection)} bytes", file=sys.stderr, ) uki.add_section( - Section.create(n, pesection.get_data(length=pesection.Misc_VirtualSize), measure=True) + Section.create(n, pesection.get_data(length=pe_section_size(pesection)), measure=True) ) if opts.sign_profiles: pesection = next(s for s in pe.sections if pe_strip_section_name(s.Name) == '.profile') - id = read_env_file(pesection.get_data(length=pesection.Misc_VirtualSize).decode()).get('ID') + id = read_env_file(pesection.get_data(length=pe_section_size(pesection)).decode()).get('ID') if not id or id not in opts.sign_profiles: print(f'Not signing expected PCR measurements for "{id}" profile') continue @@ -1505,8 +1509,7 @@ def inspect_section( ttype = config.output_mode if config else DEFAULT_SECTIONS_TO_SHOW.get(name, 'binary') - size = section.Misc_VirtualSize - # TODO: Use ignore_padding once we can depend on a newer version of pefile + size = pe_section_size(section) data = section.get_data(length=size) digest = sha256(data).hexdigest()