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

file: Support reading EFI zboot images #53

Closed
wants to merge 3 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
35 changes: 22 additions & 13 deletions ecleankernel/file.py
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,10 @@ def __init__(self,
super().__init__(path, KernelFileType.KERNEL)
self.internal_version = self.read_internal_version()

def decompress_raw(self, f: typing.IO[bytes]) -> bytes:
def decompress_raw(self,
f: typing.IO[bytes],
size: typing.Optional[int] = None,
) -> bytes:
magic_dict = {
b'\x1f\x8b\x08': 'gzip',
b'\x42\x5a\x68': 'bz2',
Expand All @@ -122,8 +125,11 @@ def decompress_raw(self, f: typing.IO[bytes]) -> bytes:
b'\x89\x4c\x5a\x4f\x00\x0d\x0a\x1a\x0a': 'lzo',
}
maxlen = max(len(x) for x in magic_dict)
if size and maxlen > size:
return f.read(size)
with autorewind(f):
header = f.read(maxlen)
buffer = f.read(size or -1)
for magic, comp in magic_dict.items():
if header.startswith(magic):
try:
Expand All @@ -137,21 +143,15 @@ def decompress_raw(self, f: typing.IO[bytes]) -> bytes:
# Technically a redundant import, this is just
# to make your IDE happy :)
import zstandard
reader = zstandard.ZstdDecompressor().stream_reader(f)
decomp = b''
while True:
chunk = reader.read(1024 * 1024)
if not chunk:
break
decomp += chunk
return decomp
decompress = zstandard.ZstdDecompressor().decompressobj()
return decompress(buffer)
elif comp == 'lzma':
# Using .decompress() causes an error because of
# no end-of-stream marker
return LZMADecompressor().decompress(f.read())
return LZMADecompressor().decompress(buffer)
else:
return getattr(mod, 'decompress')(f.read())
return f.read()
return getattr(mod, 'decompress')(buffer)
return buffer

def read_internal_version(self) -> str:
"""Read version from the kernel file"""
Expand Down Expand Up @@ -199,11 +199,12 @@ def read_version_from_bzimage(self,

def read_version_from_raw(self,
f: typing.IO[bytes],
size: typing.Optional[int] = None,
) -> typing.Optional[bytes]:
"""Read version from raw kernel image"""

# check if it's compressed first
b = self.decompress_raw(f)
b = self.decompress_raw(f, size)
# unlike with bzImage, the raw kernel binary has no header
# that includes the version, so we parse the version message
# that appears on boot
Expand All @@ -226,6 +227,14 @@ def read_version_from_efi(self,
buf = f.read(0x40)
if len(buf) != 0x40 or buf[:2] != b"MZ":
return None

# handle EFI zboot image
# see kernel source code drivers/firmware/efi/libstub/zboot-header.S
if buf[4:8] == b"zimg":
offset, size = struct.unpack_from("<LL", buf, 8)
f.seek(offset)
return self.read_version_from_raw(f, size)

coff_offset = struct.unpack_from("<L", buf, 0x3c)[0]

# at offset, we find PE\0\0 signature and COFF file header
Expand Down
24 changes: 24 additions & 0 deletions test/test_file.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import gzip
import hashlib
import os
import struct
import tempfile
import unittest

Expand Down Expand Up @@ -98,6 +99,22 @@ def write_efistub(path: Path,
f.write(version_line)


def write_efi_zboot(path: Path,
version_line: bytes,
) -> None:
"""Write an EFI zboot kernel image at `path`, with `version_line`"""
# generate a compressed image as our payload
write_compress(path, version_line)
b = path.read_bytes()

with open(path, "wb") as f:
f.write(b"MZ\0\0zimg")
f.write(struct.pack("<LL", 0x40, len(b)))
f.write(8 * b"\0" + b"gzip" + 4 * b"\0")
f.write(0x20 * b"\0")
f.write(b)


class KernelImageTests(unittest.TestCase):
def setUp(self) -> None:
self.td = tempfile.TemporaryDirectory()
Expand Down Expand Up @@ -147,6 +164,13 @@ def test_read_internal_version_efistub_uname_nowhitespace(self) -> None:
KernelImage(path).read_internal_version(),
"1.2.3")

def test_read_internal_version_efi_zboot(self) -> None:
path = Path(self.td.name) / "vmlinuz"
write_efi_zboot(path, b"Linux version 1.2.3 built on test")
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
Loading