N9: Server-side cheat and abuse protection
- A hostile test client crafts INPUT packets byte by byte and sends them through NetGame.take_input() into a real match: extreme axes, the same tick claimed repeatedly, the whole lead window at once, past, far-future and maximum ticks, fire toggled every tick with slot swaps, builds aimed anywhere with junk kinds and materials and no materials, and pickup/drop/select tricks. Speed stays within sprint speed per tick, shots within the weapon's rate, pieces within build reach and paid for, and every item is conserved. - Fix: a command tick beyond the simulation's lead window no longer moves NetGame's dedupe marker, so one bogus far-future tick can't lock out a player's real commands. - Connections are rate-limited per address (32 per 10 s, 8 handshakes in progress) with a new REASON_BUSY and a message for it. - ParserFuzz mutates real messages from a bot match and random bytes and feeds them to every network decoder, the snapshot receiver and the list server. 20,000 per make check; make fuzz runs 1,000,000 (about 40 s): no errors, 2.9 KB of memory growth. - ARCHITECTURE.md: "Trusting nothing from clients". Closes #40 Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
@@ -0,0 +1,156 @@
|
||||
## Feeds malformed messages to every parser that reads bytes from the network: the
|
||||
## handshake, inputs, snapshots, match notices, world and inventory messages, and
|
||||
## discovery, plus the stateful snapshot receiver and the list server's handler.
|
||||
## Messages are mutations of real ones taken from a running match (bit flips,
|
||||
## truncation, extension, splices, runs of 0x00 and 0xFF, a different type byte) and
|
||||
## plain random bytes.
|
||||
##
|
||||
## Used by test_parser_fuzz.gd with a small count and by `make fuzz` with 1,000,000.
|
||||
## Nothing here may raise an engine error; whoever runs it checks that.
|
||||
class_name ParserFuzz
|
||||
extends RefCounted
|
||||
|
||||
const MAX_LENGTH: int = 1600
|
||||
|
||||
var rng: RandomNumberGenerator = RandomNumberGenerator.new()
|
||||
## Real messages to mutate.
|
||||
var seeds: Array[PackedByteArray] = []
|
||||
var messages: int = 0
|
||||
## Messages some parser accepted as well formed.
|
||||
var accepted: int = 0
|
||||
## Static memory in use after the warm-up and at the end, in bytes.
|
||||
var memory_start: int = 0
|
||||
var memory_end: int = 0
|
||||
var memory_peak: int = 0
|
||||
|
||||
var _receiver: SnapshotReceiver = SnapshotReceiver.new()
|
||||
var _list: ListServer = ListServer.new()
|
||||
|
||||
|
||||
func _init(fuzz_seed: int) -> void:
|
||||
rng.seed = fuzz_seed
|
||||
# The list server has no socket here, so it answers nothing; and no rate limit,
|
||||
# so every message reaches its parser.
|
||||
_list.limiter = SourceLimiter.new(1 << 30)
|
||||
|
||||
|
||||
## Takes real messages from a short bot match: snapshots, world, inventory and match
|
||||
## notices, plus one of every handshake and discovery message.
|
||||
func collect_seeds() -> void:
|
||||
var loop: ServerLoop = ServerLoop.new(9, 1, 5, null)
|
||||
var game: NetGame = NetGame.new(loop, NetServer.new())
|
||||
game.adopt(1)
|
||||
seeds.append(game.world_notice())
|
||||
seeds.append(game.match_notice())
|
||||
for tick: int in 150:
|
||||
loop.run_tick()
|
||||
game.after_tick()
|
||||
if not game.world_changes.is_empty():
|
||||
seeds.append(game.world_changes)
|
||||
for bytes: PackedByteArray in game.own_changes.values():
|
||||
seeds.append(bytes)
|
||||
if tick % 10 == 0:
|
||||
seeds.append(game.snapshot_for(1))
|
||||
var command: PlayerCommand = PlayerCommand.from_input(12, Vector2(0.3, 1.0), 1.0, 0.2, PlayerCommand.BUTTON_FIRE, 2, 1)
|
||||
seeds.append(NetSnapshot.encode_input(40, [command, command] as Array[PlayerCommand]))
|
||||
seeds.append(NetProtocol.encode_hello("Fuzzer"))
|
||||
seeds.append(NetProtocol.encode_welcome(3, 77, 24, "Fuzzer"))
|
||||
seeds.append(NetProtocol.encode_reject(NetProtocol.REASON_FULL))
|
||||
var info: Dictionary = NetDiscovery.info(24650, 3, 24, 1, "Fuzz Harbour")
|
||||
seeds.append(NetDiscovery.encode_query(1))
|
||||
seeds.append(NetDiscovery.encode_info(info))
|
||||
seeds.append(NetDiscovery.encode_register(5, info))
|
||||
seeds.append(NetDiscovery.encode_challenge(5))
|
||||
info["address"] = "192.0.2.4"
|
||||
seeds.append(NetDiscovery.encode_list(0, 1, [info, info] as Array[Dictionary]))
|
||||
|
||||
|
||||
## Runs `count` messages through every parser.
|
||||
func run(count: int) -> void:
|
||||
if seeds.is_empty():
|
||||
collect_seeds()
|
||||
var warm: int = mini(count, 2000)
|
||||
for index: int in count:
|
||||
if index == warm:
|
||||
memory_start = OS.get_static_memory_usage()
|
||||
feed(next_message())
|
||||
if index % 10000 == 0:
|
||||
memory_peak = maxi(memory_peak, OS.get_static_memory_usage())
|
||||
memory_end = OS.get_static_memory_usage()
|
||||
memory_peak = maxi(memory_peak, memory_end)
|
||||
|
||||
|
||||
func next_message() -> PackedByteArray:
|
||||
if rng.randf() < 0.15:
|
||||
var noise: PackedByteArray = PackedByteArray()
|
||||
noise.resize(rng.randi_range(0, MAX_LENGTH))
|
||||
for index: int in noise.size():
|
||||
noise[index] = rng.randi() & 0xFF
|
||||
return noise
|
||||
var bytes: PackedByteArray = seeds[rng.randi_range(0, seeds.size() - 1)].duplicate()
|
||||
for _change: int in rng.randi_range(1, 4):
|
||||
_mutate(bytes)
|
||||
if bytes.size() > MAX_LENGTH:
|
||||
bytes.resize(MAX_LENGTH)
|
||||
return bytes
|
||||
|
||||
|
||||
func feed(bytes: PackedByteArray) -> void:
|
||||
messages += 1
|
||||
var results: Array = [
|
||||
NetProtocol.decode_hello(bytes), NetProtocol.decode_welcome(bytes), NetProtocol.decode_reject(bytes),
|
||||
NetSnapshot.decode_input(bytes), NetSnapshot.decode_snapshot(bytes), NetSnapshot.decode_match(bytes),
|
||||
NetWorld.decode_world(bytes), NetWorld.decode_own(bytes),
|
||||
NetDiscovery.decode_query(bytes), NetDiscovery.decode_info(bytes), NetDiscovery.decode_register(bytes),
|
||||
NetDiscovery.decode_challenge(bytes), NetDiscovery.decode_list(bytes),
|
||||
]
|
||||
for result: Dictionary in results:
|
||||
if not result.is_empty():
|
||||
accepted += 1
|
||||
break
|
||||
_receiver.apply(bytes)
|
||||
_list.handle(bytes, "198.51.100.%d" % (messages % 250 + 1), 40000 + messages % 13, messages)
|
||||
# Keep the list server's state from being the thing that grows.
|
||||
if messages % 5000 == 0:
|
||||
_list.expire(messages + ListServer.ENTRY_TTL_MSEC + 1)
|
||||
|
||||
|
||||
func listed() -> int:
|
||||
return _list.entry_count()
|
||||
|
||||
|
||||
func _mutate(bytes: PackedByteArray) -> void:
|
||||
match rng.randi_range(0, 6):
|
||||
0:
|
||||
if not bytes.is_empty():
|
||||
var at: int = rng.randi_range(0, bytes.size() - 1)
|
||||
bytes[at] = bytes[at] ^ (1 << rng.randi_range(0, 7))
|
||||
1:
|
||||
if not bytes.is_empty():
|
||||
bytes[rng.randi_range(0, bytes.size() - 1)] = rng.randi() & 0xFF
|
||||
2:
|
||||
bytes.resize(rng.randi_range(0, bytes.size()))
|
||||
3:
|
||||
for _extra: int in rng.randi_range(1, 64):
|
||||
bytes.append(rng.randi() & 0xFF)
|
||||
4:
|
||||
# A run of 0x00 or 0xFF: large counts, long varints, zero lengths.
|
||||
if not bytes.is_empty():
|
||||
var at: int = rng.randi_range(0, bytes.size() - 1)
|
||||
var fill: int = 0xFF if rng.randf() < 0.5 else 0x00
|
||||
for index: int in range(at, mini(at + rng.randi_range(1, 12), bytes.size())):
|
||||
bytes[index] = fill
|
||||
5:
|
||||
var other: PackedByteArray = seeds[rng.randi_range(0, seeds.size() - 1)]
|
||||
if not other.is_empty() and not bytes.is_empty():
|
||||
var cut: int = rng.randi_range(0, bytes.size() - 1)
|
||||
var from: int = rng.randi_range(0, other.size() - 1)
|
||||
bytes.resize(cut)
|
||||
bytes.append_array(other.slice(from))
|
||||
6:
|
||||
if not bytes.is_empty():
|
||||
var types: Array[int] = [
|
||||
NetProtocol.MSG_HELLO, NetProtocol.MSG_WELCOME, NetProtocol.MSG_REJECT, NetSnapshot.MSG_MATCH,
|
||||
NetWorld.MSG_WORLD, NetWorld.MSG_OWN, NetSnapshot.MSG_SNAPSHOT, NetSnapshot.MSG_INPUT,
|
||||
]
|
||||
bytes[0] = types[rng.randi_range(0, types.size() - 1)]
|
||||
@@ -0,0 +1 @@
|
||||
uid://rpodlivhv7av
|
||||
@@ -300,3 +300,36 @@ func test_two_servers_cannot_share_a_port() -> void:
|
||||
assert_ne(other.listen(server.port(), HOST), OK)
|
||||
assert_false(other.is_listening())
|
||||
assert_true(server.is_listening())
|
||||
|
||||
|
||||
func test_a_burst_of_connections_from_one_address_is_turned_away() -> void:
|
||||
server.connect_limiter = SourceLimiter.new(3, 60000)
|
||||
var burst: Array[NetClient] = []
|
||||
for index: int in 5:
|
||||
burst.append(_client("Wave %d" % index))
|
||||
assert_true(_pump(func() -> bool:
|
||||
var settled: int = 0
|
||||
for client: NetClient in burst:
|
||||
if client.state == NetClient.JOINED or client.state == NetClient.CLOSED:
|
||||
settled += 1
|
||||
return settled == burst.size()))
|
||||
var joined: int = 0
|
||||
for client: NetClient in burst:
|
||||
if client.state == NetClient.JOINED:
|
||||
joined += 1
|
||||
else:
|
||||
assert_eq(client.close_reason, NetProtocol.REASON_BUSY)
|
||||
assert_eq(joined, 3)
|
||||
assert_eq(server.player_count(), 3)
|
||||
assert_true(NetProtocol.reason_text(NetProtocol.REASON_BUSY).begins_with("Too many connections"))
|
||||
|
||||
|
||||
func test_handshakes_held_open_from_one_address_are_capped() -> void:
|
||||
server.max_guests_per_address = 2
|
||||
# Raw peers that connect and never say hello.
|
||||
for _index: int in 4:
|
||||
_raw()
|
||||
assert_true(_pump(func() -> bool: return _events("refused").size() >= 2))
|
||||
assert_eq(server.guest_count(), 2)
|
||||
for event: Dictionary in _events("refused"):
|
||||
assert_eq(event["reason"], NetProtocol.REASON_BUSY)
|
||||
|
||||
@@ -0,0 +1,21 @@
|
||||
extends TestCase
|
||||
|
||||
## Issue N9: malformed messages never crash a parser or make memory grow. This runs a
|
||||
## short pass on every `make check`; `make fuzz` runs 1,000,000 messages.
|
||||
|
||||
|
||||
func test_malformed_messages_are_refused_without_errors_or_growth() -> void:
|
||||
var fuzz: ParserFuzz = ParserFuzz.new(2026)
|
||||
fuzz.collect_seeds()
|
||||
assert_true(fuzz.seeds.size() > 20, "%d real messages to mutate" % fuzz.seeds.size())
|
||||
# Every seed is a message some parser takes.
|
||||
for bytes: PackedByteArray in fuzz.seeds:
|
||||
var before: int = fuzz.accepted
|
||||
fuzz.feed(bytes)
|
||||
assert_eq(fuzz.accepted, before + 1, "a seed of %d bytes, type %d, was refused" % [bytes.size(), bytes[0]])
|
||||
fuzz.run(20000)
|
||||
assert_eq(fuzz.messages, 20000 + fuzz.seeds.size())
|
||||
assert_true(fuzz.accepted > fuzz.seeds.size(), "some mutations still parse, so parsers are reached past the first byte")
|
||||
assert_eq(fuzz.listed(), 0, "no mutation guessed a list server's token")
|
||||
var growth: int = fuzz.memory_end - fuzz.memory_start
|
||||
assert_true(growth < 8 * 1024 * 1024, "memory grew %d bytes" % growth)
|
||||
@@ -0,0 +1 @@
|
||||
uid://dpkot3jo4k6pc
|
||||
Reference in New Issue
Block a user