Rule Definition
Overlapped IO is used for asynchronous Win32 API.
The caller of such API initializes and passes a Overlapped structure and completion or failure of the API call is conveyed through the structure.
This means that the memory allocated for Overlapped structure should stay allocated until said IO operation is completed.
With P/Invoke, if Overlapped IO structure is allocated memory using GC, there is a good chance that this memory will be collected by GC before the IO operation is completed.
To avoid such error states, it is important that P/Invoke declarations for API using overlapped IO be made using unsafe pointer to a Overlapped structure.
This declaration conveys to the user of P/Invoke that allocations for this function should be made in an unsafe context.
Remediation
Convert any references like "NativeOverlapped overlapped" or "IntPtr overlapped" to "NativeOverlapped* overlapped" and mark the P/Invoke declaration unsafe
Violation Code Sample
List of API functions checked for incorrect P/Invoke declarations - ConnectNamedPipe, LockFileEx, ReadFile, ReadFileEx, ReadFileScatter, UnlockFileEx, WriteFile, WriteFileEx, WriteFileGather, GetQueuedCompletionStatus, PostQueuedCompletionStatus, DeviceIoControl, GetOverlappedResult, CancelIoEx, GetOverlappedResultEx, TransactNamedPipe, WaitCommEvent, ReadDirectoryChangesW, ReadDirectoryChangesExW
[ComVisible(true)]
internal class NativeMethods
{
private NativeMethods() { }
[DllImport("kernel32.dll", SetLastError = true)]
internal extern static uint ReadFile(
IntPtr hFile, IntPtr lpBuffer, int nNumberOfBytesToRead,
IntPtr lpNumberOfBytesRead, IntPtr overlapped);
[DllImport("kernel32.dll", SetLastError = true)]
[return: MarshalAs(UnmanagedType.Bool)]
internal extern static bool ReadFileEx(
IntPtr hFile, IntPtr lpBuffer, int nNumberOfBytesToRead,
NativeOverlapped overlapped, IntPtr lpCompletionRoutine);
}
Fixed Code Sample
[ComVisible(true)]
internal class UnsafeNativeMethods
{
private UnsafeNativeMethods() { }
[DllImport("kernel32.dll", SetLastError = true)]
unsafe internal extern static uint ReadFile(
IntPtr hFile, IntPtr lpBuffer, int nNumberOfBytesToRead,
IntPtr lpNumberOfBytesRead, NativeOverlapped* overlapped);
[DllImport("kernel32.dll", SetLastError = true)]
[return: MarshalAs(UnmanagedType.Bool)]
unsafe internal extern static bool ReadFileEx(
IntPtr hFile, IntPtr lpBuffer, int nNumberOfBytesToRead,
NativeOverlapped* overlapped, IntPtr lpCompletionRoutine);
}
Reference
https://docs.microsoft.com/en-us/visualstudio/code-quality/ca1415-declare-p-invokes-correctly?view=vs-2015
Related Technologies
Technical Criterion
CWE-120 - Buffer Copy without Checking Size of Input ('Classic Buffer Overflow')
About CAST Appmarq
CAST Appmarq is by far the biggest repository of data about real IT systems. It's built on thousands of analyzed applications, made of 35 different technologies, by over 300 business organizations across major verticals. It provides IT Leaders with factual key analytics to let them know if their applications are on track.