Skip to content

Commit

Permalink
Some fixes for check on buffer length.
Browse files Browse the repository at this point in the history
  • Loading branch information
Holt59 committed Jun 23, 2024
1 parent 8e1985e commit 7267310
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 5 deletions.
14 changes: 9 additions & 5 deletions src/usvfs_dll/hooks/ntdll.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1068,7 +1068,7 @@ DLLEXPORT NTSTATUS WINAPI usvfs::hook_NtQueryObject(
ObjectInformationLength, ReturnLength);
POST_REALCALL

if ((res == STATUS_SUCCESS || res == STATUS_INFO_LENGTH_MISMATCH)
if ((res == STATUS_SUCCESS || res == STATUS_BUFFER_OVERFLOW)
&& (ObjectInformationClass == ObjectNameInformation)) {
const auto trackerInfo = ntdllHandleTracker.lookup(Handle);
const auto redir = applyReroute(READ_CONTEXT(), callContext, trackerInfo);
Expand All @@ -1094,7 +1094,7 @@ DLLEXPORT NTSTATUS WINAPI usvfs::hook_NtQueryObject(

// TODO: check this...
if (ObjectInformationLength < buffer.size() * 2 + sizeof(OBJECT_NAME_INFORMATION)) {
res = STATUS_INFO_LENGTH_MISMATCH;
res = STATUS_BUFFER_OVERFLOW;

if (ReturnLength) {
*ReturnLength = buffer.size() * 2 + sizeof(OBJECT_NAME_INFORMATION);
Expand All @@ -1116,6 +1116,8 @@ DLLEXPORT NTSTATUS WINAPI usvfs::hook_NtQueryObject(
info->Name.Buffer = unicodeBuffer;
info->Name.Length = buffer.size() * 2;
info->Name.MaximumLength = unicodeBufferLength;

res = STATUS_SUCCESS;
}
}

Expand Down Expand Up @@ -1151,7 +1153,8 @@ DLLEXPORT NTSTATUS WINAPI usvfs::hook_NtQueryInformationFile(
FileInformationClass);
POST_REALCALL

if ((res == STATUS_SUCCESS || res == STATUS_INFO_LENGTH_MISMATCH) && (
if ((res == STATUS_SUCCESS || res == STATUS_BUFFER_OVERFLOW) &&
(
FileInformationClass == FileNameInformation
|| FileInformationClass == FileAllInformation
|| FileInformationClass == FileNormalizedNameInformation)) {
Expand All @@ -1174,15 +1177,16 @@ DLLEXPORT NTSTATUS WINAPI usvfs::hook_NtQueryInformationFile(

if (redir.redirected)
{
if (maxNameSize < trackerInfo.size()) {
res = STATUS_INFO_LENGTH_MISMATCH;
if (maxNameSize < trackerInfo.size() - 6) {
res = STATUS_BUFFER_OVERFLOW;
} else {
LPCWSTR filenameFixed = static_cast<LPCWSTR>(trackerInfo);
if (info->FileName[0] == L'\\') {
// strip the \??\X: prefix (X being the drive name)
filenameFixed = filenameFixed + 6;
}
SetInfoFilename(FileInformation, FileInformationClass, filenameFixed);
res = STATUS_SUCCESS;
}
}

Expand Down
14 changes: 14 additions & 0 deletions test/tvfs_test/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -424,6 +424,20 @@ TEST_F(USVFSTest, NtQueryObjectVirtualFile)
ASSERT_EQ(0, wcscmp(fileNameInfo->FileName, L"\\np.exe"));
}

// buffer of size should be too small for the original path (\Windows\notepad.exe)
// but not for \np.exe
{
char buffer[sizeof(ULONG) + 8 * 2];
IO_STATUS_BLOCK status;
const auto res = usvfs::hook_NtQueryInformationFile(
hdl, &status, buffer, sizeof(buffer), FileNormalizedNameInformation);
ASSERT_EQ(STATUS_SUCCESS, status.Status);

FILE_NAME_INFORMATION* fileNameInfo =
reinterpret_cast<FILE_NAME_INFORMATION*>(buffer);
ASSERT_EQ(0, wcscmp(fileNameInfo->FileName, L"\\np.exe"));
}

{
char buffer[2048];
const auto res = usvfs::hook_NtQueryObject(hdl, ObjectNameInformation, buffer,
Expand Down

0 comments on commit 7267310

Please sign in to comment.