[KERNEL32] Use RtlStringCbCopyNW() to correctly fill the WIN32_FIND_DATA.cFileName and cAlternateFileName members without any overflow and with a terminating NULL character.

This commit is contained in:
Hermès Bélusca-Maïto 2018-07-22 21:32:38 +02:00
parent b003d68ca5
commit 2158f31b71
No known key found for this signature in database
GPG key ID: 3B2539C65E7B93D0
2 changed files with 20 additions and 18 deletions

View file

@ -19,7 +19,6 @@
*/ */
#include <k32.h> #include <k32.h>
#include <strsafe.h>
#define NDEBUG #define NDEBUG
#include <debug.h> #include <debug.h>

View file

@ -5,12 +5,14 @@
* PURPOSE: Find functions * PURPOSE: Find functions
* PROGRAMMERS: Ariadne (ariadne@xs4all.nl) * PROGRAMMERS: Ariadne (ariadne@xs4all.nl)
* Pierre Schweitzer (pierre.schweitzer@reactos.org) * Pierre Schweitzer (pierre.schweitzer@reactos.org)
* Hermes BELUSCA - MAITO (hermes.belusca@sfr.fr) * Hermes Belusca-Maito
*/ */
/* INCLUDES *******************************************************************/ /* INCLUDES *******************************************************************/
#include <k32.h> #include <k32.h>
#include <ntstrsafe.h>
#define NDEBUG #define NDEBUG
#include <debug.h> #include <debug.h>
DEBUG_CHANNEL(kernel32file); DEBUG_CHANNEL(kernel32file);
@ -71,7 +73,7 @@ typedef struct _FIND_FILE_DATA
/* /*
* For handling STATUS_BUFFER_OVERFLOW errors emitted by * For handling STATUS_BUFFER_OVERFLOW errors emitted by
* NtQueryDirectoryFile in the FildNextFile function. * NtQueryDirectoryFile in the FindNextFile function.
*/ */
BOOLEAN HasMoreData; BOOLEAN HasMoreData;
@ -128,9 +130,9 @@ CopyDeviceFindData(OUT LPWIN32_FIND_DATAW lpFindFileData,
/* Return the data */ /* Return the data */
RtlZeroMemory(lpFindFileData, sizeof(*lpFindFileData)); RtlZeroMemory(lpFindFileData, sizeof(*lpFindFileData));
lpFindFileData->dwFileAttributes = FILE_ATTRIBUTE_ARCHIVE; lpFindFileData->dwFileAttributes = FILE_ATTRIBUTE_ARCHIVE;
RtlCopyMemory(lpFindFileData->cFileName, RtlStringCbCopyNW(lpFindFileData->cFileName,
DeviceName, sizeof(lpFindFileData->cFileName),
Length); DeviceName, Length);
} }
_SEH2_EXCEPT(EXCEPTION_EXECUTE_HANDLER) _SEH2_EXCEPT(EXCEPTION_EXECUTE_HANDLER)
{ {
@ -175,22 +177,22 @@ do { \
if (fInfoLevelId == FindExInfoStandard) if (fInfoLevelId == FindExInfoStandard)
{ {
RtlCopyMemory(lpFindFileData->cFileName, RtlStringCbCopyNW(lpFindFileData->cFileName,
DirInfo.BothDirInfo->FileName, sizeof(lpFindFileData->cFileName),
DirInfo.BothDirInfo->FileNameLength); DirInfo.BothDirInfo->FileName,
lpFindFileData->cFileName[DirInfo.BothDirInfo->FileNameLength / sizeof(WCHAR)] = UNICODE_NULL; DirInfo.BothDirInfo->FileNameLength);
RtlCopyMemory(lpFindFileData->cAlternateFileName, RtlStringCbCopyNW(lpFindFileData->cAlternateFileName,
DirInfo.BothDirInfo->ShortName, sizeof(lpFindFileData->cAlternateFileName),
DirInfo.BothDirInfo->ShortNameLength); DirInfo.BothDirInfo->ShortName,
lpFindFileData->cAlternateFileName[DirInfo.BothDirInfo->ShortNameLength / sizeof(WCHAR)] = UNICODE_NULL; DirInfo.BothDirInfo->ShortNameLength);
} }
else if (fInfoLevelId == FindExInfoBasic) else if (fInfoLevelId == FindExInfoBasic)
{ {
RtlCopyMemory(lpFindFileData->cFileName, RtlStringCbCopyNW(lpFindFileData->cFileName,
DirInfo.FullDirInfo->FileName, sizeof(lpFindFileData->cFileName),
DirInfo.FullDirInfo->FileNameLength); DirInfo.FullDirInfo->FileName,
lpFindFileData->cFileName[DirInfo.FullDirInfo->FileNameLength / sizeof(WCHAR)] = UNICODE_NULL; DirInfo.FullDirInfo->FileNameLength);
lpFindFileData->cAlternateFileName[0] = UNICODE_NULL; lpFindFileData->cAlternateFileName[0] = UNICODE_NULL;
} }
@ -554,6 +556,7 @@ FindClose(HANDLE hFindFile)
_SEH2_YIELD(return FALSE); _SEH2_YIELD(return FALSE);
} }
_SEH2_END; _SEH2_END;
return TRUE; return TRUE;
} }