@@ -740,6 +740,10 @@ def pe_strip_section_name(name: bytes) -> str:
740
740
return name .rstrip (b'\x00 ' ).decode ()
741
741
742
742
743
+ def pe_section_size (section : pefile .SectionStructure ) -> int :
744
+ return cast (int , min (section .Misc_VirtualSize , section .SizeOfRawData ))
745
+
746
+
743
747
def call_systemd_measure (uki : UKI , opts : UkifyConfig , profile_start : int = 0 ) -> None :
744
748
measure_tool = find_tool (
745
749
'systemd-measure' ,
@@ -1364,16 +1368,16 @@ def make_uki(opts: UkifyConfig) -> None:
1364
1368
continue
1365
1369
1366
1370
print (
1367
- f"Copying section '{ n } ' from '{ profile } ': { pesection . Misc_VirtualSize } bytes" ,
1371
+ f"Copying section '{ n } ' from '{ profile } ': { pe_section_size ( pesection ) } bytes" ,
1368
1372
file = sys .stderr ,
1369
1373
)
1370
1374
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 )
1372
1376
)
1373
1377
1374
1378
if opts .sign_profiles :
1375
1379
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' )
1377
1381
if not id or id not in opts .sign_profiles :
1378
1382
print (f'Not signing expected PCR measurements for "{ id } " profile' )
1379
1383
continue
@@ -1555,12 +1559,11 @@ def inspect_section(
1555
1559
1556
1560
ttype = config .output_mode if config else DEFAULT_SECTIONS_TO_SHOW .get (name , 'binary' )
1557
1561
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 )
1560
1563
data = section .get_data (length = size )
1561
1564
digest = sha256 (data ).hexdigest ()
1562
1565
1563
- struct = {
1566
+ struct : dict [ str , Union [ int , str ]] = {
1564
1567
'size' : size ,
1565
1568
'sha256' : digest ,
1566
1569
}
@@ -1579,7 +1582,7 @@ def inspect_section(
1579
1582
if opts .json == 'off' :
1580
1583
print (f'{ name } :\n size: { size } bytes\n sha256: { digest } ' )
1581
1584
if ttype == 'text' :
1582
- text = textwrap .indent (struct ['text' ].rstrip (), ' ' * 4 )
1585
+ text = textwrap .indent (cast ( str , struct ['text' ]) .rstrip (), ' ' * 4 )
1583
1586
print (f' text:\n { text } ' )
1584
1587
1585
1588
return name , struct
0 commit comments