Skip to content

Instantly share code, notes, and snippets.

@brenard
Created December 8, 2024 20:24
Show Gist options
  • Select an option

  • Save brenard/6481df9c26d9109394b58ab1c73082de to your computer and use it in GitHub Desktop.

Select an option

Save brenard/6481df9c26d9109394b58ab1c73082de to your computer and use it in GitHub Desktop.
Identify bad binary libs in x86 HA docker image
#! /usr/bin/env python3
import sys
from pathlib import Path
from readelf import readelf
from readelf.err import ParseError
from readelf.const import ISA
from importlib import metadata as importlib_metadata
excepted_isa = ISA.ISA_X86
ignored_packages = {
"debugpy" : ["attach_linux_x86.so", "attach_linux_amd64.so"],
}
print("Computing SO files to python package map...")
packages_version = {}
so_files = {}
for dist in importlib_metadata.distributions():
packages_version[dist.name] = dist.version
for file in dist.files or []:
if str(file).lower().endswith(".so"):
so_files[str(dist.locate_file(file))] = dist.name
print(f"Searching for non-{excepted_isa.name.replace('ISA-', '')} SO file...")
bad_packages = {}
for so_file in Path("/usr/local/lib/python3.13/site-packages/").rglob("*.so"):
try:
path = str(so_file)
elf = readelf(path)
if elf.isa != excepted_isa:
package = so_files.get(path, path)
if package in ignored_packages and so_file.name in ignored_packages[package]:
continue
if package not in bad_packages:
bad_packages[package] = {
"version": packages_version.get(package),
"bad_sofiles": {},
}
bad_packages[package]["bad_sofiles"][path] = elf.isa.name
except ParseError:
print(f"Failed to parse ELF headers of {path}")
if not bad_packages:
print("No bad python packages found")
sys.exit(0)
print(f"{len(bad_packages)} bad python packages found:")
for package_name in sorted(bad_packages):
package = bad_packages[package_name]
print(f" - {package_name} == {package['version']}:")
for sofile, arch in package["bad_sofiles"].items():
print(f" - {sofile} ({arch})")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment