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

Parse UKIs produced by ukify #47

Closed
wants to merge 2 commits into from
Closed
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
20 changes: 17 additions & 3 deletions ecleankernel/file.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import enum
import errno
import importlib
import logging
import os
import shutil
import struct
Expand Down Expand Up @@ -247,16 +248,29 @@ def read_version_from_efi(self,
if len(buf) != 40:
raise UnrecognizedKernelError(
f"PE file {self.path}: EOF in section table!")
if buf[:8] == b".linux\0\0":
offset = struct.unpack_from("<I", buf, 20)[0]
size = struct.unpack_from("<I", buf, 8)[0]
offset = struct.unpack_from("<I", buf, 20)[0]
if buf[:8] == b".uname\0\0":
# ukify writes uname -r output into the .uname section
# https://uapi-group.org/specifications/specs/unified_kernel_image/
f.seek(initial_offset + offset)
# the ' (ukify)' suffix is a hack to avoid raising
# an exception in read_internal_version()
ver = f.read(size) + b' (ukify)'
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
ver = f.read(size) + b' (ukify)'
ver = f.read(size) + b" (ukify)"

Please use "" in new code to match PEP 8.

logging.debug(
f"Found version {ver!r} in '.uname' section")
return ver
elif buf[:8] == b".linux\0\0":
f.seek(initial_offset + offset)
for func in (self.read_version_from_bzimage,
self.read_version_from_raw,
):
verbuf = func(f)
if verbuf is not None:
logging.debug(
f"Found version {verbuf!r} in"
f" '.linux' section (generic)")
return verbuf
break
return None

def __repr__(self) -> str:
Expand Down
36 changes: 31 additions & 5 deletions test/test_file.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ def write_compress(path: Path,

def write_efistub(path: Path,
version_line: bytes,
uname: bool = False,
) -> None:
"""Write an EFIstub kernel image at `path`, with `version_line`"""

Expand All @@ -68,23 +69,34 @@ def write_efistub(path: Path,
f.write(b"PE\0\0\0\0\4\0" + 12 * b"\0" + b"\x08\0\0\0")
# opt header (padding)
f.write(8 * b"\0")
# 4 sections
# 5 sections
section = b".cmdline"
val = b"\0\0\0\0"
if uname:
section = b".uname\0\0" + bytes([len(version_line)])
val = b"\x68\1\0\0"

sections = {
b".code": b"\0\0\0\0",
b".data": b"\0\0\0\0",
b".linux": b"\x80\1\0\0",
section: val,
b".linux": b"\xA8\1\0\0",
b".initrd": b"\0\0\0\0",
}
for name, offset in sections.items():
f.write(name + (20 - len(name)) * b"\0" + offset + 16 * b"\0")
# padding
f.write(64 * b"\0")
# .uname/padding
if uname:
f.write(version_line + (64 - len(version_line)) * b'\0')
else:
f.write(64 * b'\0')
# bzImage
f.write(0x202 * b'\0')
f.write(b'HdrS')
f.write(8 * b'\0')
f.write(b'\x10\x00')
f.write(version_line)
if not uname:
f.write(version_line)


class KernelImageTests(unittest.TestCase):
Expand Down Expand Up @@ -122,6 +134,20 @@ def test_read_internal_version_efistub(self) -> None:
KernelImage(path).read_internal_version(),
"1.2.3")

def test_read_internal_version_efistub_uname(self) -> None:
path = Path(self.td.name) / "vmlinuz"
write_efistub(path, b"1.2.3 built on test", True)
self.assertEqual(
KernelImage(path).read_internal_version(),
"1.2.3")

def test_read_internal_version_efistub_uname_nowhitespace(self) -> None:
path = Path(self.td.name) / "vmlinuz"
write_efistub(path, b"1.2.3", True)
self.assertEqual(
KernelImage(path).read_internal_version(),
"1.2.3")

def test_very_short(self) -> None:
path = Path(self.td.name) / 'vmlinuz'
with open(path, 'wb') as f:
Expand Down