Skip to content

Commit 91a2b04

Browse files
authored
Merge pull request #455 from onekey-sec/fix-kernel-extraction-without-initramfs
fix(elf/kernel/initramfs) extraction when initrd for kernel is disabled
2 parents 649cd4b + ca404a3 commit 91a2b04

File tree

1 file changed

+15
-5
lines changed
  • unblob/handlers/executable

1 file changed

+15
-5
lines changed

unblob/handlers/executable/elf.py

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -93,11 +93,21 @@ def extract_initramfs(self, elf, inpath, outdir):
9393
initramfs_end = initramfs_size_offset - padding
9494
initramfs_start = initramfs_end - initramfs_size
9595

96-
carve_chunk_to_file(
97-
outdir.joinpath("initramfs"),
98-
file,
99-
ValidChunk(start_offset=initramfs_start, end_offset=initramfs_end),
100-
)
96+
# initramfs can be turned off (https://www.linux.com/training-tutorials/kernel-newbie-corner-initrd-and-initramfs-whats/)
97+
# in which case the above calculations most probably end up with bogus chunk offsets
98+
if (
99+
init_data.file_offset
100+
<= initramfs_start
101+
< initramfs_end
102+
<= init_data_end_offset
103+
):
104+
carve_chunk_to_file(
105+
outdir.joinpath("initramfs"),
106+
file,
107+
ValidChunk(start_offset=initramfs_start, end_offset=initramfs_end),
108+
)
109+
else:
110+
raise NullExtract()
101111

102112

103113
class _ELFBase(StructHandler):

0 commit comments

Comments
 (0)