77 lines
3.2 KiB
Python
77 lines
3.2 KiB
Python
#!/usr/bin/env python3
|
|
"""Assemble a Tumbleweed-derived, shell-free runtime from trusted distro ELFs."""
|
|
import os
|
|
from pathlib import Path
|
|
import re
|
|
import shutil
|
|
import subprocess
|
|
import sys
|
|
|
|
root = Path(sys.argv[1])
|
|
root.mkdir(parents=True, exist_ok=True)
|
|
|
|
copied = set()
|
|
library_directories = set()
|
|
|
|
def copy(source):
|
|
source = Path(os.path.normpath(source))
|
|
if source in copied:
|
|
return
|
|
copied.add(source)
|
|
target = root / str(source).lstrip('/')
|
|
target.parent.mkdir(parents=True, exist_ok=True)
|
|
if source.is_symlink():
|
|
link = os.readlink(source)
|
|
if not target.is_symlink():
|
|
target.symlink_to(link)
|
|
# Preserve every hop, not just the final file in a multi-link chain.
|
|
copy(link if os.path.isabs(link) else source.parent / link)
|
|
elif source.is_dir():
|
|
target.mkdir(exist_ok=True)
|
|
for child in source.iterdir():
|
|
copy(child)
|
|
else:
|
|
shutil.copy2(source, target)
|
|
|
|
def copy_elf(executable):
|
|
# ldd includes transitive DT_NEEDED libraries and the ELF interpreter.
|
|
# Only invoke it on installed distro binaries and our compiled helpers.
|
|
result = subprocess.run(['ldd', executable], check=True, text=True, capture_output=True)
|
|
paths = re.findall(r'(/[^\s()]+)', result.stdout)
|
|
if 'not found' in result.stdout or not paths or any(not Path(p).exists() for p in paths):
|
|
raise SystemExit(f'Unresolved runtime dependency for {executable}: {result.stdout}')
|
|
copy(executable)
|
|
for path in paths:
|
|
copy(path)
|
|
library_directories.add(str(Path(path).parent))
|
|
|
|
for executable in sys.argv[2:]:
|
|
copy_elf(executable)
|
|
|
|
# NSS modules can be loaded dynamically rather than appearing in DT_NEEDED.
|
|
for module in ['libnss_dns.so.2', 'libnss_files.so.2']:
|
|
for library in Path('/usr/lib64').glob(module):
|
|
copy_elf(str(library))
|
|
|
|
for data in ['/etc/ssl/ca-bundle.pem', '/etc/ssl/certs', '/etc/ssl/openssl.cnf',
|
|
'/var/lib/ca-certificates', '/usr/lib64/ossl-modules', '/usr/share/zoneinfo',
|
|
'/etc/terminfo', '/usr/share/terminfo', '/usr/share/rtorrent',
|
|
'/etc/services', '/etc/protocols', '/etc/gai.conf', '/etc/os-release']:
|
|
if Path(data).exists():
|
|
copy(data)
|
|
for directory in ['tmp', 'root', 'myconfig', 'home/nfs_download/rsession', 'server/db', 'config', 'etc']:
|
|
(root / directory).mkdir(parents=True, exist_ok=True)
|
|
os.chmod(root / 'tmp', 0o1777)
|
|
(root / 'root/.rtorrent.rc').touch()
|
|
(root / 'etc/passwd').write_text('root:x:0:0:root:/root:/sbin/nologin\n')
|
|
(root / 'etc/group').write_text('root:x:0:\n')
|
|
(root / 'etc/nsswitch.conf').write_text('passwd: files\ngroup: files\nhosts: files dns\nnetworks: files\n')
|
|
# Tumbleweed may select libraries outside the loader's default directories
|
|
# (for example zlib-ng-compat). Generate a cache for the exported files only.
|
|
(root / 'etc/ld.so.conf').write_text('\n'.join(sorted(library_directories)) + '\n')
|
|
subprocess.run(['ldconfig', '-r', str(root)], check=True)
|
|
for forbidden in ['bin/sh', 'bin/bash', 'bin/busybox', 'sbin/apk', 'usr/bin/apk',
|
|
'usr/bin/zypper', 'usr/bin/rpm', 'usr/bin/gcc',
|
|
'usr/bin/npm', 'usr/local/bin/npm', 'usr/bin/python3']:
|
|
assert not (root / forbidden).exists(), f'Unexpected runtime tool: {forbidden}'
|