JonathonReinhart / staticx

Create static executable from dynamic executable
https://staticx.readthedocs.io/
Other
345 stars 37 forks source link

Invalid ELF image: Magic number does not match #268

Closed JonathonReinhart closed 10 months ago

JonathonReinhart commented 10 months ago

https://github.com/JonathonReinhart/staticx/actions/runs/7442806204/job/20246774931?pr=267

INFO:root:Running StaticX version 0.14.1+5.gc8e1b6a
INFO:root:Libraries:
INFO:root:  elftools: 0.30
DEBUG:root:External tools:
INFO:root:  ldd: /usr/bin/ldd: ldd (Ubuntu GLIBC 2.35-0ubuntu3.5) 2.35
INFO:root:  objcopy: /usr/bin/objcopy: GNU objcopy (GNU Binutils for Ubuntu) 2.38
INFO:root:  strip: /usr/bin/strip: GNU strip (GNU Binutils for Ubuntu) 2.38
INFO:root:  patchelf: /usr/bin/patchelf: patchelf 0.14.3
DEBUG:root:Arguments:
DEBUG:root:  prog:      './dist/app'
DEBUG:root:  output:    './dist/app.staticx'
DEBUG:root:  libs:      None
DEBUG:root:  strip:     False
DEBUG:root:  compress:  True
DEBUG:root:  debug:     True
INFO:root:Using XZ BCJ filter FILTER_X86
DEBUG:root:Bootloader: bootloader version 0.14.1+5.gc8e1b6a compiled Jan  8 2024 at 03:22:15 by gcc version 11.4.0
INFO:root:Program interpreter: /lib64/ld-linux-x86-64.so.2
DEBUG:root:Running ['patchelf', '--remove-rpath', '/tmp/staticx-prog-g5n19hl1']
DEBUG:root:Running ['patchelf', '--set-interpreter', 'iiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiii', '--set-rpath', 'rrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrr', '--force-rpath', '/tmp/staticx-prog-g5n19hl1']
DEBUG:root:Running ['patchelf', '--no-default-lib', '/tmp/staticx-prog-g5n19hl1']
INFO:root:Using PyInstaller version 6.3.0
INFO:root:Opened PyInstaller archive!
DEBUG:root:Extracting to /tmp/staticx-pyi-c3x5s9f6/_cffi_backend.cpython-310-x86_64-linux-gnu.so
DEBUG:root:Extracting to /tmp/staticx-pyi-c3x5s9f6/_dbus_bindings.cpython-310-x86_64-linux-gnu.so
DEBUG:root:Extracting to /tmp/staticx-pyi-c3x5s9f6/_dbus_glib_bindings.cpython-310-x86_64-linux-gnu.so
DEBUG:root:Extracting to /tmp/staticx-pyi-c3x5s9f6/apt_pkg.cpython-310-x86_64-linux-gnu.so
DEBUG:root:Extracting to /tmp/staticx-pyi-c3x5s9f6/bcrypt/_bcrypt.abi3.so
DEBUG:root:Extracting to /tmp/staticx-pyi-c3x5s9f6/cryptography/hazmat/bindings/_openssl.abi3.so
DEBUG:root:Extracting to /tmp/staticx-pyi-c3x5s9f6/gi/_gi.cpython-310-x86_64-linux-gnu.so
DEBUG:root:Extracting to /tmp/staticx-pyi-c3x5s9f6/gio_modules/libdconfsettings.so
DEBUG:root:Extracting to /tmp/staticx-pyi-c3x5s9f6/gio_modules/libgioenvironmentproxy.so
DEBUG:root:Extracting to /tmp/staticx-pyi-c3x5s9f6/gio_modules/libgiognomeproxy.so
DEBUG:root:Extracting to /tmp/staticx-pyi-c3x5s9f6/gio_modules/libgiognutls.so
DEBUG:root:Extracting to /tmp/staticx-pyi-c3x5s9f6/gio_modules/libgiolibproxy.so
DEBUG:root:Extracting to /tmp/staticx-pyi-c3x5s9f6/lib-dynload/_asyncio.cpython-310-x86_64-linux-gnu.so
DEBUG:root:Extracting to /tmp/staticx-pyi-c3x5s9f6/lib-dynload/_bz2.cpython-310-x86_64-linux-gnu.so
DEBUG:root:Extracting to /tmp/staticx-pyi-c3x5s9f6/lib-dynload/_codecs_cn.cpython-310-x86_64-linux-gnu.so
DEBUG:root:Extracting to /tmp/staticx-pyi-c3x5s9f6/lib-dynload/_codecs_hk.cpython-310-x86_64-linux-gnu.so
DEBUG:root:Extracting to /tmp/staticx-pyi-c3x5s9f6/lib-dynload/_codecs_iso2022.cpython-310-x86_64-linux-gnu.so
DEBUG:root:Extracting to /tmp/staticx-pyi-c3x5s9f6/lib-dynload/_codecs_jp.cpython-310-x86_64-linux-gnu.so
DEBUG:root:Extracting to /tmp/staticx-pyi-c3x5s9f6/lib-dynload/_codecs_kr.cpython-310-x86_64-linux-gnu.so
DEBUG:root:Extracting to /tmp/staticx-pyi-c3x5s9f6/lib-dynload/_codecs_tw.cpython-310-x86_64-linux-gnu.so
DEBUG:root:Extracting to /tmp/staticx-pyi-c3x5s9f6/lib-dynload/_contextvars.cpython-310-x86_64-linux-gnu.so
DEBUG:root:Extracting to /tmp/staticx-pyi-c3x5s9f6/lib-dynload/_ctypes.cpython-310-x86_64-linux-gnu.so
DEBUG:root:Extracting to /tmp/staticx-pyi-c3x5s9f6/lib-dynload/_decimal.cpython-310-x86_64-linux-gnu.so
DEBUG:root:Extracting to /tmp/staticx-pyi-c3x5s9f6/lib-dynload/_hashlib.cpython-310-x86_64-linux-gnu.so
DEBUG:root:Extracting to /tmp/staticx-pyi-c3x5s9f6/lib-dynload/_json.cpython-310-x86_64-linux-gnu.so
DEBUG:root:Extracting to /tmp/staticx-pyi-c3x5s9f6/lib-dynload/_lzma.cpython-310-x86_64-linux-gnu.so
DEBUG:root:Extracting to /tmp/staticx-pyi-c3x5s9f6/lib-dynload/_multibytecodec.cpython-310-x86_64-linux-gnu.so
DEBUG:root:Extracting to /tmp/staticx-pyi-c3x5s9f6/lib-dynload/_multiprocessing.cpython-310-x86_64-linux-gnu.so
DEBUG:root:Extracting to /tmp/staticx-pyi-c3x5s9f6/lib-dynload/_opcode.cpython-310-x86_64-linux-gnu.so
DEBUG:root:Extracting to /tmp/staticx-pyi-c3x5s9f6/lib-dynload/_posixshmem.cpython-310-x86_64-linux-gnu.so
DEBUG:root:Extracting to /tmp/staticx-pyi-c3x5s9f6/lib-dynload/_queue.cpython-310-x86_64-linux-gnu.so
DEBUG:root:Extracting to /tmp/staticx-pyi-c3x5s9f6/lib-dynload/_ssl.cpython-310-x86_64-linux-gnu.so
DEBUG:root:Extracting to /tmp/staticx-pyi-c3x5s9f6/lib-dynload/_uuid.cpython-310-x86_64-linux-gnu.so
DEBUG:root:Extracting to /tmp/staticx-pyi-c3x5s9f6/lib-dynload/mmap.cpython-310-x86_64-linux-gnu.so
DEBUG:root:Extracting to /tmp/staticx-pyi-c3x5s9f6/lib-dynload/readline.cpython-310-x86_64-linux-gnu.so
DEBUG:root:Extracting to /tmp/staticx-pyi-c3x5s9f6/lib-dynload/resource.cpython-310-x86_64-linux-gnu.so
DEBUG:root:Extracting to /tmp/staticx-pyi-c3x5s9f6/lib-dynload/termios.cpython-310-x86_64-linux-gnu.so
DEBUG:root:Extracting to /tmp/staticx-pyi-c3x5s9f6/libapt-pkg.so.6.0
DEBUG:root:Extracting to /tmp/staticx-pyi-c3x5s9f6/libblkid.so.1
DEBUG:root:Extracting to /tmp/staticx-pyi-c3x5s9f6/libbz2.so.1.0
DEBUG:root:Extracting to /tmp/staticx-pyi-c3x5s9f6/libcap.so.2
DEBUG:root:Extracting to /tmp/staticx-pyi-c3x5s9f6/libcrypto.so.3
DEBUG:root:Extracting to /tmp/staticx-pyi-c3x5s9f6/libdbus-1.so.3
DEBUG:root:Extracting to /tmp/staticx-pyi-c3x5s9f6/libexpat.so.1
DEBUG:root:Extracting to /tmp/staticx-pyi-c3x5s9f6/libffi.so.8
DEBUG:root:Extracting to /tmp/staticx-pyi-c3x5s9f6/libgcc_s.so.1
DEBUG:root:Extracting to /tmp/staticx-pyi-c3x5s9f6/libgcrypt.so.20
DEBUG:root:Extracting to /tmp/staticx-pyi-c3x5s9f6/libgio-2.0.so.0
DEBUG:root:Extracting to /tmp/staticx-pyi-c3x5s9f6/libgirepository-1.0.so.1
DEBUG:root:Extracting to /tmp/staticx-pyi-c3x5s9f6/libglib-2.0.so.0
DEBUG:root:Extracting to /tmp/staticx-pyi-c3x5s9f6/libgmodule-2.0.so.0
DEBUG:root:Extracting to /tmp/staticx-pyi-c3x5s9f6/libgmp.so.10
DEBUG:root:Extracting to /tmp/staticx-pyi-c3x5s9f6/libgnutls.so.30
DEBUG:root:Extracting to /tmp/staticx-pyi-c3x5s9f6/libgobject-2.0.so.0
DEBUG:root:Extracting to /tmp/staticx-pyi-c3x5s9f6/libgpg-error.so.0
DEBUG:root:Extracting to /tmp/staticx-pyi-c3x5s9f6/libhogweed.so.6
DEBUG:root:Extracting to /tmp/staticx-pyi-c3x5s9f6/libidn2.so.0
DEBUG:root:Extracting to /tmp/staticx-pyi-c3x5s9f6/liblz4.so.1
DEBUG:root:Extracting to /tmp/staticx-pyi-c3x5s9f6/liblzma.so.5
DEBUG:root:Extracting to /tmp/staticx-pyi-c3x5s9f6/libmount.so.1
DEBUG:root:Extracting to /tmp/staticx-pyi-c3x5s9f6/libmpdec.so.3
DEBUG:root:Extracting to /tmp/staticx-pyi-c3x5s9f6/libnettle.so.8
DEBUG:root:Extracting to /tmp/staticx-pyi-c3x5s9f6/libp11-kit.so.0
DEBUG:root:Extracting to /tmp/staticx-pyi-c3x5s9f6/libpcre.so.3
DEBUG:root:Extracting to /tmp/staticx-pyi-c3x5s9f6/libpcre2-8.so.0
DEBUG:root:Extracting to /tmp/staticx-pyi-c3x5s9f6/libproxy.so.1
DEBUG:root:Extracting to /tmp/staticx-pyi-c3x5s9f6/libpython3.10.so.1.0
DEBUG:root:Extracting to /tmp/staticx-pyi-c3x5s9f6/libreadline.so.8
DEBUG:root:Extracting to /tmp/staticx-pyi-c3x5s9f6/libselinux.so.1
DEBUG:root:Extracting to /tmp/staticx-pyi-c3x5s9f6/libssl.so.3
DEBUG:root:Extracting to /tmp/staticx-pyi-c3x5s9f6/libstdc++.so.6
DEBUG:root:Extracting to /tmp/staticx-pyi-c3x5s9f6/libsystemd.so.0
DEBUG:root:Extracting to /tmp/staticx-pyi-c3x5s9f6/libtasn1.so.6
DEBUG:root:Extracting to /tmp/staticx-pyi-c3x5s9f6/libtinfo.so.6
DEBUG:root:Extracting to /tmp/staticx-pyi-c3x5s9f6/libudev.so.1
DEBUG:root:Extracting to /tmp/staticx-pyi-c3x5s9f6/libunistring.so.2
DEBUG:root:Extracting to /tmp/staticx-pyi-c3x5s9f6/libuuid.so.1
DEBUG:root:Extracting to /tmp/staticx-pyi-c3x5s9f6/libxxhash.so.0
DEBUG:root:Extracting to /tmp/staticx-pyi-c3x5s9f6/libyaml-0.so.2
DEBUG:root:Extracting to /tmp/staticx-pyi-c3x5s9f6/libz.so.1
DEBUG:root:Extracting to /tmp/staticx-pyi-c3x5s9f6/libzstd.so.1
DEBUG:root:Extracting to /tmp/staticx-pyi-c3x5s9f6/share/mime/mime.cache
DEBUG:root:Extracting to /tmp/staticx-pyi-c3x5s9f6/yaml/_yaml.cpython-310-x86_64-linux-gnu.so
Traceback (most recent call last):
  File "/home/runner/.local/lib/python3.10/site-packages/staticx/elf.py", line 318, in open_elf
    return ELFFileX.open(path, mode)
  File "/home/runner/.local/lib/python3.10/site-packages/staticx/elf.py", line 263, in open
    return cls(open(path, mode), path=path)
  File "/home/runner/.local/lib/python3.10/site-packages/staticx/elf.py", line 259, in __init__
    super().__init__(stream)
  File "/home/runner/.local/lib/python3.10/site-packages/elftools/elf/elffile.py", line 84, in __init__
    self._identify_file()
  File "/home/runner/.local/lib/python3.10/site-packages/elftools/elf/elffile.py", line 570, in _identify_file
    elf_assert(magic == b'\x7fELF', 'Magic number does not match')
  File "/home/runner/.local/lib/python3.10/site-packages/elftools/common/utils.py", line 80, in elf_assert
    _assert_with_exception(cond, msg, ELFError)
  File "/home/runner/.local/lib/python3.10/site-packages/elftools/common/utils.py", line 143, in _assert_with_exception
    raise exception_type(msg)
elftools.common.exceptions.ELFError: Magic number does not match

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/home/runner/.local/bin/staticx", line 8, in <module>
    sys.exit(main())
  File "/home/runner/.local/lib/python3.10/site-packages/staticx/__main__.py", line 49, in main
    generate(args.prog, args.output,
  File "/home/runner/.local/lib/python3.10/site-packages/staticx/api.py", line 328, in generate
    gen.generate(output=output)
  File "/home/runner/.local/lib/python3.10/site-packages/staticx/api.py", line 134, in generate
    run_hooks(self)
  File "/home/runner/.local/lib/python3.10/site-packages/staticx/hooks/__init__.py", line 12, in run_hooks
    hook(sx)
  File "/home/runner/.local/lib/python3.10/site-packages/staticx/hooks/pyinstaller.py", line 49, in process_pyinstaller_archive
    h.process()
  File "/home/runner/.local/lib/python3.10/site-packages/staticx/hooks/pyinstaller.py", line 71, in process
    binaries = [b for b in binaries if is_dynamic(b)]
  File "/home/runner/.local/lib/python3.10/site-packages/staticx/hooks/pyinstaller.py", line 71, in <listcomp>
    binaries = [b for b in binaries if is_dynamic(b)]
  File "/home/runner/.local/lib/python3.10/site-packages/staticx/elf.py", line 332, in is_dynamic
    with open_elf(path) as elf:
  File "/home/runner/.local/lib/python3.10/site-packages/staticx/elf.py", line 320, in open_elf
    raise InvalidInputError(f"{path}: Invalid ELF image: {e}")
staticx.errors.InvalidInputError: /tmp/staticx-pyi-c3x5s9f6/share/mime/mime.cache: Invalid ELF image: Magic number does not match
Error: Process completed with exit code 1.
JonathonReinhart commented 10 months ago

For whatever reason, mime.cache appears as a binary file in the pyinstaller archive.

The current code unconditionally calls is_dynamic which raises an InvalidInputError exception if the file is not an ELF.

I honestly have no idea how this has never been hit before. Even before #83.