Migrate distroless rTorrent runtime to openSUSE Tumbleweed
Build and Push Docker Image / build (push) Successful in 1m23s

This commit is contained in:
Ketan Patel committed 2026-11-01 21:45:11 -05:00
1 parent 18c861f52c
commit 5295c67f77
4 files changed
+84 -25

No files matched your search

+27 -12
View File
@@ -1,7 +1,8 @@
#!/usr/bin/env python3
"""Assemble an Alpine-derived, shell-free runtime from an explicit ELF allowlist."""
"""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
@@ -10,6 +11,7 @@ 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))
@@ -31,23 +33,30 @@ def copy(source):
else:
shutil.copy2(source, target)
for executable in sys.argv[2:]:
result = subprocess.run(['lddtreepax', '-l', executable], check=True, text=True, capture_output=True)
paths = result.stdout.splitlines()
if not paths or any(not p.startswith('/') or not Path(p).exists() for p in paths):
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))
# musl uses this file for nonstandard library directories (for example Lua).
for search_path in Path('/etc').glob('ld-musl-*.path'):
copy(search_path)
for executable in sys.argv[2:]:
copy_elf(executable)
for data in ['/etc/ssl/certs/ca-certificates.crt', '/etc/ssl/cert.pem',
'/etc/ssl/openssl.cnf', '/usr/lib/ossl-modules', '/usr/share/zoneinfo',
'/etc/terminfo', '/usr/share/terminfo', '/etc/services', '/etc/protocols',
'/etc/alpine-release', '/etc/os-release']:
# 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']:
@@ -56,6 +65,12 @@ 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}'