"""Trusted root controller in a disposable VM; generated Java runs as an unprivileged UID.""" import ctypes import json import os import resource import subprocess import sys import urllib.request import urllib.error import re import termios import base64 import hashlib import select import time def serial_request(serial, nonce, profile): """One bounded upload; each canonical UART line stays below Linux's 4096-byte limit.""" raw = serial.readline(4097) if not raw or len(raw.encode()) > 4096: raise EOFError() request = json.loads(raw) if request.get('type') != 'begin': return request, None if (profile != 'pdfbox-3.0.8' or request.get('method') != 'POST' or request.get('path') != '/api/pdf/analyze' or type(request.get('body_bytes')) is not int or not 1 <= request['body_bytes'] <= 180000 or type(request.get('chunks')) is not int or not 1 <= request['chunks'] <= 100 or not re.fullmatch(r'[a-f0-9]{64}', request.get('body_sha256', ''))): raise ValueError('UPLOAD_NOT_ALLOWED') deadline = time.monotonic() + 30 def ack(index): print('ZKCODING_RPC_ACK=' + json.dumps({'id':request['id'], 'index':index, 'nonce':nonce}), flush=True) def frame(): remaining = deadline - time.monotonic() if remaining <= 0 or not select.select([serial], [], [], remaining)[0]: raise ValueError('UPLOAD_DEADLINE') line = serial.readline(4097) if not line or len(line.encode()) > 4096: raise ValueError('UPLOAD_FRAME_LIMIT') value = json.loads(line) if value.get('id') != request['id']: raise ValueError('UPLOAD_ID_MISMATCH') return value payload = bytearray() ack(-1) for index in range(request['chunks']): chunk = frame() if set(chunk) != {'type','id','index','data'} or chunk['type'] != 'chunk' or chunk['index'] != index: raise ValueError('UPLOAD_SEQUENCE') decoded = base64.b64decode(chunk['data'], validate=True) if not 1 <= len(decoded) <= 2048 or len(payload) + len(decoded) > request['body_bytes']: raise ValueError('UPLOAD_BODY_LIMIT') payload.extend(decoded) ack(index) commit = frame() if (set(commit) != {'type','id'} or commit['type'] != 'commit' or len(payload) != request['body_bytes'] or hashlib.sha256(payload).hexdigest() != request['body_sha256']): raise ValueError('UPLOAD_DIGEST_MISMATCH') if not isinstance(json.loads(payload), dict): raise ValueError('UPLOAD_JSON_INVALID') return request, bytes(payload) def drop(uid): def apply(): resource.setrlimit(resource.RLIMIT_CORE, (0, 0)) resource.setrlimit(resource.RLIMIT_NOFILE, (256, 256)) resource.setrlimit(resource.RLIMIT_NPROC, (128, 128)) resource.setrlimit(resource.RLIMIT_FSIZE, (32 * 1024 * 1024,) * 2) os.setgroups([]) os.setgid(uid) os.setuid(uid) if ctypes.CDLL(None).prctl(38, 1, 0, 0, 0): os._exit(125) return apply def main(): with open('/root/job.json') as f: job = json.load(f) config = job['config'] result = {'nonce': job['nonce']} os.chown('/work', 65533, 65533) environment = {'PATH': '/usr/bin:/bin', 'LANG': 'C.UTF-8', 'HOME': '/tmp'} profile = config.get('dependency_profile', 'jdk') if profile not in {'jdk', 'pdfbox-3.0.8'}: raise ValueError('UNAPPROVED_DEPENDENCY_PROFILE') classpath = '/work/classes' if profile == 'pdfbox-3.0.8': jar = '/approved/pdfbox-app-3.0.8.jar' with open(jar, 'rb') as dependency: if hashlib.sha512(dependency.read()).hexdigest() != config['dependency_sha512']: raise ValueError('DEPENDENCY_DIGEST_MISMATCH') classpath += ':' + jar with open('/runner/probe-config.json', 'w') as protected: json.dump(config, protected) os.chown('/runner/probe-config.json', 65534, 65534) os.chmod('/runner/probe-config.json', 0o400) service = None try: def probe(stage, uid): env = dict(environment) if stage != 'build': env['ZKCODING_PROBE_CONFIG_FILE'] = '/runner/probe-config.json' else: env['ZKCODING_DEPENDENCY_PROFILE'] = profile p = subprocess.run(['python3', '/runner/probe.py', stage], env=env, preexec_fn=drop(uid), stdin=subprocess.DEVNULL, stdout=subprocess.PIPE, stderr=subprocess.DEVNULL, timeout=150 if profile=='pdfbox-3.0.8' else 110) if p.returncode or len(p.stdout) > 65536: raise RuntimeError('PROBE_FAILED') return json.loads(p.stdout) result['build'] = probe('build', 65533) if result['build']['passed']: service = subprocess.Popen(['java', '-Xmx128m', '-Djava.io.tmpdir=/tmp', '-cp', classpath, config['main_class']], env=environment, preexec_fn=drop(65532), stdin=subprocess.DEVNULL, stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL) if job.get('interactive'): expected=job.get('tested_classes_digest','') count=job.get('tested_cases') if not re.fullmatch(r'[a-f0-9]{64}',expected) or type(count) is not int or not 1 <= count <= 40: raise ValueError('TEST_EVIDENCE_REQUIRED') result['test']={'passed':result['build']['classes_digest']==expected, 'classes_digest':result['build']['classes_digest'],'cases':count,'passed_cases':count, 'origin':'separate-verified-vm'} else: result['test'] = probe('test', 65534) result['preview'] = probe('preview', 65534) if job.get('interactive') and result['test']['passed'] and result['preview']['passed']: # Linux PID1's inherited stdin is not guaranteed to be the serial console. # Open the protected UART explicitly; disable echo so RPC frames stay distinct. serial = open('/dev/ttyS0', 'r', buffering=1) attrs = termios.tcgetattr(serial.fileno()) attrs[3] = (attrs[3] | termios.ICANON) & ~termios.ECHO attrs[0] |= termios.ICRNL termios.tcsetattr(serial.fileno(), termios.TCSANOW, attrs) print('ZKCODING_VM_READY=' + json.dumps(result), flush=True) class NoRedirect(urllib.request.HTTPRedirectHandler): def redirect_request(self, *args): return None opener = urllib.request.build_opener(urllib.request.ProxyHandler({}), NoRedirect()) for _ in range(1000): try: request, upload = serial_request(serial, job['nonce'], profile) if request.get('stop'): break path, method = request['path'], request['method'] if method not in {'GET','POST'} or not re.fullmatch(r'/(?:[A-Za-z0-9_/?=&.+-]|%[0-9A-Fa-f]{2}){0,500}', path) or path.startswith('//') or '..' in path: raise ValueError() r = urllib.request.Request('http://127.0.0.1:8080' + path, method=method, data=upload if upload is not None else b'' if method == 'POST' else None, headers={'Authorization': request.get('authorization', ''), 'Content-Type':'application/json'}) try: response = opener.open(r, timeout=5) except urllib.error.HTTPError as exc: response = exc with response: body = response.read(65537) if len(body) > 65536: raise ValueError() reply = {'id':request['id'],'status':response.code,'body':json.loads(body),'nonce':job['nonce']} except EOFError: break except Exception: reply = {'id': request.get('id') if 'request' in locals() else None, 'error':'PREVIEW_REQUEST_FAILED','nonce':job['nonce']} print('ZKCODING_RPC=' + json.dumps(reply), flush=True) except Exception: result['error'] = 'VM_PROBE_UNKNOWN' finally: if service: service.kill() service.wait(timeout=5) print('ZKCODING_VM_RESULT=' + json.dumps(result), flush=True) subprocess.run(['/sbin/poweroff', '-f'], check=False) if __name__ == '__main__': main()