Datasets:
File size: 2,355 Bytes
af2930e | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 | 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)))
|