#!/usr/bin/env python3 """verify-image-arch.py - prove an image platform really contains binaries of that architecture. An image whose config says linux/arm64 can still carry x86-64 binaries (the "x86 in arm64" exec-format-error loop seen with haproxy-manager-base). The registry manifest cannot tell; the ELF header can. This pulls REF for one platform, copies the named key binaries out of a created (never started, never executed) container and checks e_machine of each: aarch64 = 0xB7, x86-64 = 0x3E. Same idea as haproxy-manager-base scripts/verify-image-arch.py (PR #22), adapted to docker cp so no QEMU execution is needed. verify-image-arch.py --image REF --platform linux/arm64|linux/amd64 --bin /usr/bin/php [--bin ...] Exit 0 = every --bin is an ELF64 little-endian binary of the platform's machine type. Exit 1 = a binary is missing, not an ELF, or of the wrong architecture (or docker failed). Exit 2 = usage. Env: DOCKER overrides the docker command (tests use a stub). Nothing in REF is ever run. """ import argparse import os import shlex import subprocess import sys import tarfile MACHINES = {'linux/arm64': (0xB7, 'AArch64'), 'linux/amd64': (0x3E, 'x86-64')} NAMES = {0x3E: 'x86-64', 0xB7: 'AArch64', 0x03: 'x86', 0x28: 'ARM', 0xF3: 'RISC-V', 0x15: 'PowerPC64', 0x16: 'S390'} MAX_STREAM = 1 << 31 # refuse to read more than 2 GiB of one tar stream def elf_machine(head): """Return e_machine of an ELF64 little-endian header, or raise ValueError.""" if len(head) < 20 or head[:4] != b'\x7fELF': raise ValueError('not an ELF file') if head[4] != 2: raise ValueError('not ELF64 (EI_CLASS=%d)' % head[4]) if head[5] != 1: raise ValueError('not little-endian (EI_DATA=%d)' % head[5]) return int.from_bytes(head[18:20], 'little') def docker_cmd(): return shlex.split(os.environ.get('DOCKER') or 'docker') def run(args, **kw): return subprocess.run(docker_cmd() + args, capture_output=True, **kw) def first_bytes_of(cid, path): """First 64 bytes of the regular file at path (symlinks followed) inside container cid.""" p = subprocess.Popen(docker_cmd() + ['cp', '-L', '%s:%s' % (cid, path), '-'], stdout=subprocess.PIPE, stderr=subprocess.PIPE) try: tf = tarfile.open(fileobj=p.stdout, mode='r|') m = tf.next() if m is None or not m.isreg(): raise ValueError('%s is not a regular file' % path) return tf.extractfile(m).read(64) except tarfile.TarError as e: err = p.stderr.read(300).decode('utf-8', 'replace').strip() raise ValueError('cannot read %s: %s %s' % (path, e, err)) finally: p.kill() p.wait() def main(argv): ap = argparse.ArgumentParser(description=__doc__.split('\n')[0]) ap.add_argument('--image', required=True) ap.add_argument('--platform', required=True, choices=sorted(MACHINES)) ap.add_argument('--bin', dest='bins', action='append', required=True) ap.add_argument('--no-pull', action='store_true', help='image is already in the local store') a = ap.parse_args(argv) want, label = MACHINES[a.platform] if not a.no_pull: r = run(['pull', '--quiet', '--platform', a.platform, a.image]) if r.returncode != 0: print('verify-image-arch: docker pull failed: %s' % r.stderr.decode('utf-8', 'replace')[:300].strip(), file=sys.stderr) return 1 r = run(['create', '--platform', a.platform, '--entrypoint', '/nonexistent', a.image]) cid = r.stdout.decode().strip() if r.returncode != 0 or not cid: print('verify-image-arch: docker create failed: %s' % r.stderr.decode('utf-8', 'replace')[:300].strip(), file=sys.stderr) return 1 rc = 0 try: for path in a.bins: try: m = elf_machine(first_bytes_of(cid, path)) except ValueError as e: print('FAIL %s %s %s: %s' % (a.image, a.platform, path, e)) rc = 1 continue if m != want: print('FAIL %s %s %s: e_machine=0x%X (%s), want 0x%X (%s)' % ( a.image, a.platform, path, m, NAMES.get(m, '?'), want, label)) rc = 1 else: print('OK %s %s %s: e_machine=0x%X (%s)' % (a.image, a.platform, path, m, label)) finally: run(['rm', '-f', cid]) return rc if __name__ == '__main__': sys.exit(main(sys.argv[1:]))