Skip to content

Commit

Permalink
[CMLIB] Avoid buffer overflow and clarify bit map. CORE-19337
Browse files Browse the repository at this point in the history
  • Loading branch information
ThFabba committed Nov 26, 2023
1 parent c77a5ff commit b4a0e7d
Showing 1 changed file with 11 additions and 12 deletions.
23 changes: 11 additions & 12 deletions sdk/lib/cmlib/hivewrt.c
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ HvpWriteLog(
* Now calculate the bitmap and buffer sizes to hold up our
* contents in a buffer.
*/
BitmapSize = ROUND_UP(sizeof(ULONG) + RegistryHive->DirtyVector.SizeOfBitMap / 8, HSECTOR_SIZE);
BitmapSize = ROUND_UP(sizeof(ULONG) + RegistryHive->DirtyVector.SizeOfBitMap, HSECTOR_SIZE);
BufferSize = HV_LOG_HEADER_SIZE + BitmapSize;

/* Now allocate the base header block buffer */
Expand Down Expand Up @@ -167,11 +167,14 @@ HvpWriteLog(
Ptr += sizeof(HV_LOG_DIRTY_SIGNATURE);

/*
* FIXME: In ReactOS a vector contains one bit per block
* whereas in Windows each bit within a vector is per
* sector. Furthermore, the dirty blocks within a respective
* hive has to be marked as such in an appropriate function
* for this purpose (probably HvMarkDirty or similar).
* The dirty bitmap in the log file uses one bit per 512 bytes,
* but the log file always contains a full 4096 bytes (HBLOCK_SIZE)
* of data, which matches our in-memory block tracking. So to indicate a
* dirty block, we set 8 bits at once (HV_LOG_DIRTY_BLOCK).
*
* FIXME: The dirty blocks within a respective hive have to be
* marked as such in an appropriate function for this purpose
* (probably HvMarkDirty or similar).
*
* For the moment being, mark the relevant dirty blocks
* here.
Expand All @@ -182,17 +185,13 @@ HvpWriteLog(
/* Check if the block is clean or we're past the last block */
LastIndex = BlockIndex;
BlockIndex = RtlFindSetBits(&RegistryHive->DirtyVector, 1, BlockIndex);
if (BlockIndex == ~HV_CLEAN_BLOCK || BlockIndex < LastIndex)
if (BlockIndex == 0xFFFFFFFF || BlockIndex < LastIndex)
{
break;
}

/*
* Mark this block as dirty and go to the next one.
*
* FIXME: We should rather use RtlSetBits but that crashes
* the system with a bugckeck. So for now mark blocks manually
* by hand.
*/
Ptr[BlockIndex] = HV_LOG_DIRTY_BLOCK;
BlockIndex++;
Expand All @@ -217,7 +216,7 @@ HvpWriteLog(
/* Check if the block is clean or we're past the last block */
LastIndex = BlockIndex;
BlockIndex = RtlFindSetBits(&RegistryHive->DirtyVector, 1, BlockIndex);
if (BlockIndex == ~HV_CLEAN_BLOCK || BlockIndex < LastIndex)
if (BlockIndex == 0xFFFFFFFF || BlockIndex < LastIndex)
{
break;
}
Expand Down

0 comments on commit b4a0e7d

Please sign in to comment.