ReactOS 0.4.16-dev-1520-gb558596
ZwMapViewOfSection.c File Reference
#include <kmt_test.h>
Include dependency graph for ZwMapViewOfSection.c:

Go to the source code of this file.

Macros

#define IGNORE   -99
 
#define NEW_CONTENT   "NewContent"
 
#define NEW_CONTENT_LEN   sizeof(NEW_CONTENT)
 
#define IsInvalidParamStatus(Status)
 
#define ok_invalid_parameter(Status)   ok(IsInvalidParamStatus(Status), "Invalid status code (0x%X)\n", Status)
 
#define TestMapView(SectionHandle, ProcessHandle, BaseAddress2, ZeroBits, CommitSize, SectionOffset, ViewSize2, InheritDisposition, AllocationType, Win32Protect, MapStatus, UnmapStatus)
 
#define MmTestMapView(Object, ProcessHandle, BaseAddress2, ZeroBits, CommitSize, SectionOffset, ViewSize2, InheritDisposition, AllocationType, Win32Protect, MapStatus, UnmapStatus)
 
#define CheckObject(Handle, Pointers, Handles)
 

Functions

static VOID KmtInitTestFiles (PHANDLE ReadOnlyFile, PHANDLE WriteOnlyFile, PHANDLE ExecutableFile)
 
static VOID SimpleErrorChecks (HANDLE FileHandleReadOnly, HANDLE FileHandleWriteOnly, HANDLE ExecutableImg)
 
static VOID AdvancedErrorChecks (HANDLE FileHandleReadOnly, HANDLE FileHandleWriteOnly)
 
static SIZE_T CompareFileContents (HANDLE FileHandle, ULONG BufferLength, PVOID Buffer)
 
static VOID NTAPI SystemProcessWorker (PVOID StartContext)
 
static VOID BehaviorChecks (HANDLE FileHandleReadOnly, HANDLE FileHandleWriteOnly)
 
static VOID PageFileBehaviorChecks ()
 
 START_TEST (ZwMapViewOfSection)
 

Variables

static UNICODE_STRING FileReadOnlyPath = RTL_CONSTANT_STRING(L"\\SystemRoot\\system32\\ntdll.dll")
 
static UNICODE_STRING NtosImgPath = RTL_CONSTANT_STRING(L"\\SystemRoot\\system32\\ntoskrnl.exe")
 
static UNICODE_STRING WritableFilePath = RTL_CONSTANT_STRING(L"\\SystemRoot\\kmtest-MmSection.txt")
 
static UNICODE_STRING SharedSectionName = RTL_CONSTANT_STRING(L"\\BaseNamedObjects\\kmtest-SharedSection")
 
const char TestString []
 
const ULONG TestStringSize
 
static OBJECT_ATTRIBUTES NtdllObject
 
static OBJECT_ATTRIBUTES KmtestFileObject
 
static OBJECT_ATTRIBUTES NtoskrnlFileObject
 

Macro Definition Documentation

◆ CheckObject

#define CheckObject (   Handle,
  Pointers,
  Handles 
)
Value:
do \
{ \
Status = ZwQueryObject(Handle, ObjectBasicInformation, \
&ObjectInfo, sizeof ObjectInfo, NULL); \
ok_eq_hex(Status, STATUS_SUCCESS); \
ok_eq_ulong(ObjectInfo.PointerCount, Pointers); \
ok_eq_ulong(ObjectInfo.HandleCount, Handles); \
} while (0) \
@ ObjectBasicInformation
Definition: DriverTester.h:54
#define NULL
Definition: types.h:112
ULONG Handle
Definition: gdb_input.c:15
Status
Definition: gdiplustypes.h:25
#define STATUS_SUCCESS
Definition: shellext.h:65

Definition at line 57 of file ZwMapViewOfSection.c.

◆ IGNORE

#define IGNORE   -99

Definition at line 10 of file ZwMapViewOfSection.c.

◆ IsInvalidParamStatus

#define IsInvalidParamStatus (   Status)
Value:
#define STATUS_INVALID_PARAMETER_MIX
Definition: ntstatus.h:285
#define STATUS_INVALID_PARAMETER_12
Definition: ntstatus.h:486
#define STATUS_INVALID_PARAMETER_1
Definition: ntstatus.h:475
#define STATUS_INVALID_PARAMETER
Definition: udferr_usr.h:135

Definition at line 13 of file ZwMapViewOfSection.c.

◆ MmTestMapView

#define MmTestMapView (   Object,
  ProcessHandle,
  BaseAddress2,
  ZeroBits,
  CommitSize,
  SectionOffset,
  ViewSize2,
  InheritDisposition,
  AllocationType,
  Win32Protect,
  MapStatus,
  UnmapStatus 
)
Value:
do \
{ \
ok_eq_hex(Status, MapStatus); \
{ \
if (UnmapStatus != IGNORE) ok_eq_hex(Status, UnmapStatus); \
*BaseAddress2 = NULL; \
*ViewSize2 = 0; \
} \
} while (0) \
NTSTATUS NTAPI MmUnmapViewOfSection(IN PEPROCESS Process, IN PVOID BaseAddress)
Definition: section.c:2752
#define IGNORE
#define ok_eq_hex(value, expected)
Definition: apitest.h:62
#define NT_SUCCESS(StatCode)
Definition: apphelp.c:33
_In_ HANDLE _Outptr_result_bytebuffer_ ViewSize PVOID _In_ ULONG_PTR _In_ SIZE_T _Inout_opt_ PLARGE_INTEGER _Inout_ PSIZE_T _In_ SECTION_INHERIT InheritDisposition
Definition: mmfuncs.h:409
_In_ HANDLE _Outptr_result_bytebuffer_ ViewSize PVOID _In_ ULONG_PTR _In_ SIZE_T _Inout_opt_ PLARGE_INTEGER _Inout_ PSIZE_T _In_ SECTION_INHERIT _In_ ULONG _In_ ULONG Win32Protect
Definition: mmfuncs.h:412
_In_ HANDLE ProcessHandle
Definition: mmfuncs.h:403
_In_ HANDLE _Outptr_result_bytebuffer_ ViewSize PVOID _In_ ULONG_PTR ZeroBits
Definition: mmfuncs.h:405
_In_ HANDLE _Outptr_result_bytebuffer_ ViewSize PVOID * BaseAddress
Definition: mmfuncs.h:404
_In_ HANDLE _Outptr_result_bytebuffer_ ViewSize PVOID _In_ ULONG_PTR _In_ SIZE_T _Inout_opt_ PLARGE_INTEGER _Inout_ PSIZE_T _In_ SECTION_INHERIT _In_ ULONG AllocationType
Definition: mmfuncs.h:410
_In_ HANDLE _Outptr_result_bytebuffer_ ViewSize PVOID _In_ ULONG_PTR _In_ SIZE_T _Inout_opt_ PLARGE_INTEGER SectionOffset
Definition: mmfuncs.h:407
_In_ HANDLE _Outptr_result_bytebuffer_ ViewSize PVOID _In_ ULONG_PTR _In_ SIZE_T CommitSize
Definition: mmfuncs.h:406
NTSTATUS NTAPI MmMapViewOfSection(IN PVOID SectionObject, IN PEPROCESS Process, IN OUT PVOID *BaseAddress, IN ULONG_PTR ZeroBits, IN SIZE_T CommitSize, IN OUT PLARGE_INTEGER SectionOffset OPTIONAL, IN OUT PSIZE_T ViewSize, IN SECTION_INHERIT InheritDisposition, IN ULONG AllocationType, IN ULONG Protect)
Definition: section.c:4024
_Must_inspect_result_ _In_ WDFCOLLECTION _In_ WDFOBJECT Object

Definition at line 44 of file ZwMapViewOfSection.c.

◆ NEW_CONTENT

#define NEW_CONTENT   "NewContent"

Definition at line 11 of file ZwMapViewOfSection.c.

◆ NEW_CONTENT_LEN

#define NEW_CONTENT_LEN   sizeof(NEW_CONTENT)

Definition at line 12 of file ZwMapViewOfSection.c.

◆ ok_invalid_parameter

#define ok_invalid_parameter (   Status)    ok(IsInvalidParamStatus(Status), "Invalid status code (0x%X)\n", Status)

Definition at line 16 of file ZwMapViewOfSection.c.

◆ TestMapView

#define TestMapView (   SectionHandle,
  ProcessHandle,
  BaseAddress2,
  ZeroBits,
  CommitSize,
  SectionOffset,
  ViewSize2,
  InheritDisposition,
  AllocationType,
  Win32Protect,
  MapStatus,
  UnmapStatus 
)
Value:
do \
{ \
Status = ZwMapViewOfSection(SectionHandle, ProcessHandle, BaseAddress2, ZeroBits, CommitSize, SectionOffset, ViewSize2, InheritDisposition, AllocationType, Win32Protect); \
ok_invalid_parameter(MapStatus); \
else \
ok_eq_hex(Status, MapStatus); \
{ \
Status = ZwUnmapViewOfSection(ProcessHandle, BaseAddress); \
if (UnmapStatus != IGNORE) ok_eq_hex(Status, UnmapStatus); \
*BaseAddress2 = NULL; \
*ViewSize2 = 0; \
} \
} while (0) \
#define IsInvalidParamStatus(Status)
#define GetNTVersion()
Definition: apitest.h:17
#define _WIN32_WINNT_WIN10
Definition: sdkddkver.h:32

Definition at line 28 of file ZwMapViewOfSection.c.

Function Documentation

◆ AdvancedErrorChecks()

static VOID AdvancedErrorChecks ( HANDLE  FileHandleReadOnly,
HANDLE  FileHandleWriteOnly 
)
static

Definition at line 246 of file ZwMapViewOfSection.c.

247{
250 HANDLE FileSectionHandle;
253 SIZE_T ViewSize = 0;
255
256 MaximumSize.QuadPart = TestStringSize;
257 //Used for parameters working on file-based section
258 Status = ZwCreateSection(&FileSectionHandle, SECTION_ALL_ACCESS, NULL, &MaximumSize, PAGE_READWRITE, SEC_COMMIT, FileHandleWriteOnly);
260
261 Status = ObReferenceObjectByHandle(FileSectionHandle,
263 NULL,
266 NULL);
267
269
270 //Bypassing Zw function calls mean bypassing the alignment checks which are not crucial for the branches being tested here
271
272 //test first conditional branch
273 ViewSize = -1;
274#ifdef _M_IX86
275 NTSTATUS MapStatus;
276
277 switch (GetNTVersion())
278 {
281 MapStatus = STATUS_INVALID_VIEW_SIZE;
282 break;
285 break;
286 default:
287 MapStatus = STATUS_SUCCESS;
288 break;
289 }
290
293#else
296#endif
297
298 //test second conditional branch
299 ViewSize = 1;
300 SectionOffset.QuadPart = TestStringSize;
302
304 ZwClose(FileSectionHandle);
305}
#define MmTestMapView(Object, ProcessHandle, BaseAddress2, ZeroBits, CommitSize, SectionOffset, ViewSize2, InheritDisposition, AllocationType, Win32Protect, MapStatus, UnmapStatus)
const ULONG TestStringSize
LONG NTSTATUS
Definition: precomp.h:26
_Must_inspect_result_ _Outptr_ PVOID * SectionObject
Definition: fsrtlfuncs.h:860
#define KernelMode
Definition: asm.h:38
_In_ ACCESS_MASK _In_opt_ POBJECT_ATTRIBUTES _In_opt_ PLARGE_INTEGER MaximumSize
Definition: mmfuncs.h:362
_In_ HANDLE _Outptr_result_bytebuffer_ ViewSize PVOID _In_ ULONG_PTR _In_ SIZE_T _Inout_opt_ PLARGE_INTEGER _Inout_ PSIZE_T ViewSize
Definition: mmfuncs.h:408
#define SEC_COMMIT
Definition: mmtypes.h:100
NTSYSAPI NTSTATUS NTAPI ZwClose(_In_ HANDLE Handle)
#define PAGE_READWRITE
Definition: nt_native.h:1304
#define SECTION_ALL_ACCESS
Definition: nt_native.h:1293
@ ViewUnmap
Definition: nt_native.h:1279
#define STANDARD_RIGHTS_ALL
Definition: nt_native.h:69
#define MEM_RESERVE
Definition: nt_native.h:1314
#define STATUS_INVALID_VIEW_SIZE
Definition: ntstatus.h:268
#define STATUS_CONFLICTING_ADDRESSES
Definition: ntstatus.h:261
NTSTATUS NTAPI ObReferenceObjectByHandle(IN HANDLE Handle, IN ACCESS_MASK DesiredAccess, IN POBJECT_TYPE ObjectType, IN KPROCESSOR_MODE AccessMode, OUT PVOID *Object, OUT POBJECT_HANDLE_INFORMATION HandleInformation OPTIONAL)
Definition: obref.c:494
#define _WIN32_WINNT_WINBLUE
Definition: sdkddkver.h:30
#define _WIN32_WINNT_WIN8
Definition: sdkddkver.h:29
ULONG_PTR SIZE_T
Definition: typedefs.h:80
#define ObDereferenceObject
Definition: obfuncs.h:203
#define PsGetCurrentProcess
Definition: psfuncs.h:17

Referenced by START_TEST().

◆ BehaviorChecks()

static VOID BehaviorChecks ( HANDLE  FileHandleReadOnly,
HANDLE  FileHandleWriteOnly 
)
static

Definition at line 400 of file ZwMapViewOfSection.c.

401{
405 HANDLE WriteSectionHandle;
406 HANDLE SysThreadHandle;
410 SIZE_T Match;
411 SIZE_T ViewSize = 0;
412 ULONG PtrCnt;
413
415#ifdef _M_IX86
416 PtrCnt = 34;
417#else
418 PtrCnt = 32770;
419#endif
420 else
421 PtrCnt = 3;
422
424 MaximumSize.QuadPart = TestStringSize;
425 SectionOffset.QuadPart = 0;
426
427 Status = ZwCreateSection(&WriteSectionHandle, SECTION_ALL_ACCESS, &ObjectAttributes, &MaximumSize, PAGE_READWRITE, SEC_COMMIT, FileHandleWriteOnly);
428 CheckObject(WriteSectionHandle, PtrCnt, 1);
430 PtrCnt -= 2;
431 ok(NT_SUCCESS(Status), "Error creating write section from file. Error = %p\n", Status);
432
433 //check for section reading/writing by comparing section content to a well-known value.
434 Status = ZwMapViewOfSection(WriteSectionHandle, NtCurrentProcess() ,&BaseAddress, 0, 0, &SectionOffset, &ViewSize, ViewUnmap, 0, PAGE_READWRITE);
435 CheckObject(WriteSectionHandle, PtrCnt, 1);
436 if (!skip(NT_SUCCESS(Status), "Error mapping view with READ/WRITE priv. Error = %p\n", Status))
437 {
440
441 //now check writing to section
443
446
447 //check to see if the contents have been flushed to the actual file on disk.
448 Match = CompareFileContents(FileHandleWriteOnly, NEW_CONTENT_LEN, NEW_CONTENT);
450
451 //bring everything back to normal
453
454 //Initiate an external thread to modify the file
457 if (!skip(NT_SUCCESS(Status), "Error creating System thread. Error = %p\n", Status))
458 {
460 if (!skip(NT_SUCCESS(Status), "Error getting reference to System thread when testing file-backed section\n"))
461 {
462 //wait until the system thread actually terminates
464
465 //no longer need the thread object
467
468 //test for bi-directional access to the shared page file
471
472 //bring everything back to normal, again
474 }
475 }
476
477 ZwUnmapViewOfSection(NtCurrentProcess(), BaseAddress);
478 }
479
480 //Try to write to read-only mapped view
482 ViewSize = 0;
483 SectionOffset.QuadPart = 0;
484 Status = ZwMapViewOfSection(WriteSectionHandle, NtCurrentProcess(), &BaseAddress, 0, 0, &SectionOffset, &ViewSize, ViewUnmap, 0, PAGE_READONLY);
485 if (!skip(NT_SUCCESS(Status), "Error mapping view with READ priv. Error = %p\n", Status))
486 {
489
493
494 ZwUnmapViewOfSection(NtCurrentProcess(), BaseAddress);
495 }
496
497 //try to access forbidden memory
499 ViewSize = 0;
501 Status = ZwMapViewOfSection(WriteSectionHandle, NtCurrentProcess(), &BaseAddress, 0, 0, &SectionOffset, &ViewSize, ViewUnmap, 0, PAGE_NOACCESS);
503 {
507
508 ZwUnmapViewOfSection(NtCurrentProcess(), BaseAddress);
509 }
510
511 //try to access guarded memory
513 ViewSize = 0;
515 Status = ZwMapViewOfSection(WriteSectionHandle, NtCurrentProcess(), &BaseAddress, 0, 0, &SectionOffset, &ViewSize, ViewUnmap, 0, PAGE_GUARD | PAGE_READWRITE);
517 {
521
525
526 ZwUnmapViewOfSection(NtCurrentProcess(), BaseAddress);
527 }
528
529 ZwClose(WriteSectionHandle);
530
531 //section created with sec_reserve should not be commited.
533 ViewSize = 0;
535 Status = ZwCreateSection(&WriteSectionHandle, SECTION_ALL_ACCESS, &ObjectAttributes, &MaximumSize, PAGE_READWRITE, SEC_RESERVE, FileHandleWriteOnly);
536 if (!skip(NT_SUCCESS(Status), "Error creating page file section. Error = %p\n", Status))
537 {
538 Status = ZwMapViewOfSection(WriteSectionHandle, NtCurrentProcess(), &BaseAddress, 0, TestStringSize, &SectionOffset, &ViewSize, ViewUnmap, MEM_RESERVE, PAGE_READWRITE);
539 if (!skip(NT_SUCCESS(Status), "Error mapping page file view. Error = %p\n", Status))
540 {
541 //check also the SEC_COMMIT flag
542 /* This test proves that MSDN is once again wrong
543 * https://learn.microsoft.com/en-us/windows/win32/api/memoryapi/nf-memoryapi-createfilemappingw states that SEC_RESERVE
544 * should cause the allocated memory for the view to be reserved but in fact it is always committed.
545 * It fails also on windows.
546 */
548 ZwUnmapViewOfSection(NtCurrentProcess(), BaseAddress);
549 }
550
551 ZwClose(WriteSectionHandle);
552 }
553}
static VOID NTAPI SystemProcessWorker(PVOID StartContext)
static SIZE_T CompareFileContents(HANDLE FileHandle, ULONG BufferLength, PVOID Buffer)
static UNICODE_STRING SharedSectionName
#define CheckObject(Handle, Pointers, Handles)
#define NEW_CONTENT_LEN
#define NEW_CONTENT
#define ok_eq_size(value, expected)
Definition: apitest.h:54
#define ok(value,...)
Definition: atltest.h:57
#define skip(...)
Definition: atltest.h:64
BOOL Error
Definition: chkdsk.c:66
IN PUNICODE_STRING IN POBJECT_ATTRIBUTES ObjectAttributes
Definition: conport.c:36
#define FALSE
Definition: types.h:117
#define PAGE_READONLY
Definition: compat.h:138
#define KeWaitForSingleObject(pEvt, foo, a, b, c)
Definition: env_spec_w32.h:478
#define RtlCompareMemory(s1, s2, l)
Definition: env_spec_w32.h:465
#define PAGE_SIZE
Definition: env_spec_w32.h:49
GLdouble n
Definition: glext.h:7729
GLenum GLenum GLenum GLenum mapping
Definition: glext.h:9031
GLfloat GLfloat p
Definition: glext.h:8902
#define OBJ_KERNEL_HANDLE
Definition: winternl.h:231
#define OBJ_CASE_INSENSITIVE
Definition: winternl.h:228
#define KmtStartSeh()
Definition: kmt_test.h:285
#define Test_NtQueryVirtualMemory(BaseAddress, Size, AllocationType, ProtectionType)
Definition: kmt_test.h:74
#define KmtEndSeh(ExpectedStatus)
Definition: kmt_test.h:291
if(dx< 0)
Definition: linetemp.h:194
#define InitializeObjectAttributes(p, n, a, r, s)
Definition: reg.c:106
@ ThreadObject
Definition: ketypes.h:412
#define THREAD_ALL_ACCESS
Definition: nt_native.h:1339
#define SEC_RESERVE
Definition: nt_native.h:1323
#define NtCurrentProcess()
Definition: nt_native.h:1657
#define MEM_COMMIT
Definition: nt_native.h:1313
#define PAGE_NOACCESS
Definition: nt_native.h:1302
#define PAGE_GUARD
Definition: nt_native.h:1310
POBJECT_TYPE PsThreadType
Definition: thread.c:20
NTSTATUS NTAPI PsCreateSystemThread(OUT PHANDLE ThreadHandle, IN ACCESS_MASK DesiredAccess, IN POBJECT_ATTRIBUTES ObjectAttributes, IN HANDLE ProcessHandle, IN PCLIENT_ID ClientId, IN PKSTART_ROUTINE StartRoutine, IN PVOID StartContext)
Definition: thread.c:602
#define STATUS_ACCESS_VIOLATION
Definition: ntstatus.h:242
#define STATUS_GUARD_PAGE_VIOLATION
Definition: ntstatus.h:182
Definition: fci.c:127
Definition: module.h:576
Definition: parser.c:56
EH_STD::basic_string< char, EH_STD::char_traits< char >, eh_allocator(char) > TestString
Definition: test_string.cpp:30
#define RtlCopyMemory(Destination, Source, Length)
Definition: typedefs.h:263
uint32_t ULONG
Definition: typedefs.h:59
@ Executive
Definition: ketypes.h:415
ret QuadPart
Definition: rtlfuncs.h:3106

Referenced by START_TEST().

◆ CompareFileContents()

static SIZE_T CompareFileContents ( HANDLE  FileHandle,
ULONG  BufferLength,
PVOID  Buffer 
)
static

Definition at line 309 of file ZwMapViewOfSection.c.

310{
314 PVOID FileContent;
315 SIZE_T Match;
316
317 Match = 0;
318 ByteOffset.QuadPart = 0;
319
320 FileContent = ExAllocatePoolWithTag(PagedPool, BufferLength, 'Test');
321 if (!skip((FileContent != NULL), "Error allocating memory for FileContent\n"))
322 {
323 Status = ZwReadFile(FileHandle, NULL, NULL, NULL, &IoStatusBlock, FileContent, BufferLength, &ByteOffset, NULL);
324 ok(Status == STATUS_SUCCESS || Status == STATUS_PENDING, "Unexpected status (0x%X).\n", Status);
326
327 Match = 0;
328 Match = RtlCompareMemory(FileContent, Buffer, BufferLength);
329 ExFreePoolWithTag(FileContent, 'Test');
330 }
331
332 return Match;
333}
#define ok_eq_ulongptr(value, expected)
Definition: apitest.h:56
Definition: bufpool.h:45
#define STATUS_PENDING
Definition: d3dkmdt.h:43
#define ExAllocatePoolWithTag(hernya, size, tag)
Definition: env_spec_w32.h:350
#define PagedPool
Definition: env_spec_w32.h:308
IN PDCB IN PCCB IN VBO IN OUT PULONG OUT PDIRENT OUT PBCB OUT PVBO ByteOffset
Definition: fatprocs.h:732
_Must_inspect_result_ _In_opt_ PFLT_INSTANCE _Out_ PHANDLE FileHandle
Definition: fltkernel.h:1231
#define ExFreePoolWithTag(_P, _T)
Definition: module.h:1109
static OUT PIO_STATUS_BLOCK IoStatusBlock
Definition: pipe.c:75
_Must_inspect_result_ _In_ WDFDEVICE _In_ DEVICE_REGISTRY_PROPERTY _In_ ULONG BufferLength
Definition: wdfdevice.h:3771

Referenced by BehaviorChecks().

◆ KmtInitTestFiles()

static VOID KmtInitTestFiles ( PHANDLE  ReadOnlyFile,
PHANDLE  WriteOnlyFile,
PHANDLE  ExecutableFile 
)
static

Definition at line 69 of file ZwMapViewOfSection.c.

70{
74
75 //INIT THE READ-ONLY FILE
78 ok(*ReadOnlyFile != NULL, "Couldn't acquire READONLY handle\n");
79
80 //INIT THE EXECUTABLE FILE
83 ok(*ExecutableFile != NULL, "Couldn't acquire EXECUTE handle\n");
84
85 //INIT THE WRITE-ONLY FILE
86 //TODO: Delete the file when the tests are all executed
90 ok(*WriteOnlyFile != NULL, "WriteOnlyFile is NULL\n");
91 if (!skip(*WriteOnlyFile != NULL, "No WriteOnlyFile\n"))
92 {
93 FileOffset.QuadPart = 0;
94 Status = ZwWriteFile(*WriteOnlyFile, NULL, NULL, NULL, &IoStatusBlock, (PVOID)TestString, TestStringSize, &FileOffset, NULL);
95 ok(Status == STATUS_SUCCESS || Status == STATUS_PENDING, "Status = 0x%08lx\n", Status);
96 Status = ZwWaitForSingleObject(*WriteOnlyFile, FALSE, NULL);
99 }
100}
static OBJECT_ATTRIBUTES KmtestFileObject
static OBJECT_ATTRIBUTES NtdllObject
#define FILE_NON_DIRECTORY_FILE
Definition: constants.h:492
#define FILE_DELETE_ON_CLOSE
Definition: constants.h:494
_In_ PFCB _In_ LONGLONG FileOffset
Definition: cdprocs.h:160
#define GENERIC_READ
Definition: compat.h:135
#define FILE_ATTRIBUTE_NORMAL
Definition: compat.h:137
#define FILE_SHARE_READ
Definition: compat.h:136
#define FILE_OPEN
Definition: from_kernel.h:54
#define FILE_SUPERSEDE
Definition: from_kernel.h:53
#define FILE_SHARE_WRITE
Definition: nt_native.h:681
#define SYNCHRONIZE
Definition: nt_native.h:61
#define FILE_CREATED
Definition: nt_native.h:770
#define GENERIC_WRITE
Definition: nt_native.h:90
#define GENERIC_EXECUTE
Definition: nt_native.h:91

Referenced by START_TEST().

◆ PageFileBehaviorChecks()

static VOID PageFileBehaviorChecks ( )
static

Definition at line 558 of file ZwMapViewOfSection.c.

559{
561 LARGE_INTEGER MaxSectionSize;
563 HANDLE PageFileSectionHandle;
566 SIZE_T Match;
569 ULONG PtrCnt;
570
572#ifdef _M_IX86
573 PtrCnt = 34;
574#else
575 PtrCnt = 32770;
576#endif
577 else
578 PtrCnt = 3;
579
580 MaxSectionSize.QuadPart = TestStringSize;
581 SectionOffset.QuadPart = 0;
582 PageFileSectionHandle = INVALID_HANDLE_VALUE;
586
587 //test memory sharing between 2 different processes
588 Status = ZwCreateSection(&PageFileSectionHandle, SECTION_ALL_ACCESS, &ObjectAttributes, &MaxSectionSize, PAGE_READWRITE, SEC_COMMIT, NULL);
589 if (!skip(NT_SUCCESS(Status), "Error creating page file section. Error = %p\n", Status))
590 {
591 CheckObject(PageFileSectionHandle, PtrCnt, 1);
592 Status = ZwMapViewOfSection(PageFileSectionHandle, NtCurrentProcess(), &BaseAddress, 0, TestStringSize, &SectionOffset, &ViewSize, ViewUnmap, 0, PAGE_READWRITE);
594 PtrCnt -= 2;
595 if (!skip(NT_SUCCESS(Status), "Error mapping page file view. Error = %p\n", Status))
596 {
597 HANDLE SysThreadHandle;
598
599 CheckObject(PageFileSectionHandle, PtrCnt, 1);
600
601 //check also the SEC_COMMIT flag
603
605
608
609 if (!skip(NT_SUCCESS(Status), "Error creating System thread. Error = %p\n", Status))
610 {
612 if (!skip(NT_SUCCESS(Status), "Error getting reference to System thread when testing pagefile-backed section\n"))
613 {
614 //wait until the system thread actually terminates
616
617 //no longer need the thread object
619
620 //test for bi-directional access to the shared page file
623 }
624 }
625 ZwUnmapViewOfSection(NtCurrentProcess(), BaseAddress);
626 }
627 ZwClose(PageFileSectionHandle);
628 }
629}
#define INVALID_HANDLE_VALUE
Definition: compat.h:731
LONGLONG QuadPart
Definition: typedefs.h:114

Referenced by START_TEST().

◆ SimpleErrorChecks()

static VOID SimpleErrorChecks ( HANDLE  FileHandleReadOnly,
HANDLE  FileHandleWriteOnly,
HANDLE  ExecutableImg 
)
static

Definition at line 104 of file ZwMapViewOfSection.c.

105{
107 HANDLE WriteSectionHandle;
108 HANDLE ReadOnlySection;
109 HANDLE PageFileSectionHandle;
112 SIZE_T AllocSize = TestStringSize;
113 SIZE_T ViewSize = 0;
115 PVOID AllocBase = NULL;
116 MaximumSize.QuadPart = TestStringSize;
117
118 //Used for parameters working on file-based section
119 Status = ZwCreateSection(&WriteSectionHandle, SECTION_ALL_ACCESS, NULL, &MaximumSize, PAGE_READWRITE, SEC_COMMIT, FileHandleWriteOnly);
121
122 Status = ZwCreateSection(&ReadOnlySection, SECTION_ALL_ACCESS, NULL, &MaximumSize, PAGE_READONLY, SEC_COMMIT, FileHandleReadOnly);
124
125 //Used for parameters taking effect only on page-file backed section
127 Status = ZwCreateSection(&PageFileSectionHandle, SECTION_ALL_ACCESS, NULL, &MaximumSize, PAGE_READWRITE, SEC_COMMIT, NULL);
129
130 MaximumSize.QuadPart = TestStringSize;
131
132 //section handle
137
138 //process handle
139 TestMapView(WriteSectionHandle, (HANDLE)(ULONG_PTR)0xDEADBEEFDEADBEEFull, &BaseAddress, 0, 0, NULL, &ViewSize, ViewUnmap, 0, PAGE_READWRITE, STATUS_INVALID_HANDLE, IGNORE);
141
142 //base address
143 BaseAddress = (PVOID)(ULONG_PTR)0x00567A20;
145
146 BaseAddress = (PVOID)(ULONG_PTR)0x60000000;
148
149 BaseAddress = (PVOID)((char *)MmSystemRangeStart + 200);
151
152 //invalid section handle AND unaligned base address
153 BaseAddress = (PVOID)(ULONG_PTR)0x00567A20;
155
156 //invalid process handle AND unaligned base address
157 BaseAddress = (PVOID)(ULONG_PTR)0x00567A20;
158 TestMapView(WriteSectionHandle, (HANDLE)(ULONG_PTR)0xDEADBEEFDEADBEEFull, &BaseAddress, 0, 0, NULL, &ViewSize, ViewUnmap, 0, PAGE_READWRITE, STATUS_INVALID_HANDLE, IGNORE);
159
160 //try mapping section to an already mapped address
161 Status = ZwAllocateVirtualMemory(NtCurrentProcess(), &AllocBase, 0, &AllocSize, MEM_COMMIT, PAGE_READWRITE);
162 if (!skip(NT_SUCCESS(Status), "Cannot allocate memory\n"))
163 {
164 BaseAddress = AllocBase;
166 Status = ZwFreeVirtualMemory(NtCurrentProcess(), &AllocBase, &AllocSize, MEM_RELEASE);
168 }
169
170 //zero bits
171#ifdef _M_IX86
175#else
179#endif
183
184 //commit size
191
192 //section offset
193 SectionOffset.QuadPart = 0;
195 ok_eq_ulonglong(SectionOffset.QuadPart, 0);
196
197 SectionOffset.QuadPart = 0x00040211; //MSDN is wrong, in w2k3 the ZwMapViewOfSection doesn't align offsets automatically
199
200 SectionOffset.QuadPart = -1;
202
203 //View Size
205
206 ViewSize = -1;
208
212
215
218
219 //allocation type
224
225 //win32protect
237
238 ZwClose(WriteSectionHandle);
239 ZwClose(PageFileSectionHandle);
240 ZwClose(ReadOnlySection);
241}
#define TestMapView(SectionHandle, ProcessHandle, BaseAddress2, ZeroBits, CommitSize, SectionOffset, ViewSize2, InheritDisposition, AllocationType, Win32Protect, MapStatus, UnmapStatus)
#define ok_eq_ulonglong(value, expected)
Definition: apitest.h:50
#define STATUS_INVALID_HANDLE
Definition: d3dkmdt.h:40
#define STATUS_NO_MEMORY
Definition: d3dkmdt.h:51
#define STATUS_OBJECT_TYPE_MISMATCH
Definition: d3dkmdt.h:46
#define MM_ALLOCATION_GRANULARITY
Definition: mmtypes.h:36
#define PAGE_WRITECOPY
Definition: nt_native.h:1305
#define PAGE_EXECUTE_READ
Definition: nt_native.h:1307
#define PAGE_EXECUTE
Definition: nt_native.h:1306
#define PAGE_EXECUTE_WRITECOPY
Definition: nt_native.h:1309
#define MEM_LARGE_PAGES
Definition: nt_native.h:1322
#define MEM_RELEASE
Definition: nt_native.h:1316
#define MmSystemRangeStart
Definition: mm.h:32
#define STATUS_MAPPED_ALIGNMENT
Definition: ntstatus.h:676
#define STATUS_SECTION_PROTECTION
Definition: ntstatus.h:314
#define STATUS_INVALID_PARAMETER_9
Definition: ntstatus.h:483
#define STATUS_INVALID_PARAMETER_4
Definition: ntstatus.h:478
#define STATUS_INVALID_PAGE_PROTECTION
Definition: ntstatus.h:305
#define STATUS_INVALID_PARAMETER_3
Definition: ntstatus.h:477
#define STATUS_INVALID_PARAMETER_5
Definition: ntstatus.h:479
void * PVOID
Definition: typedefs.h:50
uint32_t ULONG_PTR
Definition: typedefs.h:65

Referenced by START_TEST().

◆ START_TEST()

START_TEST ( ZwMapViewOfSection  )

Definition at line 632 of file ZwMapViewOfSection.c.

633{
634 HANDLE FileHandleReadOnly = NULL;
635 HANDLE FileHandleWriteOnly = NULL;
636 HANDLE ExecutableFileHandle = NULL;
637
641
642 KmtInitTestFiles(&FileHandleReadOnly, &FileHandleWriteOnly, &ExecutableFileHandle);
643
644 SimpleErrorChecks(FileHandleReadOnly, FileHandleWriteOnly, ExecutableFileHandle);
645 AdvancedErrorChecks(FileHandleReadOnly, FileHandleWriteOnly);
646 BehaviorChecks(FileHandleReadOnly, FileHandleWriteOnly);
648
649 if (FileHandleReadOnly)
650 ZwClose(FileHandleReadOnly);
651
652 if (FileHandleWriteOnly)
653 ZwClose(FileHandleWriteOnly);
654
655 if (ExecutableFileHandle)
656 ZwClose(ExecutableFileHandle);
657}
static OBJECT_ATTRIBUTES NtoskrnlFileObject
static VOID KmtInitTestFiles(PHANDLE ReadOnlyFile, PHANDLE WriteOnlyFile, PHANDLE ExecutableFile)
static UNICODE_STRING FileReadOnlyPath
static VOID AdvancedErrorChecks(HANDLE FileHandleReadOnly, HANDLE FileHandleWriteOnly)
static VOID SimpleErrorChecks(HANDLE FileHandleReadOnly, HANDLE FileHandleWriteOnly, HANDLE ExecutableImg)
static UNICODE_STRING WritableFilePath
static VOID BehaviorChecks(HANDLE FileHandleReadOnly, HANDLE FileHandleWriteOnly)
static VOID PageFileBehaviorChecks()
static UNICODE_STRING NtosImgPath

◆ SystemProcessWorker()

static VOID NTAPI SystemProcessWorker ( PVOID  StartContext)
static

Definition at line 339 of file ZwMapViewOfSection.c.

340{
343 HANDLE SectionHandle;
345 SIZE_T Match;
348 ULONG PtrCnt;
349
350 UNREFERENCED_PARAMETER(StartContext);
351
353#ifdef _M_IX86
354 PtrCnt = 64;
355#else
356 PtrCnt = 65536;
357#endif
358 else
359 PtrCnt = 4;
360
363 SectionOffset.QuadPart = 0;
364
367 if (!skip(NT_SUCCESS(Status), "Error acquiring handle to section. Error = %p\n", Status))
368 {
369 CheckObject(SectionHandle, PtrCnt, 2);
370 Status = ZwMapViewOfSection(SectionHandle, NtCurrentProcess(), &BaseAddress, 0, TestStringSize, &SectionOffset, &ViewSize, ViewUnmap, 0, PAGE_READWRITE);
372 PtrCnt -= 2;
373
374 //make sure ZwMapViewofSection doesn't touch the section ref counts.
375 CheckObject(SectionHandle, PtrCnt, 2);
377 PtrCnt--;
378
379 if (!skip(NT_SUCCESS(Status), "Error mapping page file view in system process. Error = %p\n", Status))
380 {
383
385 ZwUnmapViewOfSection(NtCurrentProcess(), BaseAddress);
386
387 //make sure ZwMapViewofSection doesn't touch the section ref counts.
388 CheckObject(SectionHandle, PtrCnt, 2);
389 }
390
391 ZwClose(SectionHandle);
392 }
393
395}
NTSYSAPI NTSTATUS NTAPI ZwOpenSection(_Out_ PHANDLE SectionHandle, _In_ ACCESS_MASK DesiredAccess, _In_ POBJECT_ATTRIBUTES ObjectAttributes)
#define UNREFERENCED_PARAMETER(P)
Definition: ntbasedef.h:329
NTSTATUS NTAPI PsTerminateSystemThread(IN NTSTATUS ExitStatus)
Definition: kill.c:1145

Referenced by BehaviorChecks(), and PageFileBehaviorChecks().

Variable Documentation

◆ FileReadOnlyPath

UNICODE_STRING FileReadOnlyPath = RTL_CONSTANT_STRING(L"\\SystemRoot\\system32\\ntdll.dll")
static

Definition at line 18 of file ZwMapViewOfSection.c.

Referenced by START_TEST().

◆ KmtestFileObject

OBJECT_ATTRIBUTES KmtestFileObject
static

Definition at line 25 of file ZwMapViewOfSection.c.

Referenced by KmtInitTestFiles(), and START_TEST().

◆ NtdllObject

OBJECT_ATTRIBUTES NtdllObject
static

Definition at line 24 of file ZwMapViewOfSection.c.

Referenced by KmtInitTestFiles(), and START_TEST().

◆ NtosImgPath

UNICODE_STRING NtosImgPath = RTL_CONSTANT_STRING(L"\\SystemRoot\\system32\\ntoskrnl.exe")
static

Definition at line 19 of file ZwMapViewOfSection.c.

Referenced by START_TEST().

◆ NtoskrnlFileObject

OBJECT_ATTRIBUTES NtoskrnlFileObject
static

Definition at line 26 of file ZwMapViewOfSection.c.

Referenced by START_TEST().

◆ SharedSectionName

UNICODE_STRING SharedSectionName = RTL_CONSTANT_STRING(L"\\BaseNamedObjects\\kmtest-SharedSection")
static

◆ TestString

const char TestString[]
extern

Definition at line 16 of file ZwAllocateVirtualMemory.c.

◆ TestStringSize

◆ WritableFilePath

UNICODE_STRING WritableFilePath = RTL_CONSTANT_STRING(L"\\SystemRoot\\kmtest-MmSection.txt")
static

Definition at line 20 of file ZwMapViewOfSection.c.

Referenced by START_TEST().