Skip to content

Commit

Permalink
fix(state/statebag): filter frequent & large data from client
Browse files Browse the repository at this point in the history
* if we accept these willy-nilly, clients could possibly OOM the server, or do a one-to-many make-shift DoS on larger a server
* the size limits here are kept high to keep compatibility with people who "misuse" state bags
* sane defaults for servers that don't send large data via state bags would be `set rateLimiter_stateBagSize_rate 8196` and `set rateLimiter_stateBagSize_burst 16364`
  • Loading branch information
AvarianKnight committed Jan 25, 2024
1 parent c18c321 commit fe15b78
Showing 1 changed file with 51 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@
#include <boost/range/adaptors.hpp>
#include <boost/math/constants/constants.hpp>

#include <KeyedRateLimiter.h>

#include <OneSyncVars.h>
#include <DebugAlias.h>

Expand Down Expand Up @@ -4250,8 +4252,56 @@ void ServerGameState::AttachToObject(fx::ServerInstanceBase* instance)
sbac->SetGameInterface(this);

instance->GetComponent<fx::GameServer>()->GetComponent<fx::HandlerMapComponent>()->Add(HashRageString("msgStateBag"),
{ fx::ThreadIdx::Sync, [this](const fx::ClientSharedPtr& client, net::Buffer& buffer)
{ fx::ThreadIdx::Sync, [this, instance](const fx::ClientSharedPtr& client, net::Buffer& buffer)
{
static fx::RateLimiterStore<uint32_t, false> stateBagRateLimiterStore{ instance->GetComponent<console::Context>().GetRef() };

constexpr double kStateBagRateLimit = 50.f;
constexpr double kStateBagRateLimitBurst = 100.f;
static auto stateBagRateLimiter = stateBagRateLimiterStore.GetRateLimiter("stateBag", fx::RateLimiterDefaults{ kStateBagRateLimit, kStateBagRateLimitBurst });

constexpr double kStateBagRateFloodLimit = 75.f;
constexpr double kStateBagRateLimitFloodBurst = 100.f;
static auto stateBagRateFloodLimiter = stateBagRateLimiterStore.GetRateLimiter("stateBagFlood", fx::RateLimiterDefaults{ kStateBagRateFloodLimit, kStateBagRateLimitFloodBurst });

constexpr double kStateBagSizeLimit = 64 * 1024.0;
constexpr double kStateBagSizeLimitBurst = 128 * 1024.0;
static auto stateBagSizeRateLimiter = stateBagRateLimiterStore.GetRateLimiter("stateBagSize", fx::RateLimiterDefaults{ kStateBagSizeLimit, kStateBagSizeLimitBurst });

const uint32_t netId = client->GetNetId();

if (!stateBagRateLimiter->Consume(netId))
{
if (!stateBagRateFloodLimiter->Consume(netId))
{
if (!client->IsDropping())
{
console::Printf("state-bag-warning", "Client %s %i got dropped for sending too many state bag value updates.\n", client->GetName(), netId);
console::Printf("state-bag-warning", "If you believe this to be a mistake please increase your rateLimiter_stateBag_rate and rateLimiter_stateBag_burst. ");
console::Printf("state-bag-warning", "You can do this with `set rateLimiter_stateBag_rate [new value]`. The default rate limit is 50 and burst limit is 100\n");
console::Printf("state-bag-warning", "You can disable this warning with `con_addChannelFilter state-bag-warning drop` if you think you have this properly set up.\n");
}

instance->GetComponent<fx::GameServer>()->DropClient(client, "Reliable state bag packet overflow.");
}

return;
}

uint32_t dataLength = buffer.GetRemainingBytes();
if (!stateBagSizeRateLimiter->Consume(netId, double(dataLength)))
{
if (!client->IsDropping())
{
console::Printf("state-bag-warning", "Client %s %d got dropped for sending too large of a state bag update.\n", client->GetName(), netId);
console::Printf("state-bag-warning", "If you believe this to be a mistake please increase your rateLimiter_stateBagSize_rate. ");
console::Printf("state-bag-warning", "You can do this with `set rateLimiter_stateBag_rate [new value]`. The default size rate limit is 64000.\n");
console::Printf("state-bag-warning", "You can disable this warning with `con_addChannelFilter state-bag-warning drop` if you think you have this properly set up.\n");
}

instance->GetComponent<fx::GameServer>()->DropClient(client, "Reliable state bag packet overflow.");
return;
}

uint32_t slotId = client->GetSlotId();
if (slotId != -1)
Expand Down

0 comments on commit fe15b78

Please sign in to comment.