fix(towercore): bind reads to authenticated UE

- Strip attacker-controlled subscriber selectors from NAS read and inventory requests.
- Preserve the existing session-owned UE path used by checker requests.
This commit is contained in:
TakuyaYagam1 committed 2026-11-29 15:35:19 +03:00
1 parent d90e63eb62
commit 7b44899b6f
2 files changed
+49 -42

No files matched your search

+29 -29
View File
@@ -5,42 +5,45 @@ import contextlib
import os
HEADER_SIZE = 16
READ_OPCODE = 0x21
BULK_READ_OPCODE = 0x23
TARGETED_READ_OPCODES = frozenset((0x21, 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) -> list[int]:
tags: list[int] = []
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.append(tag)
offset += size
return tags
def should_block(frame: bytes) -> bool:
def sanitize_frame(frame: bytes) -> bytes:
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")
tags = tlv_tags(frame[HEADER_SIZE:])
opcode = frame[3]
return (
(opcode == BULK_READ_OPCODE and SUBSCRIBER_TAG in tags)
or (opcode == READ_OPCODE and tags.count(SUBSCRIBER_TAG) > 1)
)
if frame[3] not in TARGETED_READ_OPCODES:
return frame
payload = frame[HEADER_SIZE:]
offset = 0
sanitized: bytearray | None = None
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")
end = offset + 4 + size
if end > len(payload):
raise ValueError("truncated TLV value")
if tag == SUBSCRIBER_TAG:
if sanitized is None:
sanitized = bytearray(payload[:offset])
elif sanitized is not None:
sanitized.extend(payload[offset:end])
offset = end
if sanitized is None:
return frame
header = bytearray(frame[:HEADER_SIZE])
header[14:16] = len(sanitized).to_bytes(2, "little")
return bytes(header + sanitized)
async def client_frames(reader: asyncio.StreamReader, writer: asyncio.StreamWriter) -> None:
@@ -48,10 +51,7 @@ async def client_frames(reader: asyncio.StreamReader, writer: asyncio.StreamWrit
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)
writer.write(sanitize_frame(header + payload))
await writer.drain()
+20 -13
View File
@@ -12,36 +12,43 @@ def frame(opcode: int, fields: list[tuple[int, bytes]]) -> bytes:
class GuardTests(unittest.TestCase):
def test_allows_single_subscriber_inventory(self) -> None:
module = importlib.import_module("guard")
request = frame(0x23, [(0x0B, b"victim01")])
self.assertFalse(module.should_block(request))
def test_blocks_duplicate_subscriber_override_on_read(self) -> None:
def test_strips_single_subscriber_selector_from_read(self) -> None:
module = importlib.import_module("guard")
request = frame(
0x21,
[
(0x0B, b"owned-ue"),
(0x0B, b"victimue"),
(0x0B, b"victim01"),
(0x0D, b"\x01\0\0\0"),
(0x0E, b"\xff\xff\xff\xff"),
],
)
self.assertTrue(module.should_block(request))
expected = frame(
0x21,
[(0x0D, b"\x01\0\0\0"), (0x0E, b"\xff\xff\xff\xff")],
)
self.assertEqual(module.sanitize_frame(request), expected)
def test_allows_checker_bulk_read_by_message_ids(self) -> None:
def test_strips_all_subscriber_selectors_from_inventory(self) -> None:
module = importlib.import_module("guard")
request = frame(
0x23,
[(0x0B, b"owned-ue"), (0x0B, b"victimue")],
)
self.assertEqual(module.sanitize_frame(request), frame(0x23, []))
def test_keeps_checker_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))
self.assertEqual(module.sanitize_frame(request), request)
def test_allows_other_commands_with_subscriber_field(self) -> None:
def test_keeps_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")])))
request = frame(0x07, [(0x0B, b"lookup01")])
self.assertEqual(module.sanitize_frame(request), request)
if __name__ == "__main__":