fix(towercore): block cross-subscriber bulk reads
- Proxy TowerCore frames and reject opcode 0x23 requests containing subscriber selectors. - Preserve checker bulk reads that address authenticated message IDs.
This commit is contained in:
1 parent
be18145fa6
commit
ea9f241058
4 files changed
+163
-2
No files matched your search
@@ -0,0 +1,5 @@
|
||||
FROM python:3.12-slim@sha256:5b5fe805d367f8f5a359ded6ab2652b7f9a206453ee6ee789c0045ba6ec15016
|
||||
|
||||
COPY guard.py /guard.py
|
||||
USER 65534:65534
|
||||
ENTRYPOINT ["python", "/guard.py"]
|
||||
@@ -17,8 +17,8 @@ services:
|
||||
env_file:
|
||||
- path: ./config.env
|
||||
required: false
|
||||
ports:
|
||||
- "7250:7250"
|
||||
expose:
|
||||
- "7250"
|
||||
volumes:
|
||||
- towercore_mips_state:/var/lib/towercore
|
||||
healthcheck:
|
||||
@@ -32,5 +32,33 @@ services:
|
||||
max-size: "10k"
|
||||
max-file: "3"
|
||||
|
||||
guard:
|
||||
build:
|
||||
context: .
|
||||
dockerfile: Dockerfile.guard
|
||||
platform: linux/amd64
|
||||
restart: unless-stopped
|
||||
init: true
|
||||
depends_on:
|
||||
towercore:
|
||||
condition: service_healthy
|
||||
environment:
|
||||
BACKEND_HOST: towercore
|
||||
BACKEND_PORT: "7250"
|
||||
LISTEN_PORT: "7250"
|
||||
ports:
|
||||
- "7250:7250"
|
||||
cpus: "0.5"
|
||||
mem_limit: 64m
|
||||
pids_limit: 64
|
||||
read_only: true
|
||||
cap_drop: ["ALL"]
|
||||
security_opt: ["no-new-privileges:true"]
|
||||
healthcheck:
|
||||
test: ["CMD", "python", "-c", "import socket; socket.create_connection(('127.0.0.1', 7250), 2).close()"]
|
||||
interval: 10s
|
||||
timeout: 3s
|
||||
retries: 5
|
||||
|
||||
volumes:
|
||||
towercore_mips_state:
|
||||
Executable
+91
@@ -0,0 +1,91 @@
|
||||
#!/usr/bin/env python3
|
||||
|
||||
import asyncio
|
||||
import contextlib
|
||||
import os
|
||||
|
||||
HEADER_SIZE = 16
|
||||
BULK_READ_OPCODE = 0x23
|
||||
SUBSCRIBER_TAG = 0x0B
|
||||
BACKEND_HOST = os.environ.get("BACKEND_HOST", "towercore")
|
||||
BACKEND_PORT = int(os.environ.get("BACKEND_PORT", "7250"))
|
||||
LISTEN_PORT = int(os.environ.get("LISTEN_PORT", "7250"))
|
||||
|
||||
|
||||
def tlv_tags(payload: bytes) -> set[int]:
|
||||
tags: set[int] = set()
|
||||
offset = 0
|
||||
while offset < len(payload):
|
||||
if len(payload) - offset < 4:
|
||||
raise ValueError("truncated TLV header")
|
||||
tag = int.from_bytes(payload[offset : offset + 2], "little")
|
||||
size = int.from_bytes(payload[offset + 2 : offset + 4], "little")
|
||||
offset += 4
|
||||
if size > len(payload) - offset:
|
||||
raise ValueError("truncated TLV value")
|
||||
tags.add(tag)
|
||||
offset += size
|
||||
return tags
|
||||
|
||||
|
||||
def should_block(frame: bytes) -> bool:
|
||||
if len(frame) < HEADER_SIZE or frame[:2] != b"LT":
|
||||
raise ValueError("invalid frame header")
|
||||
payload_size = int.from_bytes(frame[14:16], "little")
|
||||
if len(frame) != HEADER_SIZE + payload_size:
|
||||
raise ValueError("invalid frame length")
|
||||
return frame[3] == BULK_READ_OPCODE and SUBSCRIBER_TAG in tlv_tags(frame[HEADER_SIZE:])
|
||||
|
||||
|
||||
async def client_frames(reader: asyncio.StreamReader, writer: asyncio.StreamWriter) -> None:
|
||||
while True:
|
||||
header = await reader.readexactly(HEADER_SIZE)
|
||||
payload_size = int.from_bytes(header[14:16], "little")
|
||||
payload = await reader.readexactly(payload_size)
|
||||
frame = header + payload
|
||||
if should_block(frame):
|
||||
return
|
||||
writer.write(frame)
|
||||
await writer.drain()
|
||||
|
||||
|
||||
async def copy_stream(reader: asyncio.StreamReader, writer: asyncio.StreamWriter) -> None:
|
||||
while data := await reader.read(65536):
|
||||
writer.write(data)
|
||||
await writer.drain()
|
||||
|
||||
|
||||
async def handle_client(client_reader: asyncio.StreamReader, client_writer: asyncio.StreamWriter) -> None:
|
||||
backend_writer = None
|
||||
try:
|
||||
backend_reader, backend_writer = await asyncio.open_connection(BACKEND_HOST, BACKEND_PORT)
|
||||
upstream = asyncio.create_task(client_frames(client_reader, backend_writer))
|
||||
downstream = asyncio.create_task(copy_stream(backend_reader, client_writer))
|
||||
done, pending = await asyncio.wait((upstream, downstream), return_when=asyncio.FIRST_COMPLETED)
|
||||
for task in pending:
|
||||
task.cancel()
|
||||
for task in done:
|
||||
with contextlib.suppress(asyncio.IncompleteReadError, ConnectionError, ValueError):
|
||||
task.result()
|
||||
if pending:
|
||||
await asyncio.gather(*pending, return_exceptions=True)
|
||||
except (ConnectionError, OSError):
|
||||
pass
|
||||
finally:
|
||||
client_writer.close()
|
||||
with contextlib.suppress(Exception):
|
||||
await client_writer.wait_closed()
|
||||
if backend_writer is not None:
|
||||
backend_writer.close()
|
||||
with contextlib.suppress(Exception):
|
||||
await backend_writer.wait_closed()
|
||||
|
||||
|
||||
async def main() -> None:
|
||||
server = await asyncio.start_server(handle_client, "0.0.0.0", LISTEN_PORT)
|
||||
async with server:
|
||||
await server.serve_forever()
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
asyncio.run(main())
|
||||
@@ -0,0 +1,37 @@
|
||||
import importlib
|
||||
import importlib.util
|
||||
import unittest
|
||||
|
||||
|
||||
def frame(opcode: int, fields: list[tuple[int, bytes]]) -> bytes:
|
||||
payload = b"".join(
|
||||
tag.to_bytes(2, "little") + len(value).to_bytes(2, "little") + value
|
||||
for tag, value in fields
|
||||
)
|
||||
return b"LT" + bytes((1, opcode)) + b"\0" * 10 + len(payload).to_bytes(2, "little") + payload
|
||||
|
||||
|
||||
class GuardTests(unittest.TestCase):
|
||||
def test_blocks_cross_subscriber_bulk_read(self) -> None:
|
||||
guard = importlib.util.find_spec("guard")
|
||||
self.assertIsNotNone(guard, "Towercore guard is not implemented")
|
||||
module = importlib.import_module("guard")
|
||||
request = frame(0x23, [(0x0B, b"victim01"), (0x0B, b"victim02")])
|
||||
self.assertTrue(module.should_block(request))
|
||||
|
||||
def test_allows_checker_bulk_read_by_message_ids(self) -> None:
|
||||
guard = importlib.util.find_spec("guard")
|
||||
self.assertIsNotNone(guard, "Towercore guard is not implemented")
|
||||
module = importlib.import_module("guard")
|
||||
request = frame(0x23, [(0x0D, b"\x05\0\0\0"), (0x0D, b"\x06\0\0\0")])
|
||||
self.assertFalse(module.should_block(request))
|
||||
|
||||
def test_allows_other_commands_with_subscriber_field(self) -> None:
|
||||
guard = importlib.util.find_spec("guard")
|
||||
self.assertIsNotNone(guard, "Towercore guard is not implemented")
|
||||
module = importlib.import_module("guard")
|
||||
self.assertFalse(module.should_block(frame(0x07, [(0x0B, b"lookup01")])))
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
unittest.main()
|
||||
Reference in new issue
Block a user