From 7ae00dbd574e317043c20f1fcf0ee5b2ea66ac40 Mon Sep 17 00:00:00 2001 From: Henglie Date: Sat, 14 Nov 2026 12:31:51 +0800 Subject: [PATCH] elf: set the mmap floor for shellcode instances QlLoaderELF returns early when Qiling is built from raw code, before the profile is loaded and mmap_address is derived. A guest mmap syscall then fails with AttributeError: 'QlLoaderELF' object has no attribute 'mmap_address'. Load the profile before the code branch and set mmap_address from it, so code-only instances expose the same floor as file-backed ones. Add a regression test that creates a code-only x86_64 Linux instance, checks mmap_address and invokes the mmap syscall. Fixes #1444 --- qiling/loader/elf.py | 5 +++-- tests/test_elf.py | 13 +++++++++++++ 2 files changed, 16 insertions(+), 2 deletions(-) diff --git a/qiling/loader/elf.py b/qiling/loader/elf.py index a5a8ac6fe..a21ba5c16 100644 --- a/qiling/loader/elf.py +++ b/qiling/loader/elf.py @@ -70,6 +70,8 @@ def __init__(self, ql: Qiling): super().__init__(ql) def run(self): + self.profile = self.ql.os.profile[f'OS{self.ql.arch.bits}'] + if self.ql.code: self.ql.mem.map(self.ql.os.entry_point, self.ql.os.code_ram_size, info="[shellcode_stack]") @@ -79,11 +81,10 @@ def run(self): self.ql.arch.regs.arch_sp = shellcode_base self.ql.os.entry_point = shellcode_base self.load_address = shellcode_base + self.mmap_address = self.profile.getint('mmap_address') return - self.profile = self.ql.os.profile[f'OS{self.ql.arch.bits}'] - # setup program stack stack_address = self.profile.getint('stack_address') stack_size = self.profile.getint('stack_size') diff --git a/tests/test_elf.py b/tests/test_elf.py index 26f453a5c..2ba786e79 100644 --- a/tests/test_elf.py +++ b/tests/test_elf.py @@ -905,6 +905,19 @@ def test_memory_search(self): del ql + def test_shellcode_mmap_address(self): + # a code-only instance has no ELF image to derive the mmap floor from; + # the loader still has to expose the profile value for the mmap syscall + from qiling.os.posix.syscall.mman import ql_syscall_mmap + + ql = Qiling(code=b"\xCC", archtype=QL_ARCH.X8664, ostype=QL_OS.LINUX, verbose=QL_VERBOSE.OFF) + self.assertEqual(ql.loader.mmap_address, ql.os.profile.getint('OS64', 'mmap_address')) + + addr = ql_syscall_mmap(ql, 0, 0x1000, 0x3, 0x22, -1, 0) + self.assertGreater(addr, 0) + + del ql + def test_elf_linux_x8664_path_traversion(self): ql = Qiling(["../examples/rootfs/x8664_linux/bin/path_traverse_static"], "../examples/rootfs/x8664_linux", verbose=QL_VERBOSE.DEBUG)