[client] lgmp: validate frame message alignment

This commit is contained in:
Geoffrey McRae
2026-08-24 13:15:42 +10:00
parent 357d5fd3c5
commit 957e4ee54b
3 changed files with 15 additions and 5 deletions

View File

@@ -97,11 +97,12 @@ static bool waitForEmpty(TestState * state)
return false; return false;
} }
static bool allocMemory(TestState * state, uint32_t size, static bool allocMemory(TestState * state, uint32_t size, uint32_t alignment,
PLGMPMemory * memory, void ** pointer) PLGMPMemory * memory, void ** pointer)
{ {
CHECK(state->allocationCount < TEST_MAX_ALLOCS); CHECK(state->allocationCount < TEST_MAX_ALLOCS);
CHECK(lgmpHostMemAlloc(state->host, size, memory) == LGMP_OK); CHECK(lgmpHostMemAllocAligned(
state->host, size, alignment, memory) == LGMP_OK);
state->allocations[state->allocationCount++] = *memory; state->allocations[state->allocationCount++] = *memory;
*pointer = lgmpHostMemPtr(*memory); *pointer = lgmpHostMemPtr(*memory);
return true; return true;
@@ -112,7 +113,8 @@ static bool newFrame(TestState * state, uint32_t serial, TestFrame * result)
const uint32_t size = sizeof(KVMFRFrame) + sizeof(FrameBuffer) + const uint32_t size = sizeof(KVMFRFrame) + sizeof(FrameBuffer) +
TEST_FRAME_DATA; TEST_FRAME_DATA;
void * pointer; void * pointer;
CHECK(allocMemory(state, size, &result->memory, &pointer)); CHECK(allocMemory(state, size, _Alignof(KVMFRFrame),
&result->memory, &pointer));
result->wire = pointer; result->wire = pointer;
result->wire->formatVer = 1; result->wire->formatVer = 1;
@@ -199,7 +201,8 @@ static bool testMalformed(TestState * state)
PLGMPMemory shortMemory; PLGMPMemory shortMemory;
void * shortPointer; void * shortPointer;
CHECK(allocMemory(state, sizeof(KVMFRFrame) - 4, &shortMemory, CHECK(allocMemory(state, sizeof(KVMFRFrame) - 4, _Alignof(uint32_t),
&shortMemory,
&shortPointer)); &shortPointer));
CHECK(lgmpHostQueuePost(state->queues[0], 0, shortMemory) == LGMP_OK); CHECK(lgmpHostQueuePost(state->queues[0], 0, shortMemory) == LGMP_OK);
LG_TransportFrame result; LG_TransportFrame result;

View File

@@ -381,7 +381,8 @@ int main(void)
}; };
const uint32_t frameSize = const uint32_t frameSize =
sizeof(KVMFRFrame) + sizeof(FrameBuffer) + sizeof(uint32_t); sizeof(KVMFRFrame) + sizeof(FrameBuffer) + sizeof(uint32_t);
CHECK(lgmpHostMemAlloc(host, frameSize, &frameMemory) == LGMP_OK); CHECK(lgmpHostMemAllocAligned(host, frameSize, _Alignof(KVMFRFrame),
&frameMemory) == LGMP_OK);
KVMFRFrame * wireFrame = lgmpHostMemPtr(frameMemory); KVMFRFrame * wireFrame = lgmpHostMemPtr(frameMemory);
wireFrame->formatVer = 1; wireFrame->formatVer = 1;
wireFrame->frameSerial = 1; wireFrame->frameSerial = 1;

View File

@@ -1786,6 +1786,12 @@ static bool lgmp_validateFrameMessage(LG_Transport * this,
return false; return false;
} }
if ((uintptr_t)message->message.mem % _Alignof(KVMFRFrame))
{
DEBUG_ERROR("LGMP frame payload is not correctly aligned");
return false;
}
memcpy(&message->lease->snapshot, message->message.mem, memcpy(&message->lease->snapshot, message->message.mem,
sizeof(message->lease->snapshot)); sizeof(message->lease->snapshot));
const KVMFRFrame * frame = &message->lease->snapshot; const KVMFRFrame * frame = &message->lease->snapshot;