import argparse import hashlib import json import os from pathlib import Path import socket import subprocess parser = argparse.ArgumentParser() parser.add_argument('gold_sha256') parser.add_argument('git_blob') args = parser.parse_args() gold = Path('/workspace/executable') assert os.getuid() == 1000 assert gold.stat().st_uid == gold.stat().st_gid == 0 assert gold.stat().st_mode & 0o777 == 0o111 assert not os.access(gold, os.R_OK) and os.access(gold, os.X_OK) assert [name for _, name in socket.if_nameindex()] == ['lo'] sock = socket.socket() sock.settimeout(2) assert sock.connect_ex(('1.1.1.1', 443)) != 0 sock.close() git = ['git', '-C', '/workspace'] assert subprocess.run(git + ['rev-parse', '--verify', 'HEAD'], capture_output=True).returncode == 0 assert subprocess.run(git + ['show', 'HEAD:executable'], capture_output=True).returncode != 0 assert subprocess.run(git + ['checkout', '--', 'executable'], capture_output=True).returncode != 0 assert subprocess.run(git + ['cat-file', '-e', args.git_blob], capture_output=True).returncode != 0 history = subprocess.run(git + ['rev-list', '--all', '--objects', '--', 'executable'], capture_output=True) assert history.returncode == 0 and not history.stdout.strip() for directory, dirs, files in os.walk('/workspace'): dirs[:] = [name for name in dirs if name != '.git'] for name in files: path = Path(directory) / name if path == gold or not path.is_file() or not os.access(path, os.R_OK): continue with path.open('rb') as stream: digest = hashlib.sha256() for chunk in iter(lambda: stream.read(1024 * 1024), b''): digest.update(chunk) assert digest.hexdigest() != args.gold_sha256, str(path) try: process = subprocess.run([str(gold), '--help'], capture_output=True, timeout=10) text = (process.stdout + process.stderr).decode(errors='replace') assert process.returncode >= 0 and process.returncode not in (126, 127) assert not any(error in text for error in ('error while loading shared libraries', 'symbol lookup error')) launch = dict(exit_code=process.returncode, output=text[-1500:]) except subprocess.TimeoutExpired: launch = dict(started=True, help_timeout_seconds=10) print(json.dumps(dict(status='pass', uid=os.getuid(), network='none', executable_launch=launch)))