fix: Fixes crashing due to bad packet assumptions. (#1829)

### Summary
- Fixes various exploits that can crash the shard when the client misbehaves.
- Clients will now be disconnected if they send packets that are marked as out of game only (new flag), while they are in-game.


> [!Note]
> **Developer Note**
> Added an `OutOfGameOnly` which should be used to flag packets as only available out of the game.
> This is the opposite of, yet not the converse to `InGameOnly`.
This commit is contained in:
Kamron Batman 2024-06-07 18:08:07 -07:00 committed by GitHub
parent 2429b00638
commit 6f444488a5
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
10 changed files with 113 additions and 46 deletions

View file

@ -806,20 +806,27 @@ public partial class NetState : IComparable<NetState>, IValueLinkListNode<NetSta
return ParserState.AwaitingPartialPacket;
}
if (handler.Ingame)
if (handler.InGameOnly)
{
if (Mobile == null)
{
LogInfo($"received packet 0x{packetId:X2} before having been attached to a mobile");
LogInfo($"Received packet 0x{packetId:X2} before having been attached to a mobile.");
return ParserState.Error;
}
if (Mobile.Deleted)
{
LogInfo($"Received packet 0x{packetId:X2} after having been attached to a deleted mobile.");
return ParserState.Error;
}
}
if (handler.OutOfGameOnly && Mobile?.Deleted == false)
{
LogInfo($"Received packet 0x{packetId:X2} after having been attached to a mobile.");
return ParserState.Error;
}
var throttler = handler.ThrottleCallback;
if (throttler != null)
{

View file

@ -21,11 +21,20 @@ public unsafe class PacketHandler
{
private readonly int _length;
public PacketHandler(int packetID, int length, bool ingame, delegate*<NetState, SpanReader, void> onReceive)
public PacketHandler(
int packetID, delegate*<NetState, SpanReader, void> onReceive,
int length = 0, bool inGameOnly = false, bool outGameOnly = false
) : this(packetID, length, inGameOnly, outGameOnly, onReceive)
{
}
public PacketHandler(int packetID, int length, bool inGameOnly, bool outGameOnly, delegate*<NetState, SpanReader, void> onReceive)
{
_length = length;
PacketID = packetID;
Ingame = ingame;
InGameOnly = inGameOnly;
OutOfGameOnly = outGameOnly;
OnReceive = onReceive;
}
@ -37,5 +46,7 @@ public unsafe class PacketHandler
public delegate*<int, NetState, bool> ThrottleCallback { get; set; }
public bool Ingame { get; }
public bool InGameOnly { get; }
public bool OutOfGameOnly { get; }
}

View file

@ -25,9 +25,20 @@ public static class IncomingPackets
public static PacketHandler[] Handlers { get; } = new PacketHandler[0x100];
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static unsafe void Register(int packetID, int length, bool ingame,
delegate*<NetState, SpanReader, void> onReceive) =>
Register(new PacketHandler(packetID, length, ingame, onReceive));
public static unsafe void Register(
int packetID, delegate*<NetState, SpanReader, void> onReceive, int length = 0, bool ingameOnly = false,
bool outgameOnly = false
) => Register(packetID, length, ingameOnly, outgameOnly, onReceive);
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static unsafe void Register(
int packetID, int length, bool ingame, delegate*<NetState, SpanReader, void> onReceive
) => Register(packetID, length, ingame, false, onReceive);
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static unsafe void Register(
int packetID, int length, bool ingame, bool outgame, delegate*<NetState, SpanReader, void> onReceive
) => Register(new PacketHandler(packetID, length, ingame, outgame, onReceive));
public static void Register(PacketHandler packetHandler)
{