Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[MKW] Prevent the game from crashing if a nonexistent player uses a Thunder Cloud #74

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
57 changes: 53 additions & 4 deletions payload/import/mkw/net/eventHandler.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,29 @@ class EventHandler

static_assert(sizeof(EventInfo) == 0x01);

struct ItemUsedEvent {
bool isValid(u8 itemObject, u8 playerAid) const
{
using namespace mkw::Item;

ItemObject item = static_cast<ItemObject>(itemObject);

switch (item) {
case ItemObject::ThunderCloud: {
return this->playerAid == playerAid;
}
default: {
return true;
}
}
}

/* 0x00 */ u8 _00[0x02 - 0x00];
/* 0x02 */ u8 playerAid;
};

static_assert(sizeof(ItemUsedEvent) == 0x03);

bool containsInvalidItemObject() const
{
for (size_t n = 0; n < sizeof(eventInfo); n++) {
Expand All @@ -94,9 +117,9 @@ class EventHandler
return false;
}

bool isValid(u8 packetSize) const
bool isValid(u8 packetSize, u8 playerAid) const
{
u32 expectedPacketSize = sizeof(eventInfo);
size_t expectedPacketSize = sizeof(eventInfo);

for (size_t n = 0; n < sizeof(eventInfo); n++) {
if (!eventInfo[n].isValid()) {
Expand All @@ -106,11 +129,37 @@ class EventHandler
expectedPacketSize += eventInfo[n].getEventDataSize();
}

return expectedPacketSize == packetSize;
if (expectedPacketSize != packetSize) {
return false;
}

expectedPacketSize = 0;
for (size_t n = 0; n < sizeof(eventInfo); n++) {
EventInfo info = eventInfo[n];
u8 itemObject = info.itemObject;
const u8* data = eventData + expectedPacketSize;

switch (info.eventType) {
case EventInfo::EventType::ItemUsed: {
const ItemUsedEvent* itemUsedEvent =
reinterpret_cast<const ItemUsedEvent*>(data);

if (!itemUsedEvent->isValid(itemObject, playerAid)) {
return false;
}
}
default: {
}
}

expectedPacketSize += info.getEventDataSize();
}

return true;
}

/* 0x00 */ EventInfo eventInfo[0x18];
/* 0x18 */ u8 _18[0xF8 - 0x18];
/* 0x18 */ u8 eventData[0xF8 - 0x18];
};

static_assert(sizeof(Packet) == 0xF8);
Expand Down
2 changes: 1 addition & 1 deletion payload/import/mkw/net/selectHandler.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,7 @@ class SelectHandler

// Support modifications that allow for clients to be connected to more
// than 11 peers at once.
for (u32 n = 0; n < sizeof(aidsStillLoading) * 8; n++) {
for (size_t n = 0; n < sizeof(aidsStillLoading) * 8; n++) {
if (((aidsStillLoading >> n) & 1) == 0) {
continue;
}
Expand Down
8 changes: 4 additions & 4 deletions payload/wwfcSecurity.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -238,6 +238,7 @@ static bool IsHeaderPacketDataValid(
const void* /* packet */, u8 /* packetSize */, u8 /* playerAid */
)
{
// This packet is validated by the function 'IsRacePacketValid'
return true;
}

Expand Down Expand Up @@ -497,9 +498,8 @@ IsItemPacketDataValid(const void* packet, u8 packetSize, u8 /* playerAid */)
return true;
}

static bool IsEventPacketDataValid(
const void* packet, u8 packetSize, u8 /* playerAid */
)
static bool
IsEventPacketDataValid(const void* packet, u8 packetSize, u8 playerAid)
{
using namespace mkw::System;

Expand All @@ -522,7 +522,7 @@ static bool IsEventPacketDataValid(
return true;
}

if (!eventPacket->isValid(packetSize)) {
if (!eventPacket->isValid(packetSize, playerAid)) {
return false;
}

Expand Down