From 70485a0ecf6b1ae9520deb4af0490fdb48643836 Mon Sep 17 00:00:00 2001 From: Dirk Engling Date: Sat, 28 Nov 2026 21:05:13 +0100 Subject: Prevent amplification attacks, only send id mis-match errors for sensible packets lengths --- src/ot_udp.c | 9 +++++++-- tests/integration/test_udp.py | 11 +++++++++++ 2 files changed, 18 insertions(+), 2 deletions(-) diff --git a/src/ot_udp.c b/src/ot_udp.c index 7a05b61..609089b 100644 --- a/src/ot_udp.c +++ b/src/ot_udp.c @@ -161,8 +161,13 @@ int handle_udp6(int64_t serversocket, struct ot_workstruct *ws) { match, return an error packet */ udp_make_connectionid(connid, remoteip, 1); if (memcmp(request + UDP_REQUEST_CONNECTION_ID_OFFSET, connid, sizeof(connid))) { - udp_send_error(serversocket, ws, "Connection ID missmatch.", remoteip, remoteport); - stats_issue_event(ws, EVENT_CONNID_MISSMATCH, FLAG_UDP, ws->reply_size); + static const char err_idmiss[] = "Connection ID missmatch."; + + /* Nobody has vouched for this address yet, so it is not sent more + than was sent in its name */ + if (byte_count >= UDP_REPLY_HEADER_SIZE + sizeof(err_idmiss)) + udp_send_error(serversocket, ws, err_idmiss, remoteip, remoteport); + stats_issue_event(ws, EVENT_CONNID_MISSMATCH, FLAG_UDP, byte_count); return 1; } } diff --git a/tests/integration/test_udp.py b/tests/integration/test_udp.py index 3afe4ef..b60bd56 100644 --- a/tests/integration/test_udp.py +++ b/tests/integration/test_udp.py @@ -44,6 +44,17 @@ class UdpProtocol(TrackerTestCase): self.assertEqual(action, 3, "action 3 is the error packet") self.assertIn(b"Connection ID missmatch", answer[8:]) + def test_a_wrong_connection_id_is_not_answered_with_more_than_was_sent(self): + """The sender's address is unverified at this point, so the error + must not be a way to throw bytes at somebody else.""" + scrape = struct.pack(">QII", 0, 2, 3) + self.hash_for("udpbadscrape") + answer = self.tracker.udp_raw(scrape) + self.assertIsNotNone(answer, "a whole scrape is long enough to be told") + self.assertLessEqual(len(answer), len(scrape)) + self.assertIsNone(self.tracker.udp_raw(scrape[:16])) + self.assertIsNone(self.tracker.udp_raw(scrape[: len(answer) - 1])) + self.assertIsNotNone(self.tracker.udp_raw(scrape[: len(answer)])) + def test_a_packet_shorter_than_the_header_is_ignored(self): self.assertIsNone(self.tracker.udp_raw(b"\x00" * 15)) -- cgit v1.3