From aba00ab3edb7706c0e5e584c07c132367bc62781 Mon Sep 17 00:00:00 2001 From: TakuyaYagam1 Date: Sun, 29 Nov 2026 15:40:57 +0300 Subject: [PATCH] fix(towercore): bind exported context to session - Force EXPORT_CONTEXT to use session->UE after both peer selector paths. - Strip tag 0x09 and 0x0b peer selectors in the protocol guard. --- towercore/Dockerfile.towercore | 9 ++++++--- towercore/guard.py | 12 ++++++++---- towercore/test_guard.py | 9 +++++++++ 3 files changed, 23 insertions(+), 7 deletions(-) diff --git a/towercore/Dockerfile.towercore b/towercore/Dockerfile.towercore index 062f198..31c9295 100644 --- a/towercore/Dockerfile.towercore +++ b/towercore/Dockerfile.towercore @@ -7,9 +7,12 @@ from pathlib import Path path = Path("/towercore") data = bytearray(path.read_bytes()) -original = bytes.fromhex("84 00 c2 90") -replacement = bytes.fromhex("25 10 00 00") -for offset in (0x24604, 0x2487C): +patches = ( + (0x23DC0, bytes.fromhex("d7 04 a0 10"), bytes.fromhex("10 00 65 de")), + (0x24604, bytes.fromhex("84 00 c2 90"), bytes.fromhex("25 10 00 00")), + (0x2487C, bytes.fromhex("84 00 c2 90"), bytes.fromhex("25 10 00 00")), +) +for offset, original, replacement in patches: actual = bytes(data[offset : offset + len(original)]) if actual != original: raise SystemExit(f"unexpected bytes at {offset:#x}: {actual.hex()}") diff --git a/towercore/guard.py b/towercore/guard.py index 55b10f7..29630ab 100755 --- a/towercore/guard.py +++ b/towercore/guard.py @@ -5,8 +5,11 @@ import contextlib import os HEADER_SIZE = 16 -TARGETED_READ_OPCODES = frozenset((0x21, 0x23)) -SUBSCRIBER_TAG = 0x0B +FILTERED_SELECTOR_TAGS = { + 0x21: frozenset((0x0B,)), + 0x23: frozenset((0x0B,)), + 0x40: frozenset((0x09, 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")) @@ -18,7 +21,8 @@ def sanitize_frame(frame: bytes) -> bytes: payload_size = int.from_bytes(frame[14:16], "little") if len(frame) != HEADER_SIZE + payload_size: raise ValueError("invalid frame length") - if frame[3] not in TARGETED_READ_OPCODES: + filtered_tags = FILTERED_SELECTOR_TAGS.get(frame[3]) + if filtered_tags is None: return frame payload = frame[HEADER_SIZE:] @@ -32,7 +36,7 @@ def sanitize_frame(frame: bytes) -> bytes: end = offset + 4 + size if end > len(payload): raise ValueError("truncated TLV value") - if tag == SUBSCRIBER_TAG: + if tag in filtered_tags: if sanitized is None: sanitized = bytearray(payload[:offset]) elif sanitized is not None: diff --git a/towercore/test_guard.py b/towercore/test_guard.py index 58cb362..ccbe8c1 100644 --- a/towercore/test_guard.py +++ b/towercore/test_guard.py @@ -36,6 +36,15 @@ class GuardTests(unittest.TestCase): ) self.assertEqual(module.sanitize_frame(request), frame(0x23, [])) + def test_strips_peer_selector_from_export_context(self) -> None: + module = importlib.import_module("guard") + request = frame( + 0x40, + [(0x09, b"peer-id"), (0x0B, b"victim01"), (0x0C, b"\x01\0\0\0")], + ) + expected = frame(0x40, [(0x0C, b"\x01\0\0\0")]) + self.assertEqual(module.sanitize_frame(request), expected) + def test_keeps_checker_read_by_message_ids(self) -> None: guard = importlib.util.find_spec("guard") self.assertIsNotNone(guard, "Towercore guard is not implemented")