Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

(RHEL-30372) efi: check if all sections of our EFI binaries are properly aligned #248

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .github/workflows/build_test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ PACKAGES=(
python3-evdev
python3-jinja2
python3-lxml
python3-pefile
python3-pip
python3-pyparsing
python3-setuptools
Expand Down
1 change: 1 addition & 0 deletions .github/workflows/unit_tests.sh
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ ADDITIONAL_DEPS=(
libzstd-dev
perl
python3-libevdev
python3-pefile
python3-pyparsing
rpm
zstd
Expand Down
1 change: 1 addition & 0 deletions README
Original file line number Diff line number Diff line change
Expand Up @@ -202,6 +202,7 @@ REQUIREMENTS:
docbook-xsl (optional, required for documentation)
xsltproc (optional, required for documentation)
python-jinja2
python-pefile
python-lxml (optional, required to build the indices)
python >= 3.5
meson >= 0.53.2
Expand Down
1 change: 1 addition & 0 deletions meson.build
Original file line number Diff line number Diff line change
Expand Up @@ -1913,6 +1913,7 @@ catalogs = []

############################################################

check_efi_alignment_py = find_program('tools/check-efi-alignment.py')
# Include these now as they provide gnu-efi detection.
subdir('src/fundamental')
subdir('src/boot/efi')
Expand Down
5 changes: 5 additions & 0 deletions src/boot/efi/meson.build
Original file line number Diff line number Diff line change
Expand Up @@ -494,4 +494,9 @@ foreach tuple : [['systemd-boot@0@.@1@', systemd_boot_objects, false, 'systemd-b
install_dir : bootlibdir)

alias_target(tuple[3], efi)

test('check-alignment-@0@'.format(tuple[0].format(efi_arch[0], 'efi')),
check_efi_alignment_py,
args : efi.full_path(),
suite : 'efi')
endforeach
36 changes: 36 additions & 0 deletions tools/check-efi-alignment.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
#!/usr/bin/python3
# SPDX-License-Identifier: LGPL-2.1-or-later
# vi: set tw=110 sw=4 ts=4 et:

import sys

try:
import pefile
except ImportError as e:
print(str(e), file=sys.stderr)
sys.exit(77)


def main():
pe = pefile.PE(sys.argv[1], fast_load=True)

for section in pe.sections:
name = section.Name.rstrip(b"\x00").decode()
file_addr = section.PointerToRawData
virt_addr = section.VirtualAddress
print(f"{name:10s} file=0x{file_addr:08x} virt=0x{virt_addr:08x}")

if file_addr % 512 != 0:
print(f"File address of {name} section is not aligned to 512 bytes", file=sys.stderr)
return 1

if virt_addr % 512 != 0:
print(f"Virt address of {name} section is not aligned to 512 bytes", file=sys.stderr)
return 1

if __name__ == '__main__':
if len(sys.argv) != 2:
print(f"Usage: {sys.argv[0]} pe-image")
sys.exit(1)

sys.exit(main())
Loading