H5: Security review, and fixes for connection-slot exhaustion
docs/SECURITY_REVIEW.md lists the scope, nine findings and what was done. - High, fixed: a peer that connects and stops answering kept its ENet slot for up to 15 s after being turned away, so one address could fill every slot. Flood refusals now disconnect at once (reason in the disconnect data, which NetClient now honours for REASON_BUSY), peers told to leave get 1 s to acknowledge, and when slots run short the longest-waiting silent guest makes room. - Medium, mitigated: ENet holds half-open slots for spoofed CONNECTs that the script never sees. The host now has 512 slots (NetServer.peer_slots). - Medium, fixed: SourceLimiter, handshakes per source and the list server's per-address cap count an IPv6 /64 as one source. - Low, fixed: CommandLog refuses a header with non-numeric fields instead of raising errors. - tools/godot.sh unsets DISPLAY and WAYLAND_DISPLAY so the engine's error alerts can never reach the host desktop. - A test fixture's 10.0.0.9 is now the documentation address 192.0.2.9. Tests: 4 new handshake tests with real non-acknowledging ENet peers, test_source_limiter (3), a CommandLog type test. make check, make fuzz, make client-check, make discovery-check and make soak-selfcheck pass. Closes #58 Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
@@ -92,7 +92,7 @@ func poll(now_msec: int = -1) -> void:
|
||||
var data: int = int(event[2])
|
||||
var reason: int = close_reason
|
||||
if reason == NetProtocol.REASON_NONE:
|
||||
reason = data if data > NetProtocol.REASON_NONE and data <= NetProtocol.REASON_KICKED \
|
||||
reason = data if NetProtocol.is_server_reason(data) \
|
||||
else (NetProtocol.REASON_NO_ANSWER if state == CONNECTING else NetProtocol.REASON_LOST)
|
||||
_close(reason)
|
||||
if state == CONNECTING and now - _since_msec >= connect_timeout_msec:
|
||||
|
||||
@@ -85,6 +85,12 @@ static func clean_name(raw: String, max_chars: int = NAME_MAX_CHARS, max_bytes:
|
||||
return kept if not kept.is_empty() else fallback
|
||||
|
||||
|
||||
## True for a reason a server sends when it ends a connection, which a client may take
|
||||
## from the disconnect's data when the REJECT before it was lost or never sent.
|
||||
static func is_server_reason(reason: int) -> bool:
|
||||
return (reason > REASON_NONE and reason <= REASON_KICKED) or reason == REASON_BUSY
|
||||
|
||||
|
||||
static func encode_hello(player_name: String, version: int = VERSION) -> PackedByteArray:
|
||||
var name_bytes: PackedByteArray = clean_name(player_name).to_utf8_buffer()
|
||||
var out: StreamPeerBuffer = _writer()
|
||||
|
||||
+78
-11
@@ -5,6 +5,13 @@
|
||||
## A connection is a guest until its HELLO is accepted. Guests can't do anything else,
|
||||
## get one try, and are dropped if they stay silent past the handshake timeout. Player
|
||||
## handles are positive, count up from 1 and are never reused while the server runs.
|
||||
##
|
||||
## ENet has a fixed number of peer slots (the player limit plus a few), and a peer that
|
||||
## stops answering keeps its slot until ENet times it out. So no slot is left to a
|
||||
## silent stranger for long (H5): a connection turned away for flooding is dropped at
|
||||
## once, a peer told to leave gets `leave_grace_msec` to acknowledge before it is cut
|
||||
## off, and when the slots run short the guest that has waited longest for its HELLO
|
||||
## makes room for the newcomer. An honest client says HELLO within one round trip.
|
||||
class_name NetServer
|
||||
extends RefCounted
|
||||
|
||||
@@ -32,12 +39,26 @@ var bad_packets: int = 0
|
||||
var connect_limiter: SourceLimiter = SourceLimiter.new(32, 10000, 4096)
|
||||
## Connections from one address still in the handshake at once.
|
||||
var max_guests_per_address: int = 8
|
||||
## ENet peer slots to create at least. ENet takes a slot for every CONNECT it is sent,
|
||||
## before the sender has proved it receives at its address, and holds it for ENet's
|
||||
## own timeout without ever reporting it here, so spoofed CONNECTs can only be outlasted,
|
||||
## not seen. Headroom makes that take hundreds of packets every few seconds instead of
|
||||
## a few dozen (H5). Never fewer than the player limit plus 8. Set before listen().
|
||||
var peer_slots: int = 512
|
||||
## Milliseconds a peer being turned away keeps its slot while the notice is delivered.
|
||||
var leave_grace_msec: int = 1000
|
||||
## Silent guests dropped to make room for newcomers, and peers cut off after their
|
||||
## grace, so far.
|
||||
var evicted: int = 0
|
||||
|
||||
var _host: ENetConnection = null
|
||||
## Peers that haven't finished the handshake: ENetPacketPeer -> msec they connected.
|
||||
var _guests: Dictionary = {}
|
||||
## Peers being turned away: ENetPacketPeer -> the reason, until ENet reports them gone.
|
||||
## Peers being turned away: ENetPacketPeer -> {"reason", "since"}, until ENet reports
|
||||
## them gone or their grace runs out.
|
||||
var _leaving: Dictionary = {}
|
||||
## ENet peer slots on the host.
|
||||
var _capacity: int = 0
|
||||
## Handle -> {"peer": ENetPacketPeer, "name": String}.
|
||||
var _players: Dictionary = {}
|
||||
## ENetPacketPeer -> handle.
|
||||
@@ -52,8 +73,9 @@ var _packets_from: Dictionary = {}
|
||||
func listen(port: int, address: String = "*") -> Error:
|
||||
close()
|
||||
var host: ENetConnection = ENetConnection.new()
|
||||
# Room for a few guests beyond the player limit, so a full server can say so.
|
||||
var error: Error = host.create_host_bound(address, port, max_players + 8, NetProtocol.CHANNEL_COUNT)
|
||||
# Room for guests beyond the player limit, so a full server can say so.
|
||||
_capacity = clampi(maxi(peer_slots, max_players + 8), 1, 4095)
|
||||
var error: Error = host.create_host_bound(address, port, _capacity, NetProtocol.CHANNEL_COUNT)
|
||||
if error != OK:
|
||||
return error
|
||||
_host = host
|
||||
@@ -77,6 +99,11 @@ func guest_count() -> int:
|
||||
return _guests.size()
|
||||
|
||||
|
||||
## Peers told to leave that haven't gone yet.
|
||||
func leaving_count() -> int:
|
||||
return _leaving.size()
|
||||
|
||||
|
||||
func handles() -> Array[int]:
|
||||
var result: Array[int] = []
|
||||
for handle: int in _players:
|
||||
@@ -110,16 +137,23 @@ func poll(now_msec: int = -1) -> void:
|
||||
peer.set_timeout(NetProtocol.PEER_TIMEOUT_LIMIT, peer_timeout_min_msec, peer_timeout_max_msec)
|
||||
var address: String = peer.get_remote_address()
|
||||
if not connect_limiter.allow(address, now) or _guests_from(address) >= max_guests_per_address:
|
||||
_refuse(peer, NetProtocol.REASON_BUSY)
|
||||
# A flood: the reason goes only as the disconnect's data, and the
|
||||
# slot is free again at once.
|
||||
events.append({"type": "refused", "reason": NetProtocol.REASON_BUSY})
|
||||
peer.peer_disconnect_now(NetProtocol.REASON_BUSY)
|
||||
continue
|
||||
_guests[peer] = now
|
||||
_guests[peer] = {"since": now, "source": SourceLimiter.source_of(address)}
|
||||
_make_room(peer)
|
||||
ENetConnection.EVENT_DISCONNECT:
|
||||
_gone(peer, int(event[2]))
|
||||
ENetConnection.EVENT_RECEIVE:
|
||||
_received(peer, int(event[3]), peer.get_packet())
|
||||
for peer: ENetPacketPeer in _guests.keys():
|
||||
if now - int(_guests[peer]) >= handshake_timeout_msec:
|
||||
_refuse(peer, NetProtocol.REASON_HANDSHAKE_TIMEOUT)
|
||||
if now - int(_guests[peer]["since"]) >= handshake_timeout_msec:
|
||||
_refuse(peer, NetProtocol.REASON_HANDSHAKE_TIMEOUT, now)
|
||||
for peer: ENetPacketPeer in _leaving.keys():
|
||||
if now - int(_leaving[peer]["since"]) >= leave_grace_msec:
|
||||
_cut_off(peer)
|
||||
|
||||
|
||||
## Sends `bytes` to a player. Returns false if they aren't connected.
|
||||
@@ -139,7 +173,7 @@ func kick(handle: int, reason: int = NetProtocol.REASON_KICKED) -> bool:
|
||||
events.append({"type": "left", "handle": handle, "name": name_of(handle), "reason": reason})
|
||||
_players.erase(handle)
|
||||
_handle_of.erase(peer)
|
||||
_leaving[peer] = reason
|
||||
_leaving[peer] = {"reason": reason, "since": Time.get_ticks_msec()}
|
||||
peer.send(NetProtocol.CHANNEL_CONTROL, NetProtocol.encode_reject(reason), ENetPacketPeer.FLAG_RELIABLE)
|
||||
peer.peer_disconnect_later(reason)
|
||||
return true
|
||||
@@ -209,9 +243,9 @@ func _received(peer: ENetPacketPeer, channel: int, bytes: PackedByteArray) -> vo
|
||||
|
||||
|
||||
## Turns a guest away: tells it why, then disconnects once that has been delivered.
|
||||
func _refuse(peer: ENetPacketPeer, reason: int) -> void:
|
||||
func _refuse(peer: ENetPacketPeer, reason: int, now: int = -1) -> void:
|
||||
_guests.erase(peer)
|
||||
_leaving[peer] = reason
|
||||
_leaving[peer] = {"reason": reason, "since": now if now >= 0 else Time.get_ticks_msec()}
|
||||
events.append({"type": "refused", "reason": reason})
|
||||
peer.send(NetProtocol.CHANNEL_CONTROL, NetProtocol.encode_reject(reason), ENetPacketPeer.FLAG_RELIABLE)
|
||||
peer.peer_disconnect_later(reason)
|
||||
@@ -238,14 +272,47 @@ func _any_active(peers: Array) -> bool:
|
||||
return false
|
||||
|
||||
|
||||
## Guests from the same source as `address` (an IPv6 address counts with its /64).
|
||||
func _guests_from(address: String) -> int:
|
||||
var source: String = SourceLimiter.source_of(address)
|
||||
var count: int = 0
|
||||
for peer: ENetPacketPeer in _guests:
|
||||
if peer.get_remote_address() == address:
|
||||
if _guests[peer]["source"] == source:
|
||||
count += 1
|
||||
return count
|
||||
|
||||
|
||||
## Drops a peer that was told to leave and hasn't acknowledged, freeing its slot. The
|
||||
## reason goes again as the disconnect's data.
|
||||
func _cut_off(peer: ENetPacketPeer) -> void:
|
||||
var reason: int = int(_leaving[peer]["reason"])
|
||||
_leaving.erase(peer)
|
||||
evicted += 1
|
||||
if peer.is_active():
|
||||
peer.peer_disconnect_now(reason)
|
||||
|
||||
|
||||
## Keeps a slot free for the next connection: when the newest guest took the last but
|
||||
## one, peers already told to leave go first, then the guest that has waited longest.
|
||||
func _make_room(newest: ENetPacketPeer) -> void:
|
||||
if _guests.size() + _players.size() + _leaving.size() < _capacity - 1:
|
||||
return
|
||||
for peer: ENetPacketPeer in _leaving.keys():
|
||||
_cut_off(peer)
|
||||
if _guests.size() + _players.size() < _capacity - 1:
|
||||
return
|
||||
var oldest: ENetPacketPeer = null
|
||||
for peer: ENetPacketPeer in _guests:
|
||||
if peer != newest and (oldest == null or int(_guests[peer]["since"]) < int(_guests[oldest]["since"])):
|
||||
oldest = peer
|
||||
if oldest == null:
|
||||
return
|
||||
_guests.erase(oldest)
|
||||
evicted += 1
|
||||
events.append({"type": "refused", "reason": NetProtocol.REASON_HANDSHAKE_TIMEOUT})
|
||||
oldest.peer_disconnect_now(NetProtocol.REASON_HANDSHAKE_TIMEOUT)
|
||||
|
||||
|
||||
## `wanted`, or `wanted` with a number after it if someone here already has that name.
|
||||
func _unique_name(wanted: String) -> String:
|
||||
var taken: Dictionary = {}
|
||||
|
||||
@@ -3,6 +3,10 @@
|
||||
## the list server) so a flood from one address is mostly ignored. Memory is bounded:
|
||||
## once `max_sources` addresses have been seen in a window, new ones are refused
|
||||
## until the next window.
|
||||
##
|
||||
## Limits count sources, not addresses: an IPv4 address is one source, and an IPv6
|
||||
## address counts with the rest of its /64, since one ordinary connection is handed a
|
||||
## whole /64 and could otherwise pass for millions of senders (H5).
|
||||
class_name SourceLimiter
|
||||
extends RefCounted
|
||||
|
||||
@@ -25,6 +29,7 @@ func _init(packets_per_window: int = 20, window: int = 1000, sources: int = 4096
|
||||
## True if a packet from `address` may be handled now. `now_msec` is for tests.
|
||||
func allow(address: String, now_msec: int = -1) -> bool:
|
||||
var now: int = now_msec if now_msec >= 0 else Time.get_ticks_msec()
|
||||
address = source_of(address)
|
||||
if _window_start < 0 or now - _window_start >= window_msec:
|
||||
_window_start = now
|
||||
_counts.clear()
|
||||
@@ -42,3 +47,55 @@ func allow(address: String, now_msec: int = -1) -> bool:
|
||||
## Addresses being counted in the current window.
|
||||
func tracked() -> int:
|
||||
return _counts.size()
|
||||
|
||||
|
||||
## The source an address counts as: an IPv4 address (also when written IPv4-mapped,
|
||||
## "::ffff:192.0.2.1") as itself, an IPv6 address as its /64 prefix ("2001:db8:0:1::/64"),
|
||||
## and anything that isn't an IP address unchanged.
|
||||
static func source_of(address: String) -> String:
|
||||
var text: String = address.strip_edges()
|
||||
var zone: int = text.find("%")
|
||||
if zone >= 0:
|
||||
text = text.substr(0, zone)
|
||||
if not text.contains(":"):
|
||||
return text
|
||||
var lower: String = text.to_lower()
|
||||
if lower.begins_with("::ffff:") and lower.contains("."):
|
||||
return lower.substr(7)
|
||||
var groups: PackedStringArray = _ipv6_groups(lower)
|
||||
if groups.is_empty():
|
||||
return address
|
||||
return "%s:%s:%s:%s::/64" % [groups[0], groups[1], groups[2], groups[3]]
|
||||
|
||||
|
||||
## The eight groups of an IPv6 address, each without leading zeros, or empty if it
|
||||
## doesn't parse.
|
||||
static func _ipv6_groups(text: String) -> PackedStringArray:
|
||||
var halves: PackedStringArray = text.split("::")
|
||||
if halves.size() > 2:
|
||||
return PackedStringArray()
|
||||
var head: PackedStringArray = halves[0].split(":", false) if not halves[0].is_empty() else PackedStringArray()
|
||||
var tail: PackedStringArray = PackedStringArray()
|
||||
if halves.size() == 2 and not halves[1].is_empty():
|
||||
tail = halves[1].split(":", false)
|
||||
# An embedded IPv4 tail ("::1.2.3.4") counts as two groups; only the prefix matters.
|
||||
var parts: int = head.size() + tail.size()
|
||||
if not tail.is_empty() and tail[tail.size() - 1].contains("."):
|
||||
parts += 1
|
||||
elif tail.is_empty() and not head.is_empty() and head[head.size() - 1].contains("."):
|
||||
parts += 1
|
||||
if (halves.size() == 1 and parts != 8) or parts > 8:
|
||||
return PackedStringArray()
|
||||
var groups: PackedStringArray = PackedStringArray()
|
||||
groups.append_array(head)
|
||||
for _index: int in 8 - parts:
|
||||
groups.append("0")
|
||||
groups.append_array(tail)
|
||||
for index: int in mini(groups.size(), 4):
|
||||
var group: String = groups[index]
|
||||
if group.is_empty() or group.length() > 4 or not group.is_valid_hex_number():
|
||||
return PackedStringArray()
|
||||
groups[index] = "%x" % group.hex_to_int()
|
||||
if groups.size() < 4:
|
||||
return PackedStringArray()
|
||||
return groups
|
||||
|
||||
@@ -137,7 +137,9 @@ static func from_bytes(bytes: PackedByteArray) -> CommandLog:
|
||||
var result: CommandLog = CommandLog.new(str(head["seed"]).to_int(), clampi(int(head.get("humans", 0)), 0, Tuning.SOLO_PLAYERS))
|
||||
var rule_values: Dictionary = head["rules"]
|
||||
for key: String in RULE_KEYS:
|
||||
if not rule_values.has(key):
|
||||
# A hand-edited or damaged file could hold any JSON here; int() of an array or
|
||||
# a dictionary is an error, so only numbers and booleans are read (H5).
|
||||
if not rule_values.has(key) or not _is_number(rule_values[key]):
|
||||
return null
|
||||
if key == "fill_with_bots":
|
||||
result.rules.fill_with_bots = bool(rule_values[key])
|
||||
@@ -145,10 +147,12 @@ static func from_bytes(bytes: PackedByteArray) -> CommandLog:
|
||||
result.rules.set(key, int(rule_values[key]))
|
||||
result.rules.max_players = clampi(result.rules.max_players, 1, Tuning.SOLO_PLAYERS)
|
||||
result.rules.team_size = maxi(result.rules.team_size, 1)
|
||||
result.final_tick = int(head.get("final_tick", 0))
|
||||
result.final_tick = int(head.get("final_tick", 0)) if _is_number(head.get("final_tick", 0)) else 0
|
||||
result.final_hash = str(head.get("final_hash", ""))
|
||||
if head.get("winners") is Array:
|
||||
for winner: Variant in head["winners"]:
|
||||
if not _is_number(winner):
|
||||
return null
|
||||
result.winners.append(int(winner))
|
||||
if head.get("notes") is Dictionary:
|
||||
result.notes = head["notes"]
|
||||
@@ -156,6 +160,10 @@ static func from_bytes(bytes: PackedByteArray) -> CommandLog:
|
||||
return result
|
||||
|
||||
|
||||
static func _is_number(value: Variant) -> bool:
|
||||
return value is int or value is float or value is bool
|
||||
|
||||
|
||||
## Writes the log to `path`, creating its folder. Returns OK or the error.
|
||||
func save(path: String) -> Error:
|
||||
var folder: String = path.get_base_dir()
|
||||
|
||||
@@ -5,7 +5,7 @@
|
||||
## A server is listed under the address its packets come from, never one it claims,
|
||||
## and only after it has echoed a CHALLENGE token sent to that address, so nobody can
|
||||
## list a machine they don't control. Entries expire unless renewed, there is a cap
|
||||
## per address and overall, and every source is rate-limited.
|
||||
## per address (per /64 for IPv6) and overall, and every source is rate-limited.
|
||||
class_name ListServer
|
||||
extends RefCounted
|
||||
|
||||
@@ -137,10 +137,12 @@ func _answer(page: int, to_ip: String, to_port: int) -> void:
|
||||
_send(NetDiscovery.encode_list(page, pages, chosen), to_ip, to_port)
|
||||
|
||||
|
||||
## Entries from the same source as `address` (an IPv6 address counts with its /64).
|
||||
func _count_at(address: String) -> int:
|
||||
var source: String = SourceLimiter.source_of(address)
|
||||
var count: int = 0
|
||||
for key: String in _entries:
|
||||
if _entries[key]["address"] == address:
|
||||
if SourceLimiter.source_of(String(_entries[key]["address"])) == source:
|
||||
count += 1
|
||||
return count
|
||||
|
||||
|
||||
Reference in New Issue
Block a user