File size: 8,197 Bytes
5ca3c16
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
"""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)
            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()