From bf25be5eaabcee6528157954d97a1e79e047a395 Mon Sep 17 00:00:00 2001 From: Yan Date: Mon, 24 Mar 2025 16:24:53 -0700 Subject: [PATCH 1/3] let's accept reality, and make the Auto Load Libs value False! --- cle/loader.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/cle/loader.py b/cle/loader.py index 068daa8d1..d5a81acfe 100644 --- a/cle/loader.py +++ b/cle/loader.py @@ -56,7 +56,7 @@ class Loader: def __init__( self, main_binary: str | BinaryIO | Path | Backend, - auto_load_libs: bool = True, + auto_load_libs: bool = False, concrete_target=None, force_load_libs: Iterable[str | BinaryIO | Path] = (), skip_libs: Iterable[str] = (), From 6a922e399f2d564738e499cf5ed5acb10cc2218a Mon Sep 17 00:00:00 2001 From: Yan Date: Mon, 24 Mar 2025 16:37:16 -0700 Subject: [PATCH 2/3] load libs explicitly in testcases --- tests/test_aarch64_relocations.py | 2 +- tests/test_arch_detect.py | 2 +- tests/test_arm_firmware.py | 4 ++-- tests/test_blob.py | 2 ++ tests/test_coff.py | 4 ++-- tests/test_elfcore.py | 4 ++-- tests/test_gdb.py | 4 ++-- tests/test_got.py | 2 +- tests/test_macho_dyld.py | 4 ++-- tests/test_macho_libs.py | 5 +++-- tests/test_macho_reloc.py | 3 ++- tests/test_namedregion.py | 2 +- tests/test_patched_stream.py | 2 +- tests/test_plt.py | 1 + tests/test_ppc_relocations.py | 2 +- tests/test_relocated.py | 9 +++++---- tests/test_runpath.py | 2 +- tests/test_simdata.py | 2 +- tests/test_stream.py | 2 +- tests/test_tls_resiliency.py | 2 +- tests/test_unpackword.py | 1 + 21 files changed, 34 insertions(+), 27 deletions(-) diff --git a/tests/test_aarch64_relocations.py b/tests/test_aarch64_relocations.py index 7fbc7e391..13a52afde 100644 --- a/tests/test_aarch64_relocations.py +++ b/tests/test_aarch64_relocations.py @@ -13,7 +13,7 @@ def test_aarch64_relocs(): """ test_location = str(os.path.join(os.path.dirname(os.path.realpath(__file__)), "..", "..", "binaries", "tests")) path = os.path.join(test_location, "aarch64", "aarch64-relocs.o") - loader = cle.Loader(path, main_opts={"base_addr": 0x210120}) + loader = cle.Loader(path, main_opts={"base_addr": 0x210120}, auto_load_libs=True) relocations = loader.main_object.relocs aarch64_backend = cle.backends.elf.relocation.arm64 diff --git a/tests/test_arch_detect.py b/tests/test_arch_detect.py index ee3511c25..03ff3c516 100644 --- a/tests/test_arch_detect.py +++ b/tests/test_arch_detect.py @@ -24,7 +24,7 @@ class TestArchPcodeDetect(unittest.TestCase): def test_elf_m68k(self): binpath = os.path.join(test_location, "m68k/mul_add_sub_xor_m68k_be") - ld = cle.Loader(binpath) + ld = cle.Loader(binpath, auto_load_libs=True) arch = ld.main_object.arch assert isinstance(arch, archinfo.ArchPcode) assert arch.name == "68000:BE:32:default" diff --git a/tests/test_arm_firmware.py b/tests/test_arm_firmware.py index 750ef1557..a97442634 100644 --- a/tests/test_arm_firmware.py +++ b/tests/test_arm_firmware.py @@ -17,7 +17,7 @@ def test_empty_segements(): :return: """ path = os.path.join(test_location, "armel", "efm32gg.elf") - cle.Loader(path, rebase_granularity=0x1000) + cle.Loader(path, rebase_granularity=0x1000, auto_load_libs=True) # If we survive this, we're doing OK! @@ -30,7 +30,7 @@ def test_thumb_object(): :return: """ path = os.path.join(test_location, "armel", "i2c_api.o") - loader = cle.Loader(path, rebase_granularity=0x1000) + loader = cle.Loader(path, rebase_granularity=0x1000, auto_load_libs=True) for r in loader.main_object.relocs: if r.__class__ == cle.backends.elf.relocation.arm.R_ARM_THM_JUMP24: if r.symbol.name == "HAL_I2C_ER_IRQHandler": diff --git a/tests/test_blob.py b/tests/test_blob.py index f0227e516..e1bdb5f22 100644 --- a/tests/test_blob.py +++ b/tests/test_blob.py @@ -22,6 +22,7 @@ def test_blob_0(): "entry_point": ENTRYPOINT, "arch": "ARM", }, + auto_load_libs=True, ) assert ld.main_object.linked_base == BASE_ADDR @@ -55,6 +56,7 @@ def test_blob_1(): "arch": "ARM", "offset": offset, }, + auto_load_libs=True, ) assert ld.main_object.linked_base == BASE_ADDR diff --git a/tests/test_coff.py b/tests/test_coff.py index f3ab4be8b..02c2a19a0 100644 --- a/tests/test_coff.py +++ b/tests/test_coff.py @@ -16,7 +16,7 @@ class TestCoff(unittest.TestCase): def test_x86(self): exe = os.path.join(TEST_BASE, "tests", "x86", "fauxware.obj") - ld = cle.Loader(exe) + ld = cle.Loader(exe, auto_load_libs=True) symbol_names = {sym.name for sym in ld.main_object.symbols} assert "_main" in symbol_names assert "_accepted" in symbol_names @@ -25,7 +25,7 @@ def test_x86(self): def test_x86_64(self): exe = os.path.join(TEST_BASE, "tests", "x86_64", "fauxware.obj") - ld = cle.Loader(exe) + ld = cle.Loader(exe, auto_load_libs=True) symbol_names = {sym.name for sym in ld.main_object.symbols} assert "main" in symbol_names assert "accepted" in symbol_names diff --git a/tests/test_elfcore.py b/tests/test_elfcore.py index 03d47d7f7..98e8d1f89 100644 --- a/tests/test_elfcore.py +++ b/tests/test_elfcore.py @@ -29,7 +29,7 @@ def test_remote_file_mapping(): "/tmp/foobar/does-not-exist/libc.so.6": f"{get_binary_directory()}/libc.so.6", "/tmp/foobar/does-not-exist/ld-linux-x86-64.so.2": f"{get_binary_directory()}/ld-linux-x86-64.so.2", } - ld = cle.Loader(get_coredump_file(), main_opts={"backend": "elfcore", "remote_file_mapping": remote_file_mapping}) + ld = cle.Loader(get_coredump_file(), main_opts={"backend": "elfcore", "remote_file_mapping": remote_file_mapping}, auto_load_libs=True) check_objects_loaded(ld) @@ -39,5 +39,5 @@ def test_remote_file_mapper(): def remote_file_mapper(x): return x.replace("/tmp/foobar/does-not-exist", directory_for_binaries) - ld = cle.Loader(get_coredump_file(), main_opts={"backend": "elfcore", "remote_file_mapper": remote_file_mapper}) + ld = cle.Loader(get_coredump_file(), main_opts={"backend": "elfcore", "remote_file_mapper": remote_file_mapper}, auto_load_libs=True) check_objects_loaded(ld) diff --git a/tests/test_gdb.py b/tests/test_gdb.py index c04c6ac22..7aa9bec7e 100644 --- a/tests/test_gdb.py +++ b/tests/test_gdb.py @@ -18,13 +18,13 @@ def check_addrs(ld): def test_info_proc_maps(): mappath = os.path.join(test_location, "../tests_data/test_gdb_plugin/procmap") - ld = cle.Loader(binpath, **cle.convert_info_proc_maps(mappath)) + ld = cle.Loader(binpath, **cle.convert_info_proc_maps(mappath), auto_load_libs=True) check_addrs(ld) def test_info_sharedlibrary(): mappath = os.path.join(test_location, "../tests_data/test_gdb_plugin/info_sharedlibs") - ld = cle.Loader(binpath, **cle.convert_info_sharedlibrary(mappath)) + ld = cle.Loader(binpath, **cle.convert_info_sharedlibrary(mappath), auto_load_libs=True) check_addrs(ld) diff --git a/tests/test_got.py b/tests/test_got.py index 2d60ced61..1fb2a4fe3 100644 --- a/tests/test_got.py +++ b/tests/test_got.py @@ -23,7 +23,7 @@ def test_ppc(self): def test_mipsel(self): ping = os.path.join(self.test_location, "mipsel", "darpa_ping") skip = ["libgcc_s.so.1", "libresolv.so.0"] - ld = cle.Loader(ping, skip_libs=skip) + ld = cle.Loader(ping, skip_libs=skip, auto_load_libs=True) dep = set(ld._satisfied_deps) loadedlibs = set(ld.shared_objects) diff --git a/tests/test_macho_dyld.py b/tests/test_macho_dyld.py index 34d343ecd..582976cdc 100644 --- a/tests/test_macho_dyld.py +++ b/tests/test_macho_dyld.py @@ -15,7 +15,7 @@ def test_fixups(): Tests the pointer format DYLD_CHAINED_PTR_64_OFFSET :return: """ - binary: MachO = cast(MachO, cle.Loader(str(TEST_BASE / "tests" / "aarch64" / "dyld_ios15.macho")).main_object) + binary: MachO = cast(MachO, cle.Loader(str(TEST_BASE / "tests" / "aarch64" / "dyld_ios15.macho"), auto_load_libs=True).main_object) expected = { 0x100008100: 0x100007A40, 0x1000081E0: 0x1000072B0, @@ -46,7 +46,7 @@ def test_fixups(): def test_symbols(): - loader = cle.Loader(str(TEST_BASE / "tests" / "aarch64" / "dyld_ios15.macho")) + loader = cle.Loader(str(TEST_BASE / "tests" / "aarch64" / "dyld_ios15.macho"), auto_load_libs=True) binary: MachO = cast(MachO, loader.main_object) expected = [ diff --git a/tests/test_macho_libs.py b/tests/test_macho_libs.py index b876849fd..03575e6cc 100644 --- a/tests/test_macho_libs.py +++ b/tests/test_macho_libs.py @@ -16,7 +16,7 @@ def test_library_15(): :return: """ - ld = cle.Loader(TEST_BASE / "FrameWorkApp.app_15" / "Frameworks" / "dynamicLibrary.framework" / "dynamicLibrary") + ld = cle.Loader(TEST_BASE / "FrameWorkApp.app_15" / "Frameworks" / "dynamicLibrary.framework" / "dynamicLibrary", auto_load_libs=True) lib = ld.main_object assert isinstance(lib, MachO) # The base address should be 0 until full rebasing support is implemented @@ -30,7 +30,7 @@ def test_library_14(): Test some basics about loading any kind of library :return: """ - ld = cle.Loader(TEST_BASE / "FrameWorkApp.app_14" / "Frameworks" / "dynamicLibrary.framework" / "dynamicLibrary") + ld = cle.Loader(TEST_BASE / "FrameWorkApp.app_14" / "Frameworks" / "dynamicLibrary.framework" / "dynamicLibrary", auto_load_libs=True) lib = ld.main_object assert isinstance(lib, MachO) # The base address should be 0 until full rebasing support is implemented @@ -51,6 +51,7 @@ def test_framework_ios15(): force_load_libs=( TEST_BASE / "FrameWorkApp.app_15" / "Frameworks" / "dynamicLibrary.framework" / "dynamicLibrary", ), + auto_load_libs=True, ) assert isinstance(ld.main_object, MachO) diff --git a/tests/test_macho_reloc.py b/tests/test_macho_reloc.py index d4fd9db47..eb3d87976 100644 --- a/tests/test_macho_reloc.py +++ b/tests/test_macho_reloc.py @@ -112,7 +112,7 @@ def test_basic_reloc_functionality(): def test_chained_fixups_relocs(): machofile = os.path.join(TEST_BASE, "tests", "aarch64", "dyld_ios15.macho") - ld = cle.Loader(machofile) + ld = cle.Loader(machofile, auto_load_libs=True) for reloc in ld.main_object.relocs: if not isinstance(reloc, MachOPointerRelocation): continue @@ -555,6 +555,7 @@ def test_all_relocs(): ld = cle.Loader( ONESIGNAL_BASE / "Frameworks" / "OneSignalLocation.framework" / "OneSignalLocation", main_opts={"base_addr": 0x1_0000_0000}, + auto_load_libs=True, ) lib = ld.main_object assert isinstance(lib, MachO) diff --git a/tests/test_namedregion.py b/tests/test_namedregion.py index 4df8b663a..e348e406f 100644 --- a/tests/test_namedregion.py +++ b/tests/test_namedregion.py @@ -10,7 +10,7 @@ def test_basic_named_region(): bin_path = os.path.join(test_location, "armel", "lwip_udpecho_bm.elf") - loader = Loader(bin_path) + loader = Loader(bin_path, auto_load_libs=True) # Standard CortexM regions mmio = NamedRegion("mmio", 0x40000000, 0x50000000) sys = NamedRegion("sys", 0xE000E000, 0xE0010000) diff --git a/tests/test_patched_stream.py b/tests/test_patched_stream.py index 43672d86c..df7ef63de 100644 --- a/tests/test_patched_stream.py +++ b/tests/test_patched_stream.py @@ -29,6 +29,6 @@ def test_patched_stream(): def test_malformed_sections(): - ld = cle.Loader(os.path.join(tests_path, "i386", "oxfoo1m3")) + ld = cle.Loader(os.path.join(tests_path, "i386", "oxfoo1m3"), auto_load_libs=True) assert len(ld.main_object.segments) == 1 assert len(ld.main_object.sections) == 0 diff --git a/tests/test_plt.py b/tests/test_plt.py index 564d0cc8f..9d5e37149 100644 --- a/tests/test_plt.py +++ b/tests/test_plt.py @@ -194,6 +194,7 @@ def test_plt_full_relro(): ld = cle.Loader( os.path.join(TESTS_BASE, "tests/i386/full-relro.bin"), main_opts={"base_addr": 0x400000}, + auto_load_libs=True, ) assert ld.main_object.plt == {"__libc_start_main": 0x400390} diff --git a/tests/test_ppc_relocations.py b/tests/test_ppc_relocations.py index 10415cea5..651701b42 100644 --- a/tests/test_ppc_relocations.py +++ b/tests/test_ppc_relocations.py @@ -19,7 +19,7 @@ def setup(): """ test_location = str(os.path.join(os.path.dirname(os.path.realpath(__file__)), "..", "..", "binaries", "tests")) path = os.path.join(test_location, "ppc", "partial.o") - loader = cle.Loader(path) + loader = cle.Loader(path, auto_load_libs=True) relocations = loader.main_object.relocs ppc_backend = cle.backends.elf.relocation.ppc diff --git a/tests/test_relocated.py b/tests/test_relocated.py index c85aa89e3..af4826501 100644 --- a/tests/test_relocated.py +++ b/tests/test_relocated.py @@ -14,7 +14,7 @@ def test_relocated(): os.path.dirname(os.path.realpath(__file__)), "../../binaries/tests/i386/prelinked", ) - ld = cle.Loader(filename, ld_path=[shared], rebase_granularity=0x1000000) + ld = cle.Loader(filename, ld_path=[shared], rebase_granularity=0x1000000, auto_load_libs=True) assert ld.main_object.mapped_base == 0x8048000 assert [x.mapped_base for x in ld.all_elf_objects] == [ 0x8048000, @@ -26,7 +26,7 @@ def test_relocated(): def test_first_fit(): filename = os.path.join(os.path.dirname(os.path.realpath(__file__)), "../../binaries/tests/x86_64/cfg_0") - ld = cle.Loader(filename) + ld = cle.Loader(filename, auto_load_libs=True) assert ld.main_object.mapped_base < ld.shared_objects["libc.so.6"].mapped_base assert ld.shared_objects["libc.so.6"].mapped_base < ld.shared_objects["ld-linux-x86-64.so.2"].mapped_base @@ -35,7 +35,7 @@ def test_first_fit(): # , # ] - ld = cle.Loader(filename, lib_opts={"libc.so.6": {"base_addr": 0x1234000}}) + ld = cle.Loader(filename, lib_opts={"libc.so.6": {"base_addr": 0x1234000}}, auto_load_libs=True) assert ld.main_object.mapped_base < ld.shared_objects["ld-linux-x86-64.so.2"].mapped_base assert ld.shared_objects["ld-linux-x86-64.so.2"].mapped_base < ld.shared_objects["libc.so.6"].mapped_base @@ -50,6 +50,7 @@ def test_first_fit(): "libc.so.6": {"base_addr": 0x1234000}, "ld-linux-x86-64.so.2": {"base_addr": 0}, }, + auto_load_libs=True, ) assert ld.shared_objects["ld-linux-x86-64.so.2"].mapped_base < ld.main_object.mapped_base assert ld.main_object.mapped_base < ld.shared_objects["libc.so.6"].mapped_base @@ -65,7 +66,7 @@ def test_local_symbol_reloc(): os.path.dirname(os.path.realpath(__file__)), "../../binaries/tests/armel/btrfs.ko", ) - ld = cle.Loader(filename) + ld = cle.Loader(filename, auto_load_libs=True) # readelf -r btrfs.ko # Relocation section '.rel.init.text' at offset 0xfe318 contains 94 entries diff --git a/tests/test_runpath.py b/tests/test_runpath.py index 456e31059..247e65984 100644 --- a/tests/test_runpath.py +++ b/tests/test_runpath.py @@ -25,7 +25,7 @@ def test_runpath(): shutil.copy(runpath_file, relocated_file) - loader = cle.Loader(relocated_file, except_missing_libs=True) + loader = cle.Loader(relocated_file, except_missing_libs=True, auto_load_libs=True) assert loader.all_objects[1].binary in expected_libs assert loader.all_objects[2].binary in expected_libs finally: diff --git a/tests/test_simdata.py b/tests/test_simdata.py index 65ffd6861..168da5c0b 100644 --- a/tests/test_simdata.py +++ b/tests/test_simdata.py @@ -24,7 +24,7 @@ def test_progname(): def test_got_relocation(): filename = os.path.join(test_location, "x86_64", "multiarch_main_main.o") - ld = cle.Loader(filename) + ld = cle.Loader(filename, auto_load_libs=True) reloc = ld.main_object.relocs[1] assert reloc.symbol.name == "vex_failure_exit" diff --git a/tests/test_stream.py b/tests/test_stream.py index 03c654c17..833c04a19 100644 --- a/tests/test_stream.py +++ b/tests/test_stream.py @@ -17,7 +17,7 @@ def test_stream(): lib1path = os.path.join(dirpath, "libc.so.6") lib2path = os.path.join(dirpath, "ld-linux.so.2") - path_ld = cle.Loader(filepath) + path_ld = cle.Loader(filepath, auto_load_libs=True) stream_ld = cle.Loader( open(filepath, "rb"), auto_load_libs=False, diff --git a/tests/test_tls_resiliency.py b/tests/test_tls_resiliency.py index 33a7fd83a..02677bfa1 100644 --- a/tests/test_tls_resiliency.py +++ b/tests/test_tls_resiliency.py @@ -13,7 +13,7 @@ class TestTlsResiliency(TestCase): @staticmethod def test_tls_pe_incorrect_tls_data_start(): p = os.path.join(test_location, "i386", "windows", "2.exe") - path_ld = cle.Loader(p) + path_ld = cle.Loader(p, auto_load_libs=True) assert path_ld is not None th = path_ld.tls.new_thread() assert th is not None diff --git a/tests/test_unpackword.py b/tests/test_unpackword.py index 731bf3e24..6d759bda5 100644 --- a/tests/test_unpackword.py +++ b/tests/test_unpackword.py @@ -26,6 +26,7 @@ def test_unpackword(): "arch": "x86", "offset": 0, }, + auto_load_libs=True, ) # little endian From 1f6ad1ba26f536ee7e56e8f8dac5302c288bc64c Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Mon, 24 Mar 2025 23:54:05 +0000 Subject: [PATCH 3/3] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- tests/test_elfcore.py | 12 ++++++++++-- tests/test_macho_dyld.py | 4 +++- tests/test_macho_libs.py | 10 ++++++++-- 3 files changed, 21 insertions(+), 5 deletions(-) diff --git a/tests/test_elfcore.py b/tests/test_elfcore.py index 98e8d1f89..23de8460f 100644 --- a/tests/test_elfcore.py +++ b/tests/test_elfcore.py @@ -29,7 +29,11 @@ def test_remote_file_mapping(): "/tmp/foobar/does-not-exist/libc.so.6": f"{get_binary_directory()}/libc.so.6", "/tmp/foobar/does-not-exist/ld-linux-x86-64.so.2": f"{get_binary_directory()}/ld-linux-x86-64.so.2", } - ld = cle.Loader(get_coredump_file(), main_opts={"backend": "elfcore", "remote_file_mapping": remote_file_mapping}, auto_load_libs=True) + ld = cle.Loader( + get_coredump_file(), + main_opts={"backend": "elfcore", "remote_file_mapping": remote_file_mapping}, + auto_load_libs=True, + ) check_objects_loaded(ld) @@ -39,5 +43,9 @@ def test_remote_file_mapper(): def remote_file_mapper(x): return x.replace("/tmp/foobar/does-not-exist", directory_for_binaries) - ld = cle.Loader(get_coredump_file(), main_opts={"backend": "elfcore", "remote_file_mapper": remote_file_mapper}, auto_load_libs=True) + ld = cle.Loader( + get_coredump_file(), + main_opts={"backend": "elfcore", "remote_file_mapper": remote_file_mapper}, + auto_load_libs=True, + ) check_objects_loaded(ld) diff --git a/tests/test_macho_dyld.py b/tests/test_macho_dyld.py index 582976cdc..7e90e6e35 100644 --- a/tests/test_macho_dyld.py +++ b/tests/test_macho_dyld.py @@ -15,7 +15,9 @@ def test_fixups(): Tests the pointer format DYLD_CHAINED_PTR_64_OFFSET :return: """ - binary: MachO = cast(MachO, cle.Loader(str(TEST_BASE / "tests" / "aarch64" / "dyld_ios15.macho"), auto_load_libs=True).main_object) + binary: MachO = cast( + MachO, cle.Loader(str(TEST_BASE / "tests" / "aarch64" / "dyld_ios15.macho"), auto_load_libs=True).main_object + ) expected = { 0x100008100: 0x100007A40, 0x1000081E0: 0x1000072B0, diff --git a/tests/test_macho_libs.py b/tests/test_macho_libs.py index 03575e6cc..9c56abfa0 100644 --- a/tests/test_macho_libs.py +++ b/tests/test_macho_libs.py @@ -16,7 +16,10 @@ def test_library_15(): :return: """ - ld = cle.Loader(TEST_BASE / "FrameWorkApp.app_15" / "Frameworks" / "dynamicLibrary.framework" / "dynamicLibrary", auto_load_libs=True) + ld = cle.Loader( + TEST_BASE / "FrameWorkApp.app_15" / "Frameworks" / "dynamicLibrary.framework" / "dynamicLibrary", + auto_load_libs=True, + ) lib = ld.main_object assert isinstance(lib, MachO) # The base address should be 0 until full rebasing support is implemented @@ -30,7 +33,10 @@ def test_library_14(): Test some basics about loading any kind of library :return: """ - ld = cle.Loader(TEST_BASE / "FrameWorkApp.app_14" / "Frameworks" / "dynamicLibrary.framework" / "dynamicLibrary", auto_load_libs=True) + ld = cle.Loader( + TEST_BASE / "FrameWorkApp.app_14" / "Frameworks" / "dynamicLibrary.framework" / "dynamicLibrary", + auto_load_libs=True, + ) lib = ld.main_object assert isinstance(lib, MachO) # The base address should be 0 until full rebasing support is implemented