- Fix two typos in the buffer size calculation code, spotted by Stefan Ginsberg.

svn path=/trunk/; revision=33301
This commit is contained in:
Aleksey Bragin 2008-05-05 18:23:55 +00:00
parent beb173aaa8
commit 26171ce642

View file

@ -78,28 +78,28 @@ MiDoMappedCopy(IN PEPROCESS SourceProcess,
_SEH_DECLARE_LOCALS(MiGetExceptionInfo); _SEH_DECLARE_LOCALS(MiGetExceptionInfo);
NTSTATUS Status = STATUS_SUCCESS; NTSTATUS Status = STATUS_SUCCESS;
PAGED_CODE(); PAGED_CODE();
/* Calculate the maximum amount of data to move */ /* Calculate the maximum amount of data to move */
TotalSize = MI_MAPPED_COPY_PAGES - 2; TotalSize = MI_MAPPED_COPY_PAGES - 2;
if (TotalSize <= (MI_MAPPED_COPY_PAGES - 2)) TotalSize = BufferSize; if (BufferSize <= (MI_MAPPED_COPY_PAGES - 2)) TotalSize = BufferSize;
CurrentSize = BufferSize; CurrentSize = BufferSize;
RemainingSize = TotalSize; RemainingSize = TotalSize;
/* Loop as long as there is still data */ /* Loop as long as there is still data */
while (RemainingSize > 0) while (RemainingSize > 0)
{ {
/* Check if this transfer will finish everything off */ /* Check if this transfer will finish everything off */
if (RemainingSize < CurrentSize) CurrentSize = RemainingSize; if (RemainingSize < CurrentSize) CurrentSize = RemainingSize;
/* Attach to the source address space */ /* Attach to the source address space */
KeStackAttachProcess(&SourceProcess->Pcb, &ApcState); KeStackAttachProcess(&SourceProcess->Pcb, &ApcState);
/* Reset state for this pass */ /* Reset state for this pass */
MdlAddress = NULL; MdlAddress = NULL;
PagesLocked = FALSE; PagesLocked = FALSE;
FailedInMoving = FALSE; FailedInMoving = FALSE;
ASSERT(FailedInProbe == FALSE); ASSERT(FailedInProbe == FALSE);
/* Protect user-mode copy */ /* Protect user-mode copy */
_SEH_TRY _SEH_TRY
{ {
@ -108,19 +108,19 @@ MiDoMappedCopy(IN PEPROCESS SourceProcess,
{ {
/* Catch a failure here */ /* Catch a failure here */
FailedInProbe = TRUE; FailedInProbe = TRUE;
/* Do the probe */ /* Do the probe */
ProbeForRead(SourceAddress, BufferSize, sizeof(CHAR)); ProbeForRead(SourceAddress, BufferSize, sizeof(CHAR));
/* Passed */ /* Passed */
FailedInProbe = FALSE; FailedInProbe = FALSE;
} }
/* Initialize and probe and lock the MDL */ /* Initialize and probe and lock the MDL */
MmInitializeMdl (Mdl, CurrentAddress, CurrentSize); MmInitializeMdl (Mdl, CurrentAddress, CurrentSize);
MmProbeAndLockPages (Mdl, PreviousMode, IoReadAccess); MmProbeAndLockPages (Mdl, PreviousMode, IoReadAccess);
PagesLocked = TRUE; PagesLocked = TRUE;
/* Now map the pages */ /* Now map the pages */
MdlAddress = MmMapLockedPagesSpecifyCache(Mdl, MdlAddress = MmMapLockedPagesSpecifyCache(Mdl,
KernelMode, KernelMode,
@ -134,24 +134,24 @@ MiDoMappedCopy(IN PEPROCESS SourceProcess,
FailedInMapping = TRUE; FailedInMapping = TRUE;
ExRaiseStatus(STATUS_INSUFFICIENT_RESOURCES); ExRaiseStatus(STATUS_INSUFFICIENT_RESOURCES);
} }
/* Now let go of the source and grab to the target process */ /* Now let go of the source and grab to the target process */
KeUnstackDetachProcess(&ApcState); KeUnstackDetachProcess(&ApcState);
KeStackAttachProcess(&TargetProcess->Pcb, &ApcState); KeStackAttachProcess(&TargetProcess->Pcb, &ApcState);
/* Check if this is our first time through */ /* Check if this is our first time through */
if ((CurrentAddress == SourceAddress) && (PreviousMode != KernelMode)) if ((CurrentAddress == SourceAddress) && (PreviousMode != KernelMode))
{ {
/* Catch a failure here */ /* Catch a failure here */
FailedInProbe = TRUE; FailedInProbe = TRUE;
/* Do the probe */ /* Do the probe */
ProbeForWrite(TargetAddress, BufferSize, sizeof(CHAR)); ProbeForWrite(TargetAddress, BufferSize, sizeof(CHAR));
/* Passed */ /* Passed */
FailedInProbe = FALSE; FailedInProbe = FALSE;
} }
/* Now do the actual move */ /* Now do the actual move */
FailedInMoving = TRUE; FailedInMoving = TRUE;
RtlCopyMemory(CurrentTargetAddress, MdlAddress, CurrentSize); RtlCopyMemory(CurrentTargetAddress, MdlAddress, CurrentSize);
@ -160,13 +160,13 @@ MiDoMappedCopy(IN PEPROCESS SourceProcess,
{ {
/* Detach from whoever we may be attached to */ /* Detach from whoever we may be attached to */
KeUnstackDetachProcess(&ApcState); KeUnstackDetachProcess(&ApcState);
/* Check if we had mapped the pages */ /* Check if we had mapped the pages */
if (MdlAddress) MmUnmapLockedPages(MdlAddress, Mdl); if (MdlAddress) MmUnmapLockedPages(MdlAddress, Mdl);
/* Check if we had locked the pages */ /* Check if we had locked the pages */
if (PagesLocked) MmUnlockPages(Mdl); if (PagesLocked) MmUnlockPages(Mdl);
/* Check if we failed during the probe or mapping */ /* Check if we failed during the probe or mapping */
if ((FailedInProbe) || (FailedInMapping)) if ((FailedInProbe) || (FailedInMapping))
{ {
@ -174,7 +174,7 @@ MiDoMappedCopy(IN PEPROCESS SourceProcess,
Status = _SEH_GetExceptionCode(); Status = _SEH_GetExceptionCode();
_SEH_YIELD(); _SEH_YIELD();
} }
/* Otherwise, we failed probably during the move */ /* Otherwise, we failed probably during the move */
*ReturnSize = BufferSize - RemainingSize; *ReturnSize = BufferSize - RemainingSize;
if (FailedInMoving) if (FailedInMoving)
@ -186,28 +186,28 @@ MiDoMappedCopy(IN PEPROCESS SourceProcess,
*ReturnSize = _SEH_VAR(BadAddress) - (ULONG_PTR)SourceAddress; *ReturnSize = _SEH_VAR(BadAddress) - (ULONG_PTR)SourceAddress;
} }
} }
/* Return partial copy */ /* Return partial copy */
Status = STATUS_PARTIAL_COPY; Status = STATUS_PARTIAL_COPY;
} }
_SEH_END; _SEH_END;
/* Check for SEH status */ /* Check for SEH status */
if (Status != STATUS_SUCCESS) return Status; if (Status != STATUS_SUCCESS) return Status;
/* Detach from target */ /* Detach from target */
KeUnstackDetachProcess(&ApcState); KeUnstackDetachProcess(&ApcState);
/* Unmap and unlock */ /* Unmap and unlock */
MmUnmapLockedPages(MdlAddress, Mdl); MmUnmapLockedPages(MdlAddress, Mdl);
MmUnlockPages(Mdl); MmUnlockPages(Mdl);
/* Update location and size */ /* Update location and size */
RemainingSize -= CurrentSize; RemainingSize -= CurrentSize;
CurrentAddress = (PVOID)((ULONG_PTR)CurrentAddress + CurrentSize); CurrentAddress = (PVOID)((ULONG_PTR)CurrentAddress + CurrentSize);
CurrentTargetAddress = (PVOID)((ULONG_PTR)CurrentTargetAddress + CurrentSize); CurrentTargetAddress = (PVOID)((ULONG_PTR)CurrentTargetAddress + CurrentSize);
} }
/* All bytes read */ /* All bytes read */
*ReturnSize = BufferSize; *ReturnSize = BufferSize;
return STATUS_SUCCESS; return STATUS_SUCCESS;
@ -232,13 +232,13 @@ MiDoPoolCopy(IN PEPROCESS SourceProcess,
_SEH_DECLARE_LOCALS(MiGetExceptionInfo); _SEH_DECLARE_LOCALS(MiGetExceptionInfo);
NTSTATUS Status = STATUS_SUCCESS; NTSTATUS Status = STATUS_SUCCESS;
PAGED_CODE(); PAGED_CODE();
/* Calculate the maximum amount of data to move */ /* Calculate the maximum amount of data to move */
TotalSize = MI_MAX_TRANSFER_SIZE; TotalSize = MI_MAX_TRANSFER_SIZE;
if (TotalSize <= MI_MAX_TRANSFER_SIZE) TotalSize = BufferSize; if (BufferSize <= MI_MAX_TRANSFER_SIZE) TotalSize = BufferSize;
CurrentSize = BufferSize; CurrentSize = BufferSize;
RemainingSize = TotalSize; RemainingSize = TotalSize;
/* Check if we can use the stack */ /* Check if we can use the stack */
if (BufferSize <= MI_POOL_COPY_BYTES) if (BufferSize <= MI_POOL_COPY_BYTES)
{ {
@ -252,20 +252,20 @@ MiDoPoolCopy(IN PEPROCESS SourceProcess,
if (!PoolAddress) ASSERT(FALSE); if (!PoolAddress) ASSERT(FALSE);
HavePoolAddress = TRUE; HavePoolAddress = TRUE;
} }
/* Loop as long as there is still data */ /* Loop as long as there is still data */
while (RemainingSize > 0) while (RemainingSize > 0)
{ {
/* Check if this transfer will finish everything off */ /* Check if this transfer will finish everything off */
if (RemainingSize < CurrentSize) CurrentSize = RemainingSize; if (RemainingSize < CurrentSize) CurrentSize = RemainingSize;
/* Attach to the source address space */ /* Attach to the source address space */
KeStackAttachProcess(&SourceProcess->Pcb, &ApcState); KeStackAttachProcess(&SourceProcess->Pcb, &ApcState);
/* Reset state for this pass */ /* Reset state for this pass */
FailedInMoving = FALSE; FailedInMoving = FALSE;
ASSERT(FailedInProbe == FALSE); ASSERT(FailedInProbe == FALSE);
/* Protect user-mode copy */ /* Protect user-mode copy */
_SEH_TRY _SEH_TRY
{ {
@ -274,34 +274,34 @@ MiDoPoolCopy(IN PEPROCESS SourceProcess,
{ {
/* Catch a failure here */ /* Catch a failure here */
FailedInProbe = TRUE; FailedInProbe = TRUE;
/* Do the probe */ /* Do the probe */
ProbeForRead(SourceAddress, BufferSize, sizeof(CHAR)); ProbeForRead(SourceAddress, BufferSize, sizeof(CHAR));
/* Passed */ /* Passed */
FailedInProbe = FALSE; FailedInProbe = FALSE;
} }
/* Do the copy */ /* Do the copy */
RtlCopyMemory(PoolAddress, CurrentAddress, CurrentSize); RtlCopyMemory(PoolAddress, CurrentAddress, CurrentSize);
/* Now let go of the source and grab to the target process */ /* Now let go of the source and grab to the target process */
KeUnstackDetachProcess(&ApcState); KeUnstackDetachProcess(&ApcState);
KeStackAttachProcess(&TargetProcess->Pcb, &ApcState); KeStackAttachProcess(&TargetProcess->Pcb, &ApcState);
/* Check if this is our first time through */ /* Check if this is our first time through */
if ((CurrentAddress == SourceAddress) && (PreviousMode != KernelMode)) if ((CurrentAddress == SourceAddress) && (PreviousMode != KernelMode))
{ {
/* Catch a failure here */ /* Catch a failure here */
FailedInProbe = TRUE; FailedInProbe = TRUE;
/* Do the probe */ /* Do the probe */
ProbeForWrite(TargetAddress, BufferSize, sizeof(CHAR)); ProbeForWrite(TargetAddress, BufferSize, sizeof(CHAR));
/* Passed */ /* Passed */
FailedInProbe = FALSE; FailedInProbe = FALSE;
} }
/* Now do the actual move */ /* Now do the actual move */
FailedInMoving = TRUE; FailedInMoving = TRUE;
RtlCopyMemory(CurrentTargetAddress, PoolAddress, CurrentSize); RtlCopyMemory(CurrentTargetAddress, PoolAddress, CurrentSize);
@ -310,10 +310,10 @@ MiDoPoolCopy(IN PEPROCESS SourceProcess,
{ {
/* Detach from whoever we may be attached to */ /* Detach from whoever we may be attached to */
KeUnstackDetachProcess(&ApcState); KeUnstackDetachProcess(&ApcState);
/* Check if we had allocated pool */ /* Check if we had allocated pool */
if (HavePoolAddress) ExFreePool(PoolAddress); if (HavePoolAddress) ExFreePool(PoolAddress);
/* Check if we failed during the probe */ /* Check if we failed during the probe */
if (FailedInProbe) if (FailedInProbe)
{ {
@ -321,7 +321,7 @@ MiDoPoolCopy(IN PEPROCESS SourceProcess,
Status = _SEH_GetExceptionCode(); Status = _SEH_GetExceptionCode();
_SEH_YIELD(); _SEH_YIELD();
} }
/* Otherwise, we failed probably during the move */ /* Otherwise, we failed probably during the move */
*ReturnSize = BufferSize - RemainingSize; *ReturnSize = BufferSize - RemainingSize;
if (FailedInMoving) if (FailedInMoving)
@ -333,27 +333,27 @@ MiDoPoolCopy(IN PEPROCESS SourceProcess,
*ReturnSize = _SEH_VAR(BadAddress) - (ULONG_PTR)SourceAddress; *ReturnSize = _SEH_VAR(BadAddress) - (ULONG_PTR)SourceAddress;
} }
} }
/* Return partial copy */ /* Return partial copy */
Status = STATUS_PARTIAL_COPY; Status = STATUS_PARTIAL_COPY;
} }
_SEH_END; _SEH_END;
/* Check for SEH status */ /* Check for SEH status */
if (Status != STATUS_SUCCESS) return Status; if (Status != STATUS_SUCCESS) return Status;
/* Detach from target */ /* Detach from target */
KeUnstackDetachProcess(&ApcState); KeUnstackDetachProcess(&ApcState);
/* Update location and size */ /* Update location and size */
RemainingSize -= CurrentSize; RemainingSize -= CurrentSize;
CurrentAddress = (PVOID)((ULONG_PTR)CurrentAddress + CurrentSize); CurrentAddress = (PVOID)((ULONG_PTR)CurrentAddress + CurrentSize);
CurrentTargetAddress = (PVOID)((ULONG_PTR)CurrentTargetAddress + CurrentSize); CurrentTargetAddress = (PVOID)((ULONG_PTR)CurrentTargetAddress + CurrentSize);
} }
/* Check if we had allocated pool */ /* Check if we had allocated pool */
if (HavePoolAddress) ExFreePool(PoolAddress); if (HavePoolAddress) ExFreePool(PoolAddress);
/* All bytes read */ /* All bytes read */
*ReturnSize = BufferSize; *ReturnSize = BufferSize;
return STATUS_SUCCESS; return STATUS_SUCCESS;