Skip to content

Commit 2e326aa

Browse files
authored
ukify: Calculate section size more correctly (systemd#36215)
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.
2 parents d35f796 + 33b25fa commit 2e326aa

File tree

1 file changed

+10
-7
lines changed

1 file changed

+10
-7
lines changed

src/ukify/ukify.py

+10-7
Original file line numberDiff line numberDiff line change
@@ -740,6 +740,10 @@ def pe_strip_section_name(name: bytes) -> str:
740740
return name.rstrip(b'\x00').decode()
741741

742742

743+
def pe_section_size(section: pefile.SectionStructure) -> int:
744+
return cast(int, min(section.Misc_VirtualSize, section.SizeOfRawData))
745+
746+
743747
def call_systemd_measure(uki: UKI, opts: UkifyConfig, profile_start: int = 0) -> None:
744748
measure_tool = find_tool(
745749
'systemd-measure',
@@ -1364,16 +1368,16 @@ def make_uki(opts: UkifyConfig) -> None:
13641368
continue
13651369

13661370
print(
1367-
f"Copying section '{n}' from '{profile}': {pesection.Misc_VirtualSize} bytes",
1371+
f"Copying section '{n}' from '{profile}': {pe_section_size(pesection)} bytes",
13681372
file=sys.stderr,
13691373
)
13701374
uki.add_section(
1371-
Section.create(n, pesection.get_data(length=pesection.Misc_VirtualSize), measure=True)
1375+
Section.create(n, pesection.get_data(length=pe_section_size(pesection)), measure=True)
13721376
)
13731377

13741378
if opts.sign_profiles:
13751379
pesection = next(s for s in pe.sections if pe_strip_section_name(s.Name) == '.profile')
1376-
id = read_env_file(pesection.get_data(length=pesection.Misc_VirtualSize).decode()).get('ID')
1380+
id = read_env_file(pesection.get_data(length=pe_section_size(pesection)).decode()).get('ID')
13771381
if not id or id not in opts.sign_profiles:
13781382
print(f'Not signing expected PCR measurements for "{id}" profile')
13791383
continue
@@ -1555,12 +1559,11 @@ def inspect_section(
15551559

15561560
ttype = config.output_mode if config else DEFAULT_SECTIONS_TO_SHOW.get(name, 'binary')
15571561

1558-
size = section.Misc_VirtualSize
1559-
# TODO: Use ignore_padding once we can depend on a newer version of pefile
1562+
size = pe_section_size(section)
15601563
data = section.get_data(length=size)
15611564
digest = sha256(data).hexdigest()
15621565

1563-
struct = {
1566+
struct: dict[str, Union[int, str]] = {
15641567
'size': size,
15651568
'sha256': digest,
15661569
}
@@ -1579,7 +1582,7 @@ def inspect_section(
15791582
if opts.json == 'off':
15801583
print(f'{name}:\n size: {size} bytes\n sha256: {digest}')
15811584
if ttype == 'text':
1582-
text = textwrap.indent(struct['text'].rstrip(), ' ' * 4)
1585+
text = textwrap.indent(cast(str, struct['text']).rstrip(), ' ' * 4)
15831586
print(f' text:\n{text}')
15841587

15851588
return name, struct

0 commit comments

Comments
 (0)