| 1 | import errno, hashlib, json, os, socket, stat, sys |
| 2 | |
| 3 | plan = json.loads(sys.argv[1]) |
| 4 | blocked = {'ENOENT', 'EACCES', 'EPERM'} |
| 5 | def category(error): |
| 6 | return errno.errorcode.get(error.errno, 'UNKNOWN') |
| 7 | |
| 8 | results = [] |
| 9 | for target in plan: |
| 10 | path = target['path'] |
| 11 | ops = {} |
| 12 | same = None |
| 13 | try: |
| 14 | fd = os.open(path, os.O_RDONLY | os.O_NONBLOCK) |
| 15 | try: |
| 16 | info = os.fstat(fd) |
| 17 | if not stat.S_ISREG(info.st_mode) or info.st_size > 4096: |
| 18 | ops['open'] = 'unexpected-kind-or-size' |
| 19 | else: |
| 20 | same = hashlib.sha256(os.read(fd, 4097)).hexdigest() == target['sha256'] |
| 21 | ops['open'] = 'sentinel-exposed' if same else 'different-file' |
| 22 | finally: |
| 23 | os.close(fd) |
| 24 | except OSError as e: |
| 25 | ops['open'] = category(e) |
| 26 | for name, action in [('stat', lambda: os.stat(path)), ('readlink', lambda: os.readlink(path)), |
| 27 | ('list', lambda: os.listdir(os.path.dirname(path)))]: |
| 28 | try: |
| 29 | value = action() |
| 30 | if name == 'list' and os.path.basename(path) not in value: |
| 31 | ops[name] = 'ENOENT' |
| 32 | else: |
| 33 | ops[name] = 'different-file' if same is False else 'visible' |
| 34 | except OSError as e: |
| 35 | ops[name] = 'different-file' if same is False and e.errno == errno.EINVAL else category(e) |
| 36 | # readlink on a regular sentinel is EINVAL, not evidence of isolation. |
| 37 | safe = all(value in blocked or value == 'different-file' for value in ops.values()) |
| 38 | results.append(dict(name=target['name'], operations=ops, safe=safe)) |
| 39 | |
| 40 | sides = {} |
| 41 | for path in ['/proc/self/fd', '/dev/fd', '/proc/1/fd']: |
| 42 | try: |
| 43 | entries = os.listdir(path) |
| 44 | safe = True |
| 45 | for entry in entries: |
| 46 | if not entry.isdigit(): |
| 47 | safe = False |
| 48 | continue |
| 49 | try: |
| 50 | info = os.stat(path + '/' + entry) |
| 51 | # No inherited mount, credential, journal, socket or helper FD. |
| 52 | if int(entry) > 2 or not stat.S_ISFIFO(info.st_mode): safe = False |
| 53 | except OSError as e: |
| 54 | if e.errno not in (errno.ENOENT, errno.EACCES, errno.EPERM): safe = False |
| 55 | sides[path] = 'no-inherited-fds' if safe else 'inherited-fd-exposed' |
| 56 | except OSError as e: |
| 57 | sides[path] = category(e) |
| 58 | try: |
| 59 | same_namespace = os.readlink('/proc/1/ns/mnt') == os.readlink('/proc/self/ns/mnt') |
| 60 | same_root = os.path.samefile('/proc/1/root', '/') |
| 61 | sides['/proc/1/root'] = 'same-sandbox-root' if same_namespace and same_root else 'different-mount-namespace' |
| 62 | except OSError as e: |
| 63 | sides['/proc/1/root'] = category(e) |
| 64 | |
| 65 | # Only a rejected CONNECT is accepted. An arbitrary timeout or reset is unknown. |
| 66 | for name, family, address in [('egress-relay', socket.AF_INET, ('127.0.0.1', 18080)), |
| 67 | ('egress-socket', socket.AF_UNIX, '/egress/proxy.sock')]: |
| 68 | try: |
| 69 | with socket.socket(family, socket.SOCK_STREAM) as s: |
| 70 | s.settimeout(2) |
| 71 | s.connect(address) |
| 72 | s.sendall(b'CONNECT 127.0.0.1:1 HTTP/1.1\r\nHost: 127.0.0.1:1\r\n\r\n') |
| 73 | response = s.recv(4096) |
| 74 | sides[name] = 'policy-denied' if response.startswith(b'HTTP/1.1 403 ') else 'unverified-response' |
| 75 | except OSError as e: |
| 76 | sides[name] = category(e) |
| 77 | try: |
| 78 | with socket.socket(socket.AF_UNIX, socket.SOCK_STREAM) as s: |
| 79 | s.settimeout(2) |
| 80 | s.connect('/tool/equipo.sock') |
| 81 | s.sendall(b'{"jsonrpc":"2.0","id":"isolation","method":"resources/read","params":{"uri":"file:///home/runner/equipo-read-sentinel"}}\n') |
| 82 | response = json.loads(s.recv(4096)) |
| 83 | sides['equipo-socket'] = 'policy-denied' if response.get('id') == 'isolation' and response.get('error', {}).get('code') == -32602 and 'result' not in response else 'unverified-response' |
| 84 | except OSError as e: |
| 85 | sides['equipo-socket'] = category(e) |
| 86 | except (ValueError, KeyError): |
| 87 | sides['equipo-socket'] = 'unverified-response' |
| 88 | safe = all(r['safe'] for r in results) and all(v in blocked or v in ('no-inherited-fds', 'policy-denied', 'same-sandbox-root') for v in sides.values()) |
| 89 | print(json.dumps(dict(uid=os.getuid(), targets=results, side_doors=sides, safe=safe)), flush=True) |
| 90 | |